| 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 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Expr constant evaluator. |
| 10 | // |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11 | // Constant expression evaluation produces four main results: |
| 12 | // |
| 13 | // * A success/failure flag indicating whether constant folding was successful. |
| 14 | // This is the 'bool' return value used by most of the code in this file. A |
| 15 | // 'false' return value indicates that constant folding has failed, and any |
| 16 | // appropriate diagnostic has already been produced. |
| 17 | // |
| 18 | // * An evaluated result, valid only if constant folding has not failed. |
| 19 | // |
| 20 | // * A flag indicating if evaluation encountered (unevaluated) side-effects. |
| 21 | // These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1), |
| 22 | // where it is possible to determine the evaluated result regardless. |
| 23 | // |
| 24 | // * 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] | 25 | // (under the C++11 / C++1y rules only, at the moment), or, if folding failed |
| 26 | // too, why the expression could not be folded. |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 27 | // |
| 28 | // If we are checking for a potential constant expression, failure to constant |
| 29 | // fold a potential constant sub-expression will be indicated by a 'false' |
| 30 | // return value (the expression could not be folded) and no diagnostic (the |
| 31 | // expression is not necessarily non-constant). |
| 32 | // |
| Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 35 | #include <cstring> |
| 36 | #include <functional> |
| 37 | #include "Interp/Context.h" |
| 38 | #include "Interp/Frame.h" |
| 39 | #include "Interp/State.h" |
| Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 40 | #include "clang/AST/APValue.h" |
| 41 | #include "clang/AST/ASTContext.h" |
| Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 42 | #include "clang/AST/ASTDiagnostic.h" |
| Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 43 | #include "clang/AST/ASTLambda.h" |
| Nandor Licker | 7bd0a78 | 2019-08-29 21:57:47 +0000 | [diff] [blame] | 44 | #include "clang/AST/CXXInheritance.h" |
| Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 45 | #include "clang/AST/CharUnits.h" |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 46 | #include "clang/AST/CurrentSourceLocExprScope.h" |
| Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 47 | #include "clang/AST/Expr.h" |
| Tim Northover | 314fbfa | 2018-11-02 13:14:11 +0000 | [diff] [blame] | 48 | #include "clang/AST/OSLog.h" |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 49 | #include "clang/AST/OptionalDiagnostic.h" |
| Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 50 | #include "clang/AST/RecordLayout.h" |
| Seo Sanghyeon | 1904f44 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 51 | #include "clang/AST/StmtVisitor.h" |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 52 | #include "clang/AST/TypeLoc.h" |
| Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 53 | #include "clang/Basic/Builtins.h" |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 54 | #include "clang/Basic/FixedPoint.h" |
| Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 55 | #include "clang/Basic/TargetInfo.h" |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/Optional.h" |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/SmallBitVector.h" |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 58 | #include "llvm/Support/SaveAndRestore.h" |
| Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 59 | #include "llvm/Support/raw_ostream.h" |
| Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 60 | |
| Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 61 | #define DEBUG_TYPE "exprconstant" |
| 62 | |
| Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 63 | using namespace clang; |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 64 | using llvm::APInt; |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 65 | using llvm::APSInt; |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 66 | using llvm::APFloat; |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 67 | using llvm::Optional; |
| Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 68 | |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 69 | namespace { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 70 | struct LValue; |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 71 | class CallStackFrame; |
| 72 | class EvalInfo; |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 73 | |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 74 | using SourceLocExprScopeGuard = |
| 75 | CurrentSourceLocExprScope::SourceLocExprScopeGuard; |
| 76 | |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 77 | static QualType getType(APValue::LValueBase B) { |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 78 | if (!B) return QualType(); |
| Richard Smith | 69cf59e | 2018-03-09 02:00:01 +0000 | [diff] [blame] | 79 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 80 | // FIXME: It's unclear where we're supposed to take the type from, and |
| Richard Smith | 69cf59e | 2018-03-09 02:00:01 +0000 | [diff] [blame] | 81 | // this actually matters for arrays of unknown bound. Eg: |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 82 | // |
| 83 | // extern int arr[]; void f() { extern int arr[3]; }; |
| 84 | // constexpr int *p = &arr[1]; // valid? |
| Richard Smith | 69cf59e | 2018-03-09 02:00:01 +0000 | [diff] [blame] | 85 | // |
| 86 | // For now, we take the array bound from the most recent declaration. |
| 87 | for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl; |
| 88 | Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())) { |
| 89 | QualType T = Redecl->getType(); |
| 90 | if (!T->isIncompleteArrayType()) |
| 91 | return T; |
| 92 | } |
| 93 | return D->getType(); |
| 94 | } |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 95 | |
| Mikael Holmen | 3b6b2e3 | 2019-05-20 11:38:33 +0000 | [diff] [blame] | 96 | if (B.is<TypeInfoLValue>()) |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 97 | return B.getTypeInfoType(); |
| 98 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 99 | if (B.is<DynamicAllocLValue>()) |
| 100 | return B.getDynamicAllocType(); |
| 101 | |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 102 | const Expr *Base = B.get<const Expr*>(); |
| 103 | |
| 104 | // For a materialized temporary, the type of the temporary we materialized |
| 105 | // may not be the type of the expression. |
| 106 | if (const MaterializeTemporaryExpr *MTE = |
| 107 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 108 | SmallVector<const Expr *, 2> CommaLHSs; |
| 109 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 110 | const Expr *Temp = MTE->GetTemporaryExpr(); |
| 111 | const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs, |
| 112 | Adjustments); |
| 113 | // Keep any cv-qualifiers from the reference if we generated a temporary |
| Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 114 | // for it directly. Otherwise use the type after adjustment. |
| 115 | if (!Adjustments.empty()) |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 116 | return Inner->getType(); |
| 117 | } |
| 118 | |
| 119 | return Base->getType(); |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 122 | /// 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] | 123 | /// field declaration. |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 124 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 125 | return dyn_cast_or_null<FieldDecl>(E.getAsBaseOrMember().getPointer()); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 126 | } |
| 127 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 128 | /// base class declaration. |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 129 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 130 | return dyn_cast_or_null<CXXRecordDecl>(E.getAsBaseOrMember().getPointer()); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 131 | } |
| 132 | /// Determine whether this LValue path entry for a base class names a virtual |
| 133 | /// base class. |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 134 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 135 | return E.getAsBaseOrMember().getInt(); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 138 | /// Given an expression, determine the type used to store the result of |
| 139 | /// evaluating that expression. |
| Richard Smith | 5769440 | 2019-10-08 23:37:49 +0000 | [diff] [blame] | 140 | static QualType getStorageType(const ASTContext &Ctx, const Expr *E) { |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 141 | if (E->isRValue()) |
| 142 | return E->getType(); |
| 143 | return Ctx.getLValueReferenceType(E->getType()); |
| 144 | } |
| 145 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 146 | /// Given a CallExpr, try to get the alloc_size attribute. May return null. |
| 147 | static const AllocSizeAttr *getAllocSizeAttr(const CallExpr *CE) { |
| 148 | const FunctionDecl *Callee = CE->getDirectCallee(); |
| 149 | return Callee ? Callee->getAttr<AllocSizeAttr>() : nullptr; |
| 150 | } |
| 151 | |
| 152 | /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr. |
| 153 | /// This will look through a single cast. |
| 154 | /// |
| 155 | /// Returns null if we couldn't unwrap a function with alloc_size. |
| 156 | static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) { |
| 157 | if (!E->getType()->isPointerType()) |
| 158 | return nullptr; |
| 159 | |
| 160 | E = E->IgnoreParens(); |
| 161 | // If we're doing a variable assignment from e.g. malloc(N), there will |
| George Burgess IV | 4763876 | 2018-03-07 04:52:34 +0000 | [diff] [blame] | 162 | // probably be a cast of some kind. In exotic cases, we might also see a |
| 163 | // top-level ExprWithCleanups. Ignore them either way. |
| Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 164 | if (const auto *FE = dyn_cast<FullExpr>(E)) |
| 165 | E = FE->getSubExpr()->IgnoreParens(); |
| George Burgess IV | 4763876 | 2018-03-07 04:52:34 +0000 | [diff] [blame] | 166 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 167 | if (const auto *Cast = dyn_cast<CastExpr>(E)) |
| 168 | E = Cast->getSubExpr()->IgnoreParens(); |
| 169 | |
| 170 | if (const auto *CE = dyn_cast<CallExpr>(E)) |
| 171 | return getAllocSizeAttr(CE) ? CE : nullptr; |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
| 175 | /// Determines whether or not the given Base contains a call to a function |
| 176 | /// with the alloc_size attribute. |
| 177 | static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) { |
| 178 | const auto *E = Base.dyn_cast<const Expr *>(); |
| 179 | return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E); |
| 180 | } |
| 181 | |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 182 | /// The bound to claim that an array of unknown bound has. |
| 183 | /// The value in MostDerivedArraySize is undefined in this case. So, set it |
| 184 | /// to an arbitrary value that's likely to loudly break things if it's used. |
| 185 | static const uint64_t AssumedSizeForUnsizedArray = |
| 186 | std::numeric_limits<uint64_t>::max() / 2; |
| 187 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 188 | /// Determines if an LValue with the given LValueBase will have an unsized |
| 189 | /// array in its designator. |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 190 | /// Find the path length and type of the most-derived subobject in the given |
| 191 | /// path, and find the size of the containing array, if any. |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 192 | static unsigned |
| 193 | findMostDerivedSubobject(ASTContext &Ctx, APValue::LValueBase Base, |
| 194 | ArrayRef<APValue::LValuePathEntry> Path, |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 195 | uint64_t &ArraySize, QualType &Type, bool &IsArray, |
| 196 | bool &FirstEntryIsUnsizedArray) { |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 197 | // This only accepts LValueBases from APValues, and APValues don't support |
| 198 | // arrays that lack size info. |
| 199 | assert(!isBaseAnAllocSizeCall(Base) && |
| 200 | "Unsized arrays shouldn't appear here"); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 201 | unsigned MostDerivedLength = 0; |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 202 | Type = getType(Base); |
| 203 | |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 204 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 205 | if (Type->isArrayType()) { |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 206 | const ArrayType *AT = Ctx.getAsArrayType(Type); |
| 207 | Type = AT->getElementType(); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 208 | MostDerivedLength = I + 1; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 209 | IsArray = true; |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 210 | |
| 211 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 212 | ArraySize = CAT->getSize().getZExtValue(); |
| 213 | } else { |
| 214 | assert(I == 0 && "unexpected unsized array designator"); |
| 215 | FirstEntryIsUnsizedArray = true; |
| 216 | ArraySize = AssumedSizeForUnsizedArray; |
| 217 | } |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 218 | } else if (Type->isAnyComplexType()) { |
| 219 | const ComplexType *CT = Type->castAs<ComplexType>(); |
| 220 | Type = CT->getElementType(); |
| 221 | ArraySize = 2; |
| 222 | MostDerivedLength = I + 1; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 223 | IsArray = true; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 224 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 225 | Type = FD->getType(); |
| 226 | ArraySize = 0; |
| 227 | MostDerivedLength = I + 1; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 228 | IsArray = false; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 229 | } else { |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 230 | // Path[I] describes a base class. |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 231 | ArraySize = 0; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 232 | IsArray = false; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 233 | } |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 234 | } |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 235 | return MostDerivedLength; |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 238 | /// A path from a glvalue to a subobject of that glvalue. |
| 239 | struct SubobjectDesignator { |
| 240 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 241 | /// lvalues can still be folded, but they are not core constant expressions |
| 242 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
| Akira Hatanaka | 3a94477 | 2016-06-30 00:07:17 +0000 | [diff] [blame] | 243 | unsigned Invalid : 1; |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 244 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 245 | /// Is this a pointer one past the end of an object? |
| Akira Hatanaka | 3a94477 | 2016-06-30 00:07:17 +0000 | [diff] [blame] | 246 | unsigned IsOnePastTheEnd : 1; |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 247 | |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 248 | /// Indicator of whether the first entry is an unsized array. |
| 249 | unsigned FirstEntryIsAnUnsizedArray : 1; |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 250 | |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 251 | /// Indicator of whether the most-derived object is an array element. |
| Akira Hatanaka | 3a94477 | 2016-06-30 00:07:17 +0000 | [diff] [blame] | 252 | unsigned MostDerivedIsArrayElement : 1; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 253 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 254 | /// The length of the path to the most-derived object of which this is a |
| 255 | /// subobject. |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 256 | unsigned MostDerivedPathLength : 28; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 257 | |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 258 | /// The size of the array of which the most-derived object is an element. |
| 259 | /// This will always be 0 if the most-derived object is not an array |
| 260 | /// element. 0 is not an indicator of whether or not the most-derived object |
| 261 | /// is an array, however, because 0-length arrays are allowed. |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 262 | /// |
| 263 | /// If the current array is an unsized array, the value of this is |
| 264 | /// undefined. |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 265 | uint64_t MostDerivedArraySize; |
| 266 | |
| 267 | /// The type of the most derived object referred to by this address. |
| 268 | QualType MostDerivedType; |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 269 | |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 270 | typedef APValue::LValuePathEntry PathEntry; |
| 271 | |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 272 | /// The entries on the path from the glvalue to the designated subobject. |
| 273 | SmallVector<PathEntry, 8> Entries; |
| 274 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 275 | SubobjectDesignator() : Invalid(true) {} |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 276 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 277 | explicit SubobjectDesignator(QualType T) |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 278 | : Invalid(false), IsOnePastTheEnd(false), |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 279 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 280 | MostDerivedPathLength(0), MostDerivedArraySize(0), |
| 281 | MostDerivedType(T) {} |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 282 | |
| 283 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 284 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 285 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 286 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
| 287 | assert(V.isLValue() && "Non-LValue used to make an LValue designator?"); |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 288 | if (!Invalid) { |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 289 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 290 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 291 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 292 | if (V.getLValueBase()) { |
| 293 | bool IsArray = false; |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 294 | bool FirstIsUnsizedArray = false; |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 295 | MostDerivedPathLength = findMostDerivedSubobject( |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 296 | Ctx, V.getLValueBase(), V.getLValuePath(), MostDerivedArraySize, |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 297 | MostDerivedType, IsArray, FirstIsUnsizedArray); |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 298 | MostDerivedIsArrayElement = IsArray; |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 299 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 300 | } |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 304 | void truncate(ASTContext &Ctx, APValue::LValueBase Base, |
| 305 | unsigned NewLength) { |
| 306 | if (Invalid) |
| 307 | return; |
| 308 | |
| 309 | assert(Base && "cannot truncate path for null pointer"); |
| 310 | assert(NewLength <= Entries.size() && "not a truncation"); |
| 311 | |
| 312 | if (NewLength == Entries.size()) |
| 313 | return; |
| 314 | Entries.resize(NewLength); |
| 315 | |
| 316 | bool IsArray = false; |
| 317 | bool FirstIsUnsizedArray = false; |
| 318 | MostDerivedPathLength = findMostDerivedSubobject( |
| 319 | Ctx, Base, Entries, MostDerivedArraySize, MostDerivedType, IsArray, |
| 320 | FirstIsUnsizedArray); |
| 321 | MostDerivedIsArrayElement = IsArray; |
| 322 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
| 323 | } |
| 324 | |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 325 | void setInvalid() { |
| 326 | Invalid = true; |
| 327 | Entries.clear(); |
| 328 | } |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 329 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 330 | /// Determine whether the most derived subobject is an array without a |
| 331 | /// known bound. |
| 332 | bool isMostDerivedAnUnsizedArray() const { |
| 333 | assert(!Invalid && "Calling this makes no sense on invalid designators"); |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 334 | return Entries.size() == 1 && FirstEntryIsAnUnsizedArray; |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /// Determine what the most derived array's size is. Results in an assertion |
| 338 | /// failure if the most derived array lacks a size. |
| 339 | uint64_t getMostDerivedArraySize() const { |
| 340 | assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size"); |
| 341 | return MostDerivedArraySize; |
| 342 | } |
| 343 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 344 | /// Determine whether this is a one-past-the-end pointer. |
| 345 | bool isOnePastTheEnd() const { |
| Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 346 | assert(!Invalid); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 347 | if (IsOnePastTheEnd) |
| 348 | return true; |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 349 | if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement && |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 350 | Entries[MostDerivedPathLength - 1].getAsArrayIndex() == |
| 351 | MostDerivedArraySize) |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 352 | return true; |
| 353 | return false; |
| 354 | } |
| 355 | |
| Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 356 | /// Get the range of valid index adjustments in the form |
| 357 | /// {maximum value that can be subtracted from this pointer, |
| 358 | /// maximum value that can be added to this pointer} |
| 359 | std::pair<uint64_t, uint64_t> validIndexAdjustments() { |
| 360 | if (Invalid || isMostDerivedAnUnsizedArray()) |
| 361 | return {0, 0}; |
| 362 | |
| 363 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 364 | // nonarray object behaves the same as a pointer to the first element of |
| 365 | // an array of length one with the type of the object as its element type. |
| 366 | bool IsArray = MostDerivedPathLength == Entries.size() && |
| 367 | MostDerivedIsArrayElement; |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 368 | uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex() |
| 369 | : (uint64_t)IsOnePastTheEnd; |
| Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 370 | uint64_t ArraySize = |
| 371 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
| 372 | return {ArrayIndex, ArraySize - ArrayIndex}; |
| 373 | } |
| 374 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 375 | /// Check that this refers to a valid subobject. |
| 376 | bool isValidSubobject() const { |
| 377 | if (Invalid) |
| 378 | return false; |
| 379 | return !isOnePastTheEnd(); |
| 380 | } |
| 381 | /// Check that this refers to a valid subobject, and if not, produce a |
| 382 | /// relevant diagnostic and set the designator as invalid. |
| 383 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 384 | |
| Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 385 | /// Get the type of the designated object. |
| 386 | QualType getType(ASTContext &Ctx) const { |
| 387 | assert(!Invalid && "invalid designator has no subobject type"); |
| 388 | return MostDerivedPathLength == Entries.size() |
| 389 | ? MostDerivedType |
| 390 | : Ctx.getRecordType(getAsBaseClass(Entries.back())); |
| 391 | } |
| 392 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 393 | /// Update this designator to refer to the first element within this array. |
| 394 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 395 | Entries.push_back(PathEntry::ArrayIndex(0)); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 396 | |
| 397 | // This is a most-derived object. |
| 398 | MostDerivedType = CAT->getElementType(); |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 399 | MostDerivedIsArrayElement = true; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 400 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 401 | MostDerivedPathLength = Entries.size(); |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 402 | } |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 403 | /// Update this designator to refer to the first element within the array of |
| 404 | /// elements of type T. This is an array of unknown size. |
| 405 | void addUnsizedArrayUnchecked(QualType ElemTy) { |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 406 | Entries.push_back(PathEntry::ArrayIndex(0)); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 407 | |
| 408 | MostDerivedType = ElemTy; |
| 409 | MostDerivedIsArrayElement = true; |
| 410 | // The value in MostDerivedArraySize is undefined in this case. So, set it |
| 411 | // to an arbitrary value that's likely to loudly break things if it's |
| 412 | // used. |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 413 | MostDerivedArraySize = AssumedSizeForUnsizedArray; |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 414 | MostDerivedPathLength = Entries.size(); |
| 415 | } |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 416 | /// Update this designator to refer to the given base or member of this |
| 417 | /// object. |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 418 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 419 | Entries.push_back(APValue::BaseOrMemberType(D, Virtual)); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 420 | |
| 421 | // If this isn't a base class, it's a new most-derived object. |
| 422 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 423 | MostDerivedType = FD->getType(); |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 424 | MostDerivedIsArrayElement = false; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 425 | MostDerivedArraySize = 0; |
| 426 | MostDerivedPathLength = Entries.size(); |
| 427 | } |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 428 | } |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 429 | /// Update this designator to refer to the given complex component. |
| 430 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 431 | Entries.push_back(PathEntry::ArrayIndex(Imag)); |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 432 | |
| 433 | // This is technically a most-derived object, though in practice this |
| 434 | // is unlikely to matter. |
| 435 | MostDerivedType = EltTy; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 436 | MostDerivedIsArrayElement = true; |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 437 | MostDerivedArraySize = 2; |
| 438 | MostDerivedPathLength = Entries.size(); |
| 439 | } |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 440 | void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E); |
| Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 441 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, |
| 442 | const APSInt &N); |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 443 | /// Add N to the address of this subobject. |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 444 | void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N) { |
| 445 | if (Invalid || !N) return; |
| 446 | uint64_t TruncatedN = N.extOrTrunc(64).getZExtValue(); |
| 447 | if (isMostDerivedAnUnsizedArray()) { |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 448 | diagnoseUnsizedArrayPointerArithmetic(Info, E); |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 449 | // Can't verify -- trust that the user is doing the right thing (or if |
| 450 | // not, trust that the caller will catch the bad behavior). |
| 451 | // FIXME: Should we reject if this overflows, at least? |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 452 | Entries.back() = PathEntry::ArrayIndex( |
| 453 | Entries.back().getAsArrayIndex() + TruncatedN); |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
| 457 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 458 | // nonarray object behaves the same as a pointer to the first element of |
| 459 | // an array of length one with the type of the object as its element type. |
| 460 | bool IsArray = MostDerivedPathLength == Entries.size() && |
| 461 | MostDerivedIsArrayElement; |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 462 | uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex() |
| 463 | : (uint64_t)IsOnePastTheEnd; |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 464 | uint64_t ArraySize = |
| 465 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
| 466 | |
| 467 | if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) { |
| 468 | // Calculate the actual index in a wide enough type, so we can include |
| 469 | // it in the note. |
| 470 | N = N.extend(std::max<unsigned>(N.getBitWidth() + 1, 65)); |
| 471 | (llvm::APInt&)N += ArrayIndex; |
| 472 | assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index"); |
| 473 | diagnosePointerArithmetic(Info, E, N); |
| 474 | setInvalid(); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | ArrayIndex += TruncatedN; |
| 479 | assert(ArrayIndex <= ArraySize && |
| 480 | "bounds check succeeded for out-of-bounds index"); |
| 481 | |
| 482 | if (IsArray) |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 483 | Entries.back() = PathEntry::ArrayIndex(ArrayIndex); |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 484 | else |
| 485 | IsOnePastTheEnd = (ArrayIndex != 0); |
| 486 | } |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 487 | }; |
| 488 | |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 489 | /// A stack frame in the constexpr call stack. |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 490 | class CallStackFrame : public interp::Frame { |
| 491 | public: |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 492 | EvalInfo &Info; |
| 493 | |
| 494 | /// Parent - The caller of this stack frame. |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 495 | CallStackFrame *Caller; |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 496 | |
| Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 497 | /// Callee - The function which was called. |
| 498 | const FunctionDecl *Callee; |
| 499 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 500 | /// This - The binding for the this pointer in this call, if any. |
| 501 | const LValue *This; |
| 502 | |
| Nick Lewycky | e2b2caa | 2013-09-22 10:07:22 +0000 | [diff] [blame] | 503 | /// Arguments - Parameter bindings for this function call, indexed by |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 504 | /// parameters' function scope indices. |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 505 | APValue *Arguments; |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 506 | |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 507 | /// Source location information about the default argument or default |
| 508 | /// initializer expression we're evaluating, if any. |
| 509 | CurrentSourceLocExprScope CurSourceLocExprScope; |
| 510 | |
| Eli Friedman | 4830ec8 | 2012-06-25 21:21:08 +0000 | [diff] [blame] | 511 | // Note that we intentionally use std::map here so that references to |
| 512 | // values are stable. |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 513 | typedef std::pair<const void *, unsigned> MapKeyTy; |
| 514 | typedef std::map<MapKeyTy, APValue> MapTy; |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 515 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 516 | MapTy Temporaries; |
| 517 | |
| Alexander Shaposhnikov | fbcf29b | 2016-09-19 15:57:29 +0000 | [diff] [blame] | 518 | /// CallLoc - The location of the call expression for this call. |
| 519 | SourceLocation CallLoc; |
| 520 | |
| 521 | /// Index - The call index of this call. |
| 522 | unsigned Index; |
| 523 | |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 524 | /// The stack of integers for tracking version numbers for temporaries. |
| 525 | SmallVector<unsigned, 2> TempVersionStack = {1}; |
| 526 | unsigned CurTempVersion = TempVersionStack.back(); |
| 527 | |
| 528 | unsigned getTempVersion() const { return TempVersionStack.back(); } |
| 529 | |
| 530 | void pushTempVersion() { |
| 531 | TempVersionStack.push_back(++CurTempVersion); |
| 532 | } |
| 533 | |
| 534 | void popTempVersion() { |
| 535 | TempVersionStack.pop_back(); |
| 536 | } |
| 537 | |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 538 | // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact |
| Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 539 | // on the overall stack usage of deeply-recursing constexpr evaluations. |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 540 | // (We should cache this map rather than recomputing it repeatedly.) |
| 541 | // But let's try this and see how it goes; we can look into caching the map |
| 542 | // as a later change. |
| 543 | |
| 544 | /// LambdaCaptureFields - Mapping from captured variables/this to |
| 545 | /// corresponding data members in the closure class. |
| 546 | llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; |
| 547 | FieldDecl *LambdaThisCaptureField; |
| 548 | |
| Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 549 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 550 | const FunctionDecl *Callee, const LValue *This, |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 551 | APValue *Arguments); |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 552 | ~CallStackFrame(); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 553 | |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 554 | // Return the temporary for Key whose version number is Version. |
| 555 | APValue *getTemporary(const void *Key, unsigned Version) { |
| 556 | MapKeyTy KV(Key, Version); |
| 557 | auto LB = Temporaries.lower_bound(KV); |
| 558 | if (LB != Temporaries.end() && LB->first == KV) |
| 559 | return &LB->second; |
| 560 | // Pair (Key,Version) wasn't found in the map. Check that no elements |
| 561 | // in the map have 'Key' as their key. |
| 562 | assert((LB == Temporaries.end() || LB->first.first != Key) && |
| 563 | (LB == Temporaries.begin() || std::prev(LB)->first.first != Key) && |
| 564 | "Element with key 'Key' found in map"); |
| 565 | return nullptr; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 566 | } |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 567 | |
| 568 | // Return the current temporary for Key in the map. |
| 569 | APValue *getCurrentTemporary(const void *Key) { |
| 570 | auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX)); |
| 571 | if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key) |
| 572 | return &std::prev(UB)->second; |
| 573 | return nullptr; |
| 574 | } |
| 575 | |
| 576 | // Return the version number of the current temporary for Key. |
| 577 | unsigned getCurrentTemporaryVersion(const void *Key) const { |
| 578 | auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX)); |
| 579 | if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key) |
| 580 | return std::prev(UB)->first.second; |
| 581 | return 0; |
| 582 | } |
| 583 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 584 | /// Allocate storage for an object of type T in this stack frame. |
| 585 | /// Populates LV with a handle to the created object. Key identifies |
| 586 | /// the temporary within the stack frame, and must not be reused without |
| 587 | /// bumping the temporary version number. |
| 588 | template<typename KeyT> |
| 589 | APValue &createTemporary(const KeyT *Key, QualType T, |
| 590 | bool IsLifetimeExtended, LValue &LV); |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 591 | |
| 592 | void describe(llvm::raw_ostream &OS) override; |
| 593 | |
| 594 | Frame *getCaller() const override { return Caller; } |
| 595 | SourceLocation getCallLocation() const override { return CallLoc; } |
| 596 | const FunctionDecl *getCallee() const override { return Callee; } |
| Richard Smith | b542602 | 2019-10-03 00:39:35 +0000 | [diff] [blame] | 597 | |
| 598 | bool isStdFunction() const { |
| 599 | for (const DeclContext *DC = Callee; DC; DC = DC->getParent()) |
| 600 | if (DC->isStdNamespace()) |
| 601 | return true; |
| 602 | return false; |
| 603 | } |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 604 | }; |
| 605 | |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 606 | /// Temporarily override 'this'. |
| 607 | class ThisOverrideRAII { |
| 608 | public: |
| 609 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
| 610 | : Frame(Frame), OldThis(Frame.This) { |
| 611 | if (Enable) |
| 612 | Frame.This = NewThis; |
| 613 | } |
| 614 | ~ThisOverrideRAII() { |
| 615 | Frame.This = OldThis; |
| 616 | } |
| 617 | private: |
| 618 | CallStackFrame &Frame; |
| 619 | const LValue *OldThis; |
| 620 | }; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 621 | } |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 622 | |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 623 | static bool HandleDestruction(EvalInfo &Info, const Expr *E, |
| 624 | const LValue &This, QualType ThisType); |
| 625 | static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc, |
| 626 | APValue::LValueBase LVBase, APValue &Value, |
| 627 | QualType T); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 628 | |
| 629 | namespace { |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 630 | /// A cleanup, and a flag indicating whether it is lifetime-extended. |
| 631 | class Cleanup { |
| 632 | llvm::PointerIntPair<APValue*, 1, bool> Value; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 633 | APValue::LValueBase Base; |
| 634 | QualType T; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 635 | |
| 636 | public: |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 637 | Cleanup(APValue *Val, APValue::LValueBase Base, QualType T, |
| 638 | bool IsLifetimeExtended) |
| 639 | : Value(Val, IsLifetimeExtended), Base(Base), T(T) {} |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 640 | |
| 641 | bool isLifetimeExtended() const { return Value.getInt(); } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 642 | bool endLifetime(EvalInfo &Info, bool RunDestructors) { |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 643 | if (RunDestructors) { |
| 644 | SourceLocation Loc; |
| 645 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) |
| 646 | Loc = VD->getLocation(); |
| 647 | else if (const Expr *E = Base.dyn_cast<const Expr*>()) |
| 648 | Loc = E->getExprLoc(); |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 649 | return HandleDestruction(Info, Loc, Base, *Value.getPointer(), T); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 650 | } |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 651 | *Value.getPointer() = APValue(); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 652 | return true; |
| 653 | } |
| 654 | |
| 655 | bool hasSideEffect() { |
| 656 | return T.isDestructedType(); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 657 | } |
| 658 | }; |
| 659 | |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 660 | /// A reference to an object whose construction we are currently evaluating. |
| 661 | struct ObjectUnderConstruction { |
| 662 | APValue::LValueBase Base; |
| 663 | ArrayRef<APValue::LValuePathEntry> Path; |
| 664 | friend bool operator==(const ObjectUnderConstruction &LHS, |
| 665 | const ObjectUnderConstruction &RHS) { |
| 666 | return LHS.Base == RHS.Base && LHS.Path == RHS.Path; |
| 667 | } |
| 668 | friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) { |
| 669 | return llvm::hash_combine(Obj.Base, Obj.Path); |
| 670 | } |
| 671 | }; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 672 | enum class ConstructionPhase { |
| 673 | None, |
| 674 | Bases, |
| 675 | AfterBases, |
| 676 | Destroying, |
| 677 | DestroyingBases |
| 678 | }; |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | namespace llvm { |
| 682 | template<> struct DenseMapInfo<ObjectUnderConstruction> { |
| 683 | using Base = DenseMapInfo<APValue::LValueBase>; |
| 684 | static ObjectUnderConstruction getEmptyKey() { |
| 685 | return {Base::getEmptyKey(), {}}; } |
| 686 | static ObjectUnderConstruction getTombstoneKey() { |
| 687 | return {Base::getTombstoneKey(), {}}; |
| 688 | } |
| 689 | static unsigned getHashValue(const ObjectUnderConstruction &Object) { |
| 690 | return hash_value(Object); |
| 691 | } |
| 692 | static bool isEqual(const ObjectUnderConstruction &LHS, |
| 693 | const ObjectUnderConstruction &RHS) { |
| 694 | return LHS == RHS; |
| 695 | } |
| 696 | }; |
| 697 | } |
| 698 | |
| 699 | namespace { |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 700 | /// A dynamically-allocated heap object. |
| 701 | struct DynAlloc { |
| 702 | /// The value of this heap-allocated object. |
| 703 | APValue Value; |
| 704 | /// The allocating expression; used for diagnostics. Either a CXXNewExpr |
| 705 | /// or a CallExpr (the latter is for direct calls to operator new inside |
| 706 | /// std::allocator<T>::allocate). |
| 707 | const Expr *AllocExpr = nullptr; |
| 708 | |
| 709 | enum Kind { |
| 710 | New, |
| 711 | ArrayNew, |
| 712 | StdAllocator |
| 713 | }; |
| 714 | |
| 715 | /// Get the kind of the allocation. This must match between allocation |
| 716 | /// and deallocation. |
| 717 | Kind getKind() const { |
| 718 | if (auto *NE = dyn_cast<CXXNewExpr>(AllocExpr)) |
| 719 | return NE->isArray() ? ArrayNew : New; |
| 720 | assert(isa<CallExpr>(AllocExpr)); |
| 721 | return StdAllocator; |
| 722 | } |
| 723 | }; |
| 724 | |
| 725 | struct DynAllocOrder { |
| 726 | bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const { |
| 727 | return L.getIndex() < R.getIndex(); |
| 728 | } |
| 729 | }; |
| 730 | |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 731 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 732 | /// information about a subexpression as it is folded. It retains information |
| 733 | /// about the AST context, but also maintains information about the folded |
| 734 | /// expression. |
| 735 | /// |
| 736 | /// If an expression could be evaluated, it is still possible it is not a C |
| 737 | /// "integer constant expression" or constant expression. If not, this struct |
| 738 | /// captures information about how and why not. |
| 739 | /// |
| 740 | /// One bit of information passed *into* the request for constant folding |
| 741 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 742 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 743 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 744 | /// certain things in certain situations. |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 745 | class EvalInfo : public interp::State { |
| 746 | public: |
| Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 747 | ASTContext &Ctx; |
| Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 748 | |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 749 | /// EvalStatus - Contains information about the evaluation. |
| 750 | Expr::EvalStatus &EvalStatus; |
| 751 | |
| 752 | /// CurrentCall - The top of the constexpr call stack. |
| 753 | CallStackFrame *CurrentCall; |
| 754 | |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 755 | /// CallStackDepth - The number of calls in the call stack right now. |
| 756 | unsigned CallStackDepth; |
| 757 | |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 758 | /// NextCallIndex - The next call index to assign. |
| 759 | unsigned NextCallIndex; |
| 760 | |
| Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 761 | /// StepsLeft - The remaining number of evaluation steps we're permitted |
| 762 | /// to perform. This is essentially a limit for the number of statements |
| 763 | /// we will evaluate. |
| 764 | unsigned StepsLeft; |
| 765 | |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 766 | /// Force the use of the experimental new constant interpreter, bailing out |
| 767 | /// with an error if a feature is not supported. |
| 768 | bool ForceNewConstInterp; |
| 769 | |
| 770 | /// Enable the experimental new constant interpreter. |
| 771 | bool EnableNewConstInterp; |
| 772 | |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 773 | /// BottomFrame - The frame in which evaluation started. This must be |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 774 | /// initialized after CurrentCall and CallStackDepth. |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 775 | CallStackFrame BottomFrame; |
| 776 | |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 777 | /// A stack of values whose lifetimes end at the end of some surrounding |
| 778 | /// evaluation frame. |
| 779 | llvm::SmallVector<Cleanup, 16> CleanupStack; |
| 780 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 781 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 782 | /// evaluated, if any. |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 783 | APValue::LValueBase EvaluatingDecl; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 784 | |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 785 | enum class EvaluatingDeclKind { |
| 786 | None, |
| 787 | /// We're evaluating the construction of EvaluatingDecl. |
| 788 | Ctor, |
| 789 | /// We're evaluating the destruction of EvaluatingDecl. |
| 790 | Dtor, |
| 791 | }; |
| 792 | EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None; |
| 793 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 794 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 795 | /// declaration whose initializer is being evaluated, if any. |
| 796 | APValue *EvaluatingDeclValue; |
| 797 | |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 798 | /// Set of objects that are currently being constructed. |
| 799 | llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase> |
| 800 | ObjectsUnderConstruction; |
| Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 801 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 802 | /// Current heap allocations, along with the location where each was |
| 803 | /// allocated. We use std::map here because we need stable addresses |
| 804 | /// for the stored APValues. |
| 805 | std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs; |
| 806 | |
| 807 | /// The number of heap allocations performed so far in this evaluation. |
| 808 | unsigned NumHeapAllocs = 0; |
| 809 | |
| Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 810 | struct EvaluatingConstructorRAII { |
| 811 | EvalInfo &EI; |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 812 | ObjectUnderConstruction Object; |
| Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 813 | bool DidInsert; |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 814 | EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object, |
| 815 | bool HasBases) |
| Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 816 | : EI(EI), Object(Object) { |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 817 | DidInsert = |
| 818 | EI.ObjectsUnderConstruction |
| 819 | .insert({Object, HasBases ? ConstructionPhase::Bases |
| 820 | : ConstructionPhase::AfterBases}) |
| 821 | .second; |
| 822 | } |
| 823 | void finishedConstructingBases() { |
| 824 | EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases; |
| Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 825 | } |
| 826 | ~EvaluatingConstructorRAII() { |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 827 | if (DidInsert) EI.ObjectsUnderConstruction.erase(Object); |
| Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 828 | } |
| 829 | }; |
| 830 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 831 | struct EvaluatingDestructorRAII { |
| 832 | EvalInfo &EI; |
| 833 | ObjectUnderConstruction Object; |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 834 | bool DidInsert; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 835 | EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object) |
| 836 | : EI(EI), Object(Object) { |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 837 | DidInsert = EI.ObjectsUnderConstruction |
| 838 | .insert({Object, ConstructionPhase::Destroying}) |
| 839 | .second; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 840 | } |
| 841 | void startedDestroyingBases() { |
| 842 | EI.ObjectsUnderConstruction[Object] = |
| 843 | ConstructionPhase::DestroyingBases; |
| 844 | } |
| 845 | ~EvaluatingDestructorRAII() { |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 846 | if (DidInsert) |
| 847 | EI.ObjectsUnderConstruction.erase(Object); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 848 | } |
| 849 | }; |
| 850 | |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 851 | ConstructionPhase |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 852 | isEvaluatingCtorDtor(APValue::LValueBase Base, |
| 853 | ArrayRef<APValue::LValuePathEntry> Path) { |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 854 | return ObjectsUnderConstruction.lookup({Base, Path}); |
| Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 857 | /// If we're currently speculatively evaluating, the outermost call stack |
| 858 | /// depth at which we can mutate state, otherwise 0. |
| 859 | unsigned SpeculativeEvaluationDepth = 0; |
| 860 | |
| Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 861 | /// The current array initialization index, if we're performing array |
| 862 | /// initialization. |
| 863 | uint64_t ArrayInitIndex = -1; |
| 864 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 865 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 866 | /// notes attached to it will also be stored, otherwise they will not be. |
| 867 | bool HasActiveDiagnostic; |
| 868 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 869 | /// Have we emitted a diagnostic explaining why we couldn't constant |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 870 | /// fold (not just why it's not strictly a constant expression)? |
| 871 | bool HasFoldFailureDiagnostic; |
| 872 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 873 | /// Whether or not we're in a context where the front end requires a |
| 874 | /// constant value. |
| 875 | bool InConstantContext; |
| 876 | |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 877 | /// Whether we're checking that an expression is a potential constant |
| 878 | /// expression. If so, do not fail on constructs that could become constant |
| 879 | /// later on (such as a use of an undefined global). |
| 880 | bool CheckingPotentialConstantExpression = false; |
| 881 | |
| 882 | /// Whether we're checking for an expression that has undefined behavior. |
| 883 | /// If so, we will produce warnings if we encounter an operation that is |
| 884 | /// always undefined. |
| 885 | bool CheckingForUndefinedBehavior = false; |
| 886 | |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 887 | enum EvaluationMode { |
| 888 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 889 | /// is not a constant expression. |
| 890 | EM_ConstantExpression, |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 891 | |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 892 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 893 | /// is not a constant expression. Some expressions can be retried in the |
| 894 | /// optimizer if we don't constant fold them here, but in an unevaluated |
| 895 | /// context we try to fold them immediately since the optimizer never |
| 896 | /// gets a chance to look at it. |
| 897 | EM_ConstantExpressionUnevaluated, |
| 898 | |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 899 | /// Fold the expression to a constant. Stop if we hit a side-effect that |
| 900 | /// we can't model. |
| 901 | EM_ConstantFold, |
| 902 | |
| 903 | /// Evaluate in any way we know how. Don't worry about side-effects that |
| 904 | /// can't be modeled. |
| 905 | EM_IgnoreSideEffects, |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 906 | } EvalMode; |
| 907 | |
| 908 | /// Are we checking whether the expression is a potential constant |
| 909 | /// expression? |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 910 | bool checkingPotentialConstantExpression() const override { |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 911 | return CheckingPotentialConstantExpression; |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /// Are we checking an expression for overflow? |
| 915 | // FIXME: We should check for any kind of undefined or suspicious behavior |
| 916 | // in such constructs, not just overflow. |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 917 | bool checkingForUndefinedBehavior() const override { |
| 918 | return CheckingForUndefinedBehavior; |
| 919 | } |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 920 | |
| 921 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode) |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 922 | : Ctx(const_cast<ASTContext &>(C)), EvalStatus(S), CurrentCall(nullptr), |
| 923 | CallStackDepth(0), NextCallIndex(1), |
| 924 | StepsLeft(getLangOpts().ConstexprStepLimit), |
| 925 | ForceNewConstInterp(getLangOpts().ForceNewConstInterp), |
| 926 | EnableNewConstInterp(ForceNewConstInterp || |
| 927 | getLangOpts().EnableNewConstInterp), |
| 928 | BottomFrame(*this, SourceLocation(), nullptr, nullptr, nullptr), |
| 929 | EvaluatingDecl((const ValueDecl *)nullptr), |
| 930 | EvaluatingDeclValue(nullptr), HasActiveDiagnostic(false), |
| 931 | HasFoldFailureDiagnostic(false), InConstantContext(false), |
| 932 | EvalMode(Mode) {} |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 933 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 934 | ~EvalInfo() { |
| 935 | discardCleanups(); |
| 936 | } |
| 937 | |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 938 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value, |
| 939 | EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) { |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 940 | EvaluatingDecl = Base; |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 941 | IsEvaluatingDecl = EDK; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 942 | EvaluatingDeclValue = &Value; |
| 943 | } |
| 944 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 945 | bool CheckCallLimit(SourceLocation Loc) { |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 946 | // Don't perform any constexpr calls (other than the call we're checking) |
| 947 | // when checking a potential constant expression. |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 948 | if (checkingPotentialConstantExpression() && CallStackDepth > 1) |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 949 | return false; |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 950 | if (NextCallIndex == 0) { |
| 951 | // NextCallIndex has wrapped around. |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 952 | FFDiag(Loc, diag::note_constexpr_call_limit_exceeded); |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 953 | return false; |
| 954 | } |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 955 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 956 | return true; |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 957 | FFDiag(Loc, diag::note_constexpr_depth_limit_exceeded) |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 958 | << getLangOpts().ConstexprCallDepth; |
| 959 | return false; |
| Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 960 | } |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 961 | |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 962 | std::pair<CallStackFrame *, unsigned> |
| 963 | getCallFrameAndDepth(unsigned CallIndex) { |
| 964 | assert(CallIndex && "no call index in getCallFrameAndDepth"); |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 965 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 966 | // be null in this loop. |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 967 | unsigned Depth = CallStackDepth; |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 968 | CallStackFrame *Frame = CurrentCall; |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 969 | while (Frame->Index > CallIndex) { |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 970 | Frame = Frame->Caller; |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 971 | --Depth; |
| 972 | } |
| 973 | if (Frame->Index == CallIndex) |
| 974 | return {Frame, Depth}; |
| 975 | return {nullptr, 0}; |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 978 | bool nextStep(const Stmt *S) { |
| 979 | if (!StepsLeft) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 980 | FFDiag(S->getBeginLoc(), diag::note_constexpr_step_limit_exceeded); |
| Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 981 | return false; |
| 982 | } |
| 983 | --StepsLeft; |
| 984 | return true; |
| 985 | } |
| 986 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 987 | APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV); |
| 988 | |
| 989 | Optional<DynAlloc*> lookupDynamicAlloc(DynamicAllocLValue DA) { |
| 990 | Optional<DynAlloc*> Result; |
| 991 | auto It = HeapAllocs.find(DA); |
| 992 | if (It != HeapAllocs.end()) |
| 993 | Result = &It->second; |
| 994 | return Result; |
| 995 | } |
| 996 | |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 997 | /// Information about a stack frame for std::allocator<T>::[de]allocate. |
| 998 | struct StdAllocatorCaller { |
| 999 | unsigned FrameIndex; |
| 1000 | QualType ElemType; |
| 1001 | explicit operator bool() const { return FrameIndex != 0; }; |
| 1002 | }; |
| 1003 | |
| 1004 | StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const { |
| 1005 | for (const CallStackFrame *Call = CurrentCall; Call != &BottomFrame; |
| 1006 | Call = Call->Caller) { |
| 1007 | const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Call->Callee); |
| 1008 | if (!MD) |
| 1009 | continue; |
| 1010 | const IdentifierInfo *FnII = MD->getIdentifier(); |
| 1011 | if (!FnII || !FnII->isStr(FnName)) |
| 1012 | continue; |
| 1013 | |
| 1014 | const auto *CTSD = |
| 1015 | dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent()); |
| 1016 | if (!CTSD) |
| 1017 | continue; |
| 1018 | |
| 1019 | const IdentifierInfo *ClassII = CTSD->getIdentifier(); |
| 1020 | const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); |
| 1021 | if (CTSD->isInStdNamespace() && ClassII && |
| 1022 | ClassII->isStr("allocator") && TAL.size() >= 1 && |
| 1023 | TAL[0].getKind() == TemplateArgument::Type) |
| 1024 | return {Call->Index, TAL[0].getAsType()}; |
| 1025 | } |
| 1026 | |
| 1027 | return {}; |
| 1028 | } |
| 1029 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 1030 | void performLifetimeExtension() { |
| 1031 | // Disable the cleanups for lifetime-extended temporaries. |
| 1032 | CleanupStack.erase( |
| 1033 | std::remove_if(CleanupStack.begin(), CleanupStack.end(), |
| 1034 | [](Cleanup &C) { return C.isLifetimeExtended(); }), |
| 1035 | CleanupStack.end()); |
| 1036 | } |
| 1037 | |
| 1038 | /// Throw away any remaining cleanups at the end of evaluation. If any |
| 1039 | /// cleanups would have had a side-effect, note that as an unmodeled |
| 1040 | /// side-effect and return false. Otherwise, return true. |
| 1041 | bool discardCleanups() { |
| 1042 | for (Cleanup &C : CleanupStack) |
| 1043 | if (C.hasSideEffect()) |
| 1044 | if (!noteSideEffect()) |
| 1045 | return false; |
| 1046 | return true; |
| 1047 | } |
| 1048 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1049 | private: |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1050 | interp::Frame *getCurrentFrame() override { return CurrentCall; } |
| 1051 | const interp::Frame *getBottomFrame() const override { return &BottomFrame; } |
| 1052 | |
| 1053 | bool hasActiveDiagnostic() override { return HasActiveDiagnostic; } |
| 1054 | void setActiveDiagnostic(bool Flag) override { HasActiveDiagnostic = Flag; } |
| 1055 | |
| 1056 | void setFoldFailureDiagnostic(bool Flag) override { |
| 1057 | HasFoldFailureDiagnostic = Flag; |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1060 | Expr::EvalStatus &getEvalStatus() const override { return EvalStatus; } |
| Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1061 | |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1062 | ASTContext &getCtx() const override { return Ctx; } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1063 | |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1064 | // If we have a prior diagnostic, it will be noting that the expression |
| 1065 | // isn't a constant expression. This diagnostic is more important, |
| 1066 | // unless we require this evaluation to produce a constant expression. |
| 1067 | // |
| 1068 | // FIXME: We might want to show both diagnostics to the user in |
| 1069 | // EM_ConstantFold mode. |
| 1070 | bool hasPriorDiagnostic() override { |
| 1071 | if (!EvalStatus.Diag->empty()) { |
| 1072 | switch (EvalMode) { |
| 1073 | case EM_ConstantFold: |
| 1074 | case EM_IgnoreSideEffects: |
| 1075 | if (!HasFoldFailureDiagnostic) |
| 1076 | break; |
| 1077 | // We've already failed to fold something. Keep that diagnostic. |
| 1078 | LLVM_FALLTHROUGH; |
| 1079 | case EM_ConstantExpression: |
| 1080 | case EM_ConstantExpressionUnevaluated: |
| 1081 | setActiveDiagnostic(false); |
| 1082 | return true; |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1083 | } |
| Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1084 | } |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1085 | return false; |
| Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 1086 | } |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1087 | |
| 1088 | unsigned getCallStackDepth() override { return CallStackDepth; } |
| 1089 | |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1090 | public: |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1091 | /// Should we continue evaluation after encountering a side-effect that we |
| 1092 | /// couldn't model? |
| 1093 | bool keepEvaluatingAfterSideEffect() { |
| 1094 | switch (EvalMode) { |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1095 | case EM_IgnoreSideEffects: |
| 1096 | return true; |
| 1097 | |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1098 | case EM_ConstantExpression: |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1099 | case EM_ConstantExpressionUnevaluated: |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1100 | case EM_ConstantFold: |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 1101 | // By default, assume any side effect might be valid in some other |
| 1102 | // evaluation of this expression from a different context. |
| 1103 | return checkingPotentialConstantExpression() || |
| 1104 | checkingForUndefinedBehavior(); |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1105 | } |
| Aaron Ballman | f682f53 | 2013-11-06 18:15:02 +0000 | [diff] [blame] | 1106 | llvm_unreachable("Missed EvalMode case"); |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /// Note that we have had a side-effect, and determine whether we should |
| 1110 | /// keep evaluating. |
| 1111 | bool noteSideEffect() { |
| 1112 | EvalStatus.HasSideEffects = true; |
| 1113 | return keepEvaluatingAfterSideEffect(); |
| 1114 | } |
| 1115 | |
| Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 1116 | /// Should we continue evaluation after encountering undefined behavior? |
| 1117 | bool keepEvaluatingAfterUndefinedBehavior() { |
| 1118 | switch (EvalMode) { |
| Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 1119 | case EM_IgnoreSideEffects: |
| 1120 | case EM_ConstantFold: |
| Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 1121 | return true; |
| 1122 | |
| Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 1123 | case EM_ConstantExpression: |
| 1124 | case EM_ConstantExpressionUnevaluated: |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 1125 | return checkingForUndefinedBehavior(); |
| Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 1126 | } |
| 1127 | llvm_unreachable("Missed EvalMode case"); |
| 1128 | } |
| 1129 | |
| 1130 | /// Note that we hit something that was technically undefined behavior, but |
| 1131 | /// that we can evaluate past it (such as signed overflow or floating-point |
| 1132 | /// division by zero.) |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1133 | bool noteUndefinedBehavior() override { |
| Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 1134 | EvalStatus.HasUndefinedBehavior = true; |
| 1135 | return keepEvaluatingAfterUndefinedBehavior(); |
| 1136 | } |
| 1137 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1138 | /// Should we continue evaluation as much as possible after encountering a |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1139 | /// construct which can't be reduced to a value? |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1140 | bool keepEvaluatingAfterFailure() const override { |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1141 | if (!StepsLeft) |
| 1142 | return false; |
| 1143 | |
| 1144 | switch (EvalMode) { |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1145 | case EM_ConstantExpression: |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1146 | case EM_ConstantExpressionUnevaluated: |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1147 | case EM_ConstantFold: |
| 1148 | case EM_IgnoreSideEffects: |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 1149 | return checkingPotentialConstantExpression() || |
| 1150 | checkingForUndefinedBehavior(); |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1151 | } |
| Aaron Ballman | f682f53 | 2013-11-06 18:15:02 +0000 | [diff] [blame] | 1152 | llvm_unreachable("Missed EvalMode case"); |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1153 | } |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1154 | |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1155 | /// Notes that we failed to evaluate an expression that other expressions |
| 1156 | /// directly depend on, and determine if we should keep evaluating. This |
| 1157 | /// should only be called if we actually intend to keep evaluating. |
| 1158 | /// |
| 1159 | /// Call noteSideEffect() instead if we may be able to ignore the value that |
| 1160 | /// we failed to evaluate, e.g. if we failed to evaluate Foo() in: |
| 1161 | /// |
| 1162 | /// (Foo(), 1) // use noteSideEffect |
| 1163 | /// (Foo() || true) // use noteSideEffect |
| 1164 | /// Foo() + 1 // use noteFailure |
| Justin Bogner | fe183d7 | 2016-10-17 06:46:35 +0000 | [diff] [blame] | 1165 | LLVM_NODISCARD bool noteFailure() { |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1166 | // Failure when evaluating some expression often means there is some |
| 1167 | // subexpression whose evaluation was skipped. Therefore, (because we |
| 1168 | // don't track whether we skipped an expression when unwinding after an |
| 1169 | // evaluation failure) every evaluation failure that bubbles up from a |
| 1170 | // subexpression implies that a side-effect has potentially happened. We |
| 1171 | // skip setting the HasSideEffects flag to true until we decide to |
| 1172 | // continue evaluating after that point, which happens here. |
| 1173 | bool KeepGoing = keepEvaluatingAfterFailure(); |
| 1174 | EvalStatus.HasSideEffects |= KeepGoing; |
| 1175 | return KeepGoing; |
| 1176 | } |
| 1177 | |
| Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 1178 | class ArrayInitLoopIndex { |
| 1179 | EvalInfo &Info; |
| 1180 | uint64_t OuterIndex; |
| 1181 | |
| 1182 | public: |
| 1183 | ArrayInitLoopIndex(EvalInfo &Info) |
| 1184 | : Info(Info), OuterIndex(Info.ArrayInitIndex) { |
| 1185 | Info.ArrayInitIndex = 0; |
| 1186 | } |
| 1187 | ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; } |
| 1188 | |
| 1189 | operator uint64_t&() { return Info.ArrayInitIndex; } |
| 1190 | }; |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1191 | }; |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1192 | |
| 1193 | /// Object used to treat all foldable expressions as constant expressions. |
| 1194 | struct FoldConstant { |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1195 | EvalInfo &Info; |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1196 | bool Enabled; |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1197 | bool HadNoPriorDiags; |
| 1198 | EvalInfo::EvaluationMode OldMode; |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1199 | |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1200 | explicit FoldConstant(EvalInfo &Info, bool Enabled) |
| 1201 | : Info(Info), |
| 1202 | Enabled(Enabled), |
| 1203 | HadNoPriorDiags(Info.EvalStatus.Diag && |
| 1204 | Info.EvalStatus.Diag->empty() && |
| 1205 | !Info.EvalStatus.HasSideEffects), |
| 1206 | OldMode(Info.EvalMode) { |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 1207 | if (Enabled) |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1208 | Info.EvalMode = EvalInfo::EM_ConstantFold; |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1209 | } |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1210 | void keepDiagnostics() { Enabled = false; } |
| 1211 | ~FoldConstant() { |
| 1212 | if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() && |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1213 | !Info.EvalStatus.HasSideEffects) |
| 1214 | Info.EvalStatus.Diag->clear(); |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1215 | Info.EvalMode = OldMode; |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1216 | } |
| 1217 | }; |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1218 | |
| James Y Knight | 892b09b | 2018-10-10 02:53:43 +0000 | [diff] [blame] | 1219 | /// RAII object used to set the current evaluation mode to ignore |
| 1220 | /// side-effects. |
| 1221 | struct IgnoreSideEffectsRAII { |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1222 | EvalInfo &Info; |
| 1223 | EvalInfo::EvaluationMode OldMode; |
| James Y Knight | 892b09b | 2018-10-10 02:53:43 +0000 | [diff] [blame] | 1224 | explicit IgnoreSideEffectsRAII(EvalInfo &Info) |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1225 | : Info(Info), OldMode(Info.EvalMode) { |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 1226 | Info.EvalMode = EvalInfo::EM_IgnoreSideEffects; |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| James Y Knight | 892b09b | 2018-10-10 02:53:43 +0000 | [diff] [blame] | 1229 | ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; } |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1230 | }; |
| 1231 | |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1232 | /// RAII object used to optionally suppress diagnostics and side-effects from |
| 1233 | /// a speculative evaluation. |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1234 | class SpeculativeEvaluationRAII { |
| Chandler Carruth | bacb80d | 2017-08-16 07:22:49 +0000 | [diff] [blame] | 1235 | EvalInfo *Info = nullptr; |
| Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1236 | Expr::EvalStatus OldStatus; |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 1237 | unsigned OldSpeculativeEvaluationDepth; |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1238 | |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1239 | void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) { |
| Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1240 | Info = Other.Info; |
| 1241 | OldStatus = Other.OldStatus; |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 1242 | OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth; |
| Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1243 | Other.Info = nullptr; |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | void maybeRestoreState() { |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1247 | if (!Info) |
| 1248 | return; |
| 1249 | |
| Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1250 | Info->EvalStatus = OldStatus; |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 1251 | Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth; |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1254 | public: |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1255 | SpeculativeEvaluationRAII() = default; |
| 1256 | |
| 1257 | SpeculativeEvaluationRAII( |
| 1258 | EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr) |
| Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1259 | : Info(&Info), OldStatus(Info.EvalStatus), |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 1260 | OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) { |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1261 | Info.EvalStatus.Diag = NewDiag; |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 1262 | Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1; |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1263 | } |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1264 | |
| 1265 | SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete; |
| 1266 | SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) { |
| 1267 | moveFromAndCancel(std::move(Other)); |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1268 | } |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1269 | |
| 1270 | SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) { |
| 1271 | maybeRestoreState(); |
| 1272 | moveFromAndCancel(std::move(Other)); |
| 1273 | return *this; |
| 1274 | } |
| 1275 | |
| 1276 | ~SpeculativeEvaluationRAII() { maybeRestoreState(); } |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1277 | }; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1278 | |
| 1279 | /// RAII object wrapping a full-expression or block scope, and handling |
| 1280 | /// the ending of the lifetime of temporaries created within it. |
| 1281 | template<bool IsFullExpression> |
| 1282 | class ScopeRAII { |
| 1283 | EvalInfo &Info; |
| 1284 | unsigned OldStackSize; |
| 1285 | public: |
| 1286 | ScopeRAII(EvalInfo &Info) |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1287 | : Info(Info), OldStackSize(Info.CleanupStack.size()) { |
| 1288 | // Push a new temporary version. This is needed to distinguish between |
| 1289 | // temporaries created in different iterations of a loop. |
| 1290 | Info.CurrentCall->pushTempVersion(); |
| 1291 | } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 1292 | bool destroy(bool RunDestructors = true) { |
| 1293 | bool OK = cleanup(Info, RunDestructors, OldStackSize); |
| 1294 | OldStackSize = -1U; |
| 1295 | return OK; |
| 1296 | } |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1297 | ~ScopeRAII() { |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 1298 | if (OldStackSize != -1U) |
| 1299 | destroy(false); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1300 | // Body moved to a static method to encourage the compiler to inline away |
| 1301 | // instances of this class. |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1302 | Info.CurrentCall->popTempVersion(); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1303 | } |
| 1304 | private: |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 1305 | static bool cleanup(EvalInfo &Info, bool RunDestructors, |
| 1306 | unsigned OldStackSize) { |
| 1307 | assert(OldStackSize <= Info.CleanupStack.size() && |
| 1308 | "running cleanups out of order?"); |
| 1309 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 1310 | // Run all cleanups for a block scope, and non-lifetime-extended cleanups |
| 1311 | // for a full-expression scope. |
| Richard Smith | 4949473 | 2019-09-27 05:36:16 +0000 | [diff] [blame] | 1312 | bool Success = true; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 1313 | for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) { |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 1314 | if (!(IsFullExpression && |
| 1315 | Info.CleanupStack[I - 1].isLifetimeExtended())) { |
| Richard Smith | 4949473 | 2019-09-27 05:36:16 +0000 | [diff] [blame] | 1316 | if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) { |
| 1317 | Success = false; |
| 1318 | break; |
| 1319 | } |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1320 | } |
| 1321 | } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 1322 | |
| 1323 | // Compact lifetime-extended cleanups. |
| 1324 | auto NewEnd = Info.CleanupStack.begin() + OldStackSize; |
| 1325 | if (IsFullExpression) |
| 1326 | NewEnd = |
| 1327 | std::remove_if(NewEnd, Info.CleanupStack.end(), |
| 1328 | [](Cleanup &C) { return !C.isLifetimeExtended(); }); |
| 1329 | Info.CleanupStack.erase(NewEnd, Info.CleanupStack.end()); |
| Richard Smith | 4949473 | 2019-09-27 05:36:16 +0000 | [diff] [blame] | 1330 | return Success; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1331 | } |
| 1332 | }; |
| 1333 | typedef ScopeRAII<false> BlockScopeRAII; |
| 1334 | typedef ScopeRAII<true> FullExpressionRAII; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1335 | } |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1336 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1337 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 1338 | CheckSubobjectKind CSK) { |
| 1339 | if (Invalid) |
| 1340 | return false; |
| 1341 | if (isOnePastTheEnd()) { |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1342 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1343 | << CSK; |
| 1344 | setInvalid(); |
| 1345 | return false; |
| 1346 | } |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 1347 | // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there |
| 1348 | // must actually be at least one array element; even a VLA cannot have a |
| 1349 | // bound of zero. And if our index is nonzero, we already had a CCEDiag. |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1350 | return true; |
| 1351 | } |
| 1352 | |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 1353 | void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, |
| 1354 | const Expr *E) { |
| 1355 | Info.CCEDiag(E, diag::note_constexpr_unsized_array_indexed); |
| 1356 | // Do not set the designator as invalid: we can represent this situation, |
| 1357 | // and correct handling of __builtin_object_size requires us to do so. |
| 1358 | } |
| 1359 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1360 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
| Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 1361 | const Expr *E, |
| 1362 | const APSInt &N) { |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1363 | // If we're complaining, we must be able to statically determine the size of |
| 1364 | // the most derived array. |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 1365 | if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement) |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1366 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1367 | << N << /*array*/ 0 |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1368 | << static_cast<unsigned>(getMostDerivedArraySize()); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1369 | else |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1370 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1371 | << N << /*non-array*/ 1; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1372 | setInvalid(); |
| 1373 | } |
| 1374 | |
| Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1375 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 1376 | const FunctionDecl *Callee, const LValue *This, |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1377 | APValue *Arguments) |
| Samuel Antao | 1197a16 | 2016-09-19 18:13:13 +0000 | [diff] [blame] | 1378 | : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This), |
| 1379 | Arguments(Arguments), CallLoc(CallLoc), Index(Info.NextCallIndex++) { |
| Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1380 | Info.CurrentCall = this; |
| 1381 | ++Info.CallStackDepth; |
| 1382 | } |
| 1383 | |
| 1384 | CallStackFrame::~CallStackFrame() { |
| 1385 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 1386 | --Info.CallStackDepth; |
| 1387 | Info.CurrentCall = Caller; |
| 1388 | } |
| 1389 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 1390 | static bool isRead(AccessKinds AK) { |
| 1391 | return AK == AK_Read || AK == AK_ReadObjectRepresentation; |
| 1392 | } |
| 1393 | |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 1394 | static bool isModification(AccessKinds AK) { |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 1395 | switch (AK) { |
| 1396 | case AK_Read: |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 1397 | case AK_ReadObjectRepresentation: |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 1398 | case AK_MemberCall: |
| 1399 | case AK_DynamicCast: |
| Richard Smith | a933030 | 2019-05-17 19:19:28 +0000 | [diff] [blame] | 1400 | case AK_TypeId: |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 1401 | return false; |
| 1402 | case AK_Assign: |
| 1403 | case AK_Increment: |
| 1404 | case AK_Decrement: |
| Richard Smith | b542602 | 2019-10-03 00:39:35 +0000 | [diff] [blame] | 1405 | case AK_Construct: |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 1406 | case AK_Destroy: |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 1407 | return true; |
| 1408 | } |
| 1409 | llvm_unreachable("unknown access kind"); |
| 1410 | } |
| 1411 | |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 1412 | static bool isAnyAccess(AccessKinds AK) { |
| 1413 | return isRead(AK) || isModification(AK); |
| 1414 | } |
| 1415 | |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 1416 | /// Is this an access per the C++ definition? |
| 1417 | static bool isFormalAccess(AccessKinds AK) { |
| Richard Smith | b542602 | 2019-10-03 00:39:35 +0000 | [diff] [blame] | 1418 | return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy; |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1421 | namespace { |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1422 | struct ComplexValue { |
| 1423 | private: |
| 1424 | bool IsInt; |
| 1425 | |
| 1426 | public: |
| 1427 | APSInt IntReal, IntImag; |
| 1428 | APFloat FloatReal, FloatImag; |
| 1429 | |
| Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1430 | ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {} |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1431 | |
| 1432 | void makeComplexFloat() { IsInt = false; } |
| 1433 | bool isComplexFloat() const { return !IsInt; } |
| 1434 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 1435 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 1436 | |
| 1437 | void makeComplexInt() { IsInt = true; } |
| 1438 | bool isComplexInt() const { return IsInt; } |
| 1439 | APSInt &getComplexIntReal() { return IntReal; } |
| 1440 | APSInt &getComplexIntImag() { return IntImag; } |
| 1441 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1442 | void moveInto(APValue &v) const { |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1443 | if (isComplexFloat()) |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1444 | v = APValue(FloatReal, FloatImag); |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1445 | else |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1446 | v = APValue(IntReal, IntImag); |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1447 | } |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1448 | void setFrom(const APValue &v) { |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1449 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 1450 | if (v.isComplexFloat()) { |
| 1451 | makeComplexFloat(); |
| 1452 | FloatReal = v.getComplexFloatReal(); |
| 1453 | FloatImag = v.getComplexFloatImag(); |
| 1454 | } else { |
| 1455 | makeComplexInt(); |
| 1456 | IntReal = v.getComplexIntReal(); |
| 1457 | IntImag = v.getComplexIntImag(); |
| 1458 | } |
| 1459 | } |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1460 | }; |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1461 | |
| 1462 | struct LValue { |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1463 | APValue::LValueBase Base; |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1464 | CharUnits Offset; |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1465 | SubobjectDesignator Designator; |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1466 | bool IsNullPtr : 1; |
| 1467 | bool InvalidBase : 1; |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1468 | |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1469 | const APValue::LValueBase getLValueBase() const { return Base; } |
| Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1470 | CharUnits &getLValueOffset() { return Offset; } |
| Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 1471 | const CharUnits &getLValueOffset() const { return Offset; } |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1472 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 1473 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1474 | bool isNullPointer() const { return IsNullPtr;} |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1475 | |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1476 | unsigned getLValueCallIndex() const { return Base.getCallIndex(); } |
| 1477 | unsigned getLValueVersion() const { return Base.getVersion(); } |
| 1478 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1479 | void moveInto(APValue &V) const { |
| 1480 | if (Designator.Invalid) |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1481 | V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1482 | else { |
| 1483 | assert(!InvalidBase && "APValues can't handle invalid LValue bases"); |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1484 | V = APValue(Base, Offset, Designator.Entries, |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1485 | Designator.IsOnePastTheEnd, IsNullPtr); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1486 | } |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1487 | } |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1488 | void setFrom(ASTContext &Ctx, const APValue &V) { |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1489 | assert(V.isLValue() && "Setting LValue from a non-LValue?"); |
| Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1490 | Base = V.getLValueBase(); |
| 1491 | Offset = V.getLValueOffset(); |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1492 | InvalidBase = false; |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1493 | Designator = SubobjectDesignator(Ctx, V); |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1494 | IsNullPtr = V.isNullPointer(); |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1497 | void set(APValue::LValueBase B, bool BInvalid = false) { |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1498 | #ifndef NDEBUG |
| 1499 | // We only allow a few types of invalid bases. Enforce that here. |
| 1500 | if (BInvalid) { |
| 1501 | const auto *E = B.get<const Expr *>(); |
| 1502 | assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) && |
| 1503 | "Unexpected type of invalid base"); |
| 1504 | } |
| 1505 | #endif |
| 1506 | |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1507 | Base = B; |
| Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1508 | Offset = CharUnits::fromQuantity(0); |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1509 | InvalidBase = BInvalid; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1510 | Designator = SubobjectDesignator(getType(B)); |
| Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1511 | IsNullPtr = false; |
| 1512 | } |
| 1513 | |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 1514 | void setNull(ASTContext &Ctx, QualType PointerTy) { |
| Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1515 | Base = (Expr *)nullptr; |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 1516 | Offset = |
| 1517 | CharUnits::fromQuantity(Ctx.getTargetNullPointerValue(PointerTy)); |
| Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1518 | InvalidBase = false; |
| Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1519 | Designator = SubobjectDesignator(PointerTy->getPointeeType()); |
| 1520 | IsNullPtr = true; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1523 | void setInvalid(APValue::LValueBase B, unsigned I = 0) { |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1524 | set(B, true); |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 1527 | std::string toString(ASTContext &Ctx, QualType T) const { |
| 1528 | APValue Printable; |
| 1529 | moveInto(Printable); |
| 1530 | return Printable.getAsString(Ctx, T); |
| 1531 | } |
| 1532 | |
| Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 1533 | private: |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1534 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 1535 | // a diagnostic and mark the designator as invalid. |
| Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 1536 | template <typename GenDiagType> |
| 1537 | bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) { |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1538 | if (Designator.Invalid) |
| 1539 | return false; |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1540 | if (IsNullPtr) { |
| Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 1541 | GenDiag(); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1542 | Designator.setInvalid(); |
| 1543 | return false; |
| 1544 | } |
| 1545 | return true; |
| 1546 | } |
| 1547 | |
| Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 1548 | public: |
| 1549 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 1550 | CheckSubobjectKind CSK) { |
| 1551 | return checkNullPointerDiagnosingWith([&Info, E, CSK] { |
| 1552 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) << CSK; |
| 1553 | }); |
| 1554 | } |
| 1555 | |
| 1556 | bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E, |
| 1557 | AccessKinds AK) { |
| 1558 | return checkNullPointerDiagnosingWith([&Info, E, AK] { |
| 1559 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
| 1560 | }); |
| 1561 | } |
| 1562 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1563 | // Check this LValue refers to an object. If not, set the designator to be |
| 1564 | // invalid and emit a diagnostic. |
| 1565 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
| Richard Smith | 6c6bbfa | 2014-04-08 12:19:28 +0000 | [diff] [blame] | 1566 | return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) && |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1567 | Designator.checkSubobject(Info, E, CSK); |
| 1568 | } |
| 1569 | |
| 1570 | void addDecl(EvalInfo &Info, const Expr *E, |
| 1571 | const Decl *D, bool Virtual = false) { |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1572 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
| 1573 | Designator.addDeclUnchecked(D, Virtual); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1574 | } |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 1575 | void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) { |
| 1576 | if (!Designator.Entries.empty()) { |
| 1577 | Info.CCEDiag(E, diag::note_constexpr_unsupported_unsized_array); |
| 1578 | Designator.setInvalid(); |
| 1579 | return; |
| 1580 | } |
| Richard Smith | efdb503 | 2017-11-15 03:03:56 +0000 | [diff] [blame] | 1581 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) { |
| 1582 | assert(getType(Base)->isPointerType() || getType(Base)->isArrayType()); |
| 1583 | Designator.FirstEntryIsAnUnsizedArray = true; |
| 1584 | Designator.addUnsizedArrayUnchecked(ElemTy); |
| 1585 | } |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1586 | } |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1587 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1588 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
| 1589 | Designator.addArrayUnchecked(CAT); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1590 | } |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1591 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1592 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
| 1593 | Designator.addComplexUnchecked(EltTy, Imag); |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1594 | } |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1595 | void clearIsNullPointer() { |
| 1596 | IsNullPtr = false; |
| 1597 | } |
| Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 1598 | void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E, |
| 1599 | const APSInt &Index, CharUnits ElementSize) { |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1600 | // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB, |
| 1601 | // but we're not required to diagnose it and it's valid in C++.) |
| 1602 | if (!Index) |
| 1603 | return; |
| 1604 | |
| 1605 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
| 1606 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
| 1607 | // offsets. |
| 1608 | uint64_t Offset64 = Offset.getQuantity(); |
| 1609 | uint64_t ElemSize64 = ElementSize.getQuantity(); |
| 1610 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
| 1611 | Offset = CharUnits::fromQuantity(Offset64 + ElemSize64 * Index64); |
| 1612 | |
| 1613 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1614 | Designator.adjustIndex(Info, E, Index); |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1615 | clearIsNullPointer(); |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1616 | } |
| 1617 | void adjustOffset(CharUnits N) { |
| 1618 | Offset += N; |
| 1619 | if (N.getQuantity()) |
| 1620 | clearIsNullPointer(); |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1621 | } |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1622 | }; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1623 | |
| 1624 | struct MemberPtr { |
| 1625 | MemberPtr() {} |
| 1626 | explicit MemberPtr(const ValueDecl *Decl) : |
| 1627 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 1628 | |
| 1629 | /// The member or (direct or indirect) field referred to by this member |
| 1630 | /// pointer, or 0 if this is a null member pointer. |
| 1631 | const ValueDecl *getDecl() const { |
| 1632 | return DeclAndIsDerivedMember.getPointer(); |
| 1633 | } |
| 1634 | /// Is this actually a member of some type derived from the relevant class? |
| 1635 | bool isDerivedMember() const { |
| 1636 | return DeclAndIsDerivedMember.getInt(); |
| 1637 | } |
| 1638 | /// Get the class which the declaration actually lives in. |
| 1639 | const CXXRecordDecl *getContainingRecord() const { |
| 1640 | return cast<CXXRecordDecl>( |
| 1641 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 1642 | } |
| 1643 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1644 | void moveInto(APValue &V) const { |
| 1645 | V = APValue(getDecl(), isDerivedMember(), Path); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1646 | } |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1647 | void setFrom(const APValue &V) { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1648 | assert(V.isMemberPointer()); |
| 1649 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 1650 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 1651 | Path.clear(); |
| 1652 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 1653 | Path.insert(Path.end(), P.begin(), P.end()); |
| 1654 | } |
| 1655 | |
| 1656 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 1657 | /// whether the member is a member of some class derived from the class type |
| 1658 | /// of the member pointer. |
| 1659 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 1660 | /// Path - The path of base/derived classes from the member declaration's |
| 1661 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 1662 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 1663 | |
| 1664 | /// Perform a cast towards the class of the Decl (either up or down the |
| 1665 | /// hierarchy). |
| 1666 | bool castBack(const CXXRecordDecl *Class) { |
| 1667 | assert(!Path.empty()); |
| 1668 | const CXXRecordDecl *Expected; |
| 1669 | if (Path.size() >= 2) |
| 1670 | Expected = Path[Path.size() - 2]; |
| 1671 | else |
| 1672 | Expected = getContainingRecord(); |
| 1673 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 1674 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 1675 | // if B does not contain the original member and is not a base or |
| 1676 | // derived class of the class containing the original member, the result |
| 1677 | // of the cast is undefined. |
| 1678 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 1679 | // (D::*). We consider that to be a language defect. |
| 1680 | return false; |
| 1681 | } |
| 1682 | Path.pop_back(); |
| 1683 | return true; |
| 1684 | } |
| 1685 | /// Perform a base-to-derived member pointer cast. |
| 1686 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 1687 | if (!getDecl()) |
| 1688 | return true; |
| 1689 | if (!isDerivedMember()) { |
| 1690 | Path.push_back(Derived); |
| 1691 | return true; |
| 1692 | } |
| 1693 | if (!castBack(Derived)) |
| 1694 | return false; |
| 1695 | if (Path.empty()) |
| 1696 | DeclAndIsDerivedMember.setInt(false); |
| 1697 | return true; |
| 1698 | } |
| 1699 | /// Perform a derived-to-base member pointer cast. |
| 1700 | bool castToBase(const CXXRecordDecl *Base) { |
| 1701 | if (!getDecl()) |
| 1702 | return true; |
| 1703 | if (Path.empty()) |
| 1704 | DeclAndIsDerivedMember.setInt(true); |
| 1705 | if (isDerivedMember()) { |
| 1706 | Path.push_back(Base); |
| 1707 | return true; |
| 1708 | } |
| 1709 | return castBack(Base); |
| 1710 | } |
| 1711 | }; |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1712 | |
| Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 1713 | /// Compare two member pointers, which are assumed to be of the same type. |
| 1714 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 1715 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 1716 | return !LHS.getDecl() && !RHS.getDecl(); |
| 1717 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 1718 | return false; |
| 1719 | return LHS.Path == RHS.Path; |
| 1720 | } |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1721 | } |
| Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1722 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1723 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1724 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 1725 | const LValue &This, const Expr *E, |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1726 | bool AllowNonLiteralTypes = false); |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 1727 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
| 1728 | bool InvalidBaseOK = false); |
| 1729 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info, |
| 1730 | bool InvalidBaseOK = false); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1731 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 1732 | EvalInfo &Info); |
| 1733 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
| George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 1734 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1735 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
| Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 1736 | EvalInfo &Info); |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1737 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1738 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 1739 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
| 1740 | EvalInfo &Info); |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1741 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result); |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1742 | |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 1743 | /// Evaluate an integer or fixed point expression into an APResult. |
| 1744 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
| 1745 | EvalInfo &Info); |
| 1746 | |
| 1747 | /// Evaluate only a fixed point expression into an APResult. |
| 1748 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
| 1749 | EvalInfo &Info); |
| 1750 | |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1751 | //===----------------------------------------------------------------------===// |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1752 | // Misc utilities |
| 1753 | //===----------------------------------------------------------------------===// |
| 1754 | |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1755 | /// Negate an APSInt in place, converting it to a signed form if necessary, and |
| 1756 | /// preserving its value (by extending by up to one bit as needed). |
| 1757 | static void negateAsSigned(APSInt &Int) { |
| 1758 | if (Int.isUnsigned() || Int.isMinSignedValue()) { |
| 1759 | Int = Int.extend(Int.getBitWidth() + 1); |
| 1760 | Int.setIsSigned(true); |
| 1761 | } |
| 1762 | Int = -Int; |
| 1763 | } |
| 1764 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 1765 | template<typename KeyT> |
| 1766 | APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T, |
| 1767 | bool IsLifetimeExtended, LValue &LV) { |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 1768 | unsigned Version = getTempVersion(); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 1769 | APValue::LValueBase Base(Key, Index, Version); |
| 1770 | LV.set(Base); |
| 1771 | APValue &Result = Temporaries[MapKeyTy(Key, Version)]; |
| 1772 | assert(Result.isAbsent() && "temporary created multiple times"); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 1773 | |
| 1774 | // If we're creating a temporary immediately in the operand of a speculative |
| 1775 | // evaluation, don't register a cleanup to be run outside the speculative |
| 1776 | // evaluation context, since we won't actually be able to initialize this |
| 1777 | // object. |
| 1778 | if (Index <= Info.SpeculativeEvaluationDepth) { |
| 1779 | if (T.isDestructedType()) |
| 1780 | Info.noteSideEffect(); |
| 1781 | } else { |
| 1782 | Info.CleanupStack.push_back(Cleanup(&Result, Base, T, IsLifetimeExtended)); |
| 1783 | } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 1784 | return Result; |
| 1785 | } |
| 1786 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 1787 | APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) { |
| 1788 | if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) { |
| 1789 | FFDiag(E, diag::note_constexpr_heap_alloc_limit_exceeded); |
| 1790 | return nullptr; |
| 1791 | } |
| 1792 | |
| 1793 | DynamicAllocLValue DA(NumHeapAllocs++); |
| 1794 | LV.set(APValue::LValueBase::getDynamicAlloc(DA, T)); |
| 1795 | auto Result = HeapAllocs.emplace(std::piecewise_construct, |
| 1796 | std::forward_as_tuple(DA), std::tuple<>()); |
| 1797 | assert(Result.second && "reused a heap alloc index?"); |
| 1798 | Result.first->second.AllocExpr = E; |
| 1799 | return &Result.first->second.Value; |
| 1800 | } |
| 1801 | |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1802 | /// Produce a string describing the given constexpr call. |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1803 | void CallStackFrame::describe(raw_ostream &Out) { |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1804 | unsigned ArgIndex = 0; |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1805 | bool IsMemberCall = isa<CXXMethodDecl>(Callee) && |
| 1806 | !isa<CXXConstructorDecl>(Callee) && |
| 1807 | cast<CXXMethodDecl>(Callee)->isInstance(); |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1808 | |
| 1809 | if (!IsMemberCall) |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1810 | Out << *Callee << '('; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1811 | |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1812 | if (This && IsMemberCall) { |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1813 | APValue Val; |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1814 | This->moveInto(Val); |
| 1815 | Val.printPretty(Out, Info.Ctx, |
| 1816 | This->Designator.MostDerivedType); |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1817 | // FIXME: Add parens around Val if needed. |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1818 | Out << "->" << *Callee << '('; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1819 | IsMemberCall = false; |
| 1820 | } |
| 1821 | |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1822 | for (FunctionDecl::param_const_iterator I = Callee->param_begin(), |
| 1823 | E = Callee->param_end(); I != E; ++I, ++ArgIndex) { |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1824 | if (ArgIndex > (unsigned)IsMemberCall) |
| 1825 | Out << ", "; |
| 1826 | |
| 1827 | const ParmVarDecl *Param = *I; |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1828 | const APValue &Arg = Arguments[ArgIndex]; |
| 1829 | Arg.printPretty(Out, Info.Ctx, Param->getType()); |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1830 | |
| 1831 | if (ArgIndex == 0 && IsMemberCall) |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 1832 | Out << "->" << *Callee << '('; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | Out << ')'; |
| 1836 | } |
| 1837 | |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1838 | /// Evaluate an expression to see if it had side-effects, and discard its |
| 1839 | /// result. |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1840 | /// \return \c true if the caller should keep evaluating. |
| 1841 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1842 | APValue Scratch; |
| Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 1843 | if (!Evaluate(Scratch, Info, E)) |
| 1844 | // We don't need the value, but we might have skipped a side effect here. |
| 1845 | return Info.noteSideEffect(); |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1846 | return true; |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1849 | /// Should this call expression be treated as a string literal? |
| 1850 | static bool IsStringLiteralCall(const CallExpr *E) { |
| Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 1851 | unsigned Builtin = E->getBuiltinCallee(); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1852 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 1853 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 1854 | } |
| 1855 | |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1856 | static bool IsGlobalLValue(APValue::LValueBase B) { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1857 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 1858 | // constant expression of pointer type that evaluates to... |
| 1859 | |
| 1860 | // ... a null pointer value, or a prvalue core constant expression of type |
| 1861 | // std::nullptr_t. |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1862 | if (!B) return true; |
| John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1863 | |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1864 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 1865 | // ... the address of an object with static storage duration, |
| 1866 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 1867 | return VD->hasGlobalStorage(); |
| 1868 | // ... the address of a function, |
| 1869 | return isa<FunctionDecl>(D); |
| 1870 | } |
| 1871 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 1872 | if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>()) |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 1873 | return true; |
| 1874 | |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1875 | const Expr *E = B.get<const Expr*>(); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1876 | switch (E->getStmtClass()) { |
| 1877 | default: |
| 1878 | return false; |
| Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1879 | case Expr::CompoundLiteralExprClass: { |
| 1880 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 1881 | return CLE->isFileScope() && CLE->isLValue(); |
| 1882 | } |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1883 | case Expr::MaterializeTemporaryExprClass: |
| 1884 | // A materialized temporary might have been lifetime-extended to static |
| 1885 | // storage duration. |
| 1886 | return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1887 | // A string literal has static storage duration. |
| 1888 | case Expr::StringLiteralClass: |
| 1889 | case Expr::PredefinedExprClass: |
| 1890 | case Expr::ObjCStringLiteralClass: |
| 1891 | case Expr::ObjCEncodeExprClass: |
| Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 1892 | case Expr::CXXUuidofExprClass: |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1893 | return true; |
| Akira Hatanaka | 1488ee4 | 2019-03-08 04:45:37 +0000 | [diff] [blame] | 1894 | case Expr::ObjCBoxedExprClass: |
| 1895 | return cast<ObjCBoxedExpr>(E)->isExpressibleAsConstantInitializer(); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1896 | case Expr::CallExprClass: |
| 1897 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 1898 | // For GCC compatibility, &&label has static storage duration. |
| 1899 | case Expr::AddrLabelExprClass: |
| 1900 | return true; |
| 1901 | // A Block literal expression may be used as the initialization value for |
| 1902 | // Block variables at global or local static scope. |
| 1903 | case Expr::BlockExprClass: |
| 1904 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1905 | case Expr::ImplicitValueInitExprClass: |
| 1906 | // FIXME: |
| 1907 | // We can never form an lvalue with an implicit value initialization as its |
| 1908 | // base through expression evaluation, so these only appear in one case: the |
| 1909 | // implicit variable declaration we invent when checking whether a constexpr |
| 1910 | // constructor can produce a constant expression. We must assume that such |
| 1911 | // an expression might be a global lvalue. |
| 1912 | return true; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1913 | } |
| John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 1916 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
| 1917 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
| 1918 | } |
| 1919 | |
| 1920 | static bool IsLiteralLValue(const LValue &Value) { |
| 1921 | if (Value.getLValueCallIndex()) |
| 1922 | return false; |
| 1923 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
| 1924 | return E && !isa<MaterializeTemporaryExpr>(E); |
| 1925 | } |
| 1926 | |
| 1927 | static bool IsWeakLValue(const LValue &Value) { |
| 1928 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
| 1929 | return Decl && Decl->isWeak(); |
| 1930 | } |
| 1931 | |
| 1932 | static bool isZeroSized(const LValue &Value) { |
| 1933 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
| 1934 | if (Decl && isa<VarDecl>(Decl)) { |
| 1935 | QualType Ty = Decl->getType(); |
| 1936 | if (Ty->isArrayType()) |
| 1937 | return Ty->isIncompleteType() || |
| 1938 | Decl->getASTContext().getTypeSize(Ty) == 0; |
| 1939 | } |
| 1940 | return false; |
| 1941 | } |
| 1942 | |
| 1943 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 1944 | if (!A.getLValueBase()) |
| 1945 | return !B.getLValueBase(); |
| 1946 | if (!B.getLValueBase()) |
| 1947 | return false; |
| 1948 | |
| 1949 | if (A.getLValueBase().getOpaqueValue() != |
| 1950 | B.getLValueBase().getOpaqueValue()) { |
| 1951 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 1952 | if (!ADecl) |
| 1953 | return false; |
| 1954 | const Decl *BDecl = GetLValueBaseDecl(B); |
| 1955 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
| 1956 | return false; |
| 1957 | } |
| 1958 | |
| 1959 | return IsGlobalLValue(A.getLValueBase()) || |
| 1960 | (A.getLValueCallIndex() == B.getLValueCallIndex() && |
| 1961 | A.getLValueVersion() == B.getLValueVersion()); |
| 1962 | } |
| 1963 | |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1964 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 1965 | assert(Base && "no location for a null lvalue"); |
| 1966 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 1967 | if (VD) |
| 1968 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 1969 | else if (const Expr *E = Base.dyn_cast<const Expr*>()) |
| 1970 | Info.Note(E->getExprLoc(), diag::note_constexpr_temporary_here); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 1971 | else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { |
| 1972 | // FIXME: Produce a note for dangling pointers too. |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 1973 | if (Optional<DynAlloc*> Alloc = Info.lookupDynamicAlloc(DA)) |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 1974 | Info.Note((*Alloc)->AllocExpr->getExprLoc(), |
| 1975 | diag::note_constexpr_dynamic_alloc_here); |
| 1976 | } |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 1977 | // We have no information to show for a typeid(T) object. |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 1980 | enum class CheckEvaluationResultKind { |
| 1981 | ConstantExpression, |
| 1982 | FullyInitialized, |
| 1983 | }; |
| 1984 | |
| 1985 | /// Materialized temporaries that we've already checked to determine if they're |
| 1986 | /// initializsed by a constant expression. |
| 1987 | using CheckedTemporaries = |
| 1988 | llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>; |
| 1989 | |
| 1990 | static bool CheckEvaluationResult(CheckEvaluationResultKind CERK, |
| 1991 | EvalInfo &Info, SourceLocation DiagLoc, |
| 1992 | QualType Type, const APValue &Value, |
| 1993 | Expr::ConstExprUsage Usage, |
| 1994 | SourceLocation SubobjectLoc, |
| 1995 | CheckedTemporaries &CheckedTemps); |
| 1996 | |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1997 | /// Check that this reference or pointer core constant expression is a valid |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1998 | /// value for an address or reference constant expression. Return true if we |
| 1999 | /// can fold this expression, whether or not it's a constant expression. |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2000 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2001 | QualType Type, const LValue &LVal, |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2002 | Expr::ConstExprUsage Usage, |
| 2003 | CheckedTemporaries &CheckedTemps) { |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2004 | bool IsReferenceType = Type->isReferenceType(); |
| 2005 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2006 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 2007 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 2008 | |
| Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 2009 | // Check that the object is a global. Note that the fake 'this' object we |
| 2010 | // manufacture when checking potential constant expressions is conservatively |
| 2011 | // assumed to be global here. |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2012 | if (!IsGlobalLValue(Base)) { |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2013 | if (Info.getLangOpts().CPlusPlus11) { |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2014 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2015 | Info.FFDiag(Loc, diag::note_constexpr_non_global, 1) |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2016 | << IsReferenceType << !Designator.Entries.empty() |
| 2017 | << !!VD << VD; |
| 2018 | NoteLValueLocation(Info, Base); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2019 | } else { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2020 | Info.FFDiag(Loc); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2021 | } |
| Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 2022 | // Don't allow references to temporaries to escape. |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 2023 | return false; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2024 | } |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2025 | assert((Info.checkingPotentialConstantExpression() || |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2026 | LVal.getLValueCallIndex() == 0) && |
| 2027 | "have call index for global lvalue"); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2028 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 2029 | if (Base.is<DynamicAllocLValue>()) { |
| 2030 | Info.FFDiag(Loc, diag::note_constexpr_dynamic_alloc) |
| 2031 | << IsReferenceType << !Designator.Entries.empty(); |
| 2032 | NoteLValueLocation(Info, Base); |
| 2033 | return false; |
| 2034 | } |
| 2035 | |
| Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 2036 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 2037 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
| David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 2038 | // Check if this is a thread-local variable. |
| Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 2039 | if (Var->getTLSKind()) |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 2040 | // FIXME: Diagnostic! |
| Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 2041 | return false; |
| David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 2042 | |
| Hans Wennborg | 82dd877 | 2014-06-25 22:19:48 +0000 | [diff] [blame] | 2043 | // A dllimport variable never acts like a constant. |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2044 | if (Usage == Expr::EvaluateForCodeGen && Var->hasAttr<DLLImportAttr>()) |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 2045 | // FIXME: Diagnostic! |
| David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 2046 | return false; |
| 2047 | } |
| 2048 | if (const auto *FD = dyn_cast<const FunctionDecl>(VD)) { |
| 2049 | // __declspec(dllimport) must be handled very carefully: |
| 2050 | // We must never initialize an expression with the thunk in C++. |
| 2051 | // Doing otherwise would allow the same id-expression to yield |
| 2052 | // different addresses for the same function in different translation |
| 2053 | // units. However, this means that we must dynamically initialize the |
| 2054 | // expression with the contents of the import address table at runtime. |
| 2055 | // |
| 2056 | // The C language has no notion of ODR; furthermore, it has no notion of |
| 2057 | // dynamic initialization. This means that we are permitted to |
| 2058 | // perform initialization with the address of the thunk. |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2059 | if (Info.getLangOpts().CPlusPlus && Usage == Expr::EvaluateForCodeGen && |
| 2060 | FD->hasAttr<DLLImportAttr>()) |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 2061 | // FIXME: Diagnostic! |
| David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 2062 | return false; |
| Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 2063 | } |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2064 | } else if (const auto *MTE = dyn_cast_or_null<MaterializeTemporaryExpr>( |
| 2065 | Base.dyn_cast<const Expr *>())) { |
| 2066 | if (CheckedTemps.insert(MTE).second) { |
| Richard Smith | 1e8c085 | 2019-09-29 06:22:54 +0000 | [diff] [blame] | 2067 | QualType TempType = getType(Base); |
| 2068 | if (TempType.isDestructedType()) { |
| 2069 | Info.FFDiag(MTE->getExprLoc(), |
| 2070 | diag::note_constexpr_unsupported_tempoarary_nontrivial_dtor) |
| 2071 | << TempType; |
| 2072 | return false; |
| 2073 | } |
| 2074 | |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2075 | APValue *V = Info.Ctx.getMaterializedTemporaryValue(MTE, false); |
| 2076 | assert(V && "evasluation result refers to uninitialised temporary"); |
| 2077 | if (!CheckEvaluationResult(CheckEvaluationResultKind::ConstantExpression, |
| Richard Smith | 1e8c085 | 2019-09-29 06:22:54 +0000 | [diff] [blame] | 2078 | Info, MTE->getExprLoc(), TempType, *V, |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2079 | Usage, SourceLocation(), CheckedTemps)) |
| 2080 | return false; |
| 2081 | } |
| Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2084 | // Allow address constant expressions to be past-the-end pointers. This is |
| 2085 | // an extension: the standard requires them to point to an object. |
| 2086 | if (!IsReferenceType) |
| 2087 | return true; |
| 2088 | |
| 2089 | // A reference constant expression must refer to an object. |
| 2090 | if (!Base) { |
| 2091 | // FIXME: diagnostic |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2092 | Info.CCEDiag(Loc); |
| Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 2093 | return true; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2096 | // Does this refer one past the end of some object? |
| Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 2097 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2098 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2099 | Info.FFDiag(Loc, diag::note_constexpr_past_end, 1) |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2100 | << !Designator.Entries.empty() << !!VD << VD; |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2101 | NoteLValueLocation(Info, Base); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 2104 | return true; |
| 2105 | } |
| 2106 | |
| Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 2107 | /// Member pointers are constant expressions unless they point to a |
| 2108 | /// non-virtual dllimport member function. |
| 2109 | static bool CheckMemberPointerConstantExpression(EvalInfo &Info, |
| 2110 | SourceLocation Loc, |
| 2111 | QualType Type, |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2112 | const APValue &Value, |
| 2113 | Expr::ConstExprUsage Usage) { |
| Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 2114 | const ValueDecl *Member = Value.getMemberPointerDecl(); |
| 2115 | const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Member); |
| 2116 | if (!FD) |
| 2117 | return true; |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2118 | return Usage == Expr::EvaluateForMangling || FD->isVirtual() || |
| 2119 | !FD->hasAttr<DLLImportAttr>(); |
| Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2122 | /// Check that this core constant expression is of literal type, and if not, |
| 2123 | /// produce an appropriate diagnostic. |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2124 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2125 | const LValue *This = nullptr) { |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2126 | if (!E->isRValue() || E->getType()->isLiteralType(Info.Ctx)) |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2127 | return true; |
| 2128 | |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2129 | // C++1y: A constant initializer for an object o [...] may also invoke |
| 2130 | // constexpr constructors for o and its subobjects even if those objects |
| 2131 | // are of non-literal class types. |
| David L. Jones | f55ce36 | 2017-01-09 21:38:07 +0000 | [diff] [blame] | 2132 | // |
| 2133 | // C++11 missed this detail for aggregates, so classes like this: |
| 2134 | // struct foo_t { union { int i; volatile int j; } u; }; |
| 2135 | // are not (obviously) initializable like so: |
| 2136 | // __attribute__((__require_constant_initialization__)) |
| 2137 | // static const foo_t x = {{0}}; |
| 2138 | // because "i" is a subobject with non-literal initialization (due to the |
| 2139 | // volatile member of the union). See: |
| 2140 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 |
| 2141 | // Therefore, we use the C++1y behavior. |
| 2142 | if (This && Info.EvaluatingDecl == This->getLValueBase()) |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2143 | return true; |
| 2144 | |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2145 | // Prvalue constant expressions must be of literal types. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2146 | if (Info.getLangOpts().CPlusPlus11) |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2147 | Info.FFDiag(E, diag::note_constexpr_nonliteral) |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2148 | << E->getType(); |
| 2149 | else |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2150 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 2151 | return false; |
| 2152 | } |
| 2153 | |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2154 | static bool CheckEvaluationResult(CheckEvaluationResultKind CERK, |
| 2155 | EvalInfo &Info, SourceLocation DiagLoc, |
| 2156 | QualType Type, const APValue &Value, |
| 2157 | Expr::ConstExprUsage Usage, |
| 2158 | SourceLocation SubobjectLoc, |
| 2159 | CheckedTemporaries &CheckedTemps) { |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 2160 | if (!Value.hasValue()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2161 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 2162 | << true << Type; |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 2163 | if (SubobjectLoc.isValid()) |
| 2164 | Info.Note(SubobjectLoc, diag::note_constexpr_subobject_declared_here); |
| Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 2165 | return false; |
| 2166 | } |
| 2167 | |
| Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 2168 | // We allow _Atomic(T) to be initialized from anything that T can be |
| 2169 | // initialized from. |
| 2170 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
| 2171 | Type = AT->getValueType(); |
| 2172 | |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2173 | // Core issue 1454: For a literal constant expression of array or class type, |
| 2174 | // each subobject of its value shall have been initialized by a constant |
| 2175 | // expression. |
| 2176 | if (Value.isArray()) { |
| 2177 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 2178 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2179 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, EltTy, |
| 2180 | Value.getArrayInitializedElt(I), Usage, |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2181 | SubobjectLoc, CheckedTemps)) |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2182 | return false; |
| 2183 | } |
| 2184 | if (!Value.hasArrayFiller()) |
| 2185 | return true; |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2186 | return CheckEvaluationResult(CERK, Info, DiagLoc, EltTy, |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2187 | Value.getArrayFiller(), Usage, SubobjectLoc, |
| 2188 | CheckedTemps); |
| Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 2189 | } |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2190 | if (Value.isUnion() && Value.getUnionField()) { |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2191 | return CheckEvaluationResult( |
| 2192 | CERK, Info, DiagLoc, Value.getUnionField()->getType(), |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2193 | Value.getUnionValue(), Usage, Value.getUnionField()->getLocation(), |
| 2194 | CheckedTemps); |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2195 | } |
| 2196 | if (Value.isStruct()) { |
| 2197 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 2198 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 2199 | unsigned BaseIndex = 0; |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2200 | for (const CXXBaseSpecifier &BS : CD->bases()) { |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2201 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, BS.getType(), |
| 2202 | Value.getStructBase(BaseIndex), Usage, |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2203 | BS.getBeginLoc(), CheckedTemps)) |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2204 | return false; |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2205 | ++BaseIndex; |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2206 | } |
| 2207 | } |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 2208 | for (const auto *I : RD->fields()) { |
| Jordan Rose | d4503da | 2017-10-24 02:17:07 +0000 | [diff] [blame] | 2209 | if (I->isUnnamedBitfield()) |
| 2210 | continue; |
| 2211 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2212 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, I->getType(), |
| 2213 | Value.getStructField(I->getFieldIndex()), |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2214 | Usage, I->getLocation(), CheckedTemps)) |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2215 | return false; |
| 2216 | } |
| 2217 | } |
| 2218 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2219 | if (Value.isLValue() && |
| 2220 | CERK == CheckEvaluationResultKind::ConstantExpression) { |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2221 | LValue LVal; |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2222 | LVal.setFrom(Info.Ctx, Value); |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2223 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal, Usage, |
| 2224 | CheckedTemps); |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2227 | if (Value.isMemberPointer() && |
| 2228 | CERK == CheckEvaluationResultKind::ConstantExpression) |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2229 | return CheckMemberPointerConstantExpression(Info, DiagLoc, Type, Value, Usage); |
| Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 2230 | |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2231 | // Everything else is fine. |
| 2232 | return true; |
| Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2235 | /// Check that this core constant expression value is a valid value for a |
| 2236 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 2237 | /// check that the expression is of literal type. |
| 2238 | static bool |
| 2239 | CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, QualType Type, |
| 2240 | const APValue &Value, |
| 2241 | Expr::ConstExprUsage Usage = Expr::EvaluateForCodeGen) { |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2242 | CheckedTemporaries CheckedTemps; |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2243 | return CheckEvaluationResult(CheckEvaluationResultKind::ConstantExpression, |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2244 | Info, DiagLoc, Type, Value, Usage, |
| 2245 | SourceLocation(), CheckedTemps); |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | /// Check that this evaluated value is fully-initialized and can be loaded by |
| 2249 | /// an lvalue-to-rvalue conversion. |
| 2250 | static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc, |
| 2251 | QualType Type, const APValue &Value) { |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 2252 | CheckedTemporaries CheckedTemps; |
| 2253 | return CheckEvaluationResult( |
| 2254 | CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value, |
| 2255 | Expr::EvaluateForCodeGen, SourceLocation(), CheckedTemps); |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 2258 | /// Enforce C++2a [expr.const]/4.17, which disallows new-expressions unless |
| 2259 | /// "the allocated storage is deallocated within the evaluation". |
| 2260 | static bool CheckMemoryLeaks(EvalInfo &Info) { |
| 2261 | if (!Info.HeapAllocs.empty()) { |
| 2262 | // We can still fold to a constant despite a compile-time memory leak, |
| 2263 | // so long as the heap allocation isn't referenced in the result (we check |
| 2264 | // that in CheckConstantExpression). |
| 2265 | Info.CCEDiag(Info.HeapAllocs.begin()->second.AllocExpr, |
| 2266 | diag::note_constexpr_memory_leak) |
| 2267 | << unsigned(Info.HeapAllocs.size() - 1); |
| 2268 | } |
| 2269 | return true; |
| 2270 | } |
| 2271 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2272 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
| John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 2273 | // A null base expression indicates a null pointer. These are always |
| 2274 | // evaluatable, and they are false unless the offset is zero. |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2275 | if (!Value.getLValueBase()) { |
| 2276 | Result = !Value.getLValueOffset().isZero(); |
| John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 2277 | return true; |
| 2278 | } |
| Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 2279 | |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2280 | // We have a non-null base. These are generally known to be true, but if it's |
| 2281 | // a weak declaration it can be null at runtime. |
| John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 2282 | Result = true; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2283 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
| Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 2284 | return !Decl || !Decl->isWeak(); |
| Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2287 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2288 | switch (Val.getKind()) { |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 2289 | case APValue::None: |
| 2290 | case APValue::Indeterminate: |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2291 | return false; |
| 2292 | case APValue::Int: |
| 2293 | Result = Val.getInt().getBoolValue(); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2294 | return true; |
| Leonard Chan | 86285d2 | 2019-01-16 18:53:05 +0000 | [diff] [blame] | 2295 | case APValue::FixedPoint: |
| 2296 | Result = Val.getFixedPoint().getBoolValue(); |
| 2297 | return true; |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2298 | case APValue::Float: |
| 2299 | Result = !Val.getFloat().isZero(); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2300 | return true; |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2301 | case APValue::ComplexInt: |
| 2302 | Result = Val.getComplexIntReal().getBoolValue() || |
| 2303 | Val.getComplexIntImag().getBoolValue(); |
| 2304 | return true; |
| 2305 | case APValue::ComplexFloat: |
| 2306 | Result = !Val.getComplexFloatReal().isZero() || |
| 2307 | !Val.getComplexFloatImag().isZero(); |
| 2308 | return true; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2309 | case APValue::LValue: |
| 2310 | return EvalPointerValueAsBool(Val, Result); |
| 2311 | case APValue::MemberPointer: |
| 2312 | Result = Val.getMemberPointerDecl(); |
| 2313 | return true; |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2314 | case APValue::Vector: |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2315 | case APValue::Array: |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2316 | case APValue::Struct: |
| 2317 | case APValue::Union: |
| Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 2318 | case APValue::AddrLabelDiff: |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2319 | return false; |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2322 | llvm_unreachable("unknown APValue kind"); |
| 2323 | } |
| 2324 | |
| 2325 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 2326 | EvalInfo &Info) { |
| 2327 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2328 | APValue Val; |
| Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 2329 | if (!Evaluate(Val, Info, E)) |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2330 | return false; |
| Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 2331 | return HandleConversionToBool(Val, Result); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2334 | template<typename T> |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2335 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2336 | const T &SrcValue, QualType DestType) { |
| Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 2337 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
| Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 2338 | << SrcValue << DestType; |
| Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 2339 | return Info.noteUndefinedBehavior(); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 2343 | QualType SrcType, const APFloat &Value, |
| 2344 | QualType DestType, APSInt &Result) { |
| 2345 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
| Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2346 | // Determine whether we are converting to unsigned or signed. |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2347 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2348 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2349 | Result = APSInt(DestWidth, !DestSigned); |
| Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2350 | bool ignored; |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2351 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 2352 | & APFloat::opInvalidOp) |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2353 | return HandleOverflow(Info, E, Value, DestType); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2354 | return true; |
| Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2357 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 2358 | QualType SrcType, QualType DestType, |
| 2359 | APFloat &Result) { |
| 2360 | APFloat Value = Result; |
| Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2361 | bool ignored; |
| Richard Smith | 9e52c43 | 2019-07-06 21:05:52 +0000 | [diff] [blame] | 2362 | Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 2363 | APFloat::rmNearestTiesToEven, &ignored); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2364 | return true; |
| Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
| Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 2367 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 2368 | QualType DestType, QualType SrcType, |
| George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 2369 | const APSInt &Value) { |
| Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 2370 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
| Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2371 | // Figure out if this is a truncate, extend or noop cast. |
| 2372 | // If the input is signed, do a sign extend, noop, or truncate. |
| Richard Smith | bd844e0 | 2018-11-12 20:11:57 +0000 | [diff] [blame] | 2373 | APSInt Result = Value.extOrTrunc(DestWidth); |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2374 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
| Richard Smith | bd844e0 | 2018-11-12 20:11:57 +0000 | [diff] [blame] | 2375 | if (DestType->isBooleanType()) |
| 2376 | Result = Value.getBoolValue(); |
| Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2377 | return Result; |
| 2378 | } |
| 2379 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2380 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 2381 | QualType SrcType, const APSInt &Value, |
| 2382 | QualType DestType, APFloat &Result) { |
| 2383 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| Richard Smith | 9e52c43 | 2019-07-06 21:05:52 +0000 | [diff] [blame] | 2384 | Result.convertFromAPInt(Value, Value.isSigned(), |
| 2385 | APFloat::rmNearestTiesToEven); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2386 | return true; |
| Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
| Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2389 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
| 2390 | APValue &Value, const FieldDecl *FD) { |
| 2391 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
| 2392 | |
| 2393 | if (!Value.isInt()) { |
| 2394 | // Trying to store a pointer-cast-to-integer into a bitfield. |
| 2395 | // FIXME: In this case, we should provide the diagnostic for casting |
| 2396 | // a pointer to an integer. |
| 2397 | assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2398 | Info.FFDiag(E); |
| Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2399 | return false; |
| 2400 | } |
| 2401 | |
| 2402 | APSInt &Int = Value.getInt(); |
| 2403 | unsigned OldBitWidth = Int.getBitWidth(); |
| 2404 | unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx); |
| 2405 | if (NewBitWidth < OldBitWidth) |
| 2406 | Int = Int.trunc(NewBitWidth).extend(OldBitWidth); |
| 2407 | return true; |
| 2408 | } |
| 2409 | |
| Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2410 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 2411 | llvm::APInt &Res) { |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2412 | APValue SVal; |
| Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2413 | if (!Evaluate(SVal, Info, E)) |
| 2414 | return false; |
| 2415 | if (SVal.isInt()) { |
| 2416 | Res = SVal.getInt(); |
| 2417 | return true; |
| 2418 | } |
| 2419 | if (SVal.isFloat()) { |
| 2420 | Res = SVal.getFloat().bitcastToAPInt(); |
| 2421 | return true; |
| 2422 | } |
| 2423 | if (SVal.isVector()) { |
| 2424 | QualType VecTy = E->getType(); |
| 2425 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 2426 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 2427 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 2428 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 2429 | Res = llvm::APInt::getNullValue(VecSize); |
| 2430 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 2431 | APValue &Elt = SVal.getVectorElt(i); |
| 2432 | llvm::APInt EltAsInt; |
| 2433 | if (Elt.isInt()) { |
| 2434 | EltAsInt = Elt.getInt(); |
| 2435 | } else if (Elt.isFloat()) { |
| 2436 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 2437 | } else { |
| 2438 | // Don't try to handle vectors of anything other than int or float |
| 2439 | // (not sure if it's possible to hit this case). |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2440 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2441 | return false; |
| 2442 | } |
| 2443 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 2444 | if (BigEndian) |
| 2445 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 2446 | else |
| 2447 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 2448 | } |
| 2449 | return true; |
| 2450 | } |
| 2451 | // Give up if the input isn't an int, float, or vector. For example, we |
| 2452 | // reject "(v4i16)(intptr_t)&a". |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2453 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2454 | return false; |
| 2455 | } |
| 2456 | |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2457 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 2458 | /// bits, and check for overflow in the original type (if that type was not an |
| 2459 | /// unsigned type). |
| 2460 | template<typename Operation> |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2461 | static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 2462 | const APSInt &LHS, const APSInt &RHS, |
| 2463 | unsigned BitWidth, Operation Op, |
| 2464 | APSInt &Result) { |
| 2465 | if (LHS.isUnsigned()) { |
| 2466 | Result = Op(LHS, RHS); |
| 2467 | return true; |
| 2468 | } |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2469 | |
| 2470 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2471 | Result = Value.trunc(LHS.getBitWidth()); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2472 | if (Result.extend(BitWidth) != Value) { |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 2473 | if (Info.checkingForUndefinedBehavior()) |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2474 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2475 | diag::warn_integer_constant_overflow) |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2476 | << Result.toString(10) << E->getType(); |
| 2477 | else |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2478 | return HandleOverflow(Info, E, Value, E->getType()); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2479 | } |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2480 | return true; |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | /// Perform the given binary integer operation. |
| 2484 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
| 2485 | BinaryOperatorKind Opcode, APSInt RHS, |
| 2486 | APSInt &Result) { |
| 2487 | switch (Opcode) { |
| 2488 | default: |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2489 | Info.FFDiag(E); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2490 | return false; |
| 2491 | case BO_Mul: |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2492 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
| 2493 | std::multiplies<APSInt>(), Result); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2494 | case BO_Add: |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2495 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 2496 | std::plus<APSInt>(), Result); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2497 | case BO_Sub: |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2498 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 2499 | std::minus<APSInt>(), Result); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2500 | case BO_And: Result = LHS & RHS; return true; |
| 2501 | case BO_Xor: Result = LHS ^ RHS; return true; |
| 2502 | case BO_Or: Result = LHS | RHS; return true; |
| 2503 | case BO_Div: |
| 2504 | case BO_Rem: |
| 2505 | if (RHS == 0) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2506 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2507 | return false; |
| 2508 | } |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2509 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
| 2510 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports |
| 2511 | // this operation and gives the two's complement result. |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2512 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 2513 | LHS.isSigned() && LHS.isMinSignedValue()) |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2514 | return HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), |
| 2515 | E->getType()); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2516 | return true; |
| 2517 | case BO_Shl: { |
| 2518 | if (Info.getLangOpts().OpenCL) |
| 2519 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 2520 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 2521 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 2522 | RHS.isUnsigned()); |
| 2523 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 2524 | // During constant-folding, a negative shift is an opposite shift. Such |
| 2525 | // a shift is not a constant expression. |
| 2526 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 2527 | RHS = -RHS; |
| 2528 | goto shift_right; |
| 2529 | } |
| 2530 | shift_left: |
| 2531 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 2532 | // the shifted type. |
| 2533 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2534 | if (SA != RHS) { |
| 2535 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 2536 | << RHS << E->getType() << LHS.getBitWidth(); |
| Richard Smith | 7939ba0 | 2019-06-25 01:45:26 +0000 | [diff] [blame] | 2537 | } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus2a) { |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2538 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 2539 | // operand, and must not overflow the corresponding unsigned type. |
| Richard Smith | 7939ba0 | 2019-06-25 01:45:26 +0000 | [diff] [blame] | 2540 | // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to |
| 2541 | // E1 x 2^E2 module 2^N. |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2542 | if (LHS.isNegative()) |
| 2543 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 2544 | else if (LHS.countLeadingZeros() < SA) |
| 2545 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 2546 | } |
| 2547 | Result = LHS << SA; |
| 2548 | return true; |
| 2549 | } |
| 2550 | case BO_Shr: { |
| 2551 | if (Info.getLangOpts().OpenCL) |
| 2552 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 2553 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 2554 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 2555 | RHS.isUnsigned()); |
| 2556 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 2557 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 2558 | // shift is not a constant expression. |
| 2559 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 2560 | RHS = -RHS; |
| 2561 | goto shift_left; |
| 2562 | } |
| 2563 | shift_right: |
| 2564 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 2565 | // shifted type. |
| 2566 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2567 | if (SA != RHS) |
| 2568 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 2569 | << RHS << E->getType() << LHS.getBitWidth(); |
| 2570 | Result = LHS >> SA; |
| 2571 | return true; |
| 2572 | } |
| 2573 | |
| 2574 | case BO_LT: Result = LHS < RHS; return true; |
| 2575 | case BO_GT: Result = LHS > RHS; return true; |
| 2576 | case BO_LE: Result = LHS <= RHS; return true; |
| 2577 | case BO_GE: Result = LHS >= RHS; return true; |
| 2578 | case BO_EQ: Result = LHS == RHS; return true; |
| 2579 | case BO_NE: Result = LHS != RHS; return true; |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 2580 | case BO_Cmp: |
| 2581 | llvm_unreachable("BO_Cmp should be handled elsewhere"); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2585 | /// Perform the given binary floating-point operation, in-place, on LHS. |
| 2586 | static bool handleFloatFloatBinOp(EvalInfo &Info, const Expr *E, |
| 2587 | APFloat &LHS, BinaryOperatorKind Opcode, |
| 2588 | const APFloat &RHS) { |
| 2589 | switch (Opcode) { |
| 2590 | default: |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2591 | Info.FFDiag(E); |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2592 | return false; |
| 2593 | case BO_Mul: |
| 2594 | LHS.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 2595 | break; |
| 2596 | case BO_Add: |
| 2597 | LHS.add(RHS, APFloat::rmNearestTiesToEven); |
| 2598 | break; |
| 2599 | case BO_Sub: |
| 2600 | LHS.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 2601 | break; |
| 2602 | case BO_Div: |
| Richard Smith | 9e52c43 | 2019-07-06 21:05:52 +0000 | [diff] [blame] | 2603 | // [expr.mul]p4: |
| 2604 | // If the second operand of / or % is zero the behavior is undefined. |
| 2605 | if (RHS.isZero()) |
| 2606 | Info.CCEDiag(E, diag::note_expr_divide_by_zero); |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2607 | LHS.divide(RHS, APFloat::rmNearestTiesToEven); |
| 2608 | break; |
| 2609 | } |
| 2610 | |
| Richard Smith | 9e52c43 | 2019-07-06 21:05:52 +0000 | [diff] [blame] | 2611 | // [expr.pre]p4: |
| 2612 | // If during the evaluation of an expression, the result is not |
| 2613 | // mathematically defined [...], the behavior is undefined. |
| 2614 | // FIXME: C++ rules require us to not conform to IEEE 754 here. |
| 2615 | if (LHS.isNaN()) { |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2616 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
| Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 2617 | return Info.noteUndefinedBehavior(); |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2618 | } |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2619 | return true; |
| 2620 | } |
| 2621 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2622 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 2623 | /// truncating the lvalue's path to the given length. |
| 2624 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 2625 | const RecordDecl *TruncatedType, |
| 2626 | unsigned TruncatedElements) { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2627 | SubobjectDesignator &D = Result.Designator; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2628 | |
| 2629 | // Check we actually point to a derived class object. |
| 2630 | if (TruncatedElements == D.Entries.size()) |
| 2631 | return true; |
| 2632 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 2633 | "not casting to a derived class"); |
| 2634 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 2635 | return false; |
| 2636 | |
| 2637 | // 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] | 2638 | const RecordDecl *RD = TruncatedType; |
| 2639 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2640 | if (RD->isInvalidDecl()) return false; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2641 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2642 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2643 | if (isVirtualBaseClass(D.Entries[I])) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2644 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2645 | else |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2646 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 2647 | RD = Base; |
| 2648 | } |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2649 | D.Entries.resize(TruncatedElements); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2650 | return true; |
| 2651 | } |
| 2652 | |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2653 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2654 | const CXXRecordDecl *Derived, |
| 2655 | const CXXRecordDecl *Base, |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2656 | const ASTRecordLayout *RL = nullptr) { |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2657 | if (!RL) { |
| 2658 | if (Derived->isInvalidDecl()) return false; |
| 2659 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 2660 | } |
| 2661 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2662 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2663 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2664 | return true; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2667 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2668 | const CXXRecordDecl *DerivedDecl, |
| 2669 | const CXXBaseSpecifier *Base) { |
| 2670 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 2671 | |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2672 | if (!Base->isVirtual()) |
| 2673 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2674 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2675 | SubobjectDesignator &D = Obj.Designator; |
| 2676 | if (D.Invalid) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2677 | return false; |
| 2678 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2679 | // Extract most-derived object and corresponding type. |
| 2680 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 2681 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 2682 | return false; |
| 2683 | |
| 2684 | // Find the virtual base class. |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2685 | if (DerivedDecl->isInvalidDecl()) return false; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2686 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 2687 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2688 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2689 | return true; |
| 2690 | } |
| 2691 | |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2692 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
| 2693 | QualType Type, LValue &Result) { |
| 2694 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2695 | PathE = E->path_end(); |
| 2696 | PathI != PathE; ++PathI) { |
| 2697 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 2698 | *PathI)) |
| 2699 | return false; |
| 2700 | Type = (*PathI)->getType(); |
| 2701 | } |
| 2702 | return true; |
| 2703 | } |
| 2704 | |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 2705 | /// Cast an lvalue referring to a derived class to a known base subobject. |
| 2706 | static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 2707 | const CXXRecordDecl *DerivedRD, |
| 2708 | const CXXRecordDecl *BaseRD) { |
| 2709 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 2710 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
| 2711 | if (!DerivedRD->isDerivedFrom(BaseRD, Paths)) |
| 2712 | llvm_unreachable("Class must be derived from the passed in base class!"); |
| 2713 | |
| 2714 | for (CXXBasePathElement &Elem : Paths.front()) |
| 2715 | if (!HandleLValueBase(Info, E, Result, Elem.Class, Elem.Base)) |
| 2716 | return false; |
| 2717 | return true; |
| 2718 | } |
| 2719 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2720 | /// Update LVal to refer to the given field, which must be a member of the type |
| 2721 | /// currently described by LVal. |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2722 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2723 | const FieldDecl *FD, |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2724 | const ASTRecordLayout *RL = nullptr) { |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2725 | if (!RL) { |
| 2726 | if (FD->getParent()->isInvalidDecl()) return false; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2727 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2728 | } |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2729 | |
| 2730 | unsigned I = FD->getFieldIndex(); |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 2731 | LVal.adjustOffset(Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I))); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2732 | LVal.addDecl(Info, E, FD); |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2733 | return true; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2736 | /// Update LVal to refer to the given indirect field. |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2737 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2738 | LValue &LVal, |
| 2739 | const IndirectFieldDecl *IFD) { |
| Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 2740 | for (const auto *C : IFD->chain()) |
| Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 2741 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(C))) |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2742 | return false; |
| 2743 | return true; |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2746 | /// Get the size of the given type in char units. |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2747 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 2748 | QualType Type, CharUnits &Size) { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2749 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 2750 | // extension. |
| 2751 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 2752 | Size = CharUnits::One(); |
| 2753 | return true; |
| 2754 | } |
| 2755 | |
| Saleem Abdulrasool | ada78fe | 2016-06-04 03:16:21 +0000 | [diff] [blame] | 2756 | if (Type->isDependentType()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2757 | Info.FFDiag(Loc); |
| Saleem Abdulrasool | ada78fe | 2016-06-04 03:16:21 +0000 | [diff] [blame] | 2758 | return false; |
| 2759 | } |
| 2760 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2761 | if (!Type->isConstantSizeType()) { |
| 2762 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2763 | // FIXME: Better diagnostic. |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2764 | Info.FFDiag(Loc); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2765 | return false; |
| 2766 | } |
| 2767 | |
| 2768 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 2769 | return true; |
| 2770 | } |
| 2771 | |
| 2772 | /// Update a pointer value to model pointer arithmetic. |
| 2773 | /// \param Info - Information about the ongoing evaluation. |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2774 | /// \param E - The expression being evaluated, for diagnostic purposes. |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2775 | /// \param LVal - The pointer value to be updated. |
| 2776 | /// \param EltTy - The pointee type represented by LVal. |
| 2777 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2778 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 2779 | LValue &LVal, QualType EltTy, |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 2780 | APSInt Adjustment) { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2781 | CharUnits SizeOfPointee; |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2782 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2783 | return false; |
| 2784 | |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 2785 | LVal.adjustOffsetAndIndex(Info, E, Adjustment, SizeOfPointee); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2786 | return true; |
| 2787 | } |
| 2788 | |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 2789 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 2790 | LValue &LVal, QualType EltTy, |
| 2791 | int64_t Adjustment) { |
| 2792 | return HandleLValueArrayAdjustment(Info, E, LVal, EltTy, |
| 2793 | APSInt::get(Adjustment)); |
| 2794 | } |
| 2795 | |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2796 | /// Update an lvalue to refer to a component of a complex number. |
| 2797 | /// \param Info - Information about the ongoing evaluation. |
| 2798 | /// \param LVal - The lvalue to be updated. |
| 2799 | /// \param EltTy - The complex number's component type. |
| 2800 | /// \param Imag - False for the real component, true for the imaginary. |
| 2801 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 2802 | LValue &LVal, QualType EltTy, |
| 2803 | bool Imag) { |
| 2804 | if (Imag) { |
| 2805 | CharUnits SizeOfComponent; |
| 2806 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 2807 | return false; |
| 2808 | LVal.Offset += SizeOfComponent; |
| 2809 | } |
| 2810 | LVal.addComplex(Info, E, EltTy, Imag); |
| 2811 | return true; |
| 2812 | } |
| 2813 | |
| Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2814 | /// Try to evaluate the initializer for a variable declaration. |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2815 | /// |
| 2816 | /// \param Info Information about the ongoing evaluation. |
| 2817 | /// \param E An expression to be used when printing diagnostics. |
| 2818 | /// \param VD The variable whose initializer should be obtained. |
| 2819 | /// \param Frame The frame in which the variable was created. Must be null |
| 2820 | /// if this variable is not local to the evaluation. |
| 2821 | /// \param Result Filled in with a pointer to the value of the variable. |
| 2822 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 2823 | const VarDecl *VD, CallStackFrame *Frame, |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 2824 | APValue *&Result, const LValue *LVal) { |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 2825 | |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2826 | // If this is a parameter to an active constexpr function call, perform |
| 2827 | // argument substitution. |
| 2828 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2829 | // Assume arguments of a potential constant expression are unknown |
| 2830 | // constant expressions. |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2831 | if (Info.checkingPotentialConstantExpression()) |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2832 | return false; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2833 | if (!Frame || !Frame->Arguments) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2834 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2835 | return false; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2836 | } |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2837 | Result = &Frame->Arguments[PVD->getFunctionScopeIndex()]; |
| Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2838 | return true; |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2839 | } |
| Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2840 | |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2841 | // If this is a local variable, dig out its value. |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2842 | if (Frame) { |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 2843 | Result = LVal ? Frame->getTemporary(VD, LVal->getLValueVersion()) |
| 2844 | : Frame->getCurrentTemporary(VD); |
| Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 2845 | if (!Result) { |
| 2846 | // Assume variables referenced within a lambda's call operator that were |
| 2847 | // not declared within the call operator are captures and during checking |
| 2848 | // of a potential constant expression, assume they are unknown constant |
| 2849 | // expressions. |
| 2850 | assert(isLambdaCallOperator(Frame->Callee) && |
| 2851 | (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && |
| 2852 | "missing value for local variable"); |
| 2853 | if (Info.checkingPotentialConstantExpression()) |
| 2854 | return false; |
| 2855 | // FIXME: implement capture evaluation during constant expr evaluation. |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2856 | Info.FFDiag(E->getBeginLoc(), |
| 2857 | diag::note_unimplemented_constexpr_lambda_feature_ast) |
| Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 2858 | << "captures not currently allowed"; |
| 2859 | return false; |
| 2860 | } |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2861 | return true; |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2864 | // Dig out the initializer, and use the declaration which it's attached to. |
| 2865 | const Expr *Init = VD->getAnyInitializer(VD); |
| 2866 | if (!Init || Init->isValueDependent()) { |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2867 | // If we're checking a potential constant expression, the variable could be |
| 2868 | // initialized later. |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2869 | if (!Info.checkingPotentialConstantExpression()) |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2870 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2871 | return false; |
| 2872 | } |
| 2873 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2874 | // If we're currently evaluating the initializer of this declaration, use that |
| 2875 | // in-flight value. |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2876 | if (Info.EvaluatingDecl.dyn_cast<const ValueDecl*>() == VD) { |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2877 | Result = Info.EvaluatingDeclValue; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2878 | return true; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2881 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 2882 | // this is the definition which will be used. |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2883 | if (VD->isWeak()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2884 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2885 | return false; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2886 | } |
| Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2887 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2888 | // Check that we can fold the initializer. In C++, we will have already done |
| 2889 | // this in the cases where it matters for conformance. |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2890 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2891 | if (!VD->evaluateValue(Notes)) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2892 | Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2893 | Notes.size() + 1) << VD; |
| 2894 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2895 | Info.addNotes(Notes); |
| Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2896 | return false; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2897 | } else if (!VD->checkInitIsICE()) { |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2898 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2899 | Notes.size() + 1) << VD; |
| 2900 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2901 | Info.addNotes(Notes); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2902 | } |
| Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2903 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2904 | Result = VD->getEvaluatedValue(); |
| Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2905 | return true; |
| Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2908 | static bool IsConstNonVolatile(QualType T) { |
| Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2909 | Qualifiers Quals = T.getQualifiers(); |
| 2910 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 2911 | } |
| 2912 | |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2913 | /// Get the base index of the given base class within an APValue representing |
| 2914 | /// the given derived class. |
| 2915 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 2916 | const CXXRecordDecl *Base) { |
| 2917 | Base = Base->getCanonicalDecl(); |
| 2918 | unsigned Index = 0; |
| 2919 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 2920 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 2921 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 2922 | return Index; |
| 2923 | } |
| 2924 | |
| 2925 | llvm_unreachable("base class missing from derived class's bases list"); |
| 2926 | } |
| 2927 | |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2928 | /// Extract the value of a character from a string literal. |
| 2929 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
| 2930 | uint64_t Index) { |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 2931 | assert(!isa<SourceLocExpr>(Lit) && |
| 2932 | "SourceLocExpr should have already been converted to a StringLiteral"); |
| 2933 | |
| Akira Hatanaka | bc33264 | 2017-01-31 02:31:39 +0000 | [diff] [blame] | 2934 | // FIXME: Support MakeStringConstant |
| 2935 | if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Lit)) { |
| 2936 | std::string Str; |
| 2937 | Info.Ctx.getObjCEncodingForType(ObjCEnc->getEncodedType(), Str); |
| 2938 | assert(Index <= Str.size() && "Index too large"); |
| 2939 | return APSInt::getUnsigned(Str.c_str()[Index]); |
| 2940 | } |
| 2941 | |
| Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2942 | if (auto PE = dyn_cast<PredefinedExpr>(Lit)) |
| 2943 | Lit = PE->getFunctionName(); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2944 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 2945 | const ConstantArrayType *CAT = |
| 2946 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2947 | assert(CAT && "string literal isn't an array"); |
| 2948 | QualType CharType = CAT->getElementType(); |
| Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2949 | assert(CharType->isIntegerType() && "unexpected character type"); |
| Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2950 | |
| 2951 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2952 | CharType->isUnsignedIntegerType()); |
| Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2953 | if (Index < S->getLength()) |
| 2954 | Value = S->getCodeUnit(Index); |
| 2955 | return Value; |
| 2956 | } |
| 2957 | |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2958 | // Expand a string literal into an array of characters. |
| Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 2959 | // |
| 2960 | // FIXME: This is inefficient; we should probably introduce something similar |
| 2961 | // to the LLVM ConstantDataArray to make this cheaper. |
| 2962 | static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S, |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 2963 | APValue &Result, |
| 2964 | QualType AllocType = QualType()) { |
| 2965 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
| 2966 | AllocType.isNull() ? S->getType() : AllocType); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2967 | assert(CAT && "string literal isn't an array"); |
| 2968 | QualType CharType = CAT->getElementType(); |
| 2969 | assert(CharType->isIntegerType() && "unexpected character type"); |
| 2970 | |
| 2971 | unsigned Elts = CAT->getSize().getZExtValue(); |
| 2972 | Result = APValue(APValue::UninitArray(), |
| 2973 | std::min(S->getLength(), Elts), Elts); |
| 2974 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 2975 | CharType->isUnsignedIntegerType()); |
| 2976 | if (Result.hasArrayFiller()) |
| 2977 | Result.getArrayFiller() = APValue(Value); |
| 2978 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
| 2979 | Value = S->getCodeUnit(I); |
| 2980 | Result.getArrayInitializedElt(I) = APValue(Value); |
| 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | // Expand an array so that it has more than Index filled elements. |
| 2985 | static void expandArray(APValue &Array, unsigned Index) { |
| 2986 | unsigned Size = Array.getArraySize(); |
| 2987 | assert(Index < Size); |
| 2988 | |
| 2989 | // Always at least double the number of elements for which we store a value. |
| 2990 | unsigned OldElts = Array.getArrayInitializedElts(); |
| 2991 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
| 2992 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
| 2993 | |
| 2994 | // Copy the data across. |
| 2995 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
| 2996 | for (unsigned I = 0; I != OldElts; ++I) |
| 2997 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
| 2998 | for (unsigned I = OldElts; I != NewElts; ++I) |
| 2999 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
| 3000 | if (NewValue.hasArrayFiller()) |
| 3001 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
| 3002 | Array.swap(NewValue); |
| 3003 | } |
| 3004 | |
| Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 3005 | /// Determine whether a type would actually be read by an lvalue-to-rvalue |
| 3006 | /// conversion. If it's of class type, we may assume that the copy operation |
| 3007 | /// is trivial. Note that this is never true for a union type with fields |
| 3008 | /// (because the copy always "reads" the active member) and always true for |
| 3009 | /// a non-class type. |
| 3010 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
| 3011 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 3012 | if (!RD || (RD->isUnion() && !RD->field_empty())) |
| 3013 | return true; |
| 3014 | if (RD->isEmpty()) |
| 3015 | return false; |
| 3016 | |
| 3017 | for (auto *Field : RD->fields()) |
| 3018 | if (isReadByLvalueToRvalueConversion(Field->getType())) |
| 3019 | return true; |
| 3020 | |
| 3021 | for (auto &BaseSpec : RD->bases()) |
| 3022 | if (isReadByLvalueToRvalueConversion(BaseSpec.getType())) |
| 3023 | return true; |
| 3024 | |
| 3025 | return false; |
| 3026 | } |
| 3027 | |
| 3028 | /// Diagnose an attempt to read from any unreadable field within the specified |
| 3029 | /// type, which might be a class type. |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3030 | static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK, |
| 3031 | QualType T) { |
| Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 3032 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 3033 | if (!RD) |
| 3034 | return false; |
| 3035 | |
| 3036 | if (!RD->hasMutableFields()) |
| 3037 | return false; |
| 3038 | |
| 3039 | for (auto *Field : RD->fields()) { |
| 3040 | // If we're actually going to read this field in some way, then it can't |
| 3041 | // be mutable. If we're in a union, then assigning to a mutable field |
| 3042 | // (even an empty one) can change the active member, so that's not OK. |
| 3043 | // FIXME: Add core issue number for the union case. |
| 3044 | if (Field->isMutable() && |
| 3045 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3046 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) << AK << Field; |
| Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 3047 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 3048 | return true; |
| 3049 | } |
| 3050 | |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3051 | if (diagnoseMutableFields(Info, E, AK, Field->getType())) |
| Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 3052 | return true; |
| 3053 | } |
| 3054 | |
| 3055 | for (auto &BaseSpec : RD->bases()) |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3056 | if (diagnoseMutableFields(Info, E, AK, BaseSpec.getType())) |
| Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 3057 | return true; |
| 3058 | |
| 3059 | // All mutable fields were empty, and thus not actually read. |
| 3060 | return false; |
| 3061 | } |
| 3062 | |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3063 | static bool lifetimeStartedInEvaluation(EvalInfo &Info, |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3064 | APValue::LValueBase Base, |
| 3065 | bool MutableSubobject = false) { |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3066 | // A temporary we created. |
| 3067 | if (Base.getCallIndex()) |
| 3068 | return true; |
| 3069 | |
| 3070 | auto *Evaluating = Info.EvaluatingDecl.dyn_cast<const ValueDecl*>(); |
| 3071 | if (!Evaluating) |
| 3072 | return false; |
| 3073 | |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3074 | auto *BaseD = Base.dyn_cast<const ValueDecl*>(); |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3075 | |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3076 | switch (Info.IsEvaluatingDecl) { |
| 3077 | case EvalInfo::EvaluatingDeclKind::None: |
| 3078 | return false; |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3079 | |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3080 | case EvalInfo::EvaluatingDeclKind::Ctor: |
| 3081 | // The variable whose initializer we're evaluating. |
| 3082 | if (BaseD) |
| 3083 | return declaresSameEntity(Evaluating, BaseD); |
| 3084 | |
| 3085 | // A temporary lifetime-extended by the variable whose initializer we're |
| 3086 | // evaluating. |
| 3087 | if (auto *BaseE = Base.dyn_cast<const Expr *>()) |
| 3088 | if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(BaseE)) |
| 3089 | return declaresSameEntity(BaseMTE->getExtendingDecl(), Evaluating); |
| 3090 | return false; |
| 3091 | |
| 3092 | case EvalInfo::EvaluatingDeclKind::Dtor: |
| 3093 | // C++2a [expr.const]p6: |
| 3094 | // [during constant destruction] the lifetime of a and its non-mutable |
| 3095 | // subobjects (but not its mutable subobjects) [are] considered to start |
| 3096 | // within e. |
| 3097 | // |
| 3098 | // FIXME: We can meaningfully extend this to cover non-const objects, but |
| 3099 | // we will need special handling: we should be able to access only |
| 3100 | // subobjects of such objects that are themselves declared const. |
| 3101 | if (!BaseD || |
| 3102 | !(BaseD->getType().isConstQualified() || |
| 3103 | BaseD->getType()->isReferenceType()) || |
| 3104 | MutableSubobject) |
| 3105 | return false; |
| 3106 | return declaresSameEntity(Evaluating, BaseD); |
| 3107 | } |
| 3108 | |
| 3109 | llvm_unreachable("unknown evaluating decl kind"); |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
| Benjamin Kramer | 5b4296a | 2015-10-28 17:16:26 +0000 | [diff] [blame] | 3112 | namespace { |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3113 | /// A handle to a complete object (an object that is not a subobject of |
| 3114 | /// another object). |
| 3115 | struct CompleteObject { |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3116 | /// The identity of the object. |
| 3117 | APValue::LValueBase Base; |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3118 | /// The value of the complete object. |
| 3119 | APValue *Value; |
| 3120 | /// The type of the complete object. |
| 3121 | QualType Type; |
| 3122 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3123 | CompleteObject() : Value(nullptr) {} |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3124 | CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type) |
| 3125 | : Base(Base), Value(Value), Type(Type) {} |
| 3126 | |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3127 | bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const { |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3128 | // In C++14 onwards, it is permitted to read a mutable member whose |
| 3129 | // lifetime began within the evaluation. |
| 3130 | // FIXME: Should we also allow this in C++11? |
| 3131 | if (!Info.getLangOpts().CPlusPlus14) |
| 3132 | return false; |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3133 | return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3136 | explicit operator bool() const { return !Type.isNull(); } |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3137 | }; |
| Benjamin Kramer | 5b4296a | 2015-10-28 17:16:26 +0000 | [diff] [blame] | 3138 | } // end anonymous namespace |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3139 | |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3140 | static QualType getSubobjectType(QualType ObjType, QualType SubobjType, |
| 3141 | bool IsMutable = false) { |
| 3142 | // C++ [basic.type.qualifier]p1: |
| 3143 | // - A const object is an object of type const T or a non-mutable subobject |
| 3144 | // of a const object. |
| 3145 | if (ObjType.isConstQualified() && !IsMutable) |
| 3146 | SubobjType.addConst(); |
| 3147 | // - A volatile object is an object of type const T or a subobject of a |
| 3148 | // volatile object. |
| 3149 | if (ObjType.isVolatileQualified()) |
| 3150 | SubobjType.addVolatile(); |
| 3151 | return SubobjType; |
| 3152 | } |
| 3153 | |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3154 | /// Find the designated sub-object of an rvalue. |
| 3155 | template<typename SubobjectHandler> |
| 3156 | typename SubobjectHandler::result_type |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3157 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3158 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3159 | if (Sub.Invalid) |
| 3160 | // A diagnostic will have already been produced. |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3161 | return handler.failed(); |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 3162 | if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) { |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3163 | if (Info.getLangOpts().CPlusPlus11) |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 3164 | Info.FFDiag(E, Sub.isOnePastTheEnd() |
| 3165 | ? diag::note_constexpr_access_past_end |
| 3166 | : diag::note_constexpr_access_unsized_array) |
| 3167 | << handler.AccessKind; |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3168 | else |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3169 | Info.FFDiag(E); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3170 | return handler.failed(); |
| Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 3171 | } |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3172 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3173 | APValue *O = Obj.Value; |
| 3174 | QualType ObjType = Obj.Type; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3175 | const FieldDecl *LastField = nullptr; |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3176 | const FieldDecl *VolatileField = nullptr; |
| Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3177 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3178 | // Walk the designator's path to find the subobject. |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3179 | for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) { |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 3180 | // Reading an indeterminate value is undefined, but assigning over one is OK. |
| Richard Smith | b542602 | 2019-10-03 00:39:35 +0000 | [diff] [blame] | 3181 | if ((O->isAbsent() && handler.AccessKind != AK_Construct) || |
| 3182 | (O->isIndeterminate() && handler.AccessKind != AK_Construct && |
| 3183 | handler.AccessKind != AK_Assign && |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3184 | handler.AccessKind != AK_ReadObjectRepresentation)) { |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3185 | if (!Info.checkingPotentialConstantExpression()) |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3186 | Info.FFDiag(E, diag::note_constexpr_access_uninit) |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 3187 | << handler.AccessKind << O->isIndeterminate(); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3188 | return handler.failed(); |
| 3189 | } |
| 3190 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 3191 | // C++ [class.ctor]p5, C++ [class.dtor]p5: |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3192 | // const and volatile semantics are not applied on an object under |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 3193 | // {con,de}struction. |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3194 | if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) && |
| 3195 | ObjType->isRecordType() && |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 3196 | Info.isEvaluatingCtorDtor( |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3197 | Obj.Base, llvm::makeArrayRef(Sub.Entries.begin(), |
| 3198 | Sub.Entries.begin() + I)) != |
| 3199 | ConstructionPhase::None) { |
| 3200 | ObjType = Info.Ctx.getCanonicalType(ObjType); |
| 3201 | ObjType.removeLocalConst(); |
| 3202 | ObjType.removeLocalVolatile(); |
| 3203 | } |
| 3204 | |
| 3205 | // If this is our last pass, check that the final object type is OK. |
| 3206 | if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) { |
| 3207 | // Accesses to volatile objects are prohibited. |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 3208 | if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) { |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3209 | if (Info.getLangOpts().CPlusPlus) { |
| 3210 | int DiagKind; |
| 3211 | SourceLocation Loc; |
| 3212 | const NamedDecl *Decl = nullptr; |
| 3213 | if (VolatileField) { |
| 3214 | DiagKind = 2; |
| 3215 | Loc = VolatileField->getLocation(); |
| 3216 | Decl = VolatileField; |
| 3217 | } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) { |
| 3218 | DiagKind = 1; |
| 3219 | Loc = VD->getLocation(); |
| 3220 | Decl = VD; |
| 3221 | } else { |
| 3222 | DiagKind = 0; |
| 3223 | if (auto *E = Obj.Base.dyn_cast<const Expr *>()) |
| 3224 | Loc = E->getExprLoc(); |
| 3225 | } |
| 3226 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
| 3227 | << handler.AccessKind << DiagKind << Decl; |
| 3228 | Info.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind; |
| 3229 | } else { |
| 3230 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| 3231 | } |
| 3232 | return handler.failed(); |
| 3233 | } |
| 3234 | |
| Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 3235 | // If we are reading an object of class type, there may still be more |
| 3236 | // things we need to check: if there are any mutable subobjects, we |
| 3237 | // cannot perform this read. (This only happens when performing a trivial |
| 3238 | // copy or assignment.) |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3239 | if (ObjType->isRecordType() && |
| 3240 | !Obj.mayAccessMutableMembers(Info, handler.AccessKind) && |
| 3241 | diagnoseMutableFields(Info, E, handler.AccessKind, ObjType)) |
| Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 3242 | return handler.failed(); |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3243 | } |
| Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 3244 | |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3245 | if (I == N) { |
| Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3246 | if (!handler.found(*O, ObjType)) |
| 3247 | return false; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3248 | |
| Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3249 | // If we modified a bit-field, truncate it to the right width. |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3250 | if (isModification(handler.AccessKind) && |
| Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3251 | LastField && LastField->isBitField() && |
| 3252 | !truncateBitfieldValue(Info, E, *O, LastField)) |
| 3253 | return false; |
| 3254 | |
| 3255 | return true; |
| 3256 | } |
| 3257 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3258 | LastField = nullptr; |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3259 | if (ObjType->isArrayType()) { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3260 | // Next subobject is an array element. |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3261 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3262 | assert(CAT && "vla in literal type?"); |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 3263 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3264 | if (CAT->getSize().ule(Index)) { |
| Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 3265 | // Note, it should not be possible to form a pointer with a valid |
| 3266 | // designator which points more than one past the end of the array. |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3267 | if (Info.getLangOpts().CPlusPlus11) |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3268 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3269 | << handler.AccessKind; |
| 3270 | else |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3271 | Info.FFDiag(E); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3272 | return handler.failed(); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3273 | } |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3274 | |
| 3275 | ObjType = CAT->getElementType(); |
| 3276 | |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3277 | if (O->getArrayInitializedElts() > Index) |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3278 | O = &O->getArrayInitializedElt(Index); |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3279 | else if (!isRead(handler.AccessKind)) { |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3280 | expandArray(*O, Index); |
| 3281 | O = &O->getArrayInitializedElt(Index); |
| 3282 | } else |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3283 | O = &O->getArrayFiller(); |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3284 | } else if (ObjType->isAnyComplexType()) { |
| 3285 | // Next subobject is a complex number. |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 3286 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3287 | if (Index > 1) { |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3288 | if (Info.getLangOpts().CPlusPlus11) |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3289 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3290 | << handler.AccessKind; |
| 3291 | else |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3292 | Info.FFDiag(E); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3293 | return handler.failed(); |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3294 | } |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3295 | |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3296 | ObjType = getSubobjectType( |
| 3297 | ObjType, ObjType->castAs<ComplexType>()->getElementType()); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3298 | |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3299 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 3300 | if (O->isComplexInt()) { |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3301 | return handler.found(Index ? O->getComplexIntImag() |
| 3302 | : O->getComplexIntReal(), ObjType); |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3303 | } else { |
| 3304 | assert(O->isComplexFloat()); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3305 | return handler.found(Index ? O->getComplexFloatImag() |
| 3306 | : O->getComplexFloatReal(), ObjType); |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3307 | } |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3308 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3309 | if (Field->isMutable() && |
| 3310 | !Obj.mayAccessMutableMembers(Info, handler.AccessKind)) { |
| 3311 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) |
| 3312 | << handler.AccessKind << Field; |
| Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 3313 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3314 | return handler.failed(); |
| Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3317 | // Next subobject is a class, struct or union field. |
| 3318 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 3319 | if (RD->isUnion()) { |
| 3320 | const FieldDecl *UnionField = O->getUnionField(); |
| 3321 | if (!UnionField || |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3322 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
| Richard Smith | b542602 | 2019-10-03 00:39:35 +0000 | [diff] [blame] | 3323 | if (I == N - 1 && handler.AccessKind == AK_Construct) { |
| 3324 | // Placement new onto an inactive union member makes it active. |
| 3325 | O->setUnion(Field, APValue()); |
| 3326 | } else { |
| 3327 | // FIXME: If O->getUnionValue() is absent, report that there's no |
| 3328 | // active union member rather than reporting the prior active union |
| 3329 | // member. We'll need to fix nullptr_t to not use APValue() as its |
| 3330 | // representation first. |
| 3331 | Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member) |
| 3332 | << handler.AccessKind << Field << !UnionField << UnionField; |
| 3333 | return handler.failed(); |
| 3334 | } |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3335 | } |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3336 | O = &O->getUnionValue(); |
| 3337 | } else |
| 3338 | O = &O->getStructField(Field->getFieldIndex()); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3339 | |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3340 | ObjType = getSubobjectType(ObjType, Field->getType(), Field->isMutable()); |
| Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 3341 | LastField = Field; |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3342 | if (Field->getType().isVolatileQualified()) |
| 3343 | VolatileField = Field; |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3344 | } else { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3345 | // Next subobject is a base class. |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3346 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 3347 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 3348 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3349 | |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 3350 | ObjType = getSubobjectType(ObjType, Info.Ctx.getRecordType(Base)); |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3351 | } |
| 3352 | } |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 3355 | namespace { |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3356 | struct ExtractSubobjectHandler { |
| 3357 | EvalInfo &Info; |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3358 | const Expr *E; |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3359 | APValue &Result; |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3360 | const AccessKinds AccessKind; |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3361 | |
| 3362 | typedef bool result_type; |
| 3363 | bool failed() { return false; } |
| 3364 | bool found(APValue &Subobj, QualType SubobjType) { |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3365 | Result = Subobj; |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3366 | if (AccessKind == AK_ReadObjectRepresentation) |
| 3367 | return true; |
| 3368 | return CheckFullyInitialized(Info, E->getExprLoc(), SubobjType, Result); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3369 | } |
| 3370 | bool found(APSInt &Value, QualType SubobjType) { |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3371 | Result = APValue(Value); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3372 | return true; |
| 3373 | } |
| 3374 | bool found(APFloat &Value, QualType SubobjType) { |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3375 | Result = APValue(Value); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3376 | return true; |
| 3377 | } |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3378 | }; |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3379 | } // end anonymous namespace |
| 3380 | |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3381 | /// Extract the designated sub-object of an rvalue. |
| 3382 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3383 | const CompleteObject &Obj, |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3384 | const SubobjectDesignator &Sub, APValue &Result, |
| 3385 | AccessKinds AK = AK_Read) { |
| 3386 | assert(AK == AK_Read || AK == AK_ReadObjectRepresentation); |
| 3387 | ExtractSubobjectHandler Handler = {Info, E, Result, AK}; |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3388 | return findSubobject(Info, E, Obj, Sub, Handler); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3391 | namespace { |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3392 | struct ModifySubobjectHandler { |
| 3393 | EvalInfo &Info; |
| 3394 | APValue &NewVal; |
| 3395 | const Expr *E; |
| 3396 | |
| 3397 | typedef bool result_type; |
| 3398 | static const AccessKinds AccessKind = AK_Assign; |
| 3399 | |
| 3400 | bool checkConst(QualType QT) { |
| 3401 | // Assigning to a const object has undefined behavior. |
| 3402 | if (QT.isConstQualified()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3403 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3404 | return false; |
| 3405 | } |
| 3406 | return true; |
| 3407 | } |
| 3408 | |
| 3409 | bool failed() { return false; } |
| 3410 | bool found(APValue &Subobj, QualType SubobjType) { |
| 3411 | if (!checkConst(SubobjType)) |
| 3412 | return false; |
| 3413 | // We've been given ownership of NewVal, so just swap it in. |
| 3414 | Subobj.swap(NewVal); |
| 3415 | return true; |
| 3416 | } |
| 3417 | bool found(APSInt &Value, QualType SubobjType) { |
| 3418 | if (!checkConst(SubobjType)) |
| 3419 | return false; |
| 3420 | if (!NewVal.isInt()) { |
| 3421 | // Maybe trying to write a cast pointer value into a complex? |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3422 | Info.FFDiag(E); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3423 | return false; |
| 3424 | } |
| 3425 | Value = NewVal.getInt(); |
| 3426 | return true; |
| 3427 | } |
| 3428 | bool found(APFloat &Value, QualType SubobjType) { |
| 3429 | if (!checkConst(SubobjType)) |
| 3430 | return false; |
| 3431 | Value = NewVal.getFloat(); |
| 3432 | return true; |
| 3433 | } |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3434 | }; |
| Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 3435 | } // end anonymous namespace |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3436 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3437 | const AccessKinds ModifySubobjectHandler::AccessKind; |
| 3438 | |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3439 | /// Update the designated sub-object of an rvalue to the given value. |
| 3440 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3441 | const CompleteObject &Obj, |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3442 | const SubobjectDesignator &Sub, |
| 3443 | APValue &NewVal) { |
| 3444 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3445 | return findSubobject(Info, E, Obj, Sub, Handler); |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3448 | /// Find the position where two subobject designators diverge, or equivalently |
| 3449 | /// the length of the common initial subsequence. |
| 3450 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 3451 | const SubobjectDesignator &A, |
| 3452 | const SubobjectDesignator &B, |
| 3453 | bool &WasArrayIndex) { |
| 3454 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 3455 | for (/**/; I != N; ++I) { |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3456 | if (!ObjType.isNull() && |
| 3457 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3458 | // Next subobject is an array element. |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 3459 | if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) { |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3460 | WasArrayIndex = true; |
| 3461 | return I; |
| 3462 | } |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3463 | if (ObjType->isAnyComplexType()) |
| 3464 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 3465 | else |
| 3466 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3467 | } else { |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 3468 | if (A.Entries[I].getAsBaseOrMember() != |
| 3469 | B.Entries[I].getAsBaseOrMember()) { |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3470 | WasArrayIndex = false; |
| 3471 | return I; |
| 3472 | } |
| 3473 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 3474 | // Next subobject is a field. |
| 3475 | ObjType = FD->getType(); |
| 3476 | else |
| 3477 | // Next subobject is a base class. |
| 3478 | ObjType = QualType(); |
| 3479 | } |
| 3480 | } |
| 3481 | WasArrayIndex = false; |
| 3482 | return I; |
| 3483 | } |
| 3484 | |
| 3485 | /// Determine whether the given subobject designators refer to elements of the |
| 3486 | /// same array object. |
| 3487 | static bool AreElementsOfSameArray(QualType ObjType, |
| 3488 | const SubobjectDesignator &A, |
| 3489 | const SubobjectDesignator &B) { |
| 3490 | if (A.Entries.size() != B.Entries.size()) |
| 3491 | return false; |
| 3492 | |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 3493 | bool IsArray = A.MostDerivedIsArrayElement; |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3494 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 3495 | // A is a subobject of the array element. |
| 3496 | return false; |
| 3497 | |
| 3498 | // If A (and B) designates an array element, the last entry will be the array |
| 3499 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 3500 | // of length 1' case, and the entire path must match. |
| 3501 | bool WasArrayIndex; |
| 3502 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 3503 | return CommonLength >= A.Entries.size() - IsArray; |
| 3504 | } |
| 3505 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3506 | /// Find the complete object to which an LValue refers. |
| Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 3507 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
| 3508 | AccessKinds AK, const LValue &LVal, |
| 3509 | QualType LValType) { |
| Richard Smith | 51ce844 | 2019-05-17 08:01:34 +0000 | [diff] [blame] | 3510 | if (LVal.InvalidBase) { |
| 3511 | Info.FFDiag(E); |
| 3512 | return CompleteObject(); |
| 3513 | } |
| 3514 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3515 | if (!LVal.Base) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3516 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3517 | return CompleteObject(); |
| 3518 | } |
| 3519 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3520 | CallStackFrame *Frame = nullptr; |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 3521 | unsigned Depth = 0; |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3522 | if (LVal.getLValueCallIndex()) { |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 3523 | std::tie(Frame, Depth) = |
| 3524 | Info.getCallFrameAndDepth(LVal.getLValueCallIndex()); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3525 | if (!Frame) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3526 | Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1) |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3527 | << AK << LVal.Base.is<const ValueDecl*>(); |
| 3528 | NoteLValueLocation(Info, LVal.Base); |
| 3529 | return CompleteObject(); |
| 3530 | } |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 3533 | bool IsAccess = isAnyAccess(AK); |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 3534 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3535 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 3536 | // is not a constant expression (even if the object is non-volatile). We also |
| 3537 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 3538 | // semantics. |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 3539 | if (isFormalAccess(AK) && LValType.isVolatileQualified()) { |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3540 | if (Info.getLangOpts().CPlusPlus) |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3541 | Info.FFDiag(E, diag::note_constexpr_access_volatile_type) |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3542 | << AK << LValType; |
| 3543 | else |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3544 | Info.FFDiag(E); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3545 | return CompleteObject(); |
| 3546 | } |
| 3547 | |
| 3548 | // Compute value storage location and type of base object. |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3549 | APValue *BaseVal = nullptr; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3550 | QualType BaseType = getType(LVal.Base); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3551 | |
| 3552 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
| 3553 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 3554 | // In C++11, constexpr, non-volatile variables initialized with constant |
| 3555 | // expressions are constant expressions too. Inside constexpr functions, |
| 3556 | // parameters are constant expressions even if they're non-const. |
| 3557 | // In C++1y, objects local to a constant expression (those with a Frame) are |
| 3558 | // both readable and writable inside constant expressions. |
| 3559 | // In C, such things can also be folded, although they are not ICEs. |
| 3560 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
| 3561 | if (VD) { |
| 3562 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 3563 | VD = VDef; |
| 3564 | } |
| 3565 | if (!VD || VD->isInvalidDecl()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3566 | Info.FFDiag(E); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3567 | return CompleteObject(); |
| 3568 | } |
| 3569 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3570 | // Unless we're looking at a local variable or argument in a constexpr call, |
| 3571 | // the variable we're reading must be const. |
| 3572 | if (!Frame) { |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3573 | if (Info.getLangOpts().CPlusPlus14 && |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3574 | lifetimeStartedInEvaluation(Info, LVal.Base)) { |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3575 | // OK, we can read and modify an object if we're in the process of |
| 3576 | // evaluating its initializer, because its lifetime began in this |
| 3577 | // evaluation. |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3578 | } else if (isModification(AK)) { |
| 3579 | // All the remaining cases do not permit modification of the object. |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3580 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3581 | return CompleteObject(); |
| George Burgess IV | b531698 | 2016-12-27 05:33:20 +0000 | [diff] [blame] | 3582 | } else if (VD->isConstexpr()) { |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3583 | // OK, we can read this variable. |
| 3584 | } else if (BaseType->isIntegralOrEnumerationType()) { |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3585 | // In OpenCL if a variable is in constant address space it is a const |
| 3586 | // value. |
| Xiuli Pan | 244e3f6 | 2016-06-07 04:34:00 +0000 | [diff] [blame] | 3587 | if (!(BaseType.isConstQualified() || |
| 3588 | (Info.getLangOpts().OpenCL && |
| 3589 | BaseType.getAddressSpace() == LangAS::opencl_constant))) { |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 3590 | if (!IsAccess) |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3591 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3592 | if (Info.getLangOpts().CPlusPlus) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3593 | Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3594 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3595 | } else { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3596 | Info.FFDiag(E); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3597 | } |
| 3598 | return CompleteObject(); |
| 3599 | } |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 3600 | } else if (!IsAccess) { |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3601 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3602 | } else if (BaseType->isFloatingType() && BaseType.isConstQualified()) { |
| 3603 | // We support folding of const floating-point types, in order to make |
| 3604 | // static const data members of such types (supported as an extension) |
| 3605 | // more useful. |
| 3606 | if (Info.getLangOpts().CPlusPlus11) { |
| 3607 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 3608 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3609 | } else { |
| 3610 | Info.CCEDiag(E); |
| 3611 | } |
| George Burgess IV | b531698 | 2016-12-27 05:33:20 +0000 | [diff] [blame] | 3612 | } else if (BaseType.isConstQualified() && VD->hasDefinition(Info.Ctx)) { |
| 3613 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr) << VD; |
| 3614 | // Keep evaluating to see what we can do. |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3615 | } else { |
| 3616 | // FIXME: Allow folding of values of any literal type in all languages. |
| Richard Smith | c0d04a2 | 2016-05-25 22:06:25 +0000 | [diff] [blame] | 3617 | if (Info.checkingPotentialConstantExpression() && |
| 3618 | VD->getType().isConstQualified() && !VD->hasDefinition(Info.Ctx)) { |
| 3619 | // The definition of this variable could be constexpr. We can't |
| 3620 | // access it right now, but may be able to in future. |
| 3621 | } else if (Info.getLangOpts().CPlusPlus11) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3622 | Info.FFDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3623 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3624 | } else { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3625 | Info.FFDiag(E); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3626 | } |
| 3627 | return CompleteObject(); |
| 3628 | } |
| 3629 | } |
| 3630 | |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3631 | if (!evaluateVarDeclInit(Info, E, VD, Frame, BaseVal, &LVal)) |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3632 | return CompleteObject(); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 3633 | } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) { |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 3634 | Optional<DynAlloc*> Alloc = Info.lookupDynamicAlloc(DA); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 3635 | if (!Alloc) { |
| 3636 | Info.FFDiag(E, diag::note_constexpr_access_deleted_object) << AK; |
| 3637 | return CompleteObject(); |
| 3638 | } |
| 3639 | return CompleteObject(LVal.Base, &(*Alloc)->Value, |
| 3640 | LVal.Base.getDynamicAllocType()); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3641 | } else { |
| 3642 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| 3643 | |
| 3644 | if (!Frame) { |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3645 | if (const MaterializeTemporaryExpr *MTE = |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 3646 | dyn_cast_or_null<MaterializeTemporaryExpr>(Base)) { |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3647 | assert(MTE->getStorageDuration() == SD_Static && |
| 3648 | "should have a frame for a non-global materialized temporary"); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3649 | |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3650 | // Per C++1y [expr.const]p2: |
| 3651 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
| 3652 | // - a [...] glvalue of integral or enumeration type that refers to |
| 3653 | // a non-volatile const object [...] |
| 3654 | // [...] |
| 3655 | // - a [...] glvalue of literal type that refers to a non-volatile |
| 3656 | // object whose lifetime began within the evaluation of e. |
| 3657 | // |
| 3658 | // C++11 misses the 'began within the evaluation of e' check and |
| 3659 | // instead allows all temporaries, including things like: |
| 3660 | // int &&r = 1; |
| 3661 | // int x = ++r; |
| 3662 | // constexpr int k = r; |
| Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3663 | // Therefore we use the C++14 rules in C++11 too. |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3664 | // |
| 3665 | // Note that temporaries whose lifetimes began while evaluating a |
| 3666 | // variable's constructor are not usable while evaluating the |
| 3667 | // corresponding destructor, not even if they're of const-qualified |
| 3668 | // types. |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3669 | if (!(BaseType.isConstQualified() && |
| 3670 | BaseType->isIntegralOrEnumerationType()) && |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 3671 | !lifetimeStartedInEvaluation(Info, LVal.Base)) { |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 3672 | if (!IsAccess) |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3673 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3674 | Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3675 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
| 3676 | return CompleteObject(); |
| 3677 | } |
| 3678 | |
| 3679 | BaseVal = Info.Ctx.getMaterializedTemporaryValue(MTE, false); |
| 3680 | assert(BaseVal && "got reference to unevaluated temporary"); |
| 3681 | } else { |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 3682 | if (!IsAccess) |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3683 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 3684 | APValue Val; |
| 3685 | LVal.moveInto(Val); |
| 3686 | Info.FFDiag(E, diag::note_constexpr_access_unreadable_object) |
| 3687 | << AK |
| 3688 | << Val.getAsString(Info.Ctx, |
| 3689 | Info.Ctx.getLValueReferenceType(LValType)); |
| 3690 | NoteLValueLocation(Info, LVal.Base); |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3691 | return CompleteObject(); |
| 3692 | } |
| 3693 | } else { |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3694 | BaseVal = Frame->getTemporary(Base, LVal.Base.getVersion()); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3695 | assert(BaseVal && "missing value for temporary"); |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3696 | } |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
| Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3699 | // In C++14, we can't safely access any mutable state when we might be |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 3700 | // evaluating after an unmodeled side effect. |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3701 | // |
| 3702 | // FIXME: Not all local state is mutable. Allow local constant subobjects |
| 3703 | // to be read here (but take care with 'mutable' fields). |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 3704 | if ((Frame && Info.getLangOpts().CPlusPlus14 && |
| 3705 | Info.EvalStatus.HasSideEffects) || |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3706 | (isModification(AK) && Depth < Info.SpeculativeEvaluationDepth)) |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3707 | return CompleteObject(); |
| 3708 | |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3709 | return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3712 | /// Perform an lvalue-to-rvalue conversion on the given glvalue. This |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3713 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
| 3714 | /// glvalue referred to by an entity of reference type. |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3715 | /// |
| 3716 | /// \param Info - Information about the ongoing evaluation. |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3717 | /// \param Conv - The expression for which we are performing the conversion. |
| 3718 | /// Used for diagnostics. |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3719 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
| 3720 | /// case of a non-class type). |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3721 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 3722 | /// \param RVal - The produced value will be placed here. |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3723 | /// \param WantObjectRepresentation - If true, we're looking for the object |
| 3724 | /// representation rather than the value, and in particular, |
| 3725 | /// there is no requirement that the result be fully initialized. |
| 3726 | static bool |
| 3727 | handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type, |
| 3728 | const LValue &LVal, APValue &RVal, |
| 3729 | bool WantObjectRepresentation = false) { |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3730 | if (LVal.Designator.Invalid) |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3731 | return false; |
| 3732 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3733 | // 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] | 3734 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 3735 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3736 | AccessKinds AK = |
| 3737 | WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read; |
| 3738 | |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3739 | if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) { |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3740 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 3741 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 3742 | // initializer until now for such expressions. Such an expression can't be |
| 3743 | // an ICE in C, so this only matters for fold. |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3744 | if (Type.isVolatileQualified()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3745 | Info.FFDiag(Conv); |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3746 | return false; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3747 | } |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3748 | APValue Lit; |
| 3749 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
| 3750 | return false; |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 3751 | CompleteObject LitObj(LVal.Base, &Lit, Base->getType()); |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3752 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal, AK); |
| Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 3753 | } else if (isa<StringLiteral>(Base) || isa<PredefinedExpr>(Base)) { |
| Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 3754 | // Special-case character extraction so we don't have to construct an |
| 3755 | // APValue for the whole string. |
| Eli Friedman | 041adb0 | 2019-02-09 02:22:17 +0000 | [diff] [blame] | 3756 | assert(LVal.Designator.Entries.size() <= 1 && |
| Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 3757 | "Can only read characters from string literals"); |
| Eli Friedman | 041adb0 | 2019-02-09 02:22:17 +0000 | [diff] [blame] | 3758 | if (LVal.Designator.Entries.empty()) { |
| 3759 | // Fail for now for LValue to RValue conversion of an array. |
| 3760 | // (This shouldn't show up in C/C++, but it could be triggered by a |
| 3761 | // weird EvaluateAsRValue call from a tool.) |
| 3762 | Info.FFDiag(Conv); |
| 3763 | return false; |
| 3764 | } |
| Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 3765 | if (LVal.Designator.isOnePastTheEnd()) { |
| 3766 | if (Info.getLangOpts().CPlusPlus11) |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3767 | Info.FFDiag(Conv, diag::note_constexpr_access_past_end) << AK; |
| Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 3768 | else |
| 3769 | Info.FFDiag(Conv); |
| 3770 | return false; |
| 3771 | } |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 3772 | uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex(); |
| Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 3773 | RVal = APValue(extractStringLiteralCharacter(Info, Base, CharIndex)); |
| 3774 | return true; |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3775 | } |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 3778 | CompleteObject Obj = findCompleteObject(Info, Conv, AK, LVal, Type); |
| 3779 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal, AK); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3783 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3784 | QualType LValType, APValue &Val) { |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3785 | if (LVal.Designator.Invalid) |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3786 | return false; |
| 3787 | |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3788 | if (!Info.getLangOpts().CPlusPlus14) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3789 | Info.FFDiag(E); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3790 | return false; |
| 3791 | } |
| 3792 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3793 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3794 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
| 3795 | } |
| 3796 | |
| 3797 | namespace { |
| 3798 | struct CompoundAssignSubobjectHandler { |
| 3799 | EvalInfo &Info; |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3800 | const Expr *E; |
| 3801 | QualType PromotedLHSType; |
| 3802 | BinaryOperatorKind Opcode; |
| 3803 | const APValue &RHS; |
| 3804 | |
| 3805 | static const AccessKinds AccessKind = AK_Assign; |
| 3806 | |
| 3807 | typedef bool result_type; |
| 3808 | |
| 3809 | bool checkConst(QualType QT) { |
| 3810 | // Assigning to a const object has undefined behavior. |
| 3811 | if (QT.isConstQualified()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3812 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3813 | return false; |
| 3814 | } |
| 3815 | return true; |
| 3816 | } |
| 3817 | |
| 3818 | bool failed() { return false; } |
| 3819 | bool found(APValue &Subobj, QualType SubobjType) { |
| 3820 | switch (Subobj.getKind()) { |
| 3821 | case APValue::Int: |
| 3822 | return found(Subobj.getInt(), SubobjType); |
| 3823 | case APValue::Float: |
| 3824 | return found(Subobj.getFloat(), SubobjType); |
| 3825 | case APValue::ComplexInt: |
| 3826 | case APValue::ComplexFloat: |
| 3827 | // FIXME: Implement complex compound assignment. |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3828 | Info.FFDiag(E); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3829 | return false; |
| 3830 | case APValue::LValue: |
| 3831 | return foundPointer(Subobj, SubobjType); |
| 3832 | default: |
| 3833 | // FIXME: can this happen? |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3834 | Info.FFDiag(E); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3835 | return false; |
| 3836 | } |
| 3837 | } |
| 3838 | bool found(APSInt &Value, QualType SubobjType) { |
| 3839 | if (!checkConst(SubobjType)) |
| 3840 | return false; |
| 3841 | |
| Tan S. B. | 9f935e8 | 2018-12-18 07:38:06 +0000 | [diff] [blame] | 3842 | if (!SubobjType->isIntegerType()) { |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3843 | // We don't support compound assignment on integer-cast-to-pointer |
| 3844 | // values. |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3845 | Info.FFDiag(E); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3846 | return false; |
| 3847 | } |
| 3848 | |
| Tan S. B. | 9f935e8 | 2018-12-18 07:38:06 +0000 | [diff] [blame] | 3849 | if (RHS.isInt()) { |
| 3850 | APSInt LHS = |
| 3851 | HandleIntToIntCast(Info, E, PromotedLHSType, SubobjType, Value); |
| 3852 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
| 3853 | return false; |
| 3854 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
| 3855 | return true; |
| 3856 | } else if (RHS.isFloat()) { |
| 3857 | APFloat FValue(0.0); |
| 3858 | return HandleIntToFloatCast(Info, E, SubobjType, Value, PromotedLHSType, |
| 3859 | FValue) && |
| 3860 | handleFloatFloatBinOp(Info, E, FValue, Opcode, RHS.getFloat()) && |
| 3861 | HandleFloatToIntCast(Info, E, PromotedLHSType, FValue, SubobjType, |
| 3862 | Value); |
| 3863 | } |
| 3864 | |
| 3865 | Info.FFDiag(E); |
| 3866 | return false; |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3867 | } |
| 3868 | bool found(APFloat &Value, QualType SubobjType) { |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3869 | return checkConst(SubobjType) && |
| 3870 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
| 3871 | Value) && |
| 3872 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
| 3873 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3874 | } |
| 3875 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 3876 | if (!checkConst(SubobjType)) |
| 3877 | return false; |
| 3878 | |
| 3879 | QualType PointeeType; |
| 3880 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 3881 | PointeeType = PT->getPointeeType(); |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3882 | |
| 3883 | if (PointeeType.isNull() || !RHS.isInt() || |
| 3884 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3885 | Info.FFDiag(E); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3886 | return false; |
| 3887 | } |
| 3888 | |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 3889 | APSInt Offset = RHS.getInt(); |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3890 | if (Opcode == BO_Sub) |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 3891 | negateAsSigned(Offset); |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3892 | |
| 3893 | LValue LVal; |
| 3894 | LVal.setFrom(Info.Ctx, Subobj); |
| 3895 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
| 3896 | return false; |
| 3897 | LVal.moveInto(Subobj); |
| 3898 | return true; |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3899 | } |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3900 | }; |
| 3901 | } // end anonymous namespace |
| 3902 | |
| 3903 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
| 3904 | |
| 3905 | /// Perform a compound assignment of LVal <op>= RVal. |
| 3906 | static bool handleCompoundAssignment( |
| 3907 | EvalInfo &Info, const Expr *E, |
| 3908 | const LValue &LVal, QualType LValType, QualType PromotedLValType, |
| 3909 | BinaryOperatorKind Opcode, const APValue &RVal) { |
| 3910 | if (LVal.Designator.Invalid) |
| 3911 | return false; |
| 3912 | |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3913 | if (!Info.getLangOpts().CPlusPlus14) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3914 | Info.FFDiag(E); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3915 | return false; |
| 3916 | } |
| 3917 | |
| 3918 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 3919 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
| 3920 | RVal }; |
| 3921 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 3922 | } |
| 3923 | |
| Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3924 | namespace { |
| 3925 | struct IncDecSubobjectHandler { |
| 3926 | EvalInfo &Info; |
| 3927 | const UnaryOperator *E; |
| 3928 | AccessKinds AccessKind; |
| 3929 | APValue *Old; |
| 3930 | |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3931 | typedef bool result_type; |
| 3932 | |
| 3933 | bool checkConst(QualType QT) { |
| 3934 | // Assigning to a const object has undefined behavior. |
| 3935 | if (QT.isConstQualified()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3936 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3937 | return false; |
| 3938 | } |
| 3939 | return true; |
| 3940 | } |
| 3941 | |
| 3942 | bool failed() { return false; } |
| 3943 | bool found(APValue &Subobj, QualType SubobjType) { |
| 3944 | // Stash the old value. Also clear Old, so we don't clobber it later |
| 3945 | // if we're post-incrementing a complex. |
| 3946 | if (Old) { |
| 3947 | *Old = Subobj; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3948 | Old = nullptr; |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
| 3951 | switch (Subobj.getKind()) { |
| 3952 | case APValue::Int: |
| 3953 | return found(Subobj.getInt(), SubobjType); |
| 3954 | case APValue::Float: |
| 3955 | return found(Subobj.getFloat(), SubobjType); |
| 3956 | case APValue::ComplexInt: |
| 3957 | return found(Subobj.getComplexIntReal(), |
| 3958 | SubobjType->castAs<ComplexType>()->getElementType() |
| 3959 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 3960 | case APValue::ComplexFloat: |
| 3961 | return found(Subobj.getComplexFloatReal(), |
| 3962 | SubobjType->castAs<ComplexType>()->getElementType() |
| 3963 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 3964 | case APValue::LValue: |
| 3965 | return foundPointer(Subobj, SubobjType); |
| 3966 | default: |
| 3967 | // FIXME: can this happen? |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3968 | Info.FFDiag(E); |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3969 | return false; |
| 3970 | } |
| 3971 | } |
| 3972 | bool found(APSInt &Value, QualType SubobjType) { |
| 3973 | if (!checkConst(SubobjType)) |
| 3974 | return false; |
| 3975 | |
| 3976 | if (!SubobjType->isIntegerType()) { |
| 3977 | // We don't support increment / decrement on integer-cast-to-pointer |
| 3978 | // values. |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3979 | Info.FFDiag(E); |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3980 | return false; |
| 3981 | } |
| 3982 | |
| 3983 | if (Old) *Old = APValue(Value); |
| 3984 | |
| 3985 | // bool arithmetic promotes to int, and the conversion back to bool |
| 3986 | // doesn't reduce mod 2^n, so special-case it. |
| 3987 | if (SubobjType->isBooleanType()) { |
| 3988 | if (AccessKind == AK_Increment) |
| 3989 | Value = 1; |
| 3990 | else |
| 3991 | Value = !Value; |
| 3992 | return true; |
| 3993 | } |
| 3994 | |
| 3995 | bool WasNegative = Value.isNegative(); |
| Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3996 | if (AccessKind == AK_Increment) { |
| 3997 | ++Value; |
| 3998 | |
| 3999 | if (!WasNegative && Value.isNegative() && E->canOverflow()) { |
| 4000 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
| 4001 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
| 4002 | } |
| 4003 | } else { |
| 4004 | --Value; |
| 4005 | |
| 4006 | if (WasNegative && !Value.isNegative() && E->canOverflow()) { |
| 4007 | unsigned BitWidth = Value.getBitWidth(); |
| 4008 | APSInt ActualValue(Value.sext(BitWidth + 1), /*IsUnsigned*/false); |
| 4009 | ActualValue.setBit(BitWidth); |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 4010 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4011 | } |
| 4012 | } |
| 4013 | return true; |
| 4014 | } |
| 4015 | bool found(APFloat &Value, QualType SubobjType) { |
| 4016 | if (!checkConst(SubobjType)) |
| 4017 | return false; |
| 4018 | |
| 4019 | if (Old) *Old = APValue(Value); |
| 4020 | |
| 4021 | APFloat One(Value.getSemantics(), 1); |
| 4022 | if (AccessKind == AK_Increment) |
| 4023 | Value.add(One, APFloat::rmNearestTiesToEven); |
| 4024 | else |
| 4025 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
| 4026 | return true; |
| 4027 | } |
| 4028 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 4029 | if (!checkConst(SubobjType)) |
| 4030 | return false; |
| 4031 | |
| 4032 | QualType PointeeType; |
| 4033 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 4034 | PointeeType = PT->getPointeeType(); |
| 4035 | else { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4036 | Info.FFDiag(E); |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4037 | return false; |
| 4038 | } |
| 4039 | |
| 4040 | LValue LVal; |
| 4041 | LVal.setFrom(Info.Ctx, Subobj); |
| 4042 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
| 4043 | AccessKind == AK_Increment ? 1 : -1)) |
| 4044 | return false; |
| 4045 | LVal.moveInto(Subobj); |
| 4046 | return true; |
| 4047 | } |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4048 | }; |
| 4049 | } // end anonymous namespace |
| 4050 | |
| 4051 | /// Perform an increment or decrement on LVal. |
| 4052 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
| 4053 | QualType LValType, bool IsIncrement, APValue *Old) { |
| 4054 | if (LVal.Designator.Invalid) |
| 4055 | return false; |
| 4056 | |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 4057 | if (!Info.getLangOpts().CPlusPlus14) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4058 | Info.FFDiag(E); |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4059 | return false; |
| 4060 | } |
| Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 4061 | |
| 4062 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
| 4063 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
| 4064 | IncDecSubobjectHandler Handler = {Info, cast<UnaryOperator>(E), AK, Old}; |
| 4065 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 4066 | } |
| 4067 | |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4068 | /// Build an lvalue for the object argument of a member function call. |
| 4069 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 4070 | LValue &This) { |
| Richard Smith | 5e18f4d | 2019-10-02 01:13:57 +0000 | [diff] [blame] | 4071 | if (Object->getType()->isPointerType() && Object->isRValue()) |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4072 | return EvaluatePointer(Object, This, Info); |
| 4073 | |
| 4074 | if (Object->isGLValue()) |
| 4075 | return EvaluateLValue(Object, This, Info); |
| 4076 | |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4077 | if (Object->getType()->isLiteralType(Info.Ctx)) |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4078 | return EvaluateTemporary(Object, This, Info); |
| 4079 | |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4080 | Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4081 | return false; |
| 4082 | } |
| 4083 | |
| 4084 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 4085 | /// lvalue referring to the result. |
| 4086 | /// |
| 4087 | /// \param Info - Information about the ongoing evaluation. |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4088 | /// \param LV - An lvalue referring to the base of the member pointer. |
| 4089 | /// \param RHS - The member pointer expression. |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4090 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 4091 | /// the resulting LValue subobject designator. This is not possible when |
| 4092 | /// creating a bound member function. |
| 4093 | /// \return The field or method declaration to which the member pointer refers, |
| 4094 | /// or 0 if evaluation fails. |
| 4095 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4096 | QualType LVType, |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4097 | LValue &LV, |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4098 | const Expr *RHS, |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4099 | bool IncludeMember = true) { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4100 | MemberPtr MemPtr; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4101 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4102 | return nullptr; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4103 | |
| 4104 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 4105 | // member value, the behavior is undefined. |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4106 | if (!MemPtr.getDecl()) { |
| 4107 | // FIXME: Specific diagnostic. |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4108 | Info.FFDiag(RHS); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4109 | return nullptr; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4110 | } |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4111 | |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4112 | if (MemPtr.isDerivedMember()) { |
| 4113 | // This is a member of some derived class. Truncate LV appropriately. |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4114 | // The end of the derived-to-base path for the base object must match the |
| 4115 | // derived-to-base path for the member pointer. |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4116 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4117 | LV.Designator.Entries.size()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4118 | Info.FFDiag(RHS); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4119 | return nullptr; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4120 | } |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4121 | unsigned PathLengthToMember = |
| 4122 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 4123 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 4124 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 4125 | LV.Designator.Entries[PathLengthToMember + I]); |
| 4126 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4127 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4128 | Info.FFDiag(RHS); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4129 | return nullptr; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4130 | } |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | // Truncate the lvalue to the appropriate derived class. |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4134 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4135 | PathLengthToMember)) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4136 | return nullptr; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4137 | } else if (!MemPtr.Path.empty()) { |
| 4138 | // Extend the LValue path with the member pointer's path. |
| 4139 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 4140 | MemPtr.Path.size() + IncludeMember); |
| 4141 | |
| 4142 | // Walk down to the appropriate base class. |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4143 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 4144 | LVType = PT->getPointeeType(); |
| 4145 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 4146 | assert(RD && "member pointer access on non-class-type expression"); |
| 4147 | // The first class in the path is that of the lvalue. |
| 4148 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 4149 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4150 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4151 | return nullptr; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4152 | RD = Base; |
| 4153 | } |
| 4154 | // Finally cast to the class containing the member. |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4155 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
| 4156 | MemPtr.getContainingRecord())) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4157 | return nullptr; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | // Add the member. Note that we cannot build bound member functions here. |
| 4161 | if (IncludeMember) { |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4162 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4163 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4164 | return nullptr; |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4165 | } else if (const IndirectFieldDecl *IFD = |
| 4166 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4167 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4168 | return nullptr; |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4169 | } else { |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4170 | llvm_unreachable("can't construct reference to bound member function"); |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4171 | } |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | return MemPtr.getDecl(); |
| 4175 | } |
| 4176 | |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4177 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 4178 | const BinaryOperator *BO, |
| 4179 | LValue &LV, |
| 4180 | bool IncludeMember = true) { |
| 4181 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 4182 | |
| 4183 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 4184 | if (Info.noteFailure()) { |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4185 | MemberPtr MemPtr; |
| 4186 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
| 4187 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4188 | return nullptr; |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
| 4192 | BO->getRHS(), IncludeMember); |
| 4193 | } |
| 4194 | |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4195 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 4196 | /// the provided lvalue, which currently refers to the base object. |
| 4197 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 4198 | LValue &Result) { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4199 | SubobjectDesignator &D = Result.Designator; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4200 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4201 | return false; |
| 4202 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4203 | QualType TargetQT = E->getType(); |
| 4204 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 4205 | TargetQT = PT->getPointeeType(); |
| 4206 | |
| 4207 | // Check this cast lands within the final derived-to-base subobject path. |
| 4208 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4209 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4210 | << D.MostDerivedType << TargetQT; |
| 4211 | return false; |
| 4212 | } |
| 4213 | |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4214 | // Check the type of the final cast. We don't need to check the path, |
| 4215 | // since a cast can only be formed if the path is unique. |
| 4216 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4217 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 4218 | const CXXRecordDecl *FinalType; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4219 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 4220 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 4221 | else |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4222 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4223 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4224 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4225 | << D.MostDerivedType << TargetQT; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4226 | return false; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4227 | } |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4228 | |
| 4229 | // Truncate the lvalue to the appropriate derived class. |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4230 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 4233 | /// Get the value to use for a default-initialized object of type T. |
| 4234 | static APValue getDefaultInitValue(QualType T) { |
| 4235 | if (auto *RD = T->getAsCXXRecordDecl()) { |
| 4236 | if (RD->isUnion()) |
| 4237 | return APValue((const FieldDecl*)nullptr); |
| 4238 | |
| 4239 | APValue Struct(APValue::UninitStruct(), RD->getNumBases(), |
| 4240 | std::distance(RD->field_begin(), RD->field_end())); |
| 4241 | |
| 4242 | unsigned Index = 0; |
| 4243 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 4244 | End = RD->bases_end(); I != End; ++I, ++Index) |
| 4245 | Struct.getStructBase(Index) = getDefaultInitValue(I->getType()); |
| 4246 | |
| 4247 | for (const auto *I : RD->fields()) { |
| 4248 | if (I->isUnnamedBitfield()) |
| 4249 | continue; |
| 4250 | Struct.getStructField(I->getFieldIndex()) = |
| 4251 | getDefaultInitValue(I->getType()); |
| 4252 | } |
| 4253 | return Struct; |
| 4254 | } |
| 4255 | |
| 4256 | if (auto *AT = |
| 4257 | dyn_cast_or_null<ConstantArrayType>(T->getAsArrayTypeUnsafe())) { |
| 4258 | APValue Array(APValue::UninitArray(), 0, AT->getSize().getZExtValue()); |
| 4259 | if (Array.hasArrayFiller()) |
| 4260 | Array.getArrayFiller() = getDefaultInitValue(AT->getElementType()); |
| 4261 | return Array; |
| 4262 | } |
| 4263 | |
| 4264 | return APValue::IndeterminateValue(); |
| 4265 | } |
| 4266 | |
| Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 4267 | namespace { |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4268 | enum EvalStmtResult { |
| 4269 | /// Evaluation failed. |
| 4270 | ESR_Failed, |
| 4271 | /// Hit a 'return' statement. |
| 4272 | ESR_Returned, |
| 4273 | /// Evaluation succeeded. |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4274 | ESR_Succeeded, |
| 4275 | /// Hit a 'continue' statement. |
| 4276 | ESR_Continue, |
| 4277 | /// Hit a 'break' statement. |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4278 | ESR_Break, |
| 4279 | /// Still scanning for 'case' or 'default' statement. |
| 4280 | ESR_CaseNotFound |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4281 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4282 | } |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4283 | |
| Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 4284 | static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) { |
| 4285 | // We don't need to evaluate the initializer for a static local. |
| 4286 | if (!VD->hasLocalStorage()) |
| 4287 | return true; |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4288 | |
| Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 4289 | LValue Result; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4290 | APValue &Val = |
| 4291 | Info.CurrentCall->createTemporary(VD, VD->getType(), true, Result); |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4292 | |
| Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 4293 | const Expr *InitE = VD->getInit(); |
| 4294 | if (!InitE) { |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 4295 | Val = getDefaultInitValue(VD->getType()); |
| 4296 | return true; |
| Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 4297 | } |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4298 | |
| Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 4299 | if (InitE->isValueDependent()) |
| 4300 | return false; |
| Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 4301 | |
| Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 4302 | if (!EvaluateInPlace(Val, Info, Result, InitE)) { |
| 4303 | // Wipe out any partially-computed value, to allow tracking that this |
| 4304 | // evaluation failed. |
| 4305 | Val = APValue(); |
| 4306 | return false; |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | return true; |
| 4310 | } |
| 4311 | |
| Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 4312 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
| 4313 | bool OK = true; |
| 4314 | |
| 4315 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4316 | OK &= EvaluateVarDecl(Info, VD); |
| 4317 | |
| 4318 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(D)) |
| 4319 | for (auto *BD : DD->bindings()) |
| 4320 | if (auto *VD = BD->getHoldingVar()) |
| 4321 | OK &= EvaluateDecl(Info, VD); |
| 4322 | |
| 4323 | return OK; |
| 4324 | } |
| 4325 | |
| 4326 | |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4327 | /// Evaluate a condition (either a variable declaration or an expression). |
| 4328 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
| 4329 | const Expr *Cond, bool &Result) { |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4330 | FullExpressionRAII Scope(Info); |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4331 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
| 4332 | return false; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4333 | if (!EvaluateAsBooleanCondition(Cond, Result, Info)) |
| 4334 | return false; |
| 4335 | return Scope.destroy(); |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
| Richard Smith | 8921007 | 2016-04-04 23:29:43 +0000 | [diff] [blame] | 4338 | namespace { |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4339 | /// A location where the result (returned value) of evaluating a |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4340 | /// statement should be stored. |
| 4341 | struct StmtResult { |
| 4342 | /// The APValue that should be filled in with the returned value. |
| 4343 | APValue &Value; |
| 4344 | /// The location containing the result, if any (used to support RVO). |
| 4345 | const LValue *Slot; |
| 4346 | }; |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4347 | |
| 4348 | struct TempVersionRAII { |
| 4349 | CallStackFrame &Frame; |
| 4350 | |
| 4351 | TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) { |
| 4352 | Frame.pushTempVersion(); |
| 4353 | } |
| 4354 | |
| 4355 | ~TempVersionRAII() { |
| 4356 | Frame.popTempVersion(); |
| 4357 | } |
| 4358 | }; |
| 4359 | |
| Richard Smith | 8921007 | 2016-04-04 23:29:43 +0000 | [diff] [blame] | 4360 | } |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4361 | |
| 4362 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4363 | const Stmt *S, |
| 4364 | const SwitchCase *SC = nullptr); |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4365 | |
| 4366 | /// Evaluate the body of a loop, and translate the result as appropriate. |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4367 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4368 | const Stmt *Body, |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4369 | const SwitchCase *Case = nullptr) { |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4370 | BlockScopeRAII Scope(Info); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4371 | |
| 4372 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case); |
| 4373 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
| 4374 | ESR = ESR_Failed; |
| 4375 | |
| 4376 | switch (ESR) { |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4377 | case ESR_Break: |
| 4378 | return ESR_Succeeded; |
| 4379 | case ESR_Succeeded: |
| 4380 | case ESR_Continue: |
| 4381 | return ESR_Continue; |
| 4382 | case ESR_Failed: |
| 4383 | case ESR_Returned: |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4384 | case ESR_CaseNotFound: |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4385 | return ESR; |
| 4386 | } |
| Hans Wennborg | 9242bd1 | 2013-05-06 15:13:34 +0000 | [diff] [blame] | 4387 | llvm_unreachable("Invalid EvalStmtResult!"); |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4390 | /// Evaluate a switch statement. |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4391 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4392 | const SwitchStmt *SS) { |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4393 | BlockScopeRAII Scope(Info); |
| 4394 | |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4395 | // Evaluate the switch condition. |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4396 | APSInt Value; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4397 | { |
| Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 4398 | if (const Stmt *Init = SS->getInit()) { |
| 4399 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4400 | if (ESR != ESR_Succeeded) { |
| 4401 | if (ESR != ESR_Failed && !Scope.destroy()) |
| 4402 | ESR = ESR_Failed; |
| Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 4403 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4404 | } |
| Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 4405 | } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4406 | |
| 4407 | FullExpressionRAII CondScope(Info); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4408 | if (SS->getConditionVariable() && |
| 4409 | !EvaluateDecl(Info, SS->getConditionVariable())) |
| 4410 | return ESR_Failed; |
| 4411 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
| 4412 | return ESR_Failed; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4413 | if (!CondScope.destroy()) |
| 4414 | return ESR_Failed; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4415 | } |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4416 | |
| 4417 | // Find the switch case corresponding to the value of the condition. |
| 4418 | // FIXME: Cache this lookup. |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4419 | const SwitchCase *Found = nullptr; |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4420 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 4421 | SC = SC->getNextSwitchCase()) { |
| 4422 | if (isa<DefaultStmt>(SC)) { |
| 4423 | Found = SC; |
| 4424 | continue; |
| 4425 | } |
| 4426 | |
| 4427 | const CaseStmt *CS = cast<CaseStmt>(SC); |
| 4428 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
| 4429 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
| 4430 | : LHS; |
| 4431 | if (LHS <= Value && Value <= RHS) { |
| 4432 | Found = SC; |
| 4433 | break; |
| 4434 | } |
| 4435 | } |
| 4436 | |
| 4437 | if (!Found) |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4438 | return Scope.destroy() ? ESR_Failed : ESR_Succeeded; |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4439 | |
| 4440 | // Search the switch body for the switch case and evaluate it from there. |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4441 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found); |
| 4442 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
| 4443 | return ESR_Failed; |
| 4444 | |
| 4445 | switch (ESR) { |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4446 | case ESR_Break: |
| 4447 | return ESR_Succeeded; |
| 4448 | case ESR_Succeeded: |
| 4449 | case ESR_Continue: |
| 4450 | case ESR_Failed: |
| 4451 | case ESR_Returned: |
| 4452 | return ESR; |
| 4453 | case ESR_CaseNotFound: |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4454 | // This can only happen if the switch case is nested within a statement |
| 4455 | // expression. We have no intention of supporting that. |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4456 | Info.FFDiag(Found->getBeginLoc(), |
| 4457 | diag::note_constexpr_stmt_expr_unsupported); |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4458 | return ESR_Failed; |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4459 | } |
| Richard Smith | f8cf9d4 | 2013-05-13 20:33:30 +0000 | [diff] [blame] | 4460 | llvm_unreachable("Invalid EvalStmtResult!"); |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4463 | // Evaluate a statement. |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4464 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4465 | const Stmt *S, const SwitchCase *Case) { |
| Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 4466 | if (!Info.nextStep(S)) |
| 4467 | return ESR_Failed; |
| 4468 | |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4469 | // If we're hunting down a 'case' or 'default' label, recurse through |
| 4470 | // substatements until we hit the label. |
| 4471 | if (Case) { |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4472 | switch (S->getStmtClass()) { |
| 4473 | case Stmt::CompoundStmtClass: |
| 4474 | // FIXME: Precompute which substatement of a compound statement we |
| 4475 | // would jump to, and go straight there rather than performing a |
| 4476 | // linear scan each time. |
| 4477 | case Stmt::LabelStmtClass: |
| 4478 | case Stmt::AttributedStmtClass: |
| 4479 | case Stmt::DoStmtClass: |
| 4480 | break; |
| 4481 | |
| 4482 | case Stmt::CaseStmtClass: |
| 4483 | case Stmt::DefaultStmtClass: |
| 4484 | if (Case == S) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4485 | Case = nullptr; |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4486 | break; |
| 4487 | |
| 4488 | case Stmt::IfStmtClass: { |
| 4489 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
| 4490 | // straight there rather than scanning both sides. |
| 4491 | const IfStmt *IS = cast<IfStmt>(S); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4492 | |
| 4493 | // Wrap the evaluation in a block scope, in case it's a DeclStmt |
| 4494 | // preceded by our switch label. |
| 4495 | BlockScopeRAII Scope(Info); |
| 4496 | |
| Richard Smith | 397a686 | 2019-09-20 23:08:59 +0000 | [diff] [blame] | 4497 | // Step into the init statement in case it brings an (uninitialized) |
| 4498 | // variable into scope. |
| 4499 | if (const Stmt *Init = IS->getInit()) { |
| 4500 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init, Case); |
| 4501 | if (ESR != ESR_CaseNotFound) { |
| 4502 | assert(ESR != ESR_Succeeded); |
| 4503 | return ESR; |
| 4504 | } |
| 4505 | } |
| 4506 | |
| 4507 | // Condition variable must be initialized if it exists. |
| 4508 | // FIXME: We can skip evaluating the body if there's a condition |
| 4509 | // variable, as there can't be any case labels within it. |
| 4510 | // (The same is true for 'for' statements.) |
| 4511 | |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4512 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4513 | if (ESR == ESR_Failed) |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4514 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4515 | if (ESR != ESR_CaseNotFound) |
| 4516 | return Scope.destroy() ? ESR : ESR_Failed; |
| 4517 | if (!IS->getElse()) |
| 4518 | return ESR_CaseNotFound; |
| 4519 | |
| 4520 | ESR = EvaluateStmt(Result, Info, IS->getElse(), Case); |
| 4521 | if (ESR == ESR_Failed) |
| 4522 | return ESR; |
| 4523 | if (ESR != ESR_CaseNotFound) |
| 4524 | return Scope.destroy() ? ESR : ESR_Failed; |
| 4525 | return ESR_CaseNotFound; |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | case Stmt::WhileStmtClass: { |
| 4529 | EvalStmtResult ESR = |
| 4530 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
| 4531 | if (ESR != ESR_Continue) |
| 4532 | return ESR; |
| 4533 | break; |
| 4534 | } |
| 4535 | |
| 4536 | case Stmt::ForStmtClass: { |
| 4537 | const ForStmt *FS = cast<ForStmt>(S); |
| Richard Smith | 397a686 | 2019-09-20 23:08:59 +0000 | [diff] [blame] | 4538 | BlockScopeRAII Scope(Info); |
| 4539 | |
| 4540 | // Step into the init statement in case it brings an (uninitialized) |
| 4541 | // variable into scope. |
| 4542 | if (const Stmt *Init = FS->getInit()) { |
| 4543 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init, Case); |
| 4544 | if (ESR != ESR_CaseNotFound) { |
| 4545 | assert(ESR != ESR_Succeeded); |
| 4546 | return ESR; |
| 4547 | } |
| 4548 | } |
| 4549 | |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4550 | EvalStmtResult ESR = |
| 4551 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
| 4552 | if (ESR != ESR_Continue) |
| 4553 | return ESR; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4554 | if (FS->getInc()) { |
| 4555 | FullExpressionRAII IncScope(Info); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4556 | if (!EvaluateIgnoredValue(Info, FS->getInc()) || !IncScope.destroy()) |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4557 | return ESR_Failed; |
| 4558 | } |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4559 | break; |
| 4560 | } |
| 4561 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 4562 | case Stmt::DeclStmtClass: { |
| 4563 | // Start the lifetime of any uninitialized variables we encounter. They |
| 4564 | // might be used by the selected branch of the switch. |
| 4565 | const DeclStmt *DS = cast<DeclStmt>(S); |
| 4566 | for (const auto *D : DS->decls()) { |
| 4567 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 4568 | if (VD->hasLocalStorage() && !VD->getInit()) |
| 4569 | if (!EvaluateVarDecl(Info, VD)) |
| 4570 | return ESR_Failed; |
| 4571 | // FIXME: If the variable has initialization that can't be jumped |
| 4572 | // over, bail out of any immediately-surrounding compound-statement |
| 4573 | // too. There can't be any case labels here. |
| 4574 | } |
| 4575 | } |
| 4576 | return ESR_CaseNotFound; |
| 4577 | } |
| 4578 | |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4579 | default: |
| 4580 | return ESR_CaseNotFound; |
| 4581 | } |
| 4582 | } |
| 4583 | |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4584 | switch (S->getStmtClass()) { |
| 4585 | default: |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4586 | if (const Expr *E = dyn_cast<Expr>(S)) { |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4587 | // Don't bother evaluating beyond an expression-statement which couldn't |
| 4588 | // be evaluated. |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4589 | // FIXME: Do we need the FullExpressionRAII object here? |
| 4590 | // VisitExprWithCleanups should create one when necessary. |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4591 | FullExpressionRAII Scope(Info); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4592 | if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy()) |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4593 | return ESR_Failed; |
| 4594 | return ESR_Succeeded; |
| 4595 | } |
| 4596 | |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4597 | Info.FFDiag(S->getBeginLoc()); |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4598 | return ESR_Failed; |
| 4599 | |
| 4600 | case Stmt::NullStmtClass: |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4601 | return ESR_Succeeded; |
| 4602 | |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4603 | case Stmt::DeclStmtClass: { |
| 4604 | const DeclStmt *DS = cast<DeclStmt>(S); |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 4605 | for (const auto *D : DS->decls()) { |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4606 | // Each declaration initialization is its own full-expression. |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4607 | FullExpressionRAII Scope(Info); |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 4608 | if (!EvaluateDecl(Info, D) && !Info.noteFailure()) |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4609 | return ESR_Failed; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4610 | if (!Scope.destroy()) |
| 4611 | return ESR_Failed; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4612 | } |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4613 | return ESR_Succeeded; |
| 4614 | } |
| 4615 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4616 | case Stmt::ReturnStmtClass: { |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4617 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4618 | FullExpressionRAII Scope(Info); |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4619 | if (RetExpr && |
| 4620 | !(Result.Slot |
| 4621 | ? EvaluateInPlace(Result.Value, Info, *Result.Slot, RetExpr) |
| 4622 | : Evaluate(Result.Value, Info, RetExpr))) |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4623 | return ESR_Failed; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4624 | return Scope.destroy() ? ESR_Returned : ESR_Failed; |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4625 | } |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4626 | |
| 4627 | case Stmt::CompoundStmtClass: { |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4628 | BlockScopeRAII Scope(Info); |
| 4629 | |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4630 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
| Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 4631 | for (const auto *BI : CS->body()) { |
| 4632 | EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case); |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4633 | if (ESR == ESR_Succeeded) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4634 | Case = nullptr; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4635 | else if (ESR != ESR_CaseNotFound) { |
| 4636 | if (ESR != ESR_Failed && !Scope.destroy()) |
| 4637 | return ESR_Failed; |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4638 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4639 | } |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4640 | } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4641 | if (Case) |
| 4642 | return ESR_CaseNotFound; |
| 4643 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4644 | } |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4645 | |
| 4646 | case Stmt::IfStmtClass: { |
| 4647 | const IfStmt *IS = cast<IfStmt>(S); |
| 4648 | |
| 4649 | // Evaluate the condition, as either a var decl or as an expression. |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4650 | BlockScopeRAII Scope(Info); |
| Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 4651 | if (const Stmt *Init = IS->getInit()) { |
| 4652 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4653 | if (ESR != ESR_Succeeded) { |
| 4654 | if (ESR != ESR_Failed && !Scope.destroy()) |
| 4655 | return ESR_Failed; |
| Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 4656 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4657 | } |
| Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 4658 | } |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4659 | bool Cond; |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4660 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4661 | return ESR_Failed; |
| 4662 | |
| 4663 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
| 4664 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4665 | if (ESR != ESR_Succeeded) { |
| 4666 | if (ESR != ESR_Failed && !Scope.destroy()) |
| 4667 | return ESR_Failed; |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4668 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4669 | } |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4670 | } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4671 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4672 | } |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4673 | |
| 4674 | case Stmt::WhileStmtClass: { |
| 4675 | const WhileStmt *WS = cast<WhileStmt>(S); |
| 4676 | while (true) { |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4677 | BlockScopeRAII Scope(Info); |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4678 | bool Continue; |
| 4679 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
| 4680 | Continue)) |
| 4681 | return ESR_Failed; |
| 4682 | if (!Continue) |
| 4683 | break; |
| 4684 | |
| 4685 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4686 | if (ESR != ESR_Continue) { |
| 4687 | if (ESR != ESR_Failed && !Scope.destroy()) |
| 4688 | return ESR_Failed; |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4689 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4690 | } |
| 4691 | if (!Scope.destroy()) |
| 4692 | return ESR_Failed; |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4693 | } |
| 4694 | return ESR_Succeeded; |
| 4695 | } |
| 4696 | |
| 4697 | case Stmt::DoStmtClass: { |
| 4698 | const DoStmt *DS = cast<DoStmt>(S); |
| 4699 | bool Continue; |
| 4700 | do { |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4701 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4702 | if (ESR != ESR_Continue) |
| 4703 | return ESR; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4704 | Case = nullptr; |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4705 | |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4706 | FullExpressionRAII CondScope(Info); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4707 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info) || |
| 4708 | !CondScope.destroy()) |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4709 | return ESR_Failed; |
| 4710 | } while (Continue); |
| 4711 | return ESR_Succeeded; |
| 4712 | } |
| 4713 | |
| 4714 | case Stmt::ForStmtClass: { |
| 4715 | const ForStmt *FS = cast<ForStmt>(S); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4716 | BlockScopeRAII ForScope(Info); |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4717 | if (FS->getInit()) { |
| 4718 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4719 | if (ESR != ESR_Succeeded) { |
| 4720 | if (ESR != ESR_Failed && !ForScope.destroy()) |
| 4721 | return ESR_Failed; |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4722 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4723 | } |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4724 | } |
| 4725 | while (true) { |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4726 | BlockScopeRAII IterScope(Info); |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4727 | bool Continue = true; |
| 4728 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
| 4729 | FS->getCond(), Continue)) |
| 4730 | return ESR_Failed; |
| 4731 | if (!Continue) |
| 4732 | break; |
| 4733 | |
| 4734 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4735 | if (ESR != ESR_Continue) { |
| 4736 | if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy())) |
| 4737 | return ESR_Failed; |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4738 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4739 | } |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4740 | |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4741 | if (FS->getInc()) { |
| 4742 | FullExpressionRAII IncScope(Info); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4743 | if (!EvaluateIgnoredValue(Info, FS->getInc()) || !IncScope.destroy()) |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4744 | return ESR_Failed; |
| 4745 | } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4746 | |
| 4747 | if (!IterScope.destroy()) |
| 4748 | return ESR_Failed; |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4749 | } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4750 | return ForScope.destroy() ? ESR_Succeeded : ESR_Failed; |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4753 | case Stmt::CXXForRangeStmtClass: { |
| 4754 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4755 | BlockScopeRAII Scope(Info); |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4756 | |
| Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 4757 | // Evaluate the init-statement if present. |
| 4758 | if (FS->getInit()) { |
| 4759 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4760 | if (ESR != ESR_Succeeded) { |
| 4761 | if (ESR != ESR_Failed && !Scope.destroy()) |
| 4762 | return ESR_Failed; |
| Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 4763 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4764 | } |
| Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 4765 | } |
| 4766 | |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4767 | // Initialize the __range variable. |
| 4768 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4769 | if (ESR != ESR_Succeeded) { |
| 4770 | if (ESR != ESR_Failed && !Scope.destroy()) |
| 4771 | return ESR_Failed; |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4772 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4773 | } |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4774 | |
| 4775 | // Create the __begin and __end iterators. |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 4776 | ESR = EvaluateStmt(Result, Info, FS->getBeginStmt()); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4777 | if (ESR != ESR_Succeeded) { |
| 4778 | if (ESR != ESR_Failed && !Scope.destroy()) |
| 4779 | return ESR_Failed; |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 4780 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4781 | } |
| Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 4782 | ESR = EvaluateStmt(Result, Info, FS->getEndStmt()); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4783 | if (ESR != ESR_Succeeded) { |
| 4784 | if (ESR != ESR_Failed && !Scope.destroy()) |
| 4785 | return ESR_Failed; |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4786 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4787 | } |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4788 | |
| 4789 | while (true) { |
| 4790 | // Condition: __begin != __end. |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4791 | { |
| 4792 | bool Continue = true; |
| 4793 | FullExpressionRAII CondExpr(Info); |
| 4794 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
| 4795 | return ESR_Failed; |
| 4796 | if (!Continue) |
| 4797 | break; |
| 4798 | } |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4799 | |
| 4800 | // User's variable declaration, initialized by *__begin. |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4801 | BlockScopeRAII InnerScope(Info); |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4802 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4803 | if (ESR != ESR_Succeeded) { |
| 4804 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
| 4805 | return ESR_Failed; |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4806 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4807 | } |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4808 | |
| 4809 | // Loop body. |
| 4810 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4811 | if (ESR != ESR_Continue) { |
| 4812 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
| 4813 | return ESR_Failed; |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4814 | return ESR; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4815 | } |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4816 | |
| 4817 | // Increment: ++__begin |
| 4818 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 4819 | return ESR_Failed; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4820 | |
| 4821 | if (!InnerScope.destroy()) |
| 4822 | return ESR_Failed; |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 4825 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
| Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4828 | case Stmt::SwitchStmtClass: |
| 4829 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
| 4830 | |
| Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4831 | case Stmt::ContinueStmtClass: |
| 4832 | return ESR_Continue; |
| 4833 | |
| 4834 | case Stmt::BreakStmtClass: |
| 4835 | return ESR_Break; |
| Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4836 | |
| 4837 | case Stmt::LabelStmtClass: |
| 4838 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
| 4839 | |
| 4840 | case Stmt::AttributedStmtClass: |
| 4841 | // As a general principle, C++11 attributes can be ignored without |
| 4842 | // any semantic impact. |
| 4843 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
| 4844 | Case); |
| 4845 | |
| 4846 | case Stmt::CaseStmtClass: |
| 4847 | case Stmt::DefaultStmtClass: |
| 4848 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
| Bruno Cardoso Lopes | 5c1399a | 2018-12-10 19:03:12 +0000 | [diff] [blame] | 4849 | case Stmt::CXXTryStmtClass: |
| 4850 | // Evaluate try blocks by evaluating all sub statements. |
| 4851 | return EvaluateStmt(Result, Info, cast<CXXTryStmt>(S)->getTryBlock(), Case); |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4852 | } |
| 4853 | } |
| 4854 | |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4855 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 4856 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 4857 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 4858 | /// so we need special handling. |
| 4859 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4860 | const CXXConstructorDecl *CD, |
| 4861 | bool IsValueInitialization) { |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4862 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 4863 | return false; |
| 4864 | |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 4865 | // Value-initialization does not call a trivial default constructor, so such a |
| 4866 | // call is a core constant expression whether or not the constructor is |
| 4867 | // constexpr. |
| 4868 | if (!CD->isConstexpr() && !IsValueInitialization) { |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4869 | if (Info.getLangOpts().CPlusPlus11) { |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 4870 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 4871 | // we should be much more explicit about why it's not constexpr. |
| 4872 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 4873 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 4874 | Info.Note(CD->getLocation(), diag::note_declared_at); |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4875 | } else { |
| 4876 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 4877 | } |
| 4878 | } |
| 4879 | return true; |
| 4880 | } |
| 4881 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4882 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 4883 | /// expression. |
| 4884 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 4885 | const FunctionDecl *Declaration, |
| Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 4886 | const FunctionDecl *Definition, |
| 4887 | const Stmt *Body) { |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4888 | // Potential constant expressions can contain calls to declared, but not yet |
| 4889 | // defined, constexpr functions. |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4890 | if (Info.checkingPotentialConstantExpression() && !Definition && |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4891 | Declaration->isConstexpr()) |
| 4892 | return false; |
| 4893 | |
| James Y Knight | c7d3e60 | 2018-10-05 17:49:48 +0000 | [diff] [blame] | 4894 | // Bail out if the function declaration itself is invalid. We will |
| 4895 | // have produced a relevant diagnostic while parsing it, so just |
| 4896 | // note the problematic sub-expression. |
| 4897 | if (Declaration->isInvalidDecl()) { |
| 4898 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| Richard Smith | 0838f3a | 2013-05-14 05:18:44 +0000 | [diff] [blame] | 4899 | return false; |
| James Y Knight | c7d3e60 | 2018-10-05 17:49:48 +0000 | [diff] [blame] | 4900 | } |
| Richard Smith | 0838f3a | 2013-05-14 05:18:44 +0000 | [diff] [blame] | 4901 | |
| Richard Smith | d9c6b03 | 2019-05-09 19:45:49 +0000 | [diff] [blame] | 4902 | // DR1872: An instantiated virtual constexpr function can't be called in a |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 4903 | // constant expression (prior to C++20). We can still constant-fold such a |
| 4904 | // call. |
| 4905 | if (!Info.Ctx.getLangOpts().CPlusPlus2a && isa<CXXMethodDecl>(Declaration) && |
| 4906 | cast<CXXMethodDecl>(Declaration)->isVirtual()) |
| 4907 | Info.CCEDiag(CallLoc, diag::note_constexpr_virtual_call); |
| 4908 | |
| 4909 | if (Definition && Definition->isInvalidDecl()) { |
| 4910 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| Richard Smith | d9c6b03 | 2019-05-09 19:45:49 +0000 | [diff] [blame] | 4911 | return false; |
| 4912 | } |
| 4913 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4914 | // Can we evaluate this function call? |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 4915 | if (Definition && Definition->isConstexpr() && Body) |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4916 | return true; |
| 4917 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4918 | if (Info.getLangOpts().CPlusPlus11) { |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4919 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4920 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4921 | // If this function is not constexpr because it is an inherited |
| 4922 | // non-constexpr constructor, diagnose that directly. |
| 4923 | auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl); |
| 4924 | if (CD && CD->isInheritingConstructor()) { |
| 4925 | auto *Inherited = CD->getInheritedConstructor().getConstructor(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4926 | if (!Inherited->isConstexpr()) |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4927 | DiagDecl = CD = Inherited; |
| 4928 | } |
| 4929 | |
| 4930 | // FIXME: If DiagDecl is an implicitly-declared special member function |
| 4931 | // or an inheriting constructor, we should be much more explicit about why |
| 4932 | // it's not constexpr. |
| 4933 | if (CD && CD->isInheritingConstructor()) |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4934 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1) |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4935 | << CD->getInheritedConstructor().getConstructor()->getParent(); |
| 4936 | else |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4937 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1) |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4938 | << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4939 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 4940 | } else { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4941 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4942 | } |
| 4943 | return false; |
| 4944 | } |
| 4945 | |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 4946 | namespace { |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 4947 | struct CheckDynamicTypeHandler { |
| 4948 | AccessKinds AccessKind; |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 4949 | typedef bool result_type; |
| 4950 | bool failed() { return false; } |
| 4951 | bool found(APValue &Subobj, QualType SubobjType) { return true; } |
| 4952 | bool found(APSInt &Value, QualType SubobjType) { return true; } |
| 4953 | bool found(APFloat &Value, QualType SubobjType) { return true; } |
| 4954 | }; |
| 4955 | } // end anonymous namespace |
| 4956 | |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 4957 | /// Check that we can access the notional vptr of an object / determine its |
| 4958 | /// dynamic type. |
| 4959 | static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This, |
| 4960 | AccessKinds AK, bool Polymorphic) { |
| Richard Smith | dab287b | 2019-05-13 07:51:29 +0000 | [diff] [blame] | 4961 | if (This.Designator.Invalid) |
| 4962 | return false; |
| 4963 | |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 4964 | CompleteObject Obj = findCompleteObject(Info, E, AK, This, QualType()); |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 4965 | |
| 4966 | if (!Obj) |
| 4967 | return false; |
| 4968 | |
| 4969 | if (!Obj.Value) { |
| 4970 | // The object is not usable in constant expressions, so we can't inspect |
| 4971 | // its value to see if it's in-lifetime or what the active union members |
| 4972 | // are. We can still check for a one-past-the-end lvalue. |
| 4973 | if (This.Designator.isOnePastTheEnd() || |
| 4974 | This.Designator.isMostDerivedAnUnsizedArray()) { |
| 4975 | Info.FFDiag(E, This.Designator.isOnePastTheEnd() |
| 4976 | ? diag::note_constexpr_access_past_end |
| 4977 | : diag::note_constexpr_access_unsized_array) |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 4978 | << AK; |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 4979 | return false; |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 4980 | } else if (Polymorphic) { |
| 4981 | // Conservatively refuse to perform a polymorphic operation if we would |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 4982 | // not be able to read a notional 'vptr' value. |
| 4983 | APValue Val; |
| 4984 | This.moveInto(Val); |
| 4985 | QualType StarThisType = |
| 4986 | Info.Ctx.getLValueReferenceType(This.Designator.getType(Info.Ctx)); |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 4987 | Info.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type) |
| 4988 | << AK << Val.getAsString(Info.Ctx, StarThisType); |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 4989 | return false; |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 4990 | } |
| 4991 | return true; |
| 4992 | } |
| 4993 | |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 4994 | CheckDynamicTypeHandler Handler{AK}; |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 4995 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
| 4996 | } |
| 4997 | |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 4998 | /// Check that the pointee of the 'this' pointer in a member function call is |
| 4999 | /// either within its lifetime or in its period of construction or destruction. |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 5000 | static bool |
| 5001 | checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E, |
| 5002 | const LValue &This, |
| 5003 | const CXXMethodDecl *NamedMember) { |
| 5004 | return checkDynamicType( |
| 5005 | Info, E, This, |
| 5006 | isa<CXXDestructorDecl>(NamedMember) ? AK_Destroy : AK_MemberCall, false); |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 5007 | } |
| 5008 | |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5009 | struct DynamicType { |
| 5010 | /// The dynamic class type of the object. |
| 5011 | const CXXRecordDecl *Type; |
| 5012 | /// The corresponding path length in the lvalue. |
| 5013 | unsigned PathLength; |
| 5014 | }; |
| 5015 | |
| 5016 | static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator, |
| 5017 | unsigned PathLength) { |
| 5018 | assert(PathLength >= Designator.MostDerivedPathLength && PathLength <= |
| 5019 | Designator.Entries.size() && "invalid path length"); |
| 5020 | return (PathLength == Designator.MostDerivedPathLength) |
| 5021 | ? Designator.MostDerivedType->getAsCXXRecordDecl() |
| 5022 | : getAsBaseClass(Designator.Entries[PathLength - 1]); |
| 5023 | } |
| 5024 | |
| 5025 | /// Determine the dynamic type of an object. |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 5026 | static Optional<DynamicType> ComputeDynamicType(EvalInfo &Info, const Expr *E, |
| 5027 | LValue &This, AccessKinds AK) { |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5028 | // If we don't have an lvalue denoting an object of class type, there is no |
| 5029 | // meaningful dynamic type. (We consider objects of non-class type to have no |
| 5030 | // dynamic type.) |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 5031 | if (!checkDynamicType(Info, E, This, AK, true)) |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5032 | return None; |
| 5033 | |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 5034 | // Refuse to compute a dynamic type in the presence of virtual bases. This |
| 5035 | // shouldn't happen other than in constant-folding situations, since literal |
| 5036 | // types can't have virtual bases. |
| 5037 | // |
| 5038 | // Note that consumers of DynamicType assume that the type has no virtual |
| 5039 | // bases, and will need modifications if this restriction is relaxed. |
| 5040 | const CXXRecordDecl *Class = |
| 5041 | This.Designator.MostDerivedType->getAsCXXRecordDecl(); |
| 5042 | if (!Class || Class->getNumVBases()) { |
| 5043 | Info.FFDiag(E); |
| 5044 | return None; |
| 5045 | } |
| 5046 | |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5047 | // FIXME: For very deep class hierarchies, it might be beneficial to use a |
| 5048 | // binary search here instead. But the overwhelmingly common case is that |
| 5049 | // we're not in the middle of a constructor, so it probably doesn't matter |
| 5050 | // in practice. |
| 5051 | ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries; |
| 5052 | for (unsigned PathLength = This.Designator.MostDerivedPathLength; |
| 5053 | PathLength <= Path.size(); ++PathLength) { |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5054 | switch (Info.isEvaluatingCtorDtor(This.getLValueBase(), |
| 5055 | Path.slice(0, PathLength))) { |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5056 | case ConstructionPhase::Bases: |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5057 | case ConstructionPhase::DestroyingBases: |
| 5058 | // We're constructing or destroying a base class. This is not the dynamic |
| 5059 | // type. |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5060 | break; |
| 5061 | |
| 5062 | case ConstructionPhase::None: |
| 5063 | case ConstructionPhase::AfterBases: |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5064 | case ConstructionPhase::Destroying: |
| 5065 | // We've finished constructing the base classes and not yet started |
| 5066 | // destroying them again, so this is the dynamic type. |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5067 | return DynamicType{getBaseClassType(This.Designator, PathLength), |
| 5068 | PathLength}; |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | // CWG issue 1517: we're constructing a base class of the object described by |
| 5073 | // 'This', so that object has not yet begun its period of construction and |
| 5074 | // any polymorphic operation on it results in undefined behavior. |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 5075 | Info.FFDiag(E); |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5076 | return None; |
| 5077 | } |
| 5078 | |
| 5079 | /// Perform virtual dispatch. |
| 5080 | static const CXXMethodDecl *HandleVirtualDispatch( |
| 5081 | EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found, |
| 5082 | llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) { |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 5083 | Optional<DynamicType> DynType = ComputeDynamicType( |
| 5084 | Info, E, This, |
| 5085 | isa<CXXDestructorDecl>(Found) ? AK_Destroy : AK_MemberCall); |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 5086 | if (!DynType) |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5087 | return nullptr; |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5088 | |
| 5089 | // Find the final overrider. It must be declared in one of the classes on the |
| 5090 | // path from the dynamic type to the static type. |
| 5091 | // FIXME: If we ever allow literal types to have virtual base classes, that |
| 5092 | // won't be true. |
| 5093 | const CXXMethodDecl *Callee = Found; |
| 5094 | unsigned PathLength = DynType->PathLength; |
| 5095 | for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) { |
| 5096 | const CXXRecordDecl *Class = getBaseClassType(This.Designator, PathLength); |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5097 | const CXXMethodDecl *Overrider = |
| 5098 | Found->getCorrespondingMethodDeclaredInClass(Class, false); |
| 5099 | if (Overrider) { |
| 5100 | Callee = Overrider; |
| 5101 | break; |
| 5102 | } |
| 5103 | } |
| 5104 | |
| 5105 | // C++2a [class.abstract]p6: |
| 5106 | // the effect of making a virtual call to a pure virtual function [...] is |
| 5107 | // undefined |
| 5108 | if (Callee->isPure()) { |
| 5109 | Info.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << Callee; |
| 5110 | Info.Note(Callee->getLocation(), diag::note_declared_at); |
| 5111 | return nullptr; |
| 5112 | } |
| 5113 | |
| 5114 | // If necessary, walk the rest of the path to determine the sequence of |
| 5115 | // covariant adjustment steps to apply. |
| 5116 | if (!Info.Ctx.hasSameUnqualifiedType(Callee->getReturnType(), |
| 5117 | Found->getReturnType())) { |
| 5118 | CovariantAdjustmentPath.push_back(Callee->getReturnType()); |
| 5119 | for (unsigned CovariantPathLength = PathLength + 1; |
| 5120 | CovariantPathLength != This.Designator.Entries.size(); |
| 5121 | ++CovariantPathLength) { |
| 5122 | const CXXRecordDecl *NextClass = |
| 5123 | getBaseClassType(This.Designator, CovariantPathLength); |
| 5124 | const CXXMethodDecl *Next = |
| 5125 | Found->getCorrespondingMethodDeclaredInClass(NextClass, false); |
| 5126 | if (Next && !Info.Ctx.hasSameUnqualifiedType( |
| 5127 | Next->getReturnType(), CovariantAdjustmentPath.back())) |
| 5128 | CovariantAdjustmentPath.push_back(Next->getReturnType()); |
| 5129 | } |
| 5130 | if (!Info.Ctx.hasSameUnqualifiedType(Found->getReturnType(), |
| 5131 | CovariantAdjustmentPath.back())) |
| 5132 | CovariantAdjustmentPath.push_back(Found->getReturnType()); |
| 5133 | } |
| 5134 | |
| 5135 | // Perform 'this' adjustment. |
| 5136 | if (!CastToDerivedClass(Info, E, This, Callee->getParent(), PathLength)) |
| 5137 | return nullptr; |
| 5138 | |
| 5139 | return Callee; |
| 5140 | } |
| 5141 | |
| 5142 | /// Perform the adjustment from a value returned by a virtual function to |
| 5143 | /// a value of the statically expected type, which may be a pointer or |
| 5144 | /// reference to a base class of the returned type. |
| 5145 | static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E, |
| 5146 | APValue &Result, |
| 5147 | ArrayRef<QualType> Path) { |
| 5148 | assert(Result.isLValue() && |
| 5149 | "unexpected kind of APValue for covariant return"); |
| 5150 | if (Result.isNullPointer()) |
| 5151 | return true; |
| 5152 | |
| 5153 | LValue LVal; |
| 5154 | LVal.setFrom(Info.Ctx, Result); |
| 5155 | |
| 5156 | const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl(); |
| 5157 | for (unsigned I = 1; I != Path.size(); ++I) { |
| 5158 | const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl(); |
| 5159 | assert(OldClass && NewClass && "unexpected kind of covariant return"); |
| 5160 | if (OldClass != NewClass && |
| 5161 | !CastToBaseClass(Info, E, LVal, OldClass, NewClass)) |
| 5162 | return false; |
| 5163 | OldClass = NewClass; |
| 5164 | } |
| 5165 | |
| 5166 | LVal.moveInto(Result); |
| 5167 | return true; |
| 5168 | } |
| 5169 | |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 5170 | /// Determine whether \p Base, which is known to be a direct base class of |
| 5171 | /// \p Derived, is a public base class. |
| 5172 | static bool isBaseClassPublic(const CXXRecordDecl *Derived, |
| 5173 | const CXXRecordDecl *Base) { |
| 5174 | for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) { |
| 5175 | auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl(); |
| 5176 | if (BaseClass && declaresSameEntity(BaseClass, Base)) |
| 5177 | return BaseSpec.getAccessSpecifier() == AS_public; |
| 5178 | } |
| 5179 | llvm_unreachable("Base is not a direct base of Derived"); |
| 5180 | } |
| 5181 | |
| 5182 | /// Apply the given dynamic cast operation on the provided lvalue. |
| 5183 | /// |
| 5184 | /// This implements the hard case of dynamic_cast, requiring a "runtime check" |
| 5185 | /// to find a suitable target subobject. |
| 5186 | static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E, |
| 5187 | LValue &Ptr) { |
| 5188 | // We can't do anything with a non-symbolic pointer value. |
| 5189 | SubobjectDesignator &D = Ptr.Designator; |
| 5190 | if (D.Invalid) |
| 5191 | return false; |
| 5192 | |
| 5193 | // C++ [expr.dynamic.cast]p6: |
| 5194 | // If v is a null pointer value, the result is a null pointer value. |
| 5195 | if (Ptr.isNullPointer() && !E->isGLValue()) |
| 5196 | return true; |
| 5197 | |
| 5198 | // For all the other cases, we need the pointer to point to an object within |
| 5199 | // its lifetime / period of construction / destruction, and we need to know |
| 5200 | // its dynamic type. |
| 5201 | Optional<DynamicType> DynType = |
| 5202 | ComputeDynamicType(Info, E, Ptr, AK_DynamicCast); |
| 5203 | if (!DynType) |
| 5204 | return false; |
| 5205 | |
| 5206 | // C++ [expr.dynamic.cast]p7: |
| 5207 | // If T is "pointer to cv void", then the result is a pointer to the most |
| 5208 | // derived object |
| 5209 | if (E->getType()->isVoidPointerType()) |
| 5210 | return CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength); |
| 5211 | |
| 5212 | const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl(); |
| 5213 | assert(C && "dynamic_cast target is not void pointer nor class"); |
| 5214 | CanQualType CQT = Info.Ctx.getCanonicalType(Info.Ctx.getRecordType(C)); |
| 5215 | |
| 5216 | auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) { |
| 5217 | // C++ [expr.dynamic.cast]p9: |
| 5218 | if (!E->isGLValue()) { |
| 5219 | // The value of a failed cast to pointer type is the null pointer value |
| 5220 | // of the required result type. |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 5221 | Ptr.setNull(Info.Ctx, E->getType()); |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 5222 | return true; |
| 5223 | } |
| 5224 | |
| 5225 | // A failed cast to reference type throws [...] std::bad_cast. |
| 5226 | unsigned DiagKind; |
| 5227 | if (!Paths && (declaresSameEntity(DynType->Type, C) || |
| 5228 | DynType->Type->isDerivedFrom(C))) |
| 5229 | DiagKind = 0; |
| 5230 | else if (!Paths || Paths->begin() == Paths->end()) |
| 5231 | DiagKind = 1; |
| 5232 | else if (Paths->isAmbiguous(CQT)) |
| 5233 | DiagKind = 2; |
| 5234 | else { |
| 5235 | assert(Paths->front().Access != AS_public && "why did the cast fail?"); |
| 5236 | DiagKind = 3; |
| 5237 | } |
| 5238 | Info.FFDiag(E, diag::note_constexpr_dynamic_cast_to_reference_failed) |
| 5239 | << DiagKind << Ptr.Designator.getType(Info.Ctx) |
| 5240 | << Info.Ctx.getRecordType(DynType->Type) |
| 5241 | << E->getType().getUnqualifiedType(); |
| 5242 | return false; |
| 5243 | }; |
| 5244 | |
| 5245 | // Runtime check, phase 1: |
| 5246 | // Walk from the base subobject towards the derived object looking for the |
| 5247 | // target type. |
| 5248 | for (int PathLength = Ptr.Designator.Entries.size(); |
| 5249 | PathLength >= (int)DynType->PathLength; --PathLength) { |
| 5250 | const CXXRecordDecl *Class = getBaseClassType(Ptr.Designator, PathLength); |
| 5251 | if (declaresSameEntity(Class, C)) |
| 5252 | return CastToDerivedClass(Info, E, Ptr, Class, PathLength); |
| 5253 | // We can only walk across public inheritance edges. |
| 5254 | if (PathLength > (int)DynType->PathLength && |
| 5255 | !isBaseClassPublic(getBaseClassType(Ptr.Designator, PathLength - 1), |
| 5256 | Class)) |
| 5257 | return RuntimeCheckFailed(nullptr); |
| 5258 | } |
| 5259 | |
| 5260 | // Runtime check, phase 2: |
| 5261 | // Search the dynamic type for an unambiguous public base of type C. |
| 5262 | CXXBasePaths Paths(/*FindAmbiguities=*/true, |
| 5263 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
| 5264 | if (DynType->Type->isDerivedFrom(C, Paths) && !Paths.isAmbiguous(CQT) && |
| 5265 | Paths.front().Access == AS_public) { |
| 5266 | // Downcast to the dynamic type... |
| 5267 | if (!CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength)) |
| 5268 | return false; |
| 5269 | // ... then upcast to the chosen base class subobject. |
| 5270 | for (CXXBasePathElement &Elem : Paths.front()) |
| 5271 | if (!HandleLValueBase(Info, E, Ptr, Elem.Class, Elem.Base)) |
| 5272 | return false; |
| 5273 | return true; |
| 5274 | } |
| 5275 | |
| 5276 | // Otherwise, the runtime check fails. |
| 5277 | return RuntimeCheckFailed(&Paths); |
| 5278 | } |
| 5279 | |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5280 | namespace { |
| 5281 | struct StartLifetimeOfUnionMemberHandler { |
| 5282 | const FieldDecl *Field; |
| 5283 | |
| 5284 | static const AccessKinds AccessKind = AK_Assign; |
| 5285 | |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5286 | typedef bool result_type; |
| 5287 | bool failed() { return false; } |
| 5288 | bool found(APValue &Subobj, QualType SubobjType) { |
| 5289 | // We are supposed to perform no initialization but begin the lifetime of |
| 5290 | // the object. We interpret that as meaning to do what default |
| 5291 | // initialization of the object would do if all constructors involved were |
| 5292 | // trivial: |
| 5293 | // * All base, non-variant member, and array element subobjects' lifetimes |
| 5294 | // begin |
| 5295 | // * No variant members' lifetimes begin |
| 5296 | // * All scalar subobjects whose lifetimes begin have indeterminate values |
| 5297 | assert(SubobjType->isUnionType()); |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 5298 | if (!declaresSameEntity(Subobj.getUnionField(), Field) || |
| 5299 | !Subobj.getUnionValue().hasValue()) |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5300 | Subobj.setUnion(Field, getDefaultInitValue(Field->getType())); |
| 5301 | return true; |
| 5302 | } |
| 5303 | bool found(APSInt &Value, QualType SubobjType) { |
| 5304 | llvm_unreachable("wrong value kind for union object"); |
| 5305 | } |
| 5306 | bool found(APFloat &Value, QualType SubobjType) { |
| 5307 | llvm_unreachable("wrong value kind for union object"); |
| 5308 | } |
| 5309 | }; |
| 5310 | } // end anonymous namespace |
| 5311 | |
| 5312 | const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind; |
| 5313 | |
| 5314 | /// Handle a builtin simple-assignment or a call to a trivial assignment |
| 5315 | /// operator whose left-hand side might involve a union member access. If it |
| 5316 | /// does, implicitly start the lifetime of any accessed union elements per |
| 5317 | /// C++20 [class.union]5. |
| 5318 | static bool HandleUnionActiveMemberChange(EvalInfo &Info, const Expr *LHSExpr, |
| 5319 | const LValue &LHS) { |
| 5320 | if (LHS.InvalidBase || LHS.Designator.Invalid) |
| 5321 | return false; |
| 5322 | |
| 5323 | llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths; |
| 5324 | // C++ [class.union]p5: |
| 5325 | // define the set S(E) of subexpressions of E as follows: |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5326 | unsigned PathLength = LHS.Designator.Entries.size(); |
| Eric Fiselier | ffafdb9 | 2019-05-23 23:34:43 +0000 | [diff] [blame] | 5327 | for (const Expr *E = LHSExpr; E != nullptr;) { |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5328 | // -- If E is of the form A.B, S(E) contains the elements of S(A)... |
| 5329 | if (auto *ME = dyn_cast<MemberExpr>(E)) { |
| 5330 | auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| Richard Smith | 9a84dc0 | 2019-10-01 00:07:14 +0000 | [diff] [blame] | 5331 | // Note that we can't implicitly start the lifetime of a reference, |
| 5332 | // so we don't need to proceed any further if we reach one. |
| 5333 | if (!FD || FD->getType()->isReferenceType()) |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5334 | break; |
| 5335 | |
| 5336 | // ... and also contains A.B if B names a union member |
| 5337 | if (FD->getParent()->isUnion()) |
| 5338 | UnionPathLengths.push_back({PathLength - 1, FD}); |
| 5339 | |
| 5340 | E = ME->getBase(); |
| 5341 | --PathLength; |
| 5342 | assert(declaresSameEntity(FD, |
| 5343 | LHS.Designator.Entries[PathLength] |
| 5344 | .getAsBaseOrMember().getPointer())); |
| 5345 | |
| 5346 | // -- If E is of the form A[B] and is interpreted as a built-in array |
| 5347 | // subscripting operator, S(E) is [S(the array operand, if any)]. |
| 5348 | } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 5349 | // Step over an ArrayToPointerDecay implicit cast. |
| 5350 | auto *Base = ASE->getBase()->IgnoreImplicit(); |
| 5351 | if (!Base->getType()->isArrayType()) |
| 5352 | break; |
| 5353 | |
| 5354 | E = Base; |
| 5355 | --PathLength; |
| 5356 | |
| 5357 | } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 5358 | // Step over a derived-to-base conversion. |
| Eric Fiselier | ffafdb9 | 2019-05-23 23:34:43 +0000 | [diff] [blame] | 5359 | E = ICE->getSubExpr(); |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5360 | if (ICE->getCastKind() == CK_NoOp) |
| 5361 | continue; |
| 5362 | if (ICE->getCastKind() != CK_DerivedToBase && |
| 5363 | ICE->getCastKind() != CK_UncheckedDerivedToBase) |
| 5364 | break; |
| Richard Smith | a481b01 | 2019-05-30 20:45:12 +0000 | [diff] [blame] | 5365 | // Walk path backwards as we walk up from the base to the derived class. |
| 5366 | for (const CXXBaseSpecifier *Elt : llvm::reverse(ICE->path())) { |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5367 | --PathLength; |
| Dmitri Gribenko | a10fe83 | 2019-05-22 06:57:23 +0000 | [diff] [blame] | 5368 | (void)Elt; |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5369 | assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(), |
| 5370 | LHS.Designator.Entries[PathLength] |
| 5371 | .getAsBaseOrMember().getPointer())); |
| 5372 | } |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5373 | |
| 5374 | // -- Otherwise, S(E) is empty. |
| 5375 | } else { |
| 5376 | break; |
| 5377 | } |
| 5378 | } |
| 5379 | |
| 5380 | // Common case: no unions' lifetimes are started. |
| 5381 | if (UnionPathLengths.empty()) |
| 5382 | return true; |
| 5383 | |
| 5384 | // if modification of X [would access an inactive union member], an object |
| 5385 | // of the type of X is implicitly created |
| 5386 | CompleteObject Obj = |
| 5387 | findCompleteObject(Info, LHSExpr, AK_Assign, LHS, LHSExpr->getType()); |
| 5388 | if (!Obj) |
| 5389 | return false; |
| 5390 | for (std::pair<unsigned, const FieldDecl *> LengthAndField : |
| 5391 | llvm::reverse(UnionPathLengths)) { |
| 5392 | // Form a designator for the union object. |
| 5393 | SubobjectDesignator D = LHS.Designator; |
| 5394 | D.truncate(Info.Ctx, LHS.Base, LengthAndField.first); |
| 5395 | |
| 5396 | StartLifetimeOfUnionMemberHandler StartLifetime{LengthAndField.second}; |
| 5397 | if (!findSubobject(Info, LHSExpr, Obj, D, StartLifetime)) |
| 5398 | return false; |
| 5399 | } |
| 5400 | |
| 5401 | return true; |
| 5402 | } |
| 5403 | |
| Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 5404 | /// Determine if a class has any fields that might need to be copied by a |
| 5405 | /// trivial copy or move operation. |
| 5406 | static bool hasFields(const CXXRecordDecl *RD) { |
| 5407 | if (!RD || RD->isEmpty()) |
| 5408 | return false; |
| 5409 | for (auto *FD : RD->fields()) { |
| 5410 | if (FD->isUnnamedBitfield()) |
| 5411 | continue; |
| 5412 | return true; |
| 5413 | } |
| 5414 | for (auto &Base : RD->bases()) |
| 5415 | if (hasFields(Base.getType()->getAsCXXRecordDecl())) |
| 5416 | return true; |
| 5417 | return false; |
| 5418 | } |
| 5419 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5420 | namespace { |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5421 | typedef SmallVector<APValue, 8> ArgVector; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 5425 | static bool EvaluateArgs(ArrayRef<const Expr *> Args, ArgVector &ArgValues, |
| 5426 | EvalInfo &Info, const FunctionDecl *Callee) { |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5427 | bool Success = true; |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 5428 | llvm::SmallBitVector ForbiddenNullArgs; |
| 5429 | if (Callee->hasAttr<NonNullAttr>()) { |
| 5430 | ForbiddenNullArgs.resize(Args.size()); |
| 5431 | for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) { |
| 5432 | if (!Attr->args_size()) { |
| 5433 | ForbiddenNullArgs.set(); |
| 5434 | break; |
| 5435 | } else |
| 5436 | for (auto Idx : Attr->args()) { |
| 5437 | unsigned ASTIdx = Idx.getASTIndex(); |
| 5438 | if (ASTIdx >= Args.size()) |
| 5439 | continue; |
| 5440 | ForbiddenNullArgs[ASTIdx] = 1; |
| 5441 | } |
| 5442 | } |
| 5443 | } |
| Gauthier Harnisch | 59c6df9 | 2019-10-10 07:13:20 +0000 | [diff] [blame^] | 5444 | for (unsigned Idx = 0; Idx < Args.size(); Idx++) { |
| 5445 | if (!Evaluate(ArgValues[Idx], Info, Args[Idx])) { |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5446 | // If we're checking for a potential constant expression, evaluate all |
| 5447 | // initializers even if some of them fail. |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5448 | if (!Info.noteFailure()) |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5449 | return false; |
| 5450 | Success = false; |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 5451 | } else if (!ForbiddenNullArgs.empty() && |
| Gauthier Harnisch | 59c6df9 | 2019-10-10 07:13:20 +0000 | [diff] [blame^] | 5452 | ForbiddenNullArgs[Idx] && |
| 5453 | ArgValues[Idx].isLValue() && |
| 5454 | ArgValues[Idx].isNullPointer()) { |
| 5455 | Info.CCEDiag(Args[Idx], diag::note_non_null_attribute_failed); |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 5456 | if (!Info.noteFailure()) |
| 5457 | return false; |
| 5458 | Success = false; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5459 | } |
| 5460 | } |
| 5461 | return Success; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5462 | } |
| 5463 | |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 5464 | /// Evaluate a function call. |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5465 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 5466 | const FunctionDecl *Callee, const LValue *This, |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5467 | ArrayRef<const Expr*> Args, const Stmt *Body, |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5468 | EvalInfo &Info, APValue &Result, |
| 5469 | const LValue *ResultSlot) { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5470 | ArgVector ArgValues(Args.size()); |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 5471 | if (!EvaluateArgs(Args, ArgValues, Info, Callee)) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5472 | return false; |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 5473 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5474 | if (!Info.CheckCallLimit(CallLoc)) |
| 5475 | return false; |
| 5476 | |
| 5477 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5478 | |
| 5479 | // For a trivial copy or move assignment, perform an APValue copy. This is |
| 5480 | // essential for unions, where the operations performed by the assignment |
| 5481 | // operator cannot be represented as statements. |
| Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 5482 | // |
| 5483 | // Skip this for non-union classes with no fields; in that case, the defaulted |
| 5484 | // copy/move does not actually read the object. |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5485 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
| Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 5486 | if (MD && MD->isDefaulted() && |
| 5487 | (MD->getParent()->isUnion() || |
| 5488 | (MD->isTrivial() && hasFields(MD->getParent())))) { |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5489 | assert(This && |
| 5490 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
| 5491 | LValue RHS; |
| 5492 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 5493 | APValue RHSValue; |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 5494 | if (!handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), RHS, |
| 5495 | RHSValue, MD->getParent()->isUnion())) |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5496 | return false; |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 5497 | if (Info.getLangOpts().CPlusPlus2a && MD->isTrivial() && |
| 5498 | !HandleUnionActiveMemberChange(Info, Args[0], *This)) |
| 5499 | return false; |
| Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 5500 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(), |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5501 | RHSValue)) |
| 5502 | return false; |
| 5503 | This->moveInto(Result); |
| 5504 | return true; |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5505 | } else if (MD && isLambdaCallOperator(MD)) { |
| Erik Pilkington | 1123291 | 2018-04-05 00:12:05 +0000 | [diff] [blame] | 5506 | // We're in a lambda; determine the lambda capture field maps unless we're |
| 5507 | // just constexpr checking a lambda's call operator. constexpr checking is |
| 5508 | // done before the captures have been added to the closure object (unless |
| 5509 | // we're inferring constexpr-ness), so we don't have access to them in this |
| 5510 | // case. But since we don't need the captures to constexpr check, we can |
| 5511 | // just ignore them. |
| 5512 | if (!Info.checkingPotentialConstantExpression()) |
| 5513 | MD->getParent()->getCaptureFields(Frame.LambdaCaptureFields, |
| 5514 | Frame.LambdaThisCaptureField); |
| Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5517 | StmtResult Ret = {Result, ResultSlot}; |
| 5518 | EvalStmtResult ESR = EvaluateStmt(Ret, Info, Body); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 5519 | if (ESR == ESR_Succeeded) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5520 | if (Callee->getReturnType()->isVoidType()) |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 5521 | return true; |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 5522 | Info.FFDiag(Callee->getEndLoc(), diag::note_constexpr_no_return); |
| Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 5523 | } |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 5524 | return ESR == ESR_Returned; |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 5525 | } |
| 5526 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5527 | /// Evaluate a constructor call. |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5528 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
| 5529 | APValue *ArgValues, |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5530 | const CXXConstructorDecl *Definition, |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5531 | EvalInfo &Info, APValue &Result) { |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5532 | SourceLocation CallLoc = E->getExprLoc(); |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5533 | if (!Info.CheckCallLimit(CallLoc)) |
| 5534 | return false; |
| 5535 | |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 5536 | const CXXRecordDecl *RD = Definition->getParent(); |
| 5537 | if (RD->getNumVBases()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5538 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 5539 | return false; |
| 5540 | } |
| 5541 | |
| Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 5542 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 5543 | Info, |
| 5544 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
| 5545 | RD->getNumBases()); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5546 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5547 | |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5548 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
| 5549 | // wasteful. |
| 5550 | APValue RetVal; |
| 5551 | StmtResult Ret = {RetVal, nullptr}; |
| 5552 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5553 | // If it's a delegating constructor, delegate. |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5554 | if (Definition->isDelegatingConstructor()) { |
| 5555 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
| Richard Smith | 9ff62af | 2013-11-07 18:45:03 +0000 | [diff] [blame] | 5556 | { |
| 5557 | FullExpressionRAII InitScope(Info); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5558 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit()) || |
| 5559 | !InitScope.destroy()) |
| Richard Smith | 9ff62af | 2013-11-07 18:45:03 +0000 | [diff] [blame] | 5560 | return false; |
| 5561 | } |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5562 | return EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
| Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 5565 | // 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] | 5566 | // essential for unions (or classes with anonymous union members), where the |
| 5567 | // operations performed by the constructor cannot be represented by |
| 5568 | // ctor-initializers. |
| 5569 | // |
| 5570 | // Skip this for empty non-union classes; we should not perform an |
| 5571 | // lvalue-to-rvalue conversion on them because their copy constructor does not |
| 5572 | // actually read them. |
| Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 5573 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
| Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 5574 | (Definition->getParent()->isUnion() || |
| Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 5575 | (Definition->isTrivial() && hasFields(Definition->getParent())))) { |
| Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 5576 | LValue RHS; |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5577 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5578 | return handleLValueToRValueConversion( |
| 5579 | Info, E, Definition->getParamDecl(0)->getType().getNonReferenceType(), |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 5580 | RHS, Result, Definition->getParent()->isUnion()); |
| Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | // Reserve space for the struct members. |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 5584 | if (!RD->isUnion() && !Result.hasValue()) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5585 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
| Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 5586 | std::distance(RD->field_begin(), RD->field_end())); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5587 | |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5588 | if (RD->isInvalidDecl()) return false; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5589 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 5590 | |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5591 | // A scope for temporaries lifetime-extended by reference members. |
| 5592 | BlockScopeRAII LifetimeExtendedScope(Info); |
| 5593 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5594 | bool Success = true; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5595 | unsigned BasesSeen = 0; |
| 5596 | #ifndef NDEBUG |
| 5597 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 5598 | #endif |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 5599 | CXXRecordDecl::field_iterator FieldIt = RD->field_begin(); |
| 5600 | auto SkipToField = [&](FieldDecl *FD, bool Indirect) { |
| 5601 | // We might be initializing the same field again if this is an indirect |
| 5602 | // field initialization. |
| 5603 | if (FieldIt == RD->field_end() || |
| 5604 | FieldIt->getFieldIndex() > FD->getFieldIndex()) { |
| 5605 | assert(Indirect && "fields out of order?"); |
| 5606 | return; |
| 5607 | } |
| 5608 | |
| 5609 | // Default-initialize any fields with no explicit initializer. |
| 5610 | for (; !declaresSameEntity(*FieldIt, FD); ++FieldIt) { |
| 5611 | assert(FieldIt != RD->field_end() && "missing field?"); |
| 5612 | if (!FieldIt->isUnnamedBitfield()) |
| 5613 | Result.getStructField(FieldIt->getFieldIndex()) = |
| 5614 | getDefaultInitValue(FieldIt->getType()); |
| 5615 | } |
| 5616 | ++FieldIt; |
| 5617 | }; |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 5618 | for (const auto *I : Definition->inits()) { |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5619 | LValue Subobject = This; |
| Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 5620 | LValue SubobjectParent = This; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5621 | APValue *Value = &Result; |
| 5622 | |
| 5623 | // Determine the subobject to initialize. |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5624 | FieldDecl *FD = nullptr; |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 5625 | if (I->isBaseInitializer()) { |
| 5626 | QualType BaseType(I->getBaseClass(), 0); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5627 | #ifndef NDEBUG |
| 5628 | // Non-virtual base classes are initialized in the order in the class |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 5629 | // definition. We have already checked for virtual base classes. |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5630 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 5631 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 5632 | "base class initializers not in expected order"); |
| 5633 | ++BaseIt; |
| 5634 | #endif |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 5635 | if (!HandleLValueDirectBase(Info, I->getInit(), Subobject, RD, |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5636 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 5637 | return false; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5638 | Value = &Result.getStructBase(BasesSeen++); |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 5639 | } else if ((FD = I->getMember())) { |
| 5640 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD, &Layout)) |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5641 | return false; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5642 | if (RD->isUnion()) { |
| 5643 | Result = APValue(FD); |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5644 | Value = &Result.getUnionValue(); |
| 5645 | } else { |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 5646 | SkipToField(FD, false); |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5647 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 5648 | } |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 5649 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5650 | // Walk the indirect field decl's chain to find the object to initialize, |
| 5651 | // and make sure we've initialized every step along it. |
| Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 5652 | auto IndirectFieldChain = IFD->chain(); |
| 5653 | for (auto *C : IndirectFieldChain) { |
| Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 5654 | FD = cast<FieldDecl>(C); |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5655 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 5656 | // Switch the union field if it differs. This happens if we had |
| 5657 | // preceding zero-initialization, and we're now initializing a union |
| 5658 | // subobject other than the first. |
| 5659 | // FIXME: In this case, the values of the other subobjects are |
| 5660 | // specified, since zero-initialization sets all padding bits to zero. |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 5661 | if (!Value->hasValue() || |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5662 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 5663 | if (CD->isUnion()) |
| 5664 | *Value = APValue(FD); |
| 5665 | else |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 5666 | // FIXME: This immediately starts the lifetime of all members of an |
| 5667 | // anonymous struct. It would be preferable to strictly start member |
| 5668 | // lifetime in initialization order. |
| 5669 | *Value = getDefaultInitValue(Info.Ctx.getRecordType(CD)); |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5670 | } |
| Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 5671 | // Store Subobject as its parent before updating it for the last element |
| 5672 | // in the chain. |
| 5673 | if (C == IndirectFieldChain.back()) |
| 5674 | SubobjectParent = Subobject; |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 5675 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD)) |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5676 | return false; |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5677 | if (CD->isUnion()) |
| 5678 | Value = &Value->getUnionValue(); |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 5679 | else { |
| 5680 | if (C == IndirectFieldChain.front() && !RD->isUnion()) |
| 5681 | SkipToField(FD, true); |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5682 | Value = &Value->getStructField(FD->getFieldIndex()); |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 5683 | } |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5684 | } |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5685 | } else { |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5686 | llvm_unreachable("unknown base initializer kind"); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5687 | } |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5688 | |
| Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 5689 | // Need to override This for implicit field initializers as in this case |
| 5690 | // This refers to innermost anonymous struct/union containing initializer, |
| 5691 | // not to currently constructed class. |
| 5692 | const Expr *Init = I->getInit(); |
| 5693 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent, |
| 5694 | isa<CXXDefaultInitExpr>(Init)); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5695 | FullExpressionRAII InitScope(Info); |
| Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 5696 | if (!EvaluateInPlace(*Value, Info, Subobject, Init) || |
| 5697 | (FD && FD->isBitField() && |
| 5698 | !truncateBitfieldValue(Info, Init, *Value, FD))) { |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5699 | // If we're checking for a potential constant expression, evaluate all |
| 5700 | // initializers even if some of them fail. |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5701 | if (!Info.noteFailure()) |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5702 | return false; |
| 5703 | Success = false; |
| 5704 | } |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 5705 | |
| 5706 | // This is the point at which the dynamic type of the object becomes this |
| 5707 | // class type. |
| 5708 | if (I->isBaseInitializer() && BasesSeen == RD->getNumBases()) |
| 5709 | EvalObj.finishedConstructingBases(); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5710 | } |
| 5711 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 5712 | // Default-initialize any remaining fields. |
| 5713 | if (!RD->isUnion()) { |
| 5714 | for (; FieldIt != RD->field_end(); ++FieldIt) { |
| 5715 | if (!FieldIt->isUnnamedBitfield()) |
| 5716 | Result.getStructField(FieldIt->getFieldIndex()) = |
| 5717 | getDefaultInitValue(FieldIt->getType()); |
| 5718 | } |
| 5719 | } |
| 5720 | |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 5721 | return Success && |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5722 | EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed && |
| 5723 | LifetimeExtendedScope.destroy(); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5724 | } |
| 5725 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5726 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
| 5727 | ArrayRef<const Expr*> Args, |
| 5728 | const CXXConstructorDecl *Definition, |
| 5729 | EvalInfo &Info, APValue &Result) { |
| 5730 | ArgVector ArgValues(Args.size()); |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 5731 | if (!EvaluateArgs(Args, ArgValues, Info, Definition)) |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 5732 | return false; |
| 5733 | |
| 5734 | return HandleConstructorCall(E, This, ArgValues.data(), Definition, |
| 5735 | Info, Result); |
| 5736 | } |
| 5737 | |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 5738 | static bool HandleDestructionImpl(EvalInfo &Info, SourceLocation CallLoc, |
| 5739 | const LValue &This, APValue &Value, |
| 5740 | QualType T) { |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 5741 | // Objects can only be destroyed while they're within their lifetimes. |
| 5742 | // FIXME: We have no representation for whether an object of type nullptr_t |
| 5743 | // is in its lifetime; it usually doesn't matter. Perhaps we should model it |
| 5744 | // as indeterminate instead? |
| 5745 | if (Value.isAbsent() && !T->isNullPtrType()) { |
| 5746 | APValue Printable; |
| 5747 | This.moveInto(Printable); |
| 5748 | Info.FFDiag(CallLoc, diag::note_constexpr_destroy_out_of_lifetime) |
| 5749 | << Printable.getAsString(Info.Ctx, Info.Ctx.getLValueReferenceType(T)); |
| 5750 | return false; |
| 5751 | } |
| 5752 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5753 | // Invent an expression for location purposes. |
| 5754 | // FIXME: We shouldn't need to do this. |
| 5755 | OpaqueValueExpr LocE(CallLoc, Info.Ctx.IntTy, VK_RValue); |
| 5756 | |
| 5757 | // For arrays, destroy elements right-to-left. |
| 5758 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) { |
| 5759 | uint64_t Size = CAT->getSize().getZExtValue(); |
| 5760 | QualType ElemT = CAT->getElementType(); |
| 5761 | |
| 5762 | LValue ElemLV = This; |
| 5763 | ElemLV.addArray(Info, &LocE, CAT); |
| 5764 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, Size)) |
| 5765 | return false; |
| 5766 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 5767 | // Ensure that we have actual array elements available to destroy; the |
| 5768 | // destructors might mutate the value, so we can't run them on the array |
| 5769 | // filler. |
| 5770 | if (Size && Size > Value.getArrayInitializedElts()) |
| 5771 | expandArray(Value, Value.getArraySize() - 1); |
| 5772 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5773 | for (; Size != 0; --Size) { |
| 5774 | APValue &Elem = Value.getArrayInitializedElt(Size - 1); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 5775 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, -1) || |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 5776 | !HandleDestructionImpl(Info, CallLoc, ElemLV, Elem, ElemT)) |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5777 | return false; |
| 5778 | } |
| 5779 | |
| 5780 | // End the lifetime of this array now. |
| 5781 | Value = APValue(); |
| 5782 | return true; |
| 5783 | } |
| 5784 | |
| 5785 | const CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
| 5786 | if (!RD) { |
| 5787 | if (T.isDestructedType()) { |
| 5788 | Info.FFDiag(CallLoc, diag::note_constexpr_unsupported_destruction) << T; |
| 5789 | return false; |
| 5790 | } |
| 5791 | |
| 5792 | Value = APValue(); |
| 5793 | return true; |
| 5794 | } |
| 5795 | |
| 5796 | if (RD->getNumVBases()) { |
| 5797 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
| 5798 | return false; |
| 5799 | } |
| 5800 | |
| 5801 | const CXXDestructorDecl *DD = RD->getDestructor(); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 5802 | if (!DD && !RD->hasTrivialDestructor()) { |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5803 | Info.FFDiag(CallLoc); |
| 5804 | return false; |
| 5805 | } |
| 5806 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 5807 | if (!DD || DD->isTrivial() || |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5808 | (RD->isAnonymousStructOrUnion() && RD->isUnion())) { |
| 5809 | // A trivial destructor just ends the lifetime of the object. Check for |
| 5810 | // this case before checking for a body, because we might not bother |
| 5811 | // building a body for a trivial destructor. Note that it doesn't matter |
| 5812 | // whether the destructor is constexpr in this case; all trivial |
| 5813 | // destructors are constexpr. |
| 5814 | // |
| 5815 | // If an anonymous union would be destroyed, some enclosing destructor must |
| 5816 | // have been explicitly defined, and the anonymous union destruction should |
| 5817 | // have no effect. |
| 5818 | Value = APValue(); |
| 5819 | return true; |
| 5820 | } |
| 5821 | |
| 5822 | if (!Info.CheckCallLimit(CallLoc)) |
| 5823 | return false; |
| 5824 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 5825 | const FunctionDecl *Definition = nullptr; |
| 5826 | const Stmt *Body = DD->getBody(Definition); |
| 5827 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5828 | if (!CheckConstexprFunction(Info, CallLoc, DD, Definition, Body)) |
| 5829 | return false; |
| 5830 | |
| 5831 | CallStackFrame Frame(Info, CallLoc, Definition, &This, nullptr); |
| 5832 | |
| 5833 | // We're now in the period of destruction of this object. |
| 5834 | unsigned BasesLeft = RD->getNumBases(); |
| 5835 | EvalInfo::EvaluatingDestructorRAII EvalObj( |
| 5836 | Info, |
| 5837 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 5838 | if (!EvalObj.DidInsert) { |
| 5839 | // C++2a [class.dtor]p19: |
| 5840 | // the behavior is undefined if the destructor is invoked for an object |
| 5841 | // whose lifetime has ended |
| 5842 | // (Note that formally the lifetime ends when the period of destruction |
| 5843 | // begins, even though certain uses of the object remain valid until the |
| 5844 | // period of destruction ends.) |
| 5845 | Info.FFDiag(CallLoc, diag::note_constexpr_double_destroy); |
| 5846 | return false; |
| 5847 | } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5848 | |
| 5849 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
| 5850 | // wasteful. |
| 5851 | APValue RetVal; |
| 5852 | StmtResult Ret = {RetVal, nullptr}; |
| 5853 | if (EvaluateStmt(Ret, Info, Definition->getBody()) == ESR_Failed) |
| 5854 | return false; |
| 5855 | |
| 5856 | // A union destructor does not implicitly destroy its members. |
| 5857 | if (RD->isUnion()) |
| 5858 | return true; |
| 5859 | |
| 5860 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 5861 | |
| 5862 | // We don't have a good way to iterate fields in reverse, so collect all the |
| 5863 | // fields first and then walk them backwards. |
| 5864 | SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
| 5865 | for (const FieldDecl *FD : llvm::reverse(Fields)) { |
| 5866 | if (FD->isUnnamedBitfield()) |
| 5867 | continue; |
| 5868 | |
| 5869 | LValue Subobject = This; |
| 5870 | if (!HandleLValueMember(Info, &LocE, Subobject, FD, &Layout)) |
| 5871 | return false; |
| 5872 | |
| 5873 | APValue *SubobjectValue = &Value.getStructField(FD->getFieldIndex()); |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 5874 | if (!HandleDestructionImpl(Info, CallLoc, Subobject, *SubobjectValue, |
| 5875 | FD->getType())) |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5876 | return false; |
| 5877 | } |
| 5878 | |
| 5879 | if (BasesLeft != 0) |
| 5880 | EvalObj.startedDestroyingBases(); |
| 5881 | |
| 5882 | // Destroy base classes in reverse order. |
| 5883 | for (const CXXBaseSpecifier &Base : llvm::reverse(RD->bases())) { |
| 5884 | --BasesLeft; |
| 5885 | |
| 5886 | QualType BaseType = Base.getType(); |
| 5887 | LValue Subobject = This; |
| 5888 | if (!HandleLValueDirectBase(Info, &LocE, Subobject, RD, |
| 5889 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 5890 | return false; |
| 5891 | |
| 5892 | APValue *SubobjectValue = &Value.getStructBase(BasesLeft); |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 5893 | if (!HandleDestructionImpl(Info, CallLoc, Subobject, *SubobjectValue, |
| 5894 | BaseType)) |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5895 | return false; |
| 5896 | } |
| 5897 | assert(BasesLeft == 0 && "NumBases was wrong?"); |
| 5898 | |
| 5899 | // The period of destruction ends now. The object is gone. |
| 5900 | Value = APValue(); |
| 5901 | return true; |
| 5902 | } |
| 5903 | |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 5904 | namespace { |
| 5905 | struct DestroyObjectHandler { |
| 5906 | EvalInfo &Info; |
| 5907 | const Expr *E; |
| 5908 | const LValue &This; |
| 5909 | const AccessKinds AccessKind; |
| 5910 | |
| 5911 | typedef bool result_type; |
| 5912 | bool failed() { return false; } |
| 5913 | bool found(APValue &Subobj, QualType SubobjType) { |
| 5914 | return HandleDestructionImpl(Info, E->getExprLoc(), This, Subobj, |
| 5915 | SubobjType); |
| 5916 | } |
| 5917 | bool found(APSInt &Value, QualType SubobjType) { |
| 5918 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
| 5919 | return false; |
| 5920 | } |
| 5921 | bool found(APFloat &Value, QualType SubobjType) { |
| 5922 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
| 5923 | return false; |
| 5924 | } |
| 5925 | }; |
| 5926 | } |
| 5927 | |
| 5928 | /// Perform a destructor or pseudo-destructor call on the given object, which |
| 5929 | /// might in general not be a complete object. |
| 5930 | static bool HandleDestruction(EvalInfo &Info, const Expr *E, |
| 5931 | const LValue &This, QualType ThisType) { |
| 5932 | CompleteObject Obj = findCompleteObject(Info, E, AK_Destroy, This, ThisType); |
| 5933 | DestroyObjectHandler Handler = {Info, E, This, AK_Destroy}; |
| 5934 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
| 5935 | } |
| 5936 | |
| 5937 | /// Destroy and end the lifetime of the given complete object. |
| 5938 | static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc, |
| 5939 | APValue::LValueBase LVBase, APValue &Value, |
| 5940 | QualType T) { |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 5941 | // If we've had an unmodeled side-effect, we can't rely on mutable state |
| 5942 | // (such as the object we're about to destroy) being correct. |
| 5943 | if (Info.EvalStatus.HasSideEffects) |
| 5944 | return false; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5945 | |
| 5946 | LValue LV; |
| 5947 | LV.set({LVBase}); |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 5948 | return HandleDestructionImpl(Info, Loc, LV, Value, T); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 5949 | } |
| 5950 | |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 5951 | /// Perform a call to 'perator new' or to `__builtin_operator_new'. |
| 5952 | static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E, |
| 5953 | LValue &Result) { |
| 5954 | if (Info.checkingPotentialConstantExpression() || |
| 5955 | Info.SpeculativeEvaluationDepth) |
| 5956 | return false; |
| 5957 | |
| 5958 | // This is permitted only within a call to std::allocator<T>::allocate. |
| 5959 | auto Caller = Info.getStdAllocatorCaller("allocate"); |
| 5960 | if (!Caller) { |
| 5961 | Info.FFDiag(E->getExprLoc(), Info.getLangOpts().CPlusPlus2a |
| 5962 | ? diag::note_constexpr_new_untyped |
| 5963 | : diag::note_constexpr_new); |
| 5964 | return false; |
| 5965 | } |
| 5966 | |
| 5967 | QualType ElemType = Caller.ElemType; |
| 5968 | if (ElemType->isIncompleteType() || ElemType->isFunctionType()) { |
| 5969 | Info.FFDiag(E->getExprLoc(), |
| 5970 | diag::note_constexpr_new_not_complete_object_type) |
| 5971 | << (ElemType->isIncompleteType() ? 0 : 1) << ElemType; |
| 5972 | return false; |
| 5973 | } |
| 5974 | |
| 5975 | APSInt ByteSize; |
| 5976 | if (!EvaluateInteger(E->getArg(0), ByteSize, Info)) |
| 5977 | return false; |
| 5978 | bool IsNothrow = false; |
| 5979 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) { |
| 5980 | EvaluateIgnoredValue(Info, E->getArg(I)); |
| 5981 | IsNothrow |= E->getType()->isNothrowT(); |
| 5982 | } |
| 5983 | |
| 5984 | CharUnits ElemSize; |
| 5985 | if (!HandleSizeof(Info, E->getExprLoc(), ElemType, ElemSize)) |
| 5986 | return false; |
| 5987 | APInt Size, Remainder; |
| 5988 | APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity()); |
| 5989 | APInt::udivrem(ByteSize, ElemSizeAP, Size, Remainder); |
| 5990 | if (Remainder != 0) { |
| 5991 | // This likely indicates a bug in the implementation of 'std::allocator'. |
| 5992 | Info.FFDiag(E->getExprLoc(), diag::note_constexpr_operator_new_bad_size) |
| 5993 | << ByteSize << APSInt(ElemSizeAP, true) << ElemType; |
| 5994 | return false; |
| 5995 | } |
| 5996 | |
| 5997 | if (ByteSize.getActiveBits() > ConstantArrayType::getMaxSizeBits(Info.Ctx)) { |
| 5998 | if (IsNothrow) { |
| 5999 | Result.setNull(Info.Ctx, E->getType()); |
| 6000 | return true; |
| 6001 | } |
| 6002 | |
| 6003 | Info.FFDiag(E, diag::note_constexpr_new_too_large) << APSInt(Size, true); |
| 6004 | return false; |
| 6005 | } |
| 6006 | |
| Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 6007 | QualType AllocType = Info.Ctx.getConstantArrayType(ElemType, Size, nullptr, |
| 6008 | ArrayType::Normal, 0); |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 6009 | APValue *Val = Info.createHeapAlloc(E, AllocType, Result); |
| 6010 | *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue()); |
| 6011 | Result.addArray(Info, E, cast<ConstantArrayType>(AllocType)); |
| 6012 | return true; |
| 6013 | } |
| 6014 | |
| 6015 | static bool hasVirtualDestructor(QualType T) { |
| 6016 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 6017 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
| 6018 | return DD->isVirtual(); |
| 6019 | return false; |
| 6020 | } |
| 6021 | |
| Richard Smith | df3761f | 2019-10-07 03:14:28 +0000 | [diff] [blame] | 6022 | static const FunctionDecl *getVirtualOperatorDelete(QualType T) { |
| 6023 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
| 6024 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
| 6025 | return DD->isVirtual() ? DD->getOperatorDelete() : nullptr; |
| 6026 | return nullptr; |
| 6027 | } |
| 6028 | |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 6029 | /// Check that the given object is a suitable pointer to a heap allocation that |
| 6030 | /// still exists and is of the right kind for the purpose of a deletion. |
| 6031 | /// |
| 6032 | /// On success, returns the heap allocation to deallocate. On failure, produces |
| 6033 | /// a diagnostic and returns None. |
| 6034 | static Optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E, |
| 6035 | const LValue &Pointer, |
| 6036 | DynAlloc::Kind DeallocKind) { |
| 6037 | auto PointerAsString = [&] { |
| 6038 | return Pointer.toString(Info.Ctx, Info.Ctx.VoidPtrTy); |
| 6039 | }; |
| 6040 | |
| 6041 | DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>(); |
| 6042 | if (!DA) { |
| 6043 | Info.FFDiag(E, diag::note_constexpr_delete_not_heap_alloc) |
| 6044 | << PointerAsString(); |
| 6045 | if (Pointer.Base) |
| 6046 | NoteLValueLocation(Info, Pointer.Base); |
| 6047 | return None; |
| 6048 | } |
| 6049 | |
| 6050 | Optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA); |
| 6051 | if (!Alloc) { |
| 6052 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
| 6053 | return None; |
| 6054 | } |
| 6055 | |
| 6056 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
| 6057 | if (DeallocKind != (*Alloc)->getKind()) { |
| 6058 | Info.FFDiag(E, diag::note_constexpr_new_delete_mismatch) |
| 6059 | << DeallocKind << (*Alloc)->getKind() << AllocType; |
| 6060 | NoteLValueLocation(Info, Pointer.Base); |
| 6061 | return None; |
| 6062 | } |
| 6063 | |
| 6064 | bool Subobject = false; |
| 6065 | if (DeallocKind == DynAlloc::New) { |
| 6066 | Subobject = Pointer.Designator.MostDerivedPathLength != 0 || |
| 6067 | Pointer.Designator.isOnePastTheEnd(); |
| 6068 | } else { |
| 6069 | Subobject = Pointer.Designator.Entries.size() != 1 || |
| 6070 | Pointer.Designator.Entries[0].getAsArrayIndex() != 0; |
| 6071 | } |
| 6072 | if (Subobject) { |
| 6073 | Info.FFDiag(E, diag::note_constexpr_delete_subobject) |
| 6074 | << PointerAsString() << Pointer.Designator.isOnePastTheEnd(); |
| 6075 | return None; |
| 6076 | } |
| 6077 | |
| 6078 | return Alloc; |
| 6079 | } |
| 6080 | |
| 6081 | // Perform a call to 'operator delete' or '__builtin_operator_delete'. |
| 6082 | bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) { |
| 6083 | if (Info.checkingPotentialConstantExpression() || |
| 6084 | Info.SpeculativeEvaluationDepth) |
| 6085 | return false; |
| 6086 | |
| 6087 | // This is permitted only within a call to std::allocator<T>::deallocate. |
| 6088 | if (!Info.getStdAllocatorCaller("deallocate")) { |
| 6089 | Info.FFDiag(E->getExprLoc()); |
| 6090 | return true; |
| 6091 | } |
| 6092 | |
| 6093 | LValue Pointer; |
| 6094 | if (!EvaluatePointer(E->getArg(0), Pointer, Info)) |
| 6095 | return false; |
| 6096 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) |
| 6097 | EvaluateIgnoredValue(Info, E->getArg(I)); |
| 6098 | |
| 6099 | if (Pointer.Designator.Invalid) |
| 6100 | return false; |
| 6101 | |
| 6102 | // Deleting a null pointer has no effect. |
| 6103 | if (Pointer.isNullPointer()) |
| 6104 | return true; |
| 6105 | |
| 6106 | if (!CheckDeleteKind(Info, E, Pointer, DynAlloc::StdAllocator)) |
| 6107 | return false; |
| 6108 | |
| 6109 | Info.HeapAllocs.erase(Pointer.Base.get<DynamicAllocLValue>()); |
| 6110 | return true; |
| 6111 | } |
| 6112 | |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6113 | //===----------------------------------------------------------------------===// |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6114 | // Generic Evaluation |
| 6115 | //===----------------------------------------------------------------------===// |
| 6116 | namespace { |
| 6117 | |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 6118 | class BitCastBuffer { |
| 6119 | // FIXME: We're going to need bit-level granularity when we support |
| 6120 | // bit-fields. |
| 6121 | // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but |
| 6122 | // we don't support a host or target where that is the case. Still, we should |
| 6123 | // use a more generic type in case we ever do. |
| 6124 | SmallVector<Optional<unsigned char>, 32> Bytes; |
| 6125 | |
| 6126 | static_assert(std::numeric_limits<unsigned char>::digits >= 8, |
| 6127 | "Need at least 8 bit unsigned char"); |
| 6128 | |
| 6129 | bool TargetIsLittleEndian; |
| 6130 | |
| 6131 | public: |
| 6132 | BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian) |
| 6133 | : Bytes(Width.getQuantity()), |
| 6134 | TargetIsLittleEndian(TargetIsLittleEndian) {} |
| 6135 | |
| 6136 | LLVM_NODISCARD |
| 6137 | bool readObject(CharUnits Offset, CharUnits Width, |
| 6138 | SmallVectorImpl<unsigned char> &Output) const { |
| 6139 | for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) { |
| 6140 | // If a byte of an integer is uninitialized, then the whole integer is |
| 6141 | // uninitalized. |
| 6142 | if (!Bytes[I.getQuantity()]) |
| 6143 | return false; |
| 6144 | Output.push_back(*Bytes[I.getQuantity()]); |
| 6145 | } |
| 6146 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
| 6147 | std::reverse(Output.begin(), Output.end()); |
| 6148 | return true; |
| 6149 | } |
| 6150 | |
| 6151 | void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) { |
| 6152 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
| 6153 | std::reverse(Input.begin(), Input.end()); |
| 6154 | |
| 6155 | size_t Index = 0; |
| 6156 | for (unsigned char Byte : Input) { |
| 6157 | assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?"); |
| 6158 | Bytes[Offset.getQuantity() + Index] = Byte; |
| 6159 | ++Index; |
| 6160 | } |
| 6161 | } |
| 6162 | |
| 6163 | size_t size() { return Bytes.size(); } |
| 6164 | }; |
| 6165 | |
| 6166 | /// Traverse an APValue to produce an BitCastBuffer, emulating how the current |
| 6167 | /// target would represent the value at runtime. |
| 6168 | class APValueToBufferConverter { |
| 6169 | EvalInfo &Info; |
| 6170 | BitCastBuffer Buffer; |
| 6171 | const CastExpr *BCE; |
| 6172 | |
| 6173 | APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth, |
| 6174 | const CastExpr *BCE) |
| 6175 | : Info(Info), |
| 6176 | Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()), |
| 6177 | BCE(BCE) {} |
| 6178 | |
| 6179 | bool visit(const APValue &Val, QualType Ty) { |
| 6180 | return visit(Val, Ty, CharUnits::fromQuantity(0)); |
| 6181 | } |
| 6182 | |
| 6183 | // Write out Val with type Ty into Buffer starting at Offset. |
| 6184 | bool visit(const APValue &Val, QualType Ty, CharUnits Offset) { |
| 6185 | assert((size_t)Offset.getQuantity() <= Buffer.size()); |
| 6186 | |
| 6187 | // As a special case, nullptr_t has an indeterminate value. |
| 6188 | if (Ty->isNullPtrType()) |
| 6189 | return true; |
| 6190 | |
| 6191 | // Dig through Src to find the byte at SrcOffset. |
| 6192 | switch (Val.getKind()) { |
| 6193 | case APValue::Indeterminate: |
| 6194 | case APValue::None: |
| 6195 | return true; |
| 6196 | |
| 6197 | case APValue::Int: |
| 6198 | return visitInt(Val.getInt(), Ty, Offset); |
| 6199 | case APValue::Float: |
| 6200 | return visitFloat(Val.getFloat(), Ty, Offset); |
| 6201 | case APValue::Array: |
| 6202 | return visitArray(Val, Ty, Offset); |
| 6203 | case APValue::Struct: |
| 6204 | return visitRecord(Val, Ty, Offset); |
| 6205 | |
| 6206 | case APValue::ComplexInt: |
| 6207 | case APValue::ComplexFloat: |
| 6208 | case APValue::Vector: |
| 6209 | case APValue::FixedPoint: |
| 6210 | // FIXME: We should support these. |
| 6211 | |
| 6212 | case APValue::Union: |
| 6213 | case APValue::MemberPointer: |
| 6214 | case APValue::AddrLabelDiff: { |
| 6215 | Info.FFDiag(BCE->getBeginLoc(), |
| 6216 | diag::note_constexpr_bit_cast_unsupported_type) |
| 6217 | << Ty; |
| 6218 | return false; |
| 6219 | } |
| 6220 | |
| 6221 | case APValue::LValue: |
| 6222 | llvm_unreachable("LValue subobject in bit_cast?"); |
| 6223 | } |
| Simon Pilgrim | 71600be | 2019-07-03 09:54:25 +0000 | [diff] [blame] | 6224 | llvm_unreachable("Unhandled APValue::ValueKind"); |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 6225 | } |
| 6226 | |
| 6227 | bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) { |
| 6228 | const RecordDecl *RD = Ty->getAsRecordDecl(); |
| 6229 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 6230 | |
| 6231 | // Visit the base classes. |
| 6232 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 6233 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
| 6234 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
| 6235 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
| 6236 | |
| 6237 | if (!visitRecord(Val.getStructBase(I), BS.getType(), |
| 6238 | Layout.getBaseClassOffset(BaseDecl) + Offset)) |
| 6239 | return false; |
| 6240 | } |
| 6241 | } |
| 6242 | |
| 6243 | // Visit the fields. |
| 6244 | unsigned FieldIdx = 0; |
| 6245 | for (FieldDecl *FD : RD->fields()) { |
| 6246 | if (FD->isBitField()) { |
| 6247 | Info.FFDiag(BCE->getBeginLoc(), |
| 6248 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
| 6249 | return false; |
| 6250 | } |
| 6251 | |
| 6252 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx); |
| 6253 | |
| 6254 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 && |
| 6255 | "only bit-fields can have sub-char alignment"); |
| 6256 | CharUnits FieldOffset = |
| 6257 | Info.Ctx.toCharUnitsFromBits(FieldOffsetBits) + Offset; |
| 6258 | QualType FieldTy = FD->getType(); |
| 6259 | if (!visit(Val.getStructField(FieldIdx), FieldTy, FieldOffset)) |
| 6260 | return false; |
| 6261 | ++FieldIdx; |
| 6262 | } |
| 6263 | |
| 6264 | return true; |
| 6265 | } |
| 6266 | |
| 6267 | bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) { |
| 6268 | const auto *CAT = |
| 6269 | dyn_cast_or_null<ConstantArrayType>(Ty->getAsArrayTypeUnsafe()); |
| 6270 | if (!CAT) |
| 6271 | return false; |
| 6272 | |
| 6273 | CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(CAT->getElementType()); |
| 6274 | unsigned NumInitializedElts = Val.getArrayInitializedElts(); |
| 6275 | unsigned ArraySize = Val.getArraySize(); |
| 6276 | // First, initialize the initialized elements. |
| 6277 | for (unsigned I = 0; I != NumInitializedElts; ++I) { |
| 6278 | const APValue &SubObj = Val.getArrayInitializedElt(I); |
| 6279 | if (!visit(SubObj, CAT->getElementType(), Offset + I * ElemWidth)) |
| 6280 | return false; |
| 6281 | } |
| 6282 | |
| 6283 | // Next, initialize the rest of the array using the filler. |
| 6284 | if (Val.hasArrayFiller()) { |
| 6285 | const APValue &Filler = Val.getArrayFiller(); |
| 6286 | for (unsigned I = NumInitializedElts; I != ArraySize; ++I) { |
| 6287 | if (!visit(Filler, CAT->getElementType(), Offset + I * ElemWidth)) |
| 6288 | return false; |
| 6289 | } |
| 6290 | } |
| 6291 | |
| 6292 | return true; |
| 6293 | } |
| 6294 | |
| 6295 | bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) { |
| 6296 | CharUnits Width = Info.Ctx.getTypeSizeInChars(Ty); |
| 6297 | SmallVector<unsigned char, 8> Bytes(Width.getQuantity()); |
| 6298 | llvm::StoreIntToMemory(Val, &*Bytes.begin(), Width.getQuantity()); |
| 6299 | Buffer.writeObject(Offset, Bytes); |
| 6300 | return true; |
| 6301 | } |
| 6302 | |
| 6303 | bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) { |
| 6304 | APSInt AsInt(Val.bitcastToAPInt()); |
| 6305 | return visitInt(AsInt, Ty, Offset); |
| 6306 | } |
| 6307 | |
| 6308 | public: |
| 6309 | static Optional<BitCastBuffer> convert(EvalInfo &Info, const APValue &Src, |
| 6310 | const CastExpr *BCE) { |
| 6311 | CharUnits DstSize = Info.Ctx.getTypeSizeInChars(BCE->getType()); |
| 6312 | APValueToBufferConverter Converter(Info, DstSize, BCE); |
| 6313 | if (!Converter.visit(Src, BCE->getSubExpr()->getType())) |
| 6314 | return None; |
| 6315 | return Converter.Buffer; |
| 6316 | } |
| 6317 | }; |
| 6318 | |
| 6319 | /// Write an BitCastBuffer into an APValue. |
| 6320 | class BufferToAPValueConverter { |
| 6321 | EvalInfo &Info; |
| 6322 | const BitCastBuffer &Buffer; |
| 6323 | const CastExpr *BCE; |
| 6324 | |
| 6325 | BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer, |
| 6326 | const CastExpr *BCE) |
| 6327 | : Info(Info), Buffer(Buffer), BCE(BCE) {} |
| 6328 | |
| 6329 | // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast |
| 6330 | // with an invalid type, so anything left is a deficiency on our part (FIXME). |
| 6331 | // Ideally this will be unreachable. |
| 6332 | llvm::NoneType unsupportedType(QualType Ty) { |
| 6333 | Info.FFDiag(BCE->getBeginLoc(), |
| 6334 | diag::note_constexpr_bit_cast_unsupported_type) |
| 6335 | << Ty; |
| 6336 | return None; |
| 6337 | } |
| 6338 | |
| 6339 | Optional<APValue> visit(const BuiltinType *T, CharUnits Offset, |
| 6340 | const EnumType *EnumSugar = nullptr) { |
| 6341 | if (T->isNullPtrType()) { |
| 6342 | uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QualType(T, 0)); |
| 6343 | return APValue((Expr *)nullptr, |
| 6344 | /*Offset=*/CharUnits::fromQuantity(NullValue), |
| 6345 | APValue::NoLValuePath{}, /*IsNullPtr=*/true); |
| 6346 | } |
| 6347 | |
| 6348 | CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T); |
| 6349 | SmallVector<uint8_t, 8> Bytes; |
| 6350 | if (!Buffer.readObject(Offset, SizeOf, Bytes)) { |
| 6351 | // If this is std::byte or unsigned char, then its okay to store an |
| 6352 | // indeterminate value. |
| 6353 | bool IsStdByte = EnumSugar && EnumSugar->isStdByteType(); |
| 6354 | bool IsUChar = |
| 6355 | !EnumSugar && (T->isSpecificBuiltinType(BuiltinType::UChar) || |
| 6356 | T->isSpecificBuiltinType(BuiltinType::Char_U)); |
| 6357 | if (!IsStdByte && !IsUChar) { |
| Simon Pilgrim | bc7f30e8 | 2019-07-03 12:20:28 +0000 | [diff] [blame] | 6358 | QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0); |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 6359 | Info.FFDiag(BCE->getExprLoc(), |
| 6360 | diag::note_constexpr_bit_cast_indet_dest) |
| 6361 | << DisplayType << Info.Ctx.getLangOpts().CharIsSigned; |
| 6362 | return None; |
| 6363 | } |
| 6364 | |
| 6365 | return APValue::IndeterminateValue(); |
| 6366 | } |
| 6367 | |
| 6368 | APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true); |
| 6369 | llvm::LoadIntFromMemory(Val, &*Bytes.begin(), Bytes.size()); |
| 6370 | |
| 6371 | if (T->isIntegralOrEnumerationType()) { |
| 6372 | Val.setIsSigned(T->isSignedIntegerOrEnumerationType()); |
| 6373 | return APValue(Val); |
| 6374 | } |
| 6375 | |
| 6376 | if (T->isRealFloatingType()) { |
| 6377 | const llvm::fltSemantics &Semantics = |
| 6378 | Info.Ctx.getFloatTypeSemantics(QualType(T, 0)); |
| 6379 | return APValue(APFloat(Semantics, Val)); |
| 6380 | } |
| 6381 | |
| 6382 | return unsupportedType(QualType(T, 0)); |
| 6383 | } |
| 6384 | |
| 6385 | Optional<APValue> visit(const RecordType *RTy, CharUnits Offset) { |
| 6386 | const RecordDecl *RD = RTy->getAsRecordDecl(); |
| 6387 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 6388 | |
| 6389 | unsigned NumBases = 0; |
| 6390 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 6391 | NumBases = CXXRD->getNumBases(); |
| 6392 | |
| 6393 | APValue ResultVal(APValue::UninitStruct(), NumBases, |
| 6394 | std::distance(RD->field_begin(), RD->field_end())); |
| 6395 | |
| 6396 | // Visit the base classes. |
| 6397 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 6398 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
| 6399 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
| 6400 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
| 6401 | if (BaseDecl->isEmpty() || |
| 6402 | Info.Ctx.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero()) |
| 6403 | continue; |
| 6404 | |
| 6405 | Optional<APValue> SubObj = visitType( |
| 6406 | BS.getType(), Layout.getBaseClassOffset(BaseDecl) + Offset); |
| 6407 | if (!SubObj) |
| 6408 | return None; |
| 6409 | ResultVal.getStructBase(I) = *SubObj; |
| 6410 | } |
| 6411 | } |
| 6412 | |
| 6413 | // Visit the fields. |
| 6414 | unsigned FieldIdx = 0; |
| 6415 | for (FieldDecl *FD : RD->fields()) { |
| 6416 | // FIXME: We don't currently support bit-fields. A lot of the logic for |
| 6417 | // this is in CodeGen, so we need to factor it around. |
| 6418 | if (FD->isBitField()) { |
| 6419 | Info.FFDiag(BCE->getBeginLoc(), |
| 6420 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
| 6421 | return None; |
| 6422 | } |
| 6423 | |
| 6424 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx); |
| 6425 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0); |
| 6426 | |
| 6427 | CharUnits FieldOffset = |
| 6428 | CharUnits::fromQuantity(FieldOffsetBits / Info.Ctx.getCharWidth()) + |
| 6429 | Offset; |
| 6430 | QualType FieldTy = FD->getType(); |
| 6431 | Optional<APValue> SubObj = visitType(FieldTy, FieldOffset); |
| 6432 | if (!SubObj) |
| 6433 | return None; |
| 6434 | ResultVal.getStructField(FieldIdx) = *SubObj; |
| 6435 | ++FieldIdx; |
| 6436 | } |
| 6437 | |
| 6438 | return ResultVal; |
| 6439 | } |
| 6440 | |
| 6441 | Optional<APValue> visit(const EnumType *Ty, CharUnits Offset) { |
| 6442 | QualType RepresentationType = Ty->getDecl()->getIntegerType(); |
| 6443 | assert(!RepresentationType.isNull() && |
| 6444 | "enum forward decl should be caught by Sema"); |
| Simon Pilgrim | c15b38e | 2019-10-03 15:08:30 +0000 | [diff] [blame] | 6445 | const auto *AsBuiltin = |
| 6446 | RepresentationType.getCanonicalType()->castAs<BuiltinType>(); |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 6447 | // Recurse into the underlying type. Treat std::byte transparently as |
| 6448 | // unsigned char. |
| 6449 | return visit(AsBuiltin, Offset, /*EnumTy=*/Ty); |
| 6450 | } |
| 6451 | |
| 6452 | Optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) { |
| 6453 | size_t Size = Ty->getSize().getLimitedValue(); |
| 6454 | CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(Ty->getElementType()); |
| 6455 | |
| 6456 | APValue ArrayValue(APValue::UninitArray(), Size, Size); |
| 6457 | for (size_t I = 0; I != Size; ++I) { |
| 6458 | Optional<APValue> ElementValue = |
| 6459 | visitType(Ty->getElementType(), Offset + I * ElementWidth); |
| 6460 | if (!ElementValue) |
| 6461 | return None; |
| 6462 | ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue); |
| 6463 | } |
| 6464 | |
| 6465 | return ArrayValue; |
| 6466 | } |
| 6467 | |
| 6468 | Optional<APValue> visit(const Type *Ty, CharUnits Offset) { |
| 6469 | return unsupportedType(QualType(Ty, 0)); |
| 6470 | } |
| 6471 | |
| 6472 | Optional<APValue> visitType(QualType Ty, CharUnits Offset) { |
| 6473 | QualType Can = Ty.getCanonicalType(); |
| 6474 | |
| 6475 | switch (Can->getTypeClass()) { |
| 6476 | #define TYPE(Class, Base) \ |
| 6477 | case Type::Class: \ |
| 6478 | return visit(cast<Class##Type>(Can.getTypePtr()), Offset); |
| 6479 | #define ABSTRACT_TYPE(Class, Base) |
| 6480 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 6481 | case Type::Class: \ |
| 6482 | llvm_unreachable("non-canonical type should be impossible!"); |
| 6483 | #define DEPENDENT_TYPE(Class, Base) \ |
| 6484 | case Type::Class: \ |
| 6485 | llvm_unreachable( \ |
| 6486 | "dependent types aren't supported in the constant evaluator!"); |
| 6487 | #define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \ |
| 6488 | case Type::Class: \ |
| 6489 | llvm_unreachable("either dependent or not canonical!"); |
| John McCall | 36b12a8 | 2019-10-02 06:35:23 +0000 | [diff] [blame] | 6490 | #include "clang/AST/TypeNodes.inc" |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 6491 | } |
| Simon Pilgrim | 71600be | 2019-07-03 09:54:25 +0000 | [diff] [blame] | 6492 | llvm_unreachable("Unhandled Type::TypeClass"); |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 6493 | } |
| 6494 | |
| 6495 | public: |
| 6496 | // Pull out a full value of type DstType. |
| 6497 | static Optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer, |
| 6498 | const CastExpr *BCE) { |
| 6499 | BufferToAPValueConverter Converter(Info, Buffer, BCE); |
| 6500 | return Converter.visitType(BCE->getType(), CharUnits::fromQuantity(0)); |
| 6501 | } |
| 6502 | }; |
| 6503 | |
| 6504 | static bool checkBitCastConstexprEligibilityType(SourceLocation Loc, |
| 6505 | QualType Ty, EvalInfo *Info, |
| 6506 | const ASTContext &Ctx, |
| 6507 | bool CheckingDest) { |
| 6508 | Ty = Ty.getCanonicalType(); |
| 6509 | |
| 6510 | auto diag = [&](int Reason) { |
| 6511 | if (Info) |
| 6512 | Info->FFDiag(Loc, diag::note_constexpr_bit_cast_invalid_type) |
| 6513 | << CheckingDest << (Reason == 4) << Reason; |
| 6514 | return false; |
| 6515 | }; |
| 6516 | auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) { |
| 6517 | if (Info) |
| 6518 | Info->Note(NoteLoc, diag::note_constexpr_bit_cast_invalid_subtype) |
| 6519 | << NoteTy << Construct << Ty; |
| 6520 | return false; |
| 6521 | }; |
| 6522 | |
| 6523 | if (Ty->isUnionType()) |
| 6524 | return diag(0); |
| 6525 | if (Ty->isPointerType()) |
| 6526 | return diag(1); |
| 6527 | if (Ty->isMemberPointerType()) |
| 6528 | return diag(2); |
| 6529 | if (Ty.isVolatileQualified()) |
| 6530 | return diag(3); |
| 6531 | |
| 6532 | if (RecordDecl *Record = Ty->getAsRecordDecl()) { |
| 6533 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Record)) { |
| 6534 | for (CXXBaseSpecifier &BS : CXXRD->bases()) |
| 6535 | if (!checkBitCastConstexprEligibilityType(Loc, BS.getType(), Info, Ctx, |
| 6536 | CheckingDest)) |
| 6537 | return note(1, BS.getType(), BS.getBeginLoc()); |
| 6538 | } |
| 6539 | for (FieldDecl *FD : Record->fields()) { |
| 6540 | if (FD->getType()->isReferenceType()) |
| 6541 | return diag(4); |
| 6542 | if (!checkBitCastConstexprEligibilityType(Loc, FD->getType(), Info, Ctx, |
| 6543 | CheckingDest)) |
| 6544 | return note(0, FD->getType(), FD->getBeginLoc()); |
| 6545 | } |
| 6546 | } |
| 6547 | |
| 6548 | if (Ty->isArrayType() && |
| 6549 | !checkBitCastConstexprEligibilityType(Loc, Ctx.getBaseElementType(Ty), |
| 6550 | Info, Ctx, CheckingDest)) |
| 6551 | return false; |
| 6552 | |
| 6553 | return true; |
| 6554 | } |
| 6555 | |
| 6556 | static bool checkBitCastConstexprEligibility(EvalInfo *Info, |
| 6557 | const ASTContext &Ctx, |
| 6558 | const CastExpr *BCE) { |
| 6559 | bool DestOK = checkBitCastConstexprEligibilityType( |
| 6560 | BCE->getBeginLoc(), BCE->getType(), Info, Ctx, true); |
| 6561 | bool SourceOK = DestOK && checkBitCastConstexprEligibilityType( |
| 6562 | BCE->getBeginLoc(), |
| 6563 | BCE->getSubExpr()->getType(), Info, Ctx, false); |
| 6564 | return SourceOK; |
| 6565 | } |
| 6566 | |
| 6567 | static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue, |
| 6568 | APValue &SourceValue, |
| 6569 | const CastExpr *BCE) { |
| 6570 | assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 && |
| 6571 | "no host or target supports non 8-bit chars"); |
| 6572 | assert(SourceValue.isLValue() && |
| 6573 | "LValueToRValueBitcast requires an lvalue operand!"); |
| 6574 | |
| 6575 | if (!checkBitCastConstexprEligibility(&Info, Info.Ctx, BCE)) |
| 6576 | return false; |
| 6577 | |
| 6578 | LValue SourceLValue; |
| 6579 | APValue SourceRValue; |
| 6580 | SourceLValue.setFrom(Info.Ctx, SourceValue); |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 6581 | if (!handleLValueToRValueConversion( |
| 6582 | Info, BCE, BCE->getSubExpr()->getType().withConst(), SourceLValue, |
| 6583 | SourceRValue, /*WantObjectRepresentation=*/true)) |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 6584 | return false; |
| 6585 | |
| 6586 | // Read out SourceValue into a char buffer. |
| 6587 | Optional<BitCastBuffer> Buffer = |
| 6588 | APValueToBufferConverter::convert(Info, SourceRValue, BCE); |
| 6589 | if (!Buffer) |
| 6590 | return false; |
| 6591 | |
| 6592 | // Write out the buffer into a new APValue. |
| 6593 | Optional<APValue> MaybeDestValue = |
| 6594 | BufferToAPValueConverter::convert(Info, *Buffer, BCE); |
| 6595 | if (!MaybeDestValue) |
| 6596 | return false; |
| 6597 | |
| 6598 | DestValue = std::move(*MaybeDestValue); |
| 6599 | return true; |
| 6600 | } |
| 6601 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6602 | template <class Derived> |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6603 | class ExprEvaluatorBase |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6604 | : public ConstStmtVisitor<Derived, bool> { |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6605 | private: |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6606 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6607 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6608 | return getDerived().Success(V, E); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6609 | } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6610 | bool DerivedZeroInitialization(const Expr *E) { |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6611 | return getDerived().ZeroInitialization(E); |
| Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 6612 | } |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6613 | |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6614 | // Check whether a conditional operator with a non-constant condition is a |
| 6615 | // potential constant expression. If neither arm is a potential constant |
| 6616 | // expression, then the conditional operator is not either. |
| 6617 | template<typename ConditionalOperator> |
| 6618 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 6619 | assert(Info.checkingPotentialConstantExpression()); |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6620 | |
| 6621 | // Speculatively evaluate both arms. |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 6622 | SmallVector<PartialDiagnosticAt, 8> Diag; |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6623 | { |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6624 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6625 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 6626 | if (Diag.empty()) |
| 6627 | return; |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 6628 | } |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6629 | |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 6630 | { |
| 6631 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6632 | Diag.clear(); |
| 6633 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 6634 | if (Diag.empty()) |
| 6635 | return; |
| 6636 | } |
| 6637 | |
| 6638 | Error(E, diag::note_constexpr_conditional_never_const); |
| 6639 | } |
| 6640 | |
| 6641 | |
| 6642 | template<typename ConditionalOperator> |
| 6643 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 6644 | bool BoolResult; |
| 6645 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
| Nick Lewycky | 20edee6 | 2017-04-27 07:11:09 +0000 | [diff] [blame] | 6646 | if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) { |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6647 | CheckPotentialConstantConditional(E); |
| Nick Lewycky | 20edee6 | 2017-04-27 07:11:09 +0000 | [diff] [blame] | 6648 | return false; |
| 6649 | } |
| 6650 | if (Info.noteFailure()) { |
| 6651 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 6652 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 6653 | } |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6654 | return false; |
| 6655 | } |
| 6656 | |
| 6657 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 6658 | return StmtVisitorTy::Visit(EvalExpr); |
| 6659 | } |
| 6660 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6661 | protected: |
| 6662 | EvalInfo &Info; |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6663 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6664 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 6665 | |
| Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 6666 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 6667 | return Info.CCEDiag(E, D); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6668 | } |
| 6669 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6670 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 6671 | |
| 6672 | public: |
| 6673 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 6674 | |
| 6675 | EvalInfo &getEvalInfo() { return Info; } |
| 6676 | |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6677 | /// Report an evaluation error. This should only be called when an error is |
| 6678 | /// first discovered. When propagating an error, just return false. |
| 6679 | bool Error(const Expr *E, diag::kind D) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 6680 | Info.FFDiag(E, D); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6681 | return false; |
| 6682 | } |
| 6683 | bool Error(const Expr *E) { |
| 6684 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 6685 | } |
| 6686 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6687 | bool VisitStmt(const Stmt *) { |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6688 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6689 | } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6690 | bool VisitExpr(const Expr *E) { |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6691 | return Error(E); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
| Bill Wendling | 8003edc | 2018-11-09 00:41:36 +0000 | [diff] [blame] | 6694 | bool VisitConstantExpr(const ConstantExpr *E) |
| 6695 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6696 | bool VisitParenExpr(const ParenExpr *E) |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6697 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6698 | bool VisitUnaryExtension(const UnaryOperator *E) |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6699 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6700 | bool VisitUnaryPlus(const UnaryOperator *E) |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6701 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6702 | bool VisitChooseExpr(const ChooseExpr *E) |
| Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 6703 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6704 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6705 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6706 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 6707 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 6708 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
| 6709 | TempVersionRAII RAII(*Info.CurrentCall); |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 6710 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 6711 | return StmtVisitorTy::Visit(E->getExpr()); |
| 6712 | } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6713 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 6714 | TempVersionRAII RAII(*Info.CurrentCall); |
| Richard Smith | 17e3246 | 2013-09-13 20:51:45 +0000 | [diff] [blame] | 6715 | // The initializer may not have been parsed yet, or might be erroneous. |
| 6716 | if (!E->getExpr()) |
| 6717 | return Error(E); |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 6718 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
| Richard Smith | 17e3246 | 2013-09-13 20:51:45 +0000 | [diff] [blame] | 6719 | return StmtVisitorTy::Visit(E->getExpr()); |
| 6720 | } |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 6721 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 6722 | bool VisitExprWithCleanups(const ExprWithCleanups *E) { |
| 6723 | FullExpressionRAII Scope(Info); |
| 6724 | return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy(); |
| 6725 | } |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6726 | |
| Richard Smith | 3b69bcc | 2019-10-01 00:41:16 +0000 | [diff] [blame] | 6727 | // Temporaries are registered when created, so we don't care about |
| 6728 | // CXXBindTemporaryExpr. |
| 6729 | bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) { |
| 6730 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 6731 | } |
| 6732 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6733 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
| Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 6734 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 6735 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 6736 | } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6737 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 6738 | if (!Info.Ctx.getLangOpts().CPlusPlus2a) |
| 6739 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 6740 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 6741 | } |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 6742 | bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) { |
| 6743 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 6744 | } |
| Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 6745 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6746 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6747 | switch (E->getOpcode()) { |
| 6748 | default: |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6749 | return Error(E); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6750 | |
| 6751 | case BO_Comma: |
| 6752 | VisitIgnoredValue(E->getLHS()); |
| 6753 | return StmtVisitorTy::Visit(E->getRHS()); |
| 6754 | |
| 6755 | case BO_PtrMemD: |
| 6756 | case BO_PtrMemI: { |
| 6757 | LValue Obj; |
| 6758 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 6759 | return false; |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6760 | APValue Result; |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 6761 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6762 | return false; |
| 6763 | return DerivedSuccess(Result, E); |
| 6764 | } |
| 6765 | } |
| 6766 | } |
| 6767 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6768 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
| Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 6769 | // Evaluate and cache the common expression. We treat it as a temporary, |
| 6770 | // even though it's not quite the same thing. |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 6771 | LValue CommonLV; |
| 6772 | if (!Evaluate(Info.CurrentCall->createTemporary( |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 6773 | E->getOpaqueValue(), |
| 6774 | getStorageType(Info.Ctx, E->getOpaqueValue()), false, |
| 6775 | CommonLV), |
| Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 6776 | Info, E->getCommon())) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6777 | return false; |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6778 | |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 6779 | return HandleConditionalOperator(E); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6780 | } |
| 6781 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6782 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6783 | bool IsBcpCall = false; |
| 6784 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 6785 | // the result is a constant expression if it can be folded without |
| 6786 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 6787 | // for discussion. |
| 6788 | if (const CallExpr *CallCE = |
| 6789 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
| Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 6790 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6791 | IsBcpCall = true; |
| 6792 | |
| 6793 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 6794 | // constant expression; we can't check whether it's potentially foldable. |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 6795 | // FIXME: We should instead treat __builtin_constant_p as non-constant if |
| 6796 | // it would return 'false' in this mode. |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 6797 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6798 | return false; |
| 6799 | |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 6800 | FoldConstant Fold(Info, IsBcpCall); |
| 6801 | if (!HandleConditionalOperator(E)) { |
| 6802 | Fold.keepDiagnostics(); |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6803 | return false; |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 6804 | } |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 6805 | |
| 6806 | return true; |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6807 | } |
| 6808 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6809 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 6810 | if (APValue *Value = Info.CurrentCall->getCurrentTemporary(E)) |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 6811 | return DerivedSuccess(*Value, E); |
| 6812 | |
| 6813 | const Expr *Source = E->getSourceExpr(); |
| 6814 | if (!Source) |
| 6815 | return Error(E); |
| 6816 | if (Source == E) { // sanity checking. |
| 6817 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
| 6818 | return Error(E); |
| Argyrios Kyrtzidis | fac35c0 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 6819 | } |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 6820 | return StmtVisitorTy::Visit(Source); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6821 | } |
| Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 6822 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6823 | bool VisitCallExpr(const CallExpr *E) { |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6824 | APValue Result; |
| 6825 | if (!handleCallExpr(E, Result, nullptr)) |
| 6826 | return false; |
| 6827 | return DerivedSuccess(Result, E); |
| 6828 | } |
| 6829 | |
| 6830 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
| Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 6831 | const LValue *ResultSlot) { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6832 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 6833 | QualType CalleeType = Callee->getType(); |
| 6834 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6835 | const FunctionDecl *FD = nullptr; |
| 6836 | LValue *This = nullptr, ThisVal; |
| Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 6837 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 6838 | bool HasQualifier = false; |
| Richard Smith | 656d49d | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 6839 | |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6840 | // Extract function decl and 'this' pointer from the callee. |
| 6841 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 6842 | const CXXMethodDecl *Member = nullptr; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6843 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 6844 | // Explicit bound member calls, such as x.f() or p->g(); |
| 6845 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6846 | return false; |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 6847 | Member = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 6848 | if (!Member) |
| 6849 | return Error(Callee); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6850 | This = &ThisVal; |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 6851 | HasQualifier = ME->hasQualifier(); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6852 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 6853 | // Indirect bound member calls ('.*' or '->*'). |
| Richard Smith | 5258202 | 2019-10-03 01:20:27 +0000 | [diff] [blame] | 6854 | const ValueDecl *D = |
| 6855 | HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 6856 | if (!D) |
| 6857 | return false; |
| 6858 | Member = dyn_cast<CXXMethodDecl>(D); |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 6859 | if (!Member) |
| 6860 | return Error(Callee); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6861 | This = &ThisVal; |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 6862 | } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Callee)) { |
| 6863 | if (!Info.getLangOpts().CPlusPlus2a) |
| 6864 | Info.CCEDiag(PDE, diag::note_constexpr_pseudo_destructor); |
| 6865 | // FIXME: If pseudo-destructor calls ever start ending the lifetime of |
| 6866 | // their callee, we should start calling HandleDestruction here. |
| 6867 | // For now, we just evaluate the object argument and discard it. |
| 6868 | return EvaluateObjectArgument(Info, PDE->getBase(), ThisVal); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6869 | } else |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6870 | return Error(Callee); |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 6871 | FD = Member; |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6872 | } else if (CalleeType->isFunctionPointerType()) { |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6873 | LValue Call; |
| 6874 | if (!EvaluatePointer(Callee, Call, Info)) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6875 | return false; |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6876 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6877 | if (!Call.getLValueOffset().isZero()) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6878 | return Error(Callee); |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 6879 | FD = dyn_cast_or_null<FunctionDecl>( |
| 6880 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6881 | if (!FD) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6882 | return Error(Callee); |
| Faisal Vali | d92e749 | 2017-01-08 18:56:11 +0000 | [diff] [blame] | 6883 | // Don't call function pointers which have been cast to some other type. |
| 6884 | // Per DR (no number yet), the caller and callee can differ in noexcept. |
| 6885 | if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec( |
| 6886 | CalleeType->getPointeeType(), FD->getType())) { |
| 6887 | return Error(E); |
| 6888 | } |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6889 | |
| 6890 | // Overloaded operator calls to member functions are represented as normal |
| 6891 | // calls with '*this' as the first argument. |
| 6892 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 6893 | if (MD && !MD->isStatic()) { |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6894 | // FIXME: When selecting an implicit conversion for an overloaded |
| 6895 | // operator delete, we sometimes try to evaluate calls to conversion |
| 6896 | // operators without a 'this' parameter! |
| 6897 | if (Args.empty()) |
| 6898 | return Error(E); |
| 6899 | |
| Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 6900 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6901 | return false; |
| 6902 | This = &ThisVal; |
| Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 6903 | Args = Args.slice(1); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6904 | } else if (MD && MD->isLambdaStaticInvoker()) { |
| Faisal Vali | d92e749 | 2017-01-08 18:56:11 +0000 | [diff] [blame] | 6905 | // Map the static invoker for the lambda back to the call operator. |
| 6906 | // Conveniently, we don't have to slice out the 'this' argument (as is |
| 6907 | // being done for the non-static case), since a static member function |
| 6908 | // doesn't have an implicit argument passed in. |
| 6909 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
| 6910 | assert( |
| 6911 | ClosureClass->captures_begin() == ClosureClass->captures_end() && |
| 6912 | "Number of captures must be zero for conversion to function-ptr"); |
| 6913 | |
| 6914 | const CXXMethodDecl *LambdaCallOp = |
| 6915 | ClosureClass->getLambdaCallOperator(); |
| 6916 | |
| 6917 | // Set 'FD', the function that will be called below, to the call |
| 6918 | // operator. If the closure object represents a generic lambda, find |
| 6919 | // the corresponding specialization of the call operator. |
| 6920 | |
| 6921 | if (ClosureClass->isGenericLambda()) { |
| 6922 | assert(MD->isFunctionTemplateSpecialization() && |
| 6923 | "A generic lambda's static-invoker function must be a " |
| 6924 | "template specialization"); |
| 6925 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
| 6926 | FunctionTemplateDecl *CallOpTemplate = |
| 6927 | LambdaCallOp->getDescribedFunctionTemplate(); |
| 6928 | void *InsertPos = nullptr; |
| 6929 | FunctionDecl *CorrespondingCallOpSpecialization = |
| 6930 | CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); |
| 6931 | assert(CorrespondingCallOpSpecialization && |
| 6932 | "We must always have a function call operator specialization " |
| 6933 | "that corresponds to our static invoker specialization"); |
| 6934 | FD = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
| 6935 | } else |
| 6936 | FD = LambdaCallOp; |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 6937 | } else if (FD->isReplaceableGlobalAllocationFunction()) { |
| 6938 | if (FD->getDeclName().getCXXOverloadedOperator() == OO_New || |
| 6939 | FD->getDeclName().getCXXOverloadedOperator() == OO_Array_New) { |
| 6940 | LValue Ptr; |
| 6941 | if (!HandleOperatorNewCall(Info, E, Ptr)) |
| 6942 | return false; |
| 6943 | Ptr.moveInto(Result); |
| 6944 | return true; |
| 6945 | } else { |
| 6946 | return HandleOperatorDeleteCall(Info, E); |
| 6947 | } |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6948 | } |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6949 | } else |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6950 | return Error(E); |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 6951 | |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 6952 | SmallVector<QualType, 4> CovariantAdjustmentPath; |
| 6953 | if (This) { |
| 6954 | auto *NamedMember = dyn_cast<CXXMethodDecl>(FD); |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 6955 | if (NamedMember && NamedMember->isVirtual() && !HasQualifier) { |
| 6956 | // Perform virtual dispatch, if necessary. |
| 6957 | FD = HandleVirtualDispatch(Info, E, *This, NamedMember, |
| 6958 | CovariantAdjustmentPath); |
| 6959 | if (!FD) |
| 6960 | return false; |
| 6961 | } else { |
| 6962 | // Check that the 'this' pointer points to an object of the right type. |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 6963 | // FIXME: If this is an assignment operator call, we may need to change |
| 6964 | // the active union member before we check this. |
| 6965 | if (!checkNonVirtualMemberCallThisPointer(Info, E, *This, NamedMember)) |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 6966 | return false; |
| 6967 | } |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 6968 | } |
| Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 6969 | |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 6970 | // Destructor calls are different enough that they have their own codepath. |
| 6971 | if (auto *DD = dyn_cast<CXXDestructorDecl>(FD)) { |
| 6972 | assert(This && "no 'this' pointer for destructor call"); |
| 6973 | return HandleDestruction(Info, E, *This, |
| 6974 | Info.Ctx.getRecordType(DD->getParent())); |
| 6975 | } |
| 6976 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6977 | const FunctionDecl *Definition = nullptr; |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 6978 | Stmt *Body = FD->getBody(Definition); |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 6979 | |
| Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 6980 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body) || |
| 6981 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, Info, |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6982 | Result, ResultSlot)) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6983 | return false; |
| 6984 | |
| Richard Smith | 921f132 | 2019-05-13 23:35:21 +0000 | [diff] [blame] | 6985 | if (!CovariantAdjustmentPath.empty() && |
| 6986 | !HandleCovariantReturnAdjustment(Info, E, Result, |
| 6987 | CovariantAdjustmentPath)) |
| 6988 | return false; |
| 6989 | |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6990 | return true; |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 6991 | } |
| 6992 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6993 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6994 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 6995 | } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6996 | bool VisitInitListExpr(const InitListExpr *E) { |
| Eli Friedman | 90dc175 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 6997 | if (E->getNumInits() == 0) |
| 6998 | return DerivedZeroInitialization(E); |
| 6999 | if (E->getNumInits() == 1) |
| 7000 | return StmtVisitorTy::Visit(E->getInit(0)); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7001 | return Error(E); |
| Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7002 | } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7003 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7004 | return DerivedZeroInitialization(E); |
| Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7005 | } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7006 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7007 | return DerivedZeroInitialization(E); |
| Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7008 | } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7009 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7010 | return DerivedZeroInitialization(E); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7011 | } |
| Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7012 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7013 | /// 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] | 7014 | bool VisitMemberExpr(const MemberExpr *E) { |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 7015 | assert(!Info.Ctx.getLangOpts().CPlusPlus11 && |
| 7016 | "missing temporary materialization conversion"); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7017 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 7018 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7019 | APValue Val; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7020 | if (!Evaluate(Val, Info, E->getBase())) |
| 7021 | return false; |
| 7022 | |
| 7023 | QualType BaseTy = E->getBase()->getType(); |
| 7024 | |
| 7025 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7026 | if (!FD) return Error(E); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7027 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7028 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7029 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 7030 | |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 7031 | // Note: there is no lvalue base here. But this case should only ever |
| 7032 | // happen in C or in C++98, where we cannot be evaluating a constexpr |
| 7033 | // constructor, which is the only case the base matters. |
| Richard Smith | debad64 | 2019-05-12 09:39:08 +0000 | [diff] [blame] | 7034 | CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy); |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 7035 | SubobjectDesignator Designator(BaseTy); |
| 7036 | Designator.addDeclUnchecked(FD); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7037 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7038 | APValue Result; |
| 7039 | return extractSubobject(Info, E, Obj, Designator, Result) && |
| 7040 | DerivedSuccess(Result, E); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7043 | bool VisitCastExpr(const CastExpr *E) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7044 | switch (E->getCastKind()) { |
| 7045 | default: |
| 7046 | break; |
| 7047 | |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7048 | case CK_AtomicToNonAtomic: { |
| 7049 | APValue AtomicVal; |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 7050 | // This does not need to be done in place even for class/array types: |
| 7051 | // atomic-to-non-atomic conversion implies copying the object |
| 7052 | // representation. |
| 7053 | if (!Evaluate(AtomicVal, Info, E->getSubExpr())) |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 7054 | return false; |
| 7055 | return DerivedSuccess(AtomicVal, E); |
| 7056 | } |
| 7057 | |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7058 | case CK_NoOp: |
| Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 7059 | case CK_UserDefinedConversion: |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7060 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 7061 | |
| 7062 | case CK_LValueToRValue: { |
| 7063 | LValue LVal; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7064 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 7065 | return false; |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7066 | APValue RVal; |
| Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 7067 | // 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] | 7068 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
| Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 7069 | LVal, RVal)) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7070 | return false; |
| 7071 | return DerivedSuccess(RVal, E); |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7072 | } |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 7073 | case CK_LValueToRValueBitCast: { |
| 7074 | APValue DestValue, SourceValue; |
| 7075 | if (!Evaluate(SourceValue, Info, E->getSubExpr())) |
| 7076 | return false; |
| 7077 | if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, E)) |
| 7078 | return false; |
| 7079 | return DerivedSuccess(DestValue, E); |
| 7080 | } |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7081 | } |
| 7082 | |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7083 | return Error(E); |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7084 | } |
| 7085 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7086 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7087 | return VisitUnaryPostIncDec(UO); |
| 7088 | } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7089 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7090 | return VisitUnaryPostIncDec(UO); |
| 7091 | } |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7092 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 7093 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7094 | return Error(UO); |
| 7095 | |
| 7096 | LValue LVal; |
| 7097 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
| 7098 | return false; |
| 7099 | APValue RVal; |
| 7100 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
| 7101 | UO->isIncrementOp(), &RVal)) |
| 7102 | return false; |
| 7103 | return DerivedSuccess(RVal, UO); |
| 7104 | } |
| 7105 | |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7106 | bool VisitStmtExpr(const StmtExpr *E) { |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 7107 | // We will have checked the full-expressions inside the statement expression |
| 7108 | // when they were completed, and don't need to check them again now. |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 7109 | if (Info.checkingForUndefinedBehavior()) |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 7110 | return Error(E); |
| 7111 | |
| 7112 | const CompoundStmt *CS = E->getSubStmt(); |
| Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 7113 | if (CS->body_empty()) |
| 7114 | return true; |
| 7115 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 7116 | BlockScopeRAII Scope(Info); |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 7117 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 7118 | BE = CS->body_end(); |
| 7119 | /**/; ++BI) { |
| 7120 | if (BI + 1 == BE) { |
| 7121 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
| 7122 | if (!FinalExpr) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7123 | Info.FFDiag((*BI)->getBeginLoc(), |
| 7124 | diag::note_constexpr_stmt_expr_unsupported); |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 7125 | return false; |
| 7126 | } |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 7127 | return this->Visit(FinalExpr) && Scope.destroy(); |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 7128 | } |
| 7129 | |
| 7130 | APValue ReturnValue; |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 7131 | StmtResult Result = { ReturnValue, nullptr }; |
| 7132 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 7133 | if (ESR != ESR_Succeeded) { |
| 7134 | // FIXME: If the statement-expression terminated due to 'return', |
| 7135 | // 'break', or 'continue', it would be nice to propagate that to |
| 7136 | // the outer statement evaluation rather than bailing out. |
| 7137 | if (ESR != ESR_Failed) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 7138 | Info.FFDiag((*BI)->getBeginLoc(), |
| 7139 | diag::note_constexpr_stmt_expr_unsupported); |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 7140 | return false; |
| 7141 | } |
| 7142 | } |
| Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 7143 | |
| 7144 | llvm_unreachable("Return from function from the loop above."); |
| Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 7145 | } |
| 7146 | |
| Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 7147 | /// Visit a value which is evaluated, but whose value is ignored. |
| 7148 | void VisitIgnoredValue(const Expr *E) { |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 7149 | EvaluateIgnoredValue(Info, E); |
| Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 7150 | } |
| David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 7151 | |
| 7152 | /// Potentially visit a MemberExpr's base expression. |
| 7153 | void VisitIgnoredBaseExpression(const Expr *E) { |
| 7154 | // While MSVC doesn't evaluate the base expression, it does diagnose the |
| 7155 | // presence of side-effecting behavior. |
| 7156 | if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Info.Ctx)) |
| 7157 | return; |
| 7158 | VisitIgnoredValue(E); |
| 7159 | } |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7160 | }; |
| 7161 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 7162 | } // namespace |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7163 | |
| 7164 | //===----------------------------------------------------------------------===// |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7165 | // Common base class for lvalue and temporary evaluation. |
| 7166 | //===----------------------------------------------------------------------===// |
| 7167 | namespace { |
| 7168 | template<class Derived> |
| 7169 | class LValueExprEvaluatorBase |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7170 | : public ExprEvaluatorBase<Derived> { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7171 | protected: |
| 7172 | LValue &Result; |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7173 | bool InvalidBaseOK; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7174 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7175 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7176 | |
| 7177 | bool Success(APValue::LValueBase B) { |
| 7178 | Result.set(B); |
| 7179 | return true; |
| 7180 | } |
| 7181 | |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7182 | bool evaluatePointer(const Expr *E, LValue &Result) { |
| 7183 | return EvaluatePointer(E, Result, this->Info, InvalidBaseOK); |
| 7184 | } |
| 7185 | |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7186 | public: |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7187 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) |
| 7188 | : ExprEvaluatorBaseTy(Info), Result(Result), |
| 7189 | InvalidBaseOK(InvalidBaseOK) {} |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7190 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7191 | bool Success(const APValue &V, const Expr *E) { |
| 7192 | Result.setFrom(this->Info.Ctx, V); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7193 | return true; |
| 7194 | } |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7195 | |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7196 | bool VisitMemberExpr(const MemberExpr *E) { |
| 7197 | // Handle non-static data members. |
| 7198 | QualType BaseTy; |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7199 | bool EvalOK; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7200 | if (E->isArrow()) { |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7201 | EvalOK = evaluatePointer(E->getBase(), Result); |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7202 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7203 | } else if (E->getBase()->isRValue()) { |
| Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 7204 | assert(E->getBase()->getType()->isRecordType()); |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7205 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 7206 | BaseTy = E->getBase()->getType(); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7207 | } else { |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7208 | EvalOK = this->Visit(E->getBase()); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7209 | BaseTy = E->getBase()->getType(); |
| 7210 | } |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7211 | if (!EvalOK) { |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7212 | if (!InvalidBaseOK) |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7213 | return false; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7214 | Result.setInvalid(E); |
| 7215 | return true; |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7216 | } |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7217 | |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 7218 | const ValueDecl *MD = E->getMemberDecl(); |
| 7219 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame] | 7220 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 7221 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 7222 | (void)BaseTy; |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 7223 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
| 7224 | return false; |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 7225 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 7226 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
| 7227 | return false; |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 7228 | } else |
| 7229 | return this->Error(E); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7230 | |
| Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 7231 | if (MD->getType()->isReferenceType()) { |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7232 | APValue RefValue; |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7233 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7234 | RefValue)) |
| 7235 | return false; |
| 7236 | return Success(RefValue, E); |
| 7237 | } |
| 7238 | return true; |
| 7239 | } |
| 7240 | |
| 7241 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 7242 | switch (E->getOpcode()) { |
| 7243 | default: |
| 7244 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 7245 | |
| 7246 | case BO_PtrMemD: |
| 7247 | case BO_PtrMemI: |
| 7248 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 7249 | } |
| 7250 | } |
| 7251 | |
| 7252 | bool VisitCastExpr(const CastExpr *E) { |
| 7253 | switch (E->getCastKind()) { |
| 7254 | default: |
| 7255 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 7256 | |
| 7257 | case CK_DerivedToBase: |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7258 | case CK_UncheckedDerivedToBase: |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7259 | if (!this->Visit(E->getSubExpr())) |
| 7260 | return false; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7261 | |
| 7262 | // Now figure out the necessary offset to add to the base LV to get from |
| 7263 | // the derived class to the base class. |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7264 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
| 7265 | Result); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7266 | } |
| 7267 | } |
| 7268 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7269 | } |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7270 | |
| 7271 | //===----------------------------------------------------------------------===// |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7272 | // LValue Evaluation |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7273 | // |
| 7274 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 7275 | // function designators (in C), decl references to void objects (in C), and |
| 7276 | // temporaries (if building with -Wno-address-of-temporary). |
| 7277 | // |
| 7278 | // LValue evaluation produces values comprising a base expression of one of the |
| 7279 | // following types: |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7280 | // - Declarations |
| 7281 | // * VarDecl |
| 7282 | // * FunctionDecl |
| 7283 | // - Literals |
| Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 7284 | // * CompoundLiteralExpr in C (and in global scope in C++) |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7285 | // * StringLiteral |
| 7286 | // * PredefinedExpr |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7287 | // * ObjCStringLiteralExpr |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7288 | // * ObjCEncodeExpr |
| 7289 | // * AddrLabelExpr |
| 7290 | // * BlockExpr |
| 7291 | // * CallExpr for a MakeStringConstant builtin |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 7292 | // - typeid(T) expressions, as TypeInfoLValues |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7293 | // - Locals and temporaries |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7294 | // * MaterializeTemporaryExpr |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7295 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7296 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
| 7297 | // from the AST (FIXME). |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7298 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
| 7299 | // CallIndex, for a lifetime-extended temporary. |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7300 | // plus an offset in bytes. |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7301 | //===----------------------------------------------------------------------===// |
| 7302 | namespace { |
| Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7303 | class LValueExprEvaluator |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7304 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7305 | public: |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7306 | LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) : |
| 7307 | LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {} |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7308 | |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7309 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7310 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7311 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7312 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 7313 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 7314 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7315 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 7316 | bool VisitMemberExpr(const MemberExpr *E); |
| 7317 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 7318 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
| Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 7319 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
| Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 7320 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7321 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 7322 | bool VisitUnaryDeref(const UnaryOperator *E); |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 7323 | bool VisitUnaryReal(const UnaryOperator *E); |
| 7324 | bool VisitUnaryImag(const UnaryOperator *E); |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7325 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
| 7326 | return VisitUnaryPreIncDec(UO); |
| 7327 | } |
| 7328 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
| 7329 | return VisitUnaryPreIncDec(UO); |
| 7330 | } |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7331 | bool VisitBinAssign(const BinaryOperator *BO); |
| 7332 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
| Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 7333 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7334 | bool VisitCastExpr(const CastExpr *E) { |
| Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 7335 | switch (E->getCastKind()) { |
| 7336 | default: |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7337 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 7338 | |
| Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 7339 | case CK_LValueBitCast: |
| Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 7340 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 7341 | if (!Visit(E->getSubExpr())) |
| 7342 | return false; |
| 7343 | Result.Designator.setInvalid(); |
| 7344 | return true; |
| Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 7345 | |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7346 | case CK_BaseToDerived: |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7347 | if (!Visit(E->getSubExpr())) |
| 7348 | return false; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7349 | return HandleBaseToDerivedCast(Info, E, Result); |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 7350 | |
| 7351 | case CK_Dynamic: |
| 7352 | if (!Visit(E->getSubExpr())) |
| 7353 | return false; |
| 7354 | return HandleDynamicCast(Info, cast<ExplicitCastExpr>(E), Result); |
| Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 7355 | } |
| 7356 | } |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7357 | }; |
| 7358 | } // end anonymous namespace |
| 7359 | |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7360 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
| Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 7361 | /// expressions which are not glvalues, in three cases: |
| Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 7362 | /// * function designators in C, and |
| 7363 | /// * "extern void" objects |
| Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 7364 | /// * @selector() expressions in Objective-C |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7365 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
| 7366 | bool InvalidBaseOK) { |
| Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 7367 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
| Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 7368 | E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E)); |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7369 | return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7370 | } |
| 7371 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7372 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
| David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 7373 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7374 | return Success(FD); |
| 7375 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7376 | return VisitVarDecl(E, VD); |
| Richard Smith | dca60b4 | 2016-08-12 00:39:32 +0000 | [diff] [blame] | 7377 | if (const BindingDecl *BD = dyn_cast<BindingDecl>(E->getDecl())) |
| Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 7378 | return Visit(BD->getBinding()); |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7379 | return Error(E); |
| 7380 | } |
| Richard Smith | 733237d | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 7381 | |
| Faisal Vali | 0528a31 | 2016-11-13 06:09:16 +0000 | [diff] [blame] | 7382 | |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7383 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 7384 | |
| 7385 | // If we are within a lambda's call operator, check whether the 'VD' referred |
| 7386 | // to within 'E' actually represents a lambda-capture that maps to a |
| 7387 | // data-member/field within the closure object, and if so, evaluate to the |
| 7388 | // field or what the field refers to. |
| Erik Pilkington | 1123291 | 2018-04-05 00:12:05 +0000 | [diff] [blame] | 7389 | if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) && |
| 7390 | isa<DeclRefExpr>(E) && |
| 7391 | cast<DeclRefExpr>(E)->refersToEnclosingVariableOrCapture()) { |
| 7392 | // We don't always have a complete capture-map when checking or inferring if |
| 7393 | // the function call operator meets the requirements of a constexpr function |
| 7394 | // - but we don't need to evaluate the captures to determine constexprness |
| 7395 | // (dcl.constexpr C++17). |
| 7396 | if (Info.checkingPotentialConstantExpression()) |
| 7397 | return false; |
| 7398 | |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 7399 | if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) { |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 7400 | // Start with 'Result' referring to the complete closure object... |
| 7401 | Result = *Info.CurrentCall->This; |
| 7402 | // ... then update it to refer to the field of the closure object |
| 7403 | // that represents the capture. |
| 7404 | if (!HandleLValueMember(Info, E, Result, FD)) |
| 7405 | return false; |
| 7406 | // And if the field is of reference type, update 'Result' to refer to what |
| 7407 | // the field refers to. |
| 7408 | if (FD->getType()->isReferenceType()) { |
| 7409 | APValue RVal; |
| 7410 | if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, |
| 7411 | RVal)) |
| 7412 | return false; |
| 7413 | Result.setFrom(Info.Ctx, RVal); |
| 7414 | } |
| 7415 | return true; |
| 7416 | } |
| 7417 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7418 | CallStackFrame *Frame = nullptr; |
| Faisal Vali | 0528a31 | 2016-11-13 06:09:16 +0000 | [diff] [blame] | 7419 | if (VD->hasLocalStorage() && Info.CurrentCall->Index > 1) { |
| 7420 | // Only if a local variable was declared in the function currently being |
| 7421 | // evaluated, do we expect to be able to find its value in the current |
| 7422 | // frame. (Otherwise it was likely declared in an enclosing context and |
| 7423 | // could either have a valid evaluatable value (for e.g. a constexpr |
| 7424 | // variable) or be ill-formed (and trigger an appropriate evaluation |
| 7425 | // diagnostic)). |
| 7426 | if (Info.CurrentCall->Callee && |
| 7427 | Info.CurrentCall->Callee->Equals(VD->getDeclContext())) { |
| 7428 | Frame = Info.CurrentCall; |
| 7429 | } |
| 7430 | } |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7431 | |
| Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 7432 | if (!VD->getType()->isReferenceType()) { |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7433 | if (Frame) { |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 7434 | Result.set({VD, Frame->Index, |
| 7435 | Info.CurrentCall->getCurrentTemporaryVersion(VD)}); |
| Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 7436 | return true; |
| 7437 | } |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7438 | return Success(VD); |
| Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 7439 | } |
| Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 7440 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7441 | APValue *V; |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 7442 | if (!evaluateVarDeclInit(Info, E, VD, Frame, V, nullptr)) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7443 | return false; |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 7444 | if (!V->hasValue()) { |
| 7445 | // FIXME: Is it possible for V to be indeterminate here? If so, we should |
| 7446 | // adjust the diagnostic to say that. |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 7447 | if (!Info.checkingPotentialConstantExpression()) |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 7448 | Info.FFDiag(E, diag::note_constexpr_use_uninit_reference); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 7449 | return false; |
| 7450 | } |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7451 | return Success(*V, E); |
| Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 7452 | } |
| 7453 | |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 7454 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 7455 | const MaterializeTemporaryExpr *E) { |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7456 | // Walk through the expression to find the materialized temporary itself. |
| 7457 | SmallVector<const Expr *, 2> CommaLHSs; |
| 7458 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 7459 | const Expr *Inner = E->GetTemporaryExpr()-> |
| 7460 | skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7461 | |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7462 | // If we passed any comma operators, evaluate their LHSs. |
| 7463 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
| 7464 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
| 7465 | return false; |
| 7466 | |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7467 | // A materialized temporary with static storage duration can appear within the |
| 7468 | // result of a constant expression evaluation, so we need to preserve its |
| 7469 | // value for use outside this evaluation. |
| 7470 | APValue *Value; |
| 7471 | if (E->getStorageDuration() == SD_Static) { |
| 7472 | Value = Info.Ctx.getMaterializedTemporaryValue(E, true); |
| Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 7473 | *Value = APValue(); |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7474 | Result.set(E); |
| 7475 | } else { |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 7476 | Value = &Info.CurrentCall->createTemporary( |
| 7477 | E, E->getType(), E->getStorageDuration() == SD_Automatic, Result); |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 7478 | } |
| 7479 | |
| Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 7480 | QualType Type = Inner->getType(); |
| 7481 | |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7482 | // Materialize the temporary itself. |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 7483 | if (!EvaluateInPlace(*Value, Info, Result, Inner)) { |
| Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 7484 | *Value = APValue(); |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7485 | return false; |
| Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 7486 | } |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7487 | |
| 7488 | // Adjust our lvalue to refer to the desired subobject. |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7489 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
| 7490 | --I; |
| 7491 | switch (Adjustments[I].Kind) { |
| 7492 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
| 7493 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
| 7494 | Type, Result)) |
| 7495 | return false; |
| 7496 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
| 7497 | break; |
| 7498 | |
| 7499 | case SubobjectAdjustment::FieldAdjustment: |
| 7500 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
| 7501 | return false; |
| 7502 | Type = Adjustments[I].Field->getType(); |
| 7503 | break; |
| 7504 | |
| 7505 | case SubobjectAdjustment::MemberPointerAdjustment: |
| 7506 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
| 7507 | Adjustments[I].Ptr.RHS)) |
| 7508 | return false; |
| 7509 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
| 7510 | break; |
| 7511 | } |
| 7512 | } |
| 7513 | |
| 7514 | return true; |
| Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 7515 | } |
| 7516 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7517 | bool |
| 7518 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
| Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 7519 | assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) && |
| 7520 | "lvalue compound literal in c++?"); |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7521 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 7522 | // 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] | 7523 | return Success(E); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7524 | } |
| 7525 | |
| Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 7526 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
| Richard Smith | a933030 | 2019-05-17 19:19:28 +0000 | [diff] [blame] | 7527 | TypeInfoLValue TypeInfo; |
| 7528 | |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 7529 | if (!E->isPotentiallyEvaluated()) { |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 7530 | if (E->isTypeOperand()) |
| 7531 | TypeInfo = TypeInfoLValue(E->getTypeOperand(Info.Ctx).getTypePtr()); |
| 7532 | else |
| 7533 | TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr()); |
| Richard Smith | a933030 | 2019-05-17 19:19:28 +0000 | [diff] [blame] | 7534 | } else { |
| 7535 | if (!Info.Ctx.getLangOpts().CPlusPlus2a) { |
| 7536 | Info.CCEDiag(E, diag::note_constexpr_typeid_polymorphic) |
| 7537 | << E->getExprOperand()->getType() |
| 7538 | << E->getExprOperand()->getSourceRange(); |
| 7539 | } |
| 7540 | |
| 7541 | if (!Visit(E->getExprOperand())) |
| 7542 | return false; |
| 7543 | |
| 7544 | Optional<DynamicType> DynType = |
| 7545 | ComputeDynamicType(Info, E, Result, AK_TypeId); |
| 7546 | if (!DynType) |
| 7547 | return false; |
| 7548 | |
| 7549 | TypeInfo = |
| 7550 | TypeInfoLValue(Info.Ctx.getRecordType(DynType->Type).getTypePtr()); |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 7551 | } |
| Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 7552 | |
| Richard Smith | a933030 | 2019-05-17 19:19:28 +0000 | [diff] [blame] | 7553 | return Success(APValue::LValueBase::getTypeInfo(TypeInfo, E->getType())); |
| Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 7554 | } |
| 7555 | |
| Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 7556 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 7557 | return Success(E); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7558 | } |
| Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 7559 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7560 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7561 | // Handle static data members. |
| 7562 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
| David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 7563 | VisitIgnoredBaseExpression(E->getBase()); |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7564 | return VisitVarDecl(E, VD); |
| 7565 | } |
| 7566 | |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 7567 | // Handle static member functions. |
| 7568 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 7569 | if (MD->isStatic()) { |
| David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 7570 | VisitIgnoredBaseExpression(E->getBase()); |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7571 | return Success(MD); |
| Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 7572 | } |
| 7573 | } |
| 7574 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7575 | // Handle non-static data members. |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7576 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7577 | } |
| 7578 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7579 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7580 | // FIXME: Deal with vectors as array subscript bases. |
| 7581 | if (E->getBase()->getType()->isVectorType()) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7582 | return Error(E); |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7583 | |
| Nick Lewycky | ad88868 | 2017-04-27 07:27:36 +0000 | [diff] [blame] | 7584 | bool Success = true; |
| 7585 | if (!evaluatePointer(E->getBase(), Result)) { |
| 7586 | if (!Info.noteFailure()) |
| 7587 | return false; |
| 7588 | Success = false; |
| 7589 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7590 | |
| Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7591 | APSInt Index; |
| 7592 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7593 | return false; |
| Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7594 | |
| Nick Lewycky | ad88868 | 2017-04-27 07:27:36 +0000 | [diff] [blame] | 7595 | return Success && |
| 7596 | HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index); |
| Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7597 | } |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7598 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7599 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7600 | return evaluatePointer(E->getSubExpr(), Result); |
| Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 7601 | } |
| 7602 | |
| Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 7603 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 7604 | if (!Visit(E->getSubExpr())) |
| 7605 | return false; |
| 7606 | // __real is a no-op on scalar lvalues. |
| 7607 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 7608 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 7609 | return true; |
| 7610 | } |
| 7611 | |
| 7612 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 7613 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 7614 | "lvalue __imag__ on scalar?"); |
| 7615 | if (!Visit(E->getSubExpr())) |
| 7616 | return false; |
| 7617 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 7618 | return true; |
| 7619 | } |
| 7620 | |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7621 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 7622 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7623 | return Error(UO); |
| 7624 | |
| 7625 | if (!this->Visit(UO->getSubExpr())) |
| 7626 | return false; |
| 7627 | |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7628 | return handleIncDec( |
| 7629 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7630 | UO->isIncrementOp(), nullptr); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7631 | } |
| 7632 | |
| 7633 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
| 7634 | const CompoundAssignOperator *CAO) { |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 7635 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7636 | return Error(CAO); |
| 7637 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7638 | APValue RHS; |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7639 | |
| 7640 | // The overall lvalue result is the result of evaluating the LHS. |
| 7641 | if (!this->Visit(CAO->getLHS())) { |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 7642 | if (Info.noteFailure()) |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7643 | Evaluate(RHS, this->Info, CAO->getRHS()); |
| 7644 | return false; |
| 7645 | } |
| 7646 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7647 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) |
| 7648 | return false; |
| 7649 | |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 7650 | return handleCompoundAssignment( |
| 7651 | this->Info, CAO, |
| 7652 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
| 7653 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7654 | } |
| 7655 | |
| 7656 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 7657 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7658 | return Error(E); |
| 7659 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7660 | APValue NewVal; |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7661 | |
| 7662 | if (!this->Visit(E->getLHS())) { |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 7663 | if (Info.noteFailure()) |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7664 | Evaluate(NewVal, this->Info, E->getRHS()); |
| 7665 | return false; |
| 7666 | } |
| 7667 | |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7668 | if (!Evaluate(NewVal, this->Info, E->getRHS())) |
| 7669 | return false; |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7670 | |
| Richard Smith | 31c69a3 | 2019-05-21 23:15:20 +0000 | [diff] [blame] | 7671 | if (Info.getLangOpts().CPlusPlus2a && |
| 7672 | !HandleUnionActiveMemberChange(Info, E->getLHS(), Result)) |
| 7673 | return false; |
| 7674 | |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 7675 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
| Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 7676 | NewVal); |
| 7677 | } |
| 7678 | |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7679 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7680 | // Pointer Evaluation |
| 7681 | //===----------------------------------------------------------------------===// |
| 7682 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7683 | /// Attempts to compute the number of bytes available at the pointer |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7684 | /// returned by a function with the alloc_size attribute. Returns true if we |
| 7685 | /// were successful. Places an unsigned number into `Result`. |
| 7686 | /// |
| 7687 | /// This expects the given CallExpr to be a call to a function with an |
| 7688 | /// alloc_size attribute. |
| 7689 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
| 7690 | const CallExpr *Call, |
| 7691 | llvm::APInt &Result) { |
| 7692 | const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call); |
| 7693 | |
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 7694 | assert(AllocSize && AllocSize->getElemSizeParam().isValid()); |
| 7695 | unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex(); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7696 | unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType()); |
| 7697 | if (Call->getNumArgs() <= SizeArgNo) |
| 7698 | return false; |
| 7699 | |
| 7700 | auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) { |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 7701 | Expr::EvalResult ExprResult; |
| 7702 | if (!E->EvaluateAsInt(ExprResult, Ctx, Expr::SE_AllowSideEffects)) |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7703 | return false; |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 7704 | Into = ExprResult.Val.getInt(); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7705 | if (Into.isNegative() || !Into.isIntN(BitsInSizeT)) |
| 7706 | return false; |
| 7707 | Into = Into.zextOrSelf(BitsInSizeT); |
| 7708 | return true; |
| 7709 | }; |
| 7710 | |
| 7711 | APSInt SizeOfElem; |
| 7712 | if (!EvaluateAsSizeT(Call->getArg(SizeArgNo), SizeOfElem)) |
| 7713 | return false; |
| 7714 | |
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 7715 | if (!AllocSize->getNumElemsParam().isValid()) { |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7716 | Result = std::move(SizeOfElem); |
| 7717 | return true; |
| 7718 | } |
| 7719 | |
| 7720 | APSInt NumberOfElems; |
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 7721 | unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex(); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7722 | if (!EvaluateAsSizeT(Call->getArg(NumArgNo), NumberOfElems)) |
| 7723 | return false; |
| 7724 | |
| 7725 | bool Overflow; |
| 7726 | llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow); |
| 7727 | if (Overflow) |
| 7728 | return false; |
| 7729 | |
| 7730 | Result = std::move(BytesAvailable); |
| 7731 | return true; |
| 7732 | } |
| 7733 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7734 | /// Convenience function. LVal's base must be a call to an alloc_size |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7735 | /// function. |
| 7736 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
| 7737 | const LValue &LVal, |
| 7738 | llvm::APInt &Result) { |
| 7739 | assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 7740 | "Can't get the size of a non alloc_size function"); |
| 7741 | const auto *Base = LVal.getLValueBase().get<const Expr *>(); |
| 7742 | const CallExpr *CE = tryUnwrapAllocSizeCall(Base); |
| 7743 | return getBytesReturnedByAllocSizeCall(Ctx, CE, Result); |
| 7744 | } |
| 7745 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7746 | /// Attempts to evaluate the given LValueBase as the result of a call to |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7747 | /// a function with the alloc_size attribute. If it was possible to do so, this |
| 7748 | /// function will return true, make Result's Base point to said function call, |
| 7749 | /// and mark Result's Base as invalid. |
| 7750 | static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base, |
| 7751 | LValue &Result) { |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7752 | if (Base.isNull()) |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7753 | return false; |
| 7754 | |
| 7755 | // Because we do no form of static analysis, we only support const variables. |
| 7756 | // |
| 7757 | // Additionally, we can't support parameters, nor can we support static |
| 7758 | // variables (in the latter case, use-before-assign isn't UB; in the former, |
| 7759 | // we have no clue what they'll be assigned to). |
| 7760 | const auto *VD = |
| 7761 | dyn_cast_or_null<VarDecl>(Base.dyn_cast<const ValueDecl *>()); |
| 7762 | if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified()) |
| 7763 | return false; |
| 7764 | |
| 7765 | const Expr *Init = VD->getAnyInitializer(); |
| 7766 | if (!Init) |
| 7767 | return false; |
| 7768 | |
| 7769 | const Expr *E = Init->IgnoreParens(); |
| 7770 | if (!tryUnwrapAllocSizeCall(E)) |
| 7771 | return false; |
| 7772 | |
| 7773 | // Store E instead of E unwrapped so that the type of the LValue's base is |
| 7774 | // what the user wanted. |
| 7775 | Result.setInvalid(E); |
| 7776 | |
| 7777 | QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType(); |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 7778 | Result.addUnsizedArray(Info, E, Pointee); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7779 | return true; |
| 7780 | } |
| 7781 | |
| Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 7782 | namespace { |
| Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7783 | class PointerExprEvaluator |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7784 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7785 | LValue &Result; |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7786 | bool InvalidBaseOK; |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7787 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7788 | bool Success(const Expr *E) { |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7789 | Result.set(E); |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7790 | return true; |
| 7791 | } |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7792 | |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7793 | bool evaluateLValue(const Expr *E, LValue &Result) { |
| 7794 | return EvaluateLValue(E, Result, Info, InvalidBaseOK); |
| 7795 | } |
| 7796 | |
| 7797 | bool evaluatePointer(const Expr *E, LValue &Result) { |
| 7798 | return EvaluatePointer(E, Result, Info, InvalidBaseOK); |
| 7799 | } |
| 7800 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7801 | bool visitNonBuiltinCallExpr(const CallExpr *E); |
| Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 7802 | public: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7803 | |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7804 | PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK) |
| 7805 | : ExprEvaluatorBaseTy(info), Result(Result), |
| 7806 | InvalidBaseOK(InvalidBaseOK) {} |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7807 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7808 | bool Success(const APValue &V, const Expr *E) { |
| 7809 | Result.setFrom(Info.Ctx, V); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7810 | return true; |
| 7811 | } |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7812 | bool ZeroInitialization(const Expr *E) { |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 7813 | Result.setNull(Info.Ctx, E->getType()); |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7814 | return true; |
| Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7815 | } |
| Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 7816 | |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7817 | bool VisitBinaryOperator(const BinaryOperator *E); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7818 | bool VisitCastExpr(const CastExpr* E); |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7819 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7820 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7821 | { return Success(E); } |
| Nick Lewycky | 19ae6dc | 2017-04-29 00:07:27 +0000 | [diff] [blame] | 7822 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
| Akira Hatanaka | 1488ee4 | 2019-03-08 04:45:37 +0000 | [diff] [blame] | 7823 | if (E->isExpressibleAsConstantInitializer()) |
| 7824 | return Success(E); |
| Nick Lewycky | 19ae6dc | 2017-04-29 00:07:27 +0000 | [diff] [blame] | 7825 | if (Info.noteFailure()) |
| 7826 | EvaluateIgnoredValue(Info, E->getSubExpr()); |
| 7827 | return Error(E); |
| 7828 | } |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7829 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7830 | { return Success(E); } |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7831 | bool VisitCallExpr(const CallExpr *E); |
| Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 7832 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7833 | bool VisitBlockExpr(const BlockExpr *E) { |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 7834 | if (!E->getBlockDecl()->hasCaptures()) |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7835 | return Success(E); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7836 | return Error(E); |
| Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 7837 | } |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7838 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7839 | // Can't look at 'this' when checking a potential constant expression. |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 7840 | if (Info.checkingPotentialConstantExpression()) |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7841 | return false; |
| Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 7842 | if (!Info.CurrentCall->This) { |
| 7843 | if (Info.getLangOpts().CPlusPlus11) |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 7844 | Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit(); |
| Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 7845 | else |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 7846 | Info.FFDiag(E); |
| Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 7847 | return false; |
| 7848 | } |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7849 | Result = *Info.CurrentCall->This; |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 7850 | // If we are inside a lambda's call operator, the 'this' expression refers |
| 7851 | // to the enclosing '*this' object (either by value or reference) which is |
| 7852 | // either copied into the closure object's field that represents the '*this' |
| 7853 | // or refers to '*this'. |
| 7854 | if (isLambdaCallOperator(Info.CurrentCall->Callee)) { |
| 7855 | // Update 'Result' to refer to the data member/field of the closure object |
| 7856 | // that represents the '*this' capture. |
| 7857 | if (!HandleLValueMember(Info, E, Result, |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7858 | Info.CurrentCall->LambdaThisCaptureField)) |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 7859 | return false; |
| 7860 | // If we captured '*this' by reference, replace the field with its referent. |
| 7861 | if (Info.CurrentCall->LambdaThisCaptureField->getType() |
| 7862 | ->isPointerType()) { |
| 7863 | APValue RVal; |
| 7864 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Result, |
| 7865 | RVal)) |
| 7866 | return false; |
| 7867 | |
| 7868 | Result.setFrom(Info.Ctx, RVal); |
| 7869 | } |
| 7870 | } |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7871 | return true; |
| 7872 | } |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 7873 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 7874 | bool VisitCXXNewExpr(const CXXNewExpr *E); |
| 7875 | |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 7876 | bool VisitSourceLocExpr(const SourceLocExpr *E) { |
| 7877 | assert(E->isStringType() && "SourceLocExpr isn't a pointer type?"); |
| 7878 | APValue LValResult = E->EvaluateInContext( |
| 7879 | Info.Ctx, Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
| 7880 | Result.setFrom(Info.Ctx, LValResult); |
| 7881 | return true; |
| 7882 | } |
| 7883 | |
| Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 7884 | // FIXME: Missing: @protocol, @selector |
| Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7885 | }; |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7886 | } // end anonymous namespace |
| Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7887 | |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7888 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info, |
| 7889 | bool InvalidBaseOK) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7890 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7891 | return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7892 | } |
| 7893 | |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7894 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7895 | if (E->getOpcode() != BO_Add && |
| 7896 | E->getOpcode() != BO_Sub) |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7897 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7898 | |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7899 | const Expr *PExp = E->getLHS(); |
| 7900 | const Expr *IExp = E->getRHS(); |
| 7901 | if (IExp->getType()->isPointerType()) |
| 7902 | std::swap(PExp, IExp); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7903 | |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7904 | bool EvalPtrOK = evaluatePointer(PExp, Result); |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 7905 | if (!EvalPtrOK && !Info.noteFailure()) |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7906 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7907 | |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7908 | llvm::APSInt Offset; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7909 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7910 | return false; |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 7911 | |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 7912 | if (E->getOpcode() == BO_Sub) |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 7913 | negateAsSigned(Offset); |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7914 | |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 7915 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 7916 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset); |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7917 | } |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7918 | |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7919 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7920 | return evaluateLValue(E->getSubExpr(), Result); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 7921 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7922 | |
| Richard Smith | 81dfef9 | 2018-07-11 00:29:05 +0000 | [diff] [blame] | 7923 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 7924 | const Expr *SubExpr = E->getSubExpr(); |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7925 | |
| Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 7926 | switch (E->getCastKind()) { |
| 7927 | default: |
| 7928 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7929 | case CK_BitCast: |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7930 | case CK_CPointerToObjCPointerCast: |
| 7931 | case CK_BlockPointerToObjCPointerCast: |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7932 | case CK_AnyPointerToBlockPointerCast: |
| Anastasia Stulova | 5d8ad8a | 2014-11-26 15:36:41 +0000 | [diff] [blame] | 7933 | case CK_AddressSpaceConversion: |
| Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 7934 | if (!Visit(SubExpr)) |
| 7935 | return false; |
| Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 7936 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 7937 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 7938 | // also static_casts, but we disallow them as a resolution to DR1312. |
| Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 7939 | if (!E->getType()->isVoidPointerType()) { |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 7940 | if (!Result.InvalidBase && !Result.Designator.Invalid && |
| 7941 | !Result.IsNullPtr && |
| 7942 | Info.Ctx.hasSameUnqualifiedType(Result.Designator.getType(Info.Ctx), |
| 7943 | E->getType()->getPointeeType()) && |
| 7944 | Info.getStdAllocatorCaller("allocate")) { |
| 7945 | // Inside a call to std::allocator::allocate and friends, we permit |
| 7946 | // casting from void* back to cv1 T* for a pointer that points to a |
| 7947 | // cv2 T. |
| 7948 | } else { |
| 7949 | Result.Designator.setInvalid(); |
| 7950 | if (SubExpr->getType()->isVoidPointerType()) |
| 7951 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 7952 | << 3 << SubExpr->getType(); |
| 7953 | else |
| 7954 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 7955 | } |
| Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 7956 | } |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7957 | if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr) |
| 7958 | ZeroInitialization(E); |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 7959 | return true; |
| Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 7960 | |
| Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 7961 | case CK_DerivedToBase: |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7962 | case CK_UncheckedDerivedToBase: |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7963 | if (!evaluatePointer(E->getSubExpr(), Result)) |
| Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 7964 | return false; |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7965 | if (!Result.Base && Result.Offset.isZero()) |
| 7966 | return true; |
| Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 7967 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7968 | // 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] | 7969 | // the derived class to the base class. |
| Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 7970 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
| 7971 | castAs<PointerType>()->getPointeeType(), |
| 7972 | Result); |
| Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 7973 | |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7974 | case CK_BaseToDerived: |
| 7975 | if (!Visit(E->getSubExpr())) |
| 7976 | return false; |
| 7977 | if (!Result.Base && Result.Offset.isZero()) |
| 7978 | return true; |
| 7979 | return HandleBaseToDerivedCast(Info, E, Result); |
| 7980 | |
| Richard Smith | 7bd54ab | 2019-05-15 20:22:21 +0000 | [diff] [blame] | 7981 | case CK_Dynamic: |
| 7982 | if (!Visit(E->getSubExpr())) |
| 7983 | return false; |
| 7984 | return HandleDynamicCast(Info, cast<ExplicitCastExpr>(E), Result); |
| 7985 | |
| Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 7986 | case CK_NullToPointer: |
| Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 7987 | VisitIgnoredValue(E->getSubExpr()); |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7988 | return ZeroInitialization(E); |
| John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7989 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7990 | case CK_IntegralToPointer: { |
| Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 7991 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 7992 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7993 | APValue Value; |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7994 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
| Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 7995 | break; |
| Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 7996 | |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 7997 | if (Value.isInt()) { |
| Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 7998 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 7999 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 8000 | Result.Base = (Expr*)nullptr; |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 8001 | Result.InvalidBase = false; |
| Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 8002 | Result.Offset = CharUnits::fromQuantity(N); |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 8003 | Result.Designator.setInvalid(); |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 8004 | Result.IsNullPtr = false; |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8005 | return true; |
| 8006 | } else { |
| 8007 | // Cast is of an lvalue, no need to change value. |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8008 | Result.setFrom(Info.Ctx, Value); |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8009 | return true; |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 8010 | } |
| 8011 | } |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 8012 | |
| 8013 | case CK_ArrayToPointerDecay: { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8014 | if (SubExpr->isGLValue()) { |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 8015 | if (!evaluateLValue(SubExpr, Result)) |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8016 | return false; |
| 8017 | } else { |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 8018 | APValue &Value = Info.CurrentCall->createTemporary( |
| 8019 | SubExpr, SubExpr->getType(), false, Result); |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 8020 | if (!EvaluateInPlace(Value, Info, Result, SubExpr)) |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8021 | return false; |
| 8022 | } |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 8023 | // The result is a pointer to the first element of the array. |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 8024 | auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType()); |
| 8025 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 8026 | Result.addArray(Info, E, CAT); |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8027 | else |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 8028 | Result.addUnsizedArray(Info, E, AT->getElementType()); |
| Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 8029 | return true; |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 8030 | } |
| Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 8031 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8032 | case CK_FunctionToPointerDecay: |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 8033 | return evaluateLValue(SubExpr, Result); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8034 | |
| 8035 | case CK_LValueToRValue: { |
| 8036 | LValue LVal; |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 8037 | if (!evaluateLValue(E->getSubExpr(), LVal)) |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8038 | return false; |
| 8039 | |
| 8040 | APValue RVal; |
| 8041 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
| 8042 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
| 8043 | LVal, RVal)) |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 8044 | return InvalidBaseOK && |
| 8045 | evaluateLValueAsAllocSize(Info, LVal.Base, Result); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8046 | return Success(RVal, E); |
| 8047 | } |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 8048 | } |
| 8049 | |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8050 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8051 | } |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 8052 | |
| Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 8053 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T, |
| 8054 | UnaryExprOrTypeTrait ExprKind) { |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8055 | // C++ [expr.alignof]p3: |
| 8056 | // When alignof is applied to a reference type, the result is the |
| 8057 | // alignment of the referenced type. |
| 8058 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 8059 | T = Ref->getPointeeType(); |
| 8060 | |
| Roger Ferrer Ibanez | 3fa38a1 | 2017-03-08 14:00:44 +0000 | [diff] [blame] | 8061 | if (T.getQualifiers().hasUnaligned()) |
| 8062 | return CharUnits::One(); |
| Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 8063 | |
| 8064 | const bool AlignOfReturnsPreferred = |
| 8065 | Info.Ctx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7; |
| 8066 | |
| 8067 | // __alignof is defined to return the preferred alignment. |
| 8068 | // Before 8, clang returned the preferred alignment for alignof and _Alignof |
| 8069 | // as well. |
| 8070 | if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) |
| 8071 | return Info.Ctx.toCharUnitsFromBits( |
| 8072 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
| 8073 | // alignof and _Alignof are defined to return the ABI alignment. |
| 8074 | else if (ExprKind == UETT_AlignOf) |
| 8075 | return Info.Ctx.getTypeAlignInChars(T.getTypePtr()); |
| 8076 | else |
| 8077 | llvm_unreachable("GetAlignOfType on a non-alignment ExprKind"); |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8078 | } |
| 8079 | |
| Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 8080 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E, |
| 8081 | UnaryExprOrTypeTrait ExprKind) { |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8082 | E = E->IgnoreParens(); |
| 8083 | |
| 8084 | // The kinds of expressions that we have special-case logic here for |
| 8085 | // should be kept up to date with the special checks for those |
| 8086 | // expressions in Sema. |
| 8087 | |
| 8088 | // alignof decl is always accepted, even if it doesn't make sense: we default |
| 8089 | // to 1 in those cases. |
| 8090 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 8091 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 8092 | /*RefAsPointee*/true); |
| 8093 | |
| 8094 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 8095 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 8096 | /*RefAsPointee*/true); |
| 8097 | |
| Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 8098 | return GetAlignOfType(Info, E->getType(), ExprKind); |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8099 | } |
| 8100 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8101 | // To be clear: this happily visits unsupported builtins. Better name welcomed. |
| 8102 | bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) { |
| 8103 | if (ExprEvaluatorBaseTy::VisitCallExpr(E)) |
| 8104 | return true; |
| 8105 | |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 8106 | if (!(InvalidBaseOK && getAllocSizeAttr(E))) |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8107 | return false; |
| 8108 | |
| 8109 | Result.setInvalid(E); |
| 8110 | QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType(); |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 8111 | Result.addUnsizedArray(Info, E, PointeeTy); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8112 | return true; |
| 8113 | } |
| 8114 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 8115 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8116 | if (IsStringLiteralCall(E)) |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 8117 | return Success(E); |
| Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 8118 | |
| Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 8119 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
| 8120 | return VisitBuiltinCallExpr(E, BuiltinOp); |
| 8121 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8122 | return visitNonBuiltinCallExpr(E); |
| Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 8123 | } |
| 8124 | |
| 8125 | bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
| 8126 | unsigned BuiltinOp) { |
| 8127 | switch (BuiltinOp) { |
| Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 8128 | case Builtin::BI__builtin_addressof: |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 8129 | return evaluateLValue(E->getArg(0), Result); |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8130 | case Builtin::BI__builtin_assume_aligned: { |
| 8131 | // We need to be very careful here because: if the pointer does not have the |
| 8132 | // asserted alignment, then the behavior is undefined, and undefined |
| 8133 | // behavior is non-constant. |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 8134 | if (!evaluatePointer(E->getArg(0), Result)) |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8135 | return false; |
| Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 8136 | |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8137 | LValue OffsetResult(Result); |
| 8138 | APSInt Alignment; |
| 8139 | if (!EvaluateInteger(E->getArg(1), Alignment, Info)) |
| 8140 | return false; |
| Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 8141 | CharUnits Align = CharUnits::fromQuantity(Alignment.getZExtValue()); |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8142 | |
| 8143 | if (E->getNumArgs() > 2) { |
| 8144 | APSInt Offset; |
| 8145 | if (!EvaluateInteger(E->getArg(2), Offset, Info)) |
| 8146 | return false; |
| 8147 | |
| Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 8148 | int64_t AdditionalOffset = -Offset.getZExtValue(); |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8149 | OffsetResult.Offset += CharUnits::fromQuantity(AdditionalOffset); |
| 8150 | } |
| 8151 | |
| 8152 | // If there is a base object, then it must have the correct alignment. |
| 8153 | if (OffsetResult.Base) { |
| 8154 | CharUnits BaseAlignment; |
| 8155 | if (const ValueDecl *VD = |
| 8156 | OffsetResult.Base.dyn_cast<const ValueDecl*>()) { |
| 8157 | BaseAlignment = Info.Ctx.getDeclAlign(VD); |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 8158 | } else if (const Expr *E = OffsetResult.Base.dyn_cast<const Expr *>()) { |
| 8159 | BaseAlignment = GetAlignOfExpr(Info, E, UETT_AlignOf); |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8160 | } else { |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 8161 | BaseAlignment = GetAlignOfType( |
| 8162 | Info, OffsetResult.Base.getTypeInfoType(), UETT_AlignOf); |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8163 | } |
| 8164 | |
| 8165 | if (BaseAlignment < Align) { |
| 8166 | Result.Designator.setInvalid(); |
| Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 8167 | // FIXME: Add support to Diagnostic for long / long long. |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8168 | CCEDiag(E->getArg(0), |
| 8169 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
| Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 8170 | << (unsigned)BaseAlignment.getQuantity() |
| 8171 | << (unsigned)Align.getQuantity(); |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8172 | return false; |
| 8173 | } |
| 8174 | } |
| 8175 | |
| 8176 | // The offset must also have the correct alignment. |
| Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8177 | if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) { |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8178 | Result.Designator.setInvalid(); |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8179 | |
| Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 8180 | (OffsetResult.Base |
| 8181 | ? CCEDiag(E->getArg(0), |
| 8182 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
| 8183 | : CCEDiag(E->getArg(0), |
| 8184 | diag::note_constexpr_baa_value_insufficient_alignment)) |
| 8185 | << (int)OffsetResult.Offset.getQuantity() |
| 8186 | << (unsigned)Align.getQuantity(); |
| Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8187 | return false; |
| 8188 | } |
| 8189 | |
| 8190 | return true; |
| 8191 | } |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 8192 | case Builtin::BI__builtin_operator_new: |
| 8193 | return HandleOperatorNewCall(Info, E, Result); |
| Eric Fiselier | 2618750 | 2018-12-14 21:11:28 +0000 | [diff] [blame] | 8194 | case Builtin::BI__builtin_launder: |
| 8195 | return evaluatePointer(E->getArg(0), Result); |
| Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 8196 | case Builtin::BIstrchr: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8197 | case Builtin::BIwcschr: |
| Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 8198 | case Builtin::BImemchr: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8199 | case Builtin::BIwmemchr: |
| Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 8200 | if (Info.getLangOpts().CPlusPlus11) |
| 8201 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| 8202 | << /*isConstexpr*/0 << /*isConstructor*/0 |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8203 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
| Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 8204 | else |
| 8205 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8206 | LLVM_FALLTHROUGH; |
| Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 8207 | case Builtin::BI__builtin_strchr: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8208 | case Builtin::BI__builtin_wcschr: |
| 8209 | case Builtin::BI__builtin_memchr: |
| Richard Smith | 5e29dd3 | 2017-01-20 00:45:35 +0000 | [diff] [blame] | 8210 | case Builtin::BI__builtin_char_memchr: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8211 | case Builtin::BI__builtin_wmemchr: { |
| Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 8212 | if (!Visit(E->getArg(0))) |
| 8213 | return false; |
| 8214 | APSInt Desired; |
| 8215 | if (!EvaluateInteger(E->getArg(1), Desired, Info)) |
| 8216 | return false; |
| 8217 | uint64_t MaxLength = uint64_t(-1); |
| 8218 | if (BuiltinOp != Builtin::BIstrchr && |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8219 | BuiltinOp != Builtin::BIwcschr && |
| 8220 | BuiltinOp != Builtin::BI__builtin_strchr && |
| 8221 | BuiltinOp != Builtin::BI__builtin_wcschr) { |
| Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 8222 | APSInt N; |
| 8223 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
| 8224 | return false; |
| 8225 | MaxLength = N.getExtValue(); |
| 8226 | } |
| Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 8227 | // We cannot find the value if there are no candidates to match against. |
| 8228 | if (MaxLength == 0u) |
| 8229 | return ZeroInitialization(E); |
| 8230 | if (!Result.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
| 8231 | Result.Designator.Invalid) |
| 8232 | return false; |
| 8233 | QualType CharTy = Result.Designator.getType(Info.Ctx); |
| 8234 | bool IsRawByte = BuiltinOp == Builtin::BImemchr || |
| 8235 | BuiltinOp == Builtin::BI__builtin_memchr; |
| 8236 | assert(IsRawByte || |
| 8237 | Info.Ctx.hasSameUnqualifiedType( |
| 8238 | CharTy, E->getArg(0)->getType()->getPointeeType())); |
| 8239 | // Pointers to const void may point to objects of incomplete type. |
| 8240 | if (IsRawByte && CharTy->isIncompleteType()) { |
| 8241 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy; |
| 8242 | return false; |
| 8243 | } |
| 8244 | // Give up on byte-oriented matching against multibyte elements. |
| 8245 | // FIXME: We can compare the bytes in the correct order. |
| 8246 | if (IsRawByte && Info.Ctx.getTypeSizeInChars(CharTy) != CharUnits::One()) |
| 8247 | return false; |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8248 | // Figure out what value we're actually looking for (after converting to |
| 8249 | // the corresponding unsigned type if necessary). |
| 8250 | uint64_t DesiredVal; |
| 8251 | bool StopAtNull = false; |
| 8252 | switch (BuiltinOp) { |
| 8253 | case Builtin::BIstrchr: |
| 8254 | case Builtin::BI__builtin_strchr: |
| 8255 | // strchr compares directly to the passed integer, and therefore |
| 8256 | // always fails if given an int that is not a char. |
| 8257 | if (!APSInt::isSameValue(HandleIntToIntCast(Info, E, CharTy, |
| 8258 | E->getArg(1)->getType(), |
| 8259 | Desired), |
| 8260 | Desired)) |
| 8261 | return ZeroInitialization(E); |
| 8262 | StopAtNull = true; |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8263 | LLVM_FALLTHROUGH; |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8264 | case Builtin::BImemchr: |
| 8265 | case Builtin::BI__builtin_memchr: |
| Richard Smith | 5e29dd3 | 2017-01-20 00:45:35 +0000 | [diff] [blame] | 8266 | case Builtin::BI__builtin_char_memchr: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8267 | // memchr compares by converting both sides to unsigned char. That's also |
| 8268 | // correct for strchr if we get this far (to cope with plain char being |
| 8269 | // unsigned in the strchr case). |
| 8270 | DesiredVal = Desired.trunc(Info.Ctx.getCharWidth()).getZExtValue(); |
| 8271 | break; |
| Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 8272 | |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8273 | case Builtin::BIwcschr: |
| 8274 | case Builtin::BI__builtin_wcschr: |
| 8275 | StopAtNull = true; |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8276 | LLVM_FALLTHROUGH; |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8277 | case Builtin::BIwmemchr: |
| 8278 | case Builtin::BI__builtin_wmemchr: |
| 8279 | // wcschr and wmemchr are given a wchar_t to look for. Just use it. |
| 8280 | DesiredVal = Desired.getZExtValue(); |
| 8281 | break; |
| 8282 | } |
| Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 8283 | |
| 8284 | for (; MaxLength; --MaxLength) { |
| 8285 | APValue Char; |
| 8286 | if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) || |
| 8287 | !Char.isInt()) |
| 8288 | return false; |
| 8289 | if (Char.getInt().getZExtValue() == DesiredVal) |
| 8290 | return true; |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8291 | if (StopAtNull && !Char.getInt()) |
| Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 8292 | break; |
| 8293 | if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1)) |
| 8294 | return false; |
| 8295 | } |
| 8296 | // Not found: return nullptr. |
| 8297 | return ZeroInitialization(E); |
| 8298 | } |
| 8299 | |
| Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 8300 | case Builtin::BImemcpy: |
| 8301 | case Builtin::BImemmove: |
| 8302 | case Builtin::BIwmemcpy: |
| 8303 | case Builtin::BIwmemmove: |
| 8304 | if (Info.getLangOpts().CPlusPlus11) |
| 8305 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| 8306 | << /*isConstexpr*/0 << /*isConstructor*/0 |
| 8307 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
| 8308 | else |
| 8309 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| 8310 | LLVM_FALLTHROUGH; |
| 8311 | case Builtin::BI__builtin_memcpy: |
| 8312 | case Builtin::BI__builtin_memmove: |
| 8313 | case Builtin::BI__builtin_wmemcpy: |
| 8314 | case Builtin::BI__builtin_wmemmove: { |
| 8315 | bool WChar = BuiltinOp == Builtin::BIwmemcpy || |
| 8316 | BuiltinOp == Builtin::BIwmemmove || |
| 8317 | BuiltinOp == Builtin::BI__builtin_wmemcpy || |
| 8318 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
| 8319 | bool Move = BuiltinOp == Builtin::BImemmove || |
| 8320 | BuiltinOp == Builtin::BIwmemmove || |
| 8321 | BuiltinOp == Builtin::BI__builtin_memmove || |
| 8322 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
| 8323 | |
| 8324 | // The result of mem* is the first argument. |
| Richard Smith | 128719c | 2018-09-13 22:47:33 +0000 | [diff] [blame] | 8325 | if (!Visit(E->getArg(0))) |
| Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 8326 | return false; |
| 8327 | LValue Dest = Result; |
| 8328 | |
| 8329 | LValue Src; |
| Richard Smith | 128719c | 2018-09-13 22:47:33 +0000 | [diff] [blame] | 8330 | if (!EvaluatePointer(E->getArg(1), Src, Info)) |
| Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 8331 | return false; |
| 8332 | |
| 8333 | APSInt N; |
| 8334 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
| 8335 | return false; |
| 8336 | assert(!N.isSigned() && "memcpy and friends take an unsigned size"); |
| 8337 | |
| 8338 | // If the size is zero, we treat this as always being a valid no-op. |
| 8339 | // (Even if one of the src and dest pointers is null.) |
| 8340 | if (!N) |
| 8341 | return true; |
| 8342 | |
| Richard Smith | 128719c | 2018-09-13 22:47:33 +0000 | [diff] [blame] | 8343 | // Otherwise, if either of the operands is null, we can't proceed. Don't |
| 8344 | // try to determine the type of the copied objects, because there aren't |
| 8345 | // any. |
| 8346 | if (!Src.Base || !Dest.Base) { |
| 8347 | APValue Val; |
| 8348 | (!Src.Base ? Src : Dest).moveInto(Val); |
| 8349 | Info.FFDiag(E, diag::note_constexpr_memcpy_null) |
| 8350 | << Move << WChar << !!Src.Base |
| 8351 | << Val.getAsString(Info.Ctx, E->getArg(0)->getType()); |
| 8352 | return false; |
| 8353 | } |
| 8354 | if (Src.Designator.Invalid || Dest.Designator.Invalid) |
| 8355 | return false; |
| 8356 | |
| Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 8357 | // We require that Src and Dest are both pointers to arrays of |
| 8358 | // trivially-copyable type. (For the wide version, the designator will be |
| 8359 | // invalid if the designated object is not a wchar_t.) |
| 8360 | QualType T = Dest.Designator.getType(Info.Ctx); |
| 8361 | QualType SrcT = Src.Designator.getType(Info.Ctx); |
| 8362 | if (!Info.Ctx.hasSameUnqualifiedType(T, SrcT)) { |
| 8363 | Info.FFDiag(E, diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T; |
| 8364 | return false; |
| 8365 | } |
| Petr Pavlu | ed083f2 | 2018-10-04 09:25:44 +0000 | [diff] [blame] | 8366 | if (T->isIncompleteType()) { |
| 8367 | Info.FFDiag(E, diag::note_constexpr_memcpy_incomplete_type) << Move << T; |
| 8368 | return false; |
| 8369 | } |
| Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 8370 | if (!T.isTriviallyCopyableType(Info.Ctx)) { |
| 8371 | Info.FFDiag(E, diag::note_constexpr_memcpy_nontrivial) << Move << T; |
| 8372 | return false; |
| 8373 | } |
| 8374 | |
| 8375 | // Figure out how many T's we're copying. |
| 8376 | uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity(); |
| 8377 | if (!WChar) { |
| 8378 | uint64_t Remainder; |
| 8379 | llvm::APInt OrigN = N; |
| 8380 | llvm::APInt::udivrem(OrigN, TSize, N, Remainder); |
| 8381 | if (Remainder) { |
| 8382 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
| 8383 | << Move << WChar << 0 << T << OrigN.toString(10, /*Signed*/false) |
| 8384 | << (unsigned)TSize; |
| 8385 | return false; |
| 8386 | } |
| 8387 | } |
| 8388 | |
| 8389 | // Check that the copying will remain within the arrays, just so that we |
| 8390 | // can give a more meaningful diagnostic. This implicitly also checks that |
| 8391 | // N fits into 64 bits. |
| 8392 | uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second; |
| 8393 | uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second; |
| 8394 | if (N.ugt(RemainingSrcSize) || N.ugt(RemainingDestSize)) { |
| 8395 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
| 8396 | << Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T |
| 8397 | << N.toString(10, /*Signed*/false); |
| 8398 | return false; |
| 8399 | } |
| 8400 | uint64_t NElems = N.getZExtValue(); |
| 8401 | uint64_t NBytes = NElems * TSize; |
| 8402 | |
| 8403 | // Check for overlap. |
| 8404 | int Direction = 1; |
| 8405 | if (HasSameBase(Src, Dest)) { |
| 8406 | uint64_t SrcOffset = Src.getLValueOffset().getQuantity(); |
| 8407 | uint64_t DestOffset = Dest.getLValueOffset().getQuantity(); |
| 8408 | if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) { |
| 8409 | // Dest is inside the source region. |
| 8410 | if (!Move) { |
| 8411 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
| 8412 | return false; |
| 8413 | } |
| 8414 | // For memmove and friends, copy backwards. |
| 8415 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, NElems - 1) || |
| 8416 | !HandleLValueArrayAdjustment(Info, E, Dest, T, NElems - 1)) |
| 8417 | return false; |
| 8418 | Direction = -1; |
| 8419 | } else if (!Move && SrcOffset >= DestOffset && |
| 8420 | SrcOffset - DestOffset < NBytes) { |
| 8421 | // Src is inside the destination region for memcpy: invalid. |
| 8422 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
| 8423 | return false; |
| 8424 | } |
| 8425 | } |
| 8426 | |
| 8427 | while (true) { |
| 8428 | APValue Val; |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 8429 | // FIXME: Set WantObjectRepresentation to true if we're copying a |
| 8430 | // char-like type? |
| Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 8431 | if (!handleLValueToRValueConversion(Info, E, T, Src, Val) || |
| 8432 | !handleAssignment(Info, E, Dest, T, Val)) |
| 8433 | return false; |
| 8434 | // Do not iterate past the last element; if we're copying backwards, that |
| 8435 | // might take us off the start of the array. |
| 8436 | if (--NElems == 0) |
| 8437 | return true; |
| 8438 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, Direction) || |
| 8439 | !HandleLValueArrayAdjustment(Info, E, Dest, T, Direction)) |
| 8440 | return false; |
| 8441 | } |
| 8442 | } |
| 8443 | |
| Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 8444 | default: |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 8445 | break; |
| Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 8446 | } |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 8447 | |
| 8448 | return visitNonBuiltinCallExpr(E); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 8449 | } |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 8450 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 8451 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
| 8452 | APValue &Result, const InitListExpr *ILE, |
| 8453 | QualType AllocType); |
| 8454 | |
| 8455 | bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) { |
| Richard Smith | 62a9556 | 2019-09-27 01:26:49 +0000 | [diff] [blame] | 8456 | if (!Info.getLangOpts().CPlusPlus2a) |
| 8457 | Info.CCEDiag(E, diag::note_constexpr_new); |
| 8458 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 8459 | // We cannot speculatively evaluate a delete expression. |
| 8460 | if (Info.SpeculativeEvaluationDepth) |
| 8461 | return false; |
| 8462 | |
| 8463 | FunctionDecl *OperatorNew = E->getOperatorNew(); |
| Richard Smith | b542602 | 2019-10-03 00:39:35 +0000 | [diff] [blame] | 8464 | |
| 8465 | bool IsNothrow = false; |
| 8466 | bool IsPlacement = false; |
| 8467 | if (OperatorNew->isReservedGlobalPlacementOperator() && |
| 8468 | Info.CurrentCall->isStdFunction() && !E->isArray()) { |
| 8469 | // FIXME Support array placement new. |
| 8470 | assert(E->getNumPlacementArgs() == 1); |
| 8471 | if (!EvaluatePointer(E->getPlacementArg(0), Result, Info)) |
| 8472 | return false; |
| 8473 | if (Result.Designator.Invalid) |
| 8474 | return false; |
| 8475 | IsPlacement = true; |
| 8476 | } else if (!OperatorNew->isReplaceableGlobalAllocationFunction()) { |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 8477 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
| 8478 | << isa<CXXMethodDecl>(OperatorNew) << OperatorNew; |
| 8479 | return false; |
| Richard Smith | b542602 | 2019-10-03 00:39:35 +0000 | [diff] [blame] | 8480 | } else if (E->getNumPlacementArgs()) { |
| Richard Smith | 62a9556 | 2019-09-27 01:26:49 +0000 | [diff] [blame] | 8481 | // The only new-placement list we support is of the form (std::nothrow). |
| 8482 | // |
| 8483 | // FIXME: There is no restriction on this, but it's not clear that any |
| 8484 | // other form makes any sense. We get here for cases such as: |
| 8485 | // |
| 8486 | // new (std::align_val_t{N}) X(int) |
| 8487 | // |
| 8488 | // (which should presumably be valid only if N is a multiple of |
| 8489 | // alignof(int), and in any case can't be deallocated unless N is |
| 8490 | // alignof(X) and X has new-extended alignment). |
| 8491 | if (E->getNumPlacementArgs() != 1 || |
| 8492 | !E->getPlacementArg(0)->getType()->isNothrowT()) |
| 8493 | return Error(E, diag::note_constexpr_new_placement); |
| 8494 | |
| 8495 | LValue Nothrow; |
| 8496 | if (!EvaluateLValue(E->getPlacementArg(0), Nothrow, Info)) |
| 8497 | return false; |
| 8498 | IsNothrow = true; |
| 8499 | } |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 8500 | |
| 8501 | const Expr *Init = E->getInitializer(); |
| 8502 | const InitListExpr *ResizedArrayILE = nullptr; |
| 8503 | |
| 8504 | QualType AllocType = E->getAllocatedType(); |
| 8505 | if (Optional<const Expr*> ArraySize = E->getArraySize()) { |
| 8506 | const Expr *Stripped = *ArraySize; |
| 8507 | for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped); |
| 8508 | Stripped = ICE->getSubExpr()) |
| 8509 | if (ICE->getCastKind() != CK_NoOp && |
| 8510 | ICE->getCastKind() != CK_IntegralCast) |
| 8511 | break; |
| 8512 | |
| 8513 | llvm::APSInt ArrayBound; |
| 8514 | if (!EvaluateInteger(Stripped, ArrayBound, Info)) |
| 8515 | return false; |
| 8516 | |
| 8517 | // C++ [expr.new]p9: |
| 8518 | // The expression is erroneous if: |
| 8519 | // -- [...] its value before converting to size_t [or] applying the |
| 8520 | // second standard conversion sequence is less than zero |
| 8521 | if (ArrayBound.isSigned() && ArrayBound.isNegative()) { |
| Richard Smith | 62a9556 | 2019-09-27 01:26:49 +0000 | [diff] [blame] | 8522 | if (IsNothrow) |
| 8523 | return ZeroInitialization(E); |
| 8524 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 8525 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_negative) |
| 8526 | << ArrayBound << (*ArraySize)->getSourceRange(); |
| 8527 | return false; |
| 8528 | } |
| 8529 | |
| 8530 | // -- its value is such that the size of the allocated object would |
| 8531 | // exceed the implementation-defined limit |
| 8532 | if (ConstantArrayType::getNumAddressingBits(Info.Ctx, AllocType, |
| 8533 | ArrayBound) > |
| 8534 | ConstantArrayType::getMaxSizeBits(Info.Ctx)) { |
| Richard Smith | 62a9556 | 2019-09-27 01:26:49 +0000 | [diff] [blame] | 8535 | if (IsNothrow) |
| 8536 | return ZeroInitialization(E); |
| 8537 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 8538 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_large) |
| 8539 | << ArrayBound << (*ArraySize)->getSourceRange(); |
| 8540 | return false; |
| 8541 | } |
| 8542 | |
| 8543 | // -- the new-initializer is a braced-init-list and the number of |
| 8544 | // array elements for which initializers are provided [...] |
| 8545 | // exceeds the number of elements to initialize |
| 8546 | if (Init) { |
| 8547 | auto *CAT = Info.Ctx.getAsConstantArrayType(Init->getType()); |
| 8548 | assert(CAT && "unexpected type for array initializer"); |
| 8549 | |
| 8550 | unsigned Bits = |
| 8551 | std::max(CAT->getSize().getBitWidth(), ArrayBound.getBitWidth()); |
| 8552 | llvm::APInt InitBound = CAT->getSize().zextOrSelf(Bits); |
| 8553 | llvm::APInt AllocBound = ArrayBound.zextOrSelf(Bits); |
| 8554 | if (InitBound.ugt(AllocBound)) { |
| Richard Smith | 62a9556 | 2019-09-27 01:26:49 +0000 | [diff] [blame] | 8555 | if (IsNothrow) |
| 8556 | return ZeroInitialization(E); |
| 8557 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 8558 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_small) |
| 8559 | << AllocBound.toString(10, /*Signed=*/false) |
| 8560 | << InitBound.toString(10, /*Signed=*/false) |
| 8561 | << (*ArraySize)->getSourceRange(); |
| 8562 | return false; |
| 8563 | } |
| 8564 | |
| 8565 | // If the sizes differ, we must have an initializer list, and we need |
| 8566 | // special handling for this case when we initialize. |
| 8567 | if (InitBound != AllocBound) |
| 8568 | ResizedArrayILE = cast<InitListExpr>(Init); |
| 8569 | } |
| 8570 | |
| Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 8571 | AllocType = Info.Ctx.getConstantArrayType(AllocType, ArrayBound, nullptr, |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 8572 | ArrayType::Normal, 0); |
| 8573 | } else { |
| 8574 | assert(!AllocType->isArrayType() && |
| 8575 | "array allocation with non-array new"); |
| 8576 | } |
| 8577 | |
| Richard Smith | b542602 | 2019-10-03 00:39:35 +0000 | [diff] [blame] | 8578 | APValue *Val; |
| 8579 | if (IsPlacement) { |
| 8580 | AccessKinds AK = AK_Construct; |
| 8581 | struct FindObjectHandler { |
| 8582 | EvalInfo &Info; |
| 8583 | const Expr *E; |
| 8584 | QualType AllocType; |
| 8585 | const AccessKinds AccessKind; |
| 8586 | APValue *Value; |
| 8587 | |
| 8588 | typedef bool result_type; |
| 8589 | bool failed() { return false; } |
| 8590 | bool found(APValue &Subobj, QualType SubobjType) { |
| 8591 | // FIXME: Reject the cases where [basic.life]p8 would not permit the |
| 8592 | // old name of the object to be used to name the new object. |
| 8593 | if (!Info.Ctx.hasSameUnqualifiedType(SubobjType, AllocType)) { |
| 8594 | Info.FFDiag(E, diag::note_constexpr_placement_new_wrong_type) << |
| 8595 | SubobjType << AllocType; |
| 8596 | return false; |
| 8597 | } |
| 8598 | Value = &Subobj; |
| 8599 | return true; |
| 8600 | } |
| 8601 | bool found(APSInt &Value, QualType SubobjType) { |
| 8602 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
| 8603 | return false; |
| 8604 | } |
| 8605 | bool found(APFloat &Value, QualType SubobjType) { |
| 8606 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
| 8607 | return false; |
| 8608 | } |
| 8609 | } Handler = {Info, E, AllocType, AK, nullptr}; |
| 8610 | |
| 8611 | CompleteObject Obj = findCompleteObject(Info, E, AK, Result, AllocType); |
| 8612 | if (!Obj || !findSubobject(Info, E, Obj, Result.Designator, Handler)) |
| 8613 | return false; |
| 8614 | |
| 8615 | Val = Handler.Value; |
| 8616 | |
| 8617 | // [basic.life]p1: |
| 8618 | // The lifetime of an object o of type T ends when [...] the storage |
| 8619 | // which the object occupies is [...] reused by an object that is not |
| 8620 | // nested within o (6.6.2). |
| 8621 | *Val = APValue(); |
| 8622 | } else { |
| 8623 | // Perform the allocation and obtain a pointer to the resulting object. |
| 8624 | Val = Info.createHeapAlloc(E, AllocType, Result); |
| 8625 | if (!Val) |
| 8626 | return false; |
| 8627 | } |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 8628 | |
| 8629 | if (ResizedArrayILE) { |
| 8630 | if (!EvaluateArrayNewInitList(Info, Result, *Val, ResizedArrayILE, |
| 8631 | AllocType)) |
| 8632 | return false; |
| 8633 | } else if (Init) { |
| 8634 | if (!EvaluateInPlace(*Val, Info, Result, Init)) |
| 8635 | return false; |
| 8636 | } else { |
| 8637 | *Val = getDefaultInitValue(AllocType); |
| 8638 | } |
| 8639 | |
| 8640 | // Array new returns a pointer to the first element, not a pointer to the |
| 8641 | // array. |
| 8642 | if (auto *AT = AllocType->getAsArrayTypeUnsafe()) |
| 8643 | Result.addArray(Info, E, cast<ConstantArrayType>(AT)); |
| 8644 | |
| 8645 | return true; |
| 8646 | } |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 8647 | //===----------------------------------------------------------------------===// |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8648 | // Member Pointer Evaluation |
| 8649 | //===----------------------------------------------------------------------===// |
| 8650 | |
| 8651 | namespace { |
| 8652 | class MemberPointerExprEvaluator |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 8653 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8654 | MemberPtr &Result; |
| 8655 | |
| 8656 | bool Success(const ValueDecl *D) { |
| 8657 | Result = MemberPtr(D); |
| 8658 | return true; |
| 8659 | } |
| 8660 | public: |
| 8661 | |
| 8662 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 8663 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 8664 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8665 | bool Success(const APValue &V, const Expr *E) { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8666 | Result.setFrom(V); |
| 8667 | return true; |
| 8668 | } |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8669 | bool ZeroInitialization(const Expr *E) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 8670 | return Success((const ValueDecl*)nullptr); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8671 | } |
| 8672 | |
| 8673 | bool VisitCastExpr(const CastExpr *E); |
| 8674 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 8675 | }; |
| 8676 | } // end anonymous namespace |
| 8677 | |
| 8678 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 8679 | EvalInfo &Info) { |
| 8680 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 8681 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 8682 | } |
| 8683 | |
| 8684 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 8685 | switch (E->getCastKind()) { |
| 8686 | default: |
| 8687 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 8688 | |
| 8689 | case CK_NullToMemberPointer: |
| Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 8690 | VisitIgnoredValue(E->getSubExpr()); |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8691 | return ZeroInitialization(E); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8692 | |
| 8693 | case CK_BaseToDerivedMemberPointer: { |
| 8694 | if (!Visit(E->getSubExpr())) |
| 8695 | return false; |
| 8696 | if (E->path_empty()) |
| 8697 | return true; |
| 8698 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 8699 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 8700 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 8701 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 8702 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 8703 | PathI != PathE; ++PathI) { |
| 8704 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 8705 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 8706 | if (!Result.castToDerived(Derived)) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8707 | return Error(E); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8708 | } |
| 8709 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 8710 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8711 | return Error(E); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8712 | return true; |
| 8713 | } |
| 8714 | |
| 8715 | case CK_DerivedToBaseMemberPointer: |
| 8716 | if (!Visit(E->getSubExpr())) |
| 8717 | return false; |
| 8718 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 8719 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 8720 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 8721 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 8722 | if (!Result.castToBase(Base)) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8723 | return Error(E); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 8724 | } |
| 8725 | return true; |
| 8726 | } |
| 8727 | } |
| 8728 | |
| 8729 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 8730 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 8731 | // member can be formed. |
| 8732 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 8733 | } |
| 8734 | |
| 8735 | //===----------------------------------------------------------------------===// |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8736 | // Record Evaluation |
| 8737 | //===----------------------------------------------------------------------===// |
| 8738 | |
| 8739 | namespace { |
| 8740 | class RecordExprEvaluator |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 8741 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8742 | const LValue &This; |
| 8743 | APValue &Result; |
| 8744 | public: |
| 8745 | |
| 8746 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 8747 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 8748 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8749 | bool Success(const APValue &V, const Expr *E) { |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8750 | Result = V; |
| 8751 | return true; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8752 | } |
| Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 8753 | bool ZeroInitialization(const Expr *E) { |
| 8754 | return ZeroInitialization(E, E->getType()); |
| 8755 | } |
| 8756 | bool ZeroInitialization(const Expr *E, QualType T); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8757 | |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 8758 | bool VisitCallExpr(const CallExpr *E) { |
| 8759 | return handleCallExpr(E, Result, &This); |
| 8760 | } |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 8761 | bool VisitCastExpr(const CastExpr *E); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8762 | bool VisitInitListExpr(const InitListExpr *E); |
| Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 8763 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 8764 | return VisitCXXConstructExpr(E, E->getType()); |
| 8765 | } |
| Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 8766 | bool VisitLambdaExpr(const LambdaExpr *E); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 8767 | bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E); |
| Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 8768 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T); |
| Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 8769 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8770 | bool VisitBinCmp(const BinaryOperator *E); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8771 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8772 | } |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8773 | |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8774 | /// Perform zero-initialization on an object of non-union class type. |
| 8775 | /// C++11 [dcl.init]p5: |
| 8776 | /// To zero-initialize an object or reference of type T means: |
| 8777 | /// [...] |
| 8778 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 8779 | /// each non-static data member and each base-class subobject is |
| 8780 | /// zero-initialized |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 8781 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 8782 | const RecordDecl *RD, |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8783 | const LValue &This, APValue &Result) { |
| 8784 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 8785 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 8786 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
| Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 8787 | std::distance(RD->field_begin(), RD->field_end())); |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8788 | |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 8789 | if (RD->isInvalidDecl()) return false; |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8790 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 8791 | |
| 8792 | if (CD) { |
| 8793 | unsigned Index = 0; |
| 8794 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 8795 | End = CD->bases_end(); I != End; ++I, ++Index) { |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8796 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 8797 | LValue Subobject = This; |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 8798 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
| 8799 | return false; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 8800 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8801 | Result.getStructBase(Index))) |
| 8802 | return false; |
| 8803 | } |
| 8804 | } |
| 8805 | |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8806 | for (const auto *I : RD->fields()) { |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8807 | // -- if T is a reference type, no initialization is performed. |
| David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8808 | if (I->getType()->isReferenceType()) |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8809 | continue; |
| 8810 | |
| 8811 | LValue Subobject = This; |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8812 | if (!HandleLValueMember(Info, E, Subobject, I, &Layout)) |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 8813 | return false; |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8814 | |
| David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8815 | ImplicitValueInitExpr VIE(I->getType()); |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8816 | if (!EvaluateInPlace( |
| David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8817 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8818 | return false; |
| 8819 | } |
| 8820 | |
| 8821 | return true; |
| 8822 | } |
| 8823 | |
| Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 8824 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) { |
| 8825 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
| John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 8826 | if (RD->isInvalidDecl()) return false; |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8827 | if (RD->isUnion()) { |
| 8828 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 8829 | // object's first non-static named data member is zero-initialized |
| 8830 | RecordDecl::field_iterator I = RD->field_begin(); |
| 8831 | if (I == RD->field_end()) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 8832 | Result = APValue((const FieldDecl*)nullptr); |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8833 | return true; |
| 8834 | } |
| 8835 | |
| 8836 | LValue Subobject = This; |
| David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 8837 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 8838 | return false; |
| David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 8839 | Result = APValue(*I); |
| David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 8840 | ImplicitValueInitExpr VIE(I->getType()); |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8841 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8842 | } |
| 8843 | |
| Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 8844 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 8845 | Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD; |
| Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 8846 | return false; |
| 8847 | } |
| 8848 | |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 8849 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8850 | } |
| 8851 | |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 8852 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 8853 | switch (E->getCastKind()) { |
| 8854 | default: |
| 8855 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 8856 | |
| 8857 | case CK_ConstructorConversion: |
| 8858 | return Visit(E->getSubExpr()); |
| 8859 | |
| 8860 | case CK_DerivedToBase: |
| 8861 | case CK_UncheckedDerivedToBase: { |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 8862 | APValue DerivedObject; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8863 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 8864 | return false; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8865 | if (!DerivedObject.isStruct()) |
| 8866 | return Error(E->getSubExpr()); |
| Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 8867 | |
| 8868 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 8869 | APValue *Value = &DerivedObject; |
| 8870 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 8871 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 8872 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 8873 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 8874 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 8875 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 8876 | RD = Base; |
| 8877 | } |
| 8878 | Result = *Value; |
| 8879 | return true; |
| 8880 | } |
| 8881 | } |
| 8882 | } |
| 8883 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8884 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 8885 | if (E->isTransparent()) |
| 8886 | return Visit(E->getInit(0)); |
| 8887 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8888 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
| John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 8889 | if (RD->isInvalidDecl()) return false; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8890 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 8891 | auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
| 8892 | |
| 8893 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
| 8894 | Info, |
| 8895 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
| 8896 | CXXRD && CXXRD->getNumBases()); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8897 | |
| 8898 | if (RD->isUnion()) { |
| Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 8899 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 8900 | Result = APValue(Field); |
| 8901 | if (!Field) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8902 | return true; |
| Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 8903 | |
| 8904 | // If the initializer list for a union does not contain any elements, the |
| 8905 | // first element of the union is value-initialized. |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 8906 | // FIXME: The element should be initialized from an initializer list. |
| 8907 | // Is this difference ever observable for initializer lists which |
| 8908 | // we don't build? |
| Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 8909 | ImplicitValueInitExpr VIE(Field->getType()); |
| 8910 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 8911 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8912 | LValue Subobject = This; |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 8913 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
| 8914 | return false; |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 8915 | |
| 8916 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 8917 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 8918 | isa<CXXDefaultInitExpr>(InitExpr)); |
| 8919 | |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 8920 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8921 | } |
| 8922 | |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 8923 | if (!Result.hasValue()) |
| Richard Smith | c0d04a2 | 2016-05-25 22:06:25 +0000 | [diff] [blame] | 8924 | Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0, |
| 8925 | std::distance(RD->field_begin(), RD->field_end())); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8926 | unsigned ElementNo = 0; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8927 | bool Success = true; |
| Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 8928 | |
| 8929 | // Initialize base classes. |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 8930 | if (CXXRD && CXXRD->getNumBases()) { |
| Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 8931 | for (const auto &Base : CXXRD->bases()) { |
| 8932 | assert(ElementNo < E->getNumInits() && "missing init for base class"); |
| 8933 | const Expr *Init = E->getInit(ElementNo); |
| 8934 | |
| 8935 | LValue Subobject = This; |
| 8936 | if (!HandleLValueBase(Info, Init, Subobject, CXXRD, &Base)) |
| 8937 | return false; |
| 8938 | |
| 8939 | APValue &FieldVal = Result.getStructBase(ElementNo); |
| 8940 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init)) { |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8941 | if (!Info.noteFailure()) |
| Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 8942 | return false; |
| 8943 | Success = false; |
| 8944 | } |
| 8945 | ++ElementNo; |
| 8946 | } |
| Richard Smith | d3d6f4f | 2019-05-12 08:57:59 +0000 | [diff] [blame] | 8947 | |
| 8948 | EvalObj.finishedConstructingBases(); |
| Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 8949 | } |
| 8950 | |
| 8951 | // Initialize members. |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8952 | for (const auto *Field : RD->fields()) { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8953 | // Anonymous bit-fields are not considered members of the class for |
| 8954 | // purposes of aggregate initialization. |
| 8955 | if (Field->isUnnamedBitfield()) |
| 8956 | continue; |
| 8957 | |
| 8958 | LValue Subobject = This; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8959 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8960 | bool HaveInit = ElementNo < E->getNumInits(); |
| 8961 | |
| 8962 | // FIXME: Diagnostics here should point to the end of the initializer |
| 8963 | // list, not the start. |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 8964 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8965 | Subobject, Field, &Layout)) |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 8966 | return false; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8967 | |
| 8968 | // Perform an implicit value-initialization for members beyond the end of |
| 8969 | // the initializer list. |
| 8970 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 8971 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8972 | |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 8973 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 8974 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 8975 | isa<CXXDefaultInitExpr>(Init)); |
| 8976 | |
| Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 8977 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 8978 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) || |
| 8979 | (Field->isBitField() && !truncateBitfieldValue(Info, Init, |
| Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8980 | FieldVal, Field))) { |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8981 | if (!Info.noteFailure()) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8982 | return false; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8983 | Success = false; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8984 | } |
| 8985 | } |
| 8986 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8987 | return Success; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8988 | } |
| 8989 | |
| Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 8990 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 8991 | QualType T) { |
| 8992 | // Note that E's type is not necessarily the type of our class here; we might |
| 8993 | // be initializing an array element instead. |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8994 | const CXXConstructorDecl *FD = E->getConstructor(); |
| John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 8995 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 8996 | |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 8997 | bool ZeroInit = E->requiresZeroInitialization(); |
| 8998 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
| Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 8999 | // If we've already performed zero-initialization, we're already done. |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 9000 | if (Result.hasValue()) |
| Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 9001 | return true; |
| 9002 | |
| Richard Smith | c667cdc | 2019-09-18 17:37:44 +0000 | [diff] [blame] | 9003 | if (ZeroInit) |
| 9004 | return ZeroInitialization(E, T); |
| 9005 | |
| 9006 | Result = getDefaultInitValue(T); |
| 9007 | return true; |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 9008 | } |
| 9009 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9010 | const FunctionDecl *Definition = nullptr; |
| Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 9011 | auto Body = FD->getBody(Definition); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9012 | |
| Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 9013 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9014 | return false; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9015 | |
| Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 9016 | // Avoid materializing a temporary for an elidable copy/move constructor. |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9017 | if (E->isElidable() && !ZeroInit) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9018 | if (const MaterializeTemporaryExpr *ME |
| 9019 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 9020 | return Visit(ME->GetTemporaryExpr()); |
| 9021 | |
| Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 9022 | if (ZeroInit && !ZeroInitialization(E, T)) |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9023 | return false; |
| 9024 | |
| Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 9025 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 9026 | return HandleConstructorCall(E, This, Args, |
| 9027 | cast<CXXConstructorDecl>(Definition), Info, |
| 9028 | Result); |
| 9029 | } |
| 9030 | |
| 9031 | bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr( |
| 9032 | const CXXInheritedCtorInitExpr *E) { |
| 9033 | if (!Info.CurrentCall) { |
| 9034 | assert(Info.checkingPotentialConstantExpression()); |
| 9035 | return false; |
| 9036 | } |
| 9037 | |
| 9038 | const CXXConstructorDecl *FD = E->getConstructor(); |
| 9039 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) |
| 9040 | return false; |
| 9041 | |
| 9042 | const FunctionDecl *Definition = nullptr; |
| 9043 | auto Body = FD->getBody(Definition); |
| 9044 | |
| 9045 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
| 9046 | return false; |
| 9047 | |
| 9048 | return HandleConstructorCall(E, This, Info.CurrentCall->Arguments, |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9049 | cast<CXXConstructorDecl>(Definition), Info, |
| 9050 | Result); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9051 | } |
| 9052 | |
| Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 9053 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
| 9054 | const CXXStdInitializerListExpr *E) { |
| 9055 | const ConstantArrayType *ArrayType = |
| 9056 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
| 9057 | |
| 9058 | LValue Array; |
| 9059 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
| 9060 | return false; |
| 9061 | |
| 9062 | // Get a pointer to the first element of the array. |
| 9063 | Array.addArray(Info, E, ArrayType); |
| 9064 | |
| 9065 | // FIXME: Perform the checks on the field types in SemaInit. |
| 9066 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
| 9067 | RecordDecl::field_iterator Field = Record->field_begin(); |
| 9068 | if (Field == Record->field_end()) |
| 9069 | return Error(E); |
| 9070 | |
| 9071 | // Start pointer. |
| 9072 | if (!Field->getType()->isPointerType() || |
| 9073 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 9074 | ArrayType->getElementType())) |
| 9075 | return Error(E); |
| 9076 | |
| 9077 | // FIXME: What if the initializer_list type has base classes, etc? |
| 9078 | Result = APValue(APValue::UninitStruct(), 0, 2); |
| 9079 | Array.moveInto(Result.getStructField(0)); |
| 9080 | |
| 9081 | if (++Field == Record->field_end()) |
| 9082 | return Error(E); |
| 9083 | |
| 9084 | if (Field->getType()->isPointerType() && |
| 9085 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 9086 | ArrayType->getElementType())) { |
| 9087 | // End pointer. |
| 9088 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
| 9089 | ArrayType->getElementType(), |
| 9090 | ArrayType->getSize().getZExtValue())) |
| 9091 | return false; |
| 9092 | Array.moveInto(Result.getStructField(1)); |
| 9093 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
| 9094 | // Length. |
| 9095 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
| 9096 | else |
| 9097 | return Error(E); |
| 9098 | |
| 9099 | if (++Field != Record->field_end()) |
| 9100 | return Error(E); |
| 9101 | |
| 9102 | return true; |
| 9103 | } |
| 9104 | |
| Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 9105 | bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) { |
| 9106 | const CXXRecordDecl *ClosureClass = E->getLambdaClass(); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 9107 | if (ClosureClass->isInvalidDecl()) |
| 9108 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9109 | |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 9110 | const size_t NumFields = |
| 9111 | std::distance(ClosureClass->field_begin(), ClosureClass->field_end()); |
| Benjamin Kramer | aad1bdc | 2017-02-16 14:08:41 +0000 | [diff] [blame] | 9112 | |
| 9113 | assert(NumFields == (size_t)std::distance(E->capture_init_begin(), |
| 9114 | E->capture_init_end()) && |
| 9115 | "The number of lambda capture initializers should equal the number of " |
| 9116 | "fields within the closure type"); |
| 9117 | |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 9118 | Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields); |
| 9119 | // Iterate through all the lambda's closure object's fields and initialize |
| 9120 | // them. |
| 9121 | auto *CaptureInitIt = E->capture_init_begin(); |
| 9122 | const LambdaCapture *CaptureIt = ClosureClass->captures_begin(); |
| 9123 | bool Success = true; |
| 9124 | for (const auto *Field : ClosureClass->fields()) { |
| 9125 | assert(CaptureInitIt != E->capture_init_end()); |
| 9126 | // Get the initializer for this field |
| 9127 | Expr *const CurFieldInit = *CaptureInitIt++; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9128 | |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 9129 | // If there is no initializer, either this is a VLA or an error has |
| 9130 | // occurred. |
| 9131 | if (!CurFieldInit) |
| 9132 | return Error(E); |
| 9133 | |
| 9134 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 9135 | if (!EvaluateInPlace(FieldVal, Info, This, CurFieldInit)) { |
| 9136 | if (!Info.keepEvaluatingAfterFailure()) |
| 9137 | return false; |
| 9138 | Success = false; |
| 9139 | } |
| 9140 | ++CaptureIt; |
| Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 9141 | } |
| Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 9142 | return Success; |
| Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 9143 | } |
| 9144 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9145 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 9146 | APValue &Result, EvalInfo &Info) { |
| 9147 | assert(E->isRValue() && E->getType()->isRecordType() && |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9148 | "can't evaluate expression as a record rvalue"); |
| 9149 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 9150 | } |
| 9151 | |
| 9152 | //===----------------------------------------------------------------------===// |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9153 | // Temporary Evaluation |
| 9154 | // |
| 9155 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 9156 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 9157 | // materialized so that a reference can bind to it. |
| 9158 | //===----------------------------------------------------------------------===// |
| 9159 | namespace { |
| 9160 | class TemporaryExprEvaluator |
| 9161 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 9162 | public: |
| 9163 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 9164 | LValueExprEvaluatorBaseTy(Info, Result, false) {} |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9165 | |
| 9166 | /// Visit an expression which constructs the value of this temporary. |
| 9167 | bool VisitConstructExpr(const Expr *E) { |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 9168 | APValue &Value = |
| 9169 | Info.CurrentCall->createTemporary(E, E->getType(), false, Result); |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 9170 | return EvaluateInPlace(Value, Info, Result, E); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9171 | } |
| 9172 | |
| 9173 | bool VisitCastExpr(const CastExpr *E) { |
| 9174 | switch (E->getCastKind()) { |
| 9175 | default: |
| 9176 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 9177 | |
| 9178 | case CK_ConstructorConversion: |
| 9179 | return VisitConstructExpr(E->getSubExpr()); |
| 9180 | } |
| 9181 | } |
| 9182 | bool VisitInitListExpr(const InitListExpr *E) { |
| 9183 | return VisitConstructExpr(E); |
| 9184 | } |
| 9185 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 9186 | return VisitConstructExpr(E); |
| 9187 | } |
| 9188 | bool VisitCallExpr(const CallExpr *E) { |
| 9189 | return VisitConstructExpr(E); |
| 9190 | } |
| Richard Smith | 513955c | 2014-12-17 19:24:30 +0000 | [diff] [blame] | 9191 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
| 9192 | return VisitConstructExpr(E); |
| 9193 | } |
| Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 9194 | bool VisitLambdaExpr(const LambdaExpr *E) { |
| 9195 | return VisitConstructExpr(E); |
| 9196 | } |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9197 | }; |
| 9198 | } // end anonymous namespace |
| 9199 | |
| 9200 | /// Evaluate an expression of record type as a temporary. |
| 9201 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
| Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 9202 | assert(E->isRValue() && E->getType()->isRecordType()); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9203 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 9204 | } |
| 9205 | |
| 9206 | //===----------------------------------------------------------------------===// |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9207 | // Vector Evaluation |
| 9208 | //===----------------------------------------------------------------------===// |
| 9209 | |
| 9210 | namespace { |
| Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 9211 | class VectorExprEvaluator |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 9212 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9213 | APValue &Result; |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9214 | public: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9215 | |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9216 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 9217 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9218 | |
| Craig Topper | 9798b93 | 2015-09-29 04:30:05 +0000 | [diff] [blame] | 9219 | bool Success(ArrayRef<APValue> V, const Expr *E) { |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9220 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 9221 | // FIXME: remove this APValue copy. |
| 9222 | Result = APValue(V.data(), V.size()); |
| 9223 | return true; |
| 9224 | } |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9225 | bool Success(const APValue &V, const Expr *E) { |
| Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 9226 | assert(V.isVector()); |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9227 | Result = V; |
| 9228 | return true; |
| 9229 | } |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9230 | bool ZeroInitialization(const Expr *E); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9231 | |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9232 | bool VisitUnaryReal(const UnaryOperator *E) |
| Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 9233 | { return Visit(E->getSubExpr()); } |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9234 | bool VisitCastExpr(const CastExpr* E); |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9235 | bool VisitInitListExpr(const InitListExpr *E); |
| 9236 | bool VisitUnaryImag(const UnaryOperator *E); |
| Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 9237 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
| Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 9238 | // binary comparisons, binary and/or/xor, |
| Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 9239 | // shufflevector, ExtVectorElementExpr |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9240 | }; |
| 9241 | } // end anonymous namespace |
| 9242 | |
| 9243 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9244 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9245 | return VectorExprEvaluator(Info, Result).Visit(E); |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9246 | } |
| 9247 | |
| George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 9248 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9249 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
| Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 9250 | unsigned NElts = VTy->getNumElements(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9251 | |
| Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 9252 | const Expr *SE = E->getSubExpr(); |
| Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 9253 | QualType SETy = SE->getType(); |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9254 | |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9255 | switch (E->getCastKind()) { |
| 9256 | case CK_VectorSplat: { |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9257 | APValue Val = APValue(); |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9258 | if (SETy->isIntegerType()) { |
| 9259 | APSInt IntResult; |
| 9260 | if (!EvaluateInteger(SE, IntResult, Info)) |
| George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 9261 | return false; |
| 9262 | Val = APValue(std::move(IntResult)); |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9263 | } else if (SETy->isRealFloatingType()) { |
| George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 9264 | APFloat FloatResult(0.0); |
| 9265 | if (!EvaluateFloat(SE, FloatResult, Info)) |
| 9266 | return false; |
| 9267 | Val = APValue(std::move(FloatResult)); |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9268 | } else { |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9269 | return Error(E); |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9270 | } |
| Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 9271 | |
| 9272 | // Splat and create vector APValue. |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9273 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 9274 | return Success(Elts, E); |
| Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 9275 | } |
| Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 9276 | case CK_BitCast: { |
| 9277 | // Evaluate the operand into an APInt we can extract from. |
| 9278 | llvm::APInt SValInt; |
| 9279 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 9280 | return false; |
| 9281 | // Extract the elements |
| 9282 | QualType EltTy = VTy->getElementType(); |
| 9283 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 9284 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 9285 | SmallVector<APValue, 4> Elts; |
| 9286 | if (EltTy->isRealFloatingType()) { |
| 9287 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
| Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 9288 | unsigned FloatEltSize = EltSize; |
| Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 9289 | if (&Sem == &APFloat::x87DoubleExtended()) |
| Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 9290 | FloatEltSize = 80; |
| 9291 | for (unsigned i = 0; i < NElts; i++) { |
| 9292 | llvm::APInt Elt; |
| 9293 | if (BigEndian) |
| 9294 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 9295 | else |
| 9296 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
| Tim Northover | 178723a | 2013-01-22 09:46:51 +0000 | [diff] [blame] | 9297 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
| Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 9298 | } |
| 9299 | } else if (EltTy->isIntegerType()) { |
| 9300 | for (unsigned i = 0; i < NElts; i++) { |
| 9301 | llvm::APInt Elt; |
| 9302 | if (BigEndian) |
| 9303 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 9304 | else |
| 9305 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 9306 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 9307 | } |
| 9308 | } else { |
| 9309 | return Error(E); |
| 9310 | } |
| 9311 | return Success(Elts, E); |
| 9312 | } |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9313 | default: |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9314 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9315 | } |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9316 | } |
| 9317 | |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9318 | bool |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9319 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9320 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9321 | unsigned NumInits = E->getNumInits(); |
| Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 9322 | unsigned NumElements = VT->getNumElements(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9323 | |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9324 | QualType EltTy = VT->getElementType(); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9325 | SmallVector<APValue, 4> Elements; |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9326 | |
| Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 9327 | // The number of initializers can be less than the number of |
| 9328 | // vector elements. For OpenCL, this can be due to nested vector |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9329 | // initialization. For GCC compatibility, missing trailing elements |
| Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 9330 | // should be initialized with zeroes. |
| 9331 | unsigned CountInits = 0, CountElts = 0; |
| 9332 | while (CountElts < NumElements) { |
| 9333 | // Handle nested vector initialization. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9334 | if (CountInits < NumInits |
| Eli Friedman | 1409e6e | 2013-09-17 04:07:02 +0000 | [diff] [blame] | 9335 | && E->getInit(CountInits)->getType()->isVectorType()) { |
| Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 9336 | APValue v; |
| 9337 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 9338 | return Error(E); |
| 9339 | unsigned vlen = v.getVectorLength(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9340 | for (unsigned j = 0; j < vlen; j++) |
| Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 9341 | Elements.push_back(v.getVectorElt(j)); |
| 9342 | CountElts += vlen; |
| 9343 | } else if (EltTy->isIntegerType()) { |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9344 | llvm::APSInt sInt(32); |
| Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 9345 | if (CountInits < NumInits) { |
| 9346 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
| Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 9347 | return false; |
| Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 9348 | } else // trailing integer zero. |
| 9349 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 9350 | Elements.push_back(APValue(sInt)); |
| 9351 | CountElts++; |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9352 | } else { |
| 9353 | llvm::APFloat f(0.0); |
| Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 9354 | if (CountInits < NumInits) { |
| 9355 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
| Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 9356 | return false; |
| Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 9357 | } else // trailing float zero. |
| 9358 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 9359 | Elements.push_back(APValue(f)); |
| 9360 | CountElts++; |
| John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 9361 | } |
| Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 9362 | CountInits++; |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9363 | } |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9364 | return Success(Elements, E); |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9365 | } |
| 9366 | |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9367 | bool |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9368 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
| Simon Pilgrim | c15b38e | 2019-10-03 15:08:30 +0000 | [diff] [blame] | 9369 | const auto *VT = E->getType()->castAs<VectorType>(); |
| Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 9370 | QualType EltTy = VT->getElementType(); |
| 9371 | APValue ZeroElement; |
| 9372 | if (EltTy->isIntegerType()) |
| 9373 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 9374 | else |
| 9375 | ZeroElement = |
| 9376 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 9377 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9378 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9379 | return Success(Elements, E); |
| Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 9380 | } |
| 9381 | |
| Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 9382 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 9383 | VisitIgnoredValue(E->getSubExpr()); |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9384 | return ZeroInitialization(E); |
| Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 9385 | } |
| 9386 | |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 9387 | //===----------------------------------------------------------------------===// |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9388 | // Array Evaluation |
| 9389 | //===----------------------------------------------------------------------===// |
| 9390 | |
| 9391 | namespace { |
| 9392 | class ArrayExprEvaluator |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 9393 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9394 | const LValue &This; |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9395 | APValue &Result; |
| 9396 | public: |
| 9397 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9398 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 9399 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9400 | |
| 9401 | bool Success(const APValue &V, const Expr *E) { |
| Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 9402 | assert(V.isArray() && "expected array"); |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9403 | Result = V; |
| 9404 | return true; |
| 9405 | } |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9406 | |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9407 | bool ZeroInitialization(const Expr *E) { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9408 | const ConstantArrayType *CAT = |
| 9409 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 9410 | if (!CAT) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9411 | return Error(E); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9412 | |
| 9413 | Result = APValue(APValue::UninitArray(), 0, |
| 9414 | CAT->getSize().getZExtValue()); |
| 9415 | if (!Result.hasArrayFiller()) return true; |
| 9416 | |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9417 | // Zero-initialize all elements. |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9418 | LValue Subobject = This; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 9419 | Subobject.addArray(Info, E, CAT); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9420 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 9421 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9422 | } |
| 9423 | |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 9424 | bool VisitCallExpr(const CallExpr *E) { |
| 9425 | return handleCallExpr(E, Result, &This); |
| 9426 | } |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 9427 | bool VisitInitListExpr(const InitListExpr *E, |
| 9428 | QualType AllocType = QualType()); |
| Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 9429 | bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9430 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9431 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 9432 | const LValue &Subobject, |
| 9433 | APValue *Value, QualType Type); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 9434 | bool VisitStringLiteral(const StringLiteral *E, |
| 9435 | QualType AllocType = QualType()) { |
| 9436 | expandStringLiteral(Info, E, Result, AllocType); |
| Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 9437 | return true; |
| 9438 | } |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9439 | }; |
| 9440 | } // end anonymous namespace |
| 9441 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9442 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 9443 | APValue &Result, EvalInfo &Info) { |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9444 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9445 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9446 | } |
| 9447 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 9448 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
| 9449 | APValue &Result, const InitListExpr *ILE, |
| 9450 | QualType AllocType) { |
| 9451 | assert(ILE->isRValue() && ILE->getType()->isArrayType() && |
| 9452 | "not an array rvalue"); |
| 9453 | return ArrayExprEvaluator(Info, This, Result) |
| 9454 | .VisitInitListExpr(ILE, AllocType); |
| 9455 | } |
| 9456 | |
| Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 9457 | // Return true iff the given array filler may depend on the element index. |
| 9458 | static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { |
| 9459 | // For now, just whitelist non-class value-initialization and initialization |
| 9460 | // lists comprised of them. |
| 9461 | if (isa<ImplicitValueInitExpr>(FillerExpr)) |
| 9462 | return false; |
| 9463 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(FillerExpr)) { |
| 9464 | for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { |
| 9465 | if (MaybeElementDependentArrayFiller(ILE->getInit(I))) |
| 9466 | return true; |
| 9467 | } |
| 9468 | return false; |
| 9469 | } |
| 9470 | return true; |
| 9471 | } |
| 9472 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 9473 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E, |
| 9474 | QualType AllocType) { |
| 9475 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
| 9476 | AllocType.isNull() ? E->getType() : AllocType); |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9477 | if (!CAT) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9478 | return Error(E); |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9479 | |
| Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 9480 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 9481 | // an appropriately-typed string literal enclosed in braces. |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 9482 | if (E->isStringLiteralInit()) { |
| 9483 | auto *SL = dyn_cast<StringLiteral>(E->getInit(0)->IgnoreParens()); |
| 9484 | // FIXME: Support ObjCEncodeExpr here once we support it in |
| 9485 | // ArrayExprEvaluator generally. |
| 9486 | if (!SL) |
| 9487 | return Error(E); |
| 9488 | return VisitStringLiteral(SL, AllocType); |
| 9489 | } |
| Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 9490 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9491 | bool Success = true; |
| 9492 | |
| Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 9493 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
| 9494 | "zero-initialized array shouldn't have any initialized elts"); |
| 9495 | APValue Filler; |
| 9496 | if (Result.isArray() && Result.hasArrayFiller()) |
| 9497 | Filler = Result.getArrayFiller(); |
| 9498 | |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9499 | unsigned NumEltsToInit = E->getNumInits(); |
| 9500 | unsigned NumElts = CAT->getSize().getZExtValue(); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9501 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : nullptr; |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9502 | |
| 9503 | // If the initializer might depend on the array index, run it for each |
| Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 9504 | // array element. |
| 9505 | if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr)) |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9506 | NumEltsToInit = NumElts; |
| 9507 | |
| Nicola Zaghen | 3538b39 | 2018-05-15 13:30:56 +0000 | [diff] [blame] | 9508 | LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: " |
| 9509 | << NumEltsToInit << ".\n"); |
| Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 9510 | |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9511 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
| Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 9512 | |
| 9513 | // If the array was previously zero-initialized, preserve the |
| 9514 | // zero-initialized values. |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 9515 | if (Filler.hasValue()) { |
| Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 9516 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
| 9517 | Result.getArrayInitializedElt(I) = Filler; |
| 9518 | if (Result.hasArrayFiller()) |
| 9519 | Result.getArrayFiller() = Filler; |
| 9520 | } |
| 9521 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9522 | LValue Subobject = This; |
| Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 9523 | Subobject.addArray(Info, E, CAT); |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9524 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
| 9525 | const Expr *Init = |
| 9526 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 9527 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9528 | Info, Subobject, Init) || |
| 9529 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9530 | CAT->getElementType(), 1)) { |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 9531 | if (!Info.noteFailure()) |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9532 | return false; |
| 9533 | Success = false; |
| 9534 | } |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9535 | } |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9536 | |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9537 | if (!Result.hasArrayFiller()) |
| 9538 | return Success; |
| 9539 | |
| 9540 | // If we get here, we have a trivial filler, which we can just evaluate |
| 9541 | // once and splat over the rest of the array elements. |
| 9542 | assert(FillerExpr && "no array filler for incomplete init list"); |
| 9543 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
| 9544 | FillerExpr) && Success; |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9545 | } |
| 9546 | |
| Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 9547 | bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 9548 | LValue CommonLV; |
| Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 9549 | if (E->getCommonExpr() && |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 9550 | !Evaluate(Info.CurrentCall->createTemporary( |
| 9551 | E->getCommonExpr(), |
| 9552 | getStorageType(Info.Ctx, E->getCommonExpr()), false, |
| 9553 | CommonLV), |
| Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 9554 | Info, E->getCommonExpr()->getSourceExpr())) |
| 9555 | return false; |
| 9556 | |
| 9557 | auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe()); |
| 9558 | |
| 9559 | uint64_t Elements = CAT->getSize().getZExtValue(); |
| 9560 | Result = APValue(APValue::UninitArray(), Elements, Elements); |
| 9561 | |
| 9562 | LValue Subobject = This; |
| 9563 | Subobject.addArray(Info, E, CAT); |
| 9564 | |
| 9565 | bool Success = true; |
| 9566 | for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) { |
| 9567 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
| 9568 | Info, Subobject, E->getSubExpr()) || |
| 9569 | !HandleLValueArrayAdjustment(Info, E, Subobject, |
| 9570 | CAT->getElementType(), 1)) { |
| 9571 | if (!Info.noteFailure()) |
| 9572 | return false; |
| 9573 | Success = false; |
| 9574 | } |
| 9575 | } |
| 9576 | |
| 9577 | return Success; |
| 9578 | } |
| 9579 | |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9580 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9581 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
| 9582 | } |
| Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 9583 | |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9584 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 9585 | const LValue &Subobject, |
| 9586 | APValue *Value, |
| 9587 | QualType Type) { |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 9588 | bool HadZeroInit = Value->hasValue(); |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9589 | |
| 9590 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
| 9591 | unsigned N = CAT->getSize().getZExtValue(); |
| 9592 | |
| 9593 | // Preserve the array filler if we had prior zero-initialization. |
| 9594 | APValue Filler = |
| 9595 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
| 9596 | : APValue(); |
| 9597 | |
| 9598 | *Value = APValue(APValue::UninitArray(), N, N); |
| 9599 | |
| 9600 | if (HadZeroInit) |
| 9601 | for (unsigned I = 0; I != N; ++I) |
| 9602 | Value->getArrayInitializedElt(I) = Filler; |
| 9603 | |
| 9604 | // Initialize the elements. |
| 9605 | LValue ArrayElt = Subobject; |
| 9606 | ArrayElt.addArray(Info, E, CAT); |
| 9607 | for (unsigned I = 0; I != N; ++I) |
| 9608 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
| 9609 | CAT->getElementType()) || |
| 9610 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
| 9611 | CAT->getElementType(), 1)) |
| 9612 | return false; |
| 9613 | |
| 9614 | return true; |
| Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 9615 | } |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9616 | |
| Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 9617 | if (!Type->isRecordType()) |
| Richard Smith | 9fce7bc | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 9618 | return Error(E); |
| 9619 | |
| Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 9620 | return RecordExprEvaluator(Info, Subobject, *Value) |
| 9621 | .VisitCXXConstructExpr(E, Type); |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9622 | } |
| 9623 | |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 9624 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 9625 | // Integer Evaluation |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9626 | // |
| 9627 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 9628 | // types and back in constant folding. Integer values are thus represented |
| 9629 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 9630 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 9631 | |
| 9632 | namespace { |
| Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 9633 | class IntExprEvaluator |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9634 | : public ExprEvaluatorBase<IntExprEvaluator> { |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9635 | APValue &Result; |
| Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 9636 | public: |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9637 | IntExprEvaluator(EvalInfo &info, APValue &result) |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9638 | : ExprEvaluatorBaseTy(info), Result(result) {} |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 9639 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9640 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
| Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 9641 | assert(E->getType()->isIntegralOrEnumerationType() && |
| Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 9642 | "Invalid evaluation result."); |
| Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 9643 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
| Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 9644 | "Invalid evaluation result."); |
| Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 9645 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
| Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 9646 | "Invalid evaluation result."); |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9647 | Result = APValue(SI); |
| Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 9648 | return true; |
| 9649 | } |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9650 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 9651 | return Success(SI, E, Result); |
| 9652 | } |
| Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 9653 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9654 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9655 | assert(E->getType()->isIntegralOrEnumerationType() && |
| Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 9656 | "Invalid evaluation result."); |
| Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 9657 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
| Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 9658 | "Invalid evaluation result."); |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9659 | Result = APValue(APSInt(I)); |
| Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 9660 | Result.getInt().setIsUnsigned( |
| 9661 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
| Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 9662 | return true; |
| 9663 | } |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9664 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 9665 | return Success(I, E, Result); |
| 9666 | } |
| Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 9667 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9668 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9669 | assert(E->getType()->isIntegralOrEnumerationType() && |
| Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 9670 | "Invalid evaluation result."); |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9671 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
| Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 9672 | return true; |
| 9673 | } |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9674 | bool Success(uint64_t Value, const Expr *E) { |
| 9675 | return Success(Value, E, Result); |
| 9676 | } |
| Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 9677 | |
| Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 9678 | bool Success(CharUnits Size, const Expr *E) { |
| 9679 | return Success(Size.getQuantity(), E); |
| 9680 | } |
| 9681 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9682 | bool Success(const APValue &V, const Expr *E) { |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 9683 | if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate()) { |
| Richard Smith | 9c8d1c5 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 9684 | Result = V; |
| 9685 | return true; |
| 9686 | } |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9687 | return Success(V.getInt(), E); |
| Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 9688 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9689 | |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9690 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
| Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 9691 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9692 | //===--------------------------------------------------------------------===// |
| 9693 | // Visitor Methods |
| 9694 | //===--------------------------------------------------------------------===// |
| Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 9695 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 9696 | bool VisitConstantExpr(const ConstantExpr *E); |
| 9697 | |
| Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 9698 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
| Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 9699 | return Success(E->getValue(), E); |
| Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 9700 | } |
| 9701 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
| Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 9702 | return Success(E->getValue(), E); |
| Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 9703 | } |
| Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 9704 | |
| 9705 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 9706 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9707 | if (CheckReferencedDecl(E, E->getDecl())) |
| 9708 | return true; |
| 9709 | |
| 9710 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
| Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 9711 | } |
| 9712 | bool VisitMemberExpr(const MemberExpr *E) { |
| 9713 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
| David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 9714 | VisitIgnoredBaseExpression(E->getBase()); |
| Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 9715 | return true; |
| 9716 | } |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9717 | |
| 9718 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
| Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 9719 | } |
| 9720 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9721 | bool VisitCallExpr(const CallExpr *E); |
| Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 9722 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
| Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 9723 | bool VisitBinaryOperator(const BinaryOperator *E); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9724 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
| Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 9725 | bool VisitUnaryOperator(const UnaryOperator *E); |
| Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 9726 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9727 | bool VisitCastExpr(const CastExpr* E); |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9728 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
| Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 9729 | |
| Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 9730 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
| Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 9731 | return Success(E->getValue(), E); |
| Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 9732 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9733 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 9734 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 9735 | return Success(E->getValue(), E); |
| 9736 | } |
| Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 9737 | |
| 9738 | bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
| 9739 | if (Info.ArrayInitIndex == uint64_t(-1)) { |
| 9740 | // We were asked to evaluate this subexpression independent of the |
| 9741 | // enclosing ArrayInitLoopExpr. We can't do that. |
| 9742 | Info.FFDiag(E); |
| 9743 | return false; |
| 9744 | } |
| 9745 | return Success(Info.ArrayInitIndex, E); |
| 9746 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9747 | |
| Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 9748 | // Note, GNU defines __null as an integer, not a pointer. |
| Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 9749 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9750 | return ZeroInitialization(E); |
| Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 9751 | } |
| 9752 | |
| Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 9753 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 9754 | return Success(E->getValue(), E); |
| 9755 | } |
| 9756 | |
| John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 9757 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 9758 | return Success(E->getValue(), E); |
| 9759 | } |
| 9760 | |
| John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 9761 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 9762 | return Success(E->getValue(), E); |
| 9763 | } |
| 9764 | |
| Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9765 | bool VisitUnaryReal(const UnaryOperator *E); |
| Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 9766 | bool VisitUnaryImag(const UnaryOperator *E); |
| 9767 | |
| Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 9768 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
| Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 9769 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 9770 | bool VisitSourceLocExpr(const SourceLocExpr *E); |
| Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 9771 | // FIXME: Missing: array subscript of vector, member of vector |
| Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9772 | }; |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9773 | |
| 9774 | class FixedPointExprEvaluator |
| 9775 | : public ExprEvaluatorBase<FixedPointExprEvaluator> { |
| 9776 | APValue &Result; |
| 9777 | |
| 9778 | public: |
| 9779 | FixedPointExprEvaluator(EvalInfo &info, APValue &result) |
| 9780 | : ExprEvaluatorBaseTy(info), Result(result) {} |
| 9781 | |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9782 | bool Success(const llvm::APInt &I, const Expr *E) { |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 9783 | return Success( |
| 9784 | APFixedPoint(I, Info.Ctx.getFixedPointSemantics(E->getType())), E); |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9785 | } |
| 9786 | |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9787 | bool Success(uint64_t Value, const Expr *E) { |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 9788 | return Success( |
| 9789 | APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(E->getType())), E); |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9790 | } |
| 9791 | |
| 9792 | bool Success(const APValue &V, const Expr *E) { |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 9793 | return Success(V.getFixedPoint(), E); |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9794 | } |
| 9795 | |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 9796 | bool Success(const APFixedPoint &V, const Expr *E) { |
| 9797 | assert(E->getType()->isFixedPointType() && "Invalid evaluation result."); |
| 9798 | assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) && |
| 9799 | "Invalid evaluation result."); |
| 9800 | Result = APValue(V); |
| 9801 | return true; |
| 9802 | } |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9803 | |
| 9804 | //===--------------------------------------------------------------------===// |
| 9805 | // Visitor Methods |
| 9806 | //===--------------------------------------------------------------------===// |
| 9807 | |
| 9808 | bool VisitFixedPointLiteral(const FixedPointLiteral *E) { |
| 9809 | return Success(E->getValue(), E); |
| 9810 | } |
| 9811 | |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 9812 | bool VisitCastExpr(const CastExpr *E); |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9813 | bool VisitUnaryOperator(const UnaryOperator *E); |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 9814 | bool VisitBinaryOperator(const BinaryOperator *E); |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9815 | }; |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 9816 | } // end anonymous namespace |
| Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 9817 | |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9818 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 9819 | /// produce either the integer value or a pointer. |
| 9820 | /// |
| 9821 | /// GCC has a heinous extension which folds casts between pointer types and |
| 9822 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 9823 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 9824 | /// Some simple arithmetic on such values is supported (they are treated much |
| 9825 | /// like char*). |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9826 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
| Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 9827 | EvalInfo &Info) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9828 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9829 | return IntExprEvaluator(Info, Result).Visit(E); |
| Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 9830 | } |
| Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 9831 | |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9832 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9833 | APValue Val; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9834 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
| Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 9835 | return false; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9836 | if (!Val.isInt()) { |
| 9837 | // FIXME: It would be better to produce the diagnostic for casting |
| 9838 | // a pointer to an integer. |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 9839 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9840 | return false; |
| 9841 | } |
| Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 9842 | Result = Val.getInt(); |
| 9843 | return true; |
| Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 9844 | } |
| Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 9845 | |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 9846 | bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) { |
| 9847 | APValue Evaluated = E->EvaluateInContext( |
| 9848 | Info.Ctx, Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
| 9849 | return Success(Evaluated, E); |
| 9850 | } |
| 9851 | |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 9852 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
| 9853 | EvalInfo &Info) { |
| 9854 | if (E->getType()->isFixedPointType()) { |
| 9855 | APValue Val; |
| 9856 | if (!FixedPointExprEvaluator(Info, Val).Visit(E)) |
| 9857 | return false; |
| 9858 | if (!Val.isFixedPoint()) |
| 9859 | return false; |
| 9860 | |
| 9861 | Result = Val.getFixedPoint(); |
| 9862 | return true; |
| 9863 | } |
| 9864 | return false; |
| 9865 | } |
| 9866 | |
| 9867 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
| 9868 | EvalInfo &Info) { |
| 9869 | if (E->getType()->isIntegerType()) { |
| 9870 | auto FXSema = Info.Ctx.getFixedPointSemantics(E->getType()); |
| 9871 | APSInt Val; |
| 9872 | if (!EvaluateInteger(E, Val, Info)) |
| 9873 | return false; |
| 9874 | Result = APFixedPoint(Val, FXSema); |
| 9875 | return true; |
| 9876 | } else if (E->getType()->isFixedPointType()) { |
| 9877 | return EvaluateFixedPoint(E, Result, Info); |
| 9878 | } |
| 9879 | return false; |
| 9880 | } |
| 9881 | |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9882 | /// Check whether the given declaration can be directly converted to an integral |
| 9883 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 9884 | /// try. |
| Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 9885 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
| Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 9886 | // Enums are integer constant exprs. |
| Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 9887 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
| Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 9888 | // Check for signedness/width mismatches between E type and ECD value. |
| 9889 | bool SameSign = (ECD->getInitVal().isSigned() |
| 9890 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 9891 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 9892 | == Info.Ctx.getIntWidth(E->getType())); |
| 9893 | if (SameSign && SameWidth) |
| 9894 | return Success(ECD->getInitVal(), E); |
| 9895 | else { |
| 9896 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 9897 | // by computing a new value matching the type of E. |
| 9898 | llvm::APSInt Val = ECD->getInitVal(); |
| 9899 | if (!SameSign) |
| 9900 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 9901 | if (!SameWidth) |
| 9902 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 9903 | return Success(Val, E); |
| 9904 | } |
| Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 9905 | } |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9906 | return false; |
| Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 9907 | } |
| 9908 | |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 9909 | /// Values returned by __builtin_classify_type, chosen to match the values |
| 9910 | /// produced by GCC's builtin. |
| 9911 | enum class GCCTypeClass { |
| 9912 | None = -1, |
| 9913 | Void = 0, |
| 9914 | Integer = 1, |
| 9915 | // GCC reserves 2 for character types, but instead classifies them as |
| 9916 | // integers. |
| 9917 | Enum = 3, |
| 9918 | Bool = 4, |
| 9919 | Pointer = 5, |
| 9920 | // GCC reserves 6 for references, but appears to never use it (because |
| 9921 | // expressions never have reference type, presumably). |
| 9922 | PointerToDataMember = 7, |
| 9923 | RealFloat = 8, |
| 9924 | Complex = 9, |
| 9925 | // GCC reserves 10 for functions, but does not use it since GCC version 6 due |
| 9926 | // to decay to pointer. (Prior to version 6 it was only used in C++ mode). |
| 9927 | // GCC claims to reserve 11 for pointers to member functions, but *actually* |
| 9928 | // uses 12 for that purpose, same as for a class or struct. Maybe it |
| 9929 | // internally implements a pointer to member as a struct? Who knows. |
| 9930 | PointerToMemberFunction = 12, // Not a bug, see above. |
| 9931 | ClassOrStruct = 12, |
| 9932 | Union = 13, |
| 9933 | // GCC reserves 14 for arrays, but does not use it since GCC version 6 due to |
| 9934 | // decay to pointer. (Prior to version 6 it was only used in C++ mode). |
| 9935 | // GCC reserves 15 for strings, but actually uses 5 (pointer) for string |
| 9936 | // literals. |
| 9937 | }; |
| 9938 | |
| Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 9939 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 9940 | /// as GCC. |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 9941 | static GCCTypeClass |
| 9942 | EvaluateBuiltinClassifyType(QualType T, const LangOptions &LangOpts) { |
| 9943 | assert(!T->isDependentType() && "unexpected dependent type"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9944 | |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 9945 | QualType CanTy = T.getCanonicalType(); |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 9946 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanTy); |
| 9947 | |
| 9948 | switch (CanTy->getTypeClass()) { |
| 9949 | #define TYPE(ID, BASE) |
| 9950 | #define DEPENDENT_TYPE(ID, BASE) case Type::ID: |
| 9951 | #define NON_CANONICAL_TYPE(ID, BASE) case Type::ID: |
| 9952 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID: |
| John McCall | 36b12a8 | 2019-10-02 06:35:23 +0000 | [diff] [blame] | 9953 | #include "clang/AST/TypeNodes.inc" |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 9954 | case Type::Auto: |
| 9955 | case Type::DeducedTemplateSpecialization: |
| 9956 | llvm_unreachable("unexpected non-canonical or dependent type"); |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 9957 | |
| 9958 | case Type::Builtin: |
| 9959 | switch (BT->getKind()) { |
| 9960 | #define BUILTIN_TYPE(ID, SINGLETON_ID) |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 9961 | #define SIGNED_TYPE(ID, SINGLETON_ID) \ |
| 9962 | case BuiltinType::ID: return GCCTypeClass::Integer; |
| 9963 | #define FLOATING_TYPE(ID, SINGLETON_ID) \ |
| 9964 | case BuiltinType::ID: return GCCTypeClass::RealFloat; |
| 9965 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \ |
| 9966 | case BuiltinType::ID: break; |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 9967 | #include "clang/AST/BuiltinTypes.def" |
| 9968 | case BuiltinType::Void: |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 9969 | return GCCTypeClass::Void; |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 9970 | |
| 9971 | case BuiltinType::Bool: |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 9972 | return GCCTypeClass::Bool; |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 9973 | |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 9974 | case BuiltinType::Char_U: |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 9975 | case BuiltinType::UChar: |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 9976 | case BuiltinType::WChar_U: |
| 9977 | case BuiltinType::Char8: |
| 9978 | case BuiltinType::Char16: |
| 9979 | case BuiltinType::Char32: |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 9980 | case BuiltinType::UShort: |
| 9981 | case BuiltinType::UInt: |
| 9982 | case BuiltinType::ULong: |
| 9983 | case BuiltinType::ULongLong: |
| 9984 | case BuiltinType::UInt128: |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 9985 | return GCCTypeClass::Integer; |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 9986 | |
| Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 9987 | case BuiltinType::UShortAccum: |
| 9988 | case BuiltinType::UAccum: |
| 9989 | case BuiltinType::ULongAccum: |
| Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 9990 | case BuiltinType::UShortFract: |
| 9991 | case BuiltinType::UFract: |
| 9992 | case BuiltinType::ULongFract: |
| 9993 | case BuiltinType::SatUShortAccum: |
| 9994 | case BuiltinType::SatUAccum: |
| 9995 | case BuiltinType::SatULongAccum: |
| 9996 | case BuiltinType::SatUShortFract: |
| 9997 | case BuiltinType::SatUFract: |
| 9998 | case BuiltinType::SatULongFract: |
| Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 9999 | return GCCTypeClass::None; |
| 10000 | |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10001 | case BuiltinType::NullPtr: |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10002 | |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10003 | case BuiltinType::ObjCId: |
| 10004 | case BuiltinType::ObjCClass: |
| 10005 | case BuiltinType::ObjCSel: |
| Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 10006 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 10007 | case BuiltinType::Id: |
| Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 10008 | #include "clang/Basic/OpenCLImageTypes.def" |
| Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 10009 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 10010 | case BuiltinType::Id: |
| 10011 | #include "clang/Basic/OpenCLExtensionTypes.def" |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10012 | case BuiltinType::OCLSampler: |
| 10013 | case BuiltinType::OCLEvent: |
| 10014 | case BuiltinType::OCLClkEvent: |
| 10015 | case BuiltinType::OCLQueue: |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10016 | case BuiltinType::OCLReserveID: |
| Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 10017 | #define SVE_TYPE(Name, Id, SingletonId) \ |
| 10018 | case BuiltinType::Id: |
| 10019 | #include "clang/Basic/AArch64SVEACLETypes.def" |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 10020 | return GCCTypeClass::None; |
| 10021 | |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10022 | case BuiltinType::Dependent: |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 10023 | llvm_unreachable("unexpected dependent type"); |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10024 | }; |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 10025 | llvm_unreachable("unexpected placeholder type"); |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10026 | |
| 10027 | case Type::Enum: |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 10028 | return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer; |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10029 | |
| 10030 | case Type::Pointer: |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10031 | case Type::ConstantArray: |
| 10032 | case Type::VariableArray: |
| 10033 | case Type::IncompleteArray: |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 10034 | case Type::FunctionNoProto: |
| 10035 | case Type::FunctionProto: |
| 10036 | return GCCTypeClass::Pointer; |
| 10037 | |
| 10038 | case Type::MemberPointer: |
| 10039 | return CanTy->isMemberDataPointerType() |
| 10040 | ? GCCTypeClass::PointerToDataMember |
| 10041 | : GCCTypeClass::PointerToMemberFunction; |
| 10042 | |
| 10043 | case Type::Complex: |
| 10044 | return GCCTypeClass::Complex; |
| 10045 | |
| 10046 | case Type::Record: |
| 10047 | return CanTy->isUnionType() ? GCCTypeClass::Union |
| 10048 | : GCCTypeClass::ClassOrStruct; |
| 10049 | |
| 10050 | case Type::Atomic: |
| 10051 | // GCC classifies _Atomic T the same as T. |
| 10052 | return EvaluateBuiltinClassifyType( |
| 10053 | CanTy->castAs<AtomicType>()->getValueType(), LangOpts); |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10054 | |
| 10055 | case Type::BlockPointer: |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10056 | case Type::Vector: |
| 10057 | case Type::ExtVector: |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10058 | case Type::ObjCObject: |
| 10059 | case Type::ObjCInterface: |
| 10060 | case Type::ObjCObjectPointer: |
| 10061 | case Type::Pipe: |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 10062 | // GCC classifies vectors as None. We follow its lead and classify all |
| 10063 | // other types that don't fit into the regular classification the same way. |
| 10064 | return GCCTypeClass::None; |
| 10065 | |
| 10066 | case Type::LValueReference: |
| 10067 | case Type::RValueReference: |
| 10068 | llvm_unreachable("invalid type for expression"); |
| Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 10069 | } |
| 10070 | |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 10071 | llvm_unreachable("unexpected type class"); |
| 10072 | } |
| 10073 | |
| 10074 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 10075 | /// as GCC. |
| 10076 | static GCCTypeClass |
| 10077 | EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) { |
| 10078 | // If no argument was supplied, default to None. This isn't |
| 10079 | // ideal, however it is what gcc does. |
| 10080 | if (E->getNumArgs() == 0) |
| 10081 | return GCCTypeClass::None; |
| 10082 | |
| 10083 | // FIXME: Bizarrely, GCC treats a call with more than one argument as not |
| 10084 | // being an ICE, but still folds it to a constant using the type of the first |
| 10085 | // argument. |
| 10086 | return EvaluateBuiltinClassifyType(E->getArg(0)->getType(), LangOpts); |
| Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 10087 | } |
| 10088 | |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10089 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10090 | /// __builtin_constant_p when applied to the given pointer. |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10091 | /// |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10092 | /// A pointer is only "constant" if it is null (or a pointer cast to integer) |
| 10093 | /// or it points to the first character of a string literal. |
| 10094 | static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) { |
| 10095 | APValue::LValueBase Base = LV.getLValueBase(); |
| 10096 | if (Base.isNull()) { |
| 10097 | // A null base is acceptable. |
| 10098 | return true; |
| 10099 | } else if (const Expr *E = Base.dyn_cast<const Expr *>()) { |
| 10100 | if (!isa<StringLiteral>(E)) |
| 10101 | return false; |
| 10102 | return LV.getLValueOffset().isZero(); |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 10103 | } else if (Base.is<TypeInfoLValue>()) { |
| 10104 | // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to |
| 10105 | // evaluate to true. |
| 10106 | return true; |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10107 | } else { |
| 10108 | // Any other base is not constant enough for GCC. |
| 10109 | return false; |
| 10110 | } |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10111 | } |
| 10112 | |
| 10113 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 10114 | /// GCC as we can manage. |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10115 | static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) { |
| Richard Smith | 37be336 | 2019-05-04 04:00:45 +0000 | [diff] [blame] | 10116 | // This evaluation is not permitted to have side-effects, so evaluate it in |
| 10117 | // a speculative evaluation context. |
| 10118 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
| 10119 | |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10120 | // Constant-folding is always enabled for the operand of __builtin_constant_p |
| 10121 | // (even when the enclosing evaluation context otherwise requires a strict |
| 10122 | // language-specific constant expression). |
| 10123 | FoldConstant Fold(Info, true); |
| 10124 | |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10125 | QualType ArgType = Arg->getType(); |
| 10126 | |
| 10127 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 10128 | // are not precisely documented, but are as follows: |
| 10129 | // |
| 10130 | // - If the operand is of integral, floating, complex or enumeration type, |
| 10131 | // and can be folded to a known value of that type, it returns 1. |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10132 | // - If the operand can be folded to a pointer to the first character |
| 10133 | // of a string literal (or such a pointer cast to an integral type) |
| 10134 | // or to a null pointer or an integer cast to a pointer, it returns 1. |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10135 | // |
| 10136 | // Otherwise, it returns 0. |
| 10137 | // |
| 10138 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10139 | // its support for this did not work prior to GCC 9 and is not yet well |
| 10140 | // understood. |
| 10141 | if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() || |
| 10142 | ArgType->isAnyComplexType() || ArgType->isPointerType() || |
| 10143 | ArgType->isNullPtrType()) { |
| 10144 | APValue V; |
| 10145 | if (!::EvaluateAsRValue(Info, Arg, V)) { |
| 10146 | Fold.keepDiagnostics(); |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10147 | return false; |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10148 | } |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10149 | |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10150 | // For a pointer (possibly cast to integer), there are special rules. |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 10151 | if (V.getKind() == APValue::LValue) |
| 10152 | return EvaluateBuiltinConstantPForLValue(V); |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10153 | |
| 10154 | // Otherwise, any constant value is good enough. |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 10155 | return V.hasValue(); |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10156 | } |
| 10157 | |
| 10158 | // Anything else isn't considered to be sufficiently constant. |
| 10159 | return false; |
| 10160 | } |
| 10161 | |
| John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 10162 | /// Retrieves the "underlying object type" of the given expression, |
| 10163 | /// as used by __builtin_object_size. |
| George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 10164 | static QualType getObjectType(APValue::LValueBase B) { |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 10165 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 10166 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 10167 | return VD->getType(); |
| Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 10168 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 10169 | if (isa<CompoundLiteralExpr>(E)) |
| 10170 | return E->getType(); |
| Richard Smith | ee0ce302 | 2019-05-17 07:06:46 +0000 | [diff] [blame] | 10171 | } else if (B.is<TypeInfoLValue>()) { |
| 10172 | return B.getTypeInfoType(); |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 10173 | } else if (B.is<DynamicAllocLValue>()) { |
| 10174 | return B.getDynamicAllocType(); |
| John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 10175 | } |
| 10176 | |
| 10177 | return QualType(); |
| 10178 | } |
| 10179 | |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 10180 | /// A more selective version of E->IgnoreParenCasts for |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10181 | /// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only |
| George Burgess IV | b40cd56 | 2015-09-04 22:36:18 +0000 | [diff] [blame] | 10182 | /// to change the type of E. |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 10183 | /// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo` |
| 10184 | /// |
| 10185 | /// Always returns an RValue with a pointer representation. |
| 10186 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
| 10187 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
| 10188 | |
| 10189 | auto *NoParens = E->IgnoreParens(); |
| 10190 | auto *Cast = dyn_cast<CastExpr>(NoParens); |
| George Burgess IV | b40cd56 | 2015-09-04 22:36:18 +0000 | [diff] [blame] | 10191 | if (Cast == nullptr) |
| 10192 | return NoParens; |
| 10193 | |
| 10194 | // We only conservatively allow a few kinds of casts, because this code is |
| 10195 | // inherently a simple solution that seeks to support the common case. |
| 10196 | auto CastKind = Cast->getCastKind(); |
| 10197 | if (CastKind != CK_NoOp && CastKind != CK_BitCast && |
| 10198 | CastKind != CK_AddressSpaceConversion) |
| George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 10199 | return NoParens; |
| 10200 | |
| 10201 | auto *SubExpr = Cast->getSubExpr(); |
| 10202 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isRValue()) |
| 10203 | return NoParens; |
| 10204 | return ignorePointerCastsAndParens(SubExpr); |
| 10205 | } |
| 10206 | |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10207 | /// Checks to see if the given LValue's Designator is at the end of the LValue's |
| 10208 | /// record layout. e.g. |
| 10209 | /// struct { struct { int a, b; } fst, snd; } obj; |
| 10210 | /// obj.fst // no |
| 10211 | /// obj.snd // yes |
| 10212 | /// obj.fst.a // no |
| 10213 | /// obj.fst.b // no |
| 10214 | /// obj.snd.a // no |
| 10215 | /// obj.snd.b // yes |
| 10216 | /// |
| 10217 | /// Please note: this function is specialized for how __builtin_object_size |
| 10218 | /// views "objects". |
| George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 10219 | /// |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 10220 | /// If this encounters an invalid RecordDecl or otherwise cannot determine the |
| 10221 | /// correct result, it will always return true. |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10222 | static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) { |
| 10223 | assert(!LVal.Designator.Invalid); |
| 10224 | |
| George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 10225 | auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD, bool &Invalid) { |
| 10226 | const RecordDecl *Parent = FD->getParent(); |
| 10227 | Invalid = Parent->isInvalidDecl(); |
| 10228 | if (Invalid || Parent->isUnion()) |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10229 | return true; |
| George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 10230 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Parent); |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10231 | return FD->getFieldIndex() + 1 == Layout.getFieldCount(); |
| 10232 | }; |
| 10233 | |
| 10234 | auto &Base = LVal.getLValueBase(); |
| 10235 | if (auto *ME = dyn_cast_or_null<MemberExpr>(Base.dyn_cast<const Expr *>())) { |
| 10236 | if (auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 10237 | bool Invalid; |
| 10238 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
| 10239 | return Invalid; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10240 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(ME->getMemberDecl())) { |
| George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 10241 | for (auto *FD : IFD->chain()) { |
| 10242 | bool Invalid; |
| 10243 | if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(FD), Invalid)) |
| 10244 | return Invalid; |
| 10245 | } |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10246 | } |
| 10247 | } |
| 10248 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10249 | unsigned I = 0; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10250 | QualType BaseType = getType(Base); |
| Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 10251 | if (LVal.Designator.FirstEntryIsAnUnsizedArray) { |
| Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 10252 | // If we don't know the array bound, conservatively assume we're looking at |
| 10253 | // the final array element. |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10254 | ++I; |
| Alex Lorenz | 4e24648 | 2017-12-20 21:03:38 +0000 | [diff] [blame] | 10255 | if (BaseType->isIncompleteArrayType()) |
| 10256 | BaseType = Ctx.getAsArrayType(BaseType)->getElementType(); |
| 10257 | else |
| 10258 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10259 | } |
| 10260 | |
| 10261 | for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) { |
| 10262 | const auto &Entry = LVal.Designator.Entries[I]; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10263 | if (BaseType->isArrayType()) { |
| 10264 | // Because __builtin_object_size treats arrays as objects, we can ignore |
| 10265 | // the index iff this is the last array in the Designator. |
| 10266 | if (I + 1 == E) |
| 10267 | return true; |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10268 | const auto *CAT = cast<ConstantArrayType>(Ctx.getAsArrayType(BaseType)); |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 10269 | uint64_t Index = Entry.getAsArrayIndex(); |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10270 | if (Index + 1 != CAT->getSize()) |
| 10271 | return false; |
| 10272 | BaseType = CAT->getElementType(); |
| 10273 | } else if (BaseType->isAnyComplexType()) { |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10274 | const auto *CT = BaseType->castAs<ComplexType>(); |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 10275 | uint64_t Index = Entry.getAsArrayIndex(); |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10276 | if (Index != 1) |
| 10277 | return false; |
| 10278 | BaseType = CT->getElementType(); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10279 | } else if (auto *FD = getAsField(Entry)) { |
| George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 10280 | bool Invalid; |
| 10281 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
| 10282 | return Invalid; |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10283 | BaseType = FD->getType(); |
| 10284 | } else { |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10285 | assert(getAsBaseClass(Entry) && "Expecting cast to a base class"); |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10286 | return false; |
| 10287 | } |
| 10288 | } |
| 10289 | return true; |
| 10290 | } |
| 10291 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10292 | /// Tests to see if the LValue has a user-specified designator (that isn't |
| 10293 | /// necessarily valid). Note that this always returns 'true' if the LValue has |
| 10294 | /// an unsized array as its first designator entry, because there's currently no |
| 10295 | /// way to tell if the user typed *foo or foo[0]. |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10296 | static bool refersToCompleteObject(const LValue &LVal) { |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10297 | if (LVal.Designator.Invalid) |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10298 | return false; |
| 10299 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10300 | if (!LVal.Designator.Entries.empty()) |
| 10301 | return LVal.Designator.isMostDerivedAnUnsizedArray(); |
| 10302 | |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10303 | if (!LVal.InvalidBase) |
| 10304 | return true; |
| 10305 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10306 | // If `E` is a MemberExpr, then the first part of the designator is hiding in |
| 10307 | // the LValueBase. |
| 10308 | const auto *E = LVal.Base.dyn_cast<const Expr *>(); |
| 10309 | return !E || !isa<MemberExpr>(E); |
| George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 10310 | } |
| 10311 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10312 | /// Attempts to detect a user writing into a piece of memory that's impossible |
| 10313 | /// to figure out the size of by just using types. |
| 10314 | static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) { |
| 10315 | const SubobjectDesignator &Designator = LVal.Designator; |
| 10316 | // Notes: |
| 10317 | // - Users can only write off of the end when we have an invalid base. Invalid |
| 10318 | // bases imply we don't know where the memory came from. |
| 10319 | // - We used to be a bit more aggressive here; we'd only be conservative if |
| 10320 | // the array at the end was flexible, or if it had 0 or 1 elements. This |
| 10321 | // broke some common standard library extensions (PR30346), but was |
| 10322 | // otherwise seemingly fine. It may be useful to reintroduce this behavior |
| 10323 | // with some sort of whitelist. OTOH, it seems that GCC is always |
| 10324 | // conservative with the last element in structs (if it's an array), so our |
| 10325 | // current behavior is more compatible than a whitelisting approach would |
| 10326 | // be. |
| 10327 | return LVal.InvalidBase && |
| 10328 | Designator.Entries.size() == Designator.MostDerivedPathLength && |
| 10329 | Designator.MostDerivedIsArrayElement && |
| 10330 | isDesignatorAtObjectEnd(Ctx, LVal); |
| 10331 | } |
| 10332 | |
| 10333 | /// Converts the given APInt to CharUnits, assuming the APInt is unsigned. |
| 10334 | /// Fails if the conversion would cause loss of precision. |
| 10335 | static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int, |
| 10336 | CharUnits &Result) { |
| 10337 | auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max(); |
| 10338 | if (Int.ugt(CharUnitsMax)) |
| 10339 | return false; |
| 10340 | Result = CharUnits::fromQuantity(Int.getZExtValue()); |
| 10341 | return true; |
| 10342 | } |
| 10343 | |
| 10344 | /// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will |
| 10345 | /// determine how many bytes exist from the beginning of the object to either |
| 10346 | /// the end of the current subobject, or the end of the object itself, depending |
| 10347 | /// on what the LValue looks like + the value of Type. |
| George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 10348 | /// |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10349 | /// If this returns false, the value of Result is undefined. |
| 10350 | static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc, |
| 10351 | unsigned Type, const LValue &LVal, |
| 10352 | CharUnits &EndOffset) { |
| 10353 | bool DetermineForCompleteObject = refersToCompleteObject(LVal); |
| Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 10354 | |
| George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 10355 | auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { |
| 10356 | if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType()) |
| 10357 | return false; |
| 10358 | return HandleSizeof(Info, ExprLoc, Ty, Result); |
| 10359 | }; |
| 10360 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10361 | // We want to evaluate the size of the entire object. This is a valid fallback |
| 10362 | // for when Type=1 and the designator is invalid, because we're asked for an |
| 10363 | // upper-bound. |
| 10364 | if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) { |
| 10365 | // Type=3 wants a lower bound, so we can't fall back to this. |
| 10366 | if (Type == 3 && !DetermineForCompleteObject) |
| George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 10367 | return false; |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10368 | |
| 10369 | llvm::APInt APEndOffset; |
| 10370 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 10371 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
| 10372 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
| 10373 | |
| 10374 | if (LVal.InvalidBase) |
| 10375 | return false; |
| 10376 | |
| 10377 | QualType BaseTy = getObjectType(LVal.getLValueBase()); |
| George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 10378 | return CheckedHandleSizeof(BaseTy, EndOffset); |
| George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 10379 | } |
| 10380 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10381 | // We want to evaluate the size of a subobject. |
| 10382 | const SubobjectDesignator &Designator = LVal.Designator; |
| Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 10383 | |
| 10384 | // The following is a moderately common idiom in C: |
| 10385 | // |
| 10386 | // struct Foo { int a; char c[1]; }; |
| 10387 | // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar)); |
| 10388 | // strcpy(&F->c[0], Bar); |
| 10389 | // |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10390 | // In order to not break too much legacy code, we need to support it. |
| 10391 | if (isUserWritingOffTheEnd(Info.Ctx, LVal)) { |
| 10392 | // If we can resolve this to an alloc_size call, we can hand that back, |
| 10393 | // because we know for certain how many bytes there are to write to. |
| 10394 | llvm::APInt APEndOffset; |
| 10395 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 10396 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
| 10397 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
| 10398 | |
| 10399 | // If we cannot determine the size of the initial allocation, then we can't |
| 10400 | // given an accurate upper-bound. However, we are still able to give |
| 10401 | // conservative lower-bounds for Type=3. |
| 10402 | if (Type == 1) |
| 10403 | return false; |
| 10404 | } |
| 10405 | |
| 10406 | CharUnits BytesPerElem; |
| George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 10407 | if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem)) |
| Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 10408 | return false; |
| 10409 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10410 | // According to the GCC documentation, we want the size of the subobject |
| 10411 | // denoted by the pointer. But that's not quite right -- what we actually |
| 10412 | // want is the size of the immediately-enclosing array, if there is one. |
| 10413 | int64_t ElemsRemaining; |
| 10414 | if (Designator.MostDerivedIsArrayElement && |
| 10415 | Designator.Entries.size() == Designator.MostDerivedPathLength) { |
| 10416 | uint64_t ArraySize = Designator.getMostDerivedArraySize(); |
| Richard Smith | 5b5e27a | 2019-05-10 20:05:31 +0000 | [diff] [blame] | 10417 | uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex(); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10418 | ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex; |
| 10419 | } else { |
| 10420 | ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1; |
| 10421 | } |
| Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 10422 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10423 | EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining; |
| 10424 | return true; |
| Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 10425 | } |
| 10426 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 10427 | /// Tries to evaluate the __builtin_object_size for @p E. If successful, |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10428 | /// returns true and stores the result in @p Size. |
| 10429 | /// |
| 10430 | /// If @p WasError is non-null, this will report whether the failure to evaluate |
| 10431 | /// is to be treated as an Error in IntExprEvaluator. |
| 10432 | static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, |
| 10433 | EvalInfo &Info, uint64_t &Size) { |
| 10434 | // Determine the denoted object. |
| 10435 | LValue LVal; |
| 10436 | { |
| 10437 | // The operand of __builtin_object_size is never evaluated for side-effects. |
| 10438 | // If there are any, but we can determine the pointed-to object anyway, then |
| 10439 | // ignore the side-effects. |
| 10440 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
| James Y Knight | 892b09b | 2018-10-10 02:53:43 +0000 | [diff] [blame] | 10441 | IgnoreSideEffectsRAII Fold(Info); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10442 | |
| 10443 | if (E->isGLValue()) { |
| 10444 | // It's possible for us to be given GLValues if we're called via |
| 10445 | // Expr::tryEvaluateObjectSize. |
| 10446 | APValue RVal; |
| 10447 | if (!EvaluateAsRValue(Info, E, RVal)) |
| 10448 | return false; |
| 10449 | LVal.setFrom(Info.Ctx, RVal); |
| George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 10450 | } else if (!EvaluatePointer(ignorePointerCastsAndParens(E), LVal, Info, |
| 10451 | /*InvalidBaseOK=*/true)) |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10452 | return false; |
| 10453 | } |
| 10454 | |
| 10455 | // If we point to before the start of the object, there are no accessible |
| 10456 | // bytes. |
| 10457 | if (LVal.getLValueOffset().isNegative()) { |
| 10458 | Size = 0; |
| 10459 | return true; |
| 10460 | } |
| 10461 | |
| 10462 | CharUnits EndOffset; |
| 10463 | if (!determineEndOffset(Info, E->getExprLoc(), Type, LVal, EndOffset)) |
| 10464 | return false; |
| 10465 | |
| 10466 | // If we've fallen outside of the end offset, just pretend there's nothing to |
| 10467 | // write to/read from. |
| 10468 | if (EndOffset <= LVal.getLValueOffset()) |
| 10469 | Size = 0; |
| 10470 | else |
| 10471 | Size = (EndOffset - LVal.getLValueOffset()).getQuantity(); |
| 10472 | return true; |
| John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 10473 | } |
| 10474 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 10475 | bool IntExprEvaluator::VisitConstantExpr(const ConstantExpr *E) { |
| 10476 | llvm::SaveAndRestore<bool> InConstantContext(Info.InConstantContext, true); |
| Gauthier Harnisch | dea9d57 | 2019-06-21 08:26:21 +0000 | [diff] [blame] | 10477 | if (E->getResultAPValueKind() != APValue::None) |
| 10478 | return Success(E->getAPValueResult(), E); |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 10479 | return ExprEvaluatorBaseTy::VisitConstantExpr(E); |
| 10480 | } |
| 10481 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10482 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
| Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 10483 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
| 10484 | return VisitBuiltinCallExpr(E, BuiltinOp); |
| 10485 | |
| 10486 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 10487 | } |
| 10488 | |
| 10489 | bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
| 10490 | unsigned BuiltinOp) { |
| Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 10491 | switch (unsigned BuiltinOp = E->getBuiltinCallee()) { |
| Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 10492 | default: |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10493 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 10494 | |
| Erik Pilkington | 9c3b588 | 2019-01-30 20:34:53 +0000 | [diff] [blame] | 10495 | case Builtin::BI__builtin_dynamic_object_size: |
| Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 10496 | case Builtin::BI__builtin_object_size: { |
| George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 10497 | // The type was checked when we built the expression. |
| 10498 | unsigned Type = |
| 10499 | E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 10500 | assert(Type <= 3 && "unexpected type"); |
| 10501 | |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10502 | uint64_t Size; |
| 10503 | if (tryEvaluateBuiltinObjectSize(E->getArg(0), Type, Info, Size)) |
| 10504 | return Success(Size, E); |
| Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 10505 | |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10506 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) |
| George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 10507 | return Success((Type & 2) ? 0 : -1, E); |
| Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 10508 | |
| Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 10509 | // Expression had no side effects, but we couldn't statically determine the |
| 10510 | // size of the referenced object. |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10511 | switch (Info.EvalMode) { |
| 10512 | case EvalInfo::EM_ConstantExpression: |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10513 | case EvalInfo::EM_ConstantFold: |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10514 | case EvalInfo::EM_IgnoreSideEffects: |
| George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 10515 | // Leave it to IR generation. |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10516 | return Error(E); |
| 10517 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
| George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 10518 | // Reduce it to a constant now. |
| 10519 | return Success((Type & 2) ? 0 : -1, E); |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10520 | } |
| Richard Smith | cb2ba5a | 2016-07-18 22:37:35 +0000 | [diff] [blame] | 10521 | |
| 10522 | llvm_unreachable("unexpected EvalMode"); |
| Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 10523 | } |
| 10524 | |
| Tim Northover | 314fbfa | 2018-11-02 13:14:11 +0000 | [diff] [blame] | 10525 | case Builtin::BI__builtin_os_log_format_buffer_size: { |
| 10526 | analyze_os_log::OSLogBufferLayout Layout; |
| 10527 | analyze_os_log::computeOSLogBufferLayout(Info.Ctx, E, Layout); |
| 10528 | return Success(Layout.size().getQuantity(), E); |
| 10529 | } |
| 10530 | |
| Benjamin Kramer | a801f4a | 2012-10-06 14:42:22 +0000 | [diff] [blame] | 10531 | case Builtin::BI__builtin_bswap16: |
| Richard Smith | 80ac9ef | 2012-09-28 20:20:52 +0000 | [diff] [blame] | 10532 | case Builtin::BI__builtin_bswap32: |
| 10533 | case Builtin::BI__builtin_bswap64: { |
| 10534 | APSInt Val; |
| 10535 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 10536 | return false; |
| 10537 | |
| 10538 | return Success(Val.byteSwap(), E); |
| 10539 | } |
| 10540 | |
| Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 10541 | case Builtin::BI__builtin_classify_type: |
| Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 10542 | return Success((int)EvaluateBuiltinClassifyType(E, Info.getLangOpts()), E); |
| Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 10543 | |
| Craig Topper | f95a6d9 | 2018-08-08 22:31:12 +0000 | [diff] [blame] | 10544 | case Builtin::BI__builtin_clrsb: |
| 10545 | case Builtin::BI__builtin_clrsbl: |
| 10546 | case Builtin::BI__builtin_clrsbll: { |
| 10547 | APSInt Val; |
| 10548 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 10549 | return false; |
| 10550 | |
| 10551 | return Success(Val.getBitWidth() - Val.getMinSignedBits(), E); |
| 10552 | } |
| Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 10553 | |
| Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 10554 | case Builtin::BI__builtin_clz: |
| 10555 | case Builtin::BI__builtin_clzl: |
| Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 10556 | case Builtin::BI__builtin_clzll: |
| 10557 | case Builtin::BI__builtin_clzs: { |
| Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 10558 | APSInt Val; |
| 10559 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 10560 | return false; |
| 10561 | if (!Val) |
| 10562 | return Error(E); |
| 10563 | |
| 10564 | return Success(Val.countLeadingZeros(), E); |
| 10565 | } |
| 10566 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 10567 | case Builtin::BI__builtin_constant_p: { |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10568 | const Expr *Arg = E->getArg(0); |
| David Blaikie | 639b3d1 | 2019-05-03 18:11:31 +0000 | [diff] [blame] | 10569 | if (EvaluateBuiltinConstantP(Info, Arg)) |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 10570 | return Success(true, E); |
| David Blaikie | 639b3d1 | 2019-05-03 18:11:31 +0000 | [diff] [blame] | 10571 | if (Info.InConstantContext || Arg->HasSideEffects(Info.Ctx)) { |
| Richard Smith | f7d3048 | 2019-05-02 23:21:28 +0000 | [diff] [blame] | 10572 | // Outside a constant context, eagerly evaluate to false in the presence |
| 10573 | // of side-effects in order to avoid -Wunsequenced false-positives in |
| 10574 | // a branch on __builtin_constant_p(expr). |
| Richard Smith | 31cfb31 | 2019-04-27 02:58:17 +0000 | [diff] [blame] | 10575 | return Success(false, E); |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 10576 | } |
| David Blaikie | 639b3d1 | 2019-05-03 18:11:31 +0000 | [diff] [blame] | 10577 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| 10578 | return false; |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 10579 | } |
| Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 10580 | |
| Eric Fiselier | add16a8 | 2019-04-24 02:23:30 +0000 | [diff] [blame] | 10581 | case Builtin::BI__builtin_is_constant_evaluated: |
| 10582 | return Success(Info.InConstantContext, E); |
| 10583 | |
| Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 10584 | case Builtin::BI__builtin_ctz: |
| 10585 | case Builtin::BI__builtin_ctzl: |
| Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 10586 | case Builtin::BI__builtin_ctzll: |
| 10587 | case Builtin::BI__builtin_ctzs: { |
| Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 10588 | APSInt Val; |
| 10589 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 10590 | return false; |
| 10591 | if (!Val) |
| 10592 | return Error(E); |
| 10593 | |
| 10594 | return Success(Val.countTrailingZeros(), E); |
| 10595 | } |
| 10596 | |
| Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 10597 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 10598 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 10599 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
| 10600 | return Success(Operand, E); |
| 10601 | } |
| 10602 | |
| 10603 | case Builtin::BI__builtin_expect: |
| 10604 | return Visit(E->getArg(0)); |
| 10605 | |
| 10606 | case Builtin::BI__builtin_ffs: |
| 10607 | case Builtin::BI__builtin_ffsl: |
| 10608 | case Builtin::BI__builtin_ffsll: { |
| 10609 | APSInt Val; |
| 10610 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 10611 | return false; |
| 10612 | |
| 10613 | unsigned N = Val.countTrailingZeros(); |
| 10614 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
| 10615 | } |
| 10616 | |
| 10617 | case Builtin::BI__builtin_fpclassify: { |
| 10618 | APFloat Val(0.0); |
| 10619 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
| 10620 | return false; |
| 10621 | unsigned Arg; |
| 10622 | switch (Val.getCategory()) { |
| 10623 | case APFloat::fcNaN: Arg = 0; break; |
| 10624 | case APFloat::fcInfinity: Arg = 1; break; |
| 10625 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
| 10626 | case APFloat::fcZero: Arg = 4; break; |
| 10627 | } |
| 10628 | return Visit(E->getArg(Arg)); |
| 10629 | } |
| 10630 | |
| 10631 | case Builtin::BI__builtin_isinf_sign: { |
| 10632 | APFloat Val(0.0); |
| Richard Smith | ab341c6 | 2013-06-13 06:31:13 +0000 | [diff] [blame] | 10633 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 10634 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
| 10635 | } |
| 10636 | |
| Richard Smith | ea3019d | 2013-10-15 19:07:14 +0000 | [diff] [blame] | 10637 | case Builtin::BI__builtin_isinf: { |
| 10638 | APFloat Val(0.0); |
| 10639 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 10640 | Success(Val.isInfinity() ? 1 : 0, E); |
| 10641 | } |
| 10642 | |
| 10643 | case Builtin::BI__builtin_isfinite: { |
| 10644 | APFloat Val(0.0); |
| 10645 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 10646 | Success(Val.isFinite() ? 1 : 0, E); |
| 10647 | } |
| 10648 | |
| 10649 | case Builtin::BI__builtin_isnan: { |
| 10650 | APFloat Val(0.0); |
| 10651 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 10652 | Success(Val.isNaN() ? 1 : 0, E); |
| 10653 | } |
| 10654 | |
| 10655 | case Builtin::BI__builtin_isnormal: { |
| 10656 | APFloat Val(0.0); |
| 10657 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 10658 | Success(Val.isNormal() ? 1 : 0, E); |
| 10659 | } |
| 10660 | |
| Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 10661 | case Builtin::BI__builtin_parity: |
| 10662 | case Builtin::BI__builtin_parityl: |
| 10663 | case Builtin::BI__builtin_parityll: { |
| 10664 | APSInt Val; |
| 10665 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 10666 | return false; |
| 10667 | |
| 10668 | return Success(Val.countPopulation() % 2, E); |
| 10669 | } |
| 10670 | |
| Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 10671 | case Builtin::BI__builtin_popcount: |
| 10672 | case Builtin::BI__builtin_popcountl: |
| 10673 | case Builtin::BI__builtin_popcountll: { |
| 10674 | APSInt Val; |
| 10675 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 10676 | return false; |
| 10677 | |
| 10678 | return Success(Val.countPopulation(), E); |
| 10679 | } |
| 10680 | |
| Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 10681 | case Builtin::BIstrlen: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10682 | case Builtin::BIwcslen: |
| Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 10683 | // A call to strlen is not a constant expression. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10684 | if (Info.getLangOpts().CPlusPlus11) |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 10685 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10686 | << /*isConstexpr*/0 << /*isConstructor*/0 |
| 10687 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
| Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 10688 | else |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 10689 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 10690 | LLVM_FALLTHROUGH; |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10691 | case Builtin::BI__builtin_strlen: |
| 10692 | case Builtin::BI__builtin_wcslen: { |
| Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 10693 | // As an extension, we support __builtin_strlen() as a constant expression, |
| 10694 | // and support folding strlen() to a constant. |
| 10695 | LValue String; |
| 10696 | if (!EvaluatePointer(E->getArg(0), String, Info)) |
| 10697 | return false; |
| 10698 | |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10699 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
| 10700 | |
| Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 10701 | // Fast path: if it's a string literal, search the string value. |
| 10702 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
| 10703 | String.getLValueBase().dyn_cast<const Expr *>())) { |
| Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 10704 | // The string literal may have embedded null characters. Find the first |
| 10705 | // one and truncate there. |
| Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 10706 | StringRef Str = S->getBytes(); |
| 10707 | int64_t Off = String.Offset.getQuantity(); |
| 10708 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10709 | S->getCharByteWidth() == 1 && |
| 10710 | // FIXME: Add fast-path for wchar_t too. |
| 10711 | Info.Ctx.hasSameUnqualifiedType(CharTy, Info.Ctx.CharTy)) { |
| Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 10712 | Str = Str.substr(Off); |
| 10713 | |
| 10714 | StringRef::size_type Pos = Str.find(0); |
| 10715 | if (Pos != StringRef::npos) |
| 10716 | Str = Str.substr(0, Pos); |
| 10717 | |
| 10718 | return Success(Str.size(), E); |
| 10719 | } |
| 10720 | |
| 10721 | // Fall through to slow path to issue appropriate diagnostic. |
| Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 10722 | } |
| Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 10723 | |
| 10724 | // Slow path: scan the bytes of the string looking for the terminating 0. |
| Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 10725 | for (uint64_t Strlen = 0; /**/; ++Strlen) { |
| 10726 | APValue Char; |
| 10727 | if (!handleLValueToRValueConversion(Info, E, CharTy, String, Char) || |
| 10728 | !Char.isInt()) |
| 10729 | return false; |
| 10730 | if (!Char.getInt()) |
| 10731 | return Success(Strlen, E); |
| 10732 | if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1)) |
| 10733 | return false; |
| 10734 | } |
| 10735 | } |
| Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 10736 | |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10737 | case Builtin::BIstrcmp: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10738 | case Builtin::BIwcscmp: |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10739 | case Builtin::BIstrncmp: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10740 | case Builtin::BIwcsncmp: |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10741 | case Builtin::BImemcmp: |
| Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 10742 | case Builtin::BIbcmp: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10743 | case Builtin::BIwmemcmp: |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10744 | // A call to strlen is not a constant expression. |
| 10745 | if (Info.getLangOpts().CPlusPlus11) |
| 10746 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| 10747 | << /*isConstexpr*/0 << /*isConstructor*/0 |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10748 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10749 | else |
| 10750 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 10751 | LLVM_FALLTHROUGH; |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10752 | case Builtin::BI__builtin_strcmp: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10753 | case Builtin::BI__builtin_wcscmp: |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10754 | case Builtin::BI__builtin_strncmp: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10755 | case Builtin::BI__builtin_wcsncmp: |
| 10756 | case Builtin::BI__builtin_memcmp: |
| Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 10757 | case Builtin::BI__builtin_bcmp: |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10758 | case Builtin::BI__builtin_wmemcmp: { |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10759 | LValue String1, String2; |
| 10760 | if (!EvaluatePointer(E->getArg(0), String1, Info) || |
| 10761 | !EvaluatePointer(E->getArg(1), String2, Info)) |
| 10762 | return false; |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10763 | |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10764 | uint64_t MaxLength = uint64_t(-1); |
| 10765 | if (BuiltinOp != Builtin::BIstrcmp && |
| Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 10766 | BuiltinOp != Builtin::BIwcscmp && |
| 10767 | BuiltinOp != Builtin::BI__builtin_strcmp && |
| 10768 | BuiltinOp != Builtin::BI__builtin_wcscmp) { |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10769 | APSInt N; |
| 10770 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
| 10771 | return false; |
| 10772 | MaxLength = N.getExtValue(); |
| 10773 | } |
| Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 10774 | |
| 10775 | // Empty substrings compare equal by definition. |
| 10776 | if (MaxLength == 0u) |
| 10777 | return Success(0, E); |
| 10778 | |
| 10779 | if (!String1.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
| 10780 | !String2.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
| 10781 | String1.Designator.Invalid || String2.Designator.Invalid) |
| 10782 | return false; |
| 10783 | |
| 10784 | QualType CharTy1 = String1.Designator.getType(Info.Ctx); |
| 10785 | QualType CharTy2 = String2.Designator.getType(Info.Ctx); |
| 10786 | |
| 10787 | bool IsRawByte = BuiltinOp == Builtin::BImemcmp || |
| Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 10788 | BuiltinOp == Builtin::BIbcmp || |
| 10789 | BuiltinOp == Builtin::BI__builtin_memcmp || |
| 10790 | BuiltinOp == Builtin::BI__builtin_bcmp; |
| Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 10791 | |
| 10792 | assert(IsRawByte || |
| 10793 | (Info.Ctx.hasSameUnqualifiedType( |
| 10794 | CharTy1, E->getArg(0)->getType()->getPointeeType()) && |
| 10795 | Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))); |
| 10796 | |
| 10797 | const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) { |
| 10798 | return handleLValueToRValueConversion(Info, E, CharTy1, String1, Char1) && |
| 10799 | handleLValueToRValueConversion(Info, E, CharTy2, String2, Char2) && |
| 10800 | Char1.isInt() && Char2.isInt(); |
| 10801 | }; |
| 10802 | const auto &AdvanceElems = [&] { |
| 10803 | return HandleLValueArrayAdjustment(Info, E, String1, CharTy1, 1) && |
| 10804 | HandleLValueArrayAdjustment(Info, E, String2, CharTy2, 1); |
| 10805 | }; |
| 10806 | |
| 10807 | if (IsRawByte) { |
| 10808 | uint64_t BytesRemaining = MaxLength; |
| 10809 | // Pointers to const void may point to objects of incomplete type. |
| 10810 | if (CharTy1->isIncompleteType()) { |
| 10811 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy1; |
| 10812 | return false; |
| 10813 | } |
| 10814 | if (CharTy2->isIncompleteType()) { |
| 10815 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy2; |
| 10816 | return false; |
| 10817 | } |
| 10818 | uint64_t CharTy1Width{Info.Ctx.getTypeSize(CharTy1)}; |
| 10819 | CharUnits CharTy1Size = Info.Ctx.toCharUnitsFromBits(CharTy1Width); |
| 10820 | // Give up on comparing between elements with disparate widths. |
| 10821 | if (CharTy1Size != Info.Ctx.getTypeSizeInChars(CharTy2)) |
| 10822 | return false; |
| 10823 | uint64_t BytesPerElement = CharTy1Size.getQuantity(); |
| 10824 | assert(BytesRemaining && "BytesRemaining should not be zero: the " |
| 10825 | "following loop considers at least one element"); |
| 10826 | while (true) { |
| 10827 | APValue Char1, Char2; |
| 10828 | if (!ReadCurElems(Char1, Char2)) |
| 10829 | return false; |
| 10830 | // We have compatible in-memory widths, but a possible type and |
| 10831 | // (for `bool`) internal representation mismatch. |
| 10832 | // Assuming two's complement representation, including 0 for `false` and |
| 10833 | // 1 for `true`, we can check an appropriate number of elements for |
| 10834 | // equality even if they are not byte-sized. |
| 10835 | APSInt Char1InMem = Char1.getInt().extOrTrunc(CharTy1Width); |
| 10836 | APSInt Char2InMem = Char2.getInt().extOrTrunc(CharTy1Width); |
| 10837 | if (Char1InMem.ne(Char2InMem)) { |
| 10838 | // If the elements are byte-sized, then we can produce a three-way |
| 10839 | // comparison result in a straightforward manner. |
| 10840 | if (BytesPerElement == 1u) { |
| 10841 | // memcmp always compares unsigned chars. |
| 10842 | return Success(Char1InMem.ult(Char2InMem) ? -1 : 1, E); |
| 10843 | } |
| 10844 | // The result is byte-order sensitive, and we have multibyte elements. |
| 10845 | // FIXME: We can compare the remaining bytes in the correct order. |
| 10846 | return false; |
| 10847 | } |
| 10848 | if (!AdvanceElems()) |
| 10849 | return false; |
| 10850 | if (BytesRemaining <= BytesPerElement) |
| 10851 | break; |
| 10852 | BytesRemaining -= BytesPerElement; |
| 10853 | } |
| 10854 | // Enough elements are equal to account for the memcmp limit. |
| 10855 | return Success(0, E); |
| 10856 | } |
| 10857 | |
| Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 10858 | bool StopAtNull = |
| 10859 | (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp && |
| 10860 | BuiltinOp != Builtin::BIwmemcmp && |
| 10861 | BuiltinOp != Builtin::BI__builtin_memcmp && |
| 10862 | BuiltinOp != Builtin::BI__builtin_bcmp && |
| 10863 | BuiltinOp != Builtin::BI__builtin_wmemcmp); |
| Benjamin Kramer | 33b7092 | 2018-04-23 22:04:34 +0000 | [diff] [blame] | 10864 | bool IsWide = BuiltinOp == Builtin::BIwcscmp || |
| 10865 | BuiltinOp == Builtin::BIwcsncmp || |
| 10866 | BuiltinOp == Builtin::BIwmemcmp || |
| 10867 | BuiltinOp == Builtin::BI__builtin_wcscmp || |
| 10868 | BuiltinOp == Builtin::BI__builtin_wcsncmp || |
| 10869 | BuiltinOp == Builtin::BI__builtin_wmemcmp; |
| Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 10870 | |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10871 | for (; MaxLength; --MaxLength) { |
| 10872 | APValue Char1, Char2; |
| Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 10873 | if (!ReadCurElems(Char1, Char2)) |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10874 | return false; |
| Benjamin Kramer | 33b7092 | 2018-04-23 22:04:34 +0000 | [diff] [blame] | 10875 | if (Char1.getInt() != Char2.getInt()) { |
| 10876 | if (IsWide) // wmemcmp compares with wchar_t signedness. |
| 10877 | return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E); |
| 10878 | // memcmp always compares unsigned chars. |
| 10879 | return Success(Char1.getInt().ult(Char2.getInt()) ? -1 : 1, E); |
| 10880 | } |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10881 | if (StopAtNull && !Char1.getInt()) |
| 10882 | return Success(0, E); |
| 10883 | assert(!(StopAtNull && !Char2.getInt())); |
| Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 10884 | if (!AdvanceElems()) |
| Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 10885 | return false; |
| 10886 | } |
| 10887 | // We hit the strncmp / memcmp limit. |
| 10888 | return Success(0, E); |
| 10889 | } |
| 10890 | |
| Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 10891 | case Builtin::BI__atomic_always_lock_free: |
| Richard Smith | b1e36c6 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 10892 | case Builtin::BI__atomic_is_lock_free: |
| 10893 | case Builtin::BI__c11_atomic_is_lock_free: { |
| Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 10894 | APSInt SizeVal; |
| 10895 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 10896 | return false; |
| 10897 | |
| 10898 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 10899 | // of two less than the maximum inline atomic width, we know it is |
| 10900 | // lock-free. If the size isn't a power of two, or greater than the |
| 10901 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 10902 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 10903 | // the answer can only be determined at runtime; for example, 16-byte |
| 10904 | // atomics have lock-free implementations on some, but not all, |
| 10905 | // x86-64 processors. |
| 10906 | |
| 10907 | // Check power-of-two. |
| 10908 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
| Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 10909 | if (Size.isPowerOfTwo()) { |
| 10910 | // Check against inlining width. |
| 10911 | unsigned InlineWidthBits = |
| 10912 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 10913 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 10914 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 10915 | Size == CharUnits::One() || |
| 10916 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 10917 | Expr::NPC_NeverValueDependent)) |
| 10918 | // OK, we will inline appropriately-aligned operations of this size, |
| 10919 | // and _Atomic(T) is appropriately-aligned. |
| 10920 | return Success(1, E); |
| Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 10921 | |
| Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 10922 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 10923 | castAs<PointerType>()->getPointeeType(); |
| 10924 | if (!PointeeType->isIncompleteType() && |
| 10925 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 10926 | // OK, we will inline operations on this object. |
| 10927 | return Success(1, E); |
| 10928 | } |
| 10929 | } |
| 10930 | } |
| Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 10931 | |
| Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 10932 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 10933 | Success(0, E) : Error(E); |
| Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 10934 | } |
| Jonas Hahnfeld | 23604a8 | 2017-10-17 14:28:14 +0000 | [diff] [blame] | 10935 | case Builtin::BIomp_is_initial_device: |
| 10936 | // We can decide statically which value the runtime would return if called. |
| 10937 | return Success(Info.getLangOpts().OpenMPIsDevice ? 0 : 1, E); |
| Erich Keane | 0095827 | 2018-06-13 20:43:27 +0000 | [diff] [blame] | 10938 | case Builtin::BI__builtin_add_overflow: |
| 10939 | case Builtin::BI__builtin_sub_overflow: |
| 10940 | case Builtin::BI__builtin_mul_overflow: |
| 10941 | case Builtin::BI__builtin_sadd_overflow: |
| 10942 | case Builtin::BI__builtin_uadd_overflow: |
| 10943 | case Builtin::BI__builtin_uaddl_overflow: |
| 10944 | case Builtin::BI__builtin_uaddll_overflow: |
| 10945 | case Builtin::BI__builtin_usub_overflow: |
| 10946 | case Builtin::BI__builtin_usubl_overflow: |
| 10947 | case Builtin::BI__builtin_usubll_overflow: |
| 10948 | case Builtin::BI__builtin_umul_overflow: |
| 10949 | case Builtin::BI__builtin_umull_overflow: |
| 10950 | case Builtin::BI__builtin_umulll_overflow: |
| 10951 | case Builtin::BI__builtin_saddl_overflow: |
| 10952 | case Builtin::BI__builtin_saddll_overflow: |
| 10953 | case Builtin::BI__builtin_ssub_overflow: |
| 10954 | case Builtin::BI__builtin_ssubl_overflow: |
| 10955 | case Builtin::BI__builtin_ssubll_overflow: |
| 10956 | case Builtin::BI__builtin_smul_overflow: |
| 10957 | case Builtin::BI__builtin_smull_overflow: |
| 10958 | case Builtin::BI__builtin_smulll_overflow: { |
| 10959 | LValue ResultLValue; |
| 10960 | APSInt LHS, RHS; |
| 10961 | |
| 10962 | QualType ResultType = E->getArg(2)->getType()->getPointeeType(); |
| 10963 | if (!EvaluateInteger(E->getArg(0), LHS, Info) || |
| 10964 | !EvaluateInteger(E->getArg(1), RHS, Info) || |
| 10965 | !EvaluatePointer(E->getArg(2), ResultLValue, Info)) |
| 10966 | return false; |
| 10967 | |
| 10968 | APSInt Result; |
| 10969 | bool DidOverflow = false; |
| 10970 | |
| 10971 | // If the types don't have to match, enlarge all 3 to the largest of them. |
| 10972 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
| 10973 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
| 10974 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
| 10975 | bool IsSigned = LHS.isSigned() || RHS.isSigned() || |
| 10976 | ResultType->isSignedIntegerOrEnumerationType(); |
| 10977 | bool AllSigned = LHS.isSigned() && RHS.isSigned() && |
| 10978 | ResultType->isSignedIntegerOrEnumerationType(); |
| 10979 | uint64_t LHSSize = LHS.getBitWidth(); |
| 10980 | uint64_t RHSSize = RHS.getBitWidth(); |
| 10981 | uint64_t ResultSize = Info.Ctx.getTypeSize(ResultType); |
| 10982 | uint64_t MaxBits = std::max(std::max(LHSSize, RHSSize), ResultSize); |
| 10983 | |
| 10984 | // Add an additional bit if the signedness isn't uniformly agreed to. We |
| 10985 | // could do this ONLY if there is a signed and an unsigned that both have |
| 10986 | // MaxBits, but the code to check that is pretty nasty. The issue will be |
| 10987 | // caught in the shrink-to-result later anyway. |
| 10988 | if (IsSigned && !AllSigned) |
| 10989 | ++MaxBits; |
| 10990 | |
| Erich Keane | fc3dfd3 | 2019-05-30 21:35:32 +0000 | [diff] [blame] | 10991 | LHS = APSInt(LHS.extOrTrunc(MaxBits), !IsSigned); |
| 10992 | RHS = APSInt(RHS.extOrTrunc(MaxBits), !IsSigned); |
| Erich Keane | 0095827 | 2018-06-13 20:43:27 +0000 | [diff] [blame] | 10993 | Result = APSInt(MaxBits, !IsSigned); |
| 10994 | } |
| 10995 | |
| 10996 | // Find largest int. |
| 10997 | switch (BuiltinOp) { |
| 10998 | default: |
| 10999 | llvm_unreachable("Invalid value for BuiltinOp"); |
| 11000 | case Builtin::BI__builtin_add_overflow: |
| 11001 | case Builtin::BI__builtin_sadd_overflow: |
| 11002 | case Builtin::BI__builtin_saddl_overflow: |
| 11003 | case Builtin::BI__builtin_saddll_overflow: |
| 11004 | case Builtin::BI__builtin_uadd_overflow: |
| 11005 | case Builtin::BI__builtin_uaddl_overflow: |
| 11006 | case Builtin::BI__builtin_uaddll_overflow: |
| 11007 | Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow) |
| 11008 | : LHS.uadd_ov(RHS, DidOverflow); |
| 11009 | break; |
| 11010 | case Builtin::BI__builtin_sub_overflow: |
| 11011 | case Builtin::BI__builtin_ssub_overflow: |
| 11012 | case Builtin::BI__builtin_ssubl_overflow: |
| 11013 | case Builtin::BI__builtin_ssubll_overflow: |
| 11014 | case Builtin::BI__builtin_usub_overflow: |
| 11015 | case Builtin::BI__builtin_usubl_overflow: |
| 11016 | case Builtin::BI__builtin_usubll_overflow: |
| 11017 | Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow) |
| 11018 | : LHS.usub_ov(RHS, DidOverflow); |
| 11019 | break; |
| 11020 | case Builtin::BI__builtin_mul_overflow: |
| 11021 | case Builtin::BI__builtin_smul_overflow: |
| 11022 | case Builtin::BI__builtin_smull_overflow: |
| 11023 | case Builtin::BI__builtin_smulll_overflow: |
| 11024 | case Builtin::BI__builtin_umul_overflow: |
| 11025 | case Builtin::BI__builtin_umull_overflow: |
| 11026 | case Builtin::BI__builtin_umulll_overflow: |
| 11027 | Result = LHS.isSigned() ? LHS.smul_ov(RHS, DidOverflow) |
| 11028 | : LHS.umul_ov(RHS, DidOverflow); |
| 11029 | break; |
| 11030 | } |
| 11031 | |
| 11032 | // In the case where multiple sizes are allowed, truncate and see if |
| 11033 | // the values are the same. |
| 11034 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
| 11035 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
| 11036 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
| 11037 | // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead, |
| 11038 | // since it will give us the behavior of a TruncOrSelf in the case where |
| 11039 | // its parameter <= its size. We previously set Result to be at least the |
| 11040 | // type-size of the result, so getTypeSize(ResultType) <= Result.BitWidth |
| 11041 | // will work exactly like TruncOrSelf. |
| 11042 | APSInt Temp = Result.extOrTrunc(Info.Ctx.getTypeSize(ResultType)); |
| 11043 | Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType()); |
| 11044 | |
| 11045 | if (!APSInt::isSameValue(Temp, Result)) |
| 11046 | DidOverflow = true; |
| 11047 | Result = Temp; |
| 11048 | } |
| 11049 | |
| 11050 | APValue APV{Result}; |
| Erich Keane | cb54964 | 2018-07-05 15:52:58 +0000 | [diff] [blame] | 11051 | if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) |
| 11052 | return false; |
| Erich Keane | 0095827 | 2018-06-13 20:43:27 +0000 | [diff] [blame] | 11053 | return Success(DidOverflow, E); |
| 11054 | } |
| Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 11055 | } |
| Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 11056 | } |
| Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 11057 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11058 | /// Determine whether this is a pointer past the end of the complete |
| Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 11059 | /// object referred to by the lvalue. |
| 11060 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
| 11061 | const LValue &LV) { |
| 11062 | // A null pointer can be viewed as being "past the end" but we don't |
| 11063 | // choose to look at it that way here. |
| 11064 | if (!LV.getLValueBase()) |
| 11065 | return false; |
| 11066 | |
| 11067 | // If the designator is valid and refers to a subobject, we're not pointing |
| 11068 | // past the end. |
| 11069 | if (!LV.getLValueDesignator().Invalid && |
| 11070 | !LV.getLValueDesignator().isOnePastTheEnd()) |
| 11071 | return false; |
| 11072 | |
| David Majnemer | c378ca5 | 2015-08-29 08:32:55 +0000 | [diff] [blame] | 11073 | // A pointer to an incomplete type might be past-the-end if the type's size is |
| 11074 | // zero. We cannot tell because the type is incomplete. |
| 11075 | QualType Ty = getType(LV.getLValueBase()); |
| 11076 | if (Ty->isIncompleteType()) |
| 11077 | return true; |
| 11078 | |
| Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 11079 | // We're a past-the-end pointer if we point to the byte after the object, |
| 11080 | // no matter what our type or path is. |
| David Majnemer | c378ca5 | 2015-08-29 08:32:55 +0000 | [diff] [blame] | 11081 | auto Size = Ctx.getTypeSizeInChars(Ty); |
| Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 11082 | return LV.getLValueOffset() == Size; |
| 11083 | } |
| 11084 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11085 | namespace { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 11086 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11087 | /// Data recursive integer evaluator of certain binary operators. |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11088 | /// |
| 11089 | /// We use a data recursive algorithm for binary operators so that we are able |
| 11090 | /// to handle extreme cases of chained binary operators without causing stack |
| 11091 | /// overflow. |
| 11092 | class DataRecursiveIntBinOpEvaluator { |
| 11093 | struct EvalResult { |
| 11094 | APValue Val; |
| 11095 | bool Failed; |
| 11096 | |
| 11097 | EvalResult() : Failed(false) { } |
| 11098 | |
| 11099 | void swap(EvalResult &RHS) { |
| 11100 | Val.swap(RHS.Val); |
| 11101 | Failed = RHS.Failed; |
| 11102 | RHS.Failed = false; |
| 11103 | } |
| 11104 | }; |
| 11105 | |
| 11106 | struct Job { |
| 11107 | const Expr *E; |
| 11108 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 11109 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 11110 | |
| David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 11111 | Job() = default; |
| Benjamin Kramer | 33e9760 | 2016-10-21 18:55:07 +0000 | [diff] [blame] | 11112 | Job(Job &&) = default; |
| David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 11113 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11114 | void startSpeculativeEval(EvalInfo &Info) { |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 11115 | SpecEvalRAII = SpeculativeEvaluationRAII(Info); |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11116 | } |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 11117 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11118 | private: |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 11119 | SpeculativeEvaluationRAII SpecEvalRAII; |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11120 | }; |
| 11121 | |
| 11122 | SmallVector<Job, 16> Queue; |
| 11123 | |
| 11124 | IntExprEvaluator &IntEval; |
| 11125 | EvalInfo &Info; |
| 11126 | APValue &FinalResult; |
| 11127 | |
| 11128 | public: |
| 11129 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 11130 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 11131 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11132 | /// True if \param E is a binary operator that we are going to handle |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11133 | /// data recursively. |
| 11134 | /// We handle binary operators that are comma, logical, or that have operands |
| 11135 | /// with integral or enumeration type. |
| 11136 | static bool shouldEnqueue(const BinaryOperator *E) { |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11137 | return E->getOpcode() == BO_Comma || E->isLogicalOp() || |
| 11138 | (E->isRValue() && E->getType()->isIntegralOrEnumerationType() && |
| Richard Smith | 3a09d8b | 2016-06-04 00:22:31 +0000 | [diff] [blame] | 11139 | E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11140 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 11141 | } |
| 11142 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11143 | bool Traverse(const BinaryOperator *E) { |
| 11144 | enqueue(E); |
| 11145 | EvalResult PrevResult; |
| Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 11146 | while (!Queue.empty()) |
| 11147 | process(PrevResult); |
| 11148 | |
| 11149 | if (PrevResult.Failed) return false; |
| Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 11150 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11151 | FinalResult.swap(PrevResult.Val); |
| 11152 | return true; |
| 11153 | } |
| 11154 | |
| 11155 | private: |
| 11156 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 11157 | return IntEval.Success(Value, E, Result); |
| 11158 | } |
| 11159 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 11160 | return IntEval.Success(Value, E, Result); |
| 11161 | } |
| 11162 | bool Error(const Expr *E) { |
| 11163 | return IntEval.Error(E); |
| 11164 | } |
| 11165 | bool Error(const Expr *E, diag::kind D) { |
| 11166 | return IntEval.Error(E, D); |
| 11167 | } |
| 11168 | |
| 11169 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 11170 | return Info.CCEDiag(E, D); |
| 11171 | } |
| 11172 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 11173 | // Returns true if visiting the RHS is necessary, false otherwise. |
| Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 11174 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11175 | bool &SuppressRHSDiags); |
| 11176 | |
| 11177 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 11178 | const BinaryOperator *E, APValue &Result); |
| 11179 | |
| 11180 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 11181 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 11182 | if (Result.Failed) |
| 11183 | Result.Val = APValue(); |
| 11184 | } |
| 11185 | |
| Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 11186 | void process(EvalResult &Result); |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11187 | |
| 11188 | void enqueue(const Expr *E) { |
| 11189 | E = E->IgnoreParens(); |
| 11190 | Queue.resize(Queue.size()+1); |
| 11191 | Queue.back().E = E; |
| 11192 | Queue.back().Kind = Job::AnyExprKind; |
| 11193 | } |
| 11194 | }; |
| 11195 | |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 11196 | } |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11197 | |
| 11198 | bool DataRecursiveIntBinOpEvaluator:: |
| Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 11199 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11200 | bool &SuppressRHSDiags) { |
| 11201 | if (E->getOpcode() == BO_Comma) { |
| 11202 | // Ignore LHS but note if we could not evaluate it. |
| 11203 | if (LHSResult.Failed) |
| Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 11204 | return Info.noteSideEffect(); |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11205 | return true; |
| 11206 | } |
| Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 11207 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11208 | if (E->isLogicalOp()) { |
| Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 11209 | bool LHSAsBool; |
| 11210 | if (!LHSResult.Failed && HandleConversionToBool(LHSResult.Val, LHSAsBool)) { |
| Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 11211 | // We were able to evaluate the LHS, see if we can get away with not |
| 11212 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
| Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 11213 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
| 11214 | Success(LHSAsBool, E, LHSResult.Val); |
| Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 11215 | return false; // Ignore RHS |
| Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 11216 | } |
| 11217 | } else { |
| Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 11218 | LHSResult.Failed = true; |
| 11219 | |
| Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 11220 | // Since we weren't able to evaluate the left hand side, it |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 11221 | // might have had side effects. |
| Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 11222 | if (!Info.noteSideEffect()) |
| 11223 | return false; |
| 11224 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11225 | // We can't evaluate the LHS; however, sometimes the result |
| 11226 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 11227 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 11228 | SuppressRHSDiags = true; |
| 11229 | } |
| Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 11230 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11231 | return true; |
| 11232 | } |
| Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 11233 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11234 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 11235 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 11236 | |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 11237 | if (LHSResult.Failed && !Info.noteFailure()) |
| Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 11238 | return false; // Ignore RHS; |
| 11239 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11240 | return true; |
| 11241 | } |
| Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 11242 | |
| Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 11243 | static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index, |
| 11244 | bool IsSub) { |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 11245 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
| 11246 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
| 11247 | // offsets. |
| 11248 | assert(!LVal.hasLValuePath() && "have designator for integer lvalue"); |
| 11249 | CharUnits &Offset = LVal.getLValueOffset(); |
| 11250 | uint64_t Offset64 = Offset.getQuantity(); |
| 11251 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
| 11252 | Offset = CharUnits::fromQuantity(IsSub ? Offset64 - Index64 |
| 11253 | : Offset64 + Index64); |
| 11254 | } |
| 11255 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11256 | bool DataRecursiveIntBinOpEvaluator:: |
| 11257 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 11258 | const BinaryOperator *E, APValue &Result) { |
| 11259 | if (E->getOpcode() == BO_Comma) { |
| 11260 | if (RHSResult.Failed) |
| 11261 | return false; |
| 11262 | Result = RHSResult.Val; |
| 11263 | return true; |
| 11264 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11265 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11266 | if (E->isLogicalOp()) { |
| 11267 | bool lhsResult, rhsResult; |
| 11268 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 11269 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11270 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11271 | if (LHSIsOK) { |
| 11272 | if (RHSIsOK) { |
| 11273 | if (E->getOpcode() == BO_LOr) |
| 11274 | return Success(lhsResult || rhsResult, E, Result); |
| 11275 | else |
| 11276 | return Success(lhsResult && rhsResult, E, Result); |
| 11277 | } |
| 11278 | } else { |
| 11279 | if (RHSIsOK) { |
| Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 11280 | // We can't evaluate the LHS; however, sometimes the result |
| 11281 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 11282 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11283 | return Success(rhsResult, E, Result); |
| Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 11284 | } |
| 11285 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11286 | |
| Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 11287 | return false; |
| 11288 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11289 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11290 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 11291 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11292 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11293 | if (LHSResult.Failed || RHSResult.Failed) |
| 11294 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11295 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11296 | const APValue &LHSVal = LHSResult.Val; |
| 11297 | const APValue &RHSVal = RHSResult.Val; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11298 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11299 | // Handle cases like (unsigned long)&a + 4. |
| 11300 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 11301 | Result = LHSVal; |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 11302 | addOrSubLValueAsInteger(Result, RHSVal.getInt(), E->getOpcode() == BO_Sub); |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11303 | return true; |
| 11304 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11305 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11306 | // Handle cases like 4 + (unsigned long)&a |
| 11307 | if (E->getOpcode() == BO_Add && |
| 11308 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 11309 | Result = RHSVal; |
| Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 11310 | addOrSubLValueAsInteger(Result, LHSVal.getInt(), /*IsSub*/false); |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11311 | return true; |
| 11312 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11313 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11314 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 11315 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 11316 | if (!LHSVal.getLValueOffset().isZero() || |
| 11317 | !RHSVal.getLValueOffset().isZero()) |
| 11318 | return false; |
| 11319 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 11320 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 11321 | if (!LHSExpr || !RHSExpr) |
| 11322 | return false; |
| 11323 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 11324 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 11325 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 11326 | return false; |
| 11327 | // Make sure both labels come from the same function. |
| 11328 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 11329 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 11330 | return false; |
| 11331 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 11332 | return true; |
| 11333 | } |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 11334 | |
| 11335 | // All the remaining cases expect both operands to be an integer |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11336 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 11337 | return Error(E); |
| Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 11338 | |
| 11339 | // Set up the width and signedness manually, in case it can't be deduced |
| 11340 | // from the operation we're performing. |
| 11341 | // FIXME: Don't do this in the cases where we can deduce it. |
| 11342 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
| 11343 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
| 11344 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
| 11345 | RHSVal.getInt(), Value)) |
| 11346 | return false; |
| 11347 | return Success(Value, E, Result); |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11348 | } |
| 11349 | |
| Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 11350 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11351 | Job &job = Queue.back(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11352 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11353 | switch (job.Kind) { |
| 11354 | case Job::AnyExprKind: { |
| 11355 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 11356 | if (shouldEnqueue(Bop)) { |
| 11357 | job.Kind = Job::BinOpKind; |
| 11358 | enqueue(Bop->getLHS()); |
| Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 11359 | return; |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11360 | } |
| 11361 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11362 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11363 | EvaluateExpr(job.E, Result); |
| 11364 | Queue.pop_back(); |
| Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 11365 | return; |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11366 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11367 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11368 | case Job::BinOpKind: { |
| 11369 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11370 | bool SuppressRHSDiags = false; |
| Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 11371 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11372 | Queue.pop_back(); |
| Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 11373 | return; |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11374 | } |
| 11375 | if (SuppressRHSDiags) |
| 11376 | job.startSpeculativeEval(Info); |
| Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 11377 | job.LHSResult.swap(Result); |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11378 | job.Kind = Job::BinOpVisitedLHSKind; |
| 11379 | enqueue(Bop->getRHS()); |
| Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 11380 | return; |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11381 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11382 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11383 | case Job::BinOpVisitedLHSKind: { |
| 11384 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 11385 | EvalResult RHS; |
| 11386 | RHS.swap(Result); |
| Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 11387 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11388 | Queue.pop_back(); |
| Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 11389 | return; |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11390 | } |
| 11391 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11392 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11393 | llvm_unreachable("Invalid Job::Kind!"); |
| 11394 | } |
| 11395 | |
| George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 11396 | namespace { |
| 11397 | /// Used when we determine that we should fail, but can keep evaluating prior to |
| 11398 | /// noting that we had a failure. |
| 11399 | class DelayedNoteFailureRAII { |
| 11400 | EvalInfo &Info; |
| 11401 | bool NoteFailure; |
| 11402 | |
| 11403 | public: |
| 11404 | DelayedNoteFailureRAII(EvalInfo &Info, bool NoteFailure = true) |
| 11405 | : Info(Info), NoteFailure(NoteFailure) {} |
| 11406 | ~DelayedNoteFailureRAII() { |
| 11407 | if (NoteFailure) { |
| 11408 | bool ContinueAfterFailure = Info.noteFailure(); |
| 11409 | (void)ContinueAfterFailure; |
| 11410 | assert(ContinueAfterFailure && |
| 11411 | "Shouldn't have kept evaluating on failure."); |
| 11412 | } |
| 11413 | } |
| 11414 | }; |
| 11415 | } |
| 11416 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11417 | template <class SuccessCB, class AfterCB> |
| 11418 | static bool |
| 11419 | EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, |
| 11420 | SuccessCB &&Success, AfterCB &&DoAfter) { |
| 11421 | assert(E->isComparisonOp() && "expected comparison operator"); |
| 11422 | assert((E->getOpcode() == BO_Cmp || |
| 11423 | E->getType()->isIntegralOrEnumerationType()) && |
| 11424 | "unsupported binary expression evaluation"); |
| 11425 | auto Error = [&](const Expr *E) { |
| 11426 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| 11427 | return false; |
| 11428 | }; |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11429 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11430 | using CCR = ComparisonCategoryResult; |
| 11431 | bool IsRelational = E->isRelationalOp(); |
| 11432 | bool IsEquality = E->isEqualityOp(); |
| 11433 | if (E->getOpcode() == BO_Cmp) { |
| 11434 | const ComparisonCategoryInfo &CmpInfo = |
| 11435 | Info.Ctx.CompCategories.getInfoForType(E->getType()); |
| 11436 | IsRelational = CmpInfo.isOrdered(); |
| 11437 | IsEquality = CmpInfo.isEquality(); |
| 11438 | } |
| Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 11439 | |
| Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 11440 | QualType LHSTy = E->getLHS()->getType(); |
| 11441 | QualType RHSTy = E->getRHS()->getType(); |
| Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 11442 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11443 | if (LHSTy->isIntegralOrEnumerationType() && |
| 11444 | RHSTy->isIntegralOrEnumerationType()) { |
| 11445 | APSInt LHS, RHS; |
| 11446 | bool LHSOK = EvaluateInteger(E->getLHS(), LHS, Info); |
| 11447 | if (!LHSOK && !Info.noteFailure()) |
| 11448 | return false; |
| 11449 | if (!EvaluateInteger(E->getRHS(), RHS, Info) || !LHSOK) |
| 11450 | return false; |
| 11451 | if (LHS < RHS) |
| 11452 | return Success(CCR::Less, E); |
| 11453 | if (LHS > RHS) |
| 11454 | return Success(CCR::Greater, E); |
| 11455 | return Success(CCR::Equal, E); |
| 11456 | } |
| 11457 | |
| Leonard Chan | ce1d4f1 | 2019-02-21 20:50:09 +0000 | [diff] [blame] | 11458 | if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) { |
| 11459 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHSTy)); |
| 11460 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHSTy)); |
| 11461 | |
| 11462 | bool LHSOK = EvaluateFixedPointOrInteger(E->getLHS(), LHSFX, Info); |
| 11463 | if (!LHSOK && !Info.noteFailure()) |
| 11464 | return false; |
| 11465 | if (!EvaluateFixedPointOrInteger(E->getRHS(), RHSFX, Info) || !LHSOK) |
| 11466 | return false; |
| 11467 | if (LHSFX < RHSFX) |
| 11468 | return Success(CCR::Less, E); |
| 11469 | if (LHSFX > RHSFX) |
| 11470 | return Success(CCR::Greater, E); |
| 11471 | return Success(CCR::Equal, E); |
| 11472 | } |
| 11473 | |
| Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 11474 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 11475 | ComplexValue LHS, RHS; |
| Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 11476 | bool LHSOK; |
| Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 11477 | if (E->isAssignmentOp()) { |
| 11478 | LValue LV; |
| 11479 | EvaluateLValue(E->getLHS(), LV, Info); |
| 11480 | LHSOK = false; |
| 11481 | } else if (LHSTy->isRealFloatingType()) { |
| Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 11482 | LHSOK = EvaluateFloat(E->getLHS(), LHS.FloatReal, Info); |
| 11483 | if (LHSOK) { |
| 11484 | LHS.makeComplexFloat(); |
| 11485 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
| 11486 | } |
| 11487 | } else { |
| 11488 | LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 11489 | } |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 11490 | if (!LHSOK && !Info.noteFailure()) |
| Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 11491 | return false; |
| 11492 | |
| Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 11493 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 11494 | if (!EvaluateFloat(E->getRHS(), RHS.FloatReal, Info) || !LHSOK) |
| 11495 | return false; |
| 11496 | RHS.makeComplexFloat(); |
| 11497 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
| 11498 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
| Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 11499 | return false; |
| 11500 | |
| 11501 | if (LHS.isComplexFloat()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11502 | APFloat::cmpResult CR_r = |
| Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 11503 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11504 | APFloat::cmpResult CR_i = |
| Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 11505 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11506 | bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual; |
| 11507 | return Success(IsEqual ? CCR::Equal : CCR::Nonequal, E); |
| Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 11508 | } else { |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11509 | assert(IsEquality && "invalid complex comparison"); |
| 11510 | bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 11511 | LHS.getComplexIntImag() == RHS.getComplexIntImag(); |
| 11512 | return Success(IsEqual ? CCR::Equal : CCR::Nonequal, E); |
| Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 11513 | } |
| 11514 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11515 | |
| Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 11516 | if (LHSTy->isRealFloatingType() && |
| 11517 | RHSTy->isRealFloatingType()) { |
| 11518 | APFloat RHS(0.0), LHS(0.0); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11519 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11520 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 11521 | if (!LHSOK && !Info.noteFailure()) |
| Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 11522 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11523 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11524 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
| Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 11525 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11526 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11527 | assert(E->isComparisonOp() && "Invalid binary operator!"); |
| 11528 | auto GetCmpRes = [&]() { |
| 11529 | switch (LHS.compare(RHS)) { |
| 11530 | case APFloat::cmpEqual: |
| 11531 | return CCR::Equal; |
| 11532 | case APFloat::cmpLessThan: |
| 11533 | return CCR::Less; |
| 11534 | case APFloat::cmpGreaterThan: |
| 11535 | return CCR::Greater; |
| 11536 | case APFloat::cmpUnordered: |
| 11537 | return CCR::Unordered; |
| 11538 | } |
| Simon Pilgrim | 3366dcf | 2018-05-08 09:40:32 +0000 | [diff] [blame] | 11539 | llvm_unreachable("Unrecognised APFloat::cmpResult enum"); |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11540 | }; |
| 11541 | return Success(GetCmpRes(), E); |
| Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 11542 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 11543 | |
| Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 11544 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11545 | LValue LHSValue, RHSValue; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11546 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11547 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 11548 | if (!LHSOK && !Info.noteFailure()) |
| 11549 | return false; |
| Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 11550 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11551 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 11552 | return false; |
| Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 11553 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11554 | // Reject differing bases from the normal codepath; we special-case |
| 11555 | // comparisons to null. |
| 11556 | if (!HasSameBase(LHSValue, RHSValue)) { |
| 11557 | // Inequalities and subtractions between unrelated pointers have |
| 11558 | // unspecified or undefined behavior. |
| 11559 | if (!IsEquality) |
| 11560 | return Error(E); |
| 11561 | // A constant address may compare equal to the address of a symbol. |
| 11562 | // The one exception is that address of an object cannot compare equal |
| 11563 | // to a null pointer constant. |
| 11564 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 11565 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
| 11566 | return Error(E); |
| 11567 | // It's implementation-defined whether distinct literals will have |
| 11568 | // distinct addresses. In clang, the result of such a comparison is |
| 11569 | // unspecified, so it is not a constant expression. However, we do know |
| 11570 | // that the address of a literal will be non-null. |
| 11571 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 11572 | LHSValue.Base && RHSValue.Base) |
| 11573 | return Error(E); |
| 11574 | // We can't tell whether weak symbols will end up pointing to the same |
| 11575 | // object. |
| 11576 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
| 11577 | return Error(E); |
| 11578 | // We can't compare the address of the start of one object with the |
| 11579 | // past-the-end address of another object, per C++ DR1652. |
| 11580 | if ((LHSValue.Base && LHSValue.Offset.isZero() && |
| 11581 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) || |
| 11582 | (RHSValue.Base && RHSValue.Offset.isZero() && |
| 11583 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue))) |
| 11584 | return Error(E); |
| 11585 | // We can't tell whether an object is at the same address as another |
| 11586 | // zero sized object. |
| 11587 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
| 11588 | (LHSValue.Base && isZeroSized(RHSValue))) |
| 11589 | return Error(E); |
| 11590 | return Success(CCR::Nonequal, E); |
| 11591 | } |
| Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 11592 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11593 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 11594 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 11595 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11596 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 11597 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 11598 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11599 | // C++11 [expr.rel]p3: |
| 11600 | // Pointers to void (after pointer conversions) can be compared, with a |
| 11601 | // result defined as follows: If both pointers represent the same |
| 11602 | // address or are both the null pointer value, the result is true if the |
| 11603 | // operator is <= or >= and false otherwise; otherwise the result is |
| 11604 | // unspecified. |
| 11605 | // We interpret this as applying to pointers to *cv* void. |
| 11606 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && IsRelational) |
| 11607 | Info.CCEDiag(E, diag::note_constexpr_void_comparison); |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 11608 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11609 | // C++11 [expr.rel]p2: |
| 11610 | // - If two pointers point to non-static data members of the same object, |
| 11611 | // or to subobjects or array elements fo such members, recursively, the |
| 11612 | // pointer to the later declared member compares greater provided the |
| 11613 | // two members have the same access control and provided their class is |
| 11614 | // not a union. |
| 11615 | // [...] |
| 11616 | // - Otherwise pointer comparisons are unspecified. |
| 11617 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) { |
| 11618 | bool WasArrayIndex; |
| 11619 | unsigned Mismatch = FindDesignatorMismatch( |
| 11620 | getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex); |
| 11621 | // At the point where the designators diverge, the comparison has a |
| 11622 | // specified value if: |
| 11623 | // - we are comparing array indices |
| 11624 | // - we are comparing fields of a union, or fields with the same access |
| 11625 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 11626 | // constant expression. |
| 11627 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 11628 | Mismatch < RHSDesignator.Entries.size()) { |
| 11629 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 11630 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 11631 | if (!LF && !RF) |
| 11632 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 11633 | else if (!LF) |
| 11634 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 11635 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 11636 | << RF->getParent() << RF; |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11637 | else if (!RF) |
| 11638 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 11639 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 11640 | << LF->getParent() << LF; |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11641 | else if (!LF->getParent()->isUnion() && |
| 11642 | LF->getAccess() != RF->getAccess()) |
| 11643 | Info.CCEDiag(E, |
| 11644 | diag::note_constexpr_pointer_comparison_differing_access) |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 11645 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 11646 | << LF->getParent(); |
| Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 11647 | } |
| Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 11648 | } |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11649 | |
| 11650 | // The comparison here must be unsigned, and performed with the same |
| 11651 | // width as the pointer. |
| 11652 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 11653 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 11654 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 11655 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 11656 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 11657 | CompareLHS &= Mask; |
| 11658 | CompareRHS &= Mask; |
| 11659 | |
| 11660 | // If there is a base and this is a relational operator, we can only |
| 11661 | // compare pointers within the object in question; otherwise, the result |
| 11662 | // depends on where the object is located in memory. |
| 11663 | if (!LHSValue.Base.isNull() && IsRelational) { |
| 11664 | QualType BaseTy = getType(LHSValue.Base); |
| 11665 | if (BaseTy->isIncompleteType()) |
| 11666 | return Error(E); |
| 11667 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 11668 | uint64_t OffsetLimit = Size.getQuantity(); |
| 11669 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 11670 | return Error(E); |
| 11671 | } |
| 11672 | |
| 11673 | if (CompareLHS < CompareRHS) |
| 11674 | return Success(CCR::Less, E); |
| 11675 | if (CompareLHS > CompareRHS) |
| 11676 | return Success(CCR::Greater, E); |
| 11677 | return Success(CCR::Equal, E); |
| Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 11678 | } |
| Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 11679 | |
| 11680 | if (LHSTy->isMemberPointerType()) { |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11681 | assert(IsEquality && "unexpected member pointer operation"); |
| Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 11682 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 11683 | |
| 11684 | MemberPtr LHSValue, RHSValue; |
| 11685 | |
| 11686 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 11687 | if (!LHSOK && !Info.noteFailure()) |
| Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 11688 | return false; |
| 11689 | |
| 11690 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 11691 | return false; |
| 11692 | |
| 11693 | // C++11 [expr.eq]p2: |
| 11694 | // If both operands are null, they compare equal. Otherwise if only one is |
| 11695 | // null, they compare unequal. |
| 11696 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 11697 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11698 | return Success(Equal ? CCR::Equal : CCR::Nonequal, E); |
| Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 11699 | } |
| 11700 | |
| 11701 | // Otherwise if either is a pointer to a virtual member function, the |
| 11702 | // result is unspecified. |
| 11703 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 11704 | if (MD->isVirtual()) |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11705 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 11706 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 11707 | if (MD->isVirtual()) |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11708 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 11709 | |
| 11710 | // Otherwise they compare equal if and only if they would refer to the |
| 11711 | // same member of the same most derived object or the same subobject if |
| 11712 | // they were dereferenced with a hypothetical object of the associated |
| 11713 | // class type. |
| 11714 | bool Equal = LHSValue == RHSValue; |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11715 | return Success(Equal ? CCR::Equal : CCR::Nonequal, E); |
| Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 11716 | } |
| 11717 | |
| Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 11718 | if (LHSTy->isNullPtrType()) { |
| 11719 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 11720 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 11721 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 11722 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 11723 | // false otherwise. |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11724 | return Success(CCR::Equal, E); |
| Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 11725 | } |
| 11726 | |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11727 | return DoAfter(); |
| 11728 | } |
| 11729 | |
| 11730 | bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) { |
| 11731 | if (!CheckLiteralType(Info, E)) |
| 11732 | return false; |
| 11733 | |
| 11734 | auto OnSuccess = [&](ComparisonCategoryResult ResKind, |
| 11735 | const BinaryOperator *E) { |
| 11736 | // Evaluation succeeded. Lookup the information for the comparison category |
| 11737 | // type and fetch the VarDecl for the result. |
| 11738 | const ComparisonCategoryInfo &CmpInfo = |
| 11739 | Info.Ctx.CompCategories.getInfoForType(E->getType()); |
| 11740 | const VarDecl *VD = |
| 11741 | CmpInfo.getValueInfo(CmpInfo.makeWeakResult(ResKind))->VD; |
| 11742 | // Check and evaluate the result as a constant expression. |
| 11743 | LValue LV; |
| 11744 | LV.set(VD); |
| 11745 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
| 11746 | return false; |
| 11747 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
| 11748 | }; |
| 11749 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
| 11750 | return ExprEvaluatorBaseTy::VisitBinCmp(E); |
| 11751 | }); |
| 11752 | } |
| 11753 | |
| 11754 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 11755 | // We don't call noteFailure immediately because the assignment happens after |
| 11756 | // we evaluate LHS and RHS. |
| 11757 | if (!Info.keepEvaluatingAfterFailure() && E->isAssignmentOp()) |
| 11758 | return Error(E); |
| 11759 | |
| 11760 | DelayedNoteFailureRAII MaybeNoteFailureLater(Info, E->isAssignmentOp()); |
| 11761 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 11762 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
| 11763 | |
| 11764 | assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() || |
| 11765 | !E->getRHS()->getType()->isIntegralOrEnumerationType()) && |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11766 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11767 | |
| 11768 | if (E->isComparisonOp()) { |
| 11769 | // Evaluate builtin binary comparisons by evaluating them as C++2a three-way |
| 11770 | // comparisons and then translating the result. |
| 11771 | auto OnSuccess = [&](ComparisonCategoryResult ResKind, |
| 11772 | const BinaryOperator *E) { |
| 11773 | using CCR = ComparisonCategoryResult; |
| 11774 | bool IsEqual = ResKind == CCR::Equal, |
| 11775 | IsLess = ResKind == CCR::Less, |
| 11776 | IsGreater = ResKind == CCR::Greater; |
| 11777 | auto Op = E->getOpcode(); |
| 11778 | switch (Op) { |
| 11779 | default: |
| 11780 | llvm_unreachable("unsupported binary operator"); |
| 11781 | case BO_EQ: |
| 11782 | case BO_NE: |
| 11783 | return Success(IsEqual == (Op == BO_EQ), E); |
| 11784 | case BO_LT: return Success(IsLess, E); |
| 11785 | case BO_GT: return Success(IsGreater, E); |
| 11786 | case BO_LE: return Success(IsEqual || IsLess, E); |
| 11787 | case BO_GE: return Success(IsEqual || IsGreater, E); |
| 11788 | } |
| 11789 | }; |
| 11790 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
| 11791 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 11792 | }); |
| 11793 | } |
| 11794 | |
| 11795 | QualType LHSTy = E->getLHS()->getType(); |
| 11796 | QualType RHSTy = E->getRHS()->getType(); |
| 11797 | |
| 11798 | if (LHSTy->isPointerType() && RHSTy->isPointerType() && |
| 11799 | E->getOpcode() == BO_Sub) { |
| 11800 | LValue LHSValue, RHSValue; |
| 11801 | |
| 11802 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 11803 | if (!LHSOK && !Info.noteFailure()) |
| 11804 | return false; |
| 11805 | |
| 11806 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 11807 | return false; |
| 11808 | |
| 11809 | // Reject differing bases from the normal codepath; we special-case |
| 11810 | // comparisons to null. |
| 11811 | if (!HasSameBase(LHSValue, RHSValue)) { |
| 11812 | // Handle &&A - &&B. |
| 11813 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 11814 | return Error(E); |
| 11815 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>(); |
| 11816 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>(); |
| 11817 | if (!LHSExpr || !RHSExpr) |
| 11818 | return Error(E); |
| 11819 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 11820 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 11821 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 11822 | return Error(E); |
| 11823 | // Make sure both labels come from the same function. |
| 11824 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 11825 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 11826 | return Error(E); |
| 11827 | return Success(APValue(LHSAddrExpr, RHSAddrExpr), E); |
| 11828 | } |
| 11829 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 11830 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 11831 | |
| 11832 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 11833 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 11834 | |
| 11835 | // C++11 [expr.add]p6: |
| 11836 | // Unless both pointers point to elements of the same array object, or |
| 11837 | // one past the last element of the array object, the behavior is |
| 11838 | // undefined. |
| 11839 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 11840 | !AreElementsOfSameArray(getType(LHSValue.Base), LHSDesignator, |
| 11841 | RHSDesignator)) |
| 11842 | Info.CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 11843 | |
| 11844 | QualType Type = E->getLHS()->getType(); |
| Simon Pilgrim | c15b38e | 2019-10-03 15:08:30 +0000 | [diff] [blame] | 11845 | QualType ElementType = Type->castAs<PointerType>()->getPointeeType(); |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11846 | |
| 11847 | CharUnits ElementSize; |
| 11848 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
| 11849 | return false; |
| 11850 | |
| 11851 | // As an extension, a type may have zero size (empty struct or union in |
| 11852 | // C, array of zero length). Pointer subtraction in such cases has |
| 11853 | // undefined behavior, so is not constant. |
| 11854 | if (ElementSize.isZero()) { |
| 11855 | Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
| 11856 | << ElementType; |
| 11857 | return false; |
| 11858 | } |
| 11859 | |
| 11860 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 11861 | // and produce incorrect results when it overflows. Such behavior |
| 11862 | // appears to be non-conforming, but is common, so perhaps we should |
| 11863 | // assume the standard intended for such cases to be undefined behavior |
| 11864 | // and check for them. |
| 11865 | |
| 11866 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 11867 | // overflow in the final conversion to ptrdiff_t. |
| 11868 | APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 11869 | APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 11870 | APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), |
| 11871 | false); |
| 11872 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 11873 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 11874 | |
| 11875 | if (Result.extend(65) != TrueResult && |
| 11876 | !HandleOverflow(Info, E, TrueResult, E->getType())) |
| 11877 | return false; |
| 11878 | return Success(Result, E); |
| 11879 | } |
| 11880 | |
| Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 11881 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 11882 | } |
| 11883 | |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11884 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 11885 | /// a result as the expression's type. |
| 11886 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 11887 | const UnaryExprOrTypeTraitExpr *E) { |
| 11888 | switch(E->getKind()) { |
| Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 11889 | case UETT_PreferredAlignOf: |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11890 | case UETT_AlignOf: { |
| Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 11891 | if (E->isArgumentType()) |
| Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 11892 | return Success(GetAlignOfType(Info, E->getArgumentType(), E->getKind()), |
| 11893 | E); |
| Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 11894 | else |
| Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 11895 | return Success(GetAlignOfExpr(Info, E->getArgumentExpr(), E->getKind()), |
| 11896 | E); |
| Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 11897 | } |
| Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 11898 | |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11899 | case UETT_VecStep: { |
| 11900 | QualType Ty = E->getTypeOfArgument(); |
| Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 11901 | |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11902 | if (Ty->isVectorType()) { |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 11903 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
| Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 11904 | |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11905 | // The vec_step built-in functions that take a 3-component |
| 11906 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 11907 | if (n == 3) |
| 11908 | n = 4; |
| Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 11909 | |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11910 | return Success(n, E); |
| 11911 | } else |
| 11912 | return Success(1, E); |
| 11913 | } |
| 11914 | |
| 11915 | case UETT_SizeOf: { |
| 11916 | QualType SrcTy = E->getTypeOfArgument(); |
| 11917 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 11918 | // the result is the size of the referenced type." |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11919 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 11920 | SrcTy = Ref->getPointeeType(); |
| 11921 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 11922 | CharUnits Sizeof; |
| Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 11923 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11924 | return false; |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 11925 | return Success(Sizeof, E); |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11926 | } |
| Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 11927 | case UETT_OpenMPRequiredSimdAlign: |
| 11928 | assert(E->isArgumentType()); |
| 11929 | return Success( |
| 11930 | Info.Ctx.toCharUnitsFromBits( |
| 11931 | Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType())) |
| 11932 | .getQuantity(), |
| 11933 | E); |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11934 | } |
| 11935 | |
| 11936 | llvm_unreachable("unknown expr/type trait"); |
| Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 11937 | } |
| 11938 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 11939 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11940 | CharUnits Result; |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 11941 | unsigned n = OOE->getNumComponents(); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11942 | if (n == 0) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11943 | return Error(OOE); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 11944 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11945 | for (unsigned i = 0; i != n; ++i) { |
| James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 11946 | OffsetOfNode ON = OOE->getComponent(i); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11947 | switch (ON.getKind()) { |
| James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 11948 | case OffsetOfNode::Array: { |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 11949 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11950 | APSInt IdxResult; |
| 11951 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 11952 | return false; |
| 11953 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 11954 | if (!AT) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11955 | return Error(OOE); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11956 | CurrentType = AT->getElementType(); |
| 11957 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 11958 | Result += IdxResult.getSExtValue() * ElementSize; |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 11959 | break; |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11960 | } |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11961 | |
| James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 11962 | case OffsetOfNode::Field: { |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11963 | FieldDecl *MemberDecl = ON.getField(); |
| 11964 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11965 | if (!RT) |
| 11966 | return Error(OOE); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11967 | RecordDecl *RD = RT->getDecl(); |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 11968 | if (RD->isInvalidDecl()) return false; |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11969 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 11970 | unsigned i = MemberDecl->getFieldIndex(); |
| Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 11971 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
| Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 11972 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11973 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 11974 | break; |
| 11975 | } |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11976 | |
| James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 11977 | case OffsetOfNode::Identifier: |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 11978 | llvm_unreachable("dependent __builtin_offsetof"); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11979 | |
| James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 11980 | case OffsetOfNode::Base: { |
| Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 11981 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 11982 | if (BaseSpec->isVirtual()) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11983 | return Error(OOE); |
| Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 11984 | |
| 11985 | // Find the layout of the class whose base we are looking into. |
| 11986 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11987 | if (!RT) |
| 11988 | return Error(OOE); |
| Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 11989 | RecordDecl *RD = RT->getDecl(); |
| John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 11990 | if (RD->isInvalidDecl()) return false; |
| Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 11991 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 11992 | |
| 11993 | // Find the base class itself. |
| 11994 | CurrentType = BaseSpec->getType(); |
| 11995 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 11996 | if (!BaseRT) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11997 | return Error(OOE); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11998 | |
| Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 11999 | // Add the offset to the base. |
| Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 12000 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
| Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 12001 | break; |
| 12002 | } |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 12003 | } |
| 12004 | } |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12005 | return Success(Result, OOE); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 12006 | } |
| 12007 | |
| Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 12008 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12009 | switch (E->getOpcode()) { |
| 12010 | default: |
| 12011 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 12012 | // See C99 6.6p3. |
| 12013 | return Error(E); |
| 12014 | case UO_Extension: |
| 12015 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 12016 | // If so, we could clear the diagnostic ID. |
| 12017 | return Visit(E->getSubExpr()); |
| 12018 | case UO_Plus: |
| 12019 | // The result is just the value. |
| 12020 | return Visit(E->getSubExpr()); |
| 12021 | case UO_Minus: { |
| 12022 | if (!Visit(E->getSubExpr())) |
| Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 12023 | return false; |
| 12024 | if (!Result.isInt()) return Error(E); |
| 12025 | const APSInt &Value = Result.getInt(); |
| 12026 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() && |
| 12027 | !HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 12028 | E->getType())) |
| 12029 | return false; |
| Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 12030 | return Success(-Value, E); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12031 | } |
| 12032 | case UO_Not: { |
| 12033 | if (!Visit(E->getSubExpr())) |
| 12034 | return false; |
| 12035 | if (!Result.isInt()) return Error(E); |
| 12036 | return Success(~Result.getInt(), E); |
| 12037 | } |
| 12038 | case UO_LNot: { |
| Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 12039 | bool bres; |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 12040 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
| Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 12041 | return false; |
| Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 12042 | return Success(!bres, E); |
| Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 12043 | } |
| Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 12044 | } |
| Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 12045 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12046 | |
| Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 12047 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 12048 | /// result type is integer. |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12049 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 12050 | const Expr *SubExpr = E->getSubExpr(); |
| Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 12051 | QualType DestType = E->getType(); |
| Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 12052 | QualType SrcType = SubExpr->getType(); |
| Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 12053 | |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12054 | switch (E->getCastKind()) { |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12055 | case CK_BaseToDerived: |
| 12056 | case CK_DerivedToBase: |
| 12057 | case CK_UncheckedDerivedToBase: |
| 12058 | case CK_Dynamic: |
| 12059 | case CK_ToUnion: |
| 12060 | case CK_ArrayToPointerDecay: |
| 12061 | case CK_FunctionToPointerDecay: |
| 12062 | case CK_NullToPointer: |
| 12063 | case CK_NullToMemberPointer: |
| 12064 | case CK_BaseToDerivedMemberPointer: |
| 12065 | case CK_DerivedToBaseMemberPointer: |
| John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 12066 | case CK_ReinterpretMemberPointer: |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12067 | case CK_ConstructorConversion: |
| 12068 | case CK_IntegralToPointer: |
| 12069 | case CK_ToVoid: |
| 12070 | case CK_VectorSplat: |
| 12071 | case CK_IntegralToFloating: |
| 12072 | case CK_FloatingCast: |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 12073 | case CK_CPointerToObjCPointerCast: |
| 12074 | case CK_BlockPointerToObjCPointerCast: |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12075 | case CK_AnyPointerToBlockPointerCast: |
| 12076 | case CK_ObjCObjectLValueCast: |
| 12077 | case CK_FloatingRealToComplex: |
| 12078 | case CK_FloatingComplexToReal: |
| 12079 | case CK_FloatingComplexCast: |
| 12080 | case CK_FloatingComplexToIntegralComplex: |
| 12081 | case CK_IntegralRealToComplex: |
| 12082 | case CK_IntegralComplexCast: |
| 12083 | case CK_IntegralComplexToFloatingComplex: |
| Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 12084 | case CK_BuiltinFnToFnPtr: |
| Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 12085 | case CK_ZeroToOCLOpaqueType: |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 12086 | case CK_NonAtomicToAtomic: |
| David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 12087 | case CK_AddressSpaceConversion: |
| Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 12088 | case CK_IntToOCLSampler: |
| Leonard Chan | 99bda37 | 2018-10-15 16:07:02 +0000 | [diff] [blame] | 12089 | case CK_FixedPointCast: |
| Leonard Chan | 8f7caae | 2019-03-06 00:28:43 +0000 | [diff] [blame] | 12090 | case CK_IntegralToFixedPoint: |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12091 | llvm_unreachable("invalid cast kind for integral value"); |
| 12092 | |
| Eli Friedman | 9faf2f9 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 12093 | case CK_BitCast: |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12094 | case CK_Dependent: |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12095 | case CK_LValueBitCast: |
| John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 12096 | case CK_ARCProduceObject: |
| 12097 | case CK_ARCConsumeObject: |
| 12098 | case CK_ARCReclaimReturnedObject: |
| 12099 | case CK_ARCExtendBlockObject: |
| Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 12100 | case CK_CopyAndAutoreleaseBlockObject: |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12101 | return Error(E); |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12102 | |
| Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 12103 | case CK_UserDefinedConversion: |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12104 | case CK_LValueToRValue: |
| David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 12105 | case CK_AtomicToNonAtomic: |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12106 | case CK_NoOp: |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 12107 | case CK_LValueToRValueBitCast: |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 12108 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12109 | |
| 12110 | case CK_MemberPointerToBoolean: |
| 12111 | case CK_PointerToBoolean: |
| 12112 | case CK_IntegralToBoolean: |
| 12113 | case CK_FloatingToBoolean: |
| George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 12114 | case CK_BooleanToSignedIntegral: |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12115 | case CK_FloatingComplexToBoolean: |
| 12116 | case CK_IntegralComplexToBoolean: { |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12117 | bool BoolResult; |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 12118 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12119 | return false; |
| George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 12120 | uint64_t IntResult = BoolResult; |
| 12121 | if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral) |
| 12122 | IntResult = (uint64_t)-1; |
| 12123 | return Success(IntResult, E); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12124 | } |
| 12125 | |
| Leonard Chan | 8f7caae | 2019-03-06 00:28:43 +0000 | [diff] [blame] | 12126 | case CK_FixedPointToIntegral: { |
| 12127 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SrcType)); |
| 12128 | if (!EvaluateFixedPoint(SubExpr, Src, Info)) |
| 12129 | return false; |
| 12130 | bool Overflowed; |
| 12131 | llvm::APSInt Result = Src.convertToInt( |
| 12132 | Info.Ctx.getIntWidth(DestType), |
| 12133 | DestType->isSignedIntegerOrEnumerationType(), &Overflowed); |
| 12134 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
| 12135 | return false; |
| 12136 | return Success(Result, E); |
| 12137 | } |
| 12138 | |
| Leonard Chan | b4ba467 | 2018-10-23 17:55:35 +0000 | [diff] [blame] | 12139 | case CK_FixedPointToBoolean: { |
| 12140 | // Unsigned padding does not affect this. |
| 12141 | APValue Val; |
| 12142 | if (!Evaluate(Val, Info, SubExpr)) |
| 12143 | return false; |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 12144 | return Success(Val.getFixedPoint().getBoolValue(), E); |
| Leonard Chan | b4ba467 | 2018-10-23 17:55:35 +0000 | [diff] [blame] | 12145 | } |
| 12146 | |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12147 | case CK_IntegralCast: { |
| Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 12148 | if (!Visit(SubExpr)) |
| Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 12149 | return false; |
| Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 12150 | |
| Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 12151 | if (!Result.isInt()) { |
| Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 12152 | // Allow casts of address-of-label differences if they are no-ops |
| 12153 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 12154 | // be constant-evaluatable except in some narrow cases which are hard |
| 12155 | // to detect here. We let it through on the assumption the user knows |
| 12156 | // what they are doing.) |
| 12157 | if (Result.isAddrLabelDiff()) |
| 12158 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
| Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 12159 | // Only allow casts of lvalues if they are lossless. |
| 12160 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 12161 | } |
| Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 12162 | |
| Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 12163 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 12164 | Result.getInt()), E); |
| Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 12165 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12166 | |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12167 | case CK_PointerToIntegral: { |
| Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 12168 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 12169 | |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 12170 | LValue LV; |
| Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 12171 | if (!EvaluatePointer(SubExpr, LV, Info)) |
| Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 12172 | return false; |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12173 | |
| Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 12174 | if (LV.getLValueBase()) { |
| 12175 | // Only allow based lvalue casts if they are lossless. |
| Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 12176 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 12177 | // narrowing back down to pointer width in subsequent integral casts. |
| 12178 | // FIXME: Check integer type's active bits, not its type size. |
| Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 12179 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12180 | return Error(E); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12181 | |
| Richard Smith | cf74da7 | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 12182 | LV.Designator.setInvalid(); |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 12183 | LV.moveInto(Result); |
| Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 12184 | return true; |
| 12185 | } |
| 12186 | |
| Hans Wennborg | dd1ea8a | 2019-03-06 10:26:19 +0000 | [diff] [blame] | 12187 | APSInt AsInt; |
| 12188 | APValue V; |
| 12189 | LV.moveInto(V); |
| 12190 | if (!V.toIntegralConstant(AsInt, SrcType, Info.Ctx)) |
| 12191 | llvm_unreachable("Can't cast this!"); |
| Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 12192 | |
| Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 12193 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
| Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 12194 | } |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12195 | |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12196 | case CK_IntegralComplexToReal: { |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12197 | ComplexValue C; |
| Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 12198 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 12199 | return false; |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12200 | return Success(C.getComplexIntReal(), E); |
| Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 12201 | } |
| Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 12202 | |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12203 | case CK_FloatingToIntegral: { |
| 12204 | APFloat F(0.0); |
| 12205 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 12206 | return false; |
| Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 12207 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 12208 | APSInt Value; |
| 12209 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 12210 | return false; |
| 12211 | return Success(Value, E); |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12212 | } |
| 12213 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12214 | |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12215 | llvm_unreachable("unknown cast resulting in integral value"); |
| Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 12216 | } |
| Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 12217 | |
| Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 12218 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 12219 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12220 | ComplexValue LV; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12221 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 12222 | return false; |
| 12223 | if (!LV.isComplexInt()) |
| 12224 | return Error(E); |
| Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 12225 | return Success(LV.getComplexIntReal(), E); |
| 12226 | } |
| 12227 | |
| 12228 | return Visit(E->getSubExpr()); |
| 12229 | } |
| 12230 | |
| Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 12231 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 12232 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12233 | ComplexValue LV; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12234 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 12235 | return false; |
| 12236 | if (!LV.isComplexInt()) |
| 12237 | return Error(E); |
| Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 12238 | return Success(LV.getComplexIntImag(), E); |
| 12239 | } |
| 12240 | |
| Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 12241 | VisitIgnoredValue(E->getSubExpr()); |
| Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 12242 | return Success(0, E); |
| 12243 | } |
| 12244 | |
| Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 12245 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 12246 | return Success(E->getPackLength(), E); |
| 12247 | } |
| 12248 | |
| Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 12249 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 12250 | return Success(E->getValue(), E); |
| 12251 | } |
| 12252 | |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 12253 | bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 12254 | switch (E->getOpcode()) { |
| 12255 | default: |
| 12256 | // Invalid unary operators |
| 12257 | return Error(E); |
| 12258 | case UO_Plus: |
| 12259 | // The result is just the value. |
| 12260 | return Visit(E->getSubExpr()); |
| 12261 | case UO_Minus: { |
| 12262 | if (!Visit(E->getSubExpr())) return false; |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 12263 | if (!Result.isFixedPoint()) |
| 12264 | return Error(E); |
| 12265 | bool Overflowed; |
| 12266 | APFixedPoint Negated = Result.getFixedPoint().negate(&Overflowed); |
| 12267 | if (Overflowed && !HandleOverflow(Info, E, Negated, E->getType())) |
| 12268 | return false; |
| 12269 | return Success(Negated, E); |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 12270 | } |
| 12271 | case UO_LNot: { |
| 12272 | bool bres; |
| 12273 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
| 12274 | return false; |
| 12275 | return Success(!bres, E); |
| 12276 | } |
| 12277 | } |
| 12278 | } |
| 12279 | |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 12280 | bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 12281 | const Expr *SubExpr = E->getSubExpr(); |
| 12282 | QualType DestType = E->getType(); |
| 12283 | assert(DestType->isFixedPointType() && |
| 12284 | "Expected destination type to be a fixed point type"); |
| 12285 | auto DestFXSema = Info.Ctx.getFixedPointSemantics(DestType); |
| 12286 | |
| 12287 | switch (E->getCastKind()) { |
| 12288 | case CK_FixedPointCast: { |
| 12289 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SubExpr->getType())); |
| 12290 | if (!EvaluateFixedPoint(SubExpr, Src, Info)) |
| 12291 | return false; |
| 12292 | bool Overflowed; |
| 12293 | APFixedPoint Result = Src.convert(DestFXSema, &Overflowed); |
| 12294 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
| 12295 | return false; |
| 12296 | return Success(Result, E); |
| 12297 | } |
| Leonard Chan | 8f7caae | 2019-03-06 00:28:43 +0000 | [diff] [blame] | 12298 | case CK_IntegralToFixedPoint: { |
| 12299 | APSInt Src; |
| 12300 | if (!EvaluateInteger(SubExpr, Src, Info)) |
| 12301 | return false; |
| 12302 | |
| 12303 | bool Overflowed; |
| 12304 | APFixedPoint IntResult = APFixedPoint::getFromIntValue( |
| 12305 | Src, Info.Ctx.getFixedPointSemantics(DestType), &Overflowed); |
| 12306 | |
| 12307 | if (Overflowed && !HandleOverflow(Info, E, IntResult, DestType)) |
| 12308 | return false; |
| 12309 | |
| 12310 | return Success(IntResult, E); |
| 12311 | } |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 12312 | case CK_NoOp: |
| 12313 | case CK_LValueToRValue: |
| 12314 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 12315 | default: |
| 12316 | return Error(E); |
| 12317 | } |
| 12318 | } |
| 12319 | |
| 12320 | bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 12321 | const Expr *LHS = E->getLHS(); |
| 12322 | const Expr *RHS = E->getRHS(); |
| 12323 | FixedPointSemantics ResultFXSema = |
| 12324 | Info.Ctx.getFixedPointSemantics(E->getType()); |
| 12325 | |
| 12326 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHS->getType())); |
| 12327 | if (!EvaluateFixedPointOrInteger(LHS, LHSFX, Info)) |
| 12328 | return false; |
| 12329 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHS->getType())); |
| 12330 | if (!EvaluateFixedPointOrInteger(RHS, RHSFX, Info)) |
| 12331 | return false; |
| 12332 | |
| 12333 | switch (E->getOpcode()) { |
| 12334 | case BO_Add: { |
| 12335 | bool AddOverflow, ConversionOverflow; |
| 12336 | APFixedPoint Result = LHSFX.add(RHSFX, &AddOverflow) |
| 12337 | .convert(ResultFXSema, &ConversionOverflow); |
| 12338 | if ((AddOverflow || ConversionOverflow) && |
| 12339 | !HandleOverflow(Info, E, Result, E->getType())) |
| 12340 | return false; |
| 12341 | return Success(Result, E); |
| 12342 | } |
| 12343 | default: |
| 12344 | return false; |
| 12345 | } |
| 12346 | llvm_unreachable("Should've exited before this"); |
| 12347 | } |
| 12348 | |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 12349 | //===----------------------------------------------------------------------===// |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12350 | // Float Evaluation |
| 12351 | //===----------------------------------------------------------------------===// |
| 12352 | |
| 12353 | namespace { |
| Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 12354 | class FloatExprEvaluator |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 12355 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12356 | APFloat &Result; |
| 12357 | public: |
| 12358 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12359 | : ExprEvaluatorBaseTy(info), Result(result) {} |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12360 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 12361 | bool Success(const APValue &V, const Expr *e) { |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12362 | Result = V.getFloat(); |
| 12363 | return true; |
| 12364 | } |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12365 | |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 12366 | bool ZeroInitialization(const Expr *E) { |
| Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 12367 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 12368 | return true; |
| 12369 | } |
| 12370 | |
| Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 12371 | bool VisitCallExpr(const CallExpr *E); |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12372 | |
| Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 12373 | bool VisitUnaryOperator(const UnaryOperator *E); |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12374 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 12375 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12376 | bool VisitCastExpr(const CastExpr *E); |
| Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 12377 | |
| John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 12378 | bool VisitUnaryReal(const UnaryOperator *E); |
| 12379 | bool VisitUnaryImag(const UnaryOperator *E); |
| Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 12380 | |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 12381 | // FIXME: Missing: array subscript of vector, member of vector |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12382 | }; |
| 12383 | } // end anonymous namespace |
| 12384 | |
| 12385 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 12386 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12387 | return FloatExprEvaluator(Info, Result).Visit(E); |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12388 | } |
| 12389 | |
| Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 12390 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
| John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 12391 | QualType ResultTy, |
| 12392 | const Expr *Arg, |
| 12393 | bool SNaN, |
| 12394 | llvm::APFloat &Result) { |
| 12395 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 12396 | if (!S) return false; |
| 12397 | |
| 12398 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 12399 | |
| 12400 | llvm::APInt fill; |
| 12401 | |
| 12402 | // Treat empty strings as if they were zero. |
| 12403 | if (S->getString().empty()) |
| 12404 | fill = llvm::APInt(32, 0); |
| 12405 | else if (S->getString().getAsInteger(0, fill)) |
| 12406 | return false; |
| 12407 | |
| Petar Jovanovic | d55ae6b | 2015-02-26 18:19:22 +0000 | [diff] [blame] | 12408 | if (Context.getTargetInfo().isNan2008()) { |
| 12409 | if (SNaN) |
| 12410 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 12411 | else |
| 12412 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 12413 | } else { |
| 12414 | // Prior to IEEE 754-2008, architectures were allowed to choose whether |
| 12415 | // the first bit of their significand was set for qNaN or sNaN. MIPS chose |
| 12416 | // a different encoding to what became a standard in 2008, and for pre- |
| 12417 | // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as |
| 12418 | // sNaN. This is now known as "legacy NaN" encoding. |
| 12419 | if (SNaN) |
| 12420 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 12421 | else |
| 12422 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 12423 | } |
| 12424 | |
| John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 12425 | return true; |
| 12426 | } |
| 12427 | |
| Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 12428 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
| Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 12429 | switch (E->getBuiltinCallee()) { |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12430 | default: |
| 12431 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 12432 | |
| Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 12433 | case Builtin::BI__builtin_huge_val: |
| 12434 | case Builtin::BI__builtin_huge_valf: |
| 12435 | case Builtin::BI__builtin_huge_vall: |
| Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 12436 | case Builtin::BI__builtin_huge_valf128: |
| Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 12437 | case Builtin::BI__builtin_inf: |
| 12438 | case Builtin::BI__builtin_inff: |
| Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 12439 | case Builtin::BI__builtin_infl: |
| 12440 | case Builtin::BI__builtin_inff128: { |
| Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 12441 | const llvm::fltSemantics &Sem = |
| 12442 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
| Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 12443 | Result = llvm::APFloat::getInf(Sem); |
| 12444 | return true; |
| Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 12445 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12446 | |
| John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 12447 | case Builtin::BI__builtin_nans: |
| 12448 | case Builtin::BI__builtin_nansf: |
| 12449 | case Builtin::BI__builtin_nansl: |
| Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 12450 | case Builtin::BI__builtin_nansf128: |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12451 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 12452 | true, Result)) |
| 12453 | return Error(E); |
| 12454 | return true; |
| John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 12455 | |
| Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 12456 | case Builtin::BI__builtin_nan: |
| 12457 | case Builtin::BI__builtin_nanf: |
| 12458 | case Builtin::BI__builtin_nanl: |
| Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 12459 | case Builtin::BI__builtin_nanf128: |
| Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 12460 | // If this is __builtin_nan() turn this into a nan, otherwise we |
| Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 12461 | // can't constant fold it. |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12462 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 12463 | false, Result)) |
| 12464 | return Error(E); |
| 12465 | return true; |
| Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 12466 | |
| 12467 | case Builtin::BI__builtin_fabs: |
| 12468 | case Builtin::BI__builtin_fabsf: |
| 12469 | case Builtin::BI__builtin_fabsl: |
| Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 12470 | case Builtin::BI__builtin_fabsf128: |
| Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 12471 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 12472 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12473 | |
| Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 12474 | if (Result.isNegative()) |
| 12475 | Result.changeSign(); |
| 12476 | return true; |
| 12477 | |
| Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 12478 | // FIXME: Builtin::BI__builtin_powi |
| 12479 | // FIXME: Builtin::BI__builtin_powif |
| 12480 | // FIXME: Builtin::BI__builtin_powil |
| 12481 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12482 | case Builtin::BI__builtin_copysign: |
| 12483 | case Builtin::BI__builtin_copysignf: |
| Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 12484 | case Builtin::BI__builtin_copysignl: |
| 12485 | case Builtin::BI__builtin_copysignf128: { |
| Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 12486 | APFloat RHS(0.); |
| 12487 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 12488 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 12489 | return false; |
| 12490 | Result.copySign(RHS); |
| 12491 | return true; |
| 12492 | } |
| Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 12493 | } |
| 12494 | } |
| 12495 | |
| John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 12496 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 12497 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 12498 | ComplexValue CV; |
| 12499 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 12500 | return false; |
| 12501 | Result = CV.FloatReal; |
| 12502 | return true; |
| 12503 | } |
| 12504 | |
| 12505 | return Visit(E->getSubExpr()); |
| John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 12506 | } |
| 12507 | |
| 12508 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 12509 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 12510 | ComplexValue CV; |
| 12511 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 12512 | return false; |
| 12513 | Result = CV.FloatImag; |
| 12514 | return true; |
| 12515 | } |
| 12516 | |
| Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 12517 | VisitIgnoredValue(E->getSubExpr()); |
| Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 12518 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 12519 | Result = llvm::APFloat::getZero(Sem); |
| John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 12520 | return true; |
| 12521 | } |
| 12522 | |
| Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 12523 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 12524 | switch (E->getOpcode()) { |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12525 | default: return Error(E); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12526 | case UO_Plus: |
| Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 12527 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12528 | case UO_Minus: |
| Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 12529 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 12530 | return false; |
| Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 12531 | Result.changeSign(); |
| 12532 | return true; |
| 12533 | } |
| 12534 | } |
| Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 12535 | |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12536 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 12537 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 12538 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| Eli Friedman | 141fbf3 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 12539 | |
| Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 12540 | APFloat RHS(0.0); |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 12541 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 12542 | if (!LHSOK && !Info.noteFailure()) |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12543 | return false; |
| Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 12544 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
| 12545 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12546 | } |
| 12547 | |
| 12548 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 12549 | Result = E->getValue(); |
| 12550 | return true; |
| 12551 | } |
| 12552 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12553 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 12554 | const Expr* SubExpr = E->getSubExpr(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12555 | |
| Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 12556 | switch (E->getCastKind()) { |
| 12557 | default: |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 12558 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 12559 | |
| 12560 | case CK_IntegralToFloating: { |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12561 | APSInt IntResult; |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 12562 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 12563 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 12564 | E->getType(), Result); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12565 | } |
| Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 12566 | |
| 12567 | case CK_FloatingCast: { |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12568 | if (!Visit(SubExpr)) |
| 12569 | return false; |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 12570 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 12571 | Result); |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12572 | } |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 12573 | |
| Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 12574 | case CK_FloatingComplexToReal: { |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 12575 | ComplexValue V; |
| 12576 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 12577 | return false; |
| 12578 | Result = V.getComplexFloatReal(); |
| 12579 | return true; |
| 12580 | } |
| Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 12581 | } |
| Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 12582 | } |
| 12583 | |
| Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 12584 | //===----------------------------------------------------------------------===// |
| Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 12585 | // Complex Evaluation (for float and integer) |
| Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 12586 | //===----------------------------------------------------------------------===// |
| 12587 | |
| 12588 | namespace { |
| Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 12589 | class ComplexExprEvaluator |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 12590 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12591 | ComplexValue &Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12592 | |
| Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 12593 | public: |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12594 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12595 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 12596 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 12597 | bool Success(const APValue &V, const Expr *e) { |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12598 | Result.setFrom(V); |
| 12599 | return true; |
| 12600 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12601 | |
| Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 12602 | bool ZeroInitialization(const Expr *E); |
| 12603 | |
| Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 12604 | //===--------------------------------------------------------------------===// |
| 12605 | // Visitor Methods |
| 12606 | //===--------------------------------------------------------------------===// |
| 12607 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12608 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12609 | bool VisitCastExpr(const CastExpr *E); |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12610 | bool VisitBinaryOperator(const BinaryOperator *E); |
| Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 12611 | bool VisitUnaryOperator(const UnaryOperator *E); |
| Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 12612 | bool VisitInitListExpr(const InitListExpr *E); |
| Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 12613 | }; |
| 12614 | } // end anonymous namespace |
| 12615 | |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12616 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 12617 | EvalInfo &Info) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 12618 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12619 | return ComplexExprEvaluator(Info, Result).Visit(E); |
| Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 12620 | } |
| 12621 | |
| Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 12622 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 12623 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
| Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 12624 | if (ElemTy->isRealFloatingType()) { |
| 12625 | Result.makeComplexFloat(); |
| 12626 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 12627 | Result.FloatReal = Zero; |
| 12628 | Result.FloatImag = Zero; |
| 12629 | } else { |
| 12630 | Result.makeComplexInt(); |
| 12631 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 12632 | Result.IntReal = Zero; |
| 12633 | Result.IntImag = Zero; |
| 12634 | } |
| 12635 | return true; |
| 12636 | } |
| 12637 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12638 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 12639 | const Expr* SubExpr = E->getSubExpr(); |
| Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 12640 | |
| 12641 | if (SubExpr->getType()->isRealFloatingType()) { |
| 12642 | Result.makeComplexFloat(); |
| 12643 | APFloat &Imag = Result.FloatImag; |
| 12644 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 12645 | return false; |
| 12646 | |
| 12647 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 12648 | return true; |
| 12649 | } else { |
| 12650 | assert(SubExpr->getType()->isIntegerType() && |
| 12651 | "Unexpected imaginary literal."); |
| 12652 | |
| 12653 | Result.makeComplexInt(); |
| 12654 | APSInt &Imag = Result.IntImag; |
| 12655 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 12656 | return false; |
| 12657 | |
| 12658 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 12659 | return true; |
| 12660 | } |
| 12661 | } |
| 12662 | |
| Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 12663 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 12664 | |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12665 | switch (E->getCastKind()) { |
| 12666 | case CK_BitCast: |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12667 | case CK_BaseToDerived: |
| 12668 | case CK_DerivedToBase: |
| 12669 | case CK_UncheckedDerivedToBase: |
| 12670 | case CK_Dynamic: |
| 12671 | case CK_ToUnion: |
| 12672 | case CK_ArrayToPointerDecay: |
| 12673 | case CK_FunctionToPointerDecay: |
| 12674 | case CK_NullToPointer: |
| 12675 | case CK_NullToMemberPointer: |
| 12676 | case CK_BaseToDerivedMemberPointer: |
| 12677 | case CK_DerivedToBaseMemberPointer: |
| 12678 | case CK_MemberPointerToBoolean: |
| John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 12679 | case CK_ReinterpretMemberPointer: |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12680 | case CK_ConstructorConversion: |
| 12681 | case CK_IntegralToPointer: |
| 12682 | case CK_PointerToIntegral: |
| 12683 | case CK_PointerToBoolean: |
| 12684 | case CK_ToVoid: |
| 12685 | case CK_VectorSplat: |
| 12686 | case CK_IntegralCast: |
| George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 12687 | case CK_BooleanToSignedIntegral: |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12688 | case CK_IntegralToBoolean: |
| 12689 | case CK_IntegralToFloating: |
| 12690 | case CK_FloatingToIntegral: |
| 12691 | case CK_FloatingToBoolean: |
| 12692 | case CK_FloatingCast: |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 12693 | case CK_CPointerToObjCPointerCast: |
| 12694 | case CK_BlockPointerToObjCPointerCast: |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12695 | case CK_AnyPointerToBlockPointerCast: |
| 12696 | case CK_ObjCObjectLValueCast: |
| 12697 | case CK_FloatingComplexToReal: |
| 12698 | case CK_FloatingComplexToBoolean: |
| 12699 | case CK_IntegralComplexToReal: |
| 12700 | case CK_IntegralComplexToBoolean: |
| John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 12701 | case CK_ARCProduceObject: |
| 12702 | case CK_ARCConsumeObject: |
| 12703 | case CK_ARCReclaimReturnedObject: |
| 12704 | case CK_ARCExtendBlockObject: |
| Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 12705 | case CK_CopyAndAutoreleaseBlockObject: |
| Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 12706 | case CK_BuiltinFnToFnPtr: |
| Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 12707 | case CK_ZeroToOCLOpaqueType: |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 12708 | case CK_NonAtomicToAtomic: |
| David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 12709 | case CK_AddressSpaceConversion: |
| Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 12710 | case CK_IntToOCLSampler: |
| Leonard Chan | 99bda37 | 2018-10-15 16:07:02 +0000 | [diff] [blame] | 12711 | case CK_FixedPointCast: |
| Leonard Chan | b4ba467 | 2018-10-23 17:55:35 +0000 | [diff] [blame] | 12712 | case CK_FixedPointToBoolean: |
| Leonard Chan | 8f7caae | 2019-03-06 00:28:43 +0000 | [diff] [blame] | 12713 | case CK_FixedPointToIntegral: |
| 12714 | case CK_IntegralToFixedPoint: |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12715 | llvm_unreachable("invalid cast kind for complex value"); |
| John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 12716 | |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12717 | case CK_LValueToRValue: |
| David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 12718 | case CK_AtomicToNonAtomic: |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12719 | case CK_NoOp: |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 12720 | case CK_LValueToRValueBitCast: |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 12721 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12722 | |
| 12723 | case CK_Dependent: |
| Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 12724 | case CK_LValueBitCast: |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12725 | case CK_UserDefinedConversion: |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12726 | return Error(E); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12727 | |
| 12728 | case CK_FloatingRealToComplex: { |
| Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 12729 | APFloat &Real = Result.FloatReal; |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12730 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
| Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 12731 | return false; |
| 12732 | |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12733 | Result.makeComplexFloat(); |
| 12734 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 12735 | return true; |
| Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 12736 | } |
| 12737 | |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12738 | case CK_FloatingComplexCast: { |
| 12739 | if (!Visit(E->getSubExpr())) |
| 12740 | return false; |
| 12741 | |
| Simon Pilgrim | c15b38e | 2019-10-03 15:08:30 +0000 | [diff] [blame] | 12742 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12743 | QualType From |
| Simon Pilgrim | c15b38e | 2019-10-03 15:08:30 +0000 | [diff] [blame] | 12744 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12745 | |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 12746 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 12747 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12748 | } |
| 12749 | |
| 12750 | case CK_FloatingComplexToIntegralComplex: { |
| 12751 | if (!Visit(E->getSubExpr())) |
| 12752 | return false; |
| 12753 | |
| Simon Pilgrim | c15b38e | 2019-10-03 15:08:30 +0000 | [diff] [blame] | 12754 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12755 | QualType From |
| Simon Pilgrim | c15b38e | 2019-10-03 15:08:30 +0000 | [diff] [blame] | 12756 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12757 | Result.makeComplexInt(); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 12758 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 12759 | To, Result.IntReal) && |
| 12760 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 12761 | To, Result.IntImag); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12762 | } |
| 12763 | |
| 12764 | case CK_IntegralRealToComplex: { |
| 12765 | APSInt &Real = Result.IntReal; |
| 12766 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 12767 | return false; |
| 12768 | |
| 12769 | Result.makeComplexInt(); |
| 12770 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 12771 | return true; |
| 12772 | } |
| 12773 | |
| 12774 | case CK_IntegralComplexCast: { |
| 12775 | if (!Visit(E->getSubExpr())) |
| 12776 | return false; |
| 12777 | |
| Simon Pilgrim | c15b38e | 2019-10-03 15:08:30 +0000 | [diff] [blame] | 12778 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12779 | QualType From |
| Simon Pilgrim | c15b38e | 2019-10-03 15:08:30 +0000 | [diff] [blame] | 12780 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12781 | |
| Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 12782 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 12783 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12784 | return true; |
| 12785 | } |
| 12786 | |
| 12787 | case CK_IntegralComplexToFloatingComplex: { |
| 12788 | if (!Visit(E->getSubExpr())) |
| 12789 | return false; |
| 12790 | |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 12791 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12792 | QualType From |
| Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 12793 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12794 | Result.makeComplexFloat(); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 12795 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 12796 | To, Result.FloatReal) && |
| 12797 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 12798 | To, Result.FloatImag); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 12799 | } |
| 12800 | } |
| 12801 | |
| 12802 | llvm_unreachable("unknown cast resulting in complex value"); |
| Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 12803 | } |
| 12804 | |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12805 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 12806 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| Richard Smith | 10f4d06 | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 12807 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 12808 | |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12809 | // Track whether the LHS or RHS is real at the type system level. When this is |
| 12810 | // the case we can simplify our evaluation strategy. |
| 12811 | bool LHSReal = false, RHSReal = false; |
| 12812 | |
| 12813 | bool LHSOK; |
| 12814 | if (E->getLHS()->getType()->isRealFloatingType()) { |
| 12815 | LHSReal = true; |
| 12816 | APFloat &Real = Result.FloatReal; |
| 12817 | LHSOK = EvaluateFloat(E->getLHS(), Real, Info); |
| 12818 | if (LHSOK) { |
| 12819 | Result.makeComplexFloat(); |
| 12820 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 12821 | } |
| 12822 | } else { |
| 12823 | LHSOK = Visit(E->getLHS()); |
| 12824 | } |
| George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 12825 | if (!LHSOK && !Info.noteFailure()) |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12826 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12827 | |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12828 | ComplexValue RHS; |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12829 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 12830 | RHSReal = true; |
| 12831 | APFloat &Real = RHS.FloatReal; |
| 12832 | if (!EvaluateFloat(E->getRHS(), Real, Info) || !LHSOK) |
| 12833 | return false; |
| 12834 | RHS.makeComplexFloat(); |
| 12835 | RHS.FloatImag = APFloat(Real.getSemantics()); |
| 12836 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12837 | return false; |
| Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 12838 | |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12839 | assert(!(LHSReal && RHSReal) && |
| 12840 | "Cannot have both operands of a complex operation be real."); |
| Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 12841 | switch (E->getOpcode()) { |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 12842 | default: return Error(E); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12843 | case BO_Add: |
| Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 12844 | if (Result.isComplexFloat()) { |
| 12845 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 12846 | APFloat::rmNearestTiesToEven); |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12847 | if (LHSReal) |
| 12848 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 12849 | else if (!RHSReal) |
| 12850 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 12851 | APFloat::rmNearestTiesToEven); |
| Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 12852 | } else { |
| 12853 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 12854 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 12855 | } |
| Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 12856 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12857 | case BO_Sub: |
| Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 12858 | if (Result.isComplexFloat()) { |
| 12859 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 12860 | APFloat::rmNearestTiesToEven); |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12861 | if (LHSReal) { |
| 12862 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 12863 | Result.getComplexFloatImag().changeSign(); |
| 12864 | } else if (!RHSReal) { |
| 12865 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 12866 | APFloat::rmNearestTiesToEven); |
| 12867 | } |
| Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 12868 | } else { |
| 12869 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 12870 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 12871 | } |
| Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 12872 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 12873 | case BO_Mul: |
| Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 12874 | if (Result.isComplexFloat()) { |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12875 | // This is an implementation of complex multiplication according to the |
| Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 12876 | // constraints laid out in C11 Annex G. The implementation uses the |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12877 | // following naming scheme: |
| 12878 | // (a + ib) * (c + id) |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12879 | ComplexValue LHS = Result; |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12880 | APFloat &A = LHS.getComplexFloatReal(); |
| 12881 | APFloat &B = LHS.getComplexFloatImag(); |
| 12882 | APFloat &C = RHS.getComplexFloatReal(); |
| 12883 | APFloat &D = RHS.getComplexFloatImag(); |
| 12884 | APFloat &ResR = Result.getComplexFloatReal(); |
| 12885 | APFloat &ResI = Result.getComplexFloatImag(); |
| 12886 | if (LHSReal) { |
| 12887 | assert(!RHSReal && "Cannot have two real operands for a complex op!"); |
| 12888 | ResR = A * C; |
| 12889 | ResI = A * D; |
| 12890 | } else if (RHSReal) { |
| 12891 | ResR = C * A; |
| 12892 | ResI = C * B; |
| 12893 | } else { |
| 12894 | // In the fully general case, we need to handle NaNs and infinities |
| 12895 | // robustly. |
| 12896 | APFloat AC = A * C; |
| 12897 | APFloat BD = B * D; |
| 12898 | APFloat AD = A * D; |
| 12899 | APFloat BC = B * C; |
| 12900 | ResR = AC - BD; |
| 12901 | ResI = AD + BC; |
| 12902 | if (ResR.isNaN() && ResI.isNaN()) { |
| 12903 | bool Recalc = false; |
| 12904 | if (A.isInfinity() || B.isInfinity()) { |
| 12905 | A = APFloat::copySign( |
| 12906 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 12907 | B = APFloat::copySign( |
| 12908 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 12909 | if (C.isNaN()) |
| 12910 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 12911 | if (D.isNaN()) |
| 12912 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 12913 | Recalc = true; |
| 12914 | } |
| 12915 | if (C.isInfinity() || D.isInfinity()) { |
| 12916 | C = APFloat::copySign( |
| 12917 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 12918 | D = APFloat::copySign( |
| 12919 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 12920 | if (A.isNaN()) |
| 12921 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 12922 | if (B.isNaN()) |
| 12923 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 12924 | Recalc = true; |
| 12925 | } |
| 12926 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
| 12927 | AD.isInfinity() || BC.isInfinity())) { |
| 12928 | if (A.isNaN()) |
| 12929 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 12930 | if (B.isNaN()) |
| 12931 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 12932 | if (C.isNaN()) |
| 12933 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 12934 | if (D.isNaN()) |
| 12935 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 12936 | Recalc = true; |
| 12937 | } |
| 12938 | if (Recalc) { |
| 12939 | ResR = APFloat::getInf(A.getSemantics()) * (A * C - B * D); |
| 12940 | ResI = APFloat::getInf(A.getSemantics()) * (A * D + B * C); |
| 12941 | } |
| 12942 | } |
| 12943 | } |
| Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 12944 | } else { |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 12945 | ComplexValue LHS = Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12946 | Result.getComplexIntReal() = |
| Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 12947 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 12948 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 12949 | Result.getComplexIntImag() = |
| Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 12950 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 12951 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 12952 | } |
| 12953 | break; |
| Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 12954 | case BO_Div: |
| 12955 | if (Result.isComplexFloat()) { |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12956 | // This is an implementation of complex division according to the |
| Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 12957 | // constraints laid out in C11 Annex G. The implementation uses the |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12958 | // following naming scheme: |
| 12959 | // (a + ib) / (c + id) |
| Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 12960 | ComplexValue LHS = Result; |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12961 | APFloat &A = LHS.getComplexFloatReal(); |
| 12962 | APFloat &B = LHS.getComplexFloatImag(); |
| 12963 | APFloat &C = RHS.getComplexFloatReal(); |
| 12964 | APFloat &D = RHS.getComplexFloatImag(); |
| 12965 | APFloat &ResR = Result.getComplexFloatReal(); |
| 12966 | APFloat &ResI = Result.getComplexFloatImag(); |
| 12967 | if (RHSReal) { |
| 12968 | ResR = A / C; |
| 12969 | ResI = B / C; |
| 12970 | } else { |
| 12971 | if (LHSReal) { |
| 12972 | // No real optimizations we can do here, stub out with zero. |
| 12973 | B = APFloat::getZero(A.getSemantics()); |
| 12974 | } |
| 12975 | int DenomLogB = 0; |
| 12976 | APFloat MaxCD = maxnum(abs(C), abs(D)); |
| 12977 | if (MaxCD.isFinite()) { |
| 12978 | DenomLogB = ilogb(MaxCD); |
| Matt Arsenault | c477f48 | 2016-03-13 05:12:47 +0000 | [diff] [blame] | 12979 | C = scalbn(C, -DenomLogB, APFloat::rmNearestTiesToEven); |
| 12980 | D = scalbn(D, -DenomLogB, APFloat::rmNearestTiesToEven); |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12981 | } |
| 12982 | APFloat Denom = C * C + D * D; |
| Matt Arsenault | c477f48 | 2016-03-13 05:12:47 +0000 | [diff] [blame] | 12983 | ResR = scalbn((A * C + B * D) / Denom, -DenomLogB, |
| 12984 | APFloat::rmNearestTiesToEven); |
| 12985 | ResI = scalbn((B * C - A * D) / Denom, -DenomLogB, |
| 12986 | APFloat::rmNearestTiesToEven); |
| Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 12987 | if (ResR.isNaN() && ResI.isNaN()) { |
| 12988 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
| 12989 | ResR = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * A; |
| 12990 | ResI = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * B; |
| 12991 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
| 12992 | D.isFinite()) { |
| 12993 | A = APFloat::copySign( |
| 12994 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 12995 | B = APFloat::copySign( |
| 12996 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 12997 | ResR = APFloat::getInf(ResR.getSemantics()) * (A * C + B * D); |
| 12998 | ResI = APFloat::getInf(ResI.getSemantics()) * (B * C - A * D); |
| 12999 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
| 13000 | C = APFloat::copySign( |
| 13001 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 13002 | D = APFloat::copySign( |
| 13003 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 13004 | ResR = APFloat::getZero(ResR.getSemantics()) * (A * C + B * D); |
| 13005 | ResI = APFloat::getZero(ResI.getSemantics()) * (B * C - A * D); |
| 13006 | } |
| 13007 | } |
| 13008 | } |
| Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 13009 | } else { |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 13010 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 13011 | return Error(E, diag::note_expr_divide_by_zero); |
| 13012 | |
| Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 13013 | ComplexValue LHS = Result; |
| 13014 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 13015 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 13016 | Result.getComplexIntReal() = |
| 13017 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 13018 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 13019 | Result.getComplexIntImag() = |
| 13020 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 13021 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 13022 | } |
| 13023 | break; |
| Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 13024 | } |
| 13025 | |
| John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 13026 | return true; |
| Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 13027 | } |
| 13028 | |
| Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 13029 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 13030 | // Get the operand value into 'Result'. |
| 13031 | if (!Visit(E->getSubExpr())) |
| 13032 | return false; |
| 13033 | |
| 13034 | switch (E->getOpcode()) { |
| 13035 | default: |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 13036 | return Error(E); |
| Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 13037 | case UO_Extension: |
| 13038 | return true; |
| 13039 | case UO_Plus: |
| 13040 | // The result is always just the subexpr. |
| 13041 | return true; |
| 13042 | case UO_Minus: |
| 13043 | if (Result.isComplexFloat()) { |
| 13044 | Result.getComplexFloatReal().changeSign(); |
| 13045 | Result.getComplexFloatImag().changeSign(); |
| 13046 | } |
| 13047 | else { |
| 13048 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 13049 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 13050 | } |
| 13051 | return true; |
| 13052 | case UO_Not: |
| 13053 | if (Result.isComplexFloat()) |
| 13054 | Result.getComplexFloatImag().changeSign(); |
| 13055 | else |
| 13056 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 13057 | return true; |
| 13058 | } |
| 13059 | } |
| 13060 | |
| Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 13061 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 13062 | if (E->getNumInits() == 2) { |
| 13063 | if (E->getType()->isComplexType()) { |
| 13064 | Result.makeComplexFloat(); |
| 13065 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 13066 | return false; |
| 13067 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 13068 | return false; |
| 13069 | } else { |
| 13070 | Result.makeComplexInt(); |
| 13071 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 13072 | return false; |
| 13073 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 13074 | return false; |
| 13075 | } |
| 13076 | return true; |
| 13077 | } |
| 13078 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 13079 | } |
| 13080 | |
| Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 13081 | //===----------------------------------------------------------------------===// |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13082 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
| 13083 | // implicit conversion. |
| 13084 | //===----------------------------------------------------------------------===// |
| 13085 | |
| 13086 | namespace { |
| 13087 | class AtomicExprEvaluator : |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 13088 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13089 | const LValue *This; |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13090 | APValue &Result; |
| 13091 | public: |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13092 | AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result) |
| 13093 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13094 | |
| 13095 | bool Success(const APValue &V, const Expr *E) { |
| 13096 | Result = V; |
| 13097 | return true; |
| 13098 | } |
| 13099 | |
| 13100 | bool ZeroInitialization(const Expr *E) { |
| 13101 | ImplicitValueInitExpr VIE( |
| 13102 | E->getType()->castAs<AtomicType>()->getValueType()); |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13103 | // For atomic-qualified class (and array) types in C++, initialize the |
| 13104 | // _Atomic-wrapped subobject directly, in-place. |
| 13105 | return This ? EvaluateInPlace(Result, Info, *This, &VIE) |
| 13106 | : Evaluate(Result, Info, &VIE); |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13107 | } |
| 13108 | |
| 13109 | bool VisitCastExpr(const CastExpr *E) { |
| 13110 | switch (E->getCastKind()) { |
| 13111 | default: |
| 13112 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 13113 | case CK_NonAtomicToAtomic: |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13114 | return This ? EvaluateInPlace(Result, Info, *This, E->getSubExpr()) |
| 13115 | : Evaluate(Result, Info, E->getSubExpr()); |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13116 | } |
| 13117 | } |
| 13118 | }; |
| 13119 | } // end anonymous namespace |
| 13120 | |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13121 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
| 13122 | EvalInfo &Info) { |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13123 | assert(E->isRValue() && E->getType()->isAtomicType()); |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13124 | return AtomicExprEvaluator(Info, This, Result).Visit(E); |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13125 | } |
| 13126 | |
| 13127 | //===----------------------------------------------------------------------===// |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 13128 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 13129 | // comma operator |
| 13130 | //===----------------------------------------------------------------------===// |
| 13131 | |
| 13132 | namespace { |
| 13133 | class VoidExprEvaluator |
| Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 13134 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 13135 | public: |
| 13136 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 13137 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 13138 | bool Success(const APValue &V, const Expr *e) { return true; } |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 13139 | |
| Richard Smith | 7cd577b | 2017-08-17 19:35:50 +0000 | [diff] [blame] | 13140 | bool ZeroInitialization(const Expr *E) { return true; } |
| 13141 | |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 13142 | bool VisitCastExpr(const CastExpr *E) { |
| 13143 | switch (E->getCastKind()) { |
| 13144 | default: |
| 13145 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 13146 | case CK_ToVoid: |
| 13147 | VisitIgnoredValue(E->getSubExpr()); |
| 13148 | return true; |
| 13149 | } |
| 13150 | } |
| Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 13151 | |
| 13152 | bool VisitCallExpr(const CallExpr *E) { |
| 13153 | switch (E->getBuiltinCallee()) { |
| Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 13154 | case Builtin::BI__assume: |
| Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 13155 | case Builtin::BI__builtin_assume: |
| Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 13156 | // The argument is not evaluated! |
| 13157 | return true; |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 13158 | |
| 13159 | case Builtin::BI__builtin_operator_delete: |
| 13160 | return HandleOperatorDeleteCall(Info, E); |
| 13161 | |
| 13162 | default: |
| 13163 | break; |
| Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 13164 | } |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 13165 | |
| 13166 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 13167 | } |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13168 | |
| 13169 | bool VisitCXXDeleteExpr(const CXXDeleteExpr *E); |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 13170 | }; |
| 13171 | } // end anonymous namespace |
| 13172 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13173 | bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
| 13174 | // We cannot speculatively evaluate a delete expression. |
| 13175 | if (Info.SpeculativeEvaluationDepth) |
| 13176 | return false; |
| 13177 | |
| 13178 | FunctionDecl *OperatorDelete = E->getOperatorDelete(); |
| 13179 | if (!OperatorDelete->isReplaceableGlobalAllocationFunction()) { |
| 13180 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
| 13181 | << isa<CXXMethodDecl>(OperatorDelete) << OperatorDelete; |
| 13182 | return false; |
| 13183 | } |
| 13184 | |
| 13185 | const Expr *Arg = E->getArgument(); |
| 13186 | |
| 13187 | LValue Pointer; |
| 13188 | if (!EvaluatePointer(Arg, Pointer, Info)) |
| 13189 | return false; |
| 13190 | if (Pointer.Designator.Invalid) |
| 13191 | return false; |
| 13192 | |
| 13193 | // Deleting a null pointer has no effect. |
| 13194 | if (Pointer.isNullPointer()) { |
| 13195 | // This is the only case where we need to produce an extension warning: |
| 13196 | // the only other way we can succeed is if we find a dynamic allocation, |
| 13197 | // and we will have warned when we allocated it in that case. |
| 13198 | if (!Info.getLangOpts().CPlusPlus2a) |
| 13199 | Info.CCEDiag(E, diag::note_constexpr_new); |
| 13200 | return true; |
| 13201 | } |
| 13202 | |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 13203 | Optional<DynAlloc *> Alloc = CheckDeleteKind( |
| 13204 | Info, E, Pointer, E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New); |
| 13205 | if (!Alloc) |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13206 | return false; |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13207 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
| 13208 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13209 | // For the non-array case, the designator must be empty if the static type |
| 13210 | // does not have a virtual destructor. |
| 13211 | if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 && |
| 13212 | !hasVirtualDestructor(Arg->getType()->getPointeeType())) { |
| 13213 | Info.FFDiag(E, diag::note_constexpr_delete_base_nonvirt_dtor) |
| 13214 | << Arg->getType()->getPointeeType() << AllocType; |
| 13215 | return false; |
| 13216 | } |
| 13217 | |
| Richard Smith | df3761f | 2019-10-07 03:14:28 +0000 | [diff] [blame] | 13218 | // For a class type with a virtual destructor, the selected operator delete |
| 13219 | // is the one looked up when building the destructor. |
| 13220 | if (!E->isArrayForm() && !E->isGlobalDelete()) { |
| 13221 | const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(AllocType); |
| 13222 | if (VirtualDelete && |
| 13223 | !VirtualDelete->isReplaceableGlobalAllocationFunction()) { |
| 13224 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
| 13225 | << isa<CXXMethodDecl>(VirtualDelete) << VirtualDelete; |
| 13226 | return false; |
| 13227 | } |
| 13228 | } |
| 13229 | |
| Richard Smith | 61422f9 | 2019-09-27 20:24:36 +0000 | [diff] [blame] | 13230 | if (!HandleDestruction(Info, E->getExprLoc(), Pointer.getLValueBase(), |
| 13231 | (*Alloc)->Value, AllocType)) |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13232 | return false; |
| 13233 | |
| Richard Smith | 19ad523 | 2019-10-03 00:39:33 +0000 | [diff] [blame] | 13234 | if (!Info.HeapAllocs.erase(Pointer.Base.dyn_cast<DynamicAllocLValue>())) { |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13235 | // The element was already erased. This means the destructor call also |
| 13236 | // deleted the object. |
| 13237 | // FIXME: This probably results in undefined behavior before we get this |
| 13238 | // far, and should be diagnosed elsewhere first. |
| 13239 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
| 13240 | return false; |
| 13241 | } |
| 13242 | |
| 13243 | return true; |
| 13244 | } |
| 13245 | |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 13246 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 13247 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 13248 | return VoidExprEvaluator(Info).Visit(E); |
| 13249 | } |
| 13250 | |
| 13251 | //===----------------------------------------------------------------------===// |
| Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 13252 | // Top level Expr::EvaluateAsRValue method. |
| Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 13253 | //===----------------------------------------------------------------------===// |
| 13254 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 13255 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 13256 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 13257 | // are. |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13258 | QualType T = E->getType(); |
| 13259 | if (E->isGLValue() || T->isFunctionType()) { |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 13260 | LValue LV; |
| 13261 | if (!EvaluateLValue(E, LV, Info)) |
| 13262 | return false; |
| 13263 | LV.moveInto(Result); |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13264 | } else if (T->isVectorType()) { |
| Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 13265 | if (!EvaluateVector(E, Result, Info)) |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 13266 | return false; |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13267 | } else if (T->isIntegralOrEnumerationType()) { |
| Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 13268 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
| Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 13269 | return false; |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13270 | } else if (T->hasPointerRepresentation()) { |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 13271 | LValue LV; |
| 13272 | if (!EvaluatePointer(E, LV, Info)) |
| Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 13273 | return false; |
| Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 13274 | LV.moveInto(Result); |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13275 | } else if (T->isRealFloatingType()) { |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 13276 | llvm::APFloat F(0.0); |
| 13277 | if (!EvaluateFloat(E, F, Info)) |
| Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 13278 | return false; |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 13279 | Result = APValue(F); |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13280 | } else if (T->isAnyComplexType()) { |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 13281 | ComplexValue C; |
| 13282 | if (!EvaluateComplex(E, C, Info)) |
| Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 13283 | return false; |
| Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 13284 | C.moveInto(Result); |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 13285 | } else if (T->isFixedPointType()) { |
| 13286 | if (!FixedPointExprEvaluator(Info, Result).Visit(E)) return false; |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13287 | } else if (T->isMemberPointerType()) { |
| Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 13288 | MemberPtr P; |
| 13289 | if (!EvaluateMemberPointer(E, P, Info)) |
| 13290 | return false; |
| 13291 | P.moveInto(Result); |
| 13292 | return true; |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13293 | } else if (T->isArrayType()) { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 13294 | LValue LV; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 13295 | APValue &Value = |
| 13296 | Info.CurrentCall->createTemporary(E, T, false, LV); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 13297 | if (!EvaluateArray(E, LV, Value, Info)) |
| Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 13298 | return false; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 13299 | Result = Value; |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13300 | } else if (T->isRecordType()) { |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 13301 | LValue LV; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 13302 | APValue &Value = Info.CurrentCall->createTemporary(E, T, false, LV); |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 13303 | if (!EvaluateRecord(E, LV, Value, Info)) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 13304 | return false; |
| Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 13305 | Result = Value; |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13306 | } else if (T->isVoidType()) { |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 13307 | if (!Info.getLangOpts().CPlusPlus11) |
| Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 13308 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 13309 | << E->getType(); |
| Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 13310 | if (!EvaluateVoid(E, Info)) |
| 13311 | return false; |
| Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 13312 | } else if (T->isAtomicType()) { |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13313 | QualType Unqual = T.getAtomicUnqualifiedType(); |
| 13314 | if (Unqual->isArrayType() || Unqual->isRecordType()) { |
| 13315 | LValue LV; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 13316 | APValue &Value = Info.CurrentCall->createTemporary(E, Unqual, false, LV); |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13317 | if (!EvaluateAtomic(E, &LV, Value, Info)) |
| 13318 | return false; |
| 13319 | } else { |
| 13320 | if (!EvaluateAtomic(E, nullptr, Result, Info)) |
| 13321 | return false; |
| 13322 | } |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 13323 | } else if (Info.getLangOpts().CPlusPlus11) { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 13324 | Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType(); |
| Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 13325 | return false; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 13326 | } else { |
| Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 13327 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 13328 | return false; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 13329 | } |
| Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 13330 | |
| Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 13331 | return true; |
| 13332 | } |
| 13333 | |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 13334 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 13335 | /// cases, the in-place evaluation is essential, since later initializers for |
| 13336 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 13337 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 13338 | const Expr *E, bool AllowNonLiteralTypes) { |
| Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 13339 | assert(!E->isValueDependent()); |
| 13340 | |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 13341 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 13342 | return false; |
| 13343 | |
| 13344 | if (E->isRValue()) { |
| Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 13345 | // Evaluate arrays and record types in-place, so that later initializers can |
| 13346 | // refer to earlier-initialized members of the object. |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13347 | QualType T = E->getType(); |
| 13348 | if (T->isArrayType()) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 13349 | return EvaluateArray(E, This, Result, Info); |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13350 | else if (T->isRecordType()) |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 13351 | return EvaluateRecord(E, This, Result, Info); |
| Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 13352 | else if (T->isAtomicType()) { |
| 13353 | QualType Unqual = T.getAtomicUnqualifiedType(); |
| 13354 | if (Unqual->isArrayType() || Unqual->isRecordType()) |
| 13355 | return EvaluateAtomic(E, &This, Result, Info); |
| 13356 | } |
| Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 13357 | } |
| 13358 | |
| 13359 | // For any other type, in-place evaluation is unimportant. |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 13360 | return Evaluate(Result, Info, E); |
| Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 13361 | } |
| 13362 | |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 13363 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 13364 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 13365 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 13366 | if (Info.EnableNewConstInterp) { |
| 13367 | auto &InterpCtx = Info.Ctx.getInterpContext(); |
| 13368 | switch (InterpCtx.evaluateAsRValue(Info, E, Result)) { |
| 13369 | case interp::InterpResult::Success: |
| 13370 | return true; |
| 13371 | case interp::InterpResult::Fail: |
| 13372 | return false; |
| 13373 | case interp::InterpResult::Bail: |
| 13374 | break; |
| 13375 | } |
| 13376 | } |
| 13377 | |
| James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 13378 | if (E->getType().isNull()) |
| 13379 | return false; |
| 13380 | |
| Nick Lewycky | c190f96 | 2017-05-02 01:06:16 +0000 | [diff] [blame] | 13381 | if (!CheckLiteralType(Info, E)) |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 13382 | return false; |
| 13383 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 13384 | if (!::Evaluate(Result, Info, E)) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 13385 | return false; |
| 13386 | |
| 13387 | if (E->isGLValue()) { |
| 13388 | LValue LV; |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 13389 | LV.setFrom(Info.Ctx, Result); |
| Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 13390 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 13391 | return false; |
| 13392 | } |
| 13393 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 13394 | // Check this core constant expression is a constant expression. |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13395 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result) && |
| 13396 | CheckMemoryLeaks(Info); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 13397 | } |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 13398 | |
| Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 13399 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
| Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 13400 | const ASTContext &Ctx, bool &IsConst) { |
| Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 13401 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 13402 | // containing vast quantities of these. |
| 13403 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
| 13404 | Result.Val = APValue(APSInt(L->getValue(), |
| 13405 | L->getType()->isUnsignedIntegerType())); |
| 13406 | IsConst = true; |
| 13407 | return true; |
| 13408 | } |
| James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 13409 | |
| 13410 | // This case should be rare, but we need to check it before we check on |
| 13411 | // the type below. |
| 13412 | if (Exp->getType().isNull()) { |
| 13413 | IsConst = false; |
| 13414 | return true; |
| 13415 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 13416 | |
| Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 13417 | // FIXME: Evaluating values of large array and record types can cause |
| 13418 | // performance problems. Only do so in C++11 for now. |
| 13419 | if (Exp->isRValue() && (Exp->getType()->isArrayType() || |
| 13420 | Exp->getType()->isRecordType()) && |
| Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 13421 | !Ctx.getLangOpts().CPlusPlus11) { |
| Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 13422 | IsConst = false; |
| 13423 | return true; |
| 13424 | } |
| 13425 | return false; |
| 13426 | } |
| 13427 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13428 | static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result, |
| 13429 | Expr::SideEffectsKind SEK) { |
| 13430 | return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) || |
| 13431 | (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior); |
| 13432 | } |
| 13433 | |
| 13434 | static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result, |
| 13435 | const ASTContext &Ctx, EvalInfo &Info) { |
| 13436 | bool IsConst; |
| 13437 | if (FastEvaluateAsRValue(E, Result, Ctx, IsConst)) |
| 13438 | return IsConst; |
| 13439 | |
| 13440 | return EvaluateAsRValue(Info, E, Result.Val); |
| 13441 | } |
| 13442 | |
| 13443 | static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult, |
| 13444 | const ASTContext &Ctx, |
| 13445 | Expr::SideEffectsKind AllowSideEffects, |
| 13446 | EvalInfo &Info) { |
| 13447 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 13448 | return false; |
| 13449 | |
| 13450 | if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info) || |
| 13451 | !ExprResult.Val.isInt() || |
| 13452 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
| 13453 | return false; |
| 13454 | |
| 13455 | return true; |
| 13456 | } |
| Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 13457 | |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 13458 | static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult, |
| 13459 | const ASTContext &Ctx, |
| 13460 | Expr::SideEffectsKind AllowSideEffects, |
| 13461 | EvalInfo &Info) { |
| 13462 | if (!E->getType()->isFixedPointType()) |
| 13463 | return false; |
| 13464 | |
| 13465 | if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info)) |
| 13466 | return false; |
| 13467 | |
| 13468 | if (!ExprResult.Val.isFixedPoint() || |
| 13469 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
| 13470 | return false; |
| 13471 | |
| 13472 | return true; |
| 13473 | } |
| 13474 | |
| Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 13475 | /// 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] | 13476 | /// any crazy technique (that has nothing to do with language standards) that |
| 13477 | /// 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] | 13478 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 13479 | /// will be applied to the result. |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13480 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, |
| 13481 | bool InConstantContext) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13482 | assert(!isValueDependent() && |
| 13483 | "Expression evaluator can't be called on a dependent expression."); |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 13484 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13485 | Info.InConstantContext = InConstantContext; |
| 13486 | return ::EvaluateAsRValue(this, Result, Ctx, Info); |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 13487 | } |
| 13488 | |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 13489 | bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, |
| 13490 | bool InConstantContext) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13491 | assert(!isValueDependent() && |
| 13492 | "Expression evaluator can't be called on a dependent expression."); |
| Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 13493 | EvalResult Scratch; |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 13494 | return EvaluateAsRValue(Scratch, Ctx, InConstantContext) && |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 13495 | HandleConversionToBool(Scratch.Val, Result); |
| John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 13496 | } |
| 13497 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13498 | bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 13499 | SideEffectsKind AllowSideEffects, |
| 13500 | bool InConstantContext) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13501 | assert(!isValueDependent() && |
| 13502 | "Expression evaluator can't be called on a dependent expression."); |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13503 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 13504 | Info.InConstantContext = InConstantContext; |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13505 | return ::EvaluateAsInt(this, Result, Ctx, AllowSideEffects, Info); |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 13506 | } |
| 13507 | |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 13508 | bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx, |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 13509 | SideEffectsKind AllowSideEffects, |
| 13510 | bool InConstantContext) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13511 | assert(!isValueDependent() && |
| 13512 | "Expression evaluator can't be called on a dependent expression."); |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 13513 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 13514 | Info.InConstantContext = InConstantContext; |
| Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 13515 | return ::EvaluateAsFixedPoint(this, Result, Ctx, AllowSideEffects, Info); |
| 13516 | } |
| 13517 | |
| Richard Trieu | be234c3 | 2016-04-21 21:04:55 +0000 | [diff] [blame] | 13518 | bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx, |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 13519 | SideEffectsKind AllowSideEffects, |
| 13520 | bool InConstantContext) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13521 | assert(!isValueDependent() && |
| 13522 | "Expression evaluator can't be called on a dependent expression."); |
| 13523 | |
| Richard Trieu | be234c3 | 2016-04-21 21:04:55 +0000 | [diff] [blame] | 13524 | if (!getType()->isRealFloatingType()) |
| 13525 | return false; |
| 13526 | |
| 13527 | EvalResult ExprResult; |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 13528 | if (!EvaluateAsRValue(ExprResult, Ctx, InConstantContext) || |
| 13529 | !ExprResult.Val.isFloat() || |
| Richard Smith | 3f1d6de | 2018-05-21 20:36:58 +0000 | [diff] [blame] | 13530 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
| Richard Trieu | be234c3 | 2016-04-21 21:04:55 +0000 | [diff] [blame] | 13531 | return false; |
| 13532 | |
| 13533 | Result = ExprResult.Val.getFloat(); |
| 13534 | return true; |
| 13535 | } |
| 13536 | |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 13537 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, |
| 13538 | bool InConstantContext) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13539 | assert(!isValueDependent() && |
| 13540 | "Expression evaluator can't be called on a dependent expression."); |
| 13541 | |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 13542 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 13543 | Info.InConstantContext = InConstantContext; |
| John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 13544 | LValue LV; |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 13545 | CheckedTemporaries CheckedTemps; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 13546 | if (!EvaluateLValue(this, LV, Info) || !Info.discardCleanups() || |
| 13547 | Result.HasSideEffects || |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 13548 | !CheckLValueConstantExpression(Info, getExprLoc(), |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 13549 | Ctx.getLValueReferenceType(getType()), LV, |
| Richard Smith | 4566f87 | 2019-09-29 05:58:31 +0000 | [diff] [blame] | 13550 | Expr::EvaluateForCodeGen, CheckedTemps)) |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 13551 | return false; |
| 13552 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 13553 | LV.moveInto(Result.Val); |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 13554 | return true; |
| Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 13555 | } |
| 13556 | |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 13557 | bool Expr::EvaluateAsConstantExpr(EvalResult &Result, ConstExprUsage Usage, |
| 13558 | const ASTContext &Ctx) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13559 | assert(!isValueDependent() && |
| 13560 | "Expression evaluator can't be called on a dependent expression."); |
| 13561 | |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 13562 | EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression; |
| 13563 | EvalInfo Info(Ctx, Result, EM); |
| Eric Fiselier | 680e865 | 2019-03-08 22:06:48 +0000 | [diff] [blame] | 13564 | Info.InConstantContext = true; |
| Eric Fiselier | add16a8 | 2019-04-24 02:23:30 +0000 | [diff] [blame] | 13565 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13566 | if (!::Evaluate(Result.Val, Info, this) || Result.HasSideEffects) |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 13567 | return false; |
| 13568 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 13569 | if (!Info.discardCleanups()) |
| 13570 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
| 13571 | |
| Richard Smith | 5769440 | 2019-10-08 23:37:49 +0000 | [diff] [blame] | 13572 | return CheckConstantExpression(Info, getExprLoc(), getStorageType(Ctx, this), |
| 13573 | Result.Val, Usage) && |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13574 | CheckMemoryLeaks(Info); |
| Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 13575 | } |
| 13576 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 13577 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 13578 | const VarDecl *VD, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 13579 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13580 | assert(!isValueDependent() && |
| 13581 | "Expression evaluator can't be called on a dependent expression."); |
| 13582 | |
| Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 13583 | // FIXME: Evaluating initializers for large array and record types can cause |
| 13584 | // performance problems. Only do so in C++11 for now. |
| 13585 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 13586 | !Ctx.getLangOpts().CPlusPlus11) |
| Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 13587 | return false; |
| 13588 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 13589 | Expr::EvalStatus EStatus; |
| 13590 | EStatus.Diag = &Notes; |
| 13591 | |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 13592 | EvalInfo Info(Ctx, EStatus, VD->isConstexpr() |
| Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 13593 | ? EvalInfo::EM_ConstantExpression |
| 13594 | : EvalInfo::EM_ConstantFold); |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 13595 | Info.setEvaluatingDecl(VD, Value); |
| 13596 | Info.InConstantContext = true; |
| 13597 | |
| 13598 | SourceLocation DeclLoc = VD->getLocation(); |
| 13599 | QualType DeclTy = VD->getType(); |
| 13600 | |
| 13601 | if (Info.EnableNewConstInterp) { |
| 13602 | auto &InterpCtx = const_cast<ASTContext &>(Ctx).getInterpContext(); |
| 13603 | switch (InterpCtx.evaluateAsInitializer(Info, VD, Value)) { |
| 13604 | case interp::InterpResult::Fail: |
| 13605 | // Bail out if an error was encountered. |
| 13606 | return false; |
| 13607 | case interp::InterpResult::Success: |
| 13608 | // Evaluation succeeded and value was set. |
| 13609 | return CheckConstantExpression(Info, DeclLoc, DeclTy, Value); |
| 13610 | case interp::InterpResult::Bail: |
| 13611 | // Evaluate the value again for the tree evaluator to use. |
| 13612 | break; |
| 13613 | } |
| 13614 | } |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 13615 | |
| 13616 | LValue LVal; |
| 13617 | LVal.set(VD); |
| 13618 | |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 13619 | // C++11 [basic.start.init]p2: |
| 13620 | // Variables with static storage duration or thread storage duration shall be |
| 13621 | // zero-initialized before any other initialization takes place. |
| 13622 | // This behavior is not present in C. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13623 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 13624 | !DeclTy->isReferenceType()) { |
| 13625 | ImplicitValueInitExpr VIE(DeclTy); |
| 13626 | if (!EvaluateInPlace(Value, Info, LVal, &VIE, |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 13627 | /*AllowNonLiteralTypes=*/true)) |
| Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 13628 | return false; |
| 13629 | } |
| 13630 | |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 13631 | if (!EvaluateInPlace(Value, Info, LVal, this, |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 13632 | /*AllowNonLiteralTypes=*/true) || |
| Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 13633 | EStatus.HasSideEffects) |
| 13634 | return false; |
| 13635 | |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 13636 | // At this point, any lifetime-extended temporaries are completely |
| 13637 | // initialized. |
| 13638 | Info.performLifetimeExtension(); |
| 13639 | |
| 13640 | if (!Info.discardCleanups()) |
| 13641 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
| 13642 | |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 13643 | return CheckConstantExpression(Info, DeclLoc, DeclTy, Value) && |
| 13644 | CheckMemoryLeaks(Info); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 13645 | } |
| 13646 | |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 13647 | bool VarDecl::evaluateDestruction( |
| 13648 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
| 13649 | assert(getEvaluatedValue() && !getEvaluatedValue()->isAbsent() && |
| 13650 | "cannot evaluate destruction of non-constant-initialized variable"); |
| 13651 | |
| 13652 | Expr::EvalStatus EStatus; |
| 13653 | EStatus.Diag = &Notes; |
| 13654 | |
| 13655 | // Make a copy of the value for the destructor to mutate. |
| 13656 | APValue DestroyedValue = *getEvaluatedValue(); |
| 13657 | |
| 13658 | EvalInfo Info(getASTContext(), EStatus, EvalInfo::EM_ConstantExpression); |
| 13659 | Info.setEvaluatingDecl(this, DestroyedValue, |
| 13660 | EvalInfo::EvaluatingDeclKind::Dtor); |
| 13661 | Info.InConstantContext = true; |
| 13662 | |
| 13663 | SourceLocation DeclLoc = getLocation(); |
| 13664 | QualType DeclTy = getType(); |
| 13665 | |
| 13666 | LValue LVal; |
| 13667 | LVal.set(this); |
| 13668 | |
| 13669 | // FIXME: Consider storing whether this variable has constant destruction in |
| 13670 | // the EvaluatedStmt so that CodeGen can query it. |
| 13671 | if (!HandleDestruction(Info, DeclLoc, LVal.Base, DestroyedValue, DeclTy) || |
| 13672 | EStatus.HasSideEffects) |
| 13673 | return false; |
| 13674 | |
| 13675 | if (!Info.discardCleanups()) |
| 13676 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
| 13677 | |
| 13678 | ensureEvaluatedStmt()->HasConstantDestruction = true; |
| 13679 | return true; |
| 13680 | } |
| 13681 | |
| Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 13682 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 13683 | /// constant folded, but discard the result. |
| Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 13684 | bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13685 | assert(!isValueDependent() && |
| 13686 | "Expression evaluator can't be called on a dependent expression."); |
| 13687 | |
| Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 13688 | EvalResult Result; |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13689 | return EvaluateAsRValue(Result, Ctx, /* in constant context */ true) && |
| Richard Smith | 3f1d6de | 2018-05-21 20:36:58 +0000 | [diff] [blame] | 13690 | !hasUnacceptableSideEffect(Result, SEK); |
| Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 13691 | } |
| Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 13692 | |
| Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 13693 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 13694 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13695 | assert(!isValueDependent() && |
| 13696 | "Expression evaluator can't be called on a dependent expression."); |
| 13697 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13698 | EvalResult EVResult; |
| 13699 | EVResult.Diag = Diag; |
| 13700 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
| 13701 | Info.InConstantContext = true; |
| 13702 | |
| 13703 | bool Result = ::EvaluateAsRValue(this, EVResult, Ctx, Info); |
| Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 13704 | (void)Result; |
| Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 13705 | assert(Result && "Could not evaluate expression"); |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13706 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
| Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 13707 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13708 | return EVResult.Val.getInt(); |
| Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 13709 | } |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13710 | |
| David Bolvansky | 3b6ae57 | 2018-10-18 20:49:06 +0000 | [diff] [blame] | 13711 | APSInt Expr::EvaluateKnownConstIntCheckOverflow( |
| 13712 | const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13713 | assert(!isValueDependent() && |
| 13714 | "Expression evaluator can't be called on a dependent expression."); |
| 13715 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13716 | EvalResult EVResult; |
| 13717 | EVResult.Diag = Diag; |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 13718 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13719 | Info.InConstantContext = true; |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 13720 | Info.CheckingForUndefinedBehavior = true; |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13721 | |
| 13722 | bool Result = ::EvaluateAsRValue(Info, this, EVResult.Val); |
| David Bolvansky | 3b6ae57 | 2018-10-18 20:49:06 +0000 | [diff] [blame] | 13723 | (void)Result; |
| 13724 | assert(Result && "Could not evaluate expression"); |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13725 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
| David Bolvansky | 3b6ae57 | 2018-10-18 20:49:06 +0000 | [diff] [blame] | 13726 | |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13727 | return EVResult.Val.getInt(); |
| David Bolvansky | 3b6ae57 | 2018-10-18 20:49:06 +0000 | [diff] [blame] | 13728 | } |
| 13729 | |
| Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 13730 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 13731 | assert(!isValueDependent() && |
| 13732 | "Expression evaluator can't be called on a dependent expression."); |
| 13733 | |
| Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 13734 | bool IsConst; |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13735 | EvalResult EVResult; |
| 13736 | if (!FastEvaluateAsRValue(this, EVResult, Ctx, IsConst)) { |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 13737 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
| 13738 | Info.CheckingForUndefinedBehavior = true; |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13739 | (void)::EvaluateAsRValue(Info, this, EVResult.Val); |
| Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 13740 | } |
| 13741 | } |
| 13742 | |
| Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 13743 | bool Expr::EvalResult::isGlobalLValue() const { |
| 13744 | assert(Val.isLValue()); |
| 13745 | return IsGlobalLValue(Val.getLValueBase()); |
| 13746 | } |
| Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 13747 | |
| 13748 | |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13749 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 13750 | /// an integer constant expression. |
| 13751 | |
| 13752 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 13753 | /// comma, etc |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13754 | |
| 13755 | // CheckICE - This function does the fundamental ICE checking: the returned |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13756 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
| 13757 | // and a (possibly null) SourceLocation indicating the location of the problem. |
| 13758 | // |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13759 | // Note that to reduce code duplication, this helper does no evaluation |
| 13760 | // itself; the caller checks whether the expression is evaluatable, and |
| 13761 | // in the rare cases where CheckICE actually cares about the evaluated |
| George Burgess IV | 5731707 | 2017-02-02 07:53:55 +0000 | [diff] [blame] | 13762 | // value, it calls into Evaluate. |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13763 | |
| Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 13764 | namespace { |
| 13765 | |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13766 | enum ICEKind { |
| 13767 | /// This expression is an ICE. |
| 13768 | IK_ICE, |
| 13769 | /// This expression is not an ICE, but if it isn't evaluated, it's |
| 13770 | /// a legal subexpression for an ICE. This return value is used to handle |
| 13771 | /// the comma operator in C99 mode, and non-constant subexpressions. |
| 13772 | IK_ICEIfUnevaluated, |
| 13773 | /// This expression is not an ICE, and is not a legal subexpression for one. |
| 13774 | IK_NotICE |
| 13775 | }; |
| 13776 | |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13777 | struct ICEDiag { |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13778 | ICEKind Kind; |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13779 | SourceLocation Loc; |
| 13780 | |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13781 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13782 | }; |
| 13783 | |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 13784 | } |
| Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 13785 | |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13786 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
| 13787 | |
| 13788 | 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] | 13789 | |
| Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 13790 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13791 | Expr::EvalResult EVResult; |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 13792 | Expr::EvalStatus Status; |
| 13793 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
| 13794 | |
| 13795 | Info.InConstantContext = true; |
| 13796 | if (!::EvaluateAsRValue(E, EVResult, Ctx, Info) || EVResult.HasSideEffects || |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13797 | !EVResult.Val.isInt()) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 13798 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13799 | |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13800 | return NoDiag(); |
| 13801 | } |
| 13802 | |
| Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 13803 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13804 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13805 | if (!E->getType()->isIntegralOrEnumerationType()) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 13806 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13807 | |
| 13808 | switch (E->getStmtClass()) { |
| John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 13809 | #define ABSTRACT_STMT(Node) |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13810 | #define STMT(Node, Base) case Expr::Node##Class: |
| 13811 | #define EXPR(Node, Base) |
| 13812 | #include "clang/AST/StmtNodes.inc" |
| 13813 | case Expr::PredefinedExprClass: |
| 13814 | case Expr::FloatingLiteralClass: |
| 13815 | case Expr::ImaginaryLiteralClass: |
| 13816 | case Expr::StringLiteralClass: |
| 13817 | case Expr::ArraySubscriptExprClass: |
| Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 13818 | case Expr::OMPArraySectionExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13819 | case Expr::MemberExprClass: |
| 13820 | case Expr::CompoundAssignOperatorClass: |
| 13821 | case Expr::CompoundLiteralExprClass: |
| 13822 | case Expr::ExtVectorElementExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13823 | case Expr::DesignatedInitExprClass: |
| Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 13824 | case Expr::ArrayInitLoopExprClass: |
| 13825 | case Expr::ArrayInitIndexExprClass: |
| Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 13826 | case Expr::NoInitExprClass: |
| 13827 | case Expr::DesignatedInitUpdateExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13828 | case Expr::ImplicitValueInitExprClass: |
| 13829 | case Expr::ParenListExprClass: |
| 13830 | case Expr::VAArgExprClass: |
| 13831 | case Expr::AddrLabelExprClass: |
| 13832 | case Expr::StmtExprClass: |
| 13833 | case Expr::CXXMemberCallExprClass: |
| Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 13834 | case Expr::CUDAKernelCallExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13835 | case Expr::CXXDynamicCastExprClass: |
| 13836 | case Expr::CXXTypeidExprClass: |
| Francois Pichet | 5cc0a67 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 13837 | case Expr::CXXUuidofExprClass: |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 13838 | case Expr::MSPropertyRefExprClass: |
| Alexey Bataev | f763027 | 2015-11-25 12:01:00 +0000 | [diff] [blame] | 13839 | case Expr::MSPropertySubscriptExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13840 | case Expr::CXXNullPtrLiteralExprClass: |
| Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 13841 | case Expr::UserDefinedLiteralClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13842 | case Expr::CXXThisExprClass: |
| 13843 | case Expr::CXXThrowExprClass: |
| 13844 | case Expr::CXXNewExprClass: |
| 13845 | case Expr::CXXDeleteExprClass: |
| 13846 | case Expr::CXXPseudoDestructorExprClass: |
| 13847 | case Expr::UnresolvedLookupExprClass: |
| Kaelyn Takata | e1f49d5 | 2014-10-27 18:07:20 +0000 | [diff] [blame] | 13848 | case Expr::TypoExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13849 | case Expr::DependentScopeDeclRefExprClass: |
| 13850 | case Expr::CXXConstructExprClass: |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 13851 | case Expr::CXXInheritedCtorInitExprClass: |
| Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 13852 | case Expr::CXXStdInitializerListExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13853 | case Expr::CXXBindTemporaryExprClass: |
| John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 13854 | case Expr::ExprWithCleanupsClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13855 | case Expr::CXXTemporaryObjectExprClass: |
| 13856 | case Expr::CXXUnresolvedConstructExprClass: |
| 13857 | case Expr::CXXDependentScopeMemberExprClass: |
| 13858 | case Expr::UnresolvedMemberExprClass: |
| 13859 | case Expr::ObjCStringLiteralClass: |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 13860 | case Expr::ObjCBoxedExprClass: |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 13861 | case Expr::ObjCArrayLiteralClass: |
| 13862 | case Expr::ObjCDictionaryLiteralClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13863 | case Expr::ObjCEncodeExprClass: |
| 13864 | case Expr::ObjCMessageExprClass: |
| 13865 | case Expr::ObjCSelectorExprClass: |
| 13866 | case Expr::ObjCProtocolExprClass: |
| 13867 | case Expr::ObjCIvarRefExprClass: |
| 13868 | case Expr::ObjCPropertyRefExprClass: |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 13869 | case Expr::ObjCSubscriptRefExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13870 | case Expr::ObjCIsaExprClass: |
| Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 13871 | case Expr::ObjCAvailabilityCheckExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13872 | case Expr::ShuffleVectorExprClass: |
| Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 13873 | case Expr::ConvertVectorExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13874 | case Expr::BlockExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13875 | case Expr::NoStmtClass: |
| John McCall | 8d69a21 | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 13876 | case Expr::OpaqueValueExprClass: |
| Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 13877 | case Expr::PackExpansionExprClass: |
| Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 13878 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
| Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 13879 | case Expr::FunctionParmPackExprClass: |
| Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 13880 | case Expr::AsTypeExprClass: |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 13881 | case Expr::ObjCIndirectCopyRestoreExprClass: |
| Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 13882 | case Expr::MaterializeTemporaryExprClass: |
| John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 13883 | case Expr::PseudoObjectExprClass: |
| Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 13884 | case Expr::AtomicExprClass: |
| Douglas Gregor | e31e606 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 13885 | case Expr::LambdaExprClass: |
| Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 13886 | case Expr::CXXFoldExprClass: |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 13887 | case Expr::CoawaitExprClass: |
| Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 13888 | case Expr::DependentCoawaitExprClass: |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 13889 | case Expr::CoyieldExprClass: |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 13890 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 13891 | |
| Richard Smith | f137f93 | 2014-01-25 20:50:08 +0000 | [diff] [blame] | 13892 | case Expr::InitListExprClass: { |
| 13893 | // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the |
| 13894 | // form "T x = { a };" is equivalent to "T x = a;". |
| 13895 | // Unless we're initializing a reference, T is a scalar as it is known to be |
| 13896 | // of integral or enumeration type. |
| 13897 | if (E->isRValue()) |
| 13898 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
| 13899 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 13900 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| Richard Smith | f137f93 | 2014-01-25 20:50:08 +0000 | [diff] [blame] | 13901 | } |
| 13902 | |
| Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 13903 | case Expr::SizeOfPackExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13904 | case Expr::GNUNullExprClass: |
| Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 13905 | case Expr::SourceLocExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13906 | return NoDiag(); |
| 13907 | |
| John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 13908 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 13909 | return |
| 13910 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 13911 | |
| Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 13912 | case Expr::ConstantExprClass: |
| 13913 | return CheckICE(cast<ConstantExpr>(E)->getSubExpr(), Ctx); |
| 13914 | |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13915 | case Expr::ParenExprClass: |
| 13916 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
| Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 13917 | case Expr::GenericSelectionExprClass: |
| 13918 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13919 | case Expr::IntegerLiteralClass: |
| Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 13920 | case Expr::FixedPointLiteralClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13921 | case Expr::CharacterLiteralClass: |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 13922 | case Expr::ObjCBoolLiteralExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13923 | case Expr::CXXBoolLiteralExprClass: |
| Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 13924 | case Expr::CXXScalarValueInitExprClass: |
| Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 13925 | case Expr::TypeTraitExprClass: |
| John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 13926 | case Expr::ArrayTypeTraitExprClass: |
| John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 13927 | case Expr::ExpressionTraitExprClass: |
| Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 13928 | case Expr::CXXNoexceptExprClass: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13929 | return NoDiag(); |
| 13930 | case Expr::CallExprClass: |
| Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 13931 | case Expr::CXXOperatorCallExprClass: { |
| Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 13932 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 13933 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 13934 | // contain an operand of (pointer to) function type. |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13935 | const CallExpr *CE = cast<CallExpr>(E); |
| Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 13936 | if (CE->getBuiltinCallee()) |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13937 | return CheckEvalInICE(E, Ctx); |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 13938 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13939 | } |
| Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 13940 | case Expr::DeclRefExprClass: { |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13941 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 13942 | return NoDiag(); |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 13943 | const ValueDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 13944 | if (Ctx.getLangOpts().CPlusPlus && |
| Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 13945 | D && IsConstNonVolatile(D->getType())) { |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13946 | // Parameter variables are never constants. Without this check, |
| 13947 | // getAnyInitializer() can find a default argument, which leads |
| 13948 | // to chaos. |
| 13949 | if (isa<ParmVarDecl>(D)) |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13950 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13951 | |
| 13952 | // C++ 7.1.5.1p2 |
| 13953 | // A variable of non-volatile const-qualified integral or enumeration |
| 13954 | // type initialized by an ICE can be used in ICEs. |
| 13955 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
| Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 13956 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13957 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
| Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 13958 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 13959 | const VarDecl *VD; |
| 13960 | // Look for a declaration of this variable that has an initializer, and |
| 13961 | // check whether it is an ICE. |
| 13962 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 13963 | return NoDiag(); |
| 13964 | else |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13965 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13966 | } |
| 13967 | } |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 13968 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 13969 | } |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13970 | case Expr::UnaryOperatorClass: { |
| 13971 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 13972 | switch (Exp->getOpcode()) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 13973 | case UO_PostInc: |
| 13974 | case UO_PostDec: |
| 13975 | case UO_PreInc: |
| 13976 | case UO_PreDec: |
| 13977 | case UO_AddrOf: |
| 13978 | case UO_Deref: |
| Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 13979 | case UO_Coawait: |
| Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 13980 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 13981 | // subexpressions of constant expressions, but they can never be ICEs |
| 13982 | // because an ICE cannot contain an lvalue operand. |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 13983 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 13984 | case UO_Extension: |
| 13985 | case UO_LNot: |
| 13986 | case UO_Plus: |
| 13987 | case UO_Minus: |
| 13988 | case UO_Not: |
| 13989 | case UO_Real: |
| 13990 | case UO_Imag: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13991 | return CheckICE(Exp->getSubExpr(), Ctx); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13992 | } |
| Reid Kleckner | e540d97 | 2018-11-01 17:51:48 +0000 | [diff] [blame] | 13993 | llvm_unreachable("invalid unary operator class"); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 13994 | } |
| 13995 | case Expr::OffsetOfExprClass: { |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 13996 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 13997 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
| 13998 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
| 13999 | // compliance: we should warn earlier for offsetof expressions with |
| 14000 | // array subscripts that aren't ICEs, and if the array subscripts |
| 14001 | // are ICEs, the value of the offsetof must be an integer constant. |
| 14002 | return CheckEvalInICE(E, Ctx); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14003 | } |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 14004 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 14005 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 14006 | if ((Exp->getKind() == UETT_SizeOf) && |
| 14007 | Exp->getTypeOfArgument()->isVariableArrayType()) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 14008 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14009 | return NoDiag(); |
| 14010 | } |
| 14011 | case Expr::BinaryOperatorClass: { |
| 14012 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 14013 | switch (Exp->getOpcode()) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 14014 | case BO_PtrMemD: |
| 14015 | case BO_PtrMemI: |
| 14016 | case BO_Assign: |
| 14017 | case BO_MulAssign: |
| 14018 | case BO_DivAssign: |
| 14019 | case BO_RemAssign: |
| 14020 | case BO_AddAssign: |
| 14021 | case BO_SubAssign: |
| 14022 | case BO_ShlAssign: |
| 14023 | case BO_ShrAssign: |
| 14024 | case BO_AndAssign: |
| 14025 | case BO_XorAssign: |
| 14026 | case BO_OrAssign: |
| Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 14027 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 14028 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 14029 | // contain an lvalue operand. |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 14030 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14031 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 14032 | case BO_Mul: |
| 14033 | case BO_Div: |
| 14034 | case BO_Rem: |
| 14035 | case BO_Add: |
| 14036 | case BO_Sub: |
| 14037 | case BO_Shl: |
| 14038 | case BO_Shr: |
| 14039 | case BO_LT: |
| 14040 | case BO_GT: |
| 14041 | case BO_LE: |
| 14042 | case BO_GE: |
| 14043 | case BO_EQ: |
| 14044 | case BO_NE: |
| 14045 | case BO_And: |
| 14046 | case BO_Xor: |
| 14047 | case BO_Or: |
| Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 14048 | case BO_Comma: |
| 14049 | case BO_Cmp: { |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14050 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 14051 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 14052 | if (Exp->getOpcode() == BO_Div || |
| 14053 | Exp->getOpcode() == BO_Rem) { |
| Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 14054 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14055 | // we don't evaluate one. |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14056 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 14057 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14058 | if (REval == 0) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 14059 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14060 | if (REval.isSigned() && REval.isAllOnesValue()) { |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 14061 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14062 | if (LEval.isMinSignedValue()) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 14063 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14064 | } |
| 14065 | } |
| 14066 | } |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 14067 | if (Exp->getOpcode() == BO_Comma) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 14068 | if (Ctx.getLangOpts().C99) { |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14069 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 14070 | // if it isn't evaluated. |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14071 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 14072 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14073 | } else { |
| 14074 | // In both C89 and C++, commas in ICEs are illegal. |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 14075 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14076 | } |
| 14077 | } |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14078 | return Worst(LHSResult, RHSResult); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14079 | } |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 14080 | case BO_LAnd: |
| 14081 | case BO_LOr: { |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14082 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 14083 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14084 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14085 | // Rare case where the RHS has a comma "side-effect"; we need |
| 14086 | // to actually check the condition to see whether the side |
| 14087 | // with the comma is evaluated. |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 14088 | if ((Exp->getOpcode() == BO_LAnd) != |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 14089 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14090 | return RHSResult; |
| 14091 | return NoDiag(); |
| 14092 | } |
| 14093 | |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14094 | return Worst(LHSResult, RHSResult); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14095 | } |
| 14096 | } |
| Reid Kleckner | e540d97 | 2018-11-01 17:51:48 +0000 | [diff] [blame] | 14097 | llvm_unreachable("invalid binary operator kind"); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14098 | } |
| 14099 | case Expr::ImplicitCastExprClass: |
| 14100 | case Expr::CStyleCastExprClass: |
| 14101 | case Expr::CXXFunctionalCastExprClass: |
| 14102 | case Expr::CXXStaticCastExprClass: |
| 14103 | case Expr::CXXReinterpretCastExprClass: |
| Richard Smith | c3e31e7 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 14104 | case Expr::CXXConstCastExprClass: |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 14105 | case Expr::ObjCBridgedCastExprClass: { |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14106 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 14107 | if (isa<ExplicitCastExpr>(E)) { |
| 14108 | if (const FloatingLiteral *FL |
| 14109 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 14110 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 14111 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 14112 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 14113 | bool Ignored; |
| 14114 | // If the value does not fit in the destination type, the behavior is |
| 14115 | // undefined, so we are not required to treat it as a constant |
| 14116 | // expression. |
| 14117 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 14118 | llvm::APFloat::rmTowardZero, |
| 14119 | &Ignored) & APFloat::opInvalidOp) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 14120 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 14121 | return NoDiag(); |
| 14122 | } |
| 14123 | } |
| Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 14124 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 14125 | case CK_LValueToRValue: |
| David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 14126 | case CK_AtomicToNonAtomic: |
| 14127 | case CK_NonAtomicToAtomic: |
| Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 14128 | case CK_NoOp: |
| 14129 | case CK_IntegralToBoolean: |
| 14130 | case CK_IntegralCast: |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14131 | return CheckICE(SubExpr, Ctx); |
| Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 14132 | default: |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 14133 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 14134 | } |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14135 | } |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 14136 | case Expr::BinaryConditionalOperatorClass: { |
| 14137 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 14138 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14139 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 14140 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14141 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
| 14142 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
| 14143 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
| Richard Smith | 74fc721 | 2012-12-28 12:53:55 +0000 | [diff] [blame] | 14144 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 14145 | return FalseResult; |
| 14146 | } |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14147 | case Expr::ConditionalOperatorClass: { |
| 14148 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 14149 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 14150 | // then only the true side is actually considered in an integer constant |
| 14151 | // expression, and it is fully evaluated. This is an important GNU |
| 14152 | // extension. See GCC PR38377 for discussion. |
| 14153 | if (const CallExpr *CallCE |
| 14154 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
| Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 14155 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
| Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 14156 | return CheckEvalInICE(E, Ctx); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14157 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14158 | if (CondResult.Kind == IK_NotICE) |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14159 | return CondResult; |
| Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 14160 | |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 14161 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 14162 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 14163 | |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14164 | if (TrueResult.Kind == IK_NotICE) |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14165 | return TrueResult; |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14166 | if (FalseResult.Kind == IK_NotICE) |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14167 | return FalseResult; |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14168 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14169 | return CondResult; |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14170 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14171 | return NoDiag(); |
| 14172 | // Rare case where the diagnostics depend on which side is evaluated |
| 14173 | // Note that if we get here, CondResult is 0, and at least one of |
| 14174 | // TrueResult and FalseResult is non-zero. |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14175 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14176 | return FalseResult; |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14177 | return TrueResult; |
| 14178 | } |
| 14179 | case Expr::CXXDefaultArgExprClass: |
| 14180 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 14181 | case Expr::CXXDefaultInitExprClass: |
| 14182 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14183 | case Expr::ChooseExprClass: { |
| Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 14184 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14185 | } |
| Erik Pilkington | eee944e | 2019-07-02 18:28:13 +0000 | [diff] [blame] | 14186 | case Expr::BuiltinBitCastExprClass: { |
| 14187 | if (!checkBitCastConstexprEligibility(nullptr, Ctx, cast<CastExpr>(E))) |
| 14188 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
| 14189 | return CheckICE(cast<CastExpr>(E)->getSubExpr(), Ctx); |
| 14190 | } |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14191 | } |
| 14192 | |
| David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 14193 | llvm_unreachable("Invalid StmtClass!"); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14194 | } |
| 14195 | |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 14196 | /// Evaluate an expression as a C++11 integral constant expression. |
| Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 14197 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 14198 | const Expr *E, |
| 14199 | llvm::APSInt *Value, |
| 14200 | SourceLocation *Loc) { |
| Erich Keane | 1ddd4bf | 2018-07-20 17:42:09 +0000 | [diff] [blame] | 14201 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 14202 | if (Loc) *Loc = E->getExprLoc(); |
| 14203 | return false; |
| 14204 | } |
| 14205 | |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 14206 | APValue Result; |
| 14207 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
| Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 14208 | return false; |
| 14209 | |
| Richard Smith | 98710fc | 2014-11-13 23:03:19 +0000 | [diff] [blame] | 14210 | if (!Result.isInt()) { |
| 14211 | if (Loc) *Loc = E->getExprLoc(); |
| 14212 | return false; |
| 14213 | } |
| 14214 | |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 14215 | if (Value) *Value = Result.getInt(); |
| Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 14216 | return true; |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 14217 | } |
| 14218 | |
| Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 14219 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
| 14220 | SourceLocation *Loc) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 14221 | assert(!isValueDependent() && |
| 14222 | "Expression evaluator can't be called on a dependent expression."); |
| 14223 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 14224 | if (Ctx.getLangOpts().CPlusPlus11) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 14225 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr, Loc); |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 14226 | |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14227 | ICEDiag D = CheckICE(this, Ctx); |
| 14228 | if (D.Kind != IK_ICE) { |
| 14229 | if (Loc) *Loc = D.Loc; |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14230 | return false; |
| 14231 | } |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 14232 | return true; |
| 14233 | } |
| 14234 | |
| Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 14235 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx, |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 14236 | SourceLocation *Loc, bool isEvaluated) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 14237 | assert(!isValueDependent() && |
| 14238 | "Expression evaluator can't be called on a dependent expression."); |
| 14239 | |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 14240 | if (Ctx.getLangOpts().CPlusPlus11) |
| Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 14241 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 14242 | |
| 14243 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 14244 | return false; |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 14245 | |
| Richard Smith | 5c40f09 | 2015-12-04 03:00:44 +0000 | [diff] [blame] | 14246 | // The only possible side-effects here are due to UB discovered in the |
| 14247 | // evaluation (for instance, INT_MAX + 1). In such a case, we are still |
| 14248 | // required to treat the expression as an ICE, so we produce the folded |
| 14249 | // value. |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 14250 | EvalResult ExprResult; |
| 14251 | Expr::EvalStatus Status; |
| 14252 | EvalInfo Info(Ctx, Status, EvalInfo::EM_IgnoreSideEffects); |
| 14253 | Info.InConstantContext = true; |
| 14254 | |
| 14255 | if (!::EvaluateAsInt(this, ExprResult, Ctx, SE_AllowSideEffects, Info)) |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14256 | llvm_unreachable("ICE cannot be evaluated!"); |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 14257 | |
| 14258 | Value = ExprResult.Val.getInt(); |
| John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 14259 | return true; |
| 14260 | } |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 14261 | |
| Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 14262 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 14263 | assert(!isValueDependent() && |
| 14264 | "Expression evaluator can't be called on a dependent expression."); |
| 14265 | |
| Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 14266 | return CheckICE(this, Ctx).Kind == IK_ICE; |
| Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 14267 | } |
| 14268 | |
| Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 14269 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 14270 | SourceLocation *Loc) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 14271 | assert(!isValueDependent() && |
| 14272 | "Expression evaluator can't be called on a dependent expression."); |
| 14273 | |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 14274 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 14275 | // issues. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 14276 | assert(Ctx.getLangOpts().CPlusPlus); |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 14277 | |
| Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 14278 | // Build evaluation settings. |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 14279 | Expr::EvalStatus Status; |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 14280 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 14281 | Status.Diag = &Diags; |
| Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 14282 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 14283 | |
| 14284 | APValue Scratch; |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 14285 | bool IsConstExpr = |
| 14286 | ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch) && |
| 14287 | // FIXME: We don't produce a diagnostic for this, but the callers that |
| 14288 | // call us on arbitrary full-expressions should generally not care. |
| Richard Smith | da1b434 | 2019-09-27 01:26:47 +0000 | [diff] [blame] | 14289 | Info.discardCleanups() && !Status.HasSideEffects; |
| Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 14290 | |
| 14291 | if (!Diags.empty()) { |
| 14292 | IsConstExpr = false; |
| 14293 | if (Loc) *Loc = Diags[0].first; |
| 14294 | } else if (!IsConstExpr) { |
| 14295 | // FIXME: This shouldn't happen. |
| 14296 | if (Loc) *Loc = getExprLoc(); |
| 14297 | } |
| 14298 | |
| 14299 | return IsConstExpr; |
| 14300 | } |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 14301 | |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14302 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
| 14303 | const FunctionDecl *Callee, |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 14304 | ArrayRef<const Expr*> Args, |
| 14305 | const Expr *This) const { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 14306 | assert(!isValueDependent() && |
| 14307 | "Expression evaluator can't be called on a dependent expression."); |
| 14308 | |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14309 | Expr::EvalStatus Status; |
| 14310 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
| Eric Fiselier | 680e865 | 2019-03-08 22:06:48 +0000 | [diff] [blame] | 14311 | Info.InConstantContext = true; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14312 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 14313 | LValue ThisVal; |
| 14314 | const LValue *ThisPtr = nullptr; |
| 14315 | if (This) { |
| 14316 | #ifndef NDEBUG |
| 14317 | auto *MD = dyn_cast<CXXMethodDecl>(Callee); |
| 14318 | assert(MD && "Don't provide `this` for non-methods."); |
| 14319 | assert(!MD->isStatic() && "Don't provide `this` for static methods."); |
| 14320 | #endif |
| 14321 | if (EvaluateObjectArgument(Info, This, ThisVal)) |
| 14322 | ThisPtr = &ThisVal; |
| 14323 | if (Info.EvalStatus.HasSideEffects) |
| 14324 | return false; |
| 14325 | } |
| 14326 | |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14327 | ArgVector ArgValues(Args.size()); |
| 14328 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 14329 | I != E; ++I) { |
| Nick Lewycky | f0202ca | 2014-12-16 06:12:01 +0000 | [diff] [blame] | 14330 | if ((*I)->isValueDependent() || |
| 14331 | !Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14332 | // If evaluation fails, throw away the argument entirely. |
| 14333 | ArgValues[I - Args.begin()] = APValue(); |
| 14334 | if (Info.EvalStatus.HasSideEffects) |
| 14335 | return false; |
| 14336 | } |
| 14337 | |
| 14338 | // Build fake call to Callee. |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 14339 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14340 | ArgValues.data()); |
| Richard Smith | 457226e | 2019-09-23 03:48:44 +0000 | [diff] [blame] | 14341 | return Evaluate(Value, Info, this) && Info.discardCleanups() && |
| 14342 | !Info.EvalStatus.HasSideEffects; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14343 | } |
| 14344 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 14345 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 14346 | SmallVectorImpl< |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 14347 | PartialDiagnosticAt> &Diags) { |
| 14348 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 14349 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 14350 | // ASTs which we build for dependent expressions. |
| 14351 | if (FD->isDependentContext()) |
| 14352 | return true; |
| 14353 | |
| 14354 | Expr::EvalStatus Status; |
| 14355 | Status.Diag = &Diags; |
| 14356 | |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 14357 | EvalInfo Info(FD->getASTContext(), Status, EvalInfo::EM_ConstantExpression); |
| Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 14358 | Info.InConstantContext = true; |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 14359 | Info.CheckingPotentialConstantExpression = true; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 14360 | |
| Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 14361 | // The constexpr VM attempts to compile all methods to bytecode here. |
| 14362 | if (Info.EnableNewConstInterp) { |
| 14363 | auto &InterpCtx = Info.Ctx.getInterpContext(); |
| 14364 | switch (InterpCtx.isPotentialConstantExpr(Info, FD)) { |
| 14365 | case interp::InterpResult::Success: |
| 14366 | case interp::InterpResult::Fail: |
| 14367 | return Diags.empty(); |
| 14368 | case interp::InterpResult::Bail: |
| 14369 | break; |
| 14370 | } |
| 14371 | } |
| 14372 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 14373 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 14374 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 14375 | |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 14376 | // Fabricate an arbitrary expression on the stack and pretend that it |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 14377 | // is a temporary being used as the 'this' pointer. |
| 14378 | LValue This; |
| 14379 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
| Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 14380 | This.set({&VIE, Info.CurrentCall->Index}); |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 14381 | |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 14382 | ArrayRef<const Expr*> Args; |
| 14383 | |
| Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 14384 | APValue Scratch; |
| Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 14385 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 14386 | // Evaluate the call as a constant initializer, to allow the construction |
| 14387 | // of objects of non-literal types. |
| 14388 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 14389 | HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch); |
| 14390 | } else { |
| 14391 | SourceLocation Loc = FD->getLocation(); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 14392 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : nullptr, |
| Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 14393 | Args, FD->getBody(), Info, Scratch, nullptr); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 14394 | } |
| Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 14395 | |
| 14396 | return Diags.empty(); |
| 14397 | } |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14398 | |
| 14399 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
| 14400 | const FunctionDecl *FD, |
| 14401 | SmallVectorImpl< |
| 14402 | PartialDiagnosticAt> &Diags) { |
| Dmitri Gribenko | 04323c2 | 2019-05-17 17:16:53 +0000 | [diff] [blame] | 14403 | assert(!E->isValueDependent() && |
| 14404 | "Expression evaluator can't be called on a dependent expression."); |
| 14405 | |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14406 | Expr::EvalStatus Status; |
| 14407 | Status.Diag = &Diags; |
| 14408 | |
| 14409 | EvalInfo Info(FD->getASTContext(), Status, |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 14410 | EvalInfo::EM_ConstantExpressionUnevaluated); |
| Eric Fiselier | 680e865 | 2019-03-08 22:06:48 +0000 | [diff] [blame] | 14411 | Info.InConstantContext = true; |
| Richard Smith | 045b227 | 2019-09-10 21:24:09 +0000 | [diff] [blame] | 14412 | Info.CheckingPotentialConstantExpression = true; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14413 | |
| 14414 | // Fabricate a call stack frame to give the arguments a plausible cover story. |
| 14415 | ArrayRef<const Expr*> Args; |
| 14416 | ArgVector ArgValues(0); |
| Gauthier Harnisch | 0bb4d46 | 2019-06-15 08:32:56 +0000 | [diff] [blame] | 14417 | bool Success = EvaluateArgs(Args, ArgValues, Info, FD); |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14418 | (void)Success; |
| 14419 | assert(Success && |
| 14420 | "Failed to set up arguments for potential constant evaluation"); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 14421 | CallStackFrame Frame(Info, SourceLocation(), FD, nullptr, ArgValues.data()); |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 14422 | |
| 14423 | APValue ResultScratch; |
| 14424 | Evaluate(ResultScratch, Info, E); |
| 14425 | return Diags.empty(); |
| 14426 | } |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 14427 | |
| 14428 | bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
| 14429 | unsigned Type) const { |
| 14430 | if (!getType()->isPointerType()) |
| 14431 | return false; |
| 14432 | |
| 14433 | Expr::EvalStatus Status; |
| 14434 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
| George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 14435 | return tryEvaluateBuiltinObjectSize(this, Type, Info, Result); |
| George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 14436 | } |