Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr constant evaluator. |
| 11 | // |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 12 | // Constant expression evaluation produces four main results: |
| 13 | // |
| 14 | // * A success/failure flag indicating whether constant folding was successful. |
| 15 | // This is the 'bool' return value used by most of the code in this file. A |
| 16 | // 'false' return value indicates that constant folding has failed, and any |
| 17 | // appropriate diagnostic has already been produced. |
| 18 | // |
| 19 | // * An evaluated result, valid only if constant folding has not failed. |
| 20 | // |
| 21 | // * A flag indicating if evaluation encountered (unevaluated) side-effects. |
| 22 | // These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1), |
| 23 | // where it is possible to determine the evaluated result regardless. |
| 24 | // |
| 25 | // * A set of notes indicating why the evaluation was not a constant expression |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 26 | // (under the C++11 / C++1y rules only, at the moment), or, if folding failed |
| 27 | // too, why the expression could not be folded. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 28 | // |
| 29 | // If we are checking for a potential constant expression, failure to constant |
| 30 | // fold a potential constant sub-expression will be indicated by a 'false' |
| 31 | // return value (the expression could not be folded) and no diagnostic (the |
| 32 | // expression is not necessarily non-constant). |
| 33 | // |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "clang/AST/APValue.h" |
| 37 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 38 | #include "clang/AST/ASTDiagnostic.h" |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 39 | #include "clang/AST/ASTLambda.h" |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 40 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 41 | #include "clang/AST/Expr.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" |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 46 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 47 | #include "llvm/Support/raw_ostream.h" |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 48 | #include <cstring> |
Richard Smith | c804232 | 2012-02-01 05:53:12 +0000 | [diff] [blame] | 49 | #include <functional> |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 50 | |
Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 51 | #define DEBUG_TYPE "exprconstant" |
| 52 | |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 53 | using namespace clang; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 54 | using llvm::APSInt; |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 55 | using llvm::APFloat; |
Anders Carlsson | 7a241ba | 2008-07-03 04:20:39 +0000 | [diff] [blame] | 56 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 57 | static bool IsGlobalLValue(APValue::LValueBase B); |
| 58 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 59 | namespace { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 60 | struct LValue; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 61 | struct CallStackFrame; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 62 | struct EvalInfo; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 63 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 64 | static QualType getType(APValue::LValueBase B) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 65 | if (!B) return QualType(); |
Richard Smith | 69cf59e | 2018-03-09 02:00:01 +0000 | [diff] [blame] | 66 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 67 | // 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] | 68 | // this actually matters for arrays of unknown bound. Eg: |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 69 | // |
| 70 | // extern int arr[]; void f() { extern int arr[3]; }; |
| 71 | // constexpr int *p = &arr[1]; // valid? |
Richard Smith | 69cf59e | 2018-03-09 02:00:01 +0000 | [diff] [blame] | 72 | // |
| 73 | // For now, we take the array bound from the most recent declaration. |
| 74 | for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl; |
| 75 | Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())) { |
| 76 | QualType T = Redecl->getType(); |
| 77 | if (!T->isIncompleteArrayType()) |
| 78 | return T; |
| 79 | } |
| 80 | return D->getType(); |
| 81 | } |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 82 | |
| 83 | const Expr *Base = B.get<const Expr*>(); |
| 84 | |
| 85 | // For a materialized temporary, the type of the temporary we materialized |
| 86 | // may not be the type of the expression. |
| 87 | if (const MaterializeTemporaryExpr *MTE = |
| 88 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 89 | SmallVector<const Expr *, 2> CommaLHSs; |
| 90 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 91 | const Expr *Temp = MTE->GetTemporaryExpr(); |
| 92 | const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs, |
| 93 | Adjustments); |
| 94 | // Keep any cv-qualifiers from the reference if we generated a temporary |
Richard Smith | b8c0f55 | 2016-12-09 18:49:13 +0000 | [diff] [blame] | 95 | // for it directly. Otherwise use the type after adjustment. |
| 96 | if (!Adjustments.empty()) |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 97 | return Inner->getType(); |
| 98 | } |
| 99 | |
| 100 | return Base->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 103 | /// 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] | 104 | /// field or base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 105 | static |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 106 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 107 | APValue::BaseOrMemberType Value; |
| 108 | Value.setFromOpaqueValue(E.BaseOrMember); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 109 | return Value; |
| 110 | } |
| 111 | |
| 112 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 113 | /// field declaration. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 114 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 115 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 116 | } |
| 117 | /// Get an LValue path entry, which is known to not be an array index, as a |
| 118 | /// base class declaration. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 119 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 120 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 121 | } |
| 122 | /// Determine whether this LValue path entry for a base class names a virtual |
| 123 | /// base class. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 124 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 125 | return getAsBaseOrMember(E).getInt(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 126 | } |
| 127 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 128 | /// Given a CallExpr, try to get the alloc_size attribute. May return null. |
| 129 | static const AllocSizeAttr *getAllocSizeAttr(const CallExpr *CE) { |
| 130 | const FunctionDecl *Callee = CE->getDirectCallee(); |
| 131 | return Callee ? Callee->getAttr<AllocSizeAttr>() : nullptr; |
| 132 | } |
| 133 | |
| 134 | /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr. |
| 135 | /// This will look through a single cast. |
| 136 | /// |
| 137 | /// Returns null if we couldn't unwrap a function with alloc_size. |
| 138 | static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) { |
| 139 | if (!E->getType()->isPointerType()) |
| 140 | return nullptr; |
| 141 | |
| 142 | E = E->IgnoreParens(); |
| 143 | // 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] | 144 | // probably be a cast of some kind. In exotic cases, we might also see a |
| 145 | // top-level ExprWithCleanups. Ignore them either way. |
| 146 | if (const auto *EC = dyn_cast<ExprWithCleanups>(E)) |
| 147 | E = EC->getSubExpr()->IgnoreParens(); |
| 148 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 149 | if (const auto *Cast = dyn_cast<CastExpr>(E)) |
| 150 | E = Cast->getSubExpr()->IgnoreParens(); |
| 151 | |
| 152 | if (const auto *CE = dyn_cast<CallExpr>(E)) |
| 153 | return getAllocSizeAttr(CE) ? CE : nullptr; |
| 154 | return nullptr; |
| 155 | } |
| 156 | |
| 157 | /// Determines whether or not the given Base contains a call to a function |
| 158 | /// with the alloc_size attribute. |
| 159 | static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) { |
| 160 | const auto *E = Base.dyn_cast<const Expr *>(); |
| 161 | return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E); |
| 162 | } |
| 163 | |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 164 | /// The bound to claim that an array of unknown bound has. |
| 165 | /// The value in MostDerivedArraySize is undefined in this case. So, set it |
| 166 | /// to an arbitrary value that's likely to loudly break things if it's used. |
| 167 | static const uint64_t AssumedSizeForUnsizedArray = |
| 168 | std::numeric_limits<uint64_t>::max() / 2; |
| 169 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 170 | /// Determines if an LValue with the given LValueBase will have an unsized |
| 171 | /// array in its designator. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 172 | /// Find the path length and type of the most-derived subobject in the given |
| 173 | /// path, and find the size of the containing array, if any. |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 174 | static unsigned |
| 175 | findMostDerivedSubobject(ASTContext &Ctx, APValue::LValueBase Base, |
| 176 | ArrayRef<APValue::LValuePathEntry> Path, |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 177 | uint64_t &ArraySize, QualType &Type, bool &IsArray, |
| 178 | bool &FirstEntryIsUnsizedArray) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 179 | // This only accepts LValueBases from APValues, and APValues don't support |
| 180 | // arrays that lack size info. |
| 181 | assert(!isBaseAnAllocSizeCall(Base) && |
| 182 | "Unsized arrays shouldn't appear here"); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 183 | unsigned MostDerivedLength = 0; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 184 | Type = getType(Base); |
| 185 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 186 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 187 | if (Type->isArrayType()) { |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 188 | const ArrayType *AT = Ctx.getAsArrayType(Type); |
| 189 | Type = AT->getElementType(); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 190 | MostDerivedLength = I + 1; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 191 | IsArray = true; |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 192 | |
| 193 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 194 | ArraySize = CAT->getSize().getZExtValue(); |
| 195 | } else { |
| 196 | assert(I == 0 && "unexpected unsized array designator"); |
| 197 | FirstEntryIsUnsizedArray = true; |
| 198 | ArraySize = AssumedSizeForUnsizedArray; |
| 199 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 200 | } else if (Type->isAnyComplexType()) { |
| 201 | const ComplexType *CT = Type->castAs<ComplexType>(); |
| 202 | Type = CT->getElementType(); |
| 203 | ArraySize = 2; |
| 204 | MostDerivedLength = I + 1; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 205 | IsArray = true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 206 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
| 207 | Type = FD->getType(); |
| 208 | ArraySize = 0; |
| 209 | MostDerivedLength = I + 1; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 210 | IsArray = false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 211 | } else { |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 212 | // Path[I] describes a base class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 213 | ArraySize = 0; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 214 | IsArray = false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 215 | } |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 216 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 217 | return MostDerivedLength; |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 220 | // The order of this enum is important for diagnostics. |
| 221 | enum CheckSubobjectKind { |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 222 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 223 | CSK_This, CSK_Real, CSK_Imag |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 226 | /// A path from a glvalue to a subobject of that glvalue. |
| 227 | struct SubobjectDesignator { |
| 228 | /// True if the subobject was named in a manner not supported by C++11. Such |
| 229 | /// lvalues can still be folded, but they are not core constant expressions |
| 230 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
Akira Hatanaka | 3a94477 | 2016-06-30 00:07:17 +0000 | [diff] [blame] | 231 | unsigned Invalid : 1; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 232 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 233 | /// Is this a pointer one past the end of an object? |
Akira Hatanaka | 3a94477 | 2016-06-30 00:07:17 +0000 | [diff] [blame] | 234 | unsigned IsOnePastTheEnd : 1; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 235 | |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 236 | /// Indicator of whether the first entry is an unsized array. |
| 237 | unsigned FirstEntryIsAnUnsizedArray : 1; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 238 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 239 | /// Indicator of whether the most-derived object is an array element. |
Akira Hatanaka | 3a94477 | 2016-06-30 00:07:17 +0000 | [diff] [blame] | 240 | unsigned MostDerivedIsArrayElement : 1; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 241 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 242 | /// The length of the path to the most-derived object of which this is a |
| 243 | /// subobject. |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 244 | unsigned MostDerivedPathLength : 28; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 245 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 246 | /// The size of the array of which the most-derived object is an element. |
| 247 | /// This will always be 0 if the most-derived object is not an array |
| 248 | /// element. 0 is not an indicator of whether or not the most-derived object |
| 249 | /// is an array, however, because 0-length arrays are allowed. |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 250 | /// |
| 251 | /// If the current array is an unsized array, the value of this is |
| 252 | /// undefined. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 253 | uint64_t MostDerivedArraySize; |
| 254 | |
| 255 | /// The type of the most derived object referred to by this address. |
| 256 | QualType MostDerivedType; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 257 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 258 | typedef APValue::LValuePathEntry PathEntry; |
| 259 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 260 | /// The entries on the path from the glvalue to the designated subobject. |
| 261 | SmallVector<PathEntry, 8> Entries; |
| 262 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 263 | SubobjectDesignator() : Invalid(true) {} |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 264 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 265 | explicit SubobjectDesignator(QualType T) |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 266 | : Invalid(false), IsOnePastTheEnd(false), |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 267 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 268 | MostDerivedPathLength(0), MostDerivedArraySize(0), |
| 269 | MostDerivedType(T) {} |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 270 | |
| 271 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 272 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 273 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 274 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
| 275 | assert(V.isLValue() && "Non-LValue used to make an LValue designator?"); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 276 | if (!Invalid) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 277 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 278 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
| 279 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 280 | if (V.getLValueBase()) { |
| 281 | bool IsArray = false; |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 282 | bool FirstIsUnsizedArray = false; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 283 | MostDerivedPathLength = findMostDerivedSubobject( |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 284 | Ctx, V.getLValueBase(), V.getLValuePath(), MostDerivedArraySize, |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 285 | MostDerivedType, IsArray, FirstIsUnsizedArray); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 286 | MostDerivedIsArrayElement = IsArray; |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 287 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 288 | } |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 292 | void setInvalid() { |
| 293 | Invalid = true; |
| 294 | Entries.clear(); |
| 295 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 296 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 297 | /// Determine whether the most derived subobject is an array without a |
| 298 | /// known bound. |
| 299 | bool isMostDerivedAnUnsizedArray() const { |
| 300 | assert(!Invalid && "Calling this makes no sense on invalid designators"); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 301 | return Entries.size() == 1 && FirstEntryIsAnUnsizedArray; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /// Determine what the most derived array's size is. Results in an assertion |
| 305 | /// failure if the most derived array lacks a size. |
| 306 | uint64_t getMostDerivedArraySize() const { |
| 307 | assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size"); |
| 308 | return MostDerivedArraySize; |
| 309 | } |
| 310 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 311 | /// Determine whether this is a one-past-the-end pointer. |
| 312 | bool isOnePastTheEnd() const { |
Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 313 | assert(!Invalid); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 314 | if (IsOnePastTheEnd) |
| 315 | return true; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 316 | if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement && |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 317 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
| 318 | return true; |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | /// Check that this refers to a valid subobject. |
| 323 | bool isValidSubobject() const { |
| 324 | if (Invalid) |
| 325 | return false; |
| 326 | return !isOnePastTheEnd(); |
| 327 | } |
| 328 | /// Check that this refers to a valid subobject, and if not, produce a |
| 329 | /// relevant diagnostic and set the designator as invalid. |
| 330 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
| 331 | |
| 332 | /// Update this designator to refer to the first element within this array. |
| 333 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 334 | PathEntry Entry; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 335 | Entry.ArrayIndex = 0; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 336 | Entries.push_back(Entry); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 337 | |
| 338 | // This is a most-derived object. |
| 339 | MostDerivedType = CAT->getElementType(); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 340 | MostDerivedIsArrayElement = true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 341 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
| 342 | MostDerivedPathLength = Entries.size(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 343 | } |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 344 | /// Update this designator to refer to the first element within the array of |
| 345 | /// elements of type T. This is an array of unknown size. |
| 346 | void addUnsizedArrayUnchecked(QualType ElemTy) { |
| 347 | PathEntry Entry; |
| 348 | Entry.ArrayIndex = 0; |
| 349 | Entries.push_back(Entry); |
| 350 | |
| 351 | MostDerivedType = ElemTy; |
| 352 | MostDerivedIsArrayElement = true; |
| 353 | // The value in MostDerivedArraySize is undefined in this case. So, set it |
| 354 | // to an arbitrary value that's likely to loudly break things if it's |
| 355 | // used. |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 356 | MostDerivedArraySize = AssumedSizeForUnsizedArray; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 357 | MostDerivedPathLength = Entries.size(); |
| 358 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 359 | /// Update this designator to refer to the given base or member of this |
| 360 | /// object. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 361 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 362 | PathEntry Entry; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 363 | APValue::BaseOrMemberType Value(D, Virtual); |
| 364 | Entry.BaseOrMember = Value.getOpaqueValue(); |
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 | // If this isn't a base class, it's a new most-derived object. |
| 368 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 369 | MostDerivedType = FD->getType(); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 370 | MostDerivedIsArrayElement = false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 371 | MostDerivedArraySize = 0; |
| 372 | MostDerivedPathLength = Entries.size(); |
| 373 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 374 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 375 | /// Update this designator to refer to the given complex component. |
| 376 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
| 377 | PathEntry Entry; |
| 378 | Entry.ArrayIndex = Imag; |
| 379 | Entries.push_back(Entry); |
| 380 | |
| 381 | // This is technically a most-derived object, though in practice this |
| 382 | // is unlikely to matter. |
| 383 | MostDerivedType = EltTy; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 384 | MostDerivedIsArrayElement = true; |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 385 | MostDerivedArraySize = 2; |
| 386 | MostDerivedPathLength = Entries.size(); |
| 387 | } |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 388 | void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E); |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 389 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, |
| 390 | const APSInt &N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 391 | /// Add N to the address of this subobject. |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 392 | void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N) { |
| 393 | if (Invalid || !N) return; |
| 394 | uint64_t TruncatedN = N.extOrTrunc(64).getZExtValue(); |
| 395 | if (isMostDerivedAnUnsizedArray()) { |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 396 | diagnoseUnsizedArrayPointerArithmetic(Info, E); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 397 | // Can't verify -- trust that the user is doing the right thing (or if |
| 398 | // not, trust that the caller will catch the bad behavior). |
| 399 | // FIXME: Should we reject if this overflows, at least? |
| 400 | Entries.back().ArrayIndex += TruncatedN; |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
| 405 | // nonarray object behaves the same as a pointer to the first element of |
| 406 | // an array of length one with the type of the object as its element type. |
| 407 | bool IsArray = MostDerivedPathLength == Entries.size() && |
| 408 | MostDerivedIsArrayElement; |
| 409 | uint64_t ArrayIndex = |
| 410 | IsArray ? Entries.back().ArrayIndex : (uint64_t)IsOnePastTheEnd; |
| 411 | uint64_t ArraySize = |
| 412 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
| 413 | |
| 414 | if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) { |
| 415 | // Calculate the actual index in a wide enough type, so we can include |
| 416 | // it in the note. |
| 417 | N = N.extend(std::max<unsigned>(N.getBitWidth() + 1, 65)); |
| 418 | (llvm::APInt&)N += ArrayIndex; |
| 419 | assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index"); |
| 420 | diagnosePointerArithmetic(Info, E, N); |
| 421 | setInvalid(); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | ArrayIndex += TruncatedN; |
| 426 | assert(ArrayIndex <= ArraySize && |
| 427 | "bounds check succeeded for out-of-bounds index"); |
| 428 | |
| 429 | if (IsArray) |
| 430 | Entries.back().ArrayIndex = ArrayIndex; |
| 431 | else |
| 432 | IsOnePastTheEnd = (ArrayIndex != 0); |
| 433 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 434 | }; |
| 435 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 436 | /// A stack frame in the constexpr call stack. |
| 437 | struct CallStackFrame { |
| 438 | EvalInfo &Info; |
| 439 | |
| 440 | /// Parent - The caller of this stack frame. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 441 | CallStackFrame *Caller; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 442 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 443 | /// Callee - The function which was called. |
| 444 | const FunctionDecl *Callee; |
| 445 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 446 | /// This - The binding for the this pointer in this call, if any. |
| 447 | const LValue *This; |
| 448 | |
Nick Lewycky | e2b2caa | 2013-09-22 10:07:22 +0000 | [diff] [blame] | 449 | /// Arguments - Parameter bindings for this function call, indexed by |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 450 | /// parameters' function scope indices. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 451 | APValue *Arguments; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 452 | |
Eli Friedman | 4830ec8 | 2012-06-25 21:21:08 +0000 | [diff] [blame] | 453 | // Note that we intentionally use std::map here so that references to |
| 454 | // values are stable. |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 455 | typedef std::pair<const void *, unsigned> MapKeyTy; |
| 456 | typedef std::map<MapKeyTy, APValue> MapTy; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 457 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
| 458 | MapTy Temporaries; |
| 459 | |
Alexander Shaposhnikov | fbcf29b | 2016-09-19 15:57:29 +0000 | [diff] [blame] | 460 | /// CallLoc - The location of the call expression for this call. |
| 461 | SourceLocation CallLoc; |
| 462 | |
| 463 | /// Index - The call index of this call. |
| 464 | unsigned Index; |
| 465 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 466 | /// The stack of integers for tracking version numbers for temporaries. |
| 467 | SmallVector<unsigned, 2> TempVersionStack = {1}; |
| 468 | unsigned CurTempVersion = TempVersionStack.back(); |
| 469 | |
| 470 | unsigned getTempVersion() const { return TempVersionStack.back(); } |
| 471 | |
| 472 | void pushTempVersion() { |
| 473 | TempVersionStack.push_back(++CurTempVersion); |
| 474 | } |
| 475 | |
| 476 | void popTempVersion() { |
| 477 | TempVersionStack.pop_back(); |
| 478 | } |
| 479 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 480 | // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact |
| 481 | // on the overall stack usage of deeply-recursing constexpr evaluataions. |
| 482 | // (We should cache this map rather than recomputing it repeatedly.) |
| 483 | // But let's try this and see how it goes; we can look into caching the map |
| 484 | // as a later change. |
| 485 | |
| 486 | /// LambdaCaptureFields - Mapping from captured variables/this to |
| 487 | /// corresponding data members in the closure class. |
| 488 | llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; |
| 489 | FieldDecl *LambdaThisCaptureField; |
| 490 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 491 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 492 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 493 | APValue *Arguments); |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 494 | ~CallStackFrame(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 495 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 496 | // Return the temporary for Key whose version number is Version. |
| 497 | APValue *getTemporary(const void *Key, unsigned Version) { |
| 498 | MapKeyTy KV(Key, Version); |
| 499 | auto LB = Temporaries.lower_bound(KV); |
| 500 | if (LB != Temporaries.end() && LB->first == KV) |
| 501 | return &LB->second; |
| 502 | // Pair (Key,Version) wasn't found in the map. Check that no elements |
| 503 | // in the map have 'Key' as their key. |
| 504 | assert((LB == Temporaries.end() || LB->first.first != Key) && |
| 505 | (LB == Temporaries.begin() || std::prev(LB)->first.first != Key) && |
| 506 | "Element with key 'Key' found in map"); |
| 507 | return nullptr; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 508 | } |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 509 | |
| 510 | // Return the current temporary for Key in the map. |
| 511 | APValue *getCurrentTemporary(const void *Key) { |
| 512 | auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX)); |
| 513 | if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key) |
| 514 | return &std::prev(UB)->second; |
| 515 | return nullptr; |
| 516 | } |
| 517 | |
| 518 | // Return the version number of the current temporary for Key. |
| 519 | unsigned getCurrentTemporaryVersion(const void *Key) const { |
| 520 | auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX)); |
| 521 | if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key) |
| 522 | return std::prev(UB)->first.second; |
| 523 | return 0; |
| 524 | } |
| 525 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 526 | APValue &createTemporary(const void *Key, bool IsLifetimeExtended); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 527 | }; |
| 528 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 529 | /// Temporarily override 'this'. |
| 530 | class ThisOverrideRAII { |
| 531 | public: |
| 532 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
| 533 | : Frame(Frame), OldThis(Frame.This) { |
| 534 | if (Enable) |
| 535 | Frame.This = NewThis; |
| 536 | } |
| 537 | ~ThisOverrideRAII() { |
| 538 | Frame.This = OldThis; |
| 539 | } |
| 540 | private: |
| 541 | CallStackFrame &Frame; |
| 542 | const LValue *OldThis; |
| 543 | }; |
| 544 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 545 | /// A partial diagnostic which we might know in advance that we are not going |
| 546 | /// to emit. |
| 547 | class OptionalDiagnostic { |
| 548 | PartialDiagnostic *Diag; |
| 549 | |
| 550 | public: |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 551 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = nullptr) |
| 552 | : Diag(Diag) {} |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 553 | |
| 554 | template<typename T> |
| 555 | OptionalDiagnostic &operator<<(const T &v) { |
| 556 | if (Diag) |
| 557 | *Diag << v; |
| 558 | return *this; |
| 559 | } |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 560 | |
| 561 | OptionalDiagnostic &operator<<(const APSInt &I) { |
| 562 | if (Diag) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 563 | SmallVector<char, 32> Buffer; |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 564 | I.toString(Buffer); |
| 565 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 566 | } |
| 567 | return *this; |
| 568 | } |
| 569 | |
| 570 | OptionalDiagnostic &operator<<(const APFloat &F) { |
| 571 | if (Diag) { |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 572 | // FIXME: Force the precision of the source value down so we don't |
| 573 | // print digits which are usually useless (we don't really care here if |
| 574 | // we truncate a digit by accident in edge cases). Ideally, |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 575 | // APFloat::toString would automatically print the shortest |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 576 | // representation which rounds to the correct value, but it's a bit |
| 577 | // tricky to implement. |
| 578 | unsigned precision = |
| 579 | llvm::APFloat::semanticsPrecision(F.getSemantics()); |
| 580 | precision = (precision * 59 + 195) / 196; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 581 | SmallVector<char, 32> Buffer; |
Eli Friedman | 0718591 | 2013-08-29 23:44:43 +0000 | [diff] [blame] | 582 | F.toString(Buffer, precision); |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 583 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
| 584 | } |
| 585 | return *this; |
| 586 | } |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 587 | }; |
| 588 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 589 | /// A cleanup, and a flag indicating whether it is lifetime-extended. |
| 590 | class Cleanup { |
| 591 | llvm::PointerIntPair<APValue*, 1, bool> Value; |
| 592 | |
| 593 | public: |
| 594 | Cleanup(APValue *Val, bool IsLifetimeExtended) |
| 595 | : Value(Val, IsLifetimeExtended) {} |
| 596 | |
| 597 | bool isLifetimeExtended() const { return Value.getInt(); } |
| 598 | void endLifetime() { |
| 599 | *Value.getPointer() = APValue(); |
| 600 | } |
| 601 | }; |
| 602 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 603 | /// EvalInfo - This is a private struct used by the evaluator to capture |
| 604 | /// information about a subexpression as it is folded. It retains information |
| 605 | /// about the AST context, but also maintains information about the folded |
| 606 | /// expression. |
| 607 | /// |
| 608 | /// If an expression could be evaluated, it is still possible it is not a C |
| 609 | /// "integer constant expression" or constant expression. If not, this struct |
| 610 | /// captures information about how and why not. |
| 611 | /// |
| 612 | /// One bit of information passed *into* the request for constant folding |
| 613 | /// indicates whether the subexpression is "evaluated" or not according to C |
| 614 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
| 615 | /// evaluate the expression regardless of what the RHS is, but C only allows |
| 616 | /// certain things in certain situations. |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 617 | struct EvalInfo { |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 618 | ASTContext &Ctx; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 619 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 620 | /// EvalStatus - Contains information about the evaluation. |
| 621 | Expr::EvalStatus &EvalStatus; |
| 622 | |
| 623 | /// CurrentCall - The top of the constexpr call stack. |
| 624 | CallStackFrame *CurrentCall; |
| 625 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 626 | /// CallStackDepth - The number of calls in the call stack right now. |
| 627 | unsigned CallStackDepth; |
| 628 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 629 | /// NextCallIndex - The next call index to assign. |
| 630 | unsigned NextCallIndex; |
| 631 | |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 632 | /// StepsLeft - The remaining number of evaluation steps we're permitted |
| 633 | /// to perform. This is essentially a limit for the number of statements |
| 634 | /// we will evaluate. |
| 635 | unsigned StepsLeft; |
| 636 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 637 | /// BottomFrame - The frame in which evaluation started. This must be |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 638 | /// initialized after CurrentCall and CallStackDepth. |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 639 | CallStackFrame BottomFrame; |
| 640 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 641 | /// A stack of values whose lifetimes end at the end of some surrounding |
| 642 | /// evaluation frame. |
| 643 | llvm::SmallVector<Cleanup, 16> CleanupStack; |
| 644 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 645 | /// EvaluatingDecl - This is the declaration whose initializer is being |
| 646 | /// evaluated, if any. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 647 | APValue::LValueBase EvaluatingDecl; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 648 | |
| 649 | /// EvaluatingDeclValue - This is the value being constructed for the |
| 650 | /// declaration whose initializer is being evaluated, if any. |
| 651 | APValue *EvaluatingDeclValue; |
| 652 | |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 653 | /// EvaluatingObject - Pair of the AST node that an lvalue represents and |
| 654 | /// the call index that that lvalue was allocated in. |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 655 | typedef std::pair<APValue::LValueBase, std::pair<unsigned, unsigned>> |
| 656 | EvaluatingObject; |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 657 | |
| 658 | /// EvaluatingConstructors - Set of objects that are currently being |
| 659 | /// constructed. |
| 660 | llvm::DenseSet<EvaluatingObject> EvaluatingConstructors; |
| 661 | |
| 662 | struct EvaluatingConstructorRAII { |
| 663 | EvalInfo &EI; |
| 664 | EvaluatingObject Object; |
| 665 | bool DidInsert; |
| 666 | EvaluatingConstructorRAII(EvalInfo &EI, EvaluatingObject Object) |
| 667 | : EI(EI), Object(Object) { |
| 668 | DidInsert = EI.EvaluatingConstructors.insert(Object).second; |
| 669 | } |
| 670 | ~EvaluatingConstructorRAII() { |
| 671 | if (DidInsert) EI.EvaluatingConstructors.erase(Object); |
| 672 | } |
| 673 | }; |
| 674 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 675 | bool isEvaluatingConstructor(APValue::LValueBase Decl, unsigned CallIndex, |
| 676 | unsigned Version) { |
| 677 | return EvaluatingConstructors.count( |
| 678 | EvaluatingObject(Decl, {CallIndex, Version})); |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 681 | /// The current array initialization index, if we're performing array |
| 682 | /// initialization. |
| 683 | uint64_t ArrayInitIndex = -1; |
| 684 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 685 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
| 686 | /// notes attached to it will also be stored, otherwise they will not be. |
| 687 | bool HasActiveDiagnostic; |
| 688 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 689 | /// Have we emitted a diagnostic explaining why we couldn't constant |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 690 | /// fold (not just why it's not strictly a constant expression)? |
| 691 | bool HasFoldFailureDiagnostic; |
| 692 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 693 | /// Whether or not we're currently speculatively evaluating. |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 694 | bool IsSpeculativelyEvaluating; |
| 695 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 696 | enum EvaluationMode { |
| 697 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 698 | /// is not a constant expression. |
| 699 | EM_ConstantExpression, |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 700 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 701 | /// Evaluate as a potential constant expression. Keep going if we hit a |
| 702 | /// construct that we can't evaluate yet (because we don't yet know the |
| 703 | /// value of something) but stop if we hit something that could never be |
| 704 | /// a constant expression. |
| 705 | EM_PotentialConstantExpression, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 706 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 707 | /// Fold the expression to a constant. Stop if we hit a side-effect that |
| 708 | /// we can't model. |
| 709 | EM_ConstantFold, |
| 710 | |
| 711 | /// Evaluate the expression looking for integer overflow and similar |
| 712 | /// issues. Don't worry about side-effects, and try to visit all |
| 713 | /// subexpressions. |
| 714 | EM_EvaluateForOverflow, |
| 715 | |
| 716 | /// Evaluate in any way we know how. Don't worry about side-effects that |
| 717 | /// can't be modeled. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 718 | EM_IgnoreSideEffects, |
| 719 | |
| 720 | /// Evaluate as a constant expression. Stop if we find that the expression |
| 721 | /// is not a constant expression. Some expressions can be retried in the |
| 722 | /// optimizer if we don't constant fold them here, but in an unevaluated |
| 723 | /// context we try to fold them immediately since the optimizer never |
| 724 | /// gets a chance to look at it. |
| 725 | EM_ConstantExpressionUnevaluated, |
| 726 | |
| 727 | /// Evaluate as a potential constant expression. Keep going if we hit a |
| 728 | /// construct that we can't evaluate yet (because we don't yet know the |
| 729 | /// value of something) but stop if we hit something that could never be |
| 730 | /// a constant expression. Some expressions can be retried in the |
| 731 | /// optimizer if we don't constant fold them here, but in an unevaluated |
| 732 | /// context we try to fold them immediately since the optimizer never |
| 733 | /// gets a chance to look at it. |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 734 | EM_PotentialConstantExpressionUnevaluated, |
| 735 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 736 | /// Evaluate as a constant expression. In certain scenarios, if: |
| 737 | /// - we find a MemberExpr with a base that can't be evaluated, or |
| 738 | /// - we find a variable initialized with a call to a function that has |
| 739 | /// the alloc_size attribute on it |
| 740 | /// then we may consider evaluation to have succeeded. |
| 741 | /// |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 742 | /// In either case, the LValue returned shall have an invalid base; in the |
| 743 | /// former, the base will be the invalid MemberExpr, in the latter, the |
| 744 | /// base will be either the alloc_size CallExpr or a CastExpr wrapping |
| 745 | /// said CallExpr. |
| 746 | EM_OffsetFold, |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 747 | } EvalMode; |
| 748 | |
| 749 | /// Are we checking whether the expression is a potential constant |
| 750 | /// expression? |
| 751 | bool checkingPotentialConstantExpression() const { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 752 | return EvalMode == EM_PotentialConstantExpression || |
| 753 | EvalMode == EM_PotentialConstantExpressionUnevaluated; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /// Are we checking an expression for overflow? |
| 757 | // FIXME: We should check for any kind of undefined or suspicious behavior |
| 758 | // in such constructs, not just overflow. |
| 759 | bool checkingForOverflow() { return EvalMode == EM_EvaluateForOverflow; } |
| 760 | |
| 761 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 762 | : Ctx(const_cast<ASTContext &>(C)), EvalStatus(S), CurrentCall(nullptr), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 763 | CallStackDepth(0), NextCallIndex(1), |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 764 | StepsLeft(getLangOpts().ConstexprStepLimit), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 765 | BottomFrame(*this, SourceLocation(), nullptr, nullptr, nullptr), |
| 766 | EvaluatingDecl((const ValueDecl *)nullptr), |
| 767 | EvaluatingDeclValue(nullptr), HasActiveDiagnostic(false), |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 768 | HasFoldFailureDiagnostic(false), IsSpeculativelyEvaluating(false), |
| 769 | EvalMode(Mode) {} |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 770 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 771 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value) { |
| 772 | EvaluatingDecl = Base; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 773 | EvaluatingDeclValue = &Value; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 774 | EvaluatingConstructors.insert({Base, {0, 0}}); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 775 | } |
| 776 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 777 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 778 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 779 | bool CheckCallLimit(SourceLocation Loc) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 780 | // Don't perform any constexpr calls (other than the call we're checking) |
| 781 | // when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 782 | if (checkingPotentialConstantExpression() && CallStackDepth > 1) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 783 | return false; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 784 | if (NextCallIndex == 0) { |
| 785 | // NextCallIndex has wrapped around. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 786 | FFDiag(Loc, diag::note_constexpr_call_limit_exceeded); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 787 | return false; |
| 788 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 789 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 790 | return true; |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 791 | FFDiag(Loc, diag::note_constexpr_depth_limit_exceeded) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 792 | << getLangOpts().ConstexprCallDepth; |
| 793 | return false; |
Richard Smith | 9a56882 | 2011-11-21 19:36:32 +0000 | [diff] [blame] | 794 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 795 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 796 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
| 797 | assert(CallIndex && "no call index in getCallFrame"); |
| 798 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
| 799 | // be null in this loop. |
| 800 | CallStackFrame *Frame = CurrentCall; |
| 801 | while (Frame->Index > CallIndex) |
| 802 | Frame = Frame->Caller; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 803 | return (Frame->Index == CallIndex) ? Frame : nullptr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 806 | bool nextStep(const Stmt *S) { |
| 807 | if (!StepsLeft) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 808 | FFDiag(S->getLocStart(), diag::note_constexpr_step_limit_exceeded); |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 809 | return false; |
| 810 | } |
| 811 | --StepsLeft; |
| 812 | return true; |
| 813 | } |
| 814 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 815 | private: |
| 816 | /// Add a diagnostic to the diagnostics list. |
| 817 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
| 818 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
| 819 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
| 820 | return EvalStatus.Diag->back().second; |
| 821 | } |
| 822 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 823 | /// Add notes containing a call stack to the current point of evaluation. |
| 824 | void addCallStack(unsigned Limit); |
| 825 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 826 | private: |
| 827 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId, |
| 828 | unsigned ExtraNotes, bool IsCCEDiag) { |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 829 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 830 | if (EvalStatus.Diag) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 831 | // If we have a prior diagnostic, it will be noting that the expression |
| 832 | // isn't a constant expression. This diagnostic is more important, |
| 833 | // unless we require this evaluation to produce a constant expression. |
| 834 | // |
| 835 | // FIXME: We might want to show both diagnostics to the user in |
| 836 | // EM_ConstantFold mode. |
| 837 | if (!EvalStatus.Diag->empty()) { |
| 838 | switch (EvalMode) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 839 | case EM_ConstantFold: |
| 840 | case EM_IgnoreSideEffects: |
| 841 | case EM_EvaluateForOverflow: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 842 | if (!HasFoldFailureDiagnostic) |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 843 | break; |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 844 | // We've already failed to fold something. Keep that diagnostic. |
Galina Kistanova | f87496d | 2017-06-03 06:31:42 +0000 | [diff] [blame] | 845 | LLVM_FALLTHROUGH; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 846 | case EM_ConstantExpression: |
| 847 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 848 | case EM_ConstantExpressionUnevaluated: |
| 849 | case EM_PotentialConstantExpressionUnevaluated: |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 850 | case EM_OffsetFold: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 851 | HasActiveDiagnostic = false; |
| 852 | return OptionalDiagnostic(); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 856 | unsigned CallStackNotes = CallStackDepth - 1; |
| 857 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
| 858 | if (Limit) |
| 859 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 860 | if (checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 861 | CallStackNotes = 0; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 862 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 863 | HasActiveDiagnostic = true; |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 864 | HasFoldFailureDiagnostic = !IsCCEDiag; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 865 | EvalStatus.Diag->clear(); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 866 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
| 867 | addDiag(Loc, DiagId); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 868 | if (!checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 869 | addCallStack(Limit); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 870 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 871 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 872 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 873 | return OptionalDiagnostic(); |
| 874 | } |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 875 | public: |
| 876 | // Diagnose that the evaluation could not be folded (FF => FoldFailure) |
| 877 | OptionalDiagnostic |
| 878 | FFDiag(SourceLocation Loc, |
| 879 | diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr, |
| 880 | unsigned ExtraNotes = 0) { |
| 881 | return Diag(Loc, DiagId, ExtraNotes, false); |
| 882 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 883 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 884 | OptionalDiagnostic FFDiag(const Expr *E, diag::kind DiagId |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 885 | = diag::note_invalid_subexpr_in_const_expr, |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 886 | unsigned ExtraNotes = 0) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 887 | if (EvalStatus.Diag) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 888 | return Diag(E->getExprLoc(), DiagId, ExtraNotes, /*IsCCEDiag*/false); |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 889 | HasActiveDiagnostic = false; |
| 890 | return OptionalDiagnostic(); |
| 891 | } |
| 892 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 893 | /// Diagnose that the evaluation does not produce a C++11 core constant |
| 894 | /// expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 895 | /// |
| 896 | /// FIXME: Stop evaluating if we're in EM_ConstantExpression or |
| 897 | /// EM_PotentialConstantExpression mode and we produce one of these. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 898 | OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 899 | = diag::note_invalid_subexpr_in_const_expr, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 900 | unsigned ExtraNotes = 0) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 901 | // Don't override a previous diagnostic. Don't bother collecting |
| 902 | // diagnostics if we're evaluating for overflow. |
Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 903 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 904 | HasActiveDiagnostic = false; |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 905 | return OptionalDiagnostic(); |
Eli Friedman | ebea9af | 2012-02-21 22:41:33 +0000 | [diff] [blame] | 906 | } |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 907 | return Diag(Loc, DiagId, ExtraNotes, true); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 908 | } |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 909 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind DiagId |
| 910 | = diag::note_invalid_subexpr_in_const_expr, |
| 911 | unsigned ExtraNotes = 0) { |
| 912 | return CCEDiag(E->getExprLoc(), DiagId, ExtraNotes); |
| 913 | } |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 914 | /// Add a note to a prior diagnostic. |
| 915 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
| 916 | if (!HasActiveDiagnostic) |
| 917 | return OptionalDiagnostic(); |
| 918 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 919 | } |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 920 | |
| 921 | /// Add a stack of notes to a prior diagnostic. |
| 922 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
| 923 | if (HasActiveDiagnostic) { |
| 924 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
| 925 | Diags.begin(), Diags.end()); |
| 926 | } |
| 927 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 928 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 929 | /// Should we continue evaluation after encountering a side-effect that we |
| 930 | /// couldn't model? |
| 931 | bool keepEvaluatingAfterSideEffect() { |
| 932 | switch (EvalMode) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 933 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 934 | case EM_PotentialConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 935 | case EM_EvaluateForOverflow: |
| 936 | case EM_IgnoreSideEffects: |
| 937 | return true; |
| 938 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 939 | case EM_ConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 940 | case EM_ConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 941 | case EM_ConstantFold: |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 942 | case EM_OffsetFold: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 943 | return false; |
| 944 | } |
Aaron Ballman | f682f53 | 2013-11-06 18:15:02 +0000 | [diff] [blame] | 945 | llvm_unreachable("Missed EvalMode case"); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /// Note that we have had a side-effect, and determine whether we should |
| 949 | /// keep evaluating. |
| 950 | bool noteSideEffect() { |
| 951 | EvalStatus.HasSideEffects = true; |
| 952 | return keepEvaluatingAfterSideEffect(); |
| 953 | } |
| 954 | |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 955 | /// Should we continue evaluation after encountering undefined behavior? |
| 956 | bool keepEvaluatingAfterUndefinedBehavior() { |
| 957 | switch (EvalMode) { |
| 958 | case EM_EvaluateForOverflow: |
| 959 | case EM_IgnoreSideEffects: |
| 960 | case EM_ConstantFold: |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 961 | case EM_OffsetFold: |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 962 | return true; |
| 963 | |
| 964 | case EM_PotentialConstantExpression: |
| 965 | case EM_PotentialConstantExpressionUnevaluated: |
| 966 | case EM_ConstantExpression: |
| 967 | case EM_ConstantExpressionUnevaluated: |
| 968 | return false; |
| 969 | } |
| 970 | llvm_unreachable("Missed EvalMode case"); |
| 971 | } |
| 972 | |
| 973 | /// Note that we hit something that was technically undefined behavior, but |
| 974 | /// that we can evaluate past it (such as signed overflow or floating-point |
| 975 | /// division by zero.) |
| 976 | bool noteUndefinedBehavior() { |
| 977 | EvalStatus.HasUndefinedBehavior = true; |
| 978 | return keepEvaluatingAfterUndefinedBehavior(); |
| 979 | } |
| 980 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 981 | /// Should we continue evaluation as much as possible after encountering a |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 982 | /// construct which can't be reduced to a value? |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 983 | bool keepEvaluatingAfterFailure() { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 984 | if (!StepsLeft) |
| 985 | return false; |
| 986 | |
| 987 | switch (EvalMode) { |
| 988 | case EM_PotentialConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 989 | case EM_PotentialConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 990 | case EM_EvaluateForOverflow: |
| 991 | return true; |
| 992 | |
| 993 | case EM_ConstantExpression: |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 994 | case EM_ConstantExpressionUnevaluated: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 995 | case EM_ConstantFold: |
| 996 | case EM_IgnoreSideEffects: |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 997 | case EM_OffsetFold: |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 998 | return false; |
| 999 | } |
Aaron Ballman | f682f53 | 2013-11-06 18:15:02 +0000 | [diff] [blame] | 1000 | llvm_unreachable("Missed EvalMode case"); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1001 | } |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1002 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1003 | /// Notes that we failed to evaluate an expression that other expressions |
| 1004 | /// directly depend on, and determine if we should keep evaluating. This |
| 1005 | /// should only be called if we actually intend to keep evaluating. |
| 1006 | /// |
| 1007 | /// Call noteSideEffect() instead if we may be able to ignore the value that |
| 1008 | /// we failed to evaluate, e.g. if we failed to evaluate Foo() in: |
| 1009 | /// |
| 1010 | /// (Foo(), 1) // use noteSideEffect |
| 1011 | /// (Foo() || true) // use noteSideEffect |
| 1012 | /// Foo() + 1 // use noteFailure |
Justin Bogner | fe183d7 | 2016-10-17 06:46:35 +0000 | [diff] [blame] | 1013 | LLVM_NODISCARD bool noteFailure() { |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1014 | // Failure when evaluating some expression often means there is some |
| 1015 | // subexpression whose evaluation was skipped. Therefore, (because we |
| 1016 | // don't track whether we skipped an expression when unwinding after an |
| 1017 | // evaluation failure) every evaluation failure that bubbles up from a |
| 1018 | // subexpression implies that a side-effect has potentially happened. We |
| 1019 | // skip setting the HasSideEffects flag to true until we decide to |
| 1020 | // continue evaluating after that point, which happens here. |
| 1021 | bool KeepGoing = keepEvaluatingAfterFailure(); |
| 1022 | EvalStatus.HasSideEffects |= KeepGoing; |
| 1023 | return KeepGoing; |
| 1024 | } |
| 1025 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 1026 | class ArrayInitLoopIndex { |
| 1027 | EvalInfo &Info; |
| 1028 | uint64_t OuterIndex; |
| 1029 | |
| 1030 | public: |
| 1031 | ArrayInitLoopIndex(EvalInfo &Info) |
| 1032 | : Info(Info), OuterIndex(Info.ArrayInitIndex) { |
| 1033 | Info.ArrayInitIndex = 0; |
| 1034 | } |
| 1035 | ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; } |
| 1036 | |
| 1037 | operator uint64_t&() { return Info.ArrayInitIndex; } |
| 1038 | }; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1039 | }; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1040 | |
| 1041 | /// Object used to treat all foldable expressions as constant expressions. |
| 1042 | struct FoldConstant { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1043 | EvalInfo &Info; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1044 | bool Enabled; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1045 | bool HadNoPriorDiags; |
| 1046 | EvalInfo::EvaluationMode OldMode; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1047 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1048 | explicit FoldConstant(EvalInfo &Info, bool Enabled) |
| 1049 | : Info(Info), |
| 1050 | Enabled(Enabled), |
| 1051 | HadNoPriorDiags(Info.EvalStatus.Diag && |
| 1052 | Info.EvalStatus.Diag->empty() && |
| 1053 | !Info.EvalStatus.HasSideEffects), |
| 1054 | OldMode(Info.EvalMode) { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1055 | if (Enabled && |
| 1056 | (Info.EvalMode == EvalInfo::EM_ConstantExpression || |
| 1057 | Info.EvalMode == EvalInfo::EM_ConstantExpressionUnevaluated)) |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1058 | Info.EvalMode = EvalInfo::EM_ConstantFold; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1059 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1060 | void keepDiagnostics() { Enabled = false; } |
| 1061 | ~FoldConstant() { |
| 1062 | if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() && |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1063 | !Info.EvalStatus.HasSideEffects) |
| 1064 | Info.EvalStatus.Diag->clear(); |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1065 | Info.EvalMode = OldMode; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 1066 | } |
| 1067 | }; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1068 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1069 | /// RAII object used to treat the current evaluation as the correct pointer |
| 1070 | /// offset fold for the current EvalMode |
| 1071 | struct FoldOffsetRAII { |
| 1072 | EvalInfo &Info; |
| 1073 | EvalInfo::EvaluationMode OldMode; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1074 | explicit FoldOffsetRAII(EvalInfo &Info) |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1075 | : Info(Info), OldMode(Info.EvalMode) { |
| 1076 | if (!Info.checkingPotentialConstantExpression()) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1077 | Info.EvalMode = EvalInfo::EM_OffsetFold; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | ~FoldOffsetRAII() { Info.EvalMode = OldMode; } |
| 1081 | }; |
| 1082 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1083 | /// RAII object used to optionally suppress diagnostics and side-effects from |
| 1084 | /// a speculative evaluation. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1085 | class SpeculativeEvaluationRAII { |
Chandler Carruth | bacb80d | 2017-08-16 07:22:49 +0000 | [diff] [blame] | 1086 | EvalInfo *Info = nullptr; |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1087 | Expr::EvalStatus OldStatus; |
| 1088 | bool OldIsSpeculativelyEvaluating; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1089 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1090 | void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) { |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1091 | Info = Other.Info; |
| 1092 | OldStatus = Other.OldStatus; |
Daniel Jasper | a7e061f | 2017-08-17 06:33:46 +0000 | [diff] [blame] | 1093 | OldIsSpeculativelyEvaluating = Other.OldIsSpeculativelyEvaluating; |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1094 | Other.Info = nullptr; |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | void maybeRestoreState() { |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1098 | if (!Info) |
| 1099 | return; |
| 1100 | |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1101 | Info->EvalStatus = OldStatus; |
| 1102 | Info->IsSpeculativelyEvaluating = OldIsSpeculativelyEvaluating; |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1105 | public: |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1106 | SpeculativeEvaluationRAII() = default; |
| 1107 | |
| 1108 | SpeculativeEvaluationRAII( |
| 1109 | EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr) |
Reid Kleckner | fdb3df6 | 2017-08-15 01:17:47 +0000 | [diff] [blame] | 1110 | : Info(&Info), OldStatus(Info.EvalStatus), |
| 1111 | OldIsSpeculativelyEvaluating(Info.IsSpeculativelyEvaluating) { |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1112 | Info.EvalStatus.Diag = NewDiag; |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1113 | Info.IsSpeculativelyEvaluating = true; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1114 | } |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1115 | |
| 1116 | SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete; |
| 1117 | SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) { |
| 1118 | moveFromAndCancel(std::move(Other)); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1119 | } |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 1120 | |
| 1121 | SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) { |
| 1122 | maybeRestoreState(); |
| 1123 | moveFromAndCancel(std::move(Other)); |
| 1124 | return *this; |
| 1125 | } |
| 1126 | |
| 1127 | ~SpeculativeEvaluationRAII() { maybeRestoreState(); } |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 1128 | }; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1129 | |
| 1130 | /// RAII object wrapping a full-expression or block scope, and handling |
| 1131 | /// the ending of the lifetime of temporaries created within it. |
| 1132 | template<bool IsFullExpression> |
| 1133 | class ScopeRAII { |
| 1134 | EvalInfo &Info; |
| 1135 | unsigned OldStackSize; |
| 1136 | public: |
| 1137 | ScopeRAII(EvalInfo &Info) |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1138 | : Info(Info), OldStackSize(Info.CleanupStack.size()) { |
| 1139 | // Push a new temporary version. This is needed to distinguish between |
| 1140 | // temporaries created in different iterations of a loop. |
| 1141 | Info.CurrentCall->pushTempVersion(); |
| 1142 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1143 | ~ScopeRAII() { |
| 1144 | // Body moved to a static method to encourage the compiler to inline away |
| 1145 | // instances of this class. |
| 1146 | cleanup(Info, OldStackSize); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1147 | Info.CurrentCall->popTempVersion(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1148 | } |
| 1149 | private: |
| 1150 | static void cleanup(EvalInfo &Info, unsigned OldStackSize) { |
| 1151 | unsigned NewEnd = OldStackSize; |
| 1152 | for (unsigned I = OldStackSize, N = Info.CleanupStack.size(); |
| 1153 | I != N; ++I) { |
| 1154 | if (IsFullExpression && Info.CleanupStack[I].isLifetimeExtended()) { |
| 1155 | // Full-expression cleanup of a lifetime-extended temporary: nothing |
| 1156 | // to do, just move this cleanup to the right place in the stack. |
| 1157 | std::swap(Info.CleanupStack[I], Info.CleanupStack[NewEnd]); |
| 1158 | ++NewEnd; |
| 1159 | } else { |
| 1160 | // End the lifetime of the object. |
| 1161 | Info.CleanupStack[I].endLifetime(); |
| 1162 | } |
| 1163 | } |
| 1164 | Info.CleanupStack.erase(Info.CleanupStack.begin() + NewEnd, |
| 1165 | Info.CleanupStack.end()); |
| 1166 | } |
| 1167 | }; |
| 1168 | typedef ScopeRAII<false> BlockScopeRAII; |
| 1169 | typedef ScopeRAII<true> FullExpressionRAII; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1170 | } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1171 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1172 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
| 1173 | CheckSubobjectKind CSK) { |
| 1174 | if (Invalid) |
| 1175 | return false; |
| 1176 | if (isOnePastTheEnd()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1177 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1178 | << CSK; |
| 1179 | setInvalid(); |
| 1180 | return false; |
| 1181 | } |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 1182 | // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there |
| 1183 | // must actually be at least one array element; even a VLA cannot have a |
| 1184 | // 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] | 1185 | return true; |
| 1186 | } |
| 1187 | |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 1188 | void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, |
| 1189 | const Expr *E) { |
| 1190 | Info.CCEDiag(E, diag::note_constexpr_unsized_array_indexed); |
| 1191 | // Do not set the designator as invalid: we can represent this situation, |
| 1192 | // and correct handling of __builtin_object_size requires us to do so. |
| 1193 | } |
| 1194 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1195 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 1196 | const Expr *E, |
| 1197 | const APSInt &N) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1198 | // If we're complaining, we must be able to statically determine the size of |
| 1199 | // the most derived array. |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 1200 | if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1201 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1202 | << N << /*array*/ 0 |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1203 | << static_cast<unsigned>(getMostDerivedArraySize()); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1204 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1205 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1206 | << N << /*non-array*/ 1; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1207 | setInvalid(); |
| 1208 | } |
| 1209 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1210 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
| 1211 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 1212 | APValue *Arguments) |
Samuel Antao | 1197a16 | 2016-09-19 18:13:13 +0000 | [diff] [blame] | 1213 | : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This), |
| 1214 | Arguments(Arguments), CallLoc(CallLoc), Index(Info.NextCallIndex++) { |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1215 | Info.CurrentCall = this; |
| 1216 | ++Info.CallStackDepth; |
| 1217 | } |
| 1218 | |
| 1219 | CallStackFrame::~CallStackFrame() { |
| 1220 | assert(Info.CurrentCall == this && "calls retired out of order"); |
| 1221 | --Info.CallStackDepth; |
| 1222 | Info.CurrentCall = Caller; |
| 1223 | } |
| 1224 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1225 | APValue &CallStackFrame::createTemporary(const void *Key, |
| 1226 | bool IsLifetimeExtended) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1227 | unsigned Version = Info.CurrentCall->getTempVersion(); |
| 1228 | APValue &Result = Temporaries[MapKeyTy(Key, Version)]; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 1229 | assert(Result.isUninit() && "temporary created multiple times"); |
| 1230 | Info.CleanupStack.push_back(Cleanup(&Result, IsLifetimeExtended)); |
| 1231 | return Result; |
| 1232 | } |
| 1233 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1234 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out); |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1235 | |
| 1236 | void EvalInfo::addCallStack(unsigned Limit) { |
| 1237 | // Determine which calls to skip, if any. |
| 1238 | unsigned ActiveCalls = CallStackDepth - 1; |
| 1239 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
| 1240 | if (Limit && Limit < ActiveCalls) { |
| 1241 | SkipStart = Limit / 2 + Limit % 2; |
| 1242 | SkipEnd = ActiveCalls - Limit / 2; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1245 | // Walk the call stack and add the diagnostics. |
| 1246 | unsigned CallIdx = 0; |
| 1247 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
| 1248 | Frame = Frame->Caller, ++CallIdx) { |
| 1249 | // Skip this call? |
| 1250 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
| 1251 | if (CallIdx == SkipStart) { |
| 1252 | // Note that we're skipping calls. |
| 1253 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
| 1254 | << unsigned(ActiveCalls - Limit); |
| 1255 | } |
| 1256 | continue; |
| 1257 | } |
| 1258 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 1259 | // Use a different note for an inheriting constructor, because from the |
| 1260 | // user's perspective it's not really a function at all. |
| 1261 | if (auto *CD = dyn_cast_or_null<CXXConstructorDecl>(Frame->Callee)) { |
| 1262 | if (CD->isInheritingConstructor()) { |
| 1263 | addDiag(Frame->CallLoc, diag::note_constexpr_inherited_ctor_call_here) |
| 1264 | << CD->getParent(); |
| 1265 | continue; |
| 1266 | } |
| 1267 | } |
| 1268 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1269 | SmallVector<char, 128> Buffer; |
Richard Smith | f6f003a | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 1270 | llvm::raw_svector_ostream Out(Buffer); |
| 1271 | describeCall(Frame, Out); |
| 1272 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | namespace { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1277 | struct ComplexValue { |
| 1278 | private: |
| 1279 | bool IsInt; |
| 1280 | |
| 1281 | public: |
| 1282 | APSInt IntReal, IntImag; |
| 1283 | APFloat FloatReal, FloatImag; |
| 1284 | |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 1285 | ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {} |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1286 | |
| 1287 | void makeComplexFloat() { IsInt = false; } |
| 1288 | bool isComplexFloat() const { return !IsInt; } |
| 1289 | APFloat &getComplexFloatReal() { return FloatReal; } |
| 1290 | APFloat &getComplexFloatImag() { return FloatImag; } |
| 1291 | |
| 1292 | void makeComplexInt() { IsInt = true; } |
| 1293 | bool isComplexInt() const { return IsInt; } |
| 1294 | APSInt &getComplexIntReal() { return IntReal; } |
| 1295 | APSInt &getComplexIntImag() { return IntImag; } |
| 1296 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1297 | void moveInto(APValue &v) const { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1298 | if (isComplexFloat()) |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1299 | v = APValue(FloatReal, FloatImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1300 | else |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1301 | v = APValue(IntReal, IntImag); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1302 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1303 | void setFrom(const APValue &v) { |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1304 | assert(v.isComplexFloat() || v.isComplexInt()); |
| 1305 | if (v.isComplexFloat()) { |
| 1306 | makeComplexFloat(); |
| 1307 | FloatReal = v.getComplexFloatReal(); |
| 1308 | FloatImag = v.getComplexFloatImag(); |
| 1309 | } else { |
| 1310 | makeComplexInt(); |
| 1311 | IntReal = v.getComplexIntReal(); |
| 1312 | IntImag = v.getComplexIntImag(); |
| 1313 | } |
| 1314 | } |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1315 | }; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1316 | |
| 1317 | struct LValue { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1318 | APValue::LValueBase Base; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1319 | CharUnits Offset; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1320 | SubobjectDesignator Designator; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1321 | bool IsNullPtr : 1; |
| 1322 | bool InvalidBase : 1; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1323 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1324 | const APValue::LValueBase getLValueBase() const { return Base; } |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1325 | CharUnits &getLValueOffset() { return Offset; } |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 1326 | const CharUnits &getLValueOffset() const { return Offset; } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1327 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
| 1328 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1329 | bool isNullPointer() const { return IsNullPtr;} |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1330 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1331 | unsigned getLValueCallIndex() const { return Base.getCallIndex(); } |
| 1332 | unsigned getLValueVersion() const { return Base.getVersion(); } |
| 1333 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1334 | void moveInto(APValue &V) const { |
| 1335 | if (Designator.Invalid) |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1336 | V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1337 | else { |
| 1338 | assert(!InvalidBase && "APValues can't handle invalid LValue bases"); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1339 | V = APValue(Base, Offset, Designator.Entries, |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1340 | Designator.IsOnePastTheEnd, IsNullPtr); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1341 | } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1342 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1343 | void setFrom(ASTContext &Ctx, const APValue &V) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1344 | assert(V.isLValue() && "Setting LValue from a non-LValue?"); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1345 | Base = V.getLValueBase(); |
| 1346 | Offset = V.getLValueOffset(); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1347 | InvalidBase = false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1348 | Designator = SubobjectDesignator(Ctx, V); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1349 | IsNullPtr = V.isNullPointer(); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1352 | void set(APValue::LValueBase B, bool BInvalid = false) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1353 | #ifndef NDEBUG |
| 1354 | // We only allow a few types of invalid bases. Enforce that here. |
| 1355 | if (BInvalid) { |
| 1356 | const auto *E = B.get<const Expr *>(); |
| 1357 | assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) && |
| 1358 | "Unexpected type of invalid base"); |
| 1359 | } |
| 1360 | #endif |
| 1361 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1362 | Base = B; |
Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1363 | Offset = CharUnits::fromQuantity(0); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1364 | InvalidBase = BInvalid; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1365 | Designator = SubobjectDesignator(getType(B)); |
Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1366 | IsNullPtr = false; |
| 1367 | } |
| 1368 | |
| 1369 | void setNull(QualType PointerTy, uint64_t TargetVal) { |
| 1370 | Base = (Expr *)nullptr; |
| 1371 | Offset = CharUnits::fromQuantity(TargetVal); |
| 1372 | InvalidBase = false; |
Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 1373 | Designator = SubobjectDesignator(PointerTy->getPointeeType()); |
| 1374 | IsNullPtr = true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1377 | void setInvalid(APValue::LValueBase B, unsigned I = 0) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1378 | set(B, true); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1381 | // Check that this LValue is not based on a null pointer. If it is, produce |
| 1382 | // a diagnostic and mark the designator as invalid. |
| 1383 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
| 1384 | CheckSubobjectKind CSK) { |
| 1385 | if (Designator.Invalid) |
| 1386 | return false; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1387 | if (IsNullPtr) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1388 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1389 | << CSK; |
| 1390 | Designator.setInvalid(); |
| 1391 | return false; |
| 1392 | } |
| 1393 | return true; |
| 1394 | } |
| 1395 | |
| 1396 | // Check this LValue refers to an object. If not, set the designator to be |
| 1397 | // invalid and emit a diagnostic. |
| 1398 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
Richard Smith | 6c6bbfa | 2014-04-08 12:19:28 +0000 | [diff] [blame] | 1399 | return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) && |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1400 | Designator.checkSubobject(Info, E, CSK); |
| 1401 | } |
| 1402 | |
| 1403 | void addDecl(EvalInfo &Info, const Expr *E, |
| 1404 | const Decl *D, bool Virtual = false) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1405 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
| 1406 | Designator.addDeclUnchecked(D, Virtual); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1407 | } |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 1408 | void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) { |
| 1409 | if (!Designator.Entries.empty()) { |
| 1410 | Info.CCEDiag(E, diag::note_constexpr_unsupported_unsized_array); |
| 1411 | Designator.setInvalid(); |
| 1412 | return; |
| 1413 | } |
Richard Smith | efdb503 | 2017-11-15 03:03:56 +0000 | [diff] [blame] | 1414 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) { |
| 1415 | assert(getType(Base)->isPointerType() || getType(Base)->isArrayType()); |
| 1416 | Designator.FirstEntryIsAnUnsizedArray = true; |
| 1417 | Designator.addUnsizedArrayUnchecked(ElemTy); |
| 1418 | } |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 1419 | } |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1420 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1421 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
| 1422 | Designator.addArrayUnchecked(CAT); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1423 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1424 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 1425 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
| 1426 | Designator.addComplexUnchecked(EltTy, Imag); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 1427 | } |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1428 | void clearIsNullPointer() { |
| 1429 | IsNullPtr = false; |
| 1430 | } |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 1431 | void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E, |
| 1432 | const APSInt &Index, CharUnits ElementSize) { |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1433 | // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB, |
| 1434 | // but we're not required to diagnose it and it's valid in C++.) |
| 1435 | if (!Index) |
| 1436 | return; |
| 1437 | |
| 1438 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
| 1439 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
| 1440 | // offsets. |
| 1441 | uint64_t Offset64 = Offset.getQuantity(); |
| 1442 | uint64_t ElemSize64 = ElementSize.getQuantity(); |
| 1443 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
| 1444 | Offset = CharUnits::fromQuantity(Offset64 + ElemSize64 * Index64); |
| 1445 | |
| 1446 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1447 | Designator.adjustIndex(Info, E, Index); |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1448 | clearIsNullPointer(); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 1449 | } |
| 1450 | void adjustOffset(CharUnits N) { |
| 1451 | Offset += N; |
| 1452 | if (N.getQuantity()) |
| 1453 | clearIsNullPointer(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 1454 | } |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 1455 | }; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1456 | |
| 1457 | struct MemberPtr { |
| 1458 | MemberPtr() {} |
| 1459 | explicit MemberPtr(const ValueDecl *Decl) : |
| 1460 | DeclAndIsDerivedMember(Decl, false), Path() {} |
| 1461 | |
| 1462 | /// The member or (direct or indirect) field referred to by this member |
| 1463 | /// pointer, or 0 if this is a null member pointer. |
| 1464 | const ValueDecl *getDecl() const { |
| 1465 | return DeclAndIsDerivedMember.getPointer(); |
| 1466 | } |
| 1467 | /// Is this actually a member of some type derived from the relevant class? |
| 1468 | bool isDerivedMember() const { |
| 1469 | return DeclAndIsDerivedMember.getInt(); |
| 1470 | } |
| 1471 | /// Get the class which the declaration actually lives in. |
| 1472 | const CXXRecordDecl *getContainingRecord() const { |
| 1473 | return cast<CXXRecordDecl>( |
| 1474 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
| 1475 | } |
| 1476 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1477 | void moveInto(APValue &V) const { |
| 1478 | V = APValue(getDecl(), isDerivedMember(), Path); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1479 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1480 | void setFrom(const APValue &V) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1481 | assert(V.isMemberPointer()); |
| 1482 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
| 1483 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
| 1484 | Path.clear(); |
| 1485 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
| 1486 | Path.insert(Path.end(), P.begin(), P.end()); |
| 1487 | } |
| 1488 | |
| 1489 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
| 1490 | /// whether the member is a member of some class derived from the class type |
| 1491 | /// of the member pointer. |
| 1492 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
| 1493 | /// Path - The path of base/derived classes from the member declaration's |
| 1494 | /// class (exclusive) to the class type of the member pointer (inclusive). |
| 1495 | SmallVector<const CXXRecordDecl*, 4> Path; |
| 1496 | |
| 1497 | /// Perform a cast towards the class of the Decl (either up or down the |
| 1498 | /// hierarchy). |
| 1499 | bool castBack(const CXXRecordDecl *Class) { |
| 1500 | assert(!Path.empty()); |
| 1501 | const CXXRecordDecl *Expected; |
| 1502 | if (Path.size() >= 2) |
| 1503 | Expected = Path[Path.size() - 2]; |
| 1504 | else |
| 1505 | Expected = getContainingRecord(); |
| 1506 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
| 1507 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
| 1508 | // if B does not contain the original member and is not a base or |
| 1509 | // derived class of the class containing the original member, the result |
| 1510 | // of the cast is undefined. |
| 1511 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
| 1512 | // (D::*). We consider that to be a language defect. |
| 1513 | return false; |
| 1514 | } |
| 1515 | Path.pop_back(); |
| 1516 | return true; |
| 1517 | } |
| 1518 | /// Perform a base-to-derived member pointer cast. |
| 1519 | bool castToDerived(const CXXRecordDecl *Derived) { |
| 1520 | if (!getDecl()) |
| 1521 | return true; |
| 1522 | if (!isDerivedMember()) { |
| 1523 | Path.push_back(Derived); |
| 1524 | return true; |
| 1525 | } |
| 1526 | if (!castBack(Derived)) |
| 1527 | return false; |
| 1528 | if (Path.empty()) |
| 1529 | DeclAndIsDerivedMember.setInt(false); |
| 1530 | return true; |
| 1531 | } |
| 1532 | /// Perform a derived-to-base member pointer cast. |
| 1533 | bool castToBase(const CXXRecordDecl *Base) { |
| 1534 | if (!getDecl()) |
| 1535 | return true; |
| 1536 | if (Path.empty()) |
| 1537 | DeclAndIsDerivedMember.setInt(true); |
| 1538 | if (isDerivedMember()) { |
| 1539 | Path.push_back(Base); |
| 1540 | return true; |
| 1541 | } |
| 1542 | return castBack(Base); |
| 1543 | } |
| 1544 | }; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1545 | |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 1546 | /// Compare two member pointers, which are assumed to be of the same type. |
| 1547 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
| 1548 | if (!LHS.getDecl() || !RHS.getDecl()) |
| 1549 | return !LHS.getDecl() && !RHS.getDecl(); |
| 1550 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
| 1551 | return false; |
| 1552 | return LHS.Path == RHS.Path; |
| 1553 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1554 | } |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 1555 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1556 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1557 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
| 1558 | const LValue &This, const Expr *E, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1559 | bool AllowNonLiteralTypes = false); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 1560 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
| 1561 | bool InvalidBaseOK = false); |
| 1562 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info, |
| 1563 | bool InvalidBaseOK = false); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1564 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 1565 | EvalInfo &Info); |
| 1566 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 1567 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1568 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Chris Lattner | 6c4d255 | 2009-10-28 23:59:40 +0000 | [diff] [blame] | 1569 | EvalInfo &Info); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 1570 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 1571 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 1572 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
| 1573 | EvalInfo &Info); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1574 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 1575 | |
| 1576 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1577 | // Misc utilities |
| 1578 | //===----------------------------------------------------------------------===// |
| 1579 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1580 | /// A helper function to create a temporary and set an LValue. |
| 1581 | template <class KeyTy> |
| 1582 | static APValue &createTemporary(const KeyTy *Key, bool IsLifetimeExtended, |
| 1583 | LValue &LV, CallStackFrame &Frame) { |
| 1584 | LV.set({Key, Frame.Info.CurrentCall->Index, |
| 1585 | Frame.Info.CurrentCall->getTempVersion()}); |
| 1586 | return Frame.createTemporary(Key, IsLifetimeExtended); |
| 1587 | } |
| 1588 | |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 1589 | /// Negate an APSInt in place, converting it to a signed form if necessary, and |
| 1590 | /// preserving its value (by extending by up to one bit as needed). |
| 1591 | static void negateAsSigned(APSInt &Int) { |
| 1592 | if (Int.isUnsigned() || Int.isMinSignedValue()) { |
| 1593 | Int = Int.extend(Int.getBitWidth() + 1); |
| 1594 | Int.setIsSigned(true); |
| 1595 | } |
| 1596 | Int = -Int; |
| 1597 | } |
| 1598 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 1599 | /// Produce a string describing the given constexpr call. |
| 1600 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out) { |
| 1601 | unsigned ArgIndex = 0; |
| 1602 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
| 1603 | !isa<CXXConstructorDecl>(Frame->Callee) && |
| 1604 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
| 1605 | |
| 1606 | if (!IsMemberCall) |
| 1607 | Out << *Frame->Callee << '('; |
| 1608 | |
| 1609 | if (Frame->This && IsMemberCall) { |
| 1610 | APValue Val; |
| 1611 | Frame->This->moveInto(Val); |
| 1612 | Val.printPretty(Out, Frame->Info.Ctx, |
| 1613 | Frame->This->Designator.MostDerivedType); |
| 1614 | // FIXME: Add parens around Val if needed. |
| 1615 | Out << "->" << *Frame->Callee << '('; |
| 1616 | IsMemberCall = false; |
| 1617 | } |
| 1618 | |
| 1619 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
| 1620 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
| 1621 | if (ArgIndex > (unsigned)IsMemberCall) |
| 1622 | Out << ", "; |
| 1623 | |
| 1624 | const ParmVarDecl *Param = *I; |
| 1625 | const APValue &Arg = Frame->Arguments[ArgIndex]; |
| 1626 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
| 1627 | |
| 1628 | if (ArgIndex == 0 && IsMemberCall) |
| 1629 | Out << "->" << *Frame->Callee << '('; |
| 1630 | } |
| 1631 | |
| 1632 | Out << ')'; |
| 1633 | } |
| 1634 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1635 | /// Evaluate an expression to see if it had side-effects, and discard its |
| 1636 | /// result. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1637 | /// \return \c true if the caller should keep evaluating. |
| 1638 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1639 | APValue Scratch; |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 1640 | if (!Evaluate(Scratch, Info, E)) |
| 1641 | // We don't need the value, but we might have skipped a side effect here. |
| 1642 | return Info.noteSideEffect(); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 1643 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1646 | /// Should this call expression be treated as a string literal? |
| 1647 | static bool IsStringLiteralCall(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 1648 | unsigned Builtin = E->getBuiltinCallee(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1649 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
| 1650 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
| 1651 | } |
| 1652 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1653 | static bool IsGlobalLValue(APValue::LValueBase B) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1654 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
| 1655 | // constant expression of pointer type that evaluates to... |
| 1656 | |
| 1657 | // ... a null pointer value, or a prvalue core constant expression of type |
| 1658 | // std::nullptr_t. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1659 | if (!B) return true; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1660 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1661 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 1662 | // ... the address of an object with static storage duration, |
| 1663 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 1664 | return VD->hasGlobalStorage(); |
| 1665 | // ... the address of a function, |
| 1666 | return isa<FunctionDecl>(D); |
| 1667 | } |
| 1668 | |
| 1669 | const Expr *E = B.get<const Expr*>(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1670 | switch (E->getStmtClass()) { |
| 1671 | default: |
| 1672 | return false; |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1673 | case Expr::CompoundLiteralExprClass: { |
| 1674 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
| 1675 | return CLE->isFileScope() && CLE->isLValue(); |
| 1676 | } |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1677 | case Expr::MaterializeTemporaryExprClass: |
| 1678 | // A materialized temporary might have been lifetime-extended to static |
| 1679 | // storage duration. |
| 1680 | return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1681 | // A string literal has static storage duration. |
| 1682 | case Expr::StringLiteralClass: |
| 1683 | case Expr::PredefinedExprClass: |
| 1684 | case Expr::ObjCStringLiteralClass: |
| 1685 | case Expr::ObjCEncodeExprClass: |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 1686 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 1687 | case Expr::CXXUuidofExprClass: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1688 | return true; |
| 1689 | case Expr::CallExprClass: |
| 1690 | return IsStringLiteralCall(cast<CallExpr>(E)); |
| 1691 | // For GCC compatibility, &&label has static storage duration. |
| 1692 | case Expr::AddrLabelExprClass: |
| 1693 | return true; |
| 1694 | // A Block literal expression may be used as the initialization value for |
| 1695 | // Block variables at global or local static scope. |
| 1696 | case Expr::BlockExprClass: |
| 1697 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 1698 | case Expr::ImplicitValueInitExprClass: |
| 1699 | // FIXME: |
| 1700 | // We can never form an lvalue with an implicit value initialization as its |
| 1701 | // base through expression evaluation, so these only appear in one case: the |
| 1702 | // implicit variable declaration we invent when checking whether a constexpr |
| 1703 | // constructor can produce a constant expression. We must assume that such |
| 1704 | // an expression might be a global lvalue. |
| 1705 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1706 | } |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1709 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
| 1710 | assert(Base && "no location for a null lvalue"); |
| 1711 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
| 1712 | if (VD) |
| 1713 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 1714 | else |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 1715 | Info.Note(Base.get<const Expr*>()->getExprLoc(), |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1716 | diag::note_constexpr_temporary_here); |
| 1717 | } |
| 1718 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1719 | /// Check that this reference or pointer core constant expression is a valid |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1720 | /// value for an address or reference constant expression. Return true if we |
| 1721 | /// can fold this expression, whether or not it's a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1722 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1723 | QualType Type, const LValue &LVal, |
| 1724 | Expr::ConstExprUsage Usage) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1725 | bool IsReferenceType = Type->isReferenceType(); |
| 1726 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1727 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 1728 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 1729 | |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1730 | // Check that the object is a global. Note that the fake 'this' object we |
| 1731 | // manufacture when checking potential constant expressions is conservatively |
| 1732 | // assumed to be global here. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1733 | if (!IsGlobalLValue(Base)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1734 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1735 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1736 | Info.FFDiag(Loc, diag::note_constexpr_non_global, 1) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1737 | << IsReferenceType << !Designator.Entries.empty() |
| 1738 | << !!VD << VD; |
| 1739 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1740 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1741 | Info.FFDiag(Loc); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1742 | } |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1743 | // Don't allow references to temporaries to escape. |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1744 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1745 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1746 | assert((Info.checkingPotentialConstantExpression() || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1747 | LVal.getLValueCallIndex() == 0) && |
| 1748 | "have call index for global lvalue"); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1749 | |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1750 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 1751 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1752 | // Check if this is a thread-local variable. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1753 | if (Var->getTLSKind()) |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1754 | return false; |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1755 | |
Hans Wennborg | 82dd877 | 2014-06-25 22:19:48 +0000 | [diff] [blame] | 1756 | // A dllimport variable never acts like a constant. |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1757 | if (Usage == Expr::EvaluateForCodeGen && Var->hasAttr<DLLImportAttr>()) |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1758 | return false; |
| 1759 | } |
| 1760 | if (const auto *FD = dyn_cast<const FunctionDecl>(VD)) { |
| 1761 | // __declspec(dllimport) must be handled very carefully: |
| 1762 | // We must never initialize an expression with the thunk in C++. |
| 1763 | // Doing otherwise would allow the same id-expression to yield |
| 1764 | // different addresses for the same function in different translation |
| 1765 | // units. However, this means that we must dynamically initialize the |
| 1766 | // expression with the contents of the import address table at runtime. |
| 1767 | // |
| 1768 | // The C language has no notion of ODR; furthermore, it has no notion of |
| 1769 | // dynamic initialization. This means that we are permitted to |
| 1770 | // perform initialization with the address of the thunk. |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1771 | if (Info.getLangOpts().CPlusPlus && Usage == Expr::EvaluateForCodeGen && |
| 1772 | FD->hasAttr<DLLImportAttr>()) |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1773 | return false; |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1774 | } |
| 1775 | } |
| 1776 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1777 | // Allow address constant expressions to be past-the-end pointers. This is |
| 1778 | // an extension: the standard requires them to point to an object. |
| 1779 | if (!IsReferenceType) |
| 1780 | return true; |
| 1781 | |
| 1782 | // A reference constant expression must refer to an object. |
| 1783 | if (!Base) { |
| 1784 | // FIXME: diagnostic |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1785 | Info.CCEDiag(Loc); |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1786 | return true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1789 | // Does this refer one past the end of some object? |
Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 1790 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1791 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1792 | Info.FFDiag(Loc, diag::note_constexpr_past_end, 1) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1793 | << !Designator.Entries.empty() << !!VD << VD; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1794 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1797 | return true; |
| 1798 | } |
| 1799 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 1800 | /// Member pointers are constant expressions unless they point to a |
| 1801 | /// non-virtual dllimport member function. |
| 1802 | static bool CheckMemberPointerConstantExpression(EvalInfo &Info, |
| 1803 | SourceLocation Loc, |
| 1804 | QualType Type, |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1805 | const APValue &Value, |
| 1806 | Expr::ConstExprUsage Usage) { |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 1807 | const ValueDecl *Member = Value.getMemberPointerDecl(); |
| 1808 | const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Member); |
| 1809 | if (!FD) |
| 1810 | return true; |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1811 | return Usage == Expr::EvaluateForMangling || FD->isVirtual() || |
| 1812 | !FD->hasAttr<DLLImportAttr>(); |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1815 | /// Check that this core constant expression is of literal type, and if not, |
| 1816 | /// produce an appropriate diagnostic. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1817 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1818 | const LValue *This = nullptr) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1819 | if (!E->isRValue() || E->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1820 | return true; |
| 1821 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1822 | // C++1y: A constant initializer for an object o [...] may also invoke |
| 1823 | // constexpr constructors for o and its subobjects even if those objects |
| 1824 | // are of non-literal class types. |
David L. Jones | f55ce36 | 2017-01-09 21:38:07 +0000 | [diff] [blame] | 1825 | // |
| 1826 | // C++11 missed this detail for aggregates, so classes like this: |
| 1827 | // struct foo_t { union { int i; volatile int j; } u; }; |
| 1828 | // are not (obviously) initializable like so: |
| 1829 | // __attribute__((__require_constant_initialization__)) |
| 1830 | // static const foo_t x = {{0}}; |
| 1831 | // because "i" is a subobject with non-literal initialization (due to the |
| 1832 | // volatile member of the union). See: |
| 1833 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 |
| 1834 | // Therefore, we use the C++1y behavior. |
| 1835 | if (This && Info.EvaluatingDecl == This->getLValueBase()) |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1836 | return true; |
| 1837 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1838 | // Prvalue constant expressions must be of literal types. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1839 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1840 | Info.FFDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1841 | << E->getType(); |
| 1842 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1843 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1844 | return false; |
| 1845 | } |
| 1846 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1847 | /// 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] | 1848 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 1849 | /// check that the expression is of literal type. |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1850 | static bool |
| 1851 | CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, QualType Type, |
| 1852 | const APValue &Value, |
| 1853 | Expr::ConstExprUsage Usage = Expr::EvaluateForCodeGen) { |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1854 | if (Value.isUninit()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1855 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 1856 | << true << Type; |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1857 | return false; |
| 1858 | } |
| 1859 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 1860 | // We allow _Atomic(T) to be initialized from anything that T can be |
| 1861 | // initialized from. |
| 1862 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
| 1863 | Type = AT->getValueType(); |
| 1864 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1865 | // Core issue 1454: For a literal constant expression of array or class type, |
| 1866 | // each subobject of its value shall have been initialized by a constant |
| 1867 | // expression. |
| 1868 | if (Value.isArray()) { |
| 1869 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 1870 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| 1871 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1872 | Value.getArrayInitializedElt(I), Usage)) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1873 | return false; |
| 1874 | } |
| 1875 | if (!Value.hasArrayFiller()) |
| 1876 | return true; |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1877 | return CheckConstantExpression(Info, DiagLoc, EltTy, Value.getArrayFiller(), |
| 1878 | Usage); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1879 | } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1880 | if (Value.isUnion() && Value.getUnionField()) { |
| 1881 | return CheckConstantExpression(Info, DiagLoc, |
| 1882 | Value.getUnionField()->getType(), |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1883 | Value.getUnionValue(), Usage); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1884 | } |
| 1885 | if (Value.isStruct()) { |
| 1886 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 1887 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1888 | unsigned BaseIndex = 0; |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1889 | for (const CXXBaseSpecifier &BS : CD->bases()) { |
| 1890 | if (!CheckConstantExpression(Info, DiagLoc, BS.getType(), |
| 1891 | Value.getStructBase(BaseIndex), Usage)) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1892 | return false; |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1893 | ++BaseIndex; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1894 | } |
| 1895 | } |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1896 | for (const auto *I : RD->fields()) { |
Jordan Rose | d4503da | 2017-10-24 02:17:07 +0000 | [diff] [blame] | 1897 | if (I->isUnnamedBitfield()) |
| 1898 | continue; |
| 1899 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1900 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1901 | Value.getStructField(I->getFieldIndex()), |
| 1902 | Usage)) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1903 | return false; |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | if (Value.isLValue()) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1908 | LValue LVal; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1909 | LVal.setFrom(Info.Ctx, Value); |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1910 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal, Usage); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 1913 | if (Value.isMemberPointer()) |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 1914 | return CheckMemberPointerConstantExpression(Info, DiagLoc, Type, Value, Usage); |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 1915 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1916 | // Everything else is fine. |
| 1917 | return true; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 1920 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1921 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | static bool IsLiteralLValue(const LValue &Value) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1925 | if (Value.getLValueCallIndex()) |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1926 | return false; |
| 1927 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
| 1928 | return E && !isa<MaterializeTemporaryExpr>(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1931 | static bool IsWeakLValue(const LValue &Value) { |
| 1932 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1933 | return Decl && Decl->isWeak(); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 1936 | static bool isZeroSized(const LValue &Value) { |
| 1937 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 1938 | if (Decl && isa<VarDecl>(Decl)) { |
| 1939 | QualType Ty = Decl->getType(); |
David Majnemer | 8c92b87 | 2014-12-14 08:40:47 +0000 | [diff] [blame] | 1940 | if (Ty->isArrayType()) |
| 1941 | return Ty->isIncompleteType() || |
| 1942 | Decl->getASTContext().getTypeSize(Ty) == 0; |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 1943 | } |
| 1944 | return false; |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1947 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1948 | // A null base expression indicates a null pointer. These are always |
| 1949 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1950 | if (!Value.getLValueBase()) { |
| 1951 | Result = !Value.getLValueOffset().isZero(); |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1952 | return true; |
| 1953 | } |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 1954 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1955 | // We have a non-null base. These are generally known to be true, but if it's |
| 1956 | // a weak declaration it can be null at runtime. |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1957 | Result = true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1958 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1959 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1962 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1963 | switch (Val.getKind()) { |
| 1964 | case APValue::Uninitialized: |
| 1965 | return false; |
| 1966 | case APValue::Int: |
| 1967 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1968 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1969 | case APValue::Float: |
| 1970 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1971 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1972 | case APValue::ComplexInt: |
| 1973 | Result = Val.getComplexIntReal().getBoolValue() || |
| 1974 | Val.getComplexIntImag().getBoolValue(); |
| 1975 | return true; |
| 1976 | case APValue::ComplexFloat: |
| 1977 | Result = !Val.getComplexFloatReal().isZero() || |
| 1978 | !Val.getComplexFloatImag().isZero(); |
| 1979 | return true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1980 | case APValue::LValue: |
| 1981 | return EvalPointerValueAsBool(Val, Result); |
| 1982 | case APValue::MemberPointer: |
| 1983 | Result = Val.getMemberPointerDecl(); |
| 1984 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1985 | case APValue::Vector: |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1986 | case APValue::Array: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1987 | case APValue::Struct: |
| 1988 | case APValue::Union: |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 1989 | case APValue::AddrLabelDiff: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1990 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1993 | llvm_unreachable("unknown APValue kind"); |
| 1994 | } |
| 1995 | |
| 1996 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 1997 | EvalInfo &Info) { |
| 1998 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1999 | APValue Val; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 2000 | if (!Evaluate(Val, Info, E)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2001 | return false; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 2002 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2005 | template<typename T> |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2006 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2007 | const T &SrcValue, QualType DestType) { |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 2008 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 2009 | << SrcValue << DestType; |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 2010 | return Info.noteUndefinedBehavior(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 2014 | QualType SrcType, const APFloat &Value, |
| 2015 | QualType DestType, APSInt &Result) { |
| 2016 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2017 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2018 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2020 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2021 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2022 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 2023 | & APFloat::opInvalidOp) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2024 | return HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2025 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2028 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 2029 | QualType SrcType, QualType DestType, |
| 2030 | APFloat &Result) { |
| 2031 | APFloat Value = Result; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2032 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2033 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 2034 | APFloat::rmNearestTiesToEven, &ignored) |
| 2035 | & APFloat::opOverflow) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2036 | return HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2037 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 2040 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 2041 | QualType DestType, QualType SrcType, |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 2042 | const APSInt &Value) { |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 2043 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2044 | APSInt Result = Value; |
| 2045 | // Figure out if this is a truncate, extend or noop cast. |
| 2046 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2047 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2048 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2049 | return Result; |
| 2050 | } |
| 2051 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2052 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 2053 | QualType SrcType, const APSInt &Value, |
| 2054 | QualType DestType, APFloat &Result) { |
| 2055 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 2056 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 2057 | APFloat::rmNearestTiesToEven) |
| 2058 | & APFloat::opOverflow) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2059 | return HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2060 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2063 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
| 2064 | APValue &Value, const FieldDecl *FD) { |
| 2065 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
| 2066 | |
| 2067 | if (!Value.isInt()) { |
| 2068 | // Trying to store a pointer-cast-to-integer into a bitfield. |
| 2069 | // FIXME: In this case, we should provide the diagnostic for casting |
| 2070 | // a pointer to an integer. |
| 2071 | assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2072 | Info.FFDiag(E); |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2073 | return false; |
| 2074 | } |
| 2075 | |
| 2076 | APSInt &Int = Value.getInt(); |
| 2077 | unsigned OldBitWidth = Int.getBitWidth(); |
| 2078 | unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx); |
| 2079 | if (NewBitWidth < OldBitWidth) |
| 2080 | Int = Int.trunc(NewBitWidth).extend(OldBitWidth); |
| 2081 | return true; |
| 2082 | } |
| 2083 | |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2084 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 2085 | llvm::APInt &Res) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2086 | APValue SVal; |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2087 | if (!Evaluate(SVal, Info, E)) |
| 2088 | return false; |
| 2089 | if (SVal.isInt()) { |
| 2090 | Res = SVal.getInt(); |
| 2091 | return true; |
| 2092 | } |
| 2093 | if (SVal.isFloat()) { |
| 2094 | Res = SVal.getFloat().bitcastToAPInt(); |
| 2095 | return true; |
| 2096 | } |
| 2097 | if (SVal.isVector()) { |
| 2098 | QualType VecTy = E->getType(); |
| 2099 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 2100 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 2101 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 2102 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 2103 | Res = llvm::APInt::getNullValue(VecSize); |
| 2104 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 2105 | APValue &Elt = SVal.getVectorElt(i); |
| 2106 | llvm::APInt EltAsInt; |
| 2107 | if (Elt.isInt()) { |
| 2108 | EltAsInt = Elt.getInt(); |
| 2109 | } else if (Elt.isFloat()) { |
| 2110 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 2111 | } else { |
| 2112 | // Don't try to handle vectors of anything other than int or float |
| 2113 | // (not sure if it's possible to hit this case). |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2114 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2115 | return false; |
| 2116 | } |
| 2117 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 2118 | if (BigEndian) |
| 2119 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 2120 | else |
| 2121 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 2122 | } |
| 2123 | return true; |
| 2124 | } |
| 2125 | // Give up if the input isn't an int, float, or vector. For example, we |
| 2126 | // reject "(v4i16)(intptr_t)&a". |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2127 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2128 | return false; |
| 2129 | } |
| 2130 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2131 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 2132 | /// bits, and check for overflow in the original type (if that type was not an |
| 2133 | /// unsigned type). |
| 2134 | template<typename Operation> |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2135 | static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 2136 | const APSInt &LHS, const APSInt &RHS, |
| 2137 | unsigned BitWidth, Operation Op, |
| 2138 | APSInt &Result) { |
| 2139 | if (LHS.isUnsigned()) { |
| 2140 | Result = Op(LHS, RHS); |
| 2141 | return true; |
| 2142 | } |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2143 | |
| 2144 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2145 | Result = Value.trunc(LHS.getBitWidth()); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2146 | if (Result.extend(BitWidth) != Value) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2147 | if (Info.checkingForOverflow()) |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2148 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2149 | diag::warn_integer_constant_overflow) |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2150 | << Result.toString(10) << E->getType(); |
| 2151 | else |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2152 | return HandleOverflow(Info, E, Value, E->getType()); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2153 | } |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2154 | return true; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | /// Perform the given binary integer operation. |
| 2158 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
| 2159 | BinaryOperatorKind Opcode, APSInt RHS, |
| 2160 | APSInt &Result) { |
| 2161 | switch (Opcode) { |
| 2162 | default: |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2163 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2164 | return false; |
| 2165 | case BO_Mul: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2166 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
| 2167 | std::multiplies<APSInt>(), Result); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2168 | case BO_Add: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2169 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 2170 | std::plus<APSInt>(), Result); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2171 | case BO_Sub: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2172 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 2173 | std::minus<APSInt>(), Result); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2174 | case BO_And: Result = LHS & RHS; return true; |
| 2175 | case BO_Xor: Result = LHS ^ RHS; return true; |
| 2176 | case BO_Or: Result = LHS | RHS; return true; |
| 2177 | case BO_Div: |
| 2178 | case BO_Rem: |
| 2179 | if (RHS == 0) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2180 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2181 | return false; |
| 2182 | } |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2183 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
| 2184 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports |
| 2185 | // this operation and gives the two's complement result. |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2186 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 2187 | LHS.isSigned() && LHS.isMinSignedValue()) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2188 | return HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), |
| 2189 | E->getType()); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2190 | return true; |
| 2191 | case BO_Shl: { |
| 2192 | if (Info.getLangOpts().OpenCL) |
| 2193 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 2194 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 2195 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 2196 | RHS.isUnsigned()); |
| 2197 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 2198 | // During constant-folding, a negative shift is an opposite shift. Such |
| 2199 | // a shift is not a constant expression. |
| 2200 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 2201 | RHS = -RHS; |
| 2202 | goto shift_right; |
| 2203 | } |
| 2204 | shift_left: |
| 2205 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 2206 | // the shifted type. |
| 2207 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2208 | if (SA != RHS) { |
| 2209 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 2210 | << RHS << E->getType() << LHS.getBitWidth(); |
| 2211 | } else if (LHS.isSigned()) { |
| 2212 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 2213 | // operand, and must not overflow the corresponding unsigned type. |
| 2214 | if (LHS.isNegative()) |
| 2215 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 2216 | else if (LHS.countLeadingZeros() < SA) |
| 2217 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 2218 | } |
| 2219 | Result = LHS << SA; |
| 2220 | return true; |
| 2221 | } |
| 2222 | case BO_Shr: { |
| 2223 | if (Info.getLangOpts().OpenCL) |
| 2224 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 2225 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 2226 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 2227 | RHS.isUnsigned()); |
| 2228 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 2229 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 2230 | // shift is not a constant expression. |
| 2231 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 2232 | RHS = -RHS; |
| 2233 | goto shift_left; |
| 2234 | } |
| 2235 | shift_right: |
| 2236 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 2237 | // shifted type. |
| 2238 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2239 | if (SA != RHS) |
| 2240 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 2241 | << RHS << E->getType() << LHS.getBitWidth(); |
| 2242 | Result = LHS >> SA; |
| 2243 | return true; |
| 2244 | } |
| 2245 | |
| 2246 | case BO_LT: Result = LHS < RHS; return true; |
| 2247 | case BO_GT: Result = LHS > RHS; return true; |
| 2248 | case BO_LE: Result = LHS <= RHS; return true; |
| 2249 | case BO_GE: Result = LHS >= RHS; return true; |
| 2250 | case BO_EQ: Result = LHS == RHS; return true; |
| 2251 | case BO_NE: Result = LHS != RHS; return true; |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 2252 | case BO_Cmp: |
| 2253 | llvm_unreachable("BO_Cmp should be handled elsewhere"); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2254 | } |
| 2255 | } |
| 2256 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2257 | /// Perform the given binary floating-point operation, in-place, on LHS. |
| 2258 | static bool handleFloatFloatBinOp(EvalInfo &Info, const Expr *E, |
| 2259 | APFloat &LHS, BinaryOperatorKind Opcode, |
| 2260 | const APFloat &RHS) { |
| 2261 | switch (Opcode) { |
| 2262 | default: |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2263 | Info.FFDiag(E); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2264 | return false; |
| 2265 | case BO_Mul: |
| 2266 | LHS.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 2267 | break; |
| 2268 | case BO_Add: |
| 2269 | LHS.add(RHS, APFloat::rmNearestTiesToEven); |
| 2270 | break; |
| 2271 | case BO_Sub: |
| 2272 | LHS.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 2273 | break; |
| 2274 | case BO_Div: |
| 2275 | LHS.divide(RHS, APFloat::rmNearestTiesToEven); |
| 2276 | break; |
| 2277 | } |
| 2278 | |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2279 | if (LHS.isInfinity() || LHS.isNaN()) { |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2280 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 2281 | return Info.noteUndefinedBehavior(); |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2282 | } |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2283 | return true; |
| 2284 | } |
| 2285 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2286 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 2287 | /// truncating the lvalue's path to the given length. |
| 2288 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 2289 | const RecordDecl *TruncatedType, |
| 2290 | unsigned TruncatedElements) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2291 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2292 | |
| 2293 | // Check we actually point to a derived class object. |
| 2294 | if (TruncatedElements == D.Entries.size()) |
| 2295 | return true; |
| 2296 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 2297 | "not casting to a derived class"); |
| 2298 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 2299 | return false; |
| 2300 | |
| 2301 | // 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] | 2302 | const RecordDecl *RD = TruncatedType; |
| 2303 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2304 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2305 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2306 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2307 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2308 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2309 | else |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2310 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 2311 | RD = Base; |
| 2312 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2313 | D.Entries.resize(TruncatedElements); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2314 | return true; |
| 2315 | } |
| 2316 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2317 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2318 | const CXXRecordDecl *Derived, |
| 2319 | const CXXRecordDecl *Base, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2320 | const ASTRecordLayout *RL = nullptr) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2321 | if (!RL) { |
| 2322 | if (Derived->isInvalidDecl()) return false; |
| 2323 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 2324 | } |
| 2325 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2326 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2327 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2328 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2331 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2332 | const CXXRecordDecl *DerivedDecl, |
| 2333 | const CXXBaseSpecifier *Base) { |
| 2334 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 2335 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2336 | if (!Base->isVirtual()) |
| 2337 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2338 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2339 | SubobjectDesignator &D = Obj.Designator; |
| 2340 | if (D.Invalid) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2341 | return false; |
| 2342 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2343 | // Extract most-derived object and corresponding type. |
| 2344 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 2345 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 2346 | return false; |
| 2347 | |
| 2348 | // Find the virtual base class. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2349 | if (DerivedDecl->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2350 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 2351 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2352 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2353 | return true; |
| 2354 | } |
| 2355 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2356 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
| 2357 | QualType Type, LValue &Result) { |
| 2358 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2359 | PathE = E->path_end(); |
| 2360 | PathI != PathE; ++PathI) { |
| 2361 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 2362 | *PathI)) |
| 2363 | return false; |
| 2364 | Type = (*PathI)->getType(); |
| 2365 | } |
| 2366 | return true; |
| 2367 | } |
| 2368 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2369 | /// Update LVal to refer to the given field, which must be a member of the type |
| 2370 | /// currently described by LVal. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2371 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2372 | const FieldDecl *FD, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2373 | const ASTRecordLayout *RL = nullptr) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2374 | if (!RL) { |
| 2375 | if (FD->getParent()->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2376 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2377 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2378 | |
| 2379 | unsigned I = FD->getFieldIndex(); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 2380 | LVal.adjustOffset(Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I))); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2381 | LVal.addDecl(Info, E, FD); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2382 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2385 | /// Update LVal to refer to the given indirect field. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2386 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2387 | LValue &LVal, |
| 2388 | const IndirectFieldDecl *IFD) { |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 2389 | for (const auto *C : IFD->chain()) |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 2390 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(C))) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2391 | return false; |
| 2392 | return true; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2395 | /// Get the size of the given type in char units. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2396 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 2397 | QualType Type, CharUnits &Size) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2398 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 2399 | // extension. |
| 2400 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 2401 | Size = CharUnits::One(); |
| 2402 | return true; |
| 2403 | } |
| 2404 | |
Saleem Abdulrasool | ada78fe | 2016-06-04 03:16:21 +0000 | [diff] [blame] | 2405 | if (Type->isDependentType()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2406 | Info.FFDiag(Loc); |
Saleem Abdulrasool | ada78fe | 2016-06-04 03:16:21 +0000 | [diff] [blame] | 2407 | return false; |
| 2408 | } |
| 2409 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2410 | if (!Type->isConstantSizeType()) { |
| 2411 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2412 | // FIXME: Better diagnostic. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2413 | Info.FFDiag(Loc); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2414 | return false; |
| 2415 | } |
| 2416 | |
| 2417 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 2418 | return true; |
| 2419 | } |
| 2420 | |
| 2421 | /// Update a pointer value to model pointer arithmetic. |
| 2422 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2423 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2424 | /// \param LVal - The pointer value to be updated. |
| 2425 | /// \param EltTy - The pointee type represented by LVal. |
| 2426 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2427 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 2428 | LValue &LVal, QualType EltTy, |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 2429 | APSInt Adjustment) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2430 | CharUnits SizeOfPointee; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2431 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2432 | return false; |
| 2433 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 2434 | LVal.adjustOffsetAndIndex(Info, E, Adjustment, SizeOfPointee); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2435 | return true; |
| 2436 | } |
| 2437 | |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 2438 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 2439 | LValue &LVal, QualType EltTy, |
| 2440 | int64_t Adjustment) { |
| 2441 | return HandleLValueArrayAdjustment(Info, E, LVal, EltTy, |
| 2442 | APSInt::get(Adjustment)); |
| 2443 | } |
| 2444 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2445 | /// Update an lvalue to refer to a component of a complex number. |
| 2446 | /// \param Info - Information about the ongoing evaluation. |
| 2447 | /// \param LVal - The lvalue to be updated. |
| 2448 | /// \param EltTy - The complex number's component type. |
| 2449 | /// \param Imag - False for the real component, true for the imaginary. |
| 2450 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 2451 | LValue &LVal, QualType EltTy, |
| 2452 | bool Imag) { |
| 2453 | if (Imag) { |
| 2454 | CharUnits SizeOfComponent; |
| 2455 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 2456 | return false; |
| 2457 | LVal.Offset += SizeOfComponent; |
| 2458 | } |
| 2459 | LVal.addComplex(Info, E, EltTy, Imag); |
| 2460 | return true; |
| 2461 | } |
| 2462 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 2463 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
| 2464 | QualType Type, const LValue &LVal, |
| 2465 | APValue &RVal); |
| 2466 | |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2467 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2468 | /// |
| 2469 | /// \param Info Information about the ongoing evaluation. |
| 2470 | /// \param E An expression to be used when printing diagnostics. |
| 2471 | /// \param VD The variable whose initializer should be obtained. |
| 2472 | /// \param Frame The frame in which the variable was created. Must be null |
| 2473 | /// if this variable is not local to the evaluation. |
| 2474 | /// \param Result Filled in with a pointer to the value of the variable. |
| 2475 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 2476 | const VarDecl *VD, CallStackFrame *Frame, |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 2477 | APValue *&Result, const LValue *LVal) { |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 2478 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2479 | // If this is a parameter to an active constexpr function call, perform |
| 2480 | // argument substitution. |
| 2481 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2482 | // Assume arguments of a potential constant expression are unknown |
| 2483 | // constant expressions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2484 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2485 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2486 | if (!Frame || !Frame->Arguments) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2487 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2488 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2489 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2490 | Result = &Frame->Arguments[PVD->getFunctionScopeIndex()]; |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2491 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2492 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2493 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2494 | // If this is a local variable, dig out its value. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2495 | if (Frame) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 2496 | Result = LVal ? Frame->getTemporary(VD, LVal->getLValueVersion()) |
| 2497 | : Frame->getCurrentTemporary(VD); |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 2498 | if (!Result) { |
| 2499 | // Assume variables referenced within a lambda's call operator that were |
| 2500 | // not declared within the call operator are captures and during checking |
| 2501 | // of a potential constant expression, assume they are unknown constant |
| 2502 | // expressions. |
| 2503 | assert(isLambdaCallOperator(Frame->Callee) && |
| 2504 | (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && |
| 2505 | "missing value for local variable"); |
| 2506 | if (Info.checkingPotentialConstantExpression()) |
| 2507 | return false; |
| 2508 | // FIXME: implement capture evaluation during constant expr evaluation. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2509 | Info.FFDiag(E->getLocStart(), |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 2510 | diag::note_unimplemented_constexpr_lambda_feature_ast) |
| 2511 | << "captures not currently allowed"; |
| 2512 | return false; |
| 2513 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2514 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2517 | // Dig out the initializer, and use the declaration which it's attached to. |
| 2518 | const Expr *Init = VD->getAnyInitializer(VD); |
| 2519 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2520 | // If we're checking a potential constant expression, the variable could be |
| 2521 | // initialized later. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2522 | if (!Info.checkingPotentialConstantExpression()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2523 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2524 | return false; |
| 2525 | } |
| 2526 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2527 | // If we're currently evaluating the initializer of this declaration, use that |
| 2528 | // in-flight value. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2529 | if (Info.EvaluatingDecl.dyn_cast<const ValueDecl*>() == VD) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2530 | Result = Info.EvaluatingDeclValue; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2531 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2534 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 2535 | // this is the definition which will be used. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2536 | if (VD->isWeak()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2537 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2538 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2539 | } |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2540 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2541 | // Check that we can fold the initializer. In C++, we will have already done |
| 2542 | // this in the cases where it matters for conformance. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2543 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2544 | if (!VD->evaluateValue(Notes)) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2545 | Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2546 | Notes.size() + 1) << VD; |
| 2547 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2548 | Info.addNotes(Notes); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2549 | return false; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2550 | } else if (!VD->checkInitIsICE()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2551 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2552 | Notes.size() + 1) << VD; |
| 2553 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2554 | Info.addNotes(Notes); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2555 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2556 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2557 | Result = VD->getEvaluatedValue(); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2558 | return true; |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2561 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2562 | Qualifiers Quals = T.getQualifiers(); |
| 2563 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 2564 | } |
| 2565 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2566 | /// Get the base index of the given base class within an APValue representing |
| 2567 | /// the given derived class. |
| 2568 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 2569 | const CXXRecordDecl *Base) { |
| 2570 | Base = Base->getCanonicalDecl(); |
| 2571 | unsigned Index = 0; |
| 2572 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 2573 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 2574 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 2575 | return Index; |
| 2576 | } |
| 2577 | |
| 2578 | llvm_unreachable("base class missing from derived class's bases list"); |
| 2579 | } |
| 2580 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2581 | /// Extract the value of a character from a string literal. |
| 2582 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
| 2583 | uint64_t Index) { |
Akira Hatanaka | bc33264 | 2017-01-31 02:31:39 +0000 | [diff] [blame] | 2584 | // FIXME: Support MakeStringConstant |
| 2585 | if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Lit)) { |
| 2586 | std::string Str; |
| 2587 | Info.Ctx.getObjCEncodingForType(ObjCEnc->getEncodedType(), Str); |
| 2588 | assert(Index <= Str.size() && "Index too large"); |
| 2589 | return APSInt::getUnsigned(Str.c_str()[Index]); |
| 2590 | } |
| 2591 | |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2592 | if (auto PE = dyn_cast<PredefinedExpr>(Lit)) |
| 2593 | Lit = PE->getFunctionName(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2594 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 2595 | const ConstantArrayType *CAT = |
| 2596 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2597 | assert(CAT && "string literal isn't an array"); |
| 2598 | QualType CharType = CAT->getElementType(); |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2599 | assert(CharType->isIntegerType() && "unexpected character type"); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2600 | |
| 2601 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2602 | CharType->isUnsignedIntegerType()); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2603 | if (Index < S->getLength()) |
| 2604 | Value = S->getCodeUnit(Index); |
| 2605 | return Value; |
| 2606 | } |
| 2607 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2608 | // Expand a string literal into an array of characters. |
| 2609 | static void expandStringLiteral(EvalInfo &Info, const Expr *Lit, |
| 2610 | APValue &Result) { |
| 2611 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 2612 | const ConstantArrayType *CAT = |
| 2613 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2614 | assert(CAT && "string literal isn't an array"); |
| 2615 | QualType CharType = CAT->getElementType(); |
| 2616 | assert(CharType->isIntegerType() && "unexpected character type"); |
| 2617 | |
| 2618 | unsigned Elts = CAT->getSize().getZExtValue(); |
| 2619 | Result = APValue(APValue::UninitArray(), |
| 2620 | std::min(S->getLength(), Elts), Elts); |
| 2621 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 2622 | CharType->isUnsignedIntegerType()); |
| 2623 | if (Result.hasArrayFiller()) |
| 2624 | Result.getArrayFiller() = APValue(Value); |
| 2625 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
| 2626 | Value = S->getCodeUnit(I); |
| 2627 | Result.getArrayInitializedElt(I) = APValue(Value); |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | // Expand an array so that it has more than Index filled elements. |
| 2632 | static void expandArray(APValue &Array, unsigned Index) { |
| 2633 | unsigned Size = Array.getArraySize(); |
| 2634 | assert(Index < Size); |
| 2635 | |
| 2636 | // Always at least double the number of elements for which we store a value. |
| 2637 | unsigned OldElts = Array.getArrayInitializedElts(); |
| 2638 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
| 2639 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
| 2640 | |
| 2641 | // Copy the data across. |
| 2642 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
| 2643 | for (unsigned I = 0; I != OldElts; ++I) |
| 2644 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
| 2645 | for (unsigned I = OldElts; I != NewElts; ++I) |
| 2646 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
| 2647 | if (NewValue.hasArrayFiller()) |
| 2648 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
| 2649 | Array.swap(NewValue); |
| 2650 | } |
| 2651 | |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2652 | /// Determine whether a type would actually be read by an lvalue-to-rvalue |
| 2653 | /// conversion. If it's of class type, we may assume that the copy operation |
| 2654 | /// is trivial. Note that this is never true for a union type with fields |
| 2655 | /// (because the copy always "reads" the active member) and always true for |
| 2656 | /// a non-class type. |
| 2657 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
| 2658 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2659 | if (!RD || (RD->isUnion() && !RD->field_empty())) |
| 2660 | return true; |
| 2661 | if (RD->isEmpty()) |
| 2662 | return false; |
| 2663 | |
| 2664 | for (auto *Field : RD->fields()) |
| 2665 | if (isReadByLvalueToRvalueConversion(Field->getType())) |
| 2666 | return true; |
| 2667 | |
| 2668 | for (auto &BaseSpec : RD->bases()) |
| 2669 | if (isReadByLvalueToRvalueConversion(BaseSpec.getType())) |
| 2670 | return true; |
| 2671 | |
| 2672 | return false; |
| 2673 | } |
| 2674 | |
| 2675 | /// Diagnose an attempt to read from any unreadable field within the specified |
| 2676 | /// type, which might be a class type. |
| 2677 | static bool diagnoseUnreadableFields(EvalInfo &Info, const Expr *E, |
| 2678 | QualType T) { |
| 2679 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2680 | if (!RD) |
| 2681 | return false; |
| 2682 | |
| 2683 | if (!RD->hasMutableFields()) |
| 2684 | return false; |
| 2685 | |
| 2686 | for (auto *Field : RD->fields()) { |
| 2687 | // If we're actually going to read this field in some way, then it can't |
| 2688 | // be mutable. If we're in a union, then assigning to a mutable field |
| 2689 | // (even an empty one) can change the active member, so that's not OK. |
| 2690 | // FIXME: Add core issue number for the union case. |
| 2691 | if (Field->isMutable() && |
| 2692 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2693 | Info.FFDiag(E, diag::note_constexpr_ltor_mutable, 1) << Field; |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2694 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2695 | return true; |
| 2696 | } |
| 2697 | |
| 2698 | if (diagnoseUnreadableFields(Info, E, Field->getType())) |
| 2699 | return true; |
| 2700 | } |
| 2701 | |
| 2702 | for (auto &BaseSpec : RD->bases()) |
| 2703 | if (diagnoseUnreadableFields(Info, E, BaseSpec.getType())) |
| 2704 | return true; |
| 2705 | |
| 2706 | // All mutable fields were empty, and thus not actually read. |
| 2707 | return false; |
| 2708 | } |
| 2709 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2710 | /// Kinds of access we can perform on an object, for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2711 | enum AccessKinds { |
| 2712 | AK_Read, |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2713 | AK_Assign, |
| 2714 | AK_Increment, |
| 2715 | AK_Decrement |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2716 | }; |
| 2717 | |
Benjamin Kramer | 5b4296a | 2015-10-28 17:16:26 +0000 | [diff] [blame] | 2718 | namespace { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2719 | /// A handle to a complete object (an object that is not a subobject of |
| 2720 | /// another object). |
| 2721 | struct CompleteObject { |
| 2722 | /// The value of the complete object. |
| 2723 | APValue *Value; |
| 2724 | /// The type of the complete object. |
| 2725 | QualType Type; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2726 | bool LifetimeStartedInEvaluation; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2727 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2728 | CompleteObject() : Value(nullptr) {} |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2729 | CompleteObject(APValue *Value, QualType Type, |
| 2730 | bool LifetimeStartedInEvaluation) |
| 2731 | : Value(Value), Type(Type), |
| 2732 | LifetimeStartedInEvaluation(LifetimeStartedInEvaluation) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2733 | assert(Value && "missing value for complete object"); |
| 2734 | } |
| 2735 | |
Aaron Ballman | 6734766 | 2015-02-15 22:00:28 +0000 | [diff] [blame] | 2736 | explicit operator bool() const { return Value; } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2737 | }; |
Benjamin Kramer | 5b4296a | 2015-10-28 17:16:26 +0000 | [diff] [blame] | 2738 | } // end anonymous namespace |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2739 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2740 | /// Find the designated sub-object of an rvalue. |
| 2741 | template<typename SubobjectHandler> |
| 2742 | typename SubobjectHandler::result_type |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2743 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2744 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2745 | if (Sub.Invalid) |
| 2746 | // A diagnostic will have already been produced. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2747 | return handler.failed(); |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 2748 | if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2749 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 2750 | Info.FFDiag(E, Sub.isOnePastTheEnd() |
| 2751 | ? diag::note_constexpr_access_past_end |
| 2752 | : diag::note_constexpr_access_unsized_array) |
| 2753 | << handler.AccessKind; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2754 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2755 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2756 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2757 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2758 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2759 | APValue *O = Obj.Value; |
| 2760 | QualType ObjType = Obj.Type; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2761 | const FieldDecl *LastField = nullptr; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2762 | const bool MayReadMutableMembers = |
| 2763 | Obj.LifetimeStartedInEvaluation && Info.getLangOpts().CPlusPlus14; |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2764 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2765 | // Walk the designator's path to find the subobject. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2766 | for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) { |
| 2767 | if (O->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2768 | if (!Info.checkingPotentialConstantExpression()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2769 | Info.FFDiag(E, diag::note_constexpr_access_uninit) << handler.AccessKind; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2770 | return handler.failed(); |
| 2771 | } |
| 2772 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2773 | if (I == N) { |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2774 | // If we are reading an object of class type, there may still be more |
| 2775 | // things we need to check: if there are any mutable subobjects, we |
| 2776 | // cannot perform this read. (This only happens when performing a trivial |
| 2777 | // copy or assignment.) |
| 2778 | if (ObjType->isRecordType() && handler.AccessKind == AK_Read && |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2779 | !MayReadMutableMembers && diagnoseUnreadableFields(Info, E, ObjType)) |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2780 | return handler.failed(); |
| 2781 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2782 | if (!handler.found(*O, ObjType)) |
| 2783 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2784 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2785 | // If we modified a bit-field, truncate it to the right width. |
| 2786 | if (handler.AccessKind != AK_Read && |
| 2787 | LastField && LastField->isBitField() && |
| 2788 | !truncateBitfieldValue(Info, E, *O, LastField)) |
| 2789 | return false; |
| 2790 | |
| 2791 | return true; |
| 2792 | } |
| 2793 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2794 | LastField = nullptr; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2795 | if (ObjType->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2796 | // Next subobject is an array element. |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2797 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2798 | assert(CAT && "vla in literal type?"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2799 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2800 | if (CAT->getSize().ule(Index)) { |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2801 | // Note, it should not be possible to form a pointer with a valid |
| 2802 | // designator which points more than one past the end of the array. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2803 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2804 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2805 | << handler.AccessKind; |
| 2806 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2807 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2808 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2809 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2810 | |
| 2811 | ObjType = CAT->getElementType(); |
| 2812 | |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2813 | // An array object is represented as either an Array APValue or as an |
| 2814 | // LValue which refers to a string literal. |
| 2815 | if (O->isLValue()) { |
| 2816 | assert(I == N - 1 && "extracting subobject of character?"); |
| 2817 | assert(!O->hasLValuePath() || O->getLValuePath().empty()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2818 | if (handler.AccessKind != AK_Read) |
| 2819 | expandStringLiteral(Info, O->getLValueBase().get<const Expr *>(), |
| 2820 | *O); |
| 2821 | else |
| 2822 | return handler.foundString(*O, ObjType, Index); |
| 2823 | } |
| 2824 | |
| 2825 | if (O->getArrayInitializedElts() > Index) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2826 | O = &O->getArrayInitializedElt(Index); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2827 | else if (handler.AccessKind != AK_Read) { |
| 2828 | expandArray(*O, Index); |
| 2829 | O = &O->getArrayInitializedElt(Index); |
| 2830 | } else |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2831 | O = &O->getArrayFiller(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2832 | } else if (ObjType->isAnyComplexType()) { |
| 2833 | // Next subobject is a complex number. |
| 2834 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 2835 | if (Index > 1) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2836 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2837 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2838 | << handler.AccessKind; |
| 2839 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2840 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2841 | return handler.failed(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2842 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2843 | |
| 2844 | bool WasConstQualified = ObjType.isConstQualified(); |
| 2845 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 2846 | if (WasConstQualified) |
| 2847 | ObjType.addConst(); |
| 2848 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2849 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 2850 | if (O->isComplexInt()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2851 | return handler.found(Index ? O->getComplexIntImag() |
| 2852 | : O->getComplexIntReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2853 | } else { |
| 2854 | assert(O->isComplexFloat()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2855 | return handler.found(Index ? O->getComplexFloatImag() |
| 2856 | : O->getComplexFloatReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2857 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2858 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2859 | // In C++14 onwards, it is permitted to read a mutable member whose |
| 2860 | // lifetime began within the evaluation. |
| 2861 | // FIXME: Should we also allow this in C++11? |
| 2862 | if (Field->isMutable() && handler.AccessKind == AK_Read && |
| 2863 | !MayReadMutableMembers) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2864 | Info.FFDiag(E, diag::note_constexpr_ltor_mutable, 1) |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2865 | << Field; |
| 2866 | Info.Note(Field->getLocation(), diag::note_declared_at); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2867 | return handler.failed(); |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2870 | // Next subobject is a class, struct or union field. |
| 2871 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 2872 | if (RD->isUnion()) { |
| 2873 | const FieldDecl *UnionField = O->getUnionField(); |
| 2874 | if (!UnionField || |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2875 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2876 | Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2877 | << handler.AccessKind << Field << !UnionField << UnionField; |
| 2878 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2879 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2880 | O = &O->getUnionValue(); |
| 2881 | } else |
| 2882 | O = &O->getStructField(Field->getFieldIndex()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2883 | |
| 2884 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2885 | ObjType = Field->getType(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2886 | if (WasConstQualified && !Field->isMutable()) |
| 2887 | ObjType.addConst(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2888 | |
| 2889 | if (ObjType.isVolatileQualified()) { |
| 2890 | if (Info.getLangOpts().CPlusPlus) { |
| 2891 | // FIXME: Include a description of the path to the volatile subobject. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2892 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2893 | << handler.AccessKind << 2 << Field; |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2894 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2895 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2896 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2897 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2898 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2899 | } |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2900 | |
| 2901 | LastField = Field; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2902 | } else { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2903 | // Next subobject is a base class. |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2904 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 2905 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 2906 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2907 | |
| 2908 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2909 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2910 | if (WasConstQualified) |
| 2911 | ObjType.addConst(); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2912 | } |
| 2913 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2916 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2917 | struct ExtractSubobjectHandler { |
| 2918 | EvalInfo &Info; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2919 | APValue &Result; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2920 | |
| 2921 | static const AccessKinds AccessKind = AK_Read; |
| 2922 | |
| 2923 | typedef bool result_type; |
| 2924 | bool failed() { return false; } |
| 2925 | bool found(APValue &Subobj, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2926 | Result = Subobj; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2927 | return true; |
| 2928 | } |
| 2929 | bool found(APSInt &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2930 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2931 | return true; |
| 2932 | } |
| 2933 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2934 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2935 | return true; |
| 2936 | } |
| 2937 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2938 | Result = APValue(extractStringLiteralCharacter( |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2939 | Info, Subobj.getLValueBase().get<const Expr *>(), Character)); |
| 2940 | return true; |
| 2941 | } |
| 2942 | }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2943 | } // end anonymous namespace |
| 2944 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2945 | const AccessKinds ExtractSubobjectHandler::AccessKind; |
| 2946 | |
| 2947 | /// Extract the designated sub-object of an rvalue. |
| 2948 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2949 | const CompleteObject &Obj, |
| 2950 | const SubobjectDesignator &Sub, |
| 2951 | APValue &Result) { |
| 2952 | ExtractSubobjectHandler Handler = { Info, Result }; |
| 2953 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2956 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2957 | struct ModifySubobjectHandler { |
| 2958 | EvalInfo &Info; |
| 2959 | APValue &NewVal; |
| 2960 | const Expr *E; |
| 2961 | |
| 2962 | typedef bool result_type; |
| 2963 | static const AccessKinds AccessKind = AK_Assign; |
| 2964 | |
| 2965 | bool checkConst(QualType QT) { |
| 2966 | // Assigning to a const object has undefined behavior. |
| 2967 | if (QT.isConstQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2968 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2969 | return false; |
| 2970 | } |
| 2971 | return true; |
| 2972 | } |
| 2973 | |
| 2974 | bool failed() { return false; } |
| 2975 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2976 | if (!checkConst(SubobjType)) |
| 2977 | return false; |
| 2978 | // We've been given ownership of NewVal, so just swap it in. |
| 2979 | Subobj.swap(NewVal); |
| 2980 | return true; |
| 2981 | } |
| 2982 | bool found(APSInt &Value, QualType SubobjType) { |
| 2983 | if (!checkConst(SubobjType)) |
| 2984 | return false; |
| 2985 | if (!NewVal.isInt()) { |
| 2986 | // Maybe trying to write a cast pointer value into a complex? |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2987 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2988 | return false; |
| 2989 | } |
| 2990 | Value = NewVal.getInt(); |
| 2991 | return true; |
| 2992 | } |
| 2993 | bool found(APFloat &Value, QualType SubobjType) { |
| 2994 | if (!checkConst(SubobjType)) |
| 2995 | return false; |
| 2996 | Value = NewVal.getFloat(); |
| 2997 | return true; |
| 2998 | } |
| 2999 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 3000 | llvm_unreachable("shouldn't encounter string elements with ExpandArrays"); |
| 3001 | } |
| 3002 | }; |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 3003 | } // end anonymous namespace |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3004 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3005 | const AccessKinds ModifySubobjectHandler::AccessKind; |
| 3006 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3007 | /// Update the designated sub-object of an rvalue to the given value. |
| 3008 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3009 | const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3010 | const SubobjectDesignator &Sub, |
| 3011 | APValue &NewVal) { |
| 3012 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3013 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3016 | /// Find the position where two subobject designators diverge, or equivalently |
| 3017 | /// the length of the common initial subsequence. |
| 3018 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 3019 | const SubobjectDesignator &A, |
| 3020 | const SubobjectDesignator &B, |
| 3021 | bool &WasArrayIndex) { |
| 3022 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 3023 | for (/**/; I != N; ++I) { |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3024 | if (!ObjType.isNull() && |
| 3025 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3026 | // Next subobject is an array element. |
| 3027 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 3028 | WasArrayIndex = true; |
| 3029 | return I; |
| 3030 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3031 | if (ObjType->isAnyComplexType()) |
| 3032 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 3033 | else |
| 3034 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3035 | } else { |
| 3036 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 3037 | WasArrayIndex = false; |
| 3038 | return I; |
| 3039 | } |
| 3040 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 3041 | // Next subobject is a field. |
| 3042 | ObjType = FD->getType(); |
| 3043 | else |
| 3044 | // Next subobject is a base class. |
| 3045 | ObjType = QualType(); |
| 3046 | } |
| 3047 | } |
| 3048 | WasArrayIndex = false; |
| 3049 | return I; |
| 3050 | } |
| 3051 | |
| 3052 | /// Determine whether the given subobject designators refer to elements of the |
| 3053 | /// same array object. |
| 3054 | static bool AreElementsOfSameArray(QualType ObjType, |
| 3055 | const SubobjectDesignator &A, |
| 3056 | const SubobjectDesignator &B) { |
| 3057 | if (A.Entries.size() != B.Entries.size()) |
| 3058 | return false; |
| 3059 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 3060 | bool IsArray = A.MostDerivedIsArrayElement; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3061 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 3062 | // A is a subobject of the array element. |
| 3063 | return false; |
| 3064 | |
| 3065 | // If A (and B) designates an array element, the last entry will be the array |
| 3066 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 3067 | // of length 1' case, and the entire path must match. |
| 3068 | bool WasArrayIndex; |
| 3069 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 3070 | return CommonLength >= A.Entries.size() - IsArray; |
| 3071 | } |
| 3072 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3073 | /// Find the complete object to which an LValue refers. |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 3074 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
| 3075 | AccessKinds AK, const LValue &LVal, |
| 3076 | QualType LValType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3077 | if (!LVal.Base) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3078 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3079 | return CompleteObject(); |
| 3080 | } |
| 3081 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3082 | CallStackFrame *Frame = nullptr; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3083 | if (LVal.getLValueCallIndex()) { |
| 3084 | Frame = Info.getCallFrame(LVal.getLValueCallIndex()); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3085 | if (!Frame) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3086 | Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3087 | << AK << LVal.Base.is<const ValueDecl*>(); |
| 3088 | NoteLValueLocation(Info, LVal.Base); |
| 3089 | return CompleteObject(); |
| 3090 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 3094 | // is not a constant expression (even if the object is non-volatile). We also |
| 3095 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 3096 | // semantics. |
| 3097 | if (LValType.isVolatileQualified()) { |
| 3098 | if (Info.getLangOpts().CPlusPlus) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3099 | Info.FFDiag(E, diag::note_constexpr_access_volatile_type) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3100 | << AK << LValType; |
| 3101 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3102 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3103 | return CompleteObject(); |
| 3104 | } |
| 3105 | |
| 3106 | // Compute value storage location and type of base object. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3107 | APValue *BaseVal = nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3108 | QualType BaseType = getType(LVal.Base); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3109 | bool LifetimeStartedInEvaluation = Frame; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3110 | |
| 3111 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
| 3112 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 3113 | // In C++11, constexpr, non-volatile variables initialized with constant |
| 3114 | // expressions are constant expressions too. Inside constexpr functions, |
| 3115 | // parameters are constant expressions even if they're non-const. |
| 3116 | // In C++1y, objects local to a constant expression (those with a Frame) are |
| 3117 | // both readable and writable inside constant expressions. |
| 3118 | // In C, such things can also be folded, although they are not ICEs. |
| 3119 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
| 3120 | if (VD) { |
| 3121 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 3122 | VD = VDef; |
| 3123 | } |
| 3124 | if (!VD || VD->isInvalidDecl()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3125 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3126 | return CompleteObject(); |
| 3127 | } |
| 3128 | |
| 3129 | // Accesses of volatile-qualified objects are not allowed. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3130 | if (BaseType.isVolatileQualified()) { |
| 3131 | if (Info.getLangOpts().CPlusPlus) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3132 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3133 | << AK << 1 << VD; |
| 3134 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3135 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3136 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3137 | } |
| 3138 | return CompleteObject(); |
| 3139 | } |
| 3140 | |
| 3141 | // Unless we're looking at a local variable or argument in a constexpr call, |
| 3142 | // the variable we're reading must be const. |
| 3143 | if (!Frame) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3144 | if (Info.getLangOpts().CPlusPlus14 && |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3145 | VD == Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()) { |
| 3146 | // OK, we can read and modify an object if we're in the process of |
| 3147 | // evaluating its initializer, because its lifetime began in this |
| 3148 | // evaluation. |
| 3149 | } else if (AK != AK_Read) { |
| 3150 | // All the remaining cases only permit reading. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3151 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3152 | return CompleteObject(); |
George Burgess IV | b531698 | 2016-12-27 05:33:20 +0000 | [diff] [blame] | 3153 | } else if (VD->isConstexpr()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3154 | // OK, we can read this variable. |
| 3155 | } else if (BaseType->isIntegralOrEnumerationType()) { |
Xiuli Pan | 244e3f6 | 2016-06-07 04:34:00 +0000 | [diff] [blame] | 3156 | // In OpenCL if a variable is in constant address space it is a const value. |
| 3157 | if (!(BaseType.isConstQualified() || |
| 3158 | (Info.getLangOpts().OpenCL && |
| 3159 | BaseType.getAddressSpace() == LangAS::opencl_constant))) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3160 | if (Info.getLangOpts().CPlusPlus) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3161 | Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3162 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3163 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3164 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3165 | } |
| 3166 | return CompleteObject(); |
| 3167 | } |
| 3168 | } else if (BaseType->isFloatingType() && BaseType.isConstQualified()) { |
| 3169 | // We support folding of const floating-point types, in order to make |
| 3170 | // static const data members of such types (supported as an extension) |
| 3171 | // more useful. |
| 3172 | if (Info.getLangOpts().CPlusPlus11) { |
| 3173 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 3174 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3175 | } else { |
| 3176 | Info.CCEDiag(E); |
| 3177 | } |
George Burgess IV | b531698 | 2016-12-27 05:33:20 +0000 | [diff] [blame] | 3178 | } else if (BaseType.isConstQualified() && VD->hasDefinition(Info.Ctx)) { |
| 3179 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr) << VD; |
| 3180 | // Keep evaluating to see what we can do. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3181 | } else { |
| 3182 | // FIXME: Allow folding of values of any literal type in all languages. |
Richard Smith | c0d04a2 | 2016-05-25 22:06:25 +0000 | [diff] [blame] | 3183 | if (Info.checkingPotentialConstantExpression() && |
| 3184 | VD->getType().isConstQualified() && !VD->hasDefinition(Info.Ctx)) { |
| 3185 | // The definition of this variable could be constexpr. We can't |
| 3186 | // access it right now, but may be able to in future. |
| 3187 | } else if (Info.getLangOpts().CPlusPlus11) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3188 | Info.FFDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3189 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3190 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3191 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3192 | } |
| 3193 | return CompleteObject(); |
| 3194 | } |
| 3195 | } |
| 3196 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3197 | if (!evaluateVarDeclInit(Info, E, VD, Frame, BaseVal, &LVal)) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3198 | return CompleteObject(); |
| 3199 | } else { |
| 3200 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| 3201 | |
| 3202 | if (!Frame) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3203 | if (const MaterializeTemporaryExpr *MTE = |
| 3204 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 3205 | assert(MTE->getStorageDuration() == SD_Static && |
| 3206 | "should have a frame for a non-global materialized temporary"); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3207 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3208 | // Per C++1y [expr.const]p2: |
| 3209 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
| 3210 | // - a [...] glvalue of integral or enumeration type that refers to |
| 3211 | // a non-volatile const object [...] |
| 3212 | // [...] |
| 3213 | // - a [...] glvalue of literal type that refers to a non-volatile |
| 3214 | // object whose lifetime began within the evaluation of e. |
| 3215 | // |
| 3216 | // C++11 misses the 'began within the evaluation of e' check and |
| 3217 | // instead allows all temporaries, including things like: |
| 3218 | // int &&r = 1; |
| 3219 | // int x = ++r; |
| 3220 | // constexpr int k = r; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3221 | // Therefore we use the C++14 rules in C++11 too. |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3222 | const ValueDecl *VD = Info.EvaluatingDecl.dyn_cast<const ValueDecl*>(); |
| 3223 | const ValueDecl *ED = MTE->getExtendingDecl(); |
| 3224 | if (!(BaseType.isConstQualified() && |
| 3225 | BaseType->isIntegralOrEnumerationType()) && |
| 3226 | !(VD && VD->getCanonicalDecl() == ED->getCanonicalDecl())) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3227 | Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3228 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
| 3229 | return CompleteObject(); |
| 3230 | } |
| 3231 | |
| 3232 | BaseVal = Info.Ctx.getMaterializedTemporaryValue(MTE, false); |
| 3233 | assert(BaseVal && "got reference to unevaluated temporary"); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3234 | LifetimeStartedInEvaluation = true; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3235 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3236 | Info.FFDiag(E); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3237 | return CompleteObject(); |
| 3238 | } |
| 3239 | } else { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3240 | BaseVal = Frame->getTemporary(Base, LVal.Base.getVersion()); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3241 | assert(BaseVal && "missing value for temporary"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3242 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3243 | |
| 3244 | // Volatile temporary objects cannot be accessed in constant expressions. |
| 3245 | if (BaseType.isVolatileQualified()) { |
| 3246 | if (Info.getLangOpts().CPlusPlus) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3247 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3248 | << AK << 0; |
| 3249 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 3250 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3251 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3252 | } |
| 3253 | return CompleteObject(); |
| 3254 | } |
| 3255 | } |
| 3256 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3257 | // During the construction of an object, it is not yet 'const'. |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 3258 | // 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] | 3259 | // object under construction. |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3260 | if (Info.isEvaluatingConstructor(LVal.getLValueBase(), |
| 3261 | LVal.getLValueCallIndex(), |
| 3262 | LVal.getLValueVersion())) { |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3263 | BaseType = Info.Ctx.getCanonicalType(BaseType); |
| 3264 | BaseType.removeLocalConst(); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3265 | LifetimeStartedInEvaluation = true; |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3268 | // 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] | 3269 | // evaluating after an unmodeled side effect. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3270 | // |
| 3271 | // FIXME: Not all local state is mutable. Allow local constant subobjects |
| 3272 | // to be read here (but take care with 'mutable' fields). |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 3273 | if ((Frame && Info.getLangOpts().CPlusPlus14 && |
| 3274 | Info.EvalStatus.HasSideEffects) || |
| 3275 | (AK != AK_Read && Info.IsSpeculativelyEvaluating)) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3276 | return CompleteObject(); |
| 3277 | |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3278 | return CompleteObject(BaseVal, BaseType, LifetimeStartedInEvaluation); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3279 | } |
| 3280 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3281 | /// Perform an lvalue-to-rvalue conversion on the given glvalue. This |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3282 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
| 3283 | /// glvalue referred to by an entity of reference type. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3284 | /// |
| 3285 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3286 | /// \param Conv - The expression for which we are performing the conversion. |
| 3287 | /// Used for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3288 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
| 3289 | /// case of a non-class type). |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3290 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 3291 | /// \param RVal - The produced value will be placed here. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3292 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3293 | QualType Type, |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3294 | const LValue &LVal, APValue &RVal) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3295 | if (LVal.Designator.Invalid) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3296 | return false; |
| 3297 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3298 | // 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] | 3299 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3300 | if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3301 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 3302 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 3303 | // initializer until now for such expressions. Such an expression can't be |
| 3304 | // an ICE in C, so this only matters for fold. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3305 | if (Type.isVolatileQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3306 | Info.FFDiag(Conv); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3307 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3308 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3309 | APValue Lit; |
| 3310 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
| 3311 | return false; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3312 | CompleteObject LitObj(&Lit, Base->getType(), false); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3313 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal); |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 3314 | } else if (isa<StringLiteral>(Base) || isa<PredefinedExpr>(Base)) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3315 | // We represent a string literal array as an lvalue pointing at the |
| 3316 | // corresponding expression, rather than building an array of chars. |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 3317 | // FIXME: Support ObjCEncodeExpr, MakeStringConstant |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3318 | APValue Str(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3319 | CompleteObject StrObj(&Str, Base->getType(), false); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3320 | return extractSubobject(Info, Conv, StrObj, LVal.Designator, RVal); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3321 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3324 | CompleteObject Obj = findCompleteObject(Info, Conv, AK_Read, LVal, Type); |
| 3325 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
| 3328 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3329 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3330 | QualType LValType, APValue &Val) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3331 | if (LVal.Designator.Invalid) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3332 | return false; |
| 3333 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3334 | if (!Info.getLangOpts().CPlusPlus14) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3335 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3336 | return false; |
| 3337 | } |
| 3338 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3339 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3340 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
| 3341 | } |
| 3342 | |
| 3343 | namespace { |
| 3344 | struct CompoundAssignSubobjectHandler { |
| 3345 | EvalInfo &Info; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3346 | const Expr *E; |
| 3347 | QualType PromotedLHSType; |
| 3348 | BinaryOperatorKind Opcode; |
| 3349 | const APValue &RHS; |
| 3350 | |
| 3351 | static const AccessKinds AccessKind = AK_Assign; |
| 3352 | |
| 3353 | typedef bool result_type; |
| 3354 | |
| 3355 | bool checkConst(QualType QT) { |
| 3356 | // Assigning to a const object has undefined behavior. |
| 3357 | if (QT.isConstQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3358 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3359 | return false; |
| 3360 | } |
| 3361 | return true; |
| 3362 | } |
| 3363 | |
| 3364 | bool failed() { return false; } |
| 3365 | bool found(APValue &Subobj, QualType SubobjType) { |
| 3366 | switch (Subobj.getKind()) { |
| 3367 | case APValue::Int: |
| 3368 | return found(Subobj.getInt(), SubobjType); |
| 3369 | case APValue::Float: |
| 3370 | return found(Subobj.getFloat(), SubobjType); |
| 3371 | case APValue::ComplexInt: |
| 3372 | case APValue::ComplexFloat: |
| 3373 | // FIXME: Implement complex compound assignment. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3374 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3375 | return false; |
| 3376 | case APValue::LValue: |
| 3377 | return foundPointer(Subobj, SubobjType); |
| 3378 | default: |
| 3379 | // FIXME: can this happen? |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3380 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3381 | return false; |
| 3382 | } |
| 3383 | } |
| 3384 | bool found(APSInt &Value, QualType SubobjType) { |
| 3385 | if (!checkConst(SubobjType)) |
| 3386 | return false; |
| 3387 | |
| 3388 | if (!SubobjType->isIntegerType() || !RHS.isInt()) { |
| 3389 | // We don't support compound assignment on integer-cast-to-pointer |
| 3390 | // values. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3391 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3392 | return false; |
| 3393 | } |
| 3394 | |
| 3395 | APSInt LHS = HandleIntToIntCast(Info, E, PromotedLHSType, |
| 3396 | SubobjType, Value); |
| 3397 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
| 3398 | return false; |
| 3399 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
| 3400 | return true; |
| 3401 | } |
| 3402 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3403 | return checkConst(SubobjType) && |
| 3404 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
| 3405 | Value) && |
| 3406 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
| 3407 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3408 | } |
| 3409 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 3410 | if (!checkConst(SubobjType)) |
| 3411 | return false; |
| 3412 | |
| 3413 | QualType PointeeType; |
| 3414 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 3415 | PointeeType = PT->getPointeeType(); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3416 | |
| 3417 | if (PointeeType.isNull() || !RHS.isInt() || |
| 3418 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3419 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3420 | return false; |
| 3421 | } |
| 3422 | |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 3423 | APSInt Offset = RHS.getInt(); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3424 | if (Opcode == BO_Sub) |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 3425 | negateAsSigned(Offset); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3426 | |
| 3427 | LValue LVal; |
| 3428 | LVal.setFrom(Info.Ctx, Subobj); |
| 3429 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
| 3430 | return false; |
| 3431 | LVal.moveInto(Subobj); |
| 3432 | return true; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3433 | } |
| 3434 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 3435 | llvm_unreachable("shouldn't encounter string elements here"); |
| 3436 | } |
| 3437 | }; |
| 3438 | } // end anonymous namespace |
| 3439 | |
| 3440 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
| 3441 | |
| 3442 | /// Perform a compound assignment of LVal <op>= RVal. |
| 3443 | static bool handleCompoundAssignment( |
| 3444 | EvalInfo &Info, const Expr *E, |
| 3445 | const LValue &LVal, QualType LValType, QualType PromotedLValType, |
| 3446 | BinaryOperatorKind Opcode, const APValue &RVal) { |
| 3447 | if (LVal.Designator.Invalid) |
| 3448 | return false; |
| 3449 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3450 | if (!Info.getLangOpts().CPlusPlus14) { |
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 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 3456 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
| 3457 | RVal }; |
| 3458 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 3459 | } |
| 3460 | |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3461 | namespace { |
| 3462 | struct IncDecSubobjectHandler { |
| 3463 | EvalInfo &Info; |
| 3464 | const UnaryOperator *E; |
| 3465 | AccessKinds AccessKind; |
| 3466 | APValue *Old; |
| 3467 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3468 | typedef bool result_type; |
| 3469 | |
| 3470 | bool checkConst(QualType QT) { |
| 3471 | // Assigning to a const object has undefined behavior. |
| 3472 | if (QT.isConstQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3473 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3474 | return false; |
| 3475 | } |
| 3476 | return true; |
| 3477 | } |
| 3478 | |
| 3479 | bool failed() { return false; } |
| 3480 | bool found(APValue &Subobj, QualType SubobjType) { |
| 3481 | // Stash the old value. Also clear Old, so we don't clobber it later |
| 3482 | // if we're post-incrementing a complex. |
| 3483 | if (Old) { |
| 3484 | *Old = Subobj; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3485 | Old = nullptr; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | switch (Subobj.getKind()) { |
| 3489 | case APValue::Int: |
| 3490 | return found(Subobj.getInt(), SubobjType); |
| 3491 | case APValue::Float: |
| 3492 | return found(Subobj.getFloat(), SubobjType); |
| 3493 | case APValue::ComplexInt: |
| 3494 | return found(Subobj.getComplexIntReal(), |
| 3495 | SubobjType->castAs<ComplexType>()->getElementType() |
| 3496 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 3497 | case APValue::ComplexFloat: |
| 3498 | return found(Subobj.getComplexFloatReal(), |
| 3499 | SubobjType->castAs<ComplexType>()->getElementType() |
| 3500 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 3501 | case APValue::LValue: |
| 3502 | return foundPointer(Subobj, SubobjType); |
| 3503 | default: |
| 3504 | // FIXME: can this happen? |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3505 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3506 | return false; |
| 3507 | } |
| 3508 | } |
| 3509 | bool found(APSInt &Value, QualType SubobjType) { |
| 3510 | if (!checkConst(SubobjType)) |
| 3511 | return false; |
| 3512 | |
| 3513 | if (!SubobjType->isIntegerType()) { |
| 3514 | // We don't support increment / decrement on integer-cast-to-pointer |
| 3515 | // values. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3516 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3517 | return false; |
| 3518 | } |
| 3519 | |
| 3520 | if (Old) *Old = APValue(Value); |
| 3521 | |
| 3522 | // bool arithmetic promotes to int, and the conversion back to bool |
| 3523 | // doesn't reduce mod 2^n, so special-case it. |
| 3524 | if (SubobjType->isBooleanType()) { |
| 3525 | if (AccessKind == AK_Increment) |
| 3526 | Value = 1; |
| 3527 | else |
| 3528 | Value = !Value; |
| 3529 | return true; |
| 3530 | } |
| 3531 | |
| 3532 | bool WasNegative = Value.isNegative(); |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3533 | if (AccessKind == AK_Increment) { |
| 3534 | ++Value; |
| 3535 | |
| 3536 | if (!WasNegative && Value.isNegative() && E->canOverflow()) { |
| 3537 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
| 3538 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
| 3539 | } |
| 3540 | } else { |
| 3541 | --Value; |
| 3542 | |
| 3543 | if (WasNegative && !Value.isNegative() && E->canOverflow()) { |
| 3544 | unsigned BitWidth = Value.getBitWidth(); |
| 3545 | APSInt ActualValue(Value.sext(BitWidth + 1), /*IsUnsigned*/false); |
| 3546 | ActualValue.setBit(BitWidth); |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 3547 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3548 | } |
| 3549 | } |
| 3550 | return true; |
| 3551 | } |
| 3552 | bool found(APFloat &Value, QualType SubobjType) { |
| 3553 | if (!checkConst(SubobjType)) |
| 3554 | return false; |
| 3555 | |
| 3556 | if (Old) *Old = APValue(Value); |
| 3557 | |
| 3558 | APFloat One(Value.getSemantics(), 1); |
| 3559 | if (AccessKind == AK_Increment) |
| 3560 | Value.add(One, APFloat::rmNearestTiesToEven); |
| 3561 | else |
| 3562 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
| 3563 | return true; |
| 3564 | } |
| 3565 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 3566 | if (!checkConst(SubobjType)) |
| 3567 | return false; |
| 3568 | |
| 3569 | QualType PointeeType; |
| 3570 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 3571 | PointeeType = PT->getPointeeType(); |
| 3572 | else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3573 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3574 | return false; |
| 3575 | } |
| 3576 | |
| 3577 | LValue LVal; |
| 3578 | LVal.setFrom(Info.Ctx, Subobj); |
| 3579 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
| 3580 | AccessKind == AK_Increment ? 1 : -1)) |
| 3581 | return false; |
| 3582 | LVal.moveInto(Subobj); |
| 3583 | return true; |
| 3584 | } |
| 3585 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 3586 | llvm_unreachable("shouldn't encounter string elements here"); |
| 3587 | } |
| 3588 | }; |
| 3589 | } // end anonymous namespace |
| 3590 | |
| 3591 | /// Perform an increment or decrement on LVal. |
| 3592 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
| 3593 | QualType LValType, bool IsIncrement, APValue *Old) { |
| 3594 | if (LVal.Designator.Invalid) |
| 3595 | return false; |
| 3596 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3597 | if (!Info.getLangOpts().CPlusPlus14) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3598 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3599 | return false; |
| 3600 | } |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 3601 | |
| 3602 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
| 3603 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
| 3604 | IncDecSubobjectHandler Handler = {Info, cast<UnaryOperator>(E), AK, Old}; |
| 3605 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 3606 | } |
| 3607 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3608 | /// Build an lvalue for the object argument of a member function call. |
| 3609 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 3610 | LValue &This) { |
| 3611 | if (Object->getType()->isPointerType()) |
| 3612 | return EvaluatePointer(Object, This, Info); |
| 3613 | |
| 3614 | if (Object->isGLValue()) |
| 3615 | return EvaluateLValue(Object, This, Info); |
| 3616 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3617 | if (Object->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3618 | return EvaluateTemporary(Object, This, Info); |
| 3619 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3620 | Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3621 | return false; |
| 3622 | } |
| 3623 | |
| 3624 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 3625 | /// lvalue referring to the result. |
| 3626 | /// |
| 3627 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3628 | /// \param LV - An lvalue referring to the base of the member pointer. |
| 3629 | /// \param RHS - The member pointer expression. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3630 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 3631 | /// the resulting LValue subobject designator. This is not possible when |
| 3632 | /// creating a bound member function. |
| 3633 | /// \return The field or method declaration to which the member pointer refers, |
| 3634 | /// or 0 if evaluation fails. |
| 3635 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3636 | QualType LVType, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3637 | LValue &LV, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3638 | const Expr *RHS, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3639 | bool IncludeMember = true) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3640 | MemberPtr MemPtr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3641 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3642 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3643 | |
| 3644 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 3645 | // member value, the behavior is undefined. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3646 | if (!MemPtr.getDecl()) { |
| 3647 | // FIXME: Specific diagnostic. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3648 | Info.FFDiag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3649 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3650 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3651 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3652 | if (MemPtr.isDerivedMember()) { |
| 3653 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3654 | // The end of the derived-to-base path for the base object must match the |
| 3655 | // derived-to-base path for the member pointer. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3656 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3657 | LV.Designator.Entries.size()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3658 | Info.FFDiag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3659 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3660 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3661 | unsigned PathLengthToMember = |
| 3662 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 3663 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 3664 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 3665 | LV.Designator.Entries[PathLengthToMember + I]); |
| 3666 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3667 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3668 | Info.FFDiag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3669 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3670 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3674 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3675 | PathLengthToMember)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3676 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3677 | } else if (!MemPtr.Path.empty()) { |
| 3678 | // Extend the LValue path with the member pointer's path. |
| 3679 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 3680 | MemPtr.Path.size() + IncludeMember); |
| 3681 | |
| 3682 | // Walk down to the appropriate base class. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3683 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 3684 | LVType = PT->getPointeeType(); |
| 3685 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 3686 | assert(RD && "member pointer access on non-class-type expression"); |
| 3687 | // The first class in the path is that of the lvalue. |
| 3688 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 3689 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3690 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3691 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3692 | RD = Base; |
| 3693 | } |
| 3694 | // Finally cast to the class containing the member. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3695 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
| 3696 | MemPtr.getContainingRecord())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3697 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | // Add the member. Note that we cannot build bound member functions here. |
| 3701 | if (IncludeMember) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3702 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3703 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3704 | return nullptr; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3705 | } else if (const IndirectFieldDecl *IFD = |
| 3706 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3707 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3708 | return nullptr; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3709 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3710 | llvm_unreachable("can't construct reference to bound member function"); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3711 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | return MemPtr.getDecl(); |
| 3715 | } |
| 3716 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3717 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 3718 | const BinaryOperator *BO, |
| 3719 | LValue &LV, |
| 3720 | bool IncludeMember = true) { |
| 3721 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 3722 | |
| 3723 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 3724 | if (Info.noteFailure()) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3725 | MemberPtr MemPtr; |
| 3726 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
| 3727 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3728 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
| 3732 | BO->getRHS(), IncludeMember); |
| 3733 | } |
| 3734 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3735 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 3736 | /// the provided lvalue, which currently refers to the base object. |
| 3737 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 3738 | LValue &Result) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3739 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3740 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3741 | return false; |
| 3742 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3743 | QualType TargetQT = E->getType(); |
| 3744 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 3745 | TargetQT = PT->getPointeeType(); |
| 3746 | |
| 3747 | // Check this cast lands within the final derived-to-base subobject path. |
| 3748 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3749 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3750 | << D.MostDerivedType << TargetQT; |
| 3751 | return false; |
| 3752 | } |
| 3753 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3754 | // Check the type of the final cast. We don't need to check the path, |
| 3755 | // since a cast can only be formed if the path is unique. |
| 3756 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3757 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 3758 | const CXXRecordDecl *FinalType; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3759 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 3760 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 3761 | else |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3762 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3763 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3764 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3765 | << D.MostDerivedType << TargetQT; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3766 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3767 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3768 | |
| 3769 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3770 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 3773 | namespace { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3774 | enum EvalStmtResult { |
| 3775 | /// Evaluation failed. |
| 3776 | ESR_Failed, |
| 3777 | /// Hit a 'return' statement. |
| 3778 | ESR_Returned, |
| 3779 | /// Evaluation succeeded. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3780 | ESR_Succeeded, |
| 3781 | /// Hit a 'continue' statement. |
| 3782 | ESR_Continue, |
| 3783 | /// Hit a 'break' statement. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3784 | ESR_Break, |
| 3785 | /// Still scanning for 'case' or 'default' statement. |
| 3786 | ESR_CaseNotFound |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3787 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3788 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3789 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3790 | static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) { |
| 3791 | // We don't need to evaluate the initializer for a static local. |
| 3792 | if (!VD->hasLocalStorage()) |
| 3793 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3794 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3795 | LValue Result; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3796 | APValue &Val = createTemporary(VD, true, Result, *Info.CurrentCall); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3797 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3798 | const Expr *InitE = VD->getInit(); |
| 3799 | if (!InitE) { |
| 3800 | Info.FFDiag(VD->getLocStart(), diag::note_constexpr_uninitialized) |
| 3801 | << false << VD->getType(); |
| 3802 | Val = APValue(); |
| 3803 | return false; |
| 3804 | } |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3805 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3806 | if (InitE->isValueDependent()) |
| 3807 | return false; |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 3808 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3809 | if (!EvaluateInPlace(Val, Info, Result, InitE)) { |
| 3810 | // Wipe out any partially-computed value, to allow tracking that this |
| 3811 | // evaluation failed. |
| 3812 | Val = APValue(); |
| 3813 | return false; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | return true; |
| 3817 | } |
| 3818 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3819 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
| 3820 | bool OK = true; |
| 3821 | |
| 3822 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 3823 | OK &= EvaluateVarDecl(Info, VD); |
| 3824 | |
| 3825 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(D)) |
| 3826 | for (auto *BD : DD->bindings()) |
| 3827 | if (auto *VD = BD->getHoldingVar()) |
| 3828 | OK &= EvaluateDecl(Info, VD); |
| 3829 | |
| 3830 | return OK; |
| 3831 | } |
| 3832 | |
| 3833 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3834 | /// Evaluate a condition (either a variable declaration or an expression). |
| 3835 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
| 3836 | const Expr *Cond, bool &Result) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3837 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3838 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
| 3839 | return false; |
| 3840 | return EvaluateAsBooleanCondition(Cond, Result, Info); |
| 3841 | } |
| 3842 | |
Richard Smith | 8921007 | 2016-04-04 23:29:43 +0000 | [diff] [blame] | 3843 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3844 | /// A location where the result (returned value) of evaluating a |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3845 | /// statement should be stored. |
| 3846 | struct StmtResult { |
| 3847 | /// The APValue that should be filled in with the returned value. |
| 3848 | APValue &Value; |
| 3849 | /// The location containing the result, if any (used to support RVO). |
| 3850 | const LValue *Slot; |
| 3851 | }; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3852 | |
| 3853 | struct TempVersionRAII { |
| 3854 | CallStackFrame &Frame; |
| 3855 | |
| 3856 | TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) { |
| 3857 | Frame.pushTempVersion(); |
| 3858 | } |
| 3859 | |
| 3860 | ~TempVersionRAII() { |
| 3861 | Frame.popTempVersion(); |
| 3862 | } |
| 3863 | }; |
| 3864 | |
Richard Smith | 8921007 | 2016-04-04 23:29:43 +0000 | [diff] [blame] | 3865 | } |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3866 | |
| 3867 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3868 | const Stmt *S, |
| 3869 | const SwitchCase *SC = nullptr); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3870 | |
| 3871 | /// Evaluate the body of a loop, and translate the result as appropriate. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3872 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3873 | const Stmt *Body, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3874 | const SwitchCase *Case = nullptr) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3875 | BlockScopeRAII Scope(Info); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3876 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case)) { |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3877 | case ESR_Break: |
| 3878 | return ESR_Succeeded; |
| 3879 | case ESR_Succeeded: |
| 3880 | case ESR_Continue: |
| 3881 | return ESR_Continue; |
| 3882 | case ESR_Failed: |
| 3883 | case ESR_Returned: |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3884 | case ESR_CaseNotFound: |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3885 | return ESR; |
| 3886 | } |
Hans Wennborg | 9242bd1 | 2013-05-06 15:13:34 +0000 | [diff] [blame] | 3887 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3890 | /// Evaluate a switch statement. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3891 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3892 | const SwitchStmt *SS) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3893 | BlockScopeRAII Scope(Info); |
| 3894 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3895 | // Evaluate the switch condition. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3896 | APSInt Value; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3897 | { |
| 3898 | FullExpressionRAII Scope(Info); |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3899 | if (const Stmt *Init = SS->getInit()) { |
| 3900 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
| 3901 | if (ESR != ESR_Succeeded) |
| 3902 | return ESR; |
| 3903 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3904 | if (SS->getConditionVariable() && |
| 3905 | !EvaluateDecl(Info, SS->getConditionVariable())) |
| 3906 | return ESR_Failed; |
| 3907 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
| 3908 | return ESR_Failed; |
| 3909 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3910 | |
| 3911 | // Find the switch case corresponding to the value of the condition. |
| 3912 | // FIXME: Cache this lookup. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3913 | const SwitchCase *Found = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3914 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 3915 | SC = SC->getNextSwitchCase()) { |
| 3916 | if (isa<DefaultStmt>(SC)) { |
| 3917 | Found = SC; |
| 3918 | continue; |
| 3919 | } |
| 3920 | |
| 3921 | const CaseStmt *CS = cast<CaseStmt>(SC); |
| 3922 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
| 3923 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
| 3924 | : LHS; |
| 3925 | if (LHS <= Value && Value <= RHS) { |
| 3926 | Found = SC; |
| 3927 | break; |
| 3928 | } |
| 3929 | } |
| 3930 | |
| 3931 | if (!Found) |
| 3932 | return ESR_Succeeded; |
| 3933 | |
| 3934 | // Search the switch body for the switch case and evaluate it from there. |
| 3935 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found)) { |
| 3936 | case ESR_Break: |
| 3937 | return ESR_Succeeded; |
| 3938 | case ESR_Succeeded: |
| 3939 | case ESR_Continue: |
| 3940 | case ESR_Failed: |
| 3941 | case ESR_Returned: |
| 3942 | return ESR; |
| 3943 | case ESR_CaseNotFound: |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3944 | // This can only happen if the switch case is nested within a statement |
| 3945 | // expression. We have no intention of supporting that. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3946 | Info.FFDiag(Found->getLocStart(), diag::note_constexpr_stmt_expr_unsupported); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3947 | return ESR_Failed; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3948 | } |
Richard Smith | f8cf9d4 | 2013-05-13 20:33:30 +0000 | [diff] [blame] | 3949 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3952 | // Evaluate a statement. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3953 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3954 | const Stmt *S, const SwitchCase *Case) { |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 3955 | if (!Info.nextStep(S)) |
| 3956 | return ESR_Failed; |
| 3957 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3958 | // If we're hunting down a 'case' or 'default' label, recurse through |
| 3959 | // substatements until we hit the label. |
| 3960 | if (Case) { |
| 3961 | // FIXME: We don't start the lifetime of objects whose initialization we |
| 3962 | // jump over. However, such objects must be of class type with a trivial |
| 3963 | // default constructor that initialize all subobjects, so must be empty, |
| 3964 | // so this almost never matters. |
| 3965 | switch (S->getStmtClass()) { |
| 3966 | case Stmt::CompoundStmtClass: |
| 3967 | // FIXME: Precompute which substatement of a compound statement we |
| 3968 | // would jump to, and go straight there rather than performing a |
| 3969 | // linear scan each time. |
| 3970 | case Stmt::LabelStmtClass: |
| 3971 | case Stmt::AttributedStmtClass: |
| 3972 | case Stmt::DoStmtClass: |
| 3973 | break; |
| 3974 | |
| 3975 | case Stmt::CaseStmtClass: |
| 3976 | case Stmt::DefaultStmtClass: |
| 3977 | if (Case == S) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3978 | Case = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3979 | break; |
| 3980 | |
| 3981 | case Stmt::IfStmtClass: { |
| 3982 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
| 3983 | // straight there rather than scanning both sides. |
| 3984 | const IfStmt *IS = cast<IfStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3985 | |
| 3986 | // Wrap the evaluation in a block scope, in case it's a DeclStmt |
| 3987 | // preceded by our switch label. |
| 3988 | BlockScopeRAII Scope(Info); |
| 3989 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3990 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
| 3991 | if (ESR != ESR_CaseNotFound || !IS->getElse()) |
| 3992 | return ESR; |
| 3993 | return EvaluateStmt(Result, Info, IS->getElse(), Case); |
| 3994 | } |
| 3995 | |
| 3996 | case Stmt::WhileStmtClass: { |
| 3997 | EvalStmtResult ESR = |
| 3998 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
| 3999 | if (ESR != ESR_Continue) |
| 4000 | return ESR; |
| 4001 | break; |
| 4002 | } |
| 4003 | |
| 4004 | case Stmt::ForStmtClass: { |
| 4005 | const ForStmt *FS = cast<ForStmt>(S); |
| 4006 | EvalStmtResult ESR = |
| 4007 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
| 4008 | if (ESR != ESR_Continue) |
| 4009 | return ESR; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4010 | if (FS->getInc()) { |
| 4011 | FullExpressionRAII IncScope(Info); |
| 4012 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 4013 | return ESR_Failed; |
| 4014 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4015 | break; |
| 4016 | } |
| 4017 | |
| 4018 | case Stmt::DeclStmtClass: |
| 4019 | // FIXME: If the variable has initialization that can't be jumped over, |
| 4020 | // bail out of any immediately-surrounding compound-statement too. |
| 4021 | default: |
| 4022 | return ESR_CaseNotFound; |
| 4023 | } |
| 4024 | } |
| 4025 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4026 | switch (S->getStmtClass()) { |
| 4027 | default: |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4028 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4029 | // Don't bother evaluating beyond an expression-statement which couldn't |
| 4030 | // be evaluated. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4031 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4032 | if (!EvaluateIgnoredValue(Info, E)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4033 | return ESR_Failed; |
| 4034 | return ESR_Succeeded; |
| 4035 | } |
| 4036 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4037 | Info.FFDiag(S->getLocStart()); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4038 | return ESR_Failed; |
| 4039 | |
| 4040 | case Stmt::NullStmtClass: |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4041 | return ESR_Succeeded; |
| 4042 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4043 | case Stmt::DeclStmtClass: { |
| 4044 | const DeclStmt *DS = cast<DeclStmt>(S); |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 4045 | for (const auto *DclIt : DS->decls()) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4046 | // Each declaration initialization is its own full-expression. |
| 4047 | // FIXME: This isn't quite right; if we're performing aggregate |
| 4048 | // initialization, each braced subexpression is its own full-expression. |
| 4049 | FullExpressionRAII Scope(Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 4050 | if (!EvaluateDecl(Info, DclIt) && !Info.noteFailure()) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4051 | return ESR_Failed; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4052 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4053 | return ESR_Succeeded; |
| 4054 | } |
| 4055 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4056 | case Stmt::ReturnStmtClass: { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4057 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4058 | FullExpressionRAII Scope(Info); |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4059 | if (RetExpr && |
| 4060 | !(Result.Slot |
| 4061 | ? EvaluateInPlace(Result.Value, Info, *Result.Slot, RetExpr) |
| 4062 | : Evaluate(Result.Value, Info, RetExpr))) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4063 | return ESR_Failed; |
| 4064 | return ESR_Returned; |
| 4065 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4066 | |
| 4067 | case Stmt::CompoundStmtClass: { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4068 | BlockScopeRAII Scope(Info); |
| 4069 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4070 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 4071 | for (const auto *BI : CS->body()) { |
| 4072 | EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4073 | if (ESR == ESR_Succeeded) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4074 | Case = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4075 | else if (ESR != ESR_CaseNotFound) |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4076 | return ESR; |
| 4077 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4078 | return Case ? ESR_CaseNotFound : ESR_Succeeded; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4079 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4080 | |
| 4081 | case Stmt::IfStmtClass: { |
| 4082 | const IfStmt *IS = cast<IfStmt>(S); |
| 4083 | |
| 4084 | // Evaluate the condition, as either a var decl or as an expression. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4085 | BlockScopeRAII Scope(Info); |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 4086 | if (const Stmt *Init = IS->getInit()) { |
| 4087 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
| 4088 | if (ESR != ESR_Succeeded) |
| 4089 | return ESR; |
| 4090 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4091 | bool Cond; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4092 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4093 | return ESR_Failed; |
| 4094 | |
| 4095 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
| 4096 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
| 4097 | if (ESR != ESR_Succeeded) |
| 4098 | return ESR; |
| 4099 | } |
| 4100 | return ESR_Succeeded; |
| 4101 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4102 | |
| 4103 | case Stmt::WhileStmtClass: { |
| 4104 | const WhileStmt *WS = cast<WhileStmt>(S); |
| 4105 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4106 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4107 | bool Continue; |
| 4108 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
| 4109 | Continue)) |
| 4110 | return ESR_Failed; |
| 4111 | if (!Continue) |
| 4112 | break; |
| 4113 | |
| 4114 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
| 4115 | if (ESR != ESR_Continue) |
| 4116 | return ESR; |
| 4117 | } |
| 4118 | return ESR_Succeeded; |
| 4119 | } |
| 4120 | |
| 4121 | case Stmt::DoStmtClass: { |
| 4122 | const DoStmt *DS = cast<DoStmt>(S); |
| 4123 | bool Continue; |
| 4124 | do { |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4125 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4126 | if (ESR != ESR_Continue) |
| 4127 | return ESR; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4128 | Case = nullptr; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4129 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4130 | FullExpressionRAII CondScope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4131 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info)) |
| 4132 | return ESR_Failed; |
| 4133 | } while (Continue); |
| 4134 | return ESR_Succeeded; |
| 4135 | } |
| 4136 | |
| 4137 | case Stmt::ForStmtClass: { |
| 4138 | const ForStmt *FS = cast<ForStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4139 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4140 | if (FS->getInit()) { |
| 4141 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| 4142 | if (ESR != ESR_Succeeded) |
| 4143 | return ESR; |
| 4144 | } |
| 4145 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4146 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4147 | bool Continue = true; |
| 4148 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
| 4149 | FS->getCond(), Continue)) |
| 4150 | return ESR_Failed; |
| 4151 | if (!Continue) |
| 4152 | break; |
| 4153 | |
| 4154 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 4155 | if (ESR != ESR_Continue) |
| 4156 | return ESR; |
| 4157 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4158 | if (FS->getInc()) { |
| 4159 | FullExpressionRAII IncScope(Info); |
| 4160 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 4161 | return ESR_Failed; |
| 4162 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4163 | } |
| 4164 | return ESR_Succeeded; |
| 4165 | } |
| 4166 | |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4167 | case Stmt::CXXForRangeStmtClass: { |
| 4168 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4169 | BlockScopeRAII Scope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4170 | |
| 4171 | // Initialize the __range variable. |
| 4172 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
| 4173 | if (ESR != ESR_Succeeded) |
| 4174 | return ESR; |
| 4175 | |
| 4176 | // Create the __begin and __end iterators. |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 4177 | ESR = EvaluateStmt(Result, Info, FS->getBeginStmt()); |
| 4178 | if (ESR != ESR_Succeeded) |
| 4179 | return ESR; |
| 4180 | ESR = EvaluateStmt(Result, Info, FS->getEndStmt()); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4181 | if (ESR != ESR_Succeeded) |
| 4182 | return ESR; |
| 4183 | |
| 4184 | while (true) { |
| 4185 | // Condition: __begin != __end. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4186 | { |
| 4187 | bool Continue = true; |
| 4188 | FullExpressionRAII CondExpr(Info); |
| 4189 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
| 4190 | return ESR_Failed; |
| 4191 | if (!Continue) |
| 4192 | break; |
| 4193 | } |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4194 | |
| 4195 | // User's variable declaration, initialized by *__begin. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4196 | BlockScopeRAII InnerScope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4197 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
| 4198 | if (ESR != ESR_Succeeded) |
| 4199 | return ESR; |
| 4200 | |
| 4201 | // Loop body. |
| 4202 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 4203 | if (ESR != ESR_Continue) |
| 4204 | return ESR; |
| 4205 | |
| 4206 | // Increment: ++__begin |
| 4207 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 4208 | return ESR_Failed; |
| 4209 | } |
| 4210 | |
| 4211 | return ESR_Succeeded; |
| 4212 | } |
| 4213 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4214 | case Stmt::SwitchStmtClass: |
| 4215 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
| 4216 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4217 | case Stmt::ContinueStmtClass: |
| 4218 | return ESR_Continue; |
| 4219 | |
| 4220 | case Stmt::BreakStmtClass: |
| 4221 | return ESR_Break; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4222 | |
| 4223 | case Stmt::LabelStmtClass: |
| 4224 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
| 4225 | |
| 4226 | case Stmt::AttributedStmtClass: |
| 4227 | // As a general principle, C++11 attributes can be ignored without |
| 4228 | // any semantic impact. |
| 4229 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
| 4230 | Case); |
| 4231 | |
| 4232 | case Stmt::CaseStmtClass: |
| 4233 | case Stmt::DefaultStmtClass: |
| 4234 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4235 | } |
| 4236 | } |
| 4237 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4238 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 4239 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 4240 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 4241 | /// so we need special handling. |
| 4242 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4243 | const CXXConstructorDecl *CD, |
| 4244 | bool IsValueInitialization) { |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4245 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 4246 | return false; |
| 4247 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 4248 | // Value-initialization does not call a trivial default constructor, so such a |
| 4249 | // call is a core constant expression whether or not the constructor is |
| 4250 | // constexpr. |
| 4251 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4252 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 4253 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 4254 | // we should be much more explicit about why it's not constexpr. |
| 4255 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 4256 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 4257 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4258 | } else { |
| 4259 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 4260 | } |
| 4261 | } |
| 4262 | return true; |
| 4263 | } |
| 4264 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4265 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 4266 | /// expression. |
| 4267 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 4268 | const FunctionDecl *Declaration, |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 4269 | const FunctionDecl *Definition, |
| 4270 | const Stmt *Body) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4271 | // Potential constant expressions can contain calls to declared, but not yet |
| 4272 | // defined, constexpr functions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4273 | if (Info.checkingPotentialConstantExpression() && !Definition && |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4274 | Declaration->isConstexpr()) |
| 4275 | return false; |
| 4276 | |
Richard Smith | 0838f3a | 2013-05-14 05:18:44 +0000 | [diff] [blame] | 4277 | // Bail out with no diagnostic if the function declaration itself is invalid. |
| 4278 | // We will have produced a relevant diagnostic while parsing it. |
| 4279 | if (Declaration->isInvalidDecl()) |
| 4280 | return false; |
| 4281 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4282 | // Can we evaluate this function call? |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 4283 | if (Definition && Definition->isConstexpr() && |
| 4284 | !Definition->isInvalidDecl() && Body) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4285 | return true; |
| 4286 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4287 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4288 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 4289 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4290 | // If this function is not constexpr because it is an inherited |
| 4291 | // non-constexpr constructor, diagnose that directly. |
| 4292 | auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl); |
| 4293 | if (CD && CD->isInheritingConstructor()) { |
| 4294 | auto *Inherited = CD->getInheritedConstructor().getConstructor(); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 4295 | if (!Inherited->isConstexpr()) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4296 | DiagDecl = CD = Inherited; |
| 4297 | } |
| 4298 | |
| 4299 | // FIXME: If DiagDecl is an implicitly-declared special member function |
| 4300 | // or an inheriting constructor, we should be much more explicit about why |
| 4301 | // it's not constexpr. |
| 4302 | if (CD && CD->isInheritingConstructor()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4303 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4304 | << CD->getInheritedConstructor().getConstructor()->getParent(); |
| 4305 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4306 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4307 | << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4308 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 4309 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4310 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4311 | } |
| 4312 | return false; |
| 4313 | } |
| 4314 | |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 4315 | /// Determine if a class has any fields that might need to be copied by a |
| 4316 | /// trivial copy or move operation. |
| 4317 | static bool hasFields(const CXXRecordDecl *RD) { |
| 4318 | if (!RD || RD->isEmpty()) |
| 4319 | return false; |
| 4320 | for (auto *FD : RD->fields()) { |
| 4321 | if (FD->isUnnamedBitfield()) |
| 4322 | continue; |
| 4323 | return true; |
| 4324 | } |
| 4325 | for (auto &Base : RD->bases()) |
| 4326 | if (hasFields(Base.getType()->getAsCXXRecordDecl())) |
| 4327 | return true; |
| 4328 | return false; |
| 4329 | } |
| 4330 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4331 | namespace { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4332 | typedef SmallVector<APValue, 8> ArgVector; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 4336 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 4337 | EvalInfo &Info) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4338 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4339 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4340 | I != E; ++I) { |
| 4341 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 4342 | // If we're checking for a potential constant expression, evaluate all |
| 4343 | // initializers even if some of them fail. |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 4344 | if (!Info.noteFailure()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4345 | return false; |
| 4346 | Success = false; |
| 4347 | } |
| 4348 | } |
| 4349 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4352 | /// Evaluate a function call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4353 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 4354 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4355 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4356 | EvalInfo &Info, APValue &Result, |
| 4357 | const LValue *ResultSlot) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4358 | ArgVector ArgValues(Args.size()); |
| 4359 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 4360 | return false; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4361 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4362 | if (!Info.CheckCallLimit(CallLoc)) |
| 4363 | return false; |
| 4364 | |
| 4365 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4366 | |
| 4367 | // For a trivial copy or move assignment, perform an APValue copy. This is |
| 4368 | // essential for unions, where the operations performed by the assignment |
| 4369 | // operator cannot be represented as statements. |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 4370 | // |
| 4371 | // Skip this for non-union classes with no fields; in that case, the defaulted |
| 4372 | // copy/move does not actually read the object. |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4373 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 4374 | if (MD && MD->isDefaulted() && |
| 4375 | (MD->getParent()->isUnion() || |
| 4376 | (MD->isTrivial() && hasFields(MD->getParent())))) { |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4377 | assert(This && |
| 4378 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
| 4379 | LValue RHS; |
| 4380 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 4381 | APValue RHSValue; |
| 4382 | if (!handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 4383 | RHS, RHSValue)) |
| 4384 | return false; |
| 4385 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(Info.Ctx), |
| 4386 | RHSValue)) |
| 4387 | return false; |
| 4388 | This->moveInto(Result); |
| 4389 | return true; |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 4390 | } else if (MD && isLambdaCallOperator(MD)) { |
Erik Pilkington | 1123291 | 2018-04-05 00:12:05 +0000 | [diff] [blame] | 4391 | // We're in a lambda; determine the lambda capture field maps unless we're |
| 4392 | // just constexpr checking a lambda's call operator. constexpr checking is |
| 4393 | // done before the captures have been added to the closure object (unless |
| 4394 | // we're inferring constexpr-ness), so we don't have access to them in this |
| 4395 | // case. But since we don't need the captures to constexpr check, we can |
| 4396 | // just ignore them. |
| 4397 | if (!Info.checkingPotentialConstantExpression()) |
| 4398 | MD->getParent()->getCaptureFields(Frame.LambdaCaptureFields, |
| 4399 | Frame.LambdaThisCaptureField); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4402 | StmtResult Ret = {Result, ResultSlot}; |
| 4403 | EvalStmtResult ESR = EvaluateStmt(Ret, Info, Body); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 4404 | if (ESR == ESR_Succeeded) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4405 | if (Callee->getReturnType()->isVoidType()) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 4406 | return true; |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4407 | Info.FFDiag(Callee->getLocEnd(), diag::note_constexpr_no_return); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 4408 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4409 | return ESR == ESR_Returned; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4412 | /// Evaluate a constructor call. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4413 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
| 4414 | APValue *ArgValues, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4415 | const CXXConstructorDecl *Definition, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4416 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4417 | SourceLocation CallLoc = E->getExprLoc(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4418 | if (!Info.CheckCallLimit(CallLoc)) |
| 4419 | return false; |
| 4420 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4421 | const CXXRecordDecl *RD = Definition->getParent(); |
| 4422 | if (RD->getNumVBases()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4423 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4424 | return false; |
| 4425 | } |
| 4426 | |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 4427 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4428 | Info, {This.getLValueBase(), |
| 4429 | {This.getLValueCallIndex(), This.getLValueVersion()}}); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4430 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4431 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4432 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
| 4433 | // wasteful. |
| 4434 | APValue RetVal; |
| 4435 | StmtResult Ret = {RetVal, nullptr}; |
| 4436 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4437 | // If it's a delegating constructor, delegate. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4438 | if (Definition->isDelegatingConstructor()) { |
| 4439 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | 9ff62af | 2013-11-07 18:45:03 +0000 | [diff] [blame] | 4440 | { |
| 4441 | FullExpressionRAII InitScope(Info); |
| 4442 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit())) |
| 4443 | return false; |
| 4444 | } |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4445 | return EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4448 | // 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] | 4449 | // essential for unions (or classes with anonymous union members), where the |
| 4450 | // operations performed by the constructor cannot be represented by |
| 4451 | // ctor-initializers. |
| 4452 | // |
| 4453 | // Skip this for empty non-union classes; we should not perform an |
| 4454 | // lvalue-to-rvalue conversion on them because their copy constructor does not |
| 4455 | // actually read them. |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 4456 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 4457 | (Definition->getParent()->isUnion() || |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 4458 | (Definition->isTrivial() && hasFields(Definition->getParent())))) { |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4459 | LValue RHS; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4460 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4461 | return handleLValueToRValueConversion( |
| 4462 | Info, E, Definition->getParamDecl(0)->getType().getNonReferenceType(), |
| 4463 | RHS, Result); |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | // Reserve space for the struct members. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4467 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4468 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 4469 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4470 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4471 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4472 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 4473 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4474 | // A scope for temporaries lifetime-extended by reference members. |
| 4475 | BlockScopeRAII LifetimeExtendedScope(Info); |
| 4476 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4477 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4478 | unsigned BasesSeen = 0; |
| 4479 | #ifndef NDEBUG |
| 4480 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 4481 | #endif |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4482 | for (const auto *I : Definition->inits()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4483 | LValue Subobject = This; |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4484 | LValue SubobjectParent = This; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4485 | APValue *Value = &Result; |
| 4486 | |
| 4487 | // Determine the subobject to initialize. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4488 | FieldDecl *FD = nullptr; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4489 | if (I->isBaseInitializer()) { |
| 4490 | QualType BaseType(I->getBaseClass(), 0); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4491 | #ifndef NDEBUG |
| 4492 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4493 | // definition. We have already checked for virtual base classes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4494 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 4495 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 4496 | "base class initializers not in expected order"); |
| 4497 | ++BaseIt; |
| 4498 | #endif |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4499 | if (!HandleLValueDirectBase(Info, I->getInit(), Subobject, RD, |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4500 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 4501 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4502 | Value = &Result.getStructBase(BasesSeen++); |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4503 | } else if ((FD = I->getMember())) { |
| 4504 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4505 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4506 | if (RD->isUnion()) { |
| 4507 | Result = APValue(FD); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4508 | Value = &Result.getUnionValue(); |
| 4509 | } else { |
| 4510 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 4511 | } |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4512 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4513 | // Walk the indirect field decl's chain to find the object to initialize, |
| 4514 | // and make sure we've initialized every step along it. |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4515 | auto IndirectFieldChain = IFD->chain(); |
| 4516 | for (auto *C : IndirectFieldChain) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 4517 | FD = cast<FieldDecl>(C); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4518 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 4519 | // Switch the union field if it differs. This happens if we had |
| 4520 | // preceding zero-initialization, and we're now initializing a union |
| 4521 | // subobject other than the first. |
| 4522 | // FIXME: In this case, the values of the other subobjects are |
| 4523 | // specified, since zero-initialization sets all padding bits to zero. |
| 4524 | if (Value->isUninit() || |
| 4525 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 4526 | if (CD->isUnion()) |
| 4527 | *Value = APValue(FD); |
| 4528 | else |
| 4529 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 4530 | std::distance(CD->field_begin(), CD->field_end())); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4531 | } |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4532 | // Store Subobject as its parent before updating it for the last element |
| 4533 | // in the chain. |
| 4534 | if (C == IndirectFieldChain.back()) |
| 4535 | SubobjectParent = Subobject; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4536 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4537 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4538 | if (CD->isUnion()) |
| 4539 | Value = &Value->getUnionValue(); |
| 4540 | else |
| 4541 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4542 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4543 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4544 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4545 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4546 | |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4547 | // Need to override This for implicit field initializers as in this case |
| 4548 | // This refers to innermost anonymous struct/union containing initializer, |
| 4549 | // not to currently constructed class. |
| 4550 | const Expr *Init = I->getInit(); |
| 4551 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent, |
| 4552 | isa<CXXDefaultInitExpr>(Init)); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4553 | FullExpressionRAII InitScope(Info); |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4554 | if (!EvaluateInPlace(*Value, Info, Subobject, Init) || |
| 4555 | (FD && FD->isBitField() && |
| 4556 | !truncateBitfieldValue(Info, Init, *Value, FD))) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4557 | // If we're checking for a potential constant expression, evaluate all |
| 4558 | // initializers even if some of them fail. |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 4559 | if (!Info.noteFailure()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4560 | return false; |
| 4561 | Success = false; |
| 4562 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4565 | return Success && |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4566 | EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4569 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
| 4570 | ArrayRef<const Expr*> Args, |
| 4571 | const CXXConstructorDecl *Definition, |
| 4572 | EvalInfo &Info, APValue &Result) { |
| 4573 | ArgVector ArgValues(Args.size()); |
| 4574 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 4575 | return false; |
| 4576 | |
| 4577 | return HandleConstructorCall(E, This, ArgValues.data(), Definition, |
| 4578 | Info, Result); |
| 4579 | } |
| 4580 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4581 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4582 | // Generic Evaluation |
| 4583 | //===----------------------------------------------------------------------===// |
| 4584 | namespace { |
| 4585 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4586 | template <class Derived> |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4587 | class ExprEvaluatorBase |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4588 | : public ConstStmtVisitor<Derived, bool> { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4589 | private: |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4590 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4591 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4592 | return getDerived().Success(V, E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4593 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4594 | bool DerivedZeroInitialization(const Expr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4595 | return getDerived().ZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4596 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4597 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4598 | // Check whether a conditional operator with a non-constant condition is a |
| 4599 | // potential constant expression. If neither arm is a potential constant |
| 4600 | // expression, then the conditional operator is not either. |
| 4601 | template<typename ConditionalOperator> |
| 4602 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4603 | assert(Info.checkingPotentialConstantExpression()); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4604 | |
| 4605 | // Speculatively evaluate both arms. |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 4606 | SmallVector<PartialDiagnosticAt, 8> Diag; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4607 | { |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4608 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4609 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 4610 | if (Diag.empty()) |
| 4611 | return; |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 4612 | } |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4613 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 4614 | { |
| 4615 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4616 | Diag.clear(); |
| 4617 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 4618 | if (Diag.empty()) |
| 4619 | return; |
| 4620 | } |
| 4621 | |
| 4622 | Error(E, diag::note_constexpr_conditional_never_const); |
| 4623 | } |
| 4624 | |
| 4625 | |
| 4626 | template<typename ConditionalOperator> |
| 4627 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 4628 | bool BoolResult; |
| 4629 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
Nick Lewycky | 20edee6 | 2017-04-27 07:11:09 +0000 | [diff] [blame] | 4630 | if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) { |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4631 | CheckPotentialConstantConditional(E); |
Nick Lewycky | 20edee6 | 2017-04-27 07:11:09 +0000 | [diff] [blame] | 4632 | return false; |
| 4633 | } |
| 4634 | if (Info.noteFailure()) { |
| 4635 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 4636 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 4637 | } |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4638 | return false; |
| 4639 | } |
| 4640 | |
| 4641 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 4642 | return StmtVisitorTy::Visit(EvalExpr); |
| 4643 | } |
| 4644 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4645 | protected: |
| 4646 | EvalInfo &Info; |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4647 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4648 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 4649 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 4650 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4651 | return Info.CCEDiag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4654 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4655 | |
| 4656 | public: |
| 4657 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 4658 | |
| 4659 | EvalInfo &getEvalInfo() { return Info; } |
| 4660 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4661 | /// Report an evaluation error. This should only be called when an error is |
| 4662 | /// first discovered. When propagating an error, just return false. |
| 4663 | bool Error(const Expr *E, diag::kind D) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4664 | Info.FFDiag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4665 | return false; |
| 4666 | } |
| 4667 | bool Error(const Expr *E) { |
| 4668 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 4669 | } |
| 4670 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4671 | bool VisitStmt(const Stmt *) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4672 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4673 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4674 | bool VisitExpr(const Expr *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4675 | return Error(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4678 | bool VisitParenExpr(const ParenExpr *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4679 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4680 | bool VisitUnaryExtension(const UnaryOperator *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4681 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4682 | bool VisitUnaryPlus(const UnaryOperator *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4683 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4684 | bool VisitChooseExpr(const ChooseExpr *E) |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 4685 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4686 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4687 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4688 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4689 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4690 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
| 4691 | TempVersionRAII RAII(*Info.CurrentCall); |
| 4692 | return StmtVisitorTy::Visit(E->getExpr()); |
| 4693 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4694 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4695 | TempVersionRAII RAII(*Info.CurrentCall); |
Richard Smith | 17e3246 | 2013-09-13 20:51:45 +0000 | [diff] [blame] | 4696 | // The initializer may not have been parsed yet, or might be erroneous. |
| 4697 | if (!E->getExpr()) |
| 4698 | return Error(E); |
| 4699 | return StmtVisitorTy::Visit(E->getExpr()); |
| 4700 | } |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 4701 | // We cannot create any objects for which cleanups are required, so there is |
| 4702 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4703 | bool VisitExprWithCleanups(const ExprWithCleanups *E) |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 4704 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4705 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4706 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4707 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 4708 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 4709 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4710 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4711 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 4712 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 4713 | } |
| 4714 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4715 | bool VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4716 | switch (E->getOpcode()) { |
| 4717 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4718 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4719 | |
| 4720 | case BO_Comma: |
| 4721 | VisitIgnoredValue(E->getLHS()); |
| 4722 | return StmtVisitorTy::Visit(E->getRHS()); |
| 4723 | |
| 4724 | case BO_PtrMemD: |
| 4725 | case BO_PtrMemI: { |
| 4726 | LValue Obj; |
| 4727 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 4728 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4729 | APValue Result; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4730 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4731 | return false; |
| 4732 | return DerivedSuccess(Result, E); |
| 4733 | } |
| 4734 | } |
| 4735 | } |
| 4736 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4737 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 4738 | // Evaluate and cache the common expression. We treat it as a temporary, |
| 4739 | // even though it's not quite the same thing. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4740 | if (!Evaluate(Info.CurrentCall->createTemporary(E->getOpaqueValue(), false), |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 4741 | Info, E->getCommon())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4742 | return false; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4743 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4744 | return HandleConditionalOperator(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4747 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4748 | bool IsBcpCall = false; |
| 4749 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 4750 | // the result is a constant expression if it can be folded without |
| 4751 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 4752 | // for discussion. |
| 4753 | if (const CallExpr *CallCE = |
| 4754 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 4755 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4756 | IsBcpCall = true; |
| 4757 | |
| 4758 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 4759 | // constant expression; we can't check whether it's potentially foldable. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4760 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4761 | return false; |
| 4762 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4763 | FoldConstant Fold(Info, IsBcpCall); |
| 4764 | if (!HandleConditionalOperator(E)) { |
| 4765 | Fold.keepDiagnostics(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4766 | return false; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4767 | } |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4768 | |
| 4769 | return true; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4770 | } |
| 4771 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4772 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4773 | if (APValue *Value = Info.CurrentCall->getCurrentTemporary(E)) |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4774 | return DerivedSuccess(*Value, E); |
| 4775 | |
| 4776 | const Expr *Source = E->getSourceExpr(); |
| 4777 | if (!Source) |
| 4778 | return Error(E); |
| 4779 | if (Source == E) { // sanity checking. |
| 4780 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
| 4781 | return Error(E); |
Argyrios Kyrtzidis | fac35c0 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 4782 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4783 | return StmtVisitorTy::Visit(Source); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4784 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4785 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4786 | bool VisitCallExpr(const CallExpr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4787 | APValue Result; |
| 4788 | if (!handleCallExpr(E, Result, nullptr)) |
| 4789 | return false; |
| 4790 | return DerivedSuccess(Result, E); |
| 4791 | } |
| 4792 | |
| 4793 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4794 | const LValue *ResultSlot) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4795 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4796 | QualType CalleeType = Callee->getType(); |
| 4797 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4798 | const FunctionDecl *FD = nullptr; |
| 4799 | LValue *This = nullptr, ThisVal; |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 4800 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4801 | bool HasQualifier = false; |
Richard Smith | 656d49d | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 4802 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4803 | // Extract function decl and 'this' pointer from the callee. |
| 4804 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4805 | const ValueDecl *Member = nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4806 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 4807 | // Explicit bound member calls, such as x.f() or p->g(); |
| 4808 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4809 | return false; |
| 4810 | Member = ME->getMemberDecl(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4811 | This = &ThisVal; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4812 | HasQualifier = ME->hasQualifier(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4813 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 4814 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4815 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 4816 | if (!Member) return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4817 | This = &ThisVal; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4818 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4819 | return Error(Callee); |
| 4820 | |
| 4821 | FD = dyn_cast<FunctionDecl>(Member); |
| 4822 | if (!FD) |
| 4823 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4824 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4825 | LValue Call; |
| 4826 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4827 | return false; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4828 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4829 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4830 | return Error(Callee); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4831 | FD = dyn_cast_or_null<FunctionDecl>( |
| 4832 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4833 | if (!FD) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4834 | return Error(Callee); |
Faisal Vali | d92e749 | 2017-01-08 18:56:11 +0000 | [diff] [blame] | 4835 | // Don't call function pointers which have been cast to some other type. |
| 4836 | // Per DR (no number yet), the caller and callee can differ in noexcept. |
| 4837 | if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec( |
| 4838 | CalleeType->getPointeeType(), FD->getType())) { |
| 4839 | return Error(E); |
| 4840 | } |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4841 | |
| 4842 | // Overloaded operator calls to member functions are represented as normal |
| 4843 | // calls with '*this' as the first argument. |
| 4844 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 4845 | if (MD && !MD->isStatic()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4846 | // FIXME: When selecting an implicit conversion for an overloaded |
| 4847 | // operator delete, we sometimes try to evaluate calls to conversion |
| 4848 | // operators without a 'this' parameter! |
| 4849 | if (Args.empty()) |
| 4850 | return Error(E); |
| 4851 | |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4852 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4853 | return false; |
| 4854 | This = &ThisVal; |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4855 | Args = Args.slice(1); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 4856 | } else if (MD && MD->isLambdaStaticInvoker()) { |
Faisal Vali | d92e749 | 2017-01-08 18:56:11 +0000 | [diff] [blame] | 4857 | // Map the static invoker for the lambda back to the call operator. |
| 4858 | // Conveniently, we don't have to slice out the 'this' argument (as is |
| 4859 | // being done for the non-static case), since a static member function |
| 4860 | // doesn't have an implicit argument passed in. |
| 4861 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
| 4862 | assert( |
| 4863 | ClosureClass->captures_begin() == ClosureClass->captures_end() && |
| 4864 | "Number of captures must be zero for conversion to function-ptr"); |
| 4865 | |
| 4866 | const CXXMethodDecl *LambdaCallOp = |
| 4867 | ClosureClass->getLambdaCallOperator(); |
| 4868 | |
| 4869 | // Set 'FD', the function that will be called below, to the call |
| 4870 | // operator. If the closure object represents a generic lambda, find |
| 4871 | // the corresponding specialization of the call operator. |
| 4872 | |
| 4873 | if (ClosureClass->isGenericLambda()) { |
| 4874 | assert(MD->isFunctionTemplateSpecialization() && |
| 4875 | "A generic lambda's static-invoker function must be a " |
| 4876 | "template specialization"); |
| 4877 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
| 4878 | FunctionTemplateDecl *CallOpTemplate = |
| 4879 | LambdaCallOp->getDescribedFunctionTemplate(); |
| 4880 | void *InsertPos = nullptr; |
| 4881 | FunctionDecl *CorrespondingCallOpSpecialization = |
| 4882 | CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); |
| 4883 | assert(CorrespondingCallOpSpecialization && |
| 4884 | "We must always have a function call operator specialization " |
| 4885 | "that corresponds to our static invoker specialization"); |
| 4886 | FD = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
| 4887 | } else |
| 4888 | FD = LambdaCallOp; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 4891 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4892 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4893 | return Error(E); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4894 | |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 4895 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 4896 | return false; |
| 4897 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4898 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 4899 | // calls to such functions in constant expressions. |
| 4900 | if (This && !HasQualifier && |
| 4901 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 4902 | return Error(E, diag::note_constexpr_virtual_call); |
| 4903 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4904 | const FunctionDecl *Definition = nullptr; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4905 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4906 | |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4907 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body) || |
| 4908 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, Info, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4909 | Result, ResultSlot)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4910 | return false; |
| 4911 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4912 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4913 | } |
| 4914 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4915 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4916 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 4917 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4918 | bool VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 90dc175 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 4919 | if (E->getNumInits() == 0) |
| 4920 | return DerivedZeroInitialization(E); |
| 4921 | if (E->getNumInits() == 1) |
| 4922 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4923 | return Error(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4924 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4925 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4926 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4927 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4928 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4929 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4930 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4931 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4932 | return DerivedZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4933 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4934 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4935 | /// 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] | 4936 | bool VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4937 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 4938 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4939 | APValue Val; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4940 | if (!Evaluate(Val, Info, E->getBase())) |
| 4941 | return false; |
| 4942 | |
| 4943 | QualType BaseTy = E->getBase()->getType(); |
| 4944 | |
| 4945 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4946 | if (!FD) return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4947 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4948 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4949 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 4950 | |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 4951 | CompleteObject Obj(&Val, BaseTy, true); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4952 | SubobjectDesignator Designator(BaseTy); |
| 4953 | Designator.addDeclUnchecked(FD); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4954 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4955 | APValue Result; |
| 4956 | return extractSubobject(Info, E, Obj, Designator, Result) && |
| 4957 | DerivedSuccess(Result, E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4960 | bool VisitCastExpr(const CastExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4961 | switch (E->getCastKind()) { |
| 4962 | default: |
| 4963 | break; |
| 4964 | |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4965 | case CK_AtomicToNonAtomic: { |
| 4966 | APValue AtomicVal; |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 4967 | // This does not need to be done in place even for class/array types: |
| 4968 | // atomic-to-non-atomic conversion implies copying the object |
| 4969 | // representation. |
| 4970 | if (!Evaluate(AtomicVal, Info, E->getSubExpr())) |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4971 | return false; |
| 4972 | return DerivedSuccess(AtomicVal, E); |
| 4973 | } |
| 4974 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4975 | case CK_NoOp: |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 4976 | case CK_UserDefinedConversion: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4977 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 4978 | |
| 4979 | case CK_LValueToRValue: { |
| 4980 | LValue LVal; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4981 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 4982 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4983 | APValue RVal; |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 4984 | // 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] | 4985 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 4986 | LVal, RVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4987 | return false; |
| 4988 | return DerivedSuccess(RVal, E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4989 | } |
| 4990 | } |
| 4991 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4992 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4995 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4996 | return VisitUnaryPostIncDec(UO); |
| 4997 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4998 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4999 | return VisitUnaryPostIncDec(UO); |
| 5000 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5001 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5002 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5003 | return Error(UO); |
| 5004 | |
| 5005 | LValue LVal; |
| 5006 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
| 5007 | return false; |
| 5008 | APValue RVal; |
| 5009 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
| 5010 | UO->isIncrementOp(), &RVal)) |
| 5011 | return false; |
| 5012 | return DerivedSuccess(RVal, UO); |
| 5013 | } |
| 5014 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5015 | bool VisitStmtExpr(const StmtExpr *E) { |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5016 | // We will have checked the full-expressions inside the statement expression |
| 5017 | // 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] | 5018 | if (Info.checkingForOverflow()) |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5019 | return Error(E); |
| 5020 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5021 | BlockScopeRAII Scope(Info); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5022 | const CompoundStmt *CS = E->getSubStmt(); |
Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 5023 | if (CS->body_empty()) |
| 5024 | return true; |
| 5025 | |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5026 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 5027 | BE = CS->body_end(); |
| 5028 | /**/; ++BI) { |
| 5029 | if (BI + 1 == BE) { |
| 5030 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
| 5031 | if (!FinalExpr) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5032 | Info.FFDiag((*BI)->getLocStart(), |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5033 | diag::note_constexpr_stmt_expr_unsupported); |
| 5034 | return false; |
| 5035 | } |
| 5036 | return this->Visit(FinalExpr); |
| 5037 | } |
| 5038 | |
| 5039 | APValue ReturnValue; |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5040 | StmtResult Result = { ReturnValue, nullptr }; |
| 5041 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5042 | if (ESR != ESR_Succeeded) { |
| 5043 | // FIXME: If the statement-expression terminated due to 'return', |
| 5044 | // 'break', or 'continue', it would be nice to propagate that to |
| 5045 | // the outer statement evaluation rather than bailing out. |
| 5046 | if (ESR != ESR_Failed) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5047 | Info.FFDiag((*BI)->getLocStart(), |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5048 | diag::note_constexpr_stmt_expr_unsupported); |
| 5049 | return false; |
| 5050 | } |
| 5051 | } |
Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 5052 | |
| 5053 | llvm_unreachable("Return from function from the loop above."); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5054 | } |
| 5055 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5056 | /// Visit a value which is evaluated, but whose value is ignored. |
| 5057 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 5058 | EvaluateIgnoredValue(Info, E); |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5059 | } |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 5060 | |
| 5061 | /// Potentially visit a MemberExpr's base expression. |
| 5062 | void VisitIgnoredBaseExpression(const Expr *E) { |
| 5063 | // While MSVC doesn't evaluate the base expression, it does diagnose the |
| 5064 | // presence of side-effecting behavior. |
| 5065 | if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Info.Ctx)) |
| 5066 | return; |
| 5067 | VisitIgnoredValue(E); |
| 5068 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5069 | }; |
| 5070 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 5071 | } // namespace |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5072 | |
| 5073 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5074 | // Common base class for lvalue and temporary evaluation. |
| 5075 | //===----------------------------------------------------------------------===// |
| 5076 | namespace { |
| 5077 | template<class Derived> |
| 5078 | class LValueExprEvaluatorBase |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5079 | : public ExprEvaluatorBase<Derived> { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5080 | protected: |
| 5081 | LValue &Result; |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5082 | bool InvalidBaseOK; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5083 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5084 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5085 | |
| 5086 | bool Success(APValue::LValueBase B) { |
| 5087 | Result.set(B); |
| 5088 | return true; |
| 5089 | } |
| 5090 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5091 | bool evaluatePointer(const Expr *E, LValue &Result) { |
| 5092 | return EvaluatePointer(E, Result, this->Info, InvalidBaseOK); |
| 5093 | } |
| 5094 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5095 | public: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5096 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) |
| 5097 | : ExprEvaluatorBaseTy(Info), Result(Result), |
| 5098 | InvalidBaseOK(InvalidBaseOK) {} |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5099 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5100 | bool Success(const APValue &V, const Expr *E) { |
| 5101 | Result.setFrom(this->Info.Ctx, V); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5102 | return true; |
| 5103 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5104 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5105 | bool VisitMemberExpr(const MemberExpr *E) { |
| 5106 | // Handle non-static data members. |
| 5107 | QualType BaseTy; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5108 | bool EvalOK; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5109 | if (E->isArrow()) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5110 | EvalOK = evaluatePointer(E->getBase(), Result); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5111 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5112 | } else if (E->getBase()->isRValue()) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 5113 | assert(E->getBase()->getType()->isRecordType()); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5114 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5115 | BaseTy = E->getBase()->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5116 | } else { |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5117 | EvalOK = this->Visit(E->getBase()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5118 | BaseTy = E->getBase()->getType(); |
| 5119 | } |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5120 | if (!EvalOK) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5121 | if (!InvalidBaseOK) |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5122 | return false; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 5123 | Result.setInvalid(E); |
| 5124 | return true; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5125 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5126 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5127 | const ValueDecl *MD = E->getMemberDecl(); |
| 5128 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 5129 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 5130 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 5131 | (void)BaseTy; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5132 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
| 5133 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5134 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5135 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
| 5136 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5137 | } else |
| 5138 | return this->Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5139 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5140 | if (MD->getType()->isReferenceType()) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5141 | APValue RefValue; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5142 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5143 | RefValue)) |
| 5144 | return false; |
| 5145 | return Success(RefValue, E); |
| 5146 | } |
| 5147 | return true; |
| 5148 | } |
| 5149 | |
| 5150 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 5151 | switch (E->getOpcode()) { |
| 5152 | default: |
| 5153 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 5154 | |
| 5155 | case BO_PtrMemD: |
| 5156 | case BO_PtrMemI: |
| 5157 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 5158 | } |
| 5159 | } |
| 5160 | |
| 5161 | bool VisitCastExpr(const CastExpr *E) { |
| 5162 | switch (E->getCastKind()) { |
| 5163 | default: |
| 5164 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5165 | |
| 5166 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5167 | case CK_UncheckedDerivedToBase: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5168 | if (!this->Visit(E->getSubExpr())) |
| 5169 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5170 | |
| 5171 | // Now figure out the necessary offset to add to the base LV to get from |
| 5172 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5173 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
| 5174 | Result); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5175 | } |
| 5176 | } |
| 5177 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5178 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5179 | |
| 5180 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5181 | // LValue Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5182 | // |
| 5183 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 5184 | // function designators (in C), decl references to void objects (in C), and |
| 5185 | // temporaries (if building with -Wno-address-of-temporary). |
| 5186 | // |
| 5187 | // LValue evaluation produces values comprising a base expression of one of the |
| 5188 | // following types: |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5189 | // - Declarations |
| 5190 | // * VarDecl |
| 5191 | // * FunctionDecl |
| 5192 | // - Literals |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 5193 | // * CompoundLiteralExpr in C (and in global scope in C++) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5194 | // * StringLiteral |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5195 | // * CXXTypeidExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5196 | // * PredefinedExpr |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5197 | // * ObjCStringLiteralExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5198 | // * ObjCEncodeExpr |
| 5199 | // * AddrLabelExpr |
| 5200 | // * BlockExpr |
| 5201 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5202 | // - Locals and temporaries |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5203 | // * MaterializeTemporaryExpr |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5204 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5205 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
| 5206 | // from the AST (FIXME). |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5207 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
| 5208 | // CallIndex, for a lifetime-extended temporary. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5209 | // plus an offset in bytes. |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5210 | //===----------------------------------------------------------------------===// |
| 5211 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5212 | class LValueExprEvaluator |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5213 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5214 | public: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5215 | LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) : |
| 5216 | LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5217 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5218 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5219 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5220 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5221 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 5222 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 5223 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5224 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 5225 | bool VisitMemberExpr(const MemberExpr *E); |
| 5226 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 5227 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5228 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 5229 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5230 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 5231 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 5232 | bool VisitUnaryReal(const UnaryOperator *E); |
| 5233 | bool VisitUnaryImag(const UnaryOperator *E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5234 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
| 5235 | return VisitUnaryPreIncDec(UO); |
| 5236 | } |
| 5237 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
| 5238 | return VisitUnaryPreIncDec(UO); |
| 5239 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5240 | bool VisitBinAssign(const BinaryOperator *BO); |
| 5241 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5242 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5243 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5244 | switch (E->getCastKind()) { |
| 5245 | default: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5246 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5247 | |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 5248 | case CK_LValueBitCast: |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5249 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5250 | if (!Visit(E->getSubExpr())) |
| 5251 | return false; |
| 5252 | Result.Designator.setInvalid(); |
| 5253 | return true; |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 5254 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5255 | case CK_BaseToDerived: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5256 | if (!Visit(E->getSubExpr())) |
| 5257 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5258 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5259 | } |
| 5260 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5261 | }; |
| 5262 | } // end anonymous namespace |
| 5263 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5264 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 5265 | /// expressions which are not glvalues, in three cases: |
Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 5266 | /// * function designators in C, and |
| 5267 | /// * "extern void" objects |
Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 5268 | /// * @selector() expressions in Objective-C |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5269 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
| 5270 | bool InvalidBaseOK) { |
Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 5271 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 5272 | E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E)); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5273 | return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5276 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 5277 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5278 | return Success(FD); |
| 5279 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5280 | return VisitVarDecl(E, VD); |
Richard Smith | dca60b4 | 2016-08-12 00:39:32 +0000 | [diff] [blame] | 5281 | if (const BindingDecl *BD = dyn_cast<BindingDecl>(E->getDecl())) |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 5282 | return Visit(BD->getBinding()); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5283 | return Error(E); |
| 5284 | } |
Richard Smith | 733237d | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 5285 | |
Faisal Vali | 0528a31 | 2016-11-13 06:09:16 +0000 | [diff] [blame] | 5286 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5287 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5288 | |
| 5289 | // If we are within a lambda's call operator, check whether the 'VD' referred |
| 5290 | // to within 'E' actually represents a lambda-capture that maps to a |
| 5291 | // data-member/field within the closure object, and if so, evaluate to the |
| 5292 | // field or what the field refers to. |
Erik Pilkington | 1123291 | 2018-04-05 00:12:05 +0000 | [diff] [blame] | 5293 | if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) && |
| 5294 | isa<DeclRefExpr>(E) && |
| 5295 | cast<DeclRefExpr>(E)->refersToEnclosingVariableOrCapture()) { |
| 5296 | // We don't always have a complete capture-map when checking or inferring if |
| 5297 | // the function call operator meets the requirements of a constexpr function |
| 5298 | // - but we don't need to evaluate the captures to determine constexprness |
| 5299 | // (dcl.constexpr C++17). |
| 5300 | if (Info.checkingPotentialConstantExpression()) |
| 5301 | return false; |
| 5302 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5303 | if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) { |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5304 | // Start with 'Result' referring to the complete closure object... |
| 5305 | Result = *Info.CurrentCall->This; |
| 5306 | // ... then update it to refer to the field of the closure object |
| 5307 | // that represents the capture. |
| 5308 | if (!HandleLValueMember(Info, E, Result, FD)) |
| 5309 | return false; |
| 5310 | // And if the field is of reference type, update 'Result' to refer to what |
| 5311 | // the field refers to. |
| 5312 | if (FD->getType()->isReferenceType()) { |
| 5313 | APValue RVal; |
| 5314 | if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, |
| 5315 | RVal)) |
| 5316 | return false; |
| 5317 | Result.setFrom(Info.Ctx, RVal); |
| 5318 | } |
| 5319 | return true; |
| 5320 | } |
| 5321 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5322 | CallStackFrame *Frame = nullptr; |
Faisal Vali | 0528a31 | 2016-11-13 06:09:16 +0000 | [diff] [blame] | 5323 | if (VD->hasLocalStorage() && Info.CurrentCall->Index > 1) { |
| 5324 | // Only if a local variable was declared in the function currently being |
| 5325 | // evaluated, do we expect to be able to find its value in the current |
| 5326 | // frame. (Otherwise it was likely declared in an enclosing context and |
| 5327 | // could either have a valid evaluatable value (for e.g. a constexpr |
| 5328 | // variable) or be ill-formed (and trigger an appropriate evaluation |
| 5329 | // diagnostic)). |
| 5330 | if (Info.CurrentCall->Callee && |
| 5331 | Info.CurrentCall->Callee->Equals(VD->getDeclContext())) { |
| 5332 | Frame = Info.CurrentCall; |
| 5333 | } |
| 5334 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5335 | |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 5336 | if (!VD->getType()->isReferenceType()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5337 | if (Frame) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5338 | Result.set({VD, Frame->Index, |
| 5339 | Info.CurrentCall->getCurrentTemporaryVersion(VD)}); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 5340 | return true; |
| 5341 | } |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5342 | return Success(VD); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 5343 | } |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 5344 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5345 | APValue *V; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5346 | if (!evaluateVarDeclInit(Info, E, VD, Frame, V, nullptr)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5347 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5348 | if (V->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 5349 | if (!Info.checkingPotentialConstantExpression()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5350 | Info.FFDiag(E, diag::note_constexpr_use_uninit_reference); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5351 | return false; |
| 5352 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5353 | return Success(*V, E); |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 5356 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 5357 | const MaterializeTemporaryExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5358 | // Walk through the expression to find the materialized temporary itself. |
| 5359 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5360 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5361 | const Expr *Inner = E->GetTemporaryExpr()-> |
| 5362 | skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5363 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5364 | // If we passed any comma operators, evaluate their LHSs. |
| 5365 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
| 5366 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
| 5367 | return false; |
| 5368 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5369 | // A materialized temporary with static storage duration can appear within the |
| 5370 | // result of a constant expression evaluation, so we need to preserve its |
| 5371 | // value for use outside this evaluation. |
| 5372 | APValue *Value; |
| 5373 | if (E->getStorageDuration() == SD_Static) { |
| 5374 | Value = Info.Ctx.getMaterializedTemporaryValue(E, true); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 5375 | *Value = APValue(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5376 | Result.set(E); |
| 5377 | } else { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5378 | Value = &createTemporary(E, E->getStorageDuration() == SD_Automatic, Result, |
| 5379 | *Info.CurrentCall); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 5382 | QualType Type = Inner->getType(); |
| 5383 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5384 | // Materialize the temporary itself. |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 5385 | if (!EvaluateInPlace(*Value, Info, Result, Inner) || |
| 5386 | (E->getStorageDuration() == SD_Static && |
| 5387 | !CheckConstantExpression(Info, E->getExprLoc(), Type, *Value))) { |
| 5388 | *Value = APValue(); |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5389 | return false; |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 5390 | } |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5391 | |
| 5392 | // Adjust our lvalue to refer to the desired subobject. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5393 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
| 5394 | --I; |
| 5395 | switch (Adjustments[I].Kind) { |
| 5396 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
| 5397 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
| 5398 | Type, Result)) |
| 5399 | return false; |
| 5400 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
| 5401 | break; |
| 5402 | |
| 5403 | case SubobjectAdjustment::FieldAdjustment: |
| 5404 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
| 5405 | return false; |
| 5406 | Type = Adjustments[I].Field->getType(); |
| 5407 | break; |
| 5408 | |
| 5409 | case SubobjectAdjustment::MemberPointerAdjustment: |
| 5410 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
| 5411 | Adjustments[I].Ptr.RHS)) |
| 5412 | return false; |
| 5413 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
| 5414 | break; |
| 5415 | } |
| 5416 | } |
| 5417 | |
| 5418 | return true; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 5419 | } |
| 5420 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5421 | bool |
| 5422 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 5423 | assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) && |
| 5424 | "lvalue compound literal in c++?"); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5425 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 5426 | // 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] | 5427 | return Success(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5430 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 5431 | if (!E->isPotentiallyEvaluated()) |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5432 | return Success(E); |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 5433 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5434 | Info.FFDiag(E, diag::note_constexpr_typeid_polymorphic) |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 5435 | << E->getExprOperand()->getType() |
| 5436 | << E->getExprOperand()->getSourceRange(); |
| 5437 | return false; |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 5440 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 5441 | return Success(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5442 | } |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 5443 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5444 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5445 | // Handle static data members. |
| 5446 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 5447 | VisitIgnoredBaseExpression(E->getBase()); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5448 | return VisitVarDecl(E, VD); |
| 5449 | } |
| 5450 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 5451 | // Handle static member functions. |
| 5452 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 5453 | if (MD->isStatic()) { |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 5454 | VisitIgnoredBaseExpression(E->getBase()); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5455 | return Success(MD); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 5456 | } |
| 5457 | } |
| 5458 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5459 | // Handle non-static data members. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5460 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5463 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5464 | // FIXME: Deal with vectors as array subscript bases. |
| 5465 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5466 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5467 | |
Nick Lewycky | ad88868 | 2017-04-27 07:27:36 +0000 | [diff] [blame] | 5468 | bool Success = true; |
| 5469 | if (!evaluatePointer(E->getBase(), Result)) { |
| 5470 | if (!Info.noteFailure()) |
| 5471 | return false; |
| 5472 | Success = false; |
| 5473 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5474 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5475 | APSInt Index; |
| 5476 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5477 | return false; |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5478 | |
Nick Lewycky | ad88868 | 2017-04-27 07:27:36 +0000 | [diff] [blame] | 5479 | return Success && |
| 5480 | HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5481 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5482 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5483 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5484 | return evaluatePointer(E->getSubExpr(), Result); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 5485 | } |
| 5486 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 5487 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 5488 | if (!Visit(E->getSubExpr())) |
| 5489 | return false; |
| 5490 | // __real is a no-op on scalar lvalues. |
| 5491 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 5492 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 5493 | return true; |
| 5494 | } |
| 5495 | |
| 5496 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 5497 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 5498 | "lvalue __imag__ on scalar?"); |
| 5499 | if (!Visit(E->getSubExpr())) |
| 5500 | return false; |
| 5501 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 5502 | return true; |
| 5503 | } |
| 5504 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5505 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5506 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5507 | return Error(UO); |
| 5508 | |
| 5509 | if (!this->Visit(UO->getSubExpr())) |
| 5510 | return false; |
| 5511 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5512 | return handleIncDec( |
| 5513 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5514 | UO->isIncrementOp(), nullptr); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
| 5518 | const CompoundAssignOperator *CAO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5519 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5520 | return Error(CAO); |
| 5521 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5522 | APValue RHS; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5523 | |
| 5524 | // The overall lvalue result is the result of evaluating the LHS. |
| 5525 | if (!this->Visit(CAO->getLHS())) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5526 | if (Info.noteFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5527 | Evaluate(RHS, this->Info, CAO->getRHS()); |
| 5528 | return false; |
| 5529 | } |
| 5530 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5531 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) |
| 5532 | return false; |
| 5533 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 5534 | return handleCompoundAssignment( |
| 5535 | this->Info, CAO, |
| 5536 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
| 5537 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5541 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5542 | return Error(E); |
| 5543 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5544 | APValue NewVal; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5545 | |
| 5546 | if (!this->Visit(E->getLHS())) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5547 | if (Info.noteFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5548 | Evaluate(NewVal, this->Info, E->getRHS()); |
| 5549 | return false; |
| 5550 | } |
| 5551 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5552 | if (!Evaluate(NewVal, this->Info, E->getRHS())) |
| 5553 | return false; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5554 | |
| 5555 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5556 | NewVal); |
| 5557 | } |
| 5558 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5559 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5560 | // Pointer Evaluation |
| 5561 | //===----------------------------------------------------------------------===// |
| 5562 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5563 | /// Attempts to compute the number of bytes available at the pointer |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5564 | /// returned by a function with the alloc_size attribute. Returns true if we |
| 5565 | /// were successful. Places an unsigned number into `Result`. |
| 5566 | /// |
| 5567 | /// This expects the given CallExpr to be a call to a function with an |
| 5568 | /// alloc_size attribute. |
| 5569 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
| 5570 | const CallExpr *Call, |
| 5571 | llvm::APInt &Result) { |
| 5572 | const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call); |
| 5573 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 5574 | assert(AllocSize && AllocSize->getElemSizeParam().isValid()); |
| 5575 | unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5576 | unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType()); |
| 5577 | if (Call->getNumArgs() <= SizeArgNo) |
| 5578 | return false; |
| 5579 | |
| 5580 | auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) { |
| 5581 | if (!E->EvaluateAsInt(Into, Ctx, Expr::SE_AllowSideEffects)) |
| 5582 | return false; |
| 5583 | if (Into.isNegative() || !Into.isIntN(BitsInSizeT)) |
| 5584 | return false; |
| 5585 | Into = Into.zextOrSelf(BitsInSizeT); |
| 5586 | return true; |
| 5587 | }; |
| 5588 | |
| 5589 | APSInt SizeOfElem; |
| 5590 | if (!EvaluateAsSizeT(Call->getArg(SizeArgNo), SizeOfElem)) |
| 5591 | return false; |
| 5592 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 5593 | if (!AllocSize->getNumElemsParam().isValid()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5594 | Result = std::move(SizeOfElem); |
| 5595 | return true; |
| 5596 | } |
| 5597 | |
| 5598 | APSInt NumberOfElems; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 5599 | unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5600 | if (!EvaluateAsSizeT(Call->getArg(NumArgNo), NumberOfElems)) |
| 5601 | return false; |
| 5602 | |
| 5603 | bool Overflow; |
| 5604 | llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow); |
| 5605 | if (Overflow) |
| 5606 | return false; |
| 5607 | |
| 5608 | Result = std::move(BytesAvailable); |
| 5609 | return true; |
| 5610 | } |
| 5611 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5612 | /// 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] | 5613 | /// function. |
| 5614 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
| 5615 | const LValue &LVal, |
| 5616 | llvm::APInt &Result) { |
| 5617 | assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 5618 | "Can't get the size of a non alloc_size function"); |
| 5619 | const auto *Base = LVal.getLValueBase().get<const Expr *>(); |
| 5620 | const CallExpr *CE = tryUnwrapAllocSizeCall(Base); |
| 5621 | return getBytesReturnedByAllocSizeCall(Ctx, CE, Result); |
| 5622 | } |
| 5623 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5624 | /// 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] | 5625 | /// a function with the alloc_size attribute. If it was possible to do so, this |
| 5626 | /// function will return true, make Result's Base point to said function call, |
| 5627 | /// and mark Result's Base as invalid. |
| 5628 | static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base, |
| 5629 | LValue &Result) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5630 | if (Base.isNull()) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5631 | return false; |
| 5632 | |
| 5633 | // Because we do no form of static analysis, we only support const variables. |
| 5634 | // |
| 5635 | // Additionally, we can't support parameters, nor can we support static |
| 5636 | // variables (in the latter case, use-before-assign isn't UB; in the former, |
| 5637 | // we have no clue what they'll be assigned to). |
| 5638 | const auto *VD = |
| 5639 | dyn_cast_or_null<VarDecl>(Base.dyn_cast<const ValueDecl *>()); |
| 5640 | if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified()) |
| 5641 | return false; |
| 5642 | |
| 5643 | const Expr *Init = VD->getAnyInitializer(); |
| 5644 | if (!Init) |
| 5645 | return false; |
| 5646 | |
| 5647 | const Expr *E = Init->IgnoreParens(); |
| 5648 | if (!tryUnwrapAllocSizeCall(E)) |
| 5649 | return false; |
| 5650 | |
| 5651 | // Store E instead of E unwrapped so that the type of the LValue's base is |
| 5652 | // what the user wanted. |
| 5653 | Result.setInvalid(E); |
| 5654 | |
| 5655 | QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5656 | Result.addUnsizedArray(Info, E, Pointee); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5657 | return true; |
| 5658 | } |
| 5659 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 5660 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5661 | class PointerExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5662 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5663 | LValue &Result; |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5664 | bool InvalidBaseOK; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5665 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5666 | bool Success(const Expr *E) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5667 | Result.set(E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5668 | return true; |
| 5669 | } |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5670 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5671 | bool evaluateLValue(const Expr *E, LValue &Result) { |
| 5672 | return EvaluateLValue(E, Result, Info, InvalidBaseOK); |
| 5673 | } |
| 5674 | |
| 5675 | bool evaluatePointer(const Expr *E, LValue &Result) { |
| 5676 | return EvaluatePointer(E, Result, Info, InvalidBaseOK); |
| 5677 | } |
| 5678 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5679 | bool visitNonBuiltinCallExpr(const CallExpr *E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5680 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5681 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5682 | PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK) |
| 5683 | : ExprEvaluatorBaseTy(info), Result(Result), |
| 5684 | InvalidBaseOK(InvalidBaseOK) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5685 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5686 | bool Success(const APValue &V, const Expr *E) { |
| 5687 | Result.setFrom(Info.Ctx, V); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5688 | return true; |
| 5689 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5690 | bool ZeroInitialization(const Expr *E) { |
Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 5691 | auto TargetVal = Info.Ctx.getTargetNullPointerValue(E->getType()); |
| 5692 | Result.setNull(E->getType(), TargetVal); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 5693 | return true; |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5694 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5695 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5696 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5697 | bool VisitCastExpr(const CastExpr* E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5698 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5699 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5700 | { return Success(E); } |
Nick Lewycky | 19ae6dc | 2017-04-29 00:07:27 +0000 | [diff] [blame] | 5701 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
| 5702 | if (Info.noteFailure()) |
| 5703 | EvaluateIgnoredValue(Info, E->getSubExpr()); |
| 5704 | return Error(E); |
| 5705 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5706 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5707 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5708 | bool VisitCallExpr(const CallExpr *E); |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 5709 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5710 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 5711 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5712 | return Success(E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5713 | return Error(E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 5714 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5715 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5716 | // Can't look at 'this' when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 5717 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5718 | return false; |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 5719 | if (!Info.CurrentCall->This) { |
| 5720 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5721 | Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit(); |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 5722 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5723 | Info.FFDiag(E); |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 5724 | return false; |
| 5725 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5726 | Result = *Info.CurrentCall->This; |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5727 | // If we are inside a lambda's call operator, the 'this' expression refers |
| 5728 | // to the enclosing '*this' object (either by value or reference) which is |
| 5729 | // either copied into the closure object's field that represents the '*this' |
| 5730 | // or refers to '*this'. |
| 5731 | if (isLambdaCallOperator(Info.CurrentCall->Callee)) { |
| 5732 | // Update 'Result' to refer to the data member/field of the closure object |
| 5733 | // that represents the '*this' capture. |
| 5734 | if (!HandleLValueMember(Info, E, Result, |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 5735 | Info.CurrentCall->LambdaThisCaptureField)) |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5736 | return false; |
| 5737 | // If we captured '*this' by reference, replace the field with its referent. |
| 5738 | if (Info.CurrentCall->LambdaThisCaptureField->getType() |
| 5739 | ->isPointerType()) { |
| 5740 | APValue RVal; |
| 5741 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Result, |
| 5742 | RVal)) |
| 5743 | return false; |
| 5744 | |
| 5745 | Result.setFrom(Info.Ctx, RVal); |
| 5746 | } |
| 5747 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5748 | return true; |
| 5749 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5750 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 5751 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5752 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5753 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5754 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5755 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info, |
| 5756 | bool InvalidBaseOK) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5757 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5758 | return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5759 | } |
| 5760 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5761 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5762 | if (E->getOpcode() != BO_Add && |
| 5763 | E->getOpcode() != BO_Sub) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5764 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5765 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5766 | const Expr *PExp = E->getLHS(); |
| 5767 | const Expr *IExp = E->getRHS(); |
| 5768 | if (IExp->getType()->isPointerType()) |
| 5769 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5770 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5771 | bool EvalPtrOK = evaluatePointer(PExp, Result); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5772 | if (!EvalPtrOK && !Info.noteFailure()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5773 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5774 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5775 | llvm::APSInt Offset; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5776 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5777 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 5778 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5779 | if (E->getOpcode() == BO_Sub) |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 5780 | negateAsSigned(Offset); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5781 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5782 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 5783 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5784 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5785 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5786 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5787 | return evaluateLValue(E->getSubExpr(), Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5788 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5789 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5790 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 5791 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5792 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 5793 | switch (E->getCastKind()) { |
| 5794 | default: |
| 5795 | break; |
| 5796 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5797 | case CK_BitCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5798 | case CK_CPointerToObjCPointerCast: |
| 5799 | case CK_BlockPointerToObjCPointerCast: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5800 | case CK_AnyPointerToBlockPointerCast: |
Anastasia Stulova | 5d8ad8a | 2014-11-26 15:36:41 +0000 | [diff] [blame] | 5801 | case CK_AddressSpaceConversion: |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 5802 | if (!Visit(SubExpr)) |
| 5803 | return false; |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5804 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 5805 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 5806 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 5807 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 5808 | Result.Designator.setInvalid(); |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 5809 | if (SubExpr->getType()->isVoidPointerType()) |
| 5810 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 5811 | << 3 << SubExpr->getType(); |
| 5812 | else |
| 5813 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 5814 | } |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 5815 | if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr) |
| 5816 | ZeroInitialization(E); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5817 | return true; |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 5818 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5819 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5820 | case CK_UncheckedDerivedToBase: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5821 | if (!evaluatePointer(E->getSubExpr(), Result)) |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5822 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5823 | if (!Result.Base && Result.Offset.isZero()) |
| 5824 | return true; |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5825 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5826 | // 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] | 5827 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5828 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
| 5829 | castAs<PointerType>()->getPointeeType(), |
| 5830 | Result); |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5831 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5832 | case CK_BaseToDerived: |
| 5833 | if (!Visit(E->getSubExpr())) |
| 5834 | return false; |
| 5835 | if (!Result.Base && Result.Offset.isZero()) |
| 5836 | return true; |
| 5837 | return HandleBaseToDerivedCast(Info, E, Result); |
| 5838 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5839 | case CK_NullToPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 5840 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5841 | return ZeroInitialization(E); |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5842 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5843 | case CK_IntegralToPointer: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5844 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 5845 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5846 | APValue Value; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5847 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 5848 | break; |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 5849 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5850 | if (Value.isInt()) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5851 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 5852 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5853 | Result.Base = (Expr*)nullptr; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5854 | Result.InvalidBase = false; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5855 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5856 | Result.Designator.setInvalid(); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 5857 | Result.IsNullPtr = false; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5858 | return true; |
| 5859 | } else { |
| 5860 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5861 | Result.setFrom(Info.Ctx, Value); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5862 | return true; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5863 | } |
| 5864 | } |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5865 | |
| 5866 | case CK_ArrayToPointerDecay: { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5867 | if (SubExpr->isGLValue()) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5868 | if (!evaluateLValue(SubExpr, Result)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5869 | return false; |
| 5870 | } else { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5871 | APValue &Value = createTemporary(SubExpr, false, Result, |
| 5872 | *Info.CurrentCall); |
| 5873 | if (!EvaluateInPlace(Value, Info, Result, SubExpr)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5874 | return false; |
| 5875 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5876 | // The result is a pointer to the first element of the array. |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5877 | auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType()); |
| 5878 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5879 | Result.addArray(Info, E, CAT); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 5880 | else |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5881 | Result.addUnsizedArray(Info, E, AT->getElementType()); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5882 | return true; |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5883 | } |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 5884 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5885 | case CK_FunctionToPointerDecay: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5886 | return evaluateLValue(SubExpr, Result); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5887 | |
| 5888 | case CK_LValueToRValue: { |
| 5889 | LValue LVal; |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5890 | if (!evaluateLValue(E->getSubExpr(), LVal)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5891 | return false; |
| 5892 | |
| 5893 | APValue RVal; |
| 5894 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
| 5895 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
| 5896 | LVal, RVal)) |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5897 | return InvalidBaseOK && |
| 5898 | evaluateLValueAsAllocSize(Info, LVal.Base, Result); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5899 | return Success(RVal, E); |
| 5900 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5901 | } |
| 5902 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5903 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5904 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5905 | |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5906 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T) { |
| 5907 | // C++ [expr.alignof]p3: |
| 5908 | // When alignof is applied to a reference type, the result is the |
| 5909 | // alignment of the referenced type. |
| 5910 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 5911 | T = Ref->getPointeeType(); |
| 5912 | |
| 5913 | // __alignof is defined to return the preferred alignment. |
Roger Ferrer Ibanez | 3fa38a1 | 2017-03-08 14:00:44 +0000 | [diff] [blame] | 5914 | if (T.getQualifiers().hasUnaligned()) |
| 5915 | return CharUnits::One(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5916 | return Info.Ctx.toCharUnitsFromBits( |
| 5917 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
| 5918 | } |
| 5919 | |
| 5920 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E) { |
| 5921 | E = E->IgnoreParens(); |
| 5922 | |
| 5923 | // The kinds of expressions that we have special-case logic here for |
| 5924 | // should be kept up to date with the special checks for those |
| 5925 | // expressions in Sema. |
| 5926 | |
| 5927 | // alignof decl is always accepted, even if it doesn't make sense: we default |
| 5928 | // to 1 in those cases. |
| 5929 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 5930 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 5931 | /*RefAsPointee*/true); |
| 5932 | |
| 5933 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 5934 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 5935 | /*RefAsPointee*/true); |
| 5936 | |
| 5937 | return GetAlignOfType(Info, E->getType()); |
| 5938 | } |
| 5939 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5940 | // To be clear: this happily visits unsupported builtins. Better name welcomed. |
| 5941 | bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) { |
| 5942 | if (ExprEvaluatorBaseTy::VisitCallExpr(E)) |
| 5943 | return true; |
| 5944 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5945 | if (!(InvalidBaseOK && getAllocSizeAttr(E))) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5946 | return false; |
| 5947 | |
| 5948 | Result.setInvalid(E); |
| 5949 | QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5950 | Result.addUnsizedArray(Info, E, PointeeTy); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5951 | return true; |
| 5952 | } |
| 5953 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5954 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5955 | if (IsStringLiteralCall(E)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5956 | return Success(E); |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 5957 | |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 5958 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
| 5959 | return VisitBuiltinCallExpr(E, BuiltinOp); |
| 5960 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5961 | return visitNonBuiltinCallExpr(E); |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 5962 | } |
| 5963 | |
| 5964 | bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
| 5965 | unsigned BuiltinOp) { |
| 5966 | switch (BuiltinOp) { |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 5967 | case Builtin::BI__builtin_addressof: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5968 | return evaluateLValue(E->getArg(0), Result); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5969 | case Builtin::BI__builtin_assume_aligned: { |
| 5970 | // We need to be very careful here because: if the pointer does not have the |
| 5971 | // asserted alignment, then the behavior is undefined, and undefined |
| 5972 | // behavior is non-constant. |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5973 | if (!evaluatePointer(E->getArg(0), Result)) |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5974 | return false; |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 5975 | |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5976 | LValue OffsetResult(Result); |
| 5977 | APSInt Alignment; |
| 5978 | if (!EvaluateInteger(E->getArg(1), Alignment, Info)) |
| 5979 | return false; |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 5980 | CharUnits Align = CharUnits::fromQuantity(Alignment.getZExtValue()); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5981 | |
| 5982 | if (E->getNumArgs() > 2) { |
| 5983 | APSInt Offset; |
| 5984 | if (!EvaluateInteger(E->getArg(2), Offset, Info)) |
| 5985 | return false; |
| 5986 | |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 5987 | int64_t AdditionalOffset = -Offset.getZExtValue(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5988 | OffsetResult.Offset += CharUnits::fromQuantity(AdditionalOffset); |
| 5989 | } |
| 5990 | |
| 5991 | // If there is a base object, then it must have the correct alignment. |
| 5992 | if (OffsetResult.Base) { |
| 5993 | CharUnits BaseAlignment; |
| 5994 | if (const ValueDecl *VD = |
| 5995 | OffsetResult.Base.dyn_cast<const ValueDecl*>()) { |
| 5996 | BaseAlignment = Info.Ctx.getDeclAlign(VD); |
| 5997 | } else { |
| 5998 | BaseAlignment = |
| 5999 | GetAlignOfExpr(Info, OffsetResult.Base.get<const Expr*>()); |
| 6000 | } |
| 6001 | |
| 6002 | if (BaseAlignment < Align) { |
| 6003 | Result.Designator.setInvalid(); |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 6004 | // FIXME: Add support to Diagnostic for long / long long. |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6005 | CCEDiag(E->getArg(0), |
| 6006 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 6007 | << (unsigned)BaseAlignment.getQuantity() |
| 6008 | << (unsigned)Align.getQuantity(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6009 | return false; |
| 6010 | } |
| 6011 | } |
| 6012 | |
| 6013 | // The offset must also have the correct alignment. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6014 | if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) { |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6015 | Result.Designator.setInvalid(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6016 | |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 6017 | (OffsetResult.Base |
| 6018 | ? CCEDiag(E->getArg(0), |
| 6019 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
| 6020 | : CCEDiag(E->getArg(0), |
| 6021 | diag::note_constexpr_baa_value_insufficient_alignment)) |
| 6022 | << (int)OffsetResult.Offset.getQuantity() |
| 6023 | << (unsigned)Align.getQuantity(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6024 | return false; |
| 6025 | } |
| 6026 | |
| 6027 | return true; |
| 6028 | } |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6029 | |
| 6030 | case Builtin::BIstrchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6031 | case Builtin::BIwcschr: |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6032 | case Builtin::BImemchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6033 | case Builtin::BIwmemchr: |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6034 | if (Info.getLangOpts().CPlusPlus11) |
| 6035 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| 6036 | << /*isConstexpr*/0 << /*isConstructor*/0 |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6037 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6038 | else |
| 6039 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 6040 | LLVM_FALLTHROUGH; |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6041 | case Builtin::BI__builtin_strchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6042 | case Builtin::BI__builtin_wcschr: |
| 6043 | case Builtin::BI__builtin_memchr: |
Richard Smith | 5e29dd3 | 2017-01-20 00:45:35 +0000 | [diff] [blame] | 6044 | case Builtin::BI__builtin_char_memchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6045 | case Builtin::BI__builtin_wmemchr: { |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6046 | if (!Visit(E->getArg(0))) |
| 6047 | return false; |
| 6048 | APSInt Desired; |
| 6049 | if (!EvaluateInteger(E->getArg(1), Desired, Info)) |
| 6050 | return false; |
| 6051 | uint64_t MaxLength = uint64_t(-1); |
| 6052 | if (BuiltinOp != Builtin::BIstrchr && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6053 | BuiltinOp != Builtin::BIwcschr && |
| 6054 | BuiltinOp != Builtin::BI__builtin_strchr && |
| 6055 | BuiltinOp != Builtin::BI__builtin_wcschr) { |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6056 | APSInt N; |
| 6057 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
| 6058 | return false; |
| 6059 | MaxLength = N.getExtValue(); |
| 6060 | } |
| 6061 | |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6062 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6063 | |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6064 | // Figure out what value we're actually looking for (after converting to |
| 6065 | // the corresponding unsigned type if necessary). |
| 6066 | uint64_t DesiredVal; |
| 6067 | bool StopAtNull = false; |
| 6068 | switch (BuiltinOp) { |
| 6069 | case Builtin::BIstrchr: |
| 6070 | case Builtin::BI__builtin_strchr: |
| 6071 | // strchr compares directly to the passed integer, and therefore |
| 6072 | // always fails if given an int that is not a char. |
| 6073 | if (!APSInt::isSameValue(HandleIntToIntCast(Info, E, CharTy, |
| 6074 | E->getArg(1)->getType(), |
| 6075 | Desired), |
| 6076 | Desired)) |
| 6077 | return ZeroInitialization(E); |
| 6078 | StopAtNull = true; |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 6079 | LLVM_FALLTHROUGH; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6080 | case Builtin::BImemchr: |
| 6081 | case Builtin::BI__builtin_memchr: |
Richard Smith | 5e29dd3 | 2017-01-20 00:45:35 +0000 | [diff] [blame] | 6082 | case Builtin::BI__builtin_char_memchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6083 | // memchr compares by converting both sides to unsigned char. That's also |
| 6084 | // correct for strchr if we get this far (to cope with plain char being |
| 6085 | // unsigned in the strchr case). |
| 6086 | DesiredVal = Desired.trunc(Info.Ctx.getCharWidth()).getZExtValue(); |
| 6087 | break; |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6088 | |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6089 | case Builtin::BIwcschr: |
| 6090 | case Builtin::BI__builtin_wcschr: |
| 6091 | StopAtNull = true; |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 6092 | LLVM_FALLTHROUGH; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6093 | case Builtin::BIwmemchr: |
| 6094 | case Builtin::BI__builtin_wmemchr: |
| 6095 | // wcschr and wmemchr are given a wchar_t to look for. Just use it. |
| 6096 | DesiredVal = Desired.getZExtValue(); |
| 6097 | break; |
| 6098 | } |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6099 | |
| 6100 | for (; MaxLength; --MaxLength) { |
| 6101 | APValue Char; |
| 6102 | if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) || |
| 6103 | !Char.isInt()) |
| 6104 | return false; |
| 6105 | if (Char.getInt().getZExtValue() == DesiredVal) |
| 6106 | return true; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6107 | if (StopAtNull && !Char.getInt()) |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6108 | break; |
| 6109 | if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1)) |
| 6110 | return false; |
| 6111 | } |
| 6112 | // Not found: return nullptr. |
| 6113 | return ZeroInitialization(E); |
| 6114 | } |
| 6115 | |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 6116 | default: |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6117 | return visitNonBuiltinCallExpr(E); |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 6118 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6119 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6120 | |
| 6121 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6122 | // Member Pointer Evaluation |
| 6123 | //===----------------------------------------------------------------------===// |
| 6124 | |
| 6125 | namespace { |
| 6126 | class MemberPointerExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6127 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6128 | MemberPtr &Result; |
| 6129 | |
| 6130 | bool Success(const ValueDecl *D) { |
| 6131 | Result = MemberPtr(D); |
| 6132 | return true; |
| 6133 | } |
| 6134 | public: |
| 6135 | |
| 6136 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 6137 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 6138 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6139 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6140 | Result.setFrom(V); |
| 6141 | return true; |
| 6142 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6143 | bool ZeroInitialization(const Expr *E) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6144 | return Success((const ValueDecl*)nullptr); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6145 | } |
| 6146 | |
| 6147 | bool VisitCastExpr(const CastExpr *E); |
| 6148 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 6149 | }; |
| 6150 | } // end anonymous namespace |
| 6151 | |
| 6152 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 6153 | EvalInfo &Info) { |
| 6154 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 6155 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 6156 | } |
| 6157 | |
| 6158 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 6159 | switch (E->getCastKind()) { |
| 6160 | default: |
| 6161 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6162 | |
| 6163 | case CK_NullToMemberPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 6164 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6165 | return ZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6166 | |
| 6167 | case CK_BaseToDerivedMemberPointer: { |
| 6168 | if (!Visit(E->getSubExpr())) |
| 6169 | return false; |
| 6170 | if (E->path_empty()) |
| 6171 | return true; |
| 6172 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 6173 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 6174 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 6175 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 6176 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 6177 | PathI != PathE; ++PathI) { |
| 6178 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 6179 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 6180 | if (!Result.castToDerived(Derived)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6181 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6182 | } |
| 6183 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 6184 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6185 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6186 | return true; |
| 6187 | } |
| 6188 | |
| 6189 | case CK_DerivedToBaseMemberPointer: |
| 6190 | if (!Visit(E->getSubExpr())) |
| 6191 | return false; |
| 6192 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 6193 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 6194 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 6195 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 6196 | if (!Result.castToBase(Base)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6197 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6198 | } |
| 6199 | return true; |
| 6200 | } |
| 6201 | } |
| 6202 | |
| 6203 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 6204 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 6205 | // member can be formed. |
| 6206 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 6207 | } |
| 6208 | |
| 6209 | //===----------------------------------------------------------------------===// |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6210 | // Record Evaluation |
| 6211 | //===----------------------------------------------------------------------===// |
| 6212 | |
| 6213 | namespace { |
| 6214 | class RecordExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6215 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6216 | const LValue &This; |
| 6217 | APValue &Result; |
| 6218 | public: |
| 6219 | |
| 6220 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 6221 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 6222 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6223 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6224 | Result = V; |
| 6225 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6226 | } |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6227 | bool ZeroInitialization(const Expr *E) { |
| 6228 | return ZeroInitialization(E, E->getType()); |
| 6229 | } |
| 6230 | bool ZeroInitialization(const Expr *E, QualType T); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6231 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6232 | bool VisitCallExpr(const CallExpr *E) { |
| 6233 | return handleCallExpr(E, Result, &This); |
| 6234 | } |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6235 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6236 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6237 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 6238 | return VisitCXXConstructExpr(E, E->getType()); |
| 6239 | } |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6240 | bool VisitLambdaExpr(const LambdaExpr *E); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6241 | bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E); |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6242 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6243 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 6244 | |
| 6245 | bool VisitBinCmp(const BinaryOperator *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6246 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6247 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6248 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6249 | /// Perform zero-initialization on an object of non-union class type. |
| 6250 | /// C++11 [dcl.init]p5: |
| 6251 | /// To zero-initialize an object or reference of type T means: |
| 6252 | /// [...] |
| 6253 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 6254 | /// each non-static data member and each base-class subobject is |
| 6255 | /// zero-initialized |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6256 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 6257 | const RecordDecl *RD, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6258 | const LValue &This, APValue &Result) { |
| 6259 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 6260 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 6261 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 6262 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6263 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6264 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6265 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 6266 | |
| 6267 | if (CD) { |
| 6268 | unsigned Index = 0; |
| 6269 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6270 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6271 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 6272 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6273 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
| 6274 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6275 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6276 | Result.getStructBase(Index))) |
| 6277 | return false; |
| 6278 | } |
| 6279 | } |
| 6280 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6281 | for (const auto *I : RD->fields()) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6282 | // -- if T is a reference type, no initialization is performed. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6283 | if (I->getType()->isReferenceType()) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6284 | continue; |
| 6285 | |
| 6286 | LValue Subobject = This; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6287 | if (!HandleLValueMember(Info, E, Subobject, I, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6288 | return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6289 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6290 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6291 | if (!EvaluateInPlace( |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6292 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6293 | return false; |
| 6294 | } |
| 6295 | |
| 6296 | return true; |
| 6297 | } |
| 6298 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6299 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) { |
| 6300 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 6301 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6302 | if (RD->isUnion()) { |
| 6303 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 6304 | // object's first non-static named data member is zero-initialized |
| 6305 | RecordDecl::field_iterator I = RD->field_begin(); |
| 6306 | if (I == RD->field_end()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6307 | Result = APValue((const FieldDecl*)nullptr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6308 | return true; |
| 6309 | } |
| 6310 | |
| 6311 | LValue Subobject = This; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 6312 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6313 | return false; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 6314 | Result = APValue(*I); |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6315 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6316 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6317 | } |
| 6318 | |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 6319 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 6320 | Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 6321 | return false; |
| 6322 | } |
| 6323 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6324 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6325 | } |
| 6326 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6327 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 6328 | switch (E->getCastKind()) { |
| 6329 | default: |
| 6330 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6331 | |
| 6332 | case CK_ConstructorConversion: |
| 6333 | return Visit(E->getSubExpr()); |
| 6334 | |
| 6335 | case CK_DerivedToBase: |
| 6336 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6337 | APValue DerivedObject; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6338 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6339 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6340 | if (!DerivedObject.isStruct()) |
| 6341 | return Error(E->getSubExpr()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6342 | |
| 6343 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 6344 | APValue *Value = &DerivedObject; |
| 6345 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 6346 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 6347 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 6348 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 6349 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 6350 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 6351 | RD = Base; |
| 6352 | } |
| 6353 | Result = *Value; |
| 6354 | return true; |
| 6355 | } |
| 6356 | } |
| 6357 | } |
| 6358 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6359 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 6360 | if (E->isTransparent()) |
| 6361 | return Visit(E->getInit(0)); |
| 6362 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6363 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 6364 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6365 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 6366 | |
| 6367 | if (RD->isUnion()) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6368 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 6369 | Result = APValue(Field); |
| 6370 | if (!Field) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6371 | return true; |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6372 | |
| 6373 | // If the initializer list for a union does not contain any elements, the |
| 6374 | // first element of the union is value-initialized. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6375 | // FIXME: The element should be initialized from an initializer list. |
| 6376 | // Is this difference ever observable for initializer lists which |
| 6377 | // we don't build? |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6378 | ImplicitValueInitExpr VIE(Field->getType()); |
| 6379 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 6380 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6381 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6382 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
| 6383 | return false; |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6384 | |
| 6385 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 6386 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 6387 | isa<CXXDefaultInitExpr>(InitExpr)); |
| 6388 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6389 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6392 | auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
Richard Smith | c0d04a2 | 2016-05-25 22:06:25 +0000 | [diff] [blame] | 6393 | if (Result.isUninit()) |
| 6394 | Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0, |
| 6395 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6396 | unsigned ElementNo = 0; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6397 | bool Success = true; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6398 | |
| 6399 | // Initialize base classes. |
| 6400 | if (CXXRD) { |
| 6401 | for (const auto &Base : CXXRD->bases()) { |
| 6402 | assert(ElementNo < E->getNumInits() && "missing init for base class"); |
| 6403 | const Expr *Init = E->getInit(ElementNo); |
| 6404 | |
| 6405 | LValue Subobject = This; |
| 6406 | if (!HandleLValueBase(Info, Init, Subobject, CXXRD, &Base)) |
| 6407 | return false; |
| 6408 | |
| 6409 | APValue &FieldVal = Result.getStructBase(ElementNo); |
| 6410 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init)) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 6411 | if (!Info.noteFailure()) |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6412 | return false; |
| 6413 | Success = false; |
| 6414 | } |
| 6415 | ++ElementNo; |
| 6416 | } |
| 6417 | } |
| 6418 | |
| 6419 | // Initialize members. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6420 | for (const auto *Field : RD->fields()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6421 | // Anonymous bit-fields are not considered members of the class for |
| 6422 | // purposes of aggregate initialization. |
| 6423 | if (Field->isUnnamedBitfield()) |
| 6424 | continue; |
| 6425 | |
| 6426 | LValue Subobject = This; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6427 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6428 | bool HaveInit = ElementNo < E->getNumInits(); |
| 6429 | |
| 6430 | // FIXME: Diagnostics here should point to the end of the initializer |
| 6431 | // list, not the start. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6432 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6433 | Subobject, Field, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6434 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6435 | |
| 6436 | // Perform an implicit value-initialization for members beyond the end of |
| 6437 | // the initializer list. |
| 6438 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6439 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6440 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6441 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 6442 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 6443 | isa<CXXDefaultInitExpr>(Init)); |
| 6444 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 6445 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 6446 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) || |
| 6447 | (Field->isBitField() && !truncateBitfieldValue(Info, Init, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6448 | FieldVal, Field))) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 6449 | if (!Info.noteFailure()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6450 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6451 | Success = false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6452 | } |
| 6453 | } |
| 6454 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6455 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6456 | } |
| 6457 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6458 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 6459 | QualType T) { |
| 6460 | // Note that E's type is not necessarily the type of our class here; we might |
| 6461 | // be initializing an array element instead. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6462 | const CXXConstructorDecl *FD = E->getConstructor(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 6463 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 6464 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6465 | bool ZeroInit = E->requiresZeroInitialization(); |
| 6466 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6467 | // If we've already performed zero-initialization, we're already done. |
| 6468 | if (!Result.isUninit()) |
| 6469 | return true; |
| 6470 | |
Richard Smith | da3f4fd | 2014-03-05 23:32:50 +0000 | [diff] [blame] | 6471 | // We can get here in two different ways: |
| 6472 | // 1) We're performing value-initialization, and should zero-initialize |
| 6473 | // the object, or |
| 6474 | // 2) We're performing default-initialization of an object with a trivial |
| 6475 | // constexpr default constructor, in which case we should start the |
| 6476 | // lifetimes of all the base subobjects (there can be no data member |
| 6477 | // subobjects in this case) per [basic.life]p1. |
| 6478 | // Either way, ZeroInitialization is appropriate. |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6479 | return ZeroInitialization(E, T); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 6480 | } |
| 6481 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6482 | const FunctionDecl *Definition = nullptr; |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 6483 | auto Body = FD->getBody(Definition); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6484 | |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 6485 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6486 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6487 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 6488 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6489 | if (E->isElidable() && !ZeroInit) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6490 | if (const MaterializeTemporaryExpr *ME |
| 6491 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 6492 | return Visit(ME->GetTemporaryExpr()); |
| 6493 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6494 | if (ZeroInit && !ZeroInitialization(E, T)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6495 | return false; |
| 6496 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 6497 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6498 | return HandleConstructorCall(E, This, Args, |
| 6499 | cast<CXXConstructorDecl>(Definition), Info, |
| 6500 | Result); |
| 6501 | } |
| 6502 | |
| 6503 | bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr( |
| 6504 | const CXXInheritedCtorInitExpr *E) { |
| 6505 | if (!Info.CurrentCall) { |
| 6506 | assert(Info.checkingPotentialConstantExpression()); |
| 6507 | return false; |
| 6508 | } |
| 6509 | |
| 6510 | const CXXConstructorDecl *FD = E->getConstructor(); |
| 6511 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) |
| 6512 | return false; |
| 6513 | |
| 6514 | const FunctionDecl *Definition = nullptr; |
| 6515 | auto Body = FD->getBody(Definition); |
| 6516 | |
| 6517 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
| 6518 | return false; |
| 6519 | |
| 6520 | return HandleConstructorCall(E, This, Info.CurrentCall->Arguments, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6521 | cast<CXXConstructorDecl>(Definition), Info, |
| 6522 | Result); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6523 | } |
| 6524 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6525 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
| 6526 | const CXXStdInitializerListExpr *E) { |
| 6527 | const ConstantArrayType *ArrayType = |
| 6528 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
| 6529 | |
| 6530 | LValue Array; |
| 6531 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
| 6532 | return false; |
| 6533 | |
| 6534 | // Get a pointer to the first element of the array. |
| 6535 | Array.addArray(Info, E, ArrayType); |
| 6536 | |
| 6537 | // FIXME: Perform the checks on the field types in SemaInit. |
| 6538 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
| 6539 | RecordDecl::field_iterator Field = Record->field_begin(); |
| 6540 | if (Field == Record->field_end()) |
| 6541 | return Error(E); |
| 6542 | |
| 6543 | // Start pointer. |
| 6544 | if (!Field->getType()->isPointerType() || |
| 6545 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 6546 | ArrayType->getElementType())) |
| 6547 | return Error(E); |
| 6548 | |
| 6549 | // FIXME: What if the initializer_list type has base classes, etc? |
| 6550 | Result = APValue(APValue::UninitStruct(), 0, 2); |
| 6551 | Array.moveInto(Result.getStructField(0)); |
| 6552 | |
| 6553 | if (++Field == Record->field_end()) |
| 6554 | return Error(E); |
| 6555 | |
| 6556 | if (Field->getType()->isPointerType() && |
| 6557 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 6558 | ArrayType->getElementType())) { |
| 6559 | // End pointer. |
| 6560 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
| 6561 | ArrayType->getElementType(), |
| 6562 | ArrayType->getSize().getZExtValue())) |
| 6563 | return false; |
| 6564 | Array.moveInto(Result.getStructField(1)); |
| 6565 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
| 6566 | // Length. |
| 6567 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
| 6568 | else |
| 6569 | return Error(E); |
| 6570 | |
| 6571 | if (++Field != Record->field_end()) |
| 6572 | return Error(E); |
| 6573 | |
| 6574 | return true; |
| 6575 | } |
| 6576 | |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6577 | bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) { |
| 6578 | const CXXRecordDecl *ClosureClass = E->getLambdaClass(); |
| 6579 | if (ClosureClass->isInvalidDecl()) return false; |
| 6580 | |
| 6581 | if (Info.checkingPotentialConstantExpression()) return true; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 6582 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6583 | const size_t NumFields = |
| 6584 | std::distance(ClosureClass->field_begin(), ClosureClass->field_end()); |
Benjamin Kramer | aad1bdc | 2017-02-16 14:08:41 +0000 | [diff] [blame] | 6585 | |
| 6586 | assert(NumFields == (size_t)std::distance(E->capture_init_begin(), |
| 6587 | E->capture_init_end()) && |
| 6588 | "The number of lambda capture initializers should equal the number of " |
| 6589 | "fields within the closure type"); |
| 6590 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6591 | Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields); |
| 6592 | // Iterate through all the lambda's closure object's fields and initialize |
| 6593 | // them. |
| 6594 | auto *CaptureInitIt = E->capture_init_begin(); |
| 6595 | const LambdaCapture *CaptureIt = ClosureClass->captures_begin(); |
| 6596 | bool Success = true; |
| 6597 | for (const auto *Field : ClosureClass->fields()) { |
| 6598 | assert(CaptureInitIt != E->capture_init_end()); |
| 6599 | // Get the initializer for this field |
| 6600 | Expr *const CurFieldInit = *CaptureInitIt++; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 6601 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6602 | // If there is no initializer, either this is a VLA or an error has |
| 6603 | // occurred. |
| 6604 | if (!CurFieldInit) |
| 6605 | return Error(E); |
| 6606 | |
| 6607 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 6608 | if (!EvaluateInPlace(FieldVal, Info, This, CurFieldInit)) { |
| 6609 | if (!Info.keepEvaluatingAfterFailure()) |
| 6610 | return false; |
| 6611 | Success = false; |
| 6612 | } |
| 6613 | ++CaptureIt; |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6614 | } |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6615 | return Success; |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6616 | } |
| 6617 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6618 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 6619 | APValue &Result, EvalInfo &Info) { |
| 6620 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6621 | "can't evaluate expression as a record rvalue"); |
| 6622 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 6623 | } |
| 6624 | |
| 6625 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6626 | // Temporary Evaluation |
| 6627 | // |
| 6628 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 6629 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 6630 | // materialized so that a reference can bind to it. |
| 6631 | //===----------------------------------------------------------------------===// |
| 6632 | namespace { |
| 6633 | class TemporaryExprEvaluator |
| 6634 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 6635 | public: |
| 6636 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 6637 | LValueExprEvaluatorBaseTy(Info, Result, false) {} |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6638 | |
| 6639 | /// Visit an expression which constructs the value of this temporary. |
| 6640 | bool VisitConstructExpr(const Expr *E) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 6641 | APValue &Value = createTemporary(E, false, Result, *Info.CurrentCall); |
| 6642 | return EvaluateInPlace(Value, Info, Result, E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6643 | } |
| 6644 | |
| 6645 | bool VisitCastExpr(const CastExpr *E) { |
| 6646 | switch (E->getCastKind()) { |
| 6647 | default: |
| 6648 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6649 | |
| 6650 | case CK_ConstructorConversion: |
| 6651 | return VisitConstructExpr(E->getSubExpr()); |
| 6652 | } |
| 6653 | } |
| 6654 | bool VisitInitListExpr(const InitListExpr *E) { |
| 6655 | return VisitConstructExpr(E); |
| 6656 | } |
| 6657 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 6658 | return VisitConstructExpr(E); |
| 6659 | } |
| 6660 | bool VisitCallExpr(const CallExpr *E) { |
| 6661 | return VisitConstructExpr(E); |
| 6662 | } |
Richard Smith | 513955c | 2014-12-17 19:24:30 +0000 | [diff] [blame] | 6663 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
| 6664 | return VisitConstructExpr(E); |
| 6665 | } |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6666 | bool VisitLambdaExpr(const LambdaExpr *E) { |
| 6667 | return VisitConstructExpr(E); |
| 6668 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6669 | }; |
| 6670 | } // end anonymous namespace |
| 6671 | |
| 6672 | /// Evaluate an expression of record type as a temporary. |
| 6673 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 6674 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6675 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 6676 | } |
| 6677 | |
| 6678 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6679 | // Vector Evaluation |
| 6680 | //===----------------------------------------------------------------------===// |
| 6681 | |
| 6682 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 6683 | class VectorExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6684 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6685 | APValue &Result; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6686 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6687 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6688 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 6689 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6690 | |
Craig Topper | 9798b93 | 2015-09-29 04:30:05 +0000 | [diff] [blame] | 6691 | bool Success(ArrayRef<APValue> V, const Expr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6692 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 6693 | // FIXME: remove this APValue copy. |
| 6694 | Result = APValue(V.data(), V.size()); |
| 6695 | return true; |
| 6696 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6697 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6698 | assert(V.isVector()); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6699 | Result = V; |
| 6700 | return true; |
| 6701 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6702 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6703 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6704 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6705 | { return Visit(E->getSubExpr()); } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6706 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6707 | bool VisitInitListExpr(const InitListExpr *E); |
| 6708 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6709 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 6710 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6711 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6712 | }; |
| 6713 | } // end anonymous namespace |
| 6714 | |
| 6715 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6716 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6717 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6718 | } |
| 6719 | |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 6720 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6721 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 6722 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6723 | |
Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 6724 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 6725 | QualType SETy = SE->getType(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6726 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6727 | switch (E->getCastKind()) { |
| 6728 | case CK_VectorSplat: { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6729 | APValue Val = APValue(); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6730 | if (SETy->isIntegerType()) { |
| 6731 | APSInt IntResult; |
| 6732 | if (!EvaluateInteger(SE, IntResult, Info)) |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 6733 | return false; |
| 6734 | Val = APValue(std::move(IntResult)); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6735 | } else if (SETy->isRealFloatingType()) { |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 6736 | APFloat FloatResult(0.0); |
| 6737 | if (!EvaluateFloat(SE, FloatResult, Info)) |
| 6738 | return false; |
| 6739 | Val = APValue(std::move(FloatResult)); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6740 | } else { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6741 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6742 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 6743 | |
| 6744 | // Splat and create vector APValue. |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6745 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 6746 | return Success(Elts, E); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 6747 | } |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 6748 | case CK_BitCast: { |
| 6749 | // Evaluate the operand into an APInt we can extract from. |
| 6750 | llvm::APInt SValInt; |
| 6751 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 6752 | return false; |
| 6753 | // Extract the elements |
| 6754 | QualType EltTy = VTy->getElementType(); |
| 6755 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 6756 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 6757 | SmallVector<APValue, 4> Elts; |
| 6758 | if (EltTy->isRealFloatingType()) { |
| 6759 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 6760 | unsigned FloatEltSize = EltSize; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 6761 | if (&Sem == &APFloat::x87DoubleExtended()) |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 6762 | FloatEltSize = 80; |
| 6763 | for (unsigned i = 0; i < NElts; i++) { |
| 6764 | llvm::APInt Elt; |
| 6765 | if (BigEndian) |
| 6766 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 6767 | else |
| 6768 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
Tim Northover | 178723a | 2013-01-22 09:46:51 +0000 | [diff] [blame] | 6769 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 6770 | } |
| 6771 | } else if (EltTy->isIntegerType()) { |
| 6772 | for (unsigned i = 0; i < NElts; i++) { |
| 6773 | llvm::APInt Elt; |
| 6774 | if (BigEndian) |
| 6775 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 6776 | else |
| 6777 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 6778 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 6779 | } |
| 6780 | } else { |
| 6781 | return Error(E); |
| 6782 | } |
| 6783 | return Success(Elts, E); |
| 6784 | } |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6785 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6786 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6787 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6788 | } |
| 6789 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6790 | bool |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6791 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6792 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6793 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6794 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6795 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6796 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6797 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6798 | |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6799 | // The number of initializers can be less than the number of |
| 6800 | // vector elements. For OpenCL, this can be due to nested vector |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 6801 | // initialization. For GCC compatibility, missing trailing elements |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6802 | // should be initialized with zeroes. |
| 6803 | unsigned CountInits = 0, CountElts = 0; |
| 6804 | while (CountElts < NumElements) { |
| 6805 | // Handle nested vector initialization. |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 6806 | if (CountInits < NumInits |
Eli Friedman | 1409e6e | 2013-09-17 04:07:02 +0000 | [diff] [blame] | 6807 | && E->getInit(CountInits)->getType()->isVectorType()) { |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6808 | APValue v; |
| 6809 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 6810 | return Error(E); |
| 6811 | unsigned vlen = v.getVectorLength(); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 6812 | for (unsigned j = 0; j < vlen; j++) |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6813 | Elements.push_back(v.getVectorElt(j)); |
| 6814 | CountElts += vlen; |
| 6815 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6816 | llvm::APSInt sInt(32); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6817 | if (CountInits < NumInits) { |
| 6818 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 6819 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6820 | } else // trailing integer zero. |
| 6821 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 6822 | Elements.push_back(APValue(sInt)); |
| 6823 | CountElts++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6824 | } else { |
| 6825 | llvm::APFloat f(0.0); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6826 | if (CountInits < NumInits) { |
| 6827 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 6828 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6829 | } else // trailing float zero. |
| 6830 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 6831 | Elements.push_back(APValue(f)); |
| 6832 | CountElts++; |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 6833 | } |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6834 | CountInits++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6835 | } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6836 | return Success(Elements, E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6837 | } |
| 6838 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6839 | bool |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6840 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6841 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6842 | QualType EltTy = VT->getElementType(); |
| 6843 | APValue ZeroElement; |
| 6844 | if (EltTy->isIntegerType()) |
| 6845 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 6846 | else |
| 6847 | ZeroElement = |
| 6848 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 6849 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6850 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6851 | return Success(Elements, E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6854 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 6855 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6856 | return ZeroInitialization(E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6857 | } |
| 6858 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6859 | //===----------------------------------------------------------------------===// |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6860 | // Array Evaluation |
| 6861 | //===----------------------------------------------------------------------===// |
| 6862 | |
| 6863 | namespace { |
| 6864 | class ArrayExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6865 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6866 | const LValue &This; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6867 | APValue &Result; |
| 6868 | public: |
| 6869 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6870 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 6871 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6872 | |
| 6873 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 6874 | assert((V.isArray() || V.isLValue()) && |
| 6875 | "expected array or string literal"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6876 | Result = V; |
| 6877 | return true; |
| 6878 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6879 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6880 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6881 | const ConstantArrayType *CAT = |
| 6882 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 6883 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6884 | return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6885 | |
| 6886 | Result = APValue(APValue::UninitArray(), 0, |
| 6887 | CAT->getSize().getZExtValue()); |
| 6888 | if (!Result.hasArrayFiller()) return true; |
| 6889 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6890 | // Zero-initialize all elements. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6891 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6892 | Subobject.addArray(Info, E, CAT); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6893 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6894 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6895 | } |
| 6896 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6897 | bool VisitCallExpr(const CallExpr *E) { |
| 6898 | return handleCallExpr(E, Result, &This); |
| 6899 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6900 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6901 | bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6902 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6903 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 6904 | const LValue &Subobject, |
| 6905 | APValue *Value, QualType Type); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6906 | }; |
| 6907 | } // end anonymous namespace |
| 6908 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6909 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 6910 | APValue &Result, EvalInfo &Info) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6911 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6912 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6913 | } |
| 6914 | |
Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 6915 | // Return true iff the given array filler may depend on the element index. |
| 6916 | static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { |
| 6917 | // For now, just whitelist non-class value-initialization and initialization |
| 6918 | // lists comprised of them. |
| 6919 | if (isa<ImplicitValueInitExpr>(FillerExpr)) |
| 6920 | return false; |
| 6921 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(FillerExpr)) { |
| 6922 | for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { |
| 6923 | if (MaybeElementDependentArrayFiller(ILE->getInit(I))) |
| 6924 | return true; |
| 6925 | } |
| 6926 | return false; |
| 6927 | } |
| 6928 | return true; |
| 6929 | } |
| 6930 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6931 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 6932 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 6933 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6934 | return Error(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6935 | |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 6936 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 6937 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 6938 | if (E->isStringLiteralInit()) { |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 6939 | LValue LV; |
| 6940 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 6941 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6942 | APValue Val; |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 6943 | LV.moveInto(Val); |
| 6944 | return Success(Val, E); |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6947 | bool Success = true; |
| 6948 | |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 6949 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
| 6950 | "zero-initialized array shouldn't have any initialized elts"); |
| 6951 | APValue Filler; |
| 6952 | if (Result.isArray() && Result.hasArrayFiller()) |
| 6953 | Filler = Result.getArrayFiller(); |
| 6954 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6955 | unsigned NumEltsToInit = E->getNumInits(); |
| 6956 | unsigned NumElts = CAT->getSize().getZExtValue(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6957 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : nullptr; |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6958 | |
| 6959 | // 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] | 6960 | // array element. |
| 6961 | if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr)) |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6962 | NumEltsToInit = NumElts; |
| 6963 | |
Nicola Zaghen | 3538b39 | 2018-05-15 13:30:56 +0000 | [diff] [blame] | 6964 | LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: " |
| 6965 | << NumEltsToInit << ".\n"); |
Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 6966 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6967 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 6968 | |
| 6969 | // If the array was previously zero-initialized, preserve the |
| 6970 | // zero-initialized values. |
| 6971 | if (!Filler.isUninit()) { |
| 6972 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
| 6973 | Result.getArrayInitializedElt(I) = Filler; |
| 6974 | if (Result.hasArrayFiller()) |
| 6975 | Result.getArrayFiller() = Filler; |
| 6976 | } |
| 6977 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6978 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6979 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6980 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
| 6981 | const Expr *Init = |
| 6982 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6983 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6984 | Info, Subobject, Init) || |
| 6985 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6986 | CAT->getElementType(), 1)) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 6987 | if (!Info.noteFailure()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6988 | return false; |
| 6989 | Success = false; |
| 6990 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6991 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6992 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6993 | if (!Result.hasArrayFiller()) |
| 6994 | return Success; |
| 6995 | |
| 6996 | // If we get here, we have a trivial filler, which we can just evaluate |
| 6997 | // once and splat over the rest of the array elements. |
| 6998 | assert(FillerExpr && "no array filler for incomplete init list"); |
| 6999 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
| 7000 | FillerExpr) && Success; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7001 | } |
| 7002 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7003 | bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
| 7004 | if (E->getCommonExpr() && |
| 7005 | !Evaluate(Info.CurrentCall->createTemporary(E->getCommonExpr(), false), |
| 7006 | Info, E->getCommonExpr()->getSourceExpr())) |
| 7007 | return false; |
| 7008 | |
| 7009 | auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe()); |
| 7010 | |
| 7011 | uint64_t Elements = CAT->getSize().getZExtValue(); |
| 7012 | Result = APValue(APValue::UninitArray(), Elements, Elements); |
| 7013 | |
| 7014 | LValue Subobject = This; |
| 7015 | Subobject.addArray(Info, E, CAT); |
| 7016 | |
| 7017 | bool Success = true; |
| 7018 | for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) { |
| 7019 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
| 7020 | Info, Subobject, E->getSubExpr()) || |
| 7021 | !HandleLValueArrayAdjustment(Info, E, Subobject, |
| 7022 | CAT->getElementType(), 1)) { |
| 7023 | if (!Info.noteFailure()) |
| 7024 | return false; |
| 7025 | Success = false; |
| 7026 | } |
| 7027 | } |
| 7028 | |
| 7029 | return Success; |
| 7030 | } |
| 7031 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7032 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7033 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
| 7034 | } |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 7035 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7036 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 7037 | const LValue &Subobject, |
| 7038 | APValue *Value, |
| 7039 | QualType Type) { |
| 7040 | bool HadZeroInit = !Value->isUninit(); |
| 7041 | |
| 7042 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
| 7043 | unsigned N = CAT->getSize().getZExtValue(); |
| 7044 | |
| 7045 | // Preserve the array filler if we had prior zero-initialization. |
| 7046 | APValue Filler = |
| 7047 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
| 7048 | : APValue(); |
| 7049 | |
| 7050 | *Value = APValue(APValue::UninitArray(), N, N); |
| 7051 | |
| 7052 | if (HadZeroInit) |
| 7053 | for (unsigned I = 0; I != N; ++I) |
| 7054 | Value->getArrayInitializedElt(I) = Filler; |
| 7055 | |
| 7056 | // Initialize the elements. |
| 7057 | LValue ArrayElt = Subobject; |
| 7058 | ArrayElt.addArray(Info, E, CAT); |
| 7059 | for (unsigned I = 0; I != N; ++I) |
| 7060 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
| 7061 | CAT->getElementType()) || |
| 7062 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
| 7063 | CAT->getElementType(), 1)) |
| 7064 | return false; |
| 7065 | |
| 7066 | return true; |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 7067 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7068 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7069 | if (!Type->isRecordType()) |
Richard Smith | 9fce7bc | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 7070 | return Error(E); |
| 7071 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 7072 | return RecordExprEvaluator(Info, Subobject, *Value) |
| 7073 | .VisitCXXConstructExpr(E, Type); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7074 | } |
| 7075 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7076 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7077 | // Integer Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7078 | // |
| 7079 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 7080 | // types and back in constant folding. Integer values are thus represented |
| 7081 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7082 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7083 | |
| 7084 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7085 | class IntExprEvaluator |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 7086 | : public ExprEvaluatorBase<IntExprEvaluator> { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7087 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 7088 | public: |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7089 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 7090 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7091 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7092 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7093 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 7094 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7095 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7096 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7097 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7098 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7099 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7100 | return true; |
| 7101 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7102 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 7103 | return Success(SI, E, Result); |
| 7104 | } |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7105 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7106 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 7107 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 7108 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7109 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7110 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7111 | Result = APValue(APSInt(I)); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 7112 | Result.getInt().setIsUnsigned( |
| 7113 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7114 | return true; |
| 7115 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7116 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 7117 | return Success(I, E, Result); |
| 7118 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7119 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7120 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 7121 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 7122 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7123 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7124 | return true; |
| 7125 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7126 | bool Success(uint64_t Value, const Expr *E) { |
| 7127 | return Success(Value, E, Result); |
| 7128 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7129 | |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 7130 | bool Success(CharUnits Size, const Expr *E) { |
| 7131 | return Success(Size.getQuantity(), E); |
| 7132 | } |
| 7133 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7134 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 7135 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 9c8d1c5 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 7136 | Result = V; |
| 7137 | return true; |
| 7138 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7139 | return Success(V.getInt(), E); |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 7140 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7141 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7142 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7143 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7144 | //===--------------------------------------------------------------------===// |
| 7145 | // Visitor Methods |
| 7146 | //===--------------------------------------------------------------------===// |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 7147 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7148 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7149 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7150 | } |
| 7151 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7152 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7153 | } |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7154 | |
| 7155 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 7156 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7157 | if (CheckReferencedDecl(E, E->getDecl())) |
| 7158 | return true; |
| 7159 | |
| 7160 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7161 | } |
| 7162 | bool VisitMemberExpr(const MemberExpr *E) { |
| 7163 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 7164 | VisitIgnoredBaseExpression(E->getBase()); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7165 | return true; |
| 7166 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7167 | |
| 7168 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7169 | } |
| 7170 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7171 | bool VisitCallExpr(const CallExpr *E); |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 7172 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7173 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7174 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7175 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 7176 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7177 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7178 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 7179 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7180 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7181 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7182 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7183 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 7184 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 7185 | return Success(E->getValue(), E); |
| 7186 | } |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7187 | |
| 7188 | bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
| 7189 | if (Info.ArrayInitIndex == uint64_t(-1)) { |
| 7190 | // We were asked to evaluate this subexpression independent of the |
| 7191 | // enclosing ArrayInitLoopExpr. We can't do that. |
| 7192 | Info.FFDiag(E); |
| 7193 | return false; |
| 7194 | } |
| 7195 | return Success(Info.ArrayInitIndex, E); |
| 7196 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 7197 | |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7198 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 7199 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7200 | return ZeroInitialization(E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7201 | } |
| 7202 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 7203 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 7204 | return Success(E->getValue(), E); |
| 7205 | } |
| 7206 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 7207 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 7208 | return Success(E->getValue(), E); |
| 7209 | } |
| 7210 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 7211 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 7212 | return Success(E->getValue(), E); |
| 7213 | } |
| 7214 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7215 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7216 | bool VisitUnaryImag(const UnaryOperator *E); |
| 7217 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 7218 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 7219 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 7220 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7221 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7222 | }; |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 7223 | |
| 7224 | class FixedPointExprEvaluator |
| 7225 | : public ExprEvaluatorBase<FixedPointExprEvaluator> { |
| 7226 | APValue &Result; |
| 7227 | |
| 7228 | public: |
| 7229 | FixedPointExprEvaluator(EvalInfo &info, APValue &result) |
| 7230 | : ExprEvaluatorBaseTy(info), Result(result) {} |
| 7231 | |
| 7232 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
| 7233 | assert(E->getType()->isFixedPointType() && "Invalid evaluation result."); |
| 7234 | assert(SI.isSigned() == E->getType()->isSignedFixedPointType() && |
| 7235 | "Invalid evaluation result."); |
| 7236 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
| 7237 | "Invalid evaluation result."); |
| 7238 | Result = APValue(SI); |
| 7239 | return true; |
| 7240 | } |
| 7241 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 7242 | return Success(SI, E, Result); |
| 7243 | } |
| 7244 | |
| 7245 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
| 7246 | assert(E->getType()->isFixedPointType() && "Invalid evaluation result."); |
| 7247 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
| 7248 | "Invalid evaluation result."); |
| 7249 | Result = APValue(APSInt(I)); |
| 7250 | Result.getInt().setIsUnsigned(E->getType()->isUnsignedFixedPointType()); |
| 7251 | return true; |
| 7252 | } |
| 7253 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 7254 | return Success(I, E, Result); |
| 7255 | } |
| 7256 | |
| 7257 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 7258 | assert(E->getType()->isFixedPointType() && "Invalid evaluation result."); |
| 7259 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
| 7260 | return true; |
| 7261 | } |
| 7262 | bool Success(uint64_t Value, const Expr *E) { |
| 7263 | return Success(Value, E, Result); |
| 7264 | } |
| 7265 | |
| 7266 | bool Success(CharUnits Size, const Expr *E) { |
| 7267 | return Success(Size.getQuantity(), E); |
| 7268 | } |
| 7269 | |
| 7270 | bool Success(const APValue &V, const Expr *E) { |
| 7271 | if (V.isLValue() || V.isAddrLabelDiff()) { |
| 7272 | Result = V; |
| 7273 | return true; |
| 7274 | } |
| 7275 | return Success(V.getInt(), E); |
| 7276 | } |
| 7277 | |
| 7278 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
| 7279 | |
| 7280 | //===--------------------------------------------------------------------===// |
| 7281 | // Visitor Methods |
| 7282 | //===--------------------------------------------------------------------===// |
| 7283 | |
| 7284 | bool VisitFixedPointLiteral(const FixedPointLiteral *E) { |
| 7285 | return Success(E->getValue(), E); |
| 7286 | } |
| 7287 | |
| 7288 | bool VisitUnaryOperator(const UnaryOperator *E); |
| 7289 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7290 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7291 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7292 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 7293 | /// produce either the integer value or a pointer. |
| 7294 | /// |
| 7295 | /// GCC has a heinous extension which folds casts between pointer types and |
| 7296 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 7297 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 7298 | /// Some simple arithmetic on such values is supported (they are treated much |
| 7299 | /// like char*). |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7300 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 7301 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7302 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7303 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 7304 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7305 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7306 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7307 | APValue Val; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7308 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 7309 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7310 | if (!Val.isInt()) { |
| 7311 | // FIXME: It would be better to produce the diagnostic for casting |
| 7312 | // a pointer to an integer. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 7313 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7314 | return false; |
| 7315 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7316 | Result = Val.getInt(); |
| 7317 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7318 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7319 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7320 | /// Check whether the given declaration can be directly converted to an integral |
| 7321 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 7322 | /// try. |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7323 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7324 | // Enums are integer constant exprs. |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 7325 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7326 | // Check for signedness/width mismatches between E type and ECD value. |
| 7327 | bool SameSign = (ECD->getInitVal().isSigned() |
| 7328 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 7329 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 7330 | == Info.Ctx.getIntWidth(E->getType())); |
| 7331 | if (SameSign && SameWidth) |
| 7332 | return Success(ECD->getInitVal(), E); |
| 7333 | else { |
| 7334 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 7335 | // by computing a new value matching the type of E. |
| 7336 | llvm::APSInt Val = ECD->getInitVal(); |
| 7337 | if (!SameSign) |
| 7338 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 7339 | if (!SameWidth) |
| 7340 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 7341 | return Success(Val, E); |
| 7342 | } |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 7343 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7344 | return false; |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7345 | } |
| 7346 | |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7347 | /// Values returned by __builtin_classify_type, chosen to match the values |
| 7348 | /// produced by GCC's builtin. |
| 7349 | enum class GCCTypeClass { |
| 7350 | None = -1, |
| 7351 | Void = 0, |
| 7352 | Integer = 1, |
| 7353 | // GCC reserves 2 for character types, but instead classifies them as |
| 7354 | // integers. |
| 7355 | Enum = 3, |
| 7356 | Bool = 4, |
| 7357 | Pointer = 5, |
| 7358 | // GCC reserves 6 for references, but appears to never use it (because |
| 7359 | // expressions never have reference type, presumably). |
| 7360 | PointerToDataMember = 7, |
| 7361 | RealFloat = 8, |
| 7362 | Complex = 9, |
| 7363 | // GCC reserves 10 for functions, but does not use it since GCC version 6 due |
| 7364 | // to decay to pointer. (Prior to version 6 it was only used in C++ mode). |
| 7365 | // GCC claims to reserve 11 for pointers to member functions, but *actually* |
| 7366 | // uses 12 for that purpose, same as for a class or struct. Maybe it |
| 7367 | // internally implements a pointer to member as a struct? Who knows. |
| 7368 | PointerToMemberFunction = 12, // Not a bug, see above. |
| 7369 | ClassOrStruct = 12, |
| 7370 | Union = 13, |
| 7371 | // GCC reserves 14 for arrays, but does not use it since GCC version 6 due to |
| 7372 | // decay to pointer. (Prior to version 6 it was only used in C++ mode). |
| 7373 | // GCC reserves 15 for strings, but actually uses 5 (pointer) for string |
| 7374 | // literals. |
| 7375 | }; |
| 7376 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7377 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 7378 | /// as GCC. |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7379 | static GCCTypeClass |
| 7380 | EvaluateBuiltinClassifyType(QualType T, const LangOptions &LangOpts) { |
| 7381 | assert(!T->isDependentType() && "unexpected dependent type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7382 | |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7383 | QualType CanTy = T.getCanonicalType(); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7384 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanTy); |
| 7385 | |
| 7386 | switch (CanTy->getTypeClass()) { |
| 7387 | #define TYPE(ID, BASE) |
| 7388 | #define DEPENDENT_TYPE(ID, BASE) case Type::ID: |
| 7389 | #define NON_CANONICAL_TYPE(ID, BASE) case Type::ID: |
| 7390 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID: |
| 7391 | #include "clang/AST/TypeNodes.def" |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7392 | case Type::Auto: |
| 7393 | case Type::DeducedTemplateSpecialization: |
| 7394 | llvm_unreachable("unexpected non-canonical or dependent type"); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7395 | |
| 7396 | case Type::Builtin: |
| 7397 | switch (BT->getKind()) { |
| 7398 | #define BUILTIN_TYPE(ID, SINGLETON_ID) |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7399 | #define SIGNED_TYPE(ID, SINGLETON_ID) \ |
| 7400 | case BuiltinType::ID: return GCCTypeClass::Integer; |
| 7401 | #define FLOATING_TYPE(ID, SINGLETON_ID) \ |
| 7402 | case BuiltinType::ID: return GCCTypeClass::RealFloat; |
| 7403 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \ |
| 7404 | case BuiltinType::ID: break; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7405 | #include "clang/AST/BuiltinTypes.def" |
| 7406 | case BuiltinType::Void: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7407 | return GCCTypeClass::Void; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7408 | |
| 7409 | case BuiltinType::Bool: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7410 | return GCCTypeClass::Bool; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7411 | |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7412 | case BuiltinType::Char_U: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7413 | case BuiltinType::UChar: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7414 | case BuiltinType::WChar_U: |
| 7415 | case BuiltinType::Char8: |
| 7416 | case BuiltinType::Char16: |
| 7417 | case BuiltinType::Char32: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7418 | case BuiltinType::UShort: |
| 7419 | case BuiltinType::UInt: |
| 7420 | case BuiltinType::ULong: |
| 7421 | case BuiltinType::ULongLong: |
| 7422 | case BuiltinType::UInt128: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7423 | return GCCTypeClass::Integer; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7424 | |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 7425 | case BuiltinType::UShortAccum: |
| 7426 | case BuiltinType::UAccum: |
| 7427 | case BuiltinType::ULongAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 7428 | case BuiltinType::UShortFract: |
| 7429 | case BuiltinType::UFract: |
| 7430 | case BuiltinType::ULongFract: |
| 7431 | case BuiltinType::SatUShortAccum: |
| 7432 | case BuiltinType::SatUAccum: |
| 7433 | case BuiltinType::SatULongAccum: |
| 7434 | case BuiltinType::SatUShortFract: |
| 7435 | case BuiltinType::SatUFract: |
| 7436 | case BuiltinType::SatULongFract: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 7437 | return GCCTypeClass::None; |
| 7438 | |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7439 | case BuiltinType::NullPtr: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7440 | |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7441 | case BuiltinType::ObjCId: |
| 7442 | case BuiltinType::ObjCClass: |
| 7443 | case BuiltinType::ObjCSel: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 7444 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 7445 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 7446 | #include "clang/Basic/OpenCLImageTypes.def" |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7447 | case BuiltinType::OCLSampler: |
| 7448 | case BuiltinType::OCLEvent: |
| 7449 | case BuiltinType::OCLClkEvent: |
| 7450 | case BuiltinType::OCLQueue: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7451 | case BuiltinType::OCLReserveID: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7452 | return GCCTypeClass::None; |
| 7453 | |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7454 | case BuiltinType::Dependent: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7455 | llvm_unreachable("unexpected dependent type"); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7456 | }; |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7457 | llvm_unreachable("unexpected placeholder type"); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7458 | |
| 7459 | case Type::Enum: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7460 | return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7461 | |
| 7462 | case Type::Pointer: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7463 | case Type::ConstantArray: |
| 7464 | case Type::VariableArray: |
| 7465 | case Type::IncompleteArray: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7466 | case Type::FunctionNoProto: |
| 7467 | case Type::FunctionProto: |
| 7468 | return GCCTypeClass::Pointer; |
| 7469 | |
| 7470 | case Type::MemberPointer: |
| 7471 | return CanTy->isMemberDataPointerType() |
| 7472 | ? GCCTypeClass::PointerToDataMember |
| 7473 | : GCCTypeClass::PointerToMemberFunction; |
| 7474 | |
| 7475 | case Type::Complex: |
| 7476 | return GCCTypeClass::Complex; |
| 7477 | |
| 7478 | case Type::Record: |
| 7479 | return CanTy->isUnionType() ? GCCTypeClass::Union |
| 7480 | : GCCTypeClass::ClassOrStruct; |
| 7481 | |
| 7482 | case Type::Atomic: |
| 7483 | // GCC classifies _Atomic T the same as T. |
| 7484 | return EvaluateBuiltinClassifyType( |
| 7485 | CanTy->castAs<AtomicType>()->getValueType(), LangOpts); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7486 | |
| 7487 | case Type::BlockPointer: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7488 | case Type::Vector: |
| 7489 | case Type::ExtVector: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7490 | case Type::ObjCObject: |
| 7491 | case Type::ObjCInterface: |
| 7492 | case Type::ObjCObjectPointer: |
| 7493 | case Type::Pipe: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7494 | // GCC classifies vectors as None. We follow its lead and classify all |
| 7495 | // other types that don't fit into the regular classification the same way. |
| 7496 | return GCCTypeClass::None; |
| 7497 | |
| 7498 | case Type::LValueReference: |
| 7499 | case Type::RValueReference: |
| 7500 | llvm_unreachable("invalid type for expression"); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7501 | } |
| 7502 | |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7503 | llvm_unreachable("unexpected type class"); |
| 7504 | } |
| 7505 | |
| 7506 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 7507 | /// as GCC. |
| 7508 | static GCCTypeClass |
| 7509 | EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) { |
| 7510 | // If no argument was supplied, default to None. This isn't |
| 7511 | // ideal, however it is what gcc does. |
| 7512 | if (E->getNumArgs() == 0) |
| 7513 | return GCCTypeClass::None; |
| 7514 | |
| 7515 | // FIXME: Bizarrely, GCC treats a call with more than one argument as not |
| 7516 | // being an ICE, but still folds it to a constant using the type of the first |
| 7517 | // argument. |
| 7518 | return EvaluateBuiltinClassifyType(E->getArg(0)->getType(), LangOpts); |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7519 | } |
| 7520 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7521 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 7522 | /// __builtin_constant_p when applied to the given lvalue. |
| 7523 | /// |
| 7524 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 7525 | /// character of a string literal. |
| 7526 | template<typename LValue> |
| 7527 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
Douglas Gregor | f31cee6 | 2012-03-11 02:23:56 +0000 | [diff] [blame] | 7528 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7529 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 7530 | } |
| 7531 | |
| 7532 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 7533 | /// GCC as we can manage. |
| 7534 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 7535 | QualType ArgType = Arg->getType(); |
| 7536 | |
| 7537 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 7538 | // are not precisely documented, but are as follows: |
| 7539 | // |
| 7540 | // - If the operand is of integral, floating, complex or enumeration type, |
| 7541 | // and can be folded to a known value of that type, it returns 1. |
| 7542 | // - If the operand and can be folded to a pointer to the first character |
| 7543 | // of a string literal (or such a pointer cast to an integral type), it |
| 7544 | // returns 1. |
| 7545 | // |
| 7546 | // Otherwise, it returns 0. |
| 7547 | // |
| 7548 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 7549 | // its support for this does not currently work. |
| 7550 | if (ArgType->isIntegralOrEnumerationType()) { |
| 7551 | Expr::EvalResult Result; |
| 7552 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 7553 | return false; |
| 7554 | |
| 7555 | APValue &V = Result.Val; |
| 7556 | if (V.getKind() == APValue::Int) |
| 7557 | return true; |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 7558 | if (V.getKind() == APValue::LValue) |
| 7559 | return EvaluateBuiltinConstantPForLValue(V); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7560 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 7561 | return Arg->isEvaluatable(Ctx); |
| 7562 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 7563 | LValue LV; |
| 7564 | Expr::EvalStatus Status; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 7565 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7566 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 7567 | : EvaluatePointer(Arg, LV, Info)) && |
| 7568 | !Status.HasSideEffects) |
| 7569 | return EvaluateBuiltinConstantPForLValue(LV); |
| 7570 | } |
| 7571 | |
| 7572 | // Anything else isn't considered to be sufficiently constant. |
| 7573 | return false; |
| 7574 | } |
| 7575 | |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7576 | /// Retrieves the "underlying object type" of the given expression, |
| 7577 | /// as used by __builtin_object_size. |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7578 | static QualType getObjectType(APValue::LValueBase B) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7579 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 7580 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7581 | return VD->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7582 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 7583 | if (isa<CompoundLiteralExpr>(E)) |
| 7584 | return E->getType(); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7585 | } |
| 7586 | |
| 7587 | return QualType(); |
| 7588 | } |
| 7589 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7590 | /// A more selective version of E->IgnoreParenCasts for |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7591 | /// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only |
George Burgess IV | b40cd56 | 2015-09-04 22:36:18 +0000 | [diff] [blame] | 7592 | /// to change the type of E. |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7593 | /// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo` |
| 7594 | /// |
| 7595 | /// Always returns an RValue with a pointer representation. |
| 7596 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
| 7597 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
| 7598 | |
| 7599 | auto *NoParens = E->IgnoreParens(); |
| 7600 | auto *Cast = dyn_cast<CastExpr>(NoParens); |
George Burgess IV | b40cd56 | 2015-09-04 22:36:18 +0000 | [diff] [blame] | 7601 | if (Cast == nullptr) |
| 7602 | return NoParens; |
| 7603 | |
| 7604 | // We only conservatively allow a few kinds of casts, because this code is |
| 7605 | // inherently a simple solution that seeks to support the common case. |
| 7606 | auto CastKind = Cast->getCastKind(); |
| 7607 | if (CastKind != CK_NoOp && CastKind != CK_BitCast && |
| 7608 | CastKind != CK_AddressSpaceConversion) |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7609 | return NoParens; |
| 7610 | |
| 7611 | auto *SubExpr = Cast->getSubExpr(); |
| 7612 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isRValue()) |
| 7613 | return NoParens; |
| 7614 | return ignorePointerCastsAndParens(SubExpr); |
| 7615 | } |
| 7616 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7617 | /// Checks to see if the given LValue's Designator is at the end of the LValue's |
| 7618 | /// record layout. e.g. |
| 7619 | /// struct { struct { int a, b; } fst, snd; } obj; |
| 7620 | /// obj.fst // no |
| 7621 | /// obj.snd // yes |
| 7622 | /// obj.fst.a // no |
| 7623 | /// obj.fst.b // no |
| 7624 | /// obj.snd.a // no |
| 7625 | /// obj.snd.b // yes |
| 7626 | /// |
| 7627 | /// Please note: this function is specialized for how __builtin_object_size |
| 7628 | /// views "objects". |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7629 | /// |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 7630 | /// If this encounters an invalid RecordDecl or otherwise cannot determine the |
| 7631 | /// correct result, it will always return true. |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7632 | static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) { |
| 7633 | assert(!LVal.Designator.Invalid); |
| 7634 | |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7635 | auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD, bool &Invalid) { |
| 7636 | const RecordDecl *Parent = FD->getParent(); |
| 7637 | Invalid = Parent->isInvalidDecl(); |
| 7638 | if (Invalid || Parent->isUnion()) |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7639 | return true; |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7640 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Parent); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7641 | return FD->getFieldIndex() + 1 == Layout.getFieldCount(); |
| 7642 | }; |
| 7643 | |
| 7644 | auto &Base = LVal.getLValueBase(); |
| 7645 | if (auto *ME = dyn_cast_or_null<MemberExpr>(Base.dyn_cast<const Expr *>())) { |
| 7646 | if (auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7647 | bool Invalid; |
| 7648 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
| 7649 | return Invalid; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7650 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(ME->getMemberDecl())) { |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7651 | for (auto *FD : IFD->chain()) { |
| 7652 | bool Invalid; |
| 7653 | if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(FD), Invalid)) |
| 7654 | return Invalid; |
| 7655 | } |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7656 | } |
| 7657 | } |
| 7658 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7659 | unsigned I = 0; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7660 | QualType BaseType = getType(Base); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 7661 | if (LVal.Designator.FirstEntryIsAnUnsizedArray) { |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 7662 | // If we don't know the array bound, conservatively assume we're looking at |
| 7663 | // the final array element. |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7664 | ++I; |
Alex Lorenz | 4e24648 | 2017-12-20 21:03:38 +0000 | [diff] [blame] | 7665 | if (BaseType->isIncompleteArrayType()) |
| 7666 | BaseType = Ctx.getAsArrayType(BaseType)->getElementType(); |
| 7667 | else |
| 7668 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7669 | } |
| 7670 | |
| 7671 | for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) { |
| 7672 | const auto &Entry = LVal.Designator.Entries[I]; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7673 | if (BaseType->isArrayType()) { |
| 7674 | // Because __builtin_object_size treats arrays as objects, we can ignore |
| 7675 | // the index iff this is the last array in the Designator. |
| 7676 | if (I + 1 == E) |
| 7677 | return true; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7678 | const auto *CAT = cast<ConstantArrayType>(Ctx.getAsArrayType(BaseType)); |
| 7679 | uint64_t Index = Entry.ArrayIndex; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7680 | if (Index + 1 != CAT->getSize()) |
| 7681 | return false; |
| 7682 | BaseType = CAT->getElementType(); |
| 7683 | } else if (BaseType->isAnyComplexType()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7684 | const auto *CT = BaseType->castAs<ComplexType>(); |
| 7685 | uint64_t Index = Entry.ArrayIndex; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7686 | if (Index != 1) |
| 7687 | return false; |
| 7688 | BaseType = CT->getElementType(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7689 | } else if (auto *FD = getAsField(Entry)) { |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7690 | bool Invalid; |
| 7691 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
| 7692 | return Invalid; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7693 | BaseType = FD->getType(); |
| 7694 | } else { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7695 | assert(getAsBaseClass(Entry) && "Expecting cast to a base class"); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7696 | return false; |
| 7697 | } |
| 7698 | } |
| 7699 | return true; |
| 7700 | } |
| 7701 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7702 | /// Tests to see if the LValue has a user-specified designator (that isn't |
| 7703 | /// necessarily valid). Note that this always returns 'true' if the LValue has |
| 7704 | /// an unsized array as its first designator entry, because there's currently no |
| 7705 | /// way to tell if the user typed *foo or foo[0]. |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7706 | static bool refersToCompleteObject(const LValue &LVal) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7707 | if (LVal.Designator.Invalid) |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7708 | return false; |
| 7709 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7710 | if (!LVal.Designator.Entries.empty()) |
| 7711 | return LVal.Designator.isMostDerivedAnUnsizedArray(); |
| 7712 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7713 | if (!LVal.InvalidBase) |
| 7714 | return true; |
| 7715 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7716 | // If `E` is a MemberExpr, then the first part of the designator is hiding in |
| 7717 | // the LValueBase. |
| 7718 | const auto *E = LVal.Base.dyn_cast<const Expr *>(); |
| 7719 | return !E || !isa<MemberExpr>(E); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7720 | } |
| 7721 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7722 | /// Attempts to detect a user writing into a piece of memory that's impossible |
| 7723 | /// to figure out the size of by just using types. |
| 7724 | static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) { |
| 7725 | const SubobjectDesignator &Designator = LVal.Designator; |
| 7726 | // Notes: |
| 7727 | // - Users can only write off of the end when we have an invalid base. Invalid |
| 7728 | // bases imply we don't know where the memory came from. |
| 7729 | // - We used to be a bit more aggressive here; we'd only be conservative if |
| 7730 | // the array at the end was flexible, or if it had 0 or 1 elements. This |
| 7731 | // broke some common standard library extensions (PR30346), but was |
| 7732 | // otherwise seemingly fine. It may be useful to reintroduce this behavior |
| 7733 | // with some sort of whitelist. OTOH, it seems that GCC is always |
| 7734 | // conservative with the last element in structs (if it's an array), so our |
| 7735 | // current behavior is more compatible than a whitelisting approach would |
| 7736 | // be. |
| 7737 | return LVal.InvalidBase && |
| 7738 | Designator.Entries.size() == Designator.MostDerivedPathLength && |
| 7739 | Designator.MostDerivedIsArrayElement && |
| 7740 | isDesignatorAtObjectEnd(Ctx, LVal); |
| 7741 | } |
| 7742 | |
| 7743 | /// Converts the given APInt to CharUnits, assuming the APInt is unsigned. |
| 7744 | /// Fails if the conversion would cause loss of precision. |
| 7745 | static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int, |
| 7746 | CharUnits &Result) { |
| 7747 | auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max(); |
| 7748 | if (Int.ugt(CharUnitsMax)) |
| 7749 | return false; |
| 7750 | Result = CharUnits::fromQuantity(Int.getZExtValue()); |
| 7751 | return true; |
| 7752 | } |
| 7753 | |
| 7754 | /// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will |
| 7755 | /// determine how many bytes exist from the beginning of the object to either |
| 7756 | /// the end of the current subobject, or the end of the object itself, depending |
| 7757 | /// on what the LValue looks like + the value of Type. |
George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 7758 | /// |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7759 | /// If this returns false, the value of Result is undefined. |
| 7760 | static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc, |
| 7761 | unsigned Type, const LValue &LVal, |
| 7762 | CharUnits &EndOffset) { |
| 7763 | bool DetermineForCompleteObject = refersToCompleteObject(LVal); |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 7764 | |
George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 7765 | auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { |
| 7766 | if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType()) |
| 7767 | return false; |
| 7768 | return HandleSizeof(Info, ExprLoc, Ty, Result); |
| 7769 | }; |
| 7770 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7771 | // We want to evaluate the size of the entire object. This is a valid fallback |
| 7772 | // for when Type=1 and the designator is invalid, because we're asked for an |
| 7773 | // upper-bound. |
| 7774 | if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) { |
| 7775 | // Type=3 wants a lower bound, so we can't fall back to this. |
| 7776 | if (Type == 3 && !DetermineForCompleteObject) |
George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 7777 | return false; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7778 | |
| 7779 | llvm::APInt APEndOffset; |
| 7780 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 7781 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
| 7782 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
| 7783 | |
| 7784 | if (LVal.InvalidBase) |
| 7785 | return false; |
| 7786 | |
| 7787 | QualType BaseTy = getObjectType(LVal.getLValueBase()); |
George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 7788 | return CheckedHandleSizeof(BaseTy, EndOffset); |
George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 7789 | } |
| 7790 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7791 | // We want to evaluate the size of a subobject. |
| 7792 | const SubobjectDesignator &Designator = LVal.Designator; |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 7793 | |
| 7794 | // The following is a moderately common idiom in C: |
| 7795 | // |
| 7796 | // struct Foo { int a; char c[1]; }; |
| 7797 | // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar)); |
| 7798 | // strcpy(&F->c[0], Bar); |
| 7799 | // |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7800 | // In order to not break too much legacy code, we need to support it. |
| 7801 | if (isUserWritingOffTheEnd(Info.Ctx, LVal)) { |
| 7802 | // If we can resolve this to an alloc_size call, we can hand that back, |
| 7803 | // because we know for certain how many bytes there are to write to. |
| 7804 | llvm::APInt APEndOffset; |
| 7805 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 7806 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
| 7807 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
| 7808 | |
| 7809 | // If we cannot determine the size of the initial allocation, then we can't |
| 7810 | // given an accurate upper-bound. However, we are still able to give |
| 7811 | // conservative lower-bounds for Type=3. |
| 7812 | if (Type == 1) |
| 7813 | return false; |
| 7814 | } |
| 7815 | |
| 7816 | CharUnits BytesPerElem; |
George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 7817 | if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem)) |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 7818 | return false; |
| 7819 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7820 | // According to the GCC documentation, we want the size of the subobject |
| 7821 | // denoted by the pointer. But that's not quite right -- what we actually |
| 7822 | // want is the size of the immediately-enclosing array, if there is one. |
| 7823 | int64_t ElemsRemaining; |
| 7824 | if (Designator.MostDerivedIsArrayElement && |
| 7825 | Designator.Entries.size() == Designator.MostDerivedPathLength) { |
| 7826 | uint64_t ArraySize = Designator.getMostDerivedArraySize(); |
| 7827 | uint64_t ArrayIndex = Designator.Entries.back().ArrayIndex; |
| 7828 | ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex; |
| 7829 | } else { |
| 7830 | ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1; |
| 7831 | } |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 7832 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7833 | EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining; |
| 7834 | return true; |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 7835 | } |
| 7836 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7837 | /// 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] | 7838 | /// returns true and stores the result in @p Size. |
| 7839 | /// |
| 7840 | /// If @p WasError is non-null, this will report whether the failure to evaluate |
| 7841 | /// is to be treated as an Error in IntExprEvaluator. |
| 7842 | static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, |
| 7843 | EvalInfo &Info, uint64_t &Size) { |
| 7844 | // Determine the denoted object. |
| 7845 | LValue LVal; |
| 7846 | { |
| 7847 | // The operand of __builtin_object_size is never evaluated for side-effects. |
| 7848 | // If there are any, but we can determine the pointed-to object anyway, then |
| 7849 | // ignore the side-effects. |
| 7850 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
| 7851 | FoldOffsetRAII Fold(Info); |
| 7852 | |
| 7853 | if (E->isGLValue()) { |
| 7854 | // It's possible for us to be given GLValues if we're called via |
| 7855 | // Expr::tryEvaluateObjectSize. |
| 7856 | APValue RVal; |
| 7857 | if (!EvaluateAsRValue(Info, E, RVal)) |
| 7858 | return false; |
| 7859 | LVal.setFrom(Info.Ctx, RVal); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7860 | } else if (!EvaluatePointer(ignorePointerCastsAndParens(E), LVal, Info, |
| 7861 | /*InvalidBaseOK=*/true)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7862 | return false; |
| 7863 | } |
| 7864 | |
| 7865 | // If we point to before the start of the object, there are no accessible |
| 7866 | // bytes. |
| 7867 | if (LVal.getLValueOffset().isNegative()) { |
| 7868 | Size = 0; |
| 7869 | return true; |
| 7870 | } |
| 7871 | |
| 7872 | CharUnits EndOffset; |
| 7873 | if (!determineEndOffset(Info, E->getExprLoc(), Type, LVal, EndOffset)) |
| 7874 | return false; |
| 7875 | |
| 7876 | // If we've fallen outside of the end offset, just pretend there's nothing to |
| 7877 | // write to/read from. |
| 7878 | if (EndOffset <= LVal.getLValueOffset()) |
| 7879 | Size = 0; |
| 7880 | else |
| 7881 | Size = (EndOffset - LVal.getLValueOffset()).getQuantity(); |
| 7882 | return true; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7883 | } |
| 7884 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7885 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 7886 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
| 7887 | return VisitBuiltinCallExpr(E, BuiltinOp); |
| 7888 | |
| 7889 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 7890 | } |
| 7891 | |
| 7892 | bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
| 7893 | unsigned BuiltinOp) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 7894 | switch (unsigned BuiltinOp = E->getBuiltinCallee()) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7895 | default: |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7896 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 7897 | |
| 7898 | case Builtin::BI__builtin_object_size: { |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7899 | // The type was checked when we built the expression. |
| 7900 | unsigned Type = |
| 7901 | E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 7902 | assert(Type <= 3 && "unexpected type"); |
| 7903 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7904 | uint64_t Size; |
| 7905 | if (tryEvaluateBuiltinObjectSize(E->getArg(0), Type, Info, Size)) |
| 7906 | return Success(Size, E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 7907 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 7908 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7909 | return Success((Type & 2) ? 0 : -1, E); |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 7910 | |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 7911 | // Expression had no side effects, but we couldn't statically determine the |
| 7912 | // size of the referenced object. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 7913 | switch (Info.EvalMode) { |
| 7914 | case EvalInfo::EM_ConstantExpression: |
| 7915 | case EvalInfo::EM_PotentialConstantExpression: |
| 7916 | case EvalInfo::EM_ConstantFold: |
| 7917 | case EvalInfo::EM_EvaluateForOverflow: |
| 7918 | case EvalInfo::EM_IgnoreSideEffects: |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7919 | case EvalInfo::EM_OffsetFold: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7920 | // Leave it to IR generation. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 7921 | return Error(E); |
| 7922 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
| 7923 | case EvalInfo::EM_PotentialConstantExpressionUnevaluated: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7924 | // Reduce it to a constant now. |
| 7925 | return Success((Type & 2) ? 0 : -1, E); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 7926 | } |
Richard Smith | cb2ba5a | 2016-07-18 22:37:35 +0000 | [diff] [blame] | 7927 | |
| 7928 | llvm_unreachable("unexpected EvalMode"); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 7929 | } |
| 7930 | |
Benjamin Kramer | a801f4a | 2012-10-06 14:42:22 +0000 | [diff] [blame] | 7931 | case Builtin::BI__builtin_bswap16: |
Richard Smith | 80ac9ef | 2012-09-28 20:20:52 +0000 | [diff] [blame] | 7932 | case Builtin::BI__builtin_bswap32: |
| 7933 | case Builtin::BI__builtin_bswap64: { |
| 7934 | APSInt Val; |
| 7935 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 7936 | return false; |
| 7937 | |
| 7938 | return Success(Val.byteSwap(), E); |
| 7939 | } |
| 7940 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7941 | case Builtin::BI__builtin_classify_type: |
Richard Smith | 08b682b | 2018-05-23 21:18:00 +0000 | [diff] [blame] | 7942 | return Success((int)EvaluateBuiltinClassifyType(E, Info.getLangOpts()), E); |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7943 | |
| 7944 | // FIXME: BI__builtin_clrsb |
| 7945 | // FIXME: BI__builtin_clrsbl |
| 7946 | // FIXME: BI__builtin_clrsbll |
| 7947 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 7948 | case Builtin::BI__builtin_clz: |
| 7949 | case Builtin::BI__builtin_clzl: |
Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 7950 | case Builtin::BI__builtin_clzll: |
| 7951 | case Builtin::BI__builtin_clzs: { |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 7952 | APSInt Val; |
| 7953 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 7954 | return false; |
| 7955 | if (!Val) |
| 7956 | return Error(E); |
| 7957 | |
| 7958 | return Success(Val.countLeadingZeros(), E); |
| 7959 | } |
| 7960 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7961 | case Builtin::BI__builtin_constant_p: |
| 7962 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
| 7963 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 7964 | case Builtin::BI__builtin_ctz: |
| 7965 | case Builtin::BI__builtin_ctzl: |
Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 7966 | case Builtin::BI__builtin_ctzll: |
| 7967 | case Builtin::BI__builtin_ctzs: { |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 7968 | APSInt Val; |
| 7969 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 7970 | return false; |
| 7971 | if (!Val) |
| 7972 | return Error(E); |
| 7973 | |
| 7974 | return Success(Val.countTrailingZeros(), E); |
| 7975 | } |
| 7976 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7977 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 7978 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 7979 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
| 7980 | return Success(Operand, E); |
| 7981 | } |
| 7982 | |
| 7983 | case Builtin::BI__builtin_expect: |
| 7984 | return Visit(E->getArg(0)); |
| 7985 | |
| 7986 | case Builtin::BI__builtin_ffs: |
| 7987 | case Builtin::BI__builtin_ffsl: |
| 7988 | case Builtin::BI__builtin_ffsll: { |
| 7989 | APSInt Val; |
| 7990 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 7991 | return false; |
| 7992 | |
| 7993 | unsigned N = Val.countTrailingZeros(); |
| 7994 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
| 7995 | } |
| 7996 | |
| 7997 | case Builtin::BI__builtin_fpclassify: { |
| 7998 | APFloat Val(0.0); |
| 7999 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
| 8000 | return false; |
| 8001 | unsigned Arg; |
| 8002 | switch (Val.getCategory()) { |
| 8003 | case APFloat::fcNaN: Arg = 0; break; |
| 8004 | case APFloat::fcInfinity: Arg = 1; break; |
| 8005 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
| 8006 | case APFloat::fcZero: Arg = 4; break; |
| 8007 | } |
| 8008 | return Visit(E->getArg(Arg)); |
| 8009 | } |
| 8010 | |
| 8011 | case Builtin::BI__builtin_isinf_sign: { |
| 8012 | APFloat Val(0.0); |
Richard Smith | ab341c6 | 2013-06-13 06:31:13 +0000 | [diff] [blame] | 8013 | return EvaluateFloat(E->getArg(0), Val, Info) && |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 8014 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
| 8015 | } |
| 8016 | |
Richard Smith | ea3019d | 2013-10-15 19:07:14 +0000 | [diff] [blame] | 8017 | case Builtin::BI__builtin_isinf: { |
| 8018 | APFloat Val(0.0); |
| 8019 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 8020 | Success(Val.isInfinity() ? 1 : 0, E); |
| 8021 | } |
| 8022 | |
| 8023 | case Builtin::BI__builtin_isfinite: { |
| 8024 | APFloat Val(0.0); |
| 8025 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 8026 | Success(Val.isFinite() ? 1 : 0, E); |
| 8027 | } |
| 8028 | |
| 8029 | case Builtin::BI__builtin_isnan: { |
| 8030 | APFloat Val(0.0); |
| 8031 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 8032 | Success(Val.isNaN() ? 1 : 0, E); |
| 8033 | } |
| 8034 | |
| 8035 | case Builtin::BI__builtin_isnormal: { |
| 8036 | APFloat Val(0.0); |
| 8037 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 8038 | Success(Val.isNormal() ? 1 : 0, E); |
| 8039 | } |
| 8040 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 8041 | case Builtin::BI__builtin_parity: |
| 8042 | case Builtin::BI__builtin_parityl: |
| 8043 | case Builtin::BI__builtin_parityll: { |
| 8044 | APSInt Val; |
| 8045 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 8046 | return false; |
| 8047 | |
| 8048 | return Success(Val.countPopulation() % 2, E); |
| 8049 | } |
| 8050 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 8051 | case Builtin::BI__builtin_popcount: |
| 8052 | case Builtin::BI__builtin_popcountl: |
| 8053 | case Builtin::BI__builtin_popcountll: { |
| 8054 | APSInt Val; |
| 8055 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 8056 | return false; |
| 8057 | |
| 8058 | return Success(Val.countPopulation(), E); |
| 8059 | } |
| 8060 | |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 8061 | case Builtin::BIstrlen: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8062 | case Builtin::BIwcslen: |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 8063 | // A call to strlen is not a constant expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 8064 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 8065 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8066 | << /*isConstexpr*/0 << /*isConstructor*/0 |
| 8067 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 8068 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 8069 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8070 | LLVM_FALLTHROUGH; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8071 | case Builtin::BI__builtin_strlen: |
| 8072 | case Builtin::BI__builtin_wcslen: { |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 8073 | // As an extension, we support __builtin_strlen() as a constant expression, |
| 8074 | // and support folding strlen() to a constant. |
| 8075 | LValue String; |
| 8076 | if (!EvaluatePointer(E->getArg(0), String, Info)) |
| 8077 | return false; |
| 8078 | |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8079 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
| 8080 | |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 8081 | // Fast path: if it's a string literal, search the string value. |
| 8082 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
| 8083 | String.getLValueBase().dyn_cast<const Expr *>())) { |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 8084 | // The string literal may have embedded null characters. Find the first |
| 8085 | // one and truncate there. |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 8086 | StringRef Str = S->getBytes(); |
| 8087 | int64_t Off = String.Offset.getQuantity(); |
| 8088 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8089 | S->getCharByteWidth() == 1 && |
| 8090 | // FIXME: Add fast-path for wchar_t too. |
| 8091 | Info.Ctx.hasSameUnqualifiedType(CharTy, Info.Ctx.CharTy)) { |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 8092 | Str = Str.substr(Off); |
| 8093 | |
| 8094 | StringRef::size_type Pos = Str.find(0); |
| 8095 | if (Pos != StringRef::npos) |
| 8096 | Str = Str.substr(0, Pos); |
| 8097 | |
| 8098 | return Success(Str.size(), E); |
| 8099 | } |
| 8100 | |
| 8101 | // Fall through to slow path to issue appropriate diagnostic. |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 8102 | } |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 8103 | |
| 8104 | // 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] | 8105 | for (uint64_t Strlen = 0; /**/; ++Strlen) { |
| 8106 | APValue Char; |
| 8107 | if (!handleLValueToRValueConversion(Info, E, CharTy, String, Char) || |
| 8108 | !Char.isInt()) |
| 8109 | return false; |
| 8110 | if (!Char.getInt()) |
| 8111 | return Success(Strlen, E); |
| 8112 | if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1)) |
| 8113 | return false; |
| 8114 | } |
| 8115 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8116 | |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8117 | case Builtin::BIstrcmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8118 | case Builtin::BIwcscmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8119 | case Builtin::BIstrncmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8120 | case Builtin::BIwcsncmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8121 | case Builtin::BImemcmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8122 | case Builtin::BIwmemcmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8123 | // A call to strlen is not a constant expression. |
| 8124 | if (Info.getLangOpts().CPlusPlus11) |
| 8125 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| 8126 | << /*isConstexpr*/0 << /*isConstructor*/0 |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8127 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8128 | else |
| 8129 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8130 | LLVM_FALLTHROUGH; |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8131 | case Builtin::BI__builtin_strcmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8132 | case Builtin::BI__builtin_wcscmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8133 | case Builtin::BI__builtin_strncmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8134 | case Builtin::BI__builtin_wcsncmp: |
| 8135 | case Builtin::BI__builtin_memcmp: |
| 8136 | case Builtin::BI__builtin_wmemcmp: { |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8137 | LValue String1, String2; |
| 8138 | if (!EvaluatePointer(E->getArg(0), String1, Info) || |
| 8139 | !EvaluatePointer(E->getArg(1), String2, Info)) |
| 8140 | return false; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8141 | |
| 8142 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
| 8143 | |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8144 | uint64_t MaxLength = uint64_t(-1); |
| 8145 | if (BuiltinOp != Builtin::BIstrcmp && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8146 | BuiltinOp != Builtin::BIwcscmp && |
| 8147 | BuiltinOp != Builtin::BI__builtin_strcmp && |
| 8148 | BuiltinOp != Builtin::BI__builtin_wcscmp) { |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8149 | APSInt N; |
| 8150 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
| 8151 | return false; |
| 8152 | MaxLength = N.getExtValue(); |
| 8153 | } |
| 8154 | bool StopAtNull = (BuiltinOp != Builtin::BImemcmp && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8155 | BuiltinOp != Builtin::BIwmemcmp && |
| 8156 | BuiltinOp != Builtin::BI__builtin_memcmp && |
| 8157 | BuiltinOp != Builtin::BI__builtin_wmemcmp); |
Benjamin Kramer | 33b7092 | 2018-04-23 22:04:34 +0000 | [diff] [blame] | 8158 | bool IsWide = BuiltinOp == Builtin::BIwcscmp || |
| 8159 | BuiltinOp == Builtin::BIwcsncmp || |
| 8160 | BuiltinOp == Builtin::BIwmemcmp || |
| 8161 | BuiltinOp == Builtin::BI__builtin_wcscmp || |
| 8162 | BuiltinOp == Builtin::BI__builtin_wcsncmp || |
| 8163 | BuiltinOp == Builtin::BI__builtin_wmemcmp; |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8164 | for (; MaxLength; --MaxLength) { |
| 8165 | APValue Char1, Char2; |
| 8166 | if (!handleLValueToRValueConversion(Info, E, CharTy, String1, Char1) || |
| 8167 | !handleLValueToRValueConversion(Info, E, CharTy, String2, Char2) || |
| 8168 | !Char1.isInt() || !Char2.isInt()) |
| 8169 | return false; |
Benjamin Kramer | 33b7092 | 2018-04-23 22:04:34 +0000 | [diff] [blame] | 8170 | if (Char1.getInt() != Char2.getInt()) { |
| 8171 | if (IsWide) // wmemcmp compares with wchar_t signedness. |
| 8172 | return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E); |
| 8173 | // memcmp always compares unsigned chars. |
| 8174 | return Success(Char1.getInt().ult(Char2.getInt()) ? -1 : 1, E); |
| 8175 | } |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8176 | if (StopAtNull && !Char1.getInt()) |
| 8177 | return Success(0, E); |
| 8178 | assert(!(StopAtNull && !Char2.getInt())); |
| 8179 | if (!HandleLValueArrayAdjustment(Info, E, String1, CharTy, 1) || |
| 8180 | !HandleLValueArrayAdjustment(Info, E, String2, CharTy, 1)) |
| 8181 | return false; |
| 8182 | } |
| 8183 | // We hit the strncmp / memcmp limit. |
| 8184 | return Success(0, E); |
| 8185 | } |
| 8186 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8187 | case Builtin::BI__atomic_always_lock_free: |
Richard Smith | b1e36c6 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 8188 | case Builtin::BI__atomic_is_lock_free: |
| 8189 | case Builtin::BI__c11_atomic_is_lock_free: { |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8190 | APSInt SizeVal; |
| 8191 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 8192 | return false; |
| 8193 | |
| 8194 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 8195 | // of two less than the maximum inline atomic width, we know it is |
| 8196 | // lock-free. If the size isn't a power of two, or greater than the |
| 8197 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 8198 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 8199 | // the answer can only be determined at runtime; for example, 16-byte |
| 8200 | // atomics have lock-free implementations on some, but not all, |
| 8201 | // x86-64 processors. |
| 8202 | |
| 8203 | // Check power-of-two. |
| 8204 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8205 | if (Size.isPowerOfTwo()) { |
| 8206 | // Check against inlining width. |
| 8207 | unsigned InlineWidthBits = |
| 8208 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 8209 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 8210 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 8211 | Size == CharUnits::One() || |
| 8212 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 8213 | Expr::NPC_NeverValueDependent)) |
| 8214 | // OK, we will inline appropriately-aligned operations of this size, |
| 8215 | // and _Atomic(T) is appropriately-aligned. |
| 8216 | return Success(1, E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8217 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8218 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 8219 | castAs<PointerType>()->getPointeeType(); |
| 8220 | if (!PointeeType->isIncompleteType() && |
| 8221 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 8222 | // OK, we will inline operations on this object. |
| 8223 | return Success(1, E); |
| 8224 | } |
| 8225 | } |
| 8226 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8227 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8228 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 8229 | Success(0, E) : Error(E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8230 | } |
Jonas Hahnfeld | 23604a8 | 2017-10-17 14:28:14 +0000 | [diff] [blame] | 8231 | case Builtin::BIomp_is_initial_device: |
| 8232 | // We can decide statically which value the runtime would return if called. |
| 8233 | return Success(Info.getLangOpts().OpenMPIsDevice ? 0 : 1, E); |
Erich Keane | 0095827 | 2018-06-13 20:43:27 +0000 | [diff] [blame] | 8234 | case Builtin::BI__builtin_add_overflow: |
| 8235 | case Builtin::BI__builtin_sub_overflow: |
| 8236 | case Builtin::BI__builtin_mul_overflow: |
| 8237 | case Builtin::BI__builtin_sadd_overflow: |
| 8238 | case Builtin::BI__builtin_uadd_overflow: |
| 8239 | case Builtin::BI__builtin_uaddl_overflow: |
| 8240 | case Builtin::BI__builtin_uaddll_overflow: |
| 8241 | case Builtin::BI__builtin_usub_overflow: |
| 8242 | case Builtin::BI__builtin_usubl_overflow: |
| 8243 | case Builtin::BI__builtin_usubll_overflow: |
| 8244 | case Builtin::BI__builtin_umul_overflow: |
| 8245 | case Builtin::BI__builtin_umull_overflow: |
| 8246 | case Builtin::BI__builtin_umulll_overflow: |
| 8247 | case Builtin::BI__builtin_saddl_overflow: |
| 8248 | case Builtin::BI__builtin_saddll_overflow: |
| 8249 | case Builtin::BI__builtin_ssub_overflow: |
| 8250 | case Builtin::BI__builtin_ssubl_overflow: |
| 8251 | case Builtin::BI__builtin_ssubll_overflow: |
| 8252 | case Builtin::BI__builtin_smul_overflow: |
| 8253 | case Builtin::BI__builtin_smull_overflow: |
| 8254 | case Builtin::BI__builtin_smulll_overflow: { |
| 8255 | LValue ResultLValue; |
| 8256 | APSInt LHS, RHS; |
| 8257 | |
| 8258 | QualType ResultType = E->getArg(2)->getType()->getPointeeType(); |
| 8259 | if (!EvaluateInteger(E->getArg(0), LHS, Info) || |
| 8260 | !EvaluateInteger(E->getArg(1), RHS, Info) || |
| 8261 | !EvaluatePointer(E->getArg(2), ResultLValue, Info)) |
| 8262 | return false; |
| 8263 | |
| 8264 | APSInt Result; |
| 8265 | bool DidOverflow = false; |
| 8266 | |
| 8267 | // If the types don't have to match, enlarge all 3 to the largest of them. |
| 8268 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
| 8269 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
| 8270 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
| 8271 | bool IsSigned = LHS.isSigned() || RHS.isSigned() || |
| 8272 | ResultType->isSignedIntegerOrEnumerationType(); |
| 8273 | bool AllSigned = LHS.isSigned() && RHS.isSigned() && |
| 8274 | ResultType->isSignedIntegerOrEnumerationType(); |
| 8275 | uint64_t LHSSize = LHS.getBitWidth(); |
| 8276 | uint64_t RHSSize = RHS.getBitWidth(); |
| 8277 | uint64_t ResultSize = Info.Ctx.getTypeSize(ResultType); |
| 8278 | uint64_t MaxBits = std::max(std::max(LHSSize, RHSSize), ResultSize); |
| 8279 | |
| 8280 | // Add an additional bit if the signedness isn't uniformly agreed to. We |
| 8281 | // could do this ONLY if there is a signed and an unsigned that both have |
| 8282 | // MaxBits, but the code to check that is pretty nasty. The issue will be |
| 8283 | // caught in the shrink-to-result later anyway. |
| 8284 | if (IsSigned && !AllSigned) |
| 8285 | ++MaxBits; |
| 8286 | |
| 8287 | LHS = APSInt(IsSigned ? LHS.sextOrSelf(MaxBits) : LHS.zextOrSelf(MaxBits), |
| 8288 | !IsSigned); |
| 8289 | RHS = APSInt(IsSigned ? RHS.sextOrSelf(MaxBits) : RHS.zextOrSelf(MaxBits), |
| 8290 | !IsSigned); |
| 8291 | Result = APSInt(MaxBits, !IsSigned); |
| 8292 | } |
| 8293 | |
| 8294 | // Find largest int. |
| 8295 | switch (BuiltinOp) { |
| 8296 | default: |
| 8297 | llvm_unreachable("Invalid value for BuiltinOp"); |
| 8298 | case Builtin::BI__builtin_add_overflow: |
| 8299 | case Builtin::BI__builtin_sadd_overflow: |
| 8300 | case Builtin::BI__builtin_saddl_overflow: |
| 8301 | case Builtin::BI__builtin_saddll_overflow: |
| 8302 | case Builtin::BI__builtin_uadd_overflow: |
| 8303 | case Builtin::BI__builtin_uaddl_overflow: |
| 8304 | case Builtin::BI__builtin_uaddll_overflow: |
| 8305 | Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow) |
| 8306 | : LHS.uadd_ov(RHS, DidOverflow); |
| 8307 | break; |
| 8308 | case Builtin::BI__builtin_sub_overflow: |
| 8309 | case Builtin::BI__builtin_ssub_overflow: |
| 8310 | case Builtin::BI__builtin_ssubl_overflow: |
| 8311 | case Builtin::BI__builtin_ssubll_overflow: |
| 8312 | case Builtin::BI__builtin_usub_overflow: |
| 8313 | case Builtin::BI__builtin_usubl_overflow: |
| 8314 | case Builtin::BI__builtin_usubll_overflow: |
| 8315 | Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow) |
| 8316 | : LHS.usub_ov(RHS, DidOverflow); |
| 8317 | break; |
| 8318 | case Builtin::BI__builtin_mul_overflow: |
| 8319 | case Builtin::BI__builtin_smul_overflow: |
| 8320 | case Builtin::BI__builtin_smull_overflow: |
| 8321 | case Builtin::BI__builtin_smulll_overflow: |
| 8322 | case Builtin::BI__builtin_umul_overflow: |
| 8323 | case Builtin::BI__builtin_umull_overflow: |
| 8324 | case Builtin::BI__builtin_umulll_overflow: |
| 8325 | Result = LHS.isSigned() ? LHS.smul_ov(RHS, DidOverflow) |
| 8326 | : LHS.umul_ov(RHS, DidOverflow); |
| 8327 | break; |
| 8328 | } |
| 8329 | |
| 8330 | // In the case where multiple sizes are allowed, truncate and see if |
| 8331 | // the values are the same. |
| 8332 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
| 8333 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
| 8334 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
| 8335 | // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead, |
| 8336 | // since it will give us the behavior of a TruncOrSelf in the case where |
| 8337 | // its parameter <= its size. We previously set Result to be at least the |
| 8338 | // type-size of the result, so getTypeSize(ResultType) <= Result.BitWidth |
| 8339 | // will work exactly like TruncOrSelf. |
| 8340 | APSInt Temp = Result.extOrTrunc(Info.Ctx.getTypeSize(ResultType)); |
| 8341 | Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType()); |
| 8342 | |
| 8343 | if (!APSInt::isSameValue(Temp, Result)) |
| 8344 | DidOverflow = true; |
| 8345 | Result = Temp; |
| 8346 | } |
| 8347 | |
| 8348 | APValue APV{Result}; |
| 8349 | handleAssignment(Info, E, ResultLValue, ResultType, APV); |
| 8350 | return Success(DidOverflow, E); |
| 8351 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 8352 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 8353 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 8354 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8355 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 8356 | if (!A.getLValueBase()) |
| 8357 | return !B.getLValueBase(); |
| 8358 | if (!B.getLValueBase()) |
| 8359 | return false; |
| 8360 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 8361 | if (A.getLValueBase().getOpaqueValue() != |
| 8362 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8363 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 8364 | if (!ADecl) |
| 8365 | return false; |
| 8366 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 8367 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8368 | return false; |
| 8369 | } |
| 8370 | |
| 8371 | return IsGlobalLValue(A.getLValueBase()) || |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 8372 | (A.getLValueCallIndex() == B.getLValueCallIndex() && |
| 8373 | A.getLValueVersion() == B.getLValueVersion()); |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8374 | } |
| 8375 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8376 | /// Determine whether this is a pointer past the end of the complete |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 8377 | /// object referred to by the lvalue. |
| 8378 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
| 8379 | const LValue &LV) { |
| 8380 | // A null pointer can be viewed as being "past the end" but we don't |
| 8381 | // choose to look at it that way here. |
| 8382 | if (!LV.getLValueBase()) |
| 8383 | return false; |
| 8384 | |
| 8385 | // If the designator is valid and refers to a subobject, we're not pointing |
| 8386 | // past the end. |
| 8387 | if (!LV.getLValueDesignator().Invalid && |
| 8388 | !LV.getLValueDesignator().isOnePastTheEnd()) |
| 8389 | return false; |
| 8390 | |
David Majnemer | c378ca5 | 2015-08-29 08:32:55 +0000 | [diff] [blame] | 8391 | // A pointer to an incomplete type might be past-the-end if the type's size is |
| 8392 | // zero. We cannot tell because the type is incomplete. |
| 8393 | QualType Ty = getType(LV.getLValueBase()); |
| 8394 | if (Ty->isIncompleteType()) |
| 8395 | return true; |
| 8396 | |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 8397 | // We're a past-the-end pointer if we point to the byte after the object, |
| 8398 | // no matter what our type or path is. |
David Majnemer | c378ca5 | 2015-08-29 08:32:55 +0000 | [diff] [blame] | 8399 | auto Size = Ctx.getTypeSizeInChars(Ty); |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 8400 | return LV.getLValueOffset() == Size; |
| 8401 | } |
| 8402 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8403 | namespace { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8404 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8405 | /// Data recursive integer evaluator of certain binary operators. |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8406 | /// |
| 8407 | /// We use a data recursive algorithm for binary operators so that we are able |
| 8408 | /// to handle extreme cases of chained binary operators without causing stack |
| 8409 | /// overflow. |
| 8410 | class DataRecursiveIntBinOpEvaluator { |
| 8411 | struct EvalResult { |
| 8412 | APValue Val; |
| 8413 | bool Failed; |
| 8414 | |
| 8415 | EvalResult() : Failed(false) { } |
| 8416 | |
| 8417 | void swap(EvalResult &RHS) { |
| 8418 | Val.swap(RHS.Val); |
| 8419 | Failed = RHS.Failed; |
| 8420 | RHS.Failed = false; |
| 8421 | } |
| 8422 | }; |
| 8423 | |
| 8424 | struct Job { |
| 8425 | const Expr *E; |
| 8426 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 8427 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 8428 | |
David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 8429 | Job() = default; |
Benjamin Kramer | 33e9760 | 2016-10-21 18:55:07 +0000 | [diff] [blame] | 8430 | Job(Job &&) = default; |
David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 8431 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8432 | void startSpeculativeEval(EvalInfo &Info) { |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8433 | SpecEvalRAII = SpeculativeEvaluationRAII(Info); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8434 | } |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8435 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8436 | private: |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8437 | SpeculativeEvaluationRAII SpecEvalRAII; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8438 | }; |
| 8439 | |
| 8440 | SmallVector<Job, 16> Queue; |
| 8441 | |
| 8442 | IntExprEvaluator &IntEval; |
| 8443 | EvalInfo &Info; |
| 8444 | APValue &FinalResult; |
| 8445 | |
| 8446 | public: |
| 8447 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 8448 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 8449 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8450 | /// 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] | 8451 | /// data recursively. |
| 8452 | /// We handle binary operators that are comma, logical, or that have operands |
| 8453 | /// with integral or enumeration type. |
| 8454 | static bool shouldEnqueue(const BinaryOperator *E) { |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8455 | return E->getOpcode() == BO_Comma || E->isLogicalOp() || |
| 8456 | (E->isRValue() && E->getType()->isIntegralOrEnumerationType() && |
Richard Smith | 3a09d8b | 2016-06-04 00:22:31 +0000 | [diff] [blame] | 8457 | E->getLHS()->getType()->isIntegralOrEnumerationType() && |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8458 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 8459 | } |
| 8460 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8461 | bool Traverse(const BinaryOperator *E) { |
| 8462 | enqueue(E); |
| 8463 | EvalResult PrevResult; |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8464 | while (!Queue.empty()) |
| 8465 | process(PrevResult); |
| 8466 | |
| 8467 | if (PrevResult.Failed) return false; |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8468 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8469 | FinalResult.swap(PrevResult.Val); |
| 8470 | return true; |
| 8471 | } |
| 8472 | |
| 8473 | private: |
| 8474 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 8475 | return IntEval.Success(Value, E, Result); |
| 8476 | } |
| 8477 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 8478 | return IntEval.Success(Value, E, Result); |
| 8479 | } |
| 8480 | bool Error(const Expr *E) { |
| 8481 | return IntEval.Error(E); |
| 8482 | } |
| 8483 | bool Error(const Expr *E, diag::kind D) { |
| 8484 | return IntEval.Error(E, D); |
| 8485 | } |
| 8486 | |
| 8487 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 8488 | return Info.CCEDiag(E, D); |
| 8489 | } |
| 8490 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 8491 | // Returns true if visiting the RHS is necessary, false otherwise. |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8492 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8493 | bool &SuppressRHSDiags); |
| 8494 | |
| 8495 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 8496 | const BinaryOperator *E, APValue &Result); |
| 8497 | |
| 8498 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 8499 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 8500 | if (Result.Failed) |
| 8501 | Result.Val = APValue(); |
| 8502 | } |
| 8503 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8504 | void process(EvalResult &Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8505 | |
| 8506 | void enqueue(const Expr *E) { |
| 8507 | E = E->IgnoreParens(); |
| 8508 | Queue.resize(Queue.size()+1); |
| 8509 | Queue.back().E = E; |
| 8510 | Queue.back().Kind = Job::AnyExprKind; |
| 8511 | } |
| 8512 | }; |
| 8513 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8514 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8515 | |
| 8516 | bool DataRecursiveIntBinOpEvaluator:: |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8517 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8518 | bool &SuppressRHSDiags) { |
| 8519 | if (E->getOpcode() == BO_Comma) { |
| 8520 | // Ignore LHS but note if we could not evaluate it. |
| 8521 | if (LHSResult.Failed) |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8522 | return Info.noteSideEffect(); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8523 | return true; |
| 8524 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8525 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8526 | if (E->isLogicalOp()) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8527 | bool LHSAsBool; |
| 8528 | if (!LHSResult.Failed && HandleConversionToBool(LHSResult.Val, LHSAsBool)) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8529 | // We were able to evaluate the LHS, see if we can get away with not |
| 8530 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8531 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
| 8532 | Success(LHSAsBool, E, LHSResult.Val); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8533 | return false; // Ignore RHS |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8534 | } |
| 8535 | } else { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8536 | LHSResult.Failed = true; |
| 8537 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8538 | // 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] | 8539 | // might have had side effects. |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8540 | if (!Info.noteSideEffect()) |
| 8541 | return false; |
| 8542 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8543 | // We can't evaluate the LHS; however, sometimes the result |
| 8544 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 8545 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 8546 | SuppressRHSDiags = true; |
| 8547 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8548 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8549 | return true; |
| 8550 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8551 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8552 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 8553 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8554 | |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8555 | if (LHSResult.Failed && !Info.noteFailure()) |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8556 | return false; // Ignore RHS; |
| 8557 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8558 | return true; |
| 8559 | } |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8560 | |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 8561 | static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index, |
| 8562 | bool IsSub) { |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 8563 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
| 8564 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
| 8565 | // offsets. |
| 8566 | assert(!LVal.hasLValuePath() && "have designator for integer lvalue"); |
| 8567 | CharUnits &Offset = LVal.getLValueOffset(); |
| 8568 | uint64_t Offset64 = Offset.getQuantity(); |
| 8569 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
| 8570 | Offset = CharUnits::fromQuantity(IsSub ? Offset64 - Index64 |
| 8571 | : Offset64 + Index64); |
| 8572 | } |
| 8573 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8574 | bool DataRecursiveIntBinOpEvaluator:: |
| 8575 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 8576 | const BinaryOperator *E, APValue &Result) { |
| 8577 | if (E->getOpcode() == BO_Comma) { |
| 8578 | if (RHSResult.Failed) |
| 8579 | return false; |
| 8580 | Result = RHSResult.Val; |
| 8581 | return true; |
| 8582 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8583 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8584 | if (E->isLogicalOp()) { |
| 8585 | bool lhsResult, rhsResult; |
| 8586 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 8587 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8588 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8589 | if (LHSIsOK) { |
| 8590 | if (RHSIsOK) { |
| 8591 | if (E->getOpcode() == BO_LOr) |
| 8592 | return Success(lhsResult || rhsResult, E, Result); |
| 8593 | else |
| 8594 | return Success(lhsResult && rhsResult, E, Result); |
| 8595 | } |
| 8596 | } else { |
| 8597 | if (RHSIsOK) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8598 | // We can't evaluate the LHS; however, sometimes the result |
| 8599 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 8600 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8601 | return Success(rhsResult, E, Result); |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8602 | } |
| 8603 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8604 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8605 | return false; |
| 8606 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8607 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8608 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 8609 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8610 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8611 | if (LHSResult.Failed || RHSResult.Failed) |
| 8612 | return false; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8613 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8614 | const APValue &LHSVal = LHSResult.Val; |
| 8615 | const APValue &RHSVal = RHSResult.Val; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8616 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8617 | // Handle cases like (unsigned long)&a + 4. |
| 8618 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 8619 | Result = LHSVal; |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 8620 | addOrSubLValueAsInteger(Result, RHSVal.getInt(), E->getOpcode() == BO_Sub); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8621 | return true; |
| 8622 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8623 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8624 | // Handle cases like 4 + (unsigned long)&a |
| 8625 | if (E->getOpcode() == BO_Add && |
| 8626 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 8627 | Result = RHSVal; |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 8628 | addOrSubLValueAsInteger(Result, LHSVal.getInt(), /*IsSub*/false); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8629 | return true; |
| 8630 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8631 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8632 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 8633 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 8634 | if (!LHSVal.getLValueOffset().isZero() || |
| 8635 | !RHSVal.getLValueOffset().isZero()) |
| 8636 | return false; |
| 8637 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 8638 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 8639 | if (!LHSExpr || !RHSExpr) |
| 8640 | return false; |
| 8641 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 8642 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 8643 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 8644 | return false; |
| 8645 | // Make sure both labels come from the same function. |
| 8646 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 8647 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 8648 | return false; |
| 8649 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 8650 | return true; |
| 8651 | } |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 8652 | |
| 8653 | // All the remaining cases expect both operands to be an integer |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8654 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 8655 | return Error(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 8656 | |
| 8657 | // Set up the width and signedness manually, in case it can't be deduced |
| 8658 | // from the operation we're performing. |
| 8659 | // FIXME: Don't do this in the cases where we can deduce it. |
| 8660 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
| 8661 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
| 8662 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
| 8663 | RHSVal.getInt(), Value)) |
| 8664 | return false; |
| 8665 | return Success(Value, E, Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8666 | } |
| 8667 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8668 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8669 | Job &job = Queue.back(); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8670 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8671 | switch (job.Kind) { |
| 8672 | case Job::AnyExprKind: { |
| 8673 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 8674 | if (shouldEnqueue(Bop)) { |
| 8675 | job.Kind = Job::BinOpKind; |
| 8676 | enqueue(Bop->getLHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8677 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8678 | } |
| 8679 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8680 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8681 | EvaluateExpr(job.E, Result); |
| 8682 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8683 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8684 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8685 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8686 | case Job::BinOpKind: { |
| 8687 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8688 | bool SuppressRHSDiags = false; |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8689 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8690 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8691 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8692 | } |
| 8693 | if (SuppressRHSDiags) |
| 8694 | job.startSpeculativeEval(Info); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8695 | job.LHSResult.swap(Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8696 | job.Kind = Job::BinOpVisitedLHSKind; |
| 8697 | enqueue(Bop->getRHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8698 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8699 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8700 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8701 | case Job::BinOpVisitedLHSKind: { |
| 8702 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 8703 | EvalResult RHS; |
| 8704 | RHS.swap(Result); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8705 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8706 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8707 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8708 | } |
| 8709 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8710 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8711 | llvm_unreachable("Invalid Job::Kind!"); |
| 8712 | } |
| 8713 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8714 | namespace { |
| 8715 | /// Used when we determine that we should fail, but can keep evaluating prior to |
| 8716 | /// noting that we had a failure. |
| 8717 | class DelayedNoteFailureRAII { |
| 8718 | EvalInfo &Info; |
| 8719 | bool NoteFailure; |
| 8720 | |
| 8721 | public: |
| 8722 | DelayedNoteFailureRAII(EvalInfo &Info, bool NoteFailure = true) |
| 8723 | : Info(Info), NoteFailure(NoteFailure) {} |
| 8724 | ~DelayedNoteFailureRAII() { |
| 8725 | if (NoteFailure) { |
| 8726 | bool ContinueAfterFailure = Info.noteFailure(); |
| 8727 | (void)ContinueAfterFailure; |
| 8728 | assert(ContinueAfterFailure && |
| 8729 | "Shouldn't have kept evaluating on failure."); |
| 8730 | } |
| 8731 | } |
| 8732 | }; |
| 8733 | } |
| 8734 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8735 | template <class SuccessCB, class AfterCB> |
| 8736 | static bool |
| 8737 | EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, |
| 8738 | SuccessCB &&Success, AfterCB &&DoAfter) { |
| 8739 | assert(E->isComparisonOp() && "expected comparison operator"); |
| 8740 | assert((E->getOpcode() == BO_Cmp || |
| 8741 | E->getType()->isIntegralOrEnumerationType()) && |
| 8742 | "unsupported binary expression evaluation"); |
| 8743 | auto Error = [&](const Expr *E) { |
| 8744 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
| 8745 | return false; |
| 8746 | }; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8747 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8748 | using CCR = ComparisonCategoryResult; |
| 8749 | bool IsRelational = E->isRelationalOp(); |
| 8750 | bool IsEquality = E->isEqualityOp(); |
| 8751 | if (E->getOpcode() == BO_Cmp) { |
| 8752 | const ComparisonCategoryInfo &CmpInfo = |
| 8753 | Info.Ctx.CompCategories.getInfoForType(E->getType()); |
| 8754 | IsRelational = CmpInfo.isOrdered(); |
| 8755 | IsEquality = CmpInfo.isEquality(); |
| 8756 | } |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 8757 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8758 | QualType LHSTy = E->getLHS()->getType(); |
| 8759 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8760 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8761 | if (LHSTy->isIntegralOrEnumerationType() && |
| 8762 | RHSTy->isIntegralOrEnumerationType()) { |
| 8763 | APSInt LHS, RHS; |
| 8764 | bool LHSOK = EvaluateInteger(E->getLHS(), LHS, Info); |
| 8765 | if (!LHSOK && !Info.noteFailure()) |
| 8766 | return false; |
| 8767 | if (!EvaluateInteger(E->getRHS(), RHS, Info) || !LHSOK) |
| 8768 | return false; |
| 8769 | if (LHS < RHS) |
| 8770 | return Success(CCR::Less, E); |
| 8771 | if (LHS > RHS) |
| 8772 | return Success(CCR::Greater, E); |
| 8773 | return Success(CCR::Equal, E); |
| 8774 | } |
| 8775 | |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 8776 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8777 | ComplexValue LHS, RHS; |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 8778 | bool LHSOK; |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 8779 | if (E->isAssignmentOp()) { |
| 8780 | LValue LV; |
| 8781 | EvaluateLValue(E->getLHS(), LV, Info); |
| 8782 | LHSOK = false; |
| 8783 | } else if (LHSTy->isRealFloatingType()) { |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 8784 | LHSOK = EvaluateFloat(E->getLHS(), LHS.FloatReal, Info); |
| 8785 | if (LHSOK) { |
| 8786 | LHS.makeComplexFloat(); |
| 8787 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
| 8788 | } |
| 8789 | } else { |
| 8790 | LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 8791 | } |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8792 | if (!LHSOK && !Info.noteFailure()) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8793 | return false; |
| 8794 | |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 8795 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 8796 | if (!EvaluateFloat(E->getRHS(), RHS.FloatReal, Info) || !LHSOK) |
| 8797 | return false; |
| 8798 | RHS.makeComplexFloat(); |
| 8799 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
| 8800 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8801 | return false; |
| 8802 | |
| 8803 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8804 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8805 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8806 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8807 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8808 | bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual; |
| 8809 | return Success(IsEqual ? CCR::Equal : CCR::Nonequal, E); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8810 | } else { |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8811 | assert(IsEquality && "invalid complex comparison"); |
| 8812 | bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 8813 | LHS.getComplexIntImag() == RHS.getComplexIntImag(); |
| 8814 | return Success(IsEqual ? CCR::Equal : CCR::Nonequal, E); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8815 | } |
| 8816 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8817 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8818 | if (LHSTy->isRealFloatingType() && |
| 8819 | RHSTy->isRealFloatingType()) { |
| 8820 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8821 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8822 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8823 | if (!LHSOK && !Info.noteFailure()) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8824 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8825 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8826 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8827 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8828 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8829 | assert(E->isComparisonOp() && "Invalid binary operator!"); |
| 8830 | auto GetCmpRes = [&]() { |
| 8831 | switch (LHS.compare(RHS)) { |
| 8832 | case APFloat::cmpEqual: |
| 8833 | return CCR::Equal; |
| 8834 | case APFloat::cmpLessThan: |
| 8835 | return CCR::Less; |
| 8836 | case APFloat::cmpGreaterThan: |
| 8837 | return CCR::Greater; |
| 8838 | case APFloat::cmpUnordered: |
| 8839 | return CCR::Unordered; |
| 8840 | } |
Simon Pilgrim | 3366dcf | 2018-05-08 09:40:32 +0000 | [diff] [blame] | 8841 | llvm_unreachable("Unrecognised APFloat::cmpResult enum"); |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8842 | }; |
| 8843 | return Success(GetCmpRes(), E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8844 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8845 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 8846 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8847 | LValue LHSValue, RHSValue; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8848 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8849 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 8850 | if (!LHSOK && !Info.noteFailure()) |
| 8851 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 8852 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8853 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 8854 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 8855 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8856 | // Reject differing bases from the normal codepath; we special-case |
| 8857 | // comparisons to null. |
| 8858 | if (!HasSameBase(LHSValue, RHSValue)) { |
| 8859 | // Inequalities and subtractions between unrelated pointers have |
| 8860 | // unspecified or undefined behavior. |
| 8861 | if (!IsEquality) |
| 8862 | return Error(E); |
| 8863 | // A constant address may compare equal to the address of a symbol. |
| 8864 | // The one exception is that address of an object cannot compare equal |
| 8865 | // to a null pointer constant. |
| 8866 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 8867 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
| 8868 | return Error(E); |
| 8869 | // It's implementation-defined whether distinct literals will have |
| 8870 | // distinct addresses. In clang, the result of such a comparison is |
| 8871 | // unspecified, so it is not a constant expression. However, we do know |
| 8872 | // that the address of a literal will be non-null. |
| 8873 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 8874 | LHSValue.Base && RHSValue.Base) |
| 8875 | return Error(E); |
| 8876 | // We can't tell whether weak symbols will end up pointing to the same |
| 8877 | // object. |
| 8878 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
| 8879 | return Error(E); |
| 8880 | // We can't compare the address of the start of one object with the |
| 8881 | // past-the-end address of another object, per C++ DR1652. |
| 8882 | if ((LHSValue.Base && LHSValue.Offset.isZero() && |
| 8883 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) || |
| 8884 | (RHSValue.Base && RHSValue.Offset.isZero() && |
| 8885 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue))) |
| 8886 | return Error(E); |
| 8887 | // We can't tell whether an object is at the same address as another |
| 8888 | // zero sized object. |
| 8889 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
| 8890 | (LHSValue.Base && isZeroSized(RHSValue))) |
| 8891 | return Error(E); |
| 8892 | return Success(CCR::Nonequal, E); |
| 8893 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 8894 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8895 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 8896 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 8897 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8898 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 8899 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 8900 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8901 | // C++11 [expr.rel]p3: |
| 8902 | // Pointers to void (after pointer conversions) can be compared, with a |
| 8903 | // result defined as follows: If both pointers represent the same |
| 8904 | // address or are both the null pointer value, the result is true if the |
| 8905 | // operator is <= or >= and false otherwise; otherwise the result is |
| 8906 | // unspecified. |
| 8907 | // We interpret this as applying to pointers to *cv* void. |
| 8908 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && IsRelational) |
| 8909 | Info.CCEDiag(E, diag::note_constexpr_void_comparison); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 8910 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8911 | // C++11 [expr.rel]p2: |
| 8912 | // - If two pointers point to non-static data members of the same object, |
| 8913 | // or to subobjects or array elements fo such members, recursively, the |
| 8914 | // pointer to the later declared member compares greater provided the |
| 8915 | // two members have the same access control and provided their class is |
| 8916 | // not a union. |
| 8917 | // [...] |
| 8918 | // - Otherwise pointer comparisons are unspecified. |
| 8919 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) { |
| 8920 | bool WasArrayIndex; |
| 8921 | unsigned Mismatch = FindDesignatorMismatch( |
| 8922 | getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex); |
| 8923 | // At the point where the designators diverge, the comparison has a |
| 8924 | // specified value if: |
| 8925 | // - we are comparing array indices |
| 8926 | // - we are comparing fields of a union, or fields with the same access |
| 8927 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 8928 | // constant expression. |
| 8929 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 8930 | Mismatch < RHSDesignator.Entries.size()) { |
| 8931 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 8932 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 8933 | if (!LF && !RF) |
| 8934 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 8935 | else if (!LF) |
| 8936 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 8937 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 8938 | << RF->getParent() << RF; |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8939 | else if (!RF) |
| 8940 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 8941 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 8942 | << LF->getParent() << LF; |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8943 | else if (!LF->getParent()->isUnion() && |
| 8944 | LF->getAccess() != RF->getAccess()) |
| 8945 | Info.CCEDiag(E, |
| 8946 | diag::note_constexpr_pointer_comparison_differing_access) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 8947 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 8948 | << LF->getParent(); |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 8949 | } |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 8950 | } |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8951 | |
| 8952 | // The comparison here must be unsigned, and performed with the same |
| 8953 | // width as the pointer. |
| 8954 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 8955 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 8956 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 8957 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 8958 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 8959 | CompareLHS &= Mask; |
| 8960 | CompareRHS &= Mask; |
| 8961 | |
| 8962 | // If there is a base and this is a relational operator, we can only |
| 8963 | // compare pointers within the object in question; otherwise, the result |
| 8964 | // depends on where the object is located in memory. |
| 8965 | if (!LHSValue.Base.isNull() && IsRelational) { |
| 8966 | QualType BaseTy = getType(LHSValue.Base); |
| 8967 | if (BaseTy->isIncompleteType()) |
| 8968 | return Error(E); |
| 8969 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 8970 | uint64_t OffsetLimit = Size.getQuantity(); |
| 8971 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 8972 | return Error(E); |
| 8973 | } |
| 8974 | |
| 8975 | if (CompareLHS < CompareRHS) |
| 8976 | return Success(CCR::Less, E); |
| 8977 | if (CompareLHS > CompareRHS) |
| 8978 | return Success(CCR::Greater, E); |
| 8979 | return Success(CCR::Equal, E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 8980 | } |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 8981 | |
| 8982 | if (LHSTy->isMemberPointerType()) { |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 8983 | assert(IsEquality && "unexpected member pointer operation"); |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 8984 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 8985 | |
| 8986 | MemberPtr LHSValue, RHSValue; |
| 8987 | |
| 8988 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8989 | if (!LHSOK && !Info.noteFailure()) |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 8990 | return false; |
| 8991 | |
| 8992 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 8993 | return false; |
| 8994 | |
| 8995 | // C++11 [expr.eq]p2: |
| 8996 | // If both operands are null, they compare equal. Otherwise if only one is |
| 8997 | // null, they compare unequal. |
| 8998 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 8999 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9000 | return Success(Equal ? CCR::Equal : CCR::Nonequal, E); |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9001 | } |
| 9002 | |
| 9003 | // Otherwise if either is a pointer to a virtual member function, the |
| 9004 | // result is unspecified. |
| 9005 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 9006 | if (MD->isVirtual()) |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9007 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9008 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 9009 | if (MD->isVirtual()) |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9010 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9011 | |
| 9012 | // Otherwise they compare equal if and only if they would refer to the |
| 9013 | // same member of the same most derived object or the same subobject if |
| 9014 | // they were dereferenced with a hypothetical object of the associated |
| 9015 | // class type. |
| 9016 | bool Equal = LHSValue == RHSValue; |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9017 | return Success(Equal ? CCR::Equal : CCR::Nonequal, E); |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 9018 | } |
| 9019 | |
Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 9020 | if (LHSTy->isNullPtrType()) { |
| 9021 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 9022 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 9023 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 9024 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 9025 | // false otherwise. |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9026 | return Success(CCR::Equal, E); |
Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 9027 | } |
| 9028 | |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9029 | return DoAfter(); |
| 9030 | } |
| 9031 | |
| 9032 | bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) { |
| 9033 | if (!CheckLiteralType(Info, E)) |
| 9034 | return false; |
| 9035 | |
| 9036 | auto OnSuccess = [&](ComparisonCategoryResult ResKind, |
| 9037 | const BinaryOperator *E) { |
| 9038 | // Evaluation succeeded. Lookup the information for the comparison category |
| 9039 | // type and fetch the VarDecl for the result. |
| 9040 | const ComparisonCategoryInfo &CmpInfo = |
| 9041 | Info.Ctx.CompCategories.getInfoForType(E->getType()); |
| 9042 | const VarDecl *VD = |
| 9043 | CmpInfo.getValueInfo(CmpInfo.makeWeakResult(ResKind))->VD; |
| 9044 | // Check and evaluate the result as a constant expression. |
| 9045 | LValue LV; |
| 9046 | LV.set(VD); |
| 9047 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
| 9048 | return false; |
| 9049 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
| 9050 | }; |
| 9051 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
| 9052 | return ExprEvaluatorBaseTy::VisitBinCmp(E); |
| 9053 | }); |
| 9054 | } |
| 9055 | |
| 9056 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
| 9057 | // We don't call noteFailure immediately because the assignment happens after |
| 9058 | // we evaluate LHS and RHS. |
| 9059 | if (!Info.keepEvaluatingAfterFailure() && E->isAssignmentOp()) |
| 9060 | return Error(E); |
| 9061 | |
| 9062 | DelayedNoteFailureRAII MaybeNoteFailureLater(Info, E->isAssignmentOp()); |
| 9063 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 9064 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
| 9065 | |
| 9066 | assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() || |
| 9067 | !E->getRHS()->getType()->isIntegralOrEnumerationType()) && |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9068 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 9069 | |
| 9070 | if (E->isComparisonOp()) { |
| 9071 | // Evaluate builtin binary comparisons by evaluating them as C++2a three-way |
| 9072 | // comparisons and then translating the result. |
| 9073 | auto OnSuccess = [&](ComparisonCategoryResult ResKind, |
| 9074 | const BinaryOperator *E) { |
| 9075 | using CCR = ComparisonCategoryResult; |
| 9076 | bool IsEqual = ResKind == CCR::Equal, |
| 9077 | IsLess = ResKind == CCR::Less, |
| 9078 | IsGreater = ResKind == CCR::Greater; |
| 9079 | auto Op = E->getOpcode(); |
| 9080 | switch (Op) { |
| 9081 | default: |
| 9082 | llvm_unreachable("unsupported binary operator"); |
| 9083 | case BO_EQ: |
| 9084 | case BO_NE: |
| 9085 | return Success(IsEqual == (Op == BO_EQ), E); |
| 9086 | case BO_LT: return Success(IsLess, E); |
| 9087 | case BO_GT: return Success(IsGreater, E); |
| 9088 | case BO_LE: return Success(IsEqual || IsLess, E); |
| 9089 | case BO_GE: return Success(IsEqual || IsGreater, E); |
| 9090 | } |
| 9091 | }; |
| 9092 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
| 9093 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 9094 | }); |
| 9095 | } |
| 9096 | |
| 9097 | QualType LHSTy = E->getLHS()->getType(); |
| 9098 | QualType RHSTy = E->getRHS()->getType(); |
| 9099 | |
| 9100 | if (LHSTy->isPointerType() && RHSTy->isPointerType() && |
| 9101 | E->getOpcode() == BO_Sub) { |
| 9102 | LValue LHSValue, RHSValue; |
| 9103 | |
| 9104 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
| 9105 | if (!LHSOK && !Info.noteFailure()) |
| 9106 | return false; |
| 9107 | |
| 9108 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 9109 | return false; |
| 9110 | |
| 9111 | // Reject differing bases from the normal codepath; we special-case |
| 9112 | // comparisons to null. |
| 9113 | if (!HasSameBase(LHSValue, RHSValue)) { |
| 9114 | // Handle &&A - &&B. |
| 9115 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
| 9116 | return Error(E); |
| 9117 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>(); |
| 9118 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>(); |
| 9119 | if (!LHSExpr || !RHSExpr) |
| 9120 | return Error(E); |
| 9121 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 9122 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 9123 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 9124 | return Error(E); |
| 9125 | // Make sure both labels come from the same function. |
| 9126 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 9127 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 9128 | return Error(E); |
| 9129 | return Success(APValue(LHSAddrExpr, RHSAddrExpr), E); |
| 9130 | } |
| 9131 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 9132 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 9133 | |
| 9134 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 9135 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 9136 | |
| 9137 | // C++11 [expr.add]p6: |
| 9138 | // Unless both pointers point to elements of the same array object, or |
| 9139 | // one past the last element of the array object, the behavior is |
| 9140 | // undefined. |
| 9141 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 9142 | !AreElementsOfSameArray(getType(LHSValue.Base), LHSDesignator, |
| 9143 | RHSDesignator)) |
| 9144 | Info.CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 9145 | |
| 9146 | QualType Type = E->getLHS()->getType(); |
| 9147 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
| 9148 | |
| 9149 | CharUnits ElementSize; |
| 9150 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
| 9151 | return false; |
| 9152 | |
| 9153 | // As an extension, a type may have zero size (empty struct or union in |
| 9154 | // C, array of zero length). Pointer subtraction in such cases has |
| 9155 | // undefined behavior, so is not constant. |
| 9156 | if (ElementSize.isZero()) { |
| 9157 | Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
| 9158 | << ElementType; |
| 9159 | return false; |
| 9160 | } |
| 9161 | |
| 9162 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 9163 | // and produce incorrect results when it overflows. Such behavior |
| 9164 | // appears to be non-conforming, but is common, so perhaps we should |
| 9165 | // assume the standard intended for such cases to be undefined behavior |
| 9166 | // and check for them. |
| 9167 | |
| 9168 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 9169 | // overflow in the final conversion to ptrdiff_t. |
| 9170 | APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 9171 | APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 9172 | APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), |
| 9173 | false); |
| 9174 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 9175 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 9176 | |
| 9177 | if (Result.extend(65) != TrueResult && |
| 9178 | !HandleOverflow(Info, E, TrueResult, E->getType())) |
| 9179 | return false; |
| 9180 | return Success(Result, E); |
| 9181 | } |
| 9182 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 9183 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9184 | } |
| 9185 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9186 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 9187 | /// a result as the expression's type. |
| 9188 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 9189 | const UnaryExprOrTypeTraitExpr *E) { |
| 9190 | switch(E->getKind()) { |
| 9191 | case UETT_AlignOf: { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 9192 | if (E->isArgumentType()) |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 9193 | return Success(GetAlignOfType(Info, E->getArgumentType()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 9194 | else |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 9195 | return Success(GetAlignOfExpr(Info, E->getArgumentExpr()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 9196 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 9197 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9198 | case UETT_VecStep: { |
| 9199 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 9200 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9201 | if (Ty->isVectorType()) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 9202 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 9203 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9204 | // The vec_step built-in functions that take a 3-component |
| 9205 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 9206 | if (n == 3) |
| 9207 | n = 4; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 9208 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9209 | return Success(n, E); |
| 9210 | } else |
| 9211 | return Success(1, E); |
| 9212 | } |
| 9213 | |
| 9214 | case UETT_SizeOf: { |
| 9215 | QualType SrcTy = E->getTypeOfArgument(); |
| 9216 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 9217 | // the result is the size of the referenced type." |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9218 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 9219 | SrcTy = Ref->getPointeeType(); |
| 9220 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9221 | CharUnits Sizeof; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 9222 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9223 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 9224 | return Success(Sizeof, E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9225 | } |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 9226 | case UETT_OpenMPRequiredSimdAlign: |
| 9227 | assert(E->isArgumentType()); |
| 9228 | return Success( |
| 9229 | Info.Ctx.toCharUnitsFromBits( |
| 9230 | Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType())) |
| 9231 | .getQuantity(), |
| 9232 | E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 9233 | } |
| 9234 | |
| 9235 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 9236 | } |
| 9237 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9238 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9239 | CharUnits Result; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9240 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9241 | if (n == 0) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9242 | return Error(OOE); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9243 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9244 | for (unsigned i = 0; i != n; ++i) { |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 9245 | OffsetOfNode ON = OOE->getComponent(i); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9246 | switch (ON.getKind()) { |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 9247 | case OffsetOfNode::Array: { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9248 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9249 | APSInt IdxResult; |
| 9250 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 9251 | return false; |
| 9252 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 9253 | if (!AT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9254 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9255 | CurrentType = AT->getElementType(); |
| 9256 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 9257 | Result += IdxResult.getSExtValue() * ElementSize; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 9258 | break; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9259 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9260 | |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 9261 | case OffsetOfNode::Field: { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9262 | FieldDecl *MemberDecl = ON.getField(); |
| 9263 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9264 | if (!RT) |
| 9265 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9266 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 9267 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9268 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 9269 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9270 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 9271 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9272 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9273 | break; |
| 9274 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9275 | |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 9276 | case OffsetOfNode::Identifier: |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9277 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9278 | |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 9279 | case OffsetOfNode::Base: { |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9280 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 9281 | if (BaseSpec->isVirtual()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9282 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9283 | |
| 9284 | // Find the layout of the class whose base we are looking into. |
| 9285 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9286 | if (!RT) |
| 9287 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9288 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 9289 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9290 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 9291 | |
| 9292 | // Find the base class itself. |
| 9293 | CurrentType = BaseSpec->getType(); |
| 9294 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 9295 | if (!BaseRT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9296 | return Error(OOE); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 9297 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9298 | // Add the offset to the base. |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 9299 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9300 | break; |
| 9301 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9302 | } |
| 9303 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9304 | return Success(Result, OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9305 | } |
| 9306 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 9307 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9308 | switch (E->getOpcode()) { |
| 9309 | default: |
| 9310 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 9311 | // See C99 6.6p3. |
| 9312 | return Error(E); |
| 9313 | case UO_Extension: |
| 9314 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 9315 | // If so, we could clear the diagnostic ID. |
| 9316 | return Visit(E->getSubExpr()); |
| 9317 | case UO_Plus: |
| 9318 | // The result is just the value. |
| 9319 | return Visit(E->getSubExpr()); |
| 9320 | case UO_Minus: { |
| 9321 | if (!Visit(E->getSubExpr())) |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame] | 9322 | return false; |
| 9323 | if (!Result.isInt()) return Error(E); |
| 9324 | const APSInt &Value = Result.getInt(); |
| 9325 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() && |
| 9326 | !HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 9327 | E->getType())) |
| 9328 | return false; |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 9329 | return Success(-Value, E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9330 | } |
| 9331 | case UO_Not: { |
| 9332 | if (!Visit(E->getSubExpr())) |
| 9333 | return false; |
| 9334 | if (!Result.isInt()) return Error(E); |
| 9335 | return Success(~Result.getInt(), E); |
| 9336 | } |
| 9337 | case UO_LNot: { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 9338 | bool bres; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9339 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 9340 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 9341 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 9342 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9343 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9344 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9345 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9346 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 9347 | /// result type is integer. |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9348 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 9349 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 9350 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 9351 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 9352 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9353 | switch (E->getCastKind()) { |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9354 | case CK_BaseToDerived: |
| 9355 | case CK_DerivedToBase: |
| 9356 | case CK_UncheckedDerivedToBase: |
| 9357 | case CK_Dynamic: |
| 9358 | case CK_ToUnion: |
| 9359 | case CK_ArrayToPointerDecay: |
| 9360 | case CK_FunctionToPointerDecay: |
| 9361 | case CK_NullToPointer: |
| 9362 | case CK_NullToMemberPointer: |
| 9363 | case CK_BaseToDerivedMemberPointer: |
| 9364 | case CK_DerivedToBaseMemberPointer: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 9365 | case CK_ReinterpretMemberPointer: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9366 | case CK_ConstructorConversion: |
| 9367 | case CK_IntegralToPointer: |
| 9368 | case CK_ToVoid: |
| 9369 | case CK_VectorSplat: |
| 9370 | case CK_IntegralToFloating: |
| 9371 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 9372 | case CK_CPointerToObjCPointerCast: |
| 9373 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9374 | case CK_AnyPointerToBlockPointerCast: |
| 9375 | case CK_ObjCObjectLValueCast: |
| 9376 | case CK_FloatingRealToComplex: |
| 9377 | case CK_FloatingComplexToReal: |
| 9378 | case CK_FloatingComplexCast: |
| 9379 | case CK_FloatingComplexToIntegralComplex: |
| 9380 | case CK_IntegralRealToComplex: |
| 9381 | case CK_IntegralComplexCast: |
| 9382 | case CK_IntegralComplexToFloatingComplex: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 9383 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 9384 | case CK_ZeroToOCLEvent: |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 9385 | case CK_ZeroToOCLQueue: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9386 | case CK_NonAtomicToAtomic: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 9387 | case CK_AddressSpaceConversion: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 9388 | case CK_IntToOCLSampler: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9389 | llvm_unreachable("invalid cast kind for integral value"); |
| 9390 | |
Eli Friedman | 9faf2f9 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 9391 | case CK_BitCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9392 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9393 | case CK_LValueBitCast: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 9394 | case CK_ARCProduceObject: |
| 9395 | case CK_ARCConsumeObject: |
| 9396 | case CK_ARCReclaimReturnedObject: |
| 9397 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9398 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9399 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9400 | |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 9401 | case CK_UserDefinedConversion: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9402 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 9403 | case CK_AtomicToNonAtomic: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9404 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9405 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9406 | |
| 9407 | case CK_MemberPointerToBoolean: |
| 9408 | case CK_PointerToBoolean: |
| 9409 | case CK_IntegralToBoolean: |
| 9410 | case CK_FloatingToBoolean: |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 9411 | case CK_BooleanToSignedIntegral: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9412 | case CK_FloatingComplexToBoolean: |
| 9413 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9414 | bool BoolResult; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9415 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9416 | return false; |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 9417 | uint64_t IntResult = BoolResult; |
| 9418 | if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral) |
| 9419 | IntResult = (uint64_t)-1; |
| 9420 | return Success(IntResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9421 | } |
| 9422 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9423 | case CK_IntegralCast: { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9424 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 9425 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 9426 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 9427 | if (!Result.isInt()) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 9428 | // Allow casts of address-of-label differences if they are no-ops |
| 9429 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 9430 | // be constant-evaluatable except in some narrow cases which are hard |
| 9431 | // to detect here. We let it through on the assumption the user knows |
| 9432 | // what they are doing.) |
| 9433 | if (Result.isAddrLabelDiff()) |
| 9434 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 9435 | // Only allow casts of lvalues if they are lossless. |
| 9436 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 9437 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 9438 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9439 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 9440 | Result.getInt()), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9441 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9442 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9443 | case CK_PointerToIntegral: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 9444 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 9445 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 9446 | LValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 9447 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 9448 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9449 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 9450 | if (LV.getLValueBase()) { |
| 9451 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9452 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 9453 | // narrowing back down to pointer width in subsequent integral casts. |
| 9454 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 9455 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9456 | return Error(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9457 | |
Richard Smith | cf74da7 | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 9458 | LV.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 9459 | LV.moveInto(Result); |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 9460 | return true; |
| 9461 | } |
| 9462 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 9463 | uint64_t V; |
| 9464 | if (LV.isNullPointer()) |
| 9465 | V = Info.Ctx.getTargetNullPointerValue(SrcType); |
| 9466 | else |
| 9467 | V = LV.getLValueOffset().getQuantity(); |
| 9468 | |
| 9469 | APSInt AsInt = Info.Ctx.MakeIntValue(V, SrcType); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9470 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 9471 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9472 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9473 | case CK_IntegralComplexToReal: { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9474 | ComplexValue C; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 9475 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 9476 | return false; |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9477 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 9478 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 9479 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9480 | case CK_FloatingToIntegral: { |
| 9481 | APFloat F(0.0); |
| 9482 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 9483 | return false; |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9484 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9485 | APSInt Value; |
| 9486 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 9487 | return false; |
| 9488 | return Success(Value, E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9489 | } |
| 9490 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9491 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9492 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9493 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 9494 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9495 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 9496 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9497 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9498 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 9499 | return false; |
| 9500 | if (!LV.isComplexInt()) |
| 9501 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9502 | return Success(LV.getComplexIntReal(), E); |
| 9503 | } |
| 9504 | |
| 9505 | return Visit(E->getSubExpr()); |
| 9506 | } |
| 9507 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 9508 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9509 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9510 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9511 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 9512 | return false; |
| 9513 | if (!LV.isComplexInt()) |
| 9514 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9515 | return Success(LV.getComplexIntImag(), E); |
| 9516 | } |
| 9517 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 9518 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 9519 | return Success(0, E); |
| 9520 | } |
| 9521 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 9522 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 9523 | return Success(E->getPackLength(), E); |
| 9524 | } |
| 9525 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 9526 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 9527 | return Success(E->getValue(), E); |
| 9528 | } |
| 9529 | |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 9530 | bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 9531 | switch (E->getOpcode()) { |
| 9532 | default: |
| 9533 | // Invalid unary operators |
| 9534 | return Error(E); |
| 9535 | case UO_Plus: |
| 9536 | // The result is just the value. |
| 9537 | return Visit(E->getSubExpr()); |
| 9538 | case UO_Minus: { |
| 9539 | if (!Visit(E->getSubExpr())) return false; |
| 9540 | if (!Result.isInt()) return Error(E); |
| 9541 | const APSInt &Value = Result.getInt(); |
| 9542 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow()) { |
| 9543 | SmallString<64> S; |
| 9544 | FixedPointValueToString(S, Value, |
| 9545 | Info.Ctx.getTypeInfo(E->getType()).Width, |
| 9546 | /*Radix=*/10); |
| 9547 | Info.CCEDiag(E, diag::note_constexpr_overflow) << S << E->getType(); |
| 9548 | if (Info.noteUndefinedBehavior()) return false; |
| 9549 | } |
| 9550 | return Success(-Value, E); |
| 9551 | } |
| 9552 | case UO_LNot: { |
| 9553 | bool bres; |
| 9554 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
| 9555 | return false; |
| 9556 | return Success(!bres, E); |
| 9557 | } |
| 9558 | } |
| 9559 | } |
| 9560 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 9561 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9562 | // Float Evaluation |
| 9563 | //===----------------------------------------------------------------------===// |
| 9564 | |
| 9565 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 9566 | class FloatExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 9567 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9568 | APFloat &Result; |
| 9569 | public: |
| 9570 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9571 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9572 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9573 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9574 | Result = V.getFloat(); |
| 9575 | return true; |
| 9576 | } |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9577 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9578 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 9579 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 9580 | return true; |
| 9581 | } |
| 9582 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9583 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9584 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9585 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9586 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 9587 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9588 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 9589 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 9590 | bool VisitUnaryReal(const UnaryOperator *E); |
| 9591 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 9592 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9593 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9594 | }; |
| 9595 | } // end anonymous namespace |
| 9596 | |
| 9597 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9598 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9599 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9600 | } |
| 9601 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 9602 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 9603 | QualType ResultTy, |
| 9604 | const Expr *Arg, |
| 9605 | bool SNaN, |
| 9606 | llvm::APFloat &Result) { |
| 9607 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 9608 | if (!S) return false; |
| 9609 | |
| 9610 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 9611 | |
| 9612 | llvm::APInt fill; |
| 9613 | |
| 9614 | // Treat empty strings as if they were zero. |
| 9615 | if (S->getString().empty()) |
| 9616 | fill = llvm::APInt(32, 0); |
| 9617 | else if (S->getString().getAsInteger(0, fill)) |
| 9618 | return false; |
| 9619 | |
Petar Jovanovic | d55ae6b | 2015-02-26 18:19:22 +0000 | [diff] [blame] | 9620 | if (Context.getTargetInfo().isNan2008()) { |
| 9621 | if (SNaN) |
| 9622 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 9623 | else |
| 9624 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 9625 | } else { |
| 9626 | // Prior to IEEE 754-2008, architectures were allowed to choose whether |
| 9627 | // the first bit of their significand was set for qNaN or sNaN. MIPS chose |
| 9628 | // a different encoding to what became a standard in 2008, and for pre- |
| 9629 | // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as |
| 9630 | // sNaN. This is now known as "legacy NaN" encoding. |
| 9631 | if (SNaN) |
| 9632 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 9633 | else |
| 9634 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 9635 | } |
| 9636 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 9637 | return true; |
| 9638 | } |
| 9639 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9640 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 9641 | switch (E->getBuiltinCallee()) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9642 | default: |
| 9643 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 9644 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9645 | case Builtin::BI__builtin_huge_val: |
| 9646 | case Builtin::BI__builtin_huge_valf: |
| 9647 | case Builtin::BI__builtin_huge_vall: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9648 | case Builtin::BI__builtin_huge_valf128: |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9649 | case Builtin::BI__builtin_inf: |
| 9650 | case Builtin::BI__builtin_inff: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9651 | case Builtin::BI__builtin_infl: |
| 9652 | case Builtin::BI__builtin_inff128: { |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 9653 | const llvm::fltSemantics &Sem = |
| 9654 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 9655 | Result = llvm::APFloat::getInf(Sem); |
| 9656 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 9657 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9658 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 9659 | case Builtin::BI__builtin_nans: |
| 9660 | case Builtin::BI__builtin_nansf: |
| 9661 | case Builtin::BI__builtin_nansl: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9662 | case Builtin::BI__builtin_nansf128: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9663 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 9664 | true, Result)) |
| 9665 | return Error(E); |
| 9666 | return true; |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 9667 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 9668 | case Builtin::BI__builtin_nan: |
| 9669 | case Builtin::BI__builtin_nanf: |
| 9670 | case Builtin::BI__builtin_nanl: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9671 | case Builtin::BI__builtin_nanf128: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 9672 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 9673 | // can't constant fold it. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9674 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 9675 | false, Result)) |
| 9676 | return Error(E); |
| 9677 | return true; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9678 | |
| 9679 | case Builtin::BI__builtin_fabs: |
| 9680 | case Builtin::BI__builtin_fabsf: |
| 9681 | case Builtin::BI__builtin_fabsl: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9682 | case Builtin::BI__builtin_fabsf128: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9683 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 9684 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9685 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9686 | if (Result.isNegative()) |
| 9687 | Result.changeSign(); |
| 9688 | return true; |
| 9689 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 9690 | // FIXME: Builtin::BI__builtin_powi |
| 9691 | // FIXME: Builtin::BI__builtin_powif |
| 9692 | // FIXME: Builtin::BI__builtin_powil |
| 9693 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9694 | case Builtin::BI__builtin_copysign: |
| 9695 | case Builtin::BI__builtin_copysignf: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9696 | case Builtin::BI__builtin_copysignl: |
| 9697 | case Builtin::BI__builtin_copysignf128: { |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9698 | APFloat RHS(0.); |
| 9699 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 9700 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 9701 | return false; |
| 9702 | Result.copySign(RHS); |
| 9703 | return true; |
| 9704 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9705 | } |
| 9706 | } |
| 9707 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 9708 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 9709 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 9710 | ComplexValue CV; |
| 9711 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 9712 | return false; |
| 9713 | Result = CV.FloatReal; |
| 9714 | return true; |
| 9715 | } |
| 9716 | |
| 9717 | return Visit(E->getSubExpr()); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 9718 | } |
| 9719 | |
| 9720 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 9721 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 9722 | ComplexValue CV; |
| 9723 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 9724 | return false; |
| 9725 | Result = CV.FloatImag; |
| 9726 | return true; |
| 9727 | } |
| 9728 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 9729 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 9730 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 9731 | Result = llvm::APFloat::getZero(Sem); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 9732 | return true; |
| 9733 | } |
| 9734 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9735 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9736 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9737 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9738 | case UO_Plus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 9739 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9740 | case UO_Minus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 9741 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 9742 | return false; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9743 | Result.changeSign(); |
| 9744 | return true; |
| 9745 | } |
| 9746 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9747 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9748 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9749 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 9750 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 141fbf3 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 9751 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9752 | APFloat RHS(0.0); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9753 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 9754 | if (!LHSOK && !Info.noteFailure()) |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9755 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 9756 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
| 9757 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9758 | } |
| 9759 | |
| 9760 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 9761 | Result = E->getValue(); |
| 9762 | return true; |
| 9763 | } |
| 9764 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9765 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 9766 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9767 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 9768 | switch (E->getCastKind()) { |
| 9769 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9770 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 9771 | |
| 9772 | case CK_IntegralToFloating: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9773 | APSInt IntResult; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9774 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 9775 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 9776 | E->getType(), Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9777 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 9778 | |
| 9779 | case CK_FloatingCast: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9780 | if (!Visit(SubExpr)) |
| 9781 | return false; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9782 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 9783 | Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9784 | } |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 9785 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 9786 | case CK_FloatingComplexToReal: { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 9787 | ComplexValue V; |
| 9788 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 9789 | return false; |
| 9790 | Result = V.getComplexFloatReal(); |
| 9791 | return true; |
| 9792 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 9793 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9794 | } |
| 9795 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9796 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 9797 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9798 | //===----------------------------------------------------------------------===// |
| 9799 | |
| 9800 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 9801 | class ComplexExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 9802 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9803 | ComplexValue &Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9804 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9805 | public: |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9806 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9807 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 9808 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9809 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9810 | Result.setFrom(V); |
| 9811 | return true; |
| 9812 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9813 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 9814 | bool ZeroInitialization(const Expr *E); |
| 9815 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9816 | //===--------------------------------------------------------------------===// |
| 9817 | // Visitor Methods |
| 9818 | //===--------------------------------------------------------------------===// |
| 9819 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9820 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9821 | bool VisitCastExpr(const CastExpr *E); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9822 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 9823 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 9824 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9825 | }; |
| 9826 | } // end anonymous namespace |
| 9827 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9828 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 9829 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9830 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9831 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9832 | } |
| 9833 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 9834 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 9835 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 9836 | if (ElemTy->isRealFloatingType()) { |
| 9837 | Result.makeComplexFloat(); |
| 9838 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 9839 | Result.FloatReal = Zero; |
| 9840 | Result.FloatImag = Zero; |
| 9841 | } else { |
| 9842 | Result.makeComplexInt(); |
| 9843 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 9844 | Result.IntReal = Zero; |
| 9845 | Result.IntImag = Zero; |
| 9846 | } |
| 9847 | return true; |
| 9848 | } |
| 9849 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9850 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 9851 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9852 | |
| 9853 | if (SubExpr->getType()->isRealFloatingType()) { |
| 9854 | Result.makeComplexFloat(); |
| 9855 | APFloat &Imag = Result.FloatImag; |
| 9856 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 9857 | return false; |
| 9858 | |
| 9859 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 9860 | return true; |
| 9861 | } else { |
| 9862 | assert(SubExpr->getType()->isIntegerType() && |
| 9863 | "Unexpected imaginary literal."); |
| 9864 | |
| 9865 | Result.makeComplexInt(); |
| 9866 | APSInt &Imag = Result.IntImag; |
| 9867 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 9868 | return false; |
| 9869 | |
| 9870 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 9871 | return true; |
| 9872 | } |
| 9873 | } |
| 9874 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9875 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9876 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9877 | switch (E->getCastKind()) { |
| 9878 | case CK_BitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9879 | case CK_BaseToDerived: |
| 9880 | case CK_DerivedToBase: |
| 9881 | case CK_UncheckedDerivedToBase: |
| 9882 | case CK_Dynamic: |
| 9883 | case CK_ToUnion: |
| 9884 | case CK_ArrayToPointerDecay: |
| 9885 | case CK_FunctionToPointerDecay: |
| 9886 | case CK_NullToPointer: |
| 9887 | case CK_NullToMemberPointer: |
| 9888 | case CK_BaseToDerivedMemberPointer: |
| 9889 | case CK_DerivedToBaseMemberPointer: |
| 9890 | case CK_MemberPointerToBoolean: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 9891 | case CK_ReinterpretMemberPointer: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9892 | case CK_ConstructorConversion: |
| 9893 | case CK_IntegralToPointer: |
| 9894 | case CK_PointerToIntegral: |
| 9895 | case CK_PointerToBoolean: |
| 9896 | case CK_ToVoid: |
| 9897 | case CK_VectorSplat: |
| 9898 | case CK_IntegralCast: |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 9899 | case CK_BooleanToSignedIntegral: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9900 | case CK_IntegralToBoolean: |
| 9901 | case CK_IntegralToFloating: |
| 9902 | case CK_FloatingToIntegral: |
| 9903 | case CK_FloatingToBoolean: |
| 9904 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 9905 | case CK_CPointerToObjCPointerCast: |
| 9906 | case CK_BlockPointerToObjCPointerCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9907 | case CK_AnyPointerToBlockPointerCast: |
| 9908 | case CK_ObjCObjectLValueCast: |
| 9909 | case CK_FloatingComplexToReal: |
| 9910 | case CK_FloatingComplexToBoolean: |
| 9911 | case CK_IntegralComplexToReal: |
| 9912 | case CK_IntegralComplexToBoolean: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 9913 | case CK_ARCProduceObject: |
| 9914 | case CK_ARCConsumeObject: |
| 9915 | case CK_ARCReclaimReturnedObject: |
| 9916 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9917 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 9918 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 9919 | case CK_ZeroToOCLEvent: |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 9920 | case CK_ZeroToOCLQueue: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9921 | case CK_NonAtomicToAtomic: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 9922 | case CK_AddressSpaceConversion: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 9923 | case CK_IntToOCLSampler: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9924 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 9925 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9926 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 9927 | case CK_AtomicToNonAtomic: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9928 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9929 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9930 | |
| 9931 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9932 | case CK_LValueBitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9933 | case CK_UserDefinedConversion: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9934 | return Error(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9935 | |
| 9936 | case CK_FloatingRealToComplex: { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9937 | APFloat &Real = Result.FloatReal; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9938 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9939 | return false; |
| 9940 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9941 | Result.makeComplexFloat(); |
| 9942 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 9943 | return true; |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9944 | } |
| 9945 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9946 | case CK_FloatingComplexCast: { |
| 9947 | if (!Visit(E->getSubExpr())) |
| 9948 | return false; |
| 9949 | |
| 9950 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 9951 | QualType From |
| 9952 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 9953 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9954 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 9955 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9956 | } |
| 9957 | |
| 9958 | case CK_FloatingComplexToIntegralComplex: { |
| 9959 | if (!Visit(E->getSubExpr())) |
| 9960 | return false; |
| 9961 | |
| 9962 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 9963 | QualType From |
| 9964 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 9965 | Result.makeComplexInt(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9966 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 9967 | To, Result.IntReal) && |
| 9968 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 9969 | To, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9970 | } |
| 9971 | |
| 9972 | case CK_IntegralRealToComplex: { |
| 9973 | APSInt &Real = Result.IntReal; |
| 9974 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 9975 | return false; |
| 9976 | |
| 9977 | Result.makeComplexInt(); |
| 9978 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 9979 | return true; |
| 9980 | } |
| 9981 | |
| 9982 | case CK_IntegralComplexCast: { |
| 9983 | if (!Visit(E->getSubExpr())) |
| 9984 | return false; |
| 9985 | |
| 9986 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 9987 | QualType From |
| 9988 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 9989 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9990 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 9991 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9992 | return true; |
| 9993 | } |
| 9994 | |
| 9995 | case CK_IntegralComplexToFloatingComplex: { |
| 9996 | if (!Visit(E->getSubExpr())) |
| 9997 | return false; |
| 9998 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 9999 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10000 | QualType From |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 10001 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10002 | Result.makeComplexFloat(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10003 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 10004 | To, Result.FloatReal) && |
| 10005 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 10006 | To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 10007 | } |
| 10008 | } |
| 10009 | |
| 10010 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 10011 | } |
| 10012 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10013 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 10014 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 10f4d06 | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 10015 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 10016 | |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10017 | // Track whether the LHS or RHS is real at the type system level. When this is |
| 10018 | // the case we can simplify our evaluation strategy. |
| 10019 | bool LHSReal = false, RHSReal = false; |
| 10020 | |
| 10021 | bool LHSOK; |
| 10022 | if (E->getLHS()->getType()->isRealFloatingType()) { |
| 10023 | LHSReal = true; |
| 10024 | APFloat &Real = Result.FloatReal; |
| 10025 | LHSOK = EvaluateFloat(E->getLHS(), Real, Info); |
| 10026 | if (LHSOK) { |
| 10027 | Result.makeComplexFloat(); |
| 10028 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 10029 | } |
| 10030 | } else { |
| 10031 | LHSOK = Visit(E->getLHS()); |
| 10032 | } |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 10033 | if (!LHSOK && !Info.noteFailure()) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10034 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10035 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10036 | ComplexValue RHS; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10037 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 10038 | RHSReal = true; |
| 10039 | APFloat &Real = RHS.FloatReal; |
| 10040 | if (!EvaluateFloat(E->getRHS(), Real, Info) || !LHSOK) |
| 10041 | return false; |
| 10042 | RHS.makeComplexFloat(); |
| 10043 | RHS.FloatImag = APFloat(Real.getSemantics()); |
| 10044 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10045 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10046 | |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10047 | assert(!(LHSReal && RHSReal) && |
| 10048 | "Cannot have both operands of a complex operation be real."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 10049 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10050 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10051 | case BO_Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10052 | if (Result.isComplexFloat()) { |
| 10053 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 10054 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10055 | if (LHSReal) |
| 10056 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 10057 | else if (!RHSReal) |
| 10058 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 10059 | APFloat::rmNearestTiesToEven); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10060 | } else { |
| 10061 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 10062 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 10063 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10064 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10065 | case BO_Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10066 | if (Result.isComplexFloat()) { |
| 10067 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 10068 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10069 | if (LHSReal) { |
| 10070 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 10071 | Result.getComplexFloatImag().changeSign(); |
| 10072 | } else if (!RHSReal) { |
| 10073 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 10074 | APFloat::rmNearestTiesToEven); |
| 10075 | } |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 10076 | } else { |
| 10077 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 10078 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 10079 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10080 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10081 | case BO_Mul: |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10082 | if (Result.isComplexFloat()) { |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10083 | // This is an implementation of complex multiplication according to the |
Hiroshi Inoue | 0c2734f | 2017-07-05 05:37:45 +0000 | [diff] [blame] | 10084 | // constraints laid out in C11 Annex G. The implemention uses the |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10085 | // following naming scheme: |
| 10086 | // (a + ib) * (c + id) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10087 | ComplexValue LHS = Result; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10088 | APFloat &A = LHS.getComplexFloatReal(); |
| 10089 | APFloat &B = LHS.getComplexFloatImag(); |
| 10090 | APFloat &C = RHS.getComplexFloatReal(); |
| 10091 | APFloat &D = RHS.getComplexFloatImag(); |
| 10092 | APFloat &ResR = Result.getComplexFloatReal(); |
| 10093 | APFloat &ResI = Result.getComplexFloatImag(); |
| 10094 | if (LHSReal) { |
| 10095 | assert(!RHSReal && "Cannot have two real operands for a complex op!"); |
| 10096 | ResR = A * C; |
| 10097 | ResI = A * D; |
| 10098 | } else if (RHSReal) { |
| 10099 | ResR = C * A; |
| 10100 | ResI = C * B; |
| 10101 | } else { |
| 10102 | // In the fully general case, we need to handle NaNs and infinities |
| 10103 | // robustly. |
| 10104 | APFloat AC = A * C; |
| 10105 | APFloat BD = B * D; |
| 10106 | APFloat AD = A * D; |
| 10107 | APFloat BC = B * C; |
| 10108 | ResR = AC - BD; |
| 10109 | ResI = AD + BC; |
| 10110 | if (ResR.isNaN() && ResI.isNaN()) { |
| 10111 | bool Recalc = false; |
| 10112 | if (A.isInfinity() || B.isInfinity()) { |
| 10113 | A = APFloat::copySign( |
| 10114 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 10115 | B = APFloat::copySign( |
| 10116 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 10117 | if (C.isNaN()) |
| 10118 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 10119 | if (D.isNaN()) |
| 10120 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 10121 | Recalc = true; |
| 10122 | } |
| 10123 | if (C.isInfinity() || D.isInfinity()) { |
| 10124 | C = APFloat::copySign( |
| 10125 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 10126 | D = APFloat::copySign( |
| 10127 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 10128 | if (A.isNaN()) |
| 10129 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 10130 | if (B.isNaN()) |
| 10131 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 10132 | Recalc = true; |
| 10133 | } |
| 10134 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
| 10135 | AD.isInfinity() || BC.isInfinity())) { |
| 10136 | if (A.isNaN()) |
| 10137 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 10138 | if (B.isNaN()) |
| 10139 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 10140 | if (C.isNaN()) |
| 10141 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 10142 | if (D.isNaN()) |
| 10143 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 10144 | Recalc = true; |
| 10145 | } |
| 10146 | if (Recalc) { |
| 10147 | ResR = APFloat::getInf(A.getSemantics()) * (A * C - B * D); |
| 10148 | ResI = APFloat::getInf(A.getSemantics()) * (A * D + B * C); |
| 10149 | } |
| 10150 | } |
| 10151 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10152 | } else { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10153 | ComplexValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10154 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10155 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 10156 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10157 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 10158 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 10159 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 10160 | } |
| 10161 | break; |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10162 | case BO_Div: |
| 10163 | if (Result.isComplexFloat()) { |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10164 | // This is an implementation of complex division according to the |
Hiroshi Inoue | 0c2734f | 2017-07-05 05:37:45 +0000 | [diff] [blame] | 10165 | // constraints laid out in C11 Annex G. The implemention uses the |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10166 | // following naming scheme: |
| 10167 | // (a + ib) / (c + id) |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10168 | ComplexValue LHS = Result; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10169 | APFloat &A = LHS.getComplexFloatReal(); |
| 10170 | APFloat &B = LHS.getComplexFloatImag(); |
| 10171 | APFloat &C = RHS.getComplexFloatReal(); |
| 10172 | APFloat &D = RHS.getComplexFloatImag(); |
| 10173 | APFloat &ResR = Result.getComplexFloatReal(); |
| 10174 | APFloat &ResI = Result.getComplexFloatImag(); |
| 10175 | if (RHSReal) { |
| 10176 | ResR = A / C; |
| 10177 | ResI = B / C; |
| 10178 | } else { |
| 10179 | if (LHSReal) { |
| 10180 | // No real optimizations we can do here, stub out with zero. |
| 10181 | B = APFloat::getZero(A.getSemantics()); |
| 10182 | } |
| 10183 | int DenomLogB = 0; |
| 10184 | APFloat MaxCD = maxnum(abs(C), abs(D)); |
| 10185 | if (MaxCD.isFinite()) { |
| 10186 | DenomLogB = ilogb(MaxCD); |
Matt Arsenault | c477f48 | 2016-03-13 05:12:47 +0000 | [diff] [blame] | 10187 | C = scalbn(C, -DenomLogB, APFloat::rmNearestTiesToEven); |
| 10188 | D = scalbn(D, -DenomLogB, APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10189 | } |
| 10190 | APFloat Denom = C * C + D * D; |
Matt Arsenault | c477f48 | 2016-03-13 05:12:47 +0000 | [diff] [blame] | 10191 | ResR = scalbn((A * C + B * D) / Denom, -DenomLogB, |
| 10192 | APFloat::rmNearestTiesToEven); |
| 10193 | ResI = scalbn((B * C - A * D) / Denom, -DenomLogB, |
| 10194 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 10195 | if (ResR.isNaN() && ResI.isNaN()) { |
| 10196 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
| 10197 | ResR = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * A; |
| 10198 | ResI = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * B; |
| 10199 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
| 10200 | D.isFinite()) { |
| 10201 | A = APFloat::copySign( |
| 10202 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 10203 | B = APFloat::copySign( |
| 10204 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 10205 | ResR = APFloat::getInf(ResR.getSemantics()) * (A * C + B * D); |
| 10206 | ResI = APFloat::getInf(ResI.getSemantics()) * (B * C - A * D); |
| 10207 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
| 10208 | C = APFloat::copySign( |
| 10209 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 10210 | D = APFloat::copySign( |
| 10211 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 10212 | ResR = APFloat::getZero(ResR.getSemantics()) * (A * C + B * D); |
| 10213 | ResI = APFloat::getZero(ResI.getSemantics()) * (B * C - A * D); |
| 10214 | } |
| 10215 | } |
| 10216 | } |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10217 | } else { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10218 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 10219 | return Error(E, diag::note_expr_divide_by_zero); |
| 10220 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10221 | ComplexValue LHS = Result; |
| 10222 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 10223 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 10224 | Result.getComplexIntReal() = |
| 10225 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 10226 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 10227 | Result.getComplexIntImag() = |
| 10228 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 10229 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 10230 | } |
| 10231 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 10232 | } |
| 10233 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 10234 | return true; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 10235 | } |
| 10236 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10237 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 10238 | // Get the operand value into 'Result'. |
| 10239 | if (!Visit(E->getSubExpr())) |
| 10240 | return false; |
| 10241 | |
| 10242 | switch (E->getOpcode()) { |
| 10243 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10244 | return Error(E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 10245 | case UO_Extension: |
| 10246 | return true; |
| 10247 | case UO_Plus: |
| 10248 | // The result is always just the subexpr. |
| 10249 | return true; |
| 10250 | case UO_Minus: |
| 10251 | if (Result.isComplexFloat()) { |
| 10252 | Result.getComplexFloatReal().changeSign(); |
| 10253 | Result.getComplexFloatImag().changeSign(); |
| 10254 | } |
| 10255 | else { |
| 10256 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 10257 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 10258 | } |
| 10259 | return true; |
| 10260 | case UO_Not: |
| 10261 | if (Result.isComplexFloat()) |
| 10262 | Result.getComplexFloatImag().changeSign(); |
| 10263 | else |
| 10264 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 10265 | return true; |
| 10266 | } |
| 10267 | } |
| 10268 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 10269 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 10270 | if (E->getNumInits() == 2) { |
| 10271 | if (E->getType()->isComplexType()) { |
| 10272 | Result.makeComplexFloat(); |
| 10273 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 10274 | return false; |
| 10275 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 10276 | return false; |
| 10277 | } else { |
| 10278 | Result.makeComplexInt(); |
| 10279 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 10280 | return false; |
| 10281 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 10282 | return false; |
| 10283 | } |
| 10284 | return true; |
| 10285 | } |
| 10286 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 10287 | } |
| 10288 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 10289 | //===----------------------------------------------------------------------===// |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10290 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
| 10291 | // implicit conversion. |
| 10292 | //===----------------------------------------------------------------------===// |
| 10293 | |
| 10294 | namespace { |
| 10295 | class AtomicExprEvaluator : |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 10296 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10297 | const LValue *This; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10298 | APValue &Result; |
| 10299 | public: |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10300 | AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result) |
| 10301 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10302 | |
| 10303 | bool Success(const APValue &V, const Expr *E) { |
| 10304 | Result = V; |
| 10305 | return true; |
| 10306 | } |
| 10307 | |
| 10308 | bool ZeroInitialization(const Expr *E) { |
| 10309 | ImplicitValueInitExpr VIE( |
| 10310 | E->getType()->castAs<AtomicType>()->getValueType()); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10311 | // For atomic-qualified class (and array) types in C++, initialize the |
| 10312 | // _Atomic-wrapped subobject directly, in-place. |
| 10313 | return This ? EvaluateInPlace(Result, Info, *This, &VIE) |
| 10314 | : Evaluate(Result, Info, &VIE); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10315 | } |
| 10316 | |
| 10317 | bool VisitCastExpr(const CastExpr *E) { |
| 10318 | switch (E->getCastKind()) { |
| 10319 | default: |
| 10320 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 10321 | case CK_NonAtomicToAtomic: |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10322 | return This ? EvaluateInPlace(Result, Info, *This, E->getSubExpr()) |
| 10323 | : Evaluate(Result, Info, E->getSubExpr()); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10324 | } |
| 10325 | } |
| 10326 | }; |
| 10327 | } // end anonymous namespace |
| 10328 | |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10329 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
| 10330 | EvalInfo &Info) { |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10331 | assert(E->isRValue() && E->getType()->isAtomicType()); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10332 | return AtomicExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10333 | } |
| 10334 | |
| 10335 | //===----------------------------------------------------------------------===// |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10336 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 10337 | // comma operator |
| 10338 | //===----------------------------------------------------------------------===// |
| 10339 | |
| 10340 | namespace { |
| 10341 | class VoidExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 10342 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10343 | public: |
| 10344 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 10345 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10346 | bool Success(const APValue &V, const Expr *e) { return true; } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10347 | |
Richard Smith | 7cd577b | 2017-08-17 19:35:50 +0000 | [diff] [blame] | 10348 | bool ZeroInitialization(const Expr *E) { return true; } |
| 10349 | |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10350 | bool VisitCastExpr(const CastExpr *E) { |
| 10351 | switch (E->getCastKind()) { |
| 10352 | default: |
| 10353 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 10354 | case CK_ToVoid: |
| 10355 | VisitIgnoredValue(E->getSubExpr()); |
| 10356 | return true; |
| 10357 | } |
| 10358 | } |
Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 10359 | |
| 10360 | bool VisitCallExpr(const CallExpr *E) { |
| 10361 | switch (E->getBuiltinCallee()) { |
| 10362 | default: |
| 10363 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 10364 | case Builtin::BI__assume: |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 10365 | case Builtin::BI__builtin_assume: |
Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 10366 | // The argument is not evaluated! |
| 10367 | return true; |
| 10368 | } |
| 10369 | } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10370 | }; |
| 10371 | } // end anonymous namespace |
| 10372 | |
| 10373 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 10374 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 10375 | return VoidExprEvaluator(Info).Visit(E); |
| 10376 | } |
| 10377 | |
| 10378 | //===----------------------------------------------------------------------===// |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10379 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 10380 | //===----------------------------------------------------------------------===// |
| 10381 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10382 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10383 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 10384 | // are. |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10385 | QualType T = E->getType(); |
| 10386 | if (E->isGLValue() || T->isFunctionType()) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10387 | LValue LV; |
| 10388 | if (!EvaluateLValue(E, LV, Info)) |
| 10389 | return false; |
| 10390 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10391 | } else if (T->isVectorType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10392 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 10393 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10394 | } else if (T->isIntegralOrEnumerationType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10395 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10396 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10397 | } else if (T->hasPointerRepresentation()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10398 | LValue LV; |
| 10399 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10400 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10401 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10402 | } else if (T->isRealFloatingType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10403 | llvm::APFloat F(0.0); |
| 10404 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10405 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10406 | Result = APValue(F); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10407 | } else if (T->isAnyComplexType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10408 | ComplexValue C; |
| 10409 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10410 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10411 | C.moveInto(Result); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 10412 | } else if (T->isFixedPointType()) { |
| 10413 | if (!FixedPointExprEvaluator(Info, Result).Visit(E)) return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10414 | } else if (T->isMemberPointerType()) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 10415 | MemberPtr P; |
| 10416 | if (!EvaluateMemberPointer(E, P, Info)) |
| 10417 | return false; |
| 10418 | P.moveInto(Result); |
| 10419 | return true; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10420 | } else if (T->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10421 | LValue LV; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 10422 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10423 | if (!EvaluateArray(E, LV, Value, Info)) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 10424 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10425 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10426 | } else if (T->isRecordType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10427 | LValue LV; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 10428 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10429 | if (!EvaluateRecord(E, LV, Value, Info)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10430 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10431 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10432 | } else if (T->isVoidType()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10433 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 10434 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10435 | << E->getType(); |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10436 | if (!EvaluateVoid(E, Info)) |
| 10437 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10438 | } else if (T->isAtomicType()) { |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10439 | QualType Unqual = T.getAtomicUnqualifiedType(); |
| 10440 | if (Unqual->isArrayType() || Unqual->isRecordType()) { |
| 10441 | LValue LV; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 10442 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10443 | if (!EvaluateAtomic(E, &LV, Value, Info)) |
| 10444 | return false; |
| 10445 | } else { |
| 10446 | if (!EvaluateAtomic(E, nullptr, Result, Info)) |
| 10447 | return false; |
| 10448 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10449 | } else if (Info.getLangOpts().CPlusPlus11) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 10450 | Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10451 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10452 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 10453 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 10454 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10455 | } |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10456 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 10457 | return true; |
| 10458 | } |
| 10459 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10460 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 10461 | /// cases, the in-place evaluation is essential, since later initializers for |
| 10462 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 10463 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10464 | const Expr *E, bool AllowNonLiteralTypes) { |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 10465 | assert(!E->isValueDependent()); |
| 10466 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10467 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10468 | return false; |
| 10469 | |
| 10470 | if (E->isRValue()) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 10471 | // Evaluate arrays and record types in-place, so that later initializers can |
| 10472 | // refer to earlier-initialized members of the object. |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10473 | QualType T = E->getType(); |
| 10474 | if (T->isArrayType()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10475 | return EvaluateArray(E, This, Result, Info); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10476 | else if (T->isRecordType()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10477 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10478 | else if (T->isAtomicType()) { |
| 10479 | QualType Unqual = T.getAtomicUnqualifiedType(); |
| 10480 | if (Unqual->isArrayType() || Unqual->isRecordType()) |
| 10481 | return EvaluateAtomic(E, &This, Result, Info); |
| 10482 | } |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 10483 | } |
| 10484 | |
| 10485 | // For any other type, in-place evaluation is unimportant. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10486 | return Evaluate(Result, Info, E); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 10487 | } |
| 10488 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10489 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 10490 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 10491 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 10492 | if (E->getType().isNull()) |
| 10493 | return false; |
| 10494 | |
Nick Lewycky | c190f96 | 2017-05-02 01:06:16 +0000 | [diff] [blame] | 10495 | if (!CheckLiteralType(Info, E)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10496 | return false; |
| 10497 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10498 | if (!::Evaluate(Result, Info, E)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10499 | return false; |
| 10500 | |
| 10501 | if (E->isGLValue()) { |
| 10502 | LValue LV; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10503 | LV.setFrom(Info.Ctx, Result); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 10504 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10505 | return false; |
| 10506 | } |
| 10507 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10508 | // Check this core constant expression is a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10509 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10510 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10511 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10512 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 10513 | const ASTContext &Ctx, bool &IsConst) { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10514 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 10515 | // containing vast quantities of these. |
| 10516 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
| 10517 | Result.Val = APValue(APSInt(L->getValue(), |
| 10518 | L->getType()->isUnsignedIntegerType())); |
| 10519 | IsConst = true; |
| 10520 | return true; |
| 10521 | } |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 10522 | |
| 10523 | // This case should be rare, but we need to check it before we check on |
| 10524 | // the type below. |
| 10525 | if (Exp->getType().isNull()) { |
| 10526 | IsConst = false; |
| 10527 | return true; |
| 10528 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 10529 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10530 | // FIXME: Evaluating values of large array and record types can cause |
| 10531 | // performance problems. Only do so in C++11 for now. |
| 10532 | if (Exp->isRValue() && (Exp->getType()->isArrayType() || |
| 10533 | Exp->getType()->isRecordType()) && |
Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 10534 | !Ctx.getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10535 | IsConst = false; |
| 10536 | return true; |
| 10537 | } |
| 10538 | return false; |
| 10539 | } |
| 10540 | |
| 10541 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10542 | /// 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] | 10543 | /// any crazy technique (that has nothing to do with language standards) that |
| 10544 | /// 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] | 10545 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 10546 | /// will be applied to the result. |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10547 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10548 | bool IsConst; |
Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 10549 | if (FastEvaluateAsRValue(this, Result, Ctx, IsConst)) |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10550 | return IsConst; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 10551 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 10552 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10553 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 10554 | } |
| 10555 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 10556 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 10557 | const ASTContext &Ctx) const { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10558 | EvalResult Scratch; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10559 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10560 | HandleConversionToBool(Scratch.Val, Result); |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 10561 | } |
| 10562 | |
Richard Smith | 3f1d6de | 2018-05-21 20:36:58 +0000 | [diff] [blame] | 10563 | static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result, |
| 10564 | Expr::SideEffectsKind SEK) { |
| 10565 | return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) || |
| 10566 | (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior); |
| 10567 | } |
| 10568 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10569 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 10570 | SideEffectsKind AllowSideEffects) const { |
| 10571 | if (!getType()->isIntegralOrEnumerationType()) |
| 10572 | return false; |
| 10573 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10574 | EvalResult ExprResult; |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10575 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
Richard Smith | 3f1d6de | 2018-05-21 20:36:58 +0000 | [diff] [blame] | 10576 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10577 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10578 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10579 | Result = ExprResult.Val.getInt(); |
| 10580 | return true; |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 10581 | } |
| 10582 | |
Richard Trieu | be234c3 | 2016-04-21 21:04:55 +0000 | [diff] [blame] | 10583 | bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx, |
| 10584 | SideEffectsKind AllowSideEffects) const { |
| 10585 | if (!getType()->isRealFloatingType()) |
| 10586 | return false; |
| 10587 | |
| 10588 | EvalResult ExprResult; |
| 10589 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isFloat() || |
Richard Smith | 3f1d6de | 2018-05-21 20:36:58 +0000 | [diff] [blame] | 10590 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
Richard Trieu | be234c3 | 2016-04-21 21:04:55 +0000 | [diff] [blame] | 10591 | return false; |
| 10592 | |
| 10593 | Result = ExprResult.Val.getFloat(); |
| 10594 | return true; |
| 10595 | } |
| 10596 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 10597 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 10598 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 10599 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10600 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10601 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 10602 | !CheckLValueConstantExpression(Info, getExprLoc(), |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 10603 | Ctx.getLValueReferenceType(getType()), LV, |
| 10604 | Expr::EvaluateForCodeGen)) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10605 | return false; |
| 10606 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10607 | LV.moveInto(Result.Val); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10608 | return true; |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 10609 | } |
| 10610 | |
Reid Kleckner | 1a840d2 | 2018-05-10 18:57:35 +0000 | [diff] [blame] | 10611 | bool Expr::EvaluateAsConstantExpr(EvalResult &Result, ConstExprUsage Usage, |
| 10612 | const ASTContext &Ctx) const { |
| 10613 | EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression; |
| 10614 | EvalInfo Info(Ctx, Result, EM); |
| 10615 | if (!::Evaluate(Result.Val, Info, this)) |
| 10616 | return false; |
| 10617 | |
| 10618 | return CheckConstantExpression(Info, getExprLoc(), getType(), Result.Val, |
| 10619 | Usage); |
| 10620 | } |
| 10621 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 10622 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 10623 | const VarDecl *VD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10624 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 10625 | // FIXME: Evaluating initializers for large array and record types can cause |
| 10626 | // performance problems. Only do so in C++11 for now. |
| 10627 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10628 | !Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 10629 | return false; |
| 10630 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 10631 | Expr::EvalStatus EStatus; |
| 10632 | EStatus.Diag = &Notes; |
| 10633 | |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 10634 | EvalInfo InitInfo(Ctx, EStatus, VD->isConstexpr() |
| 10635 | ? EvalInfo::EM_ConstantExpression |
| 10636 | : EvalInfo::EM_ConstantFold); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 10637 | InitInfo.setEvaluatingDecl(VD, Value); |
| 10638 | |
| 10639 | LValue LVal; |
| 10640 | LVal.set(VD); |
| 10641 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10642 | // C++11 [basic.start.init]p2: |
| 10643 | // Variables with static storage duration or thread storage duration shall be |
| 10644 | // zero-initialized before any other initialization takes place. |
| 10645 | // This behavior is not present in C. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10646 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10647 | !VD->getType()->isReferenceType()) { |
| 10648 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10649 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10650 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10651 | return false; |
| 10652 | } |
| 10653 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10654 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, |
| 10655 | /*AllowNonLiteralTypes=*/true) || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10656 | EStatus.HasSideEffects) |
| 10657 | return false; |
| 10658 | |
| 10659 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 10660 | Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 10661 | } |
| 10662 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10663 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 10664 | /// constant folded, but discard the result. |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 10665 | bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 10666 | EvalResult Result; |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 10667 | return EvaluateAsRValue(Result, Ctx) && |
Richard Smith | 3f1d6de | 2018-05-21 20:36:58 +0000 | [diff] [blame] | 10668 | !hasUnacceptableSideEffect(Result, SEK); |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 10669 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 10670 | |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 10671 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10672 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 10673 | EvalResult EvalResult; |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 10674 | EvalResult.Diag = Diag; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10675 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 10676 | (void)Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 10677 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 10678 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 10679 | |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 10680 | return EvalResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 10681 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10682 | |
Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 10683 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10684 | bool IsConst; |
| 10685 | EvalResult EvalResult; |
Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 10686 | if (!FastEvaluateAsRValue(this, EvalResult, Ctx, IsConst)) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 10687 | EvalInfo Info(Ctx, EvalResult, EvalInfo::EM_EvaluateForOverflow); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10688 | (void)::EvaluateAsRValue(Info, this, EvalResult.Val); |
| 10689 | } |
| 10690 | } |
| 10691 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 10692 | bool Expr::EvalResult::isGlobalLValue() const { |
| 10693 | assert(Val.isLValue()); |
| 10694 | return IsGlobalLValue(Val.getLValueBase()); |
| 10695 | } |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 10696 | |
| 10697 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10698 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 10699 | /// an integer constant expression. |
| 10700 | |
| 10701 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 10702 | /// comma, etc |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10703 | |
| 10704 | // CheckICE - This function does the fundamental ICE checking: the returned |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10705 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
| 10706 | // and a (possibly null) SourceLocation indicating the location of the problem. |
| 10707 | // |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10708 | // Note that to reduce code duplication, this helper does no evaluation |
| 10709 | // itself; the caller checks whether the expression is evaluatable, and |
| 10710 | // in the rare cases where CheckICE actually cares about the evaluated |
George Burgess IV | 5731707 | 2017-02-02 07:53:55 +0000 | [diff] [blame] | 10711 | // value, it calls into Evaluate. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10712 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 10713 | namespace { |
| 10714 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10715 | enum ICEKind { |
| 10716 | /// This expression is an ICE. |
| 10717 | IK_ICE, |
| 10718 | /// This expression is not an ICE, but if it isn't evaluated, it's |
| 10719 | /// a legal subexpression for an ICE. This return value is used to handle |
| 10720 | /// the comma operator in C99 mode, and non-constant subexpressions. |
| 10721 | IK_ICEIfUnevaluated, |
| 10722 | /// This expression is not an ICE, and is not a legal subexpression for one. |
| 10723 | IK_NotICE |
| 10724 | }; |
| 10725 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10726 | struct ICEDiag { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10727 | ICEKind Kind; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10728 | SourceLocation Loc; |
| 10729 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10730 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10731 | }; |
| 10732 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 10733 | } |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 10734 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10735 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
| 10736 | |
| 10737 | 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] | 10738 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 10739 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10740 | Expr::EvalResult EVResult; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10741 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10742 | !EVResult.Val.isInt()) |
| 10743 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 10744 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10745 | return NoDiag(); |
| 10746 | } |
| 10747 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 10748 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10749 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10750 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 10751 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10752 | |
| 10753 | switch (E->getStmtClass()) { |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 10754 | #define ABSTRACT_STMT(Node) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10755 | #define STMT(Node, Base) case Expr::Node##Class: |
| 10756 | #define EXPR(Node, Base) |
| 10757 | #include "clang/AST/StmtNodes.inc" |
| 10758 | case Expr::PredefinedExprClass: |
| 10759 | case Expr::FloatingLiteralClass: |
| 10760 | case Expr::ImaginaryLiteralClass: |
| 10761 | case Expr::StringLiteralClass: |
| 10762 | case Expr::ArraySubscriptExprClass: |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 10763 | case Expr::OMPArraySectionExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10764 | case Expr::MemberExprClass: |
| 10765 | case Expr::CompoundAssignOperatorClass: |
| 10766 | case Expr::CompoundLiteralExprClass: |
| 10767 | case Expr::ExtVectorElementExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10768 | case Expr::DesignatedInitExprClass: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 10769 | case Expr::ArrayInitLoopExprClass: |
| 10770 | case Expr::ArrayInitIndexExprClass: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 10771 | case Expr::NoInitExprClass: |
| 10772 | case Expr::DesignatedInitUpdateExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10773 | case Expr::ImplicitValueInitExprClass: |
| 10774 | case Expr::ParenListExprClass: |
| 10775 | case Expr::VAArgExprClass: |
| 10776 | case Expr::AddrLabelExprClass: |
| 10777 | case Expr::StmtExprClass: |
| 10778 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 10779 | case Expr::CUDAKernelCallExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10780 | case Expr::CXXDynamicCastExprClass: |
| 10781 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 5cc0a67 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 10782 | case Expr::CXXUuidofExprClass: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 10783 | case Expr::MSPropertyRefExprClass: |
Alexey Bataev | f763027 | 2015-11-25 12:01:00 +0000 | [diff] [blame] | 10784 | case Expr::MSPropertySubscriptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10785 | case Expr::CXXNullPtrLiteralExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 10786 | case Expr::UserDefinedLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10787 | case Expr::CXXThisExprClass: |
| 10788 | case Expr::CXXThrowExprClass: |
| 10789 | case Expr::CXXNewExprClass: |
| 10790 | case Expr::CXXDeleteExprClass: |
| 10791 | case Expr::CXXPseudoDestructorExprClass: |
| 10792 | case Expr::UnresolvedLookupExprClass: |
Kaelyn Takata | e1f49d5 | 2014-10-27 18:07:20 +0000 | [diff] [blame] | 10793 | case Expr::TypoExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10794 | case Expr::DependentScopeDeclRefExprClass: |
| 10795 | case Expr::CXXConstructExprClass: |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 10796 | case Expr::CXXInheritedCtorInitExprClass: |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 10797 | case Expr::CXXStdInitializerListExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10798 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 10799 | case Expr::ExprWithCleanupsClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10800 | case Expr::CXXTemporaryObjectExprClass: |
| 10801 | case Expr::CXXUnresolvedConstructExprClass: |
| 10802 | case Expr::CXXDependentScopeMemberExprClass: |
| 10803 | case Expr::UnresolvedMemberExprClass: |
| 10804 | case Expr::ObjCStringLiteralClass: |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 10805 | case Expr::ObjCBoxedExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 10806 | case Expr::ObjCArrayLiteralClass: |
| 10807 | case Expr::ObjCDictionaryLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10808 | case Expr::ObjCEncodeExprClass: |
| 10809 | case Expr::ObjCMessageExprClass: |
| 10810 | case Expr::ObjCSelectorExprClass: |
| 10811 | case Expr::ObjCProtocolExprClass: |
| 10812 | case Expr::ObjCIvarRefExprClass: |
| 10813 | case Expr::ObjCPropertyRefExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 10814 | case Expr::ObjCSubscriptRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10815 | case Expr::ObjCIsaExprClass: |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 10816 | case Expr::ObjCAvailabilityCheckExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10817 | case Expr::ShuffleVectorExprClass: |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 10818 | case Expr::ConvertVectorExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10819 | case Expr::BlockExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10820 | case Expr::NoStmtClass: |
John McCall | 8d69a21 | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 10821 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 10822 | case Expr::PackExpansionExprClass: |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 10823 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 10824 | case Expr::FunctionParmPackExprClass: |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 10825 | case Expr::AsTypeExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10826 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 10827 | case Expr::MaterializeTemporaryExprClass: |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 10828 | case Expr::PseudoObjectExprClass: |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 10829 | case Expr::AtomicExprClass: |
Douglas Gregor | e31e606 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 10830 | case Expr::LambdaExprClass: |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 10831 | case Expr::CXXFoldExprClass: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 10832 | case Expr::CoawaitExprClass: |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 10833 | case Expr::DependentCoawaitExprClass: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 10834 | case Expr::CoyieldExprClass: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10835 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 10836 | |
Richard Smith | f137f93 | 2014-01-25 20:50:08 +0000 | [diff] [blame] | 10837 | case Expr::InitListExprClass: { |
| 10838 | // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the |
| 10839 | // form "T x = { a };" is equivalent to "T x = a;". |
| 10840 | // Unless we're initializing a reference, T is a scalar as it is known to be |
| 10841 | // of integral or enumeration type. |
| 10842 | if (E->isRValue()) |
| 10843 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
| 10844 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
| 10845 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 10846 | } |
| 10847 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 10848 | case Expr::SizeOfPackExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10849 | case Expr::GNUNullExprClass: |
| 10850 | // GCC considers the GNU __null value to be an integral constant expression. |
| 10851 | return NoDiag(); |
| 10852 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 10853 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 10854 | return |
| 10855 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 10856 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10857 | case Expr::ParenExprClass: |
| 10858 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 10859 | case Expr::GenericSelectionExprClass: |
| 10860 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10861 | case Expr::IntegerLiteralClass: |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 10862 | case Expr::FixedPointLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10863 | case Expr::CharacterLiteralClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 10864 | case Expr::ObjCBoolLiteralExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10865 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 10866 | case Expr::CXXScalarValueInitExprClass: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 10867 | case Expr::TypeTraitExprClass: |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 10868 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 10869 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 10870 | case Expr::CXXNoexceptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10871 | return NoDiag(); |
| 10872 | case Expr::CallExprClass: |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 10873 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 10874 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 10875 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 10876 | // contain an operand of (pointer to) function type. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10877 | const CallExpr *CE = cast<CallExpr>(E); |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 10878 | if (CE->getBuiltinCallee()) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10879 | return CheckEvalInICE(E, Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10880 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10881 | } |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 10882 | case Expr::DeclRefExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10883 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 10884 | return NoDiag(); |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 10885 | const ValueDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10886 | if (Ctx.getLangOpts().CPlusPlus && |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 10887 | D && IsConstNonVolatile(D->getType())) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10888 | // Parameter variables are never constants. Without this check, |
| 10889 | // getAnyInitializer() can find a default argument, which leads |
| 10890 | // to chaos. |
| 10891 | if (isa<ParmVarDecl>(D)) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10892 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10893 | |
| 10894 | // C++ 7.1.5.1p2 |
| 10895 | // A variable of non-volatile const-qualified integral or enumeration |
| 10896 | // type initialized by an ICE can be used in ICEs. |
| 10897 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 10898 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10899 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 10900 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 10901 | const VarDecl *VD; |
| 10902 | // Look for a declaration of this variable that has an initializer, and |
| 10903 | // check whether it is an ICE. |
| 10904 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 10905 | return NoDiag(); |
| 10906 | else |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10907 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10908 | } |
| 10909 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10910 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 10911 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10912 | case Expr::UnaryOperatorClass: { |
| 10913 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 10914 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10915 | case UO_PostInc: |
| 10916 | case UO_PostDec: |
| 10917 | case UO_PreInc: |
| 10918 | case UO_PreDec: |
| 10919 | case UO_AddrOf: |
| 10920 | case UO_Deref: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 10921 | case UO_Coawait: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 10922 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 10923 | // subexpressions of constant expressions, but they can never be ICEs |
| 10924 | // because an ICE cannot contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10925 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10926 | case UO_Extension: |
| 10927 | case UO_LNot: |
| 10928 | case UO_Plus: |
| 10929 | case UO_Minus: |
| 10930 | case UO_Not: |
| 10931 | case UO_Real: |
| 10932 | case UO_Imag: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10933 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10934 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10935 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10936 | // OffsetOf falls through here. |
Galina Kistanova | f87496d | 2017-06-03 06:31:42 +0000 | [diff] [blame] | 10937 | LLVM_FALLTHROUGH; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10938 | } |
| 10939 | case Expr::OffsetOfExprClass: { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10940 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 10941 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
| 10942 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
| 10943 | // compliance: we should warn earlier for offsetof expressions with |
| 10944 | // array subscripts that aren't ICEs, and if the array subscripts |
| 10945 | // are ICEs, the value of the offsetof must be an integer constant. |
| 10946 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10947 | } |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 10948 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 10949 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 10950 | if ((Exp->getKind() == UETT_SizeOf) && |
| 10951 | Exp->getTypeOfArgument()->isVariableArrayType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10952 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10953 | return NoDiag(); |
| 10954 | } |
| 10955 | case Expr::BinaryOperatorClass: { |
| 10956 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 10957 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10958 | case BO_PtrMemD: |
| 10959 | case BO_PtrMemI: |
| 10960 | case BO_Assign: |
| 10961 | case BO_MulAssign: |
| 10962 | case BO_DivAssign: |
| 10963 | case BO_RemAssign: |
| 10964 | case BO_AddAssign: |
| 10965 | case BO_SubAssign: |
| 10966 | case BO_ShlAssign: |
| 10967 | case BO_ShrAssign: |
| 10968 | case BO_AndAssign: |
| 10969 | case BO_XorAssign: |
| 10970 | case BO_OrAssign: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 10971 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 10972 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 10973 | // contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10974 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10975 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10976 | case BO_Mul: |
| 10977 | case BO_Div: |
| 10978 | case BO_Rem: |
| 10979 | case BO_Add: |
| 10980 | case BO_Sub: |
| 10981 | case BO_Shl: |
| 10982 | case BO_Shr: |
| 10983 | case BO_LT: |
| 10984 | case BO_GT: |
| 10985 | case BO_LE: |
| 10986 | case BO_GE: |
| 10987 | case BO_EQ: |
| 10988 | case BO_NE: |
| 10989 | case BO_And: |
| 10990 | case BO_Xor: |
| 10991 | case BO_Or: |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 10992 | case BO_Comma: |
| 10993 | case BO_Cmp: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10994 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 10995 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10996 | if (Exp->getOpcode() == BO_Div || |
| 10997 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10998 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10999 | // we don't evaluate one. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11000 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11001 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11002 | if (REval == 0) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11003 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11004 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11005 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11006 | if (LEval.isMinSignedValue()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11007 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11008 | } |
| 11009 | } |
| 11010 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11011 | if (Exp->getOpcode() == BO_Comma) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11012 | if (Ctx.getLangOpts().C99) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11013 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 11014 | // if it isn't evaluated. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11015 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
| 11016 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11017 | } else { |
| 11018 | // In both C89 and C++, commas in ICEs are illegal. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11019 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11020 | } |
| 11021 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11022 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11023 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11024 | case BO_LAnd: |
| 11025 | case BO_LOr: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11026 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 11027 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11028 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11029 | // Rare case where the RHS has a comma "side-effect"; we need |
| 11030 | // to actually check the condition to see whether the side |
| 11031 | // with the comma is evaluated. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 11032 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 11033 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11034 | return RHSResult; |
| 11035 | return NoDiag(); |
| 11036 | } |
| 11037 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11038 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11039 | } |
| 11040 | } |
Galina Kistanova | f87496d | 2017-06-03 06:31:42 +0000 | [diff] [blame] | 11041 | LLVM_FALLTHROUGH; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11042 | } |
| 11043 | case Expr::ImplicitCastExprClass: |
| 11044 | case Expr::CStyleCastExprClass: |
| 11045 | case Expr::CXXFunctionalCastExprClass: |
| 11046 | case Expr::CXXStaticCastExprClass: |
| 11047 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | c3e31e7 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 11048 | case Expr::CXXConstCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11049 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11050 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 11051 | if (isa<ExplicitCastExpr>(E)) { |
| 11052 | if (const FloatingLiteral *FL |
| 11053 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 11054 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 11055 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 11056 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 11057 | bool Ignored; |
| 11058 | // If the value does not fit in the destination type, the behavior is |
| 11059 | // undefined, so we are not required to treat it as a constant |
| 11060 | // expression. |
| 11061 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 11062 | llvm::APFloat::rmTowardZero, |
| 11063 | &Ignored) & APFloat::opInvalidOp) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11064 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 11065 | return NoDiag(); |
| 11066 | } |
| 11067 | } |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 11068 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 11069 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 11070 | case CK_AtomicToNonAtomic: |
| 11071 | case CK_NonAtomicToAtomic: |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 11072 | case CK_NoOp: |
| 11073 | case CK_IntegralToBoolean: |
| 11074 | case CK_IntegralCast: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11075 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 11076 | default: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11077 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 11078 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11079 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 11080 | case Expr::BinaryConditionalOperatorClass: { |
| 11081 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 11082 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11083 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 11084 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11085 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
| 11086 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
| 11087 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
Richard Smith | 74fc721 | 2012-12-28 12:53:55 +0000 | [diff] [blame] | 11088 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 11089 | return FalseResult; |
| 11090 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11091 | case Expr::ConditionalOperatorClass: { |
| 11092 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 11093 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 11094 | // then only the true side is actually considered in an integer constant |
| 11095 | // expression, and it is fully evaluated. This is an important GNU |
| 11096 | // extension. See GCC PR38377 for discussion. |
| 11097 | if (const CallExpr *CallCE |
| 11098 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 11099 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 11100 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11101 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11102 | if (CondResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11103 | return CondResult; |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 11104 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11105 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 11106 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 11107 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11108 | if (TrueResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11109 | return TrueResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11110 | if (FalseResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11111 | return FalseResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11112 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11113 | return CondResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11114 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11115 | return NoDiag(); |
| 11116 | // Rare case where the diagnostics depend on which side is evaluated |
| 11117 | // Note that if we get here, CondResult is 0, and at least one of |
| 11118 | // TrueResult and FalseResult is non-zero. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11119 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11120 | return FalseResult; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11121 | return TrueResult; |
| 11122 | } |
| 11123 | case Expr::CXXDefaultArgExprClass: |
| 11124 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 11125 | case Expr::CXXDefaultInitExprClass: |
| 11126 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11127 | case Expr::ChooseExprClass: { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 11128 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11129 | } |
| 11130 | } |
| 11131 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 11132 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11133 | } |
| 11134 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11135 | /// Evaluate an expression as a C++11 integral constant expression. |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11136 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11137 | const Expr *E, |
| 11138 | llvm::APSInt *Value, |
| 11139 | SourceLocation *Loc) { |
| 11140 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 11141 | if (Loc) *Loc = E->getExprLoc(); |
| 11142 | return false; |
| 11143 | } |
| 11144 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11145 | APValue Result; |
| 11146 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 11147 | return false; |
| 11148 | |
Richard Smith | 98710fc | 2014-11-13 23:03:19 +0000 | [diff] [blame] | 11149 | if (!Result.isInt()) { |
| 11150 | if (Loc) *Loc = E->getExprLoc(); |
| 11151 | return false; |
| 11152 | } |
| 11153 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11154 | if (Value) *Value = Result.getInt(); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 11155 | return true; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11156 | } |
| 11157 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11158 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
| 11159 | SourceLocation *Loc) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11160 | if (Ctx.getLangOpts().CPlusPlus11) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 11161 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr, Loc); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11162 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11163 | ICEDiag D = CheckICE(this, Ctx); |
| 11164 | if (D.Kind != IK_ICE) { |
| 11165 | if (Loc) *Loc = D.Loc; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11166 | return false; |
| 11167 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11168 | return true; |
| 11169 | } |
| 11170 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11171 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11172 | SourceLocation *Loc, bool isEvaluated) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 11173 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 11174 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 11175 | |
| 11176 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 11177 | return false; |
Richard Smith | 5c40f09 | 2015-12-04 03:00:44 +0000 | [diff] [blame] | 11178 | // The only possible side-effects here are due to UB discovered in the |
| 11179 | // evaluation (for instance, INT_MAX + 1). In such a case, we are still |
| 11180 | // required to treat the expression as an ICE, so we produce the folded |
| 11181 | // value. |
| 11182 | if (!EvaluateAsInt(Value, Ctx, SE_AllowSideEffects)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11183 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 11184 | return true; |
| 11185 | } |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11186 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11187 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 11188 | return CheckICE(this, Ctx).Kind == IK_ICE; |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 11189 | } |
| 11190 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 11191 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11192 | SourceLocation *Loc) const { |
| 11193 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 11194 | // issues. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11195 | assert(Ctx.getLangOpts().CPlusPlus); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11196 | |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 11197 | // Build evaluation settings. |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11198 | Expr::EvalStatus Status; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11199 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11200 | Status.Diag = &Diags; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 11201 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 11202 | |
| 11203 | APValue Scratch; |
| 11204 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 11205 | |
| 11206 | if (!Diags.empty()) { |
| 11207 | IsConstExpr = false; |
| 11208 | if (Loc) *Loc = Diags[0].first; |
| 11209 | } else if (!IsConstExpr) { |
| 11210 | // FIXME: This shouldn't happen. |
| 11211 | if (Loc) *Loc = getExprLoc(); |
| 11212 | } |
| 11213 | |
| 11214 | return IsConstExpr; |
| 11215 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11216 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11217 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
| 11218 | const FunctionDecl *Callee, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 11219 | ArrayRef<const Expr*> Args, |
| 11220 | const Expr *This) const { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11221 | Expr::EvalStatus Status; |
| 11222 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
| 11223 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 11224 | LValue ThisVal; |
| 11225 | const LValue *ThisPtr = nullptr; |
| 11226 | if (This) { |
| 11227 | #ifndef NDEBUG |
| 11228 | auto *MD = dyn_cast<CXXMethodDecl>(Callee); |
| 11229 | assert(MD && "Don't provide `this` for non-methods."); |
| 11230 | assert(!MD->isStatic() && "Don't provide `this` for static methods."); |
| 11231 | #endif |
| 11232 | if (EvaluateObjectArgument(Info, This, ThisVal)) |
| 11233 | ThisPtr = &ThisVal; |
| 11234 | if (Info.EvalStatus.HasSideEffects) |
| 11235 | return false; |
| 11236 | } |
| 11237 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11238 | ArgVector ArgValues(Args.size()); |
| 11239 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 11240 | I != E; ++I) { |
Nick Lewycky | f0202ca | 2014-12-16 06:12:01 +0000 | [diff] [blame] | 11241 | if ((*I)->isValueDependent() || |
| 11242 | !Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11243 | // If evaluation fails, throw away the argument entirely. |
| 11244 | ArgValues[I - Args.begin()] = APValue(); |
| 11245 | if (Info.EvalStatus.HasSideEffects) |
| 11246 | return false; |
| 11247 | } |
| 11248 | |
| 11249 | // Build fake call to Callee. |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 11250 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11251 | ArgValues.data()); |
| 11252 | return Evaluate(Value, Info, this) && !Info.EvalStatus.HasSideEffects; |
| 11253 | } |
| 11254 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11255 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 11256 | SmallVectorImpl< |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11257 | PartialDiagnosticAt> &Diags) { |
| 11258 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 11259 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 11260 | // ASTs which we build for dependent expressions. |
| 11261 | if (FD->isDependentContext()) |
| 11262 | return true; |
| 11263 | |
| 11264 | Expr::EvalStatus Status; |
| 11265 | Status.Diag = &Diags; |
| 11266 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 11267 | EvalInfo Info(FD->getASTContext(), Status, |
| 11268 | EvalInfo::EM_PotentialConstantExpression); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11269 | |
| 11270 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 11271 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11272 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 11273 | // Fabricate an arbitrary expression on the stack and pretend that it |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11274 | // is a temporary being used as the 'this' pointer. |
| 11275 | LValue This; |
| 11276 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 11277 | This.set({&VIE, Info.CurrentCall->Index}); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11278 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11279 | ArrayRef<const Expr*> Args; |
| 11280 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 11281 | APValue Scratch; |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 11282 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 11283 | // Evaluate the call as a constant initializer, to allow the construction |
| 11284 | // of objects of non-literal types. |
| 11285 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 11286 | HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch); |
| 11287 | } else { |
| 11288 | SourceLocation Loc = FD->getLocation(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 11289 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : nullptr, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 11290 | Args, FD->getBody(), Info, Scratch, nullptr); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 11291 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 11292 | |
| 11293 | return Diags.empty(); |
| 11294 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11295 | |
| 11296 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
| 11297 | const FunctionDecl *FD, |
| 11298 | SmallVectorImpl< |
| 11299 | PartialDiagnosticAt> &Diags) { |
| 11300 | Expr::EvalStatus Status; |
| 11301 | Status.Diag = &Diags; |
| 11302 | |
| 11303 | EvalInfo Info(FD->getASTContext(), Status, |
| 11304 | EvalInfo::EM_PotentialConstantExpressionUnevaluated); |
| 11305 | |
| 11306 | // Fabricate a call stack frame to give the arguments a plausible cover story. |
| 11307 | ArrayRef<const Expr*> Args; |
| 11308 | ArgVector ArgValues(0); |
| 11309 | bool Success = EvaluateArgs(Args, ArgValues, Info); |
| 11310 | (void)Success; |
| 11311 | assert(Success && |
| 11312 | "Failed to set up arguments for potential constant evaluation"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 11313 | CallStackFrame Frame(Info, SourceLocation(), FD, nullptr, ArgValues.data()); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 11314 | |
| 11315 | APValue ResultScratch; |
| 11316 | Evaluate(ResultScratch, Info, E); |
| 11317 | return Diags.empty(); |
| 11318 | } |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 11319 | |
| 11320 | bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
| 11321 | unsigned Type) const { |
| 11322 | if (!getType()->isPointerType()) |
| 11323 | return false; |
| 11324 | |
| 11325 | Expr::EvalStatus Status; |
| 11326 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 11327 | return tryEvaluateBuiltinObjectSize(this, Type, Info, Result); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 11328 | } |