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 | |
| 35 | #include "clang/AST/APValue.h" |
| 36 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 37 | #include "clang/AST/ASTDiagnostic.h" |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 38 | #include "clang/AST/ASTLambda.h" |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 39 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 40 | #include "clang/AST/Expr.h" |
Tim Northover | 314fbfa | 2018-11-02 13:14:11 +0000 | [diff] [blame] | 41 | #include "clang/AST/OSLog.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 42 | #include "clang/AST/RecordLayout.h" |
Seo Sanghyeon | 1904f44 | 2008-07-08 07:23:12 +0000 | [diff] [blame] | 43 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 44 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 45 | #include "clang/Basic/Builtins.h" |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 46 | #include "clang/Basic/FixedPoint.h" |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 47 | #include "clang/Basic/TargetInfo.h" |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 48 | #include "llvm/Support/SaveAndRestore.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 49 | #include "llvm/Support/raw_ostream.h" |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 50 | #include <cstring> |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 51 | #include <functional> |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 52 | |
Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 53 | #define DEBUG_TYPE "exprconstant" |
| 54 | |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 55 | using namespace clang; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 56 | using llvm::APSInt; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 57 | using llvm::APFloat; |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 58 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 59 | static bool IsGlobalLValue(APValue::LValueBase B); |
| 60 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 61 | namespace { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 62 | struct LValue; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 63 | struct CallStackFrame; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 64 | struct EvalInfo; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 65 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 66 | static QualType getType(APValue::LValueBase B) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 67 | if (!B) return QualType(); |
Richard Smith | 69cf59e | 2018-03-09 02:00:01 +0000 | [diff] [blame] | 68 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 69 | // 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] | 70 | // this actually matters for arrays of unknown bound. Eg: |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 71 | // |
| 72 | // extern int arr[]; void f() { extern int arr[3]; }; |
| 73 | // constexpr int *p = &arr[1]; // valid? |
Richard Smith | 69cf59e | 2018-03-09 02:00:01 +0000 | [diff] [blame] | 74 | // |
| 75 | // For now, we take the array bound from the most recent declaration. |
| 76 | for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl; |
| 77 | Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())) { |
| 78 | QualType T = Redecl->getType(); |
| 79 | if (!T->isIncompleteArrayType()) |
| 80 | return T; |
| 81 | } |
| 82 | return D->getType(); |
| 83 | } |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 84 | |
| 85 | const Expr *Base = B.get<const Expr*>(); |
| 86 | |
| 87 | // For a materialized temporary, the type of the temporary we materialized |
| 88 | // may not be the type of the expression. |
| 89 | if (const MaterializeTemporaryExpr *MTE = |
| 90 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 91 | SmallVector<const Expr *, 2> CommaLHSs; |
| 92 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 93 | const Expr *Temp = MTE->GetTemporaryExpr(); |
| 94 | const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs, |
| 95 | Adjustments); |
| 96 | // Keep any cv-qualifiers from the reference if we generated a temporary |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 97 | // for it directly. Otherwise use the type after adjustment. |
| 98 | if (!Adjustments.empty()) |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 99 | return Inner->getType(); |
| 100 | } |
| 101 | |
| 102 | return Base->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 105 | /// 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] | 106 | /// field or base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 107 | static |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 108 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 109 | APValue::BaseOrMemberType Value; |
| 110 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 111 | return Value; |
| 112 | } |
| 113 | |
| 114 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 115 | /// field declaration. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 116 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 117 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 118 | } |
| 119 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 120 | /// base class declaration. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 121 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 122 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 123 | } |
| 124 | /// Determine whether this LValue path entry for a base class names a virtual |
| 125 | /// base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 126 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 127 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 128 | } |
| 129 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 130 | /// Given a CallExpr, try to get the alloc_size attribute. May return null. |
| 131 | static const AllocSizeAttr *getAllocSizeAttr(const CallExpr *CE) { |
| 132 | const FunctionDecl *Callee = CE->getDirectCallee(); |
| 133 | return Callee ? Callee->getAttr<AllocSizeAttr>() : nullptr; |
| 134 | } |
| 135 | |
| 136 | /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr. |
| 137 | /// This will look through a single cast. |
| 138 | /// |
| 139 | /// Returns null if we couldn't unwrap a function with alloc_size. |
| 140 | static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) { |
| 141 | if (!E->getType()->isPointerType()) |
| 142 | return nullptr; |
| 143 | |
| 144 | E = E->IgnoreParens(); |
| 145 | // 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] | 146 | // probably be a cast of some kind. In exotic cases, we might also see a |
| 147 | // top-level ExprWithCleanups. Ignore them either way. |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 148 | if (const auto *FE = dyn_cast<FullExpr>(E)) |
| 149 | E = FE->getSubExpr()->IgnoreParens(); |
George Burgess IV | 4763876 | 2018-03-07 04:52:34 +0000 | [diff] [blame] | 150 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 151 | if (const auto *Cast = dyn_cast<CastExpr>(E)) |
| 152 | E = Cast->getSubExpr()->IgnoreParens(); |
| 153 | |
| 154 | if (const auto *CE = dyn_cast<CallExpr>(E)) |
| 155 | return getAllocSizeAttr(CE) ? CE : nullptr; |
| 156 | return nullptr; |
| 157 | } |
| 158 | |
| 159 | /// Determines whether or not the given Base contains a call to a function |
| 160 | /// with the alloc_size attribute. |
| 161 | static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) { |
| 162 | const auto *E = Base.dyn_cast<const Expr *>(); |
| 163 | return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E); |
| 164 | } |
| 165 | |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 166 | /// The bound to claim that an array of unknown bound has. |
| 167 | /// The value in MostDerivedArraySize is undefined in this case. So, set it |
| 168 | /// to an arbitrary value that's likely to loudly break things if it's used. |
| 169 | static const uint64_t AssumedSizeForUnsizedArray = |
| 170 | std::numeric_limits<uint64_t>::max() / 2; |
| 171 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 172 | /// Determines if an LValue with the given LValueBase will have an unsized |
| 173 | /// array in its designator. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 174 | /// Find the path length and type of the most-derived subobject in the given |
| 175 | /// path, and find the size of the containing array, if any. |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 176 | static unsigned |
| 177 | findMostDerivedSubobject(ASTContext &Ctx, APValue::LValueBase Base, |
| 178 | ArrayRef<APValue::LValuePathEntry> Path, |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 179 | uint64_t &ArraySize, QualType &Type, bool &IsArray, |
| 180 | bool &FirstEntryIsUnsizedArray) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 181 | // This only accepts LValueBases from APValues, and APValues don't support |
| 182 | // arrays that lack size info. |
| 183 | assert(!isBaseAnAllocSizeCall(Base) && |
| 184 | "Unsized arrays shouldn't appear here"); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 185 | unsigned MostDerivedLength = 0; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 186 | Type = getType(Base); |
| 187 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 188 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 189 | if (Type->isArrayType()) { |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 190 | const ArrayType *AT = Ctx.getAsArrayType(Type); |
| 191 | Type = AT->getElementType(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 192 | MostDerivedLength = I + 1; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 193 | IsArray = true; |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 194 | |
| 195 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 196 | ArraySize = CAT->getSize().getZExtValue(); |
| 197 | } else { |
| 198 | assert(I == 0 && "unexpected unsized array designator"); |
| 199 | FirstEntryIsUnsizedArray = true; |
| 200 | ArraySize = AssumedSizeForUnsizedArray; |
| 201 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 202 | } else if (Type->isAnyComplexType()) { |
| 203 | const ComplexType *CT = Type->castAs<ComplexType>(); |
| 204 | Type = CT->getElementType(); |
| 205 | ArraySize = 2; |
| 206 | MostDerivedLength = I + 1; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 207 | IsArray = true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 208 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 209 | Type = FD->getType(); |
| 210 | ArraySize = 0; |
| 211 | MostDerivedLength = I + 1; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 212 | IsArray = false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 213 | } else { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 214 | // Path[I] describes a base class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 215 | ArraySize = 0; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 216 | IsArray = false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 217 | } |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 218 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 219 | return MostDerivedLength; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 222 | // The order of this enum is important for diagnostics. |
| 223 | enum CheckSubobjectKind { |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 224 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 225 | CSK_This, CSK_Real, CSK_Imag |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 228 | /// A path from a glvalue to a subobject of that glvalue. |
| 229 | struct SubobjectDesignator { |
| 230 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 231 | /// lvalues can still be folded, but they are not core constant expressions |
| 232 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
Akira Hatanaka | 3a94477 | 2016-06-30 00:07:17 +0000 | [diff] [blame] | 233 | unsigned Invalid : 1; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 234 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 235 | /// Is this a pointer one past the end of an object? |
Akira Hatanaka | 3a94477 | 2016-06-30 00:07:17 +0000 | [diff] [blame] | 236 | unsigned IsOnePastTheEnd : 1; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 237 | |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 238 | /// Indicator of whether the first entry is an unsized array. |
| 239 | unsigned FirstEntryIsAnUnsizedArray : 1; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 240 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 241 | /// Indicator of whether the most-derived object is an array element. |
Akira Hatanaka | 3a94477 | 2016-06-30 00:07:17 +0000 | [diff] [blame] | 242 | unsigned MostDerivedIsArrayElement : 1; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 243 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 244 | /// The length of the path to the most-derived object of which this is a |
| 245 | /// subobject. |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 246 | unsigned MostDerivedPathLength : 28; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 247 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 248 | /// The size of the array of which the most-derived object is an element. |
| 249 | /// This will always be 0 if the most-derived object is not an array |
| 250 | /// element. 0 is not an indicator of whether or not the most-derived object |
| 251 | /// is an array, however, because 0-length arrays are allowed. |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 252 | /// |
| 253 | /// If the current array is an unsized array, the value of this is |
| 254 | /// undefined. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 255 | uint64_t MostDerivedArraySize; |
| 256 | |
| 257 | /// The type of the most derived object referred to by this address. |
| 258 | QualType MostDerivedType; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 259 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 260 | typedef APValue::LValuePathEntry PathEntry; |
| 261 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 262 | /// The entries on the path from the glvalue to the designated subobject. |
| 263 | SmallVector<PathEntry, 8> Entries; |
| 264 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 265 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 266 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 267 | explicit SubobjectDesignator(QualType T) |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 268 | : Invalid(false), IsOnePastTheEnd(false), |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 269 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 270 | MostDerivedPathLength(0), MostDerivedArraySize(0), |
| 271 | MostDerivedType(T) {} |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 272 | |
| 273 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 274 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 275 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 276 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
| 277 | assert(V.isLValue() && "Non-LValue used to make an LValue designator?"); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 278 | if (!Invalid) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 279 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 280 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 281 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 282 | if (V.getLValueBase()) { |
| 283 | bool IsArray = false; |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 284 | bool FirstIsUnsizedArray = false; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 285 | MostDerivedPathLength = findMostDerivedSubobject( |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 286 | Ctx, V.getLValueBase(), V.getLValuePath(), MostDerivedArraySize, |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 287 | MostDerivedType, IsArray, FirstIsUnsizedArray); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 288 | MostDerivedIsArrayElement = IsArray; |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 289 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 290 | } |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 294 | void setInvalid() { |
| 295 | Invalid = true; |
| 296 | Entries.clear(); |
| 297 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 298 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 299 | /// Determine whether the most derived subobject is an array without a |
| 300 | /// known bound. |
| 301 | bool isMostDerivedAnUnsizedArray() const { |
| 302 | assert(!Invalid && "Calling this makes no sense on invalid designators"); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 303 | return Entries.size() == 1 && FirstEntryIsAnUnsizedArray; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /// Determine what the most derived array's size is. Results in an assertion |
| 307 | /// failure if the most derived array lacks a size. |
| 308 | uint64_t getMostDerivedArraySize() const { |
| 309 | assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size"); |
| 310 | return MostDerivedArraySize; |
| 311 | } |
| 312 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 313 | /// Determine whether this is a one-past-the-end pointer. |
| 314 | bool isOnePastTheEnd() const { |
Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 315 | assert(!Invalid); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 316 | if (IsOnePastTheEnd) |
| 317 | return true; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 318 | if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement && |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 319 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
| 320 | return true; |
| 321 | return false; |
| 322 | } |
| 323 | |
Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 324 | /// Get the range of valid index adjustments in the form |
| 325 | /// {maximum value that can be subtracted from this pointer, |
| 326 | /// maximum value that can be added to this pointer} |
| 327 | std::pair<uint64_t, uint64_t> validIndexAdjustments() { |
| 328 | if (Invalid || isMostDerivedAnUnsizedArray()) |
| 329 | return {0, 0}; |
| 330 | |
| 331 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 332 | // nonarray object behaves the same as a pointer to the first element of |
| 333 | // an array of length one with the type of the object as its element type. |
| 334 | bool IsArray = MostDerivedPathLength == Entries.size() && |
| 335 | MostDerivedIsArrayElement; |
| 336 | uint64_t ArrayIndex = |
| 337 | IsArray ? Entries.back().ArrayIndex : (uint64_t)IsOnePastTheEnd; |
| 338 | uint64_t ArraySize = |
| 339 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
| 340 | return {ArrayIndex, ArraySize - ArrayIndex}; |
| 341 | } |
| 342 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 343 | /// Check that this refers to a valid subobject. |
| 344 | bool isValidSubobject() const { |
| 345 | if (Invalid) |
| 346 | return false; |
| 347 | return !isOnePastTheEnd(); |
| 348 | } |
| 349 | /// Check that this refers to a valid subobject, and if not, produce a |
| 350 | /// relevant diagnostic and set the designator as invalid. |
| 351 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 352 | |
Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 353 | /// Get the type of the designated object. |
| 354 | QualType getType(ASTContext &Ctx) const { |
| 355 | assert(!Invalid && "invalid designator has no subobject type"); |
| 356 | return MostDerivedPathLength == Entries.size() |
| 357 | ? MostDerivedType |
| 358 | : Ctx.getRecordType(getAsBaseClass(Entries.back())); |
| 359 | } |
| 360 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 361 | /// Update this designator to refer to the first element within this array. |
| 362 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 363 | PathEntry Entry; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 364 | Entry.ArrayIndex = 0; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 365 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 366 | |
| 367 | // This is a most-derived object. |
| 368 | MostDerivedType = CAT->getElementType(); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 369 | MostDerivedIsArrayElement = true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 370 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 371 | MostDerivedPathLength = Entries.size(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 372 | } |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 373 | /// Update this designator to refer to the first element within the array of |
| 374 | /// elements of type T. This is an array of unknown size. |
| 375 | void addUnsizedArrayUnchecked(QualType ElemTy) { |
| 376 | PathEntry Entry; |
| 377 | Entry.ArrayIndex = 0; |
| 378 | Entries.push_back(Entry); |
| 379 | |
| 380 | MostDerivedType = ElemTy; |
| 381 | MostDerivedIsArrayElement = true; |
| 382 | // The value in MostDerivedArraySize is undefined in this case. So, set it |
| 383 | // to an arbitrary value that's likely to loudly break things if it's |
| 384 | // used. |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 385 | MostDerivedArraySize = AssumedSizeForUnsizedArray; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 386 | MostDerivedPathLength = Entries.size(); |
| 387 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 388 | /// Update this designator to refer to the given base or member of this |
| 389 | /// object. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 390 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 391 | PathEntry Entry; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 392 | APValue::BaseOrMemberType Value(D, Virtual); |
| 393 | Entry.BaseOrMember = Value.getOpaqueValue(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 394 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 395 | |
| 396 | // If this isn't a base class, it's a new most-derived object. |
| 397 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 398 | MostDerivedType = FD->getType(); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 399 | MostDerivedIsArrayElement = false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 400 | MostDerivedArraySize = 0; |
| 401 | MostDerivedPathLength = Entries.size(); |
| 402 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 403 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 404 | /// Update this designator to refer to the given complex component. |
| 405 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
| 406 | PathEntry Entry; |
| 407 | Entry.ArrayIndex = Imag; |
| 408 | Entries.push_back(Entry); |
| 409 | |
| 410 | // This is technically a most-derived object, though in practice this |
| 411 | // is unlikely to matter. |
| 412 | MostDerivedType = EltTy; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 413 | MostDerivedIsArrayElement = true; |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 414 | MostDerivedArraySize = 2; |
| 415 | MostDerivedPathLength = Entries.size(); |
| 416 | } |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 417 | void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E); |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 418 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, |
| 419 | const APSInt &N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 420 | /// Add N to the address of this subobject. |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 421 | void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N) { |
| 422 | if (Invalid || !N) return; |
| 423 | uint64_t TruncatedN = N.extOrTrunc(64).getZExtValue(); |
| 424 | if (isMostDerivedAnUnsizedArray()) { |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 425 | diagnoseUnsizedArrayPointerArithmetic(Info, E); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 426 | // Can't verify -- trust that the user is doing the right thing (or if |
| 427 | // not, trust that the caller will catch the bad behavior). |
| 428 | // FIXME: Should we reject if this overflows, at least? |
| 429 | Entries.back().ArrayIndex += TruncatedN; |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 434 | // nonarray object behaves the same as a pointer to the first element of |
| 435 | // an array of length one with the type of the object as its element type. |
| 436 | bool IsArray = MostDerivedPathLength == Entries.size() && |
| 437 | MostDerivedIsArrayElement; |
| 438 | uint64_t ArrayIndex = |
| 439 | IsArray ? Entries.back().ArrayIndex : (uint64_t)IsOnePastTheEnd; |
| 440 | uint64_t ArraySize = |
| 441 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
| 442 | |
| 443 | if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) { |
| 444 | // Calculate the actual index in a wide enough type, so we can include |
| 445 | // it in the note. |
| 446 | N = N.extend(std::max<unsigned>(N.getBitWidth() + 1, 65)); |
| 447 | (llvm::APInt&)N += ArrayIndex; |
| 448 | assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index"); |
| 449 | diagnosePointerArithmetic(Info, E, N); |
| 450 | setInvalid(); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | ArrayIndex += TruncatedN; |
| 455 | assert(ArrayIndex <= ArraySize && |
| 456 | "bounds check succeeded for out-of-bounds index"); |
| 457 | |
| 458 | if (IsArray) |
| 459 | Entries.back().ArrayIndex = ArrayIndex; |
| 460 | else |
| 461 | IsOnePastTheEnd = (ArrayIndex != 0); |
| 462 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 463 | }; |
| 464 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 465 | /// A stack frame in the constexpr call stack. |
| 466 | struct CallStackFrame { |
| 467 | EvalInfo &Info; |
| 468 | |
| 469 | /// Parent - The caller of this stack frame. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 470 | CallStackFrame *Caller; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 471 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 472 | /// Callee - The function which was called. |
| 473 | const FunctionDecl *Callee; |
| 474 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 475 | /// This - The binding for the this pointer in this call, if any. |
| 476 | const LValue *This; |
| 477 | |
Nick Lewycky | e2b2caa | 2013-09-22 10:07:22 +0000 | [diff] [blame] | 478 | /// Arguments - Parameter bindings for this function call, indexed by |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 479 | /// parameters' function scope indices. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 480 | APValue *Arguments; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 481 | |
Eli Friedman | 4830ec8 | 2012-06-25 21:21:08 +0000 | [diff] [blame] | 482 | // Note that we intentionally use std::map here so that references to |
| 483 | // values are stable. |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 484 | typedef std::pair<const void *, unsigned> MapKeyTy; |
| 485 | typedef std::map<MapKeyTy, APValue> MapTy; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 486 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 487 | MapTy Temporaries; |
| 488 | |
Alexander Shaposhnikov | fbcf29b | 2016-09-19 15:57:29 +0000 | [diff] [blame] | 489 | /// CallLoc - The location of the call expression for this call. |
| 490 | SourceLocation CallLoc; |
| 491 | |
| 492 | /// Index - The call index of this call. |
| 493 | unsigned Index; |
| 494 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 495 | /// The stack of integers for tracking version numbers for temporaries. |
| 496 | SmallVector<unsigned, 2> TempVersionStack = {1}; |
| 497 | unsigned CurTempVersion = TempVersionStack.back(); |
| 498 | |
| 499 | unsigned getTempVersion() const { return TempVersionStack.back(); } |
| 500 | |
| 501 | void pushTempVersion() { |
| 502 | TempVersionStack.push_back(++CurTempVersion); |
| 503 | } |
| 504 | |
| 505 | void popTempVersion() { |
| 506 | TempVersionStack.pop_back(); |
| 507 | } |
| 508 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 509 | // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 510 | // on the overall stack usage of deeply-recursing constexpr evaluations. |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 511 | // (We should cache this map rather than recomputing it repeatedly.) |
| 512 | // But let's try this and see how it goes; we can look into caching the map |
| 513 | // as a later change. |
| 514 | |
| 515 | /// LambdaCaptureFields - Mapping from captured variables/this to |
| 516 | /// corresponding data members in the closure class. |
| 517 | llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; |
| 518 | FieldDecl *LambdaThisCaptureField; |
| 519 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 520 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 521 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 522 | APValue *Arguments); |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 523 | ~CallStackFrame(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 524 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 525 | // Return the temporary for Key whose version number is Version. |
| 526 | APValue *getTemporary(const void *Key, unsigned Version) { |
| 527 | MapKeyTy KV(Key, Version); |
| 528 | auto LB = Temporaries.lower_bound(KV); |
| 529 | if (LB != Temporaries.end() && LB->first == KV) |
| 530 | return &LB->second; |
| 531 | // Pair (Key,Version) wasn't found in the map. Check that no elements |
| 532 | // in the map have 'Key' as their key. |
| 533 | assert((LB == Temporaries.end() || LB->first.first != Key) && |
| 534 | (LB == Temporaries.begin() || std::prev(LB)->first.first != Key) && |
| 535 | "Element with key 'Key' found in map"); |
| 536 | return nullptr; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 537 | } |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 538 | |
| 539 | // Return the current temporary for Key in the map. |
| 540 | APValue *getCurrentTemporary(const void *Key) { |
| 541 | auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX)); |
| 542 | if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key) |
| 543 | return &std::prev(UB)->second; |
| 544 | return nullptr; |
| 545 | } |
| 546 | |
| 547 | // Return the version number of the current temporary for Key. |
| 548 | unsigned getCurrentTemporaryVersion(const void *Key) const { |
| 549 | auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX)); |
| 550 | if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key) |
| 551 | return std::prev(UB)->first.second; |
| 552 | return 0; |
| 553 | } |
| 554 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 555 | APValue &createTemporary(const void *Key, bool IsLifetimeExtended); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 556 | }; |
| 557 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 558 | /// Temporarily override 'this'. |
| 559 | class ThisOverrideRAII { |
| 560 | public: |
| 561 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
| 562 | : Frame(Frame), OldThis(Frame.This) { |
| 563 | if (Enable) |
| 564 | Frame.This = NewThis; |
| 565 | } |
| 566 | ~ThisOverrideRAII() { |
| 567 | Frame.This = OldThis; |
| 568 | } |
| 569 | private: |
| 570 | CallStackFrame &Frame; |
| 571 | const LValue *OldThis; |
| 572 | }; |
| 573 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 574 | /// A partial diagnostic which we might know in advance that we are not going |
| 575 | /// to emit. |
| 576 | class OptionalDiagnostic { |
| 577 | PartialDiagnostic *Diag; |
| 578 | |
| 579 | public: |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 580 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = nullptr) |
| 581 | : Diag(Diag) {} |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 582 | |
| 583 | template<typename T> |
| 584 | OptionalDiagnostic &operator<<(const T &v) { |
| 585 | if (Diag) |
| 586 | *Diag << v; |
| 587 | return *this; |
| 588 | } |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 589 | |
| 590 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 591 | if (Diag) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 592 | SmallVector<char, 32> Buffer; |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 593 | I.toString(Buffer); |
| 594 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 595 | } |
| 596 | return *this; |
| 597 | } |
| 598 | |
| 599 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 600 | if (Diag) { |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 601 | // FIXME: Force the precision of the source value down so we don't |
| 602 | // print digits which are usually useless (we don't really care here if |
| 603 | // we truncate a digit by accident in edge cases). Ideally, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 604 | // APFloat::toString would automatically print the shortest |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 605 | // representation which rounds to the correct value, but it's a bit |
| 606 | // tricky to implement. |
| 607 | unsigned precision = |
| 608 | llvm::APFloat::semanticsPrecision(F.getSemantics()); |
| 609 | precision = (precision * 59 + 195) / 196; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 610 | SmallVector<char, 32> Buffer; |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 611 | F.toString(Buffer, precision); |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 612 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 613 | } |
| 614 | return *this; |
| 615 | } |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 616 | |
| 617 | OptionalDiagnostic &operator<<(const APFixedPoint &FX) { |
| 618 | if (Diag) { |
| 619 | SmallVector<char, 32> Buffer; |
| 620 | FX.toString(Buffer); |
| 621 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 622 | } |
| 623 | return *this; |
| 624 | } |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 625 | }; |
| 626 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 627 | /// A cleanup, and a flag indicating whether it is lifetime-extended. |
| 628 | class Cleanup { |
| 629 | llvm::PointerIntPair<APValue*, 1, bool> Value; |
| 630 | |
| 631 | public: |
| 632 | Cleanup(APValue *Val, bool IsLifetimeExtended) |
| 633 | : Value(Val, IsLifetimeExtended) {} |
| 634 | |
| 635 | bool isLifetimeExtended() const { return Value.getInt(); } |
| 636 | void endLifetime() { |
| 637 | *Value.getPointer() = APValue(); |
| 638 | } |
| 639 | }; |
| 640 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 641 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 642 | /// information about a subexpression as it is folded. It retains information |
| 643 | /// about the AST context, but also maintains information about the folded |
| 644 | /// expression. |
| 645 | /// |
| 646 | /// If an expression could be evaluated, it is still possible it is not a C |
| 647 | /// "integer constant expression" or constant expression. If not, this struct |
| 648 | /// captures information about how and why not. |
| 649 | /// |
| 650 | /// One bit of information passed *into* the request for constant folding |
| 651 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 652 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 653 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 654 | /// certain things in certain situations. |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 655 | struct EvalInfo { |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 656 | ASTContext &Ctx; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 657 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 658 | /// EvalStatus - Contains information about the evaluation. |
| 659 | Expr::EvalStatus &EvalStatus; |
| 660 | |
| 661 | /// CurrentCall - The top of the constexpr call stack. |
| 662 | CallStackFrame *CurrentCall; |
| 663 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 664 | /// CallStackDepth - The number of calls in the call stack right now. |
| 665 | unsigned CallStackDepth; |
| 666 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 667 | /// NextCallIndex - The next call index to assign. |
| 668 | unsigned NextCallIndex; |
| 669 | |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 670 | /// StepsLeft - The remaining number of evaluation steps we're permitted |
| 671 | /// to perform. This is essentially a limit for the number of statements |
| 672 | /// we will evaluate. |
| 673 | unsigned StepsLeft; |
| 674 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 675 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 676 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 677 | CallStackFrame BottomFrame; |
| 678 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 679 | /// A stack of values whose lifetimes end at the end of some surrounding |
| 680 | /// evaluation frame. |
| 681 | llvm::SmallVector<Cleanup, 16> CleanupStack; |
| 682 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 683 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 684 | /// evaluated, if any. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 685 | APValue::LValueBase EvaluatingDecl; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 686 | |
| 687 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 688 | /// declaration whose initializer is being evaluated, if any. |
| 689 | APValue *EvaluatingDeclValue; |
| 690 | |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 691 | /// EvaluatingObject - Pair of the AST node that an lvalue represents and |
| 692 | /// the call index that that lvalue was allocated in. |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 693 | typedef std::pair<APValue::LValueBase, std::pair<unsigned, unsigned>> |
| 694 | EvaluatingObject; |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 695 | |
| 696 | /// EvaluatingConstructors - Set of objects that are currently being |
| 697 | /// constructed. |
| 698 | llvm::DenseSet<EvaluatingObject> EvaluatingConstructors; |
| 699 | |
| 700 | struct EvaluatingConstructorRAII { |
| 701 | EvalInfo &EI; |
| 702 | EvaluatingObject Object; |
| 703 | bool DidInsert; |
| 704 | EvaluatingConstructorRAII(EvalInfo &EI, EvaluatingObject Object) |
| 705 | : EI(EI), Object(Object) { |
| 706 | DidInsert = EI.EvaluatingConstructors.insert(Object).second; |
| 707 | } |
| 708 | ~EvaluatingConstructorRAII() { |
| 709 | if (DidInsert) EI.EvaluatingConstructors.erase(Object); |
| 710 | } |
| 711 | }; |
| 712 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 713 | bool isEvaluatingConstructor(APValue::LValueBase Decl, unsigned CallIndex, |
| 714 | unsigned Version) { |
| 715 | return EvaluatingConstructors.count( |
| 716 | EvaluatingObject(Decl, {CallIndex, Version})); |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 719 | /// The current array initialization index, if we're performing array |
| 720 | /// initialization. |
| 721 | uint64_t ArrayInitIndex = -1; |
| 722 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 723 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 724 | /// notes attached to it will also be stored, otherwise they will not be. |
| 725 | bool HasActiveDiagnostic; |
| 726 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 727 | /// Have we emitted a diagnostic explaining why we couldn't constant |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 728 | /// fold (not just why it's not strictly a constant expression)? |
| 729 | bool HasFoldFailureDiagnostic; |
| 730 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 731 | /// Whether or not we're currently speculatively evaluating. |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 732 | bool IsSpeculativelyEvaluating; |
| 733 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 734 | /// Whether or not we're in a context where the front end requires a |
| 735 | /// constant value. |
| 736 | bool InConstantContext; |
| 737 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 738 | enum EvaluationMode { |
| 739 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 740 | /// is not a constant expression. |
| 741 | EM_ConstantExpression, |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 742 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 743 | /// Evaluate as a potential constant expression. Keep going if we hit a |
| 744 | /// construct that we can't evaluate yet (because we don't yet know the |
| 745 | /// value of something) but stop if we hit something that could never be |
| 746 | /// a constant expression. |
| 747 | EM_PotentialConstantExpression, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 748 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 749 | /// Fold the expression to a constant. Stop if we hit a side-effect that |
| 750 | /// we can't model. |
| 751 | EM_ConstantFold, |
| 752 | |
| 753 | /// Evaluate the expression looking for integer overflow and similar |
| 754 | /// issues. Don't worry about side-effects, and try to visit all |
| 755 | /// subexpressions. |
| 756 | EM_EvaluateForOverflow, |
| 757 | |
| 758 | /// Evaluate in any way we know how. Don't worry about side-effects that |
| 759 | /// can't be modeled. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 760 | EM_IgnoreSideEffects, |
| 761 | |
| 762 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 763 | /// is not a constant expression. Some expressions can be retried in the |
| 764 | /// optimizer if we don't constant fold them here, but in an unevaluated |
| 765 | /// context we try to fold them immediately since the optimizer never |
| 766 | /// gets a chance to look at it. |
| 767 | EM_ConstantExpressionUnevaluated, |
| 768 | |
| 769 | /// Evaluate as a potential constant expression. Keep going if we hit a |
| 770 | /// construct that we can't evaluate yet (because we don't yet know the |
| 771 | /// value of something) but stop if we hit something that could never be |
| 772 | /// a constant expression. Some expressions can be retried in the |
| 773 | /// optimizer if we don't constant fold them here, but in an unevaluated |
| 774 | /// context we try to fold them immediately since the optimizer never |
| 775 | /// gets a chance to look at it. |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 776 | EM_PotentialConstantExpressionUnevaluated, |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 777 | } EvalMode; |
| 778 | |
| 779 | /// Are we checking whether the expression is a potential constant |
| 780 | /// expression? |
| 781 | bool checkingPotentialConstantExpression() const { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 782 | return EvalMode == EM_PotentialConstantExpression || |
| 783 | EvalMode == EM_PotentialConstantExpressionUnevaluated; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /// Are we checking an expression for overflow? |
| 787 | // FIXME: We should check for any kind of undefined or suspicious behavior |
| 788 | // in such constructs, not just overflow. |
| 789 | bool checkingForOverflow() { return EvalMode == EM_EvaluateForOverflow; } |
| 790 | |
| 791 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 792 | : Ctx(const_cast<ASTContext &>(C)), EvalStatus(S), CurrentCall(nullptr), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 793 | CallStackDepth(0), NextCallIndex(1), |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 794 | StepsLeft(getLangOpts().ConstexprStepLimit), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 795 | BottomFrame(*this, SourceLocation(), nullptr, nullptr, nullptr), |
| 796 | EvaluatingDecl((const ValueDecl *)nullptr), |
| 797 | EvaluatingDeclValue(nullptr), HasActiveDiagnostic(false), |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 798 | HasFoldFailureDiagnostic(false), IsSpeculativelyEvaluating(false), |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 799 | InConstantContext(false), EvalMode(Mode) {} |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 800 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 801 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value) { |
| 802 | EvaluatingDecl = Base; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 803 | EvaluatingDeclValue = &Value; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 804 | EvaluatingConstructors.insert({Base, {0, 0}}); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 805 | } |
| 806 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 807 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 808 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 809 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 810 | // Don't perform any constexpr calls (other than the call we're checking) |
| 811 | // when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 812 | if (checkingPotentialConstantExpression() && CallStackDepth > 1) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 813 | return false; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 814 | if (NextCallIndex == 0) { |
| 815 | // NextCallIndex has wrapped around. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 816 | FFDiag(Loc, diag::note_constexpr_call_limit_exceeded); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 817 | return false; |
| 818 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 819 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 820 | return true; |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 821 | FFDiag(Loc, diag::note_constexpr_depth_limit_exceeded) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 822 | << getLangOpts().ConstexprCallDepth; |
| 823 | return false; |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 824 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 825 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 826 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
| 827 | assert(CallIndex && "no call index in getCallFrame"); |
| 828 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 829 | // be null in this loop. |
| 830 | CallStackFrame *Frame = CurrentCall; |
| 831 | while (Frame->Index > CallIndex) |
| 832 | Frame = Frame->Caller; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 833 | return (Frame->Index == CallIndex) ? Frame : nullptr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 836 | bool nextStep(const Stmt *S) { |
| 837 | if (!StepsLeft) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 838 | FFDiag(S->getBeginLoc(), diag::note_constexpr_step_limit_exceeded); |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 839 | return false; |
| 840 | } |
| 841 | --StepsLeft; |
| 842 | return true; |
| 843 | } |
| 844 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 845 | private: |
| 846 | /// Add a diagnostic to the diagnostics list. |
| 847 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 848 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 849 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 850 | return EvalStatus.Diag->back().second; |
| 851 | } |
| 852 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 853 | /// Add notes containing a call stack to the current point of evaluation. |
| 854 | void addCallStack(unsigned Limit); |
| 855 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 856 | private: |
| 857 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId, |
| 858 | unsigned ExtraNotes, bool IsCCEDiag) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 859 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 860 | if (EvalStatus.Diag) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 861 | // If we have a prior diagnostic, it will be noting that the expression |
| 862 | // isn't a constant expression. This diagnostic is more important, |
| 863 | // unless we require this evaluation to produce a constant expression. |
| 864 | // |
| 865 | // FIXME: We might want to show both diagnostics to the user in |
| 866 | // EM_ConstantFold mode. |
| 867 | if (!EvalStatus.Diag->empty()) { |
| 868 | switch (EvalMode) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 869 | case EM_ConstantFold: |
| 870 | case EM_IgnoreSideEffects: |
| 871 | case EM_EvaluateForOverflow: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 872 | if (!HasFoldFailureDiagnostic) |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 873 | break; |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 874 | // We've already failed to fold something. Keep that diagnostic. |
Galina Kistanova | f87496d | 2017-06-03 06:31:42 +0000 | [diff] [blame] | 875 | LLVM_FALLTHROUGH; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 876 | case EM_ConstantExpression: |
| 877 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 878 | case EM_ConstantExpressionUnevaluated: |
| 879 | case EM_PotentialConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 880 | HasActiveDiagnostic = false; |
| 881 | return OptionalDiagnostic(); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 882 | } |
| 883 | } |
| 884 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 885 | unsigned CallStackNotes = CallStackDepth - 1; |
| 886 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 887 | if (Limit) |
| 888 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 889 | if (checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 890 | CallStackNotes = 0; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 891 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 892 | HasActiveDiagnostic = true; |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 893 | HasFoldFailureDiagnostic = !IsCCEDiag; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 894 | EvalStatus.Diag->clear(); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 895 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 896 | addDiag(Loc, DiagId); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 897 | if (!checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 898 | addCallStack(Limit); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 899 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 900 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 901 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 902 | return OptionalDiagnostic(); |
| 903 | } |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 904 | public: |
| 905 | // Diagnose that the evaluation could not be folded (FF => FoldFailure) |
| 906 | OptionalDiagnostic |
| 907 | FFDiag(SourceLocation Loc, |
| 908 | diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr, |
| 909 | unsigned ExtraNotes = 0) { |
| 910 | return Diag(Loc, DiagId, ExtraNotes, false); |
| 911 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 912 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 913 | OptionalDiagnostic FFDiag(const Expr *E, diag::kind DiagId |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 914 | = diag::note_invalid_subexpr_in_const_expr, |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 915 | unsigned ExtraNotes = 0) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 916 | if (EvalStatus.Diag) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 917 | return Diag(E->getExprLoc(), DiagId, ExtraNotes, /*IsCCEDiag*/false); |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 918 | HasActiveDiagnostic = false; |
| 919 | return OptionalDiagnostic(); |
| 920 | } |
| 921 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 922 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 923 | /// expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 924 | /// |
| 925 | /// FIXME: Stop evaluating if we're in EM_ConstantExpression or |
| 926 | /// EM_PotentialConstantExpression mode and we produce one of these. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 927 | OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 928 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 929 | unsigned ExtraNotes = 0) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 930 | // Don't override a previous diagnostic. Don't bother collecting |
| 931 | // diagnostics if we're evaluating for overflow. |
Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 932 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 933 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 934 | return OptionalDiagnostic(); |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 935 | } |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 936 | return Diag(Loc, DiagId, ExtraNotes, true); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 937 | } |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 938 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind DiagId |
| 939 | = diag::note_invalid_subexpr_in_const_expr, |
| 940 | unsigned ExtraNotes = 0) { |
| 941 | return CCEDiag(E->getExprLoc(), DiagId, ExtraNotes); |
| 942 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 943 | /// Add a note to a prior diagnostic. |
| 944 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 945 | if (!HasActiveDiagnostic) |
| 946 | return OptionalDiagnostic(); |
| 947 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 948 | } |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 949 | |
| 950 | /// Add a stack of notes to a prior diagnostic. |
| 951 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 952 | if (HasActiveDiagnostic) { |
| 953 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 954 | Diags.begin(), Diags.end()); |
| 955 | } |
| 956 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 957 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 958 | /// Should we continue evaluation after encountering a side-effect that we |
| 959 | /// couldn't model? |
| 960 | bool keepEvaluatingAfterSideEffect() { |
| 961 | switch (EvalMode) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 962 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 963 | case EM_PotentialConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 964 | case EM_EvaluateForOverflow: |
| 965 | case EM_IgnoreSideEffects: |
| 966 | return true; |
| 967 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 968 | case EM_ConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 969 | case EM_ConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 970 | case EM_ConstantFold: |
| 971 | return false; |
| 972 | } |
Aaron Ballman | f682f53 | 2013-11-06 18:15:02 +0000 | [diff] [blame] | 973 | llvm_unreachable("Missed EvalMode case"); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /// Note that we have had a side-effect, and determine whether we should |
| 977 | /// keep evaluating. |
| 978 | bool noteSideEffect() { |
| 979 | EvalStatus.HasSideEffects = true; |
| 980 | return keepEvaluatingAfterSideEffect(); |
| 981 | } |
| 982 | |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 983 | /// Should we continue evaluation after encountering undefined behavior? |
| 984 | bool keepEvaluatingAfterUndefinedBehavior() { |
| 985 | switch (EvalMode) { |
| 986 | case EM_EvaluateForOverflow: |
| 987 | case EM_IgnoreSideEffects: |
| 988 | case EM_ConstantFold: |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 989 | return true; |
| 990 | |
| 991 | case EM_PotentialConstantExpression: |
| 992 | case EM_PotentialConstantExpressionUnevaluated: |
| 993 | case EM_ConstantExpression: |
| 994 | case EM_ConstantExpressionUnevaluated: |
| 995 | return false; |
| 996 | } |
| 997 | llvm_unreachable("Missed EvalMode case"); |
| 998 | } |
| 999 | |
| 1000 | /// Note that we hit something that was technically undefined behavior, but |
| 1001 | /// that we can evaluate past it (such as signed overflow or floating-point |
| 1002 | /// division by zero.) |
| 1003 | bool noteUndefinedBehavior() { |
| 1004 | EvalStatus.HasUndefinedBehavior = true; |
| 1005 | return keepEvaluatingAfterUndefinedBehavior(); |
| 1006 | } |
| 1007 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1008 | /// Should we continue evaluation as much as possible after encountering a |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1009 | /// construct which can't be reduced to a value? |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1010 | bool keepEvaluatingAfterFailure() { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1011 | if (!StepsLeft) |
| 1012 | return false; |
| 1013 | |
| 1014 | switch (EvalMode) { |
| 1015 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1016 | case EM_PotentialConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1017 | case EM_EvaluateForOverflow: |
| 1018 | return true; |
| 1019 | |
| 1020 | case EM_ConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1021 | case EM_ConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1022 | case EM_ConstantFold: |
| 1023 | case EM_IgnoreSideEffects: |
| 1024 | return false; |
| 1025 | } |
Aaron Ballman | f682f53 | 2013-11-06 18:15:02 +0000 | [diff] [blame] | 1026 | llvm_unreachable("Missed EvalMode case"); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1027 | } |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1028 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1029 | /// Notes that we failed to evaluate an expression that other expressions |
| 1030 | /// directly depend on, and determine if we should keep evaluating. This |
| 1031 | /// should only be called if we actually intend to keep evaluating. |
| 1032 | /// |
| 1033 | /// Call noteSideEffect() instead if we may be able to ignore the value that |
| 1034 | /// we failed to evaluate, e.g. if we failed to evaluate Foo() in: |
| 1035 | /// |
| 1036 | /// (Foo(), 1) // use noteSideEffect |
| 1037 | /// (Foo() || true) // use noteSideEffect |
| 1038 | /// Foo() + 1 // use noteFailure |
Justin Bogner | fe183d7 | 2016-10-17 06:46:35 +0000 | [diff] [blame] | 1039 | LLVM_NODISCARD bool noteFailure() { |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1040 | // Failure when evaluating some expression often means there is some |
| 1041 | // subexpression whose evaluation was skipped. Therefore, (because we |
| 1042 | // don't track whether we skipped an expression when unwinding after an |
| 1043 | // evaluation failure) every evaluation failure that bubbles up from a |
| 1044 | // subexpression implies that a side-effect has potentially happened. We |
| 1045 | // skip setting the HasSideEffects flag to true until we decide to |
| 1046 | // continue evaluating after that point, which happens here. |
| 1047 | bool KeepGoing = keepEvaluatingAfterFailure(); |
| 1048 | EvalStatus.HasSideEffects |= KeepGoing; |
| 1049 | return KeepGoing; |
| 1050 | } |
| 1051 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 1052 | class ArrayInitLoopIndex { |
| 1053 | EvalInfo &Info; |
| 1054 | uint64_t OuterIndex; |
| 1055 | |
| 1056 | public: |
| 1057 | ArrayInitLoopIndex(EvalInfo &Info) |
| 1058 | : Info(Info), OuterIndex(Info.ArrayInitIndex) { |
| 1059 | Info.ArrayInitIndex = 0; |
| 1060 | } |
| 1061 | ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; } |
| 1062 | |
| 1063 | operator uint64_t&() { return Info.ArrayInitIndex; } |
| 1064 | }; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1065 | }; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1066 | |
| 1067 | /// Object used to treat all foldable expressions as constant expressions. |
| 1068 | struct FoldConstant { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1069 | EvalInfo &Info; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1070 | bool Enabled; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1071 | bool HadNoPriorDiags; |
| 1072 | EvalInfo::EvaluationMode OldMode; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1073 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1074 | explicit FoldConstant(EvalInfo &Info, bool Enabled) |
| 1075 | : Info(Info), |
| 1076 | Enabled(Enabled), |
| 1077 | HadNoPriorDiags(Info.EvalStatus.Diag && |
| 1078 | Info.EvalStatus.Diag->empty() && |
| 1079 | !Info.EvalStatus.HasSideEffects), |
| 1080 | OldMode(Info.EvalMode) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1081 | if (Enabled && |
| 1082 | (Info.EvalMode == EvalInfo::EM_ConstantExpression || |
| 1083 | Info.EvalMode == EvalInfo::EM_ConstantExpressionUnevaluated)) |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1084 | Info.EvalMode = EvalInfo::EM_ConstantFold; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1085 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1086 | void keepDiagnostics() { Enabled = false; } |
| 1087 | ~FoldConstant() { |
| 1088 | if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() && |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1089 | !Info.EvalStatus.HasSideEffects) |
| 1090 | Info.EvalStatus.Diag->clear(); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1091 | Info.EvalMode = OldMode; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1092 | } |
| 1093 | }; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1094 | |
James Y Knight | 892b09b | 2018-10-10 02:53:43 +0000 | [diff] [blame] | 1095 | /// RAII object used to set the current evaluation mode to ignore |
| 1096 | /// side-effects. |
| 1097 | struct IgnoreSideEffectsRAII { |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1098 | EvalInfo &Info; |
| 1099 | EvalInfo::EvaluationMode OldMode; |
James Y Knight | 892b09b | 2018-10-10 02:53:43 +0000 | [diff] [blame] | 1100 | explicit IgnoreSideEffectsRAII(EvalInfo &Info) |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1101 | : Info(Info), OldMode(Info.EvalMode) { |
| 1102 | if (!Info.checkingPotentialConstantExpression()) |
James Y Knight | 892b09b | 2018-10-10 02:53:43 +0000 | [diff] [blame] | 1103 | Info.EvalMode = EvalInfo::EM_IgnoreSideEffects; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
James Y Knight | 892b09b | 2018-10-10 02:53:43 +0000 | [diff] [blame] | 1106 | ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; } |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1107 | }; |
| 1108 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1109 | /// RAII object used to optionally suppress diagnostics and side-effects from |
| 1110 | /// a speculative evaluation. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1111 | class SpeculativeEvaluationRAII { |
Chandler Carruth | bacb80d | 2017-08-16 07:22:49 +0000 | [diff] [blame] | 1112 | EvalInfo *Info = nullptr; |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1113 | Expr::EvalStatus OldStatus; |
| 1114 | bool OldIsSpeculativelyEvaluating; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1115 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1116 | void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) { |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1117 | Info = Other.Info; |
| 1118 | OldStatus = Other.OldStatus; |
Daniel Jasper | a7e061f | 2017-08-17 06:33:46 +0000 | [diff] [blame] | 1119 | OldIsSpeculativelyEvaluating = Other.OldIsSpeculativelyEvaluating; |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1120 | Other.Info = nullptr; |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void maybeRestoreState() { |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1124 | if (!Info) |
| 1125 | return; |
| 1126 | |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1127 | Info->EvalStatus = OldStatus; |
| 1128 | Info->IsSpeculativelyEvaluating = OldIsSpeculativelyEvaluating; |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1131 | public: |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1132 | SpeculativeEvaluationRAII() = default; |
| 1133 | |
| 1134 | SpeculativeEvaluationRAII( |
| 1135 | EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr) |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1136 | : Info(&Info), OldStatus(Info.EvalStatus), |
| 1137 | OldIsSpeculativelyEvaluating(Info.IsSpeculativelyEvaluating) { |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1138 | Info.EvalStatus.Diag = NewDiag; |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1139 | Info.IsSpeculativelyEvaluating = true; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1140 | } |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1141 | |
| 1142 | SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete; |
| 1143 | SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) { |
| 1144 | moveFromAndCancel(std::move(Other)); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1145 | } |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1146 | |
| 1147 | SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) { |
| 1148 | maybeRestoreState(); |
| 1149 | moveFromAndCancel(std::move(Other)); |
| 1150 | return *this; |
| 1151 | } |
| 1152 | |
| 1153 | ~SpeculativeEvaluationRAII() { maybeRestoreState(); } |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1154 | }; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1155 | |
| 1156 | /// RAII object wrapping a full-expression or block scope, and handling |
| 1157 | /// the ending of the lifetime of temporaries created within it. |
| 1158 | template<bool IsFullExpression> |
| 1159 | class ScopeRAII { |
| 1160 | EvalInfo &Info; |
| 1161 | unsigned OldStackSize; |
| 1162 | public: |
| 1163 | ScopeRAII(EvalInfo &Info) |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1164 | : Info(Info), OldStackSize(Info.CleanupStack.size()) { |
| 1165 | // Push a new temporary version. This is needed to distinguish between |
| 1166 | // temporaries created in different iterations of a loop. |
| 1167 | Info.CurrentCall->pushTempVersion(); |
| 1168 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1169 | ~ScopeRAII() { |
| 1170 | // Body moved to a static method to encourage the compiler to inline away |
| 1171 | // instances of this class. |
| 1172 | cleanup(Info, OldStackSize); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1173 | Info.CurrentCall->popTempVersion(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1174 | } |
| 1175 | private: |
| 1176 | static void cleanup(EvalInfo &Info, unsigned OldStackSize) { |
| 1177 | unsigned NewEnd = OldStackSize; |
| 1178 | for (unsigned I = OldStackSize, N = Info.CleanupStack.size(); |
| 1179 | I != N; ++I) { |
| 1180 | if (IsFullExpression && Info.CleanupStack[I].isLifetimeExtended()) { |
| 1181 | // Full-expression cleanup of a lifetime-extended temporary: nothing |
| 1182 | // to do, just move this cleanup to the right place in the stack. |
| 1183 | std::swap(Info.CleanupStack[I], Info.CleanupStack[NewEnd]); |
| 1184 | ++NewEnd; |
| 1185 | } else { |
| 1186 | // End the lifetime of the object. |
| 1187 | Info.CleanupStack[I].endLifetime(); |
| 1188 | } |
| 1189 | } |
| 1190 | Info.CleanupStack.erase(Info.CleanupStack.begin() + NewEnd, |
| 1191 | Info.CleanupStack.end()); |
| 1192 | } |
| 1193 | }; |
| 1194 | typedef ScopeRAII<false> BlockScopeRAII; |
| 1195 | typedef ScopeRAII<true> FullExpressionRAII; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1196 | } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1197 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1198 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 1199 | CheckSubobjectKind CSK) { |
| 1200 | if (Invalid) |
| 1201 | return false; |
| 1202 | if (isOnePastTheEnd()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1203 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1204 | << CSK; |
| 1205 | setInvalid(); |
| 1206 | return false; |
| 1207 | } |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 1208 | // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there |
| 1209 | // must actually be at least one array element; even a VLA cannot have a |
| 1210 | // 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] | 1211 | return true; |
| 1212 | } |
| 1213 | |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 1214 | void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, |
| 1215 | const Expr *E) { |
| 1216 | Info.CCEDiag(E, diag::note_constexpr_unsized_array_indexed); |
| 1217 | // Do not set the designator as invalid: we can represent this situation, |
| 1218 | // and correct handling of __builtin_object_size requires us to do so. |
| 1219 | } |
| 1220 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1221 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 1222 | const Expr *E, |
| 1223 | const APSInt &N) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1224 | // If we're complaining, we must be able to statically determine the size of |
| 1225 | // the most derived array. |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 1226 | if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1227 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1228 | << N << /*array*/ 0 |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1229 | << static_cast<unsigned>(getMostDerivedArraySize()); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1230 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1231 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1232 | << N << /*non-array*/ 1; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1233 | setInvalid(); |
| 1234 | } |
| 1235 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1236 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 1237 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1238 | APValue *Arguments) |
Samuel Antao | 1197a16 | 2016-09-19 18:13:13 +0000 | [diff] [blame] | 1239 | : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This), |
| 1240 | Arguments(Arguments), CallLoc(CallLoc), Index(Info.NextCallIndex++) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1241 | Info.CurrentCall = this; |
| 1242 | ++Info.CallStackDepth; |
| 1243 | } |
| 1244 | |
| 1245 | CallStackFrame::~CallStackFrame() { |
| 1246 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 1247 | --Info.CallStackDepth; |
| 1248 | Info.CurrentCall = Caller; |
| 1249 | } |
| 1250 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1251 | APValue &CallStackFrame::createTemporary(const void *Key, |
| 1252 | bool IsLifetimeExtended) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1253 | unsigned Version = Info.CurrentCall->getTempVersion(); |
| 1254 | APValue &Result = Temporaries[MapKeyTy(Key, Version)]; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1255 | assert(Result.isUninit() && "temporary created multiple times"); |
| 1256 | Info.CleanupStack.push_back(Cleanup(&Result, IsLifetimeExtended)); |
| 1257 | return Result; |
| 1258 | } |
| 1259 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1260 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1261 | |
| 1262 | void EvalInfo::addCallStack(unsigned Limit) { |
| 1263 | // Determine which calls to skip, if any. |
| 1264 | unsigned ActiveCalls = CallStackDepth - 1; |
| 1265 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 1266 | if (Limit && Limit < ActiveCalls) { |
| 1267 | SkipStart = Limit / 2 + Limit % 2; |
| 1268 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1271 | // Walk the call stack and add the diagnostics. |
| 1272 | unsigned CallIdx = 0; |
| 1273 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 1274 | Frame = Frame->Caller, ++CallIdx) { |
| 1275 | // Skip this call? |
| 1276 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 1277 | if (CallIdx == SkipStart) { |
| 1278 | // Note that we're skipping calls. |
| 1279 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 1280 | << unsigned(ActiveCalls - Limit); |
| 1281 | } |
| 1282 | continue; |
| 1283 | } |
| 1284 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 1285 | // Use a different note for an inheriting constructor, because from the |
| 1286 | // user's perspective it's not really a function at all. |
| 1287 | if (auto *CD = dyn_cast_or_null<CXXConstructorDecl>(Frame->Callee)) { |
| 1288 | if (CD->isInheritingConstructor()) { |
| 1289 | addDiag(Frame->CallLoc, diag::note_constexpr_inherited_ctor_call_here) |
| 1290 | << CD->getParent(); |
| 1291 | continue; |
| 1292 | } |
| 1293 | } |
| 1294 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1295 | SmallVector<char, 128> Buffer; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1296 | llvm::raw_svector_ostream Out(Buffer); |
| 1297 | describeCall(Frame, Out); |
| 1298 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 1299 | } |
| 1300 | } |
| 1301 | |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 1302 | /// Kinds of access we can perform on an object, for diagnostics. |
| 1303 | enum AccessKinds { |
| 1304 | AK_Read, |
| 1305 | AK_Assign, |
| 1306 | AK_Increment, |
| 1307 | AK_Decrement |
| 1308 | }; |
| 1309 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1310 | namespace { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1311 | struct ComplexValue { |
| 1312 | private: |
| 1313 | bool IsInt; |
| 1314 | |
| 1315 | public: |
| 1316 | APSInt IntReal, IntImag; |
| 1317 | APFloat FloatReal, FloatImag; |
| 1318 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1319 | ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {} |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1320 | |
| 1321 | void makeComplexFloat() { IsInt = false; } |
| 1322 | bool isComplexFloat() const { return !IsInt; } |
| 1323 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 1324 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 1325 | |
| 1326 | void makeComplexInt() { IsInt = true; } |
| 1327 | bool isComplexInt() const { return IsInt; } |
| 1328 | APSInt &getComplexIntReal() { return IntReal; } |
| 1329 | APSInt &getComplexIntImag() { return IntImag; } |
| 1330 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1331 | void moveInto(APValue &v) const { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1332 | if (isComplexFloat()) |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1333 | v = APValue(FloatReal, FloatImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1334 | else |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1335 | v = APValue(IntReal, IntImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1336 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1337 | void setFrom(const APValue &v) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1338 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 1339 | if (v.isComplexFloat()) { |
| 1340 | makeComplexFloat(); |
| 1341 | FloatReal = v.getComplexFloatReal(); |
| 1342 | FloatImag = v.getComplexFloatImag(); |
| 1343 | } else { |
| 1344 | makeComplexInt(); |
| 1345 | IntReal = v.getComplexIntReal(); |
| 1346 | IntImag = v.getComplexIntImag(); |
| 1347 | } |
| 1348 | } |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1349 | }; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1350 | |
| 1351 | struct LValue { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1352 | APValue::LValueBase Base; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1353 | CharUnits Offset; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1354 | SubobjectDesignator Designator; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1355 | bool IsNullPtr : 1; |
| 1356 | bool InvalidBase : 1; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1357 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1358 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1359 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 1360 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1361 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 1362 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1363 | bool isNullPointer() const { return IsNullPtr;} |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1364 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1365 | unsigned getLValueCallIndex() const { return Base.getCallIndex(); } |
| 1366 | unsigned getLValueVersion() const { return Base.getVersion(); } |
| 1367 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1368 | void moveInto(APValue &V) const { |
| 1369 | if (Designator.Invalid) |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1370 | V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1371 | else { |
| 1372 | assert(!InvalidBase && "APValues can't handle invalid LValue bases"); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1373 | V = APValue(Base, Offset, Designator.Entries, |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1374 | Designator.IsOnePastTheEnd, IsNullPtr); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1375 | } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1376 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1377 | void setFrom(ASTContext &Ctx, const APValue &V) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1378 | assert(V.isLValue() && "Setting LValue from a non-LValue?"); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1379 | Base = V.getLValueBase(); |
| 1380 | Offset = V.getLValueOffset(); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1381 | InvalidBase = false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1382 | Designator = SubobjectDesignator(Ctx, V); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1383 | IsNullPtr = V.isNullPointer(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1386 | void set(APValue::LValueBase B, bool BInvalid = false) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1387 | #ifndef NDEBUG |
| 1388 | // We only allow a few types of invalid bases. Enforce that here. |
| 1389 | if (BInvalid) { |
| 1390 | const auto *E = B.get<const Expr *>(); |
| 1391 | assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) && |
| 1392 | "Unexpected type of invalid base"); |
| 1393 | } |
| 1394 | #endif |
| 1395 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1396 | Base = B; |
Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1397 | Offset = CharUnits::fromQuantity(0); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1398 | InvalidBase = BInvalid; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1399 | Designator = SubobjectDesignator(getType(B)); |
Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1400 | IsNullPtr = false; |
| 1401 | } |
| 1402 | |
| 1403 | void setNull(QualType PointerTy, uint64_t TargetVal) { |
| 1404 | Base = (Expr *)nullptr; |
| 1405 | Offset = CharUnits::fromQuantity(TargetVal); |
| 1406 | InvalidBase = false; |
Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1407 | Designator = SubobjectDesignator(PointerTy->getPointeeType()); |
| 1408 | IsNullPtr = true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1411 | void setInvalid(APValue::LValueBase B, unsigned I = 0) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1412 | set(B, true); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 1415 | private: |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1416 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 1417 | // a diagnostic and mark the designator as invalid. |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 1418 | template <typename GenDiagType> |
| 1419 | bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1420 | if (Designator.Invalid) |
| 1421 | return false; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1422 | if (IsNullPtr) { |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 1423 | GenDiag(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1424 | Designator.setInvalid(); |
| 1425 | return false; |
| 1426 | } |
| 1427 | return true; |
| 1428 | } |
| 1429 | |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 1430 | public: |
| 1431 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 1432 | CheckSubobjectKind CSK) { |
| 1433 | return checkNullPointerDiagnosingWith([&Info, E, CSK] { |
| 1434 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) << CSK; |
| 1435 | }); |
| 1436 | } |
| 1437 | |
| 1438 | bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E, |
| 1439 | AccessKinds AK) { |
| 1440 | return checkNullPointerDiagnosingWith([&Info, E, AK] { |
| 1441 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
| 1442 | }); |
| 1443 | } |
| 1444 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1445 | // Check this LValue refers to an object. If not, set the designator to be |
| 1446 | // invalid and emit a diagnostic. |
| 1447 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
Richard Smith | 6c6bbfa | 2014-04-08 12:19:28 +0000 | [diff] [blame] | 1448 | return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) && |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1449 | Designator.checkSubobject(Info, E, CSK); |
| 1450 | } |
| 1451 | |
| 1452 | void addDecl(EvalInfo &Info, const Expr *E, |
| 1453 | const Decl *D, bool Virtual = false) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1454 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
| 1455 | Designator.addDeclUnchecked(D, Virtual); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1456 | } |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 1457 | void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) { |
| 1458 | if (!Designator.Entries.empty()) { |
| 1459 | Info.CCEDiag(E, diag::note_constexpr_unsupported_unsized_array); |
| 1460 | Designator.setInvalid(); |
| 1461 | return; |
| 1462 | } |
Richard Smith | efdb503 | 2017-11-15 03:03:56 +0000 | [diff] [blame] | 1463 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) { |
| 1464 | assert(getType(Base)->isPointerType() || getType(Base)->isArrayType()); |
| 1465 | Designator.FirstEntryIsAnUnsizedArray = true; |
| 1466 | Designator.addUnsizedArrayUnchecked(ElemTy); |
| 1467 | } |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1468 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1469 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1470 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
| 1471 | Designator.addArrayUnchecked(CAT); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1472 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1473 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1474 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
| 1475 | Designator.addComplexUnchecked(EltTy, Imag); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1476 | } |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1477 | void clearIsNullPointer() { |
| 1478 | IsNullPtr = false; |
| 1479 | } |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 1480 | void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E, |
| 1481 | const APSInt &Index, CharUnits ElementSize) { |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1482 | // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB, |
| 1483 | // but we're not required to diagnose it and it's valid in C++.) |
| 1484 | if (!Index) |
| 1485 | return; |
| 1486 | |
| 1487 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
| 1488 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
| 1489 | // offsets. |
| 1490 | uint64_t Offset64 = Offset.getQuantity(); |
| 1491 | uint64_t ElemSize64 = ElementSize.getQuantity(); |
| 1492 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
| 1493 | Offset = CharUnits::fromQuantity(Offset64 + ElemSize64 * Index64); |
| 1494 | |
| 1495 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1496 | Designator.adjustIndex(Info, E, Index); |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1497 | clearIsNullPointer(); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1498 | } |
| 1499 | void adjustOffset(CharUnits N) { |
| 1500 | Offset += N; |
| 1501 | if (N.getQuantity()) |
| 1502 | clearIsNullPointer(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1503 | } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1504 | }; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1505 | |
| 1506 | struct MemberPtr { |
| 1507 | MemberPtr() {} |
| 1508 | explicit MemberPtr(const ValueDecl *Decl) : |
| 1509 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 1510 | |
| 1511 | /// The member or (direct or indirect) field referred to by this member |
| 1512 | /// pointer, or 0 if this is a null member pointer. |
| 1513 | const ValueDecl *getDecl() const { |
| 1514 | return DeclAndIsDerivedMember.getPointer(); |
| 1515 | } |
| 1516 | /// Is this actually a member of some type derived from the relevant class? |
| 1517 | bool isDerivedMember() const { |
| 1518 | return DeclAndIsDerivedMember.getInt(); |
| 1519 | } |
| 1520 | /// Get the class which the declaration actually lives in. |
| 1521 | const CXXRecordDecl *getContainingRecord() const { |
| 1522 | return cast<CXXRecordDecl>( |
| 1523 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 1524 | } |
| 1525 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1526 | void moveInto(APValue &V) const { |
| 1527 | V = APValue(getDecl(), isDerivedMember(), Path); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1528 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1529 | void setFrom(const APValue &V) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1530 | assert(V.isMemberPointer()); |
| 1531 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 1532 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 1533 | Path.clear(); |
| 1534 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 1535 | Path.insert(Path.end(), P.begin(), P.end()); |
| 1536 | } |
| 1537 | |
| 1538 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 1539 | /// whether the member is a member of some class derived from the class type |
| 1540 | /// of the member pointer. |
| 1541 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 1542 | /// Path - The path of base/derived classes from the member declaration's |
| 1543 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 1544 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 1545 | |
| 1546 | /// Perform a cast towards the class of the Decl (either up or down the |
| 1547 | /// hierarchy). |
| 1548 | bool castBack(const CXXRecordDecl *Class) { |
| 1549 | assert(!Path.empty()); |
| 1550 | const CXXRecordDecl *Expected; |
| 1551 | if (Path.size() >= 2) |
| 1552 | Expected = Path[Path.size() - 2]; |
| 1553 | else |
| 1554 | Expected = getContainingRecord(); |
| 1555 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 1556 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 1557 | // if B does not contain the original member and is not a base or |
| 1558 | // derived class of the class containing the original member, the result |
| 1559 | // of the cast is undefined. |
| 1560 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 1561 | // (D::*). We consider that to be a language defect. |
| 1562 | return false; |
| 1563 | } |
| 1564 | Path.pop_back(); |
| 1565 | return true; |
| 1566 | } |
| 1567 | /// Perform a base-to-derived member pointer cast. |
| 1568 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 1569 | if (!getDecl()) |
| 1570 | return true; |
| 1571 | if (!isDerivedMember()) { |
| 1572 | Path.push_back(Derived); |
| 1573 | return true; |
| 1574 | } |
| 1575 | if (!castBack(Derived)) |
| 1576 | return false; |
| 1577 | if (Path.empty()) |
| 1578 | DeclAndIsDerivedMember.setInt(false); |
| 1579 | return true; |
| 1580 | } |
| 1581 | /// Perform a derived-to-base member pointer cast. |
| 1582 | bool castToBase(const CXXRecordDecl *Base) { |
| 1583 | if (!getDecl()) |
| 1584 | return true; |
| 1585 | if (Path.empty()) |
| 1586 | DeclAndIsDerivedMember.setInt(true); |
| 1587 | if (isDerivedMember()) { |
| 1588 | Path.push_back(Base); |
| 1589 | return true; |
| 1590 | } |
| 1591 | return castBack(Base); |
| 1592 | } |
| 1593 | }; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1594 | |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 1595 | /// Compare two member pointers, which are assumed to be of the same type. |
| 1596 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 1597 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 1598 | return !LHS.getDecl() && !RHS.getDecl(); |
| 1599 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 1600 | return false; |
| 1601 | return LHS.Path == RHS.Path; |
| 1602 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1603 | } |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1604 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1605 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1606 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 1607 | const LValue &This, const Expr *E, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1608 | bool AllowNonLiteralTypes = false); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 1609 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
| 1610 | bool InvalidBaseOK = false); |
| 1611 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info, |
| 1612 | bool InvalidBaseOK = false); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1613 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 1614 | EvalInfo &Info); |
| 1615 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 1616 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1617 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 1618 | EvalInfo &Info); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1619 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1620 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 1621 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
| 1622 | EvalInfo &Info); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1623 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1624 | |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 1625 | /// Evaluate an integer or fixed point expression into an APResult. |
| 1626 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
| 1627 | EvalInfo &Info); |
| 1628 | |
| 1629 | /// Evaluate only a fixed point expression into an APResult. |
| 1630 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
| 1631 | EvalInfo &Info); |
| 1632 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1633 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1634 | // Misc utilities |
| 1635 | //===----------------------------------------------------------------------===// |
| 1636 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1637 | /// A helper function to create a temporary and set an LValue. |
| 1638 | template <class KeyTy> |
| 1639 | static APValue &createTemporary(const KeyTy *Key, bool IsLifetimeExtended, |
| 1640 | LValue &LV, CallStackFrame &Frame) { |
| 1641 | LV.set({Key, Frame.Info.CurrentCall->Index, |
| 1642 | Frame.Info.CurrentCall->getTempVersion()}); |
| 1643 | return Frame.createTemporary(Key, IsLifetimeExtended); |
| 1644 | } |
| 1645 | |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1646 | /// Negate an APSInt in place, converting it to a signed form if necessary, and |
| 1647 | /// preserving its value (by extending by up to one bit as needed). |
| 1648 | static void negateAsSigned(APSInt &Int) { |
| 1649 | if (Int.isUnsigned() || Int.isMinSignedValue()) { |
| 1650 | Int = Int.extend(Int.getBitWidth() + 1); |
| 1651 | Int.setIsSigned(true); |
| 1652 | } |
| 1653 | Int = -Int; |
| 1654 | } |
| 1655 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1656 | /// Produce a string describing the given constexpr call. |
| 1657 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out) { |
| 1658 | unsigned ArgIndex = 0; |
| 1659 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
| 1660 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 1661 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
| 1662 | |
| 1663 | if (!IsMemberCall) |
| 1664 | Out << *Frame->Callee << '('; |
| 1665 | |
| 1666 | if (Frame->This && IsMemberCall) { |
| 1667 | APValue Val; |
| 1668 | Frame->This->moveInto(Val); |
| 1669 | Val.printPretty(Out, Frame->Info.Ctx, |
| 1670 | Frame->This->Designator.MostDerivedType); |
| 1671 | // FIXME: Add parens around Val if needed. |
| 1672 | Out << "->" << *Frame->Callee << '('; |
| 1673 | IsMemberCall = false; |
| 1674 | } |
| 1675 | |
| 1676 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 1677 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
| 1678 | if (ArgIndex > (unsigned)IsMemberCall) |
| 1679 | Out << ", "; |
| 1680 | |
| 1681 | const ParmVarDecl *Param = *I; |
| 1682 | const APValue &Arg = Frame->Arguments[ArgIndex]; |
| 1683 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 1684 | |
| 1685 | if (ArgIndex == 0 && IsMemberCall) |
| 1686 | Out << "->" << *Frame->Callee << '('; |
| 1687 | } |
| 1688 | |
| 1689 | Out << ')'; |
| 1690 | } |
| 1691 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1692 | /// Evaluate an expression to see if it had side-effects, and discard its |
| 1693 | /// result. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1694 | /// \return \c true if the caller should keep evaluating. |
| 1695 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1696 | APValue Scratch; |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 1697 | if (!Evaluate(Scratch, Info, E)) |
| 1698 | // We don't need the value, but we might have skipped a side effect here. |
| 1699 | return Info.noteSideEffect(); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1700 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1703 | /// Should this call expression be treated as a string literal? |
| 1704 | static bool IsStringLiteralCall(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 1705 | unsigned Builtin = E->getBuiltinCallee(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1706 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 1707 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 1708 | } |
| 1709 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1710 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1711 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 1712 | // constant expression of pointer type that evaluates to... |
| 1713 | |
| 1714 | // ... a null pointer value, or a prvalue core constant expression of type |
| 1715 | // std::nullptr_t. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1716 | if (!B) return true; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1717 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1718 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 1719 | // ... the address of an object with static storage duration, |
| 1720 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 1721 | return VD->hasGlobalStorage(); |
| 1722 | // ... the address of a function, |
| 1723 | return isa<FunctionDecl>(D); |
| 1724 | } |
| 1725 | |
| 1726 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1727 | switch (E->getStmtClass()) { |
| 1728 | default: |
| 1729 | return false; |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1730 | case Expr::CompoundLiteralExprClass: { |
| 1731 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 1732 | return CLE->isFileScope() && CLE->isLValue(); |
| 1733 | } |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1734 | case Expr::MaterializeTemporaryExprClass: |
| 1735 | // A materialized temporary might have been lifetime-extended to static |
| 1736 | // storage duration. |
| 1737 | return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1738 | // A string literal has static storage duration. |
| 1739 | case Expr::StringLiteralClass: |
| 1740 | case Expr::PredefinedExprClass: |
| 1741 | case Expr::ObjCStringLiteralClass: |
| 1742 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 1743 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 1744 | case Expr::CXXUuidofExprClass: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1745 | return true; |
| 1746 | case Expr::CallExprClass: |
| 1747 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 1748 | // For GCC compatibility, &&label has static storage duration. |
| 1749 | case Expr::AddrLabelExprClass: |
| 1750 | return true; |
| 1751 | // A Block literal expression may be used as the initialization value for |
| 1752 | // Block variables at global or local static scope. |
| 1753 | case Expr::BlockExprClass: |
| 1754 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1755 | case Expr::ImplicitValueInitExprClass: |
| 1756 | // FIXME: |
| 1757 | // We can never form an lvalue with an implicit value initialization as its |
| 1758 | // base through expression evaluation, so these only appear in one case: the |
| 1759 | // implicit variable declaration we invent when checking whether a constexpr |
| 1760 | // constructor can produce a constant expression. We must assume that such |
| 1761 | // an expression might be a global lvalue. |
| 1762 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1763 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 1766 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
| 1767 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
| 1768 | } |
| 1769 | |
| 1770 | static bool IsLiteralLValue(const LValue &Value) { |
| 1771 | if (Value.getLValueCallIndex()) |
| 1772 | return false; |
| 1773 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
| 1774 | return E && !isa<MaterializeTemporaryExpr>(E); |
| 1775 | } |
| 1776 | |
| 1777 | static bool IsWeakLValue(const LValue &Value) { |
| 1778 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
| 1779 | return Decl && Decl->isWeak(); |
| 1780 | } |
| 1781 | |
| 1782 | static bool isZeroSized(const LValue &Value) { |
| 1783 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
| 1784 | if (Decl && isa<VarDecl>(Decl)) { |
| 1785 | QualType Ty = Decl->getType(); |
| 1786 | if (Ty->isArrayType()) |
| 1787 | return Ty->isIncompleteType() || |
| 1788 | Decl->getASTContext().getTypeSize(Ty) == 0; |
| 1789 | } |
| 1790 | return false; |
| 1791 | } |
| 1792 | |
| 1793 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 1794 | if (!A.getLValueBase()) |
| 1795 | return !B.getLValueBase(); |
| 1796 | if (!B.getLValueBase()) |
| 1797 | return false; |
| 1798 | |
| 1799 | if (A.getLValueBase().getOpaqueValue() != |
| 1800 | B.getLValueBase().getOpaqueValue()) { |
| 1801 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 1802 | if (!ADecl) |
| 1803 | return false; |
| 1804 | const Decl *BDecl = GetLValueBaseDecl(B); |
| 1805 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
| 1806 | return false; |
| 1807 | } |
| 1808 | |
| 1809 | return IsGlobalLValue(A.getLValueBase()) || |
| 1810 | (A.getLValueCallIndex() == B.getLValueCallIndex() && |
| 1811 | A.getLValueVersion() == B.getLValueVersion()); |
| 1812 | } |
| 1813 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1814 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 1815 | assert(Base && "no location for a null lvalue"); |
| 1816 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 1817 | if (VD) |
| 1818 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1819 | else |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 1820 | Info.Note(Base.get<const Expr*>()->getExprLoc(), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1821 | diag::note_constexpr_temporary_here); |
| 1822 | } |
| 1823 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1824 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1825 | /// value for an address or reference constant expression. Return true if we |
| 1826 | /// can fold this expression, whether or not it's a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1827 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1828 | QualType Type, const LValue &LVal, |
| 1829 | Expr::ConstExprUsage Usage) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1830 | bool IsReferenceType = Type->isReferenceType(); |
| 1831 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1832 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 1833 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 1834 | |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1835 | // Check that the object is a global. Note that the fake 'this' object we |
| 1836 | // manufacture when checking potential constant expressions is conservatively |
| 1837 | // assumed to be global here. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1838 | if (!IsGlobalLValue(Base)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1839 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1840 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1841 | Info.FFDiag(Loc, diag::note_constexpr_non_global, 1) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1842 | << IsReferenceType << !Designator.Entries.empty() |
| 1843 | << !!VD << VD; |
| 1844 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1845 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1846 | Info.FFDiag(Loc); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1847 | } |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1848 | // Don't allow references to temporaries to escape. |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1849 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1850 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1851 | assert((Info.checkingPotentialConstantExpression() || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1852 | LVal.getLValueCallIndex() == 0) && |
| 1853 | "have call index for global lvalue"); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1854 | |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1855 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 1856 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1857 | // Check if this is a thread-local variable. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1858 | if (Var->getTLSKind()) |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1859 | return false; |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1860 | |
Hans Wennborg | 82dd877 | 2014-06-25 22:19:48 +0000 | [diff] [blame] | 1861 | // A dllimport variable never acts like a constant. |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1862 | if (Usage == Expr::EvaluateForCodeGen && Var->hasAttr<DLLImportAttr>()) |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1863 | return false; |
| 1864 | } |
| 1865 | if (const auto *FD = dyn_cast<const FunctionDecl>(VD)) { |
| 1866 | // __declspec(dllimport) must be handled very carefully: |
| 1867 | // We must never initialize an expression with the thunk in C++. |
| 1868 | // Doing otherwise would allow the same id-expression to yield |
| 1869 | // different addresses for the same function in different translation |
| 1870 | // units. However, this means that we must dynamically initialize the |
| 1871 | // expression with the contents of the import address table at runtime. |
| 1872 | // |
| 1873 | // The C language has no notion of ODR; furthermore, it has no notion of |
| 1874 | // dynamic initialization. This means that we are permitted to |
| 1875 | // perform initialization with the address of the thunk. |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1876 | if (Info.getLangOpts().CPlusPlus && Usage == Expr::EvaluateForCodeGen && |
| 1877 | FD->hasAttr<DLLImportAttr>()) |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1878 | return false; |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1879 | } |
| 1880 | } |
| 1881 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1882 | // Allow address constant expressions to be past-the-end pointers. This is |
| 1883 | // an extension: the standard requires them to point to an object. |
| 1884 | if (!IsReferenceType) |
| 1885 | return true; |
| 1886 | |
| 1887 | // A reference constant expression must refer to an object. |
| 1888 | if (!Base) { |
| 1889 | // FIXME: diagnostic |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1890 | Info.CCEDiag(Loc); |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1891 | return true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1894 | // Does this refer one past the end of some object? |
Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 1895 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1896 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1897 | Info.FFDiag(Loc, diag::note_constexpr_past_end, 1) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1898 | << !Designator.Entries.empty() << !!VD << VD; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1899 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1902 | return true; |
| 1903 | } |
| 1904 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 1905 | /// Member pointers are constant expressions unless they point to a |
| 1906 | /// non-virtual dllimport member function. |
| 1907 | static bool CheckMemberPointerConstantExpression(EvalInfo &Info, |
| 1908 | SourceLocation Loc, |
| 1909 | QualType Type, |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1910 | const APValue &Value, |
| 1911 | Expr::ConstExprUsage Usage) { |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 1912 | const ValueDecl *Member = Value.getMemberPointerDecl(); |
| 1913 | const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Member); |
| 1914 | if (!FD) |
| 1915 | return true; |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1916 | return Usage == Expr::EvaluateForMangling || FD->isVirtual() || |
| 1917 | !FD->hasAttr<DLLImportAttr>(); |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1920 | /// Check that this core constant expression is of literal type, and if not, |
| 1921 | /// produce an appropriate diagnostic. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1922 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1923 | const LValue *This = nullptr) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1924 | if (!E->isRValue() || E->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1925 | return true; |
| 1926 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1927 | // C++1y: A constant initializer for an object o [...] may also invoke |
| 1928 | // constexpr constructors for o and its subobjects even if those objects |
| 1929 | // are of non-literal class types. |
David L. Jones | f55ce36 | 2017-01-09 21:38:07 +0000 | [diff] [blame] | 1930 | // |
| 1931 | // C++11 missed this detail for aggregates, so classes like this: |
| 1932 | // struct foo_t { union { int i; volatile int j; } u; }; |
| 1933 | // are not (obviously) initializable like so: |
| 1934 | // __attribute__((__require_constant_initialization__)) |
| 1935 | // static const foo_t x = {{0}}; |
| 1936 | // because "i" is a subobject with non-literal initialization (due to the |
| 1937 | // volatile member of the union). See: |
| 1938 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 |
| 1939 | // Therefore, we use the C++1y behavior. |
| 1940 | if (This && Info.EvaluatingDecl == This->getLValueBase()) |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1941 | return true; |
| 1942 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1943 | // Prvalue constant expressions must be of literal types. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1944 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1945 | Info.FFDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1946 | << E->getType(); |
| 1947 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1948 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1949 | return false; |
| 1950 | } |
| 1951 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1952 | /// Check that this core constant expression value is a valid value for a |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1953 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 1954 | /// check that the expression is of literal type. |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1955 | static bool |
| 1956 | CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, QualType Type, |
| 1957 | const APValue &Value, |
| 1958 | Expr::ConstExprUsage Usage = Expr::EvaluateForCodeGen) { |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1959 | if (Value.isUninit()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1960 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 1961 | << true << Type; |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1962 | return false; |
| 1963 | } |
| 1964 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 1965 | // We allow _Atomic(T) to be initialized from anything that T can be |
| 1966 | // initialized from. |
| 1967 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
| 1968 | Type = AT->getValueType(); |
| 1969 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1970 | // Core issue 1454: For a literal constant expression of array or class type, |
| 1971 | // each subobject of its value shall have been initialized by a constant |
| 1972 | // expression. |
| 1973 | if (Value.isArray()) { |
| 1974 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 1975 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| 1976 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1977 | Value.getArrayInitializedElt(I), Usage)) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1978 | return false; |
| 1979 | } |
| 1980 | if (!Value.hasArrayFiller()) |
| 1981 | return true; |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1982 | return CheckConstantExpression(Info, DiagLoc, EltTy, Value.getArrayFiller(), |
| 1983 | Usage); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1984 | } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1985 | if (Value.isUnion() && Value.getUnionField()) { |
| 1986 | return CheckConstantExpression(Info, DiagLoc, |
| 1987 | Value.getUnionField()->getType(), |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1988 | Value.getUnionValue(), Usage); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1989 | } |
| 1990 | if (Value.isStruct()) { |
| 1991 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 1992 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1993 | unsigned BaseIndex = 0; |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1994 | for (const CXXBaseSpecifier &BS : CD->bases()) { |
| 1995 | if (!CheckConstantExpression(Info, DiagLoc, BS.getType(), |
| 1996 | Value.getStructBase(BaseIndex), Usage)) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1997 | return false; |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1998 | ++BaseIndex; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1999 | } |
| 2000 | } |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 2001 | for (const auto *I : RD->fields()) { |
Jordan Rose | d4503da | 2017-10-24 02:17:07 +0000 | [diff] [blame] | 2002 | if (I->isUnnamedBitfield()) |
| 2003 | continue; |
| 2004 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2005 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2006 | Value.getStructField(I->getFieldIndex()), |
| 2007 | Usage)) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2008 | return false; |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | if (Value.isLValue()) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2013 | LValue LVal; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2014 | LVal.setFrom(Info.Ctx, Value); |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2015 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal, Usage); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 2018 | if (Value.isMemberPointer()) |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 2019 | return CheckMemberPointerConstantExpression(Info, DiagLoc, Type, Value, Usage); |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 2020 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 2021 | // Everything else is fine. |
| 2022 | return true; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2025 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 2026 | // A null base expression indicates a null pointer. These are always |
| 2027 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2028 | if (!Value.getLValueBase()) { |
| 2029 | Result = !Value.getLValueOffset().isZero(); |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 2030 | return true; |
| 2031 | } |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 2032 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2033 | // We have a non-null base. These are generally known to be true, but if it's |
| 2034 | // a weak declaration it can be null at runtime. |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 2035 | Result = true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2036 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 2037 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2040 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2041 | switch (Val.getKind()) { |
| 2042 | case APValue::Uninitialized: |
| 2043 | return false; |
| 2044 | case APValue::Int: |
| 2045 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2046 | return true; |
Leonard Chan | 86285d2 | 2019-01-16 18:53:05 +0000 | [diff] [blame] | 2047 | case APValue::FixedPoint: |
| 2048 | Result = Val.getFixedPoint().getBoolValue(); |
| 2049 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2050 | case APValue::Float: |
| 2051 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2052 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2053 | case APValue::ComplexInt: |
| 2054 | Result = Val.getComplexIntReal().getBoolValue() || |
| 2055 | Val.getComplexIntImag().getBoolValue(); |
| 2056 | return true; |
| 2057 | case APValue::ComplexFloat: |
| 2058 | Result = !Val.getComplexFloatReal().isZero() || |
| 2059 | !Val.getComplexFloatImag().isZero(); |
| 2060 | return true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2061 | case APValue::LValue: |
| 2062 | return EvalPointerValueAsBool(Val, Result); |
| 2063 | case APValue::MemberPointer: |
| 2064 | Result = Val.getMemberPointerDecl(); |
| 2065 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2066 | case APValue::Vector: |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2067 | case APValue::Array: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2068 | case APValue::Struct: |
| 2069 | case APValue::Union: |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 2070 | case APValue::AddrLabelDiff: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2071 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2074 | llvm_unreachable("unknown APValue kind"); |
| 2075 | } |
| 2076 | |
| 2077 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 2078 | EvalInfo &Info) { |
| 2079 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2080 | APValue Val; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 2081 | if (!Evaluate(Val, Info, E)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2082 | return false; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 2083 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2086 | template<typename T> |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2087 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2088 | const T &SrcValue, QualType DestType) { |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 2089 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 2090 | << SrcValue << DestType; |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 2091 | return Info.noteUndefinedBehavior(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 2095 | QualType SrcType, const APFloat &Value, |
| 2096 | QualType DestType, APSInt &Result) { |
| 2097 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2098 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2099 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2101 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2102 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2103 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 2104 | & APFloat::opInvalidOp) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2105 | return HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2106 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2109 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 2110 | QualType SrcType, QualType DestType, |
| 2111 | APFloat &Result) { |
| 2112 | APFloat Value = Result; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2113 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2114 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 2115 | APFloat::rmNearestTiesToEven, &ignored) |
| 2116 | & APFloat::opOverflow) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2117 | return HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2118 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 2121 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 2122 | QualType DestType, QualType SrcType, |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 2123 | const APSInt &Value) { |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 2124 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2125 | // Figure out if this is a truncate, extend or noop cast. |
| 2126 | // If the input is signed, do a sign extend, noop, or truncate. |
Richard Smith | bd844e0 | 2018-11-12 20:11:57 +0000 | [diff] [blame] | 2127 | APSInt Result = Value.extOrTrunc(DestWidth); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2128 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Richard Smith | bd844e0 | 2018-11-12 20:11:57 +0000 | [diff] [blame] | 2129 | if (DestType->isBooleanType()) |
| 2130 | Result = Value.getBoolValue(); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2131 | return Result; |
| 2132 | } |
| 2133 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2134 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 2135 | QualType SrcType, const APSInt &Value, |
| 2136 | QualType DestType, APFloat &Result) { |
| 2137 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 2138 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 2139 | APFloat::rmNearestTiesToEven) |
| 2140 | & APFloat::opOverflow) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2141 | return HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2142 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2145 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
| 2146 | APValue &Value, const FieldDecl *FD) { |
| 2147 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
| 2148 | |
| 2149 | if (!Value.isInt()) { |
| 2150 | // Trying to store a pointer-cast-to-integer into a bitfield. |
| 2151 | // FIXME: In this case, we should provide the diagnostic for casting |
| 2152 | // a pointer to an integer. |
| 2153 | assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2154 | Info.FFDiag(E); |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2155 | return false; |
| 2156 | } |
| 2157 | |
| 2158 | APSInt &Int = Value.getInt(); |
| 2159 | unsigned OldBitWidth = Int.getBitWidth(); |
| 2160 | unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx); |
| 2161 | if (NewBitWidth < OldBitWidth) |
| 2162 | Int = Int.trunc(NewBitWidth).extend(OldBitWidth); |
| 2163 | return true; |
| 2164 | } |
| 2165 | |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2166 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 2167 | llvm::APInt &Res) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2168 | APValue SVal; |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2169 | if (!Evaluate(SVal, Info, E)) |
| 2170 | return false; |
| 2171 | if (SVal.isInt()) { |
| 2172 | Res = SVal.getInt(); |
| 2173 | return true; |
| 2174 | } |
| 2175 | if (SVal.isFloat()) { |
| 2176 | Res = SVal.getFloat().bitcastToAPInt(); |
| 2177 | return true; |
| 2178 | } |
| 2179 | if (SVal.isVector()) { |
| 2180 | QualType VecTy = E->getType(); |
| 2181 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 2182 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 2183 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 2184 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 2185 | Res = llvm::APInt::getNullValue(VecSize); |
| 2186 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 2187 | APValue &Elt = SVal.getVectorElt(i); |
| 2188 | llvm::APInt EltAsInt; |
| 2189 | if (Elt.isInt()) { |
| 2190 | EltAsInt = Elt.getInt(); |
| 2191 | } else if (Elt.isFloat()) { |
| 2192 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 2193 | } else { |
| 2194 | // Don't try to handle vectors of anything other than int or float |
| 2195 | // (not sure if it's possible to hit this case). |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2196 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2197 | return false; |
| 2198 | } |
| 2199 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 2200 | if (BigEndian) |
| 2201 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 2202 | else |
| 2203 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 2204 | } |
| 2205 | return true; |
| 2206 | } |
| 2207 | // Give up if the input isn't an int, float, or vector. For example, we |
| 2208 | // reject "(v4i16)(intptr_t)&a". |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2209 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2210 | return false; |
| 2211 | } |
| 2212 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2213 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 2214 | /// bits, and check for overflow in the original type (if that type was not an |
| 2215 | /// unsigned type). |
| 2216 | template<typename Operation> |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2217 | static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 2218 | const APSInt &LHS, const APSInt &RHS, |
| 2219 | unsigned BitWidth, Operation Op, |
| 2220 | APSInt &Result) { |
| 2221 | if (LHS.isUnsigned()) { |
| 2222 | Result = Op(LHS, RHS); |
| 2223 | return true; |
| 2224 | } |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2225 | |
| 2226 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2227 | Result = Value.trunc(LHS.getBitWidth()); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2228 | if (Result.extend(BitWidth) != Value) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2229 | if (Info.checkingForOverflow()) |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2230 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2231 | diag::warn_integer_constant_overflow) |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2232 | << Result.toString(10) << E->getType(); |
| 2233 | else |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2234 | return HandleOverflow(Info, E, Value, E->getType()); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2235 | } |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2236 | return true; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | /// Perform the given binary integer operation. |
| 2240 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
| 2241 | BinaryOperatorKind Opcode, APSInt RHS, |
| 2242 | APSInt &Result) { |
| 2243 | switch (Opcode) { |
| 2244 | default: |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2245 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2246 | return false; |
| 2247 | case BO_Mul: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2248 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
| 2249 | std::multiplies<APSInt>(), Result); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2250 | case BO_Add: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2251 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 2252 | std::plus<APSInt>(), Result); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2253 | case BO_Sub: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2254 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 2255 | std::minus<APSInt>(), Result); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2256 | case BO_And: Result = LHS & RHS; return true; |
| 2257 | case BO_Xor: Result = LHS ^ RHS; return true; |
| 2258 | case BO_Or: Result = LHS | RHS; return true; |
| 2259 | case BO_Div: |
| 2260 | case BO_Rem: |
| 2261 | if (RHS == 0) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2262 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2263 | return false; |
| 2264 | } |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2265 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
| 2266 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports |
| 2267 | // this operation and gives the two's complement result. |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2268 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 2269 | LHS.isSigned() && LHS.isMinSignedValue()) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2270 | return HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), |
| 2271 | E->getType()); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2272 | return true; |
| 2273 | case BO_Shl: { |
| 2274 | if (Info.getLangOpts().OpenCL) |
| 2275 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 2276 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 2277 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 2278 | RHS.isUnsigned()); |
| 2279 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 2280 | // During constant-folding, a negative shift is an opposite shift. Such |
| 2281 | // a shift is not a constant expression. |
| 2282 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 2283 | RHS = -RHS; |
| 2284 | goto shift_right; |
| 2285 | } |
| 2286 | shift_left: |
| 2287 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 2288 | // the shifted type. |
| 2289 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2290 | if (SA != RHS) { |
| 2291 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 2292 | << RHS << E->getType() << LHS.getBitWidth(); |
| 2293 | } else if (LHS.isSigned()) { |
| 2294 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 2295 | // operand, and must not overflow the corresponding unsigned type. |
| 2296 | if (LHS.isNegative()) |
| 2297 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 2298 | else if (LHS.countLeadingZeros() < SA) |
| 2299 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 2300 | } |
| 2301 | Result = LHS << SA; |
| 2302 | return true; |
| 2303 | } |
| 2304 | case BO_Shr: { |
| 2305 | if (Info.getLangOpts().OpenCL) |
| 2306 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 2307 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 2308 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 2309 | RHS.isUnsigned()); |
| 2310 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 2311 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 2312 | // shift is not a constant expression. |
| 2313 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 2314 | RHS = -RHS; |
| 2315 | goto shift_left; |
| 2316 | } |
| 2317 | shift_right: |
| 2318 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 2319 | // shifted type. |
| 2320 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2321 | if (SA != RHS) |
| 2322 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 2323 | << RHS << E->getType() << LHS.getBitWidth(); |
| 2324 | Result = LHS >> SA; |
| 2325 | return true; |
| 2326 | } |
| 2327 | |
| 2328 | case BO_LT: Result = LHS < RHS; return true; |
| 2329 | case BO_GT: Result = LHS > RHS; return true; |
| 2330 | case BO_LE: Result = LHS <= RHS; return true; |
| 2331 | case BO_GE: Result = LHS >= RHS; return true; |
| 2332 | case BO_EQ: Result = LHS == RHS; return true; |
| 2333 | case BO_NE: Result = LHS != RHS; return true; |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 2334 | case BO_Cmp: |
| 2335 | llvm_unreachable("BO_Cmp should be handled elsewhere"); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2336 | } |
| 2337 | } |
| 2338 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2339 | /// Perform the given binary floating-point operation, in-place, on LHS. |
| 2340 | static bool handleFloatFloatBinOp(EvalInfo &Info, const Expr *E, |
| 2341 | APFloat &LHS, BinaryOperatorKind Opcode, |
| 2342 | const APFloat &RHS) { |
| 2343 | switch (Opcode) { |
| 2344 | default: |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2345 | Info.FFDiag(E); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2346 | return false; |
| 2347 | case BO_Mul: |
| 2348 | LHS.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 2349 | break; |
| 2350 | case BO_Add: |
| 2351 | LHS.add(RHS, APFloat::rmNearestTiesToEven); |
| 2352 | break; |
| 2353 | case BO_Sub: |
| 2354 | LHS.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 2355 | break; |
| 2356 | case BO_Div: |
| 2357 | LHS.divide(RHS, APFloat::rmNearestTiesToEven); |
| 2358 | break; |
| 2359 | } |
| 2360 | |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2361 | if (LHS.isInfinity() || LHS.isNaN()) { |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2362 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 2363 | return Info.noteUndefinedBehavior(); |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2364 | } |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2365 | return true; |
| 2366 | } |
| 2367 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2368 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 2369 | /// truncating the lvalue's path to the given length. |
| 2370 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 2371 | const RecordDecl *TruncatedType, |
| 2372 | unsigned TruncatedElements) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2373 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2374 | |
| 2375 | // Check we actually point to a derived class object. |
| 2376 | if (TruncatedElements == D.Entries.size()) |
| 2377 | return true; |
| 2378 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 2379 | "not casting to a derived class"); |
| 2380 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 2381 | return false; |
| 2382 | |
| 2383 | // 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] | 2384 | const RecordDecl *RD = TruncatedType; |
| 2385 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2386 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2387 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2388 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2389 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2390 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2391 | else |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2392 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 2393 | RD = Base; |
| 2394 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2395 | D.Entries.resize(TruncatedElements); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2396 | return true; |
| 2397 | } |
| 2398 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2399 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2400 | const CXXRecordDecl *Derived, |
| 2401 | const CXXRecordDecl *Base, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2402 | const ASTRecordLayout *RL = nullptr) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2403 | if (!RL) { |
| 2404 | if (Derived->isInvalidDecl()) return false; |
| 2405 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 2406 | } |
| 2407 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2408 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2409 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2410 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2413 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2414 | const CXXRecordDecl *DerivedDecl, |
| 2415 | const CXXBaseSpecifier *Base) { |
| 2416 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 2417 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2418 | if (!Base->isVirtual()) |
| 2419 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2420 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2421 | SubobjectDesignator &D = Obj.Designator; |
| 2422 | if (D.Invalid) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2423 | return false; |
| 2424 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2425 | // Extract most-derived object and corresponding type. |
| 2426 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 2427 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 2428 | return false; |
| 2429 | |
| 2430 | // Find the virtual base class. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2431 | if (DerivedDecl->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2432 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 2433 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2434 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2435 | return true; |
| 2436 | } |
| 2437 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2438 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
| 2439 | QualType Type, LValue &Result) { |
| 2440 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2441 | PathE = E->path_end(); |
| 2442 | PathI != PathE; ++PathI) { |
| 2443 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 2444 | *PathI)) |
| 2445 | return false; |
| 2446 | Type = (*PathI)->getType(); |
| 2447 | } |
| 2448 | return true; |
| 2449 | } |
| 2450 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2451 | /// Update LVal to refer to the given field, which must be a member of the type |
| 2452 | /// currently described by LVal. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2453 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2454 | const FieldDecl *FD, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2455 | const ASTRecordLayout *RL = nullptr) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2456 | if (!RL) { |
| 2457 | if (FD->getParent()->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2458 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2459 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2460 | |
| 2461 | unsigned I = FD->getFieldIndex(); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 2462 | LVal.adjustOffset(Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I))); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2463 | LVal.addDecl(Info, E, FD); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2464 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2467 | /// Update LVal to refer to the given indirect field. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2468 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2469 | LValue &LVal, |
| 2470 | const IndirectFieldDecl *IFD) { |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 2471 | for (const auto *C : IFD->chain()) |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 2472 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(C))) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2473 | return false; |
| 2474 | return true; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2477 | /// Get the size of the given type in char units. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2478 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 2479 | QualType Type, CharUnits &Size) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2480 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 2481 | // extension. |
| 2482 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 2483 | Size = CharUnits::One(); |
| 2484 | return true; |
| 2485 | } |
| 2486 | |
Saleem Abdulrasool | ada78fe | 2016-06-04 03:16:21 +0000 | [diff] [blame] | 2487 | if (Type->isDependentType()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2488 | Info.FFDiag(Loc); |
Saleem Abdulrasool | ada78fe | 2016-06-04 03:16:21 +0000 | [diff] [blame] | 2489 | return false; |
| 2490 | } |
| 2491 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2492 | if (!Type->isConstantSizeType()) { |
| 2493 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2494 | // FIXME: Better diagnostic. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2495 | Info.FFDiag(Loc); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2496 | return false; |
| 2497 | } |
| 2498 | |
| 2499 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 2500 | return true; |
| 2501 | } |
| 2502 | |
| 2503 | /// Update a pointer value to model pointer arithmetic. |
| 2504 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2505 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2506 | /// \param LVal - The pointer value to be updated. |
| 2507 | /// \param EltTy - The pointee type represented by LVal. |
| 2508 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2509 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 2510 | LValue &LVal, QualType EltTy, |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 2511 | APSInt Adjustment) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2512 | CharUnits SizeOfPointee; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2513 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2514 | return false; |
| 2515 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 2516 | LVal.adjustOffsetAndIndex(Info, E, Adjustment, SizeOfPointee); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2517 | return true; |
| 2518 | } |
| 2519 | |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 2520 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 2521 | LValue &LVal, QualType EltTy, |
| 2522 | int64_t Adjustment) { |
| 2523 | return HandleLValueArrayAdjustment(Info, E, LVal, EltTy, |
| 2524 | APSInt::get(Adjustment)); |
| 2525 | } |
| 2526 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2527 | /// Update an lvalue to refer to a component of a complex number. |
| 2528 | /// \param Info - Information about the ongoing evaluation. |
| 2529 | /// \param LVal - The lvalue to be updated. |
| 2530 | /// \param EltTy - The complex number's component type. |
| 2531 | /// \param Imag - False for the real component, true for the imaginary. |
| 2532 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 2533 | LValue &LVal, QualType EltTy, |
| 2534 | bool Imag) { |
| 2535 | if (Imag) { |
| 2536 | CharUnits SizeOfComponent; |
| 2537 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 2538 | return false; |
| 2539 | LVal.Offset += SizeOfComponent; |
| 2540 | } |
| 2541 | LVal.addComplex(Info, E, EltTy, Imag); |
| 2542 | return true; |
| 2543 | } |
| 2544 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 2545 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
| 2546 | QualType Type, const LValue &LVal, |
| 2547 | APValue &RVal); |
| 2548 | |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2549 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2550 | /// |
| 2551 | /// \param Info Information about the ongoing evaluation. |
| 2552 | /// \param E An expression to be used when printing diagnostics. |
| 2553 | /// \param VD The variable whose initializer should be obtained. |
| 2554 | /// \param Frame The frame in which the variable was created. Must be null |
| 2555 | /// if this variable is not local to the evaluation. |
| 2556 | /// \param Result Filled in with a pointer to the value of the variable. |
| 2557 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 2558 | const VarDecl *VD, CallStackFrame *Frame, |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 2559 | APValue *&Result, const LValue *LVal) { |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 2560 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2561 | // If this is a parameter to an active constexpr function call, perform |
| 2562 | // argument substitution. |
| 2563 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2564 | // Assume arguments of a potential constant expression are unknown |
| 2565 | // constant expressions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2566 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2567 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2568 | if (!Frame || !Frame->Arguments) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2569 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2570 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2571 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2572 | Result = &Frame->Arguments[PVD->getFunctionScopeIndex()]; |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2573 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2574 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2575 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2576 | // If this is a local variable, dig out its value. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2577 | if (Frame) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 2578 | Result = LVal ? Frame->getTemporary(VD, LVal->getLValueVersion()) |
| 2579 | : Frame->getCurrentTemporary(VD); |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 2580 | if (!Result) { |
| 2581 | // Assume variables referenced within a lambda's call operator that were |
| 2582 | // not declared within the call operator are captures and during checking |
| 2583 | // of a potential constant expression, assume they are unknown constant |
| 2584 | // expressions. |
| 2585 | assert(isLambdaCallOperator(Frame->Callee) && |
| 2586 | (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && |
| 2587 | "missing value for local variable"); |
| 2588 | if (Info.checkingPotentialConstantExpression()) |
| 2589 | return false; |
| 2590 | // FIXME: implement capture evaluation during constant expr evaluation. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2591 | Info.FFDiag(E->getBeginLoc(), |
| 2592 | diag::note_unimplemented_constexpr_lambda_feature_ast) |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 2593 | << "captures not currently allowed"; |
| 2594 | return false; |
| 2595 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2596 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2599 | // Dig out the initializer, and use the declaration which it's attached to. |
| 2600 | const Expr *Init = VD->getAnyInitializer(VD); |
| 2601 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2602 | // If we're checking a potential constant expression, the variable could be |
| 2603 | // initialized later. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2604 | if (!Info.checkingPotentialConstantExpression()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2605 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2606 | return false; |
| 2607 | } |
| 2608 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2609 | // If we're currently evaluating the initializer of this declaration, use that |
| 2610 | // in-flight value. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2611 | if (Info.EvaluatingDecl.dyn_cast<const ValueDecl*>() == VD) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2612 | Result = Info.EvaluatingDeclValue; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2613 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2616 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 2617 | // this is the definition which will be used. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2618 | if (VD->isWeak()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2619 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2620 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2621 | } |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2622 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2623 | // Check that we can fold the initializer. In C++, we will have already done |
| 2624 | // this in the cases where it matters for conformance. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2625 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2626 | if (!VD->evaluateValue(Notes)) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2627 | Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2628 | Notes.size() + 1) << VD; |
| 2629 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2630 | Info.addNotes(Notes); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2631 | return false; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2632 | } else if (!VD->checkInitIsICE()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2633 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2634 | Notes.size() + 1) << VD; |
| 2635 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2636 | Info.addNotes(Notes); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2637 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2638 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2639 | Result = VD->getEvaluatedValue(); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2640 | return true; |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2643 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2644 | Qualifiers Quals = T.getQualifiers(); |
| 2645 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 2646 | } |
| 2647 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2648 | /// Get the base index of the given base class within an APValue representing |
| 2649 | /// the given derived class. |
| 2650 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 2651 | const CXXRecordDecl *Base) { |
| 2652 | Base = Base->getCanonicalDecl(); |
| 2653 | unsigned Index = 0; |
| 2654 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 2655 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 2656 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 2657 | return Index; |
| 2658 | } |
| 2659 | |
| 2660 | llvm_unreachable("base class missing from derived class's bases list"); |
| 2661 | } |
| 2662 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2663 | /// Extract the value of a character from a string literal. |
| 2664 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
| 2665 | uint64_t Index) { |
Akira Hatanaka | bc33264 | 2017-01-31 02:31:39 +0000 | [diff] [blame] | 2666 | // FIXME: Support MakeStringConstant |
| 2667 | if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Lit)) { |
| 2668 | std::string Str; |
| 2669 | Info.Ctx.getObjCEncodingForType(ObjCEnc->getEncodedType(), Str); |
| 2670 | assert(Index <= Str.size() && "Index too large"); |
| 2671 | return APSInt::getUnsigned(Str.c_str()[Index]); |
| 2672 | } |
| 2673 | |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2674 | if (auto PE = dyn_cast<PredefinedExpr>(Lit)) |
| 2675 | Lit = PE->getFunctionName(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2676 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 2677 | const ConstantArrayType *CAT = |
| 2678 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2679 | assert(CAT && "string literal isn't an array"); |
| 2680 | QualType CharType = CAT->getElementType(); |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2681 | assert(CharType->isIntegerType() && "unexpected character type"); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2682 | |
| 2683 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2684 | CharType->isUnsignedIntegerType()); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2685 | if (Index < S->getLength()) |
| 2686 | Value = S->getCodeUnit(Index); |
| 2687 | return Value; |
| 2688 | } |
| 2689 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2690 | // Expand a string literal into an array of characters. |
Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 2691 | // |
| 2692 | // FIXME: This is inefficient; we should probably introduce something similar |
| 2693 | // to the LLVM ConstantDataArray to make this cheaper. |
| 2694 | static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2695 | APValue &Result) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2696 | const ConstantArrayType *CAT = |
| 2697 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2698 | assert(CAT && "string literal isn't an array"); |
| 2699 | QualType CharType = CAT->getElementType(); |
| 2700 | assert(CharType->isIntegerType() && "unexpected character type"); |
| 2701 | |
| 2702 | unsigned Elts = CAT->getSize().getZExtValue(); |
| 2703 | Result = APValue(APValue::UninitArray(), |
| 2704 | std::min(S->getLength(), Elts), Elts); |
| 2705 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 2706 | CharType->isUnsignedIntegerType()); |
| 2707 | if (Result.hasArrayFiller()) |
| 2708 | Result.getArrayFiller() = APValue(Value); |
| 2709 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
| 2710 | Value = S->getCodeUnit(I); |
| 2711 | Result.getArrayInitializedElt(I) = APValue(Value); |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | // Expand an array so that it has more than Index filled elements. |
| 2716 | static void expandArray(APValue &Array, unsigned Index) { |
| 2717 | unsigned Size = Array.getArraySize(); |
| 2718 | assert(Index < Size); |
| 2719 | |
| 2720 | // Always at least double the number of elements for which we store a value. |
| 2721 | unsigned OldElts = Array.getArrayInitializedElts(); |
| 2722 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
| 2723 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
| 2724 | |
| 2725 | // Copy the data across. |
| 2726 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
| 2727 | for (unsigned I = 0; I != OldElts; ++I) |
| 2728 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
| 2729 | for (unsigned I = OldElts; I != NewElts; ++I) |
| 2730 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
| 2731 | if (NewValue.hasArrayFiller()) |
| 2732 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
| 2733 | Array.swap(NewValue); |
| 2734 | } |
| 2735 | |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2736 | /// Determine whether a type would actually be read by an lvalue-to-rvalue |
| 2737 | /// conversion. If it's of class type, we may assume that the copy operation |
| 2738 | /// is trivial. Note that this is never true for a union type with fields |
| 2739 | /// (because the copy always "reads" the active member) and always true for |
| 2740 | /// a non-class type. |
| 2741 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
| 2742 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2743 | if (!RD || (RD->isUnion() && !RD->field_empty())) |
| 2744 | return true; |
| 2745 | if (RD->isEmpty()) |
| 2746 | return false; |
| 2747 | |
| 2748 | for (auto *Field : RD->fields()) |
| 2749 | if (isReadByLvalueToRvalueConversion(Field->getType())) |
| 2750 | return true; |
| 2751 | |
| 2752 | for (auto &BaseSpec : RD->bases()) |
| 2753 | if (isReadByLvalueToRvalueConversion(BaseSpec.getType())) |
| 2754 | return true; |
| 2755 | |
| 2756 | return false; |
| 2757 | } |
| 2758 | |
| 2759 | /// Diagnose an attempt to read from any unreadable field within the specified |
| 2760 | /// type, which might be a class type. |
| 2761 | static bool diagnoseUnreadableFields(EvalInfo &Info, const Expr *E, |
| 2762 | QualType T) { |
| 2763 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2764 | if (!RD) |
| 2765 | return false; |
| 2766 | |
| 2767 | if (!RD->hasMutableFields()) |
| 2768 | return false; |
| 2769 | |
| 2770 | for (auto *Field : RD->fields()) { |
| 2771 | // If we're actually going to read this field in some way, then it can't |
| 2772 | // be mutable. If we're in a union, then assigning to a mutable field |
| 2773 | // (even an empty one) can change the active member, so that's not OK. |
| 2774 | // FIXME: Add core issue number for the union case. |
| 2775 | if (Field->isMutable() && |
| 2776 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2777 | Info.FFDiag(E, diag::note_constexpr_ltor_mutable, 1) << Field; |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2778 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2779 | return true; |
| 2780 | } |
| 2781 | |
| 2782 | if (diagnoseUnreadableFields(Info, E, Field->getType())) |
| 2783 | return true; |
| 2784 | } |
| 2785 | |
| 2786 | for (auto &BaseSpec : RD->bases()) |
| 2787 | if (diagnoseUnreadableFields(Info, E, BaseSpec.getType())) |
| 2788 | return true; |
| 2789 | |
| 2790 | // All mutable fields were empty, and thus not actually read. |
| 2791 | return false; |
| 2792 | } |
| 2793 | |
Benjamin Kramer | 5b4296a | 2015-10-28 17:16:26 +0000 | [diff] [blame] | 2794 | namespace { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2795 | /// A handle to a complete object (an object that is not a subobject of |
| 2796 | /// another object). |
| 2797 | struct CompleteObject { |
| 2798 | /// The value of the complete object. |
| 2799 | APValue *Value; |
| 2800 | /// The type of the complete object. |
| 2801 | QualType Type; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2802 | bool LifetimeStartedInEvaluation; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2803 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2804 | CompleteObject() : Value(nullptr) {} |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2805 | CompleteObject(APValue *Value, QualType Type, |
| 2806 | bool LifetimeStartedInEvaluation) |
| 2807 | : Value(Value), Type(Type), |
| 2808 | LifetimeStartedInEvaluation(LifetimeStartedInEvaluation) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2809 | assert(Value && "missing value for complete object"); |
| 2810 | } |
| 2811 | |
Aaron Ballman | 6734766 | 2015-02-15 22:00:28 +0000 | [diff] [blame] | 2812 | explicit operator bool() const { return Value; } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2813 | }; |
Benjamin Kramer | 5b4296a | 2015-10-28 17:16:26 +0000 | [diff] [blame] | 2814 | } // end anonymous namespace |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2815 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2816 | /// Find the designated sub-object of an rvalue. |
| 2817 | template<typename SubobjectHandler> |
| 2818 | typename SubobjectHandler::result_type |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2819 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2820 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2821 | if (Sub.Invalid) |
| 2822 | // A diagnostic will have already been produced. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2823 | return handler.failed(); |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 2824 | if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2825 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 2826 | Info.FFDiag(E, Sub.isOnePastTheEnd() |
| 2827 | ? diag::note_constexpr_access_past_end |
| 2828 | : diag::note_constexpr_access_unsized_array) |
| 2829 | << handler.AccessKind; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2830 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2831 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2832 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2833 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2834 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2835 | APValue *O = Obj.Value; |
| 2836 | QualType ObjType = Obj.Type; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2837 | const FieldDecl *LastField = nullptr; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2838 | const bool MayReadMutableMembers = |
| 2839 | Obj.LifetimeStartedInEvaluation && Info.getLangOpts().CPlusPlus14; |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2840 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2841 | // Walk the designator's path to find the subobject. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2842 | for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) { |
| 2843 | if (O->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2844 | if (!Info.checkingPotentialConstantExpression()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2845 | Info.FFDiag(E, diag::note_constexpr_access_uninit) << handler.AccessKind; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2846 | return handler.failed(); |
| 2847 | } |
| 2848 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2849 | if (I == N) { |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2850 | // If we are reading an object of class type, there may still be more |
| 2851 | // things we need to check: if there are any mutable subobjects, we |
| 2852 | // cannot perform this read. (This only happens when performing a trivial |
| 2853 | // copy or assignment.) |
| 2854 | if (ObjType->isRecordType() && handler.AccessKind == AK_Read && |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2855 | !MayReadMutableMembers && diagnoseUnreadableFields(Info, E, ObjType)) |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2856 | return handler.failed(); |
| 2857 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2858 | if (!handler.found(*O, ObjType)) |
| 2859 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2860 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2861 | // If we modified a bit-field, truncate it to the right width. |
| 2862 | if (handler.AccessKind != AK_Read && |
| 2863 | LastField && LastField->isBitField() && |
| 2864 | !truncateBitfieldValue(Info, E, *O, LastField)) |
| 2865 | return false; |
| 2866 | |
| 2867 | return true; |
| 2868 | } |
| 2869 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2870 | LastField = nullptr; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2871 | if (ObjType->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2872 | // Next subobject is an array element. |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2873 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2874 | assert(CAT && "vla in literal type?"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2875 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2876 | if (CAT->getSize().ule(Index)) { |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2877 | // Note, it should not be possible to form a pointer with a valid |
| 2878 | // designator which points more than one past the end of the array. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2879 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2880 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2881 | << handler.AccessKind; |
| 2882 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2883 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2884 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2885 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2886 | |
| 2887 | ObjType = CAT->getElementType(); |
| 2888 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2889 | if (O->getArrayInitializedElts() > Index) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2890 | O = &O->getArrayInitializedElt(Index); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2891 | else if (handler.AccessKind != AK_Read) { |
| 2892 | expandArray(*O, Index); |
| 2893 | O = &O->getArrayInitializedElt(Index); |
| 2894 | } else |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2895 | O = &O->getArrayFiller(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2896 | } else if (ObjType->isAnyComplexType()) { |
| 2897 | // Next subobject is a complex number. |
| 2898 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 2899 | if (Index > 1) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2900 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2901 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2902 | << handler.AccessKind; |
| 2903 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2904 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2905 | return handler.failed(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2906 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2907 | |
| 2908 | bool WasConstQualified = ObjType.isConstQualified(); |
| 2909 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 2910 | if (WasConstQualified) |
| 2911 | ObjType.addConst(); |
| 2912 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2913 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 2914 | if (O->isComplexInt()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2915 | return handler.found(Index ? O->getComplexIntImag() |
| 2916 | : O->getComplexIntReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2917 | } else { |
| 2918 | assert(O->isComplexFloat()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2919 | return handler.found(Index ? O->getComplexFloatImag() |
| 2920 | : O->getComplexFloatReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2921 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2922 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2923 | // In C++14 onwards, it is permitted to read a mutable member whose |
| 2924 | // lifetime began within the evaluation. |
| 2925 | // FIXME: Should we also allow this in C++11? |
| 2926 | if (Field->isMutable() && handler.AccessKind == AK_Read && |
| 2927 | !MayReadMutableMembers) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2928 | Info.FFDiag(E, diag::note_constexpr_ltor_mutable, 1) |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2929 | << Field; |
| 2930 | Info.Note(Field->getLocation(), diag::note_declared_at); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2931 | return handler.failed(); |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2934 | // Next subobject is a class, struct or union field. |
| 2935 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 2936 | if (RD->isUnion()) { |
| 2937 | const FieldDecl *UnionField = O->getUnionField(); |
| 2938 | if (!UnionField || |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2939 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2940 | Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2941 | << handler.AccessKind << Field << !UnionField << UnionField; |
| 2942 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2943 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2944 | O = &O->getUnionValue(); |
| 2945 | } else |
| 2946 | O = &O->getStructField(Field->getFieldIndex()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2947 | |
| 2948 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2949 | ObjType = Field->getType(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2950 | if (WasConstQualified && !Field->isMutable()) |
| 2951 | ObjType.addConst(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2952 | |
| 2953 | if (ObjType.isVolatileQualified()) { |
| 2954 | if (Info.getLangOpts().CPlusPlus) { |
| 2955 | // FIXME: Include a description of the path to the volatile subobject. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2956 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2957 | << handler.AccessKind << 2 << Field; |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2958 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2959 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2960 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2961 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2962 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2963 | } |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2964 | |
| 2965 | LastField = Field; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2966 | } else { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2967 | // Next subobject is a base class. |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2968 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 2969 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 2970 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2971 | |
| 2972 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2973 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2974 | if (WasConstQualified) |
| 2975 | ObjType.addConst(); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2976 | } |
| 2977 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2980 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2981 | struct ExtractSubobjectHandler { |
| 2982 | EvalInfo &Info; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2983 | APValue &Result; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2984 | |
| 2985 | static const AccessKinds AccessKind = AK_Read; |
| 2986 | |
| 2987 | typedef bool result_type; |
| 2988 | bool failed() { return false; } |
| 2989 | bool found(APValue &Subobj, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2990 | Result = Subobj; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2991 | return true; |
| 2992 | } |
| 2993 | bool found(APSInt &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2994 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2995 | return true; |
| 2996 | } |
| 2997 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2998 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2999 | return true; |
| 3000 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3001 | }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3002 | } // end anonymous namespace |
| 3003 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3004 | const AccessKinds ExtractSubobjectHandler::AccessKind; |
| 3005 | |
| 3006 | /// Extract the designated sub-object of an rvalue. |
| 3007 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3008 | const CompleteObject &Obj, |
| 3009 | const SubobjectDesignator &Sub, |
| 3010 | APValue &Result) { |
| 3011 | ExtractSubobjectHandler Handler = { Info, Result }; |
| 3012 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3015 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3016 | struct ModifySubobjectHandler { |
| 3017 | EvalInfo &Info; |
| 3018 | APValue &NewVal; |
| 3019 | const Expr *E; |
| 3020 | |
| 3021 | typedef bool result_type; |
| 3022 | static const AccessKinds AccessKind = AK_Assign; |
| 3023 | |
| 3024 | bool checkConst(QualType QT) { |
| 3025 | // Assigning to a const object has undefined behavior. |
| 3026 | if (QT.isConstQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3027 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3028 | return false; |
| 3029 | } |
| 3030 | return true; |
| 3031 | } |
| 3032 | |
| 3033 | bool failed() { return false; } |
| 3034 | bool found(APValue &Subobj, QualType SubobjType) { |
| 3035 | if (!checkConst(SubobjType)) |
| 3036 | return false; |
| 3037 | // We've been given ownership of NewVal, so just swap it in. |
| 3038 | Subobj.swap(NewVal); |
| 3039 | return true; |
| 3040 | } |
| 3041 | bool found(APSInt &Value, QualType SubobjType) { |
| 3042 | if (!checkConst(SubobjType)) |
| 3043 | return false; |
| 3044 | if (!NewVal.isInt()) { |
| 3045 | // Maybe trying to write a cast pointer value into a complex? |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3046 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3047 | return false; |
| 3048 | } |
| 3049 | Value = NewVal.getInt(); |
| 3050 | return true; |
| 3051 | } |
| 3052 | bool found(APFloat &Value, QualType SubobjType) { |
| 3053 | if (!checkConst(SubobjType)) |
| 3054 | return false; |
| 3055 | Value = NewVal.getFloat(); |
| 3056 | return true; |
| 3057 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3058 | }; |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 3059 | } // end anonymous namespace |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3060 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3061 | const AccessKinds ModifySubobjectHandler::AccessKind; |
| 3062 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3063 | /// Update the designated sub-object of an rvalue to the given value. |
| 3064 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3065 | const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3066 | const SubobjectDesignator &Sub, |
| 3067 | APValue &NewVal) { |
| 3068 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3069 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3072 | /// Find the position where two subobject designators diverge, or equivalently |
| 3073 | /// the length of the common initial subsequence. |
| 3074 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 3075 | const SubobjectDesignator &A, |
| 3076 | const SubobjectDesignator &B, |
| 3077 | bool &WasArrayIndex) { |
| 3078 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 3079 | for (/**/; I != N; ++I) { |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3080 | if (!ObjType.isNull() && |
| 3081 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3082 | // Next subobject is an array element. |
| 3083 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 3084 | WasArrayIndex = true; |
| 3085 | return I; |
| 3086 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3087 | if (ObjType->isAnyComplexType()) |
| 3088 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 3089 | else |
| 3090 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3091 | } else { |
| 3092 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 3093 | WasArrayIndex = false; |
| 3094 | return I; |
| 3095 | } |
| 3096 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 3097 | // Next subobject is a field. |
| 3098 | ObjType = FD->getType(); |
| 3099 | else |
| 3100 | // Next subobject is a base class. |
| 3101 | ObjType = QualType(); |
| 3102 | } |
| 3103 | } |
| 3104 | WasArrayIndex = false; |
| 3105 | return I; |
| 3106 | } |
| 3107 | |
| 3108 | /// Determine whether the given subobject designators refer to elements of the |
| 3109 | /// same array object. |
| 3110 | static bool AreElementsOfSameArray(QualType ObjType, |
| 3111 | const SubobjectDesignator &A, |
| 3112 | const SubobjectDesignator &B) { |
| 3113 | if (A.Entries.size() != B.Entries.size()) |
| 3114 | return false; |
| 3115 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 3116 | bool IsArray = A.MostDerivedIsArrayElement; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3117 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 3118 | // A is a subobject of the array element. |
| 3119 | return false; |
| 3120 | |
| 3121 | // If A (and B) designates an array element, the last entry will be the array |
| 3122 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 3123 | // of length 1' case, and the entire path must match. |
| 3124 | bool WasArrayIndex; |
| 3125 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 3126 | return CommonLength >= A.Entries.size() - IsArray; |
| 3127 | } |
| 3128 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3129 | /// Find the complete object to which an LValue refers. |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 3130 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
| 3131 | AccessKinds AK, const LValue &LVal, |
| 3132 | QualType LValType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3133 | if (!LVal.Base) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3134 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3135 | return CompleteObject(); |
| 3136 | } |
| 3137 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3138 | CallStackFrame *Frame = nullptr; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3139 | if (LVal.getLValueCallIndex()) { |
| 3140 | Frame = Info.getCallFrame(LVal.getLValueCallIndex()); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3141 | if (!Frame) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3142 | Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3143 | << AK << LVal.Base.is<const ValueDecl*>(); |
| 3144 | NoteLValueLocation(Info, LVal.Base); |
| 3145 | return CompleteObject(); |
| 3146 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 3150 | // is not a constant expression (even if the object is non-volatile). We also |
| 3151 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 3152 | // semantics. |
| 3153 | if (LValType.isVolatileQualified()) { |
| 3154 | if (Info.getLangOpts().CPlusPlus) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3155 | Info.FFDiag(E, diag::note_constexpr_access_volatile_type) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3156 | << AK << LValType; |
| 3157 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3158 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3159 | return CompleteObject(); |
| 3160 | } |
| 3161 | |
| 3162 | // Compute value storage location and type of base object. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3163 | APValue *BaseVal = nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3164 | QualType BaseType = getType(LVal.Base); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3165 | bool LifetimeStartedInEvaluation = Frame; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3166 | |
| 3167 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
| 3168 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 3169 | // In C++11, constexpr, non-volatile variables initialized with constant |
| 3170 | // expressions are constant expressions too. Inside constexpr functions, |
| 3171 | // parameters are constant expressions even if they're non-const. |
| 3172 | // In C++1y, objects local to a constant expression (those with a Frame) are |
| 3173 | // both readable and writable inside constant expressions. |
| 3174 | // In C, such things can also be folded, although they are not ICEs. |
| 3175 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
| 3176 | if (VD) { |
| 3177 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 3178 | VD = VDef; |
| 3179 | } |
| 3180 | if (!VD || VD->isInvalidDecl()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3181 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3182 | return CompleteObject(); |
| 3183 | } |
| 3184 | |
| 3185 | // Accesses of volatile-qualified objects are not allowed. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3186 | if (BaseType.isVolatileQualified()) { |
| 3187 | if (Info.getLangOpts().CPlusPlus) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3188 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3189 | << AK << 1 << VD; |
| 3190 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3191 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3192 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3193 | } |
| 3194 | return CompleteObject(); |
| 3195 | } |
| 3196 | |
| 3197 | // Unless we're looking at a local variable or argument in a constexpr call, |
| 3198 | // the variable we're reading must be const. |
| 3199 | if (!Frame) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3200 | if (Info.getLangOpts().CPlusPlus14 && |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3201 | VD == Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()) { |
| 3202 | // OK, we can read and modify an object if we're in the process of |
| 3203 | // evaluating its initializer, because its lifetime began in this |
| 3204 | // evaluation. |
| 3205 | } else if (AK != AK_Read) { |
| 3206 | // All the remaining cases only permit reading. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3207 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3208 | return CompleteObject(); |
George Burgess IV | b531698 | 2016-12-27 05:33:20 +0000 | [diff] [blame] | 3209 | } else if (VD->isConstexpr()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3210 | // OK, we can read this variable. |
| 3211 | } else if (BaseType->isIntegralOrEnumerationType()) { |
Xiuli Pan | 244e3f6 | 2016-06-07 04:34:00 +0000 | [diff] [blame] | 3212 | // In OpenCL if a variable is in constant address space it is a const value. |
| 3213 | if (!(BaseType.isConstQualified() || |
| 3214 | (Info.getLangOpts().OpenCL && |
| 3215 | BaseType.getAddressSpace() == LangAS::opencl_constant))) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3216 | if (Info.getLangOpts().CPlusPlus) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3217 | Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3218 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3219 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3220 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3221 | } |
| 3222 | return CompleteObject(); |
| 3223 | } |
| 3224 | } else if (BaseType->isFloatingType() && BaseType.isConstQualified()) { |
| 3225 | // We support folding of const floating-point types, in order to make |
| 3226 | // static const data members of such types (supported as an extension) |
| 3227 | // more useful. |
| 3228 | if (Info.getLangOpts().CPlusPlus11) { |
| 3229 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 3230 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3231 | } else { |
| 3232 | Info.CCEDiag(E); |
| 3233 | } |
George Burgess IV | b531698 | 2016-12-27 05:33:20 +0000 | [diff] [blame] | 3234 | } else if (BaseType.isConstQualified() && VD->hasDefinition(Info.Ctx)) { |
| 3235 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr) << VD; |
| 3236 | // Keep evaluating to see what we can do. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3237 | } else { |
| 3238 | // FIXME: Allow folding of values of any literal type in all languages. |
Richard Smith | c0d04a2 | 2016-05-25 22:06:25 +0000 | [diff] [blame] | 3239 | if (Info.checkingPotentialConstantExpression() && |
| 3240 | VD->getType().isConstQualified() && !VD->hasDefinition(Info.Ctx)) { |
| 3241 | // The definition of this variable could be constexpr. We can't |
| 3242 | // access it right now, but may be able to in future. |
| 3243 | } else if (Info.getLangOpts().CPlusPlus11) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3244 | Info.FFDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3245 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3246 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3247 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3248 | } |
| 3249 | return CompleteObject(); |
| 3250 | } |
| 3251 | } |
| 3252 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3253 | if (!evaluateVarDeclInit(Info, E, VD, Frame, BaseVal, &LVal)) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3254 | return CompleteObject(); |
| 3255 | } else { |
| 3256 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| 3257 | |
| 3258 | if (!Frame) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3259 | if (const MaterializeTemporaryExpr *MTE = |
| 3260 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 3261 | assert(MTE->getStorageDuration() == SD_Static && |
| 3262 | "should have a frame for a non-global materialized temporary"); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3263 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3264 | // Per C++1y [expr.const]p2: |
| 3265 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
| 3266 | // - a [...] glvalue of integral or enumeration type that refers to |
| 3267 | // a non-volatile const object [...] |
| 3268 | // [...] |
| 3269 | // - a [...] glvalue of literal type that refers to a non-volatile |
| 3270 | // object whose lifetime began within the evaluation of e. |
| 3271 | // |
| 3272 | // C++11 misses the 'began within the evaluation of e' check and |
| 3273 | // instead allows all temporaries, including things like: |
| 3274 | // int &&r = 1; |
| 3275 | // int x = ++r; |
| 3276 | // constexpr int k = r; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3277 | // Therefore we use the C++14 rules in C++11 too. |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3278 | const ValueDecl *VD = Info.EvaluatingDecl.dyn_cast<const ValueDecl*>(); |
| 3279 | const ValueDecl *ED = MTE->getExtendingDecl(); |
| 3280 | if (!(BaseType.isConstQualified() && |
| 3281 | BaseType->isIntegralOrEnumerationType()) && |
| 3282 | !(VD && VD->getCanonicalDecl() == ED->getCanonicalDecl())) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3283 | Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3284 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
| 3285 | return CompleteObject(); |
| 3286 | } |
| 3287 | |
| 3288 | BaseVal = Info.Ctx.getMaterializedTemporaryValue(MTE, false); |
| 3289 | assert(BaseVal && "got reference to unevaluated temporary"); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3290 | LifetimeStartedInEvaluation = true; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3291 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3292 | Info.FFDiag(E); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3293 | return CompleteObject(); |
| 3294 | } |
| 3295 | } else { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3296 | BaseVal = Frame->getTemporary(Base, LVal.Base.getVersion()); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3297 | assert(BaseVal && "missing value for temporary"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3298 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3299 | |
| 3300 | // Volatile temporary objects cannot be accessed in constant expressions. |
| 3301 | if (BaseType.isVolatileQualified()) { |
| 3302 | if (Info.getLangOpts().CPlusPlus) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3303 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3304 | << AK << 0; |
| 3305 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 3306 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3307 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3308 | } |
| 3309 | return CompleteObject(); |
| 3310 | } |
| 3311 | } |
| 3312 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3313 | // During the construction of an object, it is not yet 'const'. |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 3314 | // FIXME: This doesn't do quite the right thing for const subobjects of the |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3315 | // object under construction. |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3316 | if (Info.isEvaluatingConstructor(LVal.getLValueBase(), |
| 3317 | LVal.getLValueCallIndex(), |
| 3318 | LVal.getLValueVersion())) { |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3319 | BaseType = Info.Ctx.getCanonicalType(BaseType); |
| 3320 | BaseType.removeLocalConst(); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3321 | LifetimeStartedInEvaluation = true; |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3324 | // 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] | 3325 | // evaluating after an unmodeled side effect. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3326 | // |
| 3327 | // FIXME: Not all local state is mutable. Allow local constant subobjects |
| 3328 | // to be read here (but take care with 'mutable' fields). |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 3329 | if ((Frame && Info.getLangOpts().CPlusPlus14 && |
| 3330 | Info.EvalStatus.HasSideEffects) || |
| 3331 | (AK != AK_Read && Info.IsSpeculativelyEvaluating)) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3332 | return CompleteObject(); |
| 3333 | |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3334 | return CompleteObject(BaseVal, BaseType, LifetimeStartedInEvaluation); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3337 | /// Perform an lvalue-to-rvalue conversion on the given glvalue. This |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3338 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
| 3339 | /// glvalue referred to by an entity of reference type. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3340 | /// |
| 3341 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3342 | /// \param Conv - The expression for which we are performing the conversion. |
| 3343 | /// Used for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3344 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
| 3345 | /// case of a non-class type). |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3346 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 3347 | /// \param RVal - The produced value will be placed here. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3348 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3349 | QualType Type, |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3350 | const LValue &LVal, APValue &RVal) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3351 | if (LVal.Designator.Invalid) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3352 | return false; |
| 3353 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3354 | // 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] | 3355 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3356 | if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3357 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 3358 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 3359 | // initializer until now for such expressions. Such an expression can't be |
| 3360 | // an ICE in C, so this only matters for fold. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3361 | if (Type.isVolatileQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3362 | Info.FFDiag(Conv); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3363 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3364 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3365 | APValue Lit; |
| 3366 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
| 3367 | return false; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3368 | CompleteObject LitObj(&Lit, Base->getType(), false); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3369 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal); |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 3370 | } else if (isa<StringLiteral>(Base) || isa<PredefinedExpr>(Base)) { |
Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 3371 | // Special-case character extraction so we don't have to construct an |
| 3372 | // APValue for the whole string. |
Eli Friedman | 041adb0 | 2019-02-09 02:22:17 +0000 | [diff] [blame] | 3373 | assert(LVal.Designator.Entries.size() <= 1 && |
Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 3374 | "Can only read characters from string literals"); |
Eli Friedman | 041adb0 | 2019-02-09 02:22:17 +0000 | [diff] [blame] | 3375 | if (LVal.Designator.Entries.empty()) { |
| 3376 | // Fail for now for LValue to RValue conversion of an array. |
| 3377 | // (This shouldn't show up in C/C++, but it could be triggered by a |
| 3378 | // weird EvaluateAsRValue call from a tool.) |
| 3379 | Info.FFDiag(Conv); |
| 3380 | return false; |
| 3381 | } |
Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 3382 | if (LVal.Designator.isOnePastTheEnd()) { |
| 3383 | if (Info.getLangOpts().CPlusPlus11) |
| 3384 | Info.FFDiag(Conv, diag::note_constexpr_access_past_end) << AK_Read; |
| 3385 | else |
| 3386 | Info.FFDiag(Conv); |
| 3387 | return false; |
| 3388 | } |
| 3389 | uint64_t CharIndex = LVal.Designator.Entries[0].ArrayIndex; |
| 3390 | RVal = APValue(extractStringLiteralCharacter(Info, Base, CharIndex)); |
| 3391 | return true; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3392 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3393 | } |
| 3394 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3395 | CompleteObject Obj = findCompleteObject(Info, Conv, AK_Read, LVal, Type); |
| 3396 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3400 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3401 | QualType LValType, APValue &Val) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3402 | if (LVal.Designator.Invalid) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3403 | return false; |
| 3404 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3405 | if (!Info.getLangOpts().CPlusPlus14) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3406 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3407 | return false; |
| 3408 | } |
| 3409 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3410 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3411 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
| 3412 | } |
| 3413 | |
| 3414 | namespace { |
| 3415 | struct CompoundAssignSubobjectHandler { |
| 3416 | EvalInfo &Info; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3417 | const Expr *E; |
| 3418 | QualType PromotedLHSType; |
| 3419 | BinaryOperatorKind Opcode; |
| 3420 | const APValue &RHS; |
| 3421 | |
| 3422 | static const AccessKinds AccessKind = AK_Assign; |
| 3423 | |
| 3424 | typedef bool result_type; |
| 3425 | |
| 3426 | bool checkConst(QualType QT) { |
| 3427 | // Assigning to a const object has undefined behavior. |
| 3428 | if (QT.isConstQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3429 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3430 | return false; |
| 3431 | } |
| 3432 | return true; |
| 3433 | } |
| 3434 | |
| 3435 | bool failed() { return false; } |
| 3436 | bool found(APValue &Subobj, QualType SubobjType) { |
| 3437 | switch (Subobj.getKind()) { |
| 3438 | case APValue::Int: |
| 3439 | return found(Subobj.getInt(), SubobjType); |
| 3440 | case APValue::Float: |
| 3441 | return found(Subobj.getFloat(), SubobjType); |
| 3442 | case APValue::ComplexInt: |
| 3443 | case APValue::ComplexFloat: |
| 3444 | // FIXME: Implement complex compound assignment. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3445 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3446 | return false; |
| 3447 | case APValue::LValue: |
| 3448 | return foundPointer(Subobj, SubobjType); |
| 3449 | default: |
| 3450 | // FIXME: can this happen? |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3451 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3452 | return false; |
| 3453 | } |
| 3454 | } |
| 3455 | bool found(APSInt &Value, QualType SubobjType) { |
| 3456 | if (!checkConst(SubobjType)) |
| 3457 | return false; |
| 3458 | |
Tan S. B. | 9f935e8 | 2018-12-18 07:38:06 +0000 | [diff] [blame] | 3459 | if (!SubobjType->isIntegerType()) { |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3460 | // We don't support compound assignment on integer-cast-to-pointer |
| 3461 | // values. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3462 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3463 | return false; |
| 3464 | } |
| 3465 | |
Tan S. B. | 9f935e8 | 2018-12-18 07:38:06 +0000 | [diff] [blame] | 3466 | if (RHS.isInt()) { |
| 3467 | APSInt LHS = |
| 3468 | HandleIntToIntCast(Info, E, PromotedLHSType, SubobjType, Value); |
| 3469 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
| 3470 | return false; |
| 3471 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
| 3472 | return true; |
| 3473 | } else if (RHS.isFloat()) { |
| 3474 | APFloat FValue(0.0); |
| 3475 | return HandleIntToFloatCast(Info, E, SubobjType, Value, PromotedLHSType, |
| 3476 | FValue) && |
| 3477 | handleFloatFloatBinOp(Info, E, FValue, Opcode, RHS.getFloat()) && |
| 3478 | HandleFloatToIntCast(Info, E, PromotedLHSType, FValue, SubobjType, |
| 3479 | Value); |
| 3480 | } |
| 3481 | |
| 3482 | Info.FFDiag(E); |
| 3483 | return false; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3484 | } |
| 3485 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3486 | return checkConst(SubobjType) && |
| 3487 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
| 3488 | Value) && |
| 3489 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
| 3490 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3491 | } |
| 3492 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 3493 | if (!checkConst(SubobjType)) |
| 3494 | return false; |
| 3495 | |
| 3496 | QualType PointeeType; |
| 3497 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 3498 | PointeeType = PT->getPointeeType(); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3499 | |
| 3500 | if (PointeeType.isNull() || !RHS.isInt() || |
| 3501 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3502 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3503 | return false; |
| 3504 | } |
| 3505 | |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 3506 | APSInt Offset = RHS.getInt(); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3507 | if (Opcode == BO_Sub) |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 3508 | negateAsSigned(Offset); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3509 | |
| 3510 | LValue LVal; |
| 3511 | LVal.setFrom(Info.Ctx, Subobj); |
| 3512 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
| 3513 | return false; |
| 3514 | LVal.moveInto(Subobj); |
| 3515 | return true; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3516 | } |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3517 | }; |
| 3518 | } // end anonymous namespace |
| 3519 | |
| 3520 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
| 3521 | |
| 3522 | /// Perform a compound assignment of LVal <op>= RVal. |
| 3523 | static bool handleCompoundAssignment( |
| 3524 | EvalInfo &Info, const Expr *E, |
| 3525 | const LValue &LVal, QualType LValType, QualType PromotedLValType, |
| 3526 | BinaryOperatorKind Opcode, const APValue &RVal) { |
| 3527 | if (LVal.Designator.Invalid) |
| 3528 | return false; |
| 3529 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3530 | if (!Info.getLangOpts().CPlusPlus14) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3531 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3532 | return false; |
| 3533 | } |
| 3534 | |
| 3535 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 3536 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
| 3537 | RVal }; |
| 3538 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 3539 | } |
| 3540 | |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3541 | namespace { |
| 3542 | struct IncDecSubobjectHandler { |
| 3543 | EvalInfo &Info; |
| 3544 | const UnaryOperator *E; |
| 3545 | AccessKinds AccessKind; |
| 3546 | APValue *Old; |
| 3547 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3548 | typedef bool result_type; |
| 3549 | |
| 3550 | bool checkConst(QualType QT) { |
| 3551 | // Assigning to a const object has undefined behavior. |
| 3552 | if (QT.isConstQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3553 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3554 | return false; |
| 3555 | } |
| 3556 | return true; |
| 3557 | } |
| 3558 | |
| 3559 | bool failed() { return false; } |
| 3560 | bool found(APValue &Subobj, QualType SubobjType) { |
| 3561 | // Stash the old value. Also clear Old, so we don't clobber it later |
| 3562 | // if we're post-incrementing a complex. |
| 3563 | if (Old) { |
| 3564 | *Old = Subobj; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3565 | Old = nullptr; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3566 | } |
| 3567 | |
| 3568 | switch (Subobj.getKind()) { |
| 3569 | case APValue::Int: |
| 3570 | return found(Subobj.getInt(), SubobjType); |
| 3571 | case APValue::Float: |
| 3572 | return found(Subobj.getFloat(), SubobjType); |
| 3573 | case APValue::ComplexInt: |
| 3574 | return found(Subobj.getComplexIntReal(), |
| 3575 | SubobjType->castAs<ComplexType>()->getElementType() |
| 3576 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 3577 | case APValue::ComplexFloat: |
| 3578 | return found(Subobj.getComplexFloatReal(), |
| 3579 | SubobjType->castAs<ComplexType>()->getElementType() |
| 3580 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 3581 | case APValue::LValue: |
| 3582 | return foundPointer(Subobj, SubobjType); |
| 3583 | default: |
| 3584 | // FIXME: can this happen? |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3585 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3586 | return false; |
| 3587 | } |
| 3588 | } |
| 3589 | bool found(APSInt &Value, QualType SubobjType) { |
| 3590 | if (!checkConst(SubobjType)) |
| 3591 | return false; |
| 3592 | |
| 3593 | if (!SubobjType->isIntegerType()) { |
| 3594 | // We don't support increment / decrement on integer-cast-to-pointer |
| 3595 | // values. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3596 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3597 | return false; |
| 3598 | } |
| 3599 | |
| 3600 | if (Old) *Old = APValue(Value); |
| 3601 | |
| 3602 | // bool arithmetic promotes to int, and the conversion back to bool |
| 3603 | // doesn't reduce mod 2^n, so special-case it. |
| 3604 | if (SubobjType->isBooleanType()) { |
| 3605 | if (AccessKind == AK_Increment) |
| 3606 | Value = 1; |
| 3607 | else |
| 3608 | Value = !Value; |
| 3609 | return true; |
| 3610 | } |
| 3611 | |
| 3612 | bool WasNegative = Value.isNegative(); |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3613 | if (AccessKind == AK_Increment) { |
| 3614 | ++Value; |
| 3615 | |
| 3616 | if (!WasNegative && Value.isNegative() && E->canOverflow()) { |
| 3617 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
| 3618 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
| 3619 | } |
| 3620 | } else { |
| 3621 | --Value; |
| 3622 | |
| 3623 | if (WasNegative && !Value.isNegative() && E->canOverflow()) { |
| 3624 | unsigned BitWidth = Value.getBitWidth(); |
| 3625 | APSInt ActualValue(Value.sext(BitWidth + 1), /*IsUnsigned*/false); |
| 3626 | ActualValue.setBit(BitWidth); |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 3627 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3628 | } |
| 3629 | } |
| 3630 | return true; |
| 3631 | } |
| 3632 | bool found(APFloat &Value, QualType SubobjType) { |
| 3633 | if (!checkConst(SubobjType)) |
| 3634 | return false; |
| 3635 | |
| 3636 | if (Old) *Old = APValue(Value); |
| 3637 | |
| 3638 | APFloat One(Value.getSemantics(), 1); |
| 3639 | if (AccessKind == AK_Increment) |
| 3640 | Value.add(One, APFloat::rmNearestTiesToEven); |
| 3641 | else |
| 3642 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
| 3643 | return true; |
| 3644 | } |
| 3645 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 3646 | if (!checkConst(SubobjType)) |
| 3647 | return false; |
| 3648 | |
| 3649 | QualType PointeeType; |
| 3650 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 3651 | PointeeType = PT->getPointeeType(); |
| 3652 | else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3653 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3654 | return false; |
| 3655 | } |
| 3656 | |
| 3657 | LValue LVal; |
| 3658 | LVal.setFrom(Info.Ctx, Subobj); |
| 3659 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
| 3660 | AccessKind == AK_Increment ? 1 : -1)) |
| 3661 | return false; |
| 3662 | LVal.moveInto(Subobj); |
| 3663 | return true; |
| 3664 | } |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3665 | }; |
| 3666 | } // end anonymous namespace |
| 3667 | |
| 3668 | /// Perform an increment or decrement on LVal. |
| 3669 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
| 3670 | QualType LValType, bool IsIncrement, APValue *Old) { |
| 3671 | if (LVal.Designator.Invalid) |
| 3672 | return false; |
| 3673 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3674 | if (!Info.getLangOpts().CPlusPlus14) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3675 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3676 | return false; |
| 3677 | } |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3678 | |
| 3679 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
| 3680 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
| 3681 | IncDecSubobjectHandler Handler = {Info, cast<UnaryOperator>(E), AK, Old}; |
| 3682 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 3683 | } |
| 3684 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3685 | /// Build an lvalue for the object argument of a member function call. |
| 3686 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 3687 | LValue &This) { |
| 3688 | if (Object->getType()->isPointerType()) |
| 3689 | return EvaluatePointer(Object, This, Info); |
| 3690 | |
| 3691 | if (Object->isGLValue()) |
| 3692 | return EvaluateLValue(Object, This, Info); |
| 3693 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3694 | if (Object->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3695 | return EvaluateTemporary(Object, This, Info); |
| 3696 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3697 | Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3698 | return false; |
| 3699 | } |
| 3700 | |
| 3701 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 3702 | /// lvalue referring to the result. |
| 3703 | /// |
| 3704 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3705 | /// \param LV - An lvalue referring to the base of the member pointer. |
| 3706 | /// \param RHS - The member pointer expression. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3707 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 3708 | /// the resulting LValue subobject designator. This is not possible when |
| 3709 | /// creating a bound member function. |
| 3710 | /// \return The field or method declaration to which the member pointer refers, |
| 3711 | /// or 0 if evaluation fails. |
| 3712 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3713 | QualType LVType, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3714 | LValue &LV, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3715 | const Expr *RHS, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3716 | bool IncludeMember = true) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3717 | MemberPtr MemPtr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3718 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3719 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3720 | |
| 3721 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 3722 | // member value, the behavior is undefined. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3723 | if (!MemPtr.getDecl()) { |
| 3724 | // FIXME: Specific diagnostic. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3725 | Info.FFDiag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3726 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3727 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3728 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3729 | if (MemPtr.isDerivedMember()) { |
| 3730 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3731 | // The end of the derived-to-base path for the base object must match the |
| 3732 | // derived-to-base path for the member pointer. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3733 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3734 | LV.Designator.Entries.size()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3735 | Info.FFDiag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3736 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3737 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3738 | unsigned PathLengthToMember = |
| 3739 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 3740 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 3741 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 3742 | LV.Designator.Entries[PathLengthToMember + I]); |
| 3743 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3744 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3745 | Info.FFDiag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3746 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3747 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3751 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3752 | PathLengthToMember)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3753 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3754 | } else if (!MemPtr.Path.empty()) { |
| 3755 | // Extend the LValue path with the member pointer's path. |
| 3756 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 3757 | MemPtr.Path.size() + IncludeMember); |
| 3758 | |
| 3759 | // Walk down to the appropriate base class. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3760 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 3761 | LVType = PT->getPointeeType(); |
| 3762 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 3763 | assert(RD && "member pointer access on non-class-type expression"); |
| 3764 | // The first class in the path is that of the lvalue. |
| 3765 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 3766 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3767 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3768 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3769 | RD = Base; |
| 3770 | } |
| 3771 | // Finally cast to the class containing the member. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3772 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
| 3773 | MemPtr.getContainingRecord())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3774 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | // Add the member. Note that we cannot build bound member functions here. |
| 3778 | if (IncludeMember) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3779 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3780 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3781 | return nullptr; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3782 | } else if (const IndirectFieldDecl *IFD = |
| 3783 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3784 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3785 | return nullptr; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3786 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3787 | llvm_unreachable("can't construct reference to bound member function"); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3788 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | return MemPtr.getDecl(); |
| 3792 | } |
| 3793 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3794 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 3795 | const BinaryOperator *BO, |
| 3796 | LValue &LV, |
| 3797 | bool IncludeMember = true) { |
| 3798 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 3799 | |
| 3800 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 3801 | if (Info.noteFailure()) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3802 | MemberPtr MemPtr; |
| 3803 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
| 3804 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3805 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
| 3809 | BO->getRHS(), IncludeMember); |
| 3810 | } |
| 3811 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3812 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 3813 | /// the provided lvalue, which currently refers to the base object. |
| 3814 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 3815 | LValue &Result) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3816 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3817 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3818 | return false; |
| 3819 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3820 | QualType TargetQT = E->getType(); |
| 3821 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 3822 | TargetQT = PT->getPointeeType(); |
| 3823 | |
| 3824 | // Check this cast lands within the final derived-to-base subobject path. |
| 3825 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3826 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3827 | << D.MostDerivedType << TargetQT; |
| 3828 | return false; |
| 3829 | } |
| 3830 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3831 | // Check the type of the final cast. We don't need to check the path, |
| 3832 | // since a cast can only be formed if the path is unique. |
| 3833 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3834 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 3835 | const CXXRecordDecl *FinalType; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3836 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 3837 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 3838 | else |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3839 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3840 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3841 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3842 | << D.MostDerivedType << TargetQT; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3843 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3844 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3845 | |
| 3846 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3847 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3848 | } |
| 3849 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 3850 | namespace { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3851 | enum EvalStmtResult { |
| 3852 | /// Evaluation failed. |
| 3853 | ESR_Failed, |
| 3854 | /// Hit a 'return' statement. |
| 3855 | ESR_Returned, |
| 3856 | /// Evaluation succeeded. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3857 | ESR_Succeeded, |
| 3858 | /// Hit a 'continue' statement. |
| 3859 | ESR_Continue, |
| 3860 | /// Hit a 'break' statement. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3861 | ESR_Break, |
| 3862 | /// Still scanning for 'case' or 'default' statement. |
| 3863 | ESR_CaseNotFound |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3864 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3865 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3866 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3867 | static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) { |
| 3868 | // We don't need to evaluate the initializer for a static local. |
| 3869 | if (!VD->hasLocalStorage()) |
| 3870 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3871 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3872 | LValue Result; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3873 | APValue &Val = createTemporary(VD, true, Result, *Info.CurrentCall); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3874 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3875 | const Expr *InitE = VD->getInit(); |
| 3876 | if (!InitE) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3877 | Info.FFDiag(VD->getBeginLoc(), diag::note_constexpr_uninitialized) |
| 3878 | << false << VD->getType(); |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3879 | Val = APValue(); |
| 3880 | return false; |
| 3881 | } |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3882 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3883 | if (InitE->isValueDependent()) |
| 3884 | return false; |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 3885 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3886 | if (!EvaluateInPlace(Val, Info, Result, InitE)) { |
| 3887 | // Wipe out any partially-computed value, to allow tracking that this |
| 3888 | // evaluation failed. |
| 3889 | Val = APValue(); |
| 3890 | return false; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | return true; |
| 3894 | } |
| 3895 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3896 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
| 3897 | bool OK = true; |
| 3898 | |
| 3899 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 3900 | OK &= EvaluateVarDecl(Info, VD); |
| 3901 | |
| 3902 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(D)) |
| 3903 | for (auto *BD : DD->bindings()) |
| 3904 | if (auto *VD = BD->getHoldingVar()) |
| 3905 | OK &= EvaluateDecl(Info, VD); |
| 3906 | |
| 3907 | return OK; |
| 3908 | } |
| 3909 | |
| 3910 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3911 | /// Evaluate a condition (either a variable declaration or an expression). |
| 3912 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
| 3913 | const Expr *Cond, bool &Result) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3914 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3915 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
| 3916 | return false; |
| 3917 | return EvaluateAsBooleanCondition(Cond, Result, Info); |
| 3918 | } |
| 3919 | |
Richard Smith | 8921007 | 2016-04-04 23:29:43 +0000 | [diff] [blame] | 3920 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3921 | /// A location where the result (returned value) of evaluating a |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3922 | /// statement should be stored. |
| 3923 | struct StmtResult { |
| 3924 | /// The APValue that should be filled in with the returned value. |
| 3925 | APValue &Value; |
| 3926 | /// The location containing the result, if any (used to support RVO). |
| 3927 | const LValue *Slot; |
| 3928 | }; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3929 | |
| 3930 | struct TempVersionRAII { |
| 3931 | CallStackFrame &Frame; |
| 3932 | |
| 3933 | TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) { |
| 3934 | Frame.pushTempVersion(); |
| 3935 | } |
| 3936 | |
| 3937 | ~TempVersionRAII() { |
| 3938 | Frame.popTempVersion(); |
| 3939 | } |
| 3940 | }; |
| 3941 | |
Richard Smith | 8921007 | 2016-04-04 23:29:43 +0000 | [diff] [blame] | 3942 | } |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3943 | |
| 3944 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3945 | const Stmt *S, |
| 3946 | const SwitchCase *SC = nullptr); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3947 | |
| 3948 | /// Evaluate the body of a loop, and translate the result as appropriate. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3949 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3950 | const Stmt *Body, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3951 | const SwitchCase *Case = nullptr) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3952 | BlockScopeRAII Scope(Info); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3953 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case)) { |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3954 | case ESR_Break: |
| 3955 | return ESR_Succeeded; |
| 3956 | case ESR_Succeeded: |
| 3957 | case ESR_Continue: |
| 3958 | return ESR_Continue; |
| 3959 | case ESR_Failed: |
| 3960 | case ESR_Returned: |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3961 | case ESR_CaseNotFound: |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3962 | return ESR; |
| 3963 | } |
Hans Wennborg | 9242bd1 | 2013-05-06 15:13:34 +0000 | [diff] [blame] | 3964 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3965 | } |
| 3966 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3967 | /// Evaluate a switch statement. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3968 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3969 | const SwitchStmt *SS) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3970 | BlockScopeRAII Scope(Info); |
| 3971 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3972 | // Evaluate the switch condition. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3973 | APSInt Value; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3974 | { |
| 3975 | FullExpressionRAII Scope(Info); |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3976 | if (const Stmt *Init = SS->getInit()) { |
| 3977 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
| 3978 | if (ESR != ESR_Succeeded) |
| 3979 | return ESR; |
| 3980 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3981 | if (SS->getConditionVariable() && |
| 3982 | !EvaluateDecl(Info, SS->getConditionVariable())) |
| 3983 | return ESR_Failed; |
| 3984 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
| 3985 | return ESR_Failed; |
| 3986 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3987 | |
| 3988 | // Find the switch case corresponding to the value of the condition. |
| 3989 | // FIXME: Cache this lookup. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3990 | const SwitchCase *Found = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3991 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 3992 | SC = SC->getNextSwitchCase()) { |
| 3993 | if (isa<DefaultStmt>(SC)) { |
| 3994 | Found = SC; |
| 3995 | continue; |
| 3996 | } |
| 3997 | |
| 3998 | const CaseStmt *CS = cast<CaseStmt>(SC); |
| 3999 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
| 4000 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
| 4001 | : LHS; |
| 4002 | if (LHS <= Value && Value <= RHS) { |
| 4003 | Found = SC; |
| 4004 | break; |
| 4005 | } |
| 4006 | } |
| 4007 | |
| 4008 | if (!Found) |
| 4009 | return ESR_Succeeded; |
| 4010 | |
| 4011 | // Search the switch body for the switch case and evaluate it from there. |
| 4012 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found)) { |
| 4013 | case ESR_Break: |
| 4014 | return ESR_Succeeded; |
| 4015 | case ESR_Succeeded: |
| 4016 | case ESR_Continue: |
| 4017 | case ESR_Failed: |
| 4018 | case ESR_Returned: |
| 4019 | return ESR; |
| 4020 | case ESR_CaseNotFound: |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4021 | // This can only happen if the switch case is nested within a statement |
| 4022 | // expression. We have no intention of supporting that. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4023 | Info.FFDiag(Found->getBeginLoc(), |
| 4024 | diag::note_constexpr_stmt_expr_unsupported); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 4025 | return ESR_Failed; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4026 | } |
Richard Smith | f8cf9d4 | 2013-05-13 20:33:30 +0000 | [diff] [blame] | 4027 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4030 | // Evaluate a statement. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4031 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4032 | const Stmt *S, const SwitchCase *Case) { |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 4033 | if (!Info.nextStep(S)) |
| 4034 | return ESR_Failed; |
| 4035 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4036 | // If we're hunting down a 'case' or 'default' label, recurse through |
| 4037 | // substatements until we hit the label. |
| 4038 | if (Case) { |
| 4039 | // FIXME: We don't start the lifetime of objects whose initialization we |
| 4040 | // jump over. However, such objects must be of class type with a trivial |
| 4041 | // default constructor that initialize all subobjects, so must be empty, |
| 4042 | // so this almost never matters. |
| 4043 | switch (S->getStmtClass()) { |
| 4044 | case Stmt::CompoundStmtClass: |
| 4045 | // FIXME: Precompute which substatement of a compound statement we |
| 4046 | // would jump to, and go straight there rather than performing a |
| 4047 | // linear scan each time. |
| 4048 | case Stmt::LabelStmtClass: |
| 4049 | case Stmt::AttributedStmtClass: |
| 4050 | case Stmt::DoStmtClass: |
| 4051 | break; |
| 4052 | |
| 4053 | case Stmt::CaseStmtClass: |
| 4054 | case Stmt::DefaultStmtClass: |
| 4055 | if (Case == S) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4056 | Case = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4057 | break; |
| 4058 | |
| 4059 | case Stmt::IfStmtClass: { |
| 4060 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
| 4061 | // straight there rather than scanning both sides. |
| 4062 | const IfStmt *IS = cast<IfStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4063 | |
| 4064 | // Wrap the evaluation in a block scope, in case it's a DeclStmt |
| 4065 | // preceded by our switch label. |
| 4066 | BlockScopeRAII Scope(Info); |
| 4067 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4068 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
| 4069 | if (ESR != ESR_CaseNotFound || !IS->getElse()) |
| 4070 | return ESR; |
| 4071 | return EvaluateStmt(Result, Info, IS->getElse(), Case); |
| 4072 | } |
| 4073 | |
| 4074 | case Stmt::WhileStmtClass: { |
| 4075 | EvalStmtResult ESR = |
| 4076 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
| 4077 | if (ESR != ESR_Continue) |
| 4078 | return ESR; |
| 4079 | break; |
| 4080 | } |
| 4081 | |
| 4082 | case Stmt::ForStmtClass: { |
| 4083 | const ForStmt *FS = cast<ForStmt>(S); |
| 4084 | EvalStmtResult ESR = |
| 4085 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
| 4086 | if (ESR != ESR_Continue) |
| 4087 | return ESR; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4088 | if (FS->getInc()) { |
| 4089 | FullExpressionRAII IncScope(Info); |
| 4090 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 4091 | return ESR_Failed; |
| 4092 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4093 | break; |
| 4094 | } |
| 4095 | |
| 4096 | case Stmt::DeclStmtClass: |
| 4097 | // FIXME: If the variable has initialization that can't be jumped over, |
| 4098 | // bail out of any immediately-surrounding compound-statement too. |
| 4099 | default: |
| 4100 | return ESR_CaseNotFound; |
| 4101 | } |
| 4102 | } |
| 4103 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4104 | switch (S->getStmtClass()) { |
| 4105 | default: |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4106 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4107 | // Don't bother evaluating beyond an expression-statement which couldn't |
| 4108 | // be evaluated. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4109 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4110 | if (!EvaluateIgnoredValue(Info, E)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4111 | return ESR_Failed; |
| 4112 | return ESR_Succeeded; |
| 4113 | } |
| 4114 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4115 | Info.FFDiag(S->getBeginLoc()); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4116 | return ESR_Failed; |
| 4117 | |
| 4118 | case Stmt::NullStmtClass: |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4119 | return ESR_Succeeded; |
| 4120 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4121 | case Stmt::DeclStmtClass: { |
| 4122 | const DeclStmt *DS = cast<DeclStmt>(S); |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 4123 | for (const auto *DclIt : DS->decls()) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4124 | // Each declaration initialization is its own full-expression. |
| 4125 | // FIXME: This isn't quite right; if we're performing aggregate |
| 4126 | // initialization, each braced subexpression is its own full-expression. |
| 4127 | FullExpressionRAII Scope(Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 4128 | if (!EvaluateDecl(Info, DclIt) && !Info.noteFailure()) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4129 | return ESR_Failed; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4130 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4131 | return ESR_Succeeded; |
| 4132 | } |
| 4133 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4134 | case Stmt::ReturnStmtClass: { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4135 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4136 | FullExpressionRAII Scope(Info); |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4137 | if (RetExpr && |
| 4138 | !(Result.Slot |
| 4139 | ? EvaluateInPlace(Result.Value, Info, *Result.Slot, RetExpr) |
| 4140 | : Evaluate(Result.Value, Info, RetExpr))) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4141 | return ESR_Failed; |
| 4142 | return ESR_Returned; |
| 4143 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4144 | |
| 4145 | case Stmt::CompoundStmtClass: { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4146 | BlockScopeRAII Scope(Info); |
| 4147 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4148 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 4149 | for (const auto *BI : CS->body()) { |
| 4150 | EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4151 | if (ESR == ESR_Succeeded) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4152 | Case = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4153 | else if (ESR != ESR_CaseNotFound) |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4154 | return ESR; |
| 4155 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4156 | return Case ? ESR_CaseNotFound : ESR_Succeeded; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4157 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4158 | |
| 4159 | case Stmt::IfStmtClass: { |
| 4160 | const IfStmt *IS = cast<IfStmt>(S); |
| 4161 | |
| 4162 | // Evaluate the condition, as either a var decl or as an expression. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4163 | BlockScopeRAII Scope(Info); |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 4164 | if (const Stmt *Init = IS->getInit()) { |
| 4165 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
| 4166 | if (ESR != ESR_Succeeded) |
| 4167 | return ESR; |
| 4168 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4169 | bool Cond; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4170 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4171 | return ESR_Failed; |
| 4172 | |
| 4173 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
| 4174 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
| 4175 | if (ESR != ESR_Succeeded) |
| 4176 | return ESR; |
| 4177 | } |
| 4178 | return ESR_Succeeded; |
| 4179 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4180 | |
| 4181 | case Stmt::WhileStmtClass: { |
| 4182 | const WhileStmt *WS = cast<WhileStmt>(S); |
| 4183 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4184 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4185 | bool Continue; |
| 4186 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
| 4187 | Continue)) |
| 4188 | return ESR_Failed; |
| 4189 | if (!Continue) |
| 4190 | break; |
| 4191 | |
| 4192 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
| 4193 | if (ESR != ESR_Continue) |
| 4194 | return ESR; |
| 4195 | } |
| 4196 | return ESR_Succeeded; |
| 4197 | } |
| 4198 | |
| 4199 | case Stmt::DoStmtClass: { |
| 4200 | const DoStmt *DS = cast<DoStmt>(S); |
| 4201 | bool Continue; |
| 4202 | do { |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4203 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4204 | if (ESR != ESR_Continue) |
| 4205 | return ESR; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4206 | Case = nullptr; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4207 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4208 | FullExpressionRAII CondScope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4209 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info)) |
| 4210 | return ESR_Failed; |
| 4211 | } while (Continue); |
| 4212 | return ESR_Succeeded; |
| 4213 | } |
| 4214 | |
| 4215 | case Stmt::ForStmtClass: { |
| 4216 | const ForStmt *FS = cast<ForStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4217 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4218 | if (FS->getInit()) { |
| 4219 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| 4220 | if (ESR != ESR_Succeeded) |
| 4221 | return ESR; |
| 4222 | } |
| 4223 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4224 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4225 | bool Continue = true; |
| 4226 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
| 4227 | FS->getCond(), Continue)) |
| 4228 | return ESR_Failed; |
| 4229 | if (!Continue) |
| 4230 | break; |
| 4231 | |
| 4232 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 4233 | if (ESR != ESR_Continue) |
| 4234 | return ESR; |
| 4235 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4236 | if (FS->getInc()) { |
| 4237 | FullExpressionRAII IncScope(Info); |
| 4238 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 4239 | return ESR_Failed; |
| 4240 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4241 | } |
| 4242 | return ESR_Succeeded; |
| 4243 | } |
| 4244 | |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4245 | case Stmt::CXXForRangeStmtClass: { |
| 4246 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4247 | BlockScopeRAII Scope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4248 | |
Richard Smith | 8baa500 | 2018-09-28 18:44:09 +0000 | [diff] [blame] | 4249 | // Evaluate the init-statement if present. |
| 4250 | if (FS->getInit()) { |
| 4251 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| 4252 | if (ESR != ESR_Succeeded) |
| 4253 | return ESR; |
| 4254 | } |
| 4255 | |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4256 | // Initialize the __range variable. |
| 4257 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
| 4258 | if (ESR != ESR_Succeeded) |
| 4259 | return ESR; |
| 4260 | |
| 4261 | // Create the __begin and __end iterators. |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 4262 | ESR = EvaluateStmt(Result, Info, FS->getBeginStmt()); |
| 4263 | if (ESR != ESR_Succeeded) |
| 4264 | return ESR; |
| 4265 | ESR = EvaluateStmt(Result, Info, FS->getEndStmt()); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4266 | if (ESR != ESR_Succeeded) |
| 4267 | return ESR; |
| 4268 | |
| 4269 | while (true) { |
| 4270 | // Condition: __begin != __end. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4271 | { |
| 4272 | bool Continue = true; |
| 4273 | FullExpressionRAII CondExpr(Info); |
| 4274 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
| 4275 | return ESR_Failed; |
| 4276 | if (!Continue) |
| 4277 | break; |
| 4278 | } |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4279 | |
| 4280 | // User's variable declaration, initialized by *__begin. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4281 | BlockScopeRAII InnerScope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4282 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
| 4283 | if (ESR != ESR_Succeeded) |
| 4284 | return ESR; |
| 4285 | |
| 4286 | // Loop body. |
| 4287 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 4288 | if (ESR != ESR_Continue) |
| 4289 | return ESR; |
| 4290 | |
| 4291 | // Increment: ++__begin |
| 4292 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 4293 | return ESR_Failed; |
| 4294 | } |
| 4295 | |
| 4296 | return ESR_Succeeded; |
| 4297 | } |
| 4298 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4299 | case Stmt::SwitchStmtClass: |
| 4300 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
| 4301 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4302 | case Stmt::ContinueStmtClass: |
| 4303 | return ESR_Continue; |
| 4304 | |
| 4305 | case Stmt::BreakStmtClass: |
| 4306 | return ESR_Break; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4307 | |
| 4308 | case Stmt::LabelStmtClass: |
| 4309 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
| 4310 | |
| 4311 | case Stmt::AttributedStmtClass: |
| 4312 | // As a general principle, C++11 attributes can be ignored without |
| 4313 | // any semantic impact. |
| 4314 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
| 4315 | Case); |
| 4316 | |
| 4317 | case Stmt::CaseStmtClass: |
| 4318 | case Stmt::DefaultStmtClass: |
| 4319 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
Bruno Cardoso Lopes | 5c1399a | 2018-12-10 19:03:12 +0000 | [diff] [blame] | 4320 | case Stmt::CXXTryStmtClass: |
| 4321 | // Evaluate try blocks by evaluating all sub statements. |
| 4322 | return EvaluateStmt(Result, Info, cast<CXXTryStmt>(S)->getTryBlock(), Case); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4323 | } |
| 4324 | } |
| 4325 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4326 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 4327 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 4328 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 4329 | /// so we need special handling. |
| 4330 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4331 | const CXXConstructorDecl *CD, |
| 4332 | bool IsValueInitialization) { |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4333 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 4334 | return false; |
| 4335 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 4336 | // Value-initialization does not call a trivial default constructor, so such a |
| 4337 | // call is a core constant expression whether or not the constructor is |
| 4338 | // constexpr. |
| 4339 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4340 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 4341 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 4342 | // we should be much more explicit about why it's not constexpr. |
| 4343 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 4344 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 4345 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4346 | } else { |
| 4347 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 4348 | } |
| 4349 | } |
| 4350 | return true; |
| 4351 | } |
| 4352 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4353 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 4354 | /// expression. |
| 4355 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 4356 | const FunctionDecl *Declaration, |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 4357 | const FunctionDecl *Definition, |
| 4358 | const Stmt *Body) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4359 | // Potential constant expressions can contain calls to declared, but not yet |
| 4360 | // defined, constexpr functions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4361 | if (Info.checkingPotentialConstantExpression() && !Definition && |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4362 | Declaration->isConstexpr()) |
| 4363 | return false; |
| 4364 | |
James Y Knight | c7d3e60 | 2018-10-05 17:49:48 +0000 | [diff] [blame] | 4365 | // Bail out if the function declaration itself is invalid. We will |
| 4366 | // have produced a relevant diagnostic while parsing it, so just |
| 4367 | // note the problematic sub-expression. |
| 4368 | if (Declaration->isInvalidDecl()) { |
| 4369 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 0838f3a | 2013-05-14 05:18:44 +0000 | [diff] [blame] | 4370 | return false; |
James Y Knight | c7d3e60 | 2018-10-05 17:49:48 +0000 | [diff] [blame] | 4371 | } |
Richard Smith | 0838f3a | 2013-05-14 05:18:44 +0000 | [diff] [blame] | 4372 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4373 | // Can we evaluate this function call? |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 4374 | if (Definition && Definition->isConstexpr() && |
| 4375 | !Definition->isInvalidDecl() && Body) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4376 | return true; |
| 4377 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4378 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4379 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4380 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4381 | // If this function is not constexpr because it is an inherited |
| 4382 | // non-constexpr constructor, diagnose that directly. |
| 4383 | auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl); |
| 4384 | if (CD && CD->isInheritingConstructor()) { |
| 4385 | auto *Inherited = CD->getInheritedConstructor().getConstructor(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4386 | if (!Inherited->isConstexpr()) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4387 | DiagDecl = CD = Inherited; |
| 4388 | } |
| 4389 | |
| 4390 | // FIXME: If DiagDecl is an implicitly-declared special member function |
| 4391 | // or an inheriting constructor, we should be much more explicit about why |
| 4392 | // it's not constexpr. |
| 4393 | if (CD && CD->isInheritingConstructor()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4394 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4395 | << CD->getInheritedConstructor().getConstructor()->getParent(); |
| 4396 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4397 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4398 | << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4399 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 4400 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4401 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4402 | } |
| 4403 | return false; |
| 4404 | } |
| 4405 | |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 4406 | /// Determine if a class has any fields that might need to be copied by a |
| 4407 | /// trivial copy or move operation. |
| 4408 | static bool hasFields(const CXXRecordDecl *RD) { |
| 4409 | if (!RD || RD->isEmpty()) |
| 4410 | return false; |
| 4411 | for (auto *FD : RD->fields()) { |
| 4412 | if (FD->isUnnamedBitfield()) |
| 4413 | continue; |
| 4414 | return true; |
| 4415 | } |
| 4416 | for (auto &Base : RD->bases()) |
| 4417 | if (hasFields(Base.getType()->getAsCXXRecordDecl())) |
| 4418 | return true; |
| 4419 | return false; |
| 4420 | } |
| 4421 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4422 | namespace { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4423 | typedef SmallVector<APValue, 8> ArgVector; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 4427 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 4428 | EvalInfo &Info) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4429 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4430 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4431 | I != E; ++I) { |
| 4432 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 4433 | // If we're checking for a potential constant expression, evaluate all |
| 4434 | // initializers even if some of them fail. |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 4435 | if (!Info.noteFailure()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4436 | return false; |
| 4437 | Success = false; |
| 4438 | } |
| 4439 | } |
| 4440 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4441 | } |
| 4442 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4443 | /// Evaluate a function call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4444 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 4445 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4446 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4447 | EvalInfo &Info, APValue &Result, |
| 4448 | const LValue *ResultSlot) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4449 | ArgVector ArgValues(Args.size()); |
| 4450 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 4451 | return false; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4452 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4453 | if (!Info.CheckCallLimit(CallLoc)) |
| 4454 | return false; |
| 4455 | |
| 4456 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4457 | |
| 4458 | // For a trivial copy or move assignment, perform an APValue copy. This is |
| 4459 | // essential for unions, where the operations performed by the assignment |
| 4460 | // operator cannot be represented as statements. |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 4461 | // |
| 4462 | // Skip this for non-union classes with no fields; in that case, the defaulted |
| 4463 | // copy/move does not actually read the object. |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4464 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 4465 | if (MD && MD->isDefaulted() && |
| 4466 | (MD->getParent()->isUnion() || |
| 4467 | (MD->isTrivial() && hasFields(MD->getParent())))) { |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4468 | assert(This && |
| 4469 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
| 4470 | LValue RHS; |
| 4471 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 4472 | APValue RHSValue; |
| 4473 | if (!handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 4474 | RHS, RHSValue)) |
| 4475 | return false; |
Brian Gesiak | 5488ab4 | 2019-01-11 01:54:53 +0000 | [diff] [blame] | 4476 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(), |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4477 | RHSValue)) |
| 4478 | return false; |
| 4479 | This->moveInto(Result); |
| 4480 | return true; |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 4481 | } else if (MD && isLambdaCallOperator(MD)) { |
Erik Pilkington | 1123291 | 2018-04-05 00:12:05 +0000 | [diff] [blame] | 4482 | // We're in a lambda; determine the lambda capture field maps unless we're |
| 4483 | // just constexpr checking a lambda's call operator. constexpr checking is |
| 4484 | // done before the captures have been added to the closure object (unless |
| 4485 | // we're inferring constexpr-ness), so we don't have access to them in this |
| 4486 | // case. But since we don't need the captures to constexpr check, we can |
| 4487 | // just ignore them. |
| 4488 | if (!Info.checkingPotentialConstantExpression()) |
| 4489 | MD->getParent()->getCaptureFields(Frame.LambdaCaptureFields, |
| 4490 | Frame.LambdaThisCaptureField); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4493 | StmtResult Ret = {Result, ResultSlot}; |
| 4494 | EvalStmtResult ESR = EvaluateStmt(Ret, Info, Body); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 4495 | if (ESR == ESR_Succeeded) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4496 | if (Callee->getReturnType()->isVoidType()) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 4497 | return true; |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 4498 | Info.FFDiag(Callee->getEndLoc(), diag::note_constexpr_no_return); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 4499 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4500 | return ESR == ESR_Returned; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4503 | /// Evaluate a constructor call. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4504 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
| 4505 | APValue *ArgValues, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4506 | const CXXConstructorDecl *Definition, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4507 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4508 | SourceLocation CallLoc = E->getExprLoc(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4509 | if (!Info.CheckCallLimit(CallLoc)) |
| 4510 | return false; |
| 4511 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4512 | const CXXRecordDecl *RD = Definition->getParent(); |
| 4513 | if (RD->getNumVBases()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4514 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4515 | return false; |
| 4516 | } |
| 4517 | |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 4518 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4519 | Info, {This.getLValueBase(), |
| 4520 | {This.getLValueCallIndex(), This.getLValueVersion()}}); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4521 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4522 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4523 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
| 4524 | // wasteful. |
| 4525 | APValue RetVal; |
| 4526 | StmtResult Ret = {RetVal, nullptr}; |
| 4527 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4528 | // If it's a delegating constructor, delegate. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4529 | if (Definition->isDelegatingConstructor()) { |
| 4530 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | 9ff62af | 2013-11-07 18:45:03 +0000 | [diff] [blame] | 4531 | { |
| 4532 | FullExpressionRAII InitScope(Info); |
| 4533 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit())) |
| 4534 | return false; |
| 4535 | } |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4536 | return EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4539 | // 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] | 4540 | // essential for unions (or classes with anonymous union members), where the |
| 4541 | // operations performed by the constructor cannot be represented by |
| 4542 | // ctor-initializers. |
| 4543 | // |
| 4544 | // Skip this for empty non-union classes; we should not perform an |
| 4545 | // lvalue-to-rvalue conversion on them because their copy constructor does not |
| 4546 | // actually read them. |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 4547 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 4548 | (Definition->getParent()->isUnion() || |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 4549 | (Definition->isTrivial() && hasFields(Definition->getParent())))) { |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4550 | LValue RHS; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4551 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4552 | return handleLValueToRValueConversion( |
| 4553 | Info, E, Definition->getParamDecl(0)->getType().getNonReferenceType(), |
| 4554 | RHS, Result); |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | // Reserve space for the struct members. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4558 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4559 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 4560 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4561 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4562 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4563 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 4564 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4565 | // A scope for temporaries lifetime-extended by reference members. |
| 4566 | BlockScopeRAII LifetimeExtendedScope(Info); |
| 4567 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4568 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4569 | unsigned BasesSeen = 0; |
| 4570 | #ifndef NDEBUG |
| 4571 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 4572 | #endif |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4573 | for (const auto *I : Definition->inits()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4574 | LValue Subobject = This; |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4575 | LValue SubobjectParent = This; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4576 | APValue *Value = &Result; |
| 4577 | |
| 4578 | // Determine the subobject to initialize. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4579 | FieldDecl *FD = nullptr; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4580 | if (I->isBaseInitializer()) { |
| 4581 | QualType BaseType(I->getBaseClass(), 0); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4582 | #ifndef NDEBUG |
| 4583 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4584 | // definition. We have already checked for virtual base classes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4585 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 4586 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 4587 | "base class initializers not in expected order"); |
| 4588 | ++BaseIt; |
| 4589 | #endif |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4590 | if (!HandleLValueDirectBase(Info, I->getInit(), Subobject, RD, |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4591 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 4592 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4593 | Value = &Result.getStructBase(BasesSeen++); |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4594 | } else if ((FD = I->getMember())) { |
| 4595 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4596 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4597 | if (RD->isUnion()) { |
| 4598 | Result = APValue(FD); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4599 | Value = &Result.getUnionValue(); |
| 4600 | } else { |
| 4601 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 4602 | } |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4603 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4604 | // Walk the indirect field decl's chain to find the object to initialize, |
| 4605 | // and make sure we've initialized every step along it. |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4606 | auto IndirectFieldChain = IFD->chain(); |
| 4607 | for (auto *C : IndirectFieldChain) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 4608 | FD = cast<FieldDecl>(C); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4609 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 4610 | // Switch the union field if it differs. This happens if we had |
| 4611 | // preceding zero-initialization, and we're now initializing a union |
| 4612 | // subobject other than the first. |
| 4613 | // FIXME: In this case, the values of the other subobjects are |
| 4614 | // specified, since zero-initialization sets all padding bits to zero. |
| 4615 | if (Value->isUninit() || |
| 4616 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 4617 | if (CD->isUnion()) |
| 4618 | *Value = APValue(FD); |
| 4619 | else |
| 4620 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 4621 | std::distance(CD->field_begin(), CD->field_end())); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4622 | } |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4623 | // Store Subobject as its parent before updating it for the last element |
| 4624 | // in the chain. |
| 4625 | if (C == IndirectFieldChain.back()) |
| 4626 | SubobjectParent = Subobject; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4627 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4628 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4629 | if (CD->isUnion()) |
| 4630 | Value = &Value->getUnionValue(); |
| 4631 | else |
| 4632 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4633 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4634 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4635 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4636 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4637 | |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4638 | // Need to override This for implicit field initializers as in this case |
| 4639 | // This refers to innermost anonymous struct/union containing initializer, |
| 4640 | // not to currently constructed class. |
| 4641 | const Expr *Init = I->getInit(); |
| 4642 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent, |
| 4643 | isa<CXXDefaultInitExpr>(Init)); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4644 | FullExpressionRAII InitScope(Info); |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4645 | if (!EvaluateInPlace(*Value, Info, Subobject, Init) || |
| 4646 | (FD && FD->isBitField() && |
| 4647 | !truncateBitfieldValue(Info, Init, *Value, FD))) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4648 | // If we're checking for a potential constant expression, evaluate all |
| 4649 | // initializers even if some of them fail. |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 4650 | if (!Info.noteFailure()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4651 | return false; |
| 4652 | Success = false; |
| 4653 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4656 | return Success && |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4657 | EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4660 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
| 4661 | ArrayRef<const Expr*> Args, |
| 4662 | const CXXConstructorDecl *Definition, |
| 4663 | EvalInfo &Info, APValue &Result) { |
| 4664 | ArgVector ArgValues(Args.size()); |
| 4665 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 4666 | return false; |
| 4667 | |
| 4668 | return HandleConstructorCall(E, This, ArgValues.data(), Definition, |
| 4669 | Info, Result); |
| 4670 | } |
| 4671 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4672 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4673 | // Generic Evaluation |
| 4674 | //===----------------------------------------------------------------------===// |
| 4675 | namespace { |
| 4676 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4677 | template <class Derived> |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4678 | class ExprEvaluatorBase |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4679 | : public ConstStmtVisitor<Derived, bool> { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4680 | private: |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4681 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4682 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4683 | return getDerived().Success(V, E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4684 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4685 | bool DerivedZeroInitialization(const Expr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4686 | return getDerived().ZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4687 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4688 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4689 | // Check whether a conditional operator with a non-constant condition is a |
| 4690 | // potential constant expression. If neither arm is a potential constant |
| 4691 | // expression, then the conditional operator is not either. |
| 4692 | template<typename ConditionalOperator> |
| 4693 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4694 | assert(Info.checkingPotentialConstantExpression()); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4695 | |
| 4696 | // Speculatively evaluate both arms. |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 4697 | SmallVector<PartialDiagnosticAt, 8> Diag; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4698 | { |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4699 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4700 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 4701 | if (Diag.empty()) |
| 4702 | return; |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 4703 | } |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4704 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 4705 | { |
| 4706 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4707 | Diag.clear(); |
| 4708 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 4709 | if (Diag.empty()) |
| 4710 | return; |
| 4711 | } |
| 4712 | |
| 4713 | Error(E, diag::note_constexpr_conditional_never_const); |
| 4714 | } |
| 4715 | |
| 4716 | |
| 4717 | template<typename ConditionalOperator> |
| 4718 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 4719 | bool BoolResult; |
| 4720 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
Nick Lewycky | 20edee6 | 2017-04-27 07:11:09 +0000 | [diff] [blame] | 4721 | if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) { |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4722 | CheckPotentialConstantConditional(E); |
Nick Lewycky | 20edee6 | 2017-04-27 07:11:09 +0000 | [diff] [blame] | 4723 | return false; |
| 4724 | } |
| 4725 | if (Info.noteFailure()) { |
| 4726 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 4727 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 4728 | } |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4729 | return false; |
| 4730 | } |
| 4731 | |
| 4732 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 4733 | return StmtVisitorTy::Visit(EvalExpr); |
| 4734 | } |
| 4735 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4736 | protected: |
| 4737 | EvalInfo &Info; |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4738 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4739 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 4740 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 4741 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4742 | return Info.CCEDiag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4745 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4746 | |
| 4747 | public: |
| 4748 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 4749 | |
| 4750 | EvalInfo &getEvalInfo() { return Info; } |
| 4751 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4752 | /// Report an evaluation error. This should only be called when an error is |
| 4753 | /// first discovered. When propagating an error, just return false. |
| 4754 | bool Error(const Expr *E, diag::kind D) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4755 | Info.FFDiag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4756 | return false; |
| 4757 | } |
| 4758 | bool Error(const Expr *E) { |
| 4759 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 4760 | } |
| 4761 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4762 | bool VisitStmt(const Stmt *) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4763 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4764 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4765 | bool VisitExpr(const Expr *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4766 | return Error(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4767 | } |
| 4768 | |
Bill Wendling | 8003edc | 2018-11-09 00:41:36 +0000 | [diff] [blame] | 4769 | bool VisitConstantExpr(const ConstantExpr *E) |
| 4770 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4771 | bool VisitParenExpr(const ParenExpr *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4772 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4773 | bool VisitUnaryExtension(const UnaryOperator *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4774 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4775 | bool VisitUnaryPlus(const UnaryOperator *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4776 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4777 | bool VisitChooseExpr(const ChooseExpr *E) |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 4778 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4779 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4780 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4781 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4782 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4783 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
| 4784 | TempVersionRAII RAII(*Info.CurrentCall); |
| 4785 | return StmtVisitorTy::Visit(E->getExpr()); |
| 4786 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4787 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4788 | TempVersionRAII RAII(*Info.CurrentCall); |
Richard Smith | 17e3246 | 2013-09-13 20:51:45 +0000 | [diff] [blame] | 4789 | // The initializer may not have been parsed yet, or might be erroneous. |
| 4790 | if (!E->getExpr()) |
| 4791 | return Error(E); |
| 4792 | return StmtVisitorTy::Visit(E->getExpr()); |
| 4793 | } |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 4794 | // We cannot create any objects for which cleanups are required, so there is |
| 4795 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4796 | bool VisitExprWithCleanups(const ExprWithCleanups *E) |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 4797 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4798 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4799 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4800 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 4801 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 4802 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4803 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4804 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 4805 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 4806 | } |
| 4807 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4808 | bool VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4809 | switch (E->getOpcode()) { |
| 4810 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4811 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4812 | |
| 4813 | case BO_Comma: |
| 4814 | VisitIgnoredValue(E->getLHS()); |
| 4815 | return StmtVisitorTy::Visit(E->getRHS()); |
| 4816 | |
| 4817 | case BO_PtrMemD: |
| 4818 | case BO_PtrMemI: { |
| 4819 | LValue Obj; |
| 4820 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 4821 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4822 | APValue Result; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4823 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4824 | return false; |
| 4825 | return DerivedSuccess(Result, E); |
| 4826 | } |
| 4827 | } |
| 4828 | } |
| 4829 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4830 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 4831 | // Evaluate and cache the common expression. We treat it as a temporary, |
| 4832 | // even though it's not quite the same thing. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4833 | if (!Evaluate(Info.CurrentCall->createTemporary(E->getOpaqueValue(), false), |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 4834 | Info, E->getCommon())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4835 | return false; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4836 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4837 | return HandleConditionalOperator(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4838 | } |
| 4839 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4840 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4841 | bool IsBcpCall = false; |
| 4842 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 4843 | // the result is a constant expression if it can be folded without |
| 4844 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 4845 | // for discussion. |
| 4846 | if (const CallExpr *CallCE = |
| 4847 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 4848 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4849 | IsBcpCall = true; |
| 4850 | |
| 4851 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 4852 | // constant expression; we can't check whether it's potentially foldable. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4853 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4854 | return false; |
| 4855 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4856 | FoldConstant Fold(Info, IsBcpCall); |
| 4857 | if (!HandleConditionalOperator(E)) { |
| 4858 | Fold.keepDiagnostics(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4859 | return false; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4860 | } |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4861 | |
| 4862 | return true; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4865 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4866 | if (APValue *Value = Info.CurrentCall->getCurrentTemporary(E)) |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4867 | return DerivedSuccess(*Value, E); |
| 4868 | |
| 4869 | const Expr *Source = E->getSourceExpr(); |
| 4870 | if (!Source) |
| 4871 | return Error(E); |
| 4872 | if (Source == E) { // sanity checking. |
| 4873 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
| 4874 | return Error(E); |
Argyrios Kyrtzidis | fac35c0 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 4875 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4876 | return StmtVisitorTy::Visit(Source); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4877 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4878 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4879 | bool VisitCallExpr(const CallExpr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4880 | APValue Result; |
| 4881 | if (!handleCallExpr(E, Result, nullptr)) |
| 4882 | return false; |
| 4883 | return DerivedSuccess(Result, E); |
| 4884 | } |
| 4885 | |
| 4886 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4887 | const LValue *ResultSlot) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4888 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4889 | QualType CalleeType = Callee->getType(); |
| 4890 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4891 | const FunctionDecl *FD = nullptr; |
| 4892 | LValue *This = nullptr, ThisVal; |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 4893 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4894 | bool HasQualifier = false; |
Richard Smith | 656d49d | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 4895 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4896 | // Extract function decl and 'this' pointer from the callee. |
| 4897 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4898 | const ValueDecl *Member = nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4899 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 4900 | // Explicit bound member calls, such as x.f() or p->g(); |
| 4901 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4902 | return false; |
| 4903 | Member = ME->getMemberDecl(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4904 | This = &ThisVal; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4905 | HasQualifier = ME->hasQualifier(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4906 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 4907 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4908 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 4909 | if (!Member) return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4910 | This = &ThisVal; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4911 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4912 | return Error(Callee); |
| 4913 | |
| 4914 | FD = dyn_cast<FunctionDecl>(Member); |
| 4915 | if (!FD) |
| 4916 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4917 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4918 | LValue Call; |
| 4919 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4920 | return false; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4921 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4922 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4923 | return Error(Callee); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4924 | FD = dyn_cast_or_null<FunctionDecl>( |
| 4925 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4926 | if (!FD) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4927 | return Error(Callee); |
Faisal Vali | d92e749 | 2017-01-08 18:56:11 +0000 | [diff] [blame] | 4928 | // Don't call function pointers which have been cast to some other type. |
| 4929 | // Per DR (no number yet), the caller and callee can differ in noexcept. |
| 4930 | if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec( |
| 4931 | CalleeType->getPointeeType(), FD->getType())) { |
| 4932 | return Error(E); |
| 4933 | } |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4934 | |
| 4935 | // Overloaded operator calls to member functions are represented as normal |
| 4936 | // calls with '*this' as the first argument. |
| 4937 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 4938 | if (MD && !MD->isStatic()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4939 | // FIXME: When selecting an implicit conversion for an overloaded |
| 4940 | // operator delete, we sometimes try to evaluate calls to conversion |
| 4941 | // operators without a 'this' parameter! |
| 4942 | if (Args.empty()) |
| 4943 | return Error(E); |
| 4944 | |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4945 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4946 | return false; |
| 4947 | This = &ThisVal; |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4948 | Args = Args.slice(1); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4949 | } else if (MD && MD->isLambdaStaticInvoker()) { |
Faisal Vali | d92e749 | 2017-01-08 18:56:11 +0000 | [diff] [blame] | 4950 | // Map the static invoker for the lambda back to the call operator. |
| 4951 | // Conveniently, we don't have to slice out the 'this' argument (as is |
| 4952 | // being done for the non-static case), since a static member function |
| 4953 | // doesn't have an implicit argument passed in. |
| 4954 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
| 4955 | assert( |
| 4956 | ClosureClass->captures_begin() == ClosureClass->captures_end() && |
| 4957 | "Number of captures must be zero for conversion to function-ptr"); |
| 4958 | |
| 4959 | const CXXMethodDecl *LambdaCallOp = |
| 4960 | ClosureClass->getLambdaCallOperator(); |
| 4961 | |
| 4962 | // Set 'FD', the function that will be called below, to the call |
| 4963 | // operator. If the closure object represents a generic lambda, find |
| 4964 | // the corresponding specialization of the call operator. |
| 4965 | |
| 4966 | if (ClosureClass->isGenericLambda()) { |
| 4967 | assert(MD->isFunctionTemplateSpecialization() && |
| 4968 | "A generic lambda's static-invoker function must be a " |
| 4969 | "template specialization"); |
| 4970 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
| 4971 | FunctionTemplateDecl *CallOpTemplate = |
| 4972 | LambdaCallOp->getDescribedFunctionTemplate(); |
| 4973 | void *InsertPos = nullptr; |
| 4974 | FunctionDecl *CorrespondingCallOpSpecialization = |
| 4975 | CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); |
| 4976 | assert(CorrespondingCallOpSpecialization && |
| 4977 | "We must always have a function call operator specialization " |
| 4978 | "that corresponds to our static invoker specialization"); |
| 4979 | FD = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
| 4980 | } else |
| 4981 | FD = LambdaCallOp; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4982 | } |
| 4983 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4984 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4985 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4986 | return Error(E); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4987 | |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 4988 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 4989 | return false; |
| 4990 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4991 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 4992 | // calls to such functions in constant expressions. |
| 4993 | if (This && !HasQualifier && |
| 4994 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 4995 | return Error(E, diag::note_constexpr_virtual_call); |
| 4996 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4997 | const FunctionDecl *Definition = nullptr; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4998 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4999 | |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 5000 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body) || |
| 5001 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, Info, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5002 | Result, ResultSlot)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5003 | return false; |
| 5004 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5005 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 5006 | } |
| 5007 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5008 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5009 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 5010 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5011 | bool VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 90dc175 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 5012 | if (E->getNumInits() == 0) |
| 5013 | return DerivedZeroInitialization(E); |
| 5014 | if (E->getNumInits() == 1) |
| 5015 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5016 | return Error(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5017 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5018 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5019 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5020 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5021 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5022 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5023 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5024 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5025 | return DerivedZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5026 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5027 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5028 | /// 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] | 5029 | bool VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5030 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 5031 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5032 | APValue Val; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5033 | if (!Evaluate(Val, Info, E->getBase())) |
| 5034 | return false; |
| 5035 | |
| 5036 | QualType BaseTy = E->getBase()->getType(); |
| 5037 | |
| 5038 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5039 | if (!FD) return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5040 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5041 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5042 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 5043 | |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 5044 | CompleteObject Obj(&Val, BaseTy, true); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5045 | SubobjectDesignator Designator(BaseTy); |
| 5046 | Designator.addDeclUnchecked(FD); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5047 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5048 | APValue Result; |
| 5049 | return extractSubobject(Info, E, Obj, Designator, Result) && |
| 5050 | DerivedSuccess(Result, E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5051 | } |
| 5052 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5053 | bool VisitCastExpr(const CastExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5054 | switch (E->getCastKind()) { |
| 5055 | default: |
| 5056 | break; |
| 5057 | |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 5058 | case CK_AtomicToNonAtomic: { |
| 5059 | APValue AtomicVal; |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 5060 | // This does not need to be done in place even for class/array types: |
| 5061 | // atomic-to-non-atomic conversion implies copying the object |
| 5062 | // representation. |
| 5063 | if (!Evaluate(AtomicVal, Info, E->getSubExpr())) |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 5064 | return false; |
| 5065 | return DerivedSuccess(AtomicVal, E); |
| 5066 | } |
| 5067 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5068 | case CK_NoOp: |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 5069 | case CK_UserDefinedConversion: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5070 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 5071 | |
| 5072 | case CK_LValueToRValue: { |
| 5073 | LValue LVal; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5074 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 5075 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5076 | APValue RVal; |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 5077 | // 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] | 5078 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 5079 | LVal, RVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5080 | return false; |
| 5081 | return DerivedSuccess(RVal, E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5082 | } |
| 5083 | } |
| 5084 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5085 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5088 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5089 | return VisitUnaryPostIncDec(UO); |
| 5090 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5091 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5092 | return VisitUnaryPostIncDec(UO); |
| 5093 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5094 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5095 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5096 | return Error(UO); |
| 5097 | |
| 5098 | LValue LVal; |
| 5099 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
| 5100 | return false; |
| 5101 | APValue RVal; |
| 5102 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
| 5103 | UO->isIncrementOp(), &RVal)) |
| 5104 | return false; |
| 5105 | return DerivedSuccess(RVal, UO); |
| 5106 | } |
| 5107 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5108 | bool VisitStmtExpr(const StmtExpr *E) { |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5109 | // We will have checked the full-expressions inside the statement expression |
| 5110 | // when they were completed, and don't need to check them again now. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 5111 | if (Info.checkingForOverflow()) |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5112 | return Error(E); |
| 5113 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5114 | BlockScopeRAII Scope(Info); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5115 | const CompoundStmt *CS = E->getSubStmt(); |
Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 5116 | if (CS->body_empty()) |
| 5117 | return true; |
| 5118 | |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5119 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 5120 | BE = CS->body_end(); |
| 5121 | /**/; ++BI) { |
| 5122 | if (BI + 1 == BE) { |
| 5123 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
| 5124 | if (!FinalExpr) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5125 | Info.FFDiag((*BI)->getBeginLoc(), |
| 5126 | diag::note_constexpr_stmt_expr_unsupported); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5127 | return false; |
| 5128 | } |
| 5129 | return this->Visit(FinalExpr); |
| 5130 | } |
| 5131 | |
| 5132 | APValue ReturnValue; |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5133 | StmtResult Result = { ReturnValue, nullptr }; |
| 5134 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5135 | if (ESR != ESR_Succeeded) { |
| 5136 | // FIXME: If the statement-expression terminated due to 'return', |
| 5137 | // 'break', or 'continue', it would be nice to propagate that to |
| 5138 | // the outer statement evaluation rather than bailing out. |
| 5139 | if (ESR != ESR_Failed) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5140 | Info.FFDiag((*BI)->getBeginLoc(), |
| 5141 | diag::note_constexpr_stmt_expr_unsupported); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5142 | return false; |
| 5143 | } |
| 5144 | } |
Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 5145 | |
| 5146 | llvm_unreachable("Return from function from the loop above."); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5147 | } |
| 5148 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5149 | /// Visit a value which is evaluated, but whose value is ignored. |
| 5150 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 5151 | EvaluateIgnoredValue(Info, E); |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5152 | } |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 5153 | |
| 5154 | /// Potentially visit a MemberExpr's base expression. |
| 5155 | void VisitIgnoredBaseExpression(const Expr *E) { |
| 5156 | // While MSVC doesn't evaluate the base expression, it does diagnose the |
| 5157 | // presence of side-effecting behavior. |
| 5158 | if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Info.Ctx)) |
| 5159 | return; |
| 5160 | VisitIgnoredValue(E); |
| 5161 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5162 | }; |
| 5163 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 5164 | } // namespace |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5165 | |
| 5166 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5167 | // Common base class for lvalue and temporary evaluation. |
| 5168 | //===----------------------------------------------------------------------===// |
| 5169 | namespace { |
| 5170 | template<class Derived> |
| 5171 | class LValueExprEvaluatorBase |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5172 | : public ExprEvaluatorBase<Derived> { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5173 | protected: |
| 5174 | LValue &Result; |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5175 | bool InvalidBaseOK; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5176 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5177 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5178 | |
| 5179 | bool Success(APValue::LValueBase B) { |
| 5180 | Result.set(B); |
| 5181 | return true; |
| 5182 | } |
| 5183 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5184 | bool evaluatePointer(const Expr *E, LValue &Result) { |
| 5185 | return EvaluatePointer(E, Result, this->Info, InvalidBaseOK); |
| 5186 | } |
| 5187 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5188 | public: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5189 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) |
| 5190 | : ExprEvaluatorBaseTy(Info), Result(Result), |
| 5191 | InvalidBaseOK(InvalidBaseOK) {} |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5192 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5193 | bool Success(const APValue &V, const Expr *E) { |
| 5194 | Result.setFrom(this->Info.Ctx, V); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5195 | return true; |
| 5196 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5197 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5198 | bool VisitMemberExpr(const MemberExpr *E) { |
| 5199 | // Handle non-static data members. |
| 5200 | QualType BaseTy; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5201 | bool EvalOK; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5202 | if (E->isArrow()) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5203 | EvalOK = evaluatePointer(E->getBase(), Result); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5204 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5205 | } else if (E->getBase()->isRValue()) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 5206 | assert(E->getBase()->getType()->isRecordType()); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5207 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5208 | BaseTy = E->getBase()->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5209 | } else { |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5210 | EvalOK = this->Visit(E->getBase()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5211 | BaseTy = E->getBase()->getType(); |
| 5212 | } |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5213 | if (!EvalOK) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5214 | if (!InvalidBaseOK) |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5215 | return false; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 5216 | Result.setInvalid(E); |
| 5217 | return true; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5218 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5219 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5220 | const ValueDecl *MD = E->getMemberDecl(); |
| 5221 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 5222 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 5223 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 5224 | (void)BaseTy; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5225 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
| 5226 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5227 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5228 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
| 5229 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5230 | } else |
| 5231 | return this->Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5232 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5233 | if (MD->getType()->isReferenceType()) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5234 | APValue RefValue; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5235 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5236 | RefValue)) |
| 5237 | return false; |
| 5238 | return Success(RefValue, E); |
| 5239 | } |
| 5240 | return true; |
| 5241 | } |
| 5242 | |
| 5243 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 5244 | switch (E->getOpcode()) { |
| 5245 | default: |
| 5246 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 5247 | |
| 5248 | case BO_PtrMemD: |
| 5249 | case BO_PtrMemI: |
| 5250 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 5251 | } |
| 5252 | } |
| 5253 | |
| 5254 | bool VisitCastExpr(const CastExpr *E) { |
| 5255 | switch (E->getCastKind()) { |
| 5256 | default: |
| 5257 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5258 | |
| 5259 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5260 | case CK_UncheckedDerivedToBase: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5261 | if (!this->Visit(E->getSubExpr())) |
| 5262 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5263 | |
| 5264 | // Now figure out the necessary offset to add to the base LV to get from |
| 5265 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5266 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
| 5267 | Result); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5268 | } |
| 5269 | } |
| 5270 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5271 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5272 | |
| 5273 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5274 | // LValue Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5275 | // |
| 5276 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 5277 | // function designators (in C), decl references to void objects (in C), and |
| 5278 | // temporaries (if building with -Wno-address-of-temporary). |
| 5279 | // |
| 5280 | // LValue evaluation produces values comprising a base expression of one of the |
| 5281 | // following types: |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5282 | // - Declarations |
| 5283 | // * VarDecl |
| 5284 | // * FunctionDecl |
| 5285 | // - Literals |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 5286 | // * CompoundLiteralExpr in C (and in global scope in C++) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5287 | // * StringLiteral |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5288 | // * CXXTypeidExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5289 | // * PredefinedExpr |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5290 | // * ObjCStringLiteralExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5291 | // * ObjCEncodeExpr |
| 5292 | // * AddrLabelExpr |
| 5293 | // * BlockExpr |
| 5294 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5295 | // - Locals and temporaries |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5296 | // * MaterializeTemporaryExpr |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5297 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5298 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
| 5299 | // from the AST (FIXME). |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5300 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
| 5301 | // CallIndex, for a lifetime-extended temporary. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5302 | // plus an offset in bytes. |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5303 | //===----------------------------------------------------------------------===// |
| 5304 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5305 | class LValueExprEvaluator |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5306 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5307 | public: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5308 | LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) : |
| 5309 | LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5310 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5311 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5312 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5313 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5314 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 5315 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 5316 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5317 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 5318 | bool VisitMemberExpr(const MemberExpr *E); |
| 5319 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 5320 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5321 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 5322 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5323 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 5324 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 5325 | bool VisitUnaryReal(const UnaryOperator *E); |
| 5326 | bool VisitUnaryImag(const UnaryOperator *E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5327 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
| 5328 | return VisitUnaryPreIncDec(UO); |
| 5329 | } |
| 5330 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
| 5331 | return VisitUnaryPreIncDec(UO); |
| 5332 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5333 | bool VisitBinAssign(const BinaryOperator *BO); |
| 5334 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5335 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5336 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5337 | switch (E->getCastKind()) { |
| 5338 | default: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5339 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5340 | |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 5341 | case CK_LValueBitCast: |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5342 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5343 | if (!Visit(E->getSubExpr())) |
| 5344 | return false; |
| 5345 | Result.Designator.setInvalid(); |
| 5346 | return true; |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 5347 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5348 | case CK_BaseToDerived: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5349 | if (!Visit(E->getSubExpr())) |
| 5350 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5351 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5352 | } |
| 5353 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5354 | }; |
| 5355 | } // end anonymous namespace |
| 5356 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5357 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 5358 | /// expressions which are not glvalues, in three cases: |
Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 5359 | /// * function designators in C, and |
| 5360 | /// * "extern void" objects |
Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 5361 | /// * @selector() expressions in Objective-C |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5362 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
| 5363 | bool InvalidBaseOK) { |
Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 5364 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 5365 | E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E)); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5366 | return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5369 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 5370 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5371 | return Success(FD); |
| 5372 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5373 | return VisitVarDecl(E, VD); |
Richard Smith | dca60b4 | 2016-08-12 00:39:32 +0000 | [diff] [blame] | 5374 | if (const BindingDecl *BD = dyn_cast<BindingDecl>(E->getDecl())) |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 5375 | return Visit(BD->getBinding()); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5376 | return Error(E); |
| 5377 | } |
Richard Smith | 733237d | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 5378 | |
Faisal Vali | 0528a31 | 2016-11-13 06:09:16 +0000 | [diff] [blame] | 5379 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5380 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5381 | |
| 5382 | // If we are within a lambda's call operator, check whether the 'VD' referred |
| 5383 | // to within 'E' actually represents a lambda-capture that maps to a |
| 5384 | // data-member/field within the closure object, and if so, evaluate to the |
| 5385 | // field or what the field refers to. |
Erik Pilkington | 1123291 | 2018-04-05 00:12:05 +0000 | [diff] [blame] | 5386 | if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) && |
| 5387 | isa<DeclRefExpr>(E) && |
| 5388 | cast<DeclRefExpr>(E)->refersToEnclosingVariableOrCapture()) { |
| 5389 | // We don't always have a complete capture-map when checking or inferring if |
| 5390 | // the function call operator meets the requirements of a constexpr function |
| 5391 | // - but we don't need to evaluate the captures to determine constexprness |
| 5392 | // (dcl.constexpr C++17). |
| 5393 | if (Info.checkingPotentialConstantExpression()) |
| 5394 | return false; |
| 5395 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5396 | if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) { |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5397 | // Start with 'Result' referring to the complete closure object... |
| 5398 | Result = *Info.CurrentCall->This; |
| 5399 | // ... then update it to refer to the field of the closure object |
| 5400 | // that represents the capture. |
| 5401 | if (!HandleLValueMember(Info, E, Result, FD)) |
| 5402 | return false; |
| 5403 | // And if the field is of reference type, update 'Result' to refer to what |
| 5404 | // the field refers to. |
| 5405 | if (FD->getType()->isReferenceType()) { |
| 5406 | APValue RVal; |
| 5407 | if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, |
| 5408 | RVal)) |
| 5409 | return false; |
| 5410 | Result.setFrom(Info.Ctx, RVal); |
| 5411 | } |
| 5412 | return true; |
| 5413 | } |
| 5414 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5415 | CallStackFrame *Frame = nullptr; |
Faisal Vali | 0528a31 | 2016-11-13 06:09:16 +0000 | [diff] [blame] | 5416 | if (VD->hasLocalStorage() && Info.CurrentCall->Index > 1) { |
| 5417 | // Only if a local variable was declared in the function currently being |
| 5418 | // evaluated, do we expect to be able to find its value in the current |
| 5419 | // frame. (Otherwise it was likely declared in an enclosing context and |
| 5420 | // could either have a valid evaluatable value (for e.g. a constexpr |
| 5421 | // variable) or be ill-formed (and trigger an appropriate evaluation |
| 5422 | // diagnostic)). |
| 5423 | if (Info.CurrentCall->Callee && |
| 5424 | Info.CurrentCall->Callee->Equals(VD->getDeclContext())) { |
| 5425 | Frame = Info.CurrentCall; |
| 5426 | } |
| 5427 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5428 | |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 5429 | if (!VD->getType()->isReferenceType()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5430 | if (Frame) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5431 | Result.set({VD, Frame->Index, |
| 5432 | Info.CurrentCall->getCurrentTemporaryVersion(VD)}); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 5433 | return true; |
| 5434 | } |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5435 | return Success(VD); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 5436 | } |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 5437 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5438 | APValue *V; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5439 | if (!evaluateVarDeclInit(Info, E, VD, Frame, V, nullptr)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5440 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5441 | if (V->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 5442 | if (!Info.checkingPotentialConstantExpression()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5443 | Info.FFDiag(E, diag::note_constexpr_use_uninit_reference); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5444 | return false; |
| 5445 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5446 | return Success(*V, E); |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 5447 | } |
| 5448 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 5449 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 5450 | const MaterializeTemporaryExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5451 | // Walk through the expression to find the materialized temporary itself. |
| 5452 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5453 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5454 | const Expr *Inner = E->GetTemporaryExpr()-> |
| 5455 | skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5456 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5457 | // If we passed any comma operators, evaluate their LHSs. |
| 5458 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
| 5459 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
| 5460 | return false; |
| 5461 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5462 | // A materialized temporary with static storage duration can appear within the |
| 5463 | // result of a constant expression evaluation, so we need to preserve its |
| 5464 | // value for use outside this evaluation. |
| 5465 | APValue *Value; |
| 5466 | if (E->getStorageDuration() == SD_Static) { |
| 5467 | Value = Info.Ctx.getMaterializedTemporaryValue(E, true); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 5468 | *Value = APValue(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5469 | Result.set(E); |
| 5470 | } else { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5471 | Value = &createTemporary(E, E->getStorageDuration() == SD_Automatic, Result, |
| 5472 | *Info.CurrentCall); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 5475 | QualType Type = Inner->getType(); |
| 5476 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5477 | // Materialize the temporary itself. |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 5478 | if (!EvaluateInPlace(*Value, Info, Result, Inner) || |
| 5479 | (E->getStorageDuration() == SD_Static && |
| 5480 | !CheckConstantExpression(Info, E->getExprLoc(), Type, *Value))) { |
| 5481 | *Value = APValue(); |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5482 | return false; |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 5483 | } |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5484 | |
| 5485 | // Adjust our lvalue to refer to the desired subobject. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5486 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
| 5487 | --I; |
| 5488 | switch (Adjustments[I].Kind) { |
| 5489 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
| 5490 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
| 5491 | Type, Result)) |
| 5492 | return false; |
| 5493 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
| 5494 | break; |
| 5495 | |
| 5496 | case SubobjectAdjustment::FieldAdjustment: |
| 5497 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
| 5498 | return false; |
| 5499 | Type = Adjustments[I].Field->getType(); |
| 5500 | break; |
| 5501 | |
| 5502 | case SubobjectAdjustment::MemberPointerAdjustment: |
| 5503 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
| 5504 | Adjustments[I].Ptr.RHS)) |
| 5505 | return false; |
| 5506 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
| 5507 | break; |
| 5508 | } |
| 5509 | } |
| 5510 | |
| 5511 | return true; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 5512 | } |
| 5513 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5514 | bool |
| 5515 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 5516 | assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) && |
| 5517 | "lvalue compound literal in c++?"); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5518 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 5519 | // 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] | 5520 | return Success(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5523 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 5524 | if (!E->isPotentiallyEvaluated()) |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5525 | return Success(E); |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 5526 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5527 | Info.FFDiag(E, diag::note_constexpr_typeid_polymorphic) |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 5528 | << E->getExprOperand()->getType() |
| 5529 | << E->getExprOperand()->getSourceRange(); |
| 5530 | return false; |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 5533 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 5534 | return Success(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5535 | } |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 5536 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5537 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5538 | // Handle static data members. |
| 5539 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 5540 | VisitIgnoredBaseExpression(E->getBase()); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5541 | return VisitVarDecl(E, VD); |
| 5542 | } |
| 5543 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 5544 | // Handle static member functions. |
| 5545 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 5546 | if (MD->isStatic()) { |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 5547 | VisitIgnoredBaseExpression(E->getBase()); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5548 | return Success(MD); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 5549 | } |
| 5550 | } |
| 5551 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5552 | // Handle non-static data members. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5553 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5556 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5557 | // FIXME: Deal with vectors as array subscript bases. |
| 5558 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5559 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5560 | |
Nick Lewycky | ad88868 | 2017-04-27 07:27:36 +0000 | [diff] [blame] | 5561 | bool Success = true; |
| 5562 | if (!evaluatePointer(E->getBase(), Result)) { |
| 5563 | if (!Info.noteFailure()) |
| 5564 | return false; |
| 5565 | Success = false; |
| 5566 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5567 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5568 | APSInt Index; |
| 5569 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5570 | return false; |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5571 | |
Nick Lewycky | ad88868 | 2017-04-27 07:27:36 +0000 | [diff] [blame] | 5572 | return Success && |
| 5573 | HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5574 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5575 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5576 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5577 | return evaluatePointer(E->getSubExpr(), Result); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 5578 | } |
| 5579 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 5580 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 5581 | if (!Visit(E->getSubExpr())) |
| 5582 | return false; |
| 5583 | // __real is a no-op on scalar lvalues. |
| 5584 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 5585 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 5586 | return true; |
| 5587 | } |
| 5588 | |
| 5589 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 5590 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 5591 | "lvalue __imag__ on scalar?"); |
| 5592 | if (!Visit(E->getSubExpr())) |
| 5593 | return false; |
| 5594 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 5595 | return true; |
| 5596 | } |
| 5597 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5598 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5599 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5600 | return Error(UO); |
| 5601 | |
| 5602 | if (!this->Visit(UO->getSubExpr())) |
| 5603 | return false; |
| 5604 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5605 | return handleIncDec( |
| 5606 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5607 | UO->isIncrementOp(), nullptr); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
| 5611 | const CompoundAssignOperator *CAO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5612 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5613 | return Error(CAO); |
| 5614 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5615 | APValue RHS; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5616 | |
| 5617 | // The overall lvalue result is the result of evaluating the LHS. |
| 5618 | if (!this->Visit(CAO->getLHS())) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5619 | if (Info.noteFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5620 | Evaluate(RHS, this->Info, CAO->getRHS()); |
| 5621 | return false; |
| 5622 | } |
| 5623 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5624 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) |
| 5625 | return false; |
| 5626 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 5627 | return handleCompoundAssignment( |
| 5628 | this->Info, CAO, |
| 5629 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
| 5630 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5634 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5635 | return Error(E); |
| 5636 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5637 | APValue NewVal; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5638 | |
| 5639 | if (!this->Visit(E->getLHS())) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5640 | if (Info.noteFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5641 | Evaluate(NewVal, this->Info, E->getRHS()); |
| 5642 | return false; |
| 5643 | } |
| 5644 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5645 | if (!Evaluate(NewVal, this->Info, E->getRHS())) |
| 5646 | return false; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5647 | |
| 5648 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5649 | NewVal); |
| 5650 | } |
| 5651 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5652 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5653 | // Pointer Evaluation |
| 5654 | //===----------------------------------------------------------------------===// |
| 5655 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5656 | /// Attempts to compute the number of bytes available at the pointer |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5657 | /// returned by a function with the alloc_size attribute. Returns true if we |
| 5658 | /// were successful. Places an unsigned number into `Result`. |
| 5659 | /// |
| 5660 | /// This expects the given CallExpr to be a call to a function with an |
| 5661 | /// alloc_size attribute. |
| 5662 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
| 5663 | const CallExpr *Call, |
| 5664 | llvm::APInt &Result) { |
| 5665 | const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call); |
| 5666 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 5667 | assert(AllocSize && AllocSize->getElemSizeParam().isValid()); |
| 5668 | unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5669 | unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType()); |
| 5670 | if (Call->getNumArgs() <= SizeArgNo) |
| 5671 | return false; |
| 5672 | |
| 5673 | auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) { |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 5674 | Expr::EvalResult ExprResult; |
| 5675 | if (!E->EvaluateAsInt(ExprResult, Ctx, Expr::SE_AllowSideEffects)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5676 | return false; |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 5677 | Into = ExprResult.Val.getInt(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5678 | if (Into.isNegative() || !Into.isIntN(BitsInSizeT)) |
| 5679 | return false; |
| 5680 | Into = Into.zextOrSelf(BitsInSizeT); |
| 5681 | return true; |
| 5682 | }; |
| 5683 | |
| 5684 | APSInt SizeOfElem; |
| 5685 | if (!EvaluateAsSizeT(Call->getArg(SizeArgNo), SizeOfElem)) |
| 5686 | return false; |
| 5687 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 5688 | if (!AllocSize->getNumElemsParam().isValid()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5689 | Result = std::move(SizeOfElem); |
| 5690 | return true; |
| 5691 | } |
| 5692 | |
| 5693 | APSInt NumberOfElems; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 5694 | unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5695 | if (!EvaluateAsSizeT(Call->getArg(NumArgNo), NumberOfElems)) |
| 5696 | return false; |
| 5697 | |
| 5698 | bool Overflow; |
| 5699 | llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow); |
| 5700 | if (Overflow) |
| 5701 | return false; |
| 5702 | |
| 5703 | Result = std::move(BytesAvailable); |
| 5704 | return true; |
| 5705 | } |
| 5706 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5707 | /// 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] | 5708 | /// function. |
| 5709 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
| 5710 | const LValue &LVal, |
| 5711 | llvm::APInt &Result) { |
| 5712 | assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 5713 | "Can't get the size of a non alloc_size function"); |
| 5714 | const auto *Base = LVal.getLValueBase().get<const Expr *>(); |
| 5715 | const CallExpr *CE = tryUnwrapAllocSizeCall(Base); |
| 5716 | return getBytesReturnedByAllocSizeCall(Ctx, CE, Result); |
| 5717 | } |
| 5718 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5719 | /// 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] | 5720 | /// a function with the alloc_size attribute. If it was possible to do so, this |
| 5721 | /// function will return true, make Result's Base point to said function call, |
| 5722 | /// and mark Result's Base as invalid. |
| 5723 | static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base, |
| 5724 | LValue &Result) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5725 | if (Base.isNull()) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5726 | return false; |
| 5727 | |
| 5728 | // Because we do no form of static analysis, we only support const variables. |
| 5729 | // |
| 5730 | // Additionally, we can't support parameters, nor can we support static |
| 5731 | // variables (in the latter case, use-before-assign isn't UB; in the former, |
| 5732 | // we have no clue what they'll be assigned to). |
| 5733 | const auto *VD = |
| 5734 | dyn_cast_or_null<VarDecl>(Base.dyn_cast<const ValueDecl *>()); |
| 5735 | if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified()) |
| 5736 | return false; |
| 5737 | |
| 5738 | const Expr *Init = VD->getAnyInitializer(); |
| 5739 | if (!Init) |
| 5740 | return false; |
| 5741 | |
| 5742 | const Expr *E = Init->IgnoreParens(); |
| 5743 | if (!tryUnwrapAllocSizeCall(E)) |
| 5744 | return false; |
| 5745 | |
| 5746 | // Store E instead of E unwrapped so that the type of the LValue's base is |
| 5747 | // what the user wanted. |
| 5748 | Result.setInvalid(E); |
| 5749 | |
| 5750 | QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5751 | Result.addUnsizedArray(Info, E, Pointee); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5752 | return true; |
| 5753 | } |
| 5754 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 5755 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5756 | class PointerExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5757 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5758 | LValue &Result; |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5759 | bool InvalidBaseOK; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5760 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5761 | bool Success(const Expr *E) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5762 | Result.set(E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5763 | return true; |
| 5764 | } |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5765 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5766 | bool evaluateLValue(const Expr *E, LValue &Result) { |
| 5767 | return EvaluateLValue(E, Result, Info, InvalidBaseOK); |
| 5768 | } |
| 5769 | |
| 5770 | bool evaluatePointer(const Expr *E, LValue &Result) { |
| 5771 | return EvaluatePointer(E, Result, Info, InvalidBaseOK); |
| 5772 | } |
| 5773 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5774 | bool visitNonBuiltinCallExpr(const CallExpr *E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5775 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5776 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5777 | PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK) |
| 5778 | : ExprEvaluatorBaseTy(info), Result(Result), |
| 5779 | InvalidBaseOK(InvalidBaseOK) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5780 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5781 | bool Success(const APValue &V, const Expr *E) { |
| 5782 | Result.setFrom(Info.Ctx, V); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5783 | return true; |
| 5784 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5785 | bool ZeroInitialization(const Expr *E) { |
Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 5786 | auto TargetVal = Info.Ctx.getTargetNullPointerValue(E->getType()); |
| 5787 | Result.setNull(E->getType(), TargetVal); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 5788 | return true; |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5789 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5790 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5791 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5792 | bool VisitCastExpr(const CastExpr* E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5793 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5794 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5795 | { return Success(E); } |
Nick Lewycky | 19ae6dc | 2017-04-29 00:07:27 +0000 | [diff] [blame] | 5796 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
| 5797 | if (Info.noteFailure()) |
| 5798 | EvaluateIgnoredValue(Info, E->getSubExpr()); |
| 5799 | return Error(E); |
| 5800 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5801 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5802 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5803 | bool VisitCallExpr(const CallExpr *E); |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 5804 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5805 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 5806 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5807 | return Success(E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5808 | return Error(E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 5809 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5810 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5811 | // Can't look at 'this' when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 5812 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5813 | return false; |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 5814 | if (!Info.CurrentCall->This) { |
| 5815 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5816 | Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit(); |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 5817 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5818 | Info.FFDiag(E); |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 5819 | return false; |
| 5820 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5821 | Result = *Info.CurrentCall->This; |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5822 | // If we are inside a lambda's call operator, the 'this' expression refers |
| 5823 | // to the enclosing '*this' object (either by value or reference) which is |
| 5824 | // either copied into the closure object's field that represents the '*this' |
| 5825 | // or refers to '*this'. |
| 5826 | if (isLambdaCallOperator(Info.CurrentCall->Callee)) { |
| 5827 | // Update 'Result' to refer to the data member/field of the closure object |
| 5828 | // that represents the '*this' capture. |
| 5829 | if (!HandleLValueMember(Info, E, Result, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5830 | Info.CurrentCall->LambdaThisCaptureField)) |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5831 | return false; |
| 5832 | // If we captured '*this' by reference, replace the field with its referent. |
| 5833 | if (Info.CurrentCall->LambdaThisCaptureField->getType() |
| 5834 | ->isPointerType()) { |
| 5835 | APValue RVal; |
| 5836 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Result, |
| 5837 | RVal)) |
| 5838 | return false; |
| 5839 | |
| 5840 | Result.setFrom(Info.Ctx, RVal); |
| 5841 | } |
| 5842 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5843 | return true; |
| 5844 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5845 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 5846 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5847 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5848 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5849 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5850 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info, |
| 5851 | bool InvalidBaseOK) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5852 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5853 | return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5854 | } |
| 5855 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5856 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5857 | if (E->getOpcode() != BO_Add && |
| 5858 | E->getOpcode() != BO_Sub) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5859 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5860 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5861 | const Expr *PExp = E->getLHS(); |
| 5862 | const Expr *IExp = E->getRHS(); |
| 5863 | if (IExp->getType()->isPointerType()) |
| 5864 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5865 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5866 | bool EvalPtrOK = evaluatePointer(PExp, Result); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5867 | if (!EvalPtrOK && !Info.noteFailure()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5868 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5869 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5870 | llvm::APSInt Offset; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5871 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5872 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 5873 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5874 | if (E->getOpcode() == BO_Sub) |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 5875 | negateAsSigned(Offset); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5876 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5877 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 5878 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5879 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5880 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5881 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5882 | return evaluateLValue(E->getSubExpr(), Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5883 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5884 | |
Richard Smith | 81dfef9 | 2018-07-11 00:29:05 +0000 | [diff] [blame] | 5885 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 5886 | const Expr *SubExpr = E->getSubExpr(); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5887 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 5888 | switch (E->getCastKind()) { |
| 5889 | default: |
| 5890 | break; |
| 5891 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5892 | case CK_BitCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5893 | case CK_CPointerToObjCPointerCast: |
| 5894 | case CK_BlockPointerToObjCPointerCast: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5895 | case CK_AnyPointerToBlockPointerCast: |
Anastasia Stulova | 5d8ad8a | 2014-11-26 15:36:41 +0000 | [diff] [blame] | 5896 | case CK_AddressSpaceConversion: |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 5897 | if (!Visit(SubExpr)) |
| 5898 | return false; |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5899 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 5900 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 5901 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 5902 | if (!E->getType()->isVoidPointerType()) { |
James Y Knight | 49bf370 | 2018-10-05 21:53:51 +0000 | [diff] [blame] | 5903 | Result.Designator.setInvalid(); |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 5904 | if (SubExpr->getType()->isVoidPointerType()) |
| 5905 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 5906 | << 3 << SubExpr->getType(); |
| 5907 | else |
| 5908 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 5909 | } |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 5910 | if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr) |
| 5911 | ZeroInitialization(E); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5912 | return true; |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 5913 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5914 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5915 | case CK_UncheckedDerivedToBase: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5916 | if (!evaluatePointer(E->getSubExpr(), Result)) |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5917 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5918 | if (!Result.Base && Result.Offset.isZero()) |
| 5919 | return true; |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5920 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5921 | // 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] | 5922 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5923 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
| 5924 | castAs<PointerType>()->getPointeeType(), |
| 5925 | Result); |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5926 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5927 | case CK_BaseToDerived: |
| 5928 | if (!Visit(E->getSubExpr())) |
| 5929 | return false; |
| 5930 | if (!Result.Base && Result.Offset.isZero()) |
| 5931 | return true; |
| 5932 | return HandleBaseToDerivedCast(Info, E, Result); |
| 5933 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5934 | case CK_NullToPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 5935 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5936 | return ZeroInitialization(E); |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5937 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5938 | case CK_IntegralToPointer: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5939 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 5940 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5941 | APValue Value; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5942 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 5943 | break; |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 5944 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5945 | if (Value.isInt()) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5946 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 5947 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5948 | Result.Base = (Expr*)nullptr; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5949 | Result.InvalidBase = false; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5950 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5951 | Result.Designator.setInvalid(); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 5952 | Result.IsNullPtr = false; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5953 | return true; |
| 5954 | } else { |
| 5955 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5956 | Result.setFrom(Info.Ctx, Value); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5957 | return true; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5958 | } |
| 5959 | } |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5960 | |
| 5961 | case CK_ArrayToPointerDecay: { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5962 | if (SubExpr->isGLValue()) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5963 | if (!evaluateLValue(SubExpr, Result)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5964 | return false; |
| 5965 | } else { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5966 | APValue &Value = createTemporary(SubExpr, false, Result, |
| 5967 | *Info.CurrentCall); |
| 5968 | if (!EvaluateInPlace(Value, Info, Result, SubExpr)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5969 | return false; |
| 5970 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5971 | // The result is a pointer to the first element of the array. |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5972 | auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType()); |
| 5973 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5974 | Result.addArray(Info, E, CAT); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 5975 | else |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5976 | Result.addUnsizedArray(Info, E, AT->getElementType()); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5977 | return true; |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5978 | } |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 5979 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5980 | case CK_FunctionToPointerDecay: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5981 | return evaluateLValue(SubExpr, Result); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5982 | |
| 5983 | case CK_LValueToRValue: { |
| 5984 | LValue LVal; |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5985 | if (!evaluateLValue(E->getSubExpr(), LVal)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5986 | return false; |
| 5987 | |
| 5988 | APValue RVal; |
| 5989 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
| 5990 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
| 5991 | LVal, RVal)) |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5992 | return InvalidBaseOK && |
| 5993 | evaluateLValueAsAllocSize(Info, LVal.Base, Result); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5994 | return Success(RVal, E); |
| 5995 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5998 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5999 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6000 | |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 6001 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T, |
| 6002 | UnaryExprOrTypeTrait ExprKind) { |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6003 | // C++ [expr.alignof]p3: |
| 6004 | // When alignof is applied to a reference type, the result is the |
| 6005 | // alignment of the referenced type. |
| 6006 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 6007 | T = Ref->getPointeeType(); |
| 6008 | |
Roger Ferrer Ibanez | 3fa38a1 | 2017-03-08 14:00:44 +0000 | [diff] [blame] | 6009 | if (T.getQualifiers().hasUnaligned()) |
| 6010 | return CharUnits::One(); |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 6011 | |
| 6012 | const bool AlignOfReturnsPreferred = |
| 6013 | Info.Ctx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7; |
| 6014 | |
| 6015 | // __alignof is defined to return the preferred alignment. |
| 6016 | // Before 8, clang returned the preferred alignment for alignof and _Alignof |
| 6017 | // as well. |
| 6018 | if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) |
| 6019 | return Info.Ctx.toCharUnitsFromBits( |
| 6020 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
| 6021 | // alignof and _Alignof are defined to return the ABI alignment. |
| 6022 | else if (ExprKind == UETT_AlignOf) |
| 6023 | return Info.Ctx.getTypeAlignInChars(T.getTypePtr()); |
| 6024 | else |
| 6025 | llvm_unreachable("GetAlignOfType on a non-alignment ExprKind"); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6026 | } |
| 6027 | |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 6028 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E, |
| 6029 | UnaryExprOrTypeTrait ExprKind) { |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6030 | E = E->IgnoreParens(); |
| 6031 | |
| 6032 | // The kinds of expressions that we have special-case logic here for |
| 6033 | // should be kept up to date with the special checks for those |
| 6034 | // expressions in Sema. |
| 6035 | |
| 6036 | // alignof decl is always accepted, even if it doesn't make sense: we default |
| 6037 | // to 1 in those cases. |
| 6038 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 6039 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 6040 | /*RefAsPointee*/true); |
| 6041 | |
| 6042 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 6043 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 6044 | /*RefAsPointee*/true); |
| 6045 | |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 6046 | return GetAlignOfType(Info, E->getType(), ExprKind); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6049 | // To be clear: this happily visits unsupported builtins. Better name welcomed. |
| 6050 | bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) { |
| 6051 | if (ExprEvaluatorBaseTy::VisitCallExpr(E)) |
| 6052 | return true; |
| 6053 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 6054 | if (!(InvalidBaseOK && getAllocSizeAttr(E))) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6055 | return false; |
| 6056 | |
| 6057 | Result.setInvalid(E); |
| 6058 | QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 6059 | Result.addUnsizedArray(Info, E, PointeeTy); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6060 | return true; |
| 6061 | } |
| 6062 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 6063 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6064 | if (IsStringLiteralCall(E)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 6065 | return Success(E); |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 6066 | |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 6067 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
| 6068 | return VisitBuiltinCallExpr(E, BuiltinOp); |
| 6069 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6070 | return visitNonBuiltinCallExpr(E); |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 6071 | } |
| 6072 | |
| 6073 | bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
| 6074 | unsigned BuiltinOp) { |
| 6075 | switch (BuiltinOp) { |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 6076 | case Builtin::BI__builtin_addressof: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 6077 | return evaluateLValue(E->getArg(0), Result); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6078 | case Builtin::BI__builtin_assume_aligned: { |
| 6079 | // We need to be very careful here because: if the pointer does not have the |
| 6080 | // asserted alignment, then the behavior is undefined, and undefined |
| 6081 | // behavior is non-constant. |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 6082 | if (!evaluatePointer(E->getArg(0), Result)) |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6083 | return false; |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 6084 | |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6085 | LValue OffsetResult(Result); |
| 6086 | APSInt Alignment; |
| 6087 | if (!EvaluateInteger(E->getArg(1), Alignment, Info)) |
| 6088 | return false; |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 6089 | CharUnits Align = CharUnits::fromQuantity(Alignment.getZExtValue()); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6090 | |
| 6091 | if (E->getNumArgs() > 2) { |
| 6092 | APSInt Offset; |
| 6093 | if (!EvaluateInteger(E->getArg(2), Offset, Info)) |
| 6094 | return false; |
| 6095 | |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 6096 | int64_t AdditionalOffset = -Offset.getZExtValue(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6097 | OffsetResult.Offset += CharUnits::fromQuantity(AdditionalOffset); |
| 6098 | } |
| 6099 | |
| 6100 | // If there is a base object, then it must have the correct alignment. |
| 6101 | if (OffsetResult.Base) { |
| 6102 | CharUnits BaseAlignment; |
| 6103 | if (const ValueDecl *VD = |
| 6104 | OffsetResult.Base.dyn_cast<const ValueDecl*>()) { |
| 6105 | BaseAlignment = Info.Ctx.getDeclAlign(VD); |
| 6106 | } else { |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 6107 | BaseAlignment = GetAlignOfExpr( |
| 6108 | Info, OffsetResult.Base.get<const Expr *>(), UETT_AlignOf); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
| 6111 | if (BaseAlignment < Align) { |
| 6112 | Result.Designator.setInvalid(); |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 6113 | // FIXME: Add support to Diagnostic for long / long long. |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6114 | CCEDiag(E->getArg(0), |
| 6115 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 6116 | << (unsigned)BaseAlignment.getQuantity() |
| 6117 | << (unsigned)Align.getQuantity(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6118 | return false; |
| 6119 | } |
| 6120 | } |
| 6121 | |
| 6122 | // The offset must also have the correct alignment. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6123 | if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) { |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6124 | Result.Designator.setInvalid(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6125 | |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 6126 | (OffsetResult.Base |
| 6127 | ? CCEDiag(E->getArg(0), |
| 6128 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
| 6129 | : CCEDiag(E->getArg(0), |
| 6130 | diag::note_constexpr_baa_value_insufficient_alignment)) |
| 6131 | << (int)OffsetResult.Offset.getQuantity() |
| 6132 | << (unsigned)Align.getQuantity(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6133 | return false; |
| 6134 | } |
| 6135 | |
| 6136 | return true; |
| 6137 | } |
Eric Fiselier | 2618750 | 2018-12-14 21:11:28 +0000 | [diff] [blame] | 6138 | case Builtin::BI__builtin_launder: |
| 6139 | return evaluatePointer(E->getArg(0), Result); |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6140 | case Builtin::BIstrchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6141 | case Builtin::BIwcschr: |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6142 | case Builtin::BImemchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6143 | case Builtin::BIwmemchr: |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6144 | if (Info.getLangOpts().CPlusPlus11) |
| 6145 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| 6146 | << /*isConstexpr*/0 << /*isConstructor*/0 |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6147 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6148 | else |
| 6149 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 6150 | LLVM_FALLTHROUGH; |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6151 | case Builtin::BI__builtin_strchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6152 | case Builtin::BI__builtin_wcschr: |
| 6153 | case Builtin::BI__builtin_memchr: |
Richard Smith | 5e29dd3 | 2017-01-20 00:45:35 +0000 | [diff] [blame] | 6154 | case Builtin::BI__builtin_char_memchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6155 | case Builtin::BI__builtin_wmemchr: { |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6156 | if (!Visit(E->getArg(0))) |
| 6157 | return false; |
| 6158 | APSInt Desired; |
| 6159 | if (!EvaluateInteger(E->getArg(1), Desired, Info)) |
| 6160 | return false; |
| 6161 | uint64_t MaxLength = uint64_t(-1); |
| 6162 | if (BuiltinOp != Builtin::BIstrchr && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6163 | BuiltinOp != Builtin::BIwcschr && |
| 6164 | BuiltinOp != Builtin::BI__builtin_strchr && |
| 6165 | BuiltinOp != Builtin::BI__builtin_wcschr) { |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6166 | APSInt N; |
| 6167 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
| 6168 | return false; |
| 6169 | MaxLength = N.getExtValue(); |
| 6170 | } |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 6171 | // We cannot find the value if there are no candidates to match against. |
| 6172 | if (MaxLength == 0u) |
| 6173 | return ZeroInitialization(E); |
| 6174 | if (!Result.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
| 6175 | Result.Designator.Invalid) |
| 6176 | return false; |
| 6177 | QualType CharTy = Result.Designator.getType(Info.Ctx); |
| 6178 | bool IsRawByte = BuiltinOp == Builtin::BImemchr || |
| 6179 | BuiltinOp == Builtin::BI__builtin_memchr; |
| 6180 | assert(IsRawByte || |
| 6181 | Info.Ctx.hasSameUnqualifiedType( |
| 6182 | CharTy, E->getArg(0)->getType()->getPointeeType())); |
| 6183 | // Pointers to const void may point to objects of incomplete type. |
| 6184 | if (IsRawByte && CharTy->isIncompleteType()) { |
| 6185 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy; |
| 6186 | return false; |
| 6187 | } |
| 6188 | // Give up on byte-oriented matching against multibyte elements. |
| 6189 | // FIXME: We can compare the bytes in the correct order. |
| 6190 | if (IsRawByte && Info.Ctx.getTypeSizeInChars(CharTy) != CharUnits::One()) |
| 6191 | return false; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6192 | // Figure out what value we're actually looking for (after converting to |
| 6193 | // the corresponding unsigned type if necessary). |
| 6194 | uint64_t DesiredVal; |
| 6195 | bool StopAtNull = false; |
| 6196 | switch (BuiltinOp) { |
| 6197 | case Builtin::BIstrchr: |
| 6198 | case Builtin::BI__builtin_strchr: |
| 6199 | // strchr compares directly to the passed integer, and therefore |
| 6200 | // always fails if given an int that is not a char. |
| 6201 | if (!APSInt::isSameValue(HandleIntToIntCast(Info, E, CharTy, |
| 6202 | E->getArg(1)->getType(), |
| 6203 | Desired), |
| 6204 | Desired)) |
| 6205 | return ZeroInitialization(E); |
| 6206 | StopAtNull = true; |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 6207 | LLVM_FALLTHROUGH; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6208 | case Builtin::BImemchr: |
| 6209 | case Builtin::BI__builtin_memchr: |
Richard Smith | 5e29dd3 | 2017-01-20 00:45:35 +0000 | [diff] [blame] | 6210 | case Builtin::BI__builtin_char_memchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6211 | // memchr compares by converting both sides to unsigned char. That's also |
| 6212 | // correct for strchr if we get this far (to cope with plain char being |
| 6213 | // unsigned in the strchr case). |
| 6214 | DesiredVal = Desired.trunc(Info.Ctx.getCharWidth()).getZExtValue(); |
| 6215 | break; |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6216 | |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6217 | case Builtin::BIwcschr: |
| 6218 | case Builtin::BI__builtin_wcschr: |
| 6219 | StopAtNull = true; |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 6220 | LLVM_FALLTHROUGH; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6221 | case Builtin::BIwmemchr: |
| 6222 | case Builtin::BI__builtin_wmemchr: |
| 6223 | // wcschr and wmemchr are given a wchar_t to look for. Just use it. |
| 6224 | DesiredVal = Desired.getZExtValue(); |
| 6225 | break; |
| 6226 | } |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6227 | |
| 6228 | for (; MaxLength; --MaxLength) { |
| 6229 | APValue Char; |
| 6230 | if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) || |
| 6231 | !Char.isInt()) |
| 6232 | return false; |
| 6233 | if (Char.getInt().getZExtValue() == DesiredVal) |
| 6234 | return true; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6235 | if (StopAtNull && !Char.getInt()) |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6236 | break; |
| 6237 | if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1)) |
| 6238 | return false; |
| 6239 | } |
| 6240 | // Not found: return nullptr. |
| 6241 | return ZeroInitialization(E); |
| 6242 | } |
| 6243 | |
Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 6244 | case Builtin::BImemcpy: |
| 6245 | case Builtin::BImemmove: |
| 6246 | case Builtin::BIwmemcpy: |
| 6247 | case Builtin::BIwmemmove: |
| 6248 | if (Info.getLangOpts().CPlusPlus11) |
| 6249 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| 6250 | << /*isConstexpr*/0 << /*isConstructor*/0 |
| 6251 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
| 6252 | else |
| 6253 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| 6254 | LLVM_FALLTHROUGH; |
| 6255 | case Builtin::BI__builtin_memcpy: |
| 6256 | case Builtin::BI__builtin_memmove: |
| 6257 | case Builtin::BI__builtin_wmemcpy: |
| 6258 | case Builtin::BI__builtin_wmemmove: { |
| 6259 | bool WChar = BuiltinOp == Builtin::BIwmemcpy || |
| 6260 | BuiltinOp == Builtin::BIwmemmove || |
| 6261 | BuiltinOp == Builtin::BI__builtin_wmemcpy || |
| 6262 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
| 6263 | bool Move = BuiltinOp == Builtin::BImemmove || |
| 6264 | BuiltinOp == Builtin::BIwmemmove || |
| 6265 | BuiltinOp == Builtin::BI__builtin_memmove || |
| 6266 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
| 6267 | |
| 6268 | // The result of mem* is the first argument. |
Richard Smith | 128719c | 2018-09-13 22:47:33 +0000 | [diff] [blame] | 6269 | if (!Visit(E->getArg(0))) |
Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 6270 | return false; |
| 6271 | LValue Dest = Result; |
| 6272 | |
| 6273 | LValue Src; |
Richard Smith | 128719c | 2018-09-13 22:47:33 +0000 | [diff] [blame] | 6274 | if (!EvaluatePointer(E->getArg(1), Src, Info)) |
Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 6275 | return false; |
| 6276 | |
| 6277 | APSInt N; |
| 6278 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
| 6279 | return false; |
| 6280 | assert(!N.isSigned() && "memcpy and friends take an unsigned size"); |
| 6281 | |
| 6282 | // If the size is zero, we treat this as always being a valid no-op. |
| 6283 | // (Even if one of the src and dest pointers is null.) |
| 6284 | if (!N) |
| 6285 | return true; |
| 6286 | |
Richard Smith | 128719c | 2018-09-13 22:47:33 +0000 | [diff] [blame] | 6287 | // Otherwise, if either of the operands is null, we can't proceed. Don't |
| 6288 | // try to determine the type of the copied objects, because there aren't |
| 6289 | // any. |
| 6290 | if (!Src.Base || !Dest.Base) { |
| 6291 | APValue Val; |
| 6292 | (!Src.Base ? Src : Dest).moveInto(Val); |
| 6293 | Info.FFDiag(E, diag::note_constexpr_memcpy_null) |
| 6294 | << Move << WChar << !!Src.Base |
| 6295 | << Val.getAsString(Info.Ctx, E->getArg(0)->getType()); |
| 6296 | return false; |
| 6297 | } |
| 6298 | if (Src.Designator.Invalid || Dest.Designator.Invalid) |
| 6299 | return false; |
| 6300 | |
Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 6301 | // We require that Src and Dest are both pointers to arrays of |
| 6302 | // trivially-copyable type. (For the wide version, the designator will be |
| 6303 | // invalid if the designated object is not a wchar_t.) |
| 6304 | QualType T = Dest.Designator.getType(Info.Ctx); |
| 6305 | QualType SrcT = Src.Designator.getType(Info.Ctx); |
| 6306 | if (!Info.Ctx.hasSameUnqualifiedType(T, SrcT)) { |
| 6307 | Info.FFDiag(E, diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T; |
| 6308 | return false; |
| 6309 | } |
Petr Pavlu | ed083f2 | 2018-10-04 09:25:44 +0000 | [diff] [blame] | 6310 | if (T->isIncompleteType()) { |
| 6311 | Info.FFDiag(E, diag::note_constexpr_memcpy_incomplete_type) << Move << T; |
| 6312 | return false; |
| 6313 | } |
Richard Smith | 06f71b5 | 2018-08-04 00:57:17 +0000 | [diff] [blame] | 6314 | if (!T.isTriviallyCopyableType(Info.Ctx)) { |
| 6315 | Info.FFDiag(E, diag::note_constexpr_memcpy_nontrivial) << Move << T; |
| 6316 | return false; |
| 6317 | } |
| 6318 | |
| 6319 | // Figure out how many T's we're copying. |
| 6320 | uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity(); |
| 6321 | if (!WChar) { |
| 6322 | uint64_t Remainder; |
| 6323 | llvm::APInt OrigN = N; |
| 6324 | llvm::APInt::udivrem(OrigN, TSize, N, Remainder); |
| 6325 | if (Remainder) { |
| 6326 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
| 6327 | << Move << WChar << 0 << T << OrigN.toString(10, /*Signed*/false) |
| 6328 | << (unsigned)TSize; |
| 6329 | return false; |
| 6330 | } |
| 6331 | } |
| 6332 | |
| 6333 | // Check that the copying will remain within the arrays, just so that we |
| 6334 | // can give a more meaningful diagnostic. This implicitly also checks that |
| 6335 | // N fits into 64 bits. |
| 6336 | uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second; |
| 6337 | uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second; |
| 6338 | if (N.ugt(RemainingSrcSize) || N.ugt(RemainingDestSize)) { |
| 6339 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
| 6340 | << Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T |
| 6341 | << N.toString(10, /*Signed*/false); |
| 6342 | return false; |
| 6343 | } |
| 6344 | uint64_t NElems = N.getZExtValue(); |
| 6345 | uint64_t NBytes = NElems * TSize; |
| 6346 | |
| 6347 | // Check for overlap. |
| 6348 | int Direction = 1; |
| 6349 | if (HasSameBase(Src, Dest)) { |
| 6350 | uint64_t SrcOffset = Src.getLValueOffset().getQuantity(); |
| 6351 | uint64_t DestOffset = Dest.getLValueOffset().getQuantity(); |
| 6352 | if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) { |
| 6353 | // Dest is inside the source region. |
| 6354 | if (!Move) { |
| 6355 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
| 6356 | return false; |
| 6357 | } |
| 6358 | // For memmove and friends, copy backwards. |
| 6359 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, NElems - 1) || |
| 6360 | !HandleLValueArrayAdjustment(Info, E, Dest, T, NElems - 1)) |
| 6361 | return false; |
| 6362 | Direction = -1; |
| 6363 | } else if (!Move && SrcOffset >= DestOffset && |
| 6364 | SrcOffset - DestOffset < NBytes) { |
| 6365 | // Src is inside the destination region for memcpy: invalid. |
| 6366 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
| 6367 | return false; |
| 6368 | } |
| 6369 | } |
| 6370 | |
| 6371 | while (true) { |
| 6372 | APValue Val; |
| 6373 | if (!handleLValueToRValueConversion(Info, E, T, Src, Val) || |
| 6374 | !handleAssignment(Info, E, Dest, T, Val)) |
| 6375 | return false; |
| 6376 | // Do not iterate past the last element; if we're copying backwards, that |
| 6377 | // might take us off the start of the array. |
| 6378 | if (--NElems == 0) |
| 6379 | return true; |
| 6380 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, Direction) || |
| 6381 | !HandleLValueArrayAdjustment(Info, E, Dest, T, Direction)) |
| 6382 | return false; |
| 6383 | } |
| 6384 | } |
| 6385 | |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 6386 | default: |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6387 | return visitNonBuiltinCallExpr(E); |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 6388 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6389 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6390 | |
| 6391 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6392 | // Member Pointer Evaluation |
| 6393 | //===----------------------------------------------------------------------===// |
| 6394 | |
| 6395 | namespace { |
| 6396 | class MemberPointerExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6397 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6398 | MemberPtr &Result; |
| 6399 | |
| 6400 | bool Success(const ValueDecl *D) { |
| 6401 | Result = MemberPtr(D); |
| 6402 | return true; |
| 6403 | } |
| 6404 | public: |
| 6405 | |
| 6406 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 6407 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 6408 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6409 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6410 | Result.setFrom(V); |
| 6411 | return true; |
| 6412 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6413 | bool ZeroInitialization(const Expr *E) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6414 | return Success((const ValueDecl*)nullptr); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
| 6417 | bool VisitCastExpr(const CastExpr *E); |
| 6418 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 6419 | }; |
| 6420 | } // end anonymous namespace |
| 6421 | |
| 6422 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 6423 | EvalInfo &Info) { |
| 6424 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 6425 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 6426 | } |
| 6427 | |
| 6428 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 6429 | switch (E->getCastKind()) { |
| 6430 | default: |
| 6431 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6432 | |
| 6433 | case CK_NullToMemberPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 6434 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6435 | return ZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6436 | |
| 6437 | case CK_BaseToDerivedMemberPointer: { |
| 6438 | if (!Visit(E->getSubExpr())) |
| 6439 | return false; |
| 6440 | if (E->path_empty()) |
| 6441 | return true; |
| 6442 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 6443 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 6444 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 6445 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 6446 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 6447 | PathI != PathE; ++PathI) { |
| 6448 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 6449 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 6450 | if (!Result.castToDerived(Derived)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6451 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6452 | } |
| 6453 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 6454 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6455 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6456 | return true; |
| 6457 | } |
| 6458 | |
| 6459 | case CK_DerivedToBaseMemberPointer: |
| 6460 | if (!Visit(E->getSubExpr())) |
| 6461 | return false; |
| 6462 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 6463 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 6464 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 6465 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 6466 | if (!Result.castToBase(Base)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6467 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6468 | } |
| 6469 | return true; |
| 6470 | } |
| 6471 | } |
| 6472 | |
| 6473 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 6474 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 6475 | // member can be formed. |
| 6476 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 6477 | } |
| 6478 | |
| 6479 | //===----------------------------------------------------------------------===// |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6480 | // Record Evaluation |
| 6481 | //===----------------------------------------------------------------------===// |
| 6482 | |
| 6483 | namespace { |
| 6484 | class RecordExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6485 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6486 | const LValue &This; |
| 6487 | APValue &Result; |
| 6488 | public: |
| 6489 | |
| 6490 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 6491 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 6492 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6493 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6494 | Result = V; |
| 6495 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6496 | } |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6497 | bool ZeroInitialization(const Expr *E) { |
| 6498 | return ZeroInitialization(E, E->getType()); |
| 6499 | } |
| 6500 | bool ZeroInitialization(const Expr *E, QualType T); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6501 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6502 | bool VisitCallExpr(const CallExpr *E) { |
| 6503 | return handleCallExpr(E, Result, &This); |
| 6504 | } |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6505 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6506 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6507 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 6508 | return VisitCXXConstructExpr(E, E->getType()); |
| 6509 | } |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6510 | bool VisitLambdaExpr(const LambdaExpr *E); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6511 | bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E); |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6512 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6513 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 6514 | |
| 6515 | bool VisitBinCmp(const BinaryOperator *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6516 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6517 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6518 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6519 | /// Perform zero-initialization on an object of non-union class type. |
| 6520 | /// C++11 [dcl.init]p5: |
| 6521 | /// To zero-initialize an object or reference of type T means: |
| 6522 | /// [...] |
| 6523 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 6524 | /// each non-static data member and each base-class subobject is |
| 6525 | /// zero-initialized |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6526 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 6527 | const RecordDecl *RD, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6528 | const LValue &This, APValue &Result) { |
| 6529 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 6530 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 6531 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 6532 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6533 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6534 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6535 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 6536 | |
| 6537 | if (CD) { |
| 6538 | unsigned Index = 0; |
| 6539 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6540 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6541 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 6542 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6543 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
| 6544 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6545 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6546 | Result.getStructBase(Index))) |
| 6547 | return false; |
| 6548 | } |
| 6549 | } |
| 6550 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6551 | for (const auto *I : RD->fields()) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6552 | // -- if T is a reference type, no initialization is performed. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6553 | if (I->getType()->isReferenceType()) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6554 | continue; |
| 6555 | |
| 6556 | LValue Subobject = This; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6557 | if (!HandleLValueMember(Info, E, Subobject, I, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6558 | return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6559 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6560 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6561 | if (!EvaluateInPlace( |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6562 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6563 | return false; |
| 6564 | } |
| 6565 | |
| 6566 | return true; |
| 6567 | } |
| 6568 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6569 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) { |
| 6570 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 6571 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6572 | if (RD->isUnion()) { |
| 6573 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 6574 | // object's first non-static named data member is zero-initialized |
| 6575 | RecordDecl::field_iterator I = RD->field_begin(); |
| 6576 | if (I == RD->field_end()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6577 | Result = APValue((const FieldDecl*)nullptr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6578 | return true; |
| 6579 | } |
| 6580 | |
| 6581 | LValue Subobject = This; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 6582 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6583 | return false; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 6584 | Result = APValue(*I); |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6585 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6586 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6587 | } |
| 6588 | |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 6589 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 6590 | Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 6591 | return false; |
| 6592 | } |
| 6593 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6594 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6595 | } |
| 6596 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6597 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 6598 | switch (E->getCastKind()) { |
| 6599 | default: |
| 6600 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6601 | |
| 6602 | case CK_ConstructorConversion: |
| 6603 | return Visit(E->getSubExpr()); |
| 6604 | |
| 6605 | case CK_DerivedToBase: |
| 6606 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6607 | APValue DerivedObject; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6608 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6609 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6610 | if (!DerivedObject.isStruct()) |
| 6611 | return Error(E->getSubExpr()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6612 | |
| 6613 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 6614 | APValue *Value = &DerivedObject; |
| 6615 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 6616 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 6617 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 6618 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 6619 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 6620 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 6621 | RD = Base; |
| 6622 | } |
| 6623 | Result = *Value; |
| 6624 | return true; |
| 6625 | } |
| 6626 | } |
| 6627 | } |
| 6628 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6629 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 6630 | if (E->isTransparent()) |
| 6631 | return Visit(E->getInit(0)); |
| 6632 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6633 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 6634 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6635 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 6636 | |
| 6637 | if (RD->isUnion()) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6638 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 6639 | Result = APValue(Field); |
| 6640 | if (!Field) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6641 | return true; |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6642 | |
| 6643 | // If the initializer list for a union does not contain any elements, the |
| 6644 | // first element of the union is value-initialized. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6645 | // FIXME: The element should be initialized from an initializer list. |
| 6646 | // Is this difference ever observable for initializer lists which |
| 6647 | // we don't build? |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6648 | ImplicitValueInitExpr VIE(Field->getType()); |
| 6649 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 6650 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6651 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6652 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
| 6653 | return false; |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6654 | |
| 6655 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 6656 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 6657 | isa<CXXDefaultInitExpr>(InitExpr)); |
| 6658 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6659 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6660 | } |
| 6661 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6662 | auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
Richard Smith | c0d04a2 | 2016-05-25 22:06:25 +0000 | [diff] [blame] | 6663 | if (Result.isUninit()) |
| 6664 | Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0, |
| 6665 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6666 | unsigned ElementNo = 0; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6667 | bool Success = true; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6668 | |
| 6669 | // Initialize base classes. |
| 6670 | if (CXXRD) { |
| 6671 | for (const auto &Base : CXXRD->bases()) { |
| 6672 | assert(ElementNo < E->getNumInits() && "missing init for base class"); |
| 6673 | const Expr *Init = E->getInit(ElementNo); |
| 6674 | |
| 6675 | LValue Subobject = This; |
| 6676 | if (!HandleLValueBase(Info, Init, Subobject, CXXRD, &Base)) |
| 6677 | return false; |
| 6678 | |
| 6679 | APValue &FieldVal = Result.getStructBase(ElementNo); |
| 6680 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init)) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 6681 | if (!Info.noteFailure()) |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6682 | return false; |
| 6683 | Success = false; |
| 6684 | } |
| 6685 | ++ElementNo; |
| 6686 | } |
| 6687 | } |
| 6688 | |
| 6689 | // Initialize members. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6690 | for (const auto *Field : RD->fields()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6691 | // Anonymous bit-fields are not considered members of the class for |
| 6692 | // purposes of aggregate initialization. |
| 6693 | if (Field->isUnnamedBitfield()) |
| 6694 | continue; |
| 6695 | |
| 6696 | LValue Subobject = This; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6697 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6698 | bool HaveInit = ElementNo < E->getNumInits(); |
| 6699 | |
| 6700 | // FIXME: Diagnostics here should point to the end of the initializer |
| 6701 | // list, not the start. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6702 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6703 | Subobject, Field, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6704 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6705 | |
| 6706 | // Perform an implicit value-initialization for members beyond the end of |
| 6707 | // the initializer list. |
| 6708 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6709 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6710 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6711 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 6712 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 6713 | isa<CXXDefaultInitExpr>(Init)); |
| 6714 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 6715 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 6716 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) || |
| 6717 | (Field->isBitField() && !truncateBitfieldValue(Info, Init, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6718 | FieldVal, Field))) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 6719 | if (!Info.noteFailure()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6720 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6721 | Success = false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6722 | } |
| 6723 | } |
| 6724 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6725 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6726 | } |
| 6727 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6728 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 6729 | QualType T) { |
| 6730 | // Note that E's type is not necessarily the type of our class here; we might |
| 6731 | // be initializing an array element instead. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6732 | const CXXConstructorDecl *FD = E->getConstructor(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 6733 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 6734 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6735 | bool ZeroInit = E->requiresZeroInitialization(); |
| 6736 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6737 | // If we've already performed zero-initialization, we're already done. |
| 6738 | if (!Result.isUninit()) |
| 6739 | return true; |
| 6740 | |
Richard Smith | da3f4fd | 2014-03-05 23:32:50 +0000 | [diff] [blame] | 6741 | // We can get here in two different ways: |
| 6742 | // 1) We're performing value-initialization, and should zero-initialize |
| 6743 | // the object, or |
| 6744 | // 2) We're performing default-initialization of an object with a trivial |
| 6745 | // constexpr default constructor, in which case we should start the |
| 6746 | // lifetimes of all the base subobjects (there can be no data member |
| 6747 | // subobjects in this case) per [basic.life]p1. |
| 6748 | // Either way, ZeroInitialization is appropriate. |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6749 | return ZeroInitialization(E, T); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 6750 | } |
| 6751 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6752 | const FunctionDecl *Definition = nullptr; |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 6753 | auto Body = FD->getBody(Definition); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6754 | |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 6755 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6756 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6757 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 6758 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6759 | if (E->isElidable() && !ZeroInit) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6760 | if (const MaterializeTemporaryExpr *ME |
| 6761 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 6762 | return Visit(ME->GetTemporaryExpr()); |
| 6763 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6764 | if (ZeroInit && !ZeroInitialization(E, T)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6765 | return false; |
| 6766 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 6767 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6768 | return HandleConstructorCall(E, This, Args, |
| 6769 | cast<CXXConstructorDecl>(Definition), Info, |
| 6770 | Result); |
| 6771 | } |
| 6772 | |
| 6773 | bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr( |
| 6774 | const CXXInheritedCtorInitExpr *E) { |
| 6775 | if (!Info.CurrentCall) { |
| 6776 | assert(Info.checkingPotentialConstantExpression()); |
| 6777 | return false; |
| 6778 | } |
| 6779 | |
| 6780 | const CXXConstructorDecl *FD = E->getConstructor(); |
| 6781 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) |
| 6782 | return false; |
| 6783 | |
| 6784 | const FunctionDecl *Definition = nullptr; |
| 6785 | auto Body = FD->getBody(Definition); |
| 6786 | |
| 6787 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
| 6788 | return false; |
| 6789 | |
| 6790 | return HandleConstructorCall(E, This, Info.CurrentCall->Arguments, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6791 | cast<CXXConstructorDecl>(Definition), Info, |
| 6792 | Result); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6793 | } |
| 6794 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6795 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
| 6796 | const CXXStdInitializerListExpr *E) { |
| 6797 | const ConstantArrayType *ArrayType = |
| 6798 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
| 6799 | |
| 6800 | LValue Array; |
| 6801 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
| 6802 | return false; |
| 6803 | |
| 6804 | // Get a pointer to the first element of the array. |
| 6805 | Array.addArray(Info, E, ArrayType); |
| 6806 | |
| 6807 | // FIXME: Perform the checks on the field types in SemaInit. |
| 6808 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
| 6809 | RecordDecl::field_iterator Field = Record->field_begin(); |
| 6810 | if (Field == Record->field_end()) |
| 6811 | return Error(E); |
| 6812 | |
| 6813 | // Start pointer. |
| 6814 | if (!Field->getType()->isPointerType() || |
| 6815 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 6816 | ArrayType->getElementType())) |
| 6817 | return Error(E); |
| 6818 | |
| 6819 | // FIXME: What if the initializer_list type has base classes, etc? |
| 6820 | Result = APValue(APValue::UninitStruct(), 0, 2); |
| 6821 | Array.moveInto(Result.getStructField(0)); |
| 6822 | |
| 6823 | if (++Field == Record->field_end()) |
| 6824 | return Error(E); |
| 6825 | |
| 6826 | if (Field->getType()->isPointerType() && |
| 6827 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 6828 | ArrayType->getElementType())) { |
| 6829 | // End pointer. |
| 6830 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
| 6831 | ArrayType->getElementType(), |
| 6832 | ArrayType->getSize().getZExtValue())) |
| 6833 | return false; |
| 6834 | Array.moveInto(Result.getStructField(1)); |
| 6835 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
| 6836 | // Length. |
| 6837 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
| 6838 | else |
| 6839 | return Error(E); |
| 6840 | |
| 6841 | if (++Field != Record->field_end()) |
| 6842 | return Error(E); |
| 6843 | |
| 6844 | return true; |
| 6845 | } |
| 6846 | |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6847 | bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) { |
| 6848 | const CXXRecordDecl *ClosureClass = E->getLambdaClass(); |
| 6849 | if (ClosureClass->isInvalidDecl()) return false; |
| 6850 | |
| 6851 | if (Info.checkingPotentialConstantExpression()) return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6852 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6853 | const size_t NumFields = |
| 6854 | std::distance(ClosureClass->field_begin(), ClosureClass->field_end()); |
Benjamin Kramer | aad1bdc | 2017-02-16 14:08:41 +0000 | [diff] [blame] | 6855 | |
| 6856 | assert(NumFields == (size_t)std::distance(E->capture_init_begin(), |
| 6857 | E->capture_init_end()) && |
| 6858 | "The number of lambda capture initializers should equal the number of " |
| 6859 | "fields within the closure type"); |
| 6860 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6861 | Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields); |
| 6862 | // Iterate through all the lambda's closure object's fields and initialize |
| 6863 | // them. |
| 6864 | auto *CaptureInitIt = E->capture_init_begin(); |
| 6865 | const LambdaCapture *CaptureIt = ClosureClass->captures_begin(); |
| 6866 | bool Success = true; |
| 6867 | for (const auto *Field : ClosureClass->fields()) { |
| 6868 | assert(CaptureInitIt != E->capture_init_end()); |
| 6869 | // Get the initializer for this field |
| 6870 | Expr *const CurFieldInit = *CaptureInitIt++; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6871 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6872 | // If there is no initializer, either this is a VLA or an error has |
| 6873 | // occurred. |
| 6874 | if (!CurFieldInit) |
| 6875 | return Error(E); |
| 6876 | |
| 6877 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 6878 | if (!EvaluateInPlace(FieldVal, Info, This, CurFieldInit)) { |
| 6879 | if (!Info.keepEvaluatingAfterFailure()) |
| 6880 | return false; |
| 6881 | Success = false; |
| 6882 | } |
| 6883 | ++CaptureIt; |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6884 | } |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6885 | return Success; |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6886 | } |
| 6887 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6888 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 6889 | APValue &Result, EvalInfo &Info) { |
| 6890 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6891 | "can't evaluate expression as a record rvalue"); |
| 6892 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 6893 | } |
| 6894 | |
| 6895 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6896 | // Temporary Evaluation |
| 6897 | // |
| 6898 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 6899 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 6900 | // materialized so that a reference can bind to it. |
| 6901 | //===----------------------------------------------------------------------===// |
| 6902 | namespace { |
| 6903 | class TemporaryExprEvaluator |
| 6904 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 6905 | public: |
| 6906 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 6907 | LValueExprEvaluatorBaseTy(Info, Result, false) {} |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6908 | |
| 6909 | /// Visit an expression which constructs the value of this temporary. |
| 6910 | bool VisitConstructExpr(const Expr *E) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 6911 | APValue &Value = createTemporary(E, false, Result, *Info.CurrentCall); |
| 6912 | return EvaluateInPlace(Value, Info, Result, E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6913 | } |
| 6914 | |
| 6915 | bool VisitCastExpr(const CastExpr *E) { |
| 6916 | switch (E->getCastKind()) { |
| 6917 | default: |
| 6918 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6919 | |
| 6920 | case CK_ConstructorConversion: |
| 6921 | return VisitConstructExpr(E->getSubExpr()); |
| 6922 | } |
| 6923 | } |
| 6924 | bool VisitInitListExpr(const InitListExpr *E) { |
| 6925 | return VisitConstructExpr(E); |
| 6926 | } |
| 6927 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 6928 | return VisitConstructExpr(E); |
| 6929 | } |
| 6930 | bool VisitCallExpr(const CallExpr *E) { |
| 6931 | return VisitConstructExpr(E); |
| 6932 | } |
Richard Smith | 513955c | 2014-12-17 19:24:30 +0000 | [diff] [blame] | 6933 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
| 6934 | return VisitConstructExpr(E); |
| 6935 | } |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6936 | bool VisitLambdaExpr(const LambdaExpr *E) { |
| 6937 | return VisitConstructExpr(E); |
| 6938 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6939 | }; |
| 6940 | } // end anonymous namespace |
| 6941 | |
| 6942 | /// Evaluate an expression of record type as a temporary. |
| 6943 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 6944 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6945 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 6946 | } |
| 6947 | |
| 6948 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6949 | // Vector Evaluation |
| 6950 | //===----------------------------------------------------------------------===// |
| 6951 | |
| 6952 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 6953 | class VectorExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6954 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6955 | APValue &Result; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6956 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6957 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6958 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 6959 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6960 | |
Craig Topper | 9798b93 | 2015-09-29 04:30:05 +0000 | [diff] [blame] | 6961 | bool Success(ArrayRef<APValue> V, const Expr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6962 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 6963 | // FIXME: remove this APValue copy. |
| 6964 | Result = APValue(V.data(), V.size()); |
| 6965 | return true; |
| 6966 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6967 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6968 | assert(V.isVector()); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6969 | Result = V; |
| 6970 | return true; |
| 6971 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6972 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6973 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6974 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6975 | { return Visit(E->getSubExpr()); } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6976 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6977 | bool VisitInitListExpr(const InitListExpr *E); |
| 6978 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6979 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 6980 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6981 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6982 | }; |
| 6983 | } // end anonymous namespace |
| 6984 | |
| 6985 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6986 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6987 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6988 | } |
| 6989 | |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 6990 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6991 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 6992 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6993 | |
Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 6994 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 6995 | QualType SETy = SE->getType(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6996 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6997 | switch (E->getCastKind()) { |
| 6998 | case CK_VectorSplat: { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6999 | APValue Val = APValue(); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7000 | if (SETy->isIntegerType()) { |
| 7001 | APSInt IntResult; |
| 7002 | if (!EvaluateInteger(SE, IntResult, Info)) |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 7003 | return false; |
| 7004 | Val = APValue(std::move(IntResult)); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7005 | } else if (SETy->isRealFloatingType()) { |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 7006 | APFloat FloatResult(0.0); |
| 7007 | if (!EvaluateFloat(SE, FloatResult, Info)) |
| 7008 | return false; |
| 7009 | Val = APValue(std::move(FloatResult)); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7010 | } else { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 7011 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7012 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 7013 | |
| 7014 | // Splat and create vector APValue. |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 7015 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 7016 | return Success(Elts, E); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 7017 | } |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 7018 | case CK_BitCast: { |
| 7019 | // Evaluate the operand into an APInt we can extract from. |
| 7020 | llvm::APInt SValInt; |
| 7021 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 7022 | return false; |
| 7023 | // Extract the elements |
| 7024 | QualType EltTy = VTy->getElementType(); |
| 7025 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 7026 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 7027 | SmallVector<APValue, 4> Elts; |
| 7028 | if (EltTy->isRealFloatingType()) { |
| 7029 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 7030 | unsigned FloatEltSize = EltSize; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 7031 | if (&Sem == &APFloat::x87DoubleExtended()) |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 7032 | FloatEltSize = 80; |
| 7033 | for (unsigned i = 0; i < NElts; i++) { |
| 7034 | llvm::APInt Elt; |
| 7035 | if (BigEndian) |
| 7036 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 7037 | else |
| 7038 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
Tim Northover | 178723a | 2013-01-22 09:46:51 +0000 | [diff] [blame] | 7039 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 7040 | } |
| 7041 | } else if (EltTy->isIntegerType()) { |
| 7042 | for (unsigned i = 0; i < NElts; i++) { |
| 7043 | llvm::APInt Elt; |
| 7044 | if (BigEndian) |
| 7045 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 7046 | else |
| 7047 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 7048 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 7049 | } |
| 7050 | } else { |
| 7051 | return Error(E); |
| 7052 | } |
| 7053 | return Success(Elts, E); |
| 7054 | } |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7055 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7056 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 7057 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7058 | } |
| 7059 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 7060 | bool |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7061 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 7062 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7063 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 7064 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7065 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7066 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7067 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7068 | |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 7069 | // The number of initializers can be less than the number of |
| 7070 | // vector elements. For OpenCL, this can be due to nested vector |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7071 | // initialization. For GCC compatibility, missing trailing elements |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 7072 | // should be initialized with zeroes. |
| 7073 | unsigned CountInits = 0, CountElts = 0; |
| 7074 | while (CountElts < NumElements) { |
| 7075 | // Handle nested vector initialization. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7076 | if (CountInits < NumInits |
Eli Friedman | 1409e6e | 2013-09-17 04:07:02 +0000 | [diff] [blame] | 7077 | && E->getInit(CountInits)->getType()->isVectorType()) { |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 7078 | APValue v; |
| 7079 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 7080 | return Error(E); |
| 7081 | unsigned vlen = v.getVectorLength(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7082 | for (unsigned j = 0; j < vlen; j++) |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 7083 | Elements.push_back(v.getVectorElt(j)); |
| 7084 | CountElts += vlen; |
| 7085 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7086 | llvm::APSInt sInt(32); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 7087 | if (CountInits < NumInits) { |
| 7088 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 7089 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 7090 | } else // trailing integer zero. |
| 7091 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 7092 | Elements.push_back(APValue(sInt)); |
| 7093 | CountElts++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7094 | } else { |
| 7095 | llvm::APFloat f(0.0); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 7096 | if (CountInits < NumInits) { |
| 7097 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 7098 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 7099 | } else // trailing float zero. |
| 7100 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 7101 | Elements.push_back(APValue(f)); |
| 7102 | CountElts++; |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 7103 | } |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 7104 | CountInits++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7105 | } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 7106 | return Success(Elements, E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7107 | } |
| 7108 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 7109 | bool |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7110 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 7111 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 7112 | QualType EltTy = VT->getElementType(); |
| 7113 | APValue ZeroElement; |
| 7114 | if (EltTy->isIntegerType()) |
| 7115 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 7116 | else |
| 7117 | ZeroElement = |
| 7118 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 7119 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7120 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 7121 | return Success(Elements, E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 7122 | } |
| 7123 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 7124 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 7125 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7126 | return ZeroInitialization(E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 7127 | } |
| 7128 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7129 | //===----------------------------------------------------------------------===// |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7130 | // Array Evaluation |
| 7131 | //===----------------------------------------------------------------------===// |
| 7132 | |
| 7133 | namespace { |
| 7134 | class ArrayExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7135 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7136 | const LValue &This; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7137 | APValue &Result; |
| 7138 | public: |
| 7139 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7140 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 7141 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7142 | |
| 7143 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 7144 | assert(V.isArray() && "expected array"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7145 | Result = V; |
| 7146 | return true; |
| 7147 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7148 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7149 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7150 | const ConstantArrayType *CAT = |
| 7151 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 7152 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7153 | return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7154 | |
| 7155 | Result = APValue(APValue::UninitArray(), 0, |
| 7156 | CAT->getSize().getZExtValue()); |
| 7157 | if (!Result.hasArrayFiller()) return true; |
| 7158 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7159 | // Zero-initialize all elements. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7160 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 7161 | Subobject.addArray(Info, E, CAT); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7162 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7163 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7164 | } |
| 7165 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 7166 | bool VisitCallExpr(const CallExpr *E) { |
| 7167 | return handleCallExpr(E, Result, &This); |
| 7168 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7169 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7170 | bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7171 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7172 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 7173 | const LValue &Subobject, |
| 7174 | APValue *Value, QualType Type); |
Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 7175 | bool VisitStringLiteral(const StringLiteral *E) { |
| 7176 | expandStringLiteral(Info, E, Result); |
| 7177 | return true; |
| 7178 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7179 | }; |
| 7180 | } // end anonymous namespace |
| 7181 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7182 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 7183 | APValue &Result, EvalInfo &Info) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7184 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7185 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7186 | } |
| 7187 | |
Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 7188 | // Return true iff the given array filler may depend on the element index. |
| 7189 | static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { |
| 7190 | // For now, just whitelist non-class value-initialization and initialization |
| 7191 | // lists comprised of them. |
| 7192 | if (isa<ImplicitValueInitExpr>(FillerExpr)) |
| 7193 | return false; |
| 7194 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(FillerExpr)) { |
| 7195 | for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { |
| 7196 | if (MaybeElementDependentArrayFiller(ILE->getInit(I))) |
| 7197 | return true; |
| 7198 | } |
| 7199 | return false; |
| 7200 | } |
| 7201 | return true; |
| 7202 | } |
| 7203 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7204 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 7205 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 7206 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7207 | return Error(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7208 | |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 7209 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 7210 | // an appropriately-typed string literal enclosed in braces. |
Eli Friedman | 3bf72d7 | 2019-02-08 21:18:46 +0000 | [diff] [blame] | 7211 | if (E->isStringLiteralInit()) |
| 7212 | return Visit(E->getInit(0)); |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 7213 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7214 | bool Success = true; |
| 7215 | |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 7216 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
| 7217 | "zero-initialized array shouldn't have any initialized elts"); |
| 7218 | APValue Filler; |
| 7219 | if (Result.isArray() && Result.hasArrayFiller()) |
| 7220 | Filler = Result.getArrayFiller(); |
| 7221 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7222 | unsigned NumEltsToInit = E->getNumInits(); |
| 7223 | unsigned NumElts = CAT->getSize().getZExtValue(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7224 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : nullptr; |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7225 | |
| 7226 | // 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] | 7227 | // array element. |
| 7228 | if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr)) |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7229 | NumEltsToInit = NumElts; |
| 7230 | |
Nicola Zaghen | 3538b39 | 2018-05-15 13:30:56 +0000 | [diff] [blame] | 7231 | LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: " |
| 7232 | << NumEltsToInit << ".\n"); |
Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 7233 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7234 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 7235 | |
| 7236 | // If the array was previously zero-initialized, preserve the |
| 7237 | // zero-initialized values. |
| 7238 | if (!Filler.isUninit()) { |
| 7239 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
| 7240 | Result.getArrayInitializedElt(I) = Filler; |
| 7241 | if (Result.hasArrayFiller()) |
| 7242 | Result.getArrayFiller() = Filler; |
| 7243 | } |
| 7244 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7245 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 7246 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7247 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
| 7248 | const Expr *Init = |
| 7249 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 7250 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7251 | Info, Subobject, Init) || |
| 7252 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7253 | CAT->getElementType(), 1)) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 7254 | if (!Info.noteFailure()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 7255 | return false; |
| 7256 | Success = false; |
| 7257 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 7258 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7259 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7260 | if (!Result.hasArrayFiller()) |
| 7261 | return Success; |
| 7262 | |
| 7263 | // If we get here, we have a trivial filler, which we can just evaluate |
| 7264 | // once and splat over the rest of the array elements. |
| 7265 | assert(FillerExpr && "no array filler for incomplete init list"); |
| 7266 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
| 7267 | FillerExpr) && Success; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7268 | } |
| 7269 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7270 | bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
| 7271 | if (E->getCommonExpr() && |
| 7272 | !Evaluate(Info.CurrentCall->createTemporary(E->getCommonExpr(), false), |
| 7273 | Info, E->getCommonExpr()->getSourceExpr())) |
| 7274 | return false; |
| 7275 | |
| 7276 | auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe()); |
| 7277 | |
| 7278 | uint64_t Elements = CAT->getSize().getZExtValue(); |
| 7279 | Result = APValue(APValue::UninitArray(), Elements, Elements); |
| 7280 | |
| 7281 | LValue Subobject = This; |
| 7282 | Subobject.addArray(Info, E, CAT); |
| 7283 | |
| 7284 | bool Success = true; |
| 7285 | for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) { |
| 7286 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
| 7287 | Info, Subobject, E->getSubExpr()) || |
| 7288 | !HandleLValueArrayAdjustment(Info, E, Subobject, |
| 7289 | CAT->getElementType(), 1)) { |
| 7290 | if (!Info.noteFailure()) |
| 7291 | return false; |
| 7292 | Success = false; |
| 7293 | } |
| 7294 | } |
| 7295 | |
| 7296 | return Success; |
| 7297 | } |
| 7298 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7299 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7300 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
| 7301 | } |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 7302 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7303 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 7304 | const LValue &Subobject, |
| 7305 | APValue *Value, |
| 7306 | QualType Type) { |
| 7307 | bool HadZeroInit = !Value->isUninit(); |
| 7308 | |
| 7309 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
| 7310 | unsigned N = CAT->getSize().getZExtValue(); |
| 7311 | |
| 7312 | // Preserve the array filler if we had prior zero-initialization. |
| 7313 | APValue Filler = |
| 7314 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
| 7315 | : APValue(); |
| 7316 | |
| 7317 | *Value = APValue(APValue::UninitArray(), N, N); |
| 7318 | |
| 7319 | if (HadZeroInit) |
| 7320 | for (unsigned I = 0; I != N; ++I) |
| 7321 | Value->getArrayInitializedElt(I) = Filler; |
| 7322 | |
| 7323 | // Initialize the elements. |
| 7324 | LValue ArrayElt = Subobject; |
| 7325 | ArrayElt.addArray(Info, E, CAT); |
| 7326 | for (unsigned I = 0; I != N; ++I) |
| 7327 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
| 7328 | CAT->getElementType()) || |
| 7329 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
| 7330 | CAT->getElementType(), 1)) |
| 7331 | return false; |
| 7332 | |
| 7333 | return true; |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 7334 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7335 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7336 | if (!Type->isRecordType()) |
Richard Smith | 9fce7bc | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 7337 | return Error(E); |
| 7338 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 7339 | return RecordExprEvaluator(Info, Subobject, *Value) |
| 7340 | .VisitCXXConstructExpr(E, Type); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7341 | } |
| 7342 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7343 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7344 | // Integer Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7345 | // |
| 7346 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 7347 | // types and back in constant folding. Integer values are thus represented |
| 7348 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7349 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7350 | |
| 7351 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7352 | class IntExprEvaluator |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 7353 | : public ExprEvaluatorBase<IntExprEvaluator> { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7354 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 7355 | public: |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7356 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 7357 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7358 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7359 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7360 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 7361 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7362 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7363 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7364 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7365 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7366 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7367 | return true; |
| 7368 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7369 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 7370 | return Success(SI, E, Result); |
| 7371 | } |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7372 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7373 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7374 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 7375 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7376 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7377 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7378 | Result = APValue(APSInt(I)); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 7379 | Result.getInt().setIsUnsigned( |
| 7380 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7381 | return true; |
| 7382 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7383 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 7384 | return Success(I, E, Result); |
| 7385 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7386 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7387 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 7388 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 7389 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7390 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7391 | return true; |
| 7392 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7393 | bool Success(uint64_t Value, const Expr *E) { |
| 7394 | return Success(Value, E, Result); |
| 7395 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7396 | |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 7397 | bool Success(CharUnits Size, const Expr *E) { |
| 7398 | return Success(Size.getQuantity(), E); |
| 7399 | } |
| 7400 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7401 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 7402 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 9c8d1c5 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 7403 | Result = V; |
| 7404 | return true; |
| 7405 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7406 | return Success(V.getInt(), E); |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 7407 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7408 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7409 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7410 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7411 | //===--------------------------------------------------------------------===// |
| 7412 | // Visitor Methods |
| 7413 | //===--------------------------------------------------------------------===// |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 7414 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 7415 | bool VisitConstantExpr(const ConstantExpr *E); |
| 7416 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7417 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7418 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7419 | } |
| 7420 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7421 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7422 | } |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7423 | |
| 7424 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 7425 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7426 | if (CheckReferencedDecl(E, E->getDecl())) |
| 7427 | return true; |
| 7428 | |
| 7429 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7430 | } |
| 7431 | bool VisitMemberExpr(const MemberExpr *E) { |
| 7432 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 7433 | VisitIgnoredBaseExpression(E->getBase()); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7434 | return true; |
| 7435 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7436 | |
| 7437 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7438 | } |
| 7439 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7440 | bool VisitCallExpr(const CallExpr *E); |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 7441 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7442 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7443 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7444 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 7445 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7446 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7447 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 7448 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7449 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7450 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7451 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7452 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 7453 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 7454 | return Success(E->getValue(), E); |
| 7455 | } |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7456 | |
| 7457 | bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
| 7458 | if (Info.ArrayInitIndex == uint64_t(-1)) { |
| 7459 | // We were asked to evaluate this subexpression independent of the |
| 7460 | // enclosing ArrayInitLoopExpr. We can't do that. |
| 7461 | Info.FFDiag(E); |
| 7462 | return false; |
| 7463 | } |
| 7464 | return Success(Info.ArrayInitIndex, E); |
| 7465 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7466 | |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7467 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 7468 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7469 | return ZeroInitialization(E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7470 | } |
| 7471 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 7472 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 7473 | return Success(E->getValue(), E); |
| 7474 | } |
| 7475 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 7476 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 7477 | return Success(E->getValue(), E); |
| 7478 | } |
| 7479 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 7480 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 7481 | return Success(E->getValue(), E); |
| 7482 | } |
| 7483 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7484 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7485 | bool VisitUnaryImag(const UnaryOperator *E); |
| 7486 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 7487 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 7488 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 7489 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7490 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7491 | }; |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 7492 | |
| 7493 | class FixedPointExprEvaluator |
| 7494 | : public ExprEvaluatorBase<FixedPointExprEvaluator> { |
| 7495 | APValue &Result; |
| 7496 | |
| 7497 | public: |
| 7498 | FixedPointExprEvaluator(EvalInfo &info, APValue &result) |
| 7499 | : ExprEvaluatorBaseTy(info), Result(result) {} |
| 7500 | |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 7501 | bool Success(const llvm::APInt &I, const Expr *E) { |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 7502 | return Success( |
| 7503 | APFixedPoint(I, Info.Ctx.getFixedPointSemantics(E->getType())), E); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 7504 | } |
| 7505 | |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 7506 | bool Success(uint64_t Value, const Expr *E) { |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 7507 | return Success( |
| 7508 | APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(E->getType())), E); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 7509 | } |
| 7510 | |
| 7511 | bool Success(const APValue &V, const Expr *E) { |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 7512 | return Success(V.getFixedPoint(), E); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 7513 | } |
| 7514 | |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 7515 | bool Success(const APFixedPoint &V, const Expr *E) { |
| 7516 | assert(E->getType()->isFixedPointType() && "Invalid evaluation result."); |
| 7517 | assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) && |
| 7518 | "Invalid evaluation result."); |
| 7519 | Result = APValue(V); |
| 7520 | return true; |
| 7521 | } |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 7522 | |
| 7523 | //===--------------------------------------------------------------------===// |
| 7524 | // Visitor Methods |
| 7525 | //===--------------------------------------------------------------------===// |
| 7526 | |
| 7527 | bool VisitFixedPointLiteral(const FixedPointLiteral *E) { |
| 7528 | return Success(E->getValue(), E); |
| 7529 | } |
| 7530 | |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 7531 | bool VisitCastExpr(const CastExpr *E); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 7532 | bool VisitUnaryOperator(const UnaryOperator *E); |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 7533 | bool VisitBinaryOperator(const BinaryOperator *E); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 7534 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7535 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7536 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7537 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 7538 | /// produce either the integer value or a pointer. |
| 7539 | /// |
| 7540 | /// GCC has a heinous extension which folds casts between pointer types and |
| 7541 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 7542 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 7543 | /// Some simple arithmetic on such values is supported (they are treated much |
| 7544 | /// like char*). |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7545 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 7546 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7547 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7548 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 7549 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7550 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7551 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7552 | APValue Val; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7553 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 7554 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7555 | if (!Val.isInt()) { |
| 7556 | // FIXME: It would be better to produce the diagnostic for casting |
| 7557 | // a pointer to an integer. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 7558 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7559 | return false; |
| 7560 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7561 | Result = Val.getInt(); |
| 7562 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7563 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7564 | |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 7565 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
| 7566 | EvalInfo &Info) { |
| 7567 | if (E->getType()->isFixedPointType()) { |
| 7568 | APValue Val; |
| 7569 | if (!FixedPointExprEvaluator(Info, Val).Visit(E)) |
| 7570 | return false; |
| 7571 | if (!Val.isFixedPoint()) |
| 7572 | return false; |
| 7573 | |
| 7574 | Result = Val.getFixedPoint(); |
| 7575 | return true; |
| 7576 | } |
| 7577 | return false; |
| 7578 | } |
| 7579 | |
| 7580 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
| 7581 | EvalInfo &Info) { |
| 7582 | if (E->getType()->isIntegerType()) { |
| 7583 | auto FXSema = Info.Ctx.getFixedPointSemantics(E->getType()); |
| 7584 | APSInt Val; |
| 7585 | if (!EvaluateInteger(E, Val, Info)) |
| 7586 | return false; |
| 7587 | Result = APFixedPoint(Val, FXSema); |
| 7588 | return true; |
| 7589 | } else if (E->getType()->isFixedPointType()) { |
| 7590 | return EvaluateFixedPoint(E, Result, Info); |
| 7591 | } |
| 7592 | return false; |
| 7593 | } |
| 7594 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7595 | /// Check whether the given declaration can be directly converted to an integral |
| 7596 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 7597 | /// try. |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7598 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7599 | // Enums are integer constant exprs. |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 7600 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7601 | // Check for signedness/width mismatches between E type and ECD value. |
| 7602 | bool SameSign = (ECD->getInitVal().isSigned() |
| 7603 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 7604 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 7605 | == Info.Ctx.getIntWidth(E->getType())); |
| 7606 | if (SameSign && SameWidth) |
| 7607 | return Success(ECD->getInitVal(), E); |
| 7608 | else { |
| 7609 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 7610 | // by computing a new value matching the type of E. |
| 7611 | llvm::APSInt Val = ECD->getInitVal(); |
| 7612 | if (!SameSign) |
| 7613 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 7614 | if (!SameWidth) |
| 7615 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 7616 | return Success(Val, E); |
| 7617 | } |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 7618 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7619 | return false; |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7620 | } |
| 7621 | |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7622 | /// Values returned by __builtin_classify_type, chosen to match the values |
| 7623 | /// produced by GCC's builtin. |
| 7624 | enum class GCCTypeClass { |
| 7625 | None = -1, |
| 7626 | Void = 0, |
| 7627 | Integer = 1, |
| 7628 | // GCC reserves 2 for character types, but instead classifies them as |
| 7629 | // integers. |
| 7630 | Enum = 3, |
| 7631 | Bool = 4, |
| 7632 | Pointer = 5, |
| 7633 | // GCC reserves 6 for references, but appears to never use it (because |
| 7634 | // expressions never have reference type, presumably). |
| 7635 | PointerToDataMember = 7, |
| 7636 | RealFloat = 8, |
| 7637 | Complex = 9, |
| 7638 | // GCC reserves 10 for functions, but does not use it since GCC version 6 due |
| 7639 | // to decay to pointer. (Prior to version 6 it was only used in C++ mode). |
| 7640 | // GCC claims to reserve 11 for pointers to member functions, but *actually* |
| 7641 | // uses 12 for that purpose, same as for a class or struct. Maybe it |
| 7642 | // internally implements a pointer to member as a struct? Who knows. |
| 7643 | PointerToMemberFunction = 12, // Not a bug, see above. |
| 7644 | ClassOrStruct = 12, |
| 7645 | Union = 13, |
| 7646 | // GCC reserves 14 for arrays, but does not use it since GCC version 6 due to |
| 7647 | // decay to pointer. (Prior to version 6 it was only used in C++ mode). |
| 7648 | // GCC reserves 15 for strings, but actually uses 5 (pointer) for string |
| 7649 | // literals. |
| 7650 | }; |
| 7651 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7652 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 7653 | /// as GCC. |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7654 | static GCCTypeClass |
| 7655 | EvaluateBuiltinClassifyType(QualType T, const LangOptions &LangOpts) { |
| 7656 | assert(!T->isDependentType() && "unexpected dependent type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7657 | |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7658 | QualType CanTy = T.getCanonicalType(); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7659 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanTy); |
| 7660 | |
| 7661 | switch (CanTy->getTypeClass()) { |
| 7662 | #define TYPE(ID, BASE) |
| 7663 | #define DEPENDENT_TYPE(ID, BASE) case Type::ID: |
| 7664 | #define NON_CANONICAL_TYPE(ID, BASE) case Type::ID: |
| 7665 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID: |
| 7666 | #include "clang/AST/TypeNodes.def" |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7667 | case Type::Auto: |
| 7668 | case Type::DeducedTemplateSpecialization: |
| 7669 | llvm_unreachable("unexpected non-canonical or dependent type"); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7670 | |
| 7671 | case Type::Builtin: |
| 7672 | switch (BT->getKind()) { |
| 7673 | #define BUILTIN_TYPE(ID, SINGLETON_ID) |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7674 | #define SIGNED_TYPE(ID, SINGLETON_ID) \ |
| 7675 | case BuiltinType::ID: return GCCTypeClass::Integer; |
| 7676 | #define FLOATING_TYPE(ID, SINGLETON_ID) \ |
| 7677 | case BuiltinType::ID: return GCCTypeClass::RealFloat; |
| 7678 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \ |
| 7679 | case BuiltinType::ID: break; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7680 | #include "clang/AST/BuiltinTypes.def" |
| 7681 | case BuiltinType::Void: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7682 | return GCCTypeClass::Void; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7683 | |
| 7684 | case BuiltinType::Bool: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7685 | return GCCTypeClass::Bool; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7686 | |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7687 | case BuiltinType::Char_U: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7688 | case BuiltinType::UChar: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7689 | case BuiltinType::WChar_U: |
| 7690 | case BuiltinType::Char8: |
| 7691 | case BuiltinType::Char16: |
| 7692 | case BuiltinType::Char32: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7693 | case BuiltinType::UShort: |
| 7694 | case BuiltinType::UInt: |
| 7695 | case BuiltinType::ULong: |
| 7696 | case BuiltinType::ULongLong: |
| 7697 | case BuiltinType::UInt128: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7698 | return GCCTypeClass::Integer; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7699 | |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 7700 | case BuiltinType::UShortAccum: |
| 7701 | case BuiltinType::UAccum: |
| 7702 | case BuiltinType::ULongAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 7703 | case BuiltinType::UShortFract: |
| 7704 | case BuiltinType::UFract: |
| 7705 | case BuiltinType::ULongFract: |
| 7706 | case BuiltinType::SatUShortAccum: |
| 7707 | case BuiltinType::SatUAccum: |
| 7708 | case BuiltinType::SatULongAccum: |
| 7709 | case BuiltinType::SatUShortFract: |
| 7710 | case BuiltinType::SatUFract: |
| 7711 | case BuiltinType::SatULongFract: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 7712 | return GCCTypeClass::None; |
| 7713 | |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7714 | case BuiltinType::NullPtr: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7715 | |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7716 | case BuiltinType::ObjCId: |
| 7717 | case BuiltinType::ObjCClass: |
| 7718 | case BuiltinType::ObjCSel: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 7719 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 7720 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 7721 | #include "clang/Basic/OpenCLImageTypes.def" |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 7722 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 7723 | case BuiltinType::Id: |
| 7724 | #include "clang/Basic/OpenCLExtensionTypes.def" |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7725 | case BuiltinType::OCLSampler: |
| 7726 | case BuiltinType::OCLEvent: |
| 7727 | case BuiltinType::OCLClkEvent: |
| 7728 | case BuiltinType::OCLQueue: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7729 | case BuiltinType::OCLReserveID: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7730 | return GCCTypeClass::None; |
| 7731 | |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7732 | case BuiltinType::Dependent: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7733 | llvm_unreachable("unexpected dependent type"); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7734 | }; |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7735 | llvm_unreachable("unexpected placeholder type"); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7736 | |
| 7737 | case Type::Enum: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7738 | return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7739 | |
| 7740 | case Type::Pointer: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7741 | case Type::ConstantArray: |
| 7742 | case Type::VariableArray: |
| 7743 | case Type::IncompleteArray: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7744 | case Type::FunctionNoProto: |
| 7745 | case Type::FunctionProto: |
| 7746 | return GCCTypeClass::Pointer; |
| 7747 | |
| 7748 | case Type::MemberPointer: |
| 7749 | return CanTy->isMemberDataPointerType() |
| 7750 | ? GCCTypeClass::PointerToDataMember |
| 7751 | : GCCTypeClass::PointerToMemberFunction; |
| 7752 | |
| 7753 | case Type::Complex: |
| 7754 | return GCCTypeClass::Complex; |
| 7755 | |
| 7756 | case Type::Record: |
| 7757 | return CanTy->isUnionType() ? GCCTypeClass::Union |
| 7758 | : GCCTypeClass::ClassOrStruct; |
| 7759 | |
| 7760 | case Type::Atomic: |
| 7761 | // GCC classifies _Atomic T the same as T. |
| 7762 | return EvaluateBuiltinClassifyType( |
| 7763 | CanTy->castAs<AtomicType>()->getValueType(), LangOpts); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7764 | |
| 7765 | case Type::BlockPointer: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7766 | case Type::Vector: |
| 7767 | case Type::ExtVector: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7768 | case Type::ObjCObject: |
| 7769 | case Type::ObjCInterface: |
| 7770 | case Type::ObjCObjectPointer: |
| 7771 | case Type::Pipe: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7772 | // GCC classifies vectors as None. We follow its lead and classify all |
| 7773 | // other types that don't fit into the regular classification the same way. |
| 7774 | return GCCTypeClass::None; |
| 7775 | |
| 7776 | case Type::LValueReference: |
| 7777 | case Type::RValueReference: |
| 7778 | llvm_unreachable("invalid type for expression"); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7779 | } |
| 7780 | |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7781 | llvm_unreachable("unexpected type class"); |
| 7782 | } |
| 7783 | |
| 7784 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 7785 | /// as GCC. |
| 7786 | static GCCTypeClass |
| 7787 | EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) { |
| 7788 | // If no argument was supplied, default to None. This isn't |
| 7789 | // ideal, however it is what gcc does. |
| 7790 | if (E->getNumArgs() == 0) |
| 7791 | return GCCTypeClass::None; |
| 7792 | |
| 7793 | // FIXME: Bizarrely, GCC treats a call with more than one argument as not |
| 7794 | // being an ICE, but still folds it to a constant using the type of the first |
| 7795 | // argument. |
| 7796 | return EvaluateBuiltinClassifyType(E->getArg(0)->getType(), LangOpts); |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7797 | } |
| 7798 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7799 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 7800 | /// __builtin_constant_p when applied to the given lvalue. |
| 7801 | /// |
| 7802 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 7803 | /// character of a string literal. |
| 7804 | template<typename LValue> |
| 7805 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
Douglas Gregor | f31cee6 | 2012-03-11 02:23:56 +0000 | [diff] [blame] | 7806 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7807 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 7808 | } |
| 7809 | |
| 7810 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 7811 | /// GCC as we can manage. |
| 7812 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 7813 | QualType ArgType = Arg->getType(); |
| 7814 | |
| 7815 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 7816 | // are not precisely documented, but are as follows: |
| 7817 | // |
| 7818 | // - If the operand is of integral, floating, complex or enumeration type, |
| 7819 | // and can be folded to a known value of that type, it returns 1. |
| 7820 | // - If the operand and can be folded to a pointer to the first character |
| 7821 | // of a string literal (or such a pointer cast to an integral type), it |
| 7822 | // returns 1. |
| 7823 | // |
| 7824 | // Otherwise, it returns 0. |
| 7825 | // |
| 7826 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 7827 | // its support for this does not currently work. |
| 7828 | if (ArgType->isIntegralOrEnumerationType()) { |
| 7829 | Expr::EvalResult Result; |
| 7830 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 7831 | return false; |
| 7832 | |
| 7833 | APValue &V = Result.Val; |
| 7834 | if (V.getKind() == APValue::Int) |
| 7835 | return true; |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 7836 | if (V.getKind() == APValue::LValue) |
| 7837 | return EvaluateBuiltinConstantPForLValue(V); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7838 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 7839 | return Arg->isEvaluatable(Ctx); |
| 7840 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 7841 | LValue LV; |
| 7842 | Expr::EvalStatus Status; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 7843 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7844 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 7845 | : EvaluatePointer(Arg, LV, Info)) && |
| 7846 | !Status.HasSideEffects) |
| 7847 | return EvaluateBuiltinConstantPForLValue(LV); |
| 7848 | } |
| 7849 | |
| 7850 | // Anything else isn't considered to be sufficiently constant. |
| 7851 | return false; |
| 7852 | } |
| 7853 | |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7854 | /// Retrieves the "underlying object type" of the given expression, |
| 7855 | /// as used by __builtin_object_size. |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7856 | static QualType getObjectType(APValue::LValueBase B) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7857 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 7858 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7859 | return VD->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7860 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 7861 | if (isa<CompoundLiteralExpr>(E)) |
| 7862 | return E->getType(); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7863 | } |
| 7864 | |
| 7865 | return QualType(); |
| 7866 | } |
| 7867 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7868 | /// A more selective version of E->IgnoreParenCasts for |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7869 | /// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only |
George Burgess IV | b40cd56 | 2015-09-04 22:36:18 +0000 | [diff] [blame] | 7870 | /// to change the type of E. |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7871 | /// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo` |
| 7872 | /// |
| 7873 | /// Always returns an RValue with a pointer representation. |
| 7874 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
| 7875 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
| 7876 | |
| 7877 | auto *NoParens = E->IgnoreParens(); |
| 7878 | auto *Cast = dyn_cast<CastExpr>(NoParens); |
George Burgess IV | b40cd56 | 2015-09-04 22:36:18 +0000 | [diff] [blame] | 7879 | if (Cast == nullptr) |
| 7880 | return NoParens; |
| 7881 | |
| 7882 | // We only conservatively allow a few kinds of casts, because this code is |
| 7883 | // inherently a simple solution that seeks to support the common case. |
| 7884 | auto CastKind = Cast->getCastKind(); |
| 7885 | if (CastKind != CK_NoOp && CastKind != CK_BitCast && |
| 7886 | CastKind != CK_AddressSpaceConversion) |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7887 | return NoParens; |
| 7888 | |
| 7889 | auto *SubExpr = Cast->getSubExpr(); |
| 7890 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isRValue()) |
| 7891 | return NoParens; |
| 7892 | return ignorePointerCastsAndParens(SubExpr); |
| 7893 | } |
| 7894 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7895 | /// Checks to see if the given LValue's Designator is at the end of the LValue's |
| 7896 | /// record layout. e.g. |
| 7897 | /// struct { struct { int a, b; } fst, snd; } obj; |
| 7898 | /// obj.fst // no |
| 7899 | /// obj.snd // yes |
| 7900 | /// obj.fst.a // no |
| 7901 | /// obj.fst.b // no |
| 7902 | /// obj.snd.a // no |
| 7903 | /// obj.snd.b // yes |
| 7904 | /// |
| 7905 | /// Please note: this function is specialized for how __builtin_object_size |
| 7906 | /// views "objects". |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7907 | /// |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 7908 | /// If this encounters an invalid RecordDecl or otherwise cannot determine the |
| 7909 | /// correct result, it will always return true. |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7910 | static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) { |
| 7911 | assert(!LVal.Designator.Invalid); |
| 7912 | |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7913 | auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD, bool &Invalid) { |
| 7914 | const RecordDecl *Parent = FD->getParent(); |
| 7915 | Invalid = Parent->isInvalidDecl(); |
| 7916 | if (Invalid || Parent->isUnion()) |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7917 | return true; |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7918 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Parent); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7919 | return FD->getFieldIndex() + 1 == Layout.getFieldCount(); |
| 7920 | }; |
| 7921 | |
| 7922 | auto &Base = LVal.getLValueBase(); |
| 7923 | if (auto *ME = dyn_cast_or_null<MemberExpr>(Base.dyn_cast<const Expr *>())) { |
| 7924 | if (auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7925 | bool Invalid; |
| 7926 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
| 7927 | return Invalid; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7928 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(ME->getMemberDecl())) { |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7929 | for (auto *FD : IFD->chain()) { |
| 7930 | bool Invalid; |
| 7931 | if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(FD), Invalid)) |
| 7932 | return Invalid; |
| 7933 | } |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7934 | } |
| 7935 | } |
| 7936 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7937 | unsigned I = 0; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7938 | QualType BaseType = getType(Base); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 7939 | if (LVal.Designator.FirstEntryIsAnUnsizedArray) { |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 7940 | // If we don't know the array bound, conservatively assume we're looking at |
| 7941 | // the final array element. |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7942 | ++I; |
Alex Lorenz | 4e24648 | 2017-12-20 21:03:38 +0000 | [diff] [blame] | 7943 | if (BaseType->isIncompleteArrayType()) |
| 7944 | BaseType = Ctx.getAsArrayType(BaseType)->getElementType(); |
| 7945 | else |
| 7946 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7947 | } |
| 7948 | |
| 7949 | for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) { |
| 7950 | const auto &Entry = LVal.Designator.Entries[I]; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7951 | if (BaseType->isArrayType()) { |
| 7952 | // Because __builtin_object_size treats arrays as objects, we can ignore |
| 7953 | // the index iff this is the last array in the Designator. |
| 7954 | if (I + 1 == E) |
| 7955 | return true; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7956 | const auto *CAT = cast<ConstantArrayType>(Ctx.getAsArrayType(BaseType)); |
| 7957 | uint64_t Index = Entry.ArrayIndex; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7958 | if (Index + 1 != CAT->getSize()) |
| 7959 | return false; |
| 7960 | BaseType = CAT->getElementType(); |
| 7961 | } else if (BaseType->isAnyComplexType()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7962 | const auto *CT = BaseType->castAs<ComplexType>(); |
| 7963 | uint64_t Index = Entry.ArrayIndex; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7964 | if (Index != 1) |
| 7965 | return false; |
| 7966 | BaseType = CT->getElementType(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7967 | } else if (auto *FD = getAsField(Entry)) { |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7968 | bool Invalid; |
| 7969 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
| 7970 | return Invalid; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7971 | BaseType = FD->getType(); |
| 7972 | } else { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7973 | assert(getAsBaseClass(Entry) && "Expecting cast to a base class"); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7974 | return false; |
| 7975 | } |
| 7976 | } |
| 7977 | return true; |
| 7978 | } |
| 7979 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7980 | /// Tests to see if the LValue has a user-specified designator (that isn't |
| 7981 | /// necessarily valid). Note that this always returns 'true' if the LValue has |
| 7982 | /// an unsized array as its first designator entry, because there's currently no |
| 7983 | /// way to tell if the user typed *foo or foo[0]. |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7984 | static bool refersToCompleteObject(const LValue &LVal) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7985 | if (LVal.Designator.Invalid) |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7986 | return false; |
| 7987 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7988 | if (!LVal.Designator.Entries.empty()) |
| 7989 | return LVal.Designator.isMostDerivedAnUnsizedArray(); |
| 7990 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7991 | if (!LVal.InvalidBase) |
| 7992 | return true; |
| 7993 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7994 | // If `E` is a MemberExpr, then the first part of the designator is hiding in |
| 7995 | // the LValueBase. |
| 7996 | const auto *E = LVal.Base.dyn_cast<const Expr *>(); |
| 7997 | return !E || !isa<MemberExpr>(E); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7998 | } |
| 7999 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8000 | /// Attempts to detect a user writing into a piece of memory that's impossible |
| 8001 | /// to figure out the size of by just using types. |
| 8002 | static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) { |
| 8003 | const SubobjectDesignator &Designator = LVal.Designator; |
| 8004 | // Notes: |
| 8005 | // - Users can only write off of the end when we have an invalid base. Invalid |
| 8006 | // bases imply we don't know where the memory came from. |
| 8007 | // - We used to be a bit more aggressive here; we'd only be conservative if |
| 8008 | // the array at the end was flexible, or if it had 0 or 1 elements. This |
| 8009 | // broke some common standard library extensions (PR30346), but was |
| 8010 | // otherwise seemingly fine. It may be useful to reintroduce this behavior |
| 8011 | // with some sort of whitelist. OTOH, it seems that GCC is always |
| 8012 | // conservative with the last element in structs (if it's an array), so our |
| 8013 | // current behavior is more compatible than a whitelisting approach would |
| 8014 | // be. |
| 8015 | return LVal.InvalidBase && |
| 8016 | Designator.Entries.size() == Designator.MostDerivedPathLength && |
| 8017 | Designator.MostDerivedIsArrayElement && |
| 8018 | isDesignatorAtObjectEnd(Ctx, LVal); |
| 8019 | } |
| 8020 | |
| 8021 | /// Converts the given APInt to CharUnits, assuming the APInt is unsigned. |
| 8022 | /// Fails if the conversion would cause loss of precision. |
| 8023 | static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int, |
| 8024 | CharUnits &Result) { |
| 8025 | auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max(); |
| 8026 | if (Int.ugt(CharUnitsMax)) |
| 8027 | return false; |
| 8028 | Result = CharUnits::fromQuantity(Int.getZExtValue()); |
| 8029 | return true; |
| 8030 | } |
| 8031 | |
| 8032 | /// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will |
| 8033 | /// determine how many bytes exist from the beginning of the object to either |
| 8034 | /// the end of the current subobject, or the end of the object itself, depending |
| 8035 | /// on what the LValue looks like + the value of Type. |
George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 8036 | /// |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8037 | /// If this returns false, the value of Result is undefined. |
| 8038 | static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc, |
| 8039 | unsigned Type, const LValue &LVal, |
| 8040 | CharUnits &EndOffset) { |
| 8041 | bool DetermineForCompleteObject = refersToCompleteObject(LVal); |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 8042 | |
George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 8043 | auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { |
| 8044 | if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType()) |
| 8045 | return false; |
| 8046 | return HandleSizeof(Info, ExprLoc, Ty, Result); |
| 8047 | }; |
| 8048 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8049 | // We want to evaluate the size of the entire object. This is a valid fallback |
| 8050 | // for when Type=1 and the designator is invalid, because we're asked for an |
| 8051 | // upper-bound. |
| 8052 | if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) { |
| 8053 | // Type=3 wants a lower bound, so we can't fall back to this. |
| 8054 | if (Type == 3 && !DetermineForCompleteObject) |
George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 8055 | return false; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8056 | |
| 8057 | llvm::APInt APEndOffset; |
| 8058 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 8059 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
| 8060 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
| 8061 | |
| 8062 | if (LVal.InvalidBase) |
| 8063 | return false; |
| 8064 | |
| 8065 | QualType BaseTy = getObjectType(LVal.getLValueBase()); |
George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 8066 | return CheckedHandleSizeof(BaseTy, EndOffset); |
George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 8067 | } |
| 8068 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8069 | // We want to evaluate the size of a subobject. |
| 8070 | const SubobjectDesignator &Designator = LVal.Designator; |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 8071 | |
| 8072 | // The following is a moderately common idiom in C: |
| 8073 | // |
| 8074 | // struct Foo { int a; char c[1]; }; |
| 8075 | // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar)); |
| 8076 | // strcpy(&F->c[0], Bar); |
| 8077 | // |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8078 | // In order to not break too much legacy code, we need to support it. |
| 8079 | if (isUserWritingOffTheEnd(Info.Ctx, LVal)) { |
| 8080 | // If we can resolve this to an alloc_size call, we can hand that back, |
| 8081 | // because we know for certain how many bytes there are to write to. |
| 8082 | llvm::APInt APEndOffset; |
| 8083 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 8084 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
| 8085 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
| 8086 | |
| 8087 | // If we cannot determine the size of the initial allocation, then we can't |
| 8088 | // given an accurate upper-bound. However, we are still able to give |
| 8089 | // conservative lower-bounds for Type=3. |
| 8090 | if (Type == 1) |
| 8091 | return false; |
| 8092 | } |
| 8093 | |
| 8094 | CharUnits BytesPerElem; |
George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 8095 | if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem)) |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 8096 | return false; |
| 8097 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8098 | // According to the GCC documentation, we want the size of the subobject |
| 8099 | // denoted by the pointer. But that's not quite right -- what we actually |
| 8100 | // want is the size of the immediately-enclosing array, if there is one. |
| 8101 | int64_t ElemsRemaining; |
| 8102 | if (Designator.MostDerivedIsArrayElement && |
| 8103 | Designator.Entries.size() == Designator.MostDerivedPathLength) { |
| 8104 | uint64_t ArraySize = Designator.getMostDerivedArraySize(); |
| 8105 | uint64_t ArrayIndex = Designator.Entries.back().ArrayIndex; |
| 8106 | ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex; |
| 8107 | } else { |
| 8108 | ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1; |
| 8109 | } |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 8110 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8111 | EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining; |
| 8112 | return true; |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 8113 | } |
| 8114 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8115 | /// 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] | 8116 | /// returns true and stores the result in @p Size. |
| 8117 | /// |
| 8118 | /// If @p WasError is non-null, this will report whether the failure to evaluate |
| 8119 | /// is to be treated as an Error in IntExprEvaluator. |
| 8120 | static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, |
| 8121 | EvalInfo &Info, uint64_t &Size) { |
| 8122 | // Determine the denoted object. |
| 8123 | LValue LVal; |
| 8124 | { |
| 8125 | // The operand of __builtin_object_size is never evaluated for side-effects. |
| 8126 | // If there are any, but we can determine the pointed-to object anyway, then |
| 8127 | // ignore the side-effects. |
| 8128 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
James Y Knight | 892b09b | 2018-10-10 02:53:43 +0000 | [diff] [blame] | 8129 | IgnoreSideEffectsRAII Fold(Info); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8130 | |
| 8131 | if (E->isGLValue()) { |
| 8132 | // It's possible for us to be given GLValues if we're called via |
| 8133 | // Expr::tryEvaluateObjectSize. |
| 8134 | APValue RVal; |
| 8135 | if (!EvaluateAsRValue(Info, E, RVal)) |
| 8136 | return false; |
| 8137 | LVal.setFrom(Info.Ctx, RVal); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 8138 | } else if (!EvaluatePointer(ignorePointerCastsAndParens(E), LVal, Info, |
| 8139 | /*InvalidBaseOK=*/true)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8140 | return false; |
| 8141 | } |
| 8142 | |
| 8143 | // If we point to before the start of the object, there are no accessible |
| 8144 | // bytes. |
| 8145 | if (LVal.getLValueOffset().isNegative()) { |
| 8146 | Size = 0; |
| 8147 | return true; |
| 8148 | } |
| 8149 | |
| 8150 | CharUnits EndOffset; |
| 8151 | if (!determineEndOffset(Info, E->getExprLoc(), Type, LVal, EndOffset)) |
| 8152 | return false; |
| 8153 | |
| 8154 | // If we've fallen outside of the end offset, just pretend there's nothing to |
| 8155 | // write to/read from. |
| 8156 | if (EndOffset <= LVal.getLValueOffset()) |
| 8157 | Size = 0; |
| 8158 | else |
| 8159 | Size = (EndOffset - LVal.getLValueOffset()).getQuantity(); |
| 8160 | return true; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 8161 | } |
| 8162 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 8163 | bool IntExprEvaluator::VisitConstantExpr(const ConstantExpr *E) { |
| 8164 | llvm::SaveAndRestore<bool> InConstantContext(Info.InConstantContext, true); |
| 8165 | return ExprEvaluatorBaseTy::VisitConstantExpr(E); |
| 8166 | } |
| 8167 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 8168 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 8169 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
| 8170 | return VisitBuiltinCallExpr(E, BuiltinOp); |
| 8171 | |
| 8172 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 8173 | } |
| 8174 | |
| 8175 | bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
| 8176 | unsigned BuiltinOp) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 8177 | switch (unsigned BuiltinOp = E->getBuiltinCallee()) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 8178 | default: |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 8179 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 8180 | |
Erik Pilkington | 9c3b588 | 2019-01-30 20:34:53 +0000 | [diff] [blame] | 8181 | case Builtin::BI__builtin_dynamic_object_size: |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 8182 | case Builtin::BI__builtin_object_size: { |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 8183 | // The type was checked when we built the expression. |
| 8184 | unsigned Type = |
| 8185 | E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 8186 | assert(Type <= 3 && "unexpected type"); |
| 8187 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 8188 | uint64_t Size; |
| 8189 | if (tryEvaluateBuiltinObjectSize(E->getArg(0), Type, Info, Size)) |
| 8190 | return Success(Size, E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 8191 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 8192 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 8193 | return Success((Type & 2) ? 0 : -1, E); |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 8194 | |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 8195 | // Expression had no side effects, but we couldn't statically determine the |
| 8196 | // size of the referenced object. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 8197 | switch (Info.EvalMode) { |
| 8198 | case EvalInfo::EM_ConstantExpression: |
| 8199 | case EvalInfo::EM_PotentialConstantExpression: |
| 8200 | case EvalInfo::EM_ConstantFold: |
| 8201 | case EvalInfo::EM_EvaluateForOverflow: |
| 8202 | case EvalInfo::EM_IgnoreSideEffects: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 8203 | // Leave it to IR generation. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 8204 | return Error(E); |
| 8205 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
| 8206 | case EvalInfo::EM_PotentialConstantExpressionUnevaluated: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 8207 | // Reduce it to a constant now. |
| 8208 | return Success((Type & 2) ? 0 : -1, E); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 8209 | } |
Richard Smith | cb2ba5a | 2016-07-18 22:37:35 +0000 | [diff] [blame] | 8210 | |
| 8211 | llvm_unreachable("unexpected EvalMode"); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 8212 | } |
| 8213 | |
Tim Northover | 314fbfa | 2018-11-02 13:14:11 +0000 | [diff] [blame] | 8214 | case Builtin::BI__builtin_os_log_format_buffer_size: { |
| 8215 | analyze_os_log::OSLogBufferLayout Layout; |
| 8216 | analyze_os_log::computeOSLogBufferLayout(Info.Ctx, E, Layout); |
| 8217 | return Success(Layout.size().getQuantity(), E); |
| 8218 | } |
| 8219 | |
Benjamin Kramer | a801f4a | 2012-10-06 14:42:22 +0000 | [diff] [blame] | 8220 | case Builtin::BI__builtin_bswap16: |
Richard Smith | 80ac9ef | 2012-09-28 20:20:52 +0000 | [diff] [blame] | 8221 | case Builtin::BI__builtin_bswap32: |
| 8222 | case Builtin::BI__builtin_bswap64: { |
| 8223 | APSInt Val; |
| 8224 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 8225 | return false; |
| 8226 | |
| 8227 | return Success(Val.byteSwap(), E); |
| 8228 | } |
| 8229 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 8230 | case Builtin::BI__builtin_classify_type: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 8231 | return Success((int)EvaluateBuiltinClassifyType(E, Info.getLangOpts()), E); |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 8232 | |
Craig Topper | f95a6d9 | 2018-08-08 22:31:12 +0000 | [diff] [blame] | 8233 | case Builtin::BI__builtin_clrsb: |
| 8234 | case Builtin::BI__builtin_clrsbl: |
| 8235 | case Builtin::BI__builtin_clrsbll: { |
| 8236 | APSInt Val; |
| 8237 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 8238 | return false; |
| 8239 | |
| 8240 | return Success(Val.getBitWidth() - Val.getMinSignedBits(), E); |
| 8241 | } |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 8242 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 8243 | case Builtin::BI__builtin_clz: |
| 8244 | case Builtin::BI__builtin_clzl: |
Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 8245 | case Builtin::BI__builtin_clzll: |
| 8246 | case Builtin::BI__builtin_clzs: { |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 8247 | APSInt Val; |
| 8248 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 8249 | return false; |
| 8250 | if (!Val) |
| 8251 | return Error(E); |
| 8252 | |
| 8253 | return Success(Val.countLeadingZeros(), E); |
| 8254 | } |
| 8255 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 8256 | case Builtin::BI__builtin_constant_p: { |
| 8257 | auto Arg = E->getArg(0); |
| 8258 | if (EvaluateBuiltinConstantP(Info.Ctx, Arg)) |
| 8259 | return Success(true, E); |
| 8260 | auto ArgTy = Arg->IgnoreImplicit()->getType(); |
| 8261 | if (!Info.InConstantContext && !Arg->HasSideEffects(Info.Ctx) && |
| 8262 | !ArgTy->isAggregateType() && !ArgTy->isPointerType()) { |
| 8263 | // We can delay calculation of __builtin_constant_p until after |
| 8264 | // inlining. Note: This diagnostic won't be shown to the user. |
| 8265 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Bill Wendling | 2a81f66 | 2018-12-01 08:29:36 +0000 | [diff] [blame] | 8266 | return false; |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 8267 | } |
| 8268 | return Success(false, E); |
| 8269 | } |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 8270 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 8271 | case Builtin::BI__builtin_ctz: |
| 8272 | case Builtin::BI__builtin_ctzl: |
Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 8273 | case Builtin::BI__builtin_ctzll: |
| 8274 | case Builtin::BI__builtin_ctzs: { |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 8275 | APSInt Val; |
| 8276 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 8277 | return false; |
| 8278 | if (!Val) |
| 8279 | return Error(E); |
| 8280 | |
| 8281 | return Success(Val.countTrailingZeros(), E); |
| 8282 | } |
| 8283 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 8284 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 8285 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 8286 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
| 8287 | return Success(Operand, E); |
| 8288 | } |
| 8289 | |
| 8290 | case Builtin::BI__builtin_expect: |
| 8291 | return Visit(E->getArg(0)); |
| 8292 | |
| 8293 | case Builtin::BI__builtin_ffs: |
| 8294 | case Builtin::BI__builtin_ffsl: |
| 8295 | case Builtin::BI__builtin_ffsll: { |
| 8296 | APSInt Val; |
| 8297 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 8298 | return false; |
| 8299 | |
| 8300 | unsigned N = Val.countTrailingZeros(); |
| 8301 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
| 8302 | } |
| 8303 | |
| 8304 | case Builtin::BI__builtin_fpclassify: { |
| 8305 | APFloat Val(0.0); |
| 8306 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
| 8307 | return false; |
| 8308 | unsigned Arg; |
| 8309 | switch (Val.getCategory()) { |
| 8310 | case APFloat::fcNaN: Arg = 0; break; |
| 8311 | case APFloat::fcInfinity: Arg = 1; break; |
| 8312 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
| 8313 | case APFloat::fcZero: Arg = 4; break; |
| 8314 | } |
| 8315 | return Visit(E->getArg(Arg)); |
| 8316 | } |
| 8317 | |
| 8318 | case Builtin::BI__builtin_isinf_sign: { |
| 8319 | APFloat Val(0.0); |
Richard Smith | ab341c6 | 2013-06-13 06:31:13 +0000 | [diff] [blame] | 8320 | return EvaluateFloat(E->getArg(0), Val, Info) && |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 8321 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
| 8322 | } |
| 8323 | |
Richard Smith | ea3019d | 2013-10-15 19:07:14 +0000 | [diff] [blame] | 8324 | case Builtin::BI__builtin_isinf: { |
| 8325 | APFloat Val(0.0); |
| 8326 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 8327 | Success(Val.isInfinity() ? 1 : 0, E); |
| 8328 | } |
| 8329 | |
| 8330 | case Builtin::BI__builtin_isfinite: { |
| 8331 | APFloat Val(0.0); |
| 8332 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 8333 | Success(Val.isFinite() ? 1 : 0, E); |
| 8334 | } |
| 8335 | |
| 8336 | case Builtin::BI__builtin_isnan: { |
| 8337 | APFloat Val(0.0); |
| 8338 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 8339 | Success(Val.isNaN() ? 1 : 0, E); |
| 8340 | } |
| 8341 | |
| 8342 | case Builtin::BI__builtin_isnormal: { |
| 8343 | APFloat Val(0.0); |
| 8344 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 8345 | Success(Val.isNormal() ? 1 : 0, E); |
| 8346 | } |
| 8347 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 8348 | case Builtin::BI__builtin_parity: |
| 8349 | case Builtin::BI__builtin_parityl: |
| 8350 | case Builtin::BI__builtin_parityll: { |
| 8351 | APSInt Val; |
| 8352 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 8353 | return false; |
| 8354 | |
| 8355 | return Success(Val.countPopulation() % 2, E); |
| 8356 | } |
| 8357 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 8358 | case Builtin::BI__builtin_popcount: |
| 8359 | case Builtin::BI__builtin_popcountl: |
| 8360 | case Builtin::BI__builtin_popcountll: { |
| 8361 | APSInt Val; |
| 8362 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 8363 | return false; |
| 8364 | |
| 8365 | return Success(Val.countPopulation(), E); |
| 8366 | } |
| 8367 | |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 8368 | case Builtin::BIstrlen: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8369 | case Builtin::BIwcslen: |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 8370 | // A call to strlen is not a constant expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8371 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 8372 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8373 | << /*isConstexpr*/0 << /*isConstructor*/0 |
| 8374 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 8375 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 8376 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8377 | LLVM_FALLTHROUGH; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8378 | case Builtin::BI__builtin_strlen: |
| 8379 | case Builtin::BI__builtin_wcslen: { |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 8380 | // As an extension, we support __builtin_strlen() as a constant expression, |
| 8381 | // and support folding strlen() to a constant. |
| 8382 | LValue String; |
| 8383 | if (!EvaluatePointer(E->getArg(0), String, Info)) |
| 8384 | return false; |
| 8385 | |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8386 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
| 8387 | |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 8388 | // Fast path: if it's a string literal, search the string value. |
| 8389 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
| 8390 | String.getLValueBase().dyn_cast<const Expr *>())) { |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 8391 | // The string literal may have embedded null characters. Find the first |
| 8392 | // one and truncate there. |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 8393 | StringRef Str = S->getBytes(); |
| 8394 | int64_t Off = String.Offset.getQuantity(); |
| 8395 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8396 | S->getCharByteWidth() == 1 && |
| 8397 | // FIXME: Add fast-path for wchar_t too. |
| 8398 | Info.Ctx.hasSameUnqualifiedType(CharTy, Info.Ctx.CharTy)) { |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 8399 | Str = Str.substr(Off); |
| 8400 | |
| 8401 | StringRef::size_type Pos = Str.find(0); |
| 8402 | if (Pos != StringRef::npos) |
| 8403 | Str = Str.substr(0, Pos); |
| 8404 | |
| 8405 | return Success(Str.size(), E); |
| 8406 | } |
| 8407 | |
| 8408 | // Fall through to slow path to issue appropriate diagnostic. |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 8409 | } |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 8410 | |
| 8411 | // 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] | 8412 | for (uint64_t Strlen = 0; /**/; ++Strlen) { |
| 8413 | APValue Char; |
| 8414 | if (!handleLValueToRValueConversion(Info, E, CharTy, String, Char) || |
| 8415 | !Char.isInt()) |
| 8416 | return false; |
| 8417 | if (!Char.getInt()) |
| 8418 | return Success(Strlen, E); |
| 8419 | if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1)) |
| 8420 | return false; |
| 8421 | } |
| 8422 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8423 | |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8424 | case Builtin::BIstrcmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8425 | case Builtin::BIwcscmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8426 | case Builtin::BIstrncmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8427 | case Builtin::BIwcsncmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8428 | case Builtin::BImemcmp: |
Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 8429 | case Builtin::BIbcmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8430 | case Builtin::BIwmemcmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8431 | // A call to strlen is not a constant expression. |
| 8432 | if (Info.getLangOpts().CPlusPlus11) |
| 8433 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| 8434 | << /*isConstexpr*/0 << /*isConstructor*/0 |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8435 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8436 | else |
| 8437 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8438 | LLVM_FALLTHROUGH; |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8439 | case Builtin::BI__builtin_strcmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8440 | case Builtin::BI__builtin_wcscmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8441 | case Builtin::BI__builtin_strncmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8442 | case Builtin::BI__builtin_wcsncmp: |
| 8443 | case Builtin::BI__builtin_memcmp: |
Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 8444 | case Builtin::BI__builtin_bcmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8445 | case Builtin::BI__builtin_wmemcmp: { |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8446 | LValue String1, String2; |
| 8447 | if (!EvaluatePointer(E->getArg(0), String1, Info) || |
| 8448 | !EvaluatePointer(E->getArg(1), String2, Info)) |
| 8449 | return false; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8450 | |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8451 | uint64_t MaxLength = uint64_t(-1); |
| 8452 | if (BuiltinOp != Builtin::BIstrcmp && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8453 | BuiltinOp != Builtin::BIwcscmp && |
| 8454 | BuiltinOp != Builtin::BI__builtin_strcmp && |
| 8455 | BuiltinOp != Builtin::BI__builtin_wcscmp) { |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8456 | APSInt N; |
| 8457 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
| 8458 | return false; |
| 8459 | MaxLength = N.getExtValue(); |
| 8460 | } |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 8461 | |
| 8462 | // Empty substrings compare equal by definition. |
| 8463 | if (MaxLength == 0u) |
| 8464 | return Success(0, E); |
| 8465 | |
| 8466 | if (!String1.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
| 8467 | !String2.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
| 8468 | String1.Designator.Invalid || String2.Designator.Invalid) |
| 8469 | return false; |
| 8470 | |
| 8471 | QualType CharTy1 = String1.Designator.getType(Info.Ctx); |
| 8472 | QualType CharTy2 = String2.Designator.getType(Info.Ctx); |
| 8473 | |
| 8474 | bool IsRawByte = BuiltinOp == Builtin::BImemcmp || |
Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 8475 | BuiltinOp == Builtin::BIbcmp || |
| 8476 | BuiltinOp == Builtin::BI__builtin_memcmp || |
| 8477 | BuiltinOp == Builtin::BI__builtin_bcmp; |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 8478 | |
| 8479 | assert(IsRawByte || |
| 8480 | (Info.Ctx.hasSameUnqualifiedType( |
| 8481 | CharTy1, E->getArg(0)->getType()->getPointeeType()) && |
| 8482 | Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))); |
| 8483 | |
| 8484 | const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) { |
| 8485 | return handleLValueToRValueConversion(Info, E, CharTy1, String1, Char1) && |
| 8486 | handleLValueToRValueConversion(Info, E, CharTy2, String2, Char2) && |
| 8487 | Char1.isInt() && Char2.isInt(); |
| 8488 | }; |
| 8489 | const auto &AdvanceElems = [&] { |
| 8490 | return HandleLValueArrayAdjustment(Info, E, String1, CharTy1, 1) && |
| 8491 | HandleLValueArrayAdjustment(Info, E, String2, CharTy2, 1); |
| 8492 | }; |
| 8493 | |
| 8494 | if (IsRawByte) { |
| 8495 | uint64_t BytesRemaining = MaxLength; |
| 8496 | // Pointers to const void may point to objects of incomplete type. |
| 8497 | if (CharTy1->isIncompleteType()) { |
| 8498 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy1; |
| 8499 | return false; |
| 8500 | } |
| 8501 | if (CharTy2->isIncompleteType()) { |
| 8502 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy2; |
| 8503 | return false; |
| 8504 | } |
| 8505 | uint64_t CharTy1Width{Info.Ctx.getTypeSize(CharTy1)}; |
| 8506 | CharUnits CharTy1Size = Info.Ctx.toCharUnitsFromBits(CharTy1Width); |
| 8507 | // Give up on comparing between elements with disparate widths. |
| 8508 | if (CharTy1Size != Info.Ctx.getTypeSizeInChars(CharTy2)) |
| 8509 | return false; |
| 8510 | uint64_t BytesPerElement = CharTy1Size.getQuantity(); |
| 8511 | assert(BytesRemaining && "BytesRemaining should not be zero: the " |
| 8512 | "following loop considers at least one element"); |
| 8513 | while (true) { |
| 8514 | APValue Char1, Char2; |
| 8515 | if (!ReadCurElems(Char1, Char2)) |
| 8516 | return false; |
| 8517 | // We have compatible in-memory widths, but a possible type and |
| 8518 | // (for `bool`) internal representation mismatch. |
| 8519 | // Assuming two's complement representation, including 0 for `false` and |
| 8520 | // 1 for `true`, we can check an appropriate number of elements for |
| 8521 | // equality even if they are not byte-sized. |
| 8522 | APSInt Char1InMem = Char1.getInt().extOrTrunc(CharTy1Width); |
| 8523 | APSInt Char2InMem = Char2.getInt().extOrTrunc(CharTy1Width); |
| 8524 | if (Char1InMem.ne(Char2InMem)) { |
| 8525 | // If the elements are byte-sized, then we can produce a three-way |
| 8526 | // comparison result in a straightforward manner. |
| 8527 | if (BytesPerElement == 1u) { |
| 8528 | // memcmp always compares unsigned chars. |
| 8529 | return Success(Char1InMem.ult(Char2InMem) ? -1 : 1, E); |
| 8530 | } |
| 8531 | // The result is byte-order sensitive, and we have multibyte elements. |
| 8532 | // FIXME: We can compare the remaining bytes in the correct order. |
| 8533 | return false; |
| 8534 | } |
| 8535 | if (!AdvanceElems()) |
| 8536 | return false; |
| 8537 | if (BytesRemaining <= BytesPerElement) |
| 8538 | break; |
| 8539 | BytesRemaining -= BytesPerElement; |
| 8540 | } |
| 8541 | // Enough elements are equal to account for the memcmp limit. |
| 8542 | return Success(0, E); |
| 8543 | } |
| 8544 | |
Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 8545 | bool StopAtNull = |
| 8546 | (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp && |
| 8547 | BuiltinOp != Builtin::BIwmemcmp && |
| 8548 | BuiltinOp != Builtin::BI__builtin_memcmp && |
| 8549 | BuiltinOp != Builtin::BI__builtin_bcmp && |
| 8550 | BuiltinOp != Builtin::BI__builtin_wmemcmp); |
Benjamin Kramer | 33b7092 | 2018-04-23 22:04:34 +0000 | [diff] [blame] | 8551 | bool IsWide = BuiltinOp == Builtin::BIwcscmp || |
| 8552 | BuiltinOp == Builtin::BIwcsncmp || |
| 8553 | BuiltinOp == Builtin::BIwmemcmp || |
| 8554 | BuiltinOp == Builtin::BI__builtin_wcscmp || |
| 8555 | BuiltinOp == Builtin::BI__builtin_wcsncmp || |
| 8556 | BuiltinOp == Builtin::BI__builtin_wmemcmp; |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 8557 | |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8558 | for (; MaxLength; --MaxLength) { |
| 8559 | APValue Char1, Char2; |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 8560 | if (!ReadCurElems(Char1, Char2)) |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8561 | return false; |
Benjamin Kramer | 33b7092 | 2018-04-23 22:04:34 +0000 | [diff] [blame] | 8562 | if (Char1.getInt() != Char2.getInt()) { |
| 8563 | if (IsWide) // wmemcmp compares with wchar_t signedness. |
| 8564 | return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E); |
| 8565 | // memcmp always compares unsigned chars. |
| 8566 | return Success(Char1.getInt().ult(Char2.getInt()) ? -1 : 1, E); |
| 8567 | } |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8568 | if (StopAtNull && !Char1.getInt()) |
| 8569 | return Success(0, E); |
| 8570 | assert(!(StopAtNull && !Char2.getInt())); |
Hubert Tong | 147b743 | 2018-12-12 16:53:43 +0000 | [diff] [blame] | 8571 | if (!AdvanceElems()) |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8572 | return false; |
| 8573 | } |
| 8574 | // We hit the strncmp / memcmp limit. |
| 8575 | return Success(0, E); |
| 8576 | } |
| 8577 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8578 | case Builtin::BI__atomic_always_lock_free: |
Richard Smith | b1e36c6 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 8579 | case Builtin::BI__atomic_is_lock_free: |
| 8580 | case Builtin::BI__c11_atomic_is_lock_free: { |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8581 | APSInt SizeVal; |
| 8582 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 8583 | return false; |
| 8584 | |
| 8585 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 8586 | // of two less than the maximum inline atomic width, we know it is |
| 8587 | // lock-free. If the size isn't a power of two, or greater than the |
| 8588 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 8589 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 8590 | // the answer can only be determined at runtime; for example, 16-byte |
| 8591 | // atomics have lock-free implementations on some, but not all, |
| 8592 | // x86-64 processors. |
| 8593 | |
| 8594 | // Check power-of-two. |
| 8595 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8596 | if (Size.isPowerOfTwo()) { |
| 8597 | // Check against inlining width. |
| 8598 | unsigned InlineWidthBits = |
| 8599 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 8600 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 8601 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 8602 | Size == CharUnits::One() || |
| 8603 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 8604 | Expr::NPC_NeverValueDependent)) |
| 8605 | // OK, we will inline appropriately-aligned operations of this size, |
| 8606 | // and _Atomic(T) is appropriately-aligned. |
| 8607 | return Success(1, E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8608 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8609 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 8610 | castAs<PointerType>()->getPointeeType(); |
| 8611 | if (!PointeeType->isIncompleteType() && |
| 8612 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 8613 | // OK, we will inline operations on this object. |
| 8614 | return Success(1, E); |
| 8615 | } |
| 8616 | } |
| 8617 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8618 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8619 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 8620 | Success(0, E) : Error(E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8621 | } |
Jonas Hahnfeld | 23604a8 | 2017-10-17 14:28:14 +0000 | [diff] [blame] | 8622 | case Builtin::BIomp_is_initial_device: |
| 8623 | // We can decide statically which value the runtime would return if called. |
| 8624 | return Success(Info.getLangOpts().OpenMPIsDevice ? 0 : 1, E); |
Erich Keane | 0095827 | 2018-06-13 20:43:27 +0000 | [diff] [blame] | 8625 | case Builtin::BI__builtin_add_overflow: |
| 8626 | case Builtin::BI__builtin_sub_overflow: |
| 8627 | case Builtin::BI__builtin_mul_overflow: |
| 8628 | case Builtin::BI__builtin_sadd_overflow: |
| 8629 | case Builtin::BI__builtin_uadd_overflow: |
| 8630 | case Builtin::BI__builtin_uaddl_overflow: |
| 8631 | case Builtin::BI__builtin_uaddll_overflow: |
| 8632 | case Builtin::BI__builtin_usub_overflow: |
| 8633 | case Builtin::BI__builtin_usubl_overflow: |
| 8634 | case Builtin::BI__builtin_usubll_overflow: |
| 8635 | case Builtin::BI__builtin_umul_overflow: |
| 8636 | case Builtin::BI__builtin_umull_overflow: |
| 8637 | case Builtin::BI__builtin_umulll_overflow: |
| 8638 | case Builtin::BI__builtin_saddl_overflow: |
| 8639 | case Builtin::BI__builtin_saddll_overflow: |
| 8640 | case Builtin::BI__builtin_ssub_overflow: |
| 8641 | case Builtin::BI__builtin_ssubl_overflow: |
| 8642 | case Builtin::BI__builtin_ssubll_overflow: |
| 8643 | case Builtin::BI__builtin_smul_overflow: |
| 8644 | case Builtin::BI__builtin_smull_overflow: |
| 8645 | case Builtin::BI__builtin_smulll_overflow: { |
| 8646 | LValue ResultLValue; |
| 8647 | APSInt LHS, RHS; |
| 8648 | |
| 8649 | QualType ResultType = E->getArg(2)->getType()->getPointeeType(); |
| 8650 | if (!EvaluateInteger(E->getArg(0), LHS, Info) || |
| 8651 | !EvaluateInteger(E->getArg(1), RHS, Info) || |
| 8652 | !EvaluatePointer(E->getArg(2), ResultLValue, Info)) |
| 8653 | return false; |
| 8654 | |
| 8655 | APSInt Result; |
| 8656 | bool DidOverflow = false; |
| 8657 | |
| 8658 | // If the types don't have to match, enlarge all 3 to the largest of them. |
| 8659 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
| 8660 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
| 8661 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
| 8662 | bool IsSigned = LHS.isSigned() || RHS.isSigned() || |
| 8663 | ResultType->isSignedIntegerOrEnumerationType(); |
| 8664 | bool AllSigned = LHS.isSigned() && RHS.isSigned() && |
| 8665 | ResultType->isSignedIntegerOrEnumerationType(); |
| 8666 | uint64_t LHSSize = LHS.getBitWidth(); |
| 8667 | uint64_t RHSSize = RHS.getBitWidth(); |
| 8668 | uint64_t ResultSize = Info.Ctx.getTypeSize(ResultType); |
| 8669 | uint64_t MaxBits = std::max(std::max(LHSSize, RHSSize), ResultSize); |
| 8670 | |
| 8671 | // Add an additional bit if the signedness isn't uniformly agreed to. We |
| 8672 | // could do this ONLY if there is a signed and an unsigned that both have |
| 8673 | // MaxBits, but the code to check that is pretty nasty. The issue will be |
| 8674 | // caught in the shrink-to-result later anyway. |
| 8675 | if (IsSigned && !AllSigned) |
| 8676 | ++MaxBits; |
| 8677 | |
| 8678 | LHS = APSInt(IsSigned ? LHS.sextOrSelf(MaxBits) : LHS.zextOrSelf(MaxBits), |
| 8679 | !IsSigned); |
| 8680 | RHS = APSInt(IsSigned ? RHS.sextOrSelf(MaxBits) : RHS.zextOrSelf(MaxBits), |
| 8681 | !IsSigned); |
| 8682 | Result = APSInt(MaxBits, !IsSigned); |
| 8683 | } |
| 8684 | |
| 8685 | // Find largest int. |
| 8686 | switch (BuiltinOp) { |
| 8687 | default: |
| 8688 | llvm_unreachable("Invalid value for BuiltinOp"); |
| 8689 | case Builtin::BI__builtin_add_overflow: |
| 8690 | case Builtin::BI__builtin_sadd_overflow: |
| 8691 | case Builtin::BI__builtin_saddl_overflow: |
| 8692 | case Builtin::BI__builtin_saddll_overflow: |
| 8693 | case Builtin::BI__builtin_uadd_overflow: |
| 8694 | case Builtin::BI__builtin_uaddl_overflow: |
| 8695 | case Builtin::BI__builtin_uaddll_overflow: |
| 8696 | Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow) |
| 8697 | : LHS.uadd_ov(RHS, DidOverflow); |
| 8698 | break; |
| 8699 | case Builtin::BI__builtin_sub_overflow: |
| 8700 | case Builtin::BI__builtin_ssub_overflow: |
| 8701 | case Builtin::BI__builtin_ssubl_overflow: |
| 8702 | case Builtin::BI__builtin_ssubll_overflow: |
| 8703 | case Builtin::BI__builtin_usub_overflow: |
| 8704 | case Builtin::BI__builtin_usubl_overflow: |
| 8705 | case Builtin::BI__builtin_usubll_overflow: |
| 8706 | Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow) |
| 8707 | : LHS.usub_ov(RHS, DidOverflow); |
| 8708 | break; |
| 8709 | case Builtin::BI__builtin_mul_overflow: |
| 8710 | case Builtin::BI__builtin_smul_overflow: |
| 8711 | case Builtin::BI__builtin_smull_overflow: |
| 8712 | case Builtin::BI__builtin_smulll_overflow: |
| 8713 | case Builtin::BI__builtin_umul_overflow: |
| 8714 | case Builtin::BI__builtin_umull_overflow: |
| 8715 | case Builtin::BI__builtin_umulll_overflow: |
| 8716 | Result = LHS.isSigned() ? LHS.smul_ov(RHS, DidOverflow) |
| 8717 | : LHS.umul_ov(RHS, DidOverflow); |
| 8718 | break; |
| 8719 | } |
| 8720 | |
| 8721 | // In the case where multiple sizes are allowed, truncate and see if |
| 8722 | // the values are the same. |
| 8723 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
| 8724 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
| 8725 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
| 8726 | // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead, |
| 8727 | // since it will give us the behavior of a TruncOrSelf in the case where |
| 8728 | // its parameter <= its size. We previously set Result to be at least the |
| 8729 | // type-size of the result, so getTypeSize(ResultType) <= Result.BitWidth |
| 8730 | // will work exactly like TruncOrSelf. |
| 8731 | APSInt Temp = Result.extOrTrunc(Info.Ctx.getTypeSize(ResultType)); |
| 8732 | Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType()); |
| 8733 | |
| 8734 | if (!APSInt::isSameValue(Temp, Result)) |
| 8735 | DidOverflow = true; |
| 8736 | Result = Temp; |
| 8737 | } |
| 8738 | |
| 8739 | APValue APV{Result}; |
Erich Keane | cb54964 | 2018-07-05 15:52:58 +0000 | [diff] [blame] | 8740 | if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) |
| 8741 | return false; |
Erich Keane | 0095827 | 2018-06-13 20:43:27 +0000 | [diff] [blame] | 8742 | return Success(DidOverflow, E); |
| 8743 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 8744 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 8745 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 8746 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8747 | /// Determine whether this is a pointer past the end of the complete |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 8748 | /// object referred to by the lvalue. |
| 8749 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
| 8750 | const LValue &LV) { |
| 8751 | // A null pointer can be viewed as being "past the end" but we don't |
| 8752 | // choose to look at it that way here. |
| 8753 | if (!LV.getLValueBase()) |
| 8754 | return false; |
| 8755 | |
| 8756 | // If the designator is valid and refers to a subobject, we're not pointing |
| 8757 | // past the end. |
| 8758 | if (!LV.getLValueDesignator().Invalid && |
| 8759 | !LV.getLValueDesignator().isOnePastTheEnd()) |
| 8760 | return false; |
| 8761 | |
David Majnemer | c378ca5 | 2015-08-29 08:32:55 +0000 | [diff] [blame] | 8762 | // A pointer to an incomplete type might be past-the-end if the type's size is |
| 8763 | // zero. We cannot tell because the type is incomplete. |
| 8764 | QualType Ty = getType(LV.getLValueBase()); |
| 8765 | if (Ty->isIncompleteType()) |
| 8766 | return true; |
| 8767 | |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 8768 | // We're a past-the-end pointer if we point to the byte after the object, |
| 8769 | // no matter what our type or path is. |
David Majnemer | c378ca5 | 2015-08-29 08:32:55 +0000 | [diff] [blame] | 8770 | auto Size = Ctx.getTypeSizeInChars(Ty); |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 8771 | return LV.getLValueOffset() == Size; |
| 8772 | } |
| 8773 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8774 | namespace { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8775 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8776 | /// Data recursive integer evaluator of certain binary operators. |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8777 | /// |
| 8778 | /// We use a data recursive algorithm for binary operators so that we are able |
| 8779 | /// to handle extreme cases of chained binary operators without causing stack |
| 8780 | /// overflow. |
| 8781 | class DataRecursiveIntBinOpEvaluator { |
| 8782 | struct EvalResult { |
| 8783 | APValue Val; |
| 8784 | bool Failed; |
| 8785 | |
| 8786 | EvalResult() : Failed(false) { } |
| 8787 | |
| 8788 | void swap(EvalResult &RHS) { |
| 8789 | Val.swap(RHS.Val); |
| 8790 | Failed = RHS.Failed; |
| 8791 | RHS.Failed = false; |
| 8792 | } |
| 8793 | }; |
| 8794 | |
| 8795 | struct Job { |
| 8796 | const Expr *E; |
| 8797 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 8798 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 8799 | |
David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 8800 | Job() = default; |
Benjamin Kramer | 33e9760 | 2016-10-21 18:55:07 +0000 | [diff] [blame] | 8801 | Job(Job &&) = default; |
David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 8802 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8803 | void startSpeculativeEval(EvalInfo &Info) { |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8804 | SpecEvalRAII = SpeculativeEvaluationRAII(Info); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8805 | } |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8806 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8807 | private: |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8808 | SpeculativeEvaluationRAII SpecEvalRAII; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8809 | }; |
| 8810 | |
| 8811 | SmallVector<Job, 16> Queue; |
| 8812 | |
| 8813 | IntExprEvaluator &IntEval; |
| 8814 | EvalInfo &Info; |
| 8815 | APValue &FinalResult; |
| 8816 | |
| 8817 | public: |
| 8818 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 8819 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 8820 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8821 | /// 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] | 8822 | /// data recursively. |
| 8823 | /// We handle binary operators that are comma, logical, or that have operands |
| 8824 | /// with integral or enumeration type. |
| 8825 | static bool shouldEnqueue(const BinaryOperator *E) { |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8826 | return E->getOpcode() == BO_Comma || E->isLogicalOp() || |
| 8827 | (E->isRValue() && E->getType()->isIntegralOrEnumerationType() && |
Richard Smith | 3a09d8b | 2016-06-04 00:22:31 +0000 | [diff] [blame] | 8828 | E->getLHS()->getType()->isIntegralOrEnumerationType() && |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8829 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 8830 | } |
| 8831 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8832 | bool Traverse(const BinaryOperator *E) { |
| 8833 | enqueue(E); |
| 8834 | EvalResult PrevResult; |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8835 | while (!Queue.empty()) |
| 8836 | process(PrevResult); |
| 8837 | |
| 8838 | if (PrevResult.Failed) return false; |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8839 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8840 | FinalResult.swap(PrevResult.Val); |
| 8841 | return true; |
| 8842 | } |
| 8843 | |
| 8844 | private: |
| 8845 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 8846 | return IntEval.Success(Value, E, Result); |
| 8847 | } |
| 8848 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 8849 | return IntEval.Success(Value, E, Result); |
| 8850 | } |
| 8851 | bool Error(const Expr *E) { |
| 8852 | return IntEval.Error(E); |
| 8853 | } |
| 8854 | bool Error(const Expr *E, diag::kind D) { |
| 8855 | return IntEval.Error(E, D); |
| 8856 | } |
| 8857 | |
| 8858 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 8859 | return Info.CCEDiag(E, D); |
| 8860 | } |
| 8861 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8862 | // Returns true if visiting the RHS is necessary, false otherwise. |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8863 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8864 | bool &SuppressRHSDiags); |
| 8865 | |
| 8866 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 8867 | const BinaryOperator *E, APValue &Result); |
| 8868 | |
| 8869 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 8870 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 8871 | if (Result.Failed) |
| 8872 | Result.Val = APValue(); |
| 8873 | } |
| 8874 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8875 | void process(EvalResult &Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8876 | |
| 8877 | void enqueue(const Expr *E) { |
| 8878 | E = E->IgnoreParens(); |
| 8879 | Queue.resize(Queue.size()+1); |
| 8880 | Queue.back().E = E; |
| 8881 | Queue.back().Kind = Job::AnyExprKind; |
| 8882 | } |
| 8883 | }; |
| 8884 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8885 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8886 | |
| 8887 | bool DataRecursiveIntBinOpEvaluator:: |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8888 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8889 | bool &SuppressRHSDiags) { |
| 8890 | if (E->getOpcode() == BO_Comma) { |
| 8891 | // Ignore LHS but note if we could not evaluate it. |
| 8892 | if (LHSResult.Failed) |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8893 | return Info.noteSideEffect(); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8894 | return true; |
| 8895 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8896 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8897 | if (E->isLogicalOp()) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8898 | bool LHSAsBool; |
| 8899 | if (!LHSResult.Failed && HandleConversionToBool(LHSResult.Val, LHSAsBool)) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8900 | // We were able to evaluate the LHS, see if we can get away with not |
| 8901 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8902 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
| 8903 | Success(LHSAsBool, E, LHSResult.Val); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8904 | return false; // Ignore RHS |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8905 | } |
| 8906 | } else { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8907 | LHSResult.Failed = true; |
| 8908 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8909 | // 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] | 8910 | // might have had side effects. |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8911 | if (!Info.noteSideEffect()) |
| 8912 | return false; |
| 8913 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8914 | // We can't evaluate the LHS; however, sometimes the result |
| 8915 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 8916 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 8917 | SuppressRHSDiags = true; |
| 8918 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8919 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8920 | return true; |
| 8921 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8922 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8923 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 8924 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8925 | |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8926 | if (LHSResult.Failed && !Info.noteFailure()) |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8927 | return false; // Ignore RHS; |
| 8928 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8929 | return true; |
| 8930 | } |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8931 | |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 8932 | static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index, |
| 8933 | bool IsSub) { |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 8934 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
| 8935 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
| 8936 | // offsets. |
| 8937 | assert(!LVal.hasLValuePath() && "have designator for integer lvalue"); |
| 8938 | CharUnits &Offset = LVal.getLValueOffset(); |
| 8939 | uint64_t Offset64 = Offset.getQuantity(); |
| 8940 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
| 8941 | Offset = CharUnits::fromQuantity(IsSub ? Offset64 - Index64 |
| 8942 | : Offset64 + Index64); |
| 8943 | } |
| 8944 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8945 | bool DataRecursiveIntBinOpEvaluator:: |
| 8946 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 8947 | const BinaryOperator *E, APValue &Result) { |
| 8948 | if (E->getOpcode() == BO_Comma) { |
| 8949 | if (RHSResult.Failed) |
| 8950 | return false; |
| 8951 | Result = RHSResult.Val; |
| 8952 | return true; |
| 8953 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8954 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8955 | if (E->isLogicalOp()) { |
| 8956 | bool lhsResult, rhsResult; |
| 8957 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 8958 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8959 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8960 | if (LHSIsOK) { |
| 8961 | if (RHSIsOK) { |
| 8962 | if (E->getOpcode() == BO_LOr) |
| 8963 | return Success(lhsResult || rhsResult, E, Result); |
| 8964 | else |
| 8965 | return Success(lhsResult && rhsResult, E, Result); |
| 8966 | } |
| 8967 | } else { |
| 8968 | if (RHSIsOK) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8969 | // We can't evaluate the LHS; however, sometimes the result |
| 8970 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 8971 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8972 | return Success(rhsResult, E, Result); |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8973 | } |
| 8974 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8975 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8976 | return false; |
| 8977 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8978 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8979 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 8980 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8981 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8982 | if (LHSResult.Failed || RHSResult.Failed) |
| 8983 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8984 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8985 | const APValue &LHSVal = LHSResult.Val; |
| 8986 | const APValue &RHSVal = RHSResult.Val; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8987 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8988 | // Handle cases like (unsigned long)&a + 4. |
| 8989 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 8990 | Result = LHSVal; |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 8991 | addOrSubLValueAsInteger(Result, RHSVal.getInt(), E->getOpcode() == BO_Sub); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8992 | return true; |
| 8993 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8994 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8995 | // Handle cases like 4 + (unsigned long)&a |
| 8996 | if (E->getOpcode() == BO_Add && |
| 8997 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 8998 | Result = RHSVal; |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 8999 | addOrSubLValueAsInteger(Result, LHSVal.getInt(), /*IsSub*/false); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9000 | return true; |
| 9001 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9002 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9003 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 9004 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 9005 | if (!LHSVal.getLValueOffset().isZero() || |
| 9006 | !RHSVal.getLValueOffset().isZero()) |
| 9007 | return false; |
| 9008 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 9009 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 9010 | if (!LHSExpr || !RHSExpr) |
| 9011 | return false; |
| 9012 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 9013 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 9014 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 9015 | return false; |
| 9016 | // Make sure both labels come from the same function. |
| 9017 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 9018 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 9019 | return false; |
| 9020 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 9021 | return true; |
| 9022 | } |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 9023 | |
| 9024 | // All the remaining cases expect both operands to be an integer |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9025 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 9026 | return Error(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 9027 | |
| 9028 | // Set up the width and signedness manually, in case it can't be deduced |
| 9029 | // from the operation we're performing. |
| 9030 | // FIXME: Don't do this in the cases where we can deduce it. |
| 9031 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
| 9032 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
| 9033 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
| 9034 | RHSVal.getInt(), Value)) |
| 9035 | return false; |
| 9036 | return Success(Value, E, Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9037 | } |
| 9038 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 9039 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9040 | Job &job = Queue.back(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9041 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9042 | switch (job.Kind) { |
| 9043 | case Job::AnyExprKind: { |
| 9044 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 9045 | if (shouldEnqueue(Bop)) { |
| 9046 | job.Kind = Job::BinOpKind; |
| 9047 | enqueue(Bop->getLHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 9048 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9049 | } |
| 9050 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9051 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9052 | EvaluateExpr(job.E, Result); |
| 9053 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 9054 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9055 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9056 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9057 | case Job::BinOpKind: { |
| 9058 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9059 | bool SuppressRHSDiags = false; |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 9060 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9061 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 9062 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9063 | } |
| 9064 | if (SuppressRHSDiags) |
| 9065 | job.startSpeculativeEval(Info); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 9066 | job.LHSResult.swap(Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9067 | job.Kind = Job::BinOpVisitedLHSKind; |
| 9068 | enqueue(Bop->getRHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 9069 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9070 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9071 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9072 | case Job::BinOpVisitedLHSKind: { |
| 9073 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 9074 | EvalResult RHS; |
| 9075 | RHS.swap(Result); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 9076 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9077 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 9078 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9079 | } |
| 9080 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9081 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9082 | llvm_unreachable("Invalid Job::Kind!"); |
| 9083 | } |
| 9084 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 9085 | namespace { |
| 9086 | /// Used when we determine that we should fail, but can keep evaluating prior to |
| 9087 | /// noting that we had a failure. |
| 9088 | class DelayedNoteFailureRAII { |
| 9089 | EvalInfo &Info; |
| 9090 | bool NoteFailure; |
| 9091 | |
| 9092 | public: |
| 9093 | DelayedNoteFailureRAII(EvalInfo &Info, bool NoteFailure = true) |
| 9094 | : Info(Info), NoteFailure(NoteFailure) {} |
| 9095 | ~DelayedNoteFailureRAII() { |
| 9096 | if (NoteFailure) { |
| 9097 | bool ContinueAfterFailure = Info.noteFailure(); |
| 9098 | (void)ContinueAfterFailure; |
| 9099 | assert(ContinueAfterFailure && |
| 9100 | "Shouldn't have kept evaluating on failure."); |
| 9101 | } |
| 9102 | } |
| 9103 | }; |
| 9104 | } |
| 9105 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9106 | template <class SuccessCB, class AfterCB> |
| 9107 | static bool |
| 9108 | EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, |
| 9109 | SuccessCB &&Success, AfterCB &&DoAfter) { |
| 9110 | assert(E->isComparisonOp() && "expected comparison operator"); |
| 9111 | assert((E->getOpcode() == BO_Cmp || |
| 9112 | E->getType()->isIntegralOrEnumerationType()) && |
| 9113 | "unsupported binary expression evaluation"); |
| 9114 | auto Error = [&](const Expr *E) { |
| 9115 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| 9116 | return false; |
| 9117 | }; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9118 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9119 | using CCR = ComparisonCategoryResult; |
| 9120 | bool IsRelational = E->isRelationalOp(); |
| 9121 | bool IsEquality = E->isEqualityOp(); |
| 9122 | if (E->getOpcode() == BO_Cmp) { |
| 9123 | const ComparisonCategoryInfo &CmpInfo = |
| 9124 | Info.Ctx.CompCategories.getInfoForType(E->getType()); |
| 9125 | IsRelational = CmpInfo.isOrdered(); |
| 9126 | IsEquality = CmpInfo.isEquality(); |
| 9127 | } |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 9128 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 9129 | QualType LHSTy = E->getLHS()->getType(); |
| 9130 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 9131 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9132 | if (LHSTy->isIntegralOrEnumerationType() && |
| 9133 | RHSTy->isIntegralOrEnumerationType()) { |
| 9134 | APSInt LHS, RHS; |
| 9135 | bool LHSOK = EvaluateInteger(E->getLHS(), LHS, Info); |
| 9136 | if (!LHSOK && !Info.noteFailure()) |
| 9137 | return false; |
| 9138 | if (!EvaluateInteger(E->getRHS(), RHS, Info) || !LHSOK) |
| 9139 | return false; |
| 9140 | if (LHS < RHS) |
| 9141 | return Success(CCR::Less, E); |
| 9142 | if (LHS > RHS) |
| 9143 | return Success(CCR::Greater, E); |
| 9144 | return Success(CCR::Equal, E); |
| 9145 | } |
| 9146 | |
Leonard Chan | ce1d4f1 | 2019-02-21 20:50:09 +0000 | [diff] [blame] | 9147 | if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) { |
| 9148 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHSTy)); |
| 9149 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHSTy)); |
| 9150 | |
| 9151 | bool LHSOK = EvaluateFixedPointOrInteger(E->getLHS(), LHSFX, Info); |
| 9152 | if (!LHSOK && !Info.noteFailure()) |
| 9153 | return false; |
| 9154 | if (!EvaluateFixedPointOrInteger(E->getRHS(), RHSFX, Info) || !LHSOK) |
| 9155 | return false; |
| 9156 | if (LHSFX < RHSFX) |
| 9157 | return Success(CCR::Less, E); |
| 9158 | if (LHSFX > RHSFX) |
| 9159 | return Success(CCR::Greater, E); |
| 9160 | return Success(CCR::Equal, E); |
| 9161 | } |
| 9162 | |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 9163 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9164 | ComplexValue LHS, RHS; |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 9165 | bool LHSOK; |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 9166 | if (E->isAssignmentOp()) { |
| 9167 | LValue LV; |
| 9168 | EvaluateLValue(E->getLHS(), LV, Info); |
| 9169 | LHSOK = false; |
| 9170 | } else if (LHSTy->isRealFloatingType()) { |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 9171 | LHSOK = EvaluateFloat(E->getLHS(), LHS.FloatReal, Info); |
| 9172 | if (LHSOK) { |
| 9173 | LHS.makeComplexFloat(); |
| 9174 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
| 9175 | } |
| 9176 | } else { |
| 9177 | LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 9178 | } |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 9179 | if (!LHSOK && !Info.noteFailure()) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 9180 | return false; |
| 9181 | |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 9182 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 9183 | if (!EvaluateFloat(E->getRHS(), RHS.FloatReal, Info) || !LHSOK) |
| 9184 | return false; |
| 9185 | RHS.makeComplexFloat(); |
| 9186 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
| 9187 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 9188 | return false; |
| 9189 | |
| 9190 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9191 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 9192 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9193 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 9194 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9195 | bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual; |
| 9196 | return Success(IsEqual ? CCR::Equal : CCR::Nonequal, E); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 9197 | } else { |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9198 | assert(IsEquality && "invalid complex comparison"); |
| 9199 | bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 9200 | LHS.getComplexIntImag() == RHS.getComplexIntImag(); |
| 9201 | return Success(IsEqual ? CCR::Equal : CCR::Nonequal, E); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 9202 | } |
| 9203 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9204 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 9205 | if (LHSTy->isRealFloatingType() && |
| 9206 | RHSTy->isRealFloatingType()) { |
| 9207 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9208 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9209 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 9210 | if (!LHSOK && !Info.noteFailure()) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 9211 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9212 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9213 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 9214 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9215 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9216 | assert(E->isComparisonOp() && "Invalid binary operator!"); |
| 9217 | auto GetCmpRes = [&]() { |
| 9218 | switch (LHS.compare(RHS)) { |
| 9219 | case APFloat::cmpEqual: |
| 9220 | return CCR::Equal; |
| 9221 | case APFloat::cmpLessThan: |
| 9222 | return CCR::Less; |
| 9223 | case APFloat::cmpGreaterThan: |
| 9224 | return CCR::Greater; |
| 9225 | case APFloat::cmpUnordered: |
| 9226 | return CCR::Unordered; |
| 9227 | } |
Simon Pilgrim | 3366dcf | 2018-05-08 09:40:32 +0000 | [diff] [blame] | 9228 | llvm_unreachable("Unrecognised APFloat::cmpResult enum"); |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9229 | }; |
| 9230 | return Success(GetCmpRes(), E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 9231 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9232 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 9233 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9234 | LValue LHSValue, RHSValue; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9235 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9236 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 9237 | if (!LHSOK && !Info.noteFailure()) |
| 9238 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 9239 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9240 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 9241 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 9242 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9243 | // Reject differing bases from the normal codepath; we special-case |
| 9244 | // comparisons to null. |
| 9245 | if (!HasSameBase(LHSValue, RHSValue)) { |
| 9246 | // Inequalities and subtractions between unrelated pointers have |
| 9247 | // unspecified or undefined behavior. |
| 9248 | if (!IsEquality) |
| 9249 | return Error(E); |
| 9250 | // A constant address may compare equal to the address of a symbol. |
| 9251 | // The one exception is that address of an object cannot compare equal |
| 9252 | // to a null pointer constant. |
| 9253 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 9254 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
| 9255 | return Error(E); |
| 9256 | // It's implementation-defined whether distinct literals will have |
| 9257 | // distinct addresses. In clang, the result of such a comparison is |
| 9258 | // unspecified, so it is not a constant expression. However, we do know |
| 9259 | // that the address of a literal will be non-null. |
| 9260 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 9261 | LHSValue.Base && RHSValue.Base) |
| 9262 | return Error(E); |
| 9263 | // We can't tell whether weak symbols will end up pointing to the same |
| 9264 | // object. |
| 9265 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
| 9266 | return Error(E); |
| 9267 | // We can't compare the address of the start of one object with the |
| 9268 | // past-the-end address of another object, per C++ DR1652. |
| 9269 | if ((LHSValue.Base && LHSValue.Offset.isZero() && |
| 9270 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) || |
| 9271 | (RHSValue.Base && RHSValue.Offset.isZero() && |
| 9272 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue))) |
| 9273 | return Error(E); |
| 9274 | // We can't tell whether an object is at the same address as another |
| 9275 | // zero sized object. |
| 9276 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
| 9277 | (LHSValue.Base && isZeroSized(RHSValue))) |
| 9278 | return Error(E); |
| 9279 | return Success(CCR::Nonequal, E); |
| 9280 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 9281 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9282 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 9283 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 9284 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9285 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 9286 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 9287 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9288 | // C++11 [expr.rel]p3: |
| 9289 | // Pointers to void (after pointer conversions) can be compared, with a |
| 9290 | // result defined as follows: If both pointers represent the same |
| 9291 | // address or are both the null pointer value, the result is true if the |
| 9292 | // operator is <= or >= and false otherwise; otherwise the result is |
| 9293 | // unspecified. |
| 9294 | // We interpret this as applying to pointers to *cv* void. |
| 9295 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && IsRelational) |
| 9296 | Info.CCEDiag(E, diag::note_constexpr_void_comparison); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 9297 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9298 | // C++11 [expr.rel]p2: |
| 9299 | // - If two pointers point to non-static data members of the same object, |
| 9300 | // or to subobjects or array elements fo such members, recursively, the |
| 9301 | // pointer to the later declared member compares greater provided the |
| 9302 | // two members have the same access control and provided their class is |
| 9303 | // not a union. |
| 9304 | // [...] |
| 9305 | // - Otherwise pointer comparisons are unspecified. |
| 9306 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) { |
| 9307 | bool WasArrayIndex; |
| 9308 | unsigned Mismatch = FindDesignatorMismatch( |
| 9309 | getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex); |
| 9310 | // At the point where the designators diverge, the comparison has a |
| 9311 | // specified value if: |
| 9312 | // - we are comparing array indices |
| 9313 | // - we are comparing fields of a union, or fields with the same access |
| 9314 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 9315 | // constant expression. |
| 9316 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 9317 | Mismatch < RHSDesignator.Entries.size()) { |
| 9318 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 9319 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 9320 | if (!LF && !RF) |
| 9321 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 9322 | else if (!LF) |
| 9323 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 9324 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 9325 | << RF->getParent() << RF; |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9326 | else if (!RF) |
| 9327 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 9328 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 9329 | << LF->getParent() << LF; |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9330 | else if (!LF->getParent()->isUnion() && |
| 9331 | LF->getAccess() != RF->getAccess()) |
| 9332 | Info.CCEDiag(E, |
| 9333 | diag::note_constexpr_pointer_comparison_differing_access) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 9334 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 9335 | << LF->getParent(); |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 9336 | } |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 9337 | } |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9338 | |
| 9339 | // The comparison here must be unsigned, and performed with the same |
| 9340 | // width as the pointer. |
| 9341 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 9342 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 9343 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 9344 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 9345 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 9346 | CompareLHS &= Mask; |
| 9347 | CompareRHS &= Mask; |
| 9348 | |
| 9349 | // If there is a base and this is a relational operator, we can only |
| 9350 | // compare pointers within the object in question; otherwise, the result |
| 9351 | // depends on where the object is located in memory. |
| 9352 | if (!LHSValue.Base.isNull() && IsRelational) { |
| 9353 | QualType BaseTy = getType(LHSValue.Base); |
| 9354 | if (BaseTy->isIncompleteType()) |
| 9355 | return Error(E); |
| 9356 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 9357 | uint64_t OffsetLimit = Size.getQuantity(); |
| 9358 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 9359 | return Error(E); |
| 9360 | } |
| 9361 | |
| 9362 | if (CompareLHS < CompareRHS) |
| 9363 | return Success(CCR::Less, E); |
| 9364 | if (CompareLHS > CompareRHS) |
| 9365 | return Success(CCR::Greater, E); |
| 9366 | return Success(CCR::Equal, E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 9367 | } |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9368 | |
| 9369 | if (LHSTy->isMemberPointerType()) { |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9370 | assert(IsEquality && "unexpected member pointer operation"); |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9371 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 9372 | |
| 9373 | MemberPtr LHSValue, RHSValue; |
| 9374 | |
| 9375 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 9376 | if (!LHSOK && !Info.noteFailure()) |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9377 | return false; |
| 9378 | |
| 9379 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 9380 | return false; |
| 9381 | |
| 9382 | // C++11 [expr.eq]p2: |
| 9383 | // If both operands are null, they compare equal. Otherwise if only one is |
| 9384 | // null, they compare unequal. |
| 9385 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 9386 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9387 | return Success(Equal ? CCR::Equal : CCR::Nonequal, E); |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9388 | } |
| 9389 | |
| 9390 | // Otherwise if either is a pointer to a virtual member function, the |
| 9391 | // result is unspecified. |
| 9392 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 9393 | if (MD->isVirtual()) |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9394 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9395 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 9396 | if (MD->isVirtual()) |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9397 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9398 | |
| 9399 | // Otherwise they compare equal if and only if they would refer to the |
| 9400 | // same member of the same most derived object or the same subobject if |
| 9401 | // they were dereferenced with a hypothetical object of the associated |
| 9402 | // class type. |
| 9403 | bool Equal = LHSValue == RHSValue; |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9404 | return Success(Equal ? CCR::Equal : CCR::Nonequal, E); |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9405 | } |
| 9406 | |
Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 9407 | if (LHSTy->isNullPtrType()) { |
| 9408 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 9409 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 9410 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 9411 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 9412 | // false otherwise. |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9413 | return Success(CCR::Equal, E); |
Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 9414 | } |
| 9415 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9416 | return DoAfter(); |
| 9417 | } |
| 9418 | |
| 9419 | bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) { |
| 9420 | if (!CheckLiteralType(Info, E)) |
| 9421 | return false; |
| 9422 | |
| 9423 | auto OnSuccess = [&](ComparisonCategoryResult ResKind, |
| 9424 | const BinaryOperator *E) { |
| 9425 | // Evaluation succeeded. Lookup the information for the comparison category |
| 9426 | // type and fetch the VarDecl for the result. |
| 9427 | const ComparisonCategoryInfo &CmpInfo = |
| 9428 | Info.Ctx.CompCategories.getInfoForType(E->getType()); |
| 9429 | const VarDecl *VD = |
| 9430 | CmpInfo.getValueInfo(CmpInfo.makeWeakResult(ResKind))->VD; |
| 9431 | // Check and evaluate the result as a constant expression. |
| 9432 | LValue LV; |
| 9433 | LV.set(VD); |
| 9434 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
| 9435 | return false; |
| 9436 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
| 9437 | }; |
| 9438 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
| 9439 | return ExprEvaluatorBaseTy::VisitBinCmp(E); |
| 9440 | }); |
| 9441 | } |
| 9442 | |
| 9443 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 9444 | // We don't call noteFailure immediately because the assignment happens after |
| 9445 | // we evaluate LHS and RHS. |
| 9446 | if (!Info.keepEvaluatingAfterFailure() && E->isAssignmentOp()) |
| 9447 | return Error(E); |
| 9448 | |
| 9449 | DelayedNoteFailureRAII MaybeNoteFailureLater(Info, E->isAssignmentOp()); |
| 9450 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 9451 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
| 9452 | |
| 9453 | assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() || |
| 9454 | !E->getRHS()->getType()->isIntegralOrEnumerationType()) && |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9455 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9456 | |
| 9457 | if (E->isComparisonOp()) { |
| 9458 | // Evaluate builtin binary comparisons by evaluating them as C++2a three-way |
| 9459 | // comparisons and then translating the result. |
| 9460 | auto OnSuccess = [&](ComparisonCategoryResult ResKind, |
| 9461 | const BinaryOperator *E) { |
| 9462 | using CCR = ComparisonCategoryResult; |
| 9463 | bool IsEqual = ResKind == CCR::Equal, |
| 9464 | IsLess = ResKind == CCR::Less, |
| 9465 | IsGreater = ResKind == CCR::Greater; |
| 9466 | auto Op = E->getOpcode(); |
| 9467 | switch (Op) { |
| 9468 | default: |
| 9469 | llvm_unreachable("unsupported binary operator"); |
| 9470 | case BO_EQ: |
| 9471 | case BO_NE: |
| 9472 | return Success(IsEqual == (Op == BO_EQ), E); |
| 9473 | case BO_LT: return Success(IsLess, E); |
| 9474 | case BO_GT: return Success(IsGreater, E); |
| 9475 | case BO_LE: return Success(IsEqual || IsLess, E); |
| 9476 | case BO_GE: return Success(IsEqual || IsGreater, E); |
| 9477 | } |
| 9478 | }; |
| 9479 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
| 9480 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 9481 | }); |
| 9482 | } |
| 9483 | |
| 9484 | QualType LHSTy = E->getLHS()->getType(); |
| 9485 | QualType RHSTy = E->getRHS()->getType(); |
| 9486 | |
| 9487 | if (LHSTy->isPointerType() && RHSTy->isPointerType() && |
| 9488 | E->getOpcode() == BO_Sub) { |
| 9489 | LValue LHSValue, RHSValue; |
| 9490 | |
| 9491 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 9492 | if (!LHSOK && !Info.noteFailure()) |
| 9493 | return false; |
| 9494 | |
| 9495 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 9496 | return false; |
| 9497 | |
| 9498 | // Reject differing bases from the normal codepath; we special-case |
| 9499 | // comparisons to null. |
| 9500 | if (!HasSameBase(LHSValue, RHSValue)) { |
| 9501 | // Handle &&A - &&B. |
| 9502 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 9503 | return Error(E); |
| 9504 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>(); |
| 9505 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>(); |
| 9506 | if (!LHSExpr || !RHSExpr) |
| 9507 | return Error(E); |
| 9508 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 9509 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 9510 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 9511 | return Error(E); |
| 9512 | // Make sure both labels come from the same function. |
| 9513 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 9514 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 9515 | return Error(E); |
| 9516 | return Success(APValue(LHSAddrExpr, RHSAddrExpr), E); |
| 9517 | } |
| 9518 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 9519 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 9520 | |
| 9521 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 9522 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 9523 | |
| 9524 | // C++11 [expr.add]p6: |
| 9525 | // Unless both pointers point to elements of the same array object, or |
| 9526 | // one past the last element of the array object, the behavior is |
| 9527 | // undefined. |
| 9528 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 9529 | !AreElementsOfSameArray(getType(LHSValue.Base), LHSDesignator, |
| 9530 | RHSDesignator)) |
| 9531 | Info.CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 9532 | |
| 9533 | QualType Type = E->getLHS()->getType(); |
| 9534 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
| 9535 | |
| 9536 | CharUnits ElementSize; |
| 9537 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
| 9538 | return false; |
| 9539 | |
| 9540 | // As an extension, a type may have zero size (empty struct or union in |
| 9541 | // C, array of zero length). Pointer subtraction in such cases has |
| 9542 | // undefined behavior, so is not constant. |
| 9543 | if (ElementSize.isZero()) { |
| 9544 | Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
| 9545 | << ElementType; |
| 9546 | return false; |
| 9547 | } |
| 9548 | |
| 9549 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 9550 | // and produce incorrect results when it overflows. Such behavior |
| 9551 | // appears to be non-conforming, but is common, so perhaps we should |
| 9552 | // assume the standard intended for such cases to be undefined behavior |
| 9553 | // and check for them. |
| 9554 | |
| 9555 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 9556 | // overflow in the final conversion to ptrdiff_t. |
| 9557 | APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 9558 | APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 9559 | APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), |
| 9560 | false); |
| 9561 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 9562 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 9563 | |
| 9564 | if (Result.extend(65) != TrueResult && |
| 9565 | !HandleOverflow(Info, E, TrueResult, E->getType())) |
| 9566 | return false; |
| 9567 | return Success(Result, E); |
| 9568 | } |
| 9569 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9570 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9571 | } |
| 9572 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9573 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 9574 | /// a result as the expression's type. |
| 9575 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 9576 | const UnaryExprOrTypeTraitExpr *E) { |
| 9577 | switch(E->getKind()) { |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 9578 | case UETT_PreferredAlignOf: |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9579 | case UETT_AlignOf: { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 9580 | if (E->isArgumentType()) |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 9581 | return Success(GetAlignOfType(Info, E->getArgumentType(), E->getKind()), |
| 9582 | E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 9583 | else |
Richard Smith | 6822bd7 | 2018-10-26 19:26:45 +0000 | [diff] [blame] | 9584 | return Success(GetAlignOfExpr(Info, E->getArgumentExpr(), E->getKind()), |
| 9585 | E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 9586 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 9587 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9588 | case UETT_VecStep: { |
| 9589 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 9590 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9591 | if (Ty->isVectorType()) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 9592 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 9593 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9594 | // The vec_step built-in functions that take a 3-component |
| 9595 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 9596 | if (n == 3) |
| 9597 | n = 4; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 9598 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9599 | return Success(n, E); |
| 9600 | } else |
| 9601 | return Success(1, E); |
| 9602 | } |
| 9603 | |
| 9604 | case UETT_SizeOf: { |
| 9605 | QualType SrcTy = E->getTypeOfArgument(); |
| 9606 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 9607 | // the result is the size of the referenced type." |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9608 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 9609 | SrcTy = Ref->getPointeeType(); |
| 9610 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9611 | CharUnits Sizeof; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 9612 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9613 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9614 | return Success(Sizeof, E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9615 | } |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 9616 | case UETT_OpenMPRequiredSimdAlign: |
| 9617 | assert(E->isArgumentType()); |
| 9618 | return Success( |
| 9619 | Info.Ctx.toCharUnitsFromBits( |
| 9620 | Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType())) |
| 9621 | .getQuantity(), |
| 9622 | E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9623 | } |
| 9624 | |
| 9625 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 9626 | } |
| 9627 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9628 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9629 | CharUnits Result; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9630 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9631 | if (n == 0) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9632 | return Error(OOE); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9633 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9634 | for (unsigned i = 0; i != n; ++i) { |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 9635 | OffsetOfNode ON = OOE->getComponent(i); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9636 | switch (ON.getKind()) { |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 9637 | case OffsetOfNode::Array: { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9638 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9639 | APSInt IdxResult; |
| 9640 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 9641 | return false; |
| 9642 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 9643 | if (!AT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9644 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9645 | CurrentType = AT->getElementType(); |
| 9646 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 9647 | Result += IdxResult.getSExtValue() * ElementSize; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 9648 | break; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9649 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9650 | |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 9651 | case OffsetOfNode::Field: { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9652 | FieldDecl *MemberDecl = ON.getField(); |
| 9653 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9654 | if (!RT) |
| 9655 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9656 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 9657 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9658 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 9659 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9660 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 9661 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9662 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9663 | break; |
| 9664 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9665 | |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 9666 | case OffsetOfNode::Identifier: |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9667 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9668 | |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 9669 | case OffsetOfNode::Base: { |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9670 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 9671 | if (BaseSpec->isVirtual()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9672 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9673 | |
| 9674 | // Find the layout of the class whose base we are looking into. |
| 9675 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9676 | if (!RT) |
| 9677 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9678 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 9679 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9680 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 9681 | |
| 9682 | // Find the base class itself. |
| 9683 | CurrentType = BaseSpec->getType(); |
| 9684 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 9685 | if (!BaseRT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9686 | return Error(OOE); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9687 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9688 | // Add the offset to the base. |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 9689 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9690 | break; |
| 9691 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9692 | } |
| 9693 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9694 | return Success(Result, OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9695 | } |
| 9696 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 9697 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9698 | switch (E->getOpcode()) { |
| 9699 | default: |
| 9700 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 9701 | // See C99 6.6p3. |
| 9702 | return Error(E); |
| 9703 | case UO_Extension: |
| 9704 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 9705 | // If so, we could clear the diagnostic ID. |
| 9706 | return Visit(E->getSubExpr()); |
| 9707 | case UO_Plus: |
| 9708 | // The result is just the value. |
| 9709 | return Visit(E->getSubExpr()); |
| 9710 | case UO_Minus: { |
| 9711 | if (!Visit(E->getSubExpr())) |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 9712 | return false; |
| 9713 | if (!Result.isInt()) return Error(E); |
| 9714 | const APSInt &Value = Result.getInt(); |
| 9715 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() && |
| 9716 | !HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 9717 | E->getType())) |
| 9718 | return false; |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 9719 | return Success(-Value, E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9720 | } |
| 9721 | case UO_Not: { |
| 9722 | if (!Visit(E->getSubExpr())) |
| 9723 | return false; |
| 9724 | if (!Result.isInt()) return Error(E); |
| 9725 | return Success(~Result.getInt(), E); |
| 9726 | } |
| 9727 | case UO_LNot: { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 9728 | bool bres; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9729 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 9730 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 9731 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 9732 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9733 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9734 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9735 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9736 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 9737 | /// result type is integer. |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9738 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 9739 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 9740 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 9741 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 9742 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9743 | switch (E->getCastKind()) { |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9744 | case CK_BaseToDerived: |
| 9745 | case CK_DerivedToBase: |
| 9746 | case CK_UncheckedDerivedToBase: |
| 9747 | case CK_Dynamic: |
| 9748 | case CK_ToUnion: |
| 9749 | case CK_ArrayToPointerDecay: |
| 9750 | case CK_FunctionToPointerDecay: |
| 9751 | case CK_NullToPointer: |
| 9752 | case CK_NullToMemberPointer: |
| 9753 | case CK_BaseToDerivedMemberPointer: |
| 9754 | case CK_DerivedToBaseMemberPointer: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 9755 | case CK_ReinterpretMemberPointer: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9756 | case CK_ConstructorConversion: |
| 9757 | case CK_IntegralToPointer: |
| 9758 | case CK_ToVoid: |
| 9759 | case CK_VectorSplat: |
| 9760 | case CK_IntegralToFloating: |
| 9761 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 9762 | case CK_CPointerToObjCPointerCast: |
| 9763 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9764 | case CK_AnyPointerToBlockPointerCast: |
| 9765 | case CK_ObjCObjectLValueCast: |
| 9766 | case CK_FloatingRealToComplex: |
| 9767 | case CK_FloatingComplexToReal: |
| 9768 | case CK_FloatingComplexCast: |
| 9769 | case CK_FloatingComplexToIntegralComplex: |
| 9770 | case CK_IntegralRealToComplex: |
| 9771 | case CK_IntegralComplexCast: |
| 9772 | case CK_IntegralComplexToFloatingComplex: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 9773 | case CK_BuiltinFnToFnPtr: |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 9774 | case CK_ZeroToOCLOpaqueType: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9775 | case CK_NonAtomicToAtomic: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 9776 | case CK_AddressSpaceConversion: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 9777 | case CK_IntToOCLSampler: |
Leonard Chan | 99bda37 | 2018-10-15 16:07:02 +0000 | [diff] [blame] | 9778 | case CK_FixedPointCast: |
Leonard Chan | 8f7caae | 2019-03-06 00:28:43 +0000 | [diff] [blame] | 9779 | case CK_IntegralToFixedPoint: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9780 | llvm_unreachable("invalid cast kind for integral value"); |
| 9781 | |
Eli Friedman | 9faf2f9 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 9782 | case CK_BitCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9783 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9784 | case CK_LValueBitCast: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 9785 | case CK_ARCProduceObject: |
| 9786 | case CK_ARCConsumeObject: |
| 9787 | case CK_ARCReclaimReturnedObject: |
| 9788 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9789 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9790 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9791 | |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 9792 | case CK_UserDefinedConversion: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9793 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 9794 | case CK_AtomicToNonAtomic: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9795 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9796 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9797 | |
| 9798 | case CK_MemberPointerToBoolean: |
| 9799 | case CK_PointerToBoolean: |
| 9800 | case CK_IntegralToBoolean: |
| 9801 | case CK_FloatingToBoolean: |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 9802 | case CK_BooleanToSignedIntegral: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9803 | case CK_FloatingComplexToBoolean: |
| 9804 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9805 | bool BoolResult; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9806 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9807 | return false; |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 9808 | uint64_t IntResult = BoolResult; |
| 9809 | if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral) |
| 9810 | IntResult = (uint64_t)-1; |
| 9811 | return Success(IntResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9812 | } |
| 9813 | |
Leonard Chan | 8f7caae | 2019-03-06 00:28:43 +0000 | [diff] [blame] | 9814 | case CK_FixedPointToIntegral: { |
| 9815 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SrcType)); |
| 9816 | if (!EvaluateFixedPoint(SubExpr, Src, Info)) |
| 9817 | return false; |
| 9818 | bool Overflowed; |
| 9819 | llvm::APSInt Result = Src.convertToInt( |
| 9820 | Info.Ctx.getIntWidth(DestType), |
| 9821 | DestType->isSignedIntegerOrEnumerationType(), &Overflowed); |
| 9822 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
| 9823 | return false; |
| 9824 | return Success(Result, E); |
| 9825 | } |
| 9826 | |
Leonard Chan | b4ba467 | 2018-10-23 17:55:35 +0000 | [diff] [blame] | 9827 | case CK_FixedPointToBoolean: { |
| 9828 | // Unsigned padding does not affect this. |
| 9829 | APValue Val; |
| 9830 | if (!Evaluate(Val, Info, SubExpr)) |
| 9831 | return false; |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 9832 | return Success(Val.getFixedPoint().getBoolValue(), E); |
Leonard Chan | b4ba467 | 2018-10-23 17:55:35 +0000 | [diff] [blame] | 9833 | } |
| 9834 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9835 | case CK_IntegralCast: { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9836 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 9837 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 9838 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 9839 | if (!Result.isInt()) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 9840 | // Allow casts of address-of-label differences if they are no-ops |
| 9841 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 9842 | // be constant-evaluatable except in some narrow cases which are hard |
| 9843 | // to detect here. We let it through on the assumption the user knows |
| 9844 | // what they are doing.) |
| 9845 | if (Result.isAddrLabelDiff()) |
| 9846 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 9847 | // Only allow casts of lvalues if they are lossless. |
| 9848 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 9849 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 9850 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9851 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 9852 | Result.getInt()), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9853 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9854 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9855 | case CK_PointerToIntegral: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 9856 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 9857 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 9858 | LValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 9859 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 9860 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9861 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 9862 | if (LV.getLValueBase()) { |
| 9863 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9864 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 9865 | // narrowing back down to pointer width in subsequent integral casts. |
| 9866 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 9867 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9868 | return Error(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9869 | |
Richard Smith | cf74da7 | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 9870 | LV.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 9871 | LV.moveInto(Result); |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 9872 | return true; |
| 9873 | } |
| 9874 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 9875 | uint64_t V; |
| 9876 | if (LV.isNullPointer()) |
| 9877 | V = Info.Ctx.getTargetNullPointerValue(SrcType); |
| 9878 | else |
| 9879 | V = LV.getLValueOffset().getQuantity(); |
| 9880 | |
| 9881 | APSInt AsInt = Info.Ctx.MakeIntValue(V, SrcType); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9882 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 9883 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9884 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9885 | case CK_IntegralComplexToReal: { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9886 | ComplexValue C; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 9887 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 9888 | return false; |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9889 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 9890 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 9891 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9892 | case CK_FloatingToIntegral: { |
| 9893 | APFloat F(0.0); |
| 9894 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 9895 | return false; |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9896 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9897 | APSInt Value; |
| 9898 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 9899 | return false; |
| 9900 | return Success(Value, E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9901 | } |
| 9902 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9903 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9904 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9905 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 9906 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9907 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 9908 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9909 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9910 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 9911 | return false; |
| 9912 | if (!LV.isComplexInt()) |
| 9913 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9914 | return Success(LV.getComplexIntReal(), E); |
| 9915 | } |
| 9916 | |
| 9917 | return Visit(E->getSubExpr()); |
| 9918 | } |
| 9919 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 9920 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9921 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9922 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9923 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 9924 | return false; |
| 9925 | if (!LV.isComplexInt()) |
| 9926 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9927 | return Success(LV.getComplexIntImag(), E); |
| 9928 | } |
| 9929 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 9930 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 9931 | return Success(0, E); |
| 9932 | } |
| 9933 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 9934 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 9935 | return Success(E->getPackLength(), E); |
| 9936 | } |
| 9937 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 9938 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 9939 | return Success(E->getValue(), E); |
| 9940 | } |
| 9941 | |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9942 | bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 9943 | switch (E->getOpcode()) { |
| 9944 | default: |
| 9945 | // Invalid unary operators |
| 9946 | return Error(E); |
| 9947 | case UO_Plus: |
| 9948 | // The result is just the value. |
| 9949 | return Visit(E->getSubExpr()); |
| 9950 | case UO_Minus: { |
| 9951 | if (!Visit(E->getSubExpr())) return false; |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 9952 | if (!Result.isFixedPoint()) |
| 9953 | return Error(E); |
| 9954 | bool Overflowed; |
| 9955 | APFixedPoint Negated = Result.getFixedPoint().negate(&Overflowed); |
| 9956 | if (Overflowed && !HandleOverflow(Info, E, Negated, E->getType())) |
| 9957 | return false; |
| 9958 | return Success(Negated, E); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9959 | } |
| 9960 | case UO_LNot: { |
| 9961 | bool bres; |
| 9962 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
| 9963 | return false; |
| 9964 | return Success(!bres, E); |
| 9965 | } |
| 9966 | } |
| 9967 | } |
| 9968 | |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 9969 | bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 9970 | const Expr *SubExpr = E->getSubExpr(); |
| 9971 | QualType DestType = E->getType(); |
| 9972 | assert(DestType->isFixedPointType() && |
| 9973 | "Expected destination type to be a fixed point type"); |
| 9974 | auto DestFXSema = Info.Ctx.getFixedPointSemantics(DestType); |
| 9975 | |
| 9976 | switch (E->getCastKind()) { |
| 9977 | case CK_FixedPointCast: { |
| 9978 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SubExpr->getType())); |
| 9979 | if (!EvaluateFixedPoint(SubExpr, Src, Info)) |
| 9980 | return false; |
| 9981 | bool Overflowed; |
| 9982 | APFixedPoint Result = Src.convert(DestFXSema, &Overflowed); |
| 9983 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
| 9984 | return false; |
| 9985 | return Success(Result, E); |
| 9986 | } |
Leonard Chan | 8f7caae | 2019-03-06 00:28:43 +0000 | [diff] [blame] | 9987 | case CK_IntegralToFixedPoint: { |
| 9988 | APSInt Src; |
| 9989 | if (!EvaluateInteger(SubExpr, Src, Info)) |
| 9990 | return false; |
| 9991 | |
| 9992 | bool Overflowed; |
| 9993 | APFixedPoint IntResult = APFixedPoint::getFromIntValue( |
| 9994 | Src, Info.Ctx.getFixedPointSemantics(DestType), &Overflowed); |
| 9995 | |
| 9996 | if (Overflowed && !HandleOverflow(Info, E, IntResult, DestType)) |
| 9997 | return false; |
| 9998 | |
| 9999 | return Success(IntResult, E); |
| 10000 | } |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 10001 | case CK_NoOp: |
| 10002 | case CK_LValueToRValue: |
| 10003 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 10004 | default: |
| 10005 | return Error(E); |
| 10006 | } |
| 10007 | } |
| 10008 | |
| 10009 | bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 10010 | const Expr *LHS = E->getLHS(); |
| 10011 | const Expr *RHS = E->getRHS(); |
| 10012 | FixedPointSemantics ResultFXSema = |
| 10013 | Info.Ctx.getFixedPointSemantics(E->getType()); |
| 10014 | |
| 10015 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHS->getType())); |
| 10016 | if (!EvaluateFixedPointOrInteger(LHS, LHSFX, Info)) |
| 10017 | return false; |
| 10018 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHS->getType())); |
| 10019 | if (!EvaluateFixedPointOrInteger(RHS, RHSFX, Info)) |
| 10020 | return false; |
| 10021 | |
| 10022 | switch (E->getOpcode()) { |
| 10023 | case BO_Add: { |
| 10024 | bool AddOverflow, ConversionOverflow; |
| 10025 | APFixedPoint Result = LHSFX.add(RHSFX, &AddOverflow) |
| 10026 | .convert(ResultFXSema, &ConversionOverflow); |
| 10027 | if ((AddOverflow || ConversionOverflow) && |
| 10028 | !HandleOverflow(Info, E, Result, E->getType())) |
| 10029 | return false; |
| 10030 | return Success(Result, E); |
| 10031 | } |
| 10032 | default: |
| 10033 | return false; |
| 10034 | } |
| 10035 | llvm_unreachable("Should've exited before this"); |
| 10036 | } |
| 10037 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 10038 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10039 | // Float Evaluation |
| 10040 | //===----------------------------------------------------------------------===// |
| 10041 | |
| 10042 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 10043 | class FloatExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 10044 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10045 | APFloat &Result; |
| 10046 | public: |
| 10047 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10048 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10049 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10050 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10051 | Result = V.getFloat(); |
| 10052 | return true; |
| 10053 | } |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10054 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10055 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 10056 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 10057 | return true; |
| 10058 | } |
| 10059 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 10060 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10061 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 10062 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10063 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 10064 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10065 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 10066 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 10067 | bool VisitUnaryReal(const UnaryOperator *E); |
| 10068 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 10069 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10070 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10071 | }; |
| 10072 | } // end anonymous namespace |
| 10073 | |
| 10074 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10075 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10076 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10077 | } |
| 10078 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 10079 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 10080 | QualType ResultTy, |
| 10081 | const Expr *Arg, |
| 10082 | bool SNaN, |
| 10083 | llvm::APFloat &Result) { |
| 10084 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 10085 | if (!S) return false; |
| 10086 | |
| 10087 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 10088 | |
| 10089 | llvm::APInt fill; |
| 10090 | |
| 10091 | // Treat empty strings as if they were zero. |
| 10092 | if (S->getString().empty()) |
| 10093 | fill = llvm::APInt(32, 0); |
| 10094 | else if (S->getString().getAsInteger(0, fill)) |
| 10095 | return false; |
| 10096 | |
Petar Jovanovic | d55ae6b | 2015-02-26 18:19:22 +0000 | [diff] [blame] | 10097 | if (Context.getTargetInfo().isNan2008()) { |
| 10098 | if (SNaN) |
| 10099 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 10100 | else |
| 10101 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 10102 | } else { |
| 10103 | // Prior to IEEE 754-2008, architectures were allowed to choose whether |
| 10104 | // the first bit of their significand was set for qNaN or sNaN. MIPS chose |
| 10105 | // a different encoding to what became a standard in 2008, and for pre- |
| 10106 | // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as |
| 10107 | // sNaN. This is now known as "legacy NaN" encoding. |
| 10108 | if (SNaN) |
| 10109 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 10110 | else |
| 10111 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 10112 | } |
| 10113 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 10114 | return true; |
| 10115 | } |
| 10116 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 10117 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 10118 | switch (E->getBuiltinCallee()) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10119 | default: |
| 10120 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 10121 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 10122 | case Builtin::BI__builtin_huge_val: |
| 10123 | case Builtin::BI__builtin_huge_valf: |
| 10124 | case Builtin::BI__builtin_huge_vall: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 10125 | case Builtin::BI__builtin_huge_valf128: |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 10126 | case Builtin::BI__builtin_inf: |
| 10127 | case Builtin::BI__builtin_inff: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 10128 | case Builtin::BI__builtin_infl: |
| 10129 | case Builtin::BI__builtin_inff128: { |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 10130 | const llvm::fltSemantics &Sem = |
| 10131 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 10132 | Result = llvm::APFloat::getInf(Sem); |
| 10133 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 10134 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10135 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 10136 | case Builtin::BI__builtin_nans: |
| 10137 | case Builtin::BI__builtin_nansf: |
| 10138 | case Builtin::BI__builtin_nansl: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 10139 | case Builtin::BI__builtin_nansf128: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10140 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 10141 | true, Result)) |
| 10142 | return Error(E); |
| 10143 | return true; |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 10144 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 10145 | case Builtin::BI__builtin_nan: |
| 10146 | case Builtin::BI__builtin_nanf: |
| 10147 | case Builtin::BI__builtin_nanl: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 10148 | case Builtin::BI__builtin_nanf128: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 10149 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 10150 | // can't constant fold it. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10151 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 10152 | false, Result)) |
| 10153 | return Error(E); |
| 10154 | return true; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 10155 | |
| 10156 | case Builtin::BI__builtin_fabs: |
| 10157 | case Builtin::BI__builtin_fabsf: |
| 10158 | case Builtin::BI__builtin_fabsl: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 10159 | case Builtin::BI__builtin_fabsf128: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 10160 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 10161 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10162 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 10163 | if (Result.isNegative()) |
| 10164 | Result.changeSign(); |
| 10165 | return true; |
| 10166 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 10167 | // FIXME: Builtin::BI__builtin_powi |
| 10168 | // FIXME: Builtin::BI__builtin_powif |
| 10169 | // FIXME: Builtin::BI__builtin_powil |
| 10170 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10171 | case Builtin::BI__builtin_copysign: |
| 10172 | case Builtin::BI__builtin_copysignf: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 10173 | case Builtin::BI__builtin_copysignl: |
| 10174 | case Builtin::BI__builtin_copysignf128: { |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 10175 | APFloat RHS(0.); |
| 10176 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 10177 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 10178 | return false; |
| 10179 | Result.copySign(RHS); |
| 10180 | return true; |
| 10181 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 10182 | } |
| 10183 | } |
| 10184 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 10185 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 10186 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 10187 | ComplexValue CV; |
| 10188 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 10189 | return false; |
| 10190 | Result = CV.FloatReal; |
| 10191 | return true; |
| 10192 | } |
| 10193 | |
| 10194 | return Visit(E->getSubExpr()); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 10195 | } |
| 10196 | |
| 10197 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 10198 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 10199 | ComplexValue CV; |
| 10200 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 10201 | return false; |
| 10202 | Result = CV.FloatImag; |
| 10203 | return true; |
| 10204 | } |
| 10205 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 10206 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 10207 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 10208 | Result = llvm::APFloat::getZero(Sem); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 10209 | return true; |
| 10210 | } |
| 10211 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 10212 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 10213 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10214 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10215 | case UO_Plus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 10216 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10217 | case UO_Minus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 10218 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 10219 | return false; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 10220 | Result.changeSign(); |
| 10221 | return true; |
| 10222 | } |
| 10223 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 10224 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10225 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 10226 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 10227 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 141fbf3 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 10228 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 10229 | APFloat RHS(0.0); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 10230 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 10231 | if (!LHSOK && !Info.noteFailure()) |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10232 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 10233 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
| 10234 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10235 | } |
| 10236 | |
| 10237 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 10238 | Result = E->getValue(); |
| 10239 | return true; |
| 10240 | } |
| 10241 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10242 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 10243 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10244 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 10245 | switch (E->getCastKind()) { |
| 10246 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10247 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 10248 | |
| 10249 | case CK_IntegralToFloating: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 10250 | APSInt IntResult; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10251 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 10252 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 10253 | E->getType(), Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 10254 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 10255 | |
| 10256 | case CK_FloatingCast: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 10257 | if (!Visit(SubExpr)) |
| 10258 | return false; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10259 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 10260 | Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 10261 | } |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 10262 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 10263 | case CK_FloatingComplexToReal: { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 10264 | ComplexValue V; |
| 10265 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 10266 | return false; |
| 10267 | Result = V.getComplexFloatReal(); |
| 10268 | return true; |
| 10269 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 10270 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 10271 | } |
| 10272 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 10273 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10274 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 10275 | //===----------------------------------------------------------------------===// |
| 10276 | |
| 10277 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 10278 | class ComplexExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 10279 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10280 | ComplexValue &Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10281 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 10282 | public: |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10283 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10284 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 10285 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10286 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10287 | Result.setFrom(V); |
| 10288 | return true; |
| 10289 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10290 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 10291 | bool ZeroInitialization(const Expr *E); |
| 10292 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 10293 | //===--------------------------------------------------------------------===// |
| 10294 | // Visitor Methods |
| 10295 | //===--------------------------------------------------------------------===// |
| 10296 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10297 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10298 | bool VisitCastExpr(const CastExpr *E); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10299 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10300 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 10301 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 10302 | }; |
| 10303 | } // end anonymous namespace |
| 10304 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10305 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 10306 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10307 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10308 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 10309 | } |
| 10310 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 10311 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 10312 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 10313 | if (ElemTy->isRealFloatingType()) { |
| 10314 | Result.makeComplexFloat(); |
| 10315 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 10316 | Result.FloatReal = Zero; |
| 10317 | Result.FloatImag = Zero; |
| 10318 | } else { |
| 10319 | Result.makeComplexInt(); |
| 10320 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 10321 | Result.IntReal = Zero; |
| 10322 | Result.IntImag = Zero; |
| 10323 | } |
| 10324 | return true; |
| 10325 | } |
| 10326 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10327 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 10328 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 10329 | |
| 10330 | if (SubExpr->getType()->isRealFloatingType()) { |
| 10331 | Result.makeComplexFloat(); |
| 10332 | APFloat &Imag = Result.FloatImag; |
| 10333 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 10334 | return false; |
| 10335 | |
| 10336 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 10337 | return true; |
| 10338 | } else { |
| 10339 | assert(SubExpr->getType()->isIntegerType() && |
| 10340 | "Unexpected imaginary literal."); |
| 10341 | |
| 10342 | Result.makeComplexInt(); |
| 10343 | APSInt &Imag = Result.IntImag; |
| 10344 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 10345 | return false; |
| 10346 | |
| 10347 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 10348 | return true; |
| 10349 | } |
| 10350 | } |
| 10351 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 10352 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 10353 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10354 | switch (E->getCastKind()) { |
| 10355 | case CK_BitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10356 | case CK_BaseToDerived: |
| 10357 | case CK_DerivedToBase: |
| 10358 | case CK_UncheckedDerivedToBase: |
| 10359 | case CK_Dynamic: |
| 10360 | case CK_ToUnion: |
| 10361 | case CK_ArrayToPointerDecay: |
| 10362 | case CK_FunctionToPointerDecay: |
| 10363 | case CK_NullToPointer: |
| 10364 | case CK_NullToMemberPointer: |
| 10365 | case CK_BaseToDerivedMemberPointer: |
| 10366 | case CK_DerivedToBaseMemberPointer: |
| 10367 | case CK_MemberPointerToBoolean: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 10368 | case CK_ReinterpretMemberPointer: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10369 | case CK_ConstructorConversion: |
| 10370 | case CK_IntegralToPointer: |
| 10371 | case CK_PointerToIntegral: |
| 10372 | case CK_PointerToBoolean: |
| 10373 | case CK_ToVoid: |
| 10374 | case CK_VectorSplat: |
| 10375 | case CK_IntegralCast: |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 10376 | case CK_BooleanToSignedIntegral: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10377 | case CK_IntegralToBoolean: |
| 10378 | case CK_IntegralToFloating: |
| 10379 | case CK_FloatingToIntegral: |
| 10380 | case CK_FloatingToBoolean: |
| 10381 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 10382 | case CK_CPointerToObjCPointerCast: |
| 10383 | case CK_BlockPointerToObjCPointerCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10384 | case CK_AnyPointerToBlockPointerCast: |
| 10385 | case CK_ObjCObjectLValueCast: |
| 10386 | case CK_FloatingComplexToReal: |
| 10387 | case CK_FloatingComplexToBoolean: |
| 10388 | case CK_IntegralComplexToReal: |
| 10389 | case CK_IntegralComplexToBoolean: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 10390 | case CK_ARCProduceObject: |
| 10391 | case CK_ARCConsumeObject: |
| 10392 | case CK_ARCReclaimReturnedObject: |
| 10393 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 10394 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 10395 | case CK_BuiltinFnToFnPtr: |
Andrew Savonichev | b555b76 | 2018-10-23 15:19:20 +0000 | [diff] [blame] | 10396 | case CK_ZeroToOCLOpaqueType: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10397 | case CK_NonAtomicToAtomic: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 10398 | case CK_AddressSpaceConversion: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 10399 | case CK_IntToOCLSampler: |
Leonard Chan | 99bda37 | 2018-10-15 16:07:02 +0000 | [diff] [blame] | 10400 | case CK_FixedPointCast: |
Leonard Chan | b4ba467 | 2018-10-23 17:55:35 +0000 | [diff] [blame] | 10401 | case CK_FixedPointToBoolean: |
Leonard Chan | 8f7caae | 2019-03-06 00:28:43 +0000 | [diff] [blame] | 10402 | case CK_FixedPointToIntegral: |
| 10403 | case CK_IntegralToFixedPoint: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10404 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 10405 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10406 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 10407 | case CK_AtomicToNonAtomic: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10408 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10409 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10410 | |
| 10411 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 10412 | case CK_LValueBitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10413 | case CK_UserDefinedConversion: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10414 | return Error(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10415 | |
| 10416 | case CK_FloatingRealToComplex: { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 10417 | APFloat &Real = Result.FloatReal; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10418 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 10419 | return false; |
| 10420 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10421 | Result.makeComplexFloat(); |
| 10422 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 10423 | return true; |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 10424 | } |
| 10425 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10426 | case CK_FloatingComplexCast: { |
| 10427 | if (!Visit(E->getSubExpr())) |
| 10428 | return false; |
| 10429 | |
| 10430 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 10431 | QualType From |
| 10432 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 10433 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10434 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 10435 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10436 | } |
| 10437 | |
| 10438 | case CK_FloatingComplexToIntegralComplex: { |
| 10439 | if (!Visit(E->getSubExpr())) |
| 10440 | return false; |
| 10441 | |
| 10442 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 10443 | QualType From |
| 10444 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 10445 | Result.makeComplexInt(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10446 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 10447 | To, Result.IntReal) && |
| 10448 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 10449 | To, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10450 | } |
| 10451 | |
| 10452 | case CK_IntegralRealToComplex: { |
| 10453 | APSInt &Real = Result.IntReal; |
| 10454 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 10455 | return false; |
| 10456 | |
| 10457 | Result.makeComplexInt(); |
| 10458 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 10459 | return true; |
| 10460 | } |
| 10461 | |
| 10462 | case CK_IntegralComplexCast: { |
| 10463 | if (!Visit(E->getSubExpr())) |
| 10464 | return false; |
| 10465 | |
| 10466 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 10467 | QualType From |
| 10468 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 10469 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 10470 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 10471 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10472 | return true; |
| 10473 | } |
| 10474 | |
| 10475 | case CK_IntegralComplexToFloatingComplex: { |
| 10476 | if (!Visit(E->getSubExpr())) |
| 10477 | return false; |
| 10478 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 10479 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10480 | QualType From |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 10481 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10482 | Result.makeComplexFloat(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10483 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 10484 | To, Result.FloatReal) && |
| 10485 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 10486 | To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10487 | } |
| 10488 | } |
| 10489 | |
| 10490 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 10491 | } |
| 10492 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10493 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 10494 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 10f4d06 | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 10495 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 10496 | |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10497 | // Track whether the LHS or RHS is real at the type system level. When this is |
| 10498 | // the case we can simplify our evaluation strategy. |
| 10499 | bool LHSReal = false, RHSReal = false; |
| 10500 | |
| 10501 | bool LHSOK; |
| 10502 | if (E->getLHS()->getType()->isRealFloatingType()) { |
| 10503 | LHSReal = true; |
| 10504 | APFloat &Real = Result.FloatReal; |
| 10505 | LHSOK = EvaluateFloat(E->getLHS(), Real, Info); |
| 10506 | if (LHSOK) { |
| 10507 | Result.makeComplexFloat(); |
| 10508 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 10509 | } |
| 10510 | } else { |
| 10511 | LHSOK = Visit(E->getLHS()); |
| 10512 | } |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 10513 | if (!LHSOK && !Info.noteFailure()) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10514 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10515 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10516 | ComplexValue RHS; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10517 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 10518 | RHSReal = true; |
| 10519 | APFloat &Real = RHS.FloatReal; |
| 10520 | if (!EvaluateFloat(E->getRHS(), Real, Info) || !LHSOK) |
| 10521 | return false; |
| 10522 | RHS.makeComplexFloat(); |
| 10523 | RHS.FloatImag = APFloat(Real.getSemantics()); |
| 10524 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10525 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10526 | |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10527 | assert(!(LHSReal && RHSReal) && |
| 10528 | "Cannot have both operands of a complex operation be real."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 10529 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10530 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10531 | case BO_Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10532 | if (Result.isComplexFloat()) { |
| 10533 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 10534 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10535 | if (LHSReal) |
| 10536 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 10537 | else if (!RHSReal) |
| 10538 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 10539 | APFloat::rmNearestTiesToEven); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10540 | } else { |
| 10541 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 10542 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 10543 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10544 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10545 | case BO_Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10546 | if (Result.isComplexFloat()) { |
| 10547 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 10548 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10549 | if (LHSReal) { |
| 10550 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 10551 | Result.getComplexFloatImag().changeSign(); |
| 10552 | } else if (!RHSReal) { |
| 10553 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 10554 | APFloat::rmNearestTiesToEven); |
| 10555 | } |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10556 | } else { |
| 10557 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 10558 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 10559 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10560 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10561 | case BO_Mul: |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10562 | if (Result.isComplexFloat()) { |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10563 | // This is an implementation of complex multiplication according to the |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 10564 | // constraints laid out in C11 Annex G. The implementation uses the |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10565 | // following naming scheme: |
| 10566 | // (a + ib) * (c + id) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10567 | ComplexValue LHS = Result; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10568 | APFloat &A = LHS.getComplexFloatReal(); |
| 10569 | APFloat &B = LHS.getComplexFloatImag(); |
| 10570 | APFloat &C = RHS.getComplexFloatReal(); |
| 10571 | APFloat &D = RHS.getComplexFloatImag(); |
| 10572 | APFloat &ResR = Result.getComplexFloatReal(); |
| 10573 | APFloat &ResI = Result.getComplexFloatImag(); |
| 10574 | if (LHSReal) { |
| 10575 | assert(!RHSReal && "Cannot have two real operands for a complex op!"); |
| 10576 | ResR = A * C; |
| 10577 | ResI = A * D; |
| 10578 | } else if (RHSReal) { |
| 10579 | ResR = C * A; |
| 10580 | ResI = C * B; |
| 10581 | } else { |
| 10582 | // In the fully general case, we need to handle NaNs and infinities |
| 10583 | // robustly. |
| 10584 | APFloat AC = A * C; |
| 10585 | APFloat BD = B * D; |
| 10586 | APFloat AD = A * D; |
| 10587 | APFloat BC = B * C; |
| 10588 | ResR = AC - BD; |
| 10589 | ResI = AD + BC; |
| 10590 | if (ResR.isNaN() && ResI.isNaN()) { |
| 10591 | bool Recalc = false; |
| 10592 | if (A.isInfinity() || B.isInfinity()) { |
| 10593 | A = APFloat::copySign( |
| 10594 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 10595 | B = APFloat::copySign( |
| 10596 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 10597 | if (C.isNaN()) |
| 10598 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 10599 | if (D.isNaN()) |
| 10600 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 10601 | Recalc = true; |
| 10602 | } |
| 10603 | if (C.isInfinity() || D.isInfinity()) { |
| 10604 | C = APFloat::copySign( |
| 10605 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 10606 | D = APFloat::copySign( |
| 10607 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 10608 | if (A.isNaN()) |
| 10609 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 10610 | if (B.isNaN()) |
| 10611 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 10612 | Recalc = true; |
| 10613 | } |
| 10614 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
| 10615 | AD.isInfinity() || BC.isInfinity())) { |
| 10616 | if (A.isNaN()) |
| 10617 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 10618 | if (B.isNaN()) |
| 10619 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 10620 | if (C.isNaN()) |
| 10621 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 10622 | if (D.isNaN()) |
| 10623 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 10624 | Recalc = true; |
| 10625 | } |
| 10626 | if (Recalc) { |
| 10627 | ResR = APFloat::getInf(A.getSemantics()) * (A * C - B * D); |
| 10628 | ResI = APFloat::getInf(A.getSemantics()) * (A * D + B * C); |
| 10629 | } |
| 10630 | } |
| 10631 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10632 | } else { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10633 | ComplexValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10634 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10635 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 10636 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10637 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10638 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 10639 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 10640 | } |
| 10641 | break; |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10642 | case BO_Div: |
| 10643 | if (Result.isComplexFloat()) { |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10644 | // This is an implementation of complex division according to the |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 10645 | // constraints laid out in C11 Annex G. The implementation uses the |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10646 | // following naming scheme: |
| 10647 | // (a + ib) / (c + id) |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10648 | ComplexValue LHS = Result; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10649 | APFloat &A = LHS.getComplexFloatReal(); |
| 10650 | APFloat &B = LHS.getComplexFloatImag(); |
| 10651 | APFloat &C = RHS.getComplexFloatReal(); |
| 10652 | APFloat &D = RHS.getComplexFloatImag(); |
| 10653 | APFloat &ResR = Result.getComplexFloatReal(); |
| 10654 | APFloat &ResI = Result.getComplexFloatImag(); |
| 10655 | if (RHSReal) { |
| 10656 | ResR = A / C; |
| 10657 | ResI = B / C; |
| 10658 | } else { |
| 10659 | if (LHSReal) { |
| 10660 | // No real optimizations we can do here, stub out with zero. |
| 10661 | B = APFloat::getZero(A.getSemantics()); |
| 10662 | } |
| 10663 | int DenomLogB = 0; |
| 10664 | APFloat MaxCD = maxnum(abs(C), abs(D)); |
| 10665 | if (MaxCD.isFinite()) { |
| 10666 | DenomLogB = ilogb(MaxCD); |
Matt Arsenault | c477f48 | 2016-03-13 05:12:47 +0000 | [diff] [blame] | 10667 | C = scalbn(C, -DenomLogB, APFloat::rmNearestTiesToEven); |
| 10668 | D = scalbn(D, -DenomLogB, APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10669 | } |
| 10670 | APFloat Denom = C * C + D * D; |
Matt Arsenault | c477f48 | 2016-03-13 05:12:47 +0000 | [diff] [blame] | 10671 | ResR = scalbn((A * C + B * D) / Denom, -DenomLogB, |
| 10672 | APFloat::rmNearestTiesToEven); |
| 10673 | ResI = scalbn((B * C - A * D) / Denom, -DenomLogB, |
| 10674 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10675 | if (ResR.isNaN() && ResI.isNaN()) { |
| 10676 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
| 10677 | ResR = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * A; |
| 10678 | ResI = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * B; |
| 10679 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
| 10680 | D.isFinite()) { |
| 10681 | A = APFloat::copySign( |
| 10682 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 10683 | B = APFloat::copySign( |
| 10684 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 10685 | ResR = APFloat::getInf(ResR.getSemantics()) * (A * C + B * D); |
| 10686 | ResI = APFloat::getInf(ResI.getSemantics()) * (B * C - A * D); |
| 10687 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
| 10688 | C = APFloat::copySign( |
| 10689 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 10690 | D = APFloat::copySign( |
| 10691 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 10692 | ResR = APFloat::getZero(ResR.getSemantics()) * (A * C + B * D); |
| 10693 | ResI = APFloat::getZero(ResI.getSemantics()) * (B * C - A * D); |
| 10694 | } |
| 10695 | } |
| 10696 | } |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10697 | } else { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10698 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 10699 | return Error(E, diag::note_expr_divide_by_zero); |
| 10700 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10701 | ComplexValue LHS = Result; |
| 10702 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 10703 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 10704 | Result.getComplexIntReal() = |
| 10705 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 10706 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 10707 | Result.getComplexIntImag() = |
| 10708 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 10709 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 10710 | } |
| 10711 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 10712 | } |
| 10713 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10714 | return true; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 10715 | } |
| 10716 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10717 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 10718 | // Get the operand value into 'Result'. |
| 10719 | if (!Visit(E->getSubExpr())) |
| 10720 | return false; |
| 10721 | |
| 10722 | switch (E->getOpcode()) { |
| 10723 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10724 | return Error(E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10725 | case UO_Extension: |
| 10726 | return true; |
| 10727 | case UO_Plus: |
| 10728 | // The result is always just the subexpr. |
| 10729 | return true; |
| 10730 | case UO_Minus: |
| 10731 | if (Result.isComplexFloat()) { |
| 10732 | Result.getComplexFloatReal().changeSign(); |
| 10733 | Result.getComplexFloatImag().changeSign(); |
| 10734 | } |
| 10735 | else { |
| 10736 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 10737 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 10738 | } |
| 10739 | return true; |
| 10740 | case UO_Not: |
| 10741 | if (Result.isComplexFloat()) |
| 10742 | Result.getComplexFloatImag().changeSign(); |
| 10743 | else |
| 10744 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 10745 | return true; |
| 10746 | } |
| 10747 | } |
| 10748 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 10749 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 10750 | if (E->getNumInits() == 2) { |
| 10751 | if (E->getType()->isComplexType()) { |
| 10752 | Result.makeComplexFloat(); |
| 10753 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 10754 | return false; |
| 10755 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 10756 | return false; |
| 10757 | } else { |
| 10758 | Result.makeComplexInt(); |
| 10759 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 10760 | return false; |
| 10761 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 10762 | return false; |
| 10763 | } |
| 10764 | return true; |
| 10765 | } |
| 10766 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 10767 | } |
| 10768 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 10769 | //===----------------------------------------------------------------------===// |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10770 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
| 10771 | // implicit conversion. |
| 10772 | //===----------------------------------------------------------------------===// |
| 10773 | |
| 10774 | namespace { |
| 10775 | class AtomicExprEvaluator : |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 10776 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10777 | const LValue *This; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10778 | APValue &Result; |
| 10779 | public: |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10780 | AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result) |
| 10781 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10782 | |
| 10783 | bool Success(const APValue &V, const Expr *E) { |
| 10784 | Result = V; |
| 10785 | return true; |
| 10786 | } |
| 10787 | |
| 10788 | bool ZeroInitialization(const Expr *E) { |
| 10789 | ImplicitValueInitExpr VIE( |
| 10790 | E->getType()->castAs<AtomicType>()->getValueType()); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10791 | // For atomic-qualified class (and array) types in C++, initialize the |
| 10792 | // _Atomic-wrapped subobject directly, in-place. |
| 10793 | return This ? EvaluateInPlace(Result, Info, *This, &VIE) |
| 10794 | : Evaluate(Result, Info, &VIE); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10795 | } |
| 10796 | |
| 10797 | bool VisitCastExpr(const CastExpr *E) { |
| 10798 | switch (E->getCastKind()) { |
| 10799 | default: |
| 10800 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 10801 | case CK_NonAtomicToAtomic: |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10802 | return This ? EvaluateInPlace(Result, Info, *This, E->getSubExpr()) |
| 10803 | : Evaluate(Result, Info, E->getSubExpr()); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10804 | } |
| 10805 | } |
| 10806 | }; |
| 10807 | } // end anonymous namespace |
| 10808 | |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10809 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
| 10810 | EvalInfo &Info) { |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10811 | assert(E->isRValue() && E->getType()->isAtomicType()); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10812 | return AtomicExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10813 | } |
| 10814 | |
| 10815 | //===----------------------------------------------------------------------===// |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10816 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 10817 | // comma operator |
| 10818 | //===----------------------------------------------------------------------===// |
| 10819 | |
| 10820 | namespace { |
| 10821 | class VoidExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 10822 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10823 | public: |
| 10824 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 10825 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10826 | bool Success(const APValue &V, const Expr *e) { return true; } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10827 | |
Richard Smith | 7cd577b | 2017-08-17 19:35:50 +0000 | [diff] [blame] | 10828 | bool ZeroInitialization(const Expr *E) { return true; } |
| 10829 | |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10830 | bool VisitCastExpr(const CastExpr *E) { |
| 10831 | switch (E->getCastKind()) { |
| 10832 | default: |
| 10833 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 10834 | case CK_ToVoid: |
| 10835 | VisitIgnoredValue(E->getSubExpr()); |
| 10836 | return true; |
| 10837 | } |
| 10838 | } |
Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 10839 | |
| 10840 | bool VisitCallExpr(const CallExpr *E) { |
| 10841 | switch (E->getBuiltinCallee()) { |
| 10842 | default: |
| 10843 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 10844 | case Builtin::BI__assume: |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 10845 | case Builtin::BI__builtin_assume: |
Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 10846 | // The argument is not evaluated! |
| 10847 | return true; |
| 10848 | } |
| 10849 | } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10850 | }; |
| 10851 | } // end anonymous namespace |
| 10852 | |
| 10853 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 10854 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 10855 | return VoidExprEvaluator(Info).Visit(E); |
| 10856 | } |
| 10857 | |
| 10858 | //===----------------------------------------------------------------------===// |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10859 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 10860 | //===----------------------------------------------------------------------===// |
| 10861 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10862 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10863 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 10864 | // are. |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10865 | QualType T = E->getType(); |
| 10866 | if (E->isGLValue() || T->isFunctionType()) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10867 | LValue LV; |
| 10868 | if (!EvaluateLValue(E, LV, Info)) |
| 10869 | return false; |
| 10870 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10871 | } else if (T->isVectorType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10872 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 10873 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10874 | } else if (T->isIntegralOrEnumerationType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10875 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10876 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10877 | } else if (T->hasPointerRepresentation()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10878 | LValue LV; |
| 10879 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10880 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10881 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10882 | } else if (T->isRealFloatingType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10883 | llvm::APFloat F(0.0); |
| 10884 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10885 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10886 | Result = APValue(F); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10887 | } else if (T->isAnyComplexType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10888 | ComplexValue C; |
| 10889 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10890 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10891 | C.moveInto(Result); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 10892 | } else if (T->isFixedPointType()) { |
| 10893 | if (!FixedPointExprEvaluator(Info, Result).Visit(E)) return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10894 | } else if (T->isMemberPointerType()) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 10895 | MemberPtr P; |
| 10896 | if (!EvaluateMemberPointer(E, P, Info)) |
| 10897 | return false; |
| 10898 | P.moveInto(Result); |
| 10899 | return true; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10900 | } else if (T->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10901 | LValue LV; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 10902 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10903 | if (!EvaluateArray(E, LV, Value, Info)) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 10904 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10905 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10906 | } else if (T->isRecordType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10907 | LValue LV; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 10908 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10909 | if (!EvaluateRecord(E, LV, Value, Info)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10910 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10911 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10912 | } else if (T->isVoidType()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10913 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 10914 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10915 | << E->getType(); |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10916 | if (!EvaluateVoid(E, Info)) |
| 10917 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10918 | } else if (T->isAtomicType()) { |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10919 | QualType Unqual = T.getAtomicUnqualifiedType(); |
| 10920 | if (Unqual->isArrayType() || Unqual->isRecordType()) { |
| 10921 | LValue LV; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 10922 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10923 | if (!EvaluateAtomic(E, &LV, Value, Info)) |
| 10924 | return false; |
| 10925 | } else { |
| 10926 | if (!EvaluateAtomic(E, nullptr, Result, Info)) |
| 10927 | return false; |
| 10928 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10929 | } else if (Info.getLangOpts().CPlusPlus11) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 10930 | Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10931 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10932 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 10933 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 10934 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10935 | } |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10936 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 10937 | return true; |
| 10938 | } |
| 10939 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10940 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 10941 | /// cases, the in-place evaluation is essential, since later initializers for |
| 10942 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 10943 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10944 | const Expr *E, bool AllowNonLiteralTypes) { |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 10945 | assert(!E->isValueDependent()); |
| 10946 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10947 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10948 | return false; |
| 10949 | |
| 10950 | if (E->isRValue()) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 10951 | // Evaluate arrays and record types in-place, so that later initializers can |
| 10952 | // refer to earlier-initialized members of the object. |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10953 | QualType T = E->getType(); |
| 10954 | if (T->isArrayType()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10955 | return EvaluateArray(E, This, Result, Info); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10956 | else if (T->isRecordType()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10957 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10958 | else if (T->isAtomicType()) { |
| 10959 | QualType Unqual = T.getAtomicUnqualifiedType(); |
| 10960 | if (Unqual->isArrayType() || Unqual->isRecordType()) |
| 10961 | return EvaluateAtomic(E, &This, Result, Info); |
| 10962 | } |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 10963 | } |
| 10964 | |
| 10965 | // For any other type, in-place evaluation is unimportant. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10966 | return Evaluate(Result, Info, E); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 10967 | } |
| 10968 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10969 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 10970 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 10971 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 10972 | if (E->getType().isNull()) |
| 10973 | return false; |
| 10974 | |
Nick Lewycky | c190f96 | 2017-05-02 01:06:16 +0000 | [diff] [blame] | 10975 | if (!CheckLiteralType(Info, E)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10976 | return false; |
| 10977 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10978 | if (!::Evaluate(Result, Info, E)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10979 | return false; |
| 10980 | |
| 10981 | if (E->isGLValue()) { |
| 10982 | LValue LV; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10983 | LV.setFrom(Info.Ctx, Result); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 10984 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10985 | return false; |
| 10986 | } |
| 10987 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10988 | // Check this core constant expression is a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10989 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10990 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10991 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10992 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 10993 | const ASTContext &Ctx, bool &IsConst) { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10994 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 10995 | // containing vast quantities of these. |
| 10996 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
| 10997 | Result.Val = APValue(APSInt(L->getValue(), |
| 10998 | L->getType()->isUnsignedIntegerType())); |
| 10999 | IsConst = true; |
| 11000 | return true; |
| 11001 | } |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 11002 | |
| 11003 | // This case should be rare, but we need to check it before we check on |
| 11004 | // the type below. |
| 11005 | if (Exp->getType().isNull()) { |
| 11006 | IsConst = false; |
| 11007 | return true; |
| 11008 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 11009 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 11010 | // FIXME: Evaluating values of large array and record types can cause |
| 11011 | // performance problems. Only do so in C++11 for now. |
| 11012 | if (Exp->isRValue() && (Exp->getType()->isArrayType() || |
| 11013 | Exp->getType()->isRecordType()) && |
Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 11014 | !Ctx.getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 11015 | IsConst = false; |
| 11016 | return true; |
| 11017 | } |
| 11018 | return false; |
| 11019 | } |
| 11020 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11021 | static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result, |
| 11022 | Expr::SideEffectsKind SEK) { |
| 11023 | return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) || |
| 11024 | (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior); |
| 11025 | } |
| 11026 | |
| 11027 | static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result, |
| 11028 | const ASTContext &Ctx, EvalInfo &Info) { |
| 11029 | bool IsConst; |
| 11030 | if (FastEvaluateAsRValue(E, Result, Ctx, IsConst)) |
| 11031 | return IsConst; |
| 11032 | |
| 11033 | return EvaluateAsRValue(Info, E, Result.Val); |
| 11034 | } |
| 11035 | |
| 11036 | static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult, |
| 11037 | const ASTContext &Ctx, |
| 11038 | Expr::SideEffectsKind AllowSideEffects, |
| 11039 | EvalInfo &Info) { |
| 11040 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 11041 | return false; |
| 11042 | |
| 11043 | if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info) || |
| 11044 | !ExprResult.Val.isInt() || |
| 11045 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
| 11046 | return false; |
| 11047 | |
| 11048 | return true; |
| 11049 | } |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 11050 | |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 11051 | static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult, |
| 11052 | const ASTContext &Ctx, |
| 11053 | Expr::SideEffectsKind AllowSideEffects, |
| 11054 | EvalInfo &Info) { |
| 11055 | if (!E->getType()->isFixedPointType()) |
| 11056 | return false; |
| 11057 | |
| 11058 | if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info)) |
| 11059 | return false; |
| 11060 | |
| 11061 | if (!ExprResult.Val.isFixedPoint() || |
| 11062 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
| 11063 | return false; |
| 11064 | |
| 11065 | return true; |
| 11066 | } |
| 11067 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 11068 | /// 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] | 11069 | /// any crazy technique (that has nothing to do with language standards) that |
| 11070 | /// 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] | 11071 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 11072 | /// will be applied to the result. |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11073 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, |
| 11074 | bool InConstantContext) const { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 11075 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11076 | Info.InConstantContext = InConstantContext; |
| 11077 | return ::EvaluateAsRValue(this, Result, Ctx, Info); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 11078 | } |
| 11079 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 11080 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 11081 | const ASTContext &Ctx) const { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 11082 | EvalResult Scratch; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 11083 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 11084 | HandleConversionToBool(Scratch.Val, Result); |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 11085 | } |
| 11086 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11087 | bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 11088 | SideEffectsKind AllowSideEffects) const { |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11089 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
| 11090 | return ::EvaluateAsInt(this, Result, Ctx, AllowSideEffects, Info); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11091 | } |
| 11092 | |
Leonard Chan | d3f3e16 | 2019-01-18 21:04:25 +0000 | [diff] [blame] | 11093 | bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx, |
| 11094 | SideEffectsKind AllowSideEffects) const { |
| 11095 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
| 11096 | return ::EvaluateAsFixedPoint(this, Result, Ctx, AllowSideEffects, Info); |
| 11097 | } |
| 11098 | |
Richard Trieu | be234c3 | 2016-04-21 21:04:55 +0000 | [diff] [blame] | 11099 | bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx, |
| 11100 | SideEffectsKind AllowSideEffects) const { |
| 11101 | if (!getType()->isRealFloatingType()) |
| 11102 | return false; |
| 11103 | |
| 11104 | EvalResult ExprResult; |
| 11105 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isFloat() || |
Richard Smith | 3f1d6de | 2018-05-21 20:36:58 +0000 | [diff] [blame] | 11106 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
Richard Trieu | be234c3 | 2016-04-21 21:04:55 +0000 | [diff] [blame] | 11107 | return false; |
| 11108 | |
| 11109 | Result = ExprResult.Val.getFloat(); |
| 11110 | return true; |
| 11111 | } |
| 11112 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 11113 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 11114 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 11115 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 11116 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 11117 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 11118 | !CheckLValueConstantExpression(Info, getExprLoc(), |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 11119 | Ctx.getLValueReferenceType(getType()), LV, |
| 11120 | Expr::EvaluateForCodeGen)) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 11121 | return false; |
| 11122 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 11123 | LV.moveInto(Result.Val); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 11124 | return true; |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 11125 | } |
| 11126 | |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 11127 | bool Expr::EvaluateAsConstantExpr(EvalResult &Result, ConstExprUsage Usage, |
| 11128 | const ASTContext &Ctx) const { |
| 11129 | EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression; |
| 11130 | EvalInfo Info(Ctx, Result, EM); |
| 11131 | if (!::Evaluate(Result.Val, Info, this)) |
| 11132 | return false; |
| 11133 | |
| 11134 | return CheckConstantExpression(Info, getExprLoc(), getType(), Result.Val, |
| 11135 | Usage); |
| 11136 | } |
| 11137 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 11138 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 11139 | const VarDecl *VD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11140 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 11141 | // FIXME: Evaluating initializers for large array and record types can cause |
| 11142 | // performance problems. Only do so in C++11 for now. |
| 11143 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11144 | !Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 11145 | return false; |
| 11146 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 11147 | Expr::EvalStatus EStatus; |
| 11148 | EStatus.Diag = &Notes; |
| 11149 | |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 11150 | EvalInfo InitInfo(Ctx, EStatus, VD->isConstexpr() |
| 11151 | ? EvalInfo::EM_ConstantExpression |
| 11152 | : EvalInfo::EM_ConstantFold); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 11153 | InitInfo.setEvaluatingDecl(VD, Value); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11154 | InitInfo.InConstantContext = true; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 11155 | |
| 11156 | LValue LVal; |
| 11157 | LVal.set(VD); |
| 11158 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 11159 | // C++11 [basic.start.init]p2: |
| 11160 | // Variables with static storage duration or thread storage duration shall be |
| 11161 | // zero-initialized before any other initialization takes place. |
| 11162 | // This behavior is not present in C. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11163 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 11164 | !VD->getType()->isReferenceType()) { |
| 11165 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 11166 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 11167 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 11168 | return false; |
| 11169 | } |
| 11170 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 11171 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, |
| 11172 | /*AllowNonLiteralTypes=*/true) || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 11173 | EStatus.HasSideEffects) |
| 11174 | return false; |
| 11175 | |
| 11176 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 11177 | Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 11178 | } |
| 11179 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 11180 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 11181 | /// constant folded, but discard the result. |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 11182 | bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 11183 | EvalResult Result; |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11184 | return EvaluateAsRValue(Result, Ctx, /* in constant context */ true) && |
Richard Smith | 3f1d6de | 2018-05-21 20:36:58 +0000 | [diff] [blame] | 11185 | !hasUnacceptableSideEffect(Result, SEK); |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 11186 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 11187 | |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 11188 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11189 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11190 | EvalResult EVResult; |
| 11191 | EVResult.Diag = Diag; |
| 11192 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
| 11193 | Info.InConstantContext = true; |
| 11194 | |
| 11195 | bool Result = ::EvaluateAsRValue(this, EVResult, Ctx, Info); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 11196 | (void)Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 11197 | assert(Result && "Could not evaluate expression"); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11198 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 11199 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11200 | return EVResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 11201 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11202 | |
David Bolvansky | 3b6ae57 | 2018-10-18 20:49:06 +0000 | [diff] [blame] | 11203 | APSInt Expr::EvaluateKnownConstIntCheckOverflow( |
| 11204 | const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11205 | EvalResult EVResult; |
| 11206 | EVResult.Diag = Diag; |
| 11207 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_EvaluateForOverflow); |
| 11208 | Info.InConstantContext = true; |
| 11209 | |
| 11210 | bool Result = ::EvaluateAsRValue(Info, this, EVResult.Val); |
David Bolvansky | 3b6ae57 | 2018-10-18 20:49:06 +0000 | [diff] [blame] | 11211 | (void)Result; |
| 11212 | assert(Result && "Could not evaluate expression"); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11213 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
David Bolvansky | 3b6ae57 | 2018-10-18 20:49:06 +0000 | [diff] [blame] | 11214 | |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11215 | return EVResult.Val.getInt(); |
David Bolvansky | 3b6ae57 | 2018-10-18 20:49:06 +0000 | [diff] [blame] | 11216 | } |
| 11217 | |
Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 11218 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 11219 | bool IsConst; |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11220 | EvalResult EVResult; |
| 11221 | if (!FastEvaluateAsRValue(this, EVResult, Ctx, IsConst)) { |
| 11222 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_EvaluateForOverflow); |
| 11223 | (void)::EvaluateAsRValue(Info, this, EVResult.Val); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 11224 | } |
| 11225 | } |
| 11226 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 11227 | bool Expr::EvalResult::isGlobalLValue() const { |
| 11228 | assert(Val.isLValue()); |
| 11229 | return IsGlobalLValue(Val.getLValueBase()); |
| 11230 | } |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 11231 | |
| 11232 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11233 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 11234 | /// an integer constant expression. |
| 11235 | |
| 11236 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 11237 | /// comma, etc |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11238 | |
| 11239 | // CheckICE - This function does the fundamental ICE checking: the returned |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11240 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
| 11241 | // and a (possibly null) SourceLocation indicating the location of the problem. |
| 11242 | // |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11243 | // Note that to reduce code duplication, this helper does no evaluation |
| 11244 | // itself; the caller checks whether the expression is evaluatable, and |
| 11245 | // in the rare cases where CheckICE actually cares about the evaluated |
George Burgess IV | 5731707 | 2017-02-02 07:53:55 +0000 | [diff] [blame] | 11246 | // value, it calls into Evaluate. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11247 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 11248 | namespace { |
| 11249 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11250 | enum ICEKind { |
| 11251 | /// This expression is an ICE. |
| 11252 | IK_ICE, |
| 11253 | /// This expression is not an ICE, but if it isn't evaluated, it's |
| 11254 | /// a legal subexpression for an ICE. This return value is used to handle |
| 11255 | /// the comma operator in C99 mode, and non-constant subexpressions. |
| 11256 | IK_ICEIfUnevaluated, |
| 11257 | /// This expression is not an ICE, and is not a legal subexpression for one. |
| 11258 | IK_NotICE |
| 11259 | }; |
| 11260 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11261 | struct ICEDiag { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11262 | ICEKind Kind; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11263 | SourceLocation Loc; |
| 11264 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11265 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11266 | }; |
| 11267 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 11268 | } |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 11269 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11270 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
| 11271 | |
| 11272 | 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] | 11273 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11274 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11275 | Expr::EvalResult EVResult; |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11276 | Expr::EvalStatus Status; |
| 11277 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
| 11278 | |
| 11279 | Info.InConstantContext = true; |
| 11280 | if (!::EvaluateAsRValue(E, EVResult, Ctx, Info) || EVResult.HasSideEffects || |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11281 | !EVResult.Val.isInt()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11282 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11283 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11284 | return NoDiag(); |
| 11285 | } |
| 11286 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11287 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11288 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11289 | if (!E->getType()->isIntegralOrEnumerationType()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11290 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11291 | |
| 11292 | switch (E->getStmtClass()) { |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 11293 | #define ABSTRACT_STMT(Node) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11294 | #define STMT(Node, Base) case Expr::Node##Class: |
| 11295 | #define EXPR(Node, Base) |
| 11296 | #include "clang/AST/StmtNodes.inc" |
| 11297 | case Expr::PredefinedExprClass: |
| 11298 | case Expr::FloatingLiteralClass: |
| 11299 | case Expr::ImaginaryLiteralClass: |
| 11300 | case Expr::StringLiteralClass: |
| 11301 | case Expr::ArraySubscriptExprClass: |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 11302 | case Expr::OMPArraySectionExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11303 | case Expr::MemberExprClass: |
| 11304 | case Expr::CompoundAssignOperatorClass: |
| 11305 | case Expr::CompoundLiteralExprClass: |
| 11306 | case Expr::ExtVectorElementExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11307 | case Expr::DesignatedInitExprClass: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 11308 | case Expr::ArrayInitLoopExprClass: |
| 11309 | case Expr::ArrayInitIndexExprClass: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 11310 | case Expr::NoInitExprClass: |
| 11311 | case Expr::DesignatedInitUpdateExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11312 | case Expr::ImplicitValueInitExprClass: |
| 11313 | case Expr::ParenListExprClass: |
| 11314 | case Expr::VAArgExprClass: |
| 11315 | case Expr::AddrLabelExprClass: |
| 11316 | case Expr::StmtExprClass: |
| 11317 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 11318 | case Expr::CUDAKernelCallExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11319 | case Expr::CXXDynamicCastExprClass: |
| 11320 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 5cc0a67 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 11321 | case Expr::CXXUuidofExprClass: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 11322 | case Expr::MSPropertyRefExprClass: |
Alexey Bataev | f763027 | 2015-11-25 12:01:00 +0000 | [diff] [blame] | 11323 | case Expr::MSPropertySubscriptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11324 | case Expr::CXXNullPtrLiteralExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 11325 | case Expr::UserDefinedLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11326 | case Expr::CXXThisExprClass: |
| 11327 | case Expr::CXXThrowExprClass: |
| 11328 | case Expr::CXXNewExprClass: |
| 11329 | case Expr::CXXDeleteExprClass: |
| 11330 | case Expr::CXXPseudoDestructorExprClass: |
| 11331 | case Expr::UnresolvedLookupExprClass: |
Kaelyn Takata | e1f49d5 | 2014-10-27 18:07:20 +0000 | [diff] [blame] | 11332 | case Expr::TypoExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11333 | case Expr::DependentScopeDeclRefExprClass: |
| 11334 | case Expr::CXXConstructExprClass: |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 11335 | case Expr::CXXInheritedCtorInitExprClass: |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 11336 | case Expr::CXXStdInitializerListExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11337 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 11338 | case Expr::ExprWithCleanupsClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11339 | case Expr::CXXTemporaryObjectExprClass: |
| 11340 | case Expr::CXXUnresolvedConstructExprClass: |
| 11341 | case Expr::CXXDependentScopeMemberExprClass: |
| 11342 | case Expr::UnresolvedMemberExprClass: |
| 11343 | case Expr::ObjCStringLiteralClass: |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 11344 | case Expr::ObjCBoxedExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11345 | case Expr::ObjCArrayLiteralClass: |
| 11346 | case Expr::ObjCDictionaryLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11347 | case Expr::ObjCEncodeExprClass: |
| 11348 | case Expr::ObjCMessageExprClass: |
| 11349 | case Expr::ObjCSelectorExprClass: |
| 11350 | case Expr::ObjCProtocolExprClass: |
| 11351 | case Expr::ObjCIvarRefExprClass: |
| 11352 | case Expr::ObjCPropertyRefExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11353 | case Expr::ObjCSubscriptRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11354 | case Expr::ObjCIsaExprClass: |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 11355 | case Expr::ObjCAvailabilityCheckExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11356 | case Expr::ShuffleVectorExprClass: |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 11357 | case Expr::ConvertVectorExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11358 | case Expr::BlockExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11359 | case Expr::NoStmtClass: |
John McCall | 8d69a21 | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 11360 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 11361 | case Expr::PackExpansionExprClass: |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 11362 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 11363 | case Expr::FunctionParmPackExprClass: |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 11364 | case Expr::AsTypeExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11365 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 11366 | case Expr::MaterializeTemporaryExprClass: |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 11367 | case Expr::PseudoObjectExprClass: |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 11368 | case Expr::AtomicExprClass: |
Douglas Gregor | e31e606 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 11369 | case Expr::LambdaExprClass: |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 11370 | case Expr::CXXFoldExprClass: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 11371 | case Expr::CoawaitExprClass: |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 11372 | case Expr::DependentCoawaitExprClass: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 11373 | case Expr::CoyieldExprClass: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11374 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 11375 | |
Richard Smith | f137f93 | 2014-01-25 20:50:08 +0000 | [diff] [blame] | 11376 | case Expr::InitListExprClass: { |
| 11377 | // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the |
| 11378 | // form "T x = { a };" is equivalent to "T x = a;". |
| 11379 | // Unless we're initializing a reference, T is a scalar as it is known to be |
| 11380 | // of integral or enumeration type. |
| 11381 | if (E->isRValue()) |
| 11382 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
| 11383 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11384 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
Richard Smith | f137f93 | 2014-01-25 20:50:08 +0000 | [diff] [blame] | 11385 | } |
| 11386 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 11387 | case Expr::SizeOfPackExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11388 | case Expr::GNUNullExprClass: |
| 11389 | // GCC considers the GNU __null value to be an integral constant expression. |
| 11390 | return NoDiag(); |
| 11391 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 11392 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 11393 | return |
| 11394 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 11395 | |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 11396 | case Expr::ConstantExprClass: |
| 11397 | return CheckICE(cast<ConstantExpr>(E)->getSubExpr(), Ctx); |
| 11398 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11399 | case Expr::ParenExprClass: |
| 11400 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 11401 | case Expr::GenericSelectionExprClass: |
| 11402 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11403 | case Expr::IntegerLiteralClass: |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 11404 | case Expr::FixedPointLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11405 | case Expr::CharacterLiteralClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11406 | case Expr::ObjCBoolLiteralExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11407 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 11408 | case Expr::CXXScalarValueInitExprClass: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 11409 | case Expr::TypeTraitExprClass: |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 11410 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 11411 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 11412 | case Expr::CXXNoexceptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11413 | return NoDiag(); |
| 11414 | case Expr::CallExprClass: |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 11415 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 11416 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 11417 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 11418 | // contain an operand of (pointer to) function type. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11419 | const CallExpr *CE = cast<CallExpr>(E); |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 11420 | if (CE->getBuiltinCallee()) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11421 | return CheckEvalInICE(E, Ctx); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11422 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11423 | } |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 11424 | case Expr::DeclRefExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11425 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 11426 | return NoDiag(); |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 11427 | const ValueDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11428 | if (Ctx.getLangOpts().CPlusPlus && |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 11429 | D && IsConstNonVolatile(D->getType())) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11430 | // Parameter variables are never constants. Without this check, |
| 11431 | // getAnyInitializer() can find a default argument, which leads |
| 11432 | // to chaos. |
| 11433 | if (isa<ParmVarDecl>(D)) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11434 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11435 | |
| 11436 | // C++ 7.1.5.1p2 |
| 11437 | // A variable of non-volatile const-qualified integral or enumeration |
| 11438 | // type initialized by an ICE can be used in ICEs. |
| 11439 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 11440 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11441 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 11442 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 11443 | const VarDecl *VD; |
| 11444 | // Look for a declaration of this variable that has an initializer, and |
| 11445 | // check whether it is an ICE. |
| 11446 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 11447 | return NoDiag(); |
| 11448 | else |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11449 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11450 | } |
| 11451 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11452 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 11453 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11454 | case Expr::UnaryOperatorClass: { |
| 11455 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 11456 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11457 | case UO_PostInc: |
| 11458 | case UO_PostDec: |
| 11459 | case UO_PreInc: |
| 11460 | case UO_PreDec: |
| 11461 | case UO_AddrOf: |
| 11462 | case UO_Deref: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 11463 | case UO_Coawait: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 11464 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 11465 | // subexpressions of constant expressions, but they can never be ICEs |
| 11466 | // because an ICE cannot contain an lvalue operand. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11467 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11468 | case UO_Extension: |
| 11469 | case UO_LNot: |
| 11470 | case UO_Plus: |
| 11471 | case UO_Minus: |
| 11472 | case UO_Not: |
| 11473 | case UO_Real: |
| 11474 | case UO_Imag: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11475 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11476 | } |
Reid Kleckner | e540d97 | 2018-11-01 17:51:48 +0000 | [diff] [blame] | 11477 | llvm_unreachable("invalid unary operator class"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11478 | } |
| 11479 | case Expr::OffsetOfExprClass: { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11480 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 11481 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
| 11482 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
| 11483 | // compliance: we should warn earlier for offsetof expressions with |
| 11484 | // array subscripts that aren't ICEs, and if the array subscripts |
| 11485 | // are ICEs, the value of the offsetof must be an integer constant. |
| 11486 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11487 | } |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 11488 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 11489 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 11490 | if ((Exp->getKind() == UETT_SizeOf) && |
| 11491 | Exp->getTypeOfArgument()->isVariableArrayType()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11492 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11493 | return NoDiag(); |
| 11494 | } |
| 11495 | case Expr::BinaryOperatorClass: { |
| 11496 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 11497 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11498 | case BO_PtrMemD: |
| 11499 | case BO_PtrMemI: |
| 11500 | case BO_Assign: |
| 11501 | case BO_MulAssign: |
| 11502 | case BO_DivAssign: |
| 11503 | case BO_RemAssign: |
| 11504 | case BO_AddAssign: |
| 11505 | case BO_SubAssign: |
| 11506 | case BO_ShlAssign: |
| 11507 | case BO_ShrAssign: |
| 11508 | case BO_AndAssign: |
| 11509 | case BO_XorAssign: |
| 11510 | case BO_OrAssign: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 11511 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 11512 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 11513 | // contain an lvalue operand. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11514 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11515 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11516 | case BO_Mul: |
| 11517 | case BO_Div: |
| 11518 | case BO_Rem: |
| 11519 | case BO_Add: |
| 11520 | case BO_Sub: |
| 11521 | case BO_Shl: |
| 11522 | case BO_Shr: |
| 11523 | case BO_LT: |
| 11524 | case BO_GT: |
| 11525 | case BO_LE: |
| 11526 | case BO_GE: |
| 11527 | case BO_EQ: |
| 11528 | case BO_NE: |
| 11529 | case BO_And: |
| 11530 | case BO_Xor: |
| 11531 | case BO_Or: |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 11532 | case BO_Comma: |
| 11533 | case BO_Cmp: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11534 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 11535 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11536 | if (Exp->getOpcode() == BO_Div || |
| 11537 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 11538 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11539 | // we don't evaluate one. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11540 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11541 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11542 | if (REval == 0) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11543 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11544 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11545 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11546 | if (LEval.isMinSignedValue()) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11547 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11548 | } |
| 11549 | } |
| 11550 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11551 | if (Exp->getOpcode() == BO_Comma) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11552 | if (Ctx.getLangOpts().C99) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11553 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 11554 | // if it isn't evaluated. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11555 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11556 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11557 | } else { |
| 11558 | // In both C89 and C++, commas in ICEs are illegal. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11559 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11560 | } |
| 11561 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11562 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11563 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11564 | case BO_LAnd: |
| 11565 | case BO_LOr: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11566 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 11567 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11568 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11569 | // Rare case where the RHS has a comma "side-effect"; we need |
| 11570 | // to actually check the condition to see whether the side |
| 11571 | // with the comma is evaluated. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11572 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11573 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11574 | return RHSResult; |
| 11575 | return NoDiag(); |
| 11576 | } |
| 11577 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11578 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11579 | } |
| 11580 | } |
Reid Kleckner | e540d97 | 2018-11-01 17:51:48 +0000 | [diff] [blame] | 11581 | llvm_unreachable("invalid binary operator kind"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11582 | } |
| 11583 | case Expr::ImplicitCastExprClass: |
| 11584 | case Expr::CStyleCastExprClass: |
| 11585 | case Expr::CXXFunctionalCastExprClass: |
| 11586 | case Expr::CXXStaticCastExprClass: |
| 11587 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | c3e31e7 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 11588 | case Expr::CXXConstCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11589 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11590 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 11591 | if (isa<ExplicitCastExpr>(E)) { |
| 11592 | if (const FloatingLiteral *FL |
| 11593 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 11594 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 11595 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 11596 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 11597 | bool Ignored; |
| 11598 | // If the value does not fit in the destination type, the behavior is |
| 11599 | // undefined, so we are not required to treat it as a constant |
| 11600 | // expression. |
| 11601 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 11602 | llvm::APFloat::rmTowardZero, |
| 11603 | &Ignored) & APFloat::opInvalidOp) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11604 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 11605 | return NoDiag(); |
| 11606 | } |
| 11607 | } |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 11608 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 11609 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 11610 | case CK_AtomicToNonAtomic: |
| 11611 | case CK_NonAtomicToAtomic: |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 11612 | case CK_NoOp: |
| 11613 | case CK_IntegralToBoolean: |
| 11614 | case CK_IntegralCast: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11615 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 11616 | default: |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 11617 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 11618 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11619 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 11620 | case Expr::BinaryConditionalOperatorClass: { |
| 11621 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 11622 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11623 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 11624 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11625 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
| 11626 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
| 11627 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
Richard Smith | 74fc721 | 2012-12-28 12:53:55 +0000 | [diff] [blame] | 11628 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 11629 | return FalseResult; |
| 11630 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11631 | case Expr::ConditionalOperatorClass: { |
| 11632 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 11633 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 11634 | // then only the true side is actually considered in an integer constant |
| 11635 | // expression, and it is fully evaluated. This is an important GNU |
| 11636 | // extension. See GCC PR38377 for discussion. |
| 11637 | if (const CallExpr *CallCE |
| 11638 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 11639 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 11640 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11641 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11642 | if (CondResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11643 | return CondResult; |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 11644 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11645 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 11646 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 11647 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11648 | if (TrueResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11649 | return TrueResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11650 | if (FalseResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11651 | return FalseResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11652 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11653 | return CondResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11654 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11655 | return NoDiag(); |
| 11656 | // Rare case where the diagnostics depend on which side is evaluated |
| 11657 | // Note that if we get here, CondResult is 0, and at least one of |
| 11658 | // TrueResult and FalseResult is non-zero. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11659 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11660 | return FalseResult; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11661 | return TrueResult; |
| 11662 | } |
| 11663 | case Expr::CXXDefaultArgExprClass: |
| 11664 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 11665 | case Expr::CXXDefaultInitExprClass: |
| 11666 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11667 | case Expr::ChooseExprClass: { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 11668 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11669 | } |
| 11670 | } |
| 11671 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 11672 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11673 | } |
| 11674 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11675 | /// Evaluate an expression as a C++11 integral constant expression. |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11676 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11677 | const Expr *E, |
| 11678 | llvm::APSInt *Value, |
| 11679 | SourceLocation *Loc) { |
Erich Keane | 1ddd4bf | 2018-07-20 17:42:09 +0000 | [diff] [blame] | 11680 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11681 | if (Loc) *Loc = E->getExprLoc(); |
| 11682 | return false; |
| 11683 | } |
| 11684 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11685 | APValue Result; |
| 11686 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 11687 | return false; |
| 11688 | |
Richard Smith | 98710fc | 2014-11-13 23:03:19 +0000 | [diff] [blame] | 11689 | if (!Result.isInt()) { |
| 11690 | if (Loc) *Loc = E->getExprLoc(); |
| 11691 | return false; |
| 11692 | } |
| 11693 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11694 | if (Value) *Value = Result.getInt(); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 11695 | return true; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11696 | } |
| 11697 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11698 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
| 11699 | SourceLocation *Loc) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11700 | if (Ctx.getLangOpts().CPlusPlus11) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 11701 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr, Loc); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11702 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11703 | ICEDiag D = CheckICE(this, Ctx); |
| 11704 | if (D.Kind != IK_ICE) { |
| 11705 | if (Loc) *Loc = D.Loc; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11706 | return false; |
| 11707 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11708 | return true; |
| 11709 | } |
| 11710 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11711 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11712 | SourceLocation *Loc, bool isEvaluated) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11713 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11714 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 11715 | |
| 11716 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 11717 | return false; |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11718 | |
Richard Smith | 5c40f09 | 2015-12-04 03:00:44 +0000 | [diff] [blame] | 11719 | // The only possible side-effects here are due to UB discovered in the |
| 11720 | // evaluation (for instance, INT_MAX + 1). In such a case, we are still |
| 11721 | // required to treat the expression as an ICE, so we produce the folded |
| 11722 | // value. |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11723 | EvalResult ExprResult; |
| 11724 | Expr::EvalStatus Status; |
| 11725 | EvalInfo Info(Ctx, Status, EvalInfo::EM_IgnoreSideEffects); |
| 11726 | Info.InConstantContext = true; |
| 11727 | |
| 11728 | if (!::EvaluateAsInt(this, ExprResult, Ctx, SE_AllowSideEffects, Info)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11729 | llvm_unreachable("ICE cannot be evaluated!"); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11730 | |
| 11731 | Value = ExprResult.Val.getInt(); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11732 | return true; |
| 11733 | } |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11734 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11735 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11736 | return CheckICE(this, Ctx).Kind == IK_ICE; |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 11737 | } |
| 11738 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11739 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11740 | SourceLocation *Loc) const { |
| 11741 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 11742 | // issues. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11743 | assert(Ctx.getLangOpts().CPlusPlus); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11744 | |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 11745 | // Build evaluation settings. |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11746 | Expr::EvalStatus Status; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11747 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11748 | Status.Diag = &Diags; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 11749 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11750 | |
| 11751 | APValue Scratch; |
| 11752 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 11753 | |
| 11754 | if (!Diags.empty()) { |
| 11755 | IsConstExpr = false; |
| 11756 | if (Loc) *Loc = Diags[0].first; |
| 11757 | } else if (!IsConstExpr) { |
| 11758 | // FIXME: This shouldn't happen. |
| 11759 | if (Loc) *Loc = getExprLoc(); |
| 11760 | } |
| 11761 | |
| 11762 | return IsConstExpr; |
| 11763 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11764 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11765 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
| 11766 | const FunctionDecl *Callee, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 11767 | ArrayRef<const Expr*> Args, |
| 11768 | const Expr *This) const { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11769 | Expr::EvalStatus Status; |
| 11770 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
| 11771 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 11772 | LValue ThisVal; |
| 11773 | const LValue *ThisPtr = nullptr; |
| 11774 | if (This) { |
| 11775 | #ifndef NDEBUG |
| 11776 | auto *MD = dyn_cast<CXXMethodDecl>(Callee); |
| 11777 | assert(MD && "Don't provide `this` for non-methods."); |
| 11778 | assert(!MD->isStatic() && "Don't provide `this` for static methods."); |
| 11779 | #endif |
| 11780 | if (EvaluateObjectArgument(Info, This, ThisVal)) |
| 11781 | ThisPtr = &ThisVal; |
| 11782 | if (Info.EvalStatus.HasSideEffects) |
| 11783 | return false; |
| 11784 | } |
| 11785 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11786 | ArgVector ArgValues(Args.size()); |
| 11787 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 11788 | I != E; ++I) { |
Nick Lewycky | f0202ca | 2014-12-16 06:12:01 +0000 | [diff] [blame] | 11789 | if ((*I)->isValueDependent() || |
| 11790 | !Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11791 | // If evaluation fails, throw away the argument entirely. |
| 11792 | ArgValues[I - Args.begin()] = APValue(); |
| 11793 | if (Info.EvalStatus.HasSideEffects) |
| 11794 | return false; |
| 11795 | } |
| 11796 | |
| 11797 | // Build fake call to Callee. |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 11798 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11799 | ArgValues.data()); |
| 11800 | return Evaluate(Value, Info, this) && !Info.EvalStatus.HasSideEffects; |
| 11801 | } |
| 11802 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11803 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11804 | SmallVectorImpl< |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11805 | PartialDiagnosticAt> &Diags) { |
| 11806 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 11807 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 11808 | // ASTs which we build for dependent expressions. |
| 11809 | if (FD->isDependentContext()) |
| 11810 | return true; |
| 11811 | |
| 11812 | Expr::EvalStatus Status; |
| 11813 | Status.Diag = &Diags; |
| 11814 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 11815 | EvalInfo Info(FD->getASTContext(), Status, |
| 11816 | EvalInfo::EM_PotentialConstantExpression); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 11817 | Info.InConstantContext = true; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11818 | |
| 11819 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 11820 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11821 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 11822 | // Fabricate an arbitrary expression on the stack and pretend that it |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11823 | // is a temporary being used as the 'this' pointer. |
| 11824 | LValue This; |
| 11825 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 11826 | This.set({&VIE, Info.CurrentCall->Index}); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11827 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11828 | ArrayRef<const Expr*> Args; |
| 11829 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 11830 | APValue Scratch; |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 11831 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 11832 | // Evaluate the call as a constant initializer, to allow the construction |
| 11833 | // of objects of non-literal types. |
| 11834 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 11835 | HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch); |
| 11836 | } else { |
| 11837 | SourceLocation Loc = FD->getLocation(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 11838 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : nullptr, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 11839 | Args, FD->getBody(), Info, Scratch, nullptr); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 11840 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11841 | |
| 11842 | return Diags.empty(); |
| 11843 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11844 | |
| 11845 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
| 11846 | const FunctionDecl *FD, |
| 11847 | SmallVectorImpl< |
| 11848 | PartialDiagnosticAt> &Diags) { |
| 11849 | Expr::EvalStatus Status; |
| 11850 | Status.Diag = &Diags; |
| 11851 | |
| 11852 | EvalInfo Info(FD->getASTContext(), Status, |
| 11853 | EvalInfo::EM_PotentialConstantExpressionUnevaluated); |
| 11854 | |
| 11855 | // Fabricate a call stack frame to give the arguments a plausible cover story. |
| 11856 | ArrayRef<const Expr*> Args; |
| 11857 | ArgVector ArgValues(0); |
| 11858 | bool Success = EvaluateArgs(Args, ArgValues, Info); |
| 11859 | (void)Success; |
| 11860 | assert(Success && |
| 11861 | "Failed to set up arguments for potential constant evaluation"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 11862 | CallStackFrame Frame(Info, SourceLocation(), FD, nullptr, ArgValues.data()); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11863 | |
| 11864 | APValue ResultScratch; |
| 11865 | Evaluate(ResultScratch, Info, E); |
| 11866 | return Diags.empty(); |
| 11867 | } |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 11868 | |
| 11869 | bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
| 11870 | unsigned Type) const { |
| 11871 | if (!getType()->isPointerType()) |
| 11872 | return false; |
| 11873 | |
| 11874 | Expr::EvalStatus Status; |
| 11875 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 11876 | return tryEvaluateBuiltinObjectSize(this, Type, Info, Result); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 11877 | } |