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 | // |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 12 | // Constant expression evaluation produces four main results: |
| 13 | // |
| 14 | // * A success/failure flag indicating whether constant folding was successful. |
| 15 | // This is the 'bool' return value used by most of the code in this file. A |
| 16 | // 'false' return value indicates that constant folding has failed, and any |
| 17 | // appropriate diagnostic has already been produced. |
| 18 | // |
| 19 | // * An evaluated result, valid only if constant folding has not failed. |
| 20 | // |
| 21 | // * A flag indicating if evaluation encountered (unevaluated) side-effects. |
| 22 | // These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1), |
| 23 | // where it is possible to determine the evaluated result regardless. |
| 24 | // |
| 25 | // * A set of notes indicating why the evaluation was not a constant expression |
| 26 | // (under the C++11 rules only, at the moment), or, if folding failed too, |
| 27 | // why the expression could not be folded. |
| 28 | // |
| 29 | // If we are checking for a potential constant expression, failure to constant |
| 30 | // fold a potential constant sub-expression will be indicated by a 'false' |
| 31 | // return value (the expression could not be folded) and no diagnostic (the |
| 32 | // expression is not necessarily non-constant). |
| 33 | // |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "clang/AST/APValue.h" |
| 37 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 199c3d6 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 38 | #include "clang/AST/CharUnits.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 39 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 0fe52e1 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 40 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 41 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 42 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 43 | #include "clang/AST/Expr.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 44 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 45 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | 7462b39 | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallString.h" |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 47 | #include <cstring> |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 48 | #include <functional> |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 49 | |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 50 | using namespace clang; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 51 | using llvm::APSInt; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 52 | using llvm::APFloat; |
Anders Carlsson | c44eec6 | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 54 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 55 | /// information about a subexpression as it is folded. It retains information |
| 56 | /// about the AST context, but also maintains information about the folded |
| 57 | /// expression. |
| 58 | /// |
| 59 | /// If an expression could be evaluated, it is still possible it is not a C |
| 60 | /// "integer constant expression" or constant expression. If not, this struct |
| 61 | /// captures information about how and why not. |
| 62 | /// |
| 63 | /// One bit of information passed *into* the request for constant folding |
| 64 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 65 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 66 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 67 | /// certain things in certain situations. |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 68 | namespace { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 69 | struct LValue; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 70 | struct CallStackFrame; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 71 | struct EvalInfo; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 72 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 73 | QualType getType(APValue::LValueBase B) { |
| 74 | if (!B) return QualType(); |
| 75 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) |
| 76 | return D->getType(); |
| 77 | return B.get<const Expr*>()->getType(); |
| 78 | } |
| 79 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 80 | /// Get an LValue path entry, which is known to not be an array index, as a |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 81 | /// field or base class. |
| 82 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 83 | APValue::BaseOrMemberType Value; |
| 84 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 85 | return Value; |
| 86 | } |
| 87 | |
| 88 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 89 | /// field declaration. |
| 90 | const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
| 91 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 92 | } |
| 93 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 94 | /// base class declaration. |
| 95 | const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 96 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 97 | } |
| 98 | /// Determine whether this LValue path entry for a base class names a virtual |
| 99 | /// base class. |
| 100 | bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 101 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 104 | /// Find the path length and type of the most-derived subobject in the given |
| 105 | /// path, and find the size of the containing array, if any. |
| 106 | static |
| 107 | unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base, |
| 108 | ArrayRef<APValue::LValuePathEntry> Path, |
| 109 | uint64_t &ArraySize, QualType &Type) { |
| 110 | unsigned MostDerivedLength = 0; |
| 111 | Type = Base; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 112 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 113 | if (Type->isArrayType()) { |
| 114 | const ConstantArrayType *CAT = |
| 115 | cast<ConstantArrayType>(Ctx.getAsArrayType(Type)); |
| 116 | Type = CAT->getElementType(); |
| 117 | ArraySize = CAT->getSize().getZExtValue(); |
| 118 | MostDerivedLength = I + 1; |
| 119 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 120 | Type = FD->getType(); |
| 121 | ArraySize = 0; |
| 122 | MostDerivedLength = I + 1; |
| 123 | } else { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 124 | // Path[I] describes a base class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 125 | ArraySize = 0; |
| 126 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 127 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 128 | return MostDerivedLength; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 131 | // The order of this enum is important for diagnostics. |
| 132 | enum CheckSubobjectKind { |
Richard Smith | b04035a | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 133 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
| 134 | CSK_This |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 137 | /// A path from a glvalue to a subobject of that glvalue. |
| 138 | struct SubobjectDesignator { |
| 139 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 140 | /// lvalues can still be folded, but they are not core constant expressions |
| 141 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 142 | bool Invalid : 1; |
| 143 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 144 | /// Is this a pointer one past the end of an object? |
| 145 | bool IsOnePastTheEnd : 1; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 146 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 147 | /// The length of the path to the most-derived object of which this is a |
| 148 | /// subobject. |
| 149 | unsigned MostDerivedPathLength : 30; |
| 150 | |
| 151 | /// The size of the array of which the most-derived object is an element, or |
| 152 | /// 0 if the most-derived object is not an array element. |
| 153 | uint64_t MostDerivedArraySize; |
| 154 | |
| 155 | /// The type of the most derived object referred to by this address. |
| 156 | QualType MostDerivedType; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 157 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 158 | typedef APValue::LValuePathEntry PathEntry; |
| 159 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 160 | /// The entries on the path from the glvalue to the designated subobject. |
| 161 | SmallVector<PathEntry, 8> Entries; |
| 162 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 163 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 164 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 165 | explicit SubobjectDesignator(QualType T) |
| 166 | : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0), |
| 167 | MostDerivedArraySize(0), MostDerivedType(T) {} |
| 168 | |
| 169 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
| 170 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
| 171 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 172 | if (!Invalid) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 173 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 174 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 175 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 176 | if (V.getLValueBase()) |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 177 | MostDerivedPathLength = |
| 178 | findMostDerivedSubobject(Ctx, getType(V.getLValueBase()), |
| 179 | V.getLValuePath(), MostDerivedArraySize, |
| 180 | MostDerivedType); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 184 | void setInvalid() { |
| 185 | Invalid = true; |
| 186 | Entries.clear(); |
| 187 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 188 | |
| 189 | /// Determine whether this is a one-past-the-end pointer. |
| 190 | bool isOnePastTheEnd() const { |
| 191 | if (IsOnePastTheEnd) |
| 192 | return true; |
| 193 | if (MostDerivedArraySize && |
| 194 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
| 195 | return true; |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | /// Check that this refers to a valid subobject. |
| 200 | bool isValidSubobject() const { |
| 201 | if (Invalid) |
| 202 | return false; |
| 203 | return !isOnePastTheEnd(); |
| 204 | } |
| 205 | /// Check that this refers to a valid subobject, and if not, produce a |
| 206 | /// relevant diagnostic and set the designator as invalid. |
| 207 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 208 | |
| 209 | /// Update this designator to refer to the first element within this array. |
| 210 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 211 | PathEntry Entry; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 212 | Entry.ArrayIndex = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 213 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 214 | |
| 215 | // This is a most-derived object. |
| 216 | MostDerivedType = CAT->getElementType(); |
| 217 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 218 | MostDerivedPathLength = Entries.size(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 219 | } |
| 220 | /// Update this designator to refer to the given base or member of this |
| 221 | /// object. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 222 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 223 | PathEntry Entry; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 224 | APValue::BaseOrMemberType Value(D, Virtual); |
| 225 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 226 | Entries.push_back(Entry); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 227 | |
| 228 | // If this isn't a base class, it's a new most-derived object. |
| 229 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 230 | MostDerivedType = FD->getType(); |
| 231 | MostDerivedArraySize = 0; |
| 232 | MostDerivedPathLength = Entries.size(); |
| 233 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 234 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 235 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 236 | /// Add N to the address of this subobject. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 237 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 238 | if (Invalid) return; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 239 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 240 | Entries.back().ArrayIndex += N; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 241 | if (Entries.back().ArrayIndex > MostDerivedArraySize) { |
| 242 | diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex); |
| 243 | setInvalid(); |
| 244 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 245 | return; |
| 246 | } |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 247 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 248 | // nonarray object behaves the same as a pointer to the first element of |
| 249 | // an array of length one with the type of the object as its element type. |
| 250 | if (IsOnePastTheEnd && N == (uint64_t)-1) |
| 251 | IsOnePastTheEnd = false; |
| 252 | else if (!IsOnePastTheEnd && N == 1) |
| 253 | IsOnePastTheEnd = true; |
| 254 | else if (N != 0) { |
| 255 | diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 256 | setInvalid(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 257 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 258 | } |
| 259 | }; |
| 260 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 261 | /// A core constant value. This can be the value of any constant expression, |
| 262 | /// 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] | 263 | /// |
| 264 | /// For an LValue, the base and offset are stored in the APValue subobject, |
| 265 | /// but the other information is stored in the SubobjectDesignator. For all |
| 266 | /// other value kinds, the value is stored directly in the APValue subobject. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 267 | class CCValue : public APValue { |
| 268 | typedef llvm::APSInt APSInt; |
| 269 | typedef llvm::APFloat APFloat; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 270 | /// If the value is a reference or pointer into a parameter or temporary, |
| 271 | /// this is the corresponding call stack frame. |
| 272 | CallStackFrame *CallFrame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 273 | /// If the value is a reference or pointer, this is a description of how the |
| 274 | /// subobject was specified. |
| 275 | SubobjectDesignator Designator; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 276 | public: |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 277 | struct GlobalValue {}; |
| 278 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 279 | CCValue() {} |
| 280 | explicit CCValue(const APSInt &I) : APValue(I) {} |
| 281 | explicit CCValue(const APFloat &F) : APValue(F) {} |
| 282 | CCValue(const APValue *E, unsigned N) : APValue(E, N) {} |
| 283 | CCValue(const APSInt &R, const APSInt &I) : APValue(R, I) {} |
| 284 | CCValue(const APFloat &R, const APFloat &I) : APValue(R, I) {} |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 285 | CCValue(const CCValue &V) : APValue(V), CallFrame(V.CallFrame) {} |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 286 | CCValue(LValueBase B, const CharUnits &O, CallStackFrame *F, |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 287 | const SubobjectDesignator &D) : |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 288 | APValue(B, O, APValue::NoLValuePath()), CallFrame(F), Designator(D) {} |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 289 | CCValue(ASTContext &Ctx, const APValue &V, GlobalValue) : |
| 290 | APValue(V), CallFrame(0), Designator(Ctx, V) {} |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 291 | CCValue(const ValueDecl *D, bool IsDerivedMember, |
| 292 | ArrayRef<const CXXRecordDecl*> Path) : |
| 293 | APValue(D, IsDerivedMember, Path) {} |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 294 | CCValue(const AddrLabelExpr* LHSExpr, const AddrLabelExpr* RHSExpr) : |
| 295 | APValue(LHSExpr, RHSExpr) {} |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 296 | |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 297 | CallStackFrame *getLValueFrame() const { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 298 | assert(getKind() == LValue); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 299 | return CallFrame; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 300 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 301 | SubobjectDesignator &getLValueDesignator() { |
| 302 | assert(getKind() == LValue); |
| 303 | return Designator; |
| 304 | } |
| 305 | const SubobjectDesignator &getLValueDesignator() const { |
| 306 | return const_cast<CCValue*>(this)->getLValueDesignator(); |
| 307 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 310 | /// A stack frame in the constexpr call stack. |
| 311 | struct CallStackFrame { |
| 312 | EvalInfo &Info; |
| 313 | |
| 314 | /// Parent - The caller of this stack frame. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 315 | CallStackFrame *Caller; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 316 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 317 | /// CallLoc - The location of the call expression for this call. |
| 318 | SourceLocation CallLoc; |
| 319 | |
| 320 | /// Callee - The function which was called. |
| 321 | const FunctionDecl *Callee; |
| 322 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 323 | /// This - The binding for the this pointer in this call, if any. |
| 324 | const LValue *This; |
| 325 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 326 | /// ParmBindings - Parameter bindings for this function call, indexed by |
| 327 | /// parameters' function scope indices. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 328 | const CCValue *Arguments; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 329 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 330 | typedef llvm::DenseMap<const Expr*, CCValue> MapTy; |
| 331 | typedef MapTy::const_iterator temp_iterator; |
| 332 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 333 | MapTy Temporaries; |
| 334 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 335 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 336 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 337 | const CCValue *Arguments); |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 338 | ~CallStackFrame(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 339 | }; |
| 340 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 341 | /// A partial diagnostic which we might know in advance that we are not going |
| 342 | /// to emit. |
| 343 | class OptionalDiagnostic { |
| 344 | PartialDiagnostic *Diag; |
| 345 | |
| 346 | public: |
| 347 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {} |
| 348 | |
| 349 | template<typename T> |
| 350 | OptionalDiagnostic &operator<<(const T &v) { |
| 351 | if (Diag) |
| 352 | *Diag << v; |
| 353 | return *this; |
| 354 | } |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 355 | |
| 356 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 357 | if (Diag) { |
| 358 | llvm::SmallVector<char, 32> Buffer; |
| 359 | I.toString(Buffer); |
| 360 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 361 | } |
| 362 | return *this; |
| 363 | } |
| 364 | |
| 365 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 366 | if (Diag) { |
| 367 | llvm::SmallVector<char, 32> Buffer; |
| 368 | F.toString(Buffer); |
| 369 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 370 | } |
| 371 | return *this; |
| 372 | } |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 373 | }; |
| 374 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 375 | struct EvalInfo { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 376 | ASTContext &Ctx; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 377 | |
| 378 | /// EvalStatus - Contains information about the evaluation. |
| 379 | Expr::EvalStatus &EvalStatus; |
| 380 | |
| 381 | /// CurrentCall - The top of the constexpr call stack. |
| 382 | CallStackFrame *CurrentCall; |
| 383 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 384 | /// CallStackDepth - The number of calls in the call stack right now. |
| 385 | unsigned CallStackDepth; |
| 386 | |
| 387 | typedef llvm::DenseMap<const OpaqueValueExpr*, CCValue> MapTy; |
| 388 | /// OpaqueValues - Values used as the common expression in a |
| 389 | /// BinaryConditionalOperator. |
| 390 | MapTy OpaqueValues; |
| 391 | |
| 392 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 393 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 394 | CallStackFrame BottomFrame; |
| 395 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 396 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 397 | /// evaluated, if any. |
| 398 | const VarDecl *EvaluatingDecl; |
| 399 | |
| 400 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 401 | /// declaration whose initializer is being evaluated, if any. |
| 402 | APValue *EvaluatingDeclValue; |
| 403 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 404 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 405 | /// notes attached to it will also be stored, otherwise they will not be. |
| 406 | bool HasActiveDiagnostic; |
| 407 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 408 | /// CheckingPotentialConstantExpression - Are we checking whether the |
| 409 | /// expression is a potential constant expression? If so, some diagnostics |
| 410 | /// are suppressed. |
| 411 | bool CheckingPotentialConstantExpression; |
| 412 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 413 | |
| 414 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 415 | : Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0), |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 416 | CallStackDepth(0), BottomFrame(*this, SourceLocation(), 0, 0, 0), |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 417 | EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false), |
| 418 | CheckingPotentialConstantExpression(false) {} |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 419 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 420 | const CCValue *getOpaqueValue(const OpaqueValueExpr *e) const { |
| 421 | MapTy::const_iterator i = OpaqueValues.find(e); |
| 422 | if (i == OpaqueValues.end()) return 0; |
| 423 | return &i->second; |
| 424 | } |
| 425 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 426 | void setEvaluatingDecl(const VarDecl *VD, APValue &Value) { |
| 427 | EvaluatingDecl = VD; |
| 428 | EvaluatingDeclValue = &Value; |
| 429 | } |
| 430 | |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 431 | const LangOptions &getLangOpts() const { return Ctx.getLangOptions(); } |
| 432 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 433 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 434 | // Don't perform any constexpr calls (other than the call we're checking) |
| 435 | // when checking a potential constant expression. |
| 436 | if (CheckingPotentialConstantExpression && CallStackDepth > 1) |
| 437 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 438 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 439 | return true; |
| 440 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 441 | << getLangOpts().ConstexprCallDepth; |
| 442 | return false; |
Richard Smith | c18c423 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 443 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 444 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 445 | private: |
| 446 | /// Add a diagnostic to the diagnostics list. |
| 447 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 448 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 449 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 450 | return EvalStatus.Diag->back().second; |
| 451 | } |
| 452 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 453 | /// Add notes containing a call stack to the current point of evaluation. |
| 454 | void addCallStack(unsigned Limit); |
| 455 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 456 | public: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 457 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 458 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId |
| 459 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 460 | unsigned ExtraNotes = 0) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 461 | // If we have a prior diagnostic, it will be noting that the expression |
| 462 | // isn't a constant expression. This diagnostic is more important. |
| 463 | // FIXME: We might want to show both diagnostics to the user. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 464 | if (EvalStatus.Diag) { |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 465 | unsigned CallStackNotes = CallStackDepth - 1; |
| 466 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 467 | if (Limit) |
| 468 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 469 | if (CheckingPotentialConstantExpression) |
| 470 | CallStackNotes = 0; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 471 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 472 | HasActiveDiagnostic = true; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 473 | EvalStatus.Diag->clear(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 474 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 475 | addDiag(Loc, DiagId); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 476 | if (!CheckingPotentialConstantExpression) |
| 477 | addCallStack(Limit); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 478 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 479 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 480 | HasActiveDiagnostic = false; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 481 | return OptionalDiagnostic(); |
| 482 | } |
| 483 | |
| 484 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 485 | /// expression. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 486 | OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId |
| 487 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 488 | unsigned ExtraNotes = 0) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 489 | // Don't override a previous diagnostic. |
| 490 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) |
| 491 | return OptionalDiagnostic(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 492 | return Diag(Loc, DiagId, ExtraNotes); |
| 493 | } |
| 494 | |
| 495 | /// Add a note to a prior diagnostic. |
| 496 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 497 | if (!HasActiveDiagnostic) |
| 498 | return OptionalDiagnostic(); |
| 499 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 500 | } |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 501 | |
| 502 | /// Add a stack of notes to a prior diagnostic. |
| 503 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 504 | if (HasActiveDiagnostic) { |
| 505 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 506 | Diags.begin(), Diags.end()); |
| 507 | } |
| 508 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 509 | |
| 510 | /// Should we continue evaluation as much as possible after encountering a |
| 511 | /// construct which can't be folded? |
| 512 | bool keepEvaluatingAfterFailure() { |
| 513 | return CheckingPotentialConstantExpression && EvalStatus.Diag->empty(); |
| 514 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 515 | }; |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 516 | |
| 517 | /// Object used to treat all foldable expressions as constant expressions. |
| 518 | struct FoldConstant { |
| 519 | bool Enabled; |
| 520 | |
| 521 | explicit FoldConstant(EvalInfo &Info) |
| 522 | : Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() && |
| 523 | !Info.EvalStatus.HasSideEffects) { |
| 524 | } |
| 525 | // Treat the value we've computed since this object was created as constant. |
| 526 | void Fold(EvalInfo &Info) { |
| 527 | if (Enabled && !Info.EvalStatus.Diag->empty() && |
| 528 | !Info.EvalStatus.HasSideEffects) |
| 529 | Info.EvalStatus.Diag->clear(); |
| 530 | } |
| 531 | }; |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 532 | } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 533 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 534 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 535 | CheckSubobjectKind CSK) { |
| 536 | if (Invalid) |
| 537 | return false; |
| 538 | if (isOnePastTheEnd()) { |
| 539 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_past_end_subobject) |
| 540 | << CSK; |
| 541 | setInvalid(); |
| 542 | return false; |
| 543 | } |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| 548 | const Expr *E, uint64_t N) { |
| 549 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) |
| 550 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_array_index) |
| 551 | << static_cast<int>(N) << /*array*/ 0 |
| 552 | << static_cast<unsigned>(MostDerivedArraySize); |
| 553 | else |
| 554 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_array_index) |
| 555 | << static_cast<int>(N) << /*non-array*/ 1; |
| 556 | setInvalid(); |
| 557 | } |
| 558 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 559 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 560 | const FunctionDecl *Callee, const LValue *This, |
| 561 | const CCValue *Arguments) |
| 562 | : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), |
| 563 | This(This), Arguments(Arguments) { |
| 564 | Info.CurrentCall = this; |
| 565 | ++Info.CallStackDepth; |
| 566 | } |
| 567 | |
| 568 | CallStackFrame::~CallStackFrame() { |
| 569 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 570 | --Info.CallStackDepth; |
| 571 | Info.CurrentCall = Caller; |
| 572 | } |
| 573 | |
| 574 | /// Produce a string describing the given constexpr call. |
| 575 | static void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) { |
| 576 | unsigned ArgIndex = 0; |
| 577 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
Richard Smith | 5ba73e1 | 2012-02-04 00:33:54 +0000 | [diff] [blame] | 578 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 579 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 580 | |
| 581 | if (!IsMemberCall) |
| 582 | Out << *Frame->Callee << '('; |
| 583 | |
| 584 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 585 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
NAKAMURA Takumi | 5fe3122 | 2012-01-26 09:37:36 +0000 | [diff] [blame] | 586 | if (ArgIndex > (unsigned)IsMemberCall) |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 587 | Out << ", "; |
| 588 | |
| 589 | const ParmVarDecl *Param = *I; |
| 590 | const CCValue &Arg = Frame->Arguments[ArgIndex]; |
| 591 | if (!Arg.isLValue() || Arg.getLValueDesignator().Invalid) |
| 592 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 593 | else { |
| 594 | // Deliberately slice off the frame to form an APValue we can print. |
| 595 | APValue Value(Arg.getLValueBase(), Arg.getLValueOffset(), |
| 596 | Arg.getLValueDesignator().Entries, |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 597 | Arg.getLValueDesignator().IsOnePastTheEnd); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 598 | Value.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 599 | } |
| 600 | |
| 601 | if (ArgIndex == 0 && IsMemberCall) |
| 602 | Out << "->" << *Frame->Callee << '('; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 605 | Out << ')'; |
| 606 | } |
| 607 | |
| 608 | void EvalInfo::addCallStack(unsigned Limit) { |
| 609 | // Determine which calls to skip, if any. |
| 610 | unsigned ActiveCalls = CallStackDepth - 1; |
| 611 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 612 | if (Limit && Limit < ActiveCalls) { |
| 613 | SkipStart = Limit / 2 + Limit % 2; |
| 614 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 617 | // Walk the call stack and add the diagnostics. |
| 618 | unsigned CallIdx = 0; |
| 619 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 620 | Frame = Frame->Caller, ++CallIdx) { |
| 621 | // Skip this call? |
| 622 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 623 | if (CallIdx == SkipStart) { |
| 624 | // Note that we're skipping calls. |
| 625 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 626 | << unsigned(ActiveCalls - Limit); |
| 627 | } |
| 628 | continue; |
| 629 | } |
| 630 | |
| 631 | llvm::SmallVector<char, 128> Buffer; |
| 632 | llvm::raw_svector_ostream Out(Buffer); |
| 633 | describeCall(Frame, Out); |
| 634 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | namespace { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 639 | struct ComplexValue { |
| 640 | private: |
| 641 | bool IsInt; |
| 642 | |
| 643 | public: |
| 644 | APSInt IntReal, IntImag; |
| 645 | APFloat FloatReal, FloatImag; |
| 646 | |
| 647 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 648 | |
| 649 | void makeComplexFloat() { IsInt = false; } |
| 650 | bool isComplexFloat() const { return !IsInt; } |
| 651 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 652 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 653 | |
| 654 | void makeComplexInt() { IsInt = true; } |
| 655 | bool isComplexInt() const { return IsInt; } |
| 656 | APSInt &getComplexIntReal() { return IntReal; } |
| 657 | APSInt &getComplexIntImag() { return IntImag; } |
| 658 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 659 | void moveInto(CCValue &v) const { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 660 | if (isComplexFloat()) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 661 | v = CCValue(FloatReal, FloatImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 662 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 663 | v = CCValue(IntReal, IntImag); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 664 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 665 | void setFrom(const CCValue &v) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 666 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 667 | if (v.isComplexFloat()) { |
| 668 | makeComplexFloat(); |
| 669 | FloatReal = v.getComplexFloatReal(); |
| 670 | FloatImag = v.getComplexFloatImag(); |
| 671 | } else { |
| 672 | makeComplexInt(); |
| 673 | IntReal = v.getComplexIntReal(); |
| 674 | IntImag = v.getComplexIntImag(); |
| 675 | } |
| 676 | } |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 677 | }; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 678 | |
| 679 | struct LValue { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 680 | APValue::LValueBase Base; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 681 | CharUnits Offset; |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 682 | CallStackFrame *Frame; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 683 | SubobjectDesignator Designator; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 684 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 685 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 686 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 687 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 688 | CallStackFrame *getLValueFrame() const { return Frame; } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 689 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 690 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 691 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 692 | void moveInto(CCValue &V) const { |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 693 | V = CCValue(Base, Offset, Frame, Designator); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 694 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 695 | void setFrom(const CCValue &V) { |
| 696 | assert(V.isLValue()); |
| 697 | Base = V.getLValueBase(); |
| 698 | Offset = V.getLValueOffset(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 699 | Frame = V.getLValueFrame(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 700 | Designator = V.getLValueDesignator(); |
| 701 | } |
| 702 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 703 | void set(APValue::LValueBase B, CallStackFrame *F = 0) { |
| 704 | Base = B; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 705 | Offset = CharUnits::Zero(); |
| 706 | Frame = F; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 707 | Designator = SubobjectDesignator(getType(B)); |
| 708 | } |
| 709 | |
| 710 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 711 | // a diagnostic and mark the designator as invalid. |
| 712 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 713 | CheckSubobjectKind CSK) { |
| 714 | if (Designator.Invalid) |
| 715 | return false; |
| 716 | if (!Base) { |
| 717 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_null_subobject) |
| 718 | << CSK; |
| 719 | Designator.setInvalid(); |
| 720 | return false; |
| 721 | } |
| 722 | return true; |
| 723 | } |
| 724 | |
| 725 | // Check this LValue refers to an object. If not, set the designator to be |
| 726 | // invalid and emit a diagnostic. |
| 727 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
| 728 | return checkNullPointer(Info, E, CSK) && |
| 729 | Designator.checkSubobject(Info, E, CSK); |
| 730 | } |
| 731 | |
| 732 | void addDecl(EvalInfo &Info, const Expr *E, |
| 733 | const Decl *D, bool Virtual = false) { |
| 734 | checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base); |
| 735 | Designator.addDeclUnchecked(D, Virtual); |
| 736 | } |
| 737 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
| 738 | checkSubobject(Info, E, CSK_ArrayToPointer); |
| 739 | Designator.addArrayUnchecked(CAT); |
| 740 | } |
| 741 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
| 742 | if (!checkNullPointer(Info, E, CSK_ArrayIndex)) |
| 743 | return; |
| 744 | Designator.adjustIndex(Info, E, N); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 745 | } |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 746 | }; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 747 | |
| 748 | struct MemberPtr { |
| 749 | MemberPtr() {} |
| 750 | explicit MemberPtr(const ValueDecl *Decl) : |
| 751 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 752 | |
| 753 | /// The member or (direct or indirect) field referred to by this member |
| 754 | /// pointer, or 0 if this is a null member pointer. |
| 755 | const ValueDecl *getDecl() const { |
| 756 | return DeclAndIsDerivedMember.getPointer(); |
| 757 | } |
| 758 | /// Is this actually a member of some type derived from the relevant class? |
| 759 | bool isDerivedMember() const { |
| 760 | return DeclAndIsDerivedMember.getInt(); |
| 761 | } |
| 762 | /// Get the class which the declaration actually lives in. |
| 763 | const CXXRecordDecl *getContainingRecord() const { |
| 764 | return cast<CXXRecordDecl>( |
| 765 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 766 | } |
| 767 | |
| 768 | void moveInto(CCValue &V) const { |
| 769 | V = CCValue(getDecl(), isDerivedMember(), Path); |
| 770 | } |
| 771 | void setFrom(const CCValue &V) { |
| 772 | assert(V.isMemberPointer()); |
| 773 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 774 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 775 | Path.clear(); |
| 776 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 777 | Path.insert(Path.end(), P.begin(), P.end()); |
| 778 | } |
| 779 | |
| 780 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 781 | /// whether the member is a member of some class derived from the class type |
| 782 | /// of the member pointer. |
| 783 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 784 | /// Path - The path of base/derived classes from the member declaration's |
| 785 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 786 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 787 | |
| 788 | /// Perform a cast towards the class of the Decl (either up or down the |
| 789 | /// hierarchy). |
| 790 | bool castBack(const CXXRecordDecl *Class) { |
| 791 | assert(!Path.empty()); |
| 792 | const CXXRecordDecl *Expected; |
| 793 | if (Path.size() >= 2) |
| 794 | Expected = Path[Path.size() - 2]; |
| 795 | else |
| 796 | Expected = getContainingRecord(); |
| 797 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 798 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 799 | // if B does not contain the original member and is not a base or |
| 800 | // derived class of the class containing the original member, the result |
| 801 | // of the cast is undefined. |
| 802 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 803 | // (D::*). We consider that to be a language defect. |
| 804 | return false; |
| 805 | } |
| 806 | Path.pop_back(); |
| 807 | return true; |
| 808 | } |
| 809 | /// Perform a base-to-derived member pointer cast. |
| 810 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 811 | if (!getDecl()) |
| 812 | return true; |
| 813 | if (!isDerivedMember()) { |
| 814 | Path.push_back(Derived); |
| 815 | return true; |
| 816 | } |
| 817 | if (!castBack(Derived)) |
| 818 | return false; |
| 819 | if (Path.empty()) |
| 820 | DeclAndIsDerivedMember.setInt(false); |
| 821 | return true; |
| 822 | } |
| 823 | /// Perform a derived-to-base member pointer cast. |
| 824 | bool castToBase(const CXXRecordDecl *Base) { |
| 825 | if (!getDecl()) |
| 826 | return true; |
| 827 | if (Path.empty()) |
| 828 | DeclAndIsDerivedMember.setInt(true); |
| 829 | if (isDerivedMember()) { |
| 830 | Path.push_back(Base); |
| 831 | return true; |
| 832 | } |
| 833 | return castBack(Base); |
| 834 | } |
| 835 | }; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 836 | |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 837 | /// Compare two member pointers, which are assumed to be of the same type. |
| 838 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 839 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 840 | return !LHS.getDecl() && !RHS.getDecl(); |
| 841 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 842 | return false; |
| 843 | return LHS.Path == RHS.Path; |
| 844 | } |
| 845 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 846 | /// Kinds of constant expression checking, for diagnostics. |
| 847 | enum CheckConstantExpressionKind { |
| 848 | CCEK_Constant, ///< A normal constant. |
| 849 | CCEK_ReturnValue, ///< A constexpr function return value. |
| 850 | CCEK_MemberInit ///< A constexpr constructor mem-initializer. |
| 851 | }; |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 852 | } |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 853 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 854 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 855 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 856 | const LValue &This, const Expr *E, |
| 857 | CheckConstantExpressionKind CCEK |
| 858 | = CCEK_Constant); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 859 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 860 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 861 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 862 | EvalInfo &Info); |
| 863 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 864 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 865 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Chris Lattner | d9becd1 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 866 | EvalInfo &Info); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 867 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 868 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 869 | |
| 870 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 871 | // Misc utilities |
| 872 | //===----------------------------------------------------------------------===// |
| 873 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 874 | /// Should this call expression be treated as a string literal? |
| 875 | static bool IsStringLiteralCall(const CallExpr *E) { |
| 876 | unsigned Builtin = E->isBuiltinCall(); |
| 877 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 878 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 879 | } |
| 880 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 881 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 882 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 883 | // constant expression of pointer type that evaluates to... |
| 884 | |
| 885 | // ... a null pointer value, or a prvalue core constant expression of type |
| 886 | // std::nullptr_t. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 887 | if (!B) return true; |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 888 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 889 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 890 | // ... the address of an object with static storage duration, |
| 891 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 892 | return VD->hasGlobalStorage(); |
| 893 | // ... the address of a function, |
| 894 | return isa<FunctionDecl>(D); |
| 895 | } |
| 896 | |
| 897 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 898 | switch (E->getStmtClass()) { |
| 899 | default: |
| 900 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 901 | case Expr::CompoundLiteralExprClass: |
| 902 | return cast<CompoundLiteralExpr>(E)->isFileScope(); |
| 903 | // A string literal has static storage duration. |
| 904 | case Expr::StringLiteralClass: |
| 905 | case Expr::PredefinedExprClass: |
| 906 | case Expr::ObjCStringLiteralClass: |
| 907 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 908 | case Expr::CXXTypeidExprClass: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 909 | return true; |
| 910 | case Expr::CallExprClass: |
| 911 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 912 | // For GCC compatibility, &&label has static storage duration. |
| 913 | case Expr::AddrLabelExprClass: |
| 914 | return true; |
| 915 | // A Block literal expression may be used as the initialization value for |
| 916 | // Block variables at global or local static scope. |
| 917 | case Expr::BlockExprClass: |
| 918 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 919 | case Expr::ImplicitValueInitExprClass: |
| 920 | // FIXME: |
| 921 | // We can never form an lvalue with an implicit value initialization as its |
| 922 | // base through expression evaluation, so these only appear in one case: the |
| 923 | // implicit variable declaration we invent when checking whether a constexpr |
| 924 | // constructor can produce a constant expression. We must assume that such |
| 925 | // an expression might be a global lvalue. |
| 926 | return true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 927 | } |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 930 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 931 | /// value for an address or reference constant expression. Type T should be |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 932 | /// either LValue or CCValue. Return true if we can fold this expression, |
| 933 | /// whether or not it's a constant expression. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 934 | template<typename T> |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 935 | static bool CheckLValueConstantExpression(EvalInfo &Info, const Expr *E, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 936 | const T &LVal, APValue &Value, |
| 937 | CheckConstantExpressionKind CCEK) { |
| 938 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 939 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 940 | |
| 941 | if (!IsGlobalLValue(Base)) { |
| 942 | if (Info.getLangOpts().CPlusPlus0x) { |
| 943 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 944 | Info.Diag(E->getExprLoc(), diag::note_constexpr_non_global, 1) |
| 945 | << E->isGLValue() << !Designator.Entries.empty() |
| 946 | << !!VD << CCEK << VD; |
| 947 | if (VD) |
| 948 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 949 | else |
| 950 | Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), |
| 951 | diag::note_constexpr_temporary_here); |
| 952 | } else { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 953 | Info.Diag(E->getExprLoc()); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 954 | } |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 955 | // Don't allow references to temporaries to escape. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 956 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 957 | } |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 958 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 959 | bool IsReferenceType = E->isGLValue(); |
| 960 | |
| 961 | if (Designator.Invalid) { |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 962 | // This is not a core constant expression. An appropriate diagnostic will |
| 963 | // have already been produced. |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 964 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
| 965 | APValue::NoLValuePath()); |
| 966 | return true; |
| 967 | } |
| 968 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 969 | Value = APValue(LVal.getLValueBase(), LVal.getLValueOffset(), |
| 970 | Designator.Entries, Designator.IsOnePastTheEnd); |
| 971 | |
| 972 | // Allow address constant expressions to be past-the-end pointers. This is |
| 973 | // an extension: the standard requires them to point to an object. |
| 974 | if (!IsReferenceType) |
| 975 | return true; |
| 976 | |
| 977 | // A reference constant expression must refer to an object. |
| 978 | if (!Base) { |
| 979 | // FIXME: diagnostic |
| 980 | Info.CCEDiag(E->getExprLoc()); |
Richard Smith | 61e6162 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 981 | return true; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 984 | // Does this refer one past the end of some object? |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 985 | if (Designator.isOnePastTheEnd()) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 986 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 987 | Info.Diag(E->getExprLoc(), diag::note_constexpr_past_end, 1) |
| 988 | << !Designator.Entries.empty() << !!VD << VD; |
| 989 | if (VD) |
| 990 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 991 | else |
| 992 | Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(), |
| 993 | diag::note_constexpr_temporary_here); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 996 | return true; |
| 997 | } |
| 998 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 999 | /// Check that this core constant expression is of literal type, and if not, |
| 1000 | /// produce an appropriate diagnostic. |
| 1001 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E) { |
| 1002 | if (!E->isRValue() || E->getType()->isLiteralType()) |
| 1003 | return true; |
| 1004 | |
| 1005 | // Prvalue constant expressions must be of literal types. |
| 1006 | if (Info.getLangOpts().CPlusPlus0x) |
| 1007 | Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) |
| 1008 | << E->getType(); |
| 1009 | else |
| 1010 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1011 | return false; |
| 1012 | } |
| 1013 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1014 | /// 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] | 1015 | /// constant expression, and if it is, produce the corresponding constant value. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1016 | /// If not, report an appropriate diagnostic. Does not check that the expression |
| 1017 | /// is of literal type. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1018 | static bool CheckConstantExpression(EvalInfo &Info, const Expr *E, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1019 | const CCValue &CCValue, APValue &Value, |
| 1020 | CheckConstantExpressionKind CCEK |
| 1021 | = CCEK_Constant) { |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1022 | if (!CCValue.isLValue()) { |
| 1023 | Value = CCValue; |
| 1024 | return true; |
| 1025 | } |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1026 | return CheckLValueConstantExpression(Info, E, CCValue, Value, CCEK); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1029 | const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1030 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1034 | return Value.Base.dyn_cast<const Expr*>() && !Value.Frame; |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1037 | static bool IsWeakLValue(const LValue &Value) { |
| 1038 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1039 | return Decl && Decl->isWeak(); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1042 | static bool EvalPointerValueAsBool(const CCValue &Value, bool &Result) { |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1043 | // A null base expression indicates a null pointer. These are always |
| 1044 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1045 | if (!Value.getLValueBase()) { |
| 1046 | Result = !Value.getLValueOffset().isZero(); |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1047 | return true; |
| 1048 | } |
Rafael Espindola | a7d3c04 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 1049 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1050 | // We have a non-null base. These are generally known to be true, but if it's |
| 1051 | // a weak declaration it can be null at runtime. |
John McCall | 3554283 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1052 | Result = true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1053 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | 0dd7a25 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1054 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1057 | static bool HandleConversionToBool(const CCValue &Val, bool &Result) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1058 | switch (Val.getKind()) { |
| 1059 | case APValue::Uninitialized: |
| 1060 | return false; |
| 1061 | case APValue::Int: |
| 1062 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1063 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1064 | case APValue::Float: |
| 1065 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1066 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1067 | case APValue::ComplexInt: |
| 1068 | Result = Val.getComplexIntReal().getBoolValue() || |
| 1069 | Val.getComplexIntImag().getBoolValue(); |
| 1070 | return true; |
| 1071 | case APValue::ComplexFloat: |
| 1072 | Result = !Val.getComplexFloatReal().isZero() || |
| 1073 | !Val.getComplexFloatImag().isZero(); |
| 1074 | return true; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1075 | case APValue::LValue: |
| 1076 | return EvalPointerValueAsBool(Val, Result); |
| 1077 | case APValue::MemberPointer: |
| 1078 | Result = Val.getMemberPointerDecl(); |
| 1079 | return true; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1080 | case APValue::Vector: |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1081 | case APValue::Array: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1082 | case APValue::Struct: |
| 1083 | case APValue::Union: |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 1084 | case APValue::AddrLabelDiff: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1085 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1088 | llvm_unreachable("unknown APValue kind"); |
| 1089 | } |
| 1090 | |
| 1091 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 1092 | EvalInfo &Info) { |
| 1093 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1094 | CCValue Val; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1095 | if (!Evaluate(Val, Info, E)) |
| 1096 | return false; |
| 1097 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1100 | template<typename T> |
| 1101 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
| 1102 | const T &SrcValue, QualType DestType) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1103 | Info.Diag(E->getExprLoc(), diag::note_constexpr_overflow) |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 1104 | << SrcValue << DestType; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 1109 | QualType SrcType, const APFloat &Value, |
| 1110 | QualType DestType, APSInt &Result) { |
| 1111 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1112 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1113 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1115 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1116 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1117 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 1118 | & APFloat::opInvalidOp) |
| 1119 | return HandleOverflow(Info, E, Value, DestType); |
| 1120 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1123 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 1124 | QualType SrcType, QualType DestType, |
| 1125 | APFloat &Result) { |
| 1126 | APFloat Value = Result; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1127 | bool ignored; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1128 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 1129 | APFloat::rmNearestTiesToEven, &ignored) |
| 1130 | & APFloat::opOverflow) |
| 1131 | return HandleOverflow(Info, E, Value, DestType); |
| 1132 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 1135 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 1136 | QualType DestType, QualType SrcType, |
| 1137 | APSInt &Value) { |
| 1138 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1139 | APSInt Result = Value; |
| 1140 | // Figure out if this is a truncate, extend or noop cast. |
| 1141 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1142 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1143 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1144 | return Result; |
| 1145 | } |
| 1146 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1147 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 1148 | QualType SrcType, const APSInt &Value, |
| 1149 | QualType DestType, APFloat &Result) { |
| 1150 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 1151 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 1152 | APFloat::rmNearestTiesToEven) |
| 1153 | & APFloat::opOverflow) |
| 1154 | return HandleOverflow(Info, E, Value, DestType); |
| 1155 | return true; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1158 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 1159 | llvm::APInt &Res) { |
| 1160 | CCValue SVal; |
| 1161 | if (!Evaluate(SVal, Info, E)) |
| 1162 | return false; |
| 1163 | if (SVal.isInt()) { |
| 1164 | Res = SVal.getInt(); |
| 1165 | return true; |
| 1166 | } |
| 1167 | if (SVal.isFloat()) { |
| 1168 | Res = SVal.getFloat().bitcastToAPInt(); |
| 1169 | return true; |
| 1170 | } |
| 1171 | if (SVal.isVector()) { |
| 1172 | QualType VecTy = E->getType(); |
| 1173 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 1174 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 1175 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 1176 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 1177 | Res = llvm::APInt::getNullValue(VecSize); |
| 1178 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 1179 | APValue &Elt = SVal.getVectorElt(i); |
| 1180 | llvm::APInt EltAsInt; |
| 1181 | if (Elt.isInt()) { |
| 1182 | EltAsInt = Elt.getInt(); |
| 1183 | } else if (Elt.isFloat()) { |
| 1184 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 1185 | } else { |
| 1186 | // Don't try to handle vectors of anything other than int or float |
| 1187 | // (not sure if it's possible to hit this case). |
| 1188 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1189 | return false; |
| 1190 | } |
| 1191 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 1192 | if (BigEndian) |
| 1193 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 1194 | else |
| 1195 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 1196 | } |
| 1197 | return true; |
| 1198 | } |
| 1199 | // Give up if the input isn't an int, float, or vector. For example, we |
| 1200 | // reject "(v4i16)(intptr_t)&a". |
| 1201 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1202 | return false; |
| 1203 | } |
| 1204 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1205 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 1206 | /// truncating the lvalue's path to the given length. |
| 1207 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 1208 | const RecordDecl *TruncatedType, |
| 1209 | unsigned TruncatedElements) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1210 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1211 | |
| 1212 | // Check we actually point to a derived class object. |
| 1213 | if (TruncatedElements == D.Entries.size()) |
| 1214 | return true; |
| 1215 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 1216 | "not casting to a derived class"); |
| 1217 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 1218 | return false; |
| 1219 | |
| 1220 | // 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] | 1221 | const RecordDecl *RD = TruncatedType; |
| 1222 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1223 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1224 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1225 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1226 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1227 | else |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1228 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 1229 | RD = Base; |
| 1230 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1231 | D.Entries.resize(TruncatedElements); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1232 | return true; |
| 1233 | } |
| 1234 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1235 | static void HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1236 | const CXXRecordDecl *Derived, |
| 1237 | const CXXRecordDecl *Base, |
| 1238 | const ASTRecordLayout *RL = 0) { |
| 1239 | if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 1240 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1241 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1244 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1245 | const CXXRecordDecl *DerivedDecl, |
| 1246 | const CXXBaseSpecifier *Base) { |
| 1247 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1248 | |
| 1249 | if (!Base->isVirtual()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1250 | HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1251 | return true; |
| 1252 | } |
| 1253 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1254 | SubobjectDesignator &D = Obj.Designator; |
| 1255 | if (D.Invalid) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1256 | return false; |
| 1257 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1258 | // Extract most-derived object and corresponding type. |
| 1259 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1260 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 1261 | return false; |
| 1262 | |
| 1263 | // Find the virtual base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1264 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1265 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1266 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1267 | return true; |
| 1268 | } |
| 1269 | |
| 1270 | /// Update LVal to refer to the given field, which must be a member of the type |
| 1271 | /// currently described by LVal. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1272 | static void HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1273 | const FieldDecl *FD, |
| 1274 | const ASTRecordLayout *RL = 0) { |
| 1275 | if (!RL) |
| 1276 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
| 1277 | |
| 1278 | unsigned I = FD->getFieldIndex(); |
| 1279 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1280 | LVal.addDecl(Info, E, FD); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1283 | /// Update LVal to refer to the given indirect field. |
| 1284 | static void HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
| 1285 | LValue &LVal, |
| 1286 | const IndirectFieldDecl *IFD) { |
| 1287 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 1288 | CE = IFD->chain_end(); C != CE; ++C) |
| 1289 | HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C)); |
| 1290 | } |
| 1291 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1292 | /// Get the size of the given type in char units. |
| 1293 | static bool HandleSizeof(EvalInfo &Info, QualType Type, CharUnits &Size) { |
| 1294 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1295 | // extension. |
| 1296 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 1297 | Size = CharUnits::One(); |
| 1298 | return true; |
| 1299 | } |
| 1300 | |
| 1301 | if (!Type->isConstantSizeType()) { |
| 1302 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1303 | // FIXME: Diagnostic. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1304 | return false; |
| 1305 | } |
| 1306 | |
| 1307 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 1308 | return true; |
| 1309 | } |
| 1310 | |
| 1311 | /// Update a pointer value to model pointer arithmetic. |
| 1312 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1313 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1314 | /// \param LVal - The pointer value to be updated. |
| 1315 | /// \param EltTy - The pointee type represented by LVal. |
| 1316 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1317 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 1318 | LValue &LVal, QualType EltTy, |
| 1319 | int64_t Adjustment) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1320 | CharUnits SizeOfPointee; |
| 1321 | if (!HandleSizeof(Info, EltTy, SizeOfPointee)) |
| 1322 | return false; |
| 1323 | |
| 1324 | // Compute the new offset in the appropriate width. |
| 1325 | LVal.Offset += Adjustment * SizeOfPointee; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1326 | LVal.adjustIndex(Info, E, Adjustment); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1327 | return true; |
| 1328 | } |
| 1329 | |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1330 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1331 | static bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 1332 | const VarDecl *VD, |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1333 | CallStackFrame *Frame, CCValue &Result) { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1334 | // If this is a parameter to an active constexpr function call, perform |
| 1335 | // argument substitution. |
| 1336 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1337 | // Assume arguments of a potential constant expression are unknown |
| 1338 | // constant expressions. |
| 1339 | if (Info.CheckingPotentialConstantExpression) |
| 1340 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1341 | if (!Frame || !Frame->Arguments) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1342 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1343 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1344 | } |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1345 | Result = Frame->Arguments[PVD->getFunctionScopeIndex()]; |
| 1346 | return true; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1347 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1348 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1349 | // Dig out the initializer, and use the declaration which it's attached to. |
| 1350 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1351 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1352 | // If we're checking a potential constant expression, the variable could be |
| 1353 | // initialized later. |
| 1354 | if (!Info.CheckingPotentialConstantExpression) |
| 1355 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1356 | return false; |
| 1357 | } |
| 1358 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1359 | // If we're currently evaluating the initializer of this declaration, use that |
| 1360 | // in-flight value. |
| 1361 | if (Info.EvaluatingDecl == VD) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1362 | Result = CCValue(Info.Ctx, *Info.EvaluatingDeclValue, |
| 1363 | CCValue::GlobalValue()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1364 | return !Result.isUninit(); |
| 1365 | } |
| 1366 | |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1367 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 1368 | // this is the definition which will be used. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1369 | if (VD->isWeak()) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1370 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1371 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1372 | } |
Richard Smith | 65ac598 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1373 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1374 | // Check that we can fold the initializer. In C++, we will have already done |
| 1375 | // this in the cases where it matters for conformance. |
| 1376 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 1377 | if (!VD->evaluateValue(Notes)) { |
| 1378 | Info.Diag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant, |
| 1379 | Notes.size() + 1) << VD; |
| 1380 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1381 | Info.addNotes(Notes); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1382 | return false; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1383 | } else if (!VD->checkInitIsICE()) { |
| 1384 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_var_init_non_constant, |
| 1385 | Notes.size() + 1) << VD; |
| 1386 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1387 | Info.addNotes(Notes); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1388 | } |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1389 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1390 | Result = CCValue(Info.Ctx, *VD->getEvaluatedValue(), CCValue::GlobalValue()); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1391 | return true; |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1394 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1395 | Qualifiers Quals = T.getQualifiers(); |
| 1396 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 1397 | } |
| 1398 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1399 | /// Get the base index of the given base class within an APValue representing |
| 1400 | /// the given derived class. |
| 1401 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 1402 | const CXXRecordDecl *Base) { |
| 1403 | Base = Base->getCanonicalDecl(); |
| 1404 | unsigned Index = 0; |
| 1405 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 1406 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 1407 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 1408 | return Index; |
| 1409 | } |
| 1410 | |
| 1411 | llvm_unreachable("base class missing from derived class's bases list"); |
| 1412 | } |
| 1413 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1414 | /// Extract the designated sub-object of an rvalue. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1415 | static bool ExtractSubobject(EvalInfo &Info, const Expr *E, |
| 1416 | CCValue &Obj, QualType ObjType, |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1417 | const SubobjectDesignator &Sub, QualType SubType) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1418 | if (Sub.Invalid) |
| 1419 | // A diagnostic will have already been produced. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1420 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1421 | if (Sub.isOnePastTheEnd()) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1422 | Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? |
Matt Beaumont-Gay | aa5d533 | 2011-12-21 19:36:37 +0000 | [diff] [blame] | 1423 | (unsigned)diag::note_constexpr_read_past_end : |
| 1424 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1425 | return false; |
| 1426 | } |
Richard Smith | f64699e | 2011-11-11 08:28:03 +0000 | [diff] [blame] | 1427 | if (Sub.Entries.empty()) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1428 | return true; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1429 | if (Info.CheckingPotentialConstantExpression && Obj.isUninit()) |
| 1430 | // This object might be initialized later. |
| 1431 | return false; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1432 | |
| 1433 | assert(!Obj.isLValue() && "extracting subobject of lvalue"); |
| 1434 | const APValue *O = &Obj; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1435 | // Walk the designator's path to find the subobject. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1436 | for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) { |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1437 | if (ObjType->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1438 | // Next subobject is an array element. |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1439 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1440 | assert(CAT && "vla in literal type?"); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1441 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1442 | if (CAT->getSize().ule(Index)) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1443 | // Note, it should not be possible to form a pointer with a valid |
| 1444 | // designator which points more than one past the end of the array. |
| 1445 | Info.Diag(E->getExprLoc(), Info.getLangOpts().CPlusPlus0x ? |
Matt Beaumont-Gay | aa5d533 | 2011-12-21 19:36:37 +0000 | [diff] [blame] | 1446 | (unsigned)diag::note_constexpr_read_past_end : |
| 1447 | (unsigned)diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1448 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1449 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1450 | if (O->getArrayInitializedElts() > Index) |
| 1451 | O = &O->getArrayInitializedElt(Index); |
| 1452 | else |
| 1453 | O = &O->getArrayFiller(); |
| 1454 | ObjType = CAT->getElementType(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1455 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
| 1456 | // Next subobject is a class, struct or union field. |
| 1457 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 1458 | if (RD->isUnion()) { |
| 1459 | const FieldDecl *UnionField = O->getUnionField(); |
| 1460 | if (!UnionField || |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1461 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1462 | Info.Diag(E->getExprLoc(), |
| 1463 | diag::note_constexpr_read_inactive_union_member) |
| 1464 | << Field << !UnionField << UnionField; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1465 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1466 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1467 | O = &O->getUnionValue(); |
| 1468 | } else |
| 1469 | O = &O->getStructField(Field->getFieldIndex()); |
| 1470 | ObjType = Field->getType(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1471 | |
| 1472 | if (ObjType.isVolatileQualified()) { |
| 1473 | if (Info.getLangOpts().CPlusPlus) { |
| 1474 | // FIXME: Include a description of the path to the volatile subobject. |
| 1475 | Info.Diag(E->getExprLoc(), diag::note_constexpr_ltor_volatile_obj, 1) |
| 1476 | << 2 << Field; |
| 1477 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 1478 | } else { |
| 1479 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1480 | } |
| 1481 | return false; |
| 1482 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1483 | } else { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1484 | // Next subobject is a base class. |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1485 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 1486 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 1487 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
| 1488 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1489 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1490 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1491 | if (O->isUninit()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1492 | if (!Info.CheckingPotentialConstantExpression) |
| 1493 | Info.Diag(E->getExprLoc(), diag::note_constexpr_read_uninit); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1494 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1495 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1498 | Obj = CCValue(Info.Ctx, *O, CCValue::GlobalValue()); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1499 | return true; |
| 1500 | } |
| 1501 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1502 | /// Find the position where two subobject designators diverge, or equivalently |
| 1503 | /// the length of the common initial subsequence. |
| 1504 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 1505 | const SubobjectDesignator &A, |
| 1506 | const SubobjectDesignator &B, |
| 1507 | bool &WasArrayIndex) { |
| 1508 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 1509 | for (/**/; I != N; ++I) { |
| 1510 | if (!ObjType.isNull() && ObjType->isArrayType()) { |
| 1511 | // Next subobject is an array element. |
| 1512 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 1513 | WasArrayIndex = true; |
| 1514 | return I; |
| 1515 | } |
| 1516 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
| 1517 | } else { |
| 1518 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 1519 | WasArrayIndex = false; |
| 1520 | return I; |
| 1521 | } |
| 1522 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 1523 | // Next subobject is a field. |
| 1524 | ObjType = FD->getType(); |
| 1525 | else |
| 1526 | // Next subobject is a base class. |
| 1527 | ObjType = QualType(); |
| 1528 | } |
| 1529 | } |
| 1530 | WasArrayIndex = false; |
| 1531 | return I; |
| 1532 | } |
| 1533 | |
| 1534 | /// Determine whether the given subobject designators refer to elements of the |
| 1535 | /// same array object. |
| 1536 | static bool AreElementsOfSameArray(QualType ObjType, |
| 1537 | const SubobjectDesignator &A, |
| 1538 | const SubobjectDesignator &B) { |
| 1539 | if (A.Entries.size() != B.Entries.size()) |
| 1540 | return false; |
| 1541 | |
| 1542 | bool IsArray = A.MostDerivedArraySize != 0; |
| 1543 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 1544 | // A is a subobject of the array element. |
| 1545 | return false; |
| 1546 | |
| 1547 | // If A (and B) designates an array element, the last entry will be the array |
| 1548 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 1549 | // of length 1' case, and the entire path must match. |
| 1550 | bool WasArrayIndex; |
| 1551 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 1552 | return CommonLength >= A.Entries.size() - IsArray; |
| 1553 | } |
| 1554 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1555 | /// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on |
| 1556 | /// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions |
| 1557 | /// for looking up the glvalue referred to by an entity of reference type. |
| 1558 | /// |
| 1559 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1560 | /// \param Conv - The expression for which we are performing the conversion. |
| 1561 | /// Used for diagnostics. |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 1562 | /// \param Type - The type we expect this conversion to produce, before |
| 1563 | /// stripping cv-qualifiers in the case of a non-clas type. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1564 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 1565 | /// \param RVal - The produced value will be placed here. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1566 | static bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
| 1567 | QualType Type, |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1568 | const LValue &LVal, CCValue &RVal) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1569 | // In C, an lvalue-to-rvalue conversion is never a constant expression. |
| 1570 | if (!Info.getLangOpts().CPlusPlus) |
| 1571 | Info.CCEDiag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 1572 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1573 | if (LVal.Designator.Invalid) |
| 1574 | // A diagnostic will have already been produced. |
| 1575 | return false; |
| 1576 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1577 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1578 | CallStackFrame *Frame = LVal.Frame; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1579 | SourceLocation Loc = Conv->getExprLoc(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1580 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1581 | if (!LVal.Base) { |
| 1582 | // FIXME: Indirection through a null pointer deserves a specific diagnostic. |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1583 | Info.Diag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 1584 | return false; |
| 1585 | } |
| 1586 | |
| 1587 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 1588 | // is not a constant expression (even if the object is non-volatile). We also |
| 1589 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 1590 | // semantics. |
| 1591 | if (Type.isVolatileQualified()) { |
| 1592 | if (Info.getLangOpts().CPlusPlus) |
| 1593 | Info.Diag(Loc, diag::note_constexpr_ltor_volatile_type) << Type; |
| 1594 | else |
| 1595 | Info.Diag(Loc); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1596 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1597 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1598 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1599 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1600 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 1601 | // In C++11, constexpr, non-volatile variables initialized with constant |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1602 | // expressions are constant expressions too. Inside constexpr functions, |
| 1603 | // parameters are constant expressions even if they're non-const. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1604 | // 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] | 1605 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1606 | if (const VarDecl *VDef = VD->getDefinition()) |
| 1607 | VD = VDef; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1608 | if (!VD || VD->isInvalidDecl()) { |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1609 | Info.Diag(Loc); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1610 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1613 | // DR1313: If the object is volatile-qualified but the glvalue was not, |
| 1614 | // behavior is undefined so the result is not a constant expression. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1615 | QualType VT = VD->getType(); |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1616 | if (VT.isVolatileQualified()) { |
| 1617 | if (Info.getLangOpts().CPlusPlus) { |
| 1618 | Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD; |
| 1619 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1620 | } else { |
| 1621 | Info.Diag(Loc); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1622 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1623 | return false; |
| 1624 | } |
| 1625 | |
| 1626 | if (!isa<ParmVarDecl>(VD)) { |
| 1627 | if (VD->isConstexpr()) { |
| 1628 | // OK, we can read this variable. |
| 1629 | } else if (VT->isIntegralOrEnumerationType()) { |
| 1630 | if (!VT.isConstQualified()) { |
| 1631 | if (Info.getLangOpts().CPlusPlus) { |
| 1632 | Info.Diag(Loc, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
| 1633 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1634 | } else { |
| 1635 | Info.Diag(Loc); |
| 1636 | } |
| 1637 | return false; |
| 1638 | } |
| 1639 | } else if (VT->isFloatingType() && VT.isConstQualified()) { |
| 1640 | // We support folding of const floating-point types, in order to make |
| 1641 | // static const data members of such types (supported as an extension) |
| 1642 | // more useful. |
| 1643 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1644 | Info.CCEDiag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 1645 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1646 | } else { |
| 1647 | Info.CCEDiag(Loc); |
| 1648 | } |
| 1649 | } else { |
| 1650 | // FIXME: Allow folding of values of any literal type in all languages. |
| 1651 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1652 | Info.Diag(Loc, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 1653 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1654 | } else { |
| 1655 | Info.Diag(Loc); |
| 1656 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1657 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1658 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1659 | } |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1660 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1661 | if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1662 | return false; |
| 1663 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1664 | if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1665 | return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1666 | |
| 1667 | // The declaration was initialized by an lvalue, with no lvalue-to-rvalue |
| 1668 | // conversion. This happens when the declaration and the lvalue should be |
| 1669 | // considered synonymous, for instance when initializing an array of char |
| 1670 | // from a string literal. Continue as if the initializer lvalue was the |
| 1671 | // value we were originally given. |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1672 | assert(RVal.getLValueOffset().isZero() && |
| 1673 | "offset for lvalue init of non-reference"); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1674 | Base = RVal.getLValueBase().get<const Expr*>(); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1675 | Frame = RVal.getLValueFrame(); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1678 | // Volatile temporary objects cannot be read in constant expressions. |
| 1679 | if (Base->getType().isVolatileQualified()) { |
| 1680 | if (Info.getLangOpts().CPlusPlus) { |
| 1681 | Info.Diag(Loc, diag::note_constexpr_ltor_volatile_obj, 1) << 0; |
| 1682 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 1683 | } else { |
| 1684 | Info.Diag(Loc); |
| 1685 | } |
| 1686 | return false; |
| 1687 | } |
| 1688 | |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1689 | // FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant |
| 1690 | if (const StringLiteral *S = dyn_cast<StringLiteral>(Base)) { |
| 1691 | const SubobjectDesignator &Designator = LVal.Designator; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1692 | if (Designator.Invalid || Designator.Entries.size() != 1) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1693 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1694 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1695 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1696 | |
| 1697 | assert(Type->isIntegerType() && "string element not integer type"); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1698 | uint64_t Index = Designator.Entries[0].ArrayIndex; |
Richard Smith | 7098cbd | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 1699 | const ConstantArrayType *CAT = |
| 1700 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 1701 | if (Index >= CAT->getSize().getZExtValue()) { |
| 1702 | // Note, it should not be possible to form a pointer which points more |
| 1703 | // than one past the end of the array without producing a prior const expr |
| 1704 | // diagnostic. |
| 1705 | Info.Diag(Loc, diag::note_constexpr_read_past_end); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1706 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1707 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1708 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 1709 | Type->isUnsignedIntegerType()); |
| 1710 | if (Index < S->getLength()) |
| 1711 | Value = S->getCodeUnit(Index); |
| 1712 | RVal = CCValue(Value); |
| 1713 | return true; |
| 1714 | } |
| 1715 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1716 | if (Frame) { |
| 1717 | // If this is a temporary expression with a nontrivial initializer, grab the |
| 1718 | // value from the relevant stack frame. |
| 1719 | RVal = Frame->Temporaries[Base]; |
| 1720 | } else if (const CompoundLiteralExpr *CLE |
| 1721 | = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 1722 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 1723 | // initializer until now for such expressions. Such an expression can't be |
| 1724 | // an ICE in C, so this only matters for fold. |
| 1725 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 1726 | if (!Evaluate(RVal, Info, CLE->getInitializer())) |
| 1727 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1728 | } else { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1729 | Info.Diag(Conv->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1730 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1731 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1732 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1733 | return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator, |
| 1734 | Type); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1737 | /// Build an lvalue for the object argument of a member function call. |
| 1738 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 1739 | LValue &This) { |
| 1740 | if (Object->getType()->isPointerType()) |
| 1741 | return EvaluatePointer(Object, This, Info); |
| 1742 | |
| 1743 | if (Object->isGLValue()) |
| 1744 | return EvaluateLValue(Object, This, Info); |
| 1745 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1746 | if (Object->getType()->isLiteralType()) |
| 1747 | return EvaluateTemporary(Object, This, Info); |
| 1748 | |
| 1749 | return false; |
| 1750 | } |
| 1751 | |
| 1752 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 1753 | /// lvalue referring to the result. |
| 1754 | /// |
| 1755 | /// \param Info - Information about the ongoing evaluation. |
| 1756 | /// \param BO - The member pointer access operation. |
| 1757 | /// \param LV - Filled in with a reference to the resulting object. |
| 1758 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 1759 | /// the resulting LValue subobject designator. This is not possible when |
| 1760 | /// creating a bound member function. |
| 1761 | /// \return The field or method declaration to which the member pointer refers, |
| 1762 | /// or 0 if evaluation fails. |
| 1763 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 1764 | const BinaryOperator *BO, |
| 1765 | LValue &LV, |
| 1766 | bool IncludeMember = true) { |
| 1767 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 1768 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1769 | bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV); |
| 1770 | if (!EvalObjOK && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1771 | return 0; |
| 1772 | |
| 1773 | MemberPtr MemPtr; |
| 1774 | if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info)) |
| 1775 | return 0; |
| 1776 | |
| 1777 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 1778 | // member value, the behavior is undefined. |
| 1779 | if (!MemPtr.getDecl()) |
| 1780 | return 0; |
| 1781 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1782 | if (!EvalObjOK) |
| 1783 | return 0; |
| 1784 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1785 | if (MemPtr.isDerivedMember()) { |
| 1786 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1787 | // The end of the derived-to-base path for the base object must match the |
| 1788 | // derived-to-base path for the member pointer. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1789 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1790 | LV.Designator.Entries.size()) |
| 1791 | return 0; |
| 1792 | unsigned PathLengthToMember = |
| 1793 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 1794 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 1795 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 1796 | LV.Designator.Entries[PathLengthToMember + I]); |
| 1797 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
| 1798 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1803 | if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(), |
| 1804 | PathLengthToMember)) |
| 1805 | return 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1806 | } else if (!MemPtr.Path.empty()) { |
| 1807 | // Extend the LValue path with the member pointer's path. |
| 1808 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 1809 | MemPtr.Path.size() + IncludeMember); |
| 1810 | |
| 1811 | // Walk down to the appropriate base class. |
| 1812 | QualType LVType = BO->getLHS()->getType(); |
| 1813 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 1814 | LVType = PT->getPointeeType(); |
| 1815 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 1816 | assert(RD && "member pointer access on non-class-type expression"); |
| 1817 | // The first class in the path is that of the lvalue. |
| 1818 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 1819 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1820 | HandleLValueDirectBase(Info, BO, LV, RD, Base); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1821 | RD = Base; |
| 1822 | } |
| 1823 | // Finally cast to the class containing the member. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1824 | HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | // Add the member. Note that we cannot build bound member functions here. |
| 1828 | if (IncludeMember) { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1829 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) |
| 1830 | HandleLValueMember(Info, BO, LV, FD); |
| 1831 | else if (const IndirectFieldDecl *IFD = |
| 1832 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) |
| 1833 | HandleLValueIndirectMember(Info, BO, LV, IFD); |
| 1834 | else |
| 1835 | llvm_unreachable("can't construct reference to bound member function"); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | return MemPtr.getDecl(); |
| 1839 | } |
| 1840 | |
| 1841 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 1842 | /// the provided lvalue, which currently refers to the base object. |
| 1843 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 1844 | LValue &Result) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1845 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1846 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1847 | return false; |
| 1848 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1849 | QualType TargetQT = E->getType(); |
| 1850 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 1851 | TargetQT = PT->getPointeeType(); |
| 1852 | |
| 1853 | // Check this cast lands within the final derived-to-base subobject path. |
| 1854 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
| 1855 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast) |
| 1856 | << D.MostDerivedType << TargetQT; |
| 1857 | return false; |
| 1858 | } |
| 1859 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1860 | // Check the type of the final cast. We don't need to check the path, |
| 1861 | // since a cast can only be formed if the path is unique. |
| 1862 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1863 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 1864 | const CXXRecordDecl *FinalType; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1865 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 1866 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1867 | else |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1868 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1869 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
| 1870 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_downcast) |
| 1871 | << D.MostDerivedType << TargetQT; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1872 | return false; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1873 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1874 | |
| 1875 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1876 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 1879 | namespace { |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1880 | enum EvalStmtResult { |
| 1881 | /// Evaluation failed. |
| 1882 | ESR_Failed, |
| 1883 | /// Hit a 'return' statement. |
| 1884 | ESR_Returned, |
| 1885 | /// Evaluation succeeded. |
| 1886 | ESR_Succeeded |
| 1887 | }; |
| 1888 | } |
| 1889 | |
| 1890 | // Evaluate a statement. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1891 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1892 | const Stmt *S) { |
| 1893 | switch (S->getStmtClass()) { |
| 1894 | default: |
| 1895 | return ESR_Failed; |
| 1896 | |
| 1897 | case Stmt::NullStmtClass: |
| 1898 | case Stmt::DeclStmtClass: |
| 1899 | return ESR_Succeeded; |
| 1900 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1901 | case Stmt::ReturnStmtClass: { |
| 1902 | CCValue CCResult; |
| 1903 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
| 1904 | if (!Evaluate(CCResult, Info, RetExpr) || |
| 1905 | !CheckConstantExpression(Info, RetExpr, CCResult, Result, |
| 1906 | CCEK_ReturnValue)) |
| 1907 | return ESR_Failed; |
| 1908 | return ESR_Returned; |
| 1909 | } |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1910 | |
| 1911 | case Stmt::CompoundStmtClass: { |
| 1912 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| 1913 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 1914 | BE = CS->body_end(); BI != BE; ++BI) { |
| 1915 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| 1916 | if (ESR != ESR_Succeeded) |
| 1917 | return ESR; |
| 1918 | } |
| 1919 | return ESR_Succeeded; |
| 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1924 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 1925 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 1926 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 1927 | /// so we need special handling. |
| 1928 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1929 | const CXXConstructorDecl *CD, |
| 1930 | bool IsValueInitialization) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1931 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 1932 | return false; |
| 1933 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 1934 | // Value-initialization does not call a trivial default constructor, so such a |
| 1935 | // call is a core constant expression whether or not the constructor is |
| 1936 | // constexpr. |
| 1937 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1938 | if (Info.getLangOpts().CPlusPlus0x) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 1939 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 1940 | // we should be much more explicit about why it's not constexpr. |
| 1941 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 1942 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 1943 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1944 | } else { |
| 1945 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 1946 | } |
| 1947 | } |
| 1948 | return true; |
| 1949 | } |
| 1950 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1951 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 1952 | /// expression. |
| 1953 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 1954 | const FunctionDecl *Declaration, |
| 1955 | const FunctionDecl *Definition) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1956 | // Potential constant expressions can contain calls to declared, but not yet |
| 1957 | // defined, constexpr functions. |
| 1958 | if (Info.CheckingPotentialConstantExpression && !Definition && |
| 1959 | Declaration->isConstexpr()) |
| 1960 | return false; |
| 1961 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1962 | // Can we evaluate this function call? |
| 1963 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 1964 | return true; |
| 1965 | |
| 1966 | if (Info.getLangOpts().CPlusPlus0x) { |
| 1967 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1968 | // FIXME: If DiagDecl is an implicitly-declared special member function, we |
| 1969 | // should be much more explicit about why it's not constexpr. |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1970 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 1971 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 1972 | << DiagDecl; |
| 1973 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 1974 | } else { |
| 1975 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 1976 | } |
| 1977 | return false; |
| 1978 | } |
| 1979 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1980 | namespace { |
Richard Smith | cd99b07 | 2011-11-11 05:48:57 +0000 | [diff] [blame] | 1981 | typedef SmallVector<CCValue, 8> ArgVector; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 1985 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 1986 | EvalInfo &Info) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1987 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1988 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1989 | I != E; ++I) { |
| 1990 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 1991 | // If we're checking for a potential constant expression, evaluate all |
| 1992 | // initializers even if some of them fail. |
| 1993 | if (!Info.keepEvaluatingAfterFailure()) |
| 1994 | return false; |
| 1995 | Success = false; |
| 1996 | } |
| 1997 | } |
| 1998 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2001 | /// Evaluate a function call. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2002 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 2003 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2004 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2005 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2006 | ArgVector ArgValues(Args.size()); |
| 2007 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 2008 | return false; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2009 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2010 | if (!Info.CheckCallLimit(CallLoc)) |
| 2011 | return false; |
| 2012 | |
| 2013 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2014 | return EvaluateStmt(Result, Info, Body) == ESR_Returned; |
| 2015 | } |
| 2016 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2017 | /// Evaluate a constructor call. |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2018 | static bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2019 | ArrayRef<const Expr*> Args, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2020 | const CXXConstructorDecl *Definition, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2021 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2022 | ArgVector ArgValues(Args.size()); |
| 2023 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 2024 | return false; |
| 2025 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2026 | if (!Info.CheckCallLimit(CallLoc)) |
| 2027 | return false; |
| 2028 | |
| 2029 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2030 | |
| 2031 | // If it's a delegating constructor, just delegate. |
| 2032 | if (Definition->isDelegatingConstructor()) { |
| 2033 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
| 2034 | return EvaluateConstantExpression(Result, Info, This, (*I)->getInit()); |
| 2035 | } |
| 2036 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2037 | // For a trivial copy or move constructor, perform an APValue copy. This is |
| 2038 | // essential for unions, where the operations performed by the constructor |
| 2039 | // cannot be represented by ctor-initializers. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2040 | const CXXRecordDecl *RD = Definition->getParent(); |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2041 | if (Definition->isDefaulted() && |
| 2042 | ((Definition->isCopyConstructor() && RD->hasTrivialCopyConstructor()) || |
| 2043 | (Definition->isMoveConstructor() && RD->hasTrivialMoveConstructor()))) { |
| 2044 | LValue RHS; |
| 2045 | RHS.setFrom(ArgValues[0]); |
| 2046 | CCValue Value; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2047 | if (!HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 2048 | RHS, Value)) |
| 2049 | return false; |
| 2050 | assert((Value.isStruct() || Value.isUnion()) && |
| 2051 | "trivial copy/move from non-class type?"); |
| 2052 | // Any CCValue of class type must already be a constant expression. |
| 2053 | Result = Value; |
| 2054 | return true; |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | // Reserve space for the struct members. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2058 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2059 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 2060 | std::distance(RD->field_begin(), RD->field_end())); |
| 2061 | |
| 2062 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2063 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2064 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2065 | unsigned BasesSeen = 0; |
| 2066 | #ifndef NDEBUG |
| 2067 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 2068 | #endif |
| 2069 | for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(), |
| 2070 | E = Definition->init_end(); I != E; ++I) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2071 | LValue Subobject = This; |
| 2072 | APValue *Value = &Result; |
| 2073 | |
| 2074 | // Determine the subobject to initialize. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2075 | if ((*I)->isBaseInitializer()) { |
| 2076 | QualType BaseType((*I)->getBaseClass(), 0); |
| 2077 | #ifndef NDEBUG |
| 2078 | // Non-virtual base classes are initialized in the order in the class |
| 2079 | // definition. We cannot have a virtual base class for a literal type. |
| 2080 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 2081 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 2082 | "base class initializers not in expected order"); |
| 2083 | ++BaseIt; |
| 2084 | #endif |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2085 | HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD, |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2086 | BaseType->getAsCXXRecordDecl(), &Layout); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2087 | Value = &Result.getStructBase(BasesSeen++); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2088 | } else if (FieldDecl *FD = (*I)->getMember()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2089 | HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2090 | if (RD->isUnion()) { |
| 2091 | Result = APValue(FD); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2092 | Value = &Result.getUnionValue(); |
| 2093 | } else { |
| 2094 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 2095 | } |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2096 | } else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2097 | // Walk the indirect field decl's chain to find the object to initialize, |
| 2098 | // and make sure we've initialized every step along it. |
| 2099 | for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(), |
| 2100 | CE = IFD->chain_end(); |
| 2101 | C != CE; ++C) { |
| 2102 | FieldDecl *FD = cast<FieldDecl>(*C); |
| 2103 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 2104 | // Switch the union field if it differs. This happens if we had |
| 2105 | // preceding zero-initialization, and we're now initializing a union |
| 2106 | // subobject other than the first. |
| 2107 | // FIXME: In this case, the values of the other subobjects are |
| 2108 | // specified, since zero-initialization sets all padding bits to zero. |
| 2109 | if (Value->isUninit() || |
| 2110 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 2111 | if (CD->isUnion()) |
| 2112 | *Value = APValue(FD); |
| 2113 | else |
| 2114 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
| 2115 | std::distance(CD->field_begin(), CD->field_end())); |
| 2116 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2117 | HandleLValueMember(Info, (*I)->getInit(), Subobject, FD); |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2118 | if (CD->isUnion()) |
| 2119 | Value = &Value->getUnionValue(); |
| 2120 | else |
| 2121 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2122 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2123 | } else { |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2124 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2125 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2126 | |
| 2127 | if (!EvaluateConstantExpression(*Value, Info, Subobject, (*I)->getInit(), |
| 2128 | (*I)->isBaseInitializer() |
| 2129 | ? CCEK_Constant : CCEK_MemberInit)) { |
| 2130 | // If we're checking for a potential constant expression, evaluate all |
| 2131 | // initializers even if some of them fail. |
| 2132 | if (!Info.keepEvaluatingAfterFailure()) |
| 2133 | return false; |
| 2134 | Success = false; |
| 2135 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2138 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2141 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2142 | class HasSideEffect |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2143 | : public ConstStmtVisitor<HasSideEffect, bool> { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2144 | const ASTContext &Ctx; |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2145 | public: |
| 2146 | |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2147 | HasSideEffect(const ASTContext &C) : Ctx(C) {} |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2148 | |
| 2149 | // Unhandled nodes conservatively default to having side effects. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2150 | bool VisitStmt(const Stmt *S) { |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2151 | return true; |
| 2152 | } |
| 2153 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2154 | bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); } |
| 2155 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2156 | return Visit(E->getResultExpr()); |
| 2157 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2158 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2159 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2160 | return true; |
| 2161 | return false; |
| 2162 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2163 | bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2164 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2165 | return true; |
| 2166 | return false; |
| 2167 | } |
| 2168 | bool VisitBlockDeclRefExpr (const BlockDeclRefExpr *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2169 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2170 | return true; |
| 2171 | return false; |
| 2172 | } |
| 2173 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2174 | // We don't want to evaluate BlockExprs multiple times, as they generate |
| 2175 | // a ton of code. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2176 | bool VisitBlockExpr(const BlockExpr *E) { return true; } |
| 2177 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; } |
| 2178 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2179 | { return Visit(E->getInitializer()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2180 | bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); } |
| 2181 | bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; } |
| 2182 | bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; } |
| 2183 | bool VisitStringLiteral(const StringLiteral *E) { return false; } |
| 2184 | bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; } |
| 2185 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2186 | { return false; } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2187 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 2188 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2189 | bool VisitChooseExpr(const ChooseExpr *E) |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2190 | { return Visit(E->getChosenSubExpr(Ctx)); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2191 | bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); } |
| 2192 | bool VisitBinAssign(const BinaryOperator *E) { return true; } |
| 2193 | bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; } |
| 2194 | bool VisitBinaryOperator(const BinaryOperator *E) |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 2195 | { return Visit(E->getLHS()) || Visit(E->getRHS()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2196 | bool VisitUnaryPreInc(const UnaryOperator *E) { return true; } |
| 2197 | bool VisitUnaryPostInc(const UnaryOperator *E) { return true; } |
| 2198 | bool VisitUnaryPreDec(const UnaryOperator *E) { return true; } |
| 2199 | bool VisitUnaryPostDec(const UnaryOperator *E) { return true; } |
| 2200 | bool VisitUnaryDeref(const UnaryOperator *E) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2201 | if (Ctx.getCanonicalType(E->getType()).isVolatileQualified()) |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2202 | return true; |
Mike Stump | 980ca22 | 2009-10-29 20:48:09 +0000 | [diff] [blame] | 2203 | return Visit(E->getSubExpr()); |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2204 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2205 | bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); } |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 2206 | |
| 2207 | // Has side effects if any element does. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2208 | bool VisitInitListExpr(const InitListExpr *E) { |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 2209 | for (unsigned i = 0, e = E->getNumInits(); i != e; ++i) |
| 2210 | if (Visit(E->getInit(i))) return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2211 | if (const Expr *filler = E->getArrayFiller()) |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 2212 | return Visit(filler); |
Chris Lattner | 363ff23 | 2010-04-13 17:34:23 +0000 | [diff] [blame] | 2213 | return false; |
| 2214 | } |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 2215 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2216 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2217 | }; |
| 2218 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2219 | class OpaqueValueEvaluation { |
| 2220 | EvalInfo &info; |
| 2221 | OpaqueValueExpr *opaqueValue; |
| 2222 | |
| 2223 | public: |
| 2224 | OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue, |
| 2225 | Expr *value) |
| 2226 | : info(info), opaqueValue(opaqueValue) { |
| 2227 | |
| 2228 | // If evaluation fails, fail immediately. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2229 | if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) { |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2230 | this->opaqueValue = 0; |
| 2231 | return; |
| 2232 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | bool hasError() const { return opaqueValue == 0; } |
| 2236 | |
| 2237 | ~OpaqueValueEvaluation() { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 2238 | // FIXME: This will not work for recursive constexpr functions using opaque |
| 2239 | // values. Restore the former value. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2240 | if (opaqueValue) info.OpaqueValues.erase(opaqueValue); |
| 2241 | } |
| 2242 | }; |
| 2243 | |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 2244 | } // end anonymous namespace |
| 2245 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2246 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2247 | // Generic Evaluation |
| 2248 | //===----------------------------------------------------------------------===// |
| 2249 | namespace { |
| 2250 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2251 | // FIXME: RetTy is always bool. Remove it. |
| 2252 | template <class Derived, typename RetTy=bool> |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2253 | class ExprEvaluatorBase |
| 2254 | : public ConstStmtVisitor<Derived, RetTy> { |
| 2255 | private: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2256 | RetTy DerivedSuccess(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2257 | return static_cast<Derived*>(this)->Success(V, E); |
| 2258 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2259 | RetTy DerivedZeroInitialization(const Expr *E) { |
| 2260 | return static_cast<Derived*>(this)->ZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2261 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2262 | |
| 2263 | protected: |
| 2264 | EvalInfo &Info; |
| 2265 | typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy; |
| 2266 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 2267 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 2268 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | d509342 | 2011-12-12 09:41:58 +0000 | [diff] [blame] | 2269 | return Info.CCEDiag(E->getExprLoc(), D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | /// Report an evaluation error. This should only be called when an error is |
| 2273 | /// first discovered. When propagating an error, just return false. |
| 2274 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 2275 | Info.Diag(E->getExprLoc(), D); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2276 | return false; |
| 2277 | } |
| 2278 | bool Error(const Expr *E) { |
| 2279 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 2280 | } |
| 2281 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2282 | RetTy ZeroInitialization(const Expr *E) { return Error(E); } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2283 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2284 | public: |
| 2285 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 2286 | |
| 2287 | RetTy VisitStmt(const Stmt *) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2288 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2289 | } |
| 2290 | RetTy VisitExpr(const Expr *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2291 | return Error(E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | RetTy VisitParenExpr(const ParenExpr *E) |
| 2295 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2296 | RetTy VisitUnaryExtension(const UnaryOperator *E) |
| 2297 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2298 | RetTy VisitUnaryPlus(const UnaryOperator *E) |
| 2299 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| 2300 | RetTy VisitChooseExpr(const ChooseExpr *E) |
| 2301 | { return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); } |
| 2302 | RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| 2303 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 2304 | RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| 2305 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Richard Smith | 3d75ca8 | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 2306 | RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
| 2307 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Richard Smith | bc6abe9 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 2308 | // We cannot create any objects for which cleanups are required, so there is |
| 2309 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
| 2310 | RetTy VisitExprWithCleanups(const ExprWithCleanups *E) |
| 2311 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2312 | |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2313 | RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
| 2314 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 2315 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 2316 | } |
| 2317 | RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| 2318 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 2319 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 2320 | } |
| 2321 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2322 | RetTy VisitBinaryOperator(const BinaryOperator *E) { |
| 2323 | switch (E->getOpcode()) { |
| 2324 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2325 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2326 | |
| 2327 | case BO_Comma: |
| 2328 | VisitIgnoredValue(E->getLHS()); |
| 2329 | return StmtVisitorTy::Visit(E->getRHS()); |
| 2330 | |
| 2331 | case BO_PtrMemD: |
| 2332 | case BO_PtrMemI: { |
| 2333 | LValue Obj; |
| 2334 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 2335 | return false; |
| 2336 | CCValue Result; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2337 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2338 | return false; |
| 2339 | return DerivedSuccess(Result, E); |
| 2340 | } |
| 2341 | } |
| 2342 | } |
| 2343 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2344 | RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
| 2345 | OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon()); |
| 2346 | if (opaque.hasError()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2347 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2348 | |
| 2349 | bool cond; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2350 | if (!EvaluateAsBooleanCondition(E->getCond(), cond, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2351 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2352 | |
| 2353 | return StmtVisitorTy::Visit(cond ? E->getTrueExpr() : E->getFalseExpr()); |
| 2354 | } |
| 2355 | |
| 2356 | RetTy VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2357 | bool IsBcpCall = false; |
| 2358 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 2359 | // the result is a constant expression if it can be folded without |
| 2360 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 2361 | // for discussion. |
| 2362 | if (const CallExpr *CallCE = |
| 2363 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
| 2364 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 2365 | IsBcpCall = true; |
| 2366 | |
| 2367 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 2368 | // constant expression; we can't check whether it's potentially foldable. |
| 2369 | if (Info.CheckingPotentialConstantExpression && IsBcpCall) |
| 2370 | return false; |
| 2371 | |
| 2372 | FoldConstant Fold(Info); |
| 2373 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2374 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2375 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2376 | return false; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2377 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2378 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2379 | if (!StmtVisitorTy::Visit(EvalExpr)) |
| 2380 | return false; |
| 2381 | |
| 2382 | if (IsBcpCall) |
| 2383 | Fold.Fold(Info); |
| 2384 | |
| 2385 | return true; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2389 | const CCValue *Value = Info.getOpaqueValue(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2390 | if (!Value) { |
| 2391 | const Expr *Source = E->getSourceExpr(); |
| 2392 | if (!Source) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2393 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2394 | if (Source == E) { // sanity checking. |
| 2395 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2396 | return Error(E); |
Argyrios Kyrtzidis | 4278683 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 2397 | } |
| 2398 | return StmtVisitorTy::Visit(Source); |
| 2399 | } |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2400 | return DerivedSuccess(*Value, E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2401 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2402 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2403 | RetTy VisitCallExpr(const CallExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2404 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2405 | QualType CalleeType = Callee->getType(); |
| 2406 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2407 | const FunctionDecl *FD = 0; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2408 | LValue *This = 0, ThisVal; |
| 2409 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 6c95787 | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 2410 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2411 | // Extract function decl and 'this' pointer from the callee. |
| 2412 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2413 | const ValueDecl *Member = 0; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2414 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 2415 | // Explicit bound member calls, such as x.f() or p->g(); |
| 2416 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2417 | return false; |
| 2418 | Member = ME->getMemberDecl(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2419 | This = &ThisVal; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2420 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 2421 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2422 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 2423 | if (!Member) return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2424 | This = &ThisVal; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2425 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2426 | return Error(Callee); |
| 2427 | |
| 2428 | FD = dyn_cast<FunctionDecl>(Member); |
| 2429 | if (!FD) |
| 2430 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2431 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2432 | LValue Call; |
| 2433 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2434 | return false; |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2435 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2436 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2437 | return Error(Callee); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2438 | FD = dyn_cast_or_null<FunctionDecl>( |
| 2439 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2440 | if (!FD) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2441 | return Error(Callee); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2442 | |
| 2443 | // Overloaded operator calls to member functions are represented as normal |
| 2444 | // calls with '*this' as the first argument. |
| 2445 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 2446 | if (MD && !MD->isStatic()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2447 | // FIXME: When selecting an implicit conversion for an overloaded |
| 2448 | // operator delete, we sometimes try to evaluate calls to conversion |
| 2449 | // operators without a 'this' parameter! |
| 2450 | if (Args.empty()) |
| 2451 | return Error(E); |
| 2452 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2453 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 2454 | return false; |
| 2455 | This = &ThisVal; |
| 2456 | Args = Args.slice(1); |
| 2457 | } |
| 2458 | |
| 2459 | // Don't call function pointers which have been cast to some other type. |
| 2460 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2461 | return Error(E); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2462 | } else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2463 | return Error(E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2464 | |
Richard Smith | b04035a | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 2465 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 2466 | return false; |
| 2467 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2468 | const FunctionDecl *Definition = 0; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2469 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 2470 | APValue Result; |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2471 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2472 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2473 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, |
| 2474 | Info, Result)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2475 | return false; |
| 2476 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2477 | return DerivedSuccess(CCValue(Info.Ctx, Result, CCValue::GlobalValue()), E); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2480 | RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| 2481 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 2482 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2483 | RetTy VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 71523d6 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 2484 | if (E->getNumInits() == 0) |
| 2485 | return DerivedZeroInitialization(E); |
| 2486 | if (E->getNumInits() == 1) |
| 2487 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2488 | return Error(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2489 | } |
| 2490 | RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2491 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2492 | } |
| 2493 | RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2494 | return DerivedZeroInitialization(E); |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2495 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2496 | RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2497 | return DerivedZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2498 | } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2499 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2500 | /// A member expression where the object is a prvalue is itself a prvalue. |
| 2501 | RetTy VisitMemberExpr(const MemberExpr *E) { |
| 2502 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 2503 | |
| 2504 | CCValue Val; |
| 2505 | if (!Evaluate(Val, Info, E->getBase())) |
| 2506 | return false; |
| 2507 | |
| 2508 | QualType BaseTy = E->getBase()->getType(); |
| 2509 | |
| 2510 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2511 | if (!FD) return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2512 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
| 2513 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 2514 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 2515 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2516 | SubobjectDesignator Designator(BaseTy); |
| 2517 | Designator.addDeclUnchecked(FD); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2518 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2519 | return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2520 | DerivedSuccess(Val, E); |
| 2521 | } |
| 2522 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2523 | RetTy VisitCastExpr(const CastExpr *E) { |
| 2524 | switch (E->getCastKind()) { |
| 2525 | default: |
| 2526 | break; |
| 2527 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 2528 | case CK_AtomicToNonAtomic: |
| 2529 | case CK_NonAtomicToAtomic: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2530 | case CK_NoOp: |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 2531 | case CK_UserDefinedConversion: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2532 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 2533 | |
| 2534 | case CK_LValueToRValue: { |
| 2535 | LValue LVal; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2536 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 2537 | return false; |
| 2538 | CCValue RVal; |
Richard Smith | 9ec7197 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 2539 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
| 2540 | if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
| 2541 | LVal, RVal)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2542 | return false; |
| 2543 | return DerivedSuccess(RVal, E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2544 | } |
| 2545 | } |
| 2546 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2547 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2550 | /// Visit a value which is evaluated, but whose value is ignored. |
| 2551 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2552 | CCValue Scratch; |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 2553 | if (!Evaluate(Scratch, Info, E)) |
| 2554 | Info.EvalStatus.HasSideEffects = true; |
| 2555 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2556 | }; |
| 2557 | |
| 2558 | } |
| 2559 | |
| 2560 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2561 | // Common base class for lvalue and temporary evaluation. |
| 2562 | //===----------------------------------------------------------------------===// |
| 2563 | namespace { |
| 2564 | template<class Derived> |
| 2565 | class LValueExprEvaluatorBase |
| 2566 | : public ExprEvaluatorBase<Derived, bool> { |
| 2567 | protected: |
| 2568 | LValue &Result; |
| 2569 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| 2570 | typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy; |
| 2571 | |
| 2572 | bool Success(APValue::LValueBase B) { |
| 2573 | Result.set(B); |
| 2574 | return true; |
| 2575 | } |
| 2576 | |
| 2577 | public: |
| 2578 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 2579 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 2580 | |
| 2581 | bool Success(const CCValue &V, const Expr *E) { |
| 2582 | Result.setFrom(V); |
| 2583 | return true; |
| 2584 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2585 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2586 | bool VisitMemberExpr(const MemberExpr *E) { |
| 2587 | // Handle non-static data members. |
| 2588 | QualType BaseTy; |
| 2589 | if (E->isArrow()) { |
| 2590 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 2591 | return false; |
| 2592 | BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2593 | } else if (E->getBase()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 2594 | assert(E->getBase()->getType()->isRecordType()); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2595 | if (!EvaluateTemporary(E->getBase(), Result, this->Info)) |
| 2596 | return false; |
| 2597 | BaseTy = E->getBase()->getType(); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2598 | } else { |
| 2599 | if (!this->Visit(E->getBase())) |
| 2600 | return false; |
| 2601 | BaseTy = E->getBase()->getType(); |
| 2602 | } |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2603 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2604 | const ValueDecl *MD = E->getMemberDecl(); |
| 2605 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 2606 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 2607 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 2608 | (void)BaseTy; |
| 2609 | HandleLValueMember(this->Info, E, Result, FD); |
| 2610 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
| 2611 | HandleLValueIndirectMember(this->Info, E, Result, IFD); |
| 2612 | } else |
| 2613 | return this->Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2614 | |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2615 | if (MD->getType()->isReferenceType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2616 | CCValue RefValue; |
Richard Smith | d9b02e7 | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2617 | if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2618 | RefValue)) |
| 2619 | return false; |
| 2620 | return Success(RefValue, E); |
| 2621 | } |
| 2622 | return true; |
| 2623 | } |
| 2624 | |
| 2625 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 2626 | switch (E->getOpcode()) { |
| 2627 | default: |
| 2628 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 2629 | |
| 2630 | case BO_PtrMemD: |
| 2631 | case BO_PtrMemI: |
| 2632 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | bool VisitCastExpr(const CastExpr *E) { |
| 2637 | switch (E->getCastKind()) { |
| 2638 | default: |
| 2639 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 2640 | |
| 2641 | case CK_DerivedToBase: |
| 2642 | case CK_UncheckedDerivedToBase: { |
| 2643 | if (!this->Visit(E->getSubExpr())) |
| 2644 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2645 | |
| 2646 | // Now figure out the necessary offset to add to the base LV to get from |
| 2647 | // the derived class to the base class. |
| 2648 | QualType Type = E->getSubExpr()->getType(); |
| 2649 | |
| 2650 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2651 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2652 | if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(), |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2653 | *PathI)) |
| 2654 | return false; |
| 2655 | Type = (*PathI)->getType(); |
| 2656 | } |
| 2657 | |
| 2658 | return true; |
| 2659 | } |
| 2660 | } |
| 2661 | } |
| 2662 | }; |
| 2663 | } |
| 2664 | |
| 2665 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2666 | // LValue Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2667 | // |
| 2668 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 2669 | // function designators (in C), decl references to void objects (in C), and |
| 2670 | // temporaries (if building with -Wno-address-of-temporary). |
| 2671 | // |
| 2672 | // LValue evaluation produces values comprising a base expression of one of the |
| 2673 | // following types: |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2674 | // - Declarations |
| 2675 | // * VarDecl |
| 2676 | // * FunctionDecl |
| 2677 | // - Literals |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2678 | // * CompoundLiteralExpr in C |
| 2679 | // * StringLiteral |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2680 | // * CXXTypeidExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2681 | // * PredefinedExpr |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2682 | // * ObjCStringLiteralExpr |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2683 | // * ObjCEncodeExpr |
| 2684 | // * AddrLabelExpr |
| 2685 | // * BlockExpr |
| 2686 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2687 | // - Locals and temporaries |
| 2688 | // * Any Expr, with a Frame indicating the function in which the temporary was |
| 2689 | // evaluated. |
| 2690 | // plus an offset in bytes. |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2691 | //===----------------------------------------------------------------------===// |
| 2692 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2693 | class LValueExprEvaluator |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2694 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2695 | public: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2696 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 2697 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2698 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2699 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
| 2700 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2701 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 2702 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2703 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2704 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 2705 | bool VisitMemberExpr(const MemberExpr *E); |
| 2706 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 2707 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2708 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2709 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 2710 | bool VisitUnaryDeref(const UnaryOperator *E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2711 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2712 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2713 | switch (E->getCastKind()) { |
| 2714 | default: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2715 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2716 | |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2717 | case CK_LValueBitCast: |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2718 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2719 | if (!Visit(E->getSubExpr())) |
| 2720 | return false; |
| 2721 | Result.Designator.setInvalid(); |
| 2722 | return true; |
Eli Friedman | db92422 | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 2723 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2724 | case CK_BaseToDerived: |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2725 | if (!Visit(E->getSubExpr())) |
| 2726 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2727 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | 26bc220 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 2728 | } |
| 2729 | } |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 2730 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 2731 | // FIXME: Missing: __real__, __imag__ |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2732 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2733 | }; |
| 2734 | } // end anonymous namespace |
| 2735 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2736 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
| 2737 | /// expressions which are not glvalues, in a few cases: |
| 2738 | /// * function designators in C, |
| 2739 | /// * "extern void" objects, |
| 2740 | /// * temporaries, if building with -Wno-address-of-temporary. |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2741 | static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2742 | assert((E->isGLValue() || E->getType()->isFunctionType() || |
| 2743 | E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) && |
| 2744 | "can't evaluate expression as an lvalue"); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2745 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2748 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2749 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| 2750 | return Success(FD); |
| 2751 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2752 | return VisitVarDecl(E, VD); |
| 2753 | return Error(E); |
| 2754 | } |
Richard Smith | 436c889 | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 2755 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2756 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2757 | if (!VD->getType()->isReferenceType()) { |
| 2758 | if (isa<ParmVarDecl>(VD)) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2759 | Result.set(VD, Info.CurrentCall); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2760 | return true; |
| 2761 | } |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2762 | return Success(VD); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2763 | } |
Eli Friedman | 50c39ea | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 2764 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2765 | CCValue V; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2766 | if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V)) |
| 2767 | return false; |
| 2768 | return Success(V, E); |
Anders Carlsson | 35873c4 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2771 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 2772 | const MaterializeTemporaryExpr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2773 | if (E->GetTemporaryExpr()->isRValue()) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 2774 | if (E->getType()->isRecordType()) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2775 | return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info); |
| 2776 | |
| 2777 | Result.set(E, Info.CurrentCall); |
| 2778 | return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, |
| 2779 | Result, E->GetTemporaryExpr()); |
| 2780 | } |
| 2781 | |
| 2782 | // Materialization of an lvalue temporary occurs when we need to force a copy |
| 2783 | // (for instance, if it's a bitfield). |
| 2784 | // FIXME: The AST should contain an lvalue-to-rvalue node for such cases. |
| 2785 | if (!Visit(E->GetTemporaryExpr())) |
| 2786 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2787 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2788 | Info.CurrentCall->Temporaries[E])) |
| 2789 | return false; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2790 | Result.set(E, Info.CurrentCall); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2791 | return true; |
Richard Smith | bd552ef | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2794 | bool |
| 2795 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2796 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 2797 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 2798 | // 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] | 2799 | return Success(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2800 | } |
| 2801 | |
Richard Smith | 47d2145 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 2802 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
| 2803 | if (E->isTypeOperand()) |
| 2804 | return Success(E); |
| 2805 | CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl(); |
| 2806 | if (RD && RD->isPolymorphic()) { |
| 2807 | Info.Diag(E->getExprLoc(), diag::note_constexpr_typeid_polymorphic) |
| 2808 | << E->getExprOperand()->getType() |
| 2809 | << E->getExprOperand()->getSourceRange(); |
| 2810 | return false; |
| 2811 | } |
| 2812 | return Success(E); |
| 2813 | } |
| 2814 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2815 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2816 | // Handle static data members. |
| 2817 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 2818 | VisitIgnoredValue(E->getBase()); |
| 2819 | return VisitVarDecl(E, VD); |
| 2820 | } |
| 2821 | |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2822 | // Handle static member functions. |
| 2823 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 2824 | if (MD->isStatic()) { |
| 2825 | VisitIgnoredValue(E->getBase()); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2826 | return Success(MD); |
Richard Smith | d0dccea | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2827 | } |
| 2828 | } |
| 2829 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2830 | // Handle non-static data members. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2831 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2834 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2835 | // FIXME: Deal with vectors as array subscript bases. |
| 2836 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2837 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2838 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2839 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2840 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2841 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2842 | APSInt Index; |
| 2843 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2844 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2845 | int64_t IndexValue |
| 2846 | = Index.isSigned() ? Index.getSExtValue() |
| 2847 | : static_cast<int64_t>(Index.getZExtValue()); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2848 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2849 | return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 2850 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2851 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2852 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2853 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | e8761c8 | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2856 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2857 | // Pointer Evaluation |
| 2858 | //===----------------------------------------------------------------------===// |
| 2859 | |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 2860 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 2861 | class PointerExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2862 | : public ExprEvaluatorBase<PointerExprEvaluator, bool> { |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2863 | LValue &Result; |
| 2864 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2865 | bool Success(const Expr *E) { |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2866 | Result.set(E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2867 | return true; |
| 2868 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2869 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2871 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2872 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2873 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2874 | bool Success(const CCValue &V, const Expr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2875 | Result.setFrom(V); |
| 2876 | return true; |
| 2877 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2878 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 2879 | return Success((Expr*)0); |
| 2880 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 2881 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2882 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2883 | bool VisitCastExpr(const CastExpr* E); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2884 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2885 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2886 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2887 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2888 | { return Success(E); } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2889 | bool VisitCallExpr(const CallExpr *E); |
| 2890 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2891 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2892 | return Success(E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2893 | return Error(E); |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 2894 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2895 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
| 2896 | if (!Info.CurrentCall->This) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2897 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2898 | Result = *Info.CurrentCall->This; |
| 2899 | return true; |
| 2900 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2901 | |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 2902 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2903 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2904 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 2905 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2906 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2907 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2908 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2911 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2912 | if (E->getOpcode() != BO_Add && |
| 2913 | E->getOpcode() != BO_Sub) |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2914 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2915 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2916 | const Expr *PExp = E->getLHS(); |
| 2917 | const Expr *IExp = E->getRHS(); |
| 2918 | if (IExp->getType()->isPointerType()) |
| 2919 | std::swap(PExp, IExp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2920 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2921 | bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); |
| 2922 | if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2923 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2924 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2925 | llvm::APSInt Offset; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2926 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2927 | return false; |
| 2928 | int64_t AdditionalOffset |
| 2929 | = Offset.isSigned() ? Offset.getSExtValue() |
| 2930 | : static_cast<int64_t>(Offset.getZExtValue()); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2931 | if (E->getOpcode() == BO_Sub) |
| 2932 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2933 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2934 | QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType(); |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2935 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, |
| 2936 | AdditionalOffset); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2937 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2938 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 2939 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 2940 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2941 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2942 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 2943 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 2944 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 2945 | |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2946 | switch (E->getCastKind()) { |
| 2947 | default: |
| 2948 | break; |
| 2949 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2950 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2951 | case CK_CPointerToObjCPointerCast: |
| 2952 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2953 | case CK_AnyPointerToBlockPointerCast: |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 2954 | if (!Visit(SubExpr)) |
| 2955 | return false; |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 2956 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 2957 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 2958 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 2959 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | 28c1ce7 | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 2960 | Result.Designator.setInvalid(); |
Richard Smith | 4cd9b8f | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 2961 | if (SubExpr->getType()->isVoidPointerType()) |
| 2962 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 2963 | << 3 << SubExpr->getType(); |
| 2964 | else |
| 2965 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 2966 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2967 | return true; |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 2968 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2969 | case CK_DerivedToBase: |
| 2970 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2971 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2972 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2973 | if (!Result.Base && Result.Offset.isZero()) |
| 2974 | return true; |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2975 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2976 | // 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] | 2977 | // the derived class to the base class. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2978 | QualType Type = |
| 2979 | E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2980 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2981 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2982 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2983 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 2984 | *PathI)) |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2985 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2986 | Type = (*PathI)->getType(); |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
Anders Carlsson | 5c5a764 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 2989 | return true; |
| 2990 | } |
| 2991 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2992 | case CK_BaseToDerived: |
| 2993 | if (!Visit(E->getSubExpr())) |
| 2994 | return false; |
| 2995 | if (!Result.Base && Result.Offset.isZero()) |
| 2996 | return true; |
| 2997 | return HandleBaseToDerivedCast(Info, E, Result); |
| 2998 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2999 | case CK_NullToPointer: |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3000 | return ZeroInitialization(E); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3001 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3002 | case CK_IntegralToPointer: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 3003 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 3004 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3005 | CCValue Value; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3006 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 09a8a0e | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 3007 | break; |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3008 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3009 | if (Value.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3010 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 3011 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3012 | Result.Base = (Expr*)0; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3013 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 3014 | Result.Frame = 0; |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3015 | Result.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3016 | return true; |
| 3017 | } else { |
| 3018 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3019 | Result.setFrom(Value); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3020 | return true; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3021 | } |
| 3022 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3023 | case CK_ArrayToPointerDecay: |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3024 | if (SubExpr->isGLValue()) { |
| 3025 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 3026 | return false; |
| 3027 | } else { |
| 3028 | Result.set(SubExpr, Info.CurrentCall); |
| 3029 | if (!EvaluateConstantExpression(Info.CurrentCall->Temporaries[SubExpr], |
| 3030 | Info, Result, SubExpr)) |
| 3031 | return false; |
| 3032 | } |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3033 | // The result is a pointer to the first element of the array. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3034 | if (const ConstantArrayType *CAT |
| 3035 | = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) |
| 3036 | Result.addArray(Info, E, CAT); |
| 3037 | else |
| 3038 | Result.Designator.setInvalid(); |
Richard Smith | 0a3bdb6 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3039 | return true; |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 3040 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3041 | case CK_FunctionToPointerDecay: |
Richard Smith | 6a7c94a | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 3042 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3045 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3047 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3048 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3049 | if (IsStringLiteralCall(E)) |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 3050 | return Success(E); |
Eli Friedman | 3941b18 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 3051 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3052 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3053 | } |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3054 | |
| 3055 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3056 | // Member Pointer Evaluation |
| 3057 | //===----------------------------------------------------------------------===// |
| 3058 | |
| 3059 | namespace { |
| 3060 | class MemberPointerExprEvaluator |
| 3061 | : public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> { |
| 3062 | MemberPtr &Result; |
| 3063 | |
| 3064 | bool Success(const ValueDecl *D) { |
| 3065 | Result = MemberPtr(D); |
| 3066 | return true; |
| 3067 | } |
| 3068 | public: |
| 3069 | |
| 3070 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 3071 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 3072 | |
| 3073 | bool Success(const CCValue &V, const Expr *E) { |
| 3074 | Result.setFrom(V); |
| 3075 | return true; |
| 3076 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3077 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3078 | return Success((const ValueDecl*)0); |
| 3079 | } |
| 3080 | |
| 3081 | bool VisitCastExpr(const CastExpr *E); |
| 3082 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 3083 | }; |
| 3084 | } // end anonymous namespace |
| 3085 | |
| 3086 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 3087 | EvalInfo &Info) { |
| 3088 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 3089 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 3090 | } |
| 3091 | |
| 3092 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3093 | switch (E->getCastKind()) { |
| 3094 | default: |
| 3095 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3096 | |
| 3097 | case CK_NullToMemberPointer: |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3098 | return ZeroInitialization(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3099 | |
| 3100 | case CK_BaseToDerivedMemberPointer: { |
| 3101 | if (!Visit(E->getSubExpr())) |
| 3102 | return false; |
| 3103 | if (E->path_empty()) |
| 3104 | return true; |
| 3105 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 3106 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 3107 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 3108 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 3109 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 3110 | PathI != PathE; ++PathI) { |
| 3111 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 3112 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3113 | if (!Result.castToDerived(Derived)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3114 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3115 | } |
| 3116 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 3117 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3118 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3119 | return true; |
| 3120 | } |
| 3121 | |
| 3122 | case CK_DerivedToBaseMemberPointer: |
| 3123 | if (!Visit(E->getSubExpr())) |
| 3124 | return false; |
| 3125 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3126 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 3127 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 3128 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3129 | if (!Result.castToBase(Base)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3130 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3131 | } |
| 3132 | return true; |
| 3133 | } |
| 3134 | } |
| 3135 | |
| 3136 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 3137 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 3138 | // member can be formed. |
| 3139 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 3140 | } |
| 3141 | |
| 3142 | //===----------------------------------------------------------------------===// |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3143 | // Record Evaluation |
| 3144 | //===----------------------------------------------------------------------===// |
| 3145 | |
| 3146 | namespace { |
| 3147 | class RecordExprEvaluator |
| 3148 | : public ExprEvaluatorBase<RecordExprEvaluator, bool> { |
| 3149 | const LValue &This; |
| 3150 | APValue &Result; |
| 3151 | public: |
| 3152 | |
| 3153 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 3154 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 3155 | |
| 3156 | bool Success(const CCValue &V, const Expr *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3157 | return CheckConstantExpression(Info, E, V, Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3158 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3159 | bool ZeroInitialization(const Expr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3160 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3161 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3162 | bool VisitInitListExpr(const InitListExpr *E); |
| 3163 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
| 3164 | }; |
| 3165 | } |
| 3166 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3167 | /// Perform zero-initialization on an object of non-union class type. |
| 3168 | /// C++11 [dcl.init]p5: |
| 3169 | /// To zero-initialize an object or reference of type T means: |
| 3170 | /// [...] |
| 3171 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 3172 | /// each non-static data member and each base-class subobject is |
| 3173 | /// zero-initialized |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3174 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 3175 | const RecordDecl *RD, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3176 | const LValue &This, APValue &Result) { |
| 3177 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 3178 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 3179 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
| 3180 | std::distance(RD->field_begin(), RD->field_end())); |
| 3181 | |
| 3182 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3183 | |
| 3184 | if (CD) { |
| 3185 | unsigned Index = 0; |
| 3186 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3187 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3188 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 3189 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3190 | HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout); |
| 3191 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3192 | Result.getStructBase(Index))) |
| 3193 | return false; |
| 3194 | } |
| 3195 | } |
| 3196 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3197 | for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end(); |
| 3198 | I != End; ++I) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3199 | // -- if T is a reference type, no initialization is performed. |
| 3200 | if ((*I)->getType()->isReferenceType()) |
| 3201 | continue; |
| 3202 | |
| 3203 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3204 | HandleLValueMember(Info, E, Subobject, *I, &Layout); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3205 | |
| 3206 | ImplicitValueInitExpr VIE((*I)->getType()); |
| 3207 | if (!EvaluateConstantExpression( |
| 3208 | Result.getStructField((*I)->getFieldIndex()), Info, Subobject, &VIE)) |
| 3209 | return false; |
| 3210 | } |
| 3211 | |
| 3212 | return true; |
| 3213 | } |
| 3214 | |
| 3215 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E) { |
| 3216 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
| 3217 | if (RD->isUnion()) { |
| 3218 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 3219 | // object's first non-static named data member is zero-initialized |
| 3220 | RecordDecl::field_iterator I = RD->field_begin(); |
| 3221 | if (I == RD->field_end()) { |
| 3222 | Result = APValue((const FieldDecl*)0); |
| 3223 | return true; |
| 3224 | } |
| 3225 | |
| 3226 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3227 | HandleLValueMember(Info, E, Subobject, *I); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3228 | Result = APValue(*I); |
| 3229 | ImplicitValueInitExpr VIE((*I)->getType()); |
| 3230 | return EvaluateConstantExpression(Result.getUnionValue(), Info, |
| 3231 | Subobject, &VIE); |
| 3232 | } |
| 3233 | |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3234 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3237 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 3238 | switch (E->getCastKind()) { |
| 3239 | default: |
| 3240 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3241 | |
| 3242 | case CK_ConstructorConversion: |
| 3243 | return Visit(E->getSubExpr()); |
| 3244 | |
| 3245 | case CK_DerivedToBase: |
| 3246 | case CK_UncheckedDerivedToBase: { |
| 3247 | CCValue DerivedObject; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3248 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3249 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3250 | if (!DerivedObject.isStruct()) |
| 3251 | return Error(E->getSubExpr()); |
Richard Smith | 59efe26 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3252 | |
| 3253 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 3254 | APValue *Value = &DerivedObject; |
| 3255 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 3256 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 3257 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 3258 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 3259 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 3260 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 3261 | RD = Base; |
| 3262 | } |
| 3263 | Result = *Value; |
| 3264 | return true; |
| 3265 | } |
| 3266 | } |
| 3267 | } |
| 3268 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3269 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 3270 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
| 3271 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3272 | |
| 3273 | if (RD->isUnion()) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3274 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 3275 | Result = APValue(Field); |
| 3276 | if (!Field) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3277 | return true; |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3278 | |
| 3279 | // If the initializer list for a union does not contain any elements, the |
| 3280 | // first element of the union is value-initialized. |
| 3281 | ImplicitValueInitExpr VIE(Field->getType()); |
| 3282 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 3283 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3284 | LValue Subobject = This; |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3285 | HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3286 | return EvaluateConstantExpression(Result.getUnionValue(), Info, |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3287 | Subobject, InitExpr); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 3291 | "initializer list for class with base classes"); |
| 3292 | Result = APValue(APValue::UninitStruct(), 0, |
| 3293 | std::distance(RD->field_begin(), RD->field_end())); |
| 3294 | unsigned ElementNo = 0; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3295 | bool Success = true; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3296 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 3297 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) { |
| 3298 | // Anonymous bit-fields are not considered members of the class for |
| 3299 | // purposes of aggregate initialization. |
| 3300 | if (Field->isUnnamedBitfield()) |
| 3301 | continue; |
| 3302 | |
| 3303 | LValue Subobject = This; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3304 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3305 | bool HaveInit = ElementNo < E->getNumInits(); |
| 3306 | |
| 3307 | // FIXME: Diagnostics here should point to the end of the initializer |
| 3308 | // list, not the start. |
| 3309 | HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, Subobject, |
| 3310 | *Field, &Layout); |
| 3311 | |
| 3312 | // Perform an implicit value-initialization for members beyond the end of |
| 3313 | // the initializer list. |
| 3314 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
| 3315 | |
| 3316 | if (!EvaluateConstantExpression( |
| 3317 | Result.getStructField((*Field)->getFieldIndex()), |
| 3318 | Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) { |
| 3319 | if (!Info.keepEvaluatingAfterFailure()) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3320 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3321 | Success = false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3322 | } |
| 3323 | } |
| 3324 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3325 | return Success; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3329 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3330 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3331 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3332 | // If we've already performed zero-initialization, we're already done. |
| 3333 | if (!Result.isUninit()) |
| 3334 | return true; |
| 3335 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3336 | if (ZeroInit) |
| 3337 | return ZeroInitialization(E); |
| 3338 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3339 | const CXXRecordDecl *RD = FD->getParent(); |
| 3340 | if (RD->isUnion()) |
| 3341 | Result = APValue((FieldDecl*)0); |
| 3342 | else |
| 3343 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3344 | std::distance(RD->field_begin(), RD->field_end())); |
| 3345 | return true; |
| 3346 | } |
| 3347 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3348 | const FunctionDecl *Definition = 0; |
| 3349 | FD->getBody(Definition); |
| 3350 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3351 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 3352 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3353 | |
Richard Smith | 610a60c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3354 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3355 | if (E->isElidable() && !ZeroInit) |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3356 | if (const MaterializeTemporaryExpr *ME |
| 3357 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 3358 | return Visit(ME->GetTemporaryExpr()); |
| 3359 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3360 | if (ZeroInit && !ZeroInitialization(E)) |
| 3361 | return false; |
| 3362 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3363 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3364 | return HandleConstructorCall(E->getExprLoc(), This, Args, |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3365 | cast<CXXConstructorDecl>(Definition), Info, |
| 3366 | Result); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 3370 | APValue &Result, EvalInfo &Info) { |
| 3371 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3372 | "can't evaluate expression as a record rvalue"); |
| 3373 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 3374 | } |
| 3375 | |
| 3376 | //===----------------------------------------------------------------------===// |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3377 | // Temporary Evaluation |
| 3378 | // |
| 3379 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 3380 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 3381 | // materialized so that a reference can bind to it. |
| 3382 | //===----------------------------------------------------------------------===// |
| 3383 | namespace { |
| 3384 | class TemporaryExprEvaluator |
| 3385 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 3386 | public: |
| 3387 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 3388 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 3389 | |
| 3390 | /// Visit an expression which constructs the value of this temporary. |
| 3391 | bool VisitConstructExpr(const Expr *E) { |
| 3392 | Result.set(E, Info.CurrentCall); |
| 3393 | return EvaluateConstantExpression(Info.CurrentCall->Temporaries[E], Info, |
| 3394 | Result, E); |
| 3395 | } |
| 3396 | |
| 3397 | bool VisitCastExpr(const CastExpr *E) { |
| 3398 | switch (E->getCastKind()) { |
| 3399 | default: |
| 3400 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 3401 | |
| 3402 | case CK_ConstructorConversion: |
| 3403 | return VisitConstructExpr(E->getSubExpr()); |
| 3404 | } |
| 3405 | } |
| 3406 | bool VisitInitListExpr(const InitListExpr *E) { |
| 3407 | return VisitConstructExpr(E); |
| 3408 | } |
| 3409 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3410 | return VisitConstructExpr(E); |
| 3411 | } |
| 3412 | bool VisitCallExpr(const CallExpr *E) { |
| 3413 | return VisitConstructExpr(E); |
| 3414 | } |
| 3415 | }; |
| 3416 | } // end anonymous namespace |
| 3417 | |
| 3418 | /// Evaluate an expression of record type as a temporary. |
| 3419 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | af2c7a1 | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 3420 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3421 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 3422 | } |
| 3423 | |
| 3424 | //===----------------------------------------------------------------------===// |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3425 | // Vector Evaluation |
| 3426 | //===----------------------------------------------------------------------===// |
| 3427 | |
| 3428 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3429 | class VectorExprEvaluator |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3430 | : public ExprEvaluatorBase<VectorExprEvaluator, bool> { |
| 3431 | APValue &Result; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3432 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3434 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 3435 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3437 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 3438 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 3439 | // FIXME: remove this APValue copy. |
| 3440 | Result = APValue(V.data(), V.size()); |
| 3441 | return true; |
| 3442 | } |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 3443 | bool Success(const CCValue &V, const Expr *E) { |
| 3444 | assert(V.isVector()); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3445 | Result = V; |
| 3446 | return true; |
| 3447 | } |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3448 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3450 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3451 | { return Visit(E->getSubExpr()); } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3452 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3453 | bool VisitInitListExpr(const InitListExpr *E); |
| 3454 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3455 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 3456 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3457 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3458 | }; |
| 3459 | } // end anonymous namespace |
| 3460 | |
| 3461 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3462 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3463 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3466 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 3467 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 3468 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 3470 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 3471 | QualType SETy = SE->getType(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3472 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3473 | switch (E->getCastKind()) { |
| 3474 | case CK_VectorSplat: { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3475 | APValue Val = APValue(); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3476 | if (SETy->isIntegerType()) { |
| 3477 | APSInt IntResult; |
| 3478 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3479 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3480 | Val = APValue(IntResult); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3481 | } else if (SETy->isRealFloatingType()) { |
| 3482 | APFloat F(0.0); |
| 3483 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3484 | return false; |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3485 | Val = APValue(F); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3486 | } else { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3487 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3488 | } |
Nate Begeman | c0b8b19 | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 3489 | |
| 3490 | // Splat and create vector APValue. |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3491 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 3492 | return Success(Elts, E); |
Nate Begeman | e8c9e92 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 3493 | } |
Eli Friedman | e6a24e8 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 3494 | case CK_BitCast: { |
| 3495 | // Evaluate the operand into an APInt we can extract from. |
| 3496 | llvm::APInt SValInt; |
| 3497 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 3498 | return false; |
| 3499 | // Extract the elements |
| 3500 | QualType EltTy = VTy->getElementType(); |
| 3501 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 3502 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 3503 | SmallVector<APValue, 4> Elts; |
| 3504 | if (EltTy->isRealFloatingType()) { |
| 3505 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
| 3506 | bool isIEESem = &Sem != &APFloat::PPCDoubleDouble; |
| 3507 | unsigned FloatEltSize = EltSize; |
| 3508 | if (&Sem == &APFloat::x87DoubleExtended) |
| 3509 | FloatEltSize = 80; |
| 3510 | for (unsigned i = 0; i < NElts; i++) { |
| 3511 | llvm::APInt Elt; |
| 3512 | if (BigEndian) |
| 3513 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 3514 | else |
| 3515 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
| 3516 | Elts.push_back(APValue(APFloat(Elt, isIEESem))); |
| 3517 | } |
| 3518 | } else if (EltTy->isIntegerType()) { |
| 3519 | for (unsigned i = 0; i < NElts; i++) { |
| 3520 | llvm::APInt Elt; |
| 3521 | if (BigEndian) |
| 3522 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 3523 | else |
| 3524 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 3525 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 3526 | } |
| 3527 | } else { |
| 3528 | return Error(E); |
| 3529 | } |
| 3530 | return Success(Elts, E); |
| 3531 | } |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3532 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3533 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 3534 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3535 | } |
| 3536 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3537 | bool |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3538 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3539 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3540 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3541 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3542 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3543 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3544 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3545 | |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3546 | // The number of initializers can be less than the number of |
| 3547 | // vector elements. For OpenCL, this can be due to nested vector |
| 3548 | // initialization. For GCC compatibility, missing trailing elements |
| 3549 | // should be initialized with zeroes. |
| 3550 | unsigned CountInits = 0, CountElts = 0; |
| 3551 | while (CountElts < NumElements) { |
| 3552 | // Handle nested vector initialization. |
| 3553 | if (CountInits < NumInits |
| 3554 | && E->getInit(CountInits)->getType()->isExtVectorType()) { |
| 3555 | APValue v; |
| 3556 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 3557 | return Error(E); |
| 3558 | unsigned vlen = v.getVectorLength(); |
| 3559 | for (unsigned j = 0; j < vlen; j++) |
| 3560 | Elements.push_back(v.getVectorElt(j)); |
| 3561 | CountElts += vlen; |
| 3562 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3563 | llvm::APSInt sInt(32); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3564 | if (CountInits < NumInits) { |
| 3565 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
| 3566 | return Error(E); |
| 3567 | } else // trailing integer zero. |
| 3568 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 3569 | Elements.push_back(APValue(sInt)); |
| 3570 | CountElts++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3571 | } else { |
| 3572 | llvm::APFloat f(0.0); |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3573 | if (CountInits < NumInits) { |
| 3574 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
| 3575 | return Error(E); |
| 3576 | } else // trailing float zero. |
| 3577 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 3578 | Elements.push_back(APValue(f)); |
| 3579 | CountElts++; |
John McCall | a7d6c22 | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 3580 | } |
Eli Friedman | 3edd5a9 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 3581 | CountInits++; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3582 | } |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3583 | return Success(Elements, E); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3586 | bool |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3587 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3588 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3589 | QualType EltTy = VT->getElementType(); |
| 3590 | APValue ZeroElement; |
| 3591 | if (EltTy->isIntegerType()) |
| 3592 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 3593 | else |
| 3594 | ZeroElement = |
| 3595 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 3596 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3597 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3598 | return Success(Elements, E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
Richard Smith | 07fc657 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 3601 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 3602 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3603 | return ZeroInitialization(E); |
Eli Friedman | 91110ee | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 3606 | //===----------------------------------------------------------------------===// |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3607 | // Array Evaluation |
| 3608 | //===----------------------------------------------------------------------===// |
| 3609 | |
| 3610 | namespace { |
| 3611 | class ArrayExprEvaluator |
| 3612 | : public ExprEvaluatorBase<ArrayExprEvaluator, bool> { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3613 | const LValue &This; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3614 | APValue &Result; |
| 3615 | public: |
| 3616 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3617 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 3618 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3619 | |
| 3620 | bool Success(const APValue &V, const Expr *E) { |
| 3621 | assert(V.isArray() && "Expected array type"); |
| 3622 | Result = V; |
| 3623 | return true; |
| 3624 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3625 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3626 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3627 | const ConstantArrayType *CAT = |
| 3628 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3629 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3630 | return Error(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3631 | |
| 3632 | Result = APValue(APValue::UninitArray(), 0, |
| 3633 | CAT->getSize().getZExtValue()); |
| 3634 | if (!Result.hasArrayFiller()) return true; |
| 3635 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3636 | // Zero-initialize all elements. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3637 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3638 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3639 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
| 3640 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
| 3641 | Subobject, &VIE); |
| 3642 | } |
| 3643 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3644 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3645 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3646 | }; |
| 3647 | } // end anonymous namespace |
| 3648 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3649 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 3650 | APValue &Result, EvalInfo &Info) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3651 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3652 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
| 3655 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 3656 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3657 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3658 | return Error(E); |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3659 | |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3660 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 3661 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3662 | if (E->getNumInits() == 1 && E->getInit(0)->isGLValue() && |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3663 | Info.Ctx.hasSameUnqualifiedType(E->getType(), E->getInit(0)->getType())) { |
| 3664 | LValue LV; |
| 3665 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 3666 | return false; |
| 3667 | uint64_t NumElements = CAT->getSize().getZExtValue(); |
| 3668 | Result = APValue(APValue::UninitArray(), NumElements, NumElements); |
| 3669 | |
| 3670 | // Copy the string literal into the array. FIXME: Do this better. |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3671 | LV.addArray(Info, E, CAT); |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3672 | for (uint64_t I = 0; I < NumElements; ++I) { |
| 3673 | CCValue Char; |
| 3674 | if (!HandleLValueToRValueConversion(Info, E->getInit(0), |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3675 | CAT->getElementType(), LV, Char) || |
| 3676 | !CheckConstantExpression(Info, E->getInit(0), Char, |
| 3677 | Result.getArrayInitializedElt(I)) || |
| 3678 | !HandleLValueArrayAdjustment(Info, E->getInit(0), LV, |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3679 | CAT->getElementType(), 1)) |
Richard Smith | 974c5f9 | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 3680 | return false; |
| 3681 | } |
| 3682 | return true; |
| 3683 | } |
| 3684 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3685 | bool Success = true; |
| 3686 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3687 | Result = APValue(APValue::UninitArray(), E->getNumInits(), |
| 3688 | CAT->getSize().getZExtValue()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3689 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3690 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3691 | unsigned Index = 0; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3692 | for (InitListExpr::const_iterator I = E->begin(), End = E->end(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3693 | I != End; ++I, ++Index) { |
| 3694 | if (!EvaluateConstantExpression(Result.getArrayInitializedElt(Index), |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3695 | Info, Subobject, cast<Expr>(*I)) || |
| 3696 | !HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject, |
| 3697 | CAT->getElementType(), 1)) { |
| 3698 | if (!Info.keepEvaluatingAfterFailure()) |
| 3699 | return false; |
| 3700 | Success = false; |
| 3701 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3702 | } |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3703 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3704 | if (!Result.hasArrayFiller()) return Success; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3705 | assert(E->hasArrayFiller() && "no array filler for incomplete init list"); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3706 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 3707 | // but sometimes does: |
| 3708 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 3709 | // S s[10] = {}; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3710 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3711 | Subobject, E->getArrayFiller()) && Success; |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3714 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 3715 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 3716 | if (!CAT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3717 | return Error(E); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3718 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3719 | bool HadZeroInit = !Result.isUninit(); |
| 3720 | if (!HadZeroInit) |
| 3721 | Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue()); |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3722 | if (!Result.hasArrayFiller()) |
| 3723 | return true; |
| 3724 | |
| 3725 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3726 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3727 | bool ZeroInit = E->requiresZeroInitialization(); |
| 3728 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3729 | if (HadZeroInit) |
| 3730 | return true; |
| 3731 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3732 | if (ZeroInit) { |
| 3733 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3734 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3735 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
| 3736 | return EvaluateConstantExpression(Result.getArrayFiller(), Info, |
| 3737 | Subobject, &VIE); |
| 3738 | } |
| 3739 | |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3740 | const CXXRecordDecl *RD = FD->getParent(); |
| 3741 | if (RD->isUnion()) |
| 3742 | Result.getArrayFiller() = APValue((FieldDecl*)0); |
| 3743 | else |
| 3744 | Result.getArrayFiller() = |
| 3745 | APValue(APValue::UninitStruct(), RD->getNumBases(), |
| 3746 | std::distance(RD->field_begin(), RD->field_end())); |
| 3747 | return true; |
| 3748 | } |
| 3749 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3750 | const FunctionDecl *Definition = 0; |
| 3751 | FD->getBody(Definition); |
| 3752 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3753 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 3754 | return false; |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3755 | |
| 3756 | // FIXME: The Subobject here isn't necessarily right. This rarely matters, |
| 3757 | // but sometimes does: |
| 3758 | // struct S { constexpr S() : p(&p) {} void *p; }; |
| 3759 | // S s[10]; |
| 3760 | LValue Subobject = This; |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3761 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3762 | |
Richard Smith | ec78916 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 3763 | if (ZeroInit && !HadZeroInit) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3764 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
| 3765 | if (!EvaluateConstantExpression(Result.getArrayFiller(), Info, Subobject, |
| 3766 | &VIE)) |
| 3767 | return false; |
| 3768 | } |
| 3769 | |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3770 | llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs()); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3771 | return HandleConstructorCall(E->getExprLoc(), Subobject, Args, |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3772 | cast<CXXConstructorDecl>(Definition), |
| 3773 | Info, Result.getArrayFiller()); |
| 3774 | } |
| 3775 | |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3776 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3777 | // Integer Evaluation |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3778 | // |
| 3779 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 3780 | // types and back in constant folding. Integer values are thus represented |
| 3781 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3782 | //===----------------------------------------------------------------------===// |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3783 | |
| 3784 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 3785 | class IntExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3786 | : public ExprEvaluatorBase<IntExprEvaluator, bool> { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3787 | CCValue &Result; |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3788 | public: |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3789 | IntExprEvaluator(EvalInfo &info, CCValue &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3790 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3791 | |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3792 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 3793 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3794 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3795 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3796 | "Invalid evaluation result."); |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3797 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3798 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3799 | Result = CCValue(SI); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3800 | return true; |
| 3801 | } |
| 3802 | |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3803 | bool Success(const llvm::APInt &I, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3804 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 3805 | "Invalid evaluation result."); |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3806 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 3807 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3808 | Result = CCValue(APSInt(I)); |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 3809 | Result.getInt().setIsUnsigned( |
| 3810 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3811 | return true; |
| 3812 | } |
| 3813 | |
| 3814 | bool Success(uint64_t Value, const Expr *E) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3815 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 3816 | "Invalid evaluation result."); |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3817 | Result = CCValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3818 | return true; |
| 3819 | } |
| 3820 | |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 3821 | bool Success(CharUnits Size, const Expr *E) { |
| 3822 | return Success(Size.getQuantity(), E); |
| 3823 | } |
| 3824 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3825 | bool Success(const CCValue &V, const Expr *E) { |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 3826 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 342f1f8 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 3827 | Result = V; |
| 3828 | return true; |
| 3829 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3830 | return Success(V.getInt(), E); |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 3831 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3832 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3833 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3834 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3835 | //===--------------------------------------------------------------------===// |
| 3836 | // Visitor Methods |
| 3837 | //===--------------------------------------------------------------------===// |
Anders Carlsson | c754aa6 | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 3838 | |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3839 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3840 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3841 | } |
| 3842 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3843 | return Success(E->getValue(), E); |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3844 | } |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3845 | |
| 3846 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 3847 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3848 | if (CheckReferencedDecl(E, E->getDecl())) |
| 3849 | return true; |
| 3850 | |
| 3851 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3852 | } |
| 3853 | bool VisitMemberExpr(const MemberExpr *E) { |
| 3854 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3855 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3856 | return true; |
| 3857 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3858 | |
| 3859 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3862 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3863 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 3864 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 3865 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 06a3675 | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 3866 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3867 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3868 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3869 | |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3870 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 3871 | return Success(E->getValue(), E); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 3872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3873 | |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3874 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 3f70456 | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 3875 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3876 | return ZeroInitialization(E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3879 | bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3880 | return Success(E->getValue(), E); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3883 | bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) { |
| 3884 | return Success(E->getValue(), E); |
| 3885 | } |
| 3886 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3887 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 3888 | return Success(E->getValue(), E); |
| 3889 | } |
| 3890 | |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3891 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 3892 | return Success(E->getValue(), E); |
| 3893 | } |
| 3894 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 3895 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3896 | bool VisitUnaryImag(const UnaryOperator *E); |
| 3897 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 3898 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 3899 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 3900 | |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 3901 | private: |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 3902 | CharUnits GetAlignOfExpr(const Expr *E); |
| 3903 | CharUnits GetAlignOfType(QualType T); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 3904 | static QualType GetObjectType(APValue::LValueBase B); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3905 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 3906 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 3907 | }; |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 3908 | } // end anonymous namespace |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3909 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3910 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 3911 | /// produce either the integer value or a pointer. |
| 3912 | /// |
| 3913 | /// GCC has a heinous extension which folds casts between pointer types and |
| 3914 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 3915 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 3916 | /// Some simple arithmetic on such values is supported (they are treated much |
| 3917 | /// like char*). |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3918 | static bool EvaluateIntegerOrLValue(const Expr *E, CCValue &Result, |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3919 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3920 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3921 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3922 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3923 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3924 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 3925 | CCValue Val; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3926 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | 69ab26a | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 3927 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3928 | if (!Val.isInt()) { |
| 3929 | // FIXME: It would be better to produce the diagnostic for casting |
| 3930 | // a pointer to an integer. |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 3931 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3932 | return false; |
| 3933 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 3934 | Result = Val.getInt(); |
| 3935 | return true; |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3936 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 3937 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3938 | /// Check whether the given declaration can be directly converted to an integral |
| 3939 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 3940 | /// try. |
Eli Friedman | 0430975 | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 3941 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3942 | // Enums are integer constant exprs. |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 3943 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 973c4fc | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 3944 | // Check for signedness/width mismatches between E type and ECD value. |
| 3945 | bool SameSign = (ECD->getInitVal().isSigned() |
| 3946 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 3947 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 3948 | == Info.Ctx.getIntWidth(E->getType())); |
| 3949 | if (SameSign && SameWidth) |
| 3950 | return Success(ECD->getInitVal(), E); |
| 3951 | else { |
| 3952 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 3953 | // by computing a new value matching the type of E. |
| 3954 | llvm::APSInt Val = ECD->getInitVal(); |
| 3955 | if (!SameSign) |
| 3956 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 3957 | if (!SameWidth) |
| 3958 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 3959 | return Success(Val, E); |
| 3960 | } |
Abramo Bagnara | bfbdcd8 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 3961 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3962 | return false; |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3965 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 3966 | /// as GCC. |
| 3967 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 3968 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3969 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3970 | enum gcc_type_class { |
| 3971 | no_type_class = -1, |
| 3972 | void_type_class, integer_type_class, char_type_class, |
| 3973 | enumeral_type_class, boolean_type_class, |
| 3974 | pointer_type_class, reference_type_class, offset_type_class, |
| 3975 | real_type_class, complex_type_class, |
| 3976 | function_type_class, method_type_class, |
| 3977 | record_type_class, union_type_class, |
| 3978 | array_type_class, string_type_class, |
| 3979 | lang_type_class |
| 3980 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3981 | |
| 3982 | // 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] | 3983 | // ideal, however it is what gcc does. |
| 3984 | if (E->getNumArgs() == 0) |
| 3985 | return no_type_class; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3986 | |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 3987 | QualType ArgTy = E->getArg(0)->getType(); |
| 3988 | if (ArgTy->isVoidType()) |
| 3989 | return void_type_class; |
| 3990 | else if (ArgTy->isEnumeralType()) |
| 3991 | return enumeral_type_class; |
| 3992 | else if (ArgTy->isBooleanType()) |
| 3993 | return boolean_type_class; |
| 3994 | else if (ArgTy->isCharType()) |
| 3995 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 3996 | else if (ArgTy->isIntegerType()) |
| 3997 | return integer_type_class; |
| 3998 | else if (ArgTy->isPointerType()) |
| 3999 | return pointer_type_class; |
| 4000 | else if (ArgTy->isReferenceType()) |
| 4001 | return reference_type_class; |
| 4002 | else if (ArgTy->isRealType()) |
| 4003 | return real_type_class; |
| 4004 | else if (ArgTy->isComplexType()) |
| 4005 | return complex_type_class; |
| 4006 | else if (ArgTy->isFunctionType()) |
| 4007 | return function_type_class; |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 4008 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4009 | return record_type_class; |
| 4010 | else if (ArgTy->isUnionType()) |
| 4011 | return union_type_class; |
| 4012 | else if (ArgTy->isArrayType()) |
| 4013 | return array_type_class; |
| 4014 | else if (ArgTy->isUnionType()) |
| 4015 | return union_type_class; |
| 4016 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4017 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4020 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 4021 | /// __builtin_constant_p when applied to the given lvalue. |
| 4022 | /// |
| 4023 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 4024 | /// character of a string literal. |
| 4025 | template<typename LValue> |
| 4026 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
| 4027 | const Expr *E = LV.getLValueBase().dyn_cast<const Expr*>(); |
| 4028 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 4029 | } |
| 4030 | |
| 4031 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 4032 | /// GCC as we can manage. |
| 4033 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 4034 | QualType ArgType = Arg->getType(); |
| 4035 | |
| 4036 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 4037 | // are not precisely documented, but are as follows: |
| 4038 | // |
| 4039 | // - If the operand is of integral, floating, complex or enumeration type, |
| 4040 | // and can be folded to a known value of that type, it returns 1. |
| 4041 | // - If the operand and can be folded to a pointer to the first character |
| 4042 | // of a string literal (or such a pointer cast to an integral type), it |
| 4043 | // returns 1. |
| 4044 | // |
| 4045 | // Otherwise, it returns 0. |
| 4046 | // |
| 4047 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 4048 | // its support for this does not currently work. |
| 4049 | if (ArgType->isIntegralOrEnumerationType()) { |
| 4050 | Expr::EvalResult Result; |
| 4051 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 4052 | return false; |
| 4053 | |
| 4054 | APValue &V = Result.Val; |
| 4055 | if (V.getKind() == APValue::Int) |
| 4056 | return true; |
| 4057 | |
| 4058 | return EvaluateBuiltinConstantPForLValue(V); |
| 4059 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 4060 | return Arg->isEvaluatable(Ctx); |
| 4061 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 4062 | LValue LV; |
| 4063 | Expr::EvalStatus Status; |
| 4064 | EvalInfo Info(Ctx, Status); |
| 4065 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 4066 | : EvaluatePointer(Arg, LV, Info)) && |
| 4067 | !Status.HasSideEffects) |
| 4068 | return EvaluateBuiltinConstantPForLValue(LV); |
| 4069 | } |
| 4070 | |
| 4071 | // Anything else isn't considered to be sufficiently constant. |
| 4072 | return false; |
| 4073 | } |
| 4074 | |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4075 | /// Retrieves the "underlying object type" of the given expression, |
| 4076 | /// as used by __builtin_object_size. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4077 | QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) { |
| 4078 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 4079 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4080 | return VD->getType(); |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4081 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 4082 | if (isa<CompoundLiteralExpr>(E)) |
| 4083 | return E->getType(); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | return QualType(); |
| 4087 | } |
| 4088 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4089 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4090 | // TODO: Perhaps we should let LLVM lower this? |
| 4091 | LValue Base; |
| 4092 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 4093 | return false; |
| 4094 | |
| 4095 | // If we can prove the base is null, lower to zero now. |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4096 | if (!Base.getLValueBase()) return Success(0, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4097 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4098 | QualType T = GetObjectType(Base.getLValueBase()); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4099 | if (T.isNull() || |
| 4100 | T->isIncompleteType() || |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 4101 | T->isFunctionType() || |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4102 | T->isVariablyModifiedType() || |
| 4103 | T->isDependentType()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4104 | return Error(E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4105 | |
| 4106 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T); |
| 4107 | CharUnits Offset = Base.getLValueOffset(); |
| 4108 | |
| 4109 | if (!Offset.isNegative() && Offset <= Size) |
| 4110 | Size -= Offset; |
| 4111 | else |
| 4112 | Size = CharUnits::Zero(); |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4113 | return Success(Size, E); |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4116 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4117 | switch (E->isBuiltinCall()) { |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4118 | default: |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4119 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4120 | |
| 4121 | case Builtin::BI__builtin_object_size: { |
John McCall | 42c8f87 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 4122 | if (TryEvaluateBuiltinObjectSize(E)) |
| 4123 | return true; |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4124 | |
Eric Christopher | b2aaf51 | 2010-01-19 22:58:35 +0000 | [diff] [blame] | 4125 | // If evaluating the argument has side-effects we can't determine |
| 4126 | // the size of the object and lower it to unknown now. |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 4127 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4128 | if (E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue() <= 1) |
Chris Lattner | cf18465 | 2009-11-03 19:48:51 +0000 | [diff] [blame] | 4129 | return Success(-1ULL, E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4130 | return Success(0, E); |
| 4131 | } |
Mike Stump | c4c9045 | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 4132 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4133 | return Error(E); |
Mike Stump | 64eda9e | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4136 | case Builtin::BI__builtin_classify_type: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4137 | return Success(EvaluateBuiltinClassifyType(E), E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4138 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 4139 | case Builtin::BI__builtin_constant_p: |
| 4140 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
Richard Smith | e052d46 | 2011-12-09 02:04:48 +0000 | [diff] [blame] | 4141 | |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 4142 | case Builtin::BI__builtin_eh_return_data_regno: { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4143 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4144 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
Chris Lattner | 21fb98e | 2009-09-23 06:06:36 +0000 | [diff] [blame] | 4145 | return Success(Operand, E); |
| 4146 | } |
Eli Friedman | c4a2638 | 2010-02-13 00:10:10 +0000 | [diff] [blame] | 4147 | |
| 4148 | case Builtin::BI__builtin_expect: |
| 4149 | return Visit(E->getArg(0)); |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4150 | |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4151 | case Builtin::BIstrlen: |
Richard Smith | 40b993a | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 4152 | // A call to strlen is not a constant expression. |
| 4153 | if (Info.getLangOpts().CPlusPlus0x) |
| 4154 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_invalid_function) |
| 4155 | << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; |
| 4156 | else |
| 4157 | Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
| 4158 | // Fall through. |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4159 | case Builtin::BI__builtin_strlen: |
| 4160 | // As an extension, we support strlen() and __builtin_strlen() as constant |
| 4161 | // expressions when the argument is a string literal. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4162 | if (const StringLiteral *S |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4163 | = dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) { |
| 4164 | // The string literal may have embedded null characters. Find the first |
| 4165 | // one and truncate there. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4166 | StringRef Str = S->getString(); |
| 4167 | StringRef::size_type Pos = Str.find(0); |
| 4168 | if (Pos != StringRef::npos) |
Douglas Gregor | 5726d40 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 4169 | Str = Str.substr(0, Pos); |
| 4170 | |
| 4171 | return Success(Str.size(), E); |
| 4172 | } |
| 4173 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4174 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4175 | |
| 4176 | case Builtin::BI__atomic_is_lock_free: { |
| 4177 | APSInt SizeVal; |
| 4178 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 4179 | return false; |
| 4180 | |
| 4181 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 4182 | // of two less than the maximum inline atomic width, we know it is |
| 4183 | // lock-free. If the size isn't a power of two, or greater than the |
| 4184 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 4185 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 4186 | // the answer can only be determined at runtime; for example, 16-byte |
| 4187 | // atomics have lock-free implementations on some, but not all, |
| 4188 | // x86-64 processors. |
| 4189 | |
| 4190 | // Check power-of-two. |
| 4191 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
| 4192 | if (!Size.isPowerOfTwo()) |
| 4193 | #if 0 |
| 4194 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 4195 | // settles. |
| 4196 | return Success(0, E); |
| 4197 | #else |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4198 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4199 | #endif |
| 4200 | |
| 4201 | #if 0 |
| 4202 | // Check against promotion width. |
| 4203 | // FIXME: Suppress this folding until the ABI for the promotion width |
| 4204 | // settles. |
| 4205 | unsigned PromoteWidthBits = |
| 4206 | Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth(); |
| 4207 | if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits)) |
| 4208 | return Success(0, E); |
| 4209 | #endif |
| 4210 | |
| 4211 | // Check against inlining width. |
| 4212 | unsigned InlineWidthBits = |
| 4213 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 4214 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) |
| 4215 | return Success(1, E); |
| 4216 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4217 | return Error(E); |
Eli Friedman | 454b57a | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 4218 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 4219 | } |
Chris Lattner | 4c4867e | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 4220 | } |
Anders Carlsson | 650c92f | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4221 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4222 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 4223 | if (!A.getLValueBase()) |
| 4224 | return !B.getLValueBase(); |
| 4225 | if (!B.getLValueBase()) |
| 4226 | return false; |
| 4227 | |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4228 | if (A.getLValueBase().getOpaqueValue() != |
| 4229 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4230 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 4231 | if (!ADecl) |
| 4232 | return false; |
| 4233 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 4234 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4235 | return false; |
| 4236 | } |
| 4237 | |
| 4238 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | 177dce7 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4239 | A.getLValueFrame() == B.getLValueFrame(); |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 4242 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 4243 | /// bits, and check for overflow in the original type (if that type was not an |
| 4244 | /// unsigned type). |
| 4245 | template<typename Operation> |
| 4246 | static APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 4247 | const APSInt &LHS, const APSInt &RHS, |
| 4248 | unsigned BitWidth, Operation Op) { |
| 4249 | if (LHS.isUnsigned()) |
| 4250 | return Op(LHS, RHS); |
| 4251 | |
| 4252 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
| 4253 | APSInt Result = Value.trunc(LHS.getBitWidth()); |
| 4254 | if (Result.extend(BitWidth) != Value) |
| 4255 | HandleOverflow(Info, E, Value, E->getType()); |
| 4256 | return Result; |
| 4257 | } |
| 4258 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4259 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4260 | if (E->isAssignmentOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4261 | return Error(E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4262 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4263 | if (E->getOpcode() == BO_Comma) { |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4264 | VisitIgnoredValue(E->getLHS()); |
| 4265 | return Visit(E->getRHS()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
| 4268 | if (E->isLogicalOp()) { |
| 4269 | // These need to be handled specially because the operands aren't |
| 4270 | // necessarily integral |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 4271 | bool lhsResult, rhsResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4273 | if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) { |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4274 | // We were able to evaluate the LHS, see if we can get away with not |
| 4275 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4276 | if (lhsResult == (E->getOpcode() == BO_LOr)) |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4277 | return Success(lhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 4278 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4279 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4280 | if (E->getOpcode() == BO_LOr) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4281 | return Success(lhsResult || rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 4282 | else |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4283 | return Success(lhsResult && rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 4284 | } |
| 4285 | } else { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4286 | // FIXME: If both evaluations fail, we should produce the diagnostic from |
| 4287 | // the LHS. If the LHS is non-constant and the RHS is unevaluatable, it's |
| 4288 | // less clear how to diagnose this. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4289 | if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) { |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 4290 | // We can't evaluate the LHS; however, sometimes the result |
| 4291 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4292 | if (rhsResult == (E->getOpcode() == BO_LOr)) { |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4293 | // Since we weren't able to evaluate the left hand side, it |
Anders Carlsson | fcb4d09 | 2008-11-30 16:51:17 +0000 | [diff] [blame] | 4294 | // must have had side effects. |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 4295 | Info.EvalStatus.HasSideEffects = true; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4296 | |
| 4297 | return Success(rhsResult, E); |
Anders Carlsson | 4bbc0e0 | 2008-11-24 04:21:33 +0000 | [diff] [blame] | 4298 | } |
| 4299 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 4300 | } |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4301 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4302 | return false; |
| 4303 | } |
| 4304 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4305 | QualType LHSTy = E->getLHS()->getType(); |
| 4306 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4307 | |
| 4308 | if (LHSTy->isAnyComplexType()) { |
| 4309 | assert(RHSTy->isAnyComplexType() && "Invalid comparison"); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 4310 | ComplexValue LHS, RHS; |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4311 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4312 | bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 4313 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4314 | return false; |
| 4315 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4316 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4317 | return false; |
| 4318 | |
| 4319 | if (LHS.isComplexFloat()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4320 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4321 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4322 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4323 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 4324 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4325 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4326 | return Success((CR_r == APFloat::cmpEqual && |
| 4327 | CR_i == APFloat::cmpEqual), E); |
| 4328 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4329 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4330 | "Invalid complex comparison."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4331 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4332 | CR_r == APFloat::cmpLessThan || |
| 4333 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4334 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4335 | CR_i == APFloat::cmpLessThan || |
| 4336 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4337 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4338 | } else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4339 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4340 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 4341 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 4342 | else { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4343 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4344 | "Invalid compex comparison."); |
| 4345 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 4346 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 4347 | } |
Daniel Dunbar | 4087e24 | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 4348 | } |
| 4349 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4350 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4351 | if (LHSTy->isRealFloatingType() && |
| 4352 | RHSTy->isRealFloatingType()) { |
| 4353 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4354 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4355 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
| 4356 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4357 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4358 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4359 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4360 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4361 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4362 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 529569e | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 4363 | |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4364 | switch (E->getOpcode()) { |
| 4365 | default: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4366 | llvm_unreachable("Invalid binary operator!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4367 | case BO_LT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4368 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4369 | case BO_GT: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4370 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4371 | case BO_LE: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4372 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4373 | case BO_GE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4374 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4375 | E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4376 | case BO_EQ: |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4377 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4378 | case BO_NE: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4379 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | fc39dc4 | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 4380 | || CR == APFloat::cmpLessThan |
| 4381 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4382 | } |
Anders Carlsson | 286f85e | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 4383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4384 | |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 4385 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4386 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4387 | LValue LHSValue, RHSValue; |
| 4388 | |
| 4389 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 4390 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4391 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4392 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4393 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4394 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4395 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4396 | // Reject differing bases from the normal codepath; we special-case |
| 4397 | // comparisons to null. |
| 4398 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4399 | if (E->getOpcode() == BO_Sub) { |
| 4400 | // Handle &&A - &&B. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4401 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 4402 | return false; |
| 4403 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
| 4404 | const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
| 4405 | if (!LHSExpr || !RHSExpr) |
| 4406 | return false; |
| 4407 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 4408 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 4409 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 4410 | return false; |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4411 | // Make sure both labels come from the same function. |
| 4412 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 4413 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 4414 | return false; |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4415 | Result = CCValue(LHSAddrExpr, RHSAddrExpr); |
| 4416 | return true; |
| 4417 | } |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4418 | // Inequalities and subtractions between unrelated pointers have |
| 4419 | // unspecified or undefined behavior. |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 4420 | if (!E->isEqualityOp()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4421 | return Error(E); |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 4422 | // A constant address may compare equal to the address of a symbol. |
| 4423 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4424 | // to a null pointer constant. |
Eli Friedman | ffbda40 | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 4425 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 4426 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4427 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4428 | // It's implementation-defined whether distinct literals will have |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 4429 | // distinct addresses. In clang, the result of such a comparison is |
| 4430 | // unspecified, so it is not a constant expression. However, we do know |
| 4431 | // that the address of a literal will be non-null. |
Richard Smith | 74f4634 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 4432 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 4433 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4434 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4435 | // We can't tell whether weak symbols will end up pointing to the same |
| 4436 | // object. |
| 4437 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4438 | return Error(E); |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4439 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | c45061b | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 4440 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 4441 | // lead to inconsistent results for comparisons involving the address |
| 4442 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 9e36b53 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 4443 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 5bc8610 | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 4444 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4445 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 4446 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 4447 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 4448 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4449 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 4450 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 4451 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4452 | if (E->getOpcode() == BO_Sub) { |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4453 | // C++11 [expr.add]p6: |
| 4454 | // Unless both pointers point to elements of the same array object, or |
| 4455 | // one past the last element of the array object, the behavior is |
| 4456 | // undefined. |
| 4457 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 4458 | !AreElementsOfSameArray(getType(LHSValue.Base), |
| 4459 | LHSDesignator, RHSDesignator)) |
| 4460 | CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 4461 | |
Chris Lattner | 4992bdd | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 4462 | QualType Type = E->getLHS()->getType(); |
| 4463 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4464 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4465 | CharUnits ElementSize; |
| 4466 | if (!HandleSizeof(Info, ElementType, ElementSize)) |
| 4467 | return false; |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4468 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 4469 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 4470 | // and produce incorrect results when it overflows. Such behavior |
| 4471 | // appears to be non-conforming, but is common, so perhaps we should |
| 4472 | // assume the standard intended for such cases to be undefined behavior |
| 4473 | // and check for them. |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4474 | |
Richard Smith | 15efc4d | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 4475 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 4476 | // overflow in the final conversion to ptrdiff_t. |
| 4477 | APSInt LHS( |
| 4478 | llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 4479 | APSInt RHS( |
| 4480 | llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 4481 | APSInt ElemSize( |
| 4482 | llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); |
| 4483 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 4484 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 4485 | |
| 4486 | if (Result.extend(65) != TrueResult) |
| 4487 | HandleOverflow(Info, E, TrueResult, E->getType()); |
| 4488 | return Success(Result, E); |
| 4489 | } |
Richard Smith | 82f2858 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 4490 | |
| 4491 | // C++11 [expr.rel]p3: |
| 4492 | // Pointers to void (after pointer conversions) can be compared, with a |
| 4493 | // result defined as follows: If both pointers represent the same |
| 4494 | // address or are both the null pointer value, the result is true if the |
| 4495 | // operator is <= or >= and false otherwise; otherwise the result is |
| 4496 | // unspecified. |
| 4497 | // We interpret this as applying to pointers to *cv* void. |
| 4498 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4499 | E->isRelationalOp()) |
Richard Smith | 82f2858 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 4500 | CCEDiag(E, diag::note_constexpr_void_comparison); |
| 4501 | |
Richard Smith | f15fda0 | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4502 | // C++11 [expr.rel]p2: |
| 4503 | // - If two pointers point to non-static data members of the same object, |
| 4504 | // or to subobjects or array elements fo such members, recursively, the |
| 4505 | // pointer to the later declared member compares greater provided the |
| 4506 | // two members have the same access control and provided their class is |
| 4507 | // not a union. |
| 4508 | // [...] |
| 4509 | // - Otherwise pointer comparisons are unspecified. |
| 4510 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 4511 | E->isRelationalOp()) { |
| 4512 | bool WasArrayIndex; |
| 4513 | unsigned Mismatch = |
| 4514 | FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, |
| 4515 | RHSDesignator, WasArrayIndex); |
| 4516 | // At the point where the designators diverge, the comparison has a |
| 4517 | // specified value if: |
| 4518 | // - we are comparing array indices |
| 4519 | // - we are comparing fields of a union, or fields with the same access |
| 4520 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 4521 | // constant expression. |
| 4522 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 4523 | Mismatch < RHSDesignator.Entries.size()) { |
| 4524 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 4525 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 4526 | if (!LF && !RF) |
| 4527 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 4528 | else if (!LF) |
| 4529 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 4530 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 4531 | << RF->getParent() << RF; |
| 4532 | else if (!RF) |
| 4533 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 4534 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 4535 | << LF->getParent() << LF; |
| 4536 | else if (!LF->getParent()->isUnion() && |
| 4537 | LF->getAccess() != RF->getAccess()) |
| 4538 | CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) |
| 4539 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 4540 | << LF->getParent(); |
| 4541 | } |
| 4542 | } |
| 4543 | |
Richard Smith | 625b807 | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 4544 | switch (E->getOpcode()) { |
| 4545 | default: llvm_unreachable("missing comparison operator"); |
| 4546 | case BO_LT: return Success(LHSOffset < RHSOffset, E); |
| 4547 | case BO_GT: return Success(LHSOffset > RHSOffset, E); |
| 4548 | case BO_LE: return Success(LHSOffset <= RHSOffset, E); |
| 4549 | case BO_GE: return Success(LHSOffset >= RHSOffset, E); |
| 4550 | case BO_EQ: return Success(LHSOffset == RHSOffset, E); |
| 4551 | case BO_NE: return Success(LHSOffset != RHSOffset, E); |
Eli Friedman | ad02d7d | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 4552 | } |
Anders Carlsson | 3068d11 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4553 | } |
| 4554 | } |
Richard Smith | b02e462 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 4555 | |
| 4556 | if (LHSTy->isMemberPointerType()) { |
| 4557 | assert(E->isEqualityOp() && "unexpected member pointer operation"); |
| 4558 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 4559 | |
| 4560 | MemberPtr LHSValue, RHSValue; |
| 4561 | |
| 4562 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
| 4563 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
| 4564 | return false; |
| 4565 | |
| 4566 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 4567 | return false; |
| 4568 | |
| 4569 | // C++11 [expr.eq]p2: |
| 4570 | // If both operands are null, they compare equal. Otherwise if only one is |
| 4571 | // null, they compare unequal. |
| 4572 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 4573 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| 4574 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 4575 | } |
| 4576 | |
| 4577 | // Otherwise if either is a pointer to a virtual member function, the |
| 4578 | // result is unspecified. |
| 4579 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 4580 | if (MD->isVirtual()) |
| 4581 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 4582 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 4583 | if (MD->isVirtual()) |
| 4584 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 4585 | |
| 4586 | // Otherwise they compare equal if and only if they would refer to the |
| 4587 | // same member of the same most derived object or the same subobject if |
| 4588 | // they were dereferenced with a hypothetical object of the associated |
| 4589 | // class type. |
| 4590 | bool Equal = LHSValue == RHSValue; |
| 4591 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 4592 | } |
| 4593 | |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4594 | if (!LHSTy->isIntegralOrEnumerationType() || |
| 4595 | !RHSTy->isIntegralOrEnumerationType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4596 | // We can't continue from here for non-integral types. |
| 4597 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4600 | // The LHS of a constant expr is always evaluated and needed. |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4601 | CCValue LHSVal; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4602 | |
| 4603 | bool LHSOK = EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info); |
| 4604 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4605 | return false; |
Eli Friedman | d9f4bcd | 2008-07-27 05:46:18 +0000 | [diff] [blame] | 4606 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4607 | if (!Visit(E->getRHS()) || !LHSOK) |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 4608 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4609 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4610 | CCValue &RHSVal = Result; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4611 | |
| 4612 | // Handle cases like (unsigned long)&a + 4. |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4613 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 4614 | CharUnits AdditionalOffset = CharUnits::fromQuantity( |
| 4615 | RHSVal.getInt().getZExtValue()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4616 | if (E->getOpcode() == BO_Add) |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4617 | LHSVal.getLValueOffset() += AdditionalOffset; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4618 | else |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4619 | LHSVal.getLValueOffset() -= AdditionalOffset; |
| 4620 | Result = LHSVal; |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4621 | return true; |
| 4622 | } |
| 4623 | |
| 4624 | // Handle cases like 4 + (unsigned long)&a |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4625 | if (E->getOpcode() == BO_Add && |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4626 | RHSVal.isLValue() && LHSVal.isInt()) { |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4627 | RHSVal.getLValueOffset() += CharUnits::fromQuantity( |
| 4628 | LHSVal.getInt().getZExtValue()); |
| 4629 | // Note that RHSVal is Result. |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4630 | return true; |
| 4631 | } |
| 4632 | |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4633 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 4634 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4635 | if (!LHSVal.getLValueOffset().isZero() || |
| 4636 | !RHSVal.getLValueOffset().isZero()) |
| 4637 | return false; |
| 4638 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 4639 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 4640 | if (!LHSExpr || !RHSExpr) |
| 4641 | return false; |
| 4642 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 4643 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 4644 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 4645 | return false; |
Eli Friedman | 5930a4c | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 4646 | // Make sure both labels come from the same function. |
| 4647 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 4648 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 4649 | return false; |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4650 | Result = CCValue(LHSAddrExpr, RHSAddrExpr); |
| 4651 | return true; |
| 4652 | } |
| 4653 | |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4654 | // All the following cases expect both operands to be an integer |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4655 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4656 | return Error(E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4657 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4658 | APSInt &LHS = LHSVal.getInt(); |
| 4659 | APSInt &RHS = RHSVal.getInt(); |
Eli Friedman | 42edd0d | 2009-03-24 01:14:50 +0000 | [diff] [blame] | 4660 | |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4661 | switch (E->getOpcode()) { |
Chris Lattner | 32fea9d | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 4662 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4663 | return Error(E); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 4664 | case BO_Mul: |
| 4665 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4666 | LHS.getBitWidth() * 2, |
| 4667 | std::multiplies<APSInt>()), E); |
| 4668 | case BO_Add: |
| 4669 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4670 | LHS.getBitWidth() + 1, |
| 4671 | std::plus<APSInt>()), E); |
| 4672 | case BO_Sub: |
| 4673 | return Success(CheckedIntArithmetic(Info, E, LHS, RHS, |
| 4674 | LHS.getBitWidth() + 1, |
| 4675 | std::minus<APSInt>()), E); |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4676 | case BO_And: return Success(LHS & RHS, E); |
| 4677 | case BO_Xor: return Success(LHS ^ RHS, E); |
| 4678 | case BO_Or: return Success(LHS | RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4679 | case BO_Div: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4680 | case BO_Rem: |
Chris Lattner | 54176fd | 2008-07-12 00:14:42 +0000 | [diff] [blame] | 4681 | if (RHS == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4682 | return Error(E, diag::note_expr_divide_by_zero); |
Richard Smith | 3df6130 | 2012-01-31 23:24:19 +0000 | [diff] [blame] | 4683 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is not |
| 4684 | // actually undefined behavior in C++11 due to a language defect. |
| 4685 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 4686 | LHS.isSigned() && LHS.isMinSignedValue()) |
| 4687 | HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); |
| 4688 | return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4689 | case BO_Shl: { |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4690 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 4691 | // shift is not a constant expression. |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4692 | if (RHS.isSigned() && RHS.isNegative()) { |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4693 | CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4694 | RHS = -RHS; |
| 4695 | goto shift_right; |
| 4696 | } |
| 4697 | |
| 4698 | shift_left: |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4699 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 4700 | // shifted type. |
| 4701 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 4702 | if (SA != RHS) { |
| 4703 | CCEDiag(E, diag::note_constexpr_large_shift) |
| 4704 | << RHS << E->getType() << LHS.getBitWidth(); |
| 4705 | } else if (LHS.isSigned()) { |
| 4706 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
Richard Smith | 925d8e7 | 2012-02-08 06:14:53 +0000 | [diff] [blame^] | 4707 | // operand, and must not overflow the corresponding unsigned type. |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4708 | if (LHS.isNegative()) |
| 4709 | CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
Richard Smith | 925d8e7 | 2012-02-08 06:14:53 +0000 | [diff] [blame^] | 4710 | else if (LHS.countLeadingZeros() < SA) |
| 4711 | CCEDiag(E, diag::note_constexpr_lshift_discards); |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4712 | } |
| 4713 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4714 | return Success(LHS << SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4715 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4716 | case BO_Shr: { |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4717 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 4718 | // shift is not a constant expression. |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4719 | if (RHS.isSigned() && RHS.isNegative()) { |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4720 | CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
John McCall | 091f23f | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 4721 | RHS = -RHS; |
| 4722 | goto shift_left; |
| 4723 | } |
| 4724 | |
| 4725 | shift_right: |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4726 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 4727 | // shifted type. |
| 4728 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 4729 | if (SA != RHS) |
| 4730 | CCEDiag(E, diag::note_constexpr_large_shift) |
| 4731 | << RHS << E->getType() << LHS.getBitWidth(); |
| 4732 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4733 | return Success(LHS >> SA, E); |
Daniel Dunbar | 3f7d995 | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 4734 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4735 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4736 | case BO_LT: return Success(LHS < RHS, E); |
| 4737 | case BO_GT: return Success(LHS > RHS, E); |
| 4738 | case BO_LE: return Success(LHS <= RHS, E); |
| 4739 | case BO_GE: return Success(LHS >= RHS, E); |
| 4740 | case BO_EQ: return Success(LHS == RHS, E); |
| 4741 | case BO_NE: return Success(LHS != RHS, E); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 4742 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4745 | CharUnits IntExprEvaluator::GetAlignOfType(QualType T) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 4746 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 4747 | // the result is the size of the referenced type." |
| 4748 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 4749 | // result shall be the alignment of the referenced type." |
| 4750 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 4751 | T = Ref->getPointeeType(); |
Chad Rosier | 9f1210c | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 4752 | |
| 4753 | // __alignof is defined to return the preferred alignment. |
| 4754 | return Info.Ctx.toCharUnitsFromBits( |
| 4755 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4758 | CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) { |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 4759 | E = E->IgnoreParens(); |
| 4760 | |
| 4761 | // 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] | 4762 | // to 1 in those cases. |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 4763 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4764 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 4765 | /*RefAsPointee*/true); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4766 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 4767 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Ken Dyck | 8b752f1 | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 4768 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 4769 | /*RefAsPointee*/true); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 4770 | |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 4771 | return GetAlignOfType(E->getType()); |
| 4772 | } |
| 4773 | |
| 4774 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4775 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 4776 | /// a result as the expression's type. |
| 4777 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 4778 | const UnaryExprOrTypeTraitExpr *E) { |
| 4779 | switch(E->getKind()) { |
| 4780 | case UETT_AlignOf: { |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 4781 | if (E->isArgumentType()) |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4782 | return Success(GetAlignOfType(E->getArgumentType()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 4783 | else |
Ken Dyck | 4f3bc8f | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 4784 | return Success(GetAlignOfExpr(E->getArgumentExpr()), E); |
Chris Lattner | e9feb47 | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 4785 | } |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4786 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4787 | case UETT_VecStep: { |
| 4788 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 4789 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4790 | if (Ty->isVectorType()) { |
| 4791 | unsigned n = Ty->getAs<VectorType>()->getNumElements(); |
Eli Friedman | a1f47c4 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 4792 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4793 | // The vec_step built-in functions that take a 3-component |
| 4794 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 4795 | if (n == 3) |
| 4796 | n = 4; |
Eli Friedman | f2da9df | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 4797 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4798 | return Success(n, E); |
| 4799 | } else |
| 4800 | return Success(1, E); |
| 4801 | } |
| 4802 | |
| 4803 | case UETT_SizeOf: { |
| 4804 | QualType SrcTy = E->getTypeOfArgument(); |
| 4805 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 4806 | // the result is the size of the referenced type." |
| 4807 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 4808 | // result shall be the alignment of the referenced type." |
| 4809 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 4810 | SrcTy = Ref->getPointeeType(); |
| 4811 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4812 | CharUnits Sizeof; |
| 4813 | if (!HandleSizeof(Info, SrcTy, Sizeof)) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4814 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4815 | return Success(Sizeof, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4816 | } |
| 4817 | } |
| 4818 | |
| 4819 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | fcee001 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4822 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4823 | CharUnits Result; |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4824 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4825 | if (n == 0) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4826 | return Error(OOE); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4827 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4828 | for (unsigned i = 0; i != n; ++i) { |
| 4829 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 4830 | switch (ON.getKind()) { |
| 4831 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4832 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4833 | APSInt IdxResult; |
| 4834 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 4835 | return false; |
| 4836 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 4837 | if (!AT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4838 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4839 | CurrentType = AT->getElementType(); |
| 4840 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 4841 | Result += IdxResult.getSExtValue() * ElementSize; |
| 4842 | break; |
| 4843 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4844 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4845 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 4846 | FieldDecl *MemberDecl = ON.getField(); |
| 4847 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4848 | if (!RT) |
| 4849 | return Error(OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4850 | RecordDecl *RD = RT->getDecl(); |
| 4851 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 4852 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4853 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | fb1e3bc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 4854 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4855 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 4856 | break; |
| 4857 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4858 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4859 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 4860 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4861 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4862 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 4863 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 4864 | if (BaseSpec->isVirtual()) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4865 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4866 | |
| 4867 | // Find the layout of the class whose base we are looking into. |
| 4868 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4869 | if (!RT) |
| 4870 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4871 | RecordDecl *RD = RT->getDecl(); |
| 4872 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 4873 | |
| 4874 | // Find the base class itself. |
| 4875 | CurrentType = BaseSpec->getType(); |
| 4876 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 4877 | if (!BaseRT) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4878 | return Error(OOE); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4879 | |
| 4880 | // Add the offset to the base. |
Ken Dyck | 7c7f820 | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 4881 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 4882 | break; |
| 4883 | } |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4884 | } |
| 4885 | } |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4886 | return Success(Result, OOE); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 4887 | } |
| 4888 | |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4889 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4890 | switch (E->getOpcode()) { |
| 4891 | default: |
| 4892 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 4893 | // See C99 6.6p3. |
| 4894 | return Error(E); |
| 4895 | case UO_Extension: |
| 4896 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 4897 | // If so, we could clear the diagnostic ID. |
| 4898 | return Visit(E->getSubExpr()); |
| 4899 | case UO_Plus: |
| 4900 | // The result is just the value. |
| 4901 | return Visit(E->getSubExpr()); |
| 4902 | case UO_Minus: { |
| 4903 | if (!Visit(E->getSubExpr())) |
| 4904 | return false; |
| 4905 | if (!Result.isInt()) return Error(E); |
Richard Smith | 789f9b6 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 4906 | const APSInt &Value = Result.getInt(); |
| 4907 | if (Value.isSigned() && Value.isMinSignedValue()) |
| 4908 | HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 4909 | E->getType()); |
| 4910 | return Success(-Value, E); |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4911 | } |
| 4912 | case UO_Not: { |
| 4913 | if (!Visit(E->getSubExpr())) |
| 4914 | return false; |
| 4915 | if (!Result.isInt()) return Error(E); |
| 4916 | return Success(~Result.getInt(), E); |
| 4917 | } |
| 4918 | case UO_LNot: { |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4919 | bool bres; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4920 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4921 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4922 | return Success(!bres, E); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 4923 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4924 | } |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 4925 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4926 | |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 4927 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 4928 | /// result type is integer. |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4929 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 4930 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 4931 | QualType DestType = E->getType(); |
Daniel Dunbar | b92dac8 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 4932 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 82206e2 | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 4933 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4934 | switch (E->getCastKind()) { |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4935 | case CK_BaseToDerived: |
| 4936 | case CK_DerivedToBase: |
| 4937 | case CK_UncheckedDerivedToBase: |
| 4938 | case CK_Dynamic: |
| 4939 | case CK_ToUnion: |
| 4940 | case CK_ArrayToPointerDecay: |
| 4941 | case CK_FunctionToPointerDecay: |
| 4942 | case CK_NullToPointer: |
| 4943 | case CK_NullToMemberPointer: |
| 4944 | case CK_BaseToDerivedMemberPointer: |
| 4945 | case CK_DerivedToBaseMemberPointer: |
| 4946 | case CK_ConstructorConversion: |
| 4947 | case CK_IntegralToPointer: |
| 4948 | case CK_ToVoid: |
| 4949 | case CK_VectorSplat: |
| 4950 | case CK_IntegralToFloating: |
| 4951 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4952 | case CK_CPointerToObjCPointerCast: |
| 4953 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4954 | case CK_AnyPointerToBlockPointerCast: |
| 4955 | case CK_ObjCObjectLValueCast: |
| 4956 | case CK_FloatingRealToComplex: |
| 4957 | case CK_FloatingComplexToReal: |
| 4958 | case CK_FloatingComplexCast: |
| 4959 | case CK_FloatingComplexToIntegralComplex: |
| 4960 | case CK_IntegralRealToComplex: |
| 4961 | case CK_IntegralComplexCast: |
| 4962 | case CK_IntegralComplexToFloatingComplex: |
| 4963 | llvm_unreachable("invalid cast kind for integral value"); |
| 4964 | |
Eli Friedman | e50c297 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 4965 | case CK_BitCast: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4966 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4967 | case CK_LValueBitCast: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4968 | case CK_ARCProduceObject: |
| 4969 | case CK_ARCConsumeObject: |
| 4970 | case CK_ARCReclaimReturnedObject: |
| 4971 | case CK_ARCExtendBlockObject: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4972 | return Error(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4973 | |
Richard Smith | 7d580a4 | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 4974 | case CK_UserDefinedConversion: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4975 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 4976 | case CK_AtomicToNonAtomic: |
| 4977 | case CK_NonAtomicToAtomic: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4978 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4979 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4980 | |
| 4981 | case CK_MemberPointerToBoolean: |
| 4982 | case CK_PointerToBoolean: |
| 4983 | case CK_IntegralToBoolean: |
| 4984 | case CK_FloatingToBoolean: |
| 4985 | case CK_FloatingComplexToBoolean: |
| 4986 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4987 | bool BoolResult; |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4988 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4989 | return false; |
Daniel Dunbar | 131eb43 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 4990 | return Success(BoolResult, E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4991 | } |
| 4992 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 4993 | case CK_IntegralCast: { |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 4994 | if (!Visit(SubExpr)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 4995 | return false; |
Daniel Dunbar | a2cfd34 | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 4996 | |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 4997 | if (!Result.isInt()) { |
Eli Friedman | 6563928 | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 4998 | // Allow casts of address-of-label differences if they are no-ops |
| 4999 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 5000 | // be constant-evaluatable except in some narrow cases which are hard |
| 5001 | // to detect here. We let it through on the assumption the user knows |
| 5002 | // what they are doing.) |
| 5003 | if (Result.isAddrLabelDiff()) |
| 5004 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | be26570 | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 5005 | // Only allow casts of lvalues if they are lossless. |
| 5006 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 5007 | } |
Daniel Dunbar | 30c37f4 | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5008 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5009 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 5010 | Result.getInt()), E); |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5011 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5012 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5013 | case CK_PointerToIntegral: { |
Richard Smith | c216a01 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5014 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 5015 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5016 | LValue LV; |
Chris Lattner | 87eae5e | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 5017 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | b542afe | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5018 | return false; |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5019 | |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5020 | if (LV.getLValueBase()) { |
| 5021 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5022 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 5023 | // narrowing back down to pointer width in subsequent integral casts. |
| 5024 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5025 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5026 | return Error(E); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5027 | |
Richard Smith | b755a9d | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 5028 | LV.Designator.setInvalid(); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5029 | LV.moveInto(Result); |
Daniel Dunbar | dd21164 | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 5030 | return true; |
| 5031 | } |
| 5032 | |
Ken Dyck | a730583 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 5033 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 5034 | SrcType); |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5035 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5036 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5037 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5038 | case CK_IntegralComplexToReal: { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5039 | ComplexValue C; |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 5040 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 5041 | return false; |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5042 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | 1725f68 | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 5043 | } |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5044 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5045 | case CK_FloatingToIntegral: { |
| 5046 | APFloat F(0.0); |
| 5047 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 5048 | return false; |
Chris Lattner | 732b223 | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 5049 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5050 | APSInt Value; |
| 5051 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 5052 | return false; |
| 5053 | return Success(Value, E); |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5054 | } |
| 5055 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5056 | |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5057 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | a25ae3d | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5058 | } |
Anders Carlsson | 2bad168 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5059 | |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5060 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 5061 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5062 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5063 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 5064 | return false; |
| 5065 | if (!LV.isComplexInt()) |
| 5066 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5067 | return Success(LV.getComplexIntReal(), E); |
| 5068 | } |
| 5069 | |
| 5070 | return Visit(E->getSubExpr()); |
| 5071 | } |
| 5072 | |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5073 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5074 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5075 | ComplexValue LV; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5076 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 5077 | return false; |
| 5078 | if (!LV.isComplexInt()) |
| 5079 | return Error(E); |
Eli Friedman | 722c717 | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5080 | return Success(LV.getComplexIntImag(), E); |
| 5081 | } |
| 5082 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5083 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 664a104 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5084 | return Success(0, E); |
| 5085 | } |
| 5086 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 5087 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 5088 | return Success(E->getPackLength(), E); |
| 5089 | } |
| 5090 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 5091 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 5092 | return Success(E->getValue(), E); |
| 5093 | } |
| 5094 | |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5095 | //===----------------------------------------------------------------------===// |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5096 | // Float Evaluation |
| 5097 | //===----------------------------------------------------------------------===// |
| 5098 | |
| 5099 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5100 | class FloatExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5101 | : public ExprEvaluatorBase<FloatExprEvaluator, bool> { |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5102 | APFloat &Result; |
| 5103 | public: |
| 5104 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5105 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5106 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5107 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5108 | Result = V.getFloat(); |
| 5109 | return true; |
| 5110 | } |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5111 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5112 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | f10d917 | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5113 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 5114 | return true; |
| 5115 | } |
| 5116 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5117 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5118 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5119 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5120 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 5121 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5122 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | 2217c87 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5123 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5124 | bool VisitUnaryReal(const UnaryOperator *E); |
| 5125 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | ba98d6b | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 5126 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5127 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5128 | }; |
| 5129 | } // end anonymous namespace |
| 5130 | |
| 5131 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5132 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5133 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5134 | } |
| 5135 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5136 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5137 | QualType ResultTy, |
| 5138 | const Expr *Arg, |
| 5139 | bool SNaN, |
| 5140 | llvm::APFloat &Result) { |
| 5141 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 5142 | if (!S) return false; |
| 5143 | |
| 5144 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 5145 | |
| 5146 | llvm::APInt fill; |
| 5147 | |
| 5148 | // Treat empty strings as if they were zero. |
| 5149 | if (S->getString().empty()) |
| 5150 | fill = llvm::APInt(32, 0); |
| 5151 | else if (S->getString().getAsInteger(0, fill)) |
| 5152 | return false; |
| 5153 | |
| 5154 | if (SNaN) |
| 5155 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 5156 | else |
| 5157 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 5158 | return true; |
| 5159 | } |
| 5160 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5161 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5162 | switch (E->isBuiltinCall()) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5163 | default: |
| 5164 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 5165 | |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5166 | case Builtin::BI__builtin_huge_val: |
| 5167 | case Builtin::BI__builtin_huge_valf: |
| 5168 | case Builtin::BI__builtin_huge_vall: |
| 5169 | case Builtin::BI__builtin_inf: |
| 5170 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 5171 | case Builtin::BI__builtin_infl: { |
| 5172 | const llvm::fltSemantics &Sem = |
| 5173 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 34a74ab | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 5174 | Result = llvm::APFloat::getInf(Sem); |
| 5175 | return true; |
Daniel Dunbar | 7cbed03 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 5176 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5177 | |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5178 | case Builtin::BI__builtin_nans: |
| 5179 | case Builtin::BI__builtin_nansf: |
| 5180 | case Builtin::BI__builtin_nansl: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5181 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 5182 | true, Result)) |
| 5183 | return Error(E); |
| 5184 | return true; |
John McCall | db7b72a | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 5185 | |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 5186 | case Builtin::BI__builtin_nan: |
| 5187 | case Builtin::BI__builtin_nanf: |
| 5188 | case Builtin::BI__builtin_nanl: |
Mike Stump | 4572bab | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 5189 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 9e62171 | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 5190 | // can't constant fold it. |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5191 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 5192 | false, Result)) |
| 5193 | return Error(E); |
| 5194 | return true; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5195 | |
| 5196 | case Builtin::BI__builtin_fabs: |
| 5197 | case Builtin::BI__builtin_fabsf: |
| 5198 | case Builtin::BI__builtin_fabsl: |
| 5199 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 5200 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5201 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5202 | if (Result.isNegative()) |
| 5203 | Result.changeSign(); |
| 5204 | return true; |
| 5205 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5206 | case Builtin::BI__builtin_copysign: |
| 5207 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5208 | case Builtin::BI__builtin_copysignl: { |
| 5209 | APFloat RHS(0.); |
| 5210 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 5211 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 5212 | return false; |
| 5213 | Result.copySign(RHS); |
| 5214 | return true; |
| 5215 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5216 | } |
| 5217 | } |
| 5218 | |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5219 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5220 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 5221 | ComplexValue CV; |
| 5222 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 5223 | return false; |
| 5224 | Result = CV.FloatReal; |
| 5225 | return true; |
| 5226 | } |
| 5227 | |
| 5228 | return Visit(E->getSubExpr()); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5232 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 5233 | ComplexValue CV; |
| 5234 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 5235 | return false; |
| 5236 | Result = CV.FloatImag; |
| 5237 | return true; |
| 5238 | } |
| 5239 | |
Richard Smith | 8327fad | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5240 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 43efa31 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 5241 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 5242 | Result = llvm::APFloat::getZero(Sem); |
John McCall | abd3a85 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 5243 | return true; |
| 5244 | } |
| 5245 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5246 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5247 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5248 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5249 | case UO_Plus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 5250 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5251 | case UO_Minus: |
Richard Smith | 7993e8a | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 5252 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 5253 | return false; |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5254 | Result.changeSign(); |
| 5255 | return true; |
| 5256 | } |
| 5257 | } |
Chris Lattner | 019f4e8 | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 5258 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5259 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5260 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 5261 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 7f92f03 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 5262 | |
Daniel Dunbar | 5db4b3f | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 5263 | APFloat RHS(0.0); |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5264 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
| 5265 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5266 | return false; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5267 | if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK) |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5268 | return false; |
| 5269 | |
| 5270 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5271 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5272 | case BO_Mul: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5273 | Result.multiply(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5274 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5275 | case BO_Add: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5276 | Result.add(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5277 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5278 | case BO_Sub: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5279 | Result.subtract(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5280 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5281 | case BO_Div: |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5282 | Result.divide(RHS, APFloat::rmNearestTiesToEven); |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5283 | break; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5284 | } |
Richard Smith | 7b48a29 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 5285 | |
| 5286 | if (Result.isInfinity() || Result.isNaN()) |
| 5287 | CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN(); |
| 5288 | return true; |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
| 5291 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 5292 | Result = E->getValue(); |
| 5293 | return true; |
| 5294 | } |
| 5295 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5296 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5297 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5298 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5299 | switch (E->getCastKind()) { |
| 5300 | default: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5301 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5302 | |
| 5303 | case CK_IntegralToFloating: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5304 | APSInt IntResult; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5305 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 5306 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 5307 | E->getType(), Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5308 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5309 | |
| 5310 | case CK_FloatingCast: { |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5311 | if (!Visit(SubExpr)) |
| 5312 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5313 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 5314 | Result); |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5315 | } |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5316 | |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5317 | case CK_FloatingComplexToReal: { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5318 | ComplexValue V; |
| 5319 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 5320 | return false; |
| 5321 | Result = V.getComplexFloatReal(); |
| 5322 | return true; |
| 5323 | } |
Eli Friedman | 2a523ee | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 5324 | } |
Eli Friedman | 4efaa27 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5325 | } |
| 5326 | |
Eli Friedman | d8bfe7f | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 5327 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5328 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5329 | //===----------------------------------------------------------------------===// |
| 5330 | |
| 5331 | namespace { |
Benjamin Kramer | 770b4a8 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5332 | class ComplexExprEvaluator |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5333 | : public ExprEvaluatorBase<ComplexExprEvaluator, bool> { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5334 | ComplexValue &Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5335 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5336 | public: |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5337 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5338 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 5339 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5340 | bool Success(const CCValue &V, const Expr *e) { |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5341 | Result.setFrom(V); |
| 5342 | return true; |
| 5343 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5344 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5345 | bool ZeroInitialization(const Expr *E); |
| 5346 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5347 | //===--------------------------------------------------------------------===// |
| 5348 | // Visitor Methods |
| 5349 | //===--------------------------------------------------------------------===// |
| 5350 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5351 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5352 | bool VisitCastExpr(const CastExpr *E); |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5353 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5354 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5355 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5356 | }; |
| 5357 | } // end anonymous namespace |
| 5358 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5359 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 5360 | EvalInfo &Info) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5361 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5362 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5365 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Eli Friedman | f6c17a4 | 2012-01-13 23:34:56 +0000 | [diff] [blame] | 5366 | QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType(); |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5367 | if (ElemTy->isRealFloatingType()) { |
| 5368 | Result.makeComplexFloat(); |
| 5369 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 5370 | Result.FloatReal = Zero; |
| 5371 | Result.FloatImag = Zero; |
| 5372 | } else { |
| 5373 | Result.makeComplexInt(); |
| 5374 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 5375 | Result.IntReal = Zero; |
| 5376 | Result.IntImag = Zero; |
| 5377 | } |
| 5378 | return true; |
| 5379 | } |
| 5380 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5381 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 5382 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5383 | |
| 5384 | if (SubExpr->getType()->isRealFloatingType()) { |
| 5385 | Result.makeComplexFloat(); |
| 5386 | APFloat &Imag = Result.FloatImag; |
| 5387 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 5388 | return false; |
| 5389 | |
| 5390 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 5391 | return true; |
| 5392 | } else { |
| 5393 | assert(SubExpr->getType()->isIntegerType() && |
| 5394 | "Unexpected imaginary literal."); |
| 5395 | |
| 5396 | Result.makeComplexInt(); |
| 5397 | APSInt &Imag = Result.IntImag; |
| 5398 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 5399 | return false; |
| 5400 | |
| 5401 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 5402 | return true; |
| 5403 | } |
| 5404 | } |
| 5405 | |
Peter Collingbourne | 8cad304 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5406 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5407 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5408 | switch (E->getCastKind()) { |
| 5409 | case CK_BitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5410 | case CK_BaseToDerived: |
| 5411 | case CK_DerivedToBase: |
| 5412 | case CK_UncheckedDerivedToBase: |
| 5413 | case CK_Dynamic: |
| 5414 | case CK_ToUnion: |
| 5415 | case CK_ArrayToPointerDecay: |
| 5416 | case CK_FunctionToPointerDecay: |
| 5417 | case CK_NullToPointer: |
| 5418 | case CK_NullToMemberPointer: |
| 5419 | case CK_BaseToDerivedMemberPointer: |
| 5420 | case CK_DerivedToBaseMemberPointer: |
| 5421 | case CK_MemberPointerToBoolean: |
| 5422 | case CK_ConstructorConversion: |
| 5423 | case CK_IntegralToPointer: |
| 5424 | case CK_PointerToIntegral: |
| 5425 | case CK_PointerToBoolean: |
| 5426 | case CK_ToVoid: |
| 5427 | case CK_VectorSplat: |
| 5428 | case CK_IntegralCast: |
| 5429 | case CK_IntegralToBoolean: |
| 5430 | case CK_IntegralToFloating: |
| 5431 | case CK_FloatingToIntegral: |
| 5432 | case CK_FloatingToBoolean: |
| 5433 | case CK_FloatingCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5434 | case CK_CPointerToObjCPointerCast: |
| 5435 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5436 | case CK_AnyPointerToBlockPointerCast: |
| 5437 | case CK_ObjCObjectLValueCast: |
| 5438 | case CK_FloatingComplexToReal: |
| 5439 | case CK_FloatingComplexToBoolean: |
| 5440 | case CK_IntegralComplexToReal: |
| 5441 | case CK_IntegralComplexToBoolean: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5442 | case CK_ARCProduceObject: |
| 5443 | case CK_ARCConsumeObject: |
| 5444 | case CK_ARCReclaimReturnedObject: |
| 5445 | case CK_ARCExtendBlockObject: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5446 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 5447 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5448 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5449 | case CK_AtomicToNonAtomic: |
| 5450 | case CK_NonAtomicToAtomic: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5451 | case CK_NoOp: |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5452 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5453 | |
| 5454 | case CK_Dependent: |
Eli Friedman | 46a5232 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5455 | case CK_LValueBitCast: |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5456 | case CK_UserDefinedConversion: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5457 | return Error(E); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5458 | |
| 5459 | case CK_FloatingRealToComplex: { |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5460 | APFloat &Real = Result.FloatReal; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5461 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5462 | return false; |
| 5463 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5464 | Result.makeComplexFloat(); |
| 5465 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 5466 | return true; |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5467 | } |
| 5468 | |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5469 | case CK_FloatingComplexCast: { |
| 5470 | if (!Visit(E->getSubExpr())) |
| 5471 | return false; |
| 5472 | |
| 5473 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5474 | QualType From |
| 5475 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5476 | |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5477 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 5478 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
| 5481 | case CK_FloatingComplexToIntegralComplex: { |
| 5482 | if (!Visit(E->getSubExpr())) |
| 5483 | return false; |
| 5484 | |
| 5485 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5486 | QualType From |
| 5487 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5488 | Result.makeComplexInt(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5489 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 5490 | To, Result.IntReal) && |
| 5491 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 5492 | To, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | case CK_IntegralRealToComplex: { |
| 5496 | APSInt &Real = Result.IntReal; |
| 5497 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 5498 | return false; |
| 5499 | |
| 5500 | Result.makeComplexInt(); |
| 5501 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 5502 | return true; |
| 5503 | } |
| 5504 | |
| 5505 | case CK_IntegralComplexCast: { |
| 5506 | if (!Visit(E->getSubExpr())) |
| 5507 | return false; |
| 5508 | |
| 5509 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5510 | QualType From |
| 5511 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5512 | |
Richard Smith | f72fccf | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 5513 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 5514 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5515 | return true; |
| 5516 | } |
| 5517 | |
| 5518 | case CK_IntegralComplexToFloatingComplex: { |
| 5519 | if (!Visit(E->getSubExpr())) |
| 5520 | return false; |
| 5521 | |
| 5522 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 5523 | QualType From |
| 5524 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 5525 | Result.makeComplexFloat(); |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5526 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 5527 | To, Result.FloatReal) && |
| 5528 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 5529 | To, Result.FloatImag); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5530 | } |
| 5531 | } |
| 5532 | |
| 5533 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | b2dc7f5 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5536 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5537 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 2ad226b | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 5538 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 5539 | |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5540 | bool LHSOK = Visit(E->getLHS()); |
| 5541 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5542 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5543 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5544 | ComplexValue RHS; |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5545 | if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5546 | return false; |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5547 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5548 | assert(Result.isComplexFloat() == RHS.isComplexFloat() && |
| 5549 | "Invalid operands to binary operator."); |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5550 | switch (E->getOpcode()) { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5551 | default: return Error(E); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5552 | case BO_Add: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5553 | if (Result.isComplexFloat()) { |
| 5554 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 5555 | APFloat::rmNearestTiesToEven); |
| 5556 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 5557 | APFloat::rmNearestTiesToEven); |
| 5558 | } else { |
| 5559 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 5560 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 5561 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5562 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5563 | case BO_Sub: |
Daniel Dunbar | a5fd07b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 5564 | if (Result.isComplexFloat()) { |
| 5565 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 5566 | APFloat::rmNearestTiesToEven); |
| 5567 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 5568 | APFloat::rmNearestTiesToEven); |
| 5569 | } else { |
| 5570 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 5571 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 5572 | } |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5573 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5574 | case BO_Mul: |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5575 | if (Result.isComplexFloat()) { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5576 | ComplexValue LHS = Result; |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5577 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 5578 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 5579 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 5580 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5581 | |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5582 | APFloat Tmp = LHS_r; |
| 5583 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5584 | Result.getComplexFloatReal() = Tmp; |
| 5585 | Tmp = LHS_i; |
| 5586 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5587 | Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 5588 | |
| 5589 | Tmp = LHS_r; |
| 5590 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5591 | Result.getComplexFloatImag() = Tmp; |
| 5592 | Tmp = LHS_i; |
| 5593 | Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5594 | Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven); |
| 5595 | } else { |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5596 | ComplexValue LHS = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5597 | Result.getComplexIntReal() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5598 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 5599 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5600 | Result.getComplexIntImag() = |
Daniel Dunbar | 3f27987 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 5601 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 5602 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 5603 | } |
| 5604 | break; |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5605 | case BO_Div: |
| 5606 | if (Result.isComplexFloat()) { |
| 5607 | ComplexValue LHS = Result; |
| 5608 | APFloat &LHS_r = LHS.getComplexFloatReal(); |
| 5609 | APFloat &LHS_i = LHS.getComplexFloatImag(); |
| 5610 | APFloat &RHS_r = RHS.getComplexFloatReal(); |
| 5611 | APFloat &RHS_i = RHS.getComplexFloatImag(); |
| 5612 | APFloat &Res_r = Result.getComplexFloatReal(); |
| 5613 | APFloat &Res_i = Result.getComplexFloatImag(); |
| 5614 | |
| 5615 | APFloat Den = RHS_r; |
| 5616 | Den.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5617 | APFloat Tmp = RHS_i; |
| 5618 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5619 | Den.add(Tmp, APFloat::rmNearestTiesToEven); |
| 5620 | |
| 5621 | Res_r = LHS_r; |
| 5622 | Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5623 | Tmp = LHS_i; |
| 5624 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5625 | Res_r.add(Tmp, APFloat::rmNearestTiesToEven); |
| 5626 | Res_r.divide(Den, APFloat::rmNearestTiesToEven); |
| 5627 | |
| 5628 | Res_i = LHS_i; |
| 5629 | Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven); |
| 5630 | Tmp = LHS_r; |
| 5631 | Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven); |
| 5632 | Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven); |
| 5633 | Res_i.divide(Den, APFloat::rmNearestTiesToEven); |
| 5634 | } else { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5635 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 5636 | return Error(E, diag::note_expr_divide_by_zero); |
| 5637 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5638 | ComplexValue LHS = Result; |
| 5639 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 5640 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 5641 | Result.getComplexIntReal() = |
| 5642 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 5643 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 5644 | Result.getComplexIntImag() = |
| 5645 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 5646 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 5647 | } |
| 5648 | break; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5649 | } |
| 5650 | |
John McCall | f4cf1a1 | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 5651 | return true; |
Anders Carlsson | ccc3fce | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5654 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 5655 | // Get the operand value into 'Result'. |
| 5656 | if (!Visit(E->getSubExpr())) |
| 5657 | return false; |
| 5658 | |
| 5659 | switch (E->getOpcode()) { |
| 5660 | default: |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5661 | return Error(E); |
Abramo Bagnara | 96fc8e4 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 5662 | case UO_Extension: |
| 5663 | return true; |
| 5664 | case UO_Plus: |
| 5665 | // The result is always just the subexpr. |
| 5666 | return true; |
| 5667 | case UO_Minus: |
| 5668 | if (Result.isComplexFloat()) { |
| 5669 | Result.getComplexFloatReal().changeSign(); |
| 5670 | Result.getComplexFloatImag().changeSign(); |
| 5671 | } |
| 5672 | else { |
| 5673 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 5674 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 5675 | } |
| 5676 | return true; |
| 5677 | case UO_Not: |
| 5678 | if (Result.isComplexFloat()) |
| 5679 | Result.getComplexFloatImag().changeSign(); |
| 5680 | else |
| 5681 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 5682 | return true; |
| 5683 | } |
| 5684 | } |
| 5685 | |
Eli Friedman | 7ead5c7 | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 5686 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 5687 | if (E->getNumInits() == 2) { |
| 5688 | if (E->getType()->isComplexType()) { |
| 5689 | Result.makeComplexFloat(); |
| 5690 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 5691 | return false; |
| 5692 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 5693 | return false; |
| 5694 | } else { |
| 5695 | Result.makeComplexInt(); |
| 5696 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 5697 | return false; |
| 5698 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 5699 | return false; |
| 5700 | } |
| 5701 | return true; |
| 5702 | } |
| 5703 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 5704 | } |
| 5705 | |
Anders Carlsson | 9ad16ae | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 5706 | //===----------------------------------------------------------------------===// |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 5707 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 5708 | // comma operator |
| 5709 | //===----------------------------------------------------------------------===// |
| 5710 | |
| 5711 | namespace { |
| 5712 | class VoidExprEvaluator |
| 5713 | : public ExprEvaluatorBase<VoidExprEvaluator, bool> { |
| 5714 | public: |
| 5715 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 5716 | |
| 5717 | bool Success(const CCValue &V, const Expr *e) { return true; } |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 5718 | |
| 5719 | bool VisitCastExpr(const CastExpr *E) { |
| 5720 | switch (E->getCastKind()) { |
| 5721 | default: |
| 5722 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5723 | case CK_ToVoid: |
| 5724 | VisitIgnoredValue(E->getSubExpr()); |
| 5725 | return true; |
| 5726 | } |
| 5727 | } |
| 5728 | }; |
| 5729 | } // end anonymous namespace |
| 5730 | |
| 5731 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 5732 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 5733 | return VoidExprEvaluator(Info).Visit(E); |
| 5734 | } |
| 5735 | |
| 5736 | //===----------------------------------------------------------------------===// |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5737 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | f5eeb05 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5738 | //===----------------------------------------------------------------------===// |
| 5739 | |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5740 | static bool Evaluate(CCValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5741 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 5742 | // are. |
| 5743 | if (E->isGLValue() || E->getType()->isFunctionType()) { |
| 5744 | LValue LV; |
| 5745 | if (!EvaluateLValue(E, LV, Info)) |
| 5746 | return false; |
| 5747 | LV.moveInto(Result); |
| 5748 | } else if (E->getType()->isVectorType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 5749 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5750 | return false; |
Douglas Gregor | 575a1c9 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5751 | } else if (E->getType()->isIntegralOrEnumerationType()) { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 5752 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 5753 | return false; |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5754 | } else if (E->getType()->hasPointerRepresentation()) { |
| 5755 | LValue LV; |
| 5756 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 5757 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 5758 | LV.moveInto(Result); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5759 | } else if (E->getType()->isRealFloatingType()) { |
| 5760 | llvm::APFloat F(0.0); |
| 5761 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 5762 | return false; |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5763 | Result = CCValue(F); |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5764 | } else if (E->getType()->isAnyComplexType()) { |
| 5765 | ComplexValue C; |
| 5766 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 5767 | return false; |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 5768 | C.moveInto(Result); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5769 | } else if (E->getType()->isMemberPointerType()) { |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5770 | MemberPtr P; |
| 5771 | if (!EvaluateMemberPointer(E, P, Info)) |
| 5772 | return false; |
| 5773 | P.moveInto(Result); |
| 5774 | return true; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5775 | } else if (E->getType()->isArrayType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5776 | LValue LV; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5777 | LV.set(E, Info.CurrentCall); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5778 | if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
Richard Smith | cc5d4f6 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5779 | return false; |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5780 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5781 | } else if (E->getType()->isRecordType()) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5782 | LValue LV; |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5783 | LV.set(E, Info.CurrentCall); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5784 | if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info)) |
| 5785 | return false; |
| 5786 | Result = Info.CurrentCall->Temporaries[E]; |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 5787 | } else if (E->getType()->isVoidType()) { |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5788 | if (Info.getLangOpts().CPlusPlus0x) |
| 5789 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_nonliteral) |
| 5790 | << E->getType(); |
| 5791 | else |
| 5792 | Info.CCEDiag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | aa9c350 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 5793 | if (!EvaluateVoid(E, Info)) |
| 5794 | return false; |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5795 | } else if (Info.getLangOpts().CPlusPlus0x) { |
| 5796 | Info.Diag(E->getExprLoc(), diag::note_constexpr_nonliteral) << E->getType(); |
| 5797 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5798 | } else { |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 5799 | Info.Diag(E->getExprLoc(), diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 9d4c157 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 5800 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5801 | } |
Anders Carlsson | 6dde0d5 | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 5802 | |
Anders Carlsson | 5b45d4e | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 5803 | return true; |
| 5804 | } |
| 5805 | |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5806 | /// EvaluateConstantExpression - Evaluate an expression as a constant expression |
| 5807 | /// in-place in an APValue. In some cases, the in-place evaluation is essential, |
| 5808 | /// since later initializers for an object can indirectly refer to subobjects |
| 5809 | /// which were initialized earlier. |
| 5810 | static bool EvaluateConstantExpression(APValue &Result, EvalInfo &Info, |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5811 | const LValue &This, const Expr *E, |
| 5812 | CheckConstantExpressionKind CCEK) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5813 | if (!CheckLiteralType(Info, E)) |
| 5814 | return false; |
| 5815 | |
| 5816 | if (E->isRValue()) { |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5817 | // Evaluate arrays and record types in-place, so that later initializers can |
| 5818 | // refer to earlier-initialized members of the object. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5819 | if (E->getType()->isArrayType()) |
| 5820 | return EvaluateArray(E, This, Result, Info); |
| 5821 | else if (E->getType()->isRecordType()) |
| 5822 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5823 | } |
| 5824 | |
| 5825 | // For any other type, in-place evaluation is unimportant. |
| 5826 | CCValue CoreConstResult; |
| 5827 | return Evaluate(CoreConstResult, Info, E) && |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5828 | CheckConstantExpression(Info, E, CoreConstResult, Result, CCEK); |
Richard Smith | 69c2c50 | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5831 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 5832 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 5833 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5834 | if (!CheckLiteralType(Info, E)) |
| 5835 | return false; |
| 5836 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5837 | CCValue Value; |
| 5838 | if (!::Evaluate(Value, Info, E)) |
| 5839 | return false; |
| 5840 | |
| 5841 | if (E->isGLValue()) { |
| 5842 | LValue LV; |
| 5843 | LV.setFrom(Value); |
| 5844 | if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Value)) |
| 5845 | return false; |
| 5846 | } |
| 5847 | |
| 5848 | // Check this core constant expression is a constant expression, and if so, |
| 5849 | // convert it to one. |
| 5850 | return CheckConstantExpression(Info, E, Value, Result); |
| 5851 | } |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5852 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5853 | /// 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] | 5854 | /// any crazy technique (that has nothing to do with language standards) that |
| 5855 | /// 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] | 5856 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 5857 | /// will be applied to the result. |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5858 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | ee19f43 | 2011-12-10 01:10:13 +0000 | [diff] [blame] | 5859 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 5860 | // containing vast quantities of these. |
| 5861 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) { |
| 5862 | Result.Val = APValue(APSInt(L->getValue(), |
| 5863 | L->getType()->isUnsignedIntegerType())); |
| 5864 | return true; |
| 5865 | } |
| 5866 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 5867 | // FIXME: Evaluating values of large array and record types can cause |
| 5868 | // performance problems. Only do so in C++11 for now. |
Richard Smith | e24f5fc | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5869 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
| 5870 | !Ctx.getLangOptions().CPlusPlus0x) |
Richard Smith | 1445bba | 2011-11-10 03:30:42 +0000 | [diff] [blame] | 5871 | return false; |
| 5872 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5873 | EvalInfo Info(Ctx, Result); |
| 5874 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5875 | } |
| 5876 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5877 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 5878 | const ASTContext &Ctx) const { |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5879 | EvalResult Scratch; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5880 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | b4e85ed | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5881 | HandleConversionToBool(CCValue(const_cast<ASTContext&>(Ctx), |
| 5882 | Scratch.Val, CCValue::GlobalValue()), |
Richard Smith | 47a1eed | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5883 | Result); |
John McCall | cd7a445 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5886 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 5887 | SideEffectsKind AllowSideEffects) const { |
| 5888 | if (!getType()->isIntegralOrEnumerationType()) |
| 5889 | return false; |
| 5890 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5891 | EvalResult ExprResult; |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 5892 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
| 5893 | (!AllowSideEffects && ExprResult.HasSideEffects)) |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5894 | return false; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5895 | |
Richard Smith | c49bd11 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5896 | Result = ExprResult.Val.getInt(); |
| 5897 | return true; |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5900 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Anders Carlsson | 1b78276 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 5901 | EvalInfo Info(Ctx, Result); |
| 5902 | |
John McCall | efdb83e | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5903 | LValue LV; |
Richard Smith | 9a17a68 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 5904 | return EvaluateLValue(this, LV, Info) && !Result.HasSideEffects && |
Richard Smith | c1c5f27 | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5905 | CheckLValueConstantExpression(Info, this, LV, Result.Val, |
| 5906 | CCEK_Constant); |
Eli Friedman | b2f295c | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 5907 | } |
| 5908 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 5909 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 5910 | const VarDecl *VD, |
| 5911 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 5912 | // FIXME: Evaluating initializers for large array and record types can cause |
| 5913 | // performance problems. Only do so in C++11 for now. |
| 5914 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
| 5915 | !Ctx.getLangOptions().CPlusPlus0x) |
| 5916 | return false; |
| 5917 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 5918 | Expr::EvalStatus EStatus; |
| 5919 | EStatus.Diag = &Notes; |
| 5920 | |
| 5921 | EvalInfo InitInfo(Ctx, EStatus); |
| 5922 | InitInfo.setEvaluatingDecl(VD, Value); |
| 5923 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5924 | if (!CheckLiteralType(InitInfo, this)) |
| 5925 | return false; |
| 5926 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 5927 | LValue LVal; |
| 5928 | LVal.set(VD); |
| 5929 | |
Richard Smith | 5120188 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5930 | // C++11 [basic.start.init]p2: |
| 5931 | // Variables with static storage duration or thread storage duration shall be |
| 5932 | // zero-initialized before any other initialization takes place. |
| 5933 | // This behavior is not present in C. |
| 5934 | if (Ctx.getLangOptions().CPlusPlus && !VD->hasLocalStorage() && |
| 5935 | !VD->getType()->isReferenceType()) { |
| 5936 | ImplicitValueInitExpr VIE(VD->getType()); |
| 5937 | if (!EvaluateConstantExpression(Value, InitInfo, LVal, &VIE)) |
| 5938 | return false; |
| 5939 | } |
| 5940 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 5941 | return EvaluateConstantExpression(Value, InitInfo, LVal, this) && |
| 5942 | !EStatus.HasSideEffects; |
| 5943 | } |
| 5944 | |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5945 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 5946 | /// constant folded, but discard the result. |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5947 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 4fdfb09 | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 5948 | EvalResult Result; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5949 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | 45b6b9d | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 5950 | } |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 5951 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5952 | bool Expr::HasSideEffects(const ASTContext &Ctx) const { |
Richard Smith | 1e12c59 | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 5953 | return HasSideEffect(Ctx).Visit(this); |
Fariborz Jahanian | 393c247 | 2009-11-05 18:03:03 +0000 | [diff] [blame] | 5954 | } |
| 5955 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5956 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 5957 | EvalResult EvalResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5958 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 5959 | (void)Result; |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 5960 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 5961 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 5962 | |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 5963 | return EvalResult.Val.getInt(); |
Anders Carlsson | 51fe996 | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 5964 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5965 | |
Abramo Bagnara | e17a643 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 5966 | bool Expr::EvalResult::isGlobalLValue() const { |
| 5967 | assert(Val.isLValue()); |
| 5968 | return IsGlobalLValue(Val.getLValueBase()); |
| 5969 | } |
| 5970 | |
| 5971 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5972 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 5973 | /// an integer constant expression. |
| 5974 | |
| 5975 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 5976 | /// comma, etc |
| 5977 | /// |
| 5978 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 5979 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 5980 | /// cast+dereference. |
| 5981 | |
| 5982 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 5983 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 5984 | // Note that to reduce code duplication, this helper does no evaluation |
| 5985 | // itself; the caller checks whether the expression is evaluatable, and |
| 5986 | // in the rare cases where CheckICE actually cares about the evaluated |
| 5987 | // value, it calls into Evalute. |
| 5988 | // |
| 5989 | // Meanings of Val: |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 5990 | // 0: This expression is an ICE. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5991 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 5992 | // a legal subexpression for an ICE. This return value is used to handle |
| 5993 | // the comma operator in C99 mode. |
| 5994 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 5995 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 5996 | namespace { |
| 5997 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 5998 | struct ICEDiag { |
| 5999 | unsigned Val; |
| 6000 | SourceLocation Loc; |
| 6001 | |
| 6002 | public: |
| 6003 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 6004 | ICEDiag() : Val(0) {} |
| 6005 | }; |
| 6006 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 6007 | } |
| 6008 | |
| 6009 | static ICEDiag NoDiag() { return ICEDiag(); } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6010 | |
| 6011 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 6012 | Expr::EvalResult EVResult; |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6013 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6014 | !EVResult.Val.isInt()) { |
| 6015 | return ICEDiag(2, E->getLocStart()); |
| 6016 | } |
| 6017 | return NoDiag(); |
| 6018 | } |
| 6019 | |
| 6020 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 6021 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 6022 | if (!E->getType()->isIntegralOrEnumerationType()) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6023 | return ICEDiag(2, E->getLocStart()); |
| 6024 | } |
| 6025 | |
| 6026 | switch (E->getStmtClass()) { |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 6027 | #define ABSTRACT_STMT(Node) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6028 | #define STMT(Node, Base) case Expr::Node##Class: |
| 6029 | #define EXPR(Node, Base) |
| 6030 | #include "clang/AST/StmtNodes.inc" |
| 6031 | case Expr::PredefinedExprClass: |
| 6032 | case Expr::FloatingLiteralClass: |
| 6033 | case Expr::ImaginaryLiteralClass: |
| 6034 | case Expr::StringLiteralClass: |
| 6035 | case Expr::ArraySubscriptExprClass: |
| 6036 | case Expr::MemberExprClass: |
| 6037 | case Expr::CompoundAssignOperatorClass: |
| 6038 | case Expr::CompoundLiteralExprClass: |
| 6039 | case Expr::ExtVectorElementExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6040 | case Expr::DesignatedInitExprClass: |
| 6041 | case Expr::ImplicitValueInitExprClass: |
| 6042 | case Expr::ParenListExprClass: |
| 6043 | case Expr::VAArgExprClass: |
| 6044 | case Expr::AddrLabelExprClass: |
| 6045 | case Expr::StmtExprClass: |
| 6046 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 6047 | case Expr::CUDAKernelCallExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6048 | case Expr::CXXDynamicCastExprClass: |
| 6049 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 9be8840 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 6050 | case Expr::CXXUuidofExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6051 | case Expr::CXXNullPtrLiteralExprClass: |
| 6052 | case Expr::CXXThisExprClass: |
| 6053 | case Expr::CXXThrowExprClass: |
| 6054 | case Expr::CXXNewExprClass: |
| 6055 | case Expr::CXXDeleteExprClass: |
| 6056 | case Expr::CXXPseudoDestructorExprClass: |
| 6057 | case Expr::UnresolvedLookupExprClass: |
| 6058 | case Expr::DependentScopeDeclRefExprClass: |
| 6059 | case Expr::CXXConstructExprClass: |
| 6060 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 6061 | case Expr::ExprWithCleanupsClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6062 | case Expr::CXXTemporaryObjectExprClass: |
| 6063 | case Expr::CXXUnresolvedConstructExprClass: |
| 6064 | case Expr::CXXDependentScopeMemberExprClass: |
| 6065 | case Expr::UnresolvedMemberExprClass: |
| 6066 | case Expr::ObjCStringLiteralClass: |
| 6067 | case Expr::ObjCEncodeExprClass: |
| 6068 | case Expr::ObjCMessageExprClass: |
| 6069 | case Expr::ObjCSelectorExprClass: |
| 6070 | case Expr::ObjCProtocolExprClass: |
| 6071 | case Expr::ObjCIvarRefExprClass: |
| 6072 | case Expr::ObjCPropertyRefExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6073 | case Expr::ObjCIsaExprClass: |
| 6074 | case Expr::ShuffleVectorExprClass: |
| 6075 | case Expr::BlockExprClass: |
| 6076 | case Expr::BlockDeclRefExprClass: |
| 6077 | case Expr::NoStmtClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 6078 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 6079 | case Expr::PackExpansionExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 6080 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 6081 | case Expr::AsTypeExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6082 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 6083 | case Expr::MaterializeTemporaryExprClass: |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 6084 | case Expr::PseudoObjectExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 6085 | case Expr::AtomicExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 6086 | case Expr::InitListExprClass: |
Douglas Gregor | 01d0801 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 6087 | case Expr::LambdaExprClass: |
Sebastian Redl | cea8d96 | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 6088 | return ICEDiag(2, E->getLocStart()); |
| 6089 | |
Douglas Gregor | ee8aff0 | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 6090 | case Expr::SizeOfPackExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6091 | case Expr::GNUNullExprClass: |
| 6092 | // GCC considers the GNU __null value to be an integral constant expression. |
| 6093 | return NoDiag(); |
| 6094 | |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 6095 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 6096 | return |
| 6097 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 6098 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6099 | case Expr::ParenExprClass: |
| 6100 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 6101 | case Expr::GenericSelectionExprClass: |
| 6102 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6103 | case Expr::IntegerLiteralClass: |
| 6104 | case Expr::CharacterLiteralClass: |
| 6105 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 6106 | case Expr::CXXScalarValueInitExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6107 | case Expr::UnaryTypeTraitExprClass: |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 6108 | case Expr::BinaryTypeTraitExprClass: |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 6109 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 6110 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 6111 | case Expr::CXXNoexceptExprClass: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6112 | return NoDiag(); |
| 6113 | case Expr::CallExprClass: |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 6114 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6115 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 6116 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 6117 | // contain an operand of (pointer to) function type. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6118 | const CallExpr *CE = cast<CallExpr>(E); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6119 | if (CE->isBuiltinCall()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6120 | return CheckEvalInICE(E, Ctx); |
| 6121 | return ICEDiag(2, E->getLocStart()); |
| 6122 | } |
| 6123 | case Expr::DeclRefExprClass: |
| 6124 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 6125 | return NoDiag(); |
Richard Smith | 03f9611 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 6126 | if (Ctx.getLangOptions().CPlusPlus && IsConstNonVolatile(E->getType())) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6127 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 6128 | |
| 6129 | // Parameter variables are never constants. Without this check, |
| 6130 | // getAnyInitializer() can find a default argument, which leads |
| 6131 | // to chaos. |
| 6132 | if (isa<ParmVarDecl>(D)) |
| 6133 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 6134 | |
| 6135 | // C++ 7.1.5.1p2 |
| 6136 | // A variable of non-volatile const-qualified integral or enumeration |
| 6137 | // type initialized by an ICE can be used in ICEs. |
| 6138 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 6139 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
| 6140 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
| 6141 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 6142 | const VarDecl *VD; |
| 6143 | // Look for a declaration of this variable that has an initializer, and |
| 6144 | // check whether it is an ICE. |
| 6145 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 6146 | return NoDiag(); |
| 6147 | else |
| 6148 | return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6149 | } |
| 6150 | } |
| 6151 | return ICEDiag(2, E->getLocStart()); |
| 6152 | case Expr::UnaryOperatorClass: { |
| 6153 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 6154 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6155 | case UO_PostInc: |
| 6156 | case UO_PostDec: |
| 6157 | case UO_PreInc: |
| 6158 | case UO_PreDec: |
| 6159 | case UO_AddrOf: |
| 6160 | case UO_Deref: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6161 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 6162 | // subexpressions of constant expressions, but they can never be ICEs |
| 6163 | // because an ICE cannot contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6164 | return ICEDiag(2, E->getLocStart()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6165 | case UO_Extension: |
| 6166 | case UO_LNot: |
| 6167 | case UO_Plus: |
| 6168 | case UO_Minus: |
| 6169 | case UO_Not: |
| 6170 | case UO_Real: |
| 6171 | case UO_Imag: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6172 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6173 | } |
| 6174 | |
| 6175 | // OffsetOf falls through here. |
| 6176 | } |
| 6177 | case Expr::OffsetOfExprClass: { |
| 6178 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6179 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6180 | // "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] | 6181 | // compliance: we should warn earlier for offsetof expressions with |
| 6182 | // array subscripts that aren't ICEs, and if the array subscripts |
| 6183 | // are ICEs, the value of the offsetof must be an integer constant. |
| 6184 | return CheckEvalInICE(E, Ctx); |
| 6185 | } |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 6186 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 6187 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 6188 | if ((Exp->getKind() == UETT_SizeOf) && |
| 6189 | Exp->getTypeOfArgument()->isVariableArrayType()) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6190 | return ICEDiag(2, E->getLocStart()); |
| 6191 | return NoDiag(); |
| 6192 | } |
| 6193 | case Expr::BinaryOperatorClass: { |
| 6194 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 6195 | switch (Exp->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6196 | case BO_PtrMemD: |
| 6197 | case BO_PtrMemI: |
| 6198 | case BO_Assign: |
| 6199 | case BO_MulAssign: |
| 6200 | case BO_DivAssign: |
| 6201 | case BO_RemAssign: |
| 6202 | case BO_AddAssign: |
| 6203 | case BO_SubAssign: |
| 6204 | case BO_ShlAssign: |
| 6205 | case BO_ShrAssign: |
| 6206 | case BO_AndAssign: |
| 6207 | case BO_XorAssign: |
| 6208 | case BO_OrAssign: |
Richard Smith | 0583014 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 6209 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 6210 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 6211 | // contain an lvalue operand. |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6212 | return ICEDiag(2, E->getLocStart()); |
| 6213 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6214 | case BO_Mul: |
| 6215 | case BO_Div: |
| 6216 | case BO_Rem: |
| 6217 | case BO_Add: |
| 6218 | case BO_Sub: |
| 6219 | case BO_Shl: |
| 6220 | case BO_Shr: |
| 6221 | case BO_LT: |
| 6222 | case BO_GT: |
| 6223 | case BO_LE: |
| 6224 | case BO_GE: |
| 6225 | case BO_EQ: |
| 6226 | case BO_NE: |
| 6227 | case BO_And: |
| 6228 | case BO_Xor: |
| 6229 | case BO_Or: |
| 6230 | case BO_Comma: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6231 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 6232 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6233 | if (Exp->getOpcode() == BO_Div || |
| 6234 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 51f4708 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 6235 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6236 | // we don't evaluate one. |
John McCall | 3b332ab | 2011-02-26 08:27:17 +0000 | [diff] [blame] | 6237 | if (LHSResult.Val == 0 && RHSResult.Val == 0) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6238 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6239 | if (REval == 0) |
| 6240 | return ICEDiag(1, E->getLocStart()); |
| 6241 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6242 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6243 | if (LEval.isMinSignedValue()) |
| 6244 | return ICEDiag(1, E->getLocStart()); |
| 6245 | } |
| 6246 | } |
| 6247 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6248 | if (Exp->getOpcode() == BO_Comma) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6249 | if (Ctx.getLangOptions().C99) { |
| 6250 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 6251 | // if it isn't evaluated. |
| 6252 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 6253 | return ICEDiag(1, E->getLocStart()); |
| 6254 | } else { |
| 6255 | // In both C89 and C++, commas in ICEs are illegal. |
| 6256 | return ICEDiag(2, E->getLocStart()); |
| 6257 | } |
| 6258 | } |
| 6259 | if (LHSResult.Val >= RHSResult.Val) |
| 6260 | return LHSResult; |
| 6261 | return RHSResult; |
| 6262 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6263 | case BO_LAnd: |
| 6264 | case BO_LOr: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6265 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 6266 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 6267 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 6268 | // Rare case where the RHS has a comma "side-effect"; we need |
| 6269 | // to actually check the condition to see whether the side |
| 6270 | // with the comma is evaluated. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6271 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6272 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6273 | return RHSResult; |
| 6274 | return NoDiag(); |
| 6275 | } |
| 6276 | |
| 6277 | if (LHSResult.Val >= RHSResult.Val) |
| 6278 | return LHSResult; |
| 6279 | return RHSResult; |
| 6280 | } |
| 6281 | } |
| 6282 | } |
| 6283 | case Expr::ImplicitCastExprClass: |
| 6284 | case Expr::CStyleCastExprClass: |
| 6285 | case Expr::CXXFunctionalCastExprClass: |
| 6286 | case Expr::CXXStaticCastExprClass: |
| 6287 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | 32cb471 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 6288 | case Expr::CXXConstCastExprClass: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6289 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6290 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 2116b14 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 6291 | if (isa<ExplicitCastExpr>(E)) { |
| 6292 | if (const FloatingLiteral *FL |
| 6293 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 6294 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 6295 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 6296 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 6297 | bool Ignored; |
| 6298 | // If the value does not fit in the destination type, the behavior is |
| 6299 | // undefined, so we are not required to treat it as a constant |
| 6300 | // expression. |
| 6301 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 6302 | llvm::APFloat::rmTowardZero, |
| 6303 | &Ignored) & APFloat::opInvalidOp) |
| 6304 | return ICEDiag(2, E->getLocStart()); |
| 6305 | return NoDiag(); |
| 6306 | } |
| 6307 | } |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6308 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 6309 | case CK_LValueToRValue: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6310 | case CK_AtomicToNonAtomic: |
| 6311 | case CK_NonAtomicToAtomic: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6312 | case CK_NoOp: |
| 6313 | case CK_IntegralToBoolean: |
| 6314 | case CK_IntegralCast: |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6315 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6316 | default: |
Eli Friedman | eea0e81 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 6317 | return ICEDiag(2, E->getLocStart()); |
| 6318 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6319 | } |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6320 | case Expr::BinaryConditionalOperatorClass: { |
| 6321 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 6322 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
| 6323 | if (CommonResult.Val == 2) return CommonResult; |
| 6324 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 6325 | if (FalseResult.Val == 2) return FalseResult; |
| 6326 | if (CommonResult.Val == 1) return CommonResult; |
| 6327 | if (FalseResult.Val == 1 && |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6328 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6329 | return FalseResult; |
| 6330 | } |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6331 | case Expr::ConditionalOperatorClass: { |
| 6332 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 6333 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 6334 | // then only the true side is actually considered in an integer constant |
| 6335 | // expression, and it is fully evaluated. This is an important GNU |
| 6336 | // extension. See GCC PR38377 for discussion. |
| 6337 | if (const CallExpr *CallCE |
| 6338 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Richard Smith | 80d4b55 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6339 | if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p) |
| 6340 | return CheckEvalInICE(E, Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6341 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6342 | if (CondResult.Val == 2) |
| 6343 | return CondResult; |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 6344 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6345 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 6346 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | 63fe681 | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 6347 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6348 | if (TrueResult.Val == 2) |
| 6349 | return TrueResult; |
| 6350 | if (FalseResult.Val == 2) |
| 6351 | return FalseResult; |
| 6352 | if (CondResult.Val == 1) |
| 6353 | return CondResult; |
| 6354 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 6355 | return NoDiag(); |
| 6356 | // Rare case where the diagnostics depend on which side is evaluated |
| 6357 | // Note that if we get here, CondResult is 0, and at least one of |
| 6358 | // TrueResult and FalseResult is non-zero. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6359 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) { |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6360 | return FalseResult; |
| 6361 | } |
| 6362 | return TrueResult; |
| 6363 | } |
| 6364 | case Expr::CXXDefaultArgExprClass: |
| 6365 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| 6366 | case Expr::ChooseExprClass: { |
| 6367 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
| 6368 | } |
| 6369 | } |
| 6370 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 6371 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6374 | /// Evaluate an expression as a C++11 integral constant expression. |
| 6375 | static bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx, |
| 6376 | const Expr *E, |
| 6377 | llvm::APSInt *Value, |
| 6378 | SourceLocation *Loc) { |
| 6379 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 6380 | if (Loc) *Loc = E->getExprLoc(); |
| 6381 | return false; |
| 6382 | } |
| 6383 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6384 | APValue Result; |
| 6385 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6386 | return false; |
| 6387 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6388 | assert(Result.isInt() && "pointer cast to int is not an ICE"); |
| 6389 | if (Value) *Value = Result.getInt(); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6390 | return true; |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6391 | } |
| 6392 | |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6393 | bool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6394 | if (Ctx.getLangOptions().CPlusPlus0x) |
| 6395 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc); |
| 6396 | |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6397 | ICEDiag d = CheckICE(this, Ctx); |
| 6398 | if (d.Val != 0) { |
| 6399 | if (Loc) *Loc = d.Loc; |
| 6400 | return false; |
| 6401 | } |
Richard Smith | f48fdb0 | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6402 | return true; |
| 6403 | } |
| 6404 | |
| 6405 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx, |
| 6406 | SourceLocation *Loc, bool isEvaluated) const { |
| 6407 | if (Ctx.getLangOptions().CPlusPlus0x) |
| 6408 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 6409 | |
| 6410 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 6411 | return false; |
| 6412 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6413 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | d905f5a | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 6414 | return true; |
| 6415 | } |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6416 | |
| 6417 | bool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result, |
| 6418 | SourceLocation *Loc) const { |
| 6419 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 6420 | // issues. |
| 6421 | assert(Ctx.getLangOptions().CPlusPlus); |
| 6422 | |
| 6423 | Expr::EvalStatus Status; |
| 6424 | llvm::SmallVector<PartialDiagnosticAt, 8> Diags; |
| 6425 | Status.Diag = &Diags; |
| 6426 | EvalInfo Info(Ctx, Status); |
| 6427 | |
| 6428 | APValue Scratch; |
| 6429 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 6430 | |
| 6431 | if (!Diags.empty()) { |
| 6432 | IsConstExpr = false; |
| 6433 | if (Loc) *Loc = Diags[0].first; |
| 6434 | } else if (!IsConstExpr) { |
| 6435 | // FIXME: This shouldn't happen. |
| 6436 | if (Loc) *Loc = getExprLoc(); |
| 6437 | } |
| 6438 | |
| 6439 | return IsConstExpr; |
| 6440 | } |
Richard Smith | 745f514 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6441 | |
| 6442 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
| 6443 | llvm::SmallVectorImpl< |
| 6444 | PartialDiagnosticAt> &Diags) { |
| 6445 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 6446 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 6447 | // ASTs which we build for dependent expressions. |
| 6448 | if (FD->isDependentContext()) |
| 6449 | return true; |
| 6450 | |
| 6451 | Expr::EvalStatus Status; |
| 6452 | Status.Diag = &Diags; |
| 6453 | |
| 6454 | EvalInfo Info(FD->getASTContext(), Status); |
| 6455 | Info.CheckingPotentialConstantExpression = true; |
| 6456 | |
| 6457 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 6458 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0; |
| 6459 | |
| 6460 | // FIXME: Fabricate an arbitrary expression on the stack and pretend that it |
| 6461 | // is a temporary being used as the 'this' pointer. |
| 6462 | LValue This; |
| 6463 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
| 6464 | This.set(&VIE, Info.CurrentCall); |
| 6465 | |
| 6466 | APValue Scratch; |
| 6467 | ArrayRef<const Expr*> Args; |
| 6468 | |
| 6469 | SourceLocation Loc = FD->getLocation(); |
| 6470 | |
| 6471 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 6472 | HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); |
| 6473 | } else |
| 6474 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0, |
| 6475 | Args, FD->getBody(), Info, Scratch); |
| 6476 | |
| 6477 | return Diags.empty(); |
| 6478 | } |