Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
Richard Smith | 253c2a3 | 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 |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 26 | // (under the C++11 / C++1y rules only, at the moment), or, if folding failed |
| 27 | // too, why the expression could not be folded. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 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 | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "clang/AST/APValue.h" |
| 37 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 38 | #include "clang/AST/ASTDiagnostic.h" |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 39 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 40 | #include "clang/AST/Expr.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 41 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 1904f44 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 42 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 43 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 44 | #include "clang/Basic/Builtins.h" |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 45 | #include "clang/Basic/TargetInfo.h" |
Mike Stump | b807c9c | 2009-05-30 14:43:18 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 48 | #include <cstring> |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 49 | #include <functional> |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 50 | |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 51 | using namespace clang; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 52 | using llvm::APSInt; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 53 | using llvm::APFloat; |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 54 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 55 | static bool IsGlobalLValue(APValue::LValueBase B); |
| 56 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 57 | namespace { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 58 | struct LValue; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 59 | struct CallStackFrame; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 60 | struct EvalInfo; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 61 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 62 | static QualType getType(APValue::LValueBase B) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 63 | if (!B) return QualType(); |
| 64 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) |
| 65 | return D->getType(); |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 66 | |
| 67 | const Expr *Base = B.get<const Expr*>(); |
| 68 | |
| 69 | // For a materialized temporary, the type of the temporary we materialized |
| 70 | // may not be the type of the expression. |
| 71 | if (const MaterializeTemporaryExpr *MTE = |
| 72 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 73 | SmallVector<const Expr *, 2> CommaLHSs; |
| 74 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 75 | const Expr *Temp = MTE->GetTemporaryExpr(); |
| 76 | const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs, |
| 77 | Adjustments); |
| 78 | // Keep any cv-qualifiers from the reference if we generated a temporary |
| 79 | // for it. |
| 80 | if (Inner != Temp) |
| 81 | return Inner->getType(); |
| 82 | } |
| 83 | |
| 84 | return Base->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 87 | /// Get an LValue path entry, which is known to not be an array index, as a |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 88 | /// field or base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 89 | static |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 90 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 91 | APValue::BaseOrMemberType Value; |
| 92 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 93 | return Value; |
| 94 | } |
| 95 | |
| 96 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 97 | /// field declaration. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 98 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 99 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 100 | } |
| 101 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 102 | /// base class declaration. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 103 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 104 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 105 | } |
| 106 | /// Determine whether this LValue path entry for a base class names a virtual |
| 107 | /// base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 108 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 109 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 112 | /// Find the path length and type of the most-derived subobject in the given |
| 113 | /// path, and find the size of the containing array, if any. |
| 114 | static |
| 115 | unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base, |
| 116 | ArrayRef<APValue::LValuePathEntry> Path, |
| 117 | uint64_t &ArraySize, QualType &Type) { |
| 118 | unsigned MostDerivedLength = 0; |
| 119 | Type = Base; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 120 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 121 | if (Type->isArrayType()) { |
| 122 | const ConstantArrayType *CAT = |
| 123 | cast<ConstantArrayType>(Ctx.getAsArrayType(Type)); |
| 124 | Type = CAT->getElementType(); |
| 125 | ArraySize = CAT->getSize().getZExtValue(); |
| 126 | MostDerivedLength = I + 1; |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 127 | } else if (Type->isAnyComplexType()) { |
| 128 | const ComplexType *CT = Type->castAs<ComplexType>(); |
| 129 | Type = CT->getElementType(); |
| 130 | ArraySize = 2; |
| 131 | MostDerivedLength = I + 1; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 132 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 133 | Type = FD->getType(); |
| 134 | ArraySize = 0; |
| 135 | MostDerivedLength = I + 1; |
| 136 | } else { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 137 | // Path[I] describes a base class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 138 | ArraySize = 0; |
| 139 | } |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 140 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 141 | return MostDerivedLength; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 144 | // The order of this enum is important for diagnostics. |
| 145 | enum CheckSubobjectKind { |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 146 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 147 | CSK_This, CSK_Real, CSK_Imag |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 150 | /// A path from a glvalue to a subobject of that glvalue. |
| 151 | struct SubobjectDesignator { |
| 152 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 153 | /// lvalues can still be folded, but they are not core constant expressions |
| 154 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| 155 | bool Invalid : 1; |
| 156 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 157 | /// Is this a pointer one past the end of an object? |
| 158 | bool IsOnePastTheEnd : 1; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 159 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 160 | /// The length of the path to the most-derived object of which this is a |
| 161 | /// subobject. |
| 162 | unsigned MostDerivedPathLength : 30; |
| 163 | |
| 164 | /// The size of the array of which the most-derived object is an element, or |
| 165 | /// 0 if the most-derived object is not an array element. |
| 166 | uint64_t MostDerivedArraySize; |
| 167 | |
| 168 | /// The type of the most derived object referred to by this address. |
| 169 | QualType MostDerivedType; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 170 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 171 | typedef APValue::LValuePathEntry PathEntry; |
| 172 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 173 | /// The entries on the path from the glvalue to the designated subobject. |
| 174 | SmallVector<PathEntry, 8> Entries; |
| 175 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 176 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 177 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 178 | explicit SubobjectDesignator(QualType T) |
| 179 | : Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0), |
| 180 | MostDerivedArraySize(0), MostDerivedType(T) {} |
| 181 | |
| 182 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
| 183 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
| 184 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 185 | if (!Invalid) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 186 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 187 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 188 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| 189 | if (V.getLValueBase()) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 190 | MostDerivedPathLength = |
| 191 | findMostDerivedSubobject(Ctx, getType(V.getLValueBase()), |
| 192 | V.getLValuePath(), MostDerivedArraySize, |
| 193 | MostDerivedType); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 197 | void setInvalid() { |
| 198 | Invalid = true; |
| 199 | Entries.clear(); |
| 200 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 201 | |
| 202 | /// Determine whether this is a one-past-the-end pointer. |
| 203 | bool isOnePastTheEnd() const { |
Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 204 | assert(!Invalid); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 205 | if (IsOnePastTheEnd) |
| 206 | return true; |
| 207 | if (MostDerivedArraySize && |
| 208 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
| 209 | return true; |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | /// Check that this refers to a valid subobject. |
| 214 | bool isValidSubobject() const { |
| 215 | if (Invalid) |
| 216 | return false; |
| 217 | return !isOnePastTheEnd(); |
| 218 | } |
| 219 | /// Check that this refers to a valid subobject, and if not, produce a |
| 220 | /// relevant diagnostic and set the designator as invalid. |
| 221 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 222 | |
| 223 | /// Update this designator to refer to the first element within this array. |
| 224 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 225 | PathEntry Entry; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 226 | Entry.ArrayIndex = 0; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 227 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 228 | |
| 229 | // This is a most-derived object. |
| 230 | MostDerivedType = CAT->getElementType(); |
| 231 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 232 | MostDerivedPathLength = Entries.size(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 233 | } |
| 234 | /// Update this designator to refer to the given base or member of this |
| 235 | /// object. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 236 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 237 | PathEntry Entry; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 238 | APValue::BaseOrMemberType Value(D, Virtual); |
| 239 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 240 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 241 | |
| 242 | // If this isn't a base class, it's a new most-derived object. |
| 243 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 244 | MostDerivedType = FD->getType(); |
| 245 | MostDerivedArraySize = 0; |
| 246 | MostDerivedPathLength = Entries.size(); |
| 247 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 248 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 249 | /// Update this designator to refer to the given complex component. |
| 250 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
| 251 | PathEntry Entry; |
| 252 | Entry.ArrayIndex = Imag; |
| 253 | Entries.push_back(Entry); |
| 254 | |
| 255 | // This is technically a most-derived object, though in practice this |
| 256 | // is unlikely to matter. |
| 257 | MostDerivedType = EltTy; |
| 258 | MostDerivedArraySize = 2; |
| 259 | MostDerivedPathLength = Entries.size(); |
| 260 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 261 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 262 | /// Add N to the address of this subobject. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 263 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 264 | if (Invalid) return; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 265 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 266 | Entries.back().ArrayIndex += N; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 267 | if (Entries.back().ArrayIndex > MostDerivedArraySize) { |
| 268 | diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex); |
| 269 | setInvalid(); |
| 270 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 271 | return; |
| 272 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 273 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 274 | // nonarray object behaves the same as a pointer to the first element of |
| 275 | // an array of length one with the type of the object as its element type. |
| 276 | if (IsOnePastTheEnd && N == (uint64_t)-1) |
| 277 | IsOnePastTheEnd = false; |
| 278 | else if (!IsOnePastTheEnd && N == 1) |
| 279 | IsOnePastTheEnd = true; |
| 280 | else if (N != 0) { |
| 281 | diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 282 | setInvalid(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 283 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 284 | } |
| 285 | }; |
| 286 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 287 | /// A stack frame in the constexpr call stack. |
| 288 | struct CallStackFrame { |
| 289 | EvalInfo &Info; |
| 290 | |
| 291 | /// Parent - The caller of this stack frame. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 292 | CallStackFrame *Caller; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 293 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 294 | /// CallLoc - The location of the call expression for this call. |
| 295 | SourceLocation CallLoc; |
| 296 | |
| 297 | /// Callee - The function which was called. |
| 298 | const FunctionDecl *Callee; |
| 299 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 300 | /// Index - The call index of this call. |
| 301 | unsigned Index; |
| 302 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 303 | /// This - The binding for the this pointer in this call, if any. |
| 304 | const LValue *This; |
| 305 | |
Nick Lewycky | e2b2caa | 2013-09-22 10:07:22 +0000 | [diff] [blame] | 306 | /// Arguments - Parameter bindings for this function call, indexed by |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 307 | /// parameters' function scope indices. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 308 | APValue *Arguments; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 309 | |
Eli Friedman | 4830ec8 | 2012-06-25 21:21:08 +0000 | [diff] [blame] | 310 | // Note that we intentionally use std::map here so that references to |
| 311 | // values are stable. |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 312 | typedef std::map<const void*, APValue> MapTy; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 313 | typedef MapTy::const_iterator temp_iterator; |
| 314 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 315 | MapTy Temporaries; |
| 316 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 317 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 318 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 319 | APValue *Arguments); |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 320 | ~CallStackFrame(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 321 | |
| 322 | APValue *getTemporary(const void *Key) { |
| 323 | MapTy::iterator I = Temporaries.find(Key); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 324 | return I == Temporaries.end() ? nullptr : &I->second; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 325 | } |
| 326 | APValue &createTemporary(const void *Key, bool IsLifetimeExtended); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 329 | /// Temporarily override 'this'. |
| 330 | class ThisOverrideRAII { |
| 331 | public: |
| 332 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
| 333 | : Frame(Frame), OldThis(Frame.This) { |
| 334 | if (Enable) |
| 335 | Frame.This = NewThis; |
| 336 | } |
| 337 | ~ThisOverrideRAII() { |
| 338 | Frame.This = OldThis; |
| 339 | } |
| 340 | private: |
| 341 | CallStackFrame &Frame; |
| 342 | const LValue *OldThis; |
| 343 | }; |
| 344 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 345 | /// A partial diagnostic which we might know in advance that we are not going |
| 346 | /// to emit. |
| 347 | class OptionalDiagnostic { |
| 348 | PartialDiagnostic *Diag; |
| 349 | |
| 350 | public: |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 351 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = nullptr) |
| 352 | : Diag(Diag) {} |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 353 | |
| 354 | template<typename T> |
| 355 | OptionalDiagnostic &operator<<(const T &v) { |
| 356 | if (Diag) |
| 357 | *Diag << v; |
| 358 | return *this; |
| 359 | } |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 360 | |
| 361 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 362 | if (Diag) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 363 | SmallVector<char, 32> Buffer; |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 364 | I.toString(Buffer); |
| 365 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 366 | } |
| 367 | return *this; |
| 368 | } |
| 369 | |
| 370 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 371 | if (Diag) { |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 372 | // FIXME: Force the precision of the source value down so we don't |
| 373 | // print digits which are usually useless (we don't really care here if |
| 374 | // we truncate a digit by accident in edge cases). Ideally, |
| 375 | // APFloat::toString would automatically print the shortest |
| 376 | // representation which rounds to the correct value, but it's a bit |
| 377 | // tricky to implement. |
| 378 | unsigned precision = |
| 379 | llvm::APFloat::semanticsPrecision(F.getSemantics()); |
| 380 | precision = (precision * 59 + 195) / 196; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 381 | SmallVector<char, 32> Buffer; |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 382 | F.toString(Buffer, precision); |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 383 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 384 | } |
| 385 | return *this; |
| 386 | } |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 389 | /// A cleanup, and a flag indicating whether it is lifetime-extended. |
| 390 | class Cleanup { |
| 391 | llvm::PointerIntPair<APValue*, 1, bool> Value; |
| 392 | |
| 393 | public: |
| 394 | Cleanup(APValue *Val, bool IsLifetimeExtended) |
| 395 | : Value(Val, IsLifetimeExtended) {} |
| 396 | |
| 397 | bool isLifetimeExtended() const { return Value.getInt(); } |
| 398 | void endLifetime() { |
| 399 | *Value.getPointer() = APValue(); |
| 400 | } |
| 401 | }; |
| 402 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 403 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 404 | /// information about a subexpression as it is folded. It retains information |
| 405 | /// about the AST context, but also maintains information about the folded |
| 406 | /// expression. |
| 407 | /// |
| 408 | /// If an expression could be evaluated, it is still possible it is not a C |
| 409 | /// "integer constant expression" or constant expression. If not, this struct |
| 410 | /// captures information about how and why not. |
| 411 | /// |
| 412 | /// One bit of information passed *into* the request for constant folding |
| 413 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 414 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 415 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 416 | /// certain things in certain situations. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 417 | struct EvalInfo { |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 418 | ASTContext &Ctx; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 419 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 420 | /// EvalStatus - Contains information about the evaluation. |
| 421 | Expr::EvalStatus &EvalStatus; |
| 422 | |
| 423 | /// CurrentCall - The top of the constexpr call stack. |
| 424 | CallStackFrame *CurrentCall; |
| 425 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 426 | /// CallStackDepth - The number of calls in the call stack right now. |
| 427 | unsigned CallStackDepth; |
| 428 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 429 | /// NextCallIndex - The next call index to assign. |
| 430 | unsigned NextCallIndex; |
| 431 | |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 432 | /// StepsLeft - The remaining number of evaluation steps we're permitted |
| 433 | /// to perform. This is essentially a limit for the number of statements |
| 434 | /// we will evaluate. |
| 435 | unsigned StepsLeft; |
| 436 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 437 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 438 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 439 | CallStackFrame BottomFrame; |
| 440 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 441 | /// A stack of values whose lifetimes end at the end of some surrounding |
| 442 | /// evaluation frame. |
| 443 | llvm::SmallVector<Cleanup, 16> CleanupStack; |
| 444 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 445 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 446 | /// evaluated, if any. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 447 | APValue::LValueBase EvaluatingDecl; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 448 | |
| 449 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 450 | /// declaration whose initializer is being evaluated, if any. |
| 451 | APValue *EvaluatingDeclValue; |
| 452 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 453 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 454 | /// notes attached to it will also be stored, otherwise they will not be. |
| 455 | bool HasActiveDiagnostic; |
| 456 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 457 | enum EvaluationMode { |
| 458 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 459 | /// is not a constant expression. |
| 460 | EM_ConstantExpression, |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 461 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 462 | /// Evaluate as a potential constant expression. Keep going if we hit a |
| 463 | /// construct that we can't evaluate yet (because we don't yet know the |
| 464 | /// value of something) but stop if we hit something that could never be |
| 465 | /// a constant expression. |
| 466 | EM_PotentialConstantExpression, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 467 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 468 | /// Fold the expression to a constant. Stop if we hit a side-effect that |
| 469 | /// we can't model. |
| 470 | EM_ConstantFold, |
| 471 | |
| 472 | /// Evaluate the expression looking for integer overflow and similar |
| 473 | /// issues. Don't worry about side-effects, and try to visit all |
| 474 | /// subexpressions. |
| 475 | EM_EvaluateForOverflow, |
| 476 | |
| 477 | /// Evaluate in any way we know how. Don't worry about side-effects that |
| 478 | /// can't be modeled. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 479 | EM_IgnoreSideEffects, |
| 480 | |
| 481 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 482 | /// is not a constant expression. Some expressions can be retried in the |
| 483 | /// optimizer if we don't constant fold them here, but in an unevaluated |
| 484 | /// context we try to fold them immediately since the optimizer never |
| 485 | /// gets a chance to look at it. |
| 486 | EM_ConstantExpressionUnevaluated, |
| 487 | |
| 488 | /// Evaluate as a potential constant expression. Keep going if we hit a |
| 489 | /// construct that we can't evaluate yet (because we don't yet know the |
| 490 | /// value of something) but stop if we hit something that could never be |
| 491 | /// a constant expression. Some expressions can be retried in the |
| 492 | /// optimizer if we don't constant fold them here, but in an unevaluated |
| 493 | /// context we try to fold them immediately since the optimizer never |
| 494 | /// gets a chance to look at it. |
| 495 | EM_PotentialConstantExpressionUnevaluated |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 496 | } EvalMode; |
| 497 | |
| 498 | /// Are we checking whether the expression is a potential constant |
| 499 | /// expression? |
| 500 | bool checkingPotentialConstantExpression() const { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 501 | return EvalMode == EM_PotentialConstantExpression || |
| 502 | EvalMode == EM_PotentialConstantExpressionUnevaluated; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /// Are we checking an expression for overflow? |
| 506 | // FIXME: We should check for any kind of undefined or suspicious behavior |
| 507 | // in such constructs, not just overflow. |
| 508 | bool checkingForOverflow() { return EvalMode == EM_EvaluateForOverflow; } |
| 509 | |
| 510 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 511 | : Ctx(const_cast<ASTContext &>(C)), EvalStatus(S), CurrentCall(nullptr), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 512 | CallStackDepth(0), NextCallIndex(1), |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 513 | StepsLeft(getLangOpts().ConstexprStepLimit), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 514 | BottomFrame(*this, SourceLocation(), nullptr, nullptr, nullptr), |
| 515 | EvaluatingDecl((const ValueDecl *)nullptr), |
| 516 | EvaluatingDeclValue(nullptr), HasActiveDiagnostic(false), |
| 517 | EvalMode(Mode) {} |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 518 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 519 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value) { |
| 520 | EvaluatingDecl = Base; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 521 | EvaluatingDeclValue = &Value; |
| 522 | } |
| 523 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 524 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 525 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 526 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 527 | // Don't perform any constexpr calls (other than the call we're checking) |
| 528 | // when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 529 | if (checkingPotentialConstantExpression() && CallStackDepth > 1) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 530 | return false; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 531 | if (NextCallIndex == 0) { |
| 532 | // NextCallIndex has wrapped around. |
| 533 | Diag(Loc, diag::note_constexpr_call_limit_exceeded); |
| 534 | return false; |
| 535 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 536 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 537 | return true; |
| 538 | Diag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| 539 | << getLangOpts().ConstexprCallDepth; |
| 540 | return false; |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 541 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 542 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 543 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
| 544 | assert(CallIndex && "no call index in getCallFrame"); |
| 545 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 546 | // be null in this loop. |
| 547 | CallStackFrame *Frame = CurrentCall; |
| 548 | while (Frame->Index > CallIndex) |
| 549 | Frame = Frame->Caller; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 550 | return (Frame->Index == CallIndex) ? Frame : nullptr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 553 | bool nextStep(const Stmt *S) { |
| 554 | if (!StepsLeft) { |
| 555 | Diag(S->getLocStart(), diag::note_constexpr_step_limit_exceeded); |
| 556 | return false; |
| 557 | } |
| 558 | --StepsLeft; |
| 559 | return true; |
| 560 | } |
| 561 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 562 | private: |
| 563 | /// Add a diagnostic to the diagnostics list. |
| 564 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 565 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 566 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 567 | return EvalStatus.Diag->back().second; |
| 568 | } |
| 569 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 570 | /// Add notes containing a call stack to the current point of evaluation. |
| 571 | void addCallStack(unsigned Limit); |
| 572 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 573 | public: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 574 | /// Diagnose that the evaluation cannot be folded. |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 575 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId |
| 576 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 577 | unsigned ExtraNotes = 0) { |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 578 | if (EvalStatus.Diag) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 579 | // If we have a prior diagnostic, it will be noting that the expression |
| 580 | // isn't a constant expression. This diagnostic is more important, |
| 581 | // unless we require this evaluation to produce a constant expression. |
| 582 | // |
| 583 | // FIXME: We might want to show both diagnostics to the user in |
| 584 | // EM_ConstantFold mode. |
| 585 | if (!EvalStatus.Diag->empty()) { |
| 586 | switch (EvalMode) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 587 | case EM_ConstantFold: |
| 588 | case EM_IgnoreSideEffects: |
| 589 | case EM_EvaluateForOverflow: |
| 590 | if (!EvalStatus.HasSideEffects) |
| 591 | break; |
| 592 | // We've had side-effects; we want the diagnostic from them, not |
| 593 | // some later problem. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 594 | case EM_ConstantExpression: |
| 595 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 596 | case EM_ConstantExpressionUnevaluated: |
| 597 | case EM_PotentialConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 598 | HasActiveDiagnostic = false; |
| 599 | return OptionalDiagnostic(); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 603 | unsigned CallStackNotes = CallStackDepth - 1; |
| 604 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 605 | if (Limit) |
| 606 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 607 | if (checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 608 | CallStackNotes = 0; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 609 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 610 | HasActiveDiagnostic = true; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 611 | EvalStatus.Diag->clear(); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 612 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 613 | addDiag(Loc, DiagId); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 614 | if (!checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 615 | addCallStack(Limit); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 616 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 617 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 618 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 619 | return OptionalDiagnostic(); |
| 620 | } |
| 621 | |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 622 | OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId |
| 623 | = diag::note_invalid_subexpr_in_const_expr, |
| 624 | unsigned ExtraNotes = 0) { |
| 625 | if (EvalStatus.Diag) |
| 626 | return Diag(E->getExprLoc(), DiagId, ExtraNotes); |
| 627 | HasActiveDiagnostic = false; |
| 628 | return OptionalDiagnostic(); |
| 629 | } |
| 630 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 631 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 632 | /// expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 633 | /// |
| 634 | /// FIXME: Stop evaluating if we're in EM_ConstantExpression or |
| 635 | /// EM_PotentialConstantExpression mode and we produce one of these. |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 636 | template<typename LocArg> |
| 637 | OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 638 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 639 | unsigned ExtraNotes = 0) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 640 | // Don't override a previous diagnostic. Don't bother collecting |
| 641 | // diagnostics if we're evaluating for overflow. |
Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 642 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 643 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 644 | return OptionalDiagnostic(); |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 645 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 646 | return Diag(Loc, DiagId, ExtraNotes); |
| 647 | } |
| 648 | |
| 649 | /// Add a note to a prior diagnostic. |
| 650 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 651 | if (!HasActiveDiagnostic) |
| 652 | return OptionalDiagnostic(); |
| 653 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 654 | } |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 655 | |
| 656 | /// Add a stack of notes to a prior diagnostic. |
| 657 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 658 | if (HasActiveDiagnostic) { |
| 659 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 660 | Diags.begin(), Diags.end()); |
| 661 | } |
| 662 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 663 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 664 | /// Should we continue evaluation after encountering a side-effect that we |
| 665 | /// couldn't model? |
| 666 | bool keepEvaluatingAfterSideEffect() { |
| 667 | switch (EvalMode) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 668 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 669 | case EM_PotentialConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 670 | case EM_EvaluateForOverflow: |
| 671 | case EM_IgnoreSideEffects: |
| 672 | return true; |
| 673 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 674 | case EM_ConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 675 | case EM_ConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 676 | case EM_ConstantFold: |
| 677 | return false; |
| 678 | } |
Aaron Ballman | f682f53 | 2013-11-06 18:15:02 +0000 | [diff] [blame] | 679 | llvm_unreachable("Missed EvalMode case"); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /// Note that we have had a side-effect, and determine whether we should |
| 683 | /// keep evaluating. |
| 684 | bool noteSideEffect() { |
| 685 | EvalStatus.HasSideEffects = true; |
| 686 | return keepEvaluatingAfterSideEffect(); |
| 687 | } |
| 688 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 689 | /// Should we continue evaluation as much as possible after encountering a |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 690 | /// construct which can't be reduced to a value? |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 691 | bool keepEvaluatingAfterFailure() { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 692 | if (!StepsLeft) |
| 693 | return false; |
| 694 | |
| 695 | switch (EvalMode) { |
| 696 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 697 | case EM_PotentialConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 698 | case EM_EvaluateForOverflow: |
| 699 | return true; |
| 700 | |
| 701 | case EM_ConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 702 | case EM_ConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 703 | case EM_ConstantFold: |
| 704 | case EM_IgnoreSideEffects: |
| 705 | return false; |
| 706 | } |
Aaron Ballman | f682f53 | 2013-11-06 18:15:02 +0000 | [diff] [blame] | 707 | llvm_unreachable("Missed EvalMode case"); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 708 | } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 709 | }; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 710 | |
| 711 | /// Object used to treat all foldable expressions as constant expressions. |
| 712 | struct FoldConstant { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 713 | EvalInfo &Info; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 714 | bool Enabled; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 715 | bool HadNoPriorDiags; |
| 716 | EvalInfo::EvaluationMode OldMode; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 717 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 718 | explicit FoldConstant(EvalInfo &Info, bool Enabled) |
| 719 | : Info(Info), |
| 720 | Enabled(Enabled), |
| 721 | HadNoPriorDiags(Info.EvalStatus.Diag && |
| 722 | Info.EvalStatus.Diag->empty() && |
| 723 | !Info.EvalStatus.HasSideEffects), |
| 724 | OldMode(Info.EvalMode) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 725 | if (Enabled && |
| 726 | (Info.EvalMode == EvalInfo::EM_ConstantExpression || |
| 727 | Info.EvalMode == EvalInfo::EM_ConstantExpressionUnevaluated)) |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 728 | Info.EvalMode = EvalInfo::EM_ConstantFold; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 729 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 730 | void keepDiagnostics() { Enabled = false; } |
| 731 | ~FoldConstant() { |
| 732 | if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() && |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 733 | !Info.EvalStatus.HasSideEffects) |
| 734 | Info.EvalStatus.Diag->clear(); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 735 | Info.EvalMode = OldMode; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 736 | } |
| 737 | }; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 738 | |
| 739 | /// RAII object used to suppress diagnostics and side-effects from a |
| 740 | /// speculative evaluation. |
| 741 | class SpeculativeEvaluationRAII { |
| 742 | EvalInfo &Info; |
| 743 | Expr::EvalStatus Old; |
| 744 | |
| 745 | public: |
| 746 | SpeculativeEvaluationRAII(EvalInfo &Info, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 747 | SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr) |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 748 | : Info(Info), Old(Info.EvalStatus) { |
| 749 | Info.EvalStatus.Diag = NewDiag; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 750 | // If we're speculatively evaluating, we may have skipped over some |
| 751 | // evaluations and missed out a side effect. |
| 752 | Info.EvalStatus.HasSideEffects = true; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 753 | } |
| 754 | ~SpeculativeEvaluationRAII() { |
| 755 | Info.EvalStatus = Old; |
| 756 | } |
| 757 | }; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 758 | |
| 759 | /// RAII object wrapping a full-expression or block scope, and handling |
| 760 | /// the ending of the lifetime of temporaries created within it. |
| 761 | template<bool IsFullExpression> |
| 762 | class ScopeRAII { |
| 763 | EvalInfo &Info; |
| 764 | unsigned OldStackSize; |
| 765 | public: |
| 766 | ScopeRAII(EvalInfo &Info) |
| 767 | : Info(Info), OldStackSize(Info.CleanupStack.size()) {} |
| 768 | ~ScopeRAII() { |
| 769 | // Body moved to a static method to encourage the compiler to inline away |
| 770 | // instances of this class. |
| 771 | cleanup(Info, OldStackSize); |
| 772 | } |
| 773 | private: |
| 774 | static void cleanup(EvalInfo &Info, unsigned OldStackSize) { |
| 775 | unsigned NewEnd = OldStackSize; |
| 776 | for (unsigned I = OldStackSize, N = Info.CleanupStack.size(); |
| 777 | I != N; ++I) { |
| 778 | if (IsFullExpression && Info.CleanupStack[I].isLifetimeExtended()) { |
| 779 | // Full-expression cleanup of a lifetime-extended temporary: nothing |
| 780 | // to do, just move this cleanup to the right place in the stack. |
| 781 | std::swap(Info.CleanupStack[I], Info.CleanupStack[NewEnd]); |
| 782 | ++NewEnd; |
| 783 | } else { |
| 784 | // End the lifetime of the object. |
| 785 | Info.CleanupStack[I].endLifetime(); |
| 786 | } |
| 787 | } |
| 788 | Info.CleanupStack.erase(Info.CleanupStack.begin() + NewEnd, |
| 789 | Info.CleanupStack.end()); |
| 790 | } |
| 791 | }; |
| 792 | typedef ScopeRAII<false> BlockScopeRAII; |
| 793 | typedef ScopeRAII<true> FullExpressionRAII; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 794 | } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 795 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 796 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 797 | CheckSubobjectKind CSK) { |
| 798 | if (Invalid) |
| 799 | return false; |
| 800 | if (isOnePastTheEnd()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 801 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 802 | << CSK; |
| 803 | setInvalid(); |
| 804 | return false; |
| 805 | } |
| 806 | return true; |
| 807 | } |
| 808 | |
| 809 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| 810 | const Expr *E, uint64_t N) { |
| 811 | if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 812 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 813 | << static_cast<int>(N) << /*array*/ 0 |
| 814 | << static_cast<unsigned>(MostDerivedArraySize); |
| 815 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 816 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 817 | << static_cast<int>(N) << /*non-array*/ 1; |
| 818 | setInvalid(); |
| 819 | } |
| 820 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 821 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 822 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 823 | APValue *Arguments) |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 824 | : Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 825 | Index(Info.NextCallIndex++), This(This), Arguments(Arguments) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 826 | Info.CurrentCall = this; |
| 827 | ++Info.CallStackDepth; |
| 828 | } |
| 829 | |
| 830 | CallStackFrame::~CallStackFrame() { |
| 831 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 832 | --Info.CallStackDepth; |
| 833 | Info.CurrentCall = Caller; |
| 834 | } |
| 835 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 836 | APValue &CallStackFrame::createTemporary(const void *Key, |
| 837 | bool IsLifetimeExtended) { |
| 838 | APValue &Result = Temporaries[Key]; |
| 839 | assert(Result.isUninit() && "temporary created multiple times"); |
| 840 | Info.CleanupStack.push_back(Cleanup(&Result, IsLifetimeExtended)); |
| 841 | return Result; |
| 842 | } |
| 843 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 844 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 845 | |
| 846 | void EvalInfo::addCallStack(unsigned Limit) { |
| 847 | // Determine which calls to skip, if any. |
| 848 | unsigned ActiveCalls = CallStackDepth - 1; |
| 849 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 850 | if (Limit && Limit < ActiveCalls) { |
| 851 | SkipStart = Limit / 2 + Limit % 2; |
| 852 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 855 | // Walk the call stack and add the diagnostics. |
| 856 | unsigned CallIdx = 0; |
| 857 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 858 | Frame = Frame->Caller, ++CallIdx) { |
| 859 | // Skip this call? |
| 860 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 861 | if (CallIdx == SkipStart) { |
| 862 | // Note that we're skipping calls. |
| 863 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 864 | << unsigned(ActiveCalls - Limit); |
| 865 | } |
| 866 | continue; |
| 867 | } |
| 868 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 869 | SmallVector<char, 128> Buffer; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 870 | llvm::raw_svector_ostream Out(Buffer); |
| 871 | describeCall(Frame, Out); |
| 872 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | namespace { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 877 | struct ComplexValue { |
| 878 | private: |
| 879 | bool IsInt; |
| 880 | |
| 881 | public: |
| 882 | APSInt IntReal, IntImag; |
| 883 | APFloat FloatReal, FloatImag; |
| 884 | |
| 885 | ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {} |
| 886 | |
| 887 | void makeComplexFloat() { IsInt = false; } |
| 888 | bool isComplexFloat() const { return !IsInt; } |
| 889 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 890 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 891 | |
| 892 | void makeComplexInt() { IsInt = true; } |
| 893 | bool isComplexInt() const { return IsInt; } |
| 894 | APSInt &getComplexIntReal() { return IntReal; } |
| 895 | APSInt &getComplexIntImag() { return IntImag; } |
| 896 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 897 | void moveInto(APValue &v) const { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 898 | if (isComplexFloat()) |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 899 | v = APValue(FloatReal, FloatImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 900 | else |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 901 | v = APValue(IntReal, IntImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 902 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 903 | void setFrom(const APValue &v) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 904 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 905 | if (v.isComplexFloat()) { |
| 906 | makeComplexFloat(); |
| 907 | FloatReal = v.getComplexFloatReal(); |
| 908 | FloatImag = v.getComplexFloatImag(); |
| 909 | } else { |
| 910 | makeComplexInt(); |
| 911 | IntReal = v.getComplexIntReal(); |
| 912 | IntImag = v.getComplexIntImag(); |
| 913 | } |
| 914 | } |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 915 | }; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 916 | |
| 917 | struct LValue { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 918 | APValue::LValueBase Base; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 919 | CharUnits Offset; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 920 | unsigned CallIndex; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 921 | SubobjectDesignator Designator; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 922 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 923 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 924 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 925 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 926 | unsigned getLValueCallIndex() const { return CallIndex; } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 927 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 928 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 929 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 930 | void moveInto(APValue &V) const { |
| 931 | if (Designator.Invalid) |
| 932 | V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex); |
| 933 | else |
| 934 | V = APValue(Base, Offset, Designator.Entries, |
| 935 | Designator.IsOnePastTheEnd, CallIndex); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 936 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 937 | void setFrom(ASTContext &Ctx, const APValue &V) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 938 | assert(V.isLValue()); |
| 939 | Base = V.getLValueBase(); |
| 940 | Offset = V.getLValueOffset(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 941 | CallIndex = V.getLValueCallIndex(); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 942 | Designator = SubobjectDesignator(Ctx, V); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 945 | void set(APValue::LValueBase B, unsigned I = 0) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 946 | Base = B; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 947 | Offset = CharUnits::Zero(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 948 | CallIndex = I; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 949 | Designator = SubobjectDesignator(getType(B)); |
| 950 | } |
| 951 | |
| 952 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 953 | // a diagnostic and mark the designator as invalid. |
| 954 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 955 | CheckSubobjectKind CSK) { |
| 956 | if (Designator.Invalid) |
| 957 | return false; |
| 958 | if (!Base) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 959 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 960 | << CSK; |
| 961 | Designator.setInvalid(); |
| 962 | return false; |
| 963 | } |
| 964 | return true; |
| 965 | } |
| 966 | |
| 967 | // Check this LValue refers to an object. If not, set the designator to be |
| 968 | // invalid and emit a diagnostic. |
| 969 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
Richard Smith | 6c6bbfa | 2014-04-08 12:19:28 +0000 | [diff] [blame] | 970 | return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) && |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 971 | Designator.checkSubobject(Info, E, CSK); |
| 972 | } |
| 973 | |
| 974 | void addDecl(EvalInfo &Info, const Expr *E, |
| 975 | const Decl *D, bool Virtual = false) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 976 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
| 977 | Designator.addDeclUnchecked(D, Virtual); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 978 | } |
| 979 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 980 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
| 981 | Designator.addArrayUnchecked(CAT); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 982 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 983 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 984 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
| 985 | Designator.addComplexUnchecked(EltTy, Imag); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 986 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 987 | void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) { |
Richard Smith | 6c6bbfa | 2014-04-08 12:19:28 +0000 | [diff] [blame] | 988 | if (N && checkNullPointer(Info, E, CSK_ArrayIndex)) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 989 | Designator.adjustIndex(Info, E, N); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 990 | } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 991 | }; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 992 | |
| 993 | struct MemberPtr { |
| 994 | MemberPtr() {} |
| 995 | explicit MemberPtr(const ValueDecl *Decl) : |
| 996 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 997 | |
| 998 | /// The member or (direct or indirect) field referred to by this member |
| 999 | /// pointer, or 0 if this is a null member pointer. |
| 1000 | const ValueDecl *getDecl() const { |
| 1001 | return DeclAndIsDerivedMember.getPointer(); |
| 1002 | } |
| 1003 | /// Is this actually a member of some type derived from the relevant class? |
| 1004 | bool isDerivedMember() const { |
| 1005 | return DeclAndIsDerivedMember.getInt(); |
| 1006 | } |
| 1007 | /// Get the class which the declaration actually lives in. |
| 1008 | const CXXRecordDecl *getContainingRecord() const { |
| 1009 | return cast<CXXRecordDecl>( |
| 1010 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 1011 | } |
| 1012 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1013 | void moveInto(APValue &V) const { |
| 1014 | V = APValue(getDecl(), isDerivedMember(), Path); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1015 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1016 | void setFrom(const APValue &V) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1017 | assert(V.isMemberPointer()); |
| 1018 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 1019 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 1020 | Path.clear(); |
| 1021 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 1022 | Path.insert(Path.end(), P.begin(), P.end()); |
| 1023 | } |
| 1024 | |
| 1025 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 1026 | /// whether the member is a member of some class derived from the class type |
| 1027 | /// of the member pointer. |
| 1028 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 1029 | /// Path - The path of base/derived classes from the member declaration's |
| 1030 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 1031 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 1032 | |
| 1033 | /// Perform a cast towards the class of the Decl (either up or down the |
| 1034 | /// hierarchy). |
| 1035 | bool castBack(const CXXRecordDecl *Class) { |
| 1036 | assert(!Path.empty()); |
| 1037 | const CXXRecordDecl *Expected; |
| 1038 | if (Path.size() >= 2) |
| 1039 | Expected = Path[Path.size() - 2]; |
| 1040 | else |
| 1041 | Expected = getContainingRecord(); |
| 1042 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 1043 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 1044 | // if B does not contain the original member and is not a base or |
| 1045 | // derived class of the class containing the original member, the result |
| 1046 | // of the cast is undefined. |
| 1047 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 1048 | // (D::*). We consider that to be a language defect. |
| 1049 | return false; |
| 1050 | } |
| 1051 | Path.pop_back(); |
| 1052 | return true; |
| 1053 | } |
| 1054 | /// Perform a base-to-derived member pointer cast. |
| 1055 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 1056 | if (!getDecl()) |
| 1057 | return true; |
| 1058 | if (!isDerivedMember()) { |
| 1059 | Path.push_back(Derived); |
| 1060 | return true; |
| 1061 | } |
| 1062 | if (!castBack(Derived)) |
| 1063 | return false; |
| 1064 | if (Path.empty()) |
| 1065 | DeclAndIsDerivedMember.setInt(false); |
| 1066 | return true; |
| 1067 | } |
| 1068 | /// Perform a derived-to-base member pointer cast. |
| 1069 | bool castToBase(const CXXRecordDecl *Base) { |
| 1070 | if (!getDecl()) |
| 1071 | return true; |
| 1072 | if (Path.empty()) |
| 1073 | DeclAndIsDerivedMember.setInt(true); |
| 1074 | if (isDerivedMember()) { |
| 1075 | Path.push_back(Base); |
| 1076 | return true; |
| 1077 | } |
| 1078 | return castBack(Base); |
| 1079 | } |
| 1080 | }; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1081 | |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 1082 | /// Compare two member pointers, which are assumed to be of the same type. |
| 1083 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 1084 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 1085 | return !LHS.getDecl() && !RHS.getDecl(); |
| 1086 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 1087 | return false; |
| 1088 | return LHS.Path == RHS.Path; |
| 1089 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1090 | } |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1091 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1092 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1093 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 1094 | const LValue &This, const Expr *E, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1095 | bool AllowNonLiteralTypes = false); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1096 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info); |
| 1097 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1098 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 1099 | EvalInfo &Info); |
| 1100 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1101 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1102 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 1103 | EvalInfo &Info); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1104 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1105 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 1106 | static bool EvaluateAtomic(const Expr *E, APValue &Result, EvalInfo &Info); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1107 | |
| 1108 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1109 | // Misc utilities |
| 1110 | //===----------------------------------------------------------------------===// |
| 1111 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1112 | /// Produce a string describing the given constexpr call. |
| 1113 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out) { |
| 1114 | unsigned ArgIndex = 0; |
| 1115 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
| 1116 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 1117 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
| 1118 | |
| 1119 | if (!IsMemberCall) |
| 1120 | Out << *Frame->Callee << '('; |
| 1121 | |
| 1122 | if (Frame->This && IsMemberCall) { |
| 1123 | APValue Val; |
| 1124 | Frame->This->moveInto(Val); |
| 1125 | Val.printPretty(Out, Frame->Info.Ctx, |
| 1126 | Frame->This->Designator.MostDerivedType); |
| 1127 | // FIXME: Add parens around Val if needed. |
| 1128 | Out << "->" << *Frame->Callee << '('; |
| 1129 | IsMemberCall = false; |
| 1130 | } |
| 1131 | |
| 1132 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 1133 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
| 1134 | if (ArgIndex > (unsigned)IsMemberCall) |
| 1135 | Out << ", "; |
| 1136 | |
| 1137 | const ParmVarDecl *Param = *I; |
| 1138 | const APValue &Arg = Frame->Arguments[ArgIndex]; |
| 1139 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 1140 | |
| 1141 | if (ArgIndex == 0 && IsMemberCall) |
| 1142 | Out << "->" << *Frame->Callee << '('; |
| 1143 | } |
| 1144 | |
| 1145 | Out << ')'; |
| 1146 | } |
| 1147 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1148 | /// Evaluate an expression to see if it had side-effects, and discard its |
| 1149 | /// result. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1150 | /// \return \c true if the caller should keep evaluating. |
| 1151 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1152 | APValue Scratch; |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 1153 | if (!Evaluate(Scratch, Info, E)) |
| 1154 | // We don't need the value, but we might have skipped a side effect here. |
| 1155 | return Info.noteSideEffect(); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1156 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 1159 | /// Sign- or zero-extend a value to 64 bits. If it's already 64 bits, just |
| 1160 | /// return its existing value. |
| 1161 | static int64_t getExtValue(const APSInt &Value) { |
| 1162 | return Value.isSigned() ? Value.getSExtValue() |
| 1163 | : static_cast<int64_t>(Value.getZExtValue()); |
| 1164 | } |
| 1165 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1166 | /// Should this call expression be treated as a string literal? |
| 1167 | static bool IsStringLiteralCall(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 1168 | unsigned Builtin = E->getBuiltinCallee(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1169 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 1170 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 1171 | } |
| 1172 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1173 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1174 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 1175 | // constant expression of pointer type that evaluates to... |
| 1176 | |
| 1177 | // ... a null pointer value, or a prvalue core constant expression of type |
| 1178 | // std::nullptr_t. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1179 | if (!B) return true; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1180 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1181 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 1182 | // ... the address of an object with static storage duration, |
| 1183 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 1184 | return VD->hasGlobalStorage(); |
| 1185 | // ... the address of a function, |
| 1186 | return isa<FunctionDecl>(D); |
| 1187 | } |
| 1188 | |
| 1189 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1190 | switch (E->getStmtClass()) { |
| 1191 | default: |
| 1192 | return false; |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1193 | case Expr::CompoundLiteralExprClass: { |
| 1194 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 1195 | return CLE->isFileScope() && CLE->isLValue(); |
| 1196 | } |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1197 | case Expr::MaterializeTemporaryExprClass: |
| 1198 | // A materialized temporary might have been lifetime-extended to static |
| 1199 | // storage duration. |
| 1200 | return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1201 | // A string literal has static storage duration. |
| 1202 | case Expr::StringLiteralClass: |
| 1203 | case Expr::PredefinedExprClass: |
| 1204 | case Expr::ObjCStringLiteralClass: |
| 1205 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 1206 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 1207 | case Expr::CXXUuidofExprClass: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1208 | return true; |
| 1209 | case Expr::CallExprClass: |
| 1210 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 1211 | // For GCC compatibility, &&label has static storage duration. |
| 1212 | case Expr::AddrLabelExprClass: |
| 1213 | return true; |
| 1214 | // A Block literal expression may be used as the initialization value for |
| 1215 | // Block variables at global or local static scope. |
| 1216 | case Expr::BlockExprClass: |
| 1217 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1218 | case Expr::ImplicitValueInitExprClass: |
| 1219 | // FIXME: |
| 1220 | // We can never form an lvalue with an implicit value initialization as its |
| 1221 | // base through expression evaluation, so these only appear in one case: the |
| 1222 | // implicit variable declaration we invent when checking whether a constexpr |
| 1223 | // constructor can produce a constant expression. We must assume that such |
| 1224 | // an expression might be a global lvalue. |
| 1225 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1226 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1229 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 1230 | assert(Base && "no location for a null lvalue"); |
| 1231 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 1232 | if (VD) |
| 1233 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1234 | else |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 1235 | Info.Note(Base.get<const Expr*>()->getExprLoc(), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1236 | diag::note_constexpr_temporary_here); |
| 1237 | } |
| 1238 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1239 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1240 | /// value for an address or reference constant expression. Return true if we |
| 1241 | /// can fold this expression, whether or not it's a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1242 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
| 1243 | QualType Type, const LValue &LVal) { |
| 1244 | bool IsReferenceType = Type->isReferenceType(); |
| 1245 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1246 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 1247 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 1248 | |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1249 | // Check that the object is a global. Note that the fake 'this' object we |
| 1250 | // manufacture when checking potential constant expressions is conservatively |
| 1251 | // assumed to be global here. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1252 | if (!IsGlobalLValue(Base)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1253 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1254 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1255 | Info.Diag(Loc, diag::note_constexpr_non_global, 1) |
| 1256 | << IsReferenceType << !Designator.Entries.empty() |
| 1257 | << !!VD << VD; |
| 1258 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1259 | } else { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1260 | Info.Diag(Loc); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1261 | } |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1262 | // Don't allow references to temporaries to escape. |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1263 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1264 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1265 | assert((Info.checkingPotentialConstantExpression() || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1266 | LVal.getLValueCallIndex() == 0) && |
| 1267 | "have call index for global lvalue"); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1268 | |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1269 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 1270 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1271 | // Check if this is a thread-local variable. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1272 | if (Var->getTLSKind()) |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1273 | return false; |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1274 | |
Hans Wennborg | 82dd877 | 2014-06-25 22:19:48 +0000 | [diff] [blame] | 1275 | // A dllimport variable never acts like a constant. |
| 1276 | if (Var->hasAttr<DLLImportAttr>()) |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1277 | return false; |
| 1278 | } |
| 1279 | if (const auto *FD = dyn_cast<const FunctionDecl>(VD)) { |
| 1280 | // __declspec(dllimport) must be handled very carefully: |
| 1281 | // We must never initialize an expression with the thunk in C++. |
| 1282 | // Doing otherwise would allow the same id-expression to yield |
| 1283 | // different addresses for the same function in different translation |
| 1284 | // units. However, this means that we must dynamically initialize the |
| 1285 | // expression with the contents of the import address table at runtime. |
| 1286 | // |
| 1287 | // The C language has no notion of ODR; furthermore, it has no notion of |
| 1288 | // dynamic initialization. This means that we are permitted to |
| 1289 | // perform initialization with the address of the thunk. |
Hans Wennborg | 82dd877 | 2014-06-25 22:19:48 +0000 | [diff] [blame] | 1290 | if (Info.getLangOpts().CPlusPlus && FD->hasAttr<DLLImportAttr>()) |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1291 | return false; |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1295 | // Allow address constant expressions to be past-the-end pointers. This is |
| 1296 | // an extension: the standard requires them to point to an object. |
| 1297 | if (!IsReferenceType) |
| 1298 | return true; |
| 1299 | |
| 1300 | // A reference constant expression must refer to an object. |
| 1301 | if (!Base) { |
| 1302 | // FIXME: diagnostic |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1303 | Info.CCEDiag(Loc); |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1304 | return true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1307 | // Does this refer one past the end of some object? |
Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 1308 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1309 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1310 | Info.Diag(Loc, diag::note_constexpr_past_end, 1) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1311 | << !Designator.Entries.empty() << !!VD << VD; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1312 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1315 | return true; |
| 1316 | } |
| 1317 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1318 | /// Check that this core constant expression is of literal type, and if not, |
| 1319 | /// produce an appropriate diagnostic. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1320 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1321 | const LValue *This = nullptr) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1322 | if (!E->isRValue() || E->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1323 | return true; |
| 1324 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1325 | // C++1y: A constant initializer for an object o [...] may also invoke |
| 1326 | // constexpr constructors for o and its subobjects even if those objects |
| 1327 | // are of non-literal class types. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1328 | if (Info.getLangOpts().CPlusPlus14 && This && |
Richard Smith | 37dc92e | 2013-05-16 05:04:51 +0000 | [diff] [blame] | 1329 | Info.EvaluatingDecl == This->getLValueBase()) |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1330 | return true; |
| 1331 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1332 | // Prvalue constant expressions must be of literal types. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1333 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1334 | Info.Diag(E, diag::note_constexpr_nonliteral) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1335 | << E->getType(); |
| 1336 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1337 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1338 | return false; |
| 1339 | } |
| 1340 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1341 | /// Check that this core constant expression value is a valid value for a |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1342 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 1343 | /// check that the expression is of literal type. |
| 1344 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
| 1345 | QualType Type, const APValue &Value) { |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1346 | if (Value.isUninit()) { |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 1347 | Info.Diag(DiagLoc, diag::note_constexpr_uninitialized) |
| 1348 | << true << Type; |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1349 | return false; |
| 1350 | } |
| 1351 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 1352 | // We allow _Atomic(T) to be initialized from anything that T can be |
| 1353 | // initialized from. |
| 1354 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
| 1355 | Type = AT->getValueType(); |
| 1356 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1357 | // Core issue 1454: For a literal constant expression of array or class type, |
| 1358 | // each subobject of its value shall have been initialized by a constant |
| 1359 | // expression. |
| 1360 | if (Value.isArray()) { |
| 1361 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 1362 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| 1363 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1364 | Value.getArrayInitializedElt(I))) |
| 1365 | return false; |
| 1366 | } |
| 1367 | if (!Value.hasArrayFiller()) |
| 1368 | return true; |
| 1369 | return CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1370 | Value.getArrayFiller()); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1371 | } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1372 | if (Value.isUnion() && Value.getUnionField()) { |
| 1373 | return CheckConstantExpression(Info, DiagLoc, |
| 1374 | Value.getUnionField()->getType(), |
| 1375 | Value.getUnionValue()); |
| 1376 | } |
| 1377 | if (Value.isStruct()) { |
| 1378 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 1379 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1380 | unsigned BaseIndex = 0; |
| 1381 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
| 1382 | End = CD->bases_end(); I != End; ++I, ++BaseIndex) { |
| 1383 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1384 | Value.getStructBase(BaseIndex))) |
| 1385 | return false; |
| 1386 | } |
| 1387 | } |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1388 | for (const auto *I : RD->fields()) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1389 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1390 | Value.getStructField(I->getFieldIndex()))) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1391 | return false; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | if (Value.isLValue()) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1396 | LValue LVal; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1397 | LVal.setFrom(Info.Ctx, Value); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1398 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal); |
| 1399 | } |
| 1400 | |
| 1401 | // Everything else is fine. |
| 1402 | return true; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 1405 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1406 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | static bool IsLiteralLValue(const LValue &Value) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1410 | if (Value.CallIndex) |
| 1411 | return false; |
| 1412 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
| 1413 | return E && !isa<MaterializeTemporaryExpr>(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1416 | static bool IsWeakLValue(const LValue &Value) { |
| 1417 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1418 | return Decl && Decl->isWeak(); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 1421 | static bool isZeroSized(const LValue &Value) { |
| 1422 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 1423 | if (Decl && isa<VarDecl>(Decl)) { |
| 1424 | QualType Ty = Decl->getType(); |
David Majnemer | 8c92b87 | 2014-12-14 08:40:47 +0000 | [diff] [blame] | 1425 | if (Ty->isArrayType()) |
| 1426 | return Ty->isIncompleteType() || |
| 1427 | Decl->getASTContext().getTypeSize(Ty) == 0; |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 1428 | } |
| 1429 | return false; |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1432 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1433 | // A null base expression indicates a null pointer. These are always |
| 1434 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1435 | if (!Value.getLValueBase()) { |
| 1436 | Result = !Value.getLValueOffset().isZero(); |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1437 | return true; |
| 1438 | } |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 1439 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1440 | // We have a non-null base. These are generally known to be true, but if it's |
| 1441 | // a weak declaration it can be null at runtime. |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1442 | Result = true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1443 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1444 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1447 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1448 | switch (Val.getKind()) { |
| 1449 | case APValue::Uninitialized: |
| 1450 | return false; |
| 1451 | case APValue::Int: |
| 1452 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1453 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1454 | case APValue::Float: |
| 1455 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1456 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1457 | case APValue::ComplexInt: |
| 1458 | Result = Val.getComplexIntReal().getBoolValue() || |
| 1459 | Val.getComplexIntImag().getBoolValue(); |
| 1460 | return true; |
| 1461 | case APValue::ComplexFloat: |
| 1462 | Result = !Val.getComplexFloatReal().isZero() || |
| 1463 | !Val.getComplexFloatImag().isZero(); |
| 1464 | return true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1465 | case APValue::LValue: |
| 1466 | return EvalPointerValueAsBool(Val, Result); |
| 1467 | case APValue::MemberPointer: |
| 1468 | Result = Val.getMemberPointerDecl(); |
| 1469 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1470 | case APValue::Vector: |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1471 | case APValue::Array: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1472 | case APValue::Struct: |
| 1473 | case APValue::Union: |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 1474 | case APValue::AddrLabelDiff: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1475 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1478 | llvm_unreachable("unknown APValue kind"); |
| 1479 | } |
| 1480 | |
| 1481 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 1482 | EvalInfo &Info) { |
| 1483 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1484 | APValue Val; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1485 | if (!Evaluate(Val, Info, E)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1486 | return false; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1487 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1490 | template<typename T> |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1491 | static void HandleOverflow(EvalInfo &Info, const Expr *E, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1492 | const T &SrcValue, QualType DestType) { |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1493 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 1494 | << SrcValue << DestType; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 1498 | QualType SrcType, const APFloat &Value, |
| 1499 | QualType DestType, APSInt &Result) { |
| 1500 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1501 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1502 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1504 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1505 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1506 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 1507 | & APFloat::opInvalidOp) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1508 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1509 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1512 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 1513 | QualType SrcType, QualType DestType, |
| 1514 | APFloat &Result) { |
| 1515 | APFloat Value = Result; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1516 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1517 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 1518 | APFloat::rmNearestTiesToEven, &ignored) |
| 1519 | & APFloat::opOverflow) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1520 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1521 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 1524 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 1525 | QualType DestType, QualType SrcType, |
| 1526 | APSInt &Value) { |
| 1527 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1528 | APSInt Result = Value; |
| 1529 | // Figure out if this is a truncate, extend or noop cast. |
| 1530 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1531 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 1532 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1533 | return Result; |
| 1534 | } |
| 1535 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1536 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 1537 | QualType SrcType, const APSInt &Value, |
| 1538 | QualType DestType, APFloat &Result) { |
| 1539 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 1540 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 1541 | APFloat::rmNearestTiesToEven) |
| 1542 | & APFloat::opOverflow) |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 1543 | HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1544 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 1547 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
| 1548 | APValue &Value, const FieldDecl *FD) { |
| 1549 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
| 1550 | |
| 1551 | if (!Value.isInt()) { |
| 1552 | // Trying to store a pointer-cast-to-integer into a bitfield. |
| 1553 | // FIXME: In this case, we should provide the diagnostic for casting |
| 1554 | // a pointer to an integer. |
| 1555 | assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
| 1556 | Info.Diag(E); |
| 1557 | return false; |
| 1558 | } |
| 1559 | |
| 1560 | APSInt &Int = Value.getInt(); |
| 1561 | unsigned OldBitWidth = Int.getBitWidth(); |
| 1562 | unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx); |
| 1563 | if (NewBitWidth < OldBitWidth) |
| 1564 | Int = Int.trunc(NewBitWidth).extend(OldBitWidth); |
| 1565 | return true; |
| 1566 | } |
| 1567 | |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1568 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 1569 | llvm::APInt &Res) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1570 | APValue SVal; |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1571 | if (!Evaluate(SVal, Info, E)) |
| 1572 | return false; |
| 1573 | if (SVal.isInt()) { |
| 1574 | Res = SVal.getInt(); |
| 1575 | return true; |
| 1576 | } |
| 1577 | if (SVal.isFloat()) { |
| 1578 | Res = SVal.getFloat().bitcastToAPInt(); |
| 1579 | return true; |
| 1580 | } |
| 1581 | if (SVal.isVector()) { |
| 1582 | QualType VecTy = E->getType(); |
| 1583 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 1584 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 1585 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 1586 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 1587 | Res = llvm::APInt::getNullValue(VecSize); |
| 1588 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 1589 | APValue &Elt = SVal.getVectorElt(i); |
| 1590 | llvm::APInt EltAsInt; |
| 1591 | if (Elt.isInt()) { |
| 1592 | EltAsInt = Elt.getInt(); |
| 1593 | } else if (Elt.isFloat()) { |
| 1594 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 1595 | } else { |
| 1596 | // Don't try to handle vectors of anything other than int or float |
| 1597 | // (not sure if it's possible to hit this case). |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1598 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1599 | return false; |
| 1600 | } |
| 1601 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 1602 | if (BigEndian) |
| 1603 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 1604 | else |
| 1605 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 1606 | } |
| 1607 | return true; |
| 1608 | } |
| 1609 | // Give up if the input isn't an int, float, or vector. For example, we |
| 1610 | // reject "(v4i16)(intptr_t)&a". |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1611 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 1612 | return false; |
| 1613 | } |
| 1614 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 1615 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 1616 | /// bits, and check for overflow in the original type (if that type was not an |
| 1617 | /// unsigned type). |
| 1618 | template<typename Operation> |
| 1619 | static APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 1620 | const APSInt &LHS, const APSInt &RHS, |
| 1621 | unsigned BitWidth, Operation Op) { |
| 1622 | if (LHS.isUnsigned()) |
| 1623 | return Op(LHS, RHS); |
| 1624 | |
| 1625 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
| 1626 | APSInt Result = Value.trunc(LHS.getBitWidth()); |
| 1627 | if (Result.extend(BitWidth) != Value) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1628 | if (Info.checkingForOverflow()) |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 1629 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
| 1630 | diag::warn_integer_constant_overflow) |
| 1631 | << Result.toString(10) << E->getType(); |
| 1632 | else |
| 1633 | HandleOverflow(Info, E, Value, E->getType()); |
| 1634 | } |
| 1635 | return Result; |
| 1636 | } |
| 1637 | |
| 1638 | /// Perform the given binary integer operation. |
| 1639 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
| 1640 | BinaryOperatorKind Opcode, APSInt RHS, |
| 1641 | APSInt &Result) { |
| 1642 | switch (Opcode) { |
| 1643 | default: |
| 1644 | Info.Diag(E); |
| 1645 | return false; |
| 1646 | case BO_Mul: |
| 1647 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
| 1648 | std::multiplies<APSInt>()); |
| 1649 | return true; |
| 1650 | case BO_Add: |
| 1651 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 1652 | std::plus<APSInt>()); |
| 1653 | return true; |
| 1654 | case BO_Sub: |
| 1655 | Result = CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 1656 | std::minus<APSInt>()); |
| 1657 | return true; |
| 1658 | case BO_And: Result = LHS & RHS; return true; |
| 1659 | case BO_Xor: Result = LHS ^ RHS; return true; |
| 1660 | case BO_Or: Result = LHS | RHS; return true; |
| 1661 | case BO_Div: |
| 1662 | case BO_Rem: |
| 1663 | if (RHS == 0) { |
| 1664 | Info.Diag(E, diag::note_expr_divide_by_zero); |
| 1665 | return false; |
| 1666 | } |
| 1667 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. |
| 1668 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 1669 | LHS.isSigned() && LHS.isMinSignedValue()) |
| 1670 | HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType()); |
| 1671 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
| 1672 | return true; |
| 1673 | case BO_Shl: { |
| 1674 | if (Info.getLangOpts().OpenCL) |
| 1675 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 1676 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 1677 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 1678 | RHS.isUnsigned()); |
| 1679 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 1680 | // During constant-folding, a negative shift is an opposite shift. Such |
| 1681 | // a shift is not a constant expression. |
| 1682 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 1683 | RHS = -RHS; |
| 1684 | goto shift_right; |
| 1685 | } |
| 1686 | shift_left: |
| 1687 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 1688 | // the shifted type. |
| 1689 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 1690 | if (SA != RHS) { |
| 1691 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 1692 | << RHS << E->getType() << LHS.getBitWidth(); |
| 1693 | } else if (LHS.isSigned()) { |
| 1694 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 1695 | // operand, and must not overflow the corresponding unsigned type. |
| 1696 | if (LHS.isNegative()) |
| 1697 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 1698 | else if (LHS.countLeadingZeros() < SA) |
| 1699 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 1700 | } |
| 1701 | Result = LHS << SA; |
| 1702 | return true; |
| 1703 | } |
| 1704 | case BO_Shr: { |
| 1705 | if (Info.getLangOpts().OpenCL) |
| 1706 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 1707 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 1708 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 1709 | RHS.isUnsigned()); |
| 1710 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 1711 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 1712 | // shift is not a constant expression. |
| 1713 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 1714 | RHS = -RHS; |
| 1715 | goto shift_left; |
| 1716 | } |
| 1717 | shift_right: |
| 1718 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 1719 | // shifted type. |
| 1720 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 1721 | if (SA != RHS) |
| 1722 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 1723 | << RHS << E->getType() << LHS.getBitWidth(); |
| 1724 | Result = LHS >> SA; |
| 1725 | return true; |
| 1726 | } |
| 1727 | |
| 1728 | case BO_LT: Result = LHS < RHS; return true; |
| 1729 | case BO_GT: Result = LHS > RHS; return true; |
| 1730 | case BO_LE: Result = LHS <= RHS; return true; |
| 1731 | case BO_GE: Result = LHS >= RHS; return true; |
| 1732 | case BO_EQ: Result = LHS == RHS; return true; |
| 1733 | case BO_NE: Result = LHS != RHS; return true; |
| 1734 | } |
| 1735 | } |
| 1736 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 1737 | /// Perform the given binary floating-point operation, in-place, on LHS. |
| 1738 | static bool handleFloatFloatBinOp(EvalInfo &Info, const Expr *E, |
| 1739 | APFloat &LHS, BinaryOperatorKind Opcode, |
| 1740 | const APFloat &RHS) { |
| 1741 | switch (Opcode) { |
| 1742 | default: |
| 1743 | Info.Diag(E); |
| 1744 | return false; |
| 1745 | case BO_Mul: |
| 1746 | LHS.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 1747 | break; |
| 1748 | case BO_Add: |
| 1749 | LHS.add(RHS, APFloat::rmNearestTiesToEven); |
| 1750 | break; |
| 1751 | case BO_Sub: |
| 1752 | LHS.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 1753 | break; |
| 1754 | case BO_Div: |
| 1755 | LHS.divide(RHS, APFloat::rmNearestTiesToEven); |
| 1756 | break; |
| 1757 | } |
| 1758 | |
| 1759 | if (LHS.isInfinity() || LHS.isNaN()) |
| 1760 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
| 1761 | return true; |
| 1762 | } |
| 1763 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1764 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 1765 | /// truncating the lvalue's path to the given length. |
| 1766 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 1767 | const RecordDecl *TruncatedType, |
| 1768 | unsigned TruncatedElements) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1769 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1770 | |
| 1771 | // Check we actually point to a derived class object. |
| 1772 | if (TruncatedElements == D.Entries.size()) |
| 1773 | return true; |
| 1774 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 1775 | "not casting to a derived class"); |
| 1776 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 1777 | return false; |
| 1778 | |
| 1779 | // Truncate the path to the subobject, and remove any derived-to-base offsets. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1780 | const RecordDecl *RD = TruncatedType; |
| 1781 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1782 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1783 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 1784 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1785 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1786 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1787 | else |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1788 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 1789 | RD = Base; |
| 1790 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1791 | D.Entries.resize(TruncatedElements); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1792 | return true; |
| 1793 | } |
| 1794 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1795 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1796 | const CXXRecordDecl *Derived, |
| 1797 | const CXXRecordDecl *Base, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1798 | const ASTRecordLayout *RL = nullptr) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1799 | if (!RL) { |
| 1800 | if (Derived->isInvalidDecl()) return false; |
| 1801 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 1802 | } |
| 1803 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1804 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1805 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1806 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1809 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1810 | const CXXRecordDecl *DerivedDecl, |
| 1811 | const CXXBaseSpecifier *Base) { |
| 1812 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 1813 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1814 | if (!Base->isVirtual()) |
| 1815 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1816 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1817 | SubobjectDesignator &D = Obj.Designator; |
| 1818 | if (D.Invalid) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1819 | return false; |
| 1820 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1821 | // Extract most-derived object and corresponding type. |
| 1822 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 1823 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 1824 | return false; |
| 1825 | |
| 1826 | // Find the virtual base class. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1827 | if (DerivedDecl->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1828 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 1829 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1830 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1831 | return true; |
| 1832 | } |
| 1833 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1834 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
| 1835 | QualType Type, LValue &Result) { |
| 1836 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 1837 | PathE = E->path_end(); |
| 1838 | PathI != PathE; ++PathI) { |
| 1839 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 1840 | *PathI)) |
| 1841 | return false; |
| 1842 | Type = (*PathI)->getType(); |
| 1843 | } |
| 1844 | return true; |
| 1845 | } |
| 1846 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1847 | /// Update LVal to refer to the given field, which must be a member of the type |
| 1848 | /// currently described by LVal. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1849 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1850 | const FieldDecl *FD, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1851 | const ASTRecordLayout *RL = nullptr) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1852 | if (!RL) { |
| 1853 | if (FD->getParent()->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1854 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1855 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1856 | |
| 1857 | unsigned I = FD->getFieldIndex(); |
| 1858 | LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I)); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1859 | LVal.addDecl(Info, E, FD); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1860 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1863 | /// Update LVal to refer to the given indirect field. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1864 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1865 | LValue &LVal, |
| 1866 | const IndirectFieldDecl *IFD) { |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 1867 | for (const auto *C : IFD->chain()) |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 1868 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(C))) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 1869 | return false; |
| 1870 | return true; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 1871 | } |
| 1872 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1873 | /// Get the size of the given type in char units. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1874 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 1875 | QualType Type, CharUnits &Size) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1876 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 1877 | // extension. |
| 1878 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 1879 | Size = CharUnits::One(); |
| 1880 | return true; |
| 1881 | } |
| 1882 | |
| 1883 | if (!Type->isConstantSizeType()) { |
| 1884 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1885 | // FIXME: Better diagnostic. |
| 1886 | Info.Diag(Loc); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1887 | return false; |
| 1888 | } |
| 1889 | |
| 1890 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 1891 | return true; |
| 1892 | } |
| 1893 | |
| 1894 | /// Update a pointer value to model pointer arithmetic. |
| 1895 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1896 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1897 | /// \param LVal - The pointer value to be updated. |
| 1898 | /// \param EltTy - The pointee type represented by LVal. |
| 1899 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1900 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 1901 | LValue &LVal, QualType EltTy, |
| 1902 | int64_t Adjustment) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1903 | CharUnits SizeOfPointee; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1904 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1905 | return false; |
| 1906 | |
| 1907 | // Compute the new offset in the appropriate width. |
| 1908 | LVal.Offset += Adjustment * SizeOfPointee; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1909 | LVal.adjustIndex(Info, E, Adjustment); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1910 | return true; |
| 1911 | } |
| 1912 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1913 | /// Update an lvalue to refer to a component of a complex number. |
| 1914 | /// \param Info - Information about the ongoing evaluation. |
| 1915 | /// \param LVal - The lvalue to be updated. |
| 1916 | /// \param EltTy - The complex number's component type. |
| 1917 | /// \param Imag - False for the real component, true for the imaginary. |
| 1918 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 1919 | LValue &LVal, QualType EltTy, |
| 1920 | bool Imag) { |
| 1921 | if (Imag) { |
| 1922 | CharUnits SizeOfComponent; |
| 1923 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 1924 | return false; |
| 1925 | LVal.Offset += SizeOfComponent; |
| 1926 | } |
| 1927 | LVal.addComplex(Info, E, EltTy, Imag); |
| 1928 | return true; |
| 1929 | } |
| 1930 | |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1931 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1932 | /// |
| 1933 | /// \param Info Information about the ongoing evaluation. |
| 1934 | /// \param E An expression to be used when printing diagnostics. |
| 1935 | /// \param VD The variable whose initializer should be obtained. |
| 1936 | /// \param Frame The frame in which the variable was created. Must be null |
| 1937 | /// if this variable is not local to the evaluation. |
| 1938 | /// \param Result Filled in with a pointer to the value of the variable. |
| 1939 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 1940 | const VarDecl *VD, CallStackFrame *Frame, |
| 1941 | APValue *&Result) { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1942 | // If this is a parameter to an active constexpr function call, perform |
| 1943 | // argument substitution. |
| 1944 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1945 | // Assume arguments of a potential constant expression are unknown |
| 1946 | // constant expressions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1947 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1948 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1949 | if (!Frame || !Frame->Arguments) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1950 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1951 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1952 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1953 | Result = &Frame->Arguments[PVD->getFunctionScopeIndex()]; |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 1954 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 1955 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 1956 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1957 | // If this is a local variable, dig out its value. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1958 | if (Frame) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1959 | Result = Frame->getTemporary(VD); |
| 1960 | assert(Result && "missing value for local variable"); |
| 1961 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1964 | // Dig out the initializer, and use the declaration which it's attached to. |
| 1965 | const Expr *Init = VD->getAnyInitializer(VD); |
| 1966 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1967 | // If we're checking a potential constant expression, the variable could be |
| 1968 | // initialized later. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1969 | if (!Info.checkingPotentialConstantExpression()) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1970 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1971 | return false; |
| 1972 | } |
| 1973 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1974 | // If we're currently evaluating the initializer of this declaration, use that |
| 1975 | // in-flight value. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1976 | if (Info.EvaluatingDecl.dyn_cast<const ValueDecl*>() == VD) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 1977 | Result = Info.EvaluatingDeclValue; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1978 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1981 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 1982 | // this is the definition which will be used. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1983 | if (VD->isWeak()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1984 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1985 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1986 | } |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1987 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1988 | // Check that we can fold the initializer. In C++, we will have already done |
| 1989 | // this in the cases where it matters for conformance. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1990 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1991 | if (!VD->evaluateValue(Notes)) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1992 | Info.Diag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1993 | Notes.size() + 1) << VD; |
| 1994 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1995 | Info.addNotes(Notes); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1996 | return false; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1997 | } else if (!VD->checkInitIsICE()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1998 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1999 | Notes.size() + 1) << VD; |
| 2000 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2001 | Info.addNotes(Notes); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2002 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2003 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2004 | Result = VD->getEvaluatedValue(); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2005 | return true; |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2008 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2009 | Qualifiers Quals = T.getQualifiers(); |
| 2010 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 2011 | } |
| 2012 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2013 | /// Get the base index of the given base class within an APValue representing |
| 2014 | /// the given derived class. |
| 2015 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 2016 | const CXXRecordDecl *Base) { |
| 2017 | Base = Base->getCanonicalDecl(); |
| 2018 | unsigned Index = 0; |
| 2019 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 2020 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 2021 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 2022 | return Index; |
| 2023 | } |
| 2024 | |
| 2025 | llvm_unreachable("base class missing from derived class's bases list"); |
| 2026 | } |
| 2027 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2028 | /// Extract the value of a character from a string literal. |
| 2029 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
| 2030 | uint64_t Index) { |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2031 | // FIXME: Support ObjCEncodeExpr, MakeStringConstant |
| 2032 | if (auto PE = dyn_cast<PredefinedExpr>(Lit)) |
| 2033 | Lit = PE->getFunctionName(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2034 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 2035 | const ConstantArrayType *CAT = |
| 2036 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2037 | assert(CAT && "string literal isn't an array"); |
| 2038 | QualType CharType = CAT->getElementType(); |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2039 | assert(CharType->isIntegerType() && "unexpected character type"); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2040 | |
| 2041 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2042 | CharType->isUnsignedIntegerType()); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2043 | if (Index < S->getLength()) |
| 2044 | Value = S->getCodeUnit(Index); |
| 2045 | return Value; |
| 2046 | } |
| 2047 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2048 | // Expand a string literal into an array of characters. |
| 2049 | static void expandStringLiteral(EvalInfo &Info, const Expr *Lit, |
| 2050 | APValue &Result) { |
| 2051 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 2052 | const ConstantArrayType *CAT = |
| 2053 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2054 | assert(CAT && "string literal isn't an array"); |
| 2055 | QualType CharType = CAT->getElementType(); |
| 2056 | assert(CharType->isIntegerType() && "unexpected character type"); |
| 2057 | |
| 2058 | unsigned Elts = CAT->getSize().getZExtValue(); |
| 2059 | Result = APValue(APValue::UninitArray(), |
| 2060 | std::min(S->getLength(), Elts), Elts); |
| 2061 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 2062 | CharType->isUnsignedIntegerType()); |
| 2063 | if (Result.hasArrayFiller()) |
| 2064 | Result.getArrayFiller() = APValue(Value); |
| 2065 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
| 2066 | Value = S->getCodeUnit(I); |
| 2067 | Result.getArrayInitializedElt(I) = APValue(Value); |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | // Expand an array so that it has more than Index filled elements. |
| 2072 | static void expandArray(APValue &Array, unsigned Index) { |
| 2073 | unsigned Size = Array.getArraySize(); |
| 2074 | assert(Index < Size); |
| 2075 | |
| 2076 | // Always at least double the number of elements for which we store a value. |
| 2077 | unsigned OldElts = Array.getArrayInitializedElts(); |
| 2078 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
| 2079 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
| 2080 | |
| 2081 | // Copy the data across. |
| 2082 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
| 2083 | for (unsigned I = 0; I != OldElts; ++I) |
| 2084 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
| 2085 | for (unsigned I = OldElts; I != NewElts; ++I) |
| 2086 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
| 2087 | if (NewValue.hasArrayFiller()) |
| 2088 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
| 2089 | Array.swap(NewValue); |
| 2090 | } |
| 2091 | |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2092 | /// Determine whether a type would actually be read by an lvalue-to-rvalue |
| 2093 | /// conversion. If it's of class type, we may assume that the copy operation |
| 2094 | /// is trivial. Note that this is never true for a union type with fields |
| 2095 | /// (because the copy always "reads" the active member) and always true for |
| 2096 | /// a non-class type. |
| 2097 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
| 2098 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2099 | if (!RD || (RD->isUnion() && !RD->field_empty())) |
| 2100 | return true; |
| 2101 | if (RD->isEmpty()) |
| 2102 | return false; |
| 2103 | |
| 2104 | for (auto *Field : RD->fields()) |
| 2105 | if (isReadByLvalueToRvalueConversion(Field->getType())) |
| 2106 | return true; |
| 2107 | |
| 2108 | for (auto &BaseSpec : RD->bases()) |
| 2109 | if (isReadByLvalueToRvalueConversion(BaseSpec.getType())) |
| 2110 | return true; |
| 2111 | |
| 2112 | return false; |
| 2113 | } |
| 2114 | |
| 2115 | /// Diagnose an attempt to read from any unreadable field within the specified |
| 2116 | /// type, which might be a class type. |
| 2117 | static bool diagnoseUnreadableFields(EvalInfo &Info, const Expr *E, |
| 2118 | QualType T) { |
| 2119 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2120 | if (!RD) |
| 2121 | return false; |
| 2122 | |
| 2123 | if (!RD->hasMutableFields()) |
| 2124 | return false; |
| 2125 | |
| 2126 | for (auto *Field : RD->fields()) { |
| 2127 | // If we're actually going to read this field in some way, then it can't |
| 2128 | // be mutable. If we're in a union, then assigning to a mutable field |
| 2129 | // (even an empty one) can change the active member, so that's not OK. |
| 2130 | // FIXME: Add core issue number for the union case. |
| 2131 | if (Field->isMutable() && |
| 2132 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
| 2133 | Info.Diag(E, diag::note_constexpr_ltor_mutable, 1) << Field; |
| 2134 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2135 | return true; |
| 2136 | } |
| 2137 | |
| 2138 | if (diagnoseUnreadableFields(Info, E, Field->getType())) |
| 2139 | return true; |
| 2140 | } |
| 2141 | |
| 2142 | for (auto &BaseSpec : RD->bases()) |
| 2143 | if (diagnoseUnreadableFields(Info, E, BaseSpec.getType())) |
| 2144 | return true; |
| 2145 | |
| 2146 | // All mutable fields were empty, and thus not actually read. |
| 2147 | return false; |
| 2148 | } |
| 2149 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2150 | /// Kinds of access we can perform on an object, for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2151 | enum AccessKinds { |
| 2152 | AK_Read, |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2153 | AK_Assign, |
| 2154 | AK_Increment, |
| 2155 | AK_Decrement |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2156 | }; |
| 2157 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2158 | /// A handle to a complete object (an object that is not a subobject of |
| 2159 | /// another object). |
| 2160 | struct CompleteObject { |
| 2161 | /// The value of the complete object. |
| 2162 | APValue *Value; |
| 2163 | /// The type of the complete object. |
| 2164 | QualType Type; |
| 2165 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2166 | CompleteObject() : Value(nullptr) {} |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2167 | CompleteObject(APValue *Value, QualType Type) |
| 2168 | : Value(Value), Type(Type) { |
| 2169 | assert(Value && "missing value for complete object"); |
| 2170 | } |
| 2171 | |
Aaron Ballman | 6734766 | 2015-02-15 22:00:28 +0000 | [diff] [blame] | 2172 | explicit operator bool() const { return Value; } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2173 | }; |
| 2174 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2175 | /// Find the designated sub-object of an rvalue. |
| 2176 | template<typename SubobjectHandler> |
| 2177 | typename SubobjectHandler::result_type |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2178 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2179 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2180 | if (Sub.Invalid) |
| 2181 | // A diagnostic will have already been produced. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2182 | return handler.failed(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2183 | if (Sub.isOnePastTheEnd()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2184 | if (Info.getLangOpts().CPlusPlus11) |
| 2185 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 2186 | << handler.AccessKind; |
| 2187 | else |
| 2188 | Info.Diag(E); |
| 2189 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2190 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2191 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2192 | APValue *O = Obj.Value; |
| 2193 | QualType ObjType = Obj.Type; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2194 | const FieldDecl *LastField = nullptr; |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2195 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2196 | // Walk the designator's path to find the subobject. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2197 | for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) { |
| 2198 | if (O->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2199 | if (!Info.checkingPotentialConstantExpression()) |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2200 | Info.Diag(E, diag::note_constexpr_access_uninit) << handler.AccessKind; |
| 2201 | return handler.failed(); |
| 2202 | } |
| 2203 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2204 | if (I == N) { |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2205 | // If we are reading an object of class type, there may still be more |
| 2206 | // things we need to check: if there are any mutable subobjects, we |
| 2207 | // cannot perform this read. (This only happens when performing a trivial |
| 2208 | // copy or assignment.) |
| 2209 | if (ObjType->isRecordType() && handler.AccessKind == AK_Read && |
| 2210 | diagnoseUnreadableFields(Info, E, ObjType)) |
| 2211 | return handler.failed(); |
| 2212 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2213 | if (!handler.found(*O, ObjType)) |
| 2214 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2215 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2216 | // If we modified a bit-field, truncate it to the right width. |
| 2217 | if (handler.AccessKind != AK_Read && |
| 2218 | LastField && LastField->isBitField() && |
| 2219 | !truncateBitfieldValue(Info, E, *O, LastField)) |
| 2220 | return false; |
| 2221 | |
| 2222 | return true; |
| 2223 | } |
| 2224 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2225 | LastField = nullptr; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2226 | if (ObjType->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2227 | // Next subobject is an array element. |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2228 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2229 | assert(CAT && "vla in literal type?"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2230 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2231 | if (CAT->getSize().ule(Index)) { |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2232 | // Note, it should not be possible to form a pointer with a valid |
| 2233 | // designator which points more than one past the end of the array. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2234 | if (Info.getLangOpts().CPlusPlus11) |
| 2235 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 2236 | << handler.AccessKind; |
| 2237 | else |
| 2238 | Info.Diag(E); |
| 2239 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2240 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2241 | |
| 2242 | ObjType = CAT->getElementType(); |
| 2243 | |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2244 | // An array object is represented as either an Array APValue or as an |
| 2245 | // LValue which refers to a string literal. |
| 2246 | if (O->isLValue()) { |
| 2247 | assert(I == N - 1 && "extracting subobject of character?"); |
| 2248 | assert(!O->hasLValuePath() || O->getLValuePath().empty()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2249 | if (handler.AccessKind != AK_Read) |
| 2250 | expandStringLiteral(Info, O->getLValueBase().get<const Expr *>(), |
| 2251 | *O); |
| 2252 | else |
| 2253 | return handler.foundString(*O, ObjType, Index); |
| 2254 | } |
| 2255 | |
| 2256 | if (O->getArrayInitializedElts() > Index) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2257 | O = &O->getArrayInitializedElt(Index); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2258 | else if (handler.AccessKind != AK_Read) { |
| 2259 | expandArray(*O, Index); |
| 2260 | O = &O->getArrayInitializedElt(Index); |
| 2261 | } else |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2262 | O = &O->getArrayFiller(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2263 | } else if (ObjType->isAnyComplexType()) { |
| 2264 | // Next subobject is a complex number. |
| 2265 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 2266 | if (Index > 1) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2267 | if (Info.getLangOpts().CPlusPlus11) |
| 2268 | Info.Diag(E, diag::note_constexpr_access_past_end) |
| 2269 | << handler.AccessKind; |
| 2270 | else |
| 2271 | Info.Diag(E); |
| 2272 | return handler.failed(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2273 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2274 | |
| 2275 | bool WasConstQualified = ObjType.isConstQualified(); |
| 2276 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 2277 | if (WasConstQualified) |
| 2278 | ObjType.addConst(); |
| 2279 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2280 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 2281 | if (O->isComplexInt()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2282 | return handler.found(Index ? O->getComplexIntImag() |
| 2283 | : O->getComplexIntReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2284 | } else { |
| 2285 | assert(O->isComplexFloat()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2286 | return handler.found(Index ? O->getComplexFloatImag() |
| 2287 | : O->getComplexFloatReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2288 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2289 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2290 | if (Field->isMutable() && handler.AccessKind == AK_Read) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2291 | Info.Diag(E, diag::note_constexpr_ltor_mutable, 1) |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2292 | << Field; |
| 2293 | Info.Note(Field->getLocation(), diag::note_declared_at); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2294 | return handler.failed(); |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2297 | // Next subobject is a class, struct or union field. |
| 2298 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 2299 | if (RD->isUnion()) { |
| 2300 | const FieldDecl *UnionField = O->getUnionField(); |
| 2301 | if (!UnionField || |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2302 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2303 | Info.Diag(E, diag::note_constexpr_access_inactive_union_member) |
| 2304 | << handler.AccessKind << Field << !UnionField << UnionField; |
| 2305 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2306 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2307 | O = &O->getUnionValue(); |
| 2308 | } else |
| 2309 | O = &O->getStructField(Field->getFieldIndex()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2310 | |
| 2311 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2312 | ObjType = Field->getType(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2313 | if (WasConstQualified && !Field->isMutable()) |
| 2314 | ObjType.addConst(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2315 | |
| 2316 | if (ObjType.isVolatileQualified()) { |
| 2317 | if (Info.getLangOpts().CPlusPlus) { |
| 2318 | // FIXME: Include a description of the path to the volatile subobject. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2319 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2320 | << handler.AccessKind << 2 << Field; |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2321 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2322 | } else { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2323 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2324 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2325 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2326 | } |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2327 | |
| 2328 | LastField = Field; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2329 | } else { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2330 | // Next subobject is a base class. |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2331 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 2332 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 2333 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2334 | |
| 2335 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2336 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2337 | if (WasConstQualified) |
| 2338 | ObjType.addConst(); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2339 | } |
| 2340 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2343 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2344 | struct ExtractSubobjectHandler { |
| 2345 | EvalInfo &Info; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2346 | APValue &Result; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2347 | |
| 2348 | static const AccessKinds AccessKind = AK_Read; |
| 2349 | |
| 2350 | typedef bool result_type; |
| 2351 | bool failed() { return false; } |
| 2352 | bool found(APValue &Subobj, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2353 | Result = Subobj; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2354 | return true; |
| 2355 | } |
| 2356 | bool found(APSInt &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2357 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2358 | return true; |
| 2359 | } |
| 2360 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2361 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2362 | return true; |
| 2363 | } |
| 2364 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2365 | Result = APValue(extractStringLiteralCharacter( |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2366 | Info, Subobj.getLValueBase().get<const Expr *>(), Character)); |
| 2367 | return true; |
| 2368 | } |
| 2369 | }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2370 | } // end anonymous namespace |
| 2371 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2372 | const AccessKinds ExtractSubobjectHandler::AccessKind; |
| 2373 | |
| 2374 | /// Extract the designated sub-object of an rvalue. |
| 2375 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2376 | const CompleteObject &Obj, |
| 2377 | const SubobjectDesignator &Sub, |
| 2378 | APValue &Result) { |
| 2379 | ExtractSubobjectHandler Handler = { Info, Result }; |
| 2380 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2383 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2384 | struct ModifySubobjectHandler { |
| 2385 | EvalInfo &Info; |
| 2386 | APValue &NewVal; |
| 2387 | const Expr *E; |
| 2388 | |
| 2389 | typedef bool result_type; |
| 2390 | static const AccessKinds AccessKind = AK_Assign; |
| 2391 | |
| 2392 | bool checkConst(QualType QT) { |
| 2393 | // Assigning to a const object has undefined behavior. |
| 2394 | if (QT.isConstQualified()) { |
| 2395 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2396 | return false; |
| 2397 | } |
| 2398 | return true; |
| 2399 | } |
| 2400 | |
| 2401 | bool failed() { return false; } |
| 2402 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2403 | if (!checkConst(SubobjType)) |
| 2404 | return false; |
| 2405 | // We've been given ownership of NewVal, so just swap it in. |
| 2406 | Subobj.swap(NewVal); |
| 2407 | return true; |
| 2408 | } |
| 2409 | bool found(APSInt &Value, QualType SubobjType) { |
| 2410 | if (!checkConst(SubobjType)) |
| 2411 | return false; |
| 2412 | if (!NewVal.isInt()) { |
| 2413 | // Maybe trying to write a cast pointer value into a complex? |
| 2414 | Info.Diag(E); |
| 2415 | return false; |
| 2416 | } |
| 2417 | Value = NewVal.getInt(); |
| 2418 | return true; |
| 2419 | } |
| 2420 | bool found(APFloat &Value, QualType SubobjType) { |
| 2421 | if (!checkConst(SubobjType)) |
| 2422 | return false; |
| 2423 | Value = NewVal.getFloat(); |
| 2424 | return true; |
| 2425 | } |
| 2426 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2427 | llvm_unreachable("shouldn't encounter string elements with ExpandArrays"); |
| 2428 | } |
| 2429 | }; |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2430 | } // end anonymous namespace |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2431 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2432 | const AccessKinds ModifySubobjectHandler::AccessKind; |
| 2433 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2434 | /// Update the designated sub-object of an rvalue to the given value. |
| 2435 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2436 | const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2437 | const SubobjectDesignator &Sub, |
| 2438 | APValue &NewVal) { |
| 2439 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2440 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2443 | /// Find the position where two subobject designators diverge, or equivalently |
| 2444 | /// the length of the common initial subsequence. |
| 2445 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 2446 | const SubobjectDesignator &A, |
| 2447 | const SubobjectDesignator &B, |
| 2448 | bool &WasArrayIndex) { |
| 2449 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 2450 | for (/**/; I != N; ++I) { |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2451 | if (!ObjType.isNull() && |
| 2452 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2453 | // Next subobject is an array element. |
| 2454 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 2455 | WasArrayIndex = true; |
| 2456 | return I; |
| 2457 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2458 | if (ObjType->isAnyComplexType()) |
| 2459 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 2460 | else |
| 2461 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2462 | } else { |
| 2463 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 2464 | WasArrayIndex = false; |
| 2465 | return I; |
| 2466 | } |
| 2467 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 2468 | // Next subobject is a field. |
| 2469 | ObjType = FD->getType(); |
| 2470 | else |
| 2471 | // Next subobject is a base class. |
| 2472 | ObjType = QualType(); |
| 2473 | } |
| 2474 | } |
| 2475 | WasArrayIndex = false; |
| 2476 | return I; |
| 2477 | } |
| 2478 | |
| 2479 | /// Determine whether the given subobject designators refer to elements of the |
| 2480 | /// same array object. |
| 2481 | static bool AreElementsOfSameArray(QualType ObjType, |
| 2482 | const SubobjectDesignator &A, |
| 2483 | const SubobjectDesignator &B) { |
| 2484 | if (A.Entries.size() != B.Entries.size()) |
| 2485 | return false; |
| 2486 | |
| 2487 | bool IsArray = A.MostDerivedArraySize != 0; |
| 2488 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 2489 | // A is a subobject of the array element. |
| 2490 | return false; |
| 2491 | |
| 2492 | // If A (and B) designates an array element, the last entry will be the array |
| 2493 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 2494 | // of length 1' case, and the entire path must match. |
| 2495 | bool WasArrayIndex; |
| 2496 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 2497 | return CommonLength >= A.Entries.size() - IsArray; |
| 2498 | } |
| 2499 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2500 | /// Find the complete object to which an LValue refers. |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 2501 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
| 2502 | AccessKinds AK, const LValue &LVal, |
| 2503 | QualType LValType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2504 | if (!LVal.Base) { |
| 2505 | Info.Diag(E, diag::note_constexpr_access_null) << AK; |
| 2506 | return CompleteObject(); |
| 2507 | } |
| 2508 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2509 | CallStackFrame *Frame = nullptr; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2510 | if (LVal.CallIndex) { |
| 2511 | Frame = Info.getCallFrame(LVal.CallIndex); |
| 2512 | if (!Frame) { |
| 2513 | Info.Diag(E, diag::note_constexpr_lifetime_ended, 1) |
| 2514 | << AK << LVal.Base.is<const ValueDecl*>(); |
| 2515 | NoteLValueLocation(Info, LVal.Base); |
| 2516 | return CompleteObject(); |
| 2517 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 2521 | // is not a constant expression (even if the object is non-volatile). We also |
| 2522 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 2523 | // semantics. |
| 2524 | if (LValType.isVolatileQualified()) { |
| 2525 | if (Info.getLangOpts().CPlusPlus) |
| 2526 | Info.Diag(E, diag::note_constexpr_access_volatile_type) |
| 2527 | << AK << LValType; |
| 2528 | else |
| 2529 | Info.Diag(E); |
| 2530 | return CompleteObject(); |
| 2531 | } |
| 2532 | |
| 2533 | // Compute value storage location and type of base object. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2534 | APValue *BaseVal = nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2535 | QualType BaseType = getType(LVal.Base); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2536 | |
| 2537 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
| 2538 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 2539 | // In C++11, constexpr, non-volatile variables initialized with constant |
| 2540 | // expressions are constant expressions too. Inside constexpr functions, |
| 2541 | // parameters are constant expressions even if they're non-const. |
| 2542 | // In C++1y, objects local to a constant expression (those with a Frame) are |
| 2543 | // both readable and writable inside constant expressions. |
| 2544 | // In C, such things can also be folded, although they are not ICEs. |
| 2545 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
| 2546 | if (VD) { |
| 2547 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 2548 | VD = VDef; |
| 2549 | } |
| 2550 | if (!VD || VD->isInvalidDecl()) { |
| 2551 | Info.Diag(E); |
| 2552 | return CompleteObject(); |
| 2553 | } |
| 2554 | |
| 2555 | // Accesses of volatile-qualified objects are not allowed. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2556 | if (BaseType.isVolatileQualified()) { |
| 2557 | if (Info.getLangOpts().CPlusPlus) { |
| 2558 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2559 | << AK << 1 << VD; |
| 2560 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2561 | } else { |
| 2562 | Info.Diag(E); |
| 2563 | } |
| 2564 | return CompleteObject(); |
| 2565 | } |
| 2566 | |
| 2567 | // Unless we're looking at a local variable or argument in a constexpr call, |
| 2568 | // the variable we're reading must be const. |
| 2569 | if (!Frame) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2570 | if (Info.getLangOpts().CPlusPlus14 && |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2571 | VD == Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()) { |
| 2572 | // OK, we can read and modify an object if we're in the process of |
| 2573 | // evaluating its initializer, because its lifetime began in this |
| 2574 | // evaluation. |
| 2575 | } else if (AK != AK_Read) { |
| 2576 | // All the remaining cases only permit reading. |
| 2577 | Info.Diag(E, diag::note_constexpr_modify_global); |
| 2578 | return CompleteObject(); |
| 2579 | } else if (VD->isConstexpr()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2580 | // OK, we can read this variable. |
| 2581 | } else if (BaseType->isIntegralOrEnumerationType()) { |
| 2582 | if (!BaseType.isConstQualified()) { |
| 2583 | if (Info.getLangOpts().CPlusPlus) { |
| 2584 | Info.Diag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
| 2585 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2586 | } else { |
| 2587 | Info.Diag(E); |
| 2588 | } |
| 2589 | return CompleteObject(); |
| 2590 | } |
| 2591 | } else if (BaseType->isFloatingType() && BaseType.isConstQualified()) { |
| 2592 | // We support folding of const floating-point types, in order to make |
| 2593 | // static const data members of such types (supported as an extension) |
| 2594 | // more useful. |
| 2595 | if (Info.getLangOpts().CPlusPlus11) { |
| 2596 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 2597 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2598 | } else { |
| 2599 | Info.CCEDiag(E); |
| 2600 | } |
| 2601 | } else { |
| 2602 | // FIXME: Allow folding of values of any literal type in all languages. |
| 2603 | if (Info.getLangOpts().CPlusPlus11) { |
| 2604 | Info.Diag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 2605 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2606 | } else { |
| 2607 | Info.Diag(E); |
| 2608 | } |
| 2609 | return CompleteObject(); |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | if (!evaluateVarDeclInit(Info, E, VD, Frame, BaseVal)) |
| 2614 | return CompleteObject(); |
| 2615 | } else { |
| 2616 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| 2617 | |
| 2618 | if (!Frame) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2619 | if (const MaterializeTemporaryExpr *MTE = |
| 2620 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 2621 | assert(MTE->getStorageDuration() == SD_Static && |
| 2622 | "should have a frame for a non-global materialized temporary"); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2623 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2624 | // Per C++1y [expr.const]p2: |
| 2625 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
| 2626 | // - a [...] glvalue of integral or enumeration type that refers to |
| 2627 | // a non-volatile const object [...] |
| 2628 | // [...] |
| 2629 | // - a [...] glvalue of literal type that refers to a non-volatile |
| 2630 | // object whose lifetime began within the evaluation of e. |
| 2631 | // |
| 2632 | // C++11 misses the 'began within the evaluation of e' check and |
| 2633 | // instead allows all temporaries, including things like: |
| 2634 | // int &&r = 1; |
| 2635 | // int x = ++r; |
| 2636 | // constexpr int k = r; |
| 2637 | // Therefore we use the C++1y rules in C++11 too. |
| 2638 | const ValueDecl *VD = Info.EvaluatingDecl.dyn_cast<const ValueDecl*>(); |
| 2639 | const ValueDecl *ED = MTE->getExtendingDecl(); |
| 2640 | if (!(BaseType.isConstQualified() && |
| 2641 | BaseType->isIntegralOrEnumerationType()) && |
| 2642 | !(VD && VD->getCanonicalDecl() == ED->getCanonicalDecl())) { |
| 2643 | Info.Diag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
| 2644 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
| 2645 | return CompleteObject(); |
| 2646 | } |
| 2647 | |
| 2648 | BaseVal = Info.Ctx.getMaterializedTemporaryValue(MTE, false); |
| 2649 | assert(BaseVal && "got reference to unevaluated temporary"); |
| 2650 | } else { |
| 2651 | Info.Diag(E); |
| 2652 | return CompleteObject(); |
| 2653 | } |
| 2654 | } else { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2655 | BaseVal = Frame->getTemporary(Base); |
| 2656 | assert(BaseVal && "missing value for temporary"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2657 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2658 | |
| 2659 | // Volatile temporary objects cannot be accessed in constant expressions. |
| 2660 | if (BaseType.isVolatileQualified()) { |
| 2661 | if (Info.getLangOpts().CPlusPlus) { |
| 2662 | Info.Diag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 2663 | << AK << 0; |
| 2664 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 2665 | } else { |
| 2666 | Info.Diag(E); |
| 2667 | } |
| 2668 | return CompleteObject(); |
| 2669 | } |
| 2670 | } |
| 2671 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2672 | // During the construction of an object, it is not yet 'const'. |
| 2673 | // FIXME: We don't set up EvaluatingDecl for local variables or temporaries, |
| 2674 | // and this doesn't do quite the right thing for const subobjects of the |
| 2675 | // object under construction. |
| 2676 | if (LVal.getLValueBase() == Info.EvaluatingDecl) { |
| 2677 | BaseType = Info.Ctx.getCanonicalType(BaseType); |
| 2678 | BaseType.removeLocalConst(); |
| 2679 | } |
| 2680 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2681 | // In C++1y, we can't safely access any mutable state when we might be |
| 2682 | // evaluating after an unmodeled side effect or an evaluation failure. |
| 2683 | // |
| 2684 | // FIXME: Not all local state is mutable. Allow local constant subobjects |
| 2685 | // to be read here (but take care with 'mutable' fields). |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2686 | if (Frame && Info.getLangOpts().CPlusPlus14 && |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2687 | (Info.EvalStatus.HasSideEffects || Info.keepEvaluatingAfterFailure())) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2688 | return CompleteObject(); |
| 2689 | |
| 2690 | return CompleteObject(BaseVal, BaseType); |
| 2691 | } |
| 2692 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2693 | /// \brief Perform an lvalue-to-rvalue conversion on the given glvalue. This |
| 2694 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
| 2695 | /// glvalue referred to by an entity of reference type. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2696 | /// |
| 2697 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2698 | /// \param Conv - The expression for which we are performing the conversion. |
| 2699 | /// Used for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2700 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
| 2701 | /// case of a non-class type). |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2702 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 2703 | /// \param RVal - The produced value will be placed here. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2704 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2705 | QualType Type, |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2706 | const LValue &LVal, APValue &RVal) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2707 | if (LVal.Designator.Invalid) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2708 | return false; |
| 2709 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2710 | // Check for special cases where there is no existing APValue to look at. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2711 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 2712 | if (Base && !LVal.CallIndex && !Type.isVolatileQualified()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2713 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 2714 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 2715 | // initializer until now for such expressions. Such an expression can't be |
| 2716 | // an ICE in C, so this only matters for fold. |
| 2717 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 2718 | if (Type.isVolatileQualified()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2719 | Info.Diag(Conv); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2720 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2721 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2722 | APValue Lit; |
| 2723 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
| 2724 | return false; |
| 2725 | CompleteObject LitObj(&Lit, Base->getType()); |
| 2726 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal); |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2727 | } else if (isa<StringLiteral>(Base) || isa<PredefinedExpr>(Base)) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2728 | // We represent a string literal array as an lvalue pointing at the |
| 2729 | // corresponding expression, rather than building an array of chars. |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2730 | // FIXME: Support ObjCEncodeExpr, MakeStringConstant |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2731 | APValue Str(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0); |
| 2732 | CompleteObject StrObj(&Str, Base->getType()); |
| 2733 | return extractSubobject(Info, Conv, StrObj, LVal.Designator, RVal); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 2734 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2737 | CompleteObject Obj = findCompleteObject(Info, Conv, AK_Read, LVal, Type); |
| 2738 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2742 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2743 | QualType LValType, APValue &Val) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2744 | if (LVal.Designator.Invalid) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2745 | return false; |
| 2746 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2747 | if (!Info.getLangOpts().CPlusPlus14) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2748 | Info.Diag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2749 | return false; |
| 2750 | } |
| 2751 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2752 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 2753 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2756 | static bool isOverflowingIntegerType(ASTContext &Ctx, QualType T) { |
| 2757 | return T->isSignedIntegerType() && |
| 2758 | Ctx.getIntWidth(T) >= Ctx.getIntWidth(Ctx.IntTy); |
| 2759 | } |
| 2760 | |
| 2761 | namespace { |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2762 | struct CompoundAssignSubobjectHandler { |
| 2763 | EvalInfo &Info; |
| 2764 | const Expr *E; |
| 2765 | QualType PromotedLHSType; |
| 2766 | BinaryOperatorKind Opcode; |
| 2767 | const APValue &RHS; |
| 2768 | |
| 2769 | static const AccessKinds AccessKind = AK_Assign; |
| 2770 | |
| 2771 | typedef bool result_type; |
| 2772 | |
| 2773 | bool checkConst(QualType QT) { |
| 2774 | // Assigning to a const object has undefined behavior. |
| 2775 | if (QT.isConstQualified()) { |
| 2776 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2777 | return false; |
| 2778 | } |
| 2779 | return true; |
| 2780 | } |
| 2781 | |
| 2782 | bool failed() { return false; } |
| 2783 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2784 | switch (Subobj.getKind()) { |
| 2785 | case APValue::Int: |
| 2786 | return found(Subobj.getInt(), SubobjType); |
| 2787 | case APValue::Float: |
| 2788 | return found(Subobj.getFloat(), SubobjType); |
| 2789 | case APValue::ComplexInt: |
| 2790 | case APValue::ComplexFloat: |
| 2791 | // FIXME: Implement complex compound assignment. |
| 2792 | Info.Diag(E); |
| 2793 | return false; |
| 2794 | case APValue::LValue: |
| 2795 | return foundPointer(Subobj, SubobjType); |
| 2796 | default: |
| 2797 | // FIXME: can this happen? |
| 2798 | Info.Diag(E); |
| 2799 | return false; |
| 2800 | } |
| 2801 | } |
| 2802 | bool found(APSInt &Value, QualType SubobjType) { |
| 2803 | if (!checkConst(SubobjType)) |
| 2804 | return false; |
| 2805 | |
| 2806 | if (!SubobjType->isIntegerType() || !RHS.isInt()) { |
| 2807 | // We don't support compound assignment on integer-cast-to-pointer |
| 2808 | // values. |
| 2809 | Info.Diag(E); |
| 2810 | return false; |
| 2811 | } |
| 2812 | |
| 2813 | APSInt LHS = HandleIntToIntCast(Info, E, PromotedLHSType, |
| 2814 | SubobjType, Value); |
| 2815 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
| 2816 | return false; |
| 2817 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
| 2818 | return true; |
| 2819 | } |
| 2820 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2821 | return checkConst(SubobjType) && |
| 2822 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
| 2823 | Value) && |
| 2824 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
| 2825 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2826 | } |
| 2827 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 2828 | if (!checkConst(SubobjType)) |
| 2829 | return false; |
| 2830 | |
| 2831 | QualType PointeeType; |
| 2832 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 2833 | PointeeType = PT->getPointeeType(); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2834 | |
| 2835 | if (PointeeType.isNull() || !RHS.isInt() || |
| 2836 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2837 | Info.Diag(E); |
| 2838 | return false; |
| 2839 | } |
| 2840 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2841 | int64_t Offset = getExtValue(RHS.getInt()); |
| 2842 | if (Opcode == BO_Sub) |
| 2843 | Offset = -Offset; |
| 2844 | |
| 2845 | LValue LVal; |
| 2846 | LVal.setFrom(Info.Ctx, Subobj); |
| 2847 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
| 2848 | return false; |
| 2849 | LVal.moveInto(Subobj); |
| 2850 | return true; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2851 | } |
| 2852 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2853 | llvm_unreachable("shouldn't encounter string elements here"); |
| 2854 | } |
| 2855 | }; |
| 2856 | } // end anonymous namespace |
| 2857 | |
| 2858 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
| 2859 | |
| 2860 | /// Perform a compound assignment of LVal <op>= RVal. |
| 2861 | static bool handleCompoundAssignment( |
| 2862 | EvalInfo &Info, const Expr *E, |
| 2863 | const LValue &LVal, QualType LValType, QualType PromotedLValType, |
| 2864 | BinaryOperatorKind Opcode, const APValue &RVal) { |
| 2865 | if (LVal.Designator.Invalid) |
| 2866 | return false; |
| 2867 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2868 | if (!Info.getLangOpts().CPlusPlus14) { |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2869 | Info.Diag(E); |
| 2870 | return false; |
| 2871 | } |
| 2872 | |
| 2873 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 2874 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
| 2875 | RVal }; |
| 2876 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 2877 | } |
| 2878 | |
| 2879 | namespace { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2880 | struct IncDecSubobjectHandler { |
| 2881 | EvalInfo &Info; |
| 2882 | const Expr *E; |
| 2883 | AccessKinds AccessKind; |
| 2884 | APValue *Old; |
| 2885 | |
| 2886 | typedef bool result_type; |
| 2887 | |
| 2888 | bool checkConst(QualType QT) { |
| 2889 | // Assigning to a const object has undefined behavior. |
| 2890 | if (QT.isConstQualified()) { |
| 2891 | Info.Diag(E, diag::note_constexpr_modify_const_type) << QT; |
| 2892 | return false; |
| 2893 | } |
| 2894 | return true; |
| 2895 | } |
| 2896 | |
| 2897 | bool failed() { return false; } |
| 2898 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2899 | // Stash the old value. Also clear Old, so we don't clobber it later |
| 2900 | // if we're post-incrementing a complex. |
| 2901 | if (Old) { |
| 2902 | *Old = Subobj; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2903 | Old = nullptr; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | switch (Subobj.getKind()) { |
| 2907 | case APValue::Int: |
| 2908 | return found(Subobj.getInt(), SubobjType); |
| 2909 | case APValue::Float: |
| 2910 | return found(Subobj.getFloat(), SubobjType); |
| 2911 | case APValue::ComplexInt: |
| 2912 | return found(Subobj.getComplexIntReal(), |
| 2913 | SubobjType->castAs<ComplexType>()->getElementType() |
| 2914 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 2915 | case APValue::ComplexFloat: |
| 2916 | return found(Subobj.getComplexFloatReal(), |
| 2917 | SubobjType->castAs<ComplexType>()->getElementType() |
| 2918 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 2919 | case APValue::LValue: |
| 2920 | return foundPointer(Subobj, SubobjType); |
| 2921 | default: |
| 2922 | // FIXME: can this happen? |
| 2923 | Info.Diag(E); |
| 2924 | return false; |
| 2925 | } |
| 2926 | } |
| 2927 | bool found(APSInt &Value, QualType SubobjType) { |
| 2928 | if (!checkConst(SubobjType)) |
| 2929 | return false; |
| 2930 | |
| 2931 | if (!SubobjType->isIntegerType()) { |
| 2932 | // We don't support increment / decrement on integer-cast-to-pointer |
| 2933 | // values. |
| 2934 | Info.Diag(E); |
| 2935 | return false; |
| 2936 | } |
| 2937 | |
| 2938 | if (Old) *Old = APValue(Value); |
| 2939 | |
| 2940 | // bool arithmetic promotes to int, and the conversion back to bool |
| 2941 | // doesn't reduce mod 2^n, so special-case it. |
| 2942 | if (SubobjType->isBooleanType()) { |
| 2943 | if (AccessKind == AK_Increment) |
| 2944 | Value = 1; |
| 2945 | else |
| 2946 | Value = !Value; |
| 2947 | return true; |
| 2948 | } |
| 2949 | |
| 2950 | bool WasNegative = Value.isNegative(); |
| 2951 | if (AccessKind == AK_Increment) { |
| 2952 | ++Value; |
| 2953 | |
| 2954 | if (!WasNegative && Value.isNegative() && |
| 2955 | isOverflowingIntegerType(Info.Ctx, SubobjType)) { |
| 2956 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
| 2957 | HandleOverflow(Info, E, ActualValue, SubobjType); |
| 2958 | } |
| 2959 | } else { |
| 2960 | --Value; |
| 2961 | |
| 2962 | if (WasNegative && !Value.isNegative() && |
| 2963 | isOverflowingIntegerType(Info.Ctx, SubobjType)) { |
| 2964 | unsigned BitWidth = Value.getBitWidth(); |
| 2965 | APSInt ActualValue(Value.sext(BitWidth + 1), /*IsUnsigned*/false); |
| 2966 | ActualValue.setBit(BitWidth); |
| 2967 | HandleOverflow(Info, E, ActualValue, SubobjType); |
| 2968 | } |
| 2969 | } |
| 2970 | return true; |
| 2971 | } |
| 2972 | bool found(APFloat &Value, QualType SubobjType) { |
| 2973 | if (!checkConst(SubobjType)) |
| 2974 | return false; |
| 2975 | |
| 2976 | if (Old) *Old = APValue(Value); |
| 2977 | |
| 2978 | APFloat One(Value.getSemantics(), 1); |
| 2979 | if (AccessKind == AK_Increment) |
| 2980 | Value.add(One, APFloat::rmNearestTiesToEven); |
| 2981 | else |
| 2982 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
| 2983 | return true; |
| 2984 | } |
| 2985 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 2986 | if (!checkConst(SubobjType)) |
| 2987 | return false; |
| 2988 | |
| 2989 | QualType PointeeType; |
| 2990 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 2991 | PointeeType = PT->getPointeeType(); |
| 2992 | else { |
| 2993 | Info.Diag(E); |
| 2994 | return false; |
| 2995 | } |
| 2996 | |
| 2997 | LValue LVal; |
| 2998 | LVal.setFrom(Info.Ctx, Subobj); |
| 2999 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
| 3000 | AccessKind == AK_Increment ? 1 : -1)) |
| 3001 | return false; |
| 3002 | LVal.moveInto(Subobj); |
| 3003 | return true; |
| 3004 | } |
| 3005 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 3006 | llvm_unreachable("shouldn't encounter string elements here"); |
| 3007 | } |
| 3008 | }; |
| 3009 | } // end anonymous namespace |
| 3010 | |
| 3011 | /// Perform an increment or decrement on LVal. |
| 3012 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
| 3013 | QualType LValType, bool IsIncrement, APValue *Old) { |
| 3014 | if (LVal.Designator.Invalid) |
| 3015 | return false; |
| 3016 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3017 | if (!Info.getLangOpts().CPlusPlus14) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3018 | Info.Diag(E); |
| 3019 | return false; |
| 3020 | } |
| 3021 | |
| 3022 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
| 3023 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
| 3024 | IncDecSubobjectHandler Handler = { Info, E, AK, Old }; |
| 3025 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 3026 | } |
| 3027 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3028 | /// Build an lvalue for the object argument of a member function call. |
| 3029 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 3030 | LValue &This) { |
| 3031 | if (Object->getType()->isPointerType()) |
| 3032 | return EvaluatePointer(Object, This, Info); |
| 3033 | |
| 3034 | if (Object->isGLValue()) |
| 3035 | return EvaluateLValue(Object, This, Info); |
| 3036 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3037 | if (Object->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3038 | return EvaluateTemporary(Object, This, Info); |
| 3039 | |
Richard Smith | 3e79a57 | 2014-06-11 19:53:12 +0000 | [diff] [blame] | 3040 | Info.Diag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3041 | return false; |
| 3042 | } |
| 3043 | |
| 3044 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 3045 | /// lvalue referring to the result. |
| 3046 | /// |
| 3047 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3048 | /// \param LV - An lvalue referring to the base of the member pointer. |
| 3049 | /// \param RHS - The member pointer expression. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3050 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 3051 | /// the resulting LValue subobject designator. This is not possible when |
| 3052 | /// creating a bound member function. |
| 3053 | /// \return The field or method declaration to which the member pointer refers, |
| 3054 | /// or 0 if evaluation fails. |
| 3055 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3056 | QualType LVType, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3057 | LValue &LV, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3058 | const Expr *RHS, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3059 | bool IncludeMember = true) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3060 | MemberPtr MemPtr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3061 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3062 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3063 | |
| 3064 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 3065 | // member value, the behavior is undefined. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3066 | if (!MemPtr.getDecl()) { |
| 3067 | // FIXME: Specific diagnostic. |
| 3068 | Info.Diag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3069 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3070 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3071 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3072 | if (MemPtr.isDerivedMember()) { |
| 3073 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3074 | // The end of the derived-to-base path for the base object must match the |
| 3075 | // derived-to-base path for the member pointer. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3076 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3077 | LV.Designator.Entries.size()) { |
| 3078 | Info.Diag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3079 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3080 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3081 | unsigned PathLengthToMember = |
| 3082 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 3083 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 3084 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 3085 | LV.Designator.Entries[PathLengthToMember + I]); |
| 3086 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3087 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
| 3088 | Info.Diag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3089 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3090 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3094 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3095 | PathLengthToMember)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3096 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3097 | } else if (!MemPtr.Path.empty()) { |
| 3098 | // Extend the LValue path with the member pointer's path. |
| 3099 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 3100 | MemPtr.Path.size() + IncludeMember); |
| 3101 | |
| 3102 | // Walk down to the appropriate base class. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3103 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 3104 | LVType = PT->getPointeeType(); |
| 3105 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 3106 | assert(RD && "member pointer access on non-class-type expression"); |
| 3107 | // The first class in the path is that of the lvalue. |
| 3108 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 3109 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3110 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3111 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3112 | RD = Base; |
| 3113 | } |
| 3114 | // Finally cast to the class containing the member. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3115 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
| 3116 | MemPtr.getContainingRecord())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3117 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | // Add the member. Note that we cannot build bound member functions here. |
| 3121 | if (IncludeMember) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3122 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3123 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3124 | return nullptr; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3125 | } else if (const IndirectFieldDecl *IFD = |
| 3126 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3127 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3128 | return nullptr; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3129 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3130 | llvm_unreachable("can't construct reference to bound member function"); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3131 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | return MemPtr.getDecl(); |
| 3135 | } |
| 3136 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3137 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 3138 | const BinaryOperator *BO, |
| 3139 | LValue &LV, |
| 3140 | bool IncludeMember = true) { |
| 3141 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 3142 | |
| 3143 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
| 3144 | if (Info.keepEvaluatingAfterFailure()) { |
| 3145 | MemberPtr MemPtr; |
| 3146 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
| 3147 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3148 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3149 | } |
| 3150 | |
| 3151 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
| 3152 | BO->getRHS(), IncludeMember); |
| 3153 | } |
| 3154 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3155 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 3156 | /// the provided lvalue, which currently refers to the base object. |
| 3157 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 3158 | LValue &Result) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3159 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3160 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3161 | return false; |
| 3162 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3163 | QualType TargetQT = E->getType(); |
| 3164 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 3165 | TargetQT = PT->getPointeeType(); |
| 3166 | |
| 3167 | // Check this cast lands within the final derived-to-base subobject path. |
| 3168 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3169 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3170 | << D.MostDerivedType << TargetQT; |
| 3171 | return false; |
| 3172 | } |
| 3173 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3174 | // Check the type of the final cast. We don't need to check the path, |
| 3175 | // since a cast can only be formed if the path is unique. |
| 3176 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3177 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 3178 | const CXXRecordDecl *FinalType; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3179 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 3180 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 3181 | else |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3182 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3183 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3184 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3185 | << D.MostDerivedType << TargetQT; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3186 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3187 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3188 | |
| 3189 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3190 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 3193 | namespace { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3194 | enum EvalStmtResult { |
| 3195 | /// Evaluation failed. |
| 3196 | ESR_Failed, |
| 3197 | /// Hit a 'return' statement. |
| 3198 | ESR_Returned, |
| 3199 | /// Evaluation succeeded. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3200 | ESR_Succeeded, |
| 3201 | /// Hit a 'continue' statement. |
| 3202 | ESR_Continue, |
| 3203 | /// Hit a 'break' statement. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3204 | ESR_Break, |
| 3205 | /// Still scanning for 'case' or 'default' statement. |
| 3206 | ESR_CaseNotFound |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3207 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3208 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3209 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3210 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
| 3211 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3212 | // We don't need to evaluate the initializer for a static local. |
| 3213 | if (!VD->hasLocalStorage()) |
| 3214 | return true; |
| 3215 | |
| 3216 | LValue Result; |
| 3217 | Result.set(VD, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3218 | APValue &Val = Info.CurrentCall->createTemporary(VD, true); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3219 | |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 3220 | const Expr *InitE = VD->getInit(); |
| 3221 | if (!InitE) { |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3222 | Info.Diag(D->getLocStart(), diag::note_constexpr_uninitialized) |
| 3223 | << false << VD->getType(); |
| 3224 | Val = APValue(); |
| 3225 | return false; |
| 3226 | } |
| 3227 | |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 3228 | if (InitE->isValueDependent()) |
| 3229 | return false; |
| 3230 | |
| 3231 | if (!EvaluateInPlace(Val, Info, Result, InitE)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3232 | // Wipe out any partially-computed value, to allow tracking that this |
| 3233 | // evaluation failed. |
| 3234 | Val = APValue(); |
| 3235 | return false; |
| 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | return true; |
| 3240 | } |
| 3241 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3242 | /// Evaluate a condition (either a variable declaration or an expression). |
| 3243 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
| 3244 | const Expr *Cond, bool &Result) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3245 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3246 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
| 3247 | return false; |
| 3248 | return EvaluateAsBooleanCondition(Cond, Result, Info); |
| 3249 | } |
| 3250 | |
| 3251 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3252 | const Stmt *S, |
| 3253 | const SwitchCase *SC = nullptr); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3254 | |
| 3255 | /// Evaluate the body of a loop, and translate the result as appropriate. |
| 3256 | static EvalStmtResult EvaluateLoopBody(APValue &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3257 | const Stmt *Body, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3258 | const SwitchCase *Case = nullptr) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3259 | BlockScopeRAII Scope(Info); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3260 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case)) { |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3261 | case ESR_Break: |
| 3262 | return ESR_Succeeded; |
| 3263 | case ESR_Succeeded: |
| 3264 | case ESR_Continue: |
| 3265 | return ESR_Continue; |
| 3266 | case ESR_Failed: |
| 3267 | case ESR_Returned: |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3268 | case ESR_CaseNotFound: |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3269 | return ESR; |
| 3270 | } |
Hans Wennborg | 9242bd1 | 2013-05-06 15:13:34 +0000 | [diff] [blame] | 3271 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3274 | /// Evaluate a switch statement. |
| 3275 | static EvalStmtResult EvaluateSwitch(APValue &Result, EvalInfo &Info, |
| 3276 | const SwitchStmt *SS) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3277 | BlockScopeRAII Scope(Info); |
| 3278 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3279 | // Evaluate the switch condition. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3280 | APSInt Value; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3281 | { |
| 3282 | FullExpressionRAII Scope(Info); |
| 3283 | if (SS->getConditionVariable() && |
| 3284 | !EvaluateDecl(Info, SS->getConditionVariable())) |
| 3285 | return ESR_Failed; |
| 3286 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
| 3287 | return ESR_Failed; |
| 3288 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3289 | |
| 3290 | // Find the switch case corresponding to the value of the condition. |
| 3291 | // FIXME: Cache this lookup. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3292 | const SwitchCase *Found = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3293 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 3294 | SC = SC->getNextSwitchCase()) { |
| 3295 | if (isa<DefaultStmt>(SC)) { |
| 3296 | Found = SC; |
| 3297 | continue; |
| 3298 | } |
| 3299 | |
| 3300 | const CaseStmt *CS = cast<CaseStmt>(SC); |
| 3301 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
| 3302 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
| 3303 | : LHS; |
| 3304 | if (LHS <= Value && Value <= RHS) { |
| 3305 | Found = SC; |
| 3306 | break; |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | if (!Found) |
| 3311 | return ESR_Succeeded; |
| 3312 | |
| 3313 | // Search the switch body for the switch case and evaluate it from there. |
| 3314 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found)) { |
| 3315 | case ESR_Break: |
| 3316 | return ESR_Succeeded; |
| 3317 | case ESR_Succeeded: |
| 3318 | case ESR_Continue: |
| 3319 | case ESR_Failed: |
| 3320 | case ESR_Returned: |
| 3321 | return ESR; |
| 3322 | case ESR_CaseNotFound: |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3323 | // This can only happen if the switch case is nested within a statement |
| 3324 | // expression. We have no intention of supporting that. |
| 3325 | Info.Diag(Found->getLocStart(), diag::note_constexpr_stmt_expr_unsupported); |
| 3326 | return ESR_Failed; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3327 | } |
Richard Smith | f8cf9d4 | 2013-05-13 20:33:30 +0000 | [diff] [blame] | 3328 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3331 | // Evaluate a statement. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3332 | static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3333 | const Stmt *S, const SwitchCase *Case) { |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 3334 | if (!Info.nextStep(S)) |
| 3335 | return ESR_Failed; |
| 3336 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3337 | // If we're hunting down a 'case' or 'default' label, recurse through |
| 3338 | // substatements until we hit the label. |
| 3339 | if (Case) { |
| 3340 | // FIXME: We don't start the lifetime of objects whose initialization we |
| 3341 | // jump over. However, such objects must be of class type with a trivial |
| 3342 | // default constructor that initialize all subobjects, so must be empty, |
| 3343 | // so this almost never matters. |
| 3344 | switch (S->getStmtClass()) { |
| 3345 | case Stmt::CompoundStmtClass: |
| 3346 | // FIXME: Precompute which substatement of a compound statement we |
| 3347 | // would jump to, and go straight there rather than performing a |
| 3348 | // linear scan each time. |
| 3349 | case Stmt::LabelStmtClass: |
| 3350 | case Stmt::AttributedStmtClass: |
| 3351 | case Stmt::DoStmtClass: |
| 3352 | break; |
| 3353 | |
| 3354 | case Stmt::CaseStmtClass: |
| 3355 | case Stmt::DefaultStmtClass: |
| 3356 | if (Case == S) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3357 | Case = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3358 | break; |
| 3359 | |
| 3360 | case Stmt::IfStmtClass: { |
| 3361 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
| 3362 | // straight there rather than scanning both sides. |
| 3363 | const IfStmt *IS = cast<IfStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3364 | |
| 3365 | // Wrap the evaluation in a block scope, in case it's a DeclStmt |
| 3366 | // preceded by our switch label. |
| 3367 | BlockScopeRAII Scope(Info); |
| 3368 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3369 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
| 3370 | if (ESR != ESR_CaseNotFound || !IS->getElse()) |
| 3371 | return ESR; |
| 3372 | return EvaluateStmt(Result, Info, IS->getElse(), Case); |
| 3373 | } |
| 3374 | |
| 3375 | case Stmt::WhileStmtClass: { |
| 3376 | EvalStmtResult ESR = |
| 3377 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
| 3378 | if (ESR != ESR_Continue) |
| 3379 | return ESR; |
| 3380 | break; |
| 3381 | } |
| 3382 | |
| 3383 | case Stmt::ForStmtClass: { |
| 3384 | const ForStmt *FS = cast<ForStmt>(S); |
| 3385 | EvalStmtResult ESR = |
| 3386 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
| 3387 | if (ESR != ESR_Continue) |
| 3388 | return ESR; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3389 | if (FS->getInc()) { |
| 3390 | FullExpressionRAII IncScope(Info); |
| 3391 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 3392 | return ESR_Failed; |
| 3393 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3394 | break; |
| 3395 | } |
| 3396 | |
| 3397 | case Stmt::DeclStmtClass: |
| 3398 | // FIXME: If the variable has initialization that can't be jumped over, |
| 3399 | // bail out of any immediately-surrounding compound-statement too. |
| 3400 | default: |
| 3401 | return ESR_CaseNotFound; |
| 3402 | } |
| 3403 | } |
| 3404 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3405 | switch (S->getStmtClass()) { |
| 3406 | default: |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3407 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3408 | // Don't bother evaluating beyond an expression-statement which couldn't |
| 3409 | // be evaluated. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3410 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3411 | if (!EvaluateIgnoredValue(Info, E)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3412 | return ESR_Failed; |
| 3413 | return ESR_Succeeded; |
| 3414 | } |
| 3415 | |
| 3416 | Info.Diag(S->getLocStart()); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3417 | return ESR_Failed; |
| 3418 | |
| 3419 | case Stmt::NullStmtClass: |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3420 | return ESR_Succeeded; |
| 3421 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3422 | case Stmt::DeclStmtClass: { |
| 3423 | const DeclStmt *DS = cast<DeclStmt>(S); |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 3424 | for (const auto *DclIt : DS->decls()) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3425 | // Each declaration initialization is its own full-expression. |
| 3426 | // FIXME: This isn't quite right; if we're performing aggregate |
| 3427 | // initialization, each braced subexpression is its own full-expression. |
| 3428 | FullExpressionRAII Scope(Info); |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 3429 | if (!EvaluateDecl(Info, DclIt) && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3430 | return ESR_Failed; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3431 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3432 | return ESR_Succeeded; |
| 3433 | } |
| 3434 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3435 | case Stmt::ReturnStmtClass: { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3436 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3437 | FullExpressionRAII Scope(Info); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3438 | if (RetExpr && !Evaluate(Result, Info, RetExpr)) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3439 | return ESR_Failed; |
| 3440 | return ESR_Returned; |
| 3441 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3442 | |
| 3443 | case Stmt::CompoundStmtClass: { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3444 | BlockScopeRAII Scope(Info); |
| 3445 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3446 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 3447 | for (const auto *BI : CS->body()) { |
| 3448 | EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3449 | if (ESR == ESR_Succeeded) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3450 | Case = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3451 | else if (ESR != ESR_CaseNotFound) |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3452 | return ESR; |
| 3453 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3454 | return Case ? ESR_CaseNotFound : ESR_Succeeded; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3455 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3456 | |
| 3457 | case Stmt::IfStmtClass: { |
| 3458 | const IfStmt *IS = cast<IfStmt>(S); |
| 3459 | |
| 3460 | // Evaluate the condition, as either a var decl or as an expression. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3461 | BlockScopeRAII Scope(Info); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3462 | bool Cond; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3463 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3464 | return ESR_Failed; |
| 3465 | |
| 3466 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
| 3467 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
| 3468 | if (ESR != ESR_Succeeded) |
| 3469 | return ESR; |
| 3470 | } |
| 3471 | return ESR_Succeeded; |
| 3472 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3473 | |
| 3474 | case Stmt::WhileStmtClass: { |
| 3475 | const WhileStmt *WS = cast<WhileStmt>(S); |
| 3476 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3477 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3478 | bool Continue; |
| 3479 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
| 3480 | Continue)) |
| 3481 | return ESR_Failed; |
| 3482 | if (!Continue) |
| 3483 | break; |
| 3484 | |
| 3485 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
| 3486 | if (ESR != ESR_Continue) |
| 3487 | return ESR; |
| 3488 | } |
| 3489 | return ESR_Succeeded; |
| 3490 | } |
| 3491 | |
| 3492 | case Stmt::DoStmtClass: { |
| 3493 | const DoStmt *DS = cast<DoStmt>(S); |
| 3494 | bool Continue; |
| 3495 | do { |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3496 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3497 | if (ESR != ESR_Continue) |
| 3498 | return ESR; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3499 | Case = nullptr; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3500 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3501 | FullExpressionRAII CondScope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3502 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info)) |
| 3503 | return ESR_Failed; |
| 3504 | } while (Continue); |
| 3505 | return ESR_Succeeded; |
| 3506 | } |
| 3507 | |
| 3508 | case Stmt::ForStmtClass: { |
| 3509 | const ForStmt *FS = cast<ForStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3510 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3511 | if (FS->getInit()) { |
| 3512 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| 3513 | if (ESR != ESR_Succeeded) |
| 3514 | return ESR; |
| 3515 | } |
| 3516 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3517 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3518 | bool Continue = true; |
| 3519 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
| 3520 | FS->getCond(), Continue)) |
| 3521 | return ESR_Failed; |
| 3522 | if (!Continue) |
| 3523 | break; |
| 3524 | |
| 3525 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 3526 | if (ESR != ESR_Continue) |
| 3527 | return ESR; |
| 3528 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3529 | if (FS->getInc()) { |
| 3530 | FullExpressionRAII IncScope(Info); |
| 3531 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 3532 | return ESR_Failed; |
| 3533 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3534 | } |
| 3535 | return ESR_Succeeded; |
| 3536 | } |
| 3537 | |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3538 | case Stmt::CXXForRangeStmtClass: { |
| 3539 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3540 | BlockScopeRAII Scope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3541 | |
| 3542 | // Initialize the __range variable. |
| 3543 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
| 3544 | if (ESR != ESR_Succeeded) |
| 3545 | return ESR; |
| 3546 | |
| 3547 | // Create the __begin and __end iterators. |
| 3548 | ESR = EvaluateStmt(Result, Info, FS->getBeginEndStmt()); |
| 3549 | if (ESR != ESR_Succeeded) |
| 3550 | return ESR; |
| 3551 | |
| 3552 | while (true) { |
| 3553 | // Condition: __begin != __end. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3554 | { |
| 3555 | bool Continue = true; |
| 3556 | FullExpressionRAII CondExpr(Info); |
| 3557 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
| 3558 | return ESR_Failed; |
| 3559 | if (!Continue) |
| 3560 | break; |
| 3561 | } |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3562 | |
| 3563 | // User's variable declaration, initialized by *__begin. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3564 | BlockScopeRAII InnerScope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 3565 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
| 3566 | if (ESR != ESR_Succeeded) |
| 3567 | return ESR; |
| 3568 | |
| 3569 | // Loop body. |
| 3570 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 3571 | if (ESR != ESR_Continue) |
| 3572 | return ESR; |
| 3573 | |
| 3574 | // Increment: ++__begin |
| 3575 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 3576 | return ESR_Failed; |
| 3577 | } |
| 3578 | |
| 3579 | return ESR_Succeeded; |
| 3580 | } |
| 3581 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3582 | case Stmt::SwitchStmtClass: |
| 3583 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
| 3584 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3585 | case Stmt::ContinueStmtClass: |
| 3586 | return ESR_Continue; |
| 3587 | |
| 3588 | case Stmt::BreakStmtClass: |
| 3589 | return ESR_Break; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3590 | |
| 3591 | case Stmt::LabelStmtClass: |
| 3592 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
| 3593 | |
| 3594 | case Stmt::AttributedStmtClass: |
| 3595 | // As a general principle, C++11 attributes can be ignored without |
| 3596 | // any semantic impact. |
| 3597 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
| 3598 | Case); |
| 3599 | |
| 3600 | case Stmt::CaseStmtClass: |
| 3601 | case Stmt::DefaultStmtClass: |
| 3602 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3603 | } |
| 3604 | } |
| 3605 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3606 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 3607 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 3608 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 3609 | /// so we need special handling. |
| 3610 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3611 | const CXXConstructorDecl *CD, |
| 3612 | bool IsValueInitialization) { |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3613 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 3614 | return false; |
| 3615 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 3616 | // Value-initialization does not call a trivial default constructor, so such a |
| 3617 | // call is a core constant expression whether or not the constructor is |
| 3618 | // constexpr. |
| 3619 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3620 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 3621 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 3622 | // we should be much more explicit about why it's not constexpr. |
| 3623 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 3624 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 3625 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 3626 | } else { |
| 3627 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 3628 | } |
| 3629 | } |
| 3630 | return true; |
| 3631 | } |
| 3632 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3633 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 3634 | /// expression. |
| 3635 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 3636 | const FunctionDecl *Declaration, |
| 3637 | const FunctionDecl *Definition) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3638 | // Potential constant expressions can contain calls to declared, but not yet |
| 3639 | // defined, constexpr functions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3640 | if (Info.checkingPotentialConstantExpression() && !Definition && |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3641 | Declaration->isConstexpr()) |
| 3642 | return false; |
| 3643 | |
Richard Smith | 0838f3a | 2013-05-14 05:18:44 +0000 | [diff] [blame] | 3644 | // Bail out with no diagnostic if the function declaration itself is invalid. |
| 3645 | // We will have produced a relevant diagnostic while parsing it. |
| 3646 | if (Declaration->isInvalidDecl()) |
| 3647 | return false; |
| 3648 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3649 | // Can we evaluate this function call? |
| 3650 | if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl()) |
| 3651 | return true; |
| 3652 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3653 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3654 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 3655 | // FIXME: If DiagDecl is an implicitly-declared special member function, we |
| 3656 | // should be much more explicit about why it's not constexpr. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 3657 | Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| 3658 | << DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl) |
| 3659 | << DiagDecl; |
| 3660 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 3661 | } else { |
| 3662 | Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| 3663 | } |
| 3664 | return false; |
| 3665 | } |
| 3666 | |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 3667 | /// Determine if a class has any fields that might need to be copied by a |
| 3668 | /// trivial copy or move operation. |
| 3669 | static bool hasFields(const CXXRecordDecl *RD) { |
| 3670 | if (!RD || RD->isEmpty()) |
| 3671 | return false; |
| 3672 | for (auto *FD : RD->fields()) { |
| 3673 | if (FD->isUnnamedBitfield()) |
| 3674 | continue; |
| 3675 | return true; |
| 3676 | } |
| 3677 | for (auto &Base : RD->bases()) |
| 3678 | if (hasFields(Base.getType()->getAsCXXRecordDecl())) |
| 3679 | return true; |
| 3680 | return false; |
| 3681 | } |
| 3682 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3683 | namespace { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3684 | typedef SmallVector<APValue, 8> ArgVector; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 3688 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 3689 | EvalInfo &Info) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3690 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3691 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3692 | I != E; ++I) { |
| 3693 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 3694 | // If we're checking for a potential constant expression, evaluate all |
| 3695 | // initializers even if some of them fail. |
| 3696 | if (!Info.keepEvaluatingAfterFailure()) |
| 3697 | return false; |
| 3698 | Success = false; |
| 3699 | } |
| 3700 | } |
| 3701 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3704 | /// Evaluate a function call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3705 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 3706 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3707 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3708 | EvalInfo &Info, APValue &Result) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3709 | ArgVector ArgValues(Args.size()); |
| 3710 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 3711 | return false; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3712 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3713 | if (!Info.CheckCallLimit(CallLoc)) |
| 3714 | return false; |
| 3715 | |
| 3716 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 3717 | |
| 3718 | // For a trivial copy or move assignment, perform an APValue copy. This is |
| 3719 | // essential for unions, where the operations performed by the assignment |
| 3720 | // operator cannot be represented as statements. |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 3721 | // |
| 3722 | // Skip this for non-union classes with no fields; in that case, the defaulted |
| 3723 | // copy/move does not actually read the object. |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 3724 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 3725 | if (MD && MD->isDefaulted() && |
| 3726 | (MD->getParent()->isUnion() || |
| 3727 | (MD->isTrivial() && hasFields(MD->getParent())))) { |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 3728 | assert(This && |
| 3729 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
| 3730 | LValue RHS; |
| 3731 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 3732 | APValue RHSValue; |
| 3733 | if (!handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 3734 | RHS, RHSValue)) |
| 3735 | return false; |
| 3736 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(Info.Ctx), |
| 3737 | RHSValue)) |
| 3738 | return false; |
| 3739 | This->moveInto(Result); |
| 3740 | return true; |
| 3741 | } |
| 3742 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3743 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Body); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3744 | if (ESR == ESR_Succeeded) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3745 | if (Callee->getReturnType()->isVoidType()) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3746 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3747 | Info.Diag(Callee->getLocEnd(), diag::note_constexpr_no_return); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3748 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3749 | return ESR == ESR_Returned; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3752 | /// Evaluate a constructor call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3753 | static bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This, |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3754 | ArrayRef<const Expr*> Args, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3755 | const CXXConstructorDecl *Definition, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3756 | EvalInfo &Info, APValue &Result) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3757 | ArgVector ArgValues(Args.size()); |
| 3758 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 3759 | return false; |
| 3760 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3761 | if (!Info.CheckCallLimit(CallLoc)) |
| 3762 | return false; |
| 3763 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3764 | const CXXRecordDecl *RD = Definition->getParent(); |
| 3765 | if (RD->getNumVBases()) { |
| 3766 | Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
| 3767 | return false; |
| 3768 | } |
| 3769 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3770 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3771 | |
| 3772 | // If it's a delegating constructor, just delegate. |
| 3773 | if (Definition->isDelegatingConstructor()) { |
| 3774 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | 9ff62af | 2013-11-07 18:45:03 +0000 | [diff] [blame] | 3775 | { |
| 3776 | FullExpressionRAII InitScope(Info); |
| 3777 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit())) |
| 3778 | return false; |
| 3779 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3780 | return EvaluateStmt(Result, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3783 | // For a trivial copy or move constructor, perform an APValue copy. This is |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 3784 | // essential for unions (or classes with anonymous union members), where the |
| 3785 | // operations performed by the constructor cannot be represented by |
| 3786 | // ctor-initializers. |
| 3787 | // |
| 3788 | // Skip this for empty non-union classes; we should not perform an |
| 3789 | // lvalue-to-rvalue conversion on them because their copy constructor does not |
| 3790 | // actually read them. |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 3791 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 3792 | (Definition->getParent()->isUnion() || |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 3793 | (Definition->isTrivial() && hasFields(Definition->getParent())))) { |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3794 | LValue RHS; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3795 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3796 | return handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3797 | RHS, Result); |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | // Reserve space for the struct members. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3801 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3802 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 3803 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3804 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3805 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3806 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 3807 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3808 | // A scope for temporaries lifetime-extended by reference members. |
| 3809 | BlockScopeRAII LifetimeExtendedScope(Info); |
| 3810 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3811 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3812 | unsigned BasesSeen = 0; |
| 3813 | #ifndef NDEBUG |
| 3814 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 3815 | #endif |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3816 | for (const auto *I : Definition->inits()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3817 | LValue Subobject = This; |
| 3818 | APValue *Value = &Result; |
| 3819 | |
| 3820 | // Determine the subobject to initialize. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3821 | FieldDecl *FD = nullptr; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3822 | if (I->isBaseInitializer()) { |
| 3823 | QualType BaseType(I->getBaseClass(), 0); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3824 | #ifndef NDEBUG |
| 3825 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 3826 | // definition. We have already checked for virtual base classes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3827 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 3828 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 3829 | "base class initializers not in expected order"); |
| 3830 | ++BaseIt; |
| 3831 | #endif |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3832 | if (!HandleLValueDirectBase(Info, I->getInit(), Subobject, RD, |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3833 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 3834 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3835 | Value = &Result.getStructBase(BasesSeen++); |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3836 | } else if ((FD = I->getMember())) { |
| 3837 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3838 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3839 | if (RD->isUnion()) { |
| 3840 | Result = APValue(FD); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3841 | Value = &Result.getUnionValue(); |
| 3842 | } else { |
| 3843 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 3844 | } |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3845 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3846 | // Walk the indirect field decl's chain to find the object to initialize, |
| 3847 | // and make sure we've initialized every step along it. |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 3848 | for (auto *C : IFD->chain()) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 3849 | FD = cast<FieldDecl>(C); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3850 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 3851 | // Switch the union field if it differs. This happens if we had |
| 3852 | // preceding zero-initialization, and we're now initializing a union |
| 3853 | // subobject other than the first. |
| 3854 | // FIXME: In this case, the values of the other subobjects are |
| 3855 | // specified, since zero-initialization sets all padding bits to zero. |
| 3856 | if (Value->isUninit() || |
| 3857 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 3858 | if (CD->isUnion()) |
| 3859 | *Value = APValue(FD); |
| 3860 | else |
| 3861 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 3862 | std::distance(CD->field_begin(), CD->field_end())); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3863 | } |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3864 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3865 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3866 | if (CD->isUnion()) |
| 3867 | Value = &Value->getUnionValue(); |
| 3868 | else |
| 3869 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3870 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3871 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3872 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3873 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3874 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3875 | FullExpressionRAII InitScope(Info); |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 3876 | if (!EvaluateInPlace(*Value, Info, Subobject, I->getInit()) || |
| 3877 | (FD && FD->isBitField() && !truncateBitfieldValue(Info, I->getInit(), |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3878 | *Value, FD))) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3879 | // If we're checking for a potential constant expression, evaluate all |
| 3880 | // initializers even if some of them fail. |
| 3881 | if (!Info.keepEvaluatingAfterFailure()) |
| 3882 | return false; |
| 3883 | Success = false; |
| 3884 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3887 | return Success && |
| 3888 | EvaluateStmt(Result, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 3891 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3892 | // Generic Evaluation |
| 3893 | //===----------------------------------------------------------------------===// |
| 3894 | namespace { |
| 3895 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3896 | template <class Derived> |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3897 | class ExprEvaluatorBase |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3898 | : public ConstStmtVisitor<Derived, bool> { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3899 | private: |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3900 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3901 | return static_cast<Derived*>(this)->Success(V, E); |
| 3902 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3903 | bool DerivedZeroInitialization(const Expr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 3904 | return static_cast<Derived*>(this)->ZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 3905 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3906 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3907 | // Check whether a conditional operator with a non-constant condition is a |
| 3908 | // potential constant expression. If neither arm is a potential constant |
| 3909 | // expression, then the conditional operator is not either. |
| 3910 | template<typename ConditionalOperator> |
| 3911 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3912 | assert(Info.checkingPotentialConstantExpression()); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3913 | |
| 3914 | // Speculatively evaluate both arms. |
| 3915 | { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3916 | SmallVector<PartialDiagnosticAt, 8> Diag; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3917 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
| 3918 | |
| 3919 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 3920 | if (Diag.empty()) |
| 3921 | return; |
| 3922 | |
| 3923 | Diag.clear(); |
| 3924 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 3925 | if (Diag.empty()) |
| 3926 | return; |
| 3927 | } |
| 3928 | |
| 3929 | Error(E, diag::note_constexpr_conditional_never_const); |
| 3930 | } |
| 3931 | |
| 3932 | |
| 3933 | template<typename ConditionalOperator> |
| 3934 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 3935 | bool BoolResult; |
| 3936 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3937 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 3938 | CheckPotentialConstantConditional(E); |
| 3939 | return false; |
| 3940 | } |
| 3941 | |
| 3942 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 3943 | return StmtVisitorTy::Visit(EvalExpr); |
| 3944 | } |
| 3945 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3946 | protected: |
| 3947 | EvalInfo &Info; |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3948 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3949 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 3950 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 3951 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3952 | return Info.CCEDiag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3955 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 3956 | |
| 3957 | public: |
| 3958 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 3959 | |
| 3960 | EvalInfo &getEvalInfo() { return Info; } |
| 3961 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3962 | /// Report an evaluation error. This should only be called when an error is |
| 3963 | /// first discovered. When propagating an error, just return false. |
| 3964 | bool Error(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3965 | Info.Diag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3966 | return false; |
| 3967 | } |
| 3968 | bool Error(const Expr *E) { |
| 3969 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 3970 | } |
| 3971 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3972 | bool VisitStmt(const Stmt *) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3973 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3974 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3975 | bool VisitExpr(const Expr *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3976 | return Error(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3977 | } |
| 3978 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3979 | bool VisitParenExpr(const ParenExpr *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3980 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3981 | bool VisitUnaryExtension(const UnaryOperator *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3982 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3983 | bool VisitUnaryPlus(const UnaryOperator *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3984 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3985 | bool VisitChooseExpr(const ChooseExpr *E) |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 3986 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3987 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 3988 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3989 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 3990 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3991 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) |
Richard Smith | f8120ca | 2011-11-09 02:12:41 +0000 | [diff] [blame] | 3992 | { return StmtVisitorTy::Visit(E->getExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 3993 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
Richard Smith | 17e3246 | 2013-09-13 20:51:45 +0000 | [diff] [blame] | 3994 | // The initializer may not have been parsed yet, or might be erroneous. |
| 3995 | if (!E->getExpr()) |
| 3996 | return Error(E); |
| 3997 | return StmtVisitorTy::Visit(E->getExpr()); |
| 3998 | } |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 3999 | // We cannot create any objects for which cleanups are required, so there is |
| 4000 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4001 | bool VisitExprWithCleanups(const ExprWithCleanups *E) |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 4002 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4003 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4004 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4005 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 4006 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 4007 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4008 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4009 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 4010 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 4011 | } |
| 4012 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4013 | bool VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4014 | switch (E->getOpcode()) { |
| 4015 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4016 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4017 | |
| 4018 | case BO_Comma: |
| 4019 | VisitIgnoredValue(E->getLHS()); |
| 4020 | return StmtVisitorTy::Visit(E->getRHS()); |
| 4021 | |
| 4022 | case BO_PtrMemD: |
| 4023 | case BO_PtrMemI: { |
| 4024 | LValue Obj; |
| 4025 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 4026 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4027 | APValue Result; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4028 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4029 | return false; |
| 4030 | return DerivedSuccess(Result, E); |
| 4031 | } |
| 4032 | } |
| 4033 | } |
| 4034 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4035 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 4036 | // Evaluate and cache the common expression. We treat it as a temporary, |
| 4037 | // even though it's not quite the same thing. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4038 | if (!Evaluate(Info.CurrentCall->createTemporary(E->getOpaqueValue(), false), |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 4039 | Info, E->getCommon())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4040 | return false; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4041 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4042 | return HandleConditionalOperator(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4045 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4046 | bool IsBcpCall = false; |
| 4047 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 4048 | // the result is a constant expression if it can be folded without |
| 4049 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 4050 | // for discussion. |
| 4051 | if (const CallExpr *CallCE = |
| 4052 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 4053 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4054 | IsBcpCall = true; |
| 4055 | |
| 4056 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 4057 | // constant expression; we can't check whether it's potentially foldable. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4058 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4059 | return false; |
| 4060 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4061 | FoldConstant Fold(Info, IsBcpCall); |
| 4062 | if (!HandleConditionalOperator(E)) { |
| 4063 | Fold.keepDiagnostics(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4064 | return false; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4065 | } |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4066 | |
| 4067 | return true; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4070 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4071 | if (APValue *Value = Info.CurrentCall->getTemporary(E)) |
| 4072 | return DerivedSuccess(*Value, E); |
| 4073 | |
| 4074 | const Expr *Source = E->getSourceExpr(); |
| 4075 | if (!Source) |
| 4076 | return Error(E); |
| 4077 | if (Source == E) { // sanity checking. |
| 4078 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
| 4079 | return Error(E); |
Argyrios Kyrtzidis | fac35c0 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 4080 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4081 | return StmtVisitorTy::Visit(Source); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4082 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4083 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4084 | bool VisitCallExpr(const CallExpr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4085 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4086 | QualType CalleeType = Callee->getType(); |
| 4087 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4088 | const FunctionDecl *FD = nullptr; |
| 4089 | LValue *This = nullptr, ThisVal; |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 4090 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4091 | bool HasQualifier = false; |
Richard Smith | 656d49d | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 4092 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4093 | // Extract function decl and 'this' pointer from the callee. |
| 4094 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4095 | const ValueDecl *Member = nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4096 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 4097 | // Explicit bound member calls, such as x.f() or p->g(); |
| 4098 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4099 | return false; |
| 4100 | Member = ME->getMemberDecl(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4101 | This = &ThisVal; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4102 | HasQualifier = ME->hasQualifier(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4103 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 4104 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4105 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 4106 | if (!Member) return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4107 | This = &ThisVal; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4108 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4109 | return Error(Callee); |
| 4110 | |
| 4111 | FD = dyn_cast<FunctionDecl>(Member); |
| 4112 | if (!FD) |
| 4113 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4114 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4115 | LValue Call; |
| 4116 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4117 | return false; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4118 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4119 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4120 | return Error(Callee); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4121 | FD = dyn_cast_or_null<FunctionDecl>( |
| 4122 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4123 | if (!FD) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4124 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4125 | |
| 4126 | // Overloaded operator calls to member functions are represented as normal |
| 4127 | // calls with '*this' as the first argument. |
| 4128 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 4129 | if (MD && !MD->isStatic()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4130 | // FIXME: When selecting an implicit conversion for an overloaded |
| 4131 | // operator delete, we sometimes try to evaluate calls to conversion |
| 4132 | // operators without a 'this' parameter! |
| 4133 | if (Args.empty()) |
| 4134 | return Error(E); |
| 4135 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4136 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| 4137 | return false; |
| 4138 | This = &ThisVal; |
| 4139 | Args = Args.slice(1); |
| 4140 | } |
| 4141 | |
| 4142 | // Don't call function pointers which have been cast to some other type. |
| 4143 | if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4144 | return Error(E); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4145 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4146 | return Error(E); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4147 | |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 4148 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 4149 | return false; |
| 4150 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4151 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 4152 | // calls to such functions in constant expressions. |
| 4153 | if (This && !HasQualifier && |
| 4154 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 4155 | return Error(E, diag::note_constexpr_virtual_call); |
| 4156 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4157 | const FunctionDecl *Definition = nullptr; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4158 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4159 | APValue Result; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4160 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4161 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) || |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4162 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, |
| 4163 | Info, Result)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4164 | return false; |
| 4165 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4166 | return DerivedSuccess(Result, E); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4169 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4170 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 4171 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4172 | bool VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 90dc175 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 4173 | if (E->getNumInits() == 0) |
| 4174 | return DerivedZeroInitialization(E); |
| 4175 | if (E->getNumInits() == 1) |
| 4176 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4177 | return Error(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4178 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4179 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4180 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4181 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4182 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4183 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4184 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4185 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4186 | return DerivedZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4187 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4188 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4189 | /// A member expression where the object is a prvalue is itself a prvalue. |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4190 | bool VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4191 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 4192 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4193 | APValue Val; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4194 | if (!Evaluate(Val, Info, E->getBase())) |
| 4195 | return false; |
| 4196 | |
| 4197 | QualType BaseTy = E->getBase()->getType(); |
| 4198 | |
| 4199 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4200 | if (!FD) return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4201 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4202 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4203 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 4204 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4205 | CompleteObject Obj(&Val, BaseTy); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4206 | SubobjectDesignator Designator(BaseTy); |
| 4207 | Designator.addDeclUnchecked(FD); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4208 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4209 | APValue Result; |
| 4210 | return extractSubobject(Info, E, Obj, Designator, Result) && |
| 4211 | DerivedSuccess(Result, E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4214 | bool VisitCastExpr(const CastExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4215 | switch (E->getCastKind()) { |
| 4216 | default: |
| 4217 | break; |
| 4218 | |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4219 | case CK_AtomicToNonAtomic: { |
| 4220 | APValue AtomicVal; |
| 4221 | if (!EvaluateAtomic(E->getSubExpr(), AtomicVal, Info)) |
| 4222 | return false; |
| 4223 | return DerivedSuccess(AtomicVal, E); |
| 4224 | } |
| 4225 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4226 | case CK_NoOp: |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 4227 | case CK_UserDefinedConversion: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4228 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 4229 | |
| 4230 | case CK_LValueToRValue: { |
| 4231 | LValue LVal; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4232 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 4233 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4234 | APValue RVal; |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 4235 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4236 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 4237 | LVal, RVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4238 | return false; |
| 4239 | return DerivedSuccess(RVal, E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4240 | } |
| 4241 | } |
| 4242 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4243 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4246 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4247 | return VisitUnaryPostIncDec(UO); |
| 4248 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4249 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4250 | return VisitUnaryPostIncDec(UO); |
| 4251 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4252 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 4253 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4254 | return Error(UO); |
| 4255 | |
| 4256 | LValue LVal; |
| 4257 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
| 4258 | return false; |
| 4259 | APValue RVal; |
| 4260 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
| 4261 | UO->isIncrementOp(), &RVal)) |
| 4262 | return false; |
| 4263 | return DerivedSuccess(RVal, UO); |
| 4264 | } |
| 4265 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4266 | bool VisitStmtExpr(const StmtExpr *E) { |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4267 | // We will have checked the full-expressions inside the statement expression |
| 4268 | // when they were completed, and don't need to check them again now. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4269 | if (Info.checkingForOverflow()) |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4270 | return Error(E); |
| 4271 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4272 | BlockScopeRAII Scope(Info); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4273 | const CompoundStmt *CS = E->getSubStmt(); |
Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 4274 | if (CS->body_empty()) |
| 4275 | return true; |
| 4276 | |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4277 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 4278 | BE = CS->body_end(); |
| 4279 | /**/; ++BI) { |
| 4280 | if (BI + 1 == BE) { |
| 4281 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
| 4282 | if (!FinalExpr) { |
| 4283 | Info.Diag((*BI)->getLocStart(), |
| 4284 | diag::note_constexpr_stmt_expr_unsupported); |
| 4285 | return false; |
| 4286 | } |
| 4287 | return this->Visit(FinalExpr); |
| 4288 | } |
| 4289 | |
| 4290 | APValue ReturnValue; |
| 4291 | EvalStmtResult ESR = EvaluateStmt(ReturnValue, Info, *BI); |
| 4292 | if (ESR != ESR_Succeeded) { |
| 4293 | // FIXME: If the statement-expression terminated due to 'return', |
| 4294 | // 'break', or 'continue', it would be nice to propagate that to |
| 4295 | // the outer statement evaluation rather than bailing out. |
| 4296 | if (ESR != ESR_Failed) |
| 4297 | Info.Diag((*BI)->getLocStart(), |
| 4298 | diag::note_constexpr_stmt_expr_unsupported); |
| 4299 | return false; |
| 4300 | } |
| 4301 | } |
Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 4302 | |
| 4303 | llvm_unreachable("Return from function from the loop above."); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4304 | } |
| 4305 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4306 | /// Visit a value which is evaluated, but whose value is ignored. |
| 4307 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4308 | EvaluateIgnoredValue(Info, E); |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 4309 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4310 | }; |
| 4311 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4312 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4313 | |
| 4314 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4315 | // Common base class for lvalue and temporary evaluation. |
| 4316 | //===----------------------------------------------------------------------===// |
| 4317 | namespace { |
| 4318 | template<class Derived> |
| 4319 | class LValueExprEvaluatorBase |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4320 | : public ExprEvaluatorBase<Derived> { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4321 | protected: |
| 4322 | LValue &Result; |
| 4323 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4324 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4325 | |
| 4326 | bool Success(APValue::LValueBase B) { |
| 4327 | Result.set(B); |
| 4328 | return true; |
| 4329 | } |
| 4330 | |
| 4331 | public: |
| 4332 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) : |
| 4333 | ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 4334 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4335 | bool Success(const APValue &V, const Expr *E) { |
| 4336 | Result.setFrom(this->Info.Ctx, V); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4337 | return true; |
| 4338 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4339 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4340 | bool VisitMemberExpr(const MemberExpr *E) { |
| 4341 | // Handle non-static data members. |
| 4342 | QualType BaseTy; |
| 4343 | if (E->isArrow()) { |
| 4344 | if (!EvaluatePointer(E->getBase(), Result, this->Info)) |
| 4345 | return false; |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4346 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4347 | } else if (E->getBase()->isRValue()) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 4348 | assert(E->getBase()->getType()->isRecordType()); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4349 | if (!EvaluateTemporary(E->getBase(), Result, this->Info)) |
| 4350 | return false; |
| 4351 | BaseTy = E->getBase()->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4352 | } else { |
| 4353 | if (!this->Visit(E->getBase())) |
| 4354 | return false; |
| 4355 | BaseTy = E->getBase()->getType(); |
| 4356 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4357 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4358 | const ValueDecl *MD = E->getMemberDecl(); |
| 4359 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 4360 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 4361 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 4362 | (void)BaseTy; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4363 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
| 4364 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4365 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4366 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
| 4367 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4368 | } else |
| 4369 | return this->Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4370 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4371 | if (MD->getType()->isReferenceType()) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4372 | APValue RefValue; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4373 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4374 | RefValue)) |
| 4375 | return false; |
| 4376 | return Success(RefValue, E); |
| 4377 | } |
| 4378 | return true; |
| 4379 | } |
| 4380 | |
| 4381 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 4382 | switch (E->getOpcode()) { |
| 4383 | default: |
| 4384 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 4385 | |
| 4386 | case BO_PtrMemD: |
| 4387 | case BO_PtrMemI: |
| 4388 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | bool VisitCastExpr(const CastExpr *E) { |
| 4393 | switch (E->getCastKind()) { |
| 4394 | default: |
| 4395 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 4396 | |
| 4397 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4398 | case CK_UncheckedDerivedToBase: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4399 | if (!this->Visit(E->getSubExpr())) |
| 4400 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4401 | |
| 4402 | // Now figure out the necessary offset to add to the base LV to get from |
| 4403 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4404 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
| 4405 | Result); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4406 | } |
| 4407 | } |
| 4408 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4409 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4410 | |
| 4411 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4412 | // LValue Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4413 | // |
| 4414 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 4415 | // function designators (in C), decl references to void objects (in C), and |
| 4416 | // temporaries (if building with -Wno-address-of-temporary). |
| 4417 | // |
| 4418 | // LValue evaluation produces values comprising a base expression of one of the |
| 4419 | // following types: |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4420 | // - Declarations |
| 4421 | // * VarDecl |
| 4422 | // * FunctionDecl |
| 4423 | // - Literals |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4424 | // * CompoundLiteralExpr in C |
| 4425 | // * StringLiteral |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4426 | // * CXXTypeidExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4427 | // * PredefinedExpr |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4428 | // * ObjCStringLiteralExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4429 | // * ObjCEncodeExpr |
| 4430 | // * AddrLabelExpr |
| 4431 | // * BlockExpr |
| 4432 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4433 | // - Locals and temporaries |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4434 | // * MaterializeTemporaryExpr |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4435 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4436 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
| 4437 | // from the AST (FIXME). |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4438 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
| 4439 | // CallIndex, for a lifetime-extended temporary. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4440 | // plus an offset in bytes. |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4441 | //===----------------------------------------------------------------------===// |
| 4442 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4443 | class LValueExprEvaluator |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4444 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4445 | public: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4446 | LValueExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 4447 | LValueExprEvaluatorBaseTy(Info, Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4448 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4449 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4450 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4451 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4452 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 4453 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4454 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4455 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 4456 | bool VisitMemberExpr(const MemberExpr *E); |
| 4457 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 4458 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4459 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4460 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4461 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 4462 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 4463 | bool VisitUnaryReal(const UnaryOperator *E); |
| 4464 | bool VisitUnaryImag(const UnaryOperator *E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4465 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
| 4466 | return VisitUnaryPreIncDec(UO); |
| 4467 | } |
| 4468 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
| 4469 | return VisitUnaryPreIncDec(UO); |
| 4470 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4471 | bool VisitBinAssign(const BinaryOperator *BO); |
| 4472 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4473 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4474 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4475 | switch (E->getCastKind()) { |
| 4476 | default: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4477 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4478 | |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 4479 | case CK_LValueBitCast: |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4480 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4481 | if (!Visit(E->getSubExpr())) |
| 4482 | return false; |
| 4483 | Result.Designator.setInvalid(); |
| 4484 | return true; |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 4485 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4486 | case CK_BaseToDerived: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4487 | if (!Visit(E->getSubExpr())) |
| 4488 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4489 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 4490 | } |
| 4491 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4492 | }; |
| 4493 | } // end anonymous namespace |
| 4494 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4495 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 4496 | /// expressions which are not glvalues, in two cases: |
| 4497 | /// * function designators in C, and |
| 4498 | /// * "extern void" objects |
| 4499 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info) { |
| 4500 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
| 4501 | E->getType()->isVoidType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4502 | return LValueExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4503 | } |
| 4504 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4505 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 4506 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4507 | return Success(FD); |
| 4508 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4509 | return VisitVarDecl(E, VD); |
| 4510 | return Error(E); |
| 4511 | } |
Richard Smith | 733237d | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 4512 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4513 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4514 | CallStackFrame *Frame = nullptr; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4515 | if (VD->hasLocalStorage() && Info.CurrentCall->Index > 1) |
| 4516 | Frame = Info.CurrentCall; |
| 4517 | |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4518 | if (!VD->getType()->isReferenceType()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4519 | if (Frame) { |
| 4520 | Result.set(VD, Frame->Index); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4521 | return true; |
| 4522 | } |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4523 | return Success(VD); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 4524 | } |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 4525 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4526 | APValue *V; |
| 4527 | if (!evaluateVarDeclInit(Info, E, VD, Frame, V)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4528 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4529 | if (V->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4530 | if (!Info.checkingPotentialConstantExpression()) |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4531 | Info.Diag(E, diag::note_constexpr_use_uninit_reference); |
| 4532 | return false; |
| 4533 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4534 | return Success(*V, E); |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4537 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 4538 | const MaterializeTemporaryExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4539 | // Walk through the expression to find the materialized temporary itself. |
| 4540 | SmallVector<const Expr *, 2> CommaLHSs; |
| 4541 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 4542 | const Expr *Inner = E->GetTemporaryExpr()-> |
| 4543 | skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4544 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4545 | // If we passed any comma operators, evaluate their LHSs. |
| 4546 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
| 4547 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
| 4548 | return false; |
| 4549 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4550 | // A materialized temporary with static storage duration can appear within the |
| 4551 | // result of a constant expression evaluation, so we need to preserve its |
| 4552 | // value for use outside this evaluation. |
| 4553 | APValue *Value; |
| 4554 | if (E->getStorageDuration() == SD_Static) { |
| 4555 | Value = Info.Ctx.getMaterializedTemporaryValue(E, true); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 4556 | *Value = APValue(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4557 | Result.set(E); |
| 4558 | } else { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4559 | Value = &Info.CurrentCall-> |
| 4560 | createTemporary(E, E->getStorageDuration() == SD_Automatic); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 4561 | Result.set(E, Info.CurrentCall->Index); |
| 4562 | } |
| 4563 | |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4564 | QualType Type = Inner->getType(); |
| 4565 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4566 | // Materialize the temporary itself. |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4567 | if (!EvaluateInPlace(*Value, Info, Result, Inner) || |
| 4568 | (E->getStorageDuration() == SD_Static && |
| 4569 | !CheckConstantExpression(Info, E->getExprLoc(), Type, *Value))) { |
| 4570 | *Value = APValue(); |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4571 | return false; |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 4572 | } |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4573 | |
| 4574 | // Adjust our lvalue to refer to the desired subobject. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4575 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
| 4576 | --I; |
| 4577 | switch (Adjustments[I].Kind) { |
| 4578 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
| 4579 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
| 4580 | Type, Result)) |
| 4581 | return false; |
| 4582 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
| 4583 | break; |
| 4584 | |
| 4585 | case SubobjectAdjustment::FieldAdjustment: |
| 4586 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
| 4587 | return false; |
| 4588 | Type = Adjustments[I].Field->getType(); |
| 4589 | break; |
| 4590 | |
| 4591 | case SubobjectAdjustment::MemberPointerAdjustment: |
| 4592 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
| 4593 | Adjustments[I].Ptr.RHS)) |
| 4594 | return false; |
| 4595 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
| 4596 | break; |
| 4597 | } |
| 4598 | } |
| 4599 | |
| 4600 | return true; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4603 | bool |
| 4604 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4605 | assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?"); |
| 4606 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 4607 | // only see this when folding in C, so there's no standard to follow here. |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4608 | return Success(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4611 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 4612 | if (!E->isPotentiallyEvaluated()) |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4613 | return Success(E); |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 4614 | |
| 4615 | Info.Diag(E, diag::note_constexpr_typeid_polymorphic) |
| 4616 | << E->getExprOperand()->getType() |
| 4617 | << E->getExprOperand()->getSourceRange(); |
| 4618 | return false; |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 4619 | } |
| 4620 | |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4621 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 4622 | return Success(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4623 | } |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 4624 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4625 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4626 | // Handle static data members. |
| 4627 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| 4628 | VisitIgnoredValue(E->getBase()); |
| 4629 | return VisitVarDecl(E, VD); |
| 4630 | } |
| 4631 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4632 | // Handle static member functions. |
| 4633 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 4634 | if (MD->isStatic()) { |
| 4635 | VisitIgnoredValue(E->getBase()); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4636 | return Success(MD); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4637 | } |
| 4638 | } |
| 4639 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4640 | // Handle non-static data members. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4641 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4644 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4645 | // FIXME: Deal with vectors as array subscript bases. |
| 4646 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4647 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4648 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4649 | if (!EvaluatePointer(E->getBase(), Result, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4650 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4651 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4652 | APSInt Index; |
| 4653 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4654 | return false; |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4655 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 4656 | return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), |
| 4657 | getExtValue(Index)); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 4658 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4659 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4660 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4661 | return EvaluatePointer(E->getSubExpr(), Result, Info); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 4664 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 4665 | if (!Visit(E->getSubExpr())) |
| 4666 | return false; |
| 4667 | // __real is a no-op on scalar lvalues. |
| 4668 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 4669 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 4670 | return true; |
| 4671 | } |
| 4672 | |
| 4673 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 4674 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 4675 | "lvalue __imag__ on scalar?"); |
| 4676 | if (!Visit(E->getSubExpr())) |
| 4677 | return false; |
| 4678 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 4679 | return true; |
| 4680 | } |
| 4681 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4682 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 4683 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4684 | return Error(UO); |
| 4685 | |
| 4686 | if (!this->Visit(UO->getSubExpr())) |
| 4687 | return false; |
| 4688 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4689 | return handleIncDec( |
| 4690 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4691 | UO->isIncrementOp(), nullptr); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
| 4695 | const CompoundAssignOperator *CAO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 4696 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4697 | return Error(CAO); |
| 4698 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4699 | APValue RHS; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4700 | |
| 4701 | // The overall lvalue result is the result of evaluating the LHS. |
| 4702 | if (!this->Visit(CAO->getLHS())) { |
| 4703 | if (Info.keepEvaluatingAfterFailure()) |
| 4704 | Evaluate(RHS, this->Info, CAO->getRHS()); |
| 4705 | return false; |
| 4706 | } |
| 4707 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4708 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) |
| 4709 | return false; |
| 4710 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 4711 | return handleCompoundAssignment( |
| 4712 | this->Info, CAO, |
| 4713 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
| 4714 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 4718 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4719 | return Error(E); |
| 4720 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4721 | APValue NewVal; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4722 | |
| 4723 | if (!this->Visit(E->getLHS())) { |
| 4724 | if (Info.keepEvaluatingAfterFailure()) |
| 4725 | Evaluate(NewVal, this->Info, E->getRHS()); |
| 4726 | return false; |
| 4727 | } |
| 4728 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4729 | if (!Evaluate(NewVal, this->Info, E->getRHS())) |
| 4730 | return false; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4731 | |
| 4732 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4733 | NewVal); |
| 4734 | } |
| 4735 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4736 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4737 | // Pointer Evaluation |
| 4738 | //===----------------------------------------------------------------------===// |
| 4739 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 4740 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 4741 | class PointerExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4742 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4743 | LValue &Result; |
| 4744 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4745 | bool Success(const Expr *E) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4746 | Result.set(E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4747 | return true; |
| 4748 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 4749 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4750 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4751 | PointerExprEvaluator(EvalInfo &info, LValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4752 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4753 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4754 | bool Success(const APValue &V, const Expr *E) { |
| 4755 | Result.setFrom(Info.Ctx, V); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4756 | return true; |
| 4757 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4758 | bool ZeroInitialization(const Expr *E) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4759 | return Success((Expr*)nullptr); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4760 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 4761 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4762 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4763 | bool VisitCastExpr(const CastExpr* E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4764 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4765 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4766 | { return Success(E); } |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 4767 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 4768 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4769 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4770 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4771 | bool VisitCallExpr(const CallExpr *E); |
| 4772 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4773 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4774 | return Success(E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4775 | return Error(E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 4776 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4777 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4778 | // Can't look at 'this' when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4779 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4780 | return false; |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 4781 | if (!Info.CurrentCall->This) { |
| 4782 | if (Info.getLangOpts().CPlusPlus11) |
| 4783 | Info.Diag(E, diag::note_constexpr_this) << E->isImplicit(); |
| 4784 | else |
| 4785 | Info.Diag(E); |
| 4786 | return false; |
| 4787 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4788 | Result = *Info.CurrentCall->This; |
| 4789 | return true; |
| 4790 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4791 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 4792 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4793 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4794 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 4795 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4796 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4797 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4798 | return PointerExprEvaluator(Info, Result).Visit(E); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4799 | } |
| 4800 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4801 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4802 | if (E->getOpcode() != BO_Add && |
| 4803 | E->getOpcode() != BO_Sub) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4804 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4805 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4806 | const Expr *PExp = E->getLHS(); |
| 4807 | const Expr *IExp = E->getRHS(); |
| 4808 | if (IExp->getType()->isPointerType()) |
| 4809 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4810 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4811 | bool EvalPtrOK = EvaluatePointer(PExp, Result, Info); |
| 4812 | if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4813 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4814 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4815 | llvm::APSInt Offset; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4816 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4817 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 4818 | |
| 4819 | int64_t AdditionalOffset = getExtValue(Offset); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4820 | if (E->getOpcode() == BO_Sub) |
| 4821 | AdditionalOffset = -AdditionalOffset; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4822 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4823 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4824 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, |
| 4825 | AdditionalOffset); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4826 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4827 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4828 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 4829 | return EvaluateLValue(E->getSubExpr(), Result, Info); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4830 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4832 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 4833 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4834 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4835 | switch (E->getCastKind()) { |
| 4836 | default: |
| 4837 | break; |
| 4838 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4839 | case CK_BitCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4840 | case CK_CPointerToObjCPointerCast: |
| 4841 | case CK_BlockPointerToObjCPointerCast: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4842 | case CK_AnyPointerToBlockPointerCast: |
Anastasia Stulova | 5d8ad8a | 2014-11-26 15:36:41 +0000 | [diff] [blame] | 4843 | case CK_AddressSpaceConversion: |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 4844 | if (!Visit(SubExpr)) |
| 4845 | return false; |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4846 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 4847 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 4848 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 4849 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 4850 | Result.Designator.setInvalid(); |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 4851 | if (SubExpr->getType()->isVoidPointerType()) |
| 4852 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 4853 | << 3 << SubExpr->getType(); |
| 4854 | else |
| 4855 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 4856 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4857 | return true; |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4858 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4859 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4860 | case CK_UncheckedDerivedToBase: |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4861 | if (!EvaluatePointer(E->getSubExpr(), Result, Info)) |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4862 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4863 | if (!Result.Base && Result.Offset.isZero()) |
| 4864 | return true; |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4865 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4866 | // Now figure out the necessary offset to add to the base LV to get from |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4867 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4868 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
| 4869 | castAs<PointerType>()->getPointeeType(), |
| 4870 | Result); |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 4871 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4872 | case CK_BaseToDerived: |
| 4873 | if (!Visit(E->getSubExpr())) |
| 4874 | return false; |
| 4875 | if (!Result.Base && Result.Offset.isZero()) |
| 4876 | return true; |
| 4877 | return HandleBaseToDerivedCast(Info, E, Result); |
| 4878 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4879 | case CK_NullToPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 4880 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4881 | return ZeroInitialization(E); |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4882 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4883 | case CK_IntegralToPointer: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4884 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 4885 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4886 | APValue Value; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4887 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 4888 | break; |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 4889 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4890 | if (Value.isInt()) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4891 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 4892 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4893 | Result.Base = (Expr*)nullptr; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 4894 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4895 | Result.CallIndex = 0; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4896 | Result.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4897 | return true; |
| 4898 | } else { |
| 4899 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4900 | Result.setFrom(Info.Ctx, Value); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4901 | return true; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4902 | } |
| 4903 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4904 | case CK_ArrayToPointerDecay: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4905 | if (SubExpr->isGLValue()) { |
| 4906 | if (!EvaluateLValue(SubExpr, Result, Info)) |
| 4907 | return false; |
| 4908 | } else { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4909 | Result.set(SubExpr, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4910 | if (!EvaluateInPlace(Info.CurrentCall->createTemporary(SubExpr, false), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 4911 | Info, Result, SubExpr)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4912 | return false; |
| 4913 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4914 | // The result is a pointer to the first element of the array. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4915 | if (const ConstantArrayType *CAT |
| 4916 | = Info.Ctx.getAsConstantArrayType(SubExpr->getType())) |
| 4917 | Result.addArray(Info, E, CAT); |
| 4918 | else |
| 4919 | Result.Designator.setInvalid(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 4920 | return true; |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 4921 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4922 | case CK_FunctionToPointerDecay: |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 4923 | return EvaluateLValue(SubExpr, Result, Info); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4926 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4927 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 4928 | |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 4929 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T) { |
| 4930 | // C++ [expr.alignof]p3: |
| 4931 | // When alignof is applied to a reference type, the result is the |
| 4932 | // alignment of the referenced type. |
| 4933 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 4934 | T = Ref->getPointeeType(); |
| 4935 | |
| 4936 | // __alignof is defined to return the preferred alignment. |
| 4937 | return Info.Ctx.toCharUnitsFromBits( |
| 4938 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
| 4939 | } |
| 4940 | |
| 4941 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E) { |
| 4942 | E = E->IgnoreParens(); |
| 4943 | |
| 4944 | // The kinds of expressions that we have special-case logic here for |
| 4945 | // should be kept up to date with the special checks for those |
| 4946 | // expressions in Sema. |
| 4947 | |
| 4948 | // alignof decl is always accepted, even if it doesn't make sense: we default |
| 4949 | // to 1 in those cases. |
| 4950 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 4951 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 4952 | /*RefAsPointee*/true); |
| 4953 | |
| 4954 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 4955 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 4956 | /*RefAsPointee*/true); |
| 4957 | |
| 4958 | return GetAlignOfType(Info, E->getType()); |
| 4959 | } |
| 4960 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4961 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4962 | if (IsStringLiteralCall(E)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 4963 | return Success(E); |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 4964 | |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 4965 | switch (E->getBuiltinCallee()) { |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 4966 | case Builtin::BI__builtin_addressof: |
| 4967 | return EvaluateLValue(E->getArg(0), Result, Info); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 4968 | case Builtin::BI__builtin_assume_aligned: { |
| 4969 | // We need to be very careful here because: if the pointer does not have the |
| 4970 | // asserted alignment, then the behavior is undefined, and undefined |
| 4971 | // behavior is non-constant. |
| 4972 | if (!EvaluatePointer(E->getArg(0), Result, Info)) |
| 4973 | return false; |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 4974 | |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 4975 | LValue OffsetResult(Result); |
| 4976 | APSInt Alignment; |
| 4977 | if (!EvaluateInteger(E->getArg(1), Alignment, Info)) |
| 4978 | return false; |
| 4979 | CharUnits Align = CharUnits::fromQuantity(getExtValue(Alignment)); |
| 4980 | |
| 4981 | if (E->getNumArgs() > 2) { |
| 4982 | APSInt Offset; |
| 4983 | if (!EvaluateInteger(E->getArg(2), Offset, Info)) |
| 4984 | return false; |
| 4985 | |
| 4986 | int64_t AdditionalOffset = -getExtValue(Offset); |
| 4987 | OffsetResult.Offset += CharUnits::fromQuantity(AdditionalOffset); |
| 4988 | } |
| 4989 | |
| 4990 | // If there is a base object, then it must have the correct alignment. |
| 4991 | if (OffsetResult.Base) { |
| 4992 | CharUnits BaseAlignment; |
| 4993 | if (const ValueDecl *VD = |
| 4994 | OffsetResult.Base.dyn_cast<const ValueDecl*>()) { |
| 4995 | BaseAlignment = Info.Ctx.getDeclAlign(VD); |
| 4996 | } else { |
| 4997 | BaseAlignment = |
| 4998 | GetAlignOfExpr(Info, OffsetResult.Base.get<const Expr*>()); |
| 4999 | } |
| 5000 | |
| 5001 | if (BaseAlignment < Align) { |
| 5002 | Result.Designator.setInvalid(); |
| 5003 | // FIXME: Quantities here cast to integers because the plural modifier |
| 5004 | // does not work on APSInts yet. |
| 5005 | CCEDiag(E->getArg(0), |
| 5006 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
| 5007 | << (int) BaseAlignment.getQuantity() |
| 5008 | << (unsigned) getExtValue(Alignment); |
| 5009 | return false; |
| 5010 | } |
| 5011 | } |
| 5012 | |
| 5013 | // The offset must also have the correct alignment. |
| 5014 | if (OffsetResult.Offset.RoundUpToAlignment(Align) != OffsetResult.Offset) { |
| 5015 | Result.Designator.setInvalid(); |
| 5016 | APSInt Offset(64, false); |
| 5017 | Offset = OffsetResult.Offset.getQuantity(); |
| 5018 | |
| 5019 | if (OffsetResult.Base) |
| 5020 | CCEDiag(E->getArg(0), |
| 5021 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
| 5022 | << (int) getExtValue(Offset) << (unsigned) getExtValue(Alignment); |
| 5023 | else |
| 5024 | CCEDiag(E->getArg(0), |
| 5025 | diag::note_constexpr_baa_value_insufficient_alignment) |
| 5026 | << Offset << (unsigned) getExtValue(Alignment); |
| 5027 | |
| 5028 | return false; |
| 5029 | } |
| 5030 | |
| 5031 | return true; |
| 5032 | } |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 5033 | default: |
| 5034 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 5035 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5036 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5037 | |
| 5038 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5039 | // Member Pointer Evaluation |
| 5040 | //===----------------------------------------------------------------------===// |
| 5041 | |
| 5042 | namespace { |
| 5043 | class MemberPointerExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5044 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5045 | MemberPtr &Result; |
| 5046 | |
| 5047 | bool Success(const ValueDecl *D) { |
| 5048 | Result = MemberPtr(D); |
| 5049 | return true; |
| 5050 | } |
| 5051 | public: |
| 5052 | |
| 5053 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 5054 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 5055 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5056 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5057 | Result.setFrom(V); |
| 5058 | return true; |
| 5059 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5060 | bool ZeroInitialization(const Expr *E) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5061 | return Success((const ValueDecl*)nullptr); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | bool VisitCastExpr(const CastExpr *E); |
| 5065 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 5066 | }; |
| 5067 | } // end anonymous namespace |
| 5068 | |
| 5069 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 5070 | EvalInfo &Info) { |
| 5071 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 5072 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 5073 | } |
| 5074 | |
| 5075 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5076 | switch (E->getCastKind()) { |
| 5077 | default: |
| 5078 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5079 | |
| 5080 | case CK_NullToMemberPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 5081 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5082 | return ZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5083 | |
| 5084 | case CK_BaseToDerivedMemberPointer: { |
| 5085 | if (!Visit(E->getSubExpr())) |
| 5086 | return false; |
| 5087 | if (E->path_empty()) |
| 5088 | return true; |
| 5089 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 5090 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 5091 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 5092 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 5093 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 5094 | PathI != PathE; ++PathI) { |
| 5095 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 5096 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 5097 | if (!Result.castToDerived(Derived)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5098 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5099 | } |
| 5100 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 5101 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5102 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5103 | return true; |
| 5104 | } |
| 5105 | |
| 5106 | case CK_DerivedToBaseMemberPointer: |
| 5107 | if (!Visit(E->getSubExpr())) |
| 5108 | return false; |
| 5109 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 5110 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 5111 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 5112 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 5113 | if (!Result.castToBase(Base)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5114 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5115 | } |
| 5116 | return true; |
| 5117 | } |
| 5118 | } |
| 5119 | |
| 5120 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 5121 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 5122 | // member can be formed. |
| 5123 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 5124 | } |
| 5125 | |
| 5126 | //===----------------------------------------------------------------------===// |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5127 | // Record Evaluation |
| 5128 | //===----------------------------------------------------------------------===// |
| 5129 | |
| 5130 | namespace { |
| 5131 | class RecordExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5132 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5133 | const LValue &This; |
| 5134 | APValue &Result; |
| 5135 | public: |
| 5136 | |
| 5137 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 5138 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 5139 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5140 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5141 | Result = V; |
| 5142 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5143 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5144 | bool ZeroInitialization(const Expr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5145 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 5146 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5147 | bool VisitInitListExpr(const InitListExpr *E); |
| 5148 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5149 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5150 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5151 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5152 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5153 | /// Perform zero-initialization on an object of non-union class type. |
| 5154 | /// C++11 [dcl.init]p5: |
| 5155 | /// To zero-initialize an object or reference of type T means: |
| 5156 | /// [...] |
| 5157 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 5158 | /// each non-static data member and each base-class subobject is |
| 5159 | /// zero-initialized |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5160 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 5161 | const RecordDecl *RD, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5162 | const LValue &This, APValue &Result) { |
| 5163 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 5164 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 5165 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 5166 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5167 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5168 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5169 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 5170 | |
| 5171 | if (CD) { |
| 5172 | unsigned Index = 0; |
| 5173 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5174 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5175 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 5176 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5177 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
| 5178 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5179 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5180 | Result.getStructBase(Index))) |
| 5181 | return false; |
| 5182 | } |
| 5183 | } |
| 5184 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5185 | for (const auto *I : RD->fields()) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5186 | // -- if T is a reference type, no initialization is performed. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5187 | if (I->getType()->isReferenceType()) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5188 | continue; |
| 5189 | |
| 5190 | LValue Subobject = This; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5191 | if (!HandleLValueMember(Info, E, Subobject, I, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5192 | return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5193 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5194 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5195 | if (!EvaluateInPlace( |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5196 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5197 | return false; |
| 5198 | } |
| 5199 | |
| 5200 | return true; |
| 5201 | } |
| 5202 | |
| 5203 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E) { |
| 5204 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 5205 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5206 | if (RD->isUnion()) { |
| 5207 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 5208 | // object's first non-static named data member is zero-initialized |
| 5209 | RecordDecl::field_iterator I = RD->field_begin(); |
| 5210 | if (I == RD->field_end()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5211 | Result = APValue((const FieldDecl*)nullptr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5212 | return true; |
| 5213 | } |
| 5214 | |
| 5215 | LValue Subobject = This; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5216 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5217 | return false; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5218 | Result = APValue(*I); |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5219 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5220 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 5223 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 5224 | Info.Diag(E, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 5225 | return false; |
| 5226 | } |
| 5227 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5228 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5229 | } |
| 5230 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 5231 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5232 | switch (E->getCastKind()) { |
| 5233 | default: |
| 5234 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5235 | |
| 5236 | case CK_ConstructorConversion: |
| 5237 | return Visit(E->getSubExpr()); |
| 5238 | |
| 5239 | case CK_DerivedToBase: |
| 5240 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5241 | APValue DerivedObject; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5242 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 5243 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5244 | if (!DerivedObject.isStruct()) |
| 5245 | return Error(E->getSubExpr()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 5246 | |
| 5247 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 5248 | APValue *Value = &DerivedObject; |
| 5249 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 5250 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 5251 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 5252 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 5253 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 5254 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 5255 | RD = Base; |
| 5256 | } |
| 5257 | Result = *Value; |
| 5258 | return true; |
| 5259 | } |
| 5260 | } |
| 5261 | } |
| 5262 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5263 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 5264 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 5265 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5266 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 5267 | |
| 5268 | if (RD->isUnion()) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5269 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 5270 | Result = APValue(Field); |
| 5271 | if (!Field) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5272 | return true; |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5273 | |
| 5274 | // If the initializer list for a union does not contain any elements, the |
| 5275 | // first element of the union is value-initialized. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5276 | // FIXME: The element should be initialized from an initializer list. |
| 5277 | // Is this difference ever observable for initializer lists which |
| 5278 | // we don't build? |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5279 | ImplicitValueInitExpr VIE(Field->getType()); |
| 5280 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 5281 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5282 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5283 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
| 5284 | return false; |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5285 | |
| 5286 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 5287 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 5288 | isa<CXXDefaultInitExpr>(InitExpr)); |
| 5289 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5290 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5291 | } |
| 5292 | |
| 5293 | assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) && |
| 5294 | "initializer list for class with base classes"); |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 5295 | Result = APValue(APValue::UninitStruct(), 0, |
| 5296 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5297 | unsigned ElementNo = 0; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5298 | bool Success = true; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5299 | for (const auto *Field : RD->fields()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5300 | // Anonymous bit-fields are not considered members of the class for |
| 5301 | // purposes of aggregate initialization. |
| 5302 | if (Field->isUnnamedBitfield()) |
| 5303 | continue; |
| 5304 | |
| 5305 | LValue Subobject = This; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5306 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5307 | bool HaveInit = ElementNo < E->getNumInits(); |
| 5308 | |
| 5309 | // FIXME: Diagnostics here should point to the end of the initializer |
| 5310 | // list, not the start. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5311 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5312 | Subobject, Field, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5313 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5314 | |
| 5315 | // Perform an implicit value-initialization for members beyond the end of |
| 5316 | // the initializer list. |
| 5317 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5318 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5319 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 5320 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 5321 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 5322 | isa<CXXDefaultInitExpr>(Init)); |
| 5323 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 5324 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 5325 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) || |
| 5326 | (Field->isBitField() && !truncateBitfieldValue(Info, Init, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5327 | FieldVal, Field))) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5328 | if (!Info.keepEvaluatingAfterFailure()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5329 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5330 | Success = false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5331 | } |
| 5332 | } |
| 5333 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5334 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5335 | } |
| 5336 | |
| 5337 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 5338 | const CXXConstructorDecl *FD = E->getConstructor(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 5339 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 5340 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5341 | bool ZeroInit = E->requiresZeroInitialization(); |
| 5342 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5343 | // If we've already performed zero-initialization, we're already done. |
| 5344 | if (!Result.isUninit()) |
| 5345 | return true; |
| 5346 | |
Richard Smith | da3f4fd | 2014-03-05 23:32:50 +0000 | [diff] [blame] | 5347 | // We can get here in two different ways: |
| 5348 | // 1) We're performing value-initialization, and should zero-initialize |
| 5349 | // the object, or |
| 5350 | // 2) We're performing default-initialization of an object with a trivial |
| 5351 | // constexpr default constructor, in which case we should start the |
| 5352 | // lifetimes of all the base subobjects (there can be no data member |
| 5353 | // subobjects in this case) per [basic.life]p1. |
| 5354 | // Either way, ZeroInitialization is appropriate. |
| 5355 | return ZeroInitialization(E); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5358 | const FunctionDecl *Definition = nullptr; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5359 | FD->getBody(Definition); |
| 5360 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5361 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 5362 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5363 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 5364 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5365 | if (E->isElidable() && !ZeroInit) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5366 | if (const MaterializeTemporaryExpr *ME |
| 5367 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 5368 | return Visit(ME->GetTemporaryExpr()); |
| 5369 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5370 | if (ZeroInit && !ZeroInitialization(E)) |
| 5371 | return false; |
| 5372 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 5373 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5374 | return HandleConstructorCall(E->getExprLoc(), This, Args, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5375 | cast<CXXConstructorDecl>(Definition), Info, |
| 5376 | Result); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5379 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
| 5380 | const CXXStdInitializerListExpr *E) { |
| 5381 | const ConstantArrayType *ArrayType = |
| 5382 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
| 5383 | |
| 5384 | LValue Array; |
| 5385 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
| 5386 | return false; |
| 5387 | |
| 5388 | // Get a pointer to the first element of the array. |
| 5389 | Array.addArray(Info, E, ArrayType); |
| 5390 | |
| 5391 | // FIXME: Perform the checks on the field types in SemaInit. |
| 5392 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
| 5393 | RecordDecl::field_iterator Field = Record->field_begin(); |
| 5394 | if (Field == Record->field_end()) |
| 5395 | return Error(E); |
| 5396 | |
| 5397 | // Start pointer. |
| 5398 | if (!Field->getType()->isPointerType() || |
| 5399 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 5400 | ArrayType->getElementType())) |
| 5401 | return Error(E); |
| 5402 | |
| 5403 | // FIXME: What if the initializer_list type has base classes, etc? |
| 5404 | Result = APValue(APValue::UninitStruct(), 0, 2); |
| 5405 | Array.moveInto(Result.getStructField(0)); |
| 5406 | |
| 5407 | if (++Field == Record->field_end()) |
| 5408 | return Error(E); |
| 5409 | |
| 5410 | if (Field->getType()->isPointerType() && |
| 5411 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 5412 | ArrayType->getElementType())) { |
| 5413 | // End pointer. |
| 5414 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
| 5415 | ArrayType->getElementType(), |
| 5416 | ArrayType->getSize().getZExtValue())) |
| 5417 | return false; |
| 5418 | Array.moveInto(Result.getStructField(1)); |
| 5419 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
| 5420 | // Length. |
| 5421 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
| 5422 | else |
| 5423 | return Error(E); |
| 5424 | |
| 5425 | if (++Field != Record->field_end()) |
| 5426 | return Error(E); |
| 5427 | |
| 5428 | return true; |
| 5429 | } |
| 5430 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5431 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 5432 | APValue &Result, EvalInfo &Info) { |
| 5433 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5434 | "can't evaluate expression as a record rvalue"); |
| 5435 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 5436 | } |
| 5437 | |
| 5438 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5439 | // Temporary Evaluation |
| 5440 | // |
| 5441 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 5442 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 5443 | // materialized so that a reference can bind to it. |
| 5444 | //===----------------------------------------------------------------------===// |
| 5445 | namespace { |
| 5446 | class TemporaryExprEvaluator |
| 5447 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 5448 | public: |
| 5449 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| 5450 | LValueExprEvaluatorBaseTy(Info, Result) {} |
| 5451 | |
| 5452 | /// Visit an expression which constructs the value of this temporary. |
| 5453 | bool VisitConstructExpr(const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5454 | Result.set(E, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5455 | return EvaluateInPlace(Info.CurrentCall->createTemporary(E, false), |
| 5456 | Info, Result, E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | bool VisitCastExpr(const CastExpr *E) { |
| 5460 | switch (E->getCastKind()) { |
| 5461 | default: |
| 5462 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5463 | |
| 5464 | case CK_ConstructorConversion: |
| 5465 | return VisitConstructExpr(E->getSubExpr()); |
| 5466 | } |
| 5467 | } |
| 5468 | bool VisitInitListExpr(const InitListExpr *E) { |
| 5469 | return VisitConstructExpr(E); |
| 5470 | } |
| 5471 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 5472 | return VisitConstructExpr(E); |
| 5473 | } |
| 5474 | bool VisitCallExpr(const CallExpr *E) { |
| 5475 | return VisitConstructExpr(E); |
| 5476 | } |
Richard Smith | 513955c | 2014-12-17 19:24:30 +0000 | [diff] [blame] | 5477 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
| 5478 | return VisitConstructExpr(E); |
| 5479 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5480 | }; |
| 5481 | } // end anonymous namespace |
| 5482 | |
| 5483 | /// Evaluate an expression of record type as a temporary. |
| 5484 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 5485 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5486 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 5487 | } |
| 5488 | |
| 5489 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5490 | // Vector Evaluation |
| 5491 | //===----------------------------------------------------------------------===// |
| 5492 | |
| 5493 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5494 | class VectorExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5495 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5496 | APValue &Result; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5497 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5498 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5499 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 5500 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5501 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5502 | bool Success(const ArrayRef<APValue> &V, const Expr *E) { |
| 5503 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 5504 | // FIXME: remove this APValue copy. |
| 5505 | Result = APValue(V.data(), V.size()); |
| 5506 | return true; |
| 5507 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5508 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 5509 | assert(V.isVector()); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5510 | Result = V; |
| 5511 | return true; |
| 5512 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5513 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5514 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5515 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5516 | { return Visit(E->getSubExpr()); } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5517 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5518 | bool VisitInitListExpr(const InitListExpr *E); |
| 5519 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5520 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 5521 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5522 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5523 | }; |
| 5524 | } // end anonymous namespace |
| 5525 | |
| 5526 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5527 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5528 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5531 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 5532 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 5533 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5534 | |
Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 5535 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 5536 | QualType SETy = SE->getType(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5537 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5538 | switch (E->getCastKind()) { |
| 5539 | case CK_VectorSplat: { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5540 | APValue Val = APValue(); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5541 | if (SETy->isIntegerType()) { |
| 5542 | APSInt IntResult; |
| 5543 | if (!EvaluateInteger(SE, IntResult, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5544 | return false; |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5545 | Val = APValue(IntResult); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5546 | } else if (SETy->isRealFloatingType()) { |
| 5547 | APFloat F(0.0); |
| 5548 | if (!EvaluateFloat(SE, F, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5549 | return false; |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5550 | Val = APValue(F); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5551 | } else { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5552 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5553 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 5554 | |
| 5555 | // Splat and create vector APValue. |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5556 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 5557 | return Success(Elts, E); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 5558 | } |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 5559 | case CK_BitCast: { |
| 5560 | // Evaluate the operand into an APInt we can extract from. |
| 5561 | llvm::APInt SValInt; |
| 5562 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 5563 | return false; |
| 5564 | // Extract the elements |
| 5565 | QualType EltTy = VTy->getElementType(); |
| 5566 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 5567 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 5568 | SmallVector<APValue, 4> Elts; |
| 5569 | if (EltTy->isRealFloatingType()) { |
| 5570 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 5571 | unsigned FloatEltSize = EltSize; |
| 5572 | if (&Sem == &APFloat::x87DoubleExtended) |
| 5573 | FloatEltSize = 80; |
| 5574 | for (unsigned i = 0; i < NElts; i++) { |
| 5575 | llvm::APInt Elt; |
| 5576 | if (BigEndian) |
| 5577 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 5578 | else |
| 5579 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
Tim Northover | 178723a | 2013-01-22 09:46:51 +0000 | [diff] [blame] | 5580 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 5581 | } |
| 5582 | } else if (EltTy->isIntegerType()) { |
| 5583 | for (unsigned i = 0; i < NElts; i++) { |
| 5584 | llvm::APInt Elt; |
| 5585 | if (BigEndian) |
| 5586 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 5587 | else |
| 5588 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 5589 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 5590 | } |
| 5591 | } else { |
| 5592 | return Error(E); |
| 5593 | } |
| 5594 | return Success(Elts, E); |
| 5595 | } |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5596 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5597 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 5598 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5599 | } |
| 5600 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5601 | bool |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5602 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5603 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5604 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5605 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5606 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5607 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5608 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5609 | |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5610 | // The number of initializers can be less than the number of |
| 5611 | // vector elements. For OpenCL, this can be due to nested vector |
| 5612 | // initialization. For GCC compatibility, missing trailing elements |
| 5613 | // should be initialized with zeroes. |
| 5614 | unsigned CountInits = 0, CountElts = 0; |
| 5615 | while (CountElts < NumElements) { |
| 5616 | // Handle nested vector initialization. |
| 5617 | if (CountInits < NumInits |
Eli Friedman | 1409e6e | 2013-09-17 04:07:02 +0000 | [diff] [blame] | 5618 | && E->getInit(CountInits)->getType()->isVectorType()) { |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5619 | APValue v; |
| 5620 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 5621 | return Error(E); |
| 5622 | unsigned vlen = v.getVectorLength(); |
| 5623 | for (unsigned j = 0; j < vlen; j++) |
| 5624 | Elements.push_back(v.getVectorElt(j)); |
| 5625 | CountElts += vlen; |
| 5626 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5627 | llvm::APSInt sInt(32); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5628 | if (CountInits < NumInits) { |
| 5629 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 5630 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5631 | } else // trailing integer zero. |
| 5632 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 5633 | Elements.push_back(APValue(sInt)); |
| 5634 | CountElts++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5635 | } else { |
| 5636 | llvm::APFloat f(0.0); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5637 | if (CountInits < NumInits) { |
| 5638 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 5639 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5640 | } else // trailing float zero. |
| 5641 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 5642 | Elements.push_back(APValue(f)); |
| 5643 | CountElts++; |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 5644 | } |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 5645 | CountInits++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5646 | } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5647 | return Success(Elements, E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5648 | } |
| 5649 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5650 | bool |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5651 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5652 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5653 | QualType EltTy = VT->getElementType(); |
| 5654 | APValue ZeroElement; |
| 5655 | if (EltTy->isIntegerType()) |
| 5656 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 5657 | else |
| 5658 | ZeroElement = |
| 5659 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 5660 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5661 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5662 | return Success(Elements, E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 5665 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5666 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5667 | return ZeroInitialization(E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 5668 | } |
| 5669 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5670 | //===----------------------------------------------------------------------===// |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5671 | // Array Evaluation |
| 5672 | //===----------------------------------------------------------------------===// |
| 5673 | |
| 5674 | namespace { |
| 5675 | class ArrayExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5676 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5677 | const LValue &This; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5678 | APValue &Result; |
| 5679 | public: |
| 5680 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5681 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 5682 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5683 | |
| 5684 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 5685 | assert((V.isArray() || V.isLValue()) && |
| 5686 | "expected array or string literal"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5687 | Result = V; |
| 5688 | return true; |
| 5689 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5690 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5691 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5692 | const ConstantArrayType *CAT = |
| 5693 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 5694 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5695 | return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5696 | |
| 5697 | Result = APValue(APValue::UninitArray(), 0, |
| 5698 | CAT->getSize().getZExtValue()); |
| 5699 | if (!Result.hasArrayFiller()) return true; |
| 5700 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5701 | // Zero-initialize all elements. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5702 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5703 | Subobject.addArray(Info, E, CAT); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5704 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5705 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5706 | } |
| 5707 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5708 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5709 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5710 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 5711 | const LValue &Subobject, |
| 5712 | APValue *Value, QualType Type); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5713 | }; |
| 5714 | } // end anonymous namespace |
| 5715 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5716 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 5717 | APValue &Result, EvalInfo &Info) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5718 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5719 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 5723 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 5724 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5725 | return Error(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5726 | |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5727 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 5728 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 5729 | if (E->isStringLiteralInit()) { |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5730 | LValue LV; |
| 5731 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 5732 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5733 | APValue Val; |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 5734 | LV.moveInto(Val); |
| 5735 | return Success(Val, E); |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 5736 | } |
| 5737 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5738 | bool Success = true; |
| 5739 | |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5740 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
| 5741 | "zero-initialized array shouldn't have any initialized elts"); |
| 5742 | APValue Filler; |
| 5743 | if (Result.isArray() && Result.hasArrayFiller()) |
| 5744 | Filler = Result.getArrayFiller(); |
| 5745 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5746 | unsigned NumEltsToInit = E->getNumInits(); |
| 5747 | unsigned NumElts = CAT->getSize().getZExtValue(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5748 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : nullptr; |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5749 | |
| 5750 | // If the initializer might depend on the array index, run it for each |
| 5751 | // array element. For now, just whitelist non-class value-initialization. |
| 5752 | if (NumEltsToInit != NumElts && !isa<ImplicitValueInitExpr>(FillerExpr)) |
| 5753 | NumEltsToInit = NumElts; |
| 5754 | |
| 5755 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5756 | |
| 5757 | // If the array was previously zero-initialized, preserve the |
| 5758 | // zero-initialized values. |
| 5759 | if (!Filler.isUninit()) { |
| 5760 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
| 5761 | Result.getArrayInitializedElt(I) = Filler; |
| 5762 | if (Result.hasArrayFiller()) |
| 5763 | Result.getArrayFiller() = Filler; |
| 5764 | } |
| 5765 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5766 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5767 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5768 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
| 5769 | const Expr *Init = |
| 5770 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5771 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5772 | Info, Subobject, Init) || |
| 5773 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5774 | CAT->getElementType(), 1)) { |
| 5775 | if (!Info.keepEvaluatingAfterFailure()) |
| 5776 | return false; |
| 5777 | Success = false; |
| 5778 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5779 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5780 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5781 | if (!Result.hasArrayFiller()) |
| 5782 | return Success; |
| 5783 | |
| 5784 | // If we get here, we have a trivial filler, which we can just evaluate |
| 5785 | // once and splat over the rest of the array elements. |
| 5786 | assert(FillerExpr && "no array filler for incomplete init list"); |
| 5787 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
| 5788 | FillerExpr) && Success; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5789 | } |
| 5790 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5791 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5792 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
| 5793 | } |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5794 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5795 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 5796 | const LValue &Subobject, |
| 5797 | APValue *Value, |
| 5798 | QualType Type) { |
| 5799 | bool HadZeroInit = !Value->isUninit(); |
| 5800 | |
| 5801 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
| 5802 | unsigned N = CAT->getSize().getZExtValue(); |
| 5803 | |
| 5804 | // Preserve the array filler if we had prior zero-initialization. |
| 5805 | APValue Filler = |
| 5806 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
| 5807 | : APValue(); |
| 5808 | |
| 5809 | *Value = APValue(APValue::UninitArray(), N, N); |
| 5810 | |
| 5811 | if (HadZeroInit) |
| 5812 | for (unsigned I = 0; I != N; ++I) |
| 5813 | Value->getArrayInitializedElt(I) = Filler; |
| 5814 | |
| 5815 | // Initialize the elements. |
| 5816 | LValue ArrayElt = Subobject; |
| 5817 | ArrayElt.addArray(Info, E, CAT); |
| 5818 | for (unsigned I = 0; I != N; ++I) |
| 5819 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
| 5820 | CAT->getElementType()) || |
| 5821 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
| 5822 | CAT->getElementType(), 1)) |
| 5823 | return false; |
| 5824 | |
| 5825 | return true; |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5826 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5827 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5828 | if (!Type->isRecordType()) |
Richard Smith | 9fce7bc | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 5829 | return Error(E); |
| 5830 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5831 | const CXXConstructorDecl *FD = E->getConstructor(); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5832 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5833 | bool ZeroInit = E->requiresZeroInitialization(); |
| 5834 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5835 | if (HadZeroInit) |
| 5836 | return true; |
| 5837 | |
Richard Smith | da3f4fd | 2014-03-05 23:32:50 +0000 | [diff] [blame] | 5838 | // See RecordExprEvaluator::VisitCXXConstructExpr for explanation. |
| 5839 | ImplicitValueInitExpr VIE(Type); |
| 5840 | return EvaluateInPlace(*Value, Info, Subobject, &VIE); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 5841 | } |
| 5842 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5843 | const FunctionDecl *Definition = nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5844 | FD->getBody(Definition); |
| 5845 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5846 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition)) |
| 5847 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5848 | |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 5849 | if (ZeroInit && !HadZeroInit) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 5850 | ImplicitValueInitExpr VIE(Type); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5851 | if (!EvaluateInPlace(*Value, Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5852 | return false; |
| 5853 | } |
| 5854 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 5855 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5856 | return HandleConstructorCall(E->getExprLoc(), Subobject, Args, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5857 | cast<CXXConstructorDecl>(Definition), |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 5858 | Info, *Value); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5859 | } |
| 5860 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 5861 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5862 | // Integer Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5863 | // |
| 5864 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 5865 | // types and back in constant folding. Integer values are thus represented |
| 5866 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5867 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5868 | |
| 5869 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5870 | class IntExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5871 | : public ExprEvaluatorBase<IntExprEvaluator> { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5872 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 5873 | public: |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5874 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5875 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5876 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5877 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5878 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5879 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5880 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5881 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 5882 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5883 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5884 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5885 | return true; |
| 5886 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5887 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 5888 | return Success(SI, E, Result); |
| 5889 | } |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5890 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5891 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5892 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 5893 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 5894 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 5895 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5896 | Result = APValue(APSInt(I)); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 5897 | Result.getInt().setIsUnsigned( |
| 5898 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5899 | return true; |
| 5900 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5901 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 5902 | return Success(I, E, Result); |
| 5903 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5904 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5905 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5906 | assert(E->getType()->isIntegralOrEnumerationType() && |
| 5907 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5908 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5909 | return true; |
| 5910 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 5911 | bool Success(uint64_t Value, const Expr *E) { |
| 5912 | return Success(Value, E, Result); |
| 5913 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5914 | |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 5915 | bool Success(CharUnits Size, const Expr *E) { |
| 5916 | return Success(Size.getQuantity(), E); |
| 5917 | } |
| 5918 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5919 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 5920 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 9c8d1c5 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 5921 | Result = V; |
| 5922 | return true; |
| 5923 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5924 | return Success(V.getInt(), E); |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 5925 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5926 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5927 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5928 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5929 | //===--------------------------------------------------------------------===// |
| 5930 | // Visitor Methods |
| 5931 | //===--------------------------------------------------------------------===// |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 5932 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5933 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5934 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5935 | } |
| 5936 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5937 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 5938 | } |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5939 | |
| 5940 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 5941 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5942 | if (CheckReferencedDecl(E, E->getDecl())) |
| 5943 | return true; |
| 5944 | |
| 5945 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5946 | } |
| 5947 | bool VisitMemberExpr(const MemberExpr *E) { |
| 5948 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5949 | VisitIgnoredValue(E->getBase()); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5950 | return true; |
| 5951 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5952 | |
| 5953 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 5954 | } |
| 5955 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5956 | bool VisitCallExpr(const CallExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5957 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 5958 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 5959 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 5960 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5961 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5962 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 5963 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5964 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 5965 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5966 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5967 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 5968 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 5969 | return Success(E->getValue(), E); |
| 5970 | } |
| 5971 | |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5972 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 5973 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5974 | return ZeroInitialization(E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5975 | } |
| 5976 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 5977 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 5978 | return Success(E->getValue(), E); |
| 5979 | } |
| 5980 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 5981 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 5982 | return Success(E->getValue(), E); |
| 5983 | } |
| 5984 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 5985 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 5986 | return Success(E->getValue(), E); |
| 5987 | } |
| 5988 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 5989 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5990 | bool VisitUnaryImag(const UnaryOperator *E); |
| 5991 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 5992 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 5993 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 5994 | |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 5995 | private: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 5996 | bool TryEvaluateBuiltinObjectSize(const CallExpr *E, unsigned Type); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 5997 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 5998 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5999 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 6000 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6001 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 6002 | /// produce either the integer value or a pointer. |
| 6003 | /// |
| 6004 | /// GCC has a heinous extension which folds casts between pointer types and |
| 6005 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 6006 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 6007 | /// Some simple arithmetic on such values is supported (they are treated much |
| 6008 | /// like char*). |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6009 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 6010 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6011 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6012 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 6013 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 6014 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6015 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6016 | APValue Val; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6017 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 6018 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6019 | if (!Val.isInt()) { |
| 6020 | // FIXME: It would be better to produce the diagnostic for casting |
| 6021 | // a pointer to an integer. |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6022 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6023 | return false; |
| 6024 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 6025 | Result = Val.getInt(); |
| 6026 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 6027 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 6028 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6029 | /// Check whether the given declaration can be directly converted to an integral |
| 6030 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 6031 | /// try. |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 6032 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 6033 | // Enums are integer constant exprs. |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 6034 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 6035 | // Check for signedness/width mismatches between E type and ECD value. |
| 6036 | bool SameSign = (ECD->getInitVal().isSigned() |
| 6037 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 6038 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 6039 | == Info.Ctx.getIntWidth(E->getType())); |
| 6040 | if (SameSign && SameWidth) |
| 6041 | return Success(ECD->getInitVal(), E); |
| 6042 | else { |
| 6043 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 6044 | // by computing a new value matching the type of E. |
| 6045 | llvm::APSInt Val = ECD->getInitVal(); |
| 6046 | if (!SameSign) |
| 6047 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 6048 | if (!SameWidth) |
| 6049 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 6050 | return Success(Val, E); |
| 6051 | } |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 6052 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6053 | return false; |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6056 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 6057 | /// as GCC. |
| 6058 | static int EvaluateBuiltinClassifyType(const CallExpr *E) { |
| 6059 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6060 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6061 | enum gcc_type_class { |
| 6062 | no_type_class = -1, |
| 6063 | void_type_class, integer_type_class, char_type_class, |
| 6064 | enumeral_type_class, boolean_type_class, |
| 6065 | pointer_type_class, reference_type_class, offset_type_class, |
| 6066 | real_type_class, complex_type_class, |
| 6067 | function_type_class, method_type_class, |
| 6068 | record_type_class, union_type_class, |
| 6069 | array_type_class, string_type_class, |
| 6070 | lang_type_class |
| 6071 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6072 | |
| 6073 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6074 | // ideal, however it is what gcc does. |
| 6075 | if (E->getNumArgs() == 0) |
| 6076 | return no_type_class; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6077 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6078 | QualType ArgTy = E->getArg(0)->getType(); |
| 6079 | if (ArgTy->isVoidType()) |
| 6080 | return void_type_class; |
| 6081 | else if (ArgTy->isEnumeralType()) |
| 6082 | return enumeral_type_class; |
| 6083 | else if (ArgTy->isBooleanType()) |
| 6084 | return boolean_type_class; |
| 6085 | else if (ArgTy->isCharType()) |
| 6086 | return string_type_class; // gcc doesn't appear to use char_type_class |
| 6087 | else if (ArgTy->isIntegerType()) |
| 6088 | return integer_type_class; |
| 6089 | else if (ArgTy->isPointerType()) |
| 6090 | return pointer_type_class; |
| 6091 | else if (ArgTy->isReferenceType()) |
| 6092 | return reference_type_class; |
| 6093 | else if (ArgTy->isRealType()) |
| 6094 | return real_type_class; |
| 6095 | else if (ArgTy->isComplexType()) |
| 6096 | return complex_type_class; |
| 6097 | else if (ArgTy->isFunctionType()) |
| 6098 | return function_type_class; |
Douglas Gregor | 8385a06 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 6099 | else if (ArgTy->isStructureOrClassType()) |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6100 | return record_type_class; |
| 6101 | else if (ArgTy->isUnionType()) |
| 6102 | return union_type_class; |
| 6103 | else if (ArgTy->isArrayType()) |
| 6104 | return array_type_class; |
| 6105 | else if (ArgTy->isUnionType()) |
| 6106 | return union_type_class; |
| 6107 | else // FIXME: offset_type_class, method_type_class, & lang_type_class? |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6108 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6111 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 6112 | /// __builtin_constant_p when applied to the given lvalue. |
| 6113 | /// |
| 6114 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 6115 | /// character of a string literal. |
| 6116 | template<typename LValue> |
| 6117 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
Douglas Gregor | f31cee6 | 2012-03-11 02:23:56 +0000 | [diff] [blame] | 6118 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6119 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 6120 | } |
| 6121 | |
| 6122 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 6123 | /// GCC as we can manage. |
| 6124 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 6125 | QualType ArgType = Arg->getType(); |
| 6126 | |
| 6127 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 6128 | // are not precisely documented, but are as follows: |
| 6129 | // |
| 6130 | // - If the operand is of integral, floating, complex or enumeration type, |
| 6131 | // and can be folded to a known value of that type, it returns 1. |
| 6132 | // - If the operand and can be folded to a pointer to the first character |
| 6133 | // of a string literal (or such a pointer cast to an integral type), it |
| 6134 | // returns 1. |
| 6135 | // |
| 6136 | // Otherwise, it returns 0. |
| 6137 | // |
| 6138 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 6139 | // its support for this does not currently work. |
| 6140 | if (ArgType->isIntegralOrEnumerationType()) { |
| 6141 | Expr::EvalResult Result; |
| 6142 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 6143 | return false; |
| 6144 | |
| 6145 | APValue &V = Result.Val; |
| 6146 | if (V.getKind() == APValue::Int) |
| 6147 | return true; |
| 6148 | |
| 6149 | return EvaluateBuiltinConstantPForLValue(V); |
| 6150 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 6151 | return Arg->isEvaluatable(Ctx); |
| 6152 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 6153 | LValue LV; |
| 6154 | Expr::EvalStatus Status; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 6155 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 6156 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 6157 | : EvaluatePointer(Arg, LV, Info)) && |
| 6158 | !Status.HasSideEffects) |
| 6159 | return EvaluateBuiltinConstantPForLValue(LV); |
| 6160 | } |
| 6161 | |
| 6162 | // Anything else isn't considered to be sufficiently constant. |
| 6163 | return false; |
| 6164 | } |
| 6165 | |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6166 | /// Retrieves the "underlying object type" of the given expression, |
| 6167 | /// as used by __builtin_object_size. |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6168 | static QualType getObjectType(APValue::LValueBase B) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 6169 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 6170 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6171 | return VD->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 6172 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 6173 | if (isa<CompoundLiteralExpr>(E)) |
| 6174 | return E->getType(); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | return QualType(); |
| 6178 | } |
| 6179 | |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6180 | bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E, |
| 6181 | unsigned Type) { |
| 6182 | // Determine the denoted object. |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6183 | LValue Base; |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 6184 | { |
| 6185 | // The operand of __builtin_object_size is never evaluated for side-effects. |
| 6186 | // If there are any, but we can determine the pointed-to object anyway, then |
| 6187 | // ignore the side-effects. |
| 6188 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6189 | FoldConstant Fold(Info, true); |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 6190 | if (!EvaluatePointer(E->getArg(0), Base, Info)) |
| 6191 | return false; |
| 6192 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6193 | |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6194 | CharUnits BaseOffset = Base.getLValueOffset(); |
| 6195 | |
| 6196 | // If we point to before the start of the object, there are no |
| 6197 | // accessible bytes. |
| 6198 | if (BaseOffset < CharUnits::Zero()) |
| 6199 | return Success(0, E); |
| 6200 | |
| 6201 | // MostDerivedType is null if we're dealing with a literal such as nullptr or |
| 6202 | // (char*)0x100000. Lower it to LLVM in either case so it can figure out what |
| 6203 | // to do with it. |
| 6204 | // FIXME(gbiv): Try to do a better job with this in clang. |
| 6205 | if (Base.Designator.MostDerivedType.isNull()) |
Nico Weber | 19999b4 | 2015-08-18 20:32:55 +0000 | [diff] [blame] | 6206 | return Error(E); |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6207 | |
| 6208 | // If Type & 1 is 0, the object in question is the complete object; reset to |
| 6209 | // a complete object designator in that case. |
| 6210 | // |
| 6211 | // If Type is 1 and we've lost track of the subobject, just find the complete |
| 6212 | // object instead. (If Type is 3, that's not correct behavior and we should |
| 6213 | // return 0 instead.) |
| 6214 | LValue End = Base; |
| 6215 | if (((Type & 1) == 0) || (End.Designator.Invalid && Type == 1)) { |
| 6216 | QualType T = getObjectType(End.getLValueBase()); |
| 6217 | if (T.isNull()) |
| 6218 | End.Designator.setInvalid(); |
| 6219 | else { |
| 6220 | End.Designator = SubobjectDesignator(T); |
| 6221 | End.Offset = CharUnits::Zero(); |
| 6222 | } |
Fariborz Jahanian | a3d8879 | 2014-09-22 17:11:59 +0000 | [diff] [blame] | 6223 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6224 | |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6225 | // FIXME: We should produce a valid object size for an unknown object with a |
| 6226 | // known designator, if Type & 1 is 1. For instance: |
| 6227 | // |
| 6228 | // extern struct X { char buff[32]; int a, b, c; } *p; |
| 6229 | // int a = __builtin_object_size(p->buff + 4, 3); // returns 28 |
| 6230 | // int b = __builtin_object_size(p->buff + 4, 2); // returns 0, not 40 |
| 6231 | // |
| 6232 | // This is GCC's behavior. We currently don't do this, but (hopefully) will in |
| 6233 | // the near future. |
| 6234 | |
| 6235 | // If it is not possible to determine which objects ptr points to at compile |
| 6236 | // time, __builtin_object_size should return (size_t) -1 for type 0 or 1 |
| 6237 | // and (size_t) 0 for type 2 or 3. |
| 6238 | if (End.Designator.Invalid) |
| 6239 | return false; |
| 6240 | |
| 6241 | // According to the GCC documentation, we want the size of the subobject |
| 6242 | // denoted by the pointer. But that's not quite right -- what we actually |
| 6243 | // want is the size of the immediately-enclosing array, if there is one. |
| 6244 | int64_t AmountToAdd = 1; |
| 6245 | if (End.Designator.MostDerivedArraySize && |
| 6246 | End.Designator.Entries.size() == End.Designator.MostDerivedPathLength) { |
| 6247 | // We got a pointer to an array. Step to its end. |
| 6248 | AmountToAdd = End.Designator.MostDerivedArraySize - |
| 6249 | End.Designator.Entries.back().ArrayIndex; |
| 6250 | } else if (End.Designator.IsOnePastTheEnd) { |
| 6251 | // We're already pointing at the end of the object. |
| 6252 | AmountToAdd = 0; |
| 6253 | } |
| 6254 | |
| 6255 | if (End.Designator.MostDerivedType->isIncompleteType() || |
| 6256 | End.Designator.MostDerivedType->isFunctionType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6257 | return Error(E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6258 | |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6259 | if (!HandleLValueArrayAdjustment(Info, E, End, End.Designator.MostDerivedType, |
| 6260 | AmountToAdd)) |
| 6261 | return false; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6262 | |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6263 | auto EndOffset = End.getLValueOffset(); |
| 6264 | if (BaseOffset > EndOffset) |
| 6265 | return Success(0, E); |
| 6266 | |
| 6267 | return Success(EndOffset - BaseOffset, E); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6268 | } |
| 6269 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6270 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 6271 | switch (unsigned BuiltinOp = E->getBuiltinCallee()) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6272 | default: |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6273 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 6274 | |
| 6275 | case Builtin::BI__builtin_object_size: { |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6276 | // The type was checked when we built the expression. |
| 6277 | unsigned Type = |
| 6278 | E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 6279 | assert(Type <= 3 && "unexpected type"); |
| 6280 | |
| 6281 | if (TryEvaluateBuiltinObjectSize(E, Type)) |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 6282 | return true; |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 6283 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 6284 | // If evaluating the argument has side-effects, we can't determine the size |
| 6285 | // of the object, and so we lower it to unknown now. CodeGen relies on us to |
| 6286 | // handle all cases where the expression has side-effects. |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6287 | // Likewise, if Type is 3, we must handle this because CodeGen cannot give a |
| 6288 | // conservatively correct answer in that case. |
| 6289 | if (E->getArg(0)->HasSideEffects(Info.Ctx) || Type == 3) |
| 6290 | return Success((Type & 2) ? 0 : -1, E); |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 6291 | |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 6292 | // Expression had no side effects, but we couldn't statically determine the |
| 6293 | // size of the referenced object. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6294 | switch (Info.EvalMode) { |
| 6295 | case EvalInfo::EM_ConstantExpression: |
| 6296 | case EvalInfo::EM_PotentialConstantExpression: |
| 6297 | case EvalInfo::EM_ConstantFold: |
| 6298 | case EvalInfo::EM_EvaluateForOverflow: |
| 6299 | case EvalInfo::EM_IgnoreSideEffects: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6300 | // Leave it to IR generation. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6301 | return Error(E); |
| 6302 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
| 6303 | case EvalInfo::EM_PotentialConstantExpressionUnevaluated: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame^] | 6304 | // Reduce it to a constant now. |
| 6305 | return Success((Type & 2) ? 0 : -1, E); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 6306 | } |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 6307 | } |
| 6308 | |
Benjamin Kramer | a801f4a | 2012-10-06 14:42:22 +0000 | [diff] [blame] | 6309 | case Builtin::BI__builtin_bswap16: |
Richard Smith | 80ac9ef | 2012-09-28 20:20:52 +0000 | [diff] [blame] | 6310 | case Builtin::BI__builtin_bswap32: |
| 6311 | case Builtin::BI__builtin_bswap64: { |
| 6312 | APSInt Val; |
| 6313 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6314 | return false; |
| 6315 | |
| 6316 | return Success(Val.byteSwap(), E); |
| 6317 | } |
| 6318 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6319 | case Builtin::BI__builtin_classify_type: |
| 6320 | return Success(EvaluateBuiltinClassifyType(E), E); |
| 6321 | |
| 6322 | // FIXME: BI__builtin_clrsb |
| 6323 | // FIXME: BI__builtin_clrsbl |
| 6324 | // FIXME: BI__builtin_clrsbll |
| 6325 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6326 | case Builtin::BI__builtin_clz: |
| 6327 | case Builtin::BI__builtin_clzl: |
Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 6328 | case Builtin::BI__builtin_clzll: |
| 6329 | case Builtin::BI__builtin_clzs: { |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6330 | APSInt Val; |
| 6331 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6332 | return false; |
| 6333 | if (!Val) |
| 6334 | return Error(E); |
| 6335 | |
| 6336 | return Success(Val.countLeadingZeros(), E); |
| 6337 | } |
| 6338 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6339 | case Builtin::BI__builtin_constant_p: |
| 6340 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
| 6341 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6342 | case Builtin::BI__builtin_ctz: |
| 6343 | case Builtin::BI__builtin_ctzl: |
Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 6344 | case Builtin::BI__builtin_ctzll: |
| 6345 | case Builtin::BI__builtin_ctzs: { |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6346 | APSInt Val; |
| 6347 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6348 | return false; |
| 6349 | if (!Val) |
| 6350 | return Error(E); |
| 6351 | |
| 6352 | return Success(Val.countTrailingZeros(), E); |
| 6353 | } |
| 6354 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6355 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 6356 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 6357 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
| 6358 | return Success(Operand, E); |
| 6359 | } |
| 6360 | |
| 6361 | case Builtin::BI__builtin_expect: |
| 6362 | return Visit(E->getArg(0)); |
| 6363 | |
| 6364 | case Builtin::BI__builtin_ffs: |
| 6365 | case Builtin::BI__builtin_ffsl: |
| 6366 | case Builtin::BI__builtin_ffsll: { |
| 6367 | APSInt Val; |
| 6368 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6369 | return false; |
| 6370 | |
| 6371 | unsigned N = Val.countTrailingZeros(); |
| 6372 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
| 6373 | } |
| 6374 | |
| 6375 | case Builtin::BI__builtin_fpclassify: { |
| 6376 | APFloat Val(0.0); |
| 6377 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
| 6378 | return false; |
| 6379 | unsigned Arg; |
| 6380 | switch (Val.getCategory()) { |
| 6381 | case APFloat::fcNaN: Arg = 0; break; |
| 6382 | case APFloat::fcInfinity: Arg = 1; break; |
| 6383 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
| 6384 | case APFloat::fcZero: Arg = 4; break; |
| 6385 | } |
| 6386 | return Visit(E->getArg(Arg)); |
| 6387 | } |
| 6388 | |
| 6389 | case Builtin::BI__builtin_isinf_sign: { |
| 6390 | APFloat Val(0.0); |
Richard Smith | ab341c6 | 2013-06-13 06:31:13 +0000 | [diff] [blame] | 6391 | return EvaluateFloat(E->getArg(0), Val, Info) && |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6392 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
| 6393 | } |
| 6394 | |
Richard Smith | ea3019d | 2013-10-15 19:07:14 +0000 | [diff] [blame] | 6395 | case Builtin::BI__builtin_isinf: { |
| 6396 | APFloat Val(0.0); |
| 6397 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6398 | Success(Val.isInfinity() ? 1 : 0, E); |
| 6399 | } |
| 6400 | |
| 6401 | case Builtin::BI__builtin_isfinite: { |
| 6402 | APFloat Val(0.0); |
| 6403 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6404 | Success(Val.isFinite() ? 1 : 0, E); |
| 6405 | } |
| 6406 | |
| 6407 | case Builtin::BI__builtin_isnan: { |
| 6408 | APFloat Val(0.0); |
| 6409 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6410 | Success(Val.isNaN() ? 1 : 0, E); |
| 6411 | } |
| 6412 | |
| 6413 | case Builtin::BI__builtin_isnormal: { |
| 6414 | APFloat Val(0.0); |
| 6415 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 6416 | Success(Val.isNormal() ? 1 : 0, E); |
| 6417 | } |
| 6418 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 6419 | case Builtin::BI__builtin_parity: |
| 6420 | case Builtin::BI__builtin_parityl: |
| 6421 | case Builtin::BI__builtin_parityll: { |
| 6422 | APSInt Val; |
| 6423 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6424 | return false; |
| 6425 | |
| 6426 | return Success(Val.countPopulation() % 2, E); |
| 6427 | } |
| 6428 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 6429 | case Builtin::BI__builtin_popcount: |
| 6430 | case Builtin::BI__builtin_popcountl: |
| 6431 | case Builtin::BI__builtin_popcountll: { |
| 6432 | APSInt Val; |
| 6433 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 6434 | return false; |
| 6435 | |
| 6436 | return Success(Val.countPopulation(), E); |
| 6437 | } |
| 6438 | |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 6439 | case Builtin::BIstrlen: |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 6440 | // A call to strlen is not a constant expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6441 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6442 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 6443 | << /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'"; |
| 6444 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6445 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 6446 | // Fall through. |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 6447 | case Builtin::BI__builtin_strlen: { |
| 6448 | // As an extension, we support __builtin_strlen() as a constant expression, |
| 6449 | // and support folding strlen() to a constant. |
| 6450 | LValue String; |
| 6451 | if (!EvaluatePointer(E->getArg(0), String, Info)) |
| 6452 | return false; |
| 6453 | |
| 6454 | // Fast path: if it's a string literal, search the string value. |
| 6455 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
| 6456 | String.getLValueBase().dyn_cast<const Expr *>())) { |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 6457 | // The string literal may have embedded null characters. Find the first |
| 6458 | // one and truncate there. |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 6459 | StringRef Str = S->getBytes(); |
| 6460 | int64_t Off = String.Offset.getQuantity(); |
| 6461 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
| 6462 | S->getCharByteWidth() == 1) { |
| 6463 | Str = Str.substr(Off); |
| 6464 | |
| 6465 | StringRef::size_type Pos = Str.find(0); |
| 6466 | if (Pos != StringRef::npos) |
| 6467 | Str = Str.substr(0, Pos); |
| 6468 | |
| 6469 | return Success(Str.size(), E); |
| 6470 | } |
| 6471 | |
| 6472 | // Fall through to slow path to issue appropriate diagnostic. |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 6473 | } |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 6474 | |
| 6475 | // Slow path: scan the bytes of the string looking for the terminating 0. |
| 6476 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
| 6477 | for (uint64_t Strlen = 0; /**/; ++Strlen) { |
| 6478 | APValue Char; |
| 6479 | if (!handleLValueToRValueConversion(Info, E, CharTy, String, Char) || |
| 6480 | !Char.isInt()) |
| 6481 | return false; |
| 6482 | if (!Char.getInt()) |
| 6483 | return Success(Strlen, E); |
| 6484 | if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1)) |
| 6485 | return false; |
| 6486 | } |
| 6487 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6488 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6489 | case Builtin::BI__atomic_always_lock_free: |
Richard Smith | b1e36c6 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 6490 | case Builtin::BI__atomic_is_lock_free: |
| 6491 | case Builtin::BI__c11_atomic_is_lock_free: { |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6492 | APSInt SizeVal; |
| 6493 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 6494 | return false; |
| 6495 | |
| 6496 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 6497 | // of two less than the maximum inline atomic width, we know it is |
| 6498 | // lock-free. If the size isn't a power of two, or greater than the |
| 6499 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 6500 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 6501 | // the answer can only be determined at runtime; for example, 16-byte |
| 6502 | // atomics have lock-free implementations on some, but not all, |
| 6503 | // x86-64 processors. |
| 6504 | |
| 6505 | // Check power-of-two. |
| 6506 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6507 | if (Size.isPowerOfTwo()) { |
| 6508 | // Check against inlining width. |
| 6509 | unsigned InlineWidthBits = |
| 6510 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 6511 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 6512 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 6513 | Size == CharUnits::One() || |
| 6514 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 6515 | Expr::NPC_NeverValueDependent)) |
| 6516 | // OK, we will inline appropriately-aligned operations of this size, |
| 6517 | // and _Atomic(T) is appropriately-aligned. |
| 6518 | return Success(1, E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6519 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6520 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 6521 | castAs<PointerType>()->getPointeeType(); |
| 6522 | if (!PointeeType->isIncompleteType() && |
| 6523 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 6524 | // OK, we will inline operations on this object. |
| 6525 | return Success(1, E); |
| 6526 | } |
| 6527 | } |
| 6528 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6529 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 6530 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 6531 | Success(0, E) : Error(E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 6532 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 6533 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 6534 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 6535 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6536 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 6537 | if (!A.getLValueBase()) |
| 6538 | return !B.getLValueBase(); |
| 6539 | if (!B.getLValueBase()) |
| 6540 | return false; |
| 6541 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 6542 | if (A.getLValueBase().getOpaqueValue() != |
| 6543 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6544 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 6545 | if (!ADecl) |
| 6546 | return false; |
| 6547 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 6548 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6549 | return false; |
| 6550 | } |
| 6551 | |
| 6552 | return IsGlobalLValue(A.getLValueBase()) || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6553 | A.getLValueCallIndex() == B.getLValueCallIndex(); |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6554 | } |
| 6555 | |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 6556 | /// \brief Determine whether this is a pointer past the end of the complete |
| 6557 | /// object referred to by the lvalue. |
| 6558 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
| 6559 | const LValue &LV) { |
| 6560 | // A null pointer can be viewed as being "past the end" but we don't |
| 6561 | // choose to look at it that way here. |
| 6562 | if (!LV.getLValueBase()) |
| 6563 | return false; |
| 6564 | |
| 6565 | // If the designator is valid and refers to a subobject, we're not pointing |
| 6566 | // past the end. |
| 6567 | if (!LV.getLValueDesignator().Invalid && |
| 6568 | !LV.getLValueDesignator().isOnePastTheEnd()) |
| 6569 | return false; |
| 6570 | |
| 6571 | // We're a past-the-end pointer if we point to the byte after the object, |
| 6572 | // no matter what our type or path is. |
| 6573 | auto Size = Ctx.getTypeSizeInChars(getType(LV.getLValueBase())); |
| 6574 | return LV.getLValueOffset() == Size; |
| 6575 | } |
| 6576 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6577 | namespace { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6578 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6579 | /// \brief Data recursive integer evaluator of certain binary operators. |
| 6580 | /// |
| 6581 | /// We use a data recursive algorithm for binary operators so that we are able |
| 6582 | /// to handle extreme cases of chained binary operators without causing stack |
| 6583 | /// overflow. |
| 6584 | class DataRecursiveIntBinOpEvaluator { |
| 6585 | struct EvalResult { |
| 6586 | APValue Val; |
| 6587 | bool Failed; |
| 6588 | |
| 6589 | EvalResult() : Failed(false) { } |
| 6590 | |
| 6591 | void swap(EvalResult &RHS) { |
| 6592 | Val.swap(RHS.Val); |
| 6593 | Failed = RHS.Failed; |
| 6594 | RHS.Failed = false; |
| 6595 | } |
| 6596 | }; |
| 6597 | |
| 6598 | struct Job { |
| 6599 | const Expr *E; |
| 6600 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 6601 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6602 | |
David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 6603 | Job() = default; |
| 6604 | Job(Job &&J) |
| 6605 | : E(J.E), LHSResult(J.LHSResult), Kind(J.Kind), |
| 6606 | StoredInfo(J.StoredInfo), OldEvalStatus(J.OldEvalStatus) { |
| 6607 | J.StoredInfo = nullptr; |
| 6608 | } |
| 6609 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6610 | void startSpeculativeEval(EvalInfo &Info) { |
| 6611 | OldEvalStatus = Info.EvalStatus; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6612 | Info.EvalStatus.Diag = nullptr; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6613 | StoredInfo = &Info; |
| 6614 | } |
| 6615 | ~Job() { |
| 6616 | if (StoredInfo) { |
| 6617 | StoredInfo->EvalStatus = OldEvalStatus; |
| 6618 | } |
| 6619 | } |
| 6620 | private: |
David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 6621 | EvalInfo *StoredInfo = nullptr; // non-null if status changed. |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6622 | Expr::EvalStatus OldEvalStatus; |
| 6623 | }; |
| 6624 | |
| 6625 | SmallVector<Job, 16> Queue; |
| 6626 | |
| 6627 | IntExprEvaluator &IntEval; |
| 6628 | EvalInfo &Info; |
| 6629 | APValue &FinalResult; |
| 6630 | |
| 6631 | public: |
| 6632 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 6633 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 6634 | |
| 6635 | /// \brief True if \param E is a binary operator that we are going to handle |
| 6636 | /// data recursively. |
| 6637 | /// We handle binary operators that are comma, logical, or that have operands |
| 6638 | /// with integral or enumeration type. |
| 6639 | static bool shouldEnqueue(const BinaryOperator *E) { |
| 6640 | return E->getOpcode() == BO_Comma || |
| 6641 | E->isLogicalOp() || |
| 6642 | (E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6643 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6644 | } |
| 6645 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6646 | bool Traverse(const BinaryOperator *E) { |
| 6647 | enqueue(E); |
| 6648 | EvalResult PrevResult; |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6649 | while (!Queue.empty()) |
| 6650 | process(PrevResult); |
| 6651 | |
| 6652 | if (PrevResult.Failed) return false; |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6653 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6654 | FinalResult.swap(PrevResult.Val); |
| 6655 | return true; |
| 6656 | } |
| 6657 | |
| 6658 | private: |
| 6659 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 6660 | return IntEval.Success(Value, E, Result); |
| 6661 | } |
| 6662 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 6663 | return IntEval.Success(Value, E, Result); |
| 6664 | } |
| 6665 | bool Error(const Expr *E) { |
| 6666 | return IntEval.Error(E); |
| 6667 | } |
| 6668 | bool Error(const Expr *E, diag::kind D) { |
| 6669 | return IntEval.Error(E, D); |
| 6670 | } |
| 6671 | |
| 6672 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 6673 | return Info.CCEDiag(E, D); |
| 6674 | } |
| 6675 | |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6676 | // \brief Returns true if visiting the RHS is necessary, false otherwise. |
| 6677 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6678 | bool &SuppressRHSDiags); |
| 6679 | |
| 6680 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 6681 | const BinaryOperator *E, APValue &Result); |
| 6682 | |
| 6683 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 6684 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 6685 | if (Result.Failed) |
| 6686 | Result.Val = APValue(); |
| 6687 | } |
| 6688 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6689 | void process(EvalResult &Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6690 | |
| 6691 | void enqueue(const Expr *E) { |
| 6692 | E = E->IgnoreParens(); |
| 6693 | Queue.resize(Queue.size()+1); |
| 6694 | Queue.back().E = E; |
| 6695 | Queue.back().Kind = Job::AnyExprKind; |
| 6696 | } |
| 6697 | }; |
| 6698 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6699 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6700 | |
| 6701 | bool DataRecursiveIntBinOpEvaluator:: |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6702 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6703 | bool &SuppressRHSDiags) { |
| 6704 | if (E->getOpcode() == BO_Comma) { |
| 6705 | // Ignore LHS but note if we could not evaluate it. |
| 6706 | if (LHSResult.Failed) |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6707 | return Info.noteSideEffect(); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6708 | return true; |
| 6709 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6710 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6711 | if (E->isLogicalOp()) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6712 | bool LHSAsBool; |
| 6713 | if (!LHSResult.Failed && HandleConversionToBool(LHSResult.Val, LHSAsBool)) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6714 | // We were able to evaluate the LHS, see if we can get away with not |
| 6715 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6716 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
| 6717 | Success(LHSAsBool, E, LHSResult.Val); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6718 | return false; // Ignore RHS |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6719 | } |
| 6720 | } else { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6721 | LHSResult.Failed = true; |
| 6722 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6723 | // Since we weren't able to evaluate the left hand side, it |
| 6724 | // must have had side effects. |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6725 | if (!Info.noteSideEffect()) |
| 6726 | return false; |
| 6727 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6728 | // We can't evaluate the LHS; however, sometimes the result |
| 6729 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 6730 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 6731 | SuppressRHSDiags = true; |
| 6732 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6733 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6734 | return true; |
| 6735 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6736 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6737 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6738 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 6739 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6740 | if (LHSResult.Failed && !Info.keepEvaluatingAfterFailure()) |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6741 | return false; // Ignore RHS; |
| 6742 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6743 | return true; |
| 6744 | } |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6745 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6746 | bool DataRecursiveIntBinOpEvaluator:: |
| 6747 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 6748 | const BinaryOperator *E, APValue &Result) { |
| 6749 | if (E->getOpcode() == BO_Comma) { |
| 6750 | if (RHSResult.Failed) |
| 6751 | return false; |
| 6752 | Result = RHSResult.Val; |
| 6753 | return true; |
| 6754 | } |
| 6755 | |
| 6756 | if (E->isLogicalOp()) { |
| 6757 | bool lhsResult, rhsResult; |
| 6758 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 6759 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
| 6760 | |
| 6761 | if (LHSIsOK) { |
| 6762 | if (RHSIsOK) { |
| 6763 | if (E->getOpcode() == BO_LOr) |
| 6764 | return Success(lhsResult || rhsResult, E, Result); |
| 6765 | else |
| 6766 | return Success(lhsResult && rhsResult, E, Result); |
| 6767 | } |
| 6768 | } else { |
| 6769 | if (RHSIsOK) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6770 | // We can't evaluate the LHS; however, sometimes the result |
| 6771 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 6772 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6773 | return Success(rhsResult, E, Result); |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6774 | } |
| 6775 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6776 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 6777 | return false; |
| 6778 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6779 | |
| 6780 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 6781 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| 6782 | |
| 6783 | if (LHSResult.Failed || RHSResult.Failed) |
| 6784 | return false; |
| 6785 | |
| 6786 | const APValue &LHSVal = LHSResult.Val; |
| 6787 | const APValue &RHSVal = RHSResult.Val; |
| 6788 | |
| 6789 | // Handle cases like (unsigned long)&a + 4. |
| 6790 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 6791 | Result = LHSVal; |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 6792 | CharUnits AdditionalOffset = |
| 6793 | CharUnits::fromQuantity(RHSVal.getInt().getZExtValue()); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6794 | if (E->getOpcode() == BO_Add) |
| 6795 | Result.getLValueOffset() += AdditionalOffset; |
| 6796 | else |
| 6797 | Result.getLValueOffset() -= AdditionalOffset; |
| 6798 | return true; |
| 6799 | } |
| 6800 | |
| 6801 | // Handle cases like 4 + (unsigned long)&a |
| 6802 | if (E->getOpcode() == BO_Add && |
| 6803 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 6804 | Result = RHSVal; |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 6805 | Result.getLValueOffset() += |
| 6806 | CharUnits::fromQuantity(LHSVal.getInt().getZExtValue()); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6807 | return true; |
| 6808 | } |
| 6809 | |
| 6810 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 6811 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 6812 | if (!LHSVal.getLValueOffset().isZero() || |
| 6813 | !RHSVal.getLValueOffset().isZero()) |
| 6814 | return false; |
| 6815 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 6816 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 6817 | if (!LHSExpr || !RHSExpr) |
| 6818 | return false; |
| 6819 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 6820 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 6821 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 6822 | return false; |
| 6823 | // Make sure both labels come from the same function. |
| 6824 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 6825 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 6826 | return false; |
| 6827 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 6828 | return true; |
| 6829 | } |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 6830 | |
| 6831 | // All the remaining cases expect both operands to be an integer |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6832 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 6833 | return Error(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 6834 | |
| 6835 | // Set up the width and signedness manually, in case it can't be deduced |
| 6836 | // from the operation we're performing. |
| 6837 | // FIXME: Don't do this in the cases where we can deduce it. |
| 6838 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
| 6839 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
| 6840 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
| 6841 | RHSVal.getInt(), Value)) |
| 6842 | return false; |
| 6843 | return Success(Value, E, Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6844 | } |
| 6845 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6846 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6847 | Job &job = Queue.back(); |
| 6848 | |
| 6849 | switch (job.Kind) { |
| 6850 | case Job::AnyExprKind: { |
| 6851 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 6852 | if (shouldEnqueue(Bop)) { |
| 6853 | job.Kind = Job::BinOpKind; |
| 6854 | enqueue(Bop->getLHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6855 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6856 | } |
| 6857 | } |
| 6858 | |
| 6859 | EvaluateExpr(job.E, Result); |
| 6860 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6861 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6862 | } |
| 6863 | |
| 6864 | case Job::BinOpKind: { |
| 6865 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6866 | bool SuppressRHSDiags = false; |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6867 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6868 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6869 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6870 | } |
| 6871 | if (SuppressRHSDiags) |
| 6872 | job.startSpeculativeEval(Info); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 6873 | job.LHSResult.swap(Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6874 | job.Kind = Job::BinOpVisitedLHSKind; |
| 6875 | enqueue(Bop->getRHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6876 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6877 | } |
| 6878 | |
| 6879 | case Job::BinOpVisitedLHSKind: { |
| 6880 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 6881 | EvalResult RHS; |
| 6882 | RHS.swap(Result); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6883 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6884 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 6885 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6886 | } |
| 6887 | } |
| 6888 | |
| 6889 | llvm_unreachable("Invalid Job::Kind!"); |
| 6890 | } |
| 6891 | |
| 6892 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 6893 | if (!Info.keepEvaluatingAfterFailure() && E->isAssignmentOp()) |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6894 | return Error(E); |
| 6895 | |
| 6896 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 6897 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 6898 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6899 | QualType LHSTy = E->getLHS()->getType(); |
| 6900 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6901 | |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 6902 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 6903 | ComplexValue LHS, RHS; |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 6904 | bool LHSOK; |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 6905 | if (E->isAssignmentOp()) { |
| 6906 | LValue LV; |
| 6907 | EvaluateLValue(E->getLHS(), LV, Info); |
| 6908 | LHSOK = false; |
| 6909 | } else if (LHSTy->isRealFloatingType()) { |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 6910 | LHSOK = EvaluateFloat(E->getLHS(), LHS.FloatReal, Info); |
| 6911 | if (LHSOK) { |
| 6912 | LHS.makeComplexFloat(); |
| 6913 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
| 6914 | } |
| 6915 | } else { |
| 6916 | LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 6917 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6918 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6919 | return false; |
| 6920 | |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 6921 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 6922 | if (!EvaluateFloat(E->getRHS(), RHS.FloatReal, Info) || !LHSOK) |
| 6923 | return false; |
| 6924 | RHS.makeComplexFloat(); |
| 6925 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
| 6926 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6927 | return false; |
| 6928 | |
| 6929 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6930 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6931 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6932 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6933 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 6934 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6935 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6936 | return Success((CR_r == APFloat::cmpEqual && |
| 6937 | CR_i == APFloat::cmpEqual), E); |
| 6938 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6939 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6940 | "Invalid complex comparison."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6941 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 6942 | CR_r == APFloat::cmpLessThan || |
| 6943 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6944 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 6945 | CR_i == APFloat::cmpLessThan || |
| 6946 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6947 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6948 | } else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6949 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6950 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 6951 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 6952 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6953 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6954 | "Invalid compex comparison."); |
| 6955 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 6956 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 6957 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 6958 | } |
| 6959 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6960 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6961 | if (LHSTy->isRealFloatingType() && |
| 6962 | RHSTy->isRealFloatingType()) { |
| 6963 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6964 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6965 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
| 6966 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6967 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6968 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6969 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6970 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6971 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6972 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 899c705 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 6973 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6974 | switch (E->getOpcode()) { |
| 6975 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6976 | llvm_unreachable("Invalid binary operator!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6977 | case BO_LT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6978 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6979 | case BO_GT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6980 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6981 | case BO_LE: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6982 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6983 | case BO_GE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6984 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6985 | E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6986 | case BO_EQ: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 6987 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6988 | case BO_NE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6989 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 6990 | || CR == APFloat::cmpLessThan |
| 6991 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6992 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 6993 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6994 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 6995 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 6996 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6997 | LValue LHSValue, RHSValue; |
| 6998 | |
| 6999 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 7000 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7001 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7002 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7003 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7004 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7005 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 7006 | // Reject differing bases from the normal codepath; we special-case |
| 7007 | // comparisons to null. |
| 7008 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7009 | if (E->getOpcode() == BO_Sub) { |
| 7010 | // Handle &&A - &&B. |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7011 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 7012 | return false; |
| 7013 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
Benjamin Kramer | daa09612 | 2012-10-03 14:15:39 +0000 | [diff] [blame] | 7014 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr*>(); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7015 | if (!LHSExpr || !RHSExpr) |
| 7016 | return false; |
| 7017 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 7018 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 7019 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 7020 | return false; |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 7021 | // Make sure both labels come from the same function. |
| 7022 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 7023 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 7024 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7025 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7026 | return true; |
| 7027 | } |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 7028 | // Inequalities and subtractions between unrelated pointers have |
| 7029 | // unspecified or undefined behavior. |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 7030 | if (!E->isEqualityOp()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7031 | return Error(E); |
Eli Friedman | c6be94b | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 7032 | // A constant address may compare equal to the address of a symbol. |
| 7033 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | 42fbd62 | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 7034 | // to a null pointer constant. |
Eli Friedman | c6be94b | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 7035 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 7036 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7037 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 7038 | // It's implementation-defined whether distinct literals will have |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 7039 | // distinct addresses. In clang, the result of such a comparison is |
| 7040 | // unspecified, so it is not a constant expression. However, we do know |
| 7041 | // that the address of a literal will be non-null. |
Richard Smith | e9e20dd3 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 7042 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 7043 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7044 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 7045 | // We can't tell whether weak symbols will end up pointing to the same |
| 7046 | // object. |
| 7047 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7048 | return Error(E); |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 7049 | // We can't compare the address of the start of one object with the |
| 7050 | // past-the-end address of another object, per C++ DR1652. |
| 7051 | if ((LHSValue.Base && LHSValue.Offset.isZero() && |
| 7052 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) || |
| 7053 | (RHSValue.Base && RHSValue.Offset.isZero() && |
| 7054 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue))) |
| 7055 | return Error(E); |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 7056 | // We can't tell whether an object is at the same address as another |
| 7057 | // zero sized object. |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 7058 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
| 7059 | (LHSValue.Base && isZeroSized(RHSValue))) |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 7060 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 7061 | // Pointers with different bases cannot represent the same object. |
Eli Friedman | 42fbd62 | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 7062 | // (Note that clang defaults to -fmerge-all-constants, which can |
| 7063 | // lead to inconsistent results for comparisons involving the address |
| 7064 | // of a constant; this generally doesn't matter in practice.) |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 7065 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 7066 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7067 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 7068 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 7069 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 7070 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 7071 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 7072 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 7073 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7074 | if (E->getOpcode() == BO_Sub) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 7075 | // C++11 [expr.add]p6: |
| 7076 | // Unless both pointers point to elements of the same array object, or |
| 7077 | // one past the last element of the array object, the behavior is |
| 7078 | // undefined. |
| 7079 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 7080 | !AreElementsOfSameArray(getType(LHSValue.Base), |
| 7081 | LHSDesignator, RHSDesignator)) |
| 7082 | CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 7083 | |
Chris Lattner | 882bdf2 | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 7084 | QualType Type = E->getLHS()->getType(); |
| 7085 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7086 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7087 | CharUnits ElementSize; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 7088 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7089 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7090 | |
Richard Smith | 84c6b3d | 2013-09-10 21:34:14 +0000 | [diff] [blame] | 7091 | // As an extension, a type may have zero size (empty struct or union in |
| 7092 | // C, array of zero length). Pointer subtraction in such cases has |
| 7093 | // undefined behavior, so is not constant. |
| 7094 | if (ElementSize.isZero()) { |
| 7095 | Info.Diag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
| 7096 | << ElementType; |
| 7097 | return false; |
| 7098 | } |
| 7099 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 7100 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 7101 | // and produce incorrect results when it overflows. Such behavior |
| 7102 | // appears to be non-conforming, but is common, so perhaps we should |
| 7103 | // assume the standard intended for such cases to be undefined behavior |
| 7104 | // and check for them. |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 7105 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 7106 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 7107 | // overflow in the final conversion to ptrdiff_t. |
| 7108 | APSInt LHS( |
| 7109 | llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 7110 | APSInt RHS( |
| 7111 | llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 7112 | APSInt ElemSize( |
| 7113 | llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); |
| 7114 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 7115 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 7116 | |
| 7117 | if (Result.extend(65) != TrueResult) |
| 7118 | HandleOverflow(Info, E, TrueResult, E->getType()); |
| 7119 | return Success(Result, E); |
| 7120 | } |
Richard Smith | de21b24 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 7121 | |
| 7122 | // C++11 [expr.rel]p3: |
| 7123 | // Pointers to void (after pointer conversions) can be compared, with a |
| 7124 | // result defined as follows: If both pointers represent the same |
| 7125 | // address or are both the null pointer value, the result is true if the |
| 7126 | // operator is <= or >= and false otherwise; otherwise the result is |
| 7127 | // unspecified. |
| 7128 | // We interpret this as applying to pointers to *cv* void. |
| 7129 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 7130 | E->isRelationalOp()) |
Richard Smith | de21b24 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 7131 | CCEDiag(E, diag::note_constexpr_void_comparison); |
| 7132 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 7133 | // C++11 [expr.rel]p2: |
| 7134 | // - If two pointers point to non-static data members of the same object, |
| 7135 | // or to subobjects or array elements fo such members, recursively, the |
| 7136 | // pointer to the later declared member compares greater provided the |
| 7137 | // two members have the same access control and provided their class is |
| 7138 | // not a union. |
| 7139 | // [...] |
| 7140 | // - Otherwise pointer comparisons are unspecified. |
| 7141 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 7142 | E->isRelationalOp()) { |
| 7143 | bool WasArrayIndex; |
| 7144 | unsigned Mismatch = |
| 7145 | FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, |
| 7146 | RHSDesignator, WasArrayIndex); |
| 7147 | // At the point where the designators diverge, the comparison has a |
| 7148 | // specified value if: |
| 7149 | // - we are comparing array indices |
| 7150 | // - we are comparing fields of a union, or fields with the same access |
| 7151 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 7152 | // constant expression. |
| 7153 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 7154 | Mismatch < RHSDesignator.Entries.size()) { |
| 7155 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 7156 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 7157 | if (!LF && !RF) |
| 7158 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 7159 | else if (!LF) |
| 7160 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 7161 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 7162 | << RF->getParent() << RF; |
| 7163 | else if (!RF) |
| 7164 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 7165 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 7166 | << LF->getParent() << LF; |
| 7167 | else if (!LF->getParent()->isUnion() && |
| 7168 | LF->getAccess() != RF->getAccess()) |
| 7169 | CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) |
| 7170 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 7171 | << LF->getParent(); |
| 7172 | } |
| 7173 | } |
| 7174 | |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 7175 | // The comparison here must be unsigned, and performed with the same |
| 7176 | // width as the pointer. |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 7177 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 7178 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 7179 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 7180 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 7181 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 7182 | CompareLHS &= Mask; |
| 7183 | CompareRHS &= Mask; |
| 7184 | |
Eli Friedman | 2f5b7c5 | 2012-04-16 19:23:57 +0000 | [diff] [blame] | 7185 | // If there is a base and this is a relational operator, we can only |
| 7186 | // compare pointers within the object in question; otherwise, the result |
| 7187 | // depends on where the object is located in memory. |
| 7188 | if (!LHSValue.Base.isNull() && E->isRelationalOp()) { |
| 7189 | QualType BaseTy = getType(LHSValue.Base); |
| 7190 | if (BaseTy->isIncompleteType()) |
| 7191 | return Error(E); |
| 7192 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 7193 | uint64_t OffsetLimit = Size.getQuantity(); |
| 7194 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 7195 | return Error(E); |
| 7196 | } |
| 7197 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 7198 | switch (E->getOpcode()) { |
| 7199 | default: llvm_unreachable("missing comparison operator"); |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 7200 | case BO_LT: return Success(CompareLHS < CompareRHS, E); |
| 7201 | case BO_GT: return Success(CompareLHS > CompareRHS, E); |
| 7202 | case BO_LE: return Success(CompareLHS <= CompareRHS, E); |
| 7203 | case BO_GE: return Success(CompareLHS >= CompareRHS, E); |
| 7204 | case BO_EQ: return Success(CompareLHS == CompareRHS, E); |
| 7205 | case BO_NE: return Success(CompareLHS != CompareRHS, E); |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 7206 | } |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7207 | } |
| 7208 | } |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 7209 | |
| 7210 | if (LHSTy->isMemberPointerType()) { |
| 7211 | assert(E->isEqualityOp() && "unexpected member pointer operation"); |
| 7212 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 7213 | |
| 7214 | MemberPtr LHSValue, RHSValue; |
| 7215 | |
| 7216 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
| 7217 | if (!LHSOK && Info.keepEvaluatingAfterFailure()) |
| 7218 | return false; |
| 7219 | |
| 7220 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 7221 | return false; |
| 7222 | |
| 7223 | // C++11 [expr.eq]p2: |
| 7224 | // If both operands are null, they compare equal. Otherwise if only one is |
| 7225 | // null, they compare unequal. |
| 7226 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 7227 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| 7228 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 7229 | } |
| 7230 | |
| 7231 | // Otherwise if either is a pointer to a virtual member function, the |
| 7232 | // result is unspecified. |
| 7233 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 7234 | if (MD->isVirtual()) |
| 7235 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 7236 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 7237 | if (MD->isVirtual()) |
| 7238 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 7239 | |
| 7240 | // Otherwise they compare equal if and only if they would refer to the |
| 7241 | // same member of the same most derived object or the same subobject if |
| 7242 | // they were dereferenced with a hypothetical object of the associated |
| 7243 | // class type. |
| 7244 | bool Equal = LHSValue == RHSValue; |
| 7245 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 7246 | } |
| 7247 | |
Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 7248 | if (LHSTy->isNullPtrType()) { |
| 7249 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 7250 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 7251 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 7252 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 7253 | // false otherwise. |
| 7254 | BinaryOperator::Opcode Opcode = E->getOpcode(); |
| 7255 | return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); |
| 7256 | } |
| 7257 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7258 | assert((!LHSTy->isIntegralOrEnumerationType() || |
| 7259 | !RHSTy->isIntegralOrEnumerationType()) && |
| 7260 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
| 7261 | // We can't continue from here for non-integral types. |
| 7262 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7263 | } |
| 7264 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7265 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 7266 | /// a result as the expression's type. |
| 7267 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 7268 | const UnaryExprOrTypeTraitExpr *E) { |
| 7269 | switch(E->getKind()) { |
| 7270 | case UETT_AlignOf: { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 7271 | if (E->isArgumentType()) |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 7272 | return Success(GetAlignOfType(Info, E->getArgumentType()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 7273 | else |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 7274 | return Success(GetAlignOfExpr(Info, E->getArgumentExpr()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 7275 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7276 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7277 | case UETT_VecStep: { |
| 7278 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 7279 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7280 | if (Ty->isVectorType()) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7281 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 7282 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7283 | // The vec_step built-in functions that take a 3-component |
| 7284 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 7285 | if (n == 3) |
| 7286 | n = 4; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 7287 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7288 | return Success(n, E); |
| 7289 | } else |
| 7290 | return Success(1, E); |
| 7291 | } |
| 7292 | |
| 7293 | case UETT_SizeOf: { |
| 7294 | QualType SrcTy = E->getTypeOfArgument(); |
| 7295 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 7296 | // the result is the size of the referenced type." |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7297 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 7298 | SrcTy = Ref->getPointeeType(); |
| 7299 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7300 | CharUnits Sizeof; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 7301 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7302 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7303 | return Success(Sizeof, E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7304 | } |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 7305 | case UETT_OpenMPRequiredSimdAlign: |
| 7306 | assert(E->isArgumentType()); |
| 7307 | return Success( |
| 7308 | Info.Ctx.toCharUnitsFromBits( |
| 7309 | Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType())) |
| 7310 | .getQuantity(), |
| 7311 | E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7312 | } |
| 7313 | |
| 7314 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 7315 | } |
| 7316 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7317 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7318 | CharUnits Result; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7319 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7320 | if (n == 0) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7321 | return Error(OOE); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7322 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7323 | for (unsigned i = 0; i != n; ++i) { |
| 7324 | OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i); |
| 7325 | switch (ON.getKind()) { |
| 7326 | case OffsetOfExpr::OffsetOfNode::Array: { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7327 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7328 | APSInt IdxResult; |
| 7329 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 7330 | return false; |
| 7331 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 7332 | if (!AT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7333 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7334 | CurrentType = AT->getElementType(); |
| 7335 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 7336 | Result += IdxResult.getSExtValue() * ElementSize; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 7337 | break; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7338 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7339 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7340 | case OffsetOfExpr::OffsetOfNode::Field: { |
| 7341 | FieldDecl *MemberDecl = ON.getField(); |
| 7342 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7343 | if (!RT) |
| 7344 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7345 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 7346 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7347 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 7348 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7349 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 7350 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7351 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 7352 | break; |
| 7353 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7354 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7355 | case OffsetOfExpr::OffsetOfNode::Identifier: |
| 7356 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7357 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7358 | case OffsetOfExpr::OffsetOfNode::Base: { |
| 7359 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 7360 | if (BaseSpec->isVirtual()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7361 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7362 | |
| 7363 | // Find the layout of the class whose base we are looking into. |
| 7364 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7365 | if (!RT) |
| 7366 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7367 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 7368 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7369 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 7370 | |
| 7371 | // Find the base class itself. |
| 7372 | CurrentType = BaseSpec->getType(); |
| 7373 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 7374 | if (!BaseRT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7375 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7376 | |
| 7377 | // Add the offset to the base. |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 7378 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7379 | break; |
| 7380 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7381 | } |
| 7382 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7383 | return Success(Result, OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7384 | } |
| 7385 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7386 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7387 | switch (E->getOpcode()) { |
| 7388 | default: |
| 7389 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 7390 | // See C99 6.6p3. |
| 7391 | return Error(E); |
| 7392 | case UO_Extension: |
| 7393 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 7394 | // If so, we could clear the diagnostic ID. |
| 7395 | return Visit(E->getSubExpr()); |
| 7396 | case UO_Plus: |
| 7397 | // The result is just the value. |
| 7398 | return Visit(E->getSubExpr()); |
| 7399 | case UO_Minus: { |
| 7400 | if (!Visit(E->getSubExpr())) |
| 7401 | return false; |
| 7402 | if (!Result.isInt()) return Error(E); |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 7403 | const APSInt &Value = Result.getInt(); |
| 7404 | if (Value.isSigned() && Value.isMinSignedValue()) |
| 7405 | HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 7406 | E->getType()); |
| 7407 | return Success(-Value, E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7408 | } |
| 7409 | case UO_Not: { |
| 7410 | if (!Visit(E->getSubExpr())) |
| 7411 | return false; |
| 7412 | if (!Result.isInt()) return Error(E); |
| 7413 | return Success(~Result.getInt(), E); |
| 7414 | } |
| 7415 | case UO_LNot: { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 7416 | bool bres; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7417 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 7418 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7419 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 7420 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7421 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7422 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7423 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7424 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 7425 | /// result type is integer. |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7426 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 7427 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 7428 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 7429 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 7430 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7431 | switch (E->getCastKind()) { |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7432 | case CK_BaseToDerived: |
| 7433 | case CK_DerivedToBase: |
| 7434 | case CK_UncheckedDerivedToBase: |
| 7435 | case CK_Dynamic: |
| 7436 | case CK_ToUnion: |
| 7437 | case CK_ArrayToPointerDecay: |
| 7438 | case CK_FunctionToPointerDecay: |
| 7439 | case CK_NullToPointer: |
| 7440 | case CK_NullToMemberPointer: |
| 7441 | case CK_BaseToDerivedMemberPointer: |
| 7442 | case CK_DerivedToBaseMemberPointer: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 7443 | case CK_ReinterpretMemberPointer: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7444 | case CK_ConstructorConversion: |
| 7445 | case CK_IntegralToPointer: |
| 7446 | case CK_ToVoid: |
| 7447 | case CK_VectorSplat: |
| 7448 | case CK_IntegralToFloating: |
| 7449 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7450 | case CK_CPointerToObjCPointerCast: |
| 7451 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7452 | case CK_AnyPointerToBlockPointerCast: |
| 7453 | case CK_ObjCObjectLValueCast: |
| 7454 | case CK_FloatingRealToComplex: |
| 7455 | case CK_FloatingComplexToReal: |
| 7456 | case CK_FloatingComplexCast: |
| 7457 | case CK_FloatingComplexToIntegralComplex: |
| 7458 | case CK_IntegralRealToComplex: |
| 7459 | case CK_IntegralComplexCast: |
| 7460 | case CK_IntegralComplexToFloatingComplex: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 7461 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7462 | case CK_ZeroToOCLEvent: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7463 | case CK_NonAtomicToAtomic: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 7464 | case CK_AddressSpaceConversion: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7465 | llvm_unreachable("invalid cast kind for integral value"); |
| 7466 | |
Eli Friedman | 9faf2f9 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 7467 | case CK_BitCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7468 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7469 | case CK_LValueBitCast: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 7470 | case CK_ARCProduceObject: |
| 7471 | case CK_ARCConsumeObject: |
| 7472 | case CK_ARCReclaimReturnedObject: |
| 7473 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 7474 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7475 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7476 | |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 7477 | case CK_UserDefinedConversion: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7478 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7479 | case CK_AtomicToNonAtomic: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7480 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7481 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7482 | |
| 7483 | case CK_MemberPointerToBoolean: |
| 7484 | case CK_PointerToBoolean: |
| 7485 | case CK_IntegralToBoolean: |
| 7486 | case CK_FloatingToBoolean: |
| 7487 | case CK_FloatingComplexToBoolean: |
| 7488 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7489 | bool BoolResult; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7490 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7491 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7492 | return Success(BoolResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7493 | } |
| 7494 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7495 | case CK_IntegralCast: { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7496 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7497 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 7498 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 7499 | if (!Result.isInt()) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 7500 | // Allow casts of address-of-label differences if they are no-ops |
| 7501 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 7502 | // be constant-evaluatable except in some narrow cases which are hard |
| 7503 | // to detect here. We let it through on the assumption the user knows |
| 7504 | // what they are doing.) |
| 7505 | if (Result.isAddrLabelDiff()) |
| 7506 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 7507 | // Only allow casts of lvalues if they are lossless. |
| 7508 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 7509 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7510 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7511 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 7512 | Result.getInt()), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7513 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7514 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7515 | case CK_PointerToIntegral: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 7516 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 7517 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7518 | LValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 7519 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7520 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7521 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 7522 | if (LV.getLValueBase()) { |
| 7523 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7524 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 7525 | // narrowing back down to pointer width in subsequent integral casts. |
| 7526 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 7527 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7528 | return Error(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7529 | |
Richard Smith | cf74da7 | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 7530 | LV.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7531 | LV.moveInto(Result); |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 7532 | return true; |
| 7533 | } |
| 7534 | |
Ken Dyck | 0299083 | 2010-01-15 12:37:54 +0000 | [diff] [blame] | 7535 | APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(), |
| 7536 | SrcType); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 7537 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 7538 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7539 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7540 | case CK_IntegralComplexToReal: { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7541 | ComplexValue C; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 7542 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 7543 | return false; |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7544 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 7545 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 7546 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7547 | case CK_FloatingToIntegral: { |
| 7548 | APFloat F(0.0); |
| 7549 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 7550 | return false; |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 7551 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7552 | APSInt Value; |
| 7553 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 7554 | return false; |
| 7555 | return Success(Value, E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7556 | } |
| 7557 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7558 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7559 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7560 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 7561 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7562 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 7563 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7564 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7565 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 7566 | return false; |
| 7567 | if (!LV.isComplexInt()) |
| 7568 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7569 | return Success(LV.getComplexIntReal(), E); |
| 7570 | } |
| 7571 | |
| 7572 | return Visit(E->getSubExpr()); |
| 7573 | } |
| 7574 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7575 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7576 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7577 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7578 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 7579 | return false; |
| 7580 | if (!LV.isComplexInt()) |
| 7581 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7582 | return Success(LV.getComplexIntImag(), E); |
| 7583 | } |
| 7584 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 7585 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7586 | return Success(0, E); |
| 7587 | } |
| 7588 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 7589 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 7590 | return Success(E->getPackLength(), E); |
| 7591 | } |
| 7592 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 7593 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 7594 | return Success(E->getValue(), E); |
| 7595 | } |
| 7596 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7597 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7598 | // Float Evaluation |
| 7599 | //===----------------------------------------------------------------------===// |
| 7600 | |
| 7601 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7602 | class FloatExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7603 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7604 | APFloat &Result; |
| 7605 | public: |
| 7606 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7607 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7608 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7609 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7610 | Result = V.getFloat(); |
| 7611 | return true; |
| 7612 | } |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7613 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7614 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7615 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 7616 | return true; |
| 7617 | } |
| 7618 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7619 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7620 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7621 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7622 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 7623 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7624 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 7625 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7626 | bool VisitUnaryReal(const UnaryOperator *E); |
| 7627 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 7628 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7629 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7630 | }; |
| 7631 | } // end anonymous namespace |
| 7632 | |
| 7633 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7634 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7635 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7636 | } |
| 7637 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7638 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7639 | QualType ResultTy, |
| 7640 | const Expr *Arg, |
| 7641 | bool SNaN, |
| 7642 | llvm::APFloat &Result) { |
| 7643 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 7644 | if (!S) return false; |
| 7645 | |
| 7646 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 7647 | |
| 7648 | llvm::APInt fill; |
| 7649 | |
| 7650 | // Treat empty strings as if they were zero. |
| 7651 | if (S->getString().empty()) |
| 7652 | fill = llvm::APInt(32, 0); |
| 7653 | else if (S->getString().getAsInteger(0, fill)) |
| 7654 | return false; |
| 7655 | |
Petar Jovanovic | d55ae6b | 2015-02-26 18:19:22 +0000 | [diff] [blame] | 7656 | if (Context.getTargetInfo().isNan2008()) { |
| 7657 | if (SNaN) |
| 7658 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 7659 | else |
| 7660 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 7661 | } else { |
| 7662 | // Prior to IEEE 754-2008, architectures were allowed to choose whether |
| 7663 | // the first bit of their significand was set for qNaN or sNaN. MIPS chose |
| 7664 | // a different encoding to what became a standard in 2008, and for pre- |
| 7665 | // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as |
| 7666 | // sNaN. This is now known as "legacy NaN" encoding. |
| 7667 | if (SNaN) |
| 7668 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 7669 | else |
| 7670 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 7671 | } |
| 7672 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7673 | return true; |
| 7674 | } |
| 7675 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7676 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 7677 | switch (E->getBuiltinCallee()) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7678 | default: |
| 7679 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 7680 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7681 | case Builtin::BI__builtin_huge_val: |
| 7682 | case Builtin::BI__builtin_huge_valf: |
| 7683 | case Builtin::BI__builtin_huge_vall: |
| 7684 | case Builtin::BI__builtin_inf: |
| 7685 | case Builtin::BI__builtin_inff: |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 7686 | case Builtin::BI__builtin_infl: { |
| 7687 | const llvm::fltSemantics &Sem = |
| 7688 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 7689 | Result = llvm::APFloat::getInf(Sem); |
| 7690 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 7691 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7692 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7693 | case Builtin::BI__builtin_nans: |
| 7694 | case Builtin::BI__builtin_nansf: |
| 7695 | case Builtin::BI__builtin_nansl: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7696 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 7697 | true, Result)) |
| 7698 | return Error(E); |
| 7699 | return true; |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 7700 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 7701 | case Builtin::BI__builtin_nan: |
| 7702 | case Builtin::BI__builtin_nanf: |
| 7703 | case Builtin::BI__builtin_nanl: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 7704 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 7705 | // can't constant fold it. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7706 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 7707 | false, Result)) |
| 7708 | return Error(E); |
| 7709 | return true; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7710 | |
| 7711 | case Builtin::BI__builtin_fabs: |
| 7712 | case Builtin::BI__builtin_fabsf: |
| 7713 | case Builtin::BI__builtin_fabsl: |
| 7714 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 7715 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7716 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7717 | if (Result.isNegative()) |
| 7718 | Result.changeSign(); |
| 7719 | return true; |
| 7720 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7721 | // FIXME: Builtin::BI__builtin_powi |
| 7722 | // FIXME: Builtin::BI__builtin_powif |
| 7723 | // FIXME: Builtin::BI__builtin_powil |
| 7724 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7725 | case Builtin::BI__builtin_copysign: |
| 7726 | case Builtin::BI__builtin_copysignf: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7727 | case Builtin::BI__builtin_copysignl: { |
| 7728 | APFloat RHS(0.); |
| 7729 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 7730 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 7731 | return false; |
| 7732 | Result.copySign(RHS); |
| 7733 | return true; |
| 7734 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7735 | } |
| 7736 | } |
| 7737 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7738 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7739 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 7740 | ComplexValue CV; |
| 7741 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 7742 | return false; |
| 7743 | Result = CV.FloatReal; |
| 7744 | return true; |
| 7745 | } |
| 7746 | |
| 7747 | return Visit(E->getSubExpr()); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
| 7750 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7751 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 7752 | ComplexValue CV; |
| 7753 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 7754 | return false; |
| 7755 | Result = CV.FloatImag; |
| 7756 | return true; |
| 7757 | } |
| 7758 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 7759 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 7760 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 7761 | Result = llvm::APFloat::getZero(Sem); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 7762 | return true; |
| 7763 | } |
| 7764 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7765 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7766 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7767 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7768 | case UO_Plus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 7769 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7770 | case UO_Minus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 7771 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 7772 | return false; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7773 | Result.changeSign(); |
| 7774 | return true; |
| 7775 | } |
| 7776 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7777 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7778 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7779 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 7780 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 141fbf3 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 7781 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 7782 | APFloat RHS(0.0); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7783 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
| 7784 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7785 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 7786 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
| 7787 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7788 | } |
| 7789 | |
| 7790 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 7791 | Result = E->getValue(); |
| 7792 | return true; |
| 7793 | } |
| 7794 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7795 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 7796 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7797 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7798 | switch (E->getCastKind()) { |
| 7799 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7800 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7801 | |
| 7802 | case CK_IntegralToFloating: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7803 | APSInt IntResult; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7804 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 7805 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 7806 | E->getType(), Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7807 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7808 | |
| 7809 | case CK_FloatingCast: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7810 | if (!Visit(SubExpr)) |
| 7811 | return false; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7812 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 7813 | Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7814 | } |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 7815 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7816 | case CK_FloatingComplexToReal: { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 7817 | ComplexValue V; |
| 7818 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 7819 | return false; |
| 7820 | Result = V.getComplexFloatReal(); |
| 7821 | return true; |
| 7822 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 7823 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7824 | } |
| 7825 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 7826 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 7827 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7828 | //===----------------------------------------------------------------------===// |
| 7829 | |
| 7830 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7831 | class ComplexExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7832 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7833 | ComplexValue &Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7834 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7835 | public: |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7836 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7837 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 7838 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7839 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7840 | Result.setFrom(V); |
| 7841 | return true; |
| 7842 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7843 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7844 | bool ZeroInitialization(const Expr *E); |
| 7845 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7846 | //===--------------------------------------------------------------------===// |
| 7847 | // Visitor Methods |
| 7848 | //===--------------------------------------------------------------------===// |
| 7849 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7850 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7851 | bool VisitCastExpr(const CastExpr *E); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7852 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 7853 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7854 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7855 | }; |
| 7856 | } // end anonymous namespace |
| 7857 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 7858 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 7859 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7860 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7861 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 7862 | } |
| 7863 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7864 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7865 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 7866 | if (ElemTy->isRealFloatingType()) { |
| 7867 | Result.makeComplexFloat(); |
| 7868 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 7869 | Result.FloatReal = Zero; |
| 7870 | Result.FloatImag = Zero; |
| 7871 | } else { |
| 7872 | Result.makeComplexInt(); |
| 7873 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 7874 | Result.IntReal = Zero; |
| 7875 | Result.IntImag = Zero; |
| 7876 | } |
| 7877 | return true; |
| 7878 | } |
| 7879 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7880 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 7881 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7882 | |
| 7883 | if (SubExpr->getType()->isRealFloatingType()) { |
| 7884 | Result.makeComplexFloat(); |
| 7885 | APFloat &Imag = Result.FloatImag; |
| 7886 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 7887 | return false; |
| 7888 | |
| 7889 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 7890 | return true; |
| 7891 | } else { |
| 7892 | assert(SubExpr->getType()->isIntegerType() && |
| 7893 | "Unexpected imaginary literal."); |
| 7894 | |
| 7895 | Result.makeComplexInt(); |
| 7896 | APSInt &Imag = Result.IntImag; |
| 7897 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 7898 | return false; |
| 7899 | |
| 7900 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 7901 | return true; |
| 7902 | } |
| 7903 | } |
| 7904 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7905 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7906 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7907 | switch (E->getCastKind()) { |
| 7908 | case CK_BitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7909 | case CK_BaseToDerived: |
| 7910 | case CK_DerivedToBase: |
| 7911 | case CK_UncheckedDerivedToBase: |
| 7912 | case CK_Dynamic: |
| 7913 | case CK_ToUnion: |
| 7914 | case CK_ArrayToPointerDecay: |
| 7915 | case CK_FunctionToPointerDecay: |
| 7916 | case CK_NullToPointer: |
| 7917 | case CK_NullToMemberPointer: |
| 7918 | case CK_BaseToDerivedMemberPointer: |
| 7919 | case CK_DerivedToBaseMemberPointer: |
| 7920 | case CK_MemberPointerToBoolean: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 7921 | case CK_ReinterpretMemberPointer: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7922 | case CK_ConstructorConversion: |
| 7923 | case CK_IntegralToPointer: |
| 7924 | case CK_PointerToIntegral: |
| 7925 | case CK_PointerToBoolean: |
| 7926 | case CK_ToVoid: |
| 7927 | case CK_VectorSplat: |
| 7928 | case CK_IntegralCast: |
| 7929 | case CK_IntegralToBoolean: |
| 7930 | case CK_IntegralToFloating: |
| 7931 | case CK_FloatingToIntegral: |
| 7932 | case CK_FloatingToBoolean: |
| 7933 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7934 | case CK_CPointerToObjCPointerCast: |
| 7935 | case CK_BlockPointerToObjCPointerCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7936 | case CK_AnyPointerToBlockPointerCast: |
| 7937 | case CK_ObjCObjectLValueCast: |
| 7938 | case CK_FloatingComplexToReal: |
| 7939 | case CK_FloatingComplexToBoolean: |
| 7940 | case CK_IntegralComplexToReal: |
| 7941 | case CK_IntegralComplexToBoolean: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 7942 | case CK_ARCProduceObject: |
| 7943 | case CK_ARCConsumeObject: |
| 7944 | case CK_ARCReclaimReturnedObject: |
| 7945 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 7946 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 7947 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 7948 | case CK_ZeroToOCLEvent: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7949 | case CK_NonAtomicToAtomic: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 7950 | case CK_AddressSpaceConversion: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7951 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 7952 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7953 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7954 | case CK_AtomicToNonAtomic: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7955 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7956 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7957 | |
| 7958 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7959 | case CK_LValueBitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7960 | case CK_UserDefinedConversion: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7961 | return Error(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7962 | |
| 7963 | case CK_FloatingRealToComplex: { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7964 | APFloat &Real = Result.FloatReal; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7965 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7966 | return false; |
| 7967 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7968 | Result.makeComplexFloat(); |
| 7969 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 7970 | return true; |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 7971 | } |
| 7972 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7973 | case CK_FloatingComplexCast: { |
| 7974 | if (!Visit(E->getSubExpr())) |
| 7975 | return false; |
| 7976 | |
| 7977 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 7978 | QualType From |
| 7979 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 7980 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7981 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 7982 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7983 | } |
| 7984 | |
| 7985 | case CK_FloatingComplexToIntegralComplex: { |
| 7986 | if (!Visit(E->getSubExpr())) |
| 7987 | return false; |
| 7988 | |
| 7989 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 7990 | QualType From |
| 7991 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 7992 | Result.makeComplexInt(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7993 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 7994 | To, Result.IntReal) && |
| 7995 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 7996 | To, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 7997 | } |
| 7998 | |
| 7999 | case CK_IntegralRealToComplex: { |
| 8000 | APSInt &Real = Result.IntReal; |
| 8001 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 8002 | return false; |
| 8003 | |
| 8004 | Result.makeComplexInt(); |
| 8005 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 8006 | return true; |
| 8007 | } |
| 8008 | |
| 8009 | case CK_IntegralComplexCast: { |
| 8010 | if (!Visit(E->getSubExpr())) |
| 8011 | return false; |
| 8012 | |
| 8013 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 8014 | QualType From |
| 8015 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 8016 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 8017 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 8018 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8019 | return true; |
| 8020 | } |
| 8021 | |
| 8022 | case CK_IntegralComplexToFloatingComplex: { |
| 8023 | if (!Visit(E->getSubExpr())) |
| 8024 | return false; |
| 8025 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 8026 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8027 | QualType From |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 8028 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8029 | Result.makeComplexFloat(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 8030 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 8031 | To, Result.FloatReal) && |
| 8032 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 8033 | To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 8034 | } |
| 8035 | } |
| 8036 | |
| 8037 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 8038 | } |
| 8039 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8040 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8041 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 10f4d06 | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 8042 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 8043 | |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8044 | // Track whether the LHS or RHS is real at the type system level. When this is |
| 8045 | // the case we can simplify our evaluation strategy. |
| 8046 | bool LHSReal = false, RHSReal = false; |
| 8047 | |
| 8048 | bool LHSOK; |
| 8049 | if (E->getLHS()->getType()->isRealFloatingType()) { |
| 8050 | LHSReal = true; |
| 8051 | APFloat &Real = Result.FloatReal; |
| 8052 | LHSOK = EvaluateFloat(E->getLHS(), Real, Info); |
| 8053 | if (LHSOK) { |
| 8054 | Result.makeComplexFloat(); |
| 8055 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 8056 | } |
| 8057 | } else { |
| 8058 | LHSOK = Visit(E->getLHS()); |
| 8059 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8060 | if (!LHSOK && !Info.keepEvaluatingAfterFailure()) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8061 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8062 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8063 | ComplexValue RHS; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8064 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 8065 | RHSReal = true; |
| 8066 | APFloat &Real = RHS.FloatReal; |
| 8067 | if (!EvaluateFloat(E->getRHS(), Real, Info) || !LHSOK) |
| 8068 | return false; |
| 8069 | RHS.makeComplexFloat(); |
| 8070 | RHS.FloatImag = APFloat(Real.getSemantics()); |
| 8071 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8072 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 8073 | |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8074 | assert(!(LHSReal && RHSReal) && |
| 8075 | "Cannot have both operands of a complex operation be real."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 8076 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8077 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8078 | case BO_Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 8079 | if (Result.isComplexFloat()) { |
| 8080 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 8081 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8082 | if (LHSReal) |
| 8083 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 8084 | else if (!RHSReal) |
| 8085 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 8086 | APFloat::rmNearestTiesToEven); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 8087 | } else { |
| 8088 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 8089 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 8090 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8091 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8092 | case BO_Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 8093 | if (Result.isComplexFloat()) { |
| 8094 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 8095 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8096 | if (LHSReal) { |
| 8097 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 8098 | Result.getComplexFloatImag().changeSign(); |
| 8099 | } else if (!RHSReal) { |
| 8100 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 8101 | APFloat::rmNearestTiesToEven); |
| 8102 | } |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 8103 | } else { |
| 8104 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 8105 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 8106 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8107 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8108 | case BO_Mul: |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8109 | if (Result.isComplexFloat()) { |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8110 | // This is an implementation of complex multiplication according to the |
| 8111 | // constraints laid out in C11 Annex G. The implemantion uses the |
| 8112 | // following naming scheme: |
| 8113 | // (a + ib) * (c + id) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8114 | ComplexValue LHS = Result; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8115 | APFloat &A = LHS.getComplexFloatReal(); |
| 8116 | APFloat &B = LHS.getComplexFloatImag(); |
| 8117 | APFloat &C = RHS.getComplexFloatReal(); |
| 8118 | APFloat &D = RHS.getComplexFloatImag(); |
| 8119 | APFloat &ResR = Result.getComplexFloatReal(); |
| 8120 | APFloat &ResI = Result.getComplexFloatImag(); |
| 8121 | if (LHSReal) { |
| 8122 | assert(!RHSReal && "Cannot have two real operands for a complex op!"); |
| 8123 | ResR = A * C; |
| 8124 | ResI = A * D; |
| 8125 | } else if (RHSReal) { |
| 8126 | ResR = C * A; |
| 8127 | ResI = C * B; |
| 8128 | } else { |
| 8129 | // In the fully general case, we need to handle NaNs and infinities |
| 8130 | // robustly. |
| 8131 | APFloat AC = A * C; |
| 8132 | APFloat BD = B * D; |
| 8133 | APFloat AD = A * D; |
| 8134 | APFloat BC = B * C; |
| 8135 | ResR = AC - BD; |
| 8136 | ResI = AD + BC; |
| 8137 | if (ResR.isNaN() && ResI.isNaN()) { |
| 8138 | bool Recalc = false; |
| 8139 | if (A.isInfinity() || B.isInfinity()) { |
| 8140 | A = APFloat::copySign( |
| 8141 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 8142 | B = APFloat::copySign( |
| 8143 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 8144 | if (C.isNaN()) |
| 8145 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 8146 | if (D.isNaN()) |
| 8147 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 8148 | Recalc = true; |
| 8149 | } |
| 8150 | if (C.isInfinity() || D.isInfinity()) { |
| 8151 | C = APFloat::copySign( |
| 8152 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 8153 | D = APFloat::copySign( |
| 8154 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 8155 | if (A.isNaN()) |
| 8156 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 8157 | if (B.isNaN()) |
| 8158 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 8159 | Recalc = true; |
| 8160 | } |
| 8161 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
| 8162 | AD.isInfinity() || BC.isInfinity())) { |
| 8163 | if (A.isNaN()) |
| 8164 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 8165 | if (B.isNaN()) |
| 8166 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 8167 | if (C.isNaN()) |
| 8168 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 8169 | if (D.isNaN()) |
| 8170 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 8171 | Recalc = true; |
| 8172 | } |
| 8173 | if (Recalc) { |
| 8174 | ResR = APFloat::getInf(A.getSemantics()) * (A * C - B * D); |
| 8175 | ResI = APFloat::getInf(A.getSemantics()) * (A * D + B * C); |
| 8176 | } |
| 8177 | } |
| 8178 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8179 | } else { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8180 | ComplexValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8181 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8182 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 8183 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8184 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 8185 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 8186 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 8187 | } |
| 8188 | break; |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8189 | case BO_Div: |
| 8190 | if (Result.isComplexFloat()) { |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8191 | // This is an implementation of complex division according to the |
| 8192 | // constraints laid out in C11 Annex G. The implemantion uses the |
| 8193 | // following naming scheme: |
| 8194 | // (a + ib) / (c + id) |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8195 | ComplexValue LHS = Result; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 8196 | APFloat &A = LHS.getComplexFloatReal(); |
| 8197 | APFloat &B = LHS.getComplexFloatImag(); |
| 8198 | APFloat &C = RHS.getComplexFloatReal(); |
| 8199 | APFloat &D = RHS.getComplexFloatImag(); |
| 8200 | APFloat &ResR = Result.getComplexFloatReal(); |
| 8201 | APFloat &ResI = Result.getComplexFloatImag(); |
| 8202 | if (RHSReal) { |
| 8203 | ResR = A / C; |
| 8204 | ResI = B / C; |
| 8205 | } else { |
| 8206 | if (LHSReal) { |
| 8207 | // No real optimizations we can do here, stub out with zero. |
| 8208 | B = APFloat::getZero(A.getSemantics()); |
| 8209 | } |
| 8210 | int DenomLogB = 0; |
| 8211 | APFloat MaxCD = maxnum(abs(C), abs(D)); |
| 8212 | if (MaxCD.isFinite()) { |
| 8213 | DenomLogB = ilogb(MaxCD); |
| 8214 | C = scalbn(C, -DenomLogB); |
| 8215 | D = scalbn(D, -DenomLogB); |
| 8216 | } |
| 8217 | APFloat Denom = C * C + D * D; |
| 8218 | ResR = scalbn((A * C + B * D) / Denom, -DenomLogB); |
| 8219 | ResI = scalbn((B * C - A * D) / Denom, -DenomLogB); |
| 8220 | if (ResR.isNaN() && ResI.isNaN()) { |
| 8221 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
| 8222 | ResR = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * A; |
| 8223 | ResI = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * B; |
| 8224 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
| 8225 | D.isFinite()) { |
| 8226 | A = APFloat::copySign( |
| 8227 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 8228 | B = APFloat::copySign( |
| 8229 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 8230 | ResR = APFloat::getInf(ResR.getSemantics()) * (A * C + B * D); |
| 8231 | ResI = APFloat::getInf(ResI.getSemantics()) * (B * C - A * D); |
| 8232 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
| 8233 | C = APFloat::copySign( |
| 8234 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 8235 | D = APFloat::copySign( |
| 8236 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 8237 | ResR = APFloat::getZero(ResR.getSemantics()) * (A * C + B * D); |
| 8238 | ResI = APFloat::getZero(ResI.getSemantics()) * (B * C - A * D); |
| 8239 | } |
| 8240 | } |
| 8241 | } |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8242 | } else { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8243 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 8244 | return Error(E, diag::note_expr_divide_by_zero); |
| 8245 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8246 | ComplexValue LHS = Result; |
| 8247 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 8248 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 8249 | Result.getComplexIntReal() = |
| 8250 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 8251 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 8252 | Result.getComplexIntImag() = |
| 8253 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 8254 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 8255 | } |
| 8256 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 8257 | } |
| 8258 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8259 | return true; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 8260 | } |
| 8261 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8262 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 8263 | // Get the operand value into 'Result'. |
| 8264 | if (!Visit(E->getSubExpr())) |
| 8265 | return false; |
| 8266 | |
| 8267 | switch (E->getOpcode()) { |
| 8268 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8269 | return Error(E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 8270 | case UO_Extension: |
| 8271 | return true; |
| 8272 | case UO_Plus: |
| 8273 | // The result is always just the subexpr. |
| 8274 | return true; |
| 8275 | case UO_Minus: |
| 8276 | if (Result.isComplexFloat()) { |
| 8277 | Result.getComplexFloatReal().changeSign(); |
| 8278 | Result.getComplexFloatImag().changeSign(); |
| 8279 | } |
| 8280 | else { |
| 8281 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 8282 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 8283 | } |
| 8284 | return true; |
| 8285 | case UO_Not: |
| 8286 | if (Result.isComplexFloat()) |
| 8287 | Result.getComplexFloatImag().changeSign(); |
| 8288 | else |
| 8289 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 8290 | return true; |
| 8291 | } |
| 8292 | } |
| 8293 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 8294 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 8295 | if (E->getNumInits() == 2) { |
| 8296 | if (E->getType()->isComplexType()) { |
| 8297 | Result.makeComplexFloat(); |
| 8298 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 8299 | return false; |
| 8300 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 8301 | return false; |
| 8302 | } else { |
| 8303 | Result.makeComplexInt(); |
| 8304 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 8305 | return false; |
| 8306 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 8307 | return false; |
| 8308 | } |
| 8309 | return true; |
| 8310 | } |
| 8311 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 8312 | } |
| 8313 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 8314 | //===----------------------------------------------------------------------===// |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8315 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
| 8316 | // implicit conversion. |
| 8317 | //===----------------------------------------------------------------------===// |
| 8318 | |
| 8319 | namespace { |
| 8320 | class AtomicExprEvaluator : |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 8321 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8322 | APValue &Result; |
| 8323 | public: |
| 8324 | AtomicExprEvaluator(EvalInfo &Info, APValue &Result) |
| 8325 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 8326 | |
| 8327 | bool Success(const APValue &V, const Expr *E) { |
| 8328 | Result = V; |
| 8329 | return true; |
| 8330 | } |
| 8331 | |
| 8332 | bool ZeroInitialization(const Expr *E) { |
| 8333 | ImplicitValueInitExpr VIE( |
| 8334 | E->getType()->castAs<AtomicType>()->getValueType()); |
| 8335 | return Evaluate(Result, Info, &VIE); |
| 8336 | } |
| 8337 | |
| 8338 | bool VisitCastExpr(const CastExpr *E) { |
| 8339 | switch (E->getCastKind()) { |
| 8340 | default: |
| 8341 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 8342 | case CK_NonAtomicToAtomic: |
| 8343 | return Evaluate(Result, Info, E->getSubExpr()); |
| 8344 | } |
| 8345 | } |
| 8346 | }; |
| 8347 | } // end anonymous namespace |
| 8348 | |
| 8349 | static bool EvaluateAtomic(const Expr *E, APValue &Result, EvalInfo &Info) { |
| 8350 | assert(E->isRValue() && E->getType()->isAtomicType()); |
| 8351 | return AtomicExprEvaluator(Info, Result).Visit(E); |
| 8352 | } |
| 8353 | |
| 8354 | //===----------------------------------------------------------------------===// |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 8355 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 8356 | // comma operator |
| 8357 | //===----------------------------------------------------------------------===// |
| 8358 | |
| 8359 | namespace { |
| 8360 | class VoidExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 8361 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 8362 | public: |
| 8363 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 8364 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8365 | bool Success(const APValue &V, const Expr *e) { return true; } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 8366 | |
| 8367 | bool VisitCastExpr(const CastExpr *E) { |
| 8368 | switch (E->getCastKind()) { |
| 8369 | default: |
| 8370 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 8371 | case CK_ToVoid: |
| 8372 | VisitIgnoredValue(E->getSubExpr()); |
| 8373 | return true; |
| 8374 | } |
| 8375 | } |
Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 8376 | |
| 8377 | bool VisitCallExpr(const CallExpr *E) { |
| 8378 | switch (E->getBuiltinCallee()) { |
| 8379 | default: |
| 8380 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 8381 | case Builtin::BI__assume: |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 8382 | case Builtin::BI__builtin_assume: |
Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 8383 | // The argument is not evaluated! |
| 8384 | return true; |
| 8385 | } |
| 8386 | } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 8387 | }; |
| 8388 | } // end anonymous namespace |
| 8389 | |
| 8390 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 8391 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 8392 | return VoidExprEvaluator(Info).Visit(E); |
| 8393 | } |
| 8394 | |
| 8395 | //===----------------------------------------------------------------------===// |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8396 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 8397 | //===----------------------------------------------------------------------===// |
| 8398 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8399 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8400 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 8401 | // are. |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8402 | QualType T = E->getType(); |
| 8403 | if (E->isGLValue() || T->isFunctionType()) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8404 | LValue LV; |
| 8405 | if (!EvaluateLValue(E, LV, Info)) |
| 8406 | return false; |
| 8407 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8408 | } else if (T->isVectorType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 8409 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 8410 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8411 | } else if (T->isIntegralOrEnumerationType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 8412 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 8413 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8414 | } else if (T->hasPointerRepresentation()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8415 | LValue LV; |
| 8416 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 8417 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 8418 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8419 | } else if (T->isRealFloatingType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8420 | llvm::APFloat F(0.0); |
| 8421 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 8422 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8423 | Result = APValue(F); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8424 | } else if (T->isAnyComplexType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8425 | ComplexValue C; |
| 8426 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 8427 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 8428 | C.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8429 | } else if (T->isMemberPointerType()) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8430 | MemberPtr P; |
| 8431 | if (!EvaluateMemberPointer(E, P, Info)) |
| 8432 | return false; |
| 8433 | P.moveInto(Result); |
| 8434 | return true; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8435 | } else if (T->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8436 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8437 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 8438 | APValue &Value = Info.CurrentCall->createTemporary(E, false); |
| 8439 | if (!EvaluateArray(E, LV, Value, Info)) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 8440 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 8441 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8442 | } else if (T->isRecordType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8443 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8444 | LV.set(E, Info.CurrentCall->Index); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 8445 | APValue &Value = Info.CurrentCall->createTemporary(E, false); |
| 8446 | if (!EvaluateRecord(E, LV, Value, Info)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8447 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 8448 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8449 | } else if (T->isVoidType()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8450 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 8451 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 8452 | << E->getType(); |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 8453 | if (!EvaluateVoid(E, Info)) |
| 8454 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 8455 | } else if (T->isAtomicType()) { |
| 8456 | if (!EvaluateAtomic(E, Result, Info)) |
| 8457 | return false; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8458 | } else if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 8459 | Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 8460 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8461 | } else { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 8462 | Info.Diag(E, diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 8463 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8464 | } |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 8465 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 8466 | return true; |
| 8467 | } |
| 8468 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8469 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 8470 | /// cases, the in-place evaluation is essential, since later initializers for |
| 8471 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 8472 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8473 | const Expr *E, bool AllowNonLiteralTypes) { |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 8474 | assert(!E->isValueDependent()); |
| 8475 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8476 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8477 | return false; |
| 8478 | |
| 8479 | if (E->isRValue()) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 8480 | // Evaluate arrays and record types in-place, so that later initializers can |
| 8481 | // refer to earlier-initialized members of the object. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8482 | if (E->getType()->isArrayType()) |
| 8483 | return EvaluateArray(E, This, Result, Info); |
| 8484 | else if (E->getType()->isRecordType()) |
| 8485 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 8486 | } |
| 8487 | |
| 8488 | // For any other type, in-place evaluation is unimportant. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8489 | return Evaluate(Result, Info, E); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 8490 | } |
| 8491 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8492 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 8493 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 8494 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 8495 | if (E->getType().isNull()) |
| 8496 | return false; |
| 8497 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8498 | if (!CheckLiteralType(Info, E)) |
| 8499 | return false; |
| 8500 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8501 | if (!::Evaluate(Result, Info, E)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8502 | return false; |
| 8503 | |
| 8504 | if (E->isGLValue()) { |
| 8505 | LValue LV; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8506 | LV.setFrom(Info.Ctx, Result); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 8507 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8508 | return false; |
| 8509 | } |
| 8510 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8511 | // Check this core constant expression is a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8512 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8513 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8514 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8515 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
| 8516 | const ASTContext &Ctx, bool &IsConst) { |
| 8517 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 8518 | // containing vast quantities of these. |
| 8519 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
| 8520 | Result.Val = APValue(APSInt(L->getValue(), |
| 8521 | L->getType()->isUnsignedIntegerType())); |
| 8522 | IsConst = true; |
| 8523 | return true; |
| 8524 | } |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 8525 | |
| 8526 | // This case should be rare, but we need to check it before we check on |
| 8527 | // the type below. |
| 8528 | if (Exp->getType().isNull()) { |
| 8529 | IsConst = false; |
| 8530 | return true; |
| 8531 | } |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8532 | |
| 8533 | // FIXME: Evaluating values of large array and record types can cause |
| 8534 | // performance problems. Only do so in C++11 for now. |
| 8535 | if (Exp->isRValue() && (Exp->getType()->isArrayType() || |
| 8536 | Exp->getType()->isRecordType()) && |
| 8537 | !Ctx.getLangOpts().CPlusPlus11) { |
| 8538 | IsConst = false; |
| 8539 | return true; |
| 8540 | } |
| 8541 | return false; |
| 8542 | } |
| 8543 | |
| 8544 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8545 | /// EvaluateAsRValue - Return true if this is a constant which we can fold using |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8546 | /// any crazy technique (that has nothing to do with language standards) that |
| 8547 | /// we want to. If this function returns true, it returns the folded constant |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8548 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 8549 | /// will be applied to the result. |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8550 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8551 | bool IsConst; |
| 8552 | if (FastEvaluateAsRValue(this, Result, Ctx, IsConst)) |
| 8553 | return IsConst; |
| 8554 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 8555 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8556 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 8557 | } |
| 8558 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8559 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 8560 | const ASTContext &Ctx) const { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8561 | EvalResult Scratch; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8562 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8563 | HandleConversionToBool(Scratch.Val, Result); |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 8564 | } |
| 8565 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 8566 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 8567 | SideEffectsKind AllowSideEffects) const { |
| 8568 | if (!getType()->isIntegralOrEnumerationType()) |
| 8569 | return false; |
| 8570 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8571 | EvalResult ExprResult; |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 8572 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
| 8573 | (!AllowSideEffects && ExprResult.HasSideEffects)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8574 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8575 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8576 | Result = ExprResult.Val.getInt(); |
| 8577 | return true; |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8578 | } |
| 8579 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8580 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 8581 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 8582 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8583 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8584 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 8585 | !CheckLValueConstantExpression(Info, getExprLoc(), |
| 8586 | Ctx.getLValueReferenceType(getType()), LV)) |
| 8587 | return false; |
| 8588 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8589 | LV.moveInto(Result.Val); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8590 | return true; |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 8591 | } |
| 8592 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8593 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 8594 | const VarDecl *VD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8595 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 8596 | // FIXME: Evaluating initializers for large array and record types can cause |
| 8597 | // performance problems. Only do so in C++11 for now. |
| 8598 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8599 | !Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 8600 | return false; |
| 8601 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8602 | Expr::EvalStatus EStatus; |
| 8603 | EStatus.Diag = &Notes; |
| 8604 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 8605 | EvalInfo InitInfo(Ctx, EStatus, EvalInfo::EM_ConstantFold); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8606 | InitInfo.setEvaluatingDecl(VD, Value); |
| 8607 | |
| 8608 | LValue LVal; |
| 8609 | LVal.set(VD); |
| 8610 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8611 | // C++11 [basic.start.init]p2: |
| 8612 | // Variables with static storage duration or thread storage duration shall be |
| 8613 | // zero-initialized before any other initialization takes place. |
| 8614 | // This behavior is not present in C. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8615 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8616 | !VD->getType()->isReferenceType()) { |
| 8617 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8618 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8619 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8620 | return false; |
| 8621 | } |
| 8622 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 8623 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, |
| 8624 | /*AllowNonLiteralTypes=*/true) || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8625 | EStatus.HasSideEffects) |
| 8626 | return false; |
| 8627 | |
| 8628 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 8629 | Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8630 | } |
| 8631 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8632 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 8633 | /// constant folded, but discard the result. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8634 | bool Expr::isEvaluatable(const ASTContext &Ctx) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 8635 | EvalResult Result; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8636 | return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects; |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 8637 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8638 | |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 8639 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 8640 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 8641 | EvalResult EvalResult; |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 8642 | EvalResult.Diag = Diag; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8643 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 8644 | (void)Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8645 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 8646 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8647 | |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 8648 | return EvalResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 8649 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8650 | |
Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 8651 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8652 | bool IsConst; |
| 8653 | EvalResult EvalResult; |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8654 | if (!FastEvaluateAsRValue(this, EvalResult, Ctx, IsConst)) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 8655 | EvalInfo Info(Ctx, EvalResult, EvalInfo::EM_EvaluateForOverflow); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 8656 | (void)::EvaluateAsRValue(Info, this, EvalResult.Val); |
| 8657 | } |
| 8658 | } |
| 8659 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 8660 | bool Expr::EvalResult::isGlobalLValue() const { |
| 8661 | assert(Val.isLValue()); |
| 8662 | return IsGlobalLValue(Val.getLValueBase()); |
| 8663 | } |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 8664 | |
| 8665 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8666 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 8667 | /// an integer constant expression. |
| 8668 | |
| 8669 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 8670 | /// comma, etc |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8671 | |
| 8672 | // CheckICE - This function does the fundamental ICE checking: the returned |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8673 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
| 8674 | // and a (possibly null) SourceLocation indicating the location of the problem. |
| 8675 | // |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8676 | // Note that to reduce code duplication, this helper does no evaluation |
| 8677 | // itself; the caller checks whether the expression is evaluatable, and |
| 8678 | // in the rare cases where CheckICE actually cares about the evaluated |
| 8679 | // value, it calls into Evalute. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8680 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 8681 | namespace { |
| 8682 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8683 | enum ICEKind { |
| 8684 | /// This expression is an ICE. |
| 8685 | IK_ICE, |
| 8686 | /// This expression is not an ICE, but if it isn't evaluated, it's |
| 8687 | /// a legal subexpression for an ICE. This return value is used to handle |
| 8688 | /// the comma operator in C99 mode, and non-constant subexpressions. |
| 8689 | IK_ICEIfUnevaluated, |
| 8690 | /// This expression is not an ICE, and is not a legal subexpression for one. |
| 8691 | IK_NotICE |
| 8692 | }; |
| 8693 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8694 | struct ICEDiag { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8695 | ICEKind Kind; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8696 | SourceLocation Loc; |
| 8697 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8698 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8699 | }; |
| 8700 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8701 | } |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 8702 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8703 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
| 8704 | |
| 8705 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8706 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8707 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8708 | Expr::EvalResult EVResult; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8709 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8710 | !EVResult.Val.isInt()) |
| 8711 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 8712 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8713 | return NoDiag(); |
| 8714 | } |
| 8715 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 8716 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8717 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8718 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 8719 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8720 | |
| 8721 | switch (E->getStmtClass()) { |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 8722 | #define ABSTRACT_STMT(Node) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8723 | #define STMT(Node, Base) case Expr::Node##Class: |
| 8724 | #define EXPR(Node, Base) |
| 8725 | #include "clang/AST/StmtNodes.inc" |
| 8726 | case Expr::PredefinedExprClass: |
| 8727 | case Expr::FloatingLiteralClass: |
| 8728 | case Expr::ImaginaryLiteralClass: |
| 8729 | case Expr::StringLiteralClass: |
| 8730 | case Expr::ArraySubscriptExprClass: |
| 8731 | case Expr::MemberExprClass: |
| 8732 | case Expr::CompoundAssignOperatorClass: |
| 8733 | case Expr::CompoundLiteralExprClass: |
| 8734 | case Expr::ExtVectorElementExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8735 | case Expr::DesignatedInitExprClass: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 8736 | case Expr::NoInitExprClass: |
| 8737 | case Expr::DesignatedInitUpdateExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8738 | case Expr::ImplicitValueInitExprClass: |
| 8739 | case Expr::ParenListExprClass: |
| 8740 | case Expr::VAArgExprClass: |
| 8741 | case Expr::AddrLabelExprClass: |
| 8742 | case Expr::StmtExprClass: |
| 8743 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 8744 | case Expr::CUDAKernelCallExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8745 | case Expr::CXXDynamicCastExprClass: |
| 8746 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 5cc0a67 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 8747 | case Expr::CXXUuidofExprClass: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 8748 | case Expr::MSPropertyRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8749 | case Expr::CXXNullPtrLiteralExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 8750 | case Expr::UserDefinedLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8751 | case Expr::CXXThisExprClass: |
| 8752 | case Expr::CXXThrowExprClass: |
| 8753 | case Expr::CXXNewExprClass: |
| 8754 | case Expr::CXXDeleteExprClass: |
| 8755 | case Expr::CXXPseudoDestructorExprClass: |
| 8756 | case Expr::UnresolvedLookupExprClass: |
Kaelyn Takata | e1f49d5 | 2014-10-27 18:07:20 +0000 | [diff] [blame] | 8757 | case Expr::TypoExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8758 | case Expr::DependentScopeDeclRefExprClass: |
| 8759 | case Expr::CXXConstructExprClass: |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8760 | case Expr::CXXStdInitializerListExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8761 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 8762 | case Expr::ExprWithCleanupsClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8763 | case Expr::CXXTemporaryObjectExprClass: |
| 8764 | case Expr::CXXUnresolvedConstructExprClass: |
| 8765 | case Expr::CXXDependentScopeMemberExprClass: |
| 8766 | case Expr::UnresolvedMemberExprClass: |
| 8767 | case Expr::ObjCStringLiteralClass: |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 8768 | case Expr::ObjCBoxedExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 8769 | case Expr::ObjCArrayLiteralClass: |
| 8770 | case Expr::ObjCDictionaryLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8771 | case Expr::ObjCEncodeExprClass: |
| 8772 | case Expr::ObjCMessageExprClass: |
| 8773 | case Expr::ObjCSelectorExprClass: |
| 8774 | case Expr::ObjCProtocolExprClass: |
| 8775 | case Expr::ObjCIvarRefExprClass: |
| 8776 | case Expr::ObjCPropertyRefExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 8777 | case Expr::ObjCSubscriptRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8778 | case Expr::ObjCIsaExprClass: |
| 8779 | case Expr::ShuffleVectorExprClass: |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 8780 | case Expr::ConvertVectorExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8781 | case Expr::BlockExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8782 | case Expr::NoStmtClass: |
John McCall | 8d69a21 | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 8783 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 8784 | case Expr::PackExpansionExprClass: |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 8785 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 8786 | case Expr::FunctionParmPackExprClass: |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 8787 | case Expr::AsTypeExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8788 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 8789 | case Expr::MaterializeTemporaryExprClass: |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 8790 | case Expr::PseudoObjectExprClass: |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 8791 | case Expr::AtomicExprClass: |
Douglas Gregor | e31e606 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 8792 | case Expr::LambdaExprClass: |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 8793 | case Expr::CXXFoldExprClass: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8794 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 8795 | |
Richard Smith | f137f93 | 2014-01-25 20:50:08 +0000 | [diff] [blame] | 8796 | case Expr::InitListExprClass: { |
| 8797 | // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the |
| 8798 | // form "T x = { a };" is equivalent to "T x = a;". |
| 8799 | // Unless we're initializing a reference, T is a scalar as it is known to be |
| 8800 | // of integral or enumeration type. |
| 8801 | if (E->isRValue()) |
| 8802 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
| 8803 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
| 8804 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 8805 | } |
| 8806 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 8807 | case Expr::SizeOfPackExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8808 | case Expr::GNUNullExprClass: |
| 8809 | // GCC considers the GNU __null value to be an integral constant expression. |
| 8810 | return NoDiag(); |
| 8811 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 8812 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 8813 | return |
| 8814 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 8815 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8816 | case Expr::ParenExprClass: |
| 8817 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 8818 | case Expr::GenericSelectionExprClass: |
| 8819 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8820 | case Expr::IntegerLiteralClass: |
| 8821 | case Expr::CharacterLiteralClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 8822 | case Expr::ObjCBoolLiteralExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8823 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 8824 | case Expr::CXXScalarValueInitExprClass: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 8825 | case Expr::TypeTraitExprClass: |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 8826 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 8827 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 8828 | case Expr::CXXNoexceptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8829 | return NoDiag(); |
| 8830 | case Expr::CallExprClass: |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 8831 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 8832 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 8833 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 8834 | // contain an operand of (pointer to) function type. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8835 | const CallExpr *CE = cast<CallExpr>(E); |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 8836 | if (CE->getBuiltinCallee()) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8837 | return CheckEvalInICE(E, Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8838 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8839 | } |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8840 | case Expr::DeclRefExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8841 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 8842 | return NoDiag(); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8843 | const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl()); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8844 | if (Ctx.getLangOpts().CPlusPlus && |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8845 | D && IsConstNonVolatile(D->getType())) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8846 | // Parameter variables are never constants. Without this check, |
| 8847 | // getAnyInitializer() can find a default argument, which leads |
| 8848 | // to chaos. |
| 8849 | if (isa<ParmVarDecl>(D)) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8850 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8851 | |
| 8852 | // C++ 7.1.5.1p2 |
| 8853 | // A variable of non-volatile const-qualified integral or enumeration |
| 8854 | // type initialized by an ICE can be used in ICEs. |
| 8855 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 8856 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8857 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 8858 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 8859 | const VarDecl *VD; |
| 8860 | // Look for a declaration of this variable that has an initializer, and |
| 8861 | // check whether it is an ICE. |
| 8862 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 8863 | return NoDiag(); |
| 8864 | else |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8865 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8866 | } |
| 8867 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8868 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 8869 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8870 | case Expr::UnaryOperatorClass: { |
| 8871 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 8872 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8873 | case UO_PostInc: |
| 8874 | case UO_PostDec: |
| 8875 | case UO_PreInc: |
| 8876 | case UO_PreDec: |
| 8877 | case UO_AddrOf: |
| 8878 | case UO_Deref: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 8879 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 8880 | // subexpressions of constant expressions, but they can never be ICEs |
| 8881 | // because an ICE cannot contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8882 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8883 | case UO_Extension: |
| 8884 | case UO_LNot: |
| 8885 | case UO_Plus: |
| 8886 | case UO_Minus: |
| 8887 | case UO_Not: |
| 8888 | case UO_Real: |
| 8889 | case UO_Imag: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8890 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8891 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8892 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8893 | // OffsetOf falls through here. |
| 8894 | } |
| 8895 | case Expr::OffsetOfExprClass: { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8896 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 8897 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
| 8898 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
| 8899 | // compliance: we should warn earlier for offsetof expressions with |
| 8900 | // array subscripts that aren't ICEs, and if the array subscripts |
| 8901 | // are ICEs, the value of the offsetof must be an integer constant. |
| 8902 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8903 | } |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8904 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 8905 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 8906 | if ((Exp->getKind() == UETT_SizeOf) && |
| 8907 | Exp->getTypeOfArgument()->isVariableArrayType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8908 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8909 | return NoDiag(); |
| 8910 | } |
| 8911 | case Expr::BinaryOperatorClass: { |
| 8912 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 8913 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8914 | case BO_PtrMemD: |
| 8915 | case BO_PtrMemI: |
| 8916 | case BO_Assign: |
| 8917 | case BO_MulAssign: |
| 8918 | case BO_DivAssign: |
| 8919 | case BO_RemAssign: |
| 8920 | case BO_AddAssign: |
| 8921 | case BO_SubAssign: |
| 8922 | case BO_ShlAssign: |
| 8923 | case BO_ShrAssign: |
| 8924 | case BO_AndAssign: |
| 8925 | case BO_XorAssign: |
| 8926 | case BO_OrAssign: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 8927 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 8928 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 8929 | // contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8930 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8931 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8932 | case BO_Mul: |
| 8933 | case BO_Div: |
| 8934 | case BO_Rem: |
| 8935 | case BO_Add: |
| 8936 | case BO_Sub: |
| 8937 | case BO_Shl: |
| 8938 | case BO_Shr: |
| 8939 | case BO_LT: |
| 8940 | case BO_GT: |
| 8941 | case BO_LE: |
| 8942 | case BO_GE: |
| 8943 | case BO_EQ: |
| 8944 | case BO_NE: |
| 8945 | case BO_And: |
| 8946 | case BO_Xor: |
| 8947 | case BO_Or: |
| 8948 | case BO_Comma: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8949 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 8950 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8951 | if (Exp->getOpcode() == BO_Div || |
| 8952 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 8953 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8954 | // we don't evaluate one. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8955 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8956 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8957 | if (REval == 0) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8958 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8959 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8960 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8961 | if (LEval.isMinSignedValue()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8962 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8963 | } |
| 8964 | } |
| 8965 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8966 | if (Exp->getOpcode() == BO_Comma) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8967 | if (Ctx.getLangOpts().C99) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8968 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 8969 | // if it isn't evaluated. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8970 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
| 8971 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8972 | } else { |
| 8973 | // In both C89 and C++, commas in ICEs are illegal. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8974 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8975 | } |
| 8976 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8977 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8978 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8979 | case BO_LAnd: |
| 8980 | case BO_LOr: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8981 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 8982 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8983 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8984 | // Rare case where the RHS has a comma "side-effect"; we need |
| 8985 | // to actually check the condition to see whether the side |
| 8986 | // with the comma is evaluated. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8987 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 8988 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8989 | return RHSResult; |
| 8990 | return NoDiag(); |
| 8991 | } |
| 8992 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 8993 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 8994 | } |
| 8995 | } |
| 8996 | } |
| 8997 | case Expr::ImplicitCastExprClass: |
| 8998 | case Expr::CStyleCastExprClass: |
| 8999 | case Expr::CXXFunctionalCastExprClass: |
| 9000 | case Expr::CXXStaticCastExprClass: |
| 9001 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | c3e31e7 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 9002 | case Expr::CXXConstCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9003 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9004 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 9005 | if (isa<ExplicitCastExpr>(E)) { |
| 9006 | if (const FloatingLiteral *FL |
| 9007 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 9008 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 9009 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 9010 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 9011 | bool Ignored; |
| 9012 | // If the value does not fit in the destination type, the behavior is |
| 9013 | // undefined, so we are not required to treat it as a constant |
| 9014 | // expression. |
| 9015 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 9016 | llvm::APFloat::rmTowardZero, |
| 9017 | &Ignored) & APFloat::opInvalidOp) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9018 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 9019 | return NoDiag(); |
| 9020 | } |
| 9021 | } |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 9022 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 9023 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 9024 | case CK_AtomicToNonAtomic: |
| 9025 | case CK_NonAtomicToAtomic: |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 9026 | case CK_NoOp: |
| 9027 | case CK_IntegralToBoolean: |
| 9028 | case CK_IntegralCast: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9029 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 9030 | default: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9031 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 9032 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9033 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 9034 | case Expr::BinaryConditionalOperatorClass: { |
| 9035 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 9036 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9037 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 9038 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9039 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
| 9040 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
| 9041 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
Richard Smith | 74fc721 | 2012-12-28 12:53:55 +0000 | [diff] [blame] | 9042 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 9043 | return FalseResult; |
| 9044 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9045 | case Expr::ConditionalOperatorClass: { |
| 9046 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 9047 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 9048 | // then only the true side is actually considered in an integer constant |
| 9049 | // expression, and it is fully evaluated. This is an important GNU |
| 9050 | // extension. See GCC PR38377 for discussion. |
| 9051 | if (const CallExpr *CallCE |
| 9052 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 9053 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 9054 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9055 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9056 | if (CondResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9057 | return CondResult; |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 9058 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9059 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 9060 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 9061 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9062 | if (TrueResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9063 | return TrueResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9064 | if (FalseResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9065 | return FalseResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9066 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9067 | return CondResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9068 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9069 | return NoDiag(); |
| 9070 | // Rare case where the diagnostics depend on which side is evaluated |
| 9071 | // Note that if we get here, CondResult is 0, and at least one of |
| 9072 | // TrueResult and FalseResult is non-zero. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9073 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9074 | return FalseResult; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9075 | return TrueResult; |
| 9076 | } |
| 9077 | case Expr::CXXDefaultArgExprClass: |
| 9078 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 9079 | case Expr::CXXDefaultInitExprClass: |
| 9080 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9081 | case Expr::ChooseExprClass: { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 9082 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9083 | } |
| 9084 | } |
| 9085 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 9086 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9087 | } |
| 9088 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9089 | /// Evaluate an expression as a C++11 integral constant expression. |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 9090 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9091 | const Expr *E, |
| 9092 | llvm::APSInt *Value, |
| 9093 | SourceLocation *Loc) { |
| 9094 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 9095 | if (Loc) *Loc = E->getExprLoc(); |
| 9096 | return false; |
| 9097 | } |
| 9098 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9099 | APValue Result; |
| 9100 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 9101 | return false; |
| 9102 | |
Richard Smith | 98710fc | 2014-11-13 23:03:19 +0000 | [diff] [blame] | 9103 | if (!Result.isInt()) { |
| 9104 | if (Loc) *Loc = E->getExprLoc(); |
| 9105 | return false; |
| 9106 | } |
| 9107 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9108 | if (Value) *Value = Result.getInt(); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 9109 | return true; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9110 | } |
| 9111 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 9112 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
| 9113 | SourceLocation *Loc) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9114 | if (Ctx.getLangOpts().CPlusPlus11) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9115 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr, Loc); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9116 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9117 | ICEDiag D = CheckICE(this, Ctx); |
| 9118 | if (D.Kind != IK_ICE) { |
| 9119 | if (Loc) *Loc = D.Loc; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9120 | return false; |
| 9121 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9122 | return true; |
| 9123 | } |
| 9124 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 9125 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9126 | SourceLocation *Loc, bool isEvaluated) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 9127 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9128 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 9129 | |
| 9130 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 9131 | return false; |
| 9132 | if (!EvaluateAsInt(Value, Ctx)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9133 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 9134 | return true; |
| 9135 | } |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9136 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 9137 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 9138 | return CheckICE(this, Ctx).Kind == IK_ICE; |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 9139 | } |
| 9140 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 9141 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9142 | SourceLocation *Loc) const { |
| 9143 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 9144 | // issues. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9145 | assert(Ctx.getLangOpts().CPlusPlus); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9146 | |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 9147 | // Build evaluation settings. |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9148 | Expr::EvalStatus Status; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9149 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9150 | Status.Diag = &Diags; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 9151 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 9152 | |
| 9153 | APValue Scratch; |
| 9154 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 9155 | |
| 9156 | if (!Diags.empty()) { |
| 9157 | IsConstExpr = false; |
| 9158 | if (Loc) *Loc = Diags[0].first; |
| 9159 | } else if (!IsConstExpr) { |
| 9160 | // FIXME: This shouldn't happen. |
| 9161 | if (Loc) *Loc = getExprLoc(); |
| 9162 | } |
| 9163 | |
| 9164 | return IsConstExpr; |
| 9165 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9166 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9167 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
| 9168 | const FunctionDecl *Callee, |
Craig Topper | 00bbdcf | 2014-06-28 23:22:23 +0000 | [diff] [blame] | 9169 | ArrayRef<const Expr*> Args) const { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9170 | Expr::EvalStatus Status; |
| 9171 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
| 9172 | |
| 9173 | ArgVector ArgValues(Args.size()); |
| 9174 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 9175 | I != E; ++I) { |
Nick Lewycky | f0202ca | 2014-12-16 06:12:01 +0000 | [diff] [blame] | 9176 | if ((*I)->isValueDependent() || |
| 9177 | !Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9178 | // If evaluation fails, throw away the argument entirely. |
| 9179 | ArgValues[I - Args.begin()] = APValue(); |
| 9180 | if (Info.EvalStatus.HasSideEffects) |
| 9181 | return false; |
| 9182 | } |
| 9183 | |
| 9184 | // Build fake call to Callee. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9185 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, /*This*/nullptr, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9186 | ArgValues.data()); |
| 9187 | return Evaluate(Value, Info, this) && !Info.EvalStatus.HasSideEffects; |
| 9188 | } |
| 9189 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9190 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 9191 | SmallVectorImpl< |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9192 | PartialDiagnosticAt> &Diags) { |
| 9193 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 9194 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 9195 | // ASTs which we build for dependent expressions. |
| 9196 | if (FD->isDependentContext()) |
| 9197 | return true; |
| 9198 | |
| 9199 | Expr::EvalStatus Status; |
| 9200 | Status.Diag = &Diags; |
| 9201 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 9202 | EvalInfo Info(FD->getASTContext(), Status, |
| 9203 | EvalInfo::EM_PotentialConstantExpression); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9204 | |
| 9205 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9206 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9207 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 9208 | // Fabricate an arbitrary expression on the stack and pretend that it |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9209 | // is a temporary being used as the 'this' pointer. |
| 9210 | LValue This; |
| 9211 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 9212 | This.set(&VIE, Info.CurrentCall->Index); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9213 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9214 | ArrayRef<const Expr*> Args; |
| 9215 | |
| 9216 | SourceLocation Loc = FD->getLocation(); |
| 9217 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9218 | APValue Scratch; |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 9219 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 9220 | // Evaluate the call as a constant initializer, to allow the construction |
| 9221 | // of objects of non-literal types. |
| 9222 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9223 | HandleConstructorCall(Loc, This, Args, CD, Info, Scratch); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 9224 | } else |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9225 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : nullptr, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9226 | Args, FD->getBody(), Info, Scratch); |
| 9227 | |
| 9228 | return Diags.empty(); |
| 9229 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9230 | |
| 9231 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
| 9232 | const FunctionDecl *FD, |
| 9233 | SmallVectorImpl< |
| 9234 | PartialDiagnosticAt> &Diags) { |
| 9235 | Expr::EvalStatus Status; |
| 9236 | Status.Diag = &Diags; |
| 9237 | |
| 9238 | EvalInfo Info(FD->getASTContext(), Status, |
| 9239 | EvalInfo::EM_PotentialConstantExpressionUnevaluated); |
| 9240 | |
| 9241 | // Fabricate a call stack frame to give the arguments a plausible cover story. |
| 9242 | ArrayRef<const Expr*> Args; |
| 9243 | ArgVector ArgValues(0); |
| 9244 | bool Success = EvaluateArgs(Args, ArgValues, Info); |
| 9245 | (void)Success; |
| 9246 | assert(Success && |
| 9247 | "Failed to set up arguments for potential constant evaluation"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9248 | CallStackFrame Frame(Info, SourceLocation(), FD, nullptr, ArgValues.data()); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 9249 | |
| 9250 | APValue ResultScratch; |
| 9251 | Evaluate(ResultScratch, Info, E); |
| 9252 | return Diags.empty(); |
| 9253 | } |