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 | |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 689 | /// \brief Have we emitted a diagnostic explaining why we couldn't constant |
| 690 | /// fold (not just why it's not strictly a constant expression)? |
| 691 | bool HasFoldFailureDiagnostic; |
| 692 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 693 | /// \brief Whether or not we're currently speculatively evaluating. |
| 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, |
| 1723 | QualType Type, const LValue &LVal) { |
| 1724 | bool IsReferenceType = Type->isReferenceType(); |
| 1725 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1726 | APValue::LValueBase Base = LVal.getLValueBase(); |
| 1727 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
| 1728 | |
Richard Smith | 0dea49e | 2012-02-18 04:58:18 +0000 | [diff] [blame] | 1729 | // Check that the object is a global. Note that the fake 'this' object we |
| 1730 | // manufacture when checking potential constant expressions is conservatively |
| 1731 | // assumed to be global here. |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1732 | if (!IsGlobalLValue(Base)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1733 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1734 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1735 | Info.FFDiag(Loc, diag::note_constexpr_non_global, 1) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1736 | << IsReferenceType << !Designator.Entries.empty() |
| 1737 | << !!VD << VD; |
| 1738 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1739 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1740 | Info.FFDiag(Loc); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1741 | } |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1742 | // Don't allow references to temporaries to escape. |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1743 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 1744 | } |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 1745 | assert((Info.checkingPotentialConstantExpression() || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1746 | LVal.getLValueCallIndex() == 0) && |
| 1747 | "have call index for global lvalue"); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1748 | |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1749 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
| 1750 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1751 | // Check if this is a thread-local variable. |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1752 | if (Var->getTLSKind()) |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1753 | return false; |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1754 | |
Hans Wennborg | 82dd877 | 2014-06-25 22:19:48 +0000 | [diff] [blame] | 1755 | // A dllimport variable never acts like a constant. |
| 1756 | if (Var->hasAttr<DLLImportAttr>()) |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1757 | return false; |
| 1758 | } |
| 1759 | if (const auto *FD = dyn_cast<const FunctionDecl>(VD)) { |
| 1760 | // __declspec(dllimport) must be handled very carefully: |
| 1761 | // We must never initialize an expression with the thunk in C++. |
| 1762 | // Doing otherwise would allow the same id-expression to yield |
| 1763 | // different addresses for the same function in different translation |
| 1764 | // units. However, this means that we must dynamically initialize the |
| 1765 | // expression with the contents of the import address table at runtime. |
| 1766 | // |
| 1767 | // The C language has no notion of ODR; furthermore, it has no notion of |
| 1768 | // dynamic initialization. This means that we are permitted to |
| 1769 | // perform initialization with the address of the thunk. |
Hans Wennborg | 82dd877 | 2014-06-25 22:19:48 +0000 | [diff] [blame] | 1770 | if (Info.getLangOpts().CPlusPlus && FD->hasAttr<DLLImportAttr>()) |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 1771 | return false; |
Hans Wennborg | cb9ad99 | 2012-08-29 18:27:29 +0000 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1775 | // Allow address constant expressions to be past-the-end pointers. This is |
| 1776 | // an extension: the standard requires them to point to an object. |
| 1777 | if (!IsReferenceType) |
| 1778 | return true; |
| 1779 | |
| 1780 | // A reference constant expression must refer to an object. |
| 1781 | if (!Base) { |
| 1782 | // FIXME: diagnostic |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1783 | Info.CCEDiag(Loc); |
Richard Smith | 02ab9c2 | 2012-01-12 06:08:57 +0000 | [diff] [blame] | 1784 | return true; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1787 | // Does this refer one past the end of some object? |
Richard Smith | 33b44ab | 2014-07-23 23:50:25 +0000 | [diff] [blame] | 1788 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1789 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1790 | Info.FFDiag(Loc, diag::note_constexpr_past_end, 1) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1791 | << !Designator.Entries.empty() << !!VD << VD; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1792 | NoteLValueLocation(Info, Base); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1795 | return true; |
| 1796 | } |
| 1797 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 1798 | /// Member pointers are constant expressions unless they point to a |
| 1799 | /// non-virtual dllimport member function. |
| 1800 | static bool CheckMemberPointerConstantExpression(EvalInfo &Info, |
| 1801 | SourceLocation Loc, |
| 1802 | QualType Type, |
| 1803 | const APValue &Value) { |
| 1804 | const ValueDecl *Member = Value.getMemberPointerDecl(); |
| 1805 | const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Member); |
| 1806 | if (!FD) |
| 1807 | return true; |
| 1808 | return FD->isVirtual() || !FD->hasAttr<DLLImportAttr>(); |
| 1809 | } |
| 1810 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1811 | /// Check that this core constant expression is of literal type, and if not, |
| 1812 | /// produce an appropriate diagnostic. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1813 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1814 | const LValue *This = nullptr) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1815 | if (!E->isRValue() || E->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1816 | return true; |
| 1817 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1818 | // C++1y: A constant initializer for an object o [...] may also invoke |
| 1819 | // constexpr constructors for o and its subobjects even if those objects |
| 1820 | // are of non-literal class types. |
David L. Jones | f55ce36 | 2017-01-09 21:38:07 +0000 | [diff] [blame] | 1821 | // |
| 1822 | // C++11 missed this detail for aggregates, so classes like this: |
| 1823 | // struct foo_t { union { int i; volatile int j; } u; }; |
| 1824 | // are not (obviously) initializable like so: |
| 1825 | // __attribute__((__require_constant_initialization__)) |
| 1826 | // static const foo_t x = {{0}}; |
| 1827 | // because "i" is a subobject with non-literal initialization (due to the |
| 1828 | // volatile member of the union). See: |
| 1829 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 |
| 1830 | // Therefore, we use the C++1y behavior. |
| 1831 | if (This && Info.EvaluatingDecl == This->getLValueBase()) |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 1832 | return true; |
| 1833 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1834 | // Prvalue constant expressions must be of literal types. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1835 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1836 | Info.FFDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1837 | << E->getType(); |
| 1838 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1839 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 1840 | return false; |
| 1841 | } |
| 1842 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1843 | /// 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] | 1844 | /// constant expression. If not, report an appropriate diagnostic. Does not |
| 1845 | /// check that the expression is of literal type. |
| 1846 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
| 1847 | QualType Type, const APValue &Value) { |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1848 | if (Value.isUninit()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 1849 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 1850 | << true << Type; |
Richard Smith | 1a90f59 | 2013-06-18 17:51:51 +0000 | [diff] [blame] | 1851 | return false; |
| 1852 | } |
| 1853 | |
Richard Smith | 77be48a | 2014-07-31 06:31:19 +0000 | [diff] [blame] | 1854 | // We allow _Atomic(T) to be initialized from anything that T can be |
| 1855 | // initialized from. |
| 1856 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
| 1857 | Type = AT->getValueType(); |
| 1858 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1859 | // Core issue 1454: For a literal constant expression of array or class type, |
| 1860 | // each subobject of its value shall have been initialized by a constant |
| 1861 | // expression. |
| 1862 | if (Value.isArray()) { |
| 1863 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
| 1864 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
| 1865 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1866 | Value.getArrayInitializedElt(I))) |
| 1867 | return false; |
| 1868 | } |
| 1869 | if (!Value.hasArrayFiller()) |
| 1870 | return true; |
| 1871 | return CheckConstantExpression(Info, DiagLoc, EltTy, |
| 1872 | Value.getArrayFiller()); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 1873 | } |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1874 | if (Value.isUnion() && Value.getUnionField()) { |
| 1875 | return CheckConstantExpression(Info, DiagLoc, |
| 1876 | Value.getUnionField()->getType(), |
| 1877 | Value.getUnionValue()); |
| 1878 | } |
| 1879 | if (Value.isStruct()) { |
| 1880 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
| 1881 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1882 | unsigned BaseIndex = 0; |
| 1883 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
| 1884 | End = CD->bases_end(); I != End; ++I, ++BaseIndex) { |
| 1885 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1886 | Value.getStructBase(BaseIndex))) |
| 1887 | return false; |
| 1888 | } |
| 1889 | } |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1890 | for (const auto *I : RD->fields()) { |
Jordan Rose | d4503da | 2017-10-24 02:17:07 +0000 | [diff] [blame] | 1891 | if (I->isUnnamedBitfield()) |
| 1892 | continue; |
| 1893 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1894 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
| 1895 | Value.getStructField(I->getFieldIndex()))) |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1896 | return false; |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | if (Value.isLValue()) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1901 | LValue LVal; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1902 | LVal.setFrom(Info.Ctx, Value); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1903 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal); |
| 1904 | } |
| 1905 | |
Reid Kleckner | cd016d8 | 2017-07-07 22:04:29 +0000 | [diff] [blame] | 1906 | if (Value.isMemberPointer()) |
| 1907 | return CheckMemberPointerConstantExpression(Info, DiagLoc, Type, Value); |
| 1908 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 1909 | // Everything else is fine. |
| 1910 | return true; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 1913 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 1914 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | static bool IsLiteralLValue(const LValue &Value) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 1918 | if (Value.getLValueCallIndex()) |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 1919 | return false; |
| 1920 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
| 1921 | return E && !isa<MaterializeTemporaryExpr>(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1924 | static bool IsWeakLValue(const LValue &Value) { |
| 1925 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1926 | return Decl && Decl->isWeak(); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 1929 | static bool isZeroSized(const LValue &Value) { |
| 1930 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 1931 | if (Decl && isa<VarDecl>(Decl)) { |
| 1932 | QualType Ty = Decl->getType(); |
David Majnemer | 8c92b87 | 2014-12-14 08:40:47 +0000 | [diff] [blame] | 1933 | if (Ty->isArrayType()) |
| 1934 | return Ty->isIncompleteType() || |
| 1935 | Decl->getASTContext().getTypeSize(Ty) == 0; |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 1936 | } |
| 1937 | return false; |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1940 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1941 | // A null base expression indicates a null pointer. These are always |
| 1942 | // evaluatable, and they are false unless the offset is zero. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1943 | if (!Value.getLValueBase()) { |
| 1944 | Result = !Value.getLValueOffset().isZero(); |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1945 | return true; |
| 1946 | } |
Rafael Espindola | a1f9cc1 | 2010-05-07 15:18:43 +0000 | [diff] [blame] | 1947 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1948 | // We have a non-null base. These are generally known to be true, but if it's |
| 1949 | // a weak declaration it can be null at runtime. |
John McCall | eb3e4f3 | 2010-05-07 21:34:32 +0000 | [diff] [blame] | 1950 | Result = true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1951 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
Lang Hames | d42bb47 | 2011-12-05 20:16:26 +0000 | [diff] [blame] | 1952 | return !Decl || !Decl->isWeak(); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1955 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1956 | switch (Val.getKind()) { |
| 1957 | case APValue::Uninitialized: |
| 1958 | return false; |
| 1959 | case APValue::Int: |
| 1960 | Result = Val.getInt().getBoolValue(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1961 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1962 | case APValue::Float: |
| 1963 | Result = !Val.getFloat().isZero(); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1964 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1965 | case APValue::ComplexInt: |
| 1966 | Result = Val.getComplexIntReal().getBoolValue() || |
| 1967 | Val.getComplexIntImag().getBoolValue(); |
| 1968 | return true; |
| 1969 | case APValue::ComplexFloat: |
| 1970 | Result = !Val.getComplexFloatReal().isZero() || |
| 1971 | !Val.getComplexFloatImag().isZero(); |
| 1972 | return true; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 1973 | case APValue::LValue: |
| 1974 | return EvalPointerValueAsBool(Val, Result); |
| 1975 | case APValue::MemberPointer: |
| 1976 | Result = Val.getMemberPointerDecl(); |
| 1977 | return true; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1978 | case APValue::Vector: |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 1979 | case APValue::Array: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 1980 | case APValue::Struct: |
| 1981 | case APValue::Union: |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 1982 | case APValue::AddrLabelDiff: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1983 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1986 | llvm_unreachable("unknown APValue kind"); |
| 1987 | } |
| 1988 | |
| 1989 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
| 1990 | EvalInfo &Info) { |
| 1991 | assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 1992 | APValue Val; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1993 | if (!Evaluate(Val, Info, E)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 1994 | return false; |
Argyrios Kyrtzidis | 91d0098 | 2012-02-27 20:21:34 +0000 | [diff] [blame] | 1995 | return HandleConversionToBool(Val, Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 1998 | template<typename T> |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 1999 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2000 | const T &SrcValue, QualType DestType) { |
Eli Friedman | 4eafb6b | 2012-07-17 21:03:05 +0000 | [diff] [blame] | 2001 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 2002 | << SrcValue << DestType; |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 2003 | return Info.noteUndefinedBehavior(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
| 2006 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
| 2007 | QualType SrcType, const APFloat &Value, |
| 2008 | QualType DestType, APSInt &Result) { |
| 2009 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2010 | // Determine whether we are converting to unsigned or signed. |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2011 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2013 | Result = APSInt(DestWidth, !DestSigned); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2014 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2015 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
| 2016 | & APFloat::opInvalidOp) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2017 | return HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2018 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2021 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
| 2022 | QualType SrcType, QualType DestType, |
| 2023 | APFloat &Result) { |
| 2024 | APFloat Value = Result; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2025 | bool ignored; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2026 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
| 2027 | APFloat::rmNearestTiesToEven, &ignored) |
| 2028 | & APFloat::opOverflow) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2029 | return HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2030 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 2033 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
| 2034 | QualType DestType, QualType SrcType, |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 2035 | const APSInt &Value) { |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 2036 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2037 | APSInt Result = Value; |
| 2038 | // Figure out if this is a truncate, extend or noop cast. |
| 2039 | // If the input is signed, do a sign extend, noop, or truncate. |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2040 | Result = Result.extOrTrunc(DestWidth); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 2041 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2042 | return Result; |
| 2043 | } |
| 2044 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2045 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
| 2046 | QualType SrcType, const APSInt &Value, |
| 2047 | QualType DestType, APFloat &Result) { |
| 2048 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
| 2049 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
| 2050 | APFloat::rmNearestTiesToEven) |
| 2051 | & APFloat::opOverflow) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2052 | return HandleOverflow(Info, E, Value, DestType); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 2053 | return true; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2056 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
| 2057 | APValue &Value, const FieldDecl *FD) { |
| 2058 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
| 2059 | |
| 2060 | if (!Value.isInt()) { |
| 2061 | // Trying to store a pointer-cast-to-integer into a bitfield. |
| 2062 | // FIXME: In this case, we should provide the diagnostic for casting |
| 2063 | // a pointer to an integer. |
| 2064 | assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2065 | Info.FFDiag(E); |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2066 | return false; |
| 2067 | } |
| 2068 | |
| 2069 | APSInt &Int = Value.getInt(); |
| 2070 | unsigned OldBitWidth = Int.getBitWidth(); |
| 2071 | unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx); |
| 2072 | if (NewBitWidth < OldBitWidth) |
| 2073 | Int = Int.trunc(NewBitWidth).extend(OldBitWidth); |
| 2074 | return true; |
| 2075 | } |
| 2076 | |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2077 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
| 2078 | llvm::APInt &Res) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 2079 | APValue SVal; |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2080 | if (!Evaluate(SVal, Info, E)) |
| 2081 | return false; |
| 2082 | if (SVal.isInt()) { |
| 2083 | Res = SVal.getInt(); |
| 2084 | return true; |
| 2085 | } |
| 2086 | if (SVal.isFloat()) { |
| 2087 | Res = SVal.getFloat().bitcastToAPInt(); |
| 2088 | return true; |
| 2089 | } |
| 2090 | if (SVal.isVector()) { |
| 2091 | QualType VecTy = E->getType(); |
| 2092 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
| 2093 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
| 2094 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 2095 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 2096 | Res = llvm::APInt::getNullValue(VecSize); |
| 2097 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
| 2098 | APValue &Elt = SVal.getVectorElt(i); |
| 2099 | llvm::APInt EltAsInt; |
| 2100 | if (Elt.isInt()) { |
| 2101 | EltAsInt = Elt.getInt(); |
| 2102 | } else if (Elt.isFloat()) { |
| 2103 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
| 2104 | } else { |
| 2105 | // Don't try to handle vectors of anything other than int or float |
| 2106 | // (not sure if it's possible to hit this case). |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2107 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2108 | return false; |
| 2109 | } |
| 2110 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
| 2111 | if (BigEndian) |
| 2112 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
| 2113 | else |
| 2114 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
| 2115 | } |
| 2116 | return true; |
| 2117 | } |
| 2118 | // Give up if the input isn't an int, float, or vector. For example, we |
| 2119 | // reject "(v4i16)(intptr_t)&a". |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2120 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 2121 | return false; |
| 2122 | } |
| 2123 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2124 | /// Perform the given integer operation, which is known to need at most BitWidth |
| 2125 | /// bits, and check for overflow in the original type (if that type was not an |
| 2126 | /// unsigned type). |
| 2127 | template<typename Operation> |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2128 | static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
| 2129 | const APSInt &LHS, const APSInt &RHS, |
| 2130 | unsigned BitWidth, Operation Op, |
| 2131 | APSInt &Result) { |
| 2132 | if (LHS.isUnsigned()) { |
| 2133 | Result = Op(LHS, RHS); |
| 2134 | return true; |
| 2135 | } |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2136 | |
| 2137 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2138 | Result = Value.trunc(LHS.getBitWidth()); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2139 | if (Result.extend(BitWidth) != Value) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2140 | if (Info.checkingForOverflow()) |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2141 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2142 | diag::warn_integer_constant_overflow) |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2143 | << Result.toString(10) << E->getType(); |
| 2144 | else |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2145 | return HandleOverflow(Info, E, Value, E->getType()); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2146 | } |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2147 | return true; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /// Perform the given binary integer operation. |
| 2151 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
| 2152 | BinaryOperatorKind Opcode, APSInt RHS, |
| 2153 | APSInt &Result) { |
| 2154 | switch (Opcode) { |
| 2155 | default: |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2156 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2157 | return false; |
| 2158 | case BO_Mul: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2159 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
| 2160 | std::multiplies<APSInt>(), Result); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2161 | case BO_Add: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2162 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 2163 | std::plus<APSInt>(), Result); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2164 | case BO_Sub: |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2165 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
| 2166 | std::minus<APSInt>(), Result); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2167 | case BO_And: Result = LHS & RHS; return true; |
| 2168 | case BO_Xor: Result = LHS ^ RHS; return true; |
| 2169 | case BO_Or: Result = LHS | RHS; return true; |
| 2170 | case BO_Div: |
| 2171 | case BO_Rem: |
| 2172 | if (RHS == 0) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2173 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2174 | return false; |
| 2175 | } |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2176 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
| 2177 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports |
| 2178 | // this operation and gives the two's complement result. |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2179 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
| 2180 | LHS.isSigned() && LHS.isMinSignedValue()) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2181 | return HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), |
| 2182 | E->getType()); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 2183 | return true; |
| 2184 | case BO_Shl: { |
| 2185 | if (Info.getLangOpts().OpenCL) |
| 2186 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 2187 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 2188 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 2189 | RHS.isUnsigned()); |
| 2190 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 2191 | // During constant-folding, a negative shift is an opposite shift. Such |
| 2192 | // a shift is not a constant expression. |
| 2193 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 2194 | RHS = -RHS; |
| 2195 | goto shift_right; |
| 2196 | } |
| 2197 | shift_left: |
| 2198 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
| 2199 | // the shifted type. |
| 2200 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2201 | if (SA != RHS) { |
| 2202 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 2203 | << RHS << E->getType() << LHS.getBitWidth(); |
| 2204 | } else if (LHS.isSigned()) { |
| 2205 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
| 2206 | // operand, and must not overflow the corresponding unsigned type. |
| 2207 | if (LHS.isNegative()) |
| 2208 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
| 2209 | else if (LHS.countLeadingZeros() < SA) |
| 2210 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
| 2211 | } |
| 2212 | Result = LHS << SA; |
| 2213 | return true; |
| 2214 | } |
| 2215 | case BO_Shr: { |
| 2216 | if (Info.getLangOpts().OpenCL) |
| 2217 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
| 2218 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
| 2219 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
| 2220 | RHS.isUnsigned()); |
| 2221 | else if (RHS.isSigned() && RHS.isNegative()) { |
| 2222 | // During constant-folding, a negative shift is an opposite shift. Such a |
| 2223 | // shift is not a constant expression. |
| 2224 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
| 2225 | RHS = -RHS; |
| 2226 | goto shift_left; |
| 2227 | } |
| 2228 | shift_right: |
| 2229 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
| 2230 | // shifted type. |
| 2231 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
| 2232 | if (SA != RHS) |
| 2233 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
| 2234 | << RHS << E->getType() << LHS.getBitWidth(); |
| 2235 | Result = LHS >> SA; |
| 2236 | return true; |
| 2237 | } |
| 2238 | |
| 2239 | case BO_LT: Result = LHS < RHS; return true; |
| 2240 | case BO_GT: Result = LHS > RHS; return true; |
| 2241 | case BO_LE: Result = LHS <= RHS; return true; |
| 2242 | case BO_GE: Result = LHS >= RHS; return true; |
| 2243 | case BO_EQ: Result = LHS == RHS; return true; |
| 2244 | case BO_NE: Result = LHS != RHS; return true; |
| 2245 | } |
| 2246 | } |
| 2247 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2248 | /// Perform the given binary floating-point operation, in-place, on LHS. |
| 2249 | static bool handleFloatFloatBinOp(EvalInfo &Info, const Expr *E, |
| 2250 | APFloat &LHS, BinaryOperatorKind Opcode, |
| 2251 | const APFloat &RHS) { |
| 2252 | switch (Opcode) { |
| 2253 | default: |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2254 | Info.FFDiag(E); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2255 | return false; |
| 2256 | case BO_Mul: |
| 2257 | LHS.multiply(RHS, APFloat::rmNearestTiesToEven); |
| 2258 | break; |
| 2259 | case BO_Add: |
| 2260 | LHS.add(RHS, APFloat::rmNearestTiesToEven); |
| 2261 | break; |
| 2262 | case BO_Sub: |
| 2263 | LHS.subtract(RHS, APFloat::rmNearestTiesToEven); |
| 2264 | break; |
| 2265 | case BO_Div: |
| 2266 | LHS.divide(RHS, APFloat::rmNearestTiesToEven); |
| 2267 | break; |
| 2268 | } |
| 2269 | |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2270 | if (LHS.isInfinity() || LHS.isNaN()) { |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2271 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 2272 | return Info.noteUndefinedBehavior(); |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 2273 | } |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2274 | return true; |
| 2275 | } |
| 2276 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2277 | /// Cast an lvalue referring to a base subobject to a derived class, by |
| 2278 | /// truncating the lvalue's path to the given length. |
| 2279 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
| 2280 | const RecordDecl *TruncatedType, |
| 2281 | unsigned TruncatedElements) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2282 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2283 | |
| 2284 | // Check we actually point to a derived class object. |
| 2285 | if (TruncatedElements == D.Entries.size()) |
| 2286 | return true; |
| 2287 | assert(TruncatedElements >= D.MostDerivedPathLength && |
| 2288 | "not casting to a derived class"); |
| 2289 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
| 2290 | return false; |
| 2291 | |
| 2292 | // 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] | 2293 | const RecordDecl *RD = TruncatedType; |
| 2294 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2295 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2296 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 2297 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2298 | if (isVirtualBaseClass(D.Entries[I])) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2299 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2300 | else |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2301 | Result.Offset -= Layout.getBaseClassOffset(Base); |
| 2302 | RD = Base; |
| 2303 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 2304 | D.Entries.resize(TruncatedElements); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2305 | return true; |
| 2306 | } |
| 2307 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2308 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2309 | const CXXRecordDecl *Derived, |
| 2310 | const CXXRecordDecl *Base, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2311 | const ASTRecordLayout *RL = nullptr) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2312 | if (!RL) { |
| 2313 | if (Derived->isInvalidDecl()) return false; |
| 2314 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
| 2315 | } |
| 2316 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2317 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2318 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2319 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2322 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2323 | const CXXRecordDecl *DerivedDecl, |
| 2324 | const CXXBaseSpecifier *Base) { |
| 2325 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
| 2326 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2327 | if (!Base->isVirtual()) |
| 2328 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2329 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2330 | SubobjectDesignator &D = Obj.Designator; |
| 2331 | if (D.Invalid) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2332 | return false; |
| 2333 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2334 | // Extract most-derived object and corresponding type. |
| 2335 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
| 2336 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
| 2337 | return false; |
| 2338 | |
| 2339 | // Find the virtual base class. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2340 | if (DerivedDecl->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2341 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
| 2342 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2343 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2344 | return true; |
| 2345 | } |
| 2346 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 2347 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
| 2348 | QualType Type, LValue &Result) { |
| 2349 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 2350 | PathE = E->path_end(); |
| 2351 | PathI != PathE; ++PathI) { |
| 2352 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
| 2353 | *PathI)) |
| 2354 | return false; |
| 2355 | Type = (*PathI)->getType(); |
| 2356 | } |
| 2357 | return true; |
| 2358 | } |
| 2359 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2360 | /// Update LVal to refer to the given field, which must be a member of the type |
| 2361 | /// currently described by LVal. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2362 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2363 | const FieldDecl *FD, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2364 | const ASTRecordLayout *RL = nullptr) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2365 | if (!RL) { |
| 2366 | if (FD->getParent()->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2367 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2368 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2369 | |
| 2370 | unsigned I = FD->getFieldIndex(); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 2371 | LVal.adjustOffset(Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I))); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2372 | LVal.addDecl(Info, E, FD); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2373 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2376 | /// Update LVal to refer to the given indirect field. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2377 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2378 | LValue &LVal, |
| 2379 | const IndirectFieldDecl *IFD) { |
Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 2380 | for (const auto *C : IFD->chain()) |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 2381 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(C))) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 2382 | return false; |
| 2383 | return true; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2386 | /// Get the size of the given type in char units. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2387 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
| 2388 | QualType Type, CharUnits &Size) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2389 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
| 2390 | // extension. |
| 2391 | if (Type->isVoidType() || Type->isFunctionType()) { |
| 2392 | Size = CharUnits::One(); |
| 2393 | return true; |
| 2394 | } |
| 2395 | |
Saleem Abdulrasool | ada78fe | 2016-06-04 03:16:21 +0000 | [diff] [blame] | 2396 | if (Type->isDependentType()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2397 | Info.FFDiag(Loc); |
Saleem Abdulrasool | ada78fe | 2016-06-04 03:16:21 +0000 | [diff] [blame] | 2398 | return false; |
| 2399 | } |
| 2400 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2401 | if (!Type->isConstantSizeType()) { |
| 2402 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2403 | // FIXME: Better diagnostic. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2404 | Info.FFDiag(Loc); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2405 | return false; |
| 2406 | } |
| 2407 | |
| 2408 | Size = Info.Ctx.getTypeSizeInChars(Type); |
| 2409 | return true; |
| 2410 | } |
| 2411 | |
| 2412 | /// Update a pointer value to model pointer arithmetic. |
| 2413 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2414 | /// \param E - The expression being evaluated, for diagnostic purposes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2415 | /// \param LVal - The pointer value to be updated. |
| 2416 | /// \param EltTy - The pointee type represented by LVal. |
| 2417 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2418 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 2419 | LValue &LVal, QualType EltTy, |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 2420 | APSInt Adjustment) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2421 | CharUnits SizeOfPointee; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 2422 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2423 | return false; |
| 2424 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 2425 | LVal.adjustOffsetAndIndex(Info, E, Adjustment, SizeOfPointee); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2426 | return true; |
| 2427 | } |
| 2428 | |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 2429 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
| 2430 | LValue &LVal, QualType EltTy, |
| 2431 | int64_t Adjustment) { |
| 2432 | return HandleLValueArrayAdjustment(Info, E, LVal, EltTy, |
| 2433 | APSInt::get(Adjustment)); |
| 2434 | } |
| 2435 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2436 | /// Update an lvalue to refer to a component of a complex number. |
| 2437 | /// \param Info - Information about the ongoing evaluation. |
| 2438 | /// \param LVal - The lvalue to be updated. |
| 2439 | /// \param EltTy - The complex number's component type. |
| 2440 | /// \param Imag - False for the real component, true for the imaginary. |
| 2441 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
| 2442 | LValue &LVal, QualType EltTy, |
| 2443 | bool Imag) { |
| 2444 | if (Imag) { |
| 2445 | CharUnits SizeOfComponent; |
| 2446 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
| 2447 | return false; |
| 2448 | LVal.Offset += SizeOfComponent; |
| 2449 | } |
| 2450 | LVal.addComplex(Info, E, EltTy, Imag); |
| 2451 | return true; |
| 2452 | } |
| 2453 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 2454 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
| 2455 | QualType Type, const LValue &LVal, |
| 2456 | APValue &RVal); |
| 2457 | |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2458 | /// Try to evaluate the initializer for a variable declaration. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2459 | /// |
| 2460 | /// \param Info Information about the ongoing evaluation. |
| 2461 | /// \param E An expression to be used when printing diagnostics. |
| 2462 | /// \param VD The variable whose initializer should be obtained. |
| 2463 | /// \param Frame The frame in which the variable was created. Must be null |
| 2464 | /// if this variable is not local to the evaluation. |
| 2465 | /// \param Result Filled in with a pointer to the value of the variable. |
| 2466 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
| 2467 | const VarDecl *VD, CallStackFrame *Frame, |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 2468 | APValue *&Result, const LValue *LVal) { |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 2469 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2470 | // If this is a parameter to an active constexpr function call, perform |
| 2471 | // argument substitution. |
| 2472 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2473 | // Assume arguments of a potential constant expression are unknown |
| 2474 | // constant expressions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2475 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2476 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2477 | if (!Frame || !Frame->Arguments) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2478 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2479 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2480 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2481 | Result = &Frame->Arguments[PVD->getFunctionScopeIndex()]; |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 2482 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 2483 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2484 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2485 | // If this is a local variable, dig out its value. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2486 | if (Frame) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 2487 | Result = LVal ? Frame->getTemporary(VD, LVal->getLValueVersion()) |
| 2488 | : Frame->getCurrentTemporary(VD); |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 2489 | if (!Result) { |
| 2490 | // Assume variables referenced within a lambda's call operator that were |
| 2491 | // not declared within the call operator are captures and during checking |
| 2492 | // of a potential constant expression, assume they are unknown constant |
| 2493 | // expressions. |
| 2494 | assert(isLambdaCallOperator(Frame->Callee) && |
| 2495 | (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && |
| 2496 | "missing value for local variable"); |
| 2497 | if (Info.checkingPotentialConstantExpression()) |
| 2498 | return false; |
| 2499 | // FIXME: implement capture evaluation during constant expr evaluation. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2500 | Info.FFDiag(E->getLocStart(), |
Faisal Vali | a734ab9 | 2016-03-26 16:11:37 +0000 | [diff] [blame] | 2501 | diag::note_unimplemented_constexpr_lambda_feature_ast) |
| 2502 | << "captures not currently allowed"; |
| 2503 | return false; |
| 2504 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2505 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2508 | // Dig out the initializer, and use the declaration which it's attached to. |
| 2509 | const Expr *Init = VD->getAnyInitializer(VD); |
| 2510 | if (!Init || Init->isValueDependent()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 2511 | // If we're checking a potential constant expression, the variable could be |
| 2512 | // initialized later. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2513 | if (!Info.checkingPotentialConstantExpression()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2514 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2515 | return false; |
| 2516 | } |
| 2517 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2518 | // If we're currently evaluating the initializer of this declaration, use that |
| 2519 | // in-flight value. |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 2520 | if (Info.EvaluatingDecl.dyn_cast<const ValueDecl*>() == VD) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2521 | Result = Info.EvaluatingDeclValue; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2522 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2525 | // Never evaluate the initializer of a weak variable. We can't be sure that |
| 2526 | // this is the definition which will be used. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2527 | if (VD->isWeak()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2528 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2529 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2530 | } |
Richard Smith | cecf184 | 2011-11-01 21:06:14 +0000 | [diff] [blame] | 2531 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2532 | // Check that we can fold the initializer. In C++, we will have already done |
| 2533 | // this in the cases where it matters for conformance. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2534 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2535 | if (!VD->evaluateValue(Notes)) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2536 | Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2537 | Notes.size() + 1) << VD; |
| 2538 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2539 | Info.addNotes(Notes); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2540 | return false; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2541 | } else if (!VD->checkInitIsICE()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 2542 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2543 | Notes.size() + 1) << VD; |
| 2544 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 2545 | Info.addNotes(Notes); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2546 | } |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2547 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2548 | Result = VD->getEvaluatedValue(); |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 2549 | return true; |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 2552 | static bool IsConstNonVolatile(QualType T) { |
Richard Smith | 2790870 | 2011-10-24 17:54:18 +0000 | [diff] [blame] | 2553 | Qualifiers Quals = T.getQualifiers(); |
| 2554 | return Quals.hasConst() && !Quals.hasVolatile(); |
| 2555 | } |
| 2556 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2557 | /// Get the base index of the given base class within an APValue representing |
| 2558 | /// the given derived class. |
| 2559 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
| 2560 | const CXXRecordDecl *Base) { |
| 2561 | Base = Base->getCanonicalDecl(); |
| 2562 | unsigned Index = 0; |
| 2563 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
| 2564 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
| 2565 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
| 2566 | return Index; |
| 2567 | } |
| 2568 | |
| 2569 | llvm_unreachable("base class missing from derived class's bases list"); |
| 2570 | } |
| 2571 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2572 | /// Extract the value of a character from a string literal. |
| 2573 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
| 2574 | uint64_t Index) { |
Akira Hatanaka | bc33264 | 2017-01-31 02:31:39 +0000 | [diff] [blame] | 2575 | // FIXME: Support MakeStringConstant |
| 2576 | if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Lit)) { |
| 2577 | std::string Str; |
| 2578 | Info.Ctx.getObjCEncodingForType(ObjCEnc->getEncodedType(), Str); |
| 2579 | assert(Index <= Str.size() && "Index too large"); |
| 2580 | return APSInt::getUnsigned(Str.c_str()[Index]); |
| 2581 | } |
| 2582 | |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 2583 | if (auto PE = dyn_cast<PredefinedExpr>(Lit)) |
| 2584 | Lit = PE->getFunctionName(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2585 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 2586 | const ConstantArrayType *CAT = |
| 2587 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2588 | assert(CAT && "string literal isn't an array"); |
| 2589 | QualType CharType = CAT->getElementType(); |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2590 | assert(CharType->isIntegerType() && "unexpected character type"); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2591 | |
| 2592 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 2593 | CharType->isUnsignedIntegerType()); |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2594 | if (Index < S->getLength()) |
| 2595 | Value = S->getCodeUnit(Index); |
| 2596 | return Value; |
| 2597 | } |
| 2598 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2599 | // Expand a string literal into an array of characters. |
| 2600 | static void expandStringLiteral(EvalInfo &Info, const Expr *Lit, |
| 2601 | APValue &Result) { |
| 2602 | const StringLiteral *S = cast<StringLiteral>(Lit); |
| 2603 | const ConstantArrayType *CAT = |
| 2604 | Info.Ctx.getAsConstantArrayType(S->getType()); |
| 2605 | assert(CAT && "string literal isn't an array"); |
| 2606 | QualType CharType = CAT->getElementType(); |
| 2607 | assert(CharType->isIntegerType() && "unexpected character type"); |
| 2608 | |
| 2609 | unsigned Elts = CAT->getSize().getZExtValue(); |
| 2610 | Result = APValue(APValue::UninitArray(), |
| 2611 | std::min(S->getLength(), Elts), Elts); |
| 2612 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
| 2613 | CharType->isUnsignedIntegerType()); |
| 2614 | if (Result.hasArrayFiller()) |
| 2615 | Result.getArrayFiller() = APValue(Value); |
| 2616 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
| 2617 | Value = S->getCodeUnit(I); |
| 2618 | Result.getArrayInitializedElt(I) = APValue(Value); |
| 2619 | } |
| 2620 | } |
| 2621 | |
| 2622 | // Expand an array so that it has more than Index filled elements. |
| 2623 | static void expandArray(APValue &Array, unsigned Index) { |
| 2624 | unsigned Size = Array.getArraySize(); |
| 2625 | assert(Index < Size); |
| 2626 | |
| 2627 | // Always at least double the number of elements for which we store a value. |
| 2628 | unsigned OldElts = Array.getArrayInitializedElts(); |
| 2629 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
| 2630 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
| 2631 | |
| 2632 | // Copy the data across. |
| 2633 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
| 2634 | for (unsigned I = 0; I != OldElts; ++I) |
| 2635 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
| 2636 | for (unsigned I = OldElts; I != NewElts; ++I) |
| 2637 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
| 2638 | if (NewValue.hasArrayFiller()) |
| 2639 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
| 2640 | Array.swap(NewValue); |
| 2641 | } |
| 2642 | |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2643 | /// Determine whether a type would actually be read by an lvalue-to-rvalue |
| 2644 | /// conversion. If it's of class type, we may assume that the copy operation |
| 2645 | /// is trivial. Note that this is never true for a union type with fields |
| 2646 | /// (because the copy always "reads" the active member) and always true for |
| 2647 | /// a non-class type. |
| 2648 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
| 2649 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2650 | if (!RD || (RD->isUnion() && !RD->field_empty())) |
| 2651 | return true; |
| 2652 | if (RD->isEmpty()) |
| 2653 | return false; |
| 2654 | |
| 2655 | for (auto *Field : RD->fields()) |
| 2656 | if (isReadByLvalueToRvalueConversion(Field->getType())) |
| 2657 | return true; |
| 2658 | |
| 2659 | for (auto &BaseSpec : RD->bases()) |
| 2660 | if (isReadByLvalueToRvalueConversion(BaseSpec.getType())) |
| 2661 | return true; |
| 2662 | |
| 2663 | return false; |
| 2664 | } |
| 2665 | |
| 2666 | /// Diagnose an attempt to read from any unreadable field within the specified |
| 2667 | /// type, which might be a class type. |
| 2668 | static bool diagnoseUnreadableFields(EvalInfo &Info, const Expr *E, |
| 2669 | QualType T) { |
| 2670 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 2671 | if (!RD) |
| 2672 | return false; |
| 2673 | |
| 2674 | if (!RD->hasMutableFields()) |
| 2675 | return false; |
| 2676 | |
| 2677 | for (auto *Field : RD->fields()) { |
| 2678 | // If we're actually going to read this field in some way, then it can't |
| 2679 | // be mutable. If we're in a union, then assigning to a mutable field |
| 2680 | // (even an empty one) can change the active member, so that's not OK. |
| 2681 | // FIXME: Add core issue number for the union case. |
| 2682 | if (Field->isMutable() && |
| 2683 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2684 | Info.FFDiag(E, diag::note_constexpr_ltor_mutable, 1) << Field; |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2685 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2686 | return true; |
| 2687 | } |
| 2688 | |
| 2689 | if (diagnoseUnreadableFields(Info, E, Field->getType())) |
| 2690 | return true; |
| 2691 | } |
| 2692 | |
| 2693 | for (auto &BaseSpec : RD->bases()) |
| 2694 | if (diagnoseUnreadableFields(Info, E, BaseSpec.getType())) |
| 2695 | return true; |
| 2696 | |
| 2697 | // All mutable fields were empty, and thus not actually read. |
| 2698 | return false; |
| 2699 | } |
| 2700 | |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 2701 | /// Kinds of access we can perform on an object, for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2702 | enum AccessKinds { |
| 2703 | AK_Read, |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 2704 | AK_Assign, |
| 2705 | AK_Increment, |
| 2706 | AK_Decrement |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2707 | }; |
| 2708 | |
Benjamin Kramer | 5b4296a | 2015-10-28 17:16:26 +0000 | [diff] [blame] | 2709 | namespace { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2710 | /// A handle to a complete object (an object that is not a subobject of |
| 2711 | /// another object). |
| 2712 | struct CompleteObject { |
| 2713 | /// The value of the complete object. |
| 2714 | APValue *Value; |
| 2715 | /// The type of the complete object. |
| 2716 | QualType Type; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2717 | bool LifetimeStartedInEvaluation; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2718 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2719 | CompleteObject() : Value(nullptr) {} |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2720 | CompleteObject(APValue *Value, QualType Type, |
| 2721 | bool LifetimeStartedInEvaluation) |
| 2722 | : Value(Value), Type(Type), |
| 2723 | LifetimeStartedInEvaluation(LifetimeStartedInEvaluation) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2724 | assert(Value && "missing value for complete object"); |
| 2725 | } |
| 2726 | |
Aaron Ballman | 6734766 | 2015-02-15 22:00:28 +0000 | [diff] [blame] | 2727 | explicit operator bool() const { return Value; } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2728 | }; |
Benjamin Kramer | 5b4296a | 2015-10-28 17:16:26 +0000 | [diff] [blame] | 2729 | } // end anonymous namespace |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2730 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2731 | /// Find the designated sub-object of an rvalue. |
| 2732 | template<typename SubobjectHandler> |
| 2733 | typename SubobjectHandler::result_type |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2734 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2735 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 2736 | if (Sub.Invalid) |
| 2737 | // A diagnostic will have already been produced. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2738 | return handler.failed(); |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 2739 | if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2740 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 2741 | Info.FFDiag(E, Sub.isOnePastTheEnd() |
| 2742 | ? diag::note_constexpr_access_past_end |
| 2743 | : diag::note_constexpr_access_unsized_array) |
| 2744 | << handler.AccessKind; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2745 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2746 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2747 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2748 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2749 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2750 | APValue *O = Obj.Value; |
| 2751 | QualType ObjType = Obj.Type; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2752 | const FieldDecl *LastField = nullptr; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2753 | const bool MayReadMutableMembers = |
| 2754 | Obj.LifetimeStartedInEvaluation && Info.getLangOpts().CPlusPlus14; |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2755 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2756 | // Walk the designator's path to find the subobject. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2757 | for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) { |
| 2758 | if (O->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 2759 | if (!Info.checkingPotentialConstantExpression()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2760 | Info.FFDiag(E, diag::note_constexpr_access_uninit) << handler.AccessKind; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2761 | return handler.failed(); |
| 2762 | } |
| 2763 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2764 | if (I == N) { |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2765 | // If we are reading an object of class type, there may still be more |
| 2766 | // things we need to check: if there are any mutable subobjects, we |
| 2767 | // cannot perform this read. (This only happens when performing a trivial |
| 2768 | // copy or assignment.) |
| 2769 | if (ObjType->isRecordType() && handler.AccessKind == AK_Read && |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2770 | !MayReadMutableMembers && diagnoseUnreadableFields(Info, E, ObjType)) |
Richard Smith | b01fe40 | 2014-09-16 01:24:02 +0000 | [diff] [blame] | 2771 | return handler.failed(); |
| 2772 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2773 | if (!handler.found(*O, ObjType)) |
| 2774 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 2775 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2776 | // If we modified a bit-field, truncate it to the right width. |
| 2777 | if (handler.AccessKind != AK_Read && |
| 2778 | LastField && LastField->isBitField() && |
| 2779 | !truncateBitfieldValue(Info, E, *O, LastField)) |
| 2780 | return false; |
| 2781 | |
| 2782 | return true; |
| 2783 | } |
| 2784 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2785 | LastField = nullptr; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2786 | if (ObjType->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2787 | // Next subobject is an array element. |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2788 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2789 | assert(CAT && "vla in literal type?"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2790 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2791 | if (CAT->getSize().ule(Index)) { |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2792 | // Note, it should not be possible to form a pointer with a valid |
| 2793 | // designator which points more than one past the end of the array. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2794 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2795 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2796 | << handler.AccessKind; |
| 2797 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2798 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2799 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2800 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2801 | |
| 2802 | ObjType = CAT->getElementType(); |
| 2803 | |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 2804 | // An array object is represented as either an Array APValue or as an |
| 2805 | // LValue which refers to a string literal. |
| 2806 | if (O->isLValue()) { |
| 2807 | assert(I == N - 1 && "extracting subobject of character?"); |
| 2808 | assert(!O->hasLValuePath() || O->getLValuePath().empty()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2809 | if (handler.AccessKind != AK_Read) |
| 2810 | expandStringLiteral(Info, O->getLValueBase().get<const Expr *>(), |
| 2811 | *O); |
| 2812 | else |
| 2813 | return handler.foundString(*O, ObjType, Index); |
| 2814 | } |
| 2815 | |
| 2816 | if (O->getArrayInitializedElts() > Index) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2817 | O = &O->getArrayInitializedElt(Index); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2818 | else if (handler.AccessKind != AK_Read) { |
| 2819 | expandArray(*O, Index); |
| 2820 | O = &O->getArrayInitializedElt(Index); |
| 2821 | } else |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2822 | O = &O->getArrayFiller(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2823 | } else if (ObjType->isAnyComplexType()) { |
| 2824 | // Next subobject is a complex number. |
| 2825 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
| 2826 | if (Index > 1) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2827 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2828 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2829 | << handler.AccessKind; |
| 2830 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2831 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2832 | return handler.failed(); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2833 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2834 | |
| 2835 | bool WasConstQualified = ObjType.isConstQualified(); |
| 2836 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 2837 | if (WasConstQualified) |
| 2838 | ObjType.addConst(); |
| 2839 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2840 | assert(I == N - 1 && "extracting subobject of scalar?"); |
| 2841 | if (O->isComplexInt()) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2842 | return handler.found(Index ? O->getComplexIntImag() |
| 2843 | : O->getComplexIntReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2844 | } else { |
| 2845 | assert(O->isComplexFloat()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2846 | return handler.found(Index ? O->getComplexFloatImag() |
| 2847 | : O->getComplexFloatReal(), ObjType); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 2848 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2849 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 2850 | // In C++14 onwards, it is permitted to read a mutable member whose |
| 2851 | // lifetime began within the evaluation. |
| 2852 | // FIXME: Should we also allow this in C++11? |
| 2853 | if (Field->isMutable() && handler.AccessKind == AK_Read && |
| 2854 | !MayReadMutableMembers) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2855 | Info.FFDiag(E, diag::note_constexpr_ltor_mutable, 1) |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2856 | << Field; |
| 2857 | Info.Note(Field->getLocation(), diag::note_declared_at); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2858 | return handler.failed(); |
Richard Smith | 5a294e6 | 2012-02-09 03:29:58 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2861 | // Next subobject is a class, struct or union field. |
| 2862 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
| 2863 | if (RD->isUnion()) { |
| 2864 | const FieldDecl *UnionField = O->getUnionField(); |
| 2865 | if (!UnionField || |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2866 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2867 | Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2868 | << handler.AccessKind << Field << !UnionField << UnionField; |
| 2869 | return handler.failed(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 2870 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2871 | O = &O->getUnionValue(); |
| 2872 | } else |
| 2873 | O = &O->getStructField(Field->getFieldIndex()); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2874 | |
| 2875 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2876 | ObjType = Field->getType(); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2877 | if (WasConstQualified && !Field->isMutable()) |
| 2878 | ObjType.addConst(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2879 | |
| 2880 | if (ObjType.isVolatileQualified()) { |
| 2881 | if (Info.getLangOpts().CPlusPlus) { |
| 2882 | // FIXME: Include a description of the path to the volatile subobject. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2883 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2884 | << handler.AccessKind << 2 << Field; |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2885 | Info.Note(Field->getLocation(), diag::note_declared_at); |
| 2886 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2887 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2888 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2889 | return handler.failed(); |
Richard Smith | f2b681b | 2011-12-21 05:04:46 +0000 | [diff] [blame] | 2890 | } |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 2891 | |
| 2892 | LastField = Field; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2893 | } else { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2894 | // Next subobject is a base class. |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2895 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
| 2896 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
| 2897 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2898 | |
| 2899 | bool WasConstQualified = ObjType.isConstQualified(); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 2900 | ObjType = Info.Ctx.getRecordType(Base); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2901 | if (WasConstQualified) |
| 2902 | ObjType.addConst(); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 2903 | } |
| 2904 | } |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2907 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2908 | struct ExtractSubobjectHandler { |
| 2909 | EvalInfo &Info; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2910 | APValue &Result; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2911 | |
| 2912 | static const AccessKinds AccessKind = AK_Read; |
| 2913 | |
| 2914 | typedef bool result_type; |
| 2915 | bool failed() { return false; } |
| 2916 | bool found(APValue &Subobj, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2917 | Result = Subobj; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2918 | return true; |
| 2919 | } |
| 2920 | bool found(APSInt &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2921 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2922 | return true; |
| 2923 | } |
| 2924 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2925 | Result = APValue(Value); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2926 | return true; |
| 2927 | } |
| 2928 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2929 | Result = APValue(extractStringLiteralCharacter( |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2930 | Info, Subobj.getLValueBase().get<const Expr *>(), Character)); |
| 2931 | return true; |
| 2932 | } |
| 2933 | }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2934 | } // end anonymous namespace |
| 2935 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2936 | const AccessKinds ExtractSubobjectHandler::AccessKind; |
| 2937 | |
| 2938 | /// Extract the designated sub-object of an rvalue. |
| 2939 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2940 | const CompleteObject &Obj, |
| 2941 | const SubobjectDesignator &Sub, |
| 2942 | APValue &Result) { |
| 2943 | ExtractSubobjectHandler Handler = { Info, Result }; |
| 2944 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2947 | namespace { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2948 | struct ModifySubobjectHandler { |
| 2949 | EvalInfo &Info; |
| 2950 | APValue &NewVal; |
| 2951 | const Expr *E; |
| 2952 | |
| 2953 | typedef bool result_type; |
| 2954 | static const AccessKinds AccessKind = AK_Assign; |
| 2955 | |
| 2956 | bool checkConst(QualType QT) { |
| 2957 | // Assigning to a const object has undefined behavior. |
| 2958 | if (QT.isConstQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2959 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2960 | return false; |
| 2961 | } |
| 2962 | return true; |
| 2963 | } |
| 2964 | |
| 2965 | bool failed() { return false; } |
| 2966 | bool found(APValue &Subobj, QualType SubobjType) { |
| 2967 | if (!checkConst(SubobjType)) |
| 2968 | return false; |
| 2969 | // We've been given ownership of NewVal, so just swap it in. |
| 2970 | Subobj.swap(NewVal); |
| 2971 | return true; |
| 2972 | } |
| 2973 | bool found(APSInt &Value, QualType SubobjType) { |
| 2974 | if (!checkConst(SubobjType)) |
| 2975 | return false; |
| 2976 | if (!NewVal.isInt()) { |
| 2977 | // Maybe trying to write a cast pointer value into a complex? |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 2978 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2979 | return false; |
| 2980 | } |
| 2981 | Value = NewVal.getInt(); |
| 2982 | return true; |
| 2983 | } |
| 2984 | bool found(APFloat &Value, QualType SubobjType) { |
| 2985 | if (!checkConst(SubobjType)) |
| 2986 | return false; |
| 2987 | Value = NewVal.getFloat(); |
| 2988 | return true; |
| 2989 | } |
| 2990 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 2991 | llvm_unreachable("shouldn't encounter string elements with ExpandArrays"); |
| 2992 | } |
| 2993 | }; |
Benjamin Kramer | 62498ab | 2013-04-26 22:01:47 +0000 | [diff] [blame] | 2994 | } // end anonymous namespace |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2995 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 2996 | const AccessKinds ModifySubobjectHandler::AccessKind; |
| 2997 | |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 2998 | /// Update the designated sub-object of an rvalue to the given value. |
| 2999 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3000 | const CompleteObject &Obj, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3001 | const SubobjectDesignator &Sub, |
| 3002 | APValue &NewVal) { |
| 3003 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3004 | return findSubobject(Info, E, Obj, Sub, Handler); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3007 | /// Find the position where two subobject designators diverge, or equivalently |
| 3008 | /// the length of the common initial subsequence. |
| 3009 | static unsigned FindDesignatorMismatch(QualType ObjType, |
| 3010 | const SubobjectDesignator &A, |
| 3011 | const SubobjectDesignator &B, |
| 3012 | bool &WasArrayIndex) { |
| 3013 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
| 3014 | for (/**/; I != N; ++I) { |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3015 | if (!ObjType.isNull() && |
| 3016 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3017 | // Next subobject is an array element. |
| 3018 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
| 3019 | WasArrayIndex = true; |
| 3020 | return I; |
| 3021 | } |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 3022 | if (ObjType->isAnyComplexType()) |
| 3023 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
| 3024 | else |
| 3025 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3026 | } else { |
| 3027 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
| 3028 | WasArrayIndex = false; |
| 3029 | return I; |
| 3030 | } |
| 3031 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
| 3032 | // Next subobject is a field. |
| 3033 | ObjType = FD->getType(); |
| 3034 | else |
| 3035 | // Next subobject is a base class. |
| 3036 | ObjType = QualType(); |
| 3037 | } |
| 3038 | } |
| 3039 | WasArrayIndex = false; |
| 3040 | return I; |
| 3041 | } |
| 3042 | |
| 3043 | /// Determine whether the given subobject designators refer to elements of the |
| 3044 | /// same array object. |
| 3045 | static bool AreElementsOfSameArray(QualType ObjType, |
| 3046 | const SubobjectDesignator &A, |
| 3047 | const SubobjectDesignator &B) { |
| 3048 | if (A.Entries.size() != B.Entries.size()) |
| 3049 | return false; |
| 3050 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 3051 | bool IsArray = A.MostDerivedIsArrayElement; |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 3052 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
| 3053 | // A is a subobject of the array element. |
| 3054 | return false; |
| 3055 | |
| 3056 | // If A (and B) designates an array element, the last entry will be the array |
| 3057 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
| 3058 | // of length 1' case, and the entire path must match. |
| 3059 | bool WasArrayIndex; |
| 3060 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
| 3061 | return CommonLength >= A.Entries.size() - IsArray; |
| 3062 | } |
| 3063 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3064 | /// Find the complete object to which an LValue refers. |
Benjamin Kramer | 8407df7 | 2015-03-09 16:47:52 +0000 | [diff] [blame] | 3065 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
| 3066 | AccessKinds AK, const LValue &LVal, |
| 3067 | QualType LValType) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3068 | if (!LVal.Base) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3069 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3070 | return CompleteObject(); |
| 3071 | } |
| 3072 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3073 | CallStackFrame *Frame = nullptr; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3074 | if (LVal.getLValueCallIndex()) { |
| 3075 | Frame = Info.getCallFrame(LVal.getLValueCallIndex()); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3076 | if (!Frame) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3077 | Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3078 | << AK << LVal.Base.is<const ValueDecl*>(); |
| 3079 | NoteLValueLocation(Info, LVal.Base); |
| 3080 | return CompleteObject(); |
| 3081 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
| 3085 | // is not a constant expression (even if the object is non-volatile). We also |
| 3086 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
| 3087 | // semantics. |
| 3088 | if (LValType.isVolatileQualified()) { |
| 3089 | if (Info.getLangOpts().CPlusPlus) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3090 | Info.FFDiag(E, diag::note_constexpr_access_volatile_type) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3091 | << AK << LValType; |
| 3092 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3093 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3094 | return CompleteObject(); |
| 3095 | } |
| 3096 | |
| 3097 | // Compute value storage location and type of base object. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3098 | APValue *BaseVal = nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3099 | QualType BaseType = getType(LVal.Base); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3100 | bool LifetimeStartedInEvaluation = Frame; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3101 | |
| 3102 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
| 3103 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
| 3104 | // In C++11, constexpr, non-volatile variables initialized with constant |
| 3105 | // expressions are constant expressions too. Inside constexpr functions, |
| 3106 | // parameters are constant expressions even if they're non-const. |
| 3107 | // In C++1y, objects local to a constant expression (those with a Frame) are |
| 3108 | // both readable and writable inside constant expressions. |
| 3109 | // In C, such things can also be folded, although they are not ICEs. |
| 3110 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
| 3111 | if (VD) { |
| 3112 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
| 3113 | VD = VDef; |
| 3114 | } |
| 3115 | if (!VD || VD->isInvalidDecl()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3116 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3117 | return CompleteObject(); |
| 3118 | } |
| 3119 | |
| 3120 | // Accesses of volatile-qualified objects are not allowed. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3121 | if (BaseType.isVolatileQualified()) { |
| 3122 | if (Info.getLangOpts().CPlusPlus) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3123 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3124 | << AK << 1 << VD; |
| 3125 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3126 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3127 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3128 | } |
| 3129 | return CompleteObject(); |
| 3130 | } |
| 3131 | |
| 3132 | // Unless we're looking at a local variable or argument in a constexpr call, |
| 3133 | // the variable we're reading must be const. |
| 3134 | if (!Frame) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3135 | if (Info.getLangOpts().CPlusPlus14 && |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3136 | VD == Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()) { |
| 3137 | // OK, we can read and modify an object if we're in the process of |
| 3138 | // evaluating its initializer, because its lifetime began in this |
| 3139 | // evaluation. |
| 3140 | } else if (AK != AK_Read) { |
| 3141 | // All the remaining cases only permit reading. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3142 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3143 | return CompleteObject(); |
George Burgess IV | b531698 | 2016-12-27 05:33:20 +0000 | [diff] [blame] | 3144 | } else if (VD->isConstexpr()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3145 | // OK, we can read this variable. |
| 3146 | } else if (BaseType->isIntegralOrEnumerationType()) { |
Xiuli Pan | 244e3f6 | 2016-06-07 04:34:00 +0000 | [diff] [blame] | 3147 | // In OpenCL if a variable is in constant address space it is a const value. |
| 3148 | if (!(BaseType.isConstQualified() || |
| 3149 | (Info.getLangOpts().OpenCL && |
| 3150 | BaseType.getAddressSpace() == LangAS::opencl_constant))) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3151 | if (Info.getLangOpts().CPlusPlus) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3152 | Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3153 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3154 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3155 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3156 | } |
| 3157 | return CompleteObject(); |
| 3158 | } |
| 3159 | } else if (BaseType->isFloatingType() && BaseType.isConstQualified()) { |
| 3160 | // We support folding of const floating-point types, in order to make |
| 3161 | // static const data members of such types (supported as an extension) |
| 3162 | // more useful. |
| 3163 | if (Info.getLangOpts().CPlusPlus11) { |
| 3164 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
| 3165 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3166 | } else { |
| 3167 | Info.CCEDiag(E); |
| 3168 | } |
George Burgess IV | b531698 | 2016-12-27 05:33:20 +0000 | [diff] [blame] | 3169 | } else if (BaseType.isConstQualified() && VD->hasDefinition(Info.Ctx)) { |
| 3170 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr) << VD; |
| 3171 | // Keep evaluating to see what we can do. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3172 | } else { |
| 3173 | // FIXME: Allow folding of values of any literal type in all languages. |
Richard Smith | c0d04a2 | 2016-05-25 22:06:25 +0000 | [diff] [blame] | 3174 | if (Info.checkingPotentialConstantExpression() && |
| 3175 | VD->getType().isConstQualified() && !VD->hasDefinition(Info.Ctx)) { |
| 3176 | // The definition of this variable could be constexpr. We can't |
| 3177 | // access it right now, but may be able to in future. |
| 3178 | } else if (Info.getLangOpts().CPlusPlus11) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3179 | Info.FFDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3180 | Info.Note(VD->getLocation(), diag::note_declared_at); |
| 3181 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3182 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3183 | } |
| 3184 | return CompleteObject(); |
| 3185 | } |
| 3186 | } |
| 3187 | |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3188 | if (!evaluateVarDeclInit(Info, E, VD, Frame, BaseVal, &LVal)) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3189 | return CompleteObject(); |
| 3190 | } else { |
| 3191 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| 3192 | |
| 3193 | if (!Frame) { |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3194 | if (const MaterializeTemporaryExpr *MTE = |
| 3195 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
| 3196 | assert(MTE->getStorageDuration() == SD_Static && |
| 3197 | "should have a frame for a non-global materialized temporary"); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3198 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3199 | // Per C++1y [expr.const]p2: |
| 3200 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
| 3201 | // - a [...] glvalue of integral or enumeration type that refers to |
| 3202 | // a non-volatile const object [...] |
| 3203 | // [...] |
| 3204 | // - a [...] glvalue of literal type that refers to a non-volatile |
| 3205 | // object whose lifetime began within the evaluation of e. |
| 3206 | // |
| 3207 | // C++11 misses the 'began within the evaluation of e' check and |
| 3208 | // instead allows all temporaries, including things like: |
| 3209 | // int &&r = 1; |
| 3210 | // int x = ++r; |
| 3211 | // constexpr int k = r; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3212 | // Therefore we use the C++14 rules in C++11 too. |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3213 | const ValueDecl *VD = Info.EvaluatingDecl.dyn_cast<const ValueDecl*>(); |
| 3214 | const ValueDecl *ED = MTE->getExtendingDecl(); |
| 3215 | if (!(BaseType.isConstQualified() && |
| 3216 | BaseType->isIntegralOrEnumerationType()) && |
| 3217 | !(VD && VD->getCanonicalDecl() == ED->getCanonicalDecl())) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3218 | Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3219 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
| 3220 | return CompleteObject(); |
| 3221 | } |
| 3222 | |
| 3223 | BaseVal = Info.Ctx.getMaterializedTemporaryValue(MTE, false); |
| 3224 | assert(BaseVal && "got reference to unevaluated temporary"); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3225 | LifetimeStartedInEvaluation = true; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3226 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3227 | Info.FFDiag(E); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3228 | return CompleteObject(); |
| 3229 | } |
| 3230 | } else { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3231 | BaseVal = Frame->getTemporary(Base, LVal.Base.getVersion()); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3232 | assert(BaseVal && "missing value for temporary"); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 3233 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3234 | |
| 3235 | // Volatile temporary objects cannot be accessed in constant expressions. |
| 3236 | if (BaseType.isVolatileQualified()) { |
| 3237 | if (Info.getLangOpts().CPlusPlus) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3238 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3239 | << AK << 0; |
| 3240 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
| 3241 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3242 | Info.FFDiag(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3243 | } |
| 3244 | return CompleteObject(); |
| 3245 | } |
| 3246 | } |
| 3247 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3248 | // During the construction of an object, it is not yet 'const'. |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 3249 | // 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] | 3250 | // object under construction. |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3251 | if (Info.isEvaluatingConstructor(LVal.getLValueBase(), |
| 3252 | LVal.getLValueCallIndex(), |
| 3253 | LVal.getLValueVersion())) { |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3254 | BaseType = Info.Ctx.getCanonicalType(BaseType); |
| 3255 | BaseType.removeLocalConst(); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3256 | LifetimeStartedInEvaluation = true; |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3259 | // 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] | 3260 | // evaluating after an unmodeled side effect. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 3261 | // |
| 3262 | // FIXME: Not all local state is mutable. Allow local constant subobjects |
| 3263 | // to be read here (but take care with 'mutable' fields). |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 3264 | if ((Frame && Info.getLangOpts().CPlusPlus14 && |
| 3265 | Info.EvalStatus.HasSideEffects) || |
| 3266 | (AK != AK_Read && Info.IsSpeculativelyEvaluating)) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3267 | return CompleteObject(); |
| 3268 | |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3269 | return CompleteObject(BaseVal, BaseType, LifetimeStartedInEvaluation); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3272 | /// \brief Perform an lvalue-to-rvalue conversion on the given glvalue. This |
| 3273 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
| 3274 | /// glvalue referred to by an entity of reference type. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3275 | /// |
| 3276 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3277 | /// \param Conv - The expression for which we are performing the conversion. |
| 3278 | /// Used for diagnostics. |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3279 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
| 3280 | /// case of a non-class type). |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3281 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
| 3282 | /// \param RVal - The produced value will be placed here. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3283 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3284 | QualType Type, |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 3285 | const LValue &LVal, APValue &RVal) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3286 | if (LVal.Designator.Invalid) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3287 | return false; |
| 3288 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3289 | // 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] | 3290 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3291 | if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3292 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
| 3293 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
| 3294 | // initializer until now for such expressions. Such an expression can't be |
| 3295 | // an ICE in C, so this only matters for fold. |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3296 | if (Type.isVolatileQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3297 | Info.FFDiag(Conv); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3298 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 3299 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3300 | APValue Lit; |
| 3301 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
| 3302 | return false; |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3303 | CompleteObject LitObj(&Lit, Base->getType(), false); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3304 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal); |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 3305 | } else if (isa<StringLiteral>(Base) || isa<PredefinedExpr>(Base)) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3306 | // We represent a string literal array as an lvalue pointing at the |
| 3307 | // corresponding expression, rather than building an array of chars. |
Alexey Bataev | ec47478 | 2014-10-09 08:45:04 +0000 | [diff] [blame] | 3308 | // FIXME: Support ObjCEncodeExpr, MakeStringConstant |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3309 | APValue Str(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0); |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 3310 | CompleteObject StrObj(&Str, Base->getType(), false); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3311 | return extractSubobject(Info, Conv, StrObj, LVal.Designator, RVal); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 3312 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3315 | CompleteObject Obj = findCompleteObject(Info, Conv, AK_Read, LVal, Type); |
| 3316 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3320 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3321 | QualType LValType, APValue &Val) { |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3322 | if (LVal.Designator.Invalid) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3323 | return false; |
| 3324 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3325 | if (!Info.getLangOpts().CPlusPlus14) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3326 | Info.FFDiag(E); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 3327 | return false; |
| 3328 | } |
| 3329 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 3330 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame^] | 3331 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
| 3332 | } |
| 3333 | |
| 3334 | namespace { |
| 3335 | struct CompoundAssignSubobjectHandler { |
| 3336 | EvalInfo &Info; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3337 | const Expr *E; |
| 3338 | QualType PromotedLHSType; |
| 3339 | BinaryOperatorKind Opcode; |
| 3340 | const APValue &RHS; |
| 3341 | |
| 3342 | static const AccessKinds AccessKind = AK_Assign; |
| 3343 | |
| 3344 | typedef bool result_type; |
| 3345 | |
| 3346 | bool checkConst(QualType QT) { |
| 3347 | // Assigning to a const object has undefined behavior. |
| 3348 | if (QT.isConstQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3349 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3350 | return false; |
| 3351 | } |
| 3352 | return true; |
| 3353 | } |
| 3354 | |
| 3355 | bool failed() { return false; } |
| 3356 | bool found(APValue &Subobj, QualType SubobjType) { |
| 3357 | switch (Subobj.getKind()) { |
| 3358 | case APValue::Int: |
| 3359 | return found(Subobj.getInt(), SubobjType); |
| 3360 | case APValue::Float: |
| 3361 | return found(Subobj.getFloat(), SubobjType); |
| 3362 | case APValue::ComplexInt: |
| 3363 | case APValue::ComplexFloat: |
| 3364 | // FIXME: Implement complex compound assignment. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3365 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3366 | return false; |
| 3367 | case APValue::LValue: |
| 3368 | return foundPointer(Subobj, SubobjType); |
| 3369 | default: |
| 3370 | // FIXME: can this happen? |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3371 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3372 | return false; |
| 3373 | } |
| 3374 | } |
| 3375 | bool found(APSInt &Value, QualType SubobjType) { |
| 3376 | if (!checkConst(SubobjType)) |
| 3377 | return false; |
| 3378 | |
| 3379 | if (!SubobjType->isIntegerType() || !RHS.isInt()) { |
| 3380 | // We don't support compound assignment on integer-cast-to-pointer |
| 3381 | // values. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3382 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3383 | return false; |
| 3384 | } |
| 3385 | |
| 3386 | APSInt LHS = HandleIntToIntCast(Info, E, PromotedLHSType, |
| 3387 | SubobjType, Value); |
| 3388 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
| 3389 | return false; |
| 3390 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
| 3391 | return true; |
| 3392 | } |
| 3393 | bool found(APFloat &Value, QualType SubobjType) { |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3394 | return checkConst(SubobjType) && |
| 3395 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
| 3396 | Value) && |
| 3397 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
| 3398 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3399 | } |
| 3400 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 3401 | if (!checkConst(SubobjType)) |
| 3402 | return false; |
| 3403 | |
| 3404 | QualType PointeeType; |
| 3405 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 3406 | PointeeType = PT->getPointeeType(); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3407 | |
| 3408 | if (PointeeType.isNull() || !RHS.isInt() || |
| 3409 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3410 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3411 | return false; |
| 3412 | } |
| 3413 | |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 3414 | APSInt Offset = RHS.getInt(); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3415 | if (Opcode == BO_Sub) |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 3416 | negateAsSigned(Offset); |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 3417 | |
| 3418 | LValue LVal; |
| 3419 | LVal.setFrom(Info.Ctx, Subobj); |
| 3420 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
| 3421 | return false; |
| 3422 | LVal.moveInto(Subobj); |
| 3423 | return true; |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3424 | } |
| 3425 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 3426 | llvm_unreachable("shouldn't encounter string elements here"); |
| 3427 | } |
| 3428 | }; |
| 3429 | } // end anonymous namespace |
| 3430 | |
| 3431 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
| 3432 | |
| 3433 | /// Perform a compound assignment of LVal <op>= RVal. |
| 3434 | static bool handleCompoundAssignment( |
| 3435 | EvalInfo &Info, const Expr *E, |
| 3436 | const LValue &LVal, QualType LValType, QualType PromotedLValType, |
| 3437 | BinaryOperatorKind Opcode, const APValue &RVal) { |
| 3438 | if (LVal.Designator.Invalid) |
| 3439 | return false; |
| 3440 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3441 | if (!Info.getLangOpts().CPlusPlus14) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3442 | Info.FFDiag(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 3443 | return false; |
| 3444 | } |
| 3445 | |
| 3446 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
| 3447 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
| 3448 | RVal }; |
| 3449 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 3450 | } |
| 3451 | |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame^] | 3452 | namespace { |
| 3453 | struct IncDecSubobjectHandler { |
| 3454 | EvalInfo &Info; |
| 3455 | const UnaryOperator *E; |
| 3456 | AccessKinds AccessKind; |
| 3457 | APValue *Old; |
| 3458 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3459 | typedef bool result_type; |
| 3460 | |
| 3461 | bool checkConst(QualType QT) { |
| 3462 | // Assigning to a const object has undefined behavior. |
| 3463 | if (QT.isConstQualified()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3464 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3465 | return false; |
| 3466 | } |
| 3467 | return true; |
| 3468 | } |
| 3469 | |
| 3470 | bool failed() { return false; } |
| 3471 | bool found(APValue &Subobj, QualType SubobjType) { |
| 3472 | // Stash the old value. Also clear Old, so we don't clobber it later |
| 3473 | // if we're post-incrementing a complex. |
| 3474 | if (Old) { |
| 3475 | *Old = Subobj; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3476 | Old = nullptr; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | switch (Subobj.getKind()) { |
| 3480 | case APValue::Int: |
| 3481 | return found(Subobj.getInt(), SubobjType); |
| 3482 | case APValue::Float: |
| 3483 | return found(Subobj.getFloat(), SubobjType); |
| 3484 | case APValue::ComplexInt: |
| 3485 | return found(Subobj.getComplexIntReal(), |
| 3486 | SubobjType->castAs<ComplexType>()->getElementType() |
| 3487 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 3488 | case APValue::ComplexFloat: |
| 3489 | return found(Subobj.getComplexFloatReal(), |
| 3490 | SubobjType->castAs<ComplexType>()->getElementType() |
| 3491 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
| 3492 | case APValue::LValue: |
| 3493 | return foundPointer(Subobj, SubobjType); |
| 3494 | default: |
| 3495 | // FIXME: can this happen? |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3496 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3497 | return false; |
| 3498 | } |
| 3499 | } |
| 3500 | bool found(APSInt &Value, QualType SubobjType) { |
| 3501 | if (!checkConst(SubobjType)) |
| 3502 | return false; |
| 3503 | |
| 3504 | if (!SubobjType->isIntegerType()) { |
| 3505 | // We don't support increment / decrement on integer-cast-to-pointer |
| 3506 | // values. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3507 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3508 | return false; |
| 3509 | } |
| 3510 | |
| 3511 | if (Old) *Old = APValue(Value); |
| 3512 | |
| 3513 | // bool arithmetic promotes to int, and the conversion back to bool |
| 3514 | // doesn't reduce mod 2^n, so special-case it. |
| 3515 | if (SubobjType->isBooleanType()) { |
| 3516 | if (AccessKind == AK_Increment) |
| 3517 | Value = 1; |
| 3518 | else |
| 3519 | Value = !Value; |
| 3520 | return true; |
| 3521 | } |
| 3522 | |
| 3523 | bool WasNegative = Value.isNegative(); |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame^] | 3524 | if (AccessKind == AK_Increment) { |
| 3525 | ++Value; |
| 3526 | |
| 3527 | if (!WasNegative && Value.isNegative() && E->canOverflow()) { |
| 3528 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
| 3529 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
| 3530 | } |
| 3531 | } else { |
| 3532 | --Value; |
| 3533 | |
| 3534 | if (WasNegative && !Value.isNegative() && E->canOverflow()) { |
| 3535 | unsigned BitWidth = Value.getBitWidth(); |
| 3536 | APSInt ActualValue(Value.sext(BitWidth + 1), /*IsUnsigned*/false); |
| 3537 | ActualValue.setBit(BitWidth); |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 3538 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3539 | } |
| 3540 | } |
| 3541 | return true; |
| 3542 | } |
| 3543 | bool found(APFloat &Value, QualType SubobjType) { |
| 3544 | if (!checkConst(SubobjType)) |
| 3545 | return false; |
| 3546 | |
| 3547 | if (Old) *Old = APValue(Value); |
| 3548 | |
| 3549 | APFloat One(Value.getSemantics(), 1); |
| 3550 | if (AccessKind == AK_Increment) |
| 3551 | Value.add(One, APFloat::rmNearestTiesToEven); |
| 3552 | else |
| 3553 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
| 3554 | return true; |
| 3555 | } |
| 3556 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
| 3557 | if (!checkConst(SubobjType)) |
| 3558 | return false; |
| 3559 | |
| 3560 | QualType PointeeType; |
| 3561 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
| 3562 | PointeeType = PT->getPointeeType(); |
| 3563 | else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3564 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3565 | return false; |
| 3566 | } |
| 3567 | |
| 3568 | LValue LVal; |
| 3569 | LVal.setFrom(Info.Ctx, Subobj); |
| 3570 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
| 3571 | AccessKind == AK_Increment ? 1 : -1)) |
| 3572 | return false; |
| 3573 | LVal.moveInto(Subobj); |
| 3574 | return true; |
| 3575 | } |
| 3576 | bool foundString(APValue &Subobj, QualType SubobjType, uint64_t Character) { |
| 3577 | llvm_unreachable("shouldn't encounter string elements here"); |
| 3578 | } |
| 3579 | }; |
| 3580 | } // end anonymous namespace |
| 3581 | |
| 3582 | /// Perform an increment or decrement on LVal. |
| 3583 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
| 3584 | QualType LValType, bool IsIncrement, APValue *Old) { |
| 3585 | if (LVal.Designator.Invalid) |
| 3586 | return false; |
| 3587 | |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3588 | if (!Info.getLangOpts().CPlusPlus14) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3589 | Info.FFDiag(E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 3590 | return false; |
| 3591 | } |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame^] | 3592 | |
| 3593 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
| 3594 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
| 3595 | IncDecSubobjectHandler Handler = {Info, cast<UnaryOperator>(E), AK, Old}; |
| 3596 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
| 3597 | } |
| 3598 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3599 | /// Build an lvalue for the object argument of a member function call. |
| 3600 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
| 3601 | LValue &This) { |
| 3602 | if (Object->getType()->isPointerType()) |
| 3603 | return EvaluatePointer(Object, This, Info); |
| 3604 | |
| 3605 | if (Object->isGLValue()) |
| 3606 | return EvaluateLValue(Object, This, Info); |
| 3607 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3608 | if (Object->getType()->isLiteralType(Info.Ctx)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3609 | return EvaluateTemporary(Object, This, Info); |
| 3610 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3611 | Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3612 | return false; |
| 3613 | } |
| 3614 | |
| 3615 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
| 3616 | /// lvalue referring to the result. |
| 3617 | /// |
| 3618 | /// \param Info - Information about the ongoing evaluation. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3619 | /// \param LV - An lvalue referring to the base of the member pointer. |
| 3620 | /// \param RHS - The member pointer expression. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3621 | /// \param IncludeMember - Specifies whether the member itself is included in |
| 3622 | /// the resulting LValue subobject designator. This is not possible when |
| 3623 | /// creating a bound member function. |
| 3624 | /// \return The field or method declaration to which the member pointer refers, |
| 3625 | /// or 0 if evaluation fails. |
| 3626 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3627 | QualType LVType, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3628 | LValue &LV, |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3629 | const Expr *RHS, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3630 | bool IncludeMember = true) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3631 | MemberPtr MemPtr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3632 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3633 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3634 | |
| 3635 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
| 3636 | // member value, the behavior is undefined. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3637 | if (!MemPtr.getDecl()) { |
| 3638 | // FIXME: Specific diagnostic. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3639 | Info.FFDiag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3640 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3641 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 3642 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3643 | if (MemPtr.isDerivedMember()) { |
| 3644 | // This is a member of some derived class. Truncate LV appropriately. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3645 | // The end of the derived-to-base path for the base object must match the |
| 3646 | // derived-to-base path for the member pointer. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3647 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3648 | LV.Designator.Entries.size()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3649 | Info.FFDiag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3650 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3651 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3652 | unsigned PathLengthToMember = |
| 3653 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
| 3654 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
| 3655 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
| 3656 | LV.Designator.Entries[PathLengthToMember + I]); |
| 3657 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3658 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3659 | Info.FFDiag(RHS); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3660 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3661 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3665 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3666 | PathLengthToMember)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3667 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3668 | } else if (!MemPtr.Path.empty()) { |
| 3669 | // Extend the LValue path with the member pointer's path. |
| 3670 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
| 3671 | MemPtr.Path.size() + IncludeMember); |
| 3672 | |
| 3673 | // Walk down to the appropriate base class. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3674 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
| 3675 | LVType = PT->getPointeeType(); |
| 3676 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
| 3677 | assert(RD && "member pointer access on non-class-type expression"); |
| 3678 | // The first class in the path is that of the lvalue. |
| 3679 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
| 3680 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3681 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3682 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3683 | RD = Base; |
| 3684 | } |
| 3685 | // Finally cast to the class containing the member. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3686 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
| 3687 | MemPtr.getContainingRecord())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3688 | return nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | // Add the member. Note that we cannot build bound member functions here. |
| 3692 | if (IncludeMember) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3693 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3694 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3695 | return nullptr; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3696 | } else if (const IndirectFieldDecl *IFD = |
| 3697 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3698 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3699 | return nullptr; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3700 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 3701 | llvm_unreachable("can't construct reference to bound member function"); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 3702 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | return MemPtr.getDecl(); |
| 3706 | } |
| 3707 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3708 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
| 3709 | const BinaryOperator *BO, |
| 3710 | LValue &LV, |
| 3711 | bool IncludeMember = true) { |
| 3712 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
| 3713 | |
| 3714 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 3715 | if (Info.noteFailure()) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3716 | MemberPtr MemPtr; |
| 3717 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
| 3718 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3719 | return nullptr; |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
| 3723 | BO->getRHS(), IncludeMember); |
| 3724 | } |
| 3725 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3726 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
| 3727 | /// the provided lvalue, which currently refers to the base object. |
| 3728 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
| 3729 | LValue &Result) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3730 | SubobjectDesignator &D = Result.Designator; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3731 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3732 | return false; |
| 3733 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3734 | QualType TargetQT = E->getType(); |
| 3735 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
| 3736 | TargetQT = PT->getPointeeType(); |
| 3737 | |
| 3738 | // Check this cast lands within the final derived-to-base subobject path. |
| 3739 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3740 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3741 | << D.MostDerivedType << TargetQT; |
| 3742 | return false; |
| 3743 | } |
| 3744 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3745 | // Check the type of the final cast. We don't need to check the path, |
| 3746 | // since a cast can only be formed if the path is unique. |
| 3747 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3748 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
| 3749 | const CXXRecordDecl *FinalType; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3750 | if (NewEntriesSize == D.MostDerivedPathLength) |
| 3751 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
| 3752 | else |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3753 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3754 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 3755 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3756 | << D.MostDerivedType << TargetQT; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3757 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3758 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 3759 | |
| 3760 | // Truncate the lvalue to the appropriate derived class. |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 3761 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 3764 | namespace { |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3765 | enum EvalStmtResult { |
| 3766 | /// Evaluation failed. |
| 3767 | ESR_Failed, |
| 3768 | /// Hit a 'return' statement. |
| 3769 | ESR_Returned, |
| 3770 | /// Evaluation succeeded. |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3771 | ESR_Succeeded, |
| 3772 | /// Hit a 'continue' statement. |
| 3773 | ESR_Continue, |
| 3774 | /// Hit a 'break' statement. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3775 | ESR_Break, |
| 3776 | /// Still scanning for 'case' or 'default' statement. |
| 3777 | ESR_CaseNotFound |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3778 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3779 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3780 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3781 | static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) { |
| 3782 | // We don't need to evaluate the initializer for a static local. |
| 3783 | if (!VD->hasLocalStorage()) |
| 3784 | return true; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3785 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3786 | LValue Result; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3787 | APValue &Val = createTemporary(VD, true, Result, *Info.CurrentCall); |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3788 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3789 | const Expr *InitE = VD->getInit(); |
| 3790 | if (!InitE) { |
| 3791 | Info.FFDiag(VD->getLocStart(), diag::note_constexpr_uninitialized) |
| 3792 | << false << VD->getType(); |
| 3793 | Val = APValue(); |
| 3794 | return false; |
| 3795 | } |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3796 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3797 | if (InitE->isValueDependent()) |
| 3798 | return false; |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 3799 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3800 | if (!EvaluateInPlace(Val, Info, Result, InitE)) { |
| 3801 | // Wipe out any partially-computed value, to allow tracking that this |
| 3802 | // evaluation failed. |
| 3803 | Val = APValue(); |
| 3804 | return false; |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | return true; |
| 3808 | } |
| 3809 | |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 3810 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
| 3811 | bool OK = true; |
| 3812 | |
| 3813 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 3814 | OK &= EvaluateVarDecl(Info, VD); |
| 3815 | |
| 3816 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(D)) |
| 3817 | for (auto *BD : DD->bindings()) |
| 3818 | if (auto *VD = BD->getHoldingVar()) |
| 3819 | OK &= EvaluateDecl(Info, VD); |
| 3820 | |
| 3821 | return OK; |
| 3822 | } |
| 3823 | |
| 3824 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3825 | /// Evaluate a condition (either a variable declaration or an expression). |
| 3826 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
| 3827 | const Expr *Cond, bool &Result) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3828 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3829 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
| 3830 | return false; |
| 3831 | return EvaluateAsBooleanCondition(Cond, Result, Info); |
| 3832 | } |
| 3833 | |
Richard Smith | 8921007 | 2016-04-04 23:29:43 +0000 | [diff] [blame] | 3834 | namespace { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3835 | /// \brief A location where the result (returned value) of evaluating a |
| 3836 | /// statement should be stored. |
| 3837 | struct StmtResult { |
| 3838 | /// The APValue that should be filled in with the returned value. |
| 3839 | APValue &Value; |
| 3840 | /// The location containing the result, if any (used to support RVO). |
| 3841 | const LValue *Slot; |
| 3842 | }; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 3843 | |
| 3844 | struct TempVersionRAII { |
| 3845 | CallStackFrame &Frame; |
| 3846 | |
| 3847 | TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) { |
| 3848 | Frame.pushTempVersion(); |
| 3849 | } |
| 3850 | |
| 3851 | ~TempVersionRAII() { |
| 3852 | Frame.popTempVersion(); |
| 3853 | } |
| 3854 | }; |
| 3855 | |
Richard Smith | 8921007 | 2016-04-04 23:29:43 +0000 | [diff] [blame] | 3856 | } |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3857 | |
| 3858 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3859 | const Stmt *S, |
| 3860 | const SwitchCase *SC = nullptr); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3861 | |
| 3862 | /// Evaluate the body of a loop, and translate the result as appropriate. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3863 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3864 | const Stmt *Body, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3865 | const SwitchCase *Case = nullptr) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3866 | BlockScopeRAII Scope(Info); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3867 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case)) { |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3868 | case ESR_Break: |
| 3869 | return ESR_Succeeded; |
| 3870 | case ESR_Succeeded: |
| 3871 | case ESR_Continue: |
| 3872 | return ESR_Continue; |
| 3873 | case ESR_Failed: |
| 3874 | case ESR_Returned: |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3875 | case ESR_CaseNotFound: |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3876 | return ESR; |
| 3877 | } |
Hans Wennborg | 9242bd1 | 2013-05-06 15:13:34 +0000 | [diff] [blame] | 3878 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3881 | /// Evaluate a switch statement. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3882 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3883 | const SwitchStmt *SS) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3884 | BlockScopeRAII Scope(Info); |
| 3885 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3886 | // Evaluate the switch condition. |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3887 | APSInt Value; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3888 | { |
| 3889 | FullExpressionRAII Scope(Info); |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 3890 | if (const Stmt *Init = SS->getInit()) { |
| 3891 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
| 3892 | if (ESR != ESR_Succeeded) |
| 3893 | return ESR; |
| 3894 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3895 | if (SS->getConditionVariable() && |
| 3896 | !EvaluateDecl(Info, SS->getConditionVariable())) |
| 3897 | return ESR_Failed; |
| 3898 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
| 3899 | return ESR_Failed; |
| 3900 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3901 | |
| 3902 | // Find the switch case corresponding to the value of the condition. |
| 3903 | // FIXME: Cache this lookup. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3904 | const SwitchCase *Found = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3905 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
| 3906 | SC = SC->getNextSwitchCase()) { |
| 3907 | if (isa<DefaultStmt>(SC)) { |
| 3908 | Found = SC; |
| 3909 | continue; |
| 3910 | } |
| 3911 | |
| 3912 | const CaseStmt *CS = cast<CaseStmt>(SC); |
| 3913 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
| 3914 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
| 3915 | : LHS; |
| 3916 | if (LHS <= Value && Value <= RHS) { |
| 3917 | Found = SC; |
| 3918 | break; |
| 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | if (!Found) |
| 3923 | return ESR_Succeeded; |
| 3924 | |
| 3925 | // Search the switch body for the switch case and evaluate it from there. |
| 3926 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found)) { |
| 3927 | case ESR_Break: |
| 3928 | return ESR_Succeeded; |
| 3929 | case ESR_Succeeded: |
| 3930 | case ESR_Continue: |
| 3931 | case ESR_Failed: |
| 3932 | case ESR_Returned: |
| 3933 | return ESR; |
| 3934 | case ESR_CaseNotFound: |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3935 | // This can only happen if the switch case is nested within a statement |
| 3936 | // expression. We have no intention of supporting that. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 3937 | Info.FFDiag(Found->getLocStart(), diag::note_constexpr_stmt_expr_unsupported); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 3938 | return ESR_Failed; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3939 | } |
Richard Smith | f8cf9d4 | 2013-05-13 20:33:30 +0000 | [diff] [blame] | 3940 | llvm_unreachable("Invalid EvalStmtResult!"); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 3943 | // Evaluate a statement. |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 3944 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3945 | const Stmt *S, const SwitchCase *Case) { |
Richard Smith | a3d3bd2 | 2013-05-08 02:12:03 +0000 | [diff] [blame] | 3946 | if (!Info.nextStep(S)) |
| 3947 | return ESR_Failed; |
| 3948 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3949 | // If we're hunting down a 'case' or 'default' label, recurse through |
| 3950 | // substatements until we hit the label. |
| 3951 | if (Case) { |
| 3952 | // FIXME: We don't start the lifetime of objects whose initialization we |
| 3953 | // jump over. However, such objects must be of class type with a trivial |
| 3954 | // default constructor that initialize all subobjects, so must be empty, |
| 3955 | // so this almost never matters. |
| 3956 | switch (S->getStmtClass()) { |
| 3957 | case Stmt::CompoundStmtClass: |
| 3958 | // FIXME: Precompute which substatement of a compound statement we |
| 3959 | // would jump to, and go straight there rather than performing a |
| 3960 | // linear scan each time. |
| 3961 | case Stmt::LabelStmtClass: |
| 3962 | case Stmt::AttributedStmtClass: |
| 3963 | case Stmt::DoStmtClass: |
| 3964 | break; |
| 3965 | |
| 3966 | case Stmt::CaseStmtClass: |
| 3967 | case Stmt::DefaultStmtClass: |
| 3968 | if (Case == S) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3969 | Case = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3970 | break; |
| 3971 | |
| 3972 | case Stmt::IfStmtClass: { |
| 3973 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
| 3974 | // straight there rather than scanning both sides. |
| 3975 | const IfStmt *IS = cast<IfStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 3976 | |
| 3977 | // Wrap the evaluation in a block scope, in case it's a DeclStmt |
| 3978 | // preceded by our switch label. |
| 3979 | BlockScopeRAII Scope(Info); |
| 3980 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 3981 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
| 3982 | if (ESR != ESR_CaseNotFound || !IS->getElse()) |
| 3983 | return ESR; |
| 3984 | return EvaluateStmt(Result, Info, IS->getElse(), Case); |
| 3985 | } |
| 3986 | |
| 3987 | case Stmt::WhileStmtClass: { |
| 3988 | EvalStmtResult ESR = |
| 3989 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
| 3990 | if (ESR != ESR_Continue) |
| 3991 | return ESR; |
| 3992 | break; |
| 3993 | } |
| 3994 | |
| 3995 | case Stmt::ForStmtClass: { |
| 3996 | const ForStmt *FS = cast<ForStmt>(S); |
| 3997 | EvalStmtResult ESR = |
| 3998 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
| 3999 | if (ESR != ESR_Continue) |
| 4000 | return ESR; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4001 | if (FS->getInc()) { |
| 4002 | FullExpressionRAII IncScope(Info); |
| 4003 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 4004 | return ESR_Failed; |
| 4005 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4006 | break; |
| 4007 | } |
| 4008 | |
| 4009 | case Stmt::DeclStmtClass: |
| 4010 | // FIXME: If the variable has initialization that can't be jumped over, |
| 4011 | // bail out of any immediately-surrounding compound-statement too. |
| 4012 | default: |
| 4013 | return ESR_CaseNotFound; |
| 4014 | } |
| 4015 | } |
| 4016 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4017 | switch (S->getStmtClass()) { |
| 4018 | default: |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4019 | if (const Expr *E = dyn_cast<Expr>(S)) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4020 | // Don't bother evaluating beyond an expression-statement which couldn't |
| 4021 | // be evaluated. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4022 | FullExpressionRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4023 | if (!EvaluateIgnoredValue(Info, E)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4024 | return ESR_Failed; |
| 4025 | return ESR_Succeeded; |
| 4026 | } |
| 4027 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4028 | Info.FFDiag(S->getLocStart()); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4029 | return ESR_Failed; |
| 4030 | |
| 4031 | case Stmt::NullStmtClass: |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4032 | return ESR_Succeeded; |
| 4033 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4034 | case Stmt::DeclStmtClass: { |
| 4035 | const DeclStmt *DS = cast<DeclStmt>(S); |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 4036 | for (const auto *DclIt : DS->decls()) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4037 | // Each declaration initialization is its own full-expression. |
| 4038 | // FIXME: This isn't quite right; if we're performing aggregate |
| 4039 | // initialization, each braced subexpression is its own full-expression. |
| 4040 | FullExpressionRAII Scope(Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 4041 | if (!EvaluateDecl(Info, DclIt) && !Info.noteFailure()) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4042 | return ESR_Failed; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4043 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4044 | return ESR_Succeeded; |
| 4045 | } |
| 4046 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4047 | case Stmt::ReturnStmtClass: { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4048 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4049 | FullExpressionRAII Scope(Info); |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4050 | if (RetExpr && |
| 4051 | !(Result.Slot |
| 4052 | ? EvaluateInPlace(Result.Value, Info, *Result.Slot, RetExpr) |
| 4053 | : Evaluate(Result.Value, Info, RetExpr))) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4054 | return ESR_Failed; |
| 4055 | return ESR_Returned; |
| 4056 | } |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4057 | |
| 4058 | case Stmt::CompoundStmtClass: { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4059 | BlockScopeRAII Scope(Info); |
| 4060 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4061 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 4062 | for (const auto *BI : CS->body()) { |
| 4063 | EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case); |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4064 | if (ESR == ESR_Succeeded) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4065 | Case = nullptr; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4066 | else if (ESR != ESR_CaseNotFound) |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4067 | return ESR; |
| 4068 | } |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4069 | return Case ? ESR_CaseNotFound : ESR_Succeeded; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4070 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4071 | |
| 4072 | case Stmt::IfStmtClass: { |
| 4073 | const IfStmt *IS = cast<IfStmt>(S); |
| 4074 | |
| 4075 | // Evaluate the condition, as either a var decl or as an expression. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4076 | BlockScopeRAII Scope(Info); |
Richard Smith | a547eb2 | 2016-07-14 00:11:03 +0000 | [diff] [blame] | 4077 | if (const Stmt *Init = IS->getInit()) { |
| 4078 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
| 4079 | if (ESR != ESR_Succeeded) |
| 4080 | return ESR; |
| 4081 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4082 | bool Cond; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4083 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4084 | return ESR_Failed; |
| 4085 | |
| 4086 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
| 4087 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
| 4088 | if (ESR != ESR_Succeeded) |
| 4089 | return ESR; |
| 4090 | } |
| 4091 | return ESR_Succeeded; |
| 4092 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4093 | |
| 4094 | case Stmt::WhileStmtClass: { |
| 4095 | const WhileStmt *WS = cast<WhileStmt>(S); |
| 4096 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4097 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4098 | bool Continue; |
| 4099 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
| 4100 | Continue)) |
| 4101 | return ESR_Failed; |
| 4102 | if (!Continue) |
| 4103 | break; |
| 4104 | |
| 4105 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
| 4106 | if (ESR != ESR_Continue) |
| 4107 | return ESR; |
| 4108 | } |
| 4109 | return ESR_Succeeded; |
| 4110 | } |
| 4111 | |
| 4112 | case Stmt::DoStmtClass: { |
| 4113 | const DoStmt *DS = cast<DoStmt>(S); |
| 4114 | bool Continue; |
| 4115 | do { |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4116 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4117 | if (ESR != ESR_Continue) |
| 4118 | return ESR; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4119 | Case = nullptr; |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4120 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4121 | FullExpressionRAII CondScope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4122 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info)) |
| 4123 | return ESR_Failed; |
| 4124 | } while (Continue); |
| 4125 | return ESR_Succeeded; |
| 4126 | } |
| 4127 | |
| 4128 | case Stmt::ForStmtClass: { |
| 4129 | const ForStmt *FS = cast<ForStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4130 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4131 | if (FS->getInit()) { |
| 4132 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
| 4133 | if (ESR != ESR_Succeeded) |
| 4134 | return ESR; |
| 4135 | } |
| 4136 | while (true) { |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4137 | BlockScopeRAII Scope(Info); |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4138 | bool Continue = true; |
| 4139 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
| 4140 | FS->getCond(), Continue)) |
| 4141 | return ESR_Failed; |
| 4142 | if (!Continue) |
| 4143 | break; |
| 4144 | |
| 4145 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 4146 | if (ESR != ESR_Continue) |
| 4147 | return ESR; |
| 4148 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4149 | if (FS->getInc()) { |
| 4150 | FullExpressionRAII IncScope(Info); |
| 4151 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 4152 | return ESR_Failed; |
| 4153 | } |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4154 | } |
| 4155 | return ESR_Succeeded; |
| 4156 | } |
| 4157 | |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4158 | case Stmt::CXXForRangeStmtClass: { |
| 4159 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4160 | BlockScopeRAII Scope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4161 | |
| 4162 | // Initialize the __range variable. |
| 4163 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
| 4164 | if (ESR != ESR_Succeeded) |
| 4165 | return ESR; |
| 4166 | |
| 4167 | // Create the __begin and __end iterators. |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 4168 | ESR = EvaluateStmt(Result, Info, FS->getBeginStmt()); |
| 4169 | if (ESR != ESR_Succeeded) |
| 4170 | return ESR; |
| 4171 | ESR = EvaluateStmt(Result, Info, FS->getEndStmt()); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4172 | if (ESR != ESR_Succeeded) |
| 4173 | return ESR; |
| 4174 | |
| 4175 | while (true) { |
| 4176 | // Condition: __begin != __end. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4177 | { |
| 4178 | bool Continue = true; |
| 4179 | FullExpressionRAII CondExpr(Info); |
| 4180 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
| 4181 | return ESR_Failed; |
| 4182 | if (!Continue) |
| 4183 | break; |
| 4184 | } |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4185 | |
| 4186 | // User's variable declaration, initialized by *__begin. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4187 | BlockScopeRAII InnerScope(Info); |
Richard Smith | 896e0d7 | 2013-05-06 06:51:17 +0000 | [diff] [blame] | 4188 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
| 4189 | if (ESR != ESR_Succeeded) |
| 4190 | return ESR; |
| 4191 | |
| 4192 | // Loop body. |
| 4193 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
| 4194 | if (ESR != ESR_Continue) |
| 4195 | return ESR; |
| 4196 | |
| 4197 | // Increment: ++__begin |
| 4198 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
| 4199 | return ESR_Failed; |
| 4200 | } |
| 4201 | |
| 4202 | return ESR_Succeeded; |
| 4203 | } |
| 4204 | |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4205 | case Stmt::SwitchStmtClass: |
| 4206 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
| 4207 | |
Richard Smith | 4e18ca5 | 2013-05-06 05:56:11 +0000 | [diff] [blame] | 4208 | case Stmt::ContinueStmtClass: |
| 4209 | return ESR_Continue; |
| 4210 | |
| 4211 | case Stmt::BreakStmtClass: |
| 4212 | return ESR_Break; |
Richard Smith | 496ddcf | 2013-05-12 17:32:42 +0000 | [diff] [blame] | 4213 | |
| 4214 | case Stmt::LabelStmtClass: |
| 4215 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
| 4216 | |
| 4217 | case Stmt::AttributedStmtClass: |
| 4218 | // As a general principle, C++11 attributes can be ignored without |
| 4219 | // any semantic impact. |
| 4220 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
| 4221 | Case); |
| 4222 | |
| 4223 | case Stmt::CaseStmtClass: |
| 4224 | case Stmt::DefaultStmtClass: |
| 4225 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4229 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
| 4230 | /// default constructor. If so, we'll fold it whether or not it's marked as |
| 4231 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
| 4232 | /// so we need special handling. |
| 4233 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4234 | const CXXConstructorDecl *CD, |
| 4235 | bool IsValueInitialization) { |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4236 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
| 4237 | return false; |
| 4238 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 4239 | // Value-initialization does not call a trivial default constructor, so such a |
| 4240 | // call is a core constant expression whether or not the constructor is |
| 4241 | // constexpr. |
| 4242 | if (!CD->isConstexpr() && !IsValueInitialization) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4243 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 4244 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
| 4245 | // we should be much more explicit about why it's not constexpr. |
| 4246 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
| 4247 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
| 4248 | Info.Note(CD->getLocation(), diag::note_declared_at); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 4249 | } else { |
| 4250 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
| 4251 | } |
| 4252 | } |
| 4253 | return true; |
| 4254 | } |
| 4255 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4256 | /// CheckConstexprFunction - Check that a function can be called in a constant |
| 4257 | /// expression. |
| 4258 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
| 4259 | const FunctionDecl *Declaration, |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 4260 | const FunctionDecl *Definition, |
| 4261 | const Stmt *Body) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4262 | // Potential constant expressions can contain calls to declared, but not yet |
| 4263 | // defined, constexpr functions. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4264 | if (Info.checkingPotentialConstantExpression() && !Definition && |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4265 | Declaration->isConstexpr()) |
| 4266 | return false; |
| 4267 | |
Richard Smith | 0838f3a | 2013-05-14 05:18:44 +0000 | [diff] [blame] | 4268 | // Bail out with no diagnostic if the function declaration itself is invalid. |
| 4269 | // We will have produced a relevant diagnostic while parsing it. |
| 4270 | if (Declaration->isInvalidDecl()) |
| 4271 | return false; |
| 4272 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4273 | // Can we evaluate this function call? |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 4274 | if (Definition && Definition->isConstexpr() && |
| 4275 | !Definition->isInvalidDecl() && Body) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4276 | return true; |
| 4277 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4278 | if (Info.getLangOpts().CPlusPlus11) { |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4279 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 4280 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4281 | // If this function is not constexpr because it is an inherited |
| 4282 | // non-constexpr constructor, diagnose that directly. |
| 4283 | auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl); |
| 4284 | if (CD && CD->isInheritingConstructor()) { |
| 4285 | auto *Inherited = CD->getInheritedConstructor().getConstructor(); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 4286 | if (!Inherited->isConstexpr()) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4287 | DiagDecl = CD = Inherited; |
| 4288 | } |
| 4289 | |
| 4290 | // FIXME: If DiagDecl is an implicitly-declared special member function |
| 4291 | // or an inheriting constructor, we should be much more explicit about why |
| 4292 | // it's not constexpr. |
| 4293 | if (CD && CD->isInheritingConstructor()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4294 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4295 | << CD->getInheritedConstructor().getConstructor()->getParent(); |
| 4296 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4297 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1) |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4298 | << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4299 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
| 4300 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4301 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 4302 | } |
| 4303 | return false; |
| 4304 | } |
| 4305 | |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 4306 | /// Determine if a class has any fields that might need to be copied by a |
| 4307 | /// trivial copy or move operation. |
| 4308 | static bool hasFields(const CXXRecordDecl *RD) { |
| 4309 | if (!RD || RD->isEmpty()) |
| 4310 | return false; |
| 4311 | for (auto *FD : RD->fields()) { |
| 4312 | if (FD->isUnnamedBitfield()) |
| 4313 | continue; |
| 4314 | return true; |
| 4315 | } |
| 4316 | for (auto &Base : RD->bases()) |
| 4317 | if (hasFields(Base.getType()->getAsCXXRecordDecl())) |
| 4318 | return true; |
| 4319 | return false; |
| 4320 | } |
| 4321 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4322 | namespace { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4323 | typedef SmallVector<APValue, 8> ArgVector; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | /// EvaluateArgs - Evaluate the arguments to a function call. |
| 4327 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
| 4328 | EvalInfo &Info) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4329 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4330 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4331 | I != E; ++I) { |
| 4332 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
| 4333 | // If we're checking for a potential constant expression, evaluate all |
| 4334 | // initializers even if some of them fail. |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 4335 | if (!Info.noteFailure()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4336 | return false; |
| 4337 | Success = false; |
| 4338 | } |
| 4339 | } |
| 4340 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4341 | } |
| 4342 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4343 | /// Evaluate a function call. |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4344 | static bool HandleFunctionCall(SourceLocation CallLoc, |
| 4345 | const FunctionDecl *Callee, const LValue *This, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4346 | ArrayRef<const Expr*> Args, const Stmt *Body, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4347 | EvalInfo &Info, APValue &Result, |
| 4348 | const LValue *ResultSlot) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4349 | ArgVector ArgValues(Args.size()); |
| 4350 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 4351 | return false; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4352 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4353 | if (!Info.CheckCallLimit(CallLoc)) |
| 4354 | return false; |
| 4355 | |
| 4356 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4357 | |
| 4358 | // For a trivial copy or move assignment, perform an APValue copy. This is |
| 4359 | // essential for unions, where the operations performed by the assignment |
| 4360 | // operator cannot be represented as statements. |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 4361 | // |
| 4362 | // Skip this for non-union classes with no fields; in that case, the defaulted |
| 4363 | // copy/move does not actually read the object. |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4364 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 4365 | if (MD && MD->isDefaulted() && |
| 4366 | (MD->getParent()->isUnion() || |
| 4367 | (MD->isTrivial() && hasFields(MD->getParent())))) { |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4368 | assert(This && |
| 4369 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
| 4370 | LValue RHS; |
| 4371 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
| 4372 | APValue RHSValue; |
| 4373 | if (!handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
| 4374 | RHS, RHSValue)) |
| 4375 | return false; |
| 4376 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(Info.Ctx), |
| 4377 | RHSValue)) |
| 4378 | return false; |
| 4379 | This->moveInto(Result); |
| 4380 | return true; |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 4381 | } else if (MD && isLambdaCallOperator(MD)) { |
Erik Pilkington | 1123291 | 2018-04-05 00:12:05 +0000 | [diff] [blame] | 4382 | // We're in a lambda; determine the lambda capture field maps unless we're |
| 4383 | // just constexpr checking a lambda's call operator. constexpr checking is |
| 4384 | // done before the captures have been added to the closure object (unless |
| 4385 | // we're inferring constexpr-ness), so we don't have access to them in this |
| 4386 | // case. But since we don't need the captures to constexpr check, we can |
| 4387 | // just ignore them. |
| 4388 | if (!Info.checkingPotentialConstantExpression()) |
| 4389 | MD->getParent()->getCaptureFields(Frame.LambdaCaptureFields, |
| 4390 | Frame.LambdaThisCaptureField); |
Richard Smith | 99005e6 | 2013-05-07 03:19:20 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4393 | StmtResult Ret = {Result, ResultSlot}; |
| 4394 | EvalStmtResult ESR = EvaluateStmt(Ret, Info, Body); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 4395 | if (ESR == ESR_Succeeded) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4396 | if (Callee->getReturnType()->isVoidType()) |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 4397 | return true; |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4398 | Info.FFDiag(Callee->getLocEnd(), diag::note_constexpr_no_return); |
Richard Smith | 3da88fa | 2013-04-26 14:36:30 +0000 | [diff] [blame] | 4399 | } |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4400 | return ESR == ESR_Returned; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4401 | } |
| 4402 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4403 | /// Evaluate a constructor call. |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4404 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
| 4405 | APValue *ArgValues, |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4406 | const CXXConstructorDecl *Definition, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4407 | EvalInfo &Info, APValue &Result) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4408 | SourceLocation CallLoc = E->getExprLoc(); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4409 | if (!Info.CheckCallLimit(CallLoc)) |
| 4410 | return false; |
| 4411 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4412 | const CXXRecordDecl *RD = Definition->getParent(); |
| 4413 | if (RD->getNumVBases()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4414 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4415 | return false; |
| 4416 | } |
| 4417 | |
Erik Pilkington | 4292549 | 2017-10-04 00:18:55 +0000 | [diff] [blame] | 4418 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4419 | Info, {This.getLValueBase(), |
| 4420 | {This.getLValueCallIndex(), This.getLValueVersion()}}); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4421 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4422 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4423 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
| 4424 | // wasteful. |
| 4425 | APValue RetVal; |
| 4426 | StmtResult Ret = {RetVal, nullptr}; |
| 4427 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4428 | // If it's a delegating constructor, delegate. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4429 | if (Definition->isDelegatingConstructor()) { |
| 4430 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
Richard Smith | 9ff62af | 2013-11-07 18:45:03 +0000 | [diff] [blame] | 4431 | { |
| 4432 | FullExpressionRAII InitScope(Info); |
| 4433 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit())) |
| 4434 | return false; |
| 4435 | } |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4436 | return EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4439 | // 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] | 4440 | // essential for unions (or classes with anonymous union members), where the |
| 4441 | // operations performed by the constructor cannot be represented by |
| 4442 | // ctor-initializers. |
| 4443 | // |
| 4444 | // Skip this for empty non-union classes; we should not perform an |
| 4445 | // lvalue-to-rvalue conversion on them because their copy constructor does not |
| 4446 | // actually read them. |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 4447 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
Richard Smith | be6dd81 | 2014-11-19 21:27:17 +0000 | [diff] [blame] | 4448 | (Definition->getParent()->isUnion() || |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 4449 | (Definition->isTrivial() && hasFields(Definition->getParent())))) { |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4450 | LValue RHS; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4451 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4452 | return handleLValueToRValueConversion( |
| 4453 | Info, E, Definition->getParamDecl(0)->getType().getNonReferenceType(), |
| 4454 | RHS, Result); |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | // Reserve space for the struct members. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4458 | if (!RD->isUnion() && Result.isUninit()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4459 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 4460 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4461 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4462 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4463 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 4464 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4465 | // A scope for temporaries lifetime-extended by reference members. |
| 4466 | BlockScopeRAII LifetimeExtendedScope(Info); |
| 4467 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4468 | bool Success = true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4469 | unsigned BasesSeen = 0; |
| 4470 | #ifndef NDEBUG |
| 4471 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
| 4472 | #endif |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4473 | for (const auto *I : Definition->inits()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4474 | LValue Subobject = This; |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4475 | LValue SubobjectParent = This; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4476 | APValue *Value = &Result; |
| 4477 | |
| 4478 | // Determine the subobject to initialize. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4479 | FieldDecl *FD = nullptr; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4480 | if (I->isBaseInitializer()) { |
| 4481 | QualType BaseType(I->getBaseClass(), 0); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4482 | #ifndef NDEBUG |
| 4483 | // Non-virtual base classes are initialized in the order in the class |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4484 | // definition. We have already checked for virtual base classes. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4485 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
| 4486 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
| 4487 | "base class initializers not in expected order"); |
| 4488 | ++BaseIt; |
| 4489 | #endif |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4490 | if (!HandleLValueDirectBase(Info, I->getInit(), Subobject, RD, |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4491 | BaseType->getAsCXXRecordDecl(), &Layout)) |
| 4492 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4493 | Value = &Result.getStructBase(BasesSeen++); |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4494 | } else if ((FD = I->getMember())) { |
| 4495 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4496 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4497 | if (RD->isUnion()) { |
| 4498 | Result = APValue(FD); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4499 | Value = &Result.getUnionValue(); |
| 4500 | } else { |
| 4501 | Value = &Result.getStructField(FD->getFieldIndex()); |
| 4502 | } |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4503 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4504 | // Walk the indirect field decl's chain to find the object to initialize, |
| 4505 | // and make sure we've initialized every step along it. |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4506 | auto IndirectFieldChain = IFD->chain(); |
| 4507 | for (auto *C : IndirectFieldChain) { |
Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 4508 | FD = cast<FieldDecl>(C); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4509 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
| 4510 | // Switch the union field if it differs. This happens if we had |
| 4511 | // preceding zero-initialization, and we're now initializing a union |
| 4512 | // subobject other than the first. |
| 4513 | // FIXME: In this case, the values of the other subobjects are |
| 4514 | // specified, since zero-initialization sets all padding bits to zero. |
| 4515 | if (Value->isUninit() || |
| 4516 | (Value->isUnion() && Value->getUnionField() != FD)) { |
| 4517 | if (CD->isUnion()) |
| 4518 | *Value = APValue(FD); |
| 4519 | else |
| 4520 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 4521 | std::distance(CD->field_begin(), CD->field_end())); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4522 | } |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4523 | // Store Subobject as its parent before updating it for the last element |
| 4524 | // in the chain. |
| 4525 | if (C == IndirectFieldChain.back()) |
| 4526 | SubobjectParent = Subobject; |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4527 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 4528 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4529 | if (CD->isUnion()) |
| 4530 | Value = &Value->getUnionValue(); |
| 4531 | else |
| 4532 | Value = &Value->getStructField(FD->getFieldIndex()); |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4533 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4534 | } else { |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 4535 | llvm_unreachable("unknown base initializer kind"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4536 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4537 | |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4538 | // Need to override This for implicit field initializers as in this case |
| 4539 | // This refers to innermost anonymous struct/union containing initializer, |
| 4540 | // not to currently constructed class. |
| 4541 | const Expr *Init = I->getInit(); |
| 4542 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent, |
| 4543 | isa<CXXDefaultInitExpr>(Init)); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4544 | FullExpressionRAII InitScope(Info); |
Volodymyr Sapsai | e8f1ffb | 2018-02-23 23:59:20 +0000 | [diff] [blame] | 4545 | if (!EvaluateInPlace(*Value, Info, Subobject, Init) || |
| 4546 | (FD && FD->isBitField() && |
| 4547 | !truncateBitfieldValue(Info, Init, *Value, FD))) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4548 | // If we're checking for a potential constant expression, evaluate all |
| 4549 | // initializers even if some of them fail. |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 4550 | if (!Info.noteFailure()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 4551 | return false; |
| 4552 | Success = false; |
| 4553 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4554 | } |
| 4555 | |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 4556 | return Success && |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4557 | EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 4560 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
| 4561 | ArrayRef<const Expr*> Args, |
| 4562 | const CXXConstructorDecl *Definition, |
| 4563 | EvalInfo &Info, APValue &Result) { |
| 4564 | ArgVector ArgValues(Args.size()); |
| 4565 | if (!EvaluateArgs(Args, ArgValues, Info)) |
| 4566 | return false; |
| 4567 | |
| 4568 | return HandleConstructorCall(E, This, ArgValues.data(), Definition, |
| 4569 | Info, Result); |
| 4570 | } |
| 4571 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 4572 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4573 | // Generic Evaluation |
| 4574 | //===----------------------------------------------------------------------===// |
| 4575 | namespace { |
| 4576 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4577 | template <class Derived> |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4578 | class ExprEvaluatorBase |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4579 | : public ConstStmtVisitor<Derived, bool> { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4580 | private: |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4581 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4582 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4583 | return getDerived().Success(V, E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4584 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4585 | bool DerivedZeroInitialization(const Expr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4586 | return getDerived().ZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4587 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4588 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4589 | // Check whether a conditional operator with a non-constant condition is a |
| 4590 | // potential constant expression. If neither arm is a potential constant |
| 4591 | // expression, then the conditional operator is not either. |
| 4592 | template<typename ConditionalOperator> |
| 4593 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4594 | assert(Info.checkingPotentialConstantExpression()); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4595 | |
| 4596 | // Speculatively evaluate both arms. |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 4597 | SmallVector<PartialDiagnosticAt, 8> Diag; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4598 | { |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4599 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4600 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 4601 | if (Diag.empty()) |
| 4602 | return; |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 4603 | } |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4604 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 4605 | { |
| 4606 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4607 | Diag.clear(); |
| 4608 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 4609 | if (Diag.empty()) |
| 4610 | return; |
| 4611 | } |
| 4612 | |
| 4613 | Error(E, diag::note_constexpr_conditional_never_const); |
| 4614 | } |
| 4615 | |
| 4616 | |
| 4617 | template<typename ConditionalOperator> |
| 4618 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
| 4619 | bool BoolResult; |
| 4620 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
Nick Lewycky | 20edee6 | 2017-04-27 07:11:09 +0000 | [diff] [blame] | 4621 | if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) { |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4622 | CheckPotentialConstantConditional(E); |
Nick Lewycky | 20edee6 | 2017-04-27 07:11:09 +0000 | [diff] [blame] | 4623 | return false; |
| 4624 | } |
| 4625 | if (Info.noteFailure()) { |
| 4626 | StmtVisitorTy::Visit(E->getTrueExpr()); |
| 4627 | StmtVisitorTy::Visit(E->getFalseExpr()); |
| 4628 | } |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4629 | return false; |
| 4630 | } |
| 4631 | |
| 4632 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
| 4633 | return StmtVisitorTy::Visit(EvalExpr); |
| 4634 | } |
| 4635 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4636 | protected: |
| 4637 | EvalInfo &Info; |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4638 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4639 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
| 4640 | |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 4641 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 4642 | return Info.CCEDiag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4645 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 4646 | |
| 4647 | public: |
| 4648 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
| 4649 | |
| 4650 | EvalInfo &getEvalInfo() { return Info; } |
| 4651 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4652 | /// Report an evaluation error. This should only be called when an error is |
| 4653 | /// first discovered. When propagating an error, just return false. |
| 4654 | bool Error(const Expr *E, diag::kind D) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 4655 | Info.FFDiag(E, D); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4656 | return false; |
| 4657 | } |
| 4658 | bool Error(const Expr *E) { |
| 4659 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
| 4660 | } |
| 4661 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4662 | bool VisitStmt(const Stmt *) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4663 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4664 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4665 | bool VisitExpr(const Expr *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4666 | return Error(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4669 | bool VisitParenExpr(const ParenExpr *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4670 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4671 | bool VisitUnaryExtension(const UnaryOperator *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4672 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4673 | bool VisitUnaryPlus(const UnaryOperator *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4674 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4675 | bool VisitChooseExpr(const ChooseExpr *E) |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 4676 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4677 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4678 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4679 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 4680 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4681 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
| 4682 | TempVersionRAII RAII(*Info.CurrentCall); |
| 4683 | return StmtVisitorTy::Visit(E->getExpr()); |
| 4684 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4685 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4686 | TempVersionRAII RAII(*Info.CurrentCall); |
Richard Smith | 17e3246 | 2013-09-13 20:51:45 +0000 | [diff] [blame] | 4687 | // The initializer may not have been parsed yet, or might be erroneous. |
| 4688 | if (!E->getExpr()) |
| 4689 | return Error(E); |
| 4690 | return StmtVisitorTy::Visit(E->getExpr()); |
| 4691 | } |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 4692 | // We cannot create any objects for which cleanups are required, so there is |
| 4693 | // nothing to do here; all cleanups must come from unevaluated subexpressions. |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4694 | bool VisitExprWithCleanups(const ExprWithCleanups *E) |
Richard Smith | 5894a91 | 2011-12-19 22:12:41 +0000 | [diff] [blame] | 4695 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4696 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4697 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4698 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
| 4699 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 4700 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4701 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 4702 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
| 4703 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
| 4704 | } |
| 4705 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4706 | bool VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4707 | switch (E->getOpcode()) { |
| 4708 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4709 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4710 | |
| 4711 | case BO_Comma: |
| 4712 | VisitIgnoredValue(E->getLHS()); |
| 4713 | return StmtVisitorTy::Visit(E->getRHS()); |
| 4714 | |
| 4715 | case BO_PtrMemD: |
| 4716 | case BO_PtrMemI: { |
| 4717 | LValue Obj; |
| 4718 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
| 4719 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4720 | APValue Result; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4721 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4722 | return false; |
| 4723 | return DerivedSuccess(Result, E); |
| 4724 | } |
| 4725 | } |
| 4726 | } |
| 4727 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4728 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 4729 | // Evaluate and cache the common expression. We treat it as a temporary, |
| 4730 | // even though it's not quite the same thing. |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4731 | if (!Evaluate(Info.CurrentCall->createTemporary(E->getOpaqueValue(), false), |
Richard Smith | 26d4cc1 | 2012-06-26 08:12:11 +0000 | [diff] [blame] | 4732 | Info, E->getCommon())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4733 | return false; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4734 | |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 4735 | return HandleConditionalOperator(E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4736 | } |
| 4737 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4738 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4739 | bool IsBcpCall = false; |
| 4740 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 4741 | // the result is a constant expression if it can be folded without |
| 4742 | // side-effects. This is an important GNU extension. See GCC PR38377 |
| 4743 | // for discussion. |
| 4744 | if (const CallExpr *CallCE = |
| 4745 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 4746 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4747 | IsBcpCall = true; |
| 4748 | |
| 4749 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
| 4750 | // constant expression; we can't check whether it's potentially foldable. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4751 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4752 | return false; |
| 4753 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4754 | FoldConstant Fold(Info, IsBcpCall); |
| 4755 | if (!HandleConditionalOperator(E)) { |
| 4756 | Fold.keepDiagnostics(); |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4757 | return false; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 4758 | } |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 4759 | |
| 4760 | return true; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4763 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 4764 | if (APValue *Value = Info.CurrentCall->getCurrentTemporary(E)) |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4765 | return DerivedSuccess(*Value, E); |
| 4766 | |
| 4767 | const Expr *Source = E->getSourceExpr(); |
| 4768 | if (!Source) |
| 4769 | return Error(E); |
| 4770 | if (Source == E) { // sanity checking. |
| 4771 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
| 4772 | return Error(E); |
Argyrios Kyrtzidis | fac35c0 | 2011-12-09 02:44:48 +0000 | [diff] [blame] | 4773 | } |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 4774 | return StmtVisitorTy::Visit(Source); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 4775 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4776 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4777 | bool VisitCallExpr(const CallExpr *E) { |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4778 | APValue Result; |
| 4779 | if (!handleCallExpr(E, Result, nullptr)) |
| 4780 | return false; |
| 4781 | return DerivedSuccess(Result, E); |
| 4782 | } |
| 4783 | |
| 4784 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4785 | const LValue *ResultSlot) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4786 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4787 | QualType CalleeType = Callee->getType(); |
| 4788 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4789 | const FunctionDecl *FD = nullptr; |
| 4790 | LValue *This = nullptr, ThisVal; |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 4791 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4792 | bool HasQualifier = false; |
Richard Smith | 656d49d | 2011-11-10 09:31:24 +0000 | [diff] [blame] | 4793 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4794 | // Extract function decl and 'this' pointer from the callee. |
| 4795 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4796 | const ValueDecl *Member = nullptr; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4797 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
| 4798 | // Explicit bound member calls, such as x.f() or p->g(); |
| 4799 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4800 | return false; |
| 4801 | Member = ME->getMemberDecl(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4802 | This = &ThisVal; |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4803 | HasQualifier = ME->hasQualifier(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4804 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
| 4805 | // Indirect bound member calls ('.*' or '->*'). |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4806 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
| 4807 | if (!Member) return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4808 | This = &ThisVal; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4809 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4810 | return Error(Callee); |
| 4811 | |
| 4812 | FD = dyn_cast<FunctionDecl>(Member); |
| 4813 | if (!FD) |
| 4814 | return Error(Callee); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4815 | } else if (CalleeType->isFunctionPointerType()) { |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4816 | LValue Call; |
| 4817 | if (!EvaluatePointer(Callee, Call, Info)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4818 | return false; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4819 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4820 | if (!Call.getLValueOffset().isZero()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4821 | return Error(Callee); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 4822 | FD = dyn_cast_or_null<FunctionDecl>( |
| 4823 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4824 | if (!FD) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4825 | return Error(Callee); |
Faisal Vali | d92e749 | 2017-01-08 18:56:11 +0000 | [diff] [blame] | 4826 | // Don't call function pointers which have been cast to some other type. |
| 4827 | // Per DR (no number yet), the caller and callee can differ in noexcept. |
| 4828 | if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec( |
| 4829 | CalleeType->getPointeeType(), FD->getType())) { |
| 4830 | return Error(E); |
| 4831 | } |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4832 | |
| 4833 | // Overloaded operator calls to member functions are represented as normal |
| 4834 | // calls with '*this' as the first argument. |
| 4835 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 4836 | if (MD && !MD->isStatic()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4837 | // FIXME: When selecting an implicit conversion for an overloaded |
| 4838 | // operator delete, we sometimes try to evaluate calls to conversion |
| 4839 | // operators without a 'this' parameter! |
| 4840 | if (Args.empty()) |
| 4841 | return Error(E); |
| 4842 | |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4843 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4844 | return false; |
| 4845 | This = &ThisVal; |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4846 | Args = Args.slice(1); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 4847 | } else if (MD && MD->isLambdaStaticInvoker()) { |
Faisal Vali | d92e749 | 2017-01-08 18:56:11 +0000 | [diff] [blame] | 4848 | // Map the static invoker for the lambda back to the call operator. |
| 4849 | // Conveniently, we don't have to slice out the 'this' argument (as is |
| 4850 | // being done for the non-static case), since a static member function |
| 4851 | // doesn't have an implicit argument passed in. |
| 4852 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
| 4853 | assert( |
| 4854 | ClosureClass->captures_begin() == ClosureClass->captures_end() && |
| 4855 | "Number of captures must be zero for conversion to function-ptr"); |
| 4856 | |
| 4857 | const CXXMethodDecl *LambdaCallOp = |
| 4858 | ClosureClass->getLambdaCallOperator(); |
| 4859 | |
| 4860 | // Set 'FD', the function that will be called below, to the call |
| 4861 | // operator. If the closure object represents a generic lambda, find |
| 4862 | // the corresponding specialization of the call operator. |
| 4863 | |
| 4864 | if (ClosureClass->isGenericLambda()) { |
| 4865 | assert(MD->isFunctionTemplateSpecialization() && |
| 4866 | "A generic lambda's static-invoker function must be a " |
| 4867 | "template specialization"); |
| 4868 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
| 4869 | FunctionTemplateDecl *CallOpTemplate = |
| 4870 | LambdaCallOp->getDescribedFunctionTemplate(); |
| 4871 | void *InsertPos = nullptr; |
| 4872 | FunctionDecl *CorrespondingCallOpSpecialization = |
| 4873 | CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); |
| 4874 | assert(CorrespondingCallOpSpecialization && |
| 4875 | "We must always have a function call operator specialization " |
| 4876 | "that corresponds to our static invoker specialization"); |
| 4877 | FD = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
| 4878 | } else |
| 4879 | FD = LambdaCallOp; |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4880 | } |
| 4881 | |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 4882 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 4883 | } else |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4884 | return Error(E); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4885 | |
Richard Smith | 47b3493 | 2012-02-01 02:39:43 +0000 | [diff] [blame] | 4886 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
| 4887 | return false; |
| 4888 | |
Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4889 | // DR1358 allows virtual constexpr functions in some cases. Don't allow |
| 4890 | // calls to such functions in constant expressions. |
| 4891 | if (This && !HasQualifier && |
| 4892 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
| 4893 | return Error(E, diag::note_constexpr_virtual_call); |
| 4894 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4895 | const FunctionDecl *Definition = nullptr; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4896 | Stmt *Body = FD->getBody(Definition); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4897 | |
Nick Lewycky | 13073a6 | 2017-06-12 21:15:44 +0000 | [diff] [blame] | 4898 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body) || |
| 4899 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, Info, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4900 | Result, ResultSlot)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4901 | return false; |
| 4902 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 4903 | return true; |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4906 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4907 | return StmtVisitorTy::Visit(E->getInitializer()); |
| 4908 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4909 | bool VisitInitListExpr(const InitListExpr *E) { |
Eli Friedman | 90dc175 | 2012-01-03 23:54:05 +0000 | [diff] [blame] | 4910 | if (E->getNumInits() == 0) |
| 4911 | return DerivedZeroInitialization(E); |
| 4912 | if (E->getNumInits() == 1) |
| 4913 | return StmtVisitorTy::Visit(E->getInit(0)); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4914 | return Error(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4915 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4916 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4917 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4918 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4919 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4920 | return DerivedZeroInitialization(E); |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4921 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4922 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 4923 | return DerivedZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 4924 | } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 4925 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4926 | /// 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] | 4927 | bool VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4928 | assert(!E->isArrow() && "missing call to bound member function?"); |
| 4929 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4930 | APValue Val; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4931 | if (!Evaluate(Val, Info, E->getBase())) |
| 4932 | return false; |
| 4933 | |
| 4934 | QualType BaseTy = E->getBase()->getType(); |
| 4935 | |
| 4936 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4937 | if (!FD) return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4938 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 4939 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4940 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 4941 | |
Richard Smith | 9defb7d | 2018-02-21 03:38:30 +0000 | [diff] [blame] | 4942 | CompleteObject Obj(&Val, BaseTy, true); |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 4943 | SubobjectDesignator Designator(BaseTy); |
| 4944 | Designator.addDeclUnchecked(FD); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4945 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 4946 | APValue Result; |
| 4947 | return extractSubobject(Info, E, Obj, Designator, Result) && |
| 4948 | DerivedSuccess(Result, E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4949 | } |
| 4950 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4951 | bool VisitCastExpr(const CastExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4952 | switch (E->getCastKind()) { |
| 4953 | default: |
| 4954 | break; |
| 4955 | |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4956 | case CK_AtomicToNonAtomic: { |
| 4957 | APValue AtomicVal; |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 4958 | // This does not need to be done in place even for class/array types: |
| 4959 | // atomic-to-non-atomic conversion implies copying the object |
| 4960 | // representation. |
| 4961 | if (!Evaluate(AtomicVal, Info, E->getSubExpr())) |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 4962 | return false; |
| 4963 | return DerivedSuccess(AtomicVal, E); |
| 4964 | } |
| 4965 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4966 | case CK_NoOp: |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 4967 | case CK_UserDefinedConversion: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4968 | return StmtVisitorTy::Visit(E->getSubExpr()); |
| 4969 | |
| 4970 | case CK_LValueToRValue: { |
| 4971 | LValue LVal; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4972 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
| 4973 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 4974 | APValue RVal; |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 4975 | // 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] | 4976 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
Richard Smith | c82fae6 | 2012-02-05 01:23:16 +0000 | [diff] [blame] | 4977 | LVal, RVal)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4978 | return false; |
| 4979 | return DerivedSuccess(RVal, E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4980 | } |
| 4981 | } |
| 4982 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 4983 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4986 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4987 | return VisitUnaryPostIncDec(UO); |
| 4988 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4989 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4990 | return VisitUnaryPostIncDec(UO); |
| 4991 | } |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 4992 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 4993 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 4994 | return Error(UO); |
| 4995 | |
| 4996 | LValue LVal; |
| 4997 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
| 4998 | return false; |
| 4999 | APValue RVal; |
| 5000 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
| 5001 | UO->isIncrementOp(), &RVal)) |
| 5002 | return false; |
| 5003 | return DerivedSuccess(RVal, UO); |
| 5004 | } |
| 5005 | |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5006 | bool VisitStmtExpr(const StmtExpr *E) { |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5007 | // We will have checked the full-expressions inside the statement expression |
| 5008 | // 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] | 5009 | if (Info.checkingForOverflow()) |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5010 | return Error(E); |
| 5011 | |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5012 | BlockScopeRAII Scope(Info); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5013 | const CompoundStmt *CS = E->getSubStmt(); |
Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 5014 | if (CS->body_empty()) |
| 5015 | return true; |
| 5016 | |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5017 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
| 5018 | BE = CS->body_end(); |
| 5019 | /**/; ++BI) { |
| 5020 | if (BI + 1 == BE) { |
| 5021 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
| 5022 | if (!FinalExpr) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5023 | Info.FFDiag((*BI)->getLocStart(), |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5024 | diag::note_constexpr_stmt_expr_unsupported); |
| 5025 | return false; |
| 5026 | } |
| 5027 | return this->Visit(FinalExpr); |
| 5028 | } |
| 5029 | |
| 5030 | APValue ReturnValue; |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 5031 | StmtResult Result = { ReturnValue, nullptr }; |
| 5032 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5033 | if (ESR != ESR_Succeeded) { |
| 5034 | // FIXME: If the statement-expression terminated due to 'return', |
| 5035 | // 'break', or 'continue', it would be nice to propagate that to |
| 5036 | // the outer statement evaluation rather than bailing out. |
| 5037 | if (ESR != ESR_Failed) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5038 | Info.FFDiag((*BI)->getLocStart(), |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5039 | diag::note_constexpr_stmt_expr_unsupported); |
| 5040 | return false; |
| 5041 | } |
| 5042 | } |
Jonathan Roelofs | 104cbf9 | 2015-06-01 16:23:08 +0000 | [diff] [blame] | 5043 | |
| 5044 | llvm_unreachable("Return from function from the loop above."); |
Richard Smith | 51f0317 | 2013-06-20 03:00:05 +0000 | [diff] [blame] | 5045 | } |
| 5046 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5047 | /// Visit a value which is evaluated, but whose value is ignored. |
| 5048 | void VisitIgnoredValue(const Expr *E) { |
Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 5049 | EvaluateIgnoredValue(Info, E); |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 5050 | } |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 5051 | |
| 5052 | /// Potentially visit a MemberExpr's base expression. |
| 5053 | void VisitIgnoredBaseExpression(const Expr *E) { |
| 5054 | // While MSVC doesn't evaluate the base expression, it does diagnose the |
| 5055 | // presence of side-effecting behavior. |
| 5056 | if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Info.Ctx)) |
| 5057 | return; |
| 5058 | VisitIgnoredValue(E); |
| 5059 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5060 | }; |
| 5061 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5062 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5063 | |
| 5064 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5065 | // Common base class for lvalue and temporary evaluation. |
| 5066 | //===----------------------------------------------------------------------===// |
| 5067 | namespace { |
| 5068 | template<class Derived> |
| 5069 | class LValueExprEvaluatorBase |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5070 | : public ExprEvaluatorBase<Derived> { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5071 | protected: |
| 5072 | LValue &Result; |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5073 | bool InvalidBaseOK; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5074 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5075 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5076 | |
| 5077 | bool Success(APValue::LValueBase B) { |
| 5078 | Result.set(B); |
| 5079 | return true; |
| 5080 | } |
| 5081 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5082 | bool evaluatePointer(const Expr *E, LValue &Result) { |
| 5083 | return EvaluatePointer(E, Result, this->Info, InvalidBaseOK); |
| 5084 | } |
| 5085 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5086 | public: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5087 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) |
| 5088 | : ExprEvaluatorBaseTy(Info), Result(Result), |
| 5089 | InvalidBaseOK(InvalidBaseOK) {} |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5090 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5091 | bool Success(const APValue &V, const Expr *E) { |
| 5092 | Result.setFrom(this->Info.Ctx, V); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5093 | return true; |
| 5094 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5095 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5096 | bool VisitMemberExpr(const MemberExpr *E) { |
| 5097 | // Handle non-static data members. |
| 5098 | QualType BaseTy; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5099 | bool EvalOK; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5100 | if (E->isArrow()) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5101 | EvalOK = evaluatePointer(E->getBase(), Result); |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5102 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5103 | } else if (E->getBase()->isRValue()) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 5104 | assert(E->getBase()->getType()->isRecordType()); |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5105 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 5106 | BaseTy = E->getBase()->getType(); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5107 | } else { |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5108 | EvalOK = this->Visit(E->getBase()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5109 | BaseTy = E->getBase()->getType(); |
| 5110 | } |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5111 | if (!EvalOK) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5112 | if (!InvalidBaseOK) |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5113 | return false; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 5114 | Result.setInvalid(E); |
| 5115 | return true; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5116 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5117 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5118 | const ValueDecl *MD = E->getMemberDecl(); |
| 5119 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
| 5120 | assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
| 5121 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
| 5122 | (void)BaseTy; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5123 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
| 5124 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5125 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 5126 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
| 5127 | return false; |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5128 | } else |
| 5129 | return this->Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5130 | |
Richard Smith | 1b78b3d | 2012-01-25 22:15:11 +0000 | [diff] [blame] | 5131 | if (MD->getType()->isReferenceType()) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5132 | APValue RefValue; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5133 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5134 | RefValue)) |
| 5135 | return false; |
| 5136 | return Success(RefValue, E); |
| 5137 | } |
| 5138 | return true; |
| 5139 | } |
| 5140 | |
| 5141 | bool VisitBinaryOperator(const BinaryOperator *E) { |
| 5142 | switch (E->getOpcode()) { |
| 5143 | default: |
| 5144 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 5145 | |
| 5146 | case BO_PtrMemD: |
| 5147 | case BO_PtrMemI: |
| 5148 | return HandleMemberPointerAccess(this->Info, E, Result); |
| 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | bool VisitCastExpr(const CastExpr *E) { |
| 5153 | switch (E->getCastKind()) { |
| 5154 | default: |
| 5155 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 5156 | |
| 5157 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5158 | case CK_UncheckedDerivedToBase: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5159 | if (!this->Visit(E->getSubExpr())) |
| 5160 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5161 | |
| 5162 | // Now figure out the necessary offset to add to the base LV to get from |
| 5163 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5164 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
| 5165 | Result); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5166 | } |
| 5167 | } |
| 5168 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5169 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5170 | |
| 5171 | //===----------------------------------------------------------------------===// |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5172 | // LValue Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5173 | // |
| 5174 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
| 5175 | // function designators (in C), decl references to void objects (in C), and |
| 5176 | // temporaries (if building with -Wno-address-of-temporary). |
| 5177 | // |
| 5178 | // LValue evaluation produces values comprising a base expression of one of the |
| 5179 | // following types: |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5180 | // - Declarations |
| 5181 | // * VarDecl |
| 5182 | // * FunctionDecl |
| 5183 | // - Literals |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 5184 | // * CompoundLiteralExpr in C (and in global scope in C++) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5185 | // * StringLiteral |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5186 | // * CXXTypeidExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5187 | // * PredefinedExpr |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5188 | // * ObjCStringLiteralExpr |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5189 | // * ObjCEncodeExpr |
| 5190 | // * AddrLabelExpr |
| 5191 | // * BlockExpr |
| 5192 | // * CallExpr for a MakeStringConstant builtin |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5193 | // - Locals and temporaries |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5194 | // * MaterializeTemporaryExpr |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 5195 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5196 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
| 5197 | // from the AST (FIXME). |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5198 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
| 5199 | // CallIndex, for a lifetime-extended temporary. |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5200 | // plus an offset in bytes. |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5201 | //===----------------------------------------------------------------------===// |
| 5202 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5203 | class LValueExprEvaluator |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5204 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5205 | public: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5206 | LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) : |
| 5207 | LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5208 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5209 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5210 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5211 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5212 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
| 5213 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 5214 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5215 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
| 5216 | bool VisitMemberExpr(const MemberExpr *E); |
| 5217 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
| 5218 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5219 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 5220 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5221 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
| 5222 | bool VisitUnaryDeref(const UnaryOperator *E); |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 5223 | bool VisitUnaryReal(const UnaryOperator *E); |
| 5224 | bool VisitUnaryImag(const UnaryOperator *E); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5225 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
| 5226 | return VisitUnaryPreIncDec(UO); |
| 5227 | } |
| 5228 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
| 5229 | return VisitUnaryPreIncDec(UO); |
| 5230 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5231 | bool VisitBinAssign(const BinaryOperator *BO); |
| 5232 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5233 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5234 | bool VisitCastExpr(const CastExpr *E) { |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5235 | switch (E->getCastKind()) { |
| 5236 | default: |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5237 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5238 | |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 5239 | case CK_LValueBitCast: |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5240 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5241 | if (!Visit(E->getSubExpr())) |
| 5242 | return false; |
| 5243 | Result.Designator.setInvalid(); |
| 5244 | return true; |
Eli Friedman | ce3e02a | 2011-10-11 00:13:24 +0000 | [diff] [blame] | 5245 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5246 | case CK_BaseToDerived: |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5247 | if (!Visit(E->getSubExpr())) |
| 5248 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5249 | return HandleBaseToDerivedCast(Info, E, Result); |
Anders Carlsson | de55f64 | 2009-10-03 16:30:22 +0000 | [diff] [blame] | 5250 | } |
| 5251 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5252 | }; |
| 5253 | } // end anonymous namespace |
| 5254 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5255 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 5256 | /// expressions which are not glvalues, in three cases: |
Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 5257 | /// * function designators in C, and |
| 5258 | /// * "extern void" objects |
Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 5259 | /// * @selector() expressions in Objective-C |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5260 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
| 5261 | bool InvalidBaseOK) { |
Richard Smith | 9f8400e | 2013-05-01 19:00:39 +0000 | [diff] [blame] | 5262 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
Nico Weber | 9677562 | 2015-09-15 23:17:17 +0000 | [diff] [blame] | 5263 | E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E)); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5264 | return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5265 | } |
| 5266 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5267 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
David Majnemer | 0c43d80 | 2014-06-25 08:15:07 +0000 | [diff] [blame] | 5268 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5269 | return Success(FD); |
| 5270 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5271 | return VisitVarDecl(E, VD); |
Richard Smith | dca60b4 | 2016-08-12 00:39:32 +0000 | [diff] [blame] | 5272 | if (const BindingDecl *BD = dyn_cast<BindingDecl>(E->getDecl())) |
Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 5273 | return Visit(BD->getBinding()); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5274 | return Error(E); |
| 5275 | } |
Richard Smith | 733237d | 2011-10-24 23:14:33 +0000 | [diff] [blame] | 5276 | |
Faisal Vali | 0528a31 | 2016-11-13 06:09:16 +0000 | [diff] [blame] | 5277 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5278 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5279 | |
| 5280 | // If we are within a lambda's call operator, check whether the 'VD' referred |
| 5281 | // to within 'E' actually represents a lambda-capture that maps to a |
| 5282 | // data-member/field within the closure object, and if so, evaluate to the |
| 5283 | // field or what the field refers to. |
Erik Pilkington | 1123291 | 2018-04-05 00:12:05 +0000 | [diff] [blame] | 5284 | if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) && |
| 5285 | isa<DeclRefExpr>(E) && |
| 5286 | cast<DeclRefExpr>(E)->refersToEnclosingVariableOrCapture()) { |
| 5287 | // We don't always have a complete capture-map when checking or inferring if |
| 5288 | // the function call operator meets the requirements of a constexpr function |
| 5289 | // - but we don't need to evaluate the captures to determine constexprness |
| 5290 | // (dcl.constexpr C++17). |
| 5291 | if (Info.checkingPotentialConstantExpression()) |
| 5292 | return false; |
| 5293 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5294 | if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) { |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5295 | // Start with 'Result' referring to the complete closure object... |
| 5296 | Result = *Info.CurrentCall->This; |
| 5297 | // ... then update it to refer to the field of the closure object |
| 5298 | // that represents the capture. |
| 5299 | if (!HandleLValueMember(Info, E, Result, FD)) |
| 5300 | return false; |
| 5301 | // And if the field is of reference type, update 'Result' to refer to what |
| 5302 | // the field refers to. |
| 5303 | if (FD->getType()->isReferenceType()) { |
| 5304 | APValue RVal; |
| 5305 | if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, |
| 5306 | RVal)) |
| 5307 | return false; |
| 5308 | Result.setFrom(Info.Ctx, RVal); |
| 5309 | } |
| 5310 | return true; |
| 5311 | } |
| 5312 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5313 | CallStackFrame *Frame = nullptr; |
Faisal Vali | 0528a31 | 2016-11-13 06:09:16 +0000 | [diff] [blame] | 5314 | if (VD->hasLocalStorage() && Info.CurrentCall->Index > 1) { |
| 5315 | // Only if a local variable was declared in the function currently being |
| 5316 | // evaluated, do we expect to be able to find its value in the current |
| 5317 | // frame. (Otherwise it was likely declared in an enclosing context and |
| 5318 | // could either have a valid evaluatable value (for e.g. a constexpr |
| 5319 | // variable) or be ill-formed (and trigger an appropriate evaluation |
| 5320 | // diagnostic)). |
| 5321 | if (Info.CurrentCall->Callee && |
| 5322 | Info.CurrentCall->Callee->Equals(VD->getDeclContext())) { |
| 5323 | Frame = Info.CurrentCall; |
| 5324 | } |
| 5325 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5326 | |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 5327 | if (!VD->getType()->isReferenceType()) { |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5328 | if (Frame) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5329 | Result.set({VD, Frame->Index, |
| 5330 | Info.CurrentCall->getCurrentTemporaryVersion(VD)}); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 5331 | return true; |
| 5332 | } |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5333 | return Success(VD); |
Richard Smith | fec0992 | 2011-11-01 16:57:24 +0000 | [diff] [blame] | 5334 | } |
Eli Friedman | 751aa72b7 | 2009-05-27 06:04:58 +0000 | [diff] [blame] | 5335 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5336 | APValue *V; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5337 | if (!evaluateVarDeclInit(Info, E, VD, Frame, V, nullptr)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5338 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5339 | if (V->isUninit()) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 5340 | if (!Info.checkingPotentialConstantExpression()) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5341 | Info.FFDiag(E, diag::note_constexpr_use_uninit_reference); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 5342 | return false; |
| 5343 | } |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5344 | return Success(*V, E); |
Anders Carlsson | a42ee44 | 2008-11-24 04:41:22 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 5347 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
| 5348 | const MaterializeTemporaryExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5349 | // Walk through the expression to find the materialized temporary itself. |
| 5350 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5351 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5352 | const Expr *Inner = E->GetTemporaryExpr()-> |
| 5353 | skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5354 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5355 | // If we passed any comma operators, evaluate their LHSs. |
| 5356 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
| 5357 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
| 5358 | return false; |
| 5359 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5360 | // A materialized temporary with static storage duration can appear within the |
| 5361 | // result of a constant expression evaluation, so we need to preserve its |
| 5362 | // value for use outside this evaluation. |
| 5363 | APValue *Value; |
| 5364 | if (E->getStorageDuration() == SD_Static) { |
| 5365 | Value = Info.Ctx.getMaterializedTemporaryValue(E, true); |
Richard Smith | a509f2f | 2013-06-14 03:07:01 +0000 | [diff] [blame] | 5366 | *Value = APValue(); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5367 | Result.set(E); |
| 5368 | } else { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5369 | Value = &createTemporary(E, E->getStorageDuration() == SD_Automatic, Result, |
| 5370 | *Info.CurrentCall); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5371 | } |
| 5372 | |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 5373 | QualType Type = Inner->getType(); |
| 5374 | |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5375 | // Materialize the temporary itself. |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 5376 | if (!EvaluateInPlace(*Value, Info, Result, Inner) || |
| 5377 | (E->getStorageDuration() == SD_Static && |
| 5378 | !CheckConstantExpression(Info, E->getExprLoc(), Type, *Value))) { |
| 5379 | *Value = APValue(); |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5380 | return false; |
Richard Smith | ea4ad5d | 2013-06-06 08:19:16 +0000 | [diff] [blame] | 5381 | } |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5382 | |
| 5383 | // Adjust our lvalue to refer to the desired subobject. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5384 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
| 5385 | --I; |
| 5386 | switch (Adjustments[I].Kind) { |
| 5387 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
| 5388 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
| 5389 | Type, Result)) |
| 5390 | return false; |
| 5391 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
| 5392 | break; |
| 5393 | |
| 5394 | case SubobjectAdjustment::FieldAdjustment: |
| 5395 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
| 5396 | return false; |
| 5397 | Type = Adjustments[I].Field->getType(); |
| 5398 | break; |
| 5399 | |
| 5400 | case SubobjectAdjustment::MemberPointerAdjustment: |
| 5401 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
| 5402 | Adjustments[I].Ptr.RHS)) |
| 5403 | return false; |
| 5404 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
| 5405 | break; |
| 5406 | } |
| 5407 | } |
| 5408 | |
| 5409 | return true; |
Richard Smith | 4e4c78ff | 2011-10-31 05:52:43 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5412 | bool |
| 5413 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
Richard Smith | b3189a1 | 2016-12-05 07:49:14 +0000 | [diff] [blame] | 5414 | assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) && |
| 5415 | "lvalue compound literal in c++?"); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5416 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
| 5417 | // 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] | 5418 | return Success(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5419 | } |
| 5420 | |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5421 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 5422 | if (!E->isPotentiallyEvaluated()) |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5423 | return Success(E); |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 5424 | |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5425 | Info.FFDiag(E, diag::note_constexpr_typeid_polymorphic) |
Richard Smith | 6f3d435 | 2012-10-17 23:52:07 +0000 | [diff] [blame] | 5426 | << E->getExprOperand()->getType() |
| 5427 | << E->getExprOperand()->getSourceRange(); |
| 5428 | return false; |
Richard Smith | 6e52514 | 2011-12-27 12:18:28 +0000 | [diff] [blame] | 5429 | } |
| 5430 | |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 5431 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
| 5432 | return Success(E); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5433 | } |
Francois Pichet | 0066db9 | 2012-04-16 04:08:35 +0000 | [diff] [blame] | 5434 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5435 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5436 | // Handle static data members. |
| 5437 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 5438 | VisitIgnoredBaseExpression(E->getBase()); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5439 | return VisitVarDecl(E, VD); |
| 5440 | } |
| 5441 | |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 5442 | // Handle static member functions. |
| 5443 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
| 5444 | if (MD->isStatic()) { |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 5445 | VisitIgnoredBaseExpression(E->getBase()); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5446 | return Success(MD); |
Richard Smith | 254a73d | 2011-10-28 22:34:42 +0000 | [diff] [blame] | 5447 | } |
| 5448 | } |
| 5449 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5450 | // Handle non-static data members. |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5451 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5454 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5455 | // FIXME: Deal with vectors as array subscript bases. |
| 5456 | if (E->getBase()->getType()->isVectorType()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5457 | return Error(E); |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5458 | |
Nick Lewycky | ad88868 | 2017-04-27 07:27:36 +0000 | [diff] [blame] | 5459 | bool Success = true; |
| 5460 | if (!evaluatePointer(E->getBase(), Result)) { |
| 5461 | if (!Info.noteFailure()) |
| 5462 | return false; |
| 5463 | Success = false; |
| 5464 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5465 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5466 | APSInt Index; |
| 5467 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5468 | return false; |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5469 | |
Nick Lewycky | ad88868 | 2017-04-27 07:27:36 +0000 | [diff] [blame] | 5470 | return Success && |
| 5471 | HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 5472 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5473 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5474 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5475 | return evaluatePointer(E->getSubExpr(), Result); |
Eli Friedman | 0b8337c | 2009-02-20 01:57:15 +0000 | [diff] [blame] | 5476 | } |
| 5477 | |
Richard Smith | 66c9699 | 2012-02-18 22:04:06 +0000 | [diff] [blame] | 5478 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 5479 | if (!Visit(E->getSubExpr())) |
| 5480 | return false; |
| 5481 | // __real is a no-op on scalar lvalues. |
| 5482 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
| 5483 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
| 5484 | return true; |
| 5485 | } |
| 5486 | |
| 5487 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
| 5488 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
| 5489 | "lvalue __imag__ on scalar?"); |
| 5490 | if (!Visit(E->getSubExpr())) |
| 5491 | return false; |
| 5492 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
| 5493 | return true; |
| 5494 | } |
| 5495 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5496 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5497 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5498 | return Error(UO); |
| 5499 | |
| 5500 | if (!this->Visit(UO->getSubExpr())) |
| 5501 | return false; |
| 5502 | |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5503 | return handleIncDec( |
| 5504 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5505 | UO->isIncrementOp(), nullptr); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
| 5509 | const CompoundAssignOperator *CAO) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5510 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5511 | return Error(CAO); |
| 5512 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5513 | APValue RHS; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5514 | |
| 5515 | // The overall lvalue result is the result of evaluating the LHS. |
| 5516 | if (!this->Visit(CAO->getLHS())) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5517 | if (Info.noteFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5518 | Evaluate(RHS, this->Info, CAO->getRHS()); |
| 5519 | return false; |
| 5520 | } |
| 5521 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5522 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) |
| 5523 | return false; |
| 5524 | |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 5525 | return handleCompoundAssignment( |
| 5526 | this->Info, CAO, |
| 5527 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
| 5528 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 5532 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5533 | return Error(E); |
| 5534 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5535 | APValue NewVal; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5536 | |
| 5537 | if (!this->Visit(E->getLHS())) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5538 | if (Info.noteFailure()) |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5539 | Evaluate(NewVal, this->Info, E->getRHS()); |
| 5540 | return false; |
| 5541 | } |
| 5542 | |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5543 | if (!Evaluate(NewVal, this->Info, E->getRHS())) |
| 5544 | return false; |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 5545 | |
| 5546 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
Richard Smith | 3229b74 | 2013-05-05 21:17:10 +0000 | [diff] [blame] | 5547 | NewVal); |
| 5548 | } |
| 5549 | |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5550 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5551 | // Pointer Evaluation |
| 5552 | //===----------------------------------------------------------------------===// |
| 5553 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5554 | /// \brief Attempts to compute the number of bytes available at the pointer |
| 5555 | /// returned by a function with the alloc_size attribute. Returns true if we |
| 5556 | /// were successful. Places an unsigned number into `Result`. |
| 5557 | /// |
| 5558 | /// This expects the given CallExpr to be a call to a function with an |
| 5559 | /// alloc_size attribute. |
| 5560 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
| 5561 | const CallExpr *Call, |
| 5562 | llvm::APInt &Result) { |
| 5563 | const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call); |
| 5564 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 5565 | assert(AllocSize && AllocSize->getElemSizeParam().isValid()); |
| 5566 | unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5567 | unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType()); |
| 5568 | if (Call->getNumArgs() <= SizeArgNo) |
| 5569 | return false; |
| 5570 | |
| 5571 | auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) { |
| 5572 | if (!E->EvaluateAsInt(Into, Ctx, Expr::SE_AllowSideEffects)) |
| 5573 | return false; |
| 5574 | if (Into.isNegative() || !Into.isIntN(BitsInSizeT)) |
| 5575 | return false; |
| 5576 | Into = Into.zextOrSelf(BitsInSizeT); |
| 5577 | return true; |
| 5578 | }; |
| 5579 | |
| 5580 | APSInt SizeOfElem; |
| 5581 | if (!EvaluateAsSizeT(Call->getArg(SizeArgNo), SizeOfElem)) |
| 5582 | return false; |
| 5583 | |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 5584 | if (!AllocSize->getNumElemsParam().isValid()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5585 | Result = std::move(SizeOfElem); |
| 5586 | return true; |
| 5587 | } |
| 5588 | |
| 5589 | APSInt NumberOfElems; |
Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 5590 | unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5591 | if (!EvaluateAsSizeT(Call->getArg(NumArgNo), NumberOfElems)) |
| 5592 | return false; |
| 5593 | |
| 5594 | bool Overflow; |
| 5595 | llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow); |
| 5596 | if (Overflow) |
| 5597 | return false; |
| 5598 | |
| 5599 | Result = std::move(BytesAvailable); |
| 5600 | return true; |
| 5601 | } |
| 5602 | |
| 5603 | /// \brief Convenience function. LVal's base must be a call to an alloc_size |
| 5604 | /// function. |
| 5605 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
| 5606 | const LValue &LVal, |
| 5607 | llvm::APInt &Result) { |
| 5608 | assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 5609 | "Can't get the size of a non alloc_size function"); |
| 5610 | const auto *Base = LVal.getLValueBase().get<const Expr *>(); |
| 5611 | const CallExpr *CE = tryUnwrapAllocSizeCall(Base); |
| 5612 | return getBytesReturnedByAllocSizeCall(Ctx, CE, Result); |
| 5613 | } |
| 5614 | |
| 5615 | /// \brief Attempts to evaluate the given LValueBase as the result of a call to |
| 5616 | /// a function with the alloc_size attribute. If it was possible to do so, this |
| 5617 | /// function will return true, make Result's Base point to said function call, |
| 5618 | /// and mark Result's Base as invalid. |
| 5619 | static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base, |
| 5620 | LValue &Result) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5621 | if (Base.isNull()) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5622 | return false; |
| 5623 | |
| 5624 | // Because we do no form of static analysis, we only support const variables. |
| 5625 | // |
| 5626 | // Additionally, we can't support parameters, nor can we support static |
| 5627 | // variables (in the latter case, use-before-assign isn't UB; in the former, |
| 5628 | // we have no clue what they'll be assigned to). |
| 5629 | const auto *VD = |
| 5630 | dyn_cast_or_null<VarDecl>(Base.dyn_cast<const ValueDecl *>()); |
| 5631 | if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified()) |
| 5632 | return false; |
| 5633 | |
| 5634 | const Expr *Init = VD->getAnyInitializer(); |
| 5635 | if (!Init) |
| 5636 | return false; |
| 5637 | |
| 5638 | const Expr *E = Init->IgnoreParens(); |
| 5639 | if (!tryUnwrapAllocSizeCall(E)) |
| 5640 | return false; |
| 5641 | |
| 5642 | // Store E instead of E unwrapped so that the type of the LValue's base is |
| 5643 | // what the user wanted. |
| 5644 | Result.setInvalid(E); |
| 5645 | |
| 5646 | QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5647 | Result.addUnsizedArray(Info, E, Pointee); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5648 | return true; |
| 5649 | } |
| 5650 | |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 5651 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 5652 | class PointerExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 5653 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5654 | LValue &Result; |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5655 | bool InvalidBaseOK; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5656 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5657 | bool Success(const Expr *E) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 5658 | Result.set(E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5659 | return true; |
| 5660 | } |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5661 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5662 | bool evaluateLValue(const Expr *E, LValue &Result) { |
| 5663 | return EvaluateLValue(E, Result, Info, InvalidBaseOK); |
| 5664 | } |
| 5665 | |
| 5666 | bool evaluatePointer(const Expr *E, LValue &Result) { |
| 5667 | return EvaluatePointer(E, Result, Info, InvalidBaseOK); |
| 5668 | } |
| 5669 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5670 | bool visitNonBuiltinCallExpr(const CallExpr *E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5671 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5672 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5673 | PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK) |
| 5674 | : ExprEvaluatorBaseTy(info), Result(Result), |
| 5675 | InvalidBaseOK(InvalidBaseOK) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5676 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5677 | bool Success(const APValue &V, const Expr *E) { |
| 5678 | Result.setFrom(Info.Ctx, V); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5679 | return true; |
| 5680 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5681 | bool ZeroInitialization(const Expr *E) { |
Tim Northover | 0150333 | 2017-05-26 02:16:00 +0000 | [diff] [blame] | 5682 | auto TargetVal = Info.Ctx.getTargetNullPointerValue(E->getType()); |
| 5683 | Result.setNull(E->getType(), TargetVal); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 5684 | return true; |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 5685 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 5686 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5687 | bool VisitBinaryOperator(const BinaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5688 | bool VisitCastExpr(const CastExpr* E); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5689 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5690 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5691 | { return Success(E); } |
Nick Lewycky | 19ae6dc | 2017-04-29 00:07:27 +0000 | [diff] [blame] | 5692 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
| 5693 | if (Info.noteFailure()) |
| 5694 | EvaluateIgnoredValue(Info, E->getSubExpr()); |
| 5695 | return Error(E); |
| 5696 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5697 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5698 | { return Success(E); } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5699 | bool VisitCallExpr(const CallExpr *E); |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 5700 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5701 | bool VisitBlockExpr(const BlockExpr *E) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 5702 | if (!E->getBlockDecl()->hasCaptures()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5703 | return Success(E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 5704 | return Error(E); |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 5705 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5706 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5707 | // Can't look at 'this' when checking a potential constant expression. |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 5708 | if (Info.checkingPotentialConstantExpression()) |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5709 | return false; |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 5710 | if (!Info.CurrentCall->This) { |
| 5711 | if (Info.getLangOpts().CPlusPlus11) |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5712 | Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit(); |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 5713 | else |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 5714 | Info.FFDiag(E); |
Richard Smith | 22a5d61 | 2014-07-07 06:00:13 +0000 | [diff] [blame] | 5715 | return false; |
| 5716 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5717 | Result = *Info.CurrentCall->This; |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5718 | // If we are inside a lambda's call operator, the 'this' expression refers |
| 5719 | // to the enclosing '*this' object (either by value or reference) which is |
| 5720 | // either copied into the closure object's field that represents the '*this' |
| 5721 | // or refers to '*this'. |
| 5722 | if (isLambdaCallOperator(Info.CurrentCall->Callee)) { |
| 5723 | // Update 'Result' to refer to the data member/field of the closure object |
| 5724 | // that represents the '*this' capture. |
| 5725 | if (!HandleLValueMember(Info, E, Result, |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 5726 | Info.CurrentCall->LambdaThisCaptureField)) |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 5727 | return false; |
| 5728 | // If we captured '*this' by reference, replace the field with its referent. |
| 5729 | if (Info.CurrentCall->LambdaThisCaptureField->getType() |
| 5730 | ->isPointerType()) { |
| 5731 | APValue RVal; |
| 5732 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Result, |
| 5733 | RVal)) |
| 5734 | return false; |
| 5735 | |
| 5736 | Result.setFrom(Info.Ctx, RVal); |
| 5737 | } |
| 5738 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5739 | return true; |
| 5740 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5741 | |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 5742 | // FIXME: Missing: @protocol, @selector |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5743 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5744 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 5745 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5746 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info, |
| 5747 | bool InvalidBaseOK) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5748 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5749 | return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5750 | } |
| 5751 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5752 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5753 | if (E->getOpcode() != BO_Add && |
| 5754 | E->getOpcode() != BO_Sub) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5755 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5756 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5757 | const Expr *PExp = E->getLHS(); |
| 5758 | const Expr *IExp = E->getRHS(); |
| 5759 | if (IExp->getType()->isPointerType()) |
| 5760 | std::swap(PExp, IExp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5761 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5762 | bool EvalPtrOK = evaluatePointer(PExp, Result); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 5763 | if (!EvalPtrOK && !Info.noteFailure()) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5764 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5765 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5766 | llvm::APSInt Offset; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 5767 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5768 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 5769 | |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5770 | if (E->getOpcode() == BO_Sub) |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 5771 | negateAsSigned(Offset); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5772 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 5773 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 5774 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5775 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5776 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5777 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5778 | return evaluateLValue(E->getSubExpr(), Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5779 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5780 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5781 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) { |
| 5782 | const Expr* SubExpr = E->getSubExpr(); |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5783 | |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 5784 | switch (E->getCastKind()) { |
| 5785 | default: |
| 5786 | break; |
| 5787 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5788 | case CK_BitCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5789 | case CK_CPointerToObjCPointerCast: |
| 5790 | case CK_BlockPointerToObjCPointerCast: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5791 | case CK_AnyPointerToBlockPointerCast: |
Anastasia Stulova | 5d8ad8a | 2014-11-26 15:36:41 +0000 | [diff] [blame] | 5792 | case CK_AddressSpaceConversion: |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 5793 | if (!Visit(SubExpr)) |
| 5794 | return false; |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5795 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
| 5796 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
| 5797 | // also static_casts, but we disallow them as a resolution to DR1312. |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 5798 | if (!E->getType()->isVoidPointerType()) { |
Richard Smith | b19ac0d | 2012-01-15 03:25:41 +0000 | [diff] [blame] | 5799 | Result.Designator.setInvalid(); |
Richard Smith | ff07af1 | 2011-12-12 19:10:03 +0000 | [diff] [blame] | 5800 | if (SubExpr->getType()->isVoidPointerType()) |
| 5801 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
| 5802 | << 3 << SubExpr->getType(); |
| 5803 | else |
| 5804 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 5805 | } |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 5806 | if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr) |
| 5807 | ZeroInitialization(E); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5808 | return true; |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 5809 | |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5810 | case CK_DerivedToBase: |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5811 | case CK_UncheckedDerivedToBase: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5812 | if (!evaluatePointer(E->getSubExpr(), Result)) |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5813 | return false; |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5814 | if (!Result.Base && Result.Offset.isZero()) |
| 5815 | return true; |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5816 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5817 | // 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] | 5818 | // the derived class to the base class. |
Richard Smith | 8440104 | 2013-06-03 05:03:02 +0000 | [diff] [blame] | 5819 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
| 5820 | castAs<PointerType>()->getPointeeType(), |
| 5821 | Result); |
Anders Carlsson | 1827509 | 2010-10-31 20:41:46 +0000 | [diff] [blame] | 5822 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5823 | case CK_BaseToDerived: |
| 5824 | if (!Visit(E->getSubExpr())) |
| 5825 | return false; |
| 5826 | if (!Result.Base && Result.Offset.isZero()) |
| 5827 | return true; |
| 5828 | return HandleBaseToDerivedCast(Info, E, Result); |
| 5829 | |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5830 | case CK_NullToPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 5831 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 5832 | return ZeroInitialization(E); |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5833 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5834 | case CK_IntegralToPointer: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 5835 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 5836 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5837 | APValue Value; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5838 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
Eli Friedman | 847a2bc | 2009-12-27 05:43:15 +0000 | [diff] [blame] | 5839 | break; |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 5840 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5841 | if (Value.isInt()) { |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5842 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
| 5843 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5844 | Result.Base = (Expr*)nullptr; |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 5845 | Result.InvalidBase = false; |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 5846 | Result.Offset = CharUnits::fromQuantity(N); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5847 | Result.Designator.setInvalid(); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 5848 | Result.IsNullPtr = false; |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5849 | return true; |
| 5850 | } else { |
| 5851 | // Cast is of an lvalue, no need to change value. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 5852 | Result.setFrom(Info.Ctx, Value); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5853 | return true; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5854 | } |
| 5855 | } |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5856 | |
| 5857 | case CK_ArrayToPointerDecay: { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5858 | if (SubExpr->isGLValue()) { |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5859 | if (!evaluateLValue(SubExpr, Result)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5860 | return false; |
| 5861 | } else { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 5862 | APValue &Value = createTemporary(SubExpr, false, Result, |
| 5863 | *Info.CurrentCall); |
| 5864 | if (!EvaluateInPlace(Value, Info, Result, SubExpr)) |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 5865 | return false; |
| 5866 | } |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5867 | // The result is a pointer to the first element of the array. |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5868 | auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType()); |
| 5869 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 5870 | Result.addArray(Info, E, CAT); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 5871 | else |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5872 | Result.addUnsizedArray(Info, E, AT->getElementType()); |
Richard Smith | 96e0c10 | 2011-11-04 02:25:55 +0000 | [diff] [blame] | 5873 | return true; |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5874 | } |
Richard Smith | dd78544 | 2011-10-31 20:57:44 +0000 | [diff] [blame] | 5875 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5876 | case CK_FunctionToPointerDecay: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5877 | return evaluateLValue(SubExpr, Result); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5878 | |
| 5879 | case CK_LValueToRValue: { |
| 5880 | LValue LVal; |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5881 | if (!evaluateLValue(E->getSubExpr(), LVal)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5882 | return false; |
| 5883 | |
| 5884 | APValue RVal; |
| 5885 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
| 5886 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
| 5887 | LVal, RVal)) |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5888 | return InvalidBaseOK && |
| 5889 | evaluateLValueAsAllocSize(Info, LVal.Base, Result); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5890 | return Success(RVal, E); |
| 5891 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 5894 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5895 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 5896 | |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5897 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T) { |
| 5898 | // C++ [expr.alignof]p3: |
| 5899 | // When alignof is applied to a reference type, the result is the |
| 5900 | // alignment of the referenced type. |
| 5901 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 5902 | T = Ref->getPointeeType(); |
| 5903 | |
| 5904 | // __alignof is defined to return the preferred alignment. |
Roger Ferrer Ibanez | 3fa38a1 | 2017-03-08 14:00:44 +0000 | [diff] [blame] | 5905 | if (T.getQualifiers().hasUnaligned()) |
| 5906 | return CharUnits::One(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5907 | return Info.Ctx.toCharUnitsFromBits( |
| 5908 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
| 5909 | } |
| 5910 | |
| 5911 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E) { |
| 5912 | E = E->IgnoreParens(); |
| 5913 | |
| 5914 | // The kinds of expressions that we have special-case logic here for |
| 5915 | // should be kept up to date with the special checks for those |
| 5916 | // expressions in Sema. |
| 5917 | |
| 5918 | // alignof decl is always accepted, even if it doesn't make sense: we default |
| 5919 | // to 1 in those cases. |
| 5920 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 5921 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
| 5922 | /*RefAsPointee*/true); |
| 5923 | |
| 5924 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 5925 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
| 5926 | /*RefAsPointee*/true); |
| 5927 | |
| 5928 | return GetAlignOfType(Info, E->getType()); |
| 5929 | } |
| 5930 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5931 | // To be clear: this happily visits unsupported builtins. Better name welcomed. |
| 5932 | bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) { |
| 5933 | if (ExprEvaluatorBaseTy::VisitCallExpr(E)) |
| 5934 | return true; |
| 5935 | |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5936 | if (!(InvalidBaseOK && getAllocSizeAttr(E))) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5937 | return false; |
| 5938 | |
| 5939 | Result.setInvalid(E); |
| 5940 | QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType(); |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 5941 | Result.addUnsizedArray(Info, E, PointeeTy); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5942 | return true; |
| 5943 | } |
| 5944 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 5945 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 5946 | if (IsStringLiteralCall(E)) |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 5947 | return Success(E); |
Eli Friedman | c69d454 | 2009-01-25 01:54:01 +0000 | [diff] [blame] | 5948 | |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 5949 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
| 5950 | return VisitBuiltinCallExpr(E, BuiltinOp); |
| 5951 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5952 | return visitNonBuiltinCallExpr(E); |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 5953 | } |
| 5954 | |
| 5955 | bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
| 5956 | unsigned BuiltinOp) { |
| 5957 | switch (BuiltinOp) { |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 5958 | case Builtin::BI__builtin_addressof: |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5959 | return evaluateLValue(E->getArg(0), Result); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5960 | case Builtin::BI__builtin_assume_aligned: { |
| 5961 | // We need to be very careful here because: if the pointer does not have the |
| 5962 | // asserted alignment, then the behavior is undefined, and undefined |
| 5963 | // behavior is non-constant. |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 5964 | if (!evaluatePointer(E->getArg(0), Result)) |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5965 | return false; |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 5966 | |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5967 | LValue OffsetResult(Result); |
| 5968 | APSInt Alignment; |
| 5969 | if (!EvaluateInteger(E->getArg(1), Alignment, Info)) |
| 5970 | return false; |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 5971 | CharUnits Align = CharUnits::fromQuantity(Alignment.getZExtValue()); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5972 | |
| 5973 | if (E->getNumArgs() > 2) { |
| 5974 | APSInt Offset; |
| 5975 | if (!EvaluateInteger(E->getArg(2), Offset, Info)) |
| 5976 | return false; |
| 5977 | |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 5978 | int64_t AdditionalOffset = -Offset.getZExtValue(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5979 | OffsetResult.Offset += CharUnits::fromQuantity(AdditionalOffset); |
| 5980 | } |
| 5981 | |
| 5982 | // If there is a base object, then it must have the correct alignment. |
| 5983 | if (OffsetResult.Base) { |
| 5984 | CharUnits BaseAlignment; |
| 5985 | if (const ValueDecl *VD = |
| 5986 | OffsetResult.Base.dyn_cast<const ValueDecl*>()) { |
| 5987 | BaseAlignment = Info.Ctx.getDeclAlign(VD); |
| 5988 | } else { |
| 5989 | BaseAlignment = |
| 5990 | GetAlignOfExpr(Info, OffsetResult.Base.get<const Expr*>()); |
| 5991 | } |
| 5992 | |
| 5993 | if (BaseAlignment < Align) { |
| 5994 | Result.Designator.setInvalid(); |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 5995 | // FIXME: Add support to Diagnostic for long / long long. |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 5996 | CCEDiag(E->getArg(0), |
| 5997 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 5998 | << (unsigned)BaseAlignment.getQuantity() |
| 5999 | << (unsigned)Align.getQuantity(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6000 | return false; |
| 6001 | } |
| 6002 | } |
| 6003 | |
| 6004 | // The offset must also have the correct alignment. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6005 | if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) { |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6006 | Result.Designator.setInvalid(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6007 | |
Richard Smith | 642a236 | 2017-01-30 23:30:26 +0000 | [diff] [blame] | 6008 | (OffsetResult.Base |
| 6009 | ? CCEDiag(E->getArg(0), |
| 6010 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
| 6011 | : CCEDiag(E->getArg(0), |
| 6012 | diag::note_constexpr_baa_value_insufficient_alignment)) |
| 6013 | << (int)OffsetResult.Offset.getQuantity() |
| 6014 | << (unsigned)Align.getQuantity(); |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 6015 | return false; |
| 6016 | } |
| 6017 | |
| 6018 | return true; |
| 6019 | } |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6020 | |
| 6021 | case Builtin::BIstrchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6022 | case Builtin::BIwcschr: |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6023 | case Builtin::BImemchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6024 | case Builtin::BIwmemchr: |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6025 | if (Info.getLangOpts().CPlusPlus11) |
| 6026 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| 6027 | << /*isConstexpr*/0 << /*isConstructor*/0 |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6028 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6029 | else |
| 6030 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 6031 | LLVM_FALLTHROUGH; |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6032 | case Builtin::BI__builtin_strchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6033 | case Builtin::BI__builtin_wcschr: |
| 6034 | case Builtin::BI__builtin_memchr: |
Richard Smith | 5e29dd3 | 2017-01-20 00:45:35 +0000 | [diff] [blame] | 6035 | case Builtin::BI__builtin_char_memchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6036 | case Builtin::BI__builtin_wmemchr: { |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6037 | if (!Visit(E->getArg(0))) |
| 6038 | return false; |
| 6039 | APSInt Desired; |
| 6040 | if (!EvaluateInteger(E->getArg(1), Desired, Info)) |
| 6041 | return false; |
| 6042 | uint64_t MaxLength = uint64_t(-1); |
| 6043 | if (BuiltinOp != Builtin::BIstrchr && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6044 | BuiltinOp != Builtin::BIwcschr && |
| 6045 | BuiltinOp != Builtin::BI__builtin_strchr && |
| 6046 | BuiltinOp != Builtin::BI__builtin_wcschr) { |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6047 | APSInt N; |
| 6048 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
| 6049 | return false; |
| 6050 | MaxLength = N.getExtValue(); |
| 6051 | } |
| 6052 | |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6053 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6054 | |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6055 | // Figure out what value we're actually looking for (after converting to |
| 6056 | // the corresponding unsigned type if necessary). |
| 6057 | uint64_t DesiredVal; |
| 6058 | bool StopAtNull = false; |
| 6059 | switch (BuiltinOp) { |
| 6060 | case Builtin::BIstrchr: |
| 6061 | case Builtin::BI__builtin_strchr: |
| 6062 | // strchr compares directly to the passed integer, and therefore |
| 6063 | // always fails if given an int that is not a char. |
| 6064 | if (!APSInt::isSameValue(HandleIntToIntCast(Info, E, CharTy, |
| 6065 | E->getArg(1)->getType(), |
| 6066 | Desired), |
| 6067 | Desired)) |
| 6068 | return ZeroInitialization(E); |
| 6069 | StopAtNull = true; |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 6070 | LLVM_FALLTHROUGH; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6071 | case Builtin::BImemchr: |
| 6072 | case Builtin::BI__builtin_memchr: |
Richard Smith | 5e29dd3 | 2017-01-20 00:45:35 +0000 | [diff] [blame] | 6073 | case Builtin::BI__builtin_char_memchr: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6074 | // memchr compares by converting both sides to unsigned char. That's also |
| 6075 | // correct for strchr if we get this far (to cope with plain char being |
| 6076 | // unsigned in the strchr case). |
| 6077 | DesiredVal = Desired.trunc(Info.Ctx.getCharWidth()).getZExtValue(); |
| 6078 | break; |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6079 | |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6080 | case Builtin::BIwcschr: |
| 6081 | case Builtin::BI__builtin_wcschr: |
| 6082 | StopAtNull = true; |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 6083 | LLVM_FALLTHROUGH; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6084 | case Builtin::BIwmemchr: |
| 6085 | case Builtin::BI__builtin_wmemchr: |
| 6086 | // wcschr and wmemchr are given a wchar_t to look for. Just use it. |
| 6087 | DesiredVal = Desired.getZExtValue(); |
| 6088 | break; |
| 6089 | } |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6090 | |
| 6091 | for (; MaxLength; --MaxLength) { |
| 6092 | APValue Char; |
| 6093 | if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) || |
| 6094 | !Char.isInt()) |
| 6095 | return false; |
| 6096 | if (Char.getInt().getZExtValue() == DesiredVal) |
| 6097 | return true; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 6098 | if (StopAtNull && !Char.getInt()) |
Richard Smith | e950795 | 2016-11-12 01:39:56 +0000 | [diff] [blame] | 6099 | break; |
| 6100 | if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1)) |
| 6101 | return false; |
| 6102 | } |
| 6103 | // Not found: return nullptr. |
| 6104 | return ZeroInitialization(E); |
| 6105 | } |
| 6106 | |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 6107 | default: |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6108 | return visitNonBuiltinCallExpr(E); |
Richard Smith | 6cbd65d | 2013-07-11 02:27:57 +0000 | [diff] [blame] | 6109 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 6110 | } |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 6111 | |
| 6112 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6113 | // Member Pointer Evaluation |
| 6114 | //===----------------------------------------------------------------------===// |
| 6115 | |
| 6116 | namespace { |
| 6117 | class MemberPointerExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6118 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6119 | MemberPtr &Result; |
| 6120 | |
| 6121 | bool Success(const ValueDecl *D) { |
| 6122 | Result = MemberPtr(D); |
| 6123 | return true; |
| 6124 | } |
| 6125 | public: |
| 6126 | |
| 6127 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
| 6128 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
| 6129 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6130 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6131 | Result.setFrom(V); |
| 6132 | return true; |
| 6133 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6134 | bool ZeroInitialization(const Expr *E) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6135 | return Success((const ValueDecl*)nullptr); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | bool VisitCastExpr(const CastExpr *E); |
| 6139 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
| 6140 | }; |
| 6141 | } // end anonymous namespace |
| 6142 | |
| 6143 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
| 6144 | EvalInfo &Info) { |
| 6145 | assert(E->isRValue() && E->getType()->isMemberPointerType()); |
| 6146 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
| 6147 | } |
| 6148 | |
| 6149 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 6150 | switch (E->getCastKind()) { |
| 6151 | default: |
| 6152 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6153 | |
| 6154 | case CK_NullToMemberPointer: |
Richard Smith | 4051ff7 | 2012-04-08 08:02:07 +0000 | [diff] [blame] | 6155 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6156 | return ZeroInitialization(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6157 | |
| 6158 | case CK_BaseToDerivedMemberPointer: { |
| 6159 | if (!Visit(E->getSubExpr())) |
| 6160 | return false; |
| 6161 | if (E->path_empty()) |
| 6162 | return true; |
| 6163 | // Base-to-derived member pointer casts store the path in derived-to-base |
| 6164 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
| 6165 | // the wrong end of the derived->base arc, so stagger the path by one class. |
| 6166 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
| 6167 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
| 6168 | PathI != PathE; ++PathI) { |
| 6169 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 6170 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 6171 | if (!Result.castToDerived(Derived)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6172 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6173 | } |
| 6174 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
| 6175 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6176 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6177 | return true; |
| 6178 | } |
| 6179 | |
| 6180 | case CK_DerivedToBaseMemberPointer: |
| 6181 | if (!Visit(E->getSubExpr())) |
| 6182 | return false; |
| 6183 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 6184 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 6185 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
| 6186 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 6187 | if (!Result.castToBase(Base)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6188 | return Error(E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6189 | } |
| 6190 | return true; |
| 6191 | } |
| 6192 | } |
| 6193 | |
| 6194 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
| 6195 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
| 6196 | // member can be formed. |
| 6197 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
| 6198 | } |
| 6199 | |
| 6200 | //===----------------------------------------------------------------------===// |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6201 | // Record Evaluation |
| 6202 | //===----------------------------------------------------------------------===// |
| 6203 | |
| 6204 | namespace { |
| 6205 | class RecordExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6206 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6207 | const LValue &This; |
| 6208 | APValue &Result; |
| 6209 | public: |
| 6210 | |
| 6211 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
| 6212 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
| 6213 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6214 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6215 | Result = V; |
| 6216 | return true; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6217 | } |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6218 | bool ZeroInitialization(const Expr *E) { |
| 6219 | return ZeroInitialization(E, E->getType()); |
| 6220 | } |
| 6221 | bool ZeroInitialization(const Expr *E, QualType T); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6222 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6223 | bool VisitCallExpr(const CallExpr *E) { |
| 6224 | return handleCallExpr(E, Result, &This); |
| 6225 | } |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6226 | bool VisitCastExpr(const CastExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6227 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6228 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 6229 | return VisitCXXConstructExpr(E, E->getType()); |
| 6230 | } |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6231 | bool VisitLambdaExpr(const LambdaExpr *E); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6232 | bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E); |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6233 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T); |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6234 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6235 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6236 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6237 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6238 | /// Perform zero-initialization on an object of non-union class type. |
| 6239 | /// C++11 [dcl.init]p5: |
| 6240 | /// To zero-initialize an object or reference of type T means: |
| 6241 | /// [...] |
| 6242 | /// -- if T is a (possibly cv-qualified) non-union class type, |
| 6243 | /// each non-static data member and each base-class subobject is |
| 6244 | /// zero-initialized |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6245 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
| 6246 | const RecordDecl *RD, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6247 | const LValue &This, APValue &Result) { |
| 6248 | assert(!RD->isUnion() && "Expected non-union class type"); |
| 6249 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
| 6250 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
Aaron Ballman | 62e47c4 | 2014-03-10 13:43:55 +0000 | [diff] [blame] | 6251 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6252 | |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6253 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6254 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 6255 | |
| 6256 | if (CD) { |
| 6257 | unsigned Index = 0; |
| 6258 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6259 | End = CD->bases_end(); I != End; ++I, ++Index) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6260 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
| 6261 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6262 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
| 6263 | return false; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6264 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6265 | Result.getStructBase(Index))) |
| 6266 | return false; |
| 6267 | } |
| 6268 | } |
| 6269 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6270 | for (const auto *I : RD->fields()) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6271 | // -- if T is a reference type, no initialization is performed. |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6272 | if (I->getType()->isReferenceType()) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6273 | continue; |
| 6274 | |
| 6275 | LValue Subobject = This; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6276 | if (!HandleLValueMember(Info, E, Subobject, I, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6277 | return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6278 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6279 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6280 | if (!EvaluateInPlace( |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6281 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6282 | return false; |
| 6283 | } |
| 6284 | |
| 6285 | return true; |
| 6286 | } |
| 6287 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6288 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) { |
| 6289 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 6290 | if (RD->isInvalidDecl()) return false; |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6291 | if (RD->isUnion()) { |
| 6292 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
| 6293 | // object's first non-static named data member is zero-initialized |
| 6294 | RecordDecl::field_iterator I = RD->field_begin(); |
| 6295 | if (I == RD->field_end()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6296 | Result = APValue((const FieldDecl*)nullptr); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6297 | return true; |
| 6298 | } |
| 6299 | |
| 6300 | LValue Subobject = This; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 6301 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6302 | return false; |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 6303 | Result = APValue(*I); |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 6304 | ImplicitValueInitExpr VIE(I->getType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6305 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6306 | } |
| 6307 | |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 6308 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 6309 | Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD; |
Richard Smith | 5d10860 | 2012-02-17 00:44:16 +0000 | [diff] [blame] | 6310 | return false; |
| 6311 | } |
| 6312 | |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6313 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6314 | } |
| 6315 | |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6316 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 6317 | switch (E->getCastKind()) { |
| 6318 | default: |
| 6319 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6320 | |
| 6321 | case CK_ConstructorConversion: |
| 6322 | return Visit(E->getSubExpr()); |
| 6323 | |
| 6324 | case CK_DerivedToBase: |
| 6325 | case CK_UncheckedDerivedToBase: { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6326 | APValue DerivedObject; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6327 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6328 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6329 | if (!DerivedObject.isStruct()) |
| 6330 | return Error(E->getSubExpr()); |
Richard Smith | e97cbd7 | 2011-11-11 04:05:33 +0000 | [diff] [blame] | 6331 | |
| 6332 | // Derived-to-base rvalue conversion: just slice off the derived part. |
| 6333 | APValue *Value = &DerivedObject; |
| 6334 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
| 6335 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
| 6336 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
| 6337 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
| 6338 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
| 6339 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
| 6340 | RD = Base; |
| 6341 | } |
| 6342 | Result = *Value; |
| 6343 | return true; |
| 6344 | } |
| 6345 | } |
| 6346 | } |
| 6347 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6348 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 122f88d | 2016-12-06 23:52:28 +0000 | [diff] [blame] | 6349 | if (E->isTransparent()) |
| 6350 | return Visit(E->getInit(0)); |
| 6351 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6352 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 6353 | if (RD->isInvalidDecl()) return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6354 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
| 6355 | |
| 6356 | if (RD->isUnion()) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6357 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
| 6358 | Result = APValue(Field); |
| 6359 | if (!Field) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6360 | return true; |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6361 | |
| 6362 | // If the initializer list for a union does not contain any elements, the |
| 6363 | // first element of the union is value-initialized. |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6364 | // FIXME: The element should be initialized from an initializer list. |
| 6365 | // Is this difference ever observable for initializer lists which |
| 6366 | // we don't build? |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6367 | ImplicitValueInitExpr VIE(Field->getType()); |
| 6368 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
| 6369 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6370 | LValue Subobject = This; |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6371 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
| 6372 | return false; |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6373 | |
| 6374 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 6375 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 6376 | isa<CXXDefaultInitExpr>(InitExpr)); |
| 6377 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6378 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6379 | } |
| 6380 | |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6381 | auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
Richard Smith | c0d04a2 | 2016-05-25 22:06:25 +0000 | [diff] [blame] | 6382 | if (Result.isUninit()) |
| 6383 | Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0, |
| 6384 | std::distance(RD->field_begin(), RD->field_end())); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6385 | unsigned ElementNo = 0; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6386 | bool Success = true; |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6387 | |
| 6388 | // Initialize base classes. |
| 6389 | if (CXXRD) { |
| 6390 | for (const auto &Base : CXXRD->bases()) { |
| 6391 | assert(ElementNo < E->getNumInits() && "missing init for base class"); |
| 6392 | const Expr *Init = E->getInit(ElementNo); |
| 6393 | |
| 6394 | LValue Subobject = This; |
| 6395 | if (!HandleLValueBase(Info, Init, Subobject, CXXRD, &Base)) |
| 6396 | return false; |
| 6397 | |
| 6398 | APValue &FieldVal = Result.getStructBase(ElementNo); |
| 6399 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init)) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 6400 | if (!Info.noteFailure()) |
Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 6401 | return false; |
| 6402 | Success = false; |
| 6403 | } |
| 6404 | ++ElementNo; |
| 6405 | } |
| 6406 | } |
| 6407 | |
| 6408 | // Initialize members. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6409 | for (const auto *Field : RD->fields()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6410 | // Anonymous bit-fields are not considered members of the class for |
| 6411 | // purposes of aggregate initialization. |
| 6412 | if (Field->isUnnamedBitfield()) |
| 6413 | continue; |
| 6414 | |
| 6415 | LValue Subobject = This; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6416 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6417 | bool HaveInit = ElementNo < E->getNumInits(); |
| 6418 | |
| 6419 | // FIXME: Diagnostics here should point to the end of the initializer |
| 6420 | // list, not the start. |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6421 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6422 | Subobject, Field, &Layout)) |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 6423 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6424 | |
| 6425 | // Perform an implicit value-initialization for members beyond the end of |
| 6426 | // the initializer list. |
| 6427 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6428 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6429 | |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 6430 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
| 6431 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
| 6432 | isa<CXXDefaultInitExpr>(Init)); |
| 6433 | |
Richard Smith | 49ca8aa | 2013-08-06 07:09:20 +0000 | [diff] [blame] | 6434 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 6435 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) || |
| 6436 | (Field->isBitField() && !truncateBitfieldValue(Info, Init, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6437 | FieldVal, Field))) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 6438 | if (!Info.noteFailure()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6439 | return false; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6440 | Success = false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6441 | } |
| 6442 | } |
| 6443 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6444 | return Success; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6445 | } |
| 6446 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6447 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 6448 | QualType T) { |
| 6449 | // Note that E's type is not necessarily the type of our class here; we might |
| 6450 | // be initializing an array element instead. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6451 | const CXXConstructorDecl *FD = E->getConstructor(); |
John McCall | 3c79d88 | 2012-04-26 18:10:01 +0000 | [diff] [blame] | 6452 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
| 6453 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6454 | bool ZeroInit = E->requiresZeroInitialization(); |
| 6455 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
Richard Smith | 9eae723 | 2012-01-12 18:54:33 +0000 | [diff] [blame] | 6456 | // If we've already performed zero-initialization, we're already done. |
| 6457 | if (!Result.isUninit()) |
| 6458 | return true; |
| 6459 | |
Richard Smith | da3f4fd | 2014-03-05 23:32:50 +0000 | [diff] [blame] | 6460 | // We can get here in two different ways: |
| 6461 | // 1) We're performing value-initialization, and should zero-initialize |
| 6462 | // the object, or |
| 6463 | // 2) We're performing default-initialization of an object with a trivial |
| 6464 | // constexpr default constructor, in which case we should start the |
| 6465 | // lifetimes of all the base subobjects (there can be no data member |
| 6466 | // subobjects in this case) per [basic.life]p1. |
| 6467 | // Either way, ZeroInitialization is appropriate. |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6468 | return ZeroInitialization(E, T); |
Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 6469 | } |
| 6470 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6471 | const FunctionDecl *Definition = nullptr; |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 6472 | auto Body = FD->getBody(Definition); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6473 | |
Olivier Goffart | 8bc0caa2e | 2016-02-12 12:34:44 +0000 | [diff] [blame] | 6474 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 6475 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6476 | |
Richard Smith | 1bc5c2c | 2012-01-10 04:32:03 +0000 | [diff] [blame] | 6477 | // Avoid materializing a temporary for an elidable copy/move constructor. |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6478 | if (E->isElidable() && !ZeroInit) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6479 | if (const MaterializeTemporaryExpr *ME |
| 6480 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
| 6481 | return Visit(ME->GetTemporaryExpr()); |
| 6482 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 6483 | if (ZeroInit && !ZeroInitialization(E, T)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6484 | return false; |
| 6485 | |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 6486 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 6487 | return HandleConstructorCall(E, This, Args, |
| 6488 | cast<CXXConstructorDecl>(Definition), Info, |
| 6489 | Result); |
| 6490 | } |
| 6491 | |
| 6492 | bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr( |
| 6493 | const CXXInheritedCtorInitExpr *E) { |
| 6494 | if (!Info.CurrentCall) { |
| 6495 | assert(Info.checkingPotentialConstantExpression()); |
| 6496 | return false; |
| 6497 | } |
| 6498 | |
| 6499 | const CXXConstructorDecl *FD = E->getConstructor(); |
| 6500 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) |
| 6501 | return false; |
| 6502 | |
| 6503 | const FunctionDecl *Definition = nullptr; |
| 6504 | auto Body = FD->getBody(Definition); |
| 6505 | |
| 6506 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
| 6507 | return false; |
| 6508 | |
| 6509 | return HandleConstructorCall(E, This, Info.CurrentCall->Arguments, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6510 | cast<CXXConstructorDecl>(Definition), Info, |
| 6511 | Result); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6512 | } |
| 6513 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6514 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
| 6515 | const CXXStdInitializerListExpr *E) { |
| 6516 | const ConstantArrayType *ArrayType = |
| 6517 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
| 6518 | |
| 6519 | LValue Array; |
| 6520 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
| 6521 | return false; |
| 6522 | |
| 6523 | // Get a pointer to the first element of the array. |
| 6524 | Array.addArray(Info, E, ArrayType); |
| 6525 | |
| 6526 | // FIXME: Perform the checks on the field types in SemaInit. |
| 6527 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
| 6528 | RecordDecl::field_iterator Field = Record->field_begin(); |
| 6529 | if (Field == Record->field_end()) |
| 6530 | return Error(E); |
| 6531 | |
| 6532 | // Start pointer. |
| 6533 | if (!Field->getType()->isPointerType() || |
| 6534 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 6535 | ArrayType->getElementType())) |
| 6536 | return Error(E); |
| 6537 | |
| 6538 | // FIXME: What if the initializer_list type has base classes, etc? |
| 6539 | Result = APValue(APValue::UninitStruct(), 0, 2); |
| 6540 | Array.moveInto(Result.getStructField(0)); |
| 6541 | |
| 6542 | if (++Field == Record->field_end()) |
| 6543 | return Error(E); |
| 6544 | |
| 6545 | if (Field->getType()->isPointerType() && |
| 6546 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
| 6547 | ArrayType->getElementType())) { |
| 6548 | // End pointer. |
| 6549 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
| 6550 | ArrayType->getElementType(), |
| 6551 | ArrayType->getSize().getZExtValue())) |
| 6552 | return false; |
| 6553 | Array.moveInto(Result.getStructField(1)); |
| 6554 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
| 6555 | // Length. |
| 6556 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
| 6557 | else |
| 6558 | return Error(E); |
| 6559 | |
| 6560 | if (++Field != Record->field_end()) |
| 6561 | return Error(E); |
| 6562 | |
| 6563 | return true; |
| 6564 | } |
| 6565 | |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6566 | bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) { |
| 6567 | const CXXRecordDecl *ClosureClass = E->getLambdaClass(); |
| 6568 | if (ClosureClass->isInvalidDecl()) return false; |
| 6569 | |
| 6570 | if (Info.checkingPotentialConstantExpression()) return true; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 6571 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6572 | const size_t NumFields = |
| 6573 | std::distance(ClosureClass->field_begin(), ClosureClass->field_end()); |
Benjamin Kramer | aad1bdc | 2017-02-16 14:08:41 +0000 | [diff] [blame] | 6574 | |
| 6575 | assert(NumFields == (size_t)std::distance(E->capture_init_begin(), |
| 6576 | E->capture_init_end()) && |
| 6577 | "The number of lambda capture initializers should equal the number of " |
| 6578 | "fields within the closure type"); |
| 6579 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6580 | Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields); |
| 6581 | // Iterate through all the lambda's closure object's fields and initialize |
| 6582 | // them. |
| 6583 | auto *CaptureInitIt = E->capture_init_begin(); |
| 6584 | const LambdaCapture *CaptureIt = ClosureClass->captures_begin(); |
| 6585 | bool Success = true; |
| 6586 | for (const auto *Field : ClosureClass->fields()) { |
| 6587 | assert(CaptureInitIt != E->capture_init_end()); |
| 6588 | // Get the initializer for this field |
| 6589 | Expr *const CurFieldInit = *CaptureInitIt++; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 6590 | |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6591 | // If there is no initializer, either this is a VLA or an error has |
| 6592 | // occurred. |
| 6593 | if (!CurFieldInit) |
| 6594 | return Error(E); |
| 6595 | |
| 6596 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
| 6597 | if (!EvaluateInPlace(FieldVal, Info, This, CurFieldInit)) { |
| 6598 | if (!Info.keepEvaluatingAfterFailure()) |
| 6599 | return false; |
| 6600 | Success = false; |
| 6601 | } |
| 6602 | ++CaptureIt; |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6603 | } |
Faisal Vali | 051e3a2 | 2017-02-16 04:12:21 +0000 | [diff] [blame] | 6604 | return Success; |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6605 | } |
| 6606 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6607 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
| 6608 | APValue &Result, EvalInfo &Info) { |
| 6609 | assert(E->isRValue() && E->getType()->isRecordType() && |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6610 | "can't evaluate expression as a record rvalue"); |
| 6611 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
| 6612 | } |
| 6613 | |
| 6614 | //===----------------------------------------------------------------------===// |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6615 | // Temporary Evaluation |
| 6616 | // |
| 6617 | // Temporaries are represented in the AST as rvalues, but generally behave like |
| 6618 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
| 6619 | // materialized so that a reference can bind to it. |
| 6620 | //===----------------------------------------------------------------------===// |
| 6621 | namespace { |
| 6622 | class TemporaryExprEvaluator |
| 6623 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
| 6624 | public: |
| 6625 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 6626 | LValueExprEvaluatorBaseTy(Info, Result, false) {} |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6627 | |
| 6628 | /// Visit an expression which constructs the value of this temporary. |
| 6629 | bool VisitConstructExpr(const Expr *E) { |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 6630 | APValue &Value = createTemporary(E, false, Result, *Info.CurrentCall); |
| 6631 | return EvaluateInPlace(Value, Info, Result, E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6632 | } |
| 6633 | |
| 6634 | bool VisitCastExpr(const CastExpr *E) { |
| 6635 | switch (E->getCastKind()) { |
| 6636 | default: |
| 6637 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
| 6638 | |
| 6639 | case CK_ConstructorConversion: |
| 6640 | return VisitConstructExpr(E->getSubExpr()); |
| 6641 | } |
| 6642 | } |
| 6643 | bool VisitInitListExpr(const InitListExpr *E) { |
| 6644 | return VisitConstructExpr(E); |
| 6645 | } |
| 6646 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
| 6647 | return VisitConstructExpr(E); |
| 6648 | } |
| 6649 | bool VisitCallExpr(const CallExpr *E) { |
| 6650 | return VisitConstructExpr(E); |
| 6651 | } |
Richard Smith | 513955c | 2014-12-17 19:24:30 +0000 | [diff] [blame] | 6652 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
| 6653 | return VisitConstructExpr(E); |
| 6654 | } |
Faisal Vali | c72a08c | 2017-01-09 03:02:53 +0000 | [diff] [blame] | 6655 | bool VisitLambdaExpr(const LambdaExpr *E) { |
| 6656 | return VisitConstructExpr(E); |
| 6657 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6658 | }; |
| 6659 | } // end anonymous namespace |
| 6660 | |
| 6661 | /// Evaluate an expression of record type as a temporary. |
| 6662 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
Richard Smith | d0b111c | 2011-12-19 22:01:37 +0000 | [diff] [blame] | 6663 | assert(E->isRValue() && E->getType()->isRecordType()); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6664 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
| 6665 | } |
| 6666 | |
| 6667 | //===----------------------------------------------------------------------===// |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6668 | // Vector Evaluation |
| 6669 | //===----------------------------------------------------------------------===// |
| 6670 | |
| 6671 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 6672 | class VectorExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6673 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6674 | APValue &Result; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6675 | public: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6676 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6677 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
| 6678 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6679 | |
Craig Topper | 9798b93 | 2015-09-29 04:30:05 +0000 | [diff] [blame] | 6680 | bool Success(ArrayRef<APValue> V, const Expr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6681 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
| 6682 | // FIXME: remove this APValue copy. |
| 6683 | Result = APValue(V.data(), V.size()); |
| 6684 | return true; |
| 6685 | } |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6686 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 6687 | assert(V.isVector()); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6688 | Result = V; |
| 6689 | return true; |
| 6690 | } |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6691 | bool ZeroInitialization(const Expr *E); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6692 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6693 | bool VisitUnaryReal(const UnaryOperator *E) |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6694 | { return Visit(E->getSubExpr()); } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6695 | bool VisitCastExpr(const CastExpr* E); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6696 | bool VisitInitListExpr(const InitListExpr *E); |
| 6697 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6698 | // FIXME: Missing: unary -, unary ~, binary add/sub/mul/div, |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 6699 | // binary comparisons, binary and/or/xor, |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6700 | // shufflevector, ExtVectorElementExpr |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6701 | }; |
| 6702 | } // end anonymous namespace |
| 6703 | |
| 6704 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6705 | assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6706 | return VectorExprEvaluator(Info, Result).Visit(E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6707 | } |
| 6708 | |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 6709 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6710 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 6711 | unsigned NElts = VTy->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6712 | |
Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 6713 | const Expr *SE = E->getSubExpr(); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 6714 | QualType SETy = SE->getType(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6715 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6716 | switch (E->getCastKind()) { |
| 6717 | case CK_VectorSplat: { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6718 | APValue Val = APValue(); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6719 | if (SETy->isIntegerType()) { |
| 6720 | APSInt IntResult; |
| 6721 | if (!EvaluateInteger(SE, IntResult, Info)) |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 6722 | return false; |
| 6723 | Val = APValue(std::move(IntResult)); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6724 | } else if (SETy->isRealFloatingType()) { |
George Burgess IV | 533ff00 | 2015-12-11 00:23:35 +0000 | [diff] [blame] | 6725 | APFloat FloatResult(0.0); |
| 6726 | if (!EvaluateFloat(SE, FloatResult, Info)) |
| 6727 | return false; |
| 6728 | Val = APValue(std::move(FloatResult)); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6729 | } else { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6730 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6731 | } |
Nate Begeman | ef1a7fa | 2009-07-01 07:50:47 +0000 | [diff] [blame] | 6732 | |
| 6733 | // Splat and create vector APValue. |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6734 | SmallVector<APValue, 4> Elts(NElts, Val); |
| 6735 | return Success(Elts, E); |
Nate Begeman | 2ffd384 | 2009-06-26 18:22:18 +0000 | [diff] [blame] | 6736 | } |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 6737 | case CK_BitCast: { |
| 6738 | // Evaluate the operand into an APInt we can extract from. |
| 6739 | llvm::APInt SValInt; |
| 6740 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
| 6741 | return false; |
| 6742 | // Extract the elements |
| 6743 | QualType EltTy = VTy->getElementType(); |
| 6744 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
| 6745 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
| 6746 | SmallVector<APValue, 4> Elts; |
| 6747 | if (EltTy->isRealFloatingType()) { |
| 6748 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 6749 | unsigned FloatEltSize = EltSize; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 6750 | if (&Sem == &APFloat::x87DoubleExtended()) |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 6751 | FloatEltSize = 80; |
| 6752 | for (unsigned i = 0; i < NElts; i++) { |
| 6753 | llvm::APInt Elt; |
| 6754 | if (BigEndian) |
| 6755 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
| 6756 | else |
| 6757 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
Tim Northover | 178723a | 2013-01-22 09:46:51 +0000 | [diff] [blame] | 6758 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
Eli Friedman | 803acb3 | 2011-12-22 03:51:45 +0000 | [diff] [blame] | 6759 | } |
| 6760 | } else if (EltTy->isIntegerType()) { |
| 6761 | for (unsigned i = 0; i < NElts; i++) { |
| 6762 | llvm::APInt Elt; |
| 6763 | if (BigEndian) |
| 6764 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
| 6765 | else |
| 6766 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
| 6767 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
| 6768 | } |
| 6769 | } else { |
| 6770 | return Error(E); |
| 6771 | } |
| 6772 | return Success(Elts, E); |
| 6773 | } |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6774 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 6775 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 6776 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6777 | } |
| 6778 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6779 | bool |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6780 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6781 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6782 | unsigned NumInits = E->getNumInits(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6783 | unsigned NumElements = VT->getNumElements(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6784 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6785 | QualType EltTy = VT->getElementType(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6786 | SmallVector<APValue, 4> Elements; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6787 | |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6788 | // The number of initializers can be less than the number of |
| 6789 | // vector elements. For OpenCL, this can be due to nested vector |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 6790 | // initialization. For GCC compatibility, missing trailing elements |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6791 | // should be initialized with zeroes. |
| 6792 | unsigned CountInits = 0, CountElts = 0; |
| 6793 | while (CountElts < NumElements) { |
| 6794 | // Handle nested vector initialization. |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 6795 | if (CountInits < NumInits |
Eli Friedman | 1409e6e | 2013-09-17 04:07:02 +0000 | [diff] [blame] | 6796 | && E->getInit(CountInits)->getType()->isVectorType()) { |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6797 | APValue v; |
| 6798 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
| 6799 | return Error(E); |
| 6800 | unsigned vlen = v.getVectorLength(); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 6801 | for (unsigned j = 0; j < vlen; j++) |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6802 | Elements.push_back(v.getVectorElt(j)); |
| 6803 | CountElts += vlen; |
| 6804 | } else if (EltTy->isIntegerType()) { |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6805 | llvm::APSInt sInt(32); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6806 | if (CountInits < NumInits) { |
| 6807 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 6808 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6809 | } else // trailing integer zero. |
| 6810 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
| 6811 | Elements.push_back(APValue(sInt)); |
| 6812 | CountElts++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6813 | } else { |
| 6814 | llvm::APFloat f(0.0); |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6815 | if (CountInits < NumInits) { |
| 6816 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
Richard Smith | ac2f0b1 | 2012-03-13 20:58:32 +0000 | [diff] [blame] | 6817 | return false; |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6818 | } else // trailing float zero. |
| 6819 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
| 6820 | Elements.push_back(APValue(f)); |
| 6821 | CountElts++; |
John McCall | 875679e | 2010-06-11 17:54:15 +0000 | [diff] [blame] | 6822 | } |
Eli Friedman | b9c7129 | 2012-01-03 23:24:20 +0000 | [diff] [blame] | 6823 | CountInits++; |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6824 | } |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6825 | return Success(Elements, E); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6826 | } |
| 6827 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6828 | bool |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6829 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6830 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6831 | QualType EltTy = VT->getElementType(); |
| 6832 | APValue ZeroElement; |
| 6833 | if (EltTy->isIntegerType()) |
| 6834 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
| 6835 | else |
| 6836 | ZeroElement = |
| 6837 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
| 6838 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6839 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6840 | return Success(Elements, E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6841 | } |
| 6842 | |
Richard Smith | 2d40634 | 2011-10-22 21:10:00 +0000 | [diff] [blame] | 6843 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 6844 | VisitIgnoredValue(E->getSubExpr()); |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6845 | return ZeroInitialization(E); |
Eli Friedman | 3ae5911 | 2009-02-23 04:23:56 +0000 | [diff] [blame] | 6846 | } |
| 6847 | |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6848 | //===----------------------------------------------------------------------===// |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6849 | // Array Evaluation |
| 6850 | //===----------------------------------------------------------------------===// |
| 6851 | |
| 6852 | namespace { |
| 6853 | class ArrayExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 6854 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6855 | const LValue &This; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6856 | APValue &Result; |
| 6857 | public: |
| 6858 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6859 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
| 6860 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6861 | |
| 6862 | bool Success(const APValue &V, const Expr *E) { |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 6863 | assert((V.isArray() || V.isLValue()) && |
| 6864 | "expected array or string literal"); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6865 | Result = V; |
| 6866 | return true; |
| 6867 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6868 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6869 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6870 | const ConstantArrayType *CAT = |
| 6871 | Info.Ctx.getAsConstantArrayType(E->getType()); |
| 6872 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6873 | return Error(E); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6874 | |
| 6875 | Result = APValue(APValue::UninitArray(), 0, |
| 6876 | CAT->getSize().getZExtValue()); |
| 6877 | if (!Result.hasArrayFiller()) return true; |
| 6878 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6879 | // Zero-initialize all elements. |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6880 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6881 | Subobject.addArray(Info, E, CAT); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6882 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6883 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6884 | } |
| 6885 | |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 6886 | bool VisitCallExpr(const CallExpr *E) { |
| 6887 | return handleCallExpr(E, Result, &This); |
| 6888 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6889 | bool VisitInitListExpr(const InitListExpr *E); |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6890 | bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 6891 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6892 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 6893 | const LValue &Subobject, |
| 6894 | APValue *Value, QualType Type); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6895 | }; |
| 6896 | } // end anonymous namespace |
| 6897 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6898 | static bool EvaluateArray(const Expr *E, const LValue &This, |
| 6899 | APValue &Result, EvalInfo &Info) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 6900 | assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6901 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6902 | } |
| 6903 | |
Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 6904 | // Return true iff the given array filler may depend on the element index. |
| 6905 | static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { |
| 6906 | // For now, just whitelist non-class value-initialization and initialization |
| 6907 | // lists comprised of them. |
| 6908 | if (isa<ImplicitValueInitExpr>(FillerExpr)) |
| 6909 | return false; |
| 6910 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(FillerExpr)) { |
| 6911 | for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { |
| 6912 | if (MaybeElementDependentArrayFiller(ILE->getInit(I))) |
| 6913 | return true; |
| 6914 | } |
| 6915 | return false; |
| 6916 | } |
| 6917 | return true; |
| 6918 | } |
| 6919 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6920 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 6921 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
| 6922 | if (!CAT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 6923 | return Error(E); |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6924 | |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 6925 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
| 6926 | // an appropriately-typed string literal enclosed in braces. |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 6927 | if (E->isStringLiteralInit()) { |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 6928 | LValue LV; |
| 6929 | if (!EvaluateLValue(E->getInit(0), LV, Info)) |
| 6930 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 6931 | APValue Val; |
Richard Smith | 14a9413 | 2012-02-17 03:35:37 +0000 | [diff] [blame] | 6932 | LV.moveInto(Val); |
| 6933 | return Success(Val, E); |
Richard Smith | ca2cfbf | 2011-12-22 01:07:19 +0000 | [diff] [blame] | 6934 | } |
| 6935 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6936 | bool Success = true; |
| 6937 | |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 6938 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
| 6939 | "zero-initialized array shouldn't have any initialized elts"); |
| 6940 | APValue Filler; |
| 6941 | if (Result.isArray() && Result.hasArrayFiller()) |
| 6942 | Filler = Result.getArrayFiller(); |
| 6943 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6944 | unsigned NumEltsToInit = E->getNumInits(); |
| 6945 | unsigned NumElts = CAT->getSize().getZExtValue(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6946 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : nullptr; |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6947 | |
| 6948 | // 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] | 6949 | // array element. |
| 6950 | if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr)) |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6951 | NumEltsToInit = NumElts; |
| 6952 | |
Ivan A. Kosarev | 01df519 | 2018-02-14 13:10:35 +0000 | [diff] [blame] | 6953 | DEBUG(llvm::dbgs() << "The number of elements to initialize: " << |
| 6954 | NumEltsToInit << ".\n"); |
| 6955 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6956 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 6957 | |
| 6958 | // If the array was previously zero-initialized, preserve the |
| 6959 | // zero-initialized values. |
| 6960 | if (!Filler.isUninit()) { |
| 6961 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
| 6962 | Result.getArrayInitializedElt(I) = Filler; |
| 6963 | if (Result.hasArrayFiller()) |
| 6964 | Result.getArrayFiller() = Filler; |
| 6965 | } |
| 6966 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6967 | LValue Subobject = This; |
Richard Smith | a8105bc | 2012-01-06 16:39:00 +0000 | [diff] [blame] | 6968 | Subobject.addArray(Info, E, CAT); |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6969 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
| 6970 | const Expr *Init = |
| 6971 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 6972 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6973 | Info, Subobject, Init) || |
| 6974 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6975 | CAT->getElementType(), 1)) { |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 6976 | if (!Info.noteFailure()) |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 6977 | return false; |
| 6978 | Success = false; |
| 6979 | } |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 6980 | } |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6981 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 6982 | if (!Result.hasArrayFiller()) |
| 6983 | return Success; |
| 6984 | |
| 6985 | // If we get here, we have a trivial filler, which we can just evaluate |
| 6986 | // once and splat over the rest of the array elements. |
| 6987 | assert(FillerExpr && "no array filler for incomplete init list"); |
| 6988 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
| 6989 | FillerExpr) && Success; |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 6990 | } |
| 6991 | |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 6992 | bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
| 6993 | if (E->getCommonExpr() && |
| 6994 | !Evaluate(Info.CurrentCall->createTemporary(E->getCommonExpr(), false), |
| 6995 | Info, E->getCommonExpr()->getSourceExpr())) |
| 6996 | return false; |
| 6997 | |
| 6998 | auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe()); |
| 6999 | |
| 7000 | uint64_t Elements = CAT->getSize().getZExtValue(); |
| 7001 | Result = APValue(APValue::UninitArray(), Elements, Elements); |
| 7002 | |
| 7003 | LValue Subobject = This; |
| 7004 | Subobject.addArray(Info, E, CAT); |
| 7005 | |
| 7006 | bool Success = true; |
| 7007 | for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) { |
| 7008 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
| 7009 | Info, Subobject, E->getSubExpr()) || |
| 7010 | !HandleLValueArrayAdjustment(Info, E, Subobject, |
| 7011 | CAT->getElementType(), 1)) { |
| 7012 | if (!Info.noteFailure()) |
| 7013 | return false; |
| 7014 | Success = false; |
| 7015 | } |
| 7016 | } |
| 7017 | |
| 7018 | return Success; |
| 7019 | } |
| 7020 | |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7021 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7022 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
| 7023 | } |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 7024 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7025 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
| 7026 | const LValue &Subobject, |
| 7027 | APValue *Value, |
| 7028 | QualType Type) { |
| 7029 | bool HadZeroInit = !Value->isUninit(); |
| 7030 | |
| 7031 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
| 7032 | unsigned N = CAT->getSize().getZExtValue(); |
| 7033 | |
| 7034 | // Preserve the array filler if we had prior zero-initialization. |
| 7035 | APValue Filler = |
| 7036 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
| 7037 | : APValue(); |
| 7038 | |
| 7039 | *Value = APValue(APValue::UninitArray(), N, N); |
| 7040 | |
| 7041 | if (HadZeroInit) |
| 7042 | for (unsigned I = 0; I != N; ++I) |
| 7043 | Value->getArrayInitializedElt(I) = Filler; |
| 7044 | |
| 7045 | // Initialize the elements. |
| 7046 | LValue ArrayElt = Subobject; |
| 7047 | ArrayElt.addArray(Info, E, CAT); |
| 7048 | for (unsigned I = 0; I != N; ++I) |
| 7049 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
| 7050 | CAT->getElementType()) || |
| 7051 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
| 7052 | CAT->getElementType(), 1)) |
| 7053 | return false; |
| 7054 | |
| 7055 | return true; |
Richard Smith | 1b9f2eb | 2012-07-07 22:48:24 +0000 | [diff] [blame] | 7056 | } |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7057 | |
Richard Smith | 9543c5e | 2013-04-22 14:44:29 +0000 | [diff] [blame] | 7058 | if (!Type->isRecordType()) |
Richard Smith | 9fce7bc | 2012-07-10 22:12:55 +0000 | [diff] [blame] | 7059 | return Error(E); |
| 7060 | |
Richard Smith | b8348f5 | 2016-05-12 22:16:28 +0000 | [diff] [blame] | 7061 | return RecordExprEvaluator(Info, Subobject, *Value) |
| 7062 | .VisitCXXConstructExpr(E, Type); |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 7063 | } |
| 7064 | |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 7065 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7066 | // Integer Evaluation |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7067 | // |
| 7068 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
| 7069 | // types and back in constant folding. Integer values are thus represented |
| 7070 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7071 | //===----------------------------------------------------------------------===// |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7072 | |
| 7073 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 7074 | class IntExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 7075 | : public ExprEvaluatorBase<IntExprEvaluator> { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7076 | APValue &Result; |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 7077 | public: |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7078 | IntExprEvaluator(EvalInfo &info, APValue &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7079 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7080 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7081 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7082 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 7083 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7084 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7085 | "Invalid evaluation result."); |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7086 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7087 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7088 | Result = APValue(SI); |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7089 | return true; |
| 7090 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7091 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
| 7092 | return Success(SI, E, Result); |
| 7093 | } |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7094 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7095 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 7096 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 7097 | "Invalid evaluation result."); |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7098 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
Daniel Dunbar | e3c92bc | 2009-02-19 18:37:50 +0000 | [diff] [blame] | 7099 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7100 | Result = APValue(APSInt(I)); |
Douglas Gregor | 6ab2fa8 | 2011-05-20 16:38:50 +0000 | [diff] [blame] | 7101 | Result.getInt().setIsUnsigned( |
| 7102 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7103 | return true; |
| 7104 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7105 | bool Success(const llvm::APInt &I, const Expr *E) { |
| 7106 | return Success(I, E, Result); |
| 7107 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7108 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7109 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 7110 | assert(E->getType()->isIntegralOrEnumerationType() && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 7111 | "Invalid evaluation result."); |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7112 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7113 | return true; |
| 7114 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 7115 | bool Success(uint64_t Value, const Expr *E) { |
| 7116 | return Success(Value, E, Result); |
| 7117 | } |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7118 | |
Ken Dyck | dbc0191 | 2011-03-11 02:13:43 +0000 | [diff] [blame] | 7119 | bool Success(CharUnits Size, const Expr *E) { |
| 7120 | return Success(Size.getQuantity(), E); |
| 7121 | } |
| 7122 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7123 | bool Success(const APValue &V, const Expr *E) { |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 7124 | if (V.isLValue() || V.isAddrLabelDiff()) { |
Richard Smith | 9c8d1c5 | 2011-10-29 22:55:55 +0000 | [diff] [blame] | 7125 | Result = V; |
| 7126 | return true; |
| 7127 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7128 | return Success(V.getInt(), E); |
Chris Lattner | fac05ae | 2008-11-12 07:43:42 +0000 | [diff] [blame] | 7129 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7130 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7131 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7132 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7133 | //===--------------------------------------------------------------------===// |
| 7134 | // Visitor Methods |
| 7135 | //===--------------------------------------------------------------------===// |
Anders Carlsson | 0a1707c | 2008-07-08 05:13:58 +0000 | [diff] [blame] | 7136 | |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7137 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7138 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7139 | } |
| 7140 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7141 | return Success(E->getValue(), E); |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7142 | } |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7143 | |
| 7144 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
| 7145 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7146 | if (CheckReferencedDecl(E, E->getDecl())) |
| 7147 | return true; |
| 7148 | |
| 7149 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7150 | } |
| 7151 | bool VisitMemberExpr(const MemberExpr *E) { |
| 7152 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
David Majnemer | e9807b2 | 2016-02-26 04:23:19 +0000 | [diff] [blame] | 7153 | VisitIgnoredBaseExpression(E->getBase()); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7154 | return true; |
| 7155 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7156 | |
| 7157 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7158 | } |
| 7159 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7160 | bool VisitCallExpr(const CallExpr *E); |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 7161 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7162 | bool VisitBinaryOperator(const BinaryOperator *E); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7163 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 7164 | bool VisitUnaryOperator(const UnaryOperator *E); |
Anders Carlsson | 374b93d | 2008-07-08 05:49:43 +0000 | [diff] [blame] | 7165 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7166 | bool VisitCastExpr(const CastExpr* E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 7167 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 7168 | |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7169 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 7170 | return Success(E->getValue(), E); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 7171 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7172 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 7173 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
| 7174 | return Success(E->getValue(), E); |
| 7175 | } |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 7176 | |
| 7177 | bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
| 7178 | if (Info.ArrayInitIndex == uint64_t(-1)) { |
| 7179 | // We were asked to evaluate this subexpression independent of the |
| 7180 | // enclosing ArrayInitLoopExpr. We can't do that. |
| 7181 | Info.FFDiag(E); |
| 7182 | return false; |
| 7183 | } |
| 7184 | return Success(Info.ArrayInitIndex, E); |
| 7185 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 7186 | |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 7187 | // Note, GNU defines __null as an integer, not a pointer. |
Anders Carlsson | 39def3a | 2008-12-21 22:39:40 +0000 | [diff] [blame] | 7188 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 7189 | return ZeroInitialization(E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7190 | } |
| 7191 | |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 7192 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
| 7193 | return Success(E->getValue(), E); |
| 7194 | } |
| 7195 | |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 7196 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
| 7197 | return Success(E->getValue(), E); |
| 7198 | } |
| 7199 | |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 7200 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
| 7201 | return Success(E->getValue(), E); |
| 7202 | } |
| 7203 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 7204 | bool VisitUnaryReal(const UnaryOperator *E); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7205 | bool VisitUnaryImag(const UnaryOperator *E); |
| 7206 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 7207 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 7208 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 7209 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 7210 | // FIXME: Missing: array subscript of vector, member of vector |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 7211 | }; |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 7212 | } // end anonymous namespace |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7213 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7214 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
| 7215 | /// produce either the integer value or a pointer. |
| 7216 | /// |
| 7217 | /// GCC has a heinous extension which folds casts between pointer types and |
| 7218 | /// pointer-sized integral types. We support this by allowing the evaluation of |
| 7219 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
| 7220 | /// Some simple arithmetic on such values is supported (they are treated much |
| 7221 | /// like char*). |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7222 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
Richard Smith | 0b0a0b6 | 2011-10-29 20:57:55 +0000 | [diff] [blame] | 7223 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 7224 | assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7225 | return IntExprEvaluator(Info, Result).Visit(E); |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 7226 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7227 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7228 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 7229 | APValue Val; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7230 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
Daniel Dunbar | ce39954 | 2009-02-20 18:22:23 +0000 | [diff] [blame] | 7231 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7232 | if (!Val.isInt()) { |
| 7233 | // FIXME: It would be better to produce the diagnostic for casting |
| 7234 | // a pointer to an integer. |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 7235 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7236 | return false; |
| 7237 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 7238 | Result = Val.getInt(); |
| 7239 | return true; |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7240 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 7241 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 7242 | /// Check whether the given declaration can be directly converted to an integral |
| 7243 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
| 7244 | /// try. |
Eli Friedman | fb8a93f | 2009-11-24 05:28:59 +0000 | [diff] [blame] | 7245 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7246 | // Enums are integer constant exprs. |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 7247 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
Abramo Bagnara | 9ae292d | 2011-07-02 13:13:53 +0000 | [diff] [blame] | 7248 | // Check for signedness/width mismatches between E type and ECD value. |
| 7249 | bool SameSign = (ECD->getInitVal().isSigned() |
| 7250 | == E->getType()->isSignedIntegerOrEnumerationType()); |
| 7251 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
| 7252 | == Info.Ctx.getIntWidth(E->getType())); |
| 7253 | if (SameSign && SameWidth) |
| 7254 | return Success(ECD->getInitVal(), E); |
| 7255 | else { |
| 7256 | // Get rid of mismatch (otherwise Success assertions will fail) |
| 7257 | // by computing a new value matching the type of E. |
| 7258 | llvm::APSInt Val = ECD->getInitVal(); |
| 7259 | if (!SameSign) |
| 7260 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
| 7261 | if (!SameWidth) |
| 7262 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
| 7263 | return Success(Val, E); |
| 7264 | } |
Abramo Bagnara | 2caedf4 | 2011-06-30 09:36:05 +0000 | [diff] [blame] | 7265 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7266 | return false; |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 7267 | } |
| 7268 | |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7269 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
| 7270 | /// as GCC. |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7271 | static int EvaluateBuiltinClassifyType(const CallExpr *E, |
| 7272 | const LangOptions &LangOpts) { |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7273 | // The following enum mimics the values returned by GCC. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 7274 | // FIXME: Does GCC differ between lvalue and rvalue references here? |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7275 | enum gcc_type_class { |
| 7276 | no_type_class = -1, |
| 7277 | void_type_class, integer_type_class, char_type_class, |
| 7278 | enumeral_type_class, boolean_type_class, |
| 7279 | pointer_type_class, reference_type_class, offset_type_class, |
| 7280 | real_type_class, complex_type_class, |
| 7281 | function_type_class, method_type_class, |
| 7282 | record_type_class, union_type_class, |
| 7283 | array_type_class, string_type_class, |
| 7284 | lang_type_class |
| 7285 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7286 | |
| 7287 | // If no argument was supplied, default to "no_type_class". This isn't |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7288 | // ideal, however it is what gcc does. |
| 7289 | if (E->getNumArgs() == 0) |
| 7290 | return no_type_class; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7291 | |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7292 | QualType CanTy = E->getArg(0)->getType().getCanonicalType(); |
| 7293 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanTy); |
| 7294 | |
| 7295 | switch (CanTy->getTypeClass()) { |
| 7296 | #define TYPE(ID, BASE) |
| 7297 | #define DEPENDENT_TYPE(ID, BASE) case Type::ID: |
| 7298 | #define NON_CANONICAL_TYPE(ID, BASE) case Type::ID: |
| 7299 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID: |
| 7300 | #include "clang/AST/TypeNodes.def" |
| 7301 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 7302 | |
| 7303 | case Type::Builtin: |
| 7304 | switch (BT->getKind()) { |
| 7305 | #define BUILTIN_TYPE(ID, SINGLETON_ID) |
| 7306 | #define SIGNED_TYPE(ID, SINGLETON_ID) case BuiltinType::ID: return integer_type_class; |
| 7307 | #define FLOATING_TYPE(ID, SINGLETON_ID) case BuiltinType::ID: return real_type_class; |
| 7308 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) case BuiltinType::ID: break; |
| 7309 | #include "clang/AST/BuiltinTypes.def" |
| 7310 | case BuiltinType::Void: |
| 7311 | return void_type_class; |
| 7312 | |
| 7313 | case BuiltinType::Bool: |
| 7314 | return boolean_type_class; |
| 7315 | |
| 7316 | case BuiltinType::Char_U: // gcc doesn't appear to use char_type_class |
| 7317 | case BuiltinType::UChar: |
| 7318 | case BuiltinType::UShort: |
| 7319 | case BuiltinType::UInt: |
| 7320 | case BuiltinType::ULong: |
| 7321 | case BuiltinType::ULongLong: |
| 7322 | case BuiltinType::UInt128: |
| 7323 | return integer_type_class; |
| 7324 | |
| 7325 | case BuiltinType::NullPtr: |
| 7326 | return pointer_type_class; |
| 7327 | |
| 7328 | case BuiltinType::WChar_U: |
| 7329 | case BuiltinType::Char16: |
| 7330 | case BuiltinType::Char32: |
| 7331 | case BuiltinType::ObjCId: |
| 7332 | case BuiltinType::ObjCClass: |
| 7333 | case BuiltinType::ObjCSel: |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 7334 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 7335 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 7336 | #include "clang/Basic/OpenCLImageTypes.def" |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7337 | case BuiltinType::OCLSampler: |
| 7338 | case BuiltinType::OCLEvent: |
| 7339 | case BuiltinType::OCLClkEvent: |
| 7340 | case BuiltinType::OCLQueue: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7341 | case BuiltinType::OCLReserveID: |
| 7342 | case BuiltinType::Dependent: |
| 7343 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 7344 | }; |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 7345 | break; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7346 | |
| 7347 | case Type::Enum: |
| 7348 | return LangOpts.CPlusPlus ? enumeral_type_class : integer_type_class; |
| 7349 | break; |
| 7350 | |
| 7351 | case Type::Pointer: |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7352 | return pointer_type_class; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7353 | break; |
| 7354 | |
| 7355 | case Type::MemberPointer: |
| 7356 | if (CanTy->isMemberDataPointerType()) |
| 7357 | return offset_type_class; |
| 7358 | else { |
| 7359 | // We expect member pointers to be either data or function pointers, |
| 7360 | // nothing else. |
| 7361 | assert(CanTy->isMemberFunctionPointerType()); |
| 7362 | return method_type_class; |
| 7363 | } |
| 7364 | |
| 7365 | case Type::Complex: |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7366 | return complex_type_class; |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7367 | |
| 7368 | case Type::FunctionNoProto: |
| 7369 | case Type::FunctionProto: |
| 7370 | return LangOpts.CPlusPlus ? function_type_class : pointer_type_class; |
| 7371 | |
| 7372 | case Type::Record: |
| 7373 | if (const RecordType *RT = CanTy->getAs<RecordType>()) { |
| 7374 | switch (RT->getDecl()->getTagKind()) { |
| 7375 | case TagTypeKind::TTK_Struct: |
| 7376 | case TagTypeKind::TTK_Class: |
| 7377 | case TagTypeKind::TTK_Interface: |
| 7378 | return record_type_class; |
| 7379 | |
| 7380 | case TagTypeKind::TTK_Enum: |
| 7381 | return LangOpts.CPlusPlus ? enumeral_type_class : integer_type_class; |
| 7382 | |
| 7383 | case TagTypeKind::TTK_Union: |
| 7384 | return union_type_class; |
| 7385 | } |
| 7386 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7387 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7388 | |
| 7389 | case Type::ConstantArray: |
| 7390 | case Type::VariableArray: |
| 7391 | case Type::IncompleteArray: |
| 7392 | return LangOpts.CPlusPlus ? array_type_class : pointer_type_class; |
| 7393 | |
| 7394 | case Type::BlockPointer: |
| 7395 | case Type::LValueReference: |
| 7396 | case Type::RValueReference: |
| 7397 | case Type::Vector: |
| 7398 | case Type::ExtVector: |
| 7399 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 7400 | case Type::DeducedTemplateSpecialization: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7401 | case Type::ObjCObject: |
| 7402 | case Type::ObjCInterface: |
| 7403 | case Type::ObjCObjectPointer: |
| 7404 | case Type::Pipe: |
| 7405 | case Type::Atomic: |
| 7406 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
| 7407 | } |
| 7408 | |
| 7409 | llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type"); |
Chris Lattner | 86ee286 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 7410 | } |
| 7411 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7412 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
| 7413 | /// __builtin_constant_p when applied to the given lvalue. |
| 7414 | /// |
| 7415 | /// An lvalue is only "constant" if it is a pointer or reference to the first |
| 7416 | /// character of a string literal. |
| 7417 | template<typename LValue> |
| 7418 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
Douglas Gregor | f31cee6 | 2012-03-11 02:23:56 +0000 | [diff] [blame] | 7419 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7420 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
| 7421 | } |
| 7422 | |
| 7423 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
| 7424 | /// GCC as we can manage. |
| 7425 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
| 7426 | QualType ArgType = Arg->getType(); |
| 7427 | |
| 7428 | // __builtin_constant_p always has one operand. The rules which gcc follows |
| 7429 | // are not precisely documented, but are as follows: |
| 7430 | // |
| 7431 | // - If the operand is of integral, floating, complex or enumeration type, |
| 7432 | // and can be folded to a known value of that type, it returns 1. |
| 7433 | // - If the operand and can be folded to a pointer to the first character |
| 7434 | // of a string literal (or such a pointer cast to an integral type), it |
| 7435 | // returns 1. |
| 7436 | // |
| 7437 | // Otherwise, it returns 0. |
| 7438 | // |
| 7439 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
| 7440 | // its support for this does not currently work. |
| 7441 | if (ArgType->isIntegralOrEnumerationType()) { |
| 7442 | Expr::EvalResult Result; |
| 7443 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
| 7444 | return false; |
| 7445 | |
| 7446 | APValue &V = Result.Val; |
| 7447 | if (V.getKind() == APValue::Int) |
| 7448 | return true; |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 7449 | if (V.getKind() == APValue::LValue) |
| 7450 | return EvaluateBuiltinConstantPForLValue(V); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7451 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
| 7452 | return Arg->isEvaluatable(Ctx); |
| 7453 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
| 7454 | LValue LV; |
| 7455 | Expr::EvalStatus Status; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 7456 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 7457 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
| 7458 | : EvaluatePointer(Arg, LV, Info)) && |
| 7459 | !Status.HasSideEffects) |
| 7460 | return EvaluateBuiltinConstantPForLValue(LV); |
| 7461 | } |
| 7462 | |
| 7463 | // Anything else isn't considered to be sufficiently constant. |
| 7464 | return false; |
| 7465 | } |
| 7466 | |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7467 | /// Retrieves the "underlying object type" of the given expression, |
| 7468 | /// as used by __builtin_object_size. |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7469 | static QualType getObjectType(APValue::LValueBase B) { |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7470 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
| 7471 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7472 | return VD->getType(); |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 7473 | } else if (const Expr *E = B.get<const Expr*>()) { |
| 7474 | if (isa<CompoundLiteralExpr>(E)) |
| 7475 | return E->getType(); |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7476 | } |
| 7477 | |
| 7478 | return QualType(); |
| 7479 | } |
| 7480 | |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7481 | /// A more selective version of E->IgnoreParenCasts for |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7482 | /// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only |
George Burgess IV | b40cd56 | 2015-09-04 22:36:18 +0000 | [diff] [blame] | 7483 | /// to change the type of E. |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7484 | /// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo` |
| 7485 | /// |
| 7486 | /// Always returns an RValue with a pointer representation. |
| 7487 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
| 7488 | assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
| 7489 | |
| 7490 | auto *NoParens = E->IgnoreParens(); |
| 7491 | auto *Cast = dyn_cast<CastExpr>(NoParens); |
George Burgess IV | b40cd56 | 2015-09-04 22:36:18 +0000 | [diff] [blame] | 7492 | if (Cast == nullptr) |
| 7493 | return NoParens; |
| 7494 | |
| 7495 | // We only conservatively allow a few kinds of casts, because this code is |
| 7496 | // inherently a simple solution that seeks to support the common case. |
| 7497 | auto CastKind = Cast->getCastKind(); |
| 7498 | if (CastKind != CK_NoOp && CastKind != CK_BitCast && |
| 7499 | CastKind != CK_AddressSpaceConversion) |
George Burgess IV | 3a03fab | 2015-09-04 21:28:13 +0000 | [diff] [blame] | 7500 | return NoParens; |
| 7501 | |
| 7502 | auto *SubExpr = Cast->getSubExpr(); |
| 7503 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isRValue()) |
| 7504 | return NoParens; |
| 7505 | return ignorePointerCastsAndParens(SubExpr); |
| 7506 | } |
| 7507 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7508 | /// Checks to see if the given LValue's Designator is at the end of the LValue's |
| 7509 | /// record layout. e.g. |
| 7510 | /// struct { struct { int a, b; } fst, snd; } obj; |
| 7511 | /// obj.fst // no |
| 7512 | /// obj.snd // yes |
| 7513 | /// obj.fst.a // no |
| 7514 | /// obj.fst.b // no |
| 7515 | /// obj.snd.a // no |
| 7516 | /// obj.snd.b // yes |
| 7517 | /// |
| 7518 | /// Please note: this function is specialized for how __builtin_object_size |
| 7519 | /// views "objects". |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7520 | /// |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 7521 | /// If this encounters an invalid RecordDecl or otherwise cannot determine the |
| 7522 | /// correct result, it will always return true. |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7523 | static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) { |
| 7524 | assert(!LVal.Designator.Invalid); |
| 7525 | |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7526 | auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD, bool &Invalid) { |
| 7527 | const RecordDecl *Parent = FD->getParent(); |
| 7528 | Invalid = Parent->isInvalidDecl(); |
| 7529 | if (Invalid || Parent->isUnion()) |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7530 | return true; |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7531 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Parent); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7532 | return FD->getFieldIndex() + 1 == Layout.getFieldCount(); |
| 7533 | }; |
| 7534 | |
| 7535 | auto &Base = LVal.getLValueBase(); |
| 7536 | if (auto *ME = dyn_cast_or_null<MemberExpr>(Base.dyn_cast<const Expr *>())) { |
| 7537 | if (auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7538 | bool Invalid; |
| 7539 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
| 7540 | return Invalid; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7541 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(ME->getMemberDecl())) { |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7542 | for (auto *FD : IFD->chain()) { |
| 7543 | bool Invalid; |
| 7544 | if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(FD), Invalid)) |
| 7545 | return Invalid; |
| 7546 | } |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7547 | } |
| 7548 | } |
| 7549 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7550 | unsigned I = 0; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7551 | QualType BaseType = getType(Base); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 7552 | if (LVal.Designator.FirstEntryIsAnUnsizedArray) { |
Richard Smith | 6f4f0f1 | 2017-10-20 22:56:25 +0000 | [diff] [blame] | 7553 | // If we don't know the array bound, conservatively assume we're looking at |
| 7554 | // the final array element. |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7555 | ++I; |
Alex Lorenz | 4e24648 | 2017-12-20 21:03:38 +0000 | [diff] [blame] | 7556 | if (BaseType->isIncompleteArrayType()) |
| 7557 | BaseType = Ctx.getAsArrayType(BaseType)->getElementType(); |
| 7558 | else |
| 7559 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7560 | } |
| 7561 | |
| 7562 | for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) { |
| 7563 | const auto &Entry = LVal.Designator.Entries[I]; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7564 | if (BaseType->isArrayType()) { |
| 7565 | // Because __builtin_object_size treats arrays as objects, we can ignore |
| 7566 | // the index iff this is the last array in the Designator. |
| 7567 | if (I + 1 == E) |
| 7568 | return true; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7569 | const auto *CAT = cast<ConstantArrayType>(Ctx.getAsArrayType(BaseType)); |
| 7570 | uint64_t Index = Entry.ArrayIndex; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7571 | if (Index + 1 != CAT->getSize()) |
| 7572 | return false; |
| 7573 | BaseType = CAT->getElementType(); |
| 7574 | } else if (BaseType->isAnyComplexType()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7575 | const auto *CT = BaseType->castAs<ComplexType>(); |
| 7576 | uint64_t Index = Entry.ArrayIndex; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7577 | if (Index != 1) |
| 7578 | return false; |
| 7579 | BaseType = CT->getElementType(); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7580 | } else if (auto *FD = getAsField(Entry)) { |
George Burgess IV | 4168d75 | 2016-06-27 19:40:41 +0000 | [diff] [blame] | 7581 | bool Invalid; |
| 7582 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
| 7583 | return Invalid; |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7584 | BaseType = FD->getType(); |
| 7585 | } else { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7586 | assert(getAsBaseClass(Entry) && "Expecting cast to a base class"); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7587 | return false; |
| 7588 | } |
| 7589 | } |
| 7590 | return true; |
| 7591 | } |
| 7592 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7593 | /// Tests to see if the LValue has a user-specified designator (that isn't |
| 7594 | /// necessarily valid). Note that this always returns 'true' if the LValue has |
| 7595 | /// an unsized array as its first designator entry, because there's currently no |
| 7596 | /// way to tell if the user typed *foo or foo[0]. |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7597 | static bool refersToCompleteObject(const LValue &LVal) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7598 | if (LVal.Designator.Invalid) |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7599 | return false; |
| 7600 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7601 | if (!LVal.Designator.Entries.empty()) |
| 7602 | return LVal.Designator.isMostDerivedAnUnsizedArray(); |
| 7603 | |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7604 | if (!LVal.InvalidBase) |
| 7605 | return true; |
| 7606 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7607 | // If `E` is a MemberExpr, then the first part of the designator is hiding in |
| 7608 | // the LValueBase. |
| 7609 | const auto *E = LVal.Base.dyn_cast<const Expr *>(); |
| 7610 | return !E || !isa<MemberExpr>(E); |
George Burgess IV | a51c407 | 2015-10-16 01:49:01 +0000 | [diff] [blame] | 7611 | } |
| 7612 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7613 | /// Attempts to detect a user writing into a piece of memory that's impossible |
| 7614 | /// to figure out the size of by just using types. |
| 7615 | static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) { |
| 7616 | const SubobjectDesignator &Designator = LVal.Designator; |
| 7617 | // Notes: |
| 7618 | // - Users can only write off of the end when we have an invalid base. Invalid |
| 7619 | // bases imply we don't know where the memory came from. |
| 7620 | // - We used to be a bit more aggressive here; we'd only be conservative if |
| 7621 | // the array at the end was flexible, or if it had 0 or 1 elements. This |
| 7622 | // broke some common standard library extensions (PR30346), but was |
| 7623 | // otherwise seemingly fine. It may be useful to reintroduce this behavior |
| 7624 | // with some sort of whitelist. OTOH, it seems that GCC is always |
| 7625 | // conservative with the last element in structs (if it's an array), so our |
| 7626 | // current behavior is more compatible than a whitelisting approach would |
| 7627 | // be. |
| 7628 | return LVal.InvalidBase && |
| 7629 | Designator.Entries.size() == Designator.MostDerivedPathLength && |
| 7630 | Designator.MostDerivedIsArrayElement && |
| 7631 | isDesignatorAtObjectEnd(Ctx, LVal); |
| 7632 | } |
| 7633 | |
| 7634 | /// Converts the given APInt to CharUnits, assuming the APInt is unsigned. |
| 7635 | /// Fails if the conversion would cause loss of precision. |
| 7636 | static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int, |
| 7637 | CharUnits &Result) { |
| 7638 | auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max(); |
| 7639 | if (Int.ugt(CharUnitsMax)) |
| 7640 | return false; |
| 7641 | Result = CharUnits::fromQuantity(Int.getZExtValue()); |
| 7642 | return true; |
| 7643 | } |
| 7644 | |
| 7645 | /// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will |
| 7646 | /// determine how many bytes exist from the beginning of the object to either |
| 7647 | /// the end of the current subobject, or the end of the object itself, depending |
| 7648 | /// on what the LValue looks like + the value of Type. |
George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 7649 | /// |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7650 | /// If this returns false, the value of Result is undefined. |
| 7651 | static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc, |
| 7652 | unsigned Type, const LValue &LVal, |
| 7653 | CharUnits &EndOffset) { |
| 7654 | bool DetermineForCompleteObject = refersToCompleteObject(LVal); |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 7655 | |
George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 7656 | auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { |
| 7657 | if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType()) |
| 7658 | return false; |
| 7659 | return HandleSizeof(Info, ExprLoc, Ty, Result); |
| 7660 | }; |
| 7661 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7662 | // We want to evaluate the size of the entire object. This is a valid fallback |
| 7663 | // for when Type=1 and the designator is invalid, because we're asked for an |
| 7664 | // upper-bound. |
| 7665 | if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) { |
| 7666 | // Type=3 wants a lower bound, so we can't fall back to this. |
| 7667 | if (Type == 3 && !DetermineForCompleteObject) |
George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 7668 | return false; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7669 | |
| 7670 | llvm::APInt APEndOffset; |
| 7671 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 7672 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
| 7673 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
| 7674 | |
| 7675 | if (LVal.InvalidBase) |
| 7676 | return false; |
| 7677 | |
| 7678 | QualType BaseTy = getObjectType(LVal.getLValueBase()); |
George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 7679 | return CheckedHandleSizeof(BaseTy, EndOffset); |
George Burgess IV | a747027 | 2016-12-20 01:05:42 +0000 | [diff] [blame] | 7680 | } |
| 7681 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7682 | // We want to evaluate the size of a subobject. |
| 7683 | const SubobjectDesignator &Designator = LVal.Designator; |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 7684 | |
| 7685 | // The following is a moderately common idiom in C: |
| 7686 | // |
| 7687 | // struct Foo { int a; char c[1]; }; |
| 7688 | // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar)); |
| 7689 | // strcpy(&F->c[0], Bar); |
| 7690 | // |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7691 | // In order to not break too much legacy code, we need to support it. |
| 7692 | if (isUserWritingOffTheEnd(Info.Ctx, LVal)) { |
| 7693 | // If we can resolve this to an alloc_size call, we can hand that back, |
| 7694 | // because we know for certain how many bytes there are to write to. |
| 7695 | llvm::APInt APEndOffset; |
| 7696 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
| 7697 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
| 7698 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
| 7699 | |
| 7700 | // If we cannot determine the size of the initial allocation, then we can't |
| 7701 | // given an accurate upper-bound. However, we are still able to give |
| 7702 | // conservative lower-bounds for Type=3. |
| 7703 | if (Type == 1) |
| 7704 | return false; |
| 7705 | } |
| 7706 | |
| 7707 | CharUnits BytesPerElem; |
George Burgess IV | 7fb7e36 | 2017-01-03 23:35:19 +0000 | [diff] [blame] | 7708 | if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem)) |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 7709 | return false; |
| 7710 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7711 | // According to the GCC documentation, we want the size of the subobject |
| 7712 | // denoted by the pointer. But that's not quite right -- what we actually |
| 7713 | // want is the size of the immediately-enclosing array, if there is one. |
| 7714 | int64_t ElemsRemaining; |
| 7715 | if (Designator.MostDerivedIsArrayElement && |
| 7716 | Designator.Entries.size() == Designator.MostDerivedPathLength) { |
| 7717 | uint64_t ArraySize = Designator.getMostDerivedArraySize(); |
| 7718 | uint64_t ArrayIndex = Designator.Entries.back().ArrayIndex; |
| 7719 | ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex; |
| 7720 | } else { |
| 7721 | ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1; |
| 7722 | } |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 7723 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7724 | EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining; |
| 7725 | return true; |
Chandler Carruth | d7738fe | 2016-12-20 08:28:19 +0000 | [diff] [blame] | 7726 | } |
| 7727 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7728 | /// \brief Tries to evaluate the __builtin_object_size for @p E. If successful, |
| 7729 | /// returns true and stores the result in @p Size. |
| 7730 | /// |
| 7731 | /// If @p WasError is non-null, this will report whether the failure to evaluate |
| 7732 | /// is to be treated as an Error in IntExprEvaluator. |
| 7733 | static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, |
| 7734 | EvalInfo &Info, uint64_t &Size) { |
| 7735 | // Determine the denoted object. |
| 7736 | LValue LVal; |
| 7737 | { |
| 7738 | // The operand of __builtin_object_size is never evaluated for side-effects. |
| 7739 | // If there are any, but we can determine the pointed-to object anyway, then |
| 7740 | // ignore the side-effects. |
| 7741 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
| 7742 | FoldOffsetRAII Fold(Info); |
| 7743 | |
| 7744 | if (E->isGLValue()) { |
| 7745 | // It's possible for us to be given GLValues if we're called via |
| 7746 | // Expr::tryEvaluateObjectSize. |
| 7747 | APValue RVal; |
| 7748 | if (!EvaluateAsRValue(Info, E, RVal)) |
| 7749 | return false; |
| 7750 | LVal.setFrom(Info.Ctx, RVal); |
George Burgess IV | f9013bf | 2017-02-10 22:52:29 +0000 | [diff] [blame] | 7751 | } else if (!EvaluatePointer(ignorePointerCastsAndParens(E), LVal, Info, |
| 7752 | /*InvalidBaseOK=*/true)) |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7753 | return false; |
| 7754 | } |
| 7755 | |
| 7756 | // If we point to before the start of the object, there are no accessible |
| 7757 | // bytes. |
| 7758 | if (LVal.getLValueOffset().isNegative()) { |
| 7759 | Size = 0; |
| 7760 | return true; |
| 7761 | } |
| 7762 | |
| 7763 | CharUnits EndOffset; |
| 7764 | if (!determineEndOffset(Info, E->getExprLoc(), Type, LVal, EndOffset)) |
| 7765 | return false; |
| 7766 | |
| 7767 | // If we've fallen outside of the end offset, just pretend there's nothing to |
| 7768 | // write to/read from. |
| 7769 | if (EndOffset <= LVal.getLValueOffset()) |
| 7770 | Size = 0; |
| 7771 | else |
| 7772 | Size = (EndOffset - LVal.getLValueOffset()).getQuantity(); |
| 7773 | return true; |
John McCall | 9500760 | 2010-05-10 23:27:23 +0000 | [diff] [blame] | 7774 | } |
| 7775 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7776 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Richard Smith | 6328cbd | 2016-11-16 00:57:23 +0000 | [diff] [blame] | 7777 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
| 7778 | return VisitBuiltinCallExpr(E, BuiltinOp); |
| 7779 | |
| 7780 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 7781 | } |
| 7782 | |
| 7783 | bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
| 7784 | unsigned BuiltinOp) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 7785 | switch (unsigned BuiltinOp = E->getBuiltinCallee()) { |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 7786 | default: |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 7787 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 7788 | |
| 7789 | case Builtin::BI__builtin_object_size: { |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7790 | // The type was checked when we built the expression. |
| 7791 | unsigned Type = |
| 7792 | E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 7793 | assert(Type <= 3 && "unexpected type"); |
| 7794 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7795 | uint64_t Size; |
| 7796 | if (tryEvaluateBuiltinObjectSize(E->getArg(0), Type, Info, Size)) |
| 7797 | return Success(Size, E); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 7798 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 7799 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7800 | return Success((Type & 2) ? 0 : -1, E); |
Mike Stump | 876387b | 2009-10-27 22:09:17 +0000 | [diff] [blame] | 7801 | |
Richard Smith | 01ade17 | 2012-05-23 04:13:20 +0000 | [diff] [blame] | 7802 | // Expression had no side effects, but we couldn't statically determine the |
| 7803 | // size of the referenced object. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 7804 | switch (Info.EvalMode) { |
| 7805 | case EvalInfo::EM_ConstantExpression: |
| 7806 | case EvalInfo::EM_PotentialConstantExpression: |
| 7807 | case EvalInfo::EM_ConstantFold: |
| 7808 | case EvalInfo::EM_EvaluateForOverflow: |
| 7809 | case EvalInfo::EM_IgnoreSideEffects: |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 7810 | case EvalInfo::EM_OffsetFold: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7811 | // Leave it to IR generation. |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 7812 | return Error(E); |
| 7813 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
| 7814 | case EvalInfo::EM_PotentialConstantExpressionUnevaluated: |
George Burgess IV | bdb5b26 | 2015-08-19 02:19:07 +0000 | [diff] [blame] | 7815 | // Reduce it to a constant now. |
| 7816 | return Success((Type & 2) ? 0 : -1, E); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 7817 | } |
Richard Smith | cb2ba5a | 2016-07-18 22:37:35 +0000 | [diff] [blame] | 7818 | |
| 7819 | llvm_unreachable("unexpected EvalMode"); |
Mike Stump | 722cedf | 2009-10-26 18:35:08 +0000 | [diff] [blame] | 7820 | } |
| 7821 | |
Benjamin Kramer | a801f4a | 2012-10-06 14:42:22 +0000 | [diff] [blame] | 7822 | case Builtin::BI__builtin_bswap16: |
Richard Smith | 80ac9ef | 2012-09-28 20:20:52 +0000 | [diff] [blame] | 7823 | case Builtin::BI__builtin_bswap32: |
| 7824 | case Builtin::BI__builtin_bswap64: { |
| 7825 | APSInt Val; |
| 7826 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 7827 | return false; |
| 7828 | |
| 7829 | return Success(Val.byteSwap(), E); |
| 7830 | } |
| 7831 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7832 | case Builtin::BI__builtin_classify_type: |
Andrey Bokhanko | 5f6588e | 2016-02-15 10:39:04 +0000 | [diff] [blame] | 7833 | return Success(EvaluateBuiltinClassifyType(E, Info.getLangOpts()), E); |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7834 | |
| 7835 | // FIXME: BI__builtin_clrsb |
| 7836 | // FIXME: BI__builtin_clrsbl |
| 7837 | // FIXME: BI__builtin_clrsbll |
| 7838 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 7839 | case Builtin::BI__builtin_clz: |
| 7840 | case Builtin::BI__builtin_clzl: |
Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 7841 | case Builtin::BI__builtin_clzll: |
| 7842 | case Builtin::BI__builtin_clzs: { |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 7843 | APSInt Val; |
| 7844 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 7845 | return false; |
| 7846 | if (!Val) |
| 7847 | return Error(E); |
| 7848 | |
| 7849 | return Success(Val.countLeadingZeros(), E); |
| 7850 | } |
| 7851 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7852 | case Builtin::BI__builtin_constant_p: |
| 7853 | return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E); |
| 7854 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 7855 | case Builtin::BI__builtin_ctz: |
| 7856 | case Builtin::BI__builtin_ctzl: |
Anders Carlsson | 1a9fe3d | 2014-07-07 15:53:44 +0000 | [diff] [blame] | 7857 | case Builtin::BI__builtin_ctzll: |
| 7858 | case Builtin::BI__builtin_ctzs: { |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 7859 | APSInt Val; |
| 7860 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 7861 | return false; |
| 7862 | if (!Val) |
| 7863 | return Error(E); |
| 7864 | |
| 7865 | return Success(Val.countTrailingZeros(), E); |
| 7866 | } |
| 7867 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7868 | case Builtin::BI__builtin_eh_return_data_regno: { |
| 7869 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
| 7870 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
| 7871 | return Success(Operand, E); |
| 7872 | } |
| 7873 | |
| 7874 | case Builtin::BI__builtin_expect: |
| 7875 | return Visit(E->getArg(0)); |
| 7876 | |
| 7877 | case Builtin::BI__builtin_ffs: |
| 7878 | case Builtin::BI__builtin_ffsl: |
| 7879 | case Builtin::BI__builtin_ffsll: { |
| 7880 | APSInt Val; |
| 7881 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 7882 | return false; |
| 7883 | |
| 7884 | unsigned N = Val.countTrailingZeros(); |
| 7885 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
| 7886 | } |
| 7887 | |
| 7888 | case Builtin::BI__builtin_fpclassify: { |
| 7889 | APFloat Val(0.0); |
| 7890 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
| 7891 | return false; |
| 7892 | unsigned Arg; |
| 7893 | switch (Val.getCategory()) { |
| 7894 | case APFloat::fcNaN: Arg = 0; break; |
| 7895 | case APFloat::fcInfinity: Arg = 1; break; |
| 7896 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
| 7897 | case APFloat::fcZero: Arg = 4; break; |
| 7898 | } |
| 7899 | return Visit(E->getArg(Arg)); |
| 7900 | } |
| 7901 | |
| 7902 | case Builtin::BI__builtin_isinf_sign: { |
| 7903 | APFloat Val(0.0); |
Richard Smith | ab341c6 | 2013-06-13 06:31:13 +0000 | [diff] [blame] | 7904 | return EvaluateFloat(E->getArg(0), Val, Info) && |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7905 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
| 7906 | } |
| 7907 | |
Richard Smith | ea3019d | 2013-10-15 19:07:14 +0000 | [diff] [blame] | 7908 | case Builtin::BI__builtin_isinf: { |
| 7909 | APFloat Val(0.0); |
| 7910 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 7911 | Success(Val.isInfinity() ? 1 : 0, E); |
| 7912 | } |
| 7913 | |
| 7914 | case Builtin::BI__builtin_isfinite: { |
| 7915 | APFloat Val(0.0); |
| 7916 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 7917 | Success(Val.isFinite() ? 1 : 0, E); |
| 7918 | } |
| 7919 | |
| 7920 | case Builtin::BI__builtin_isnan: { |
| 7921 | APFloat Val(0.0); |
| 7922 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 7923 | Success(Val.isNaN() ? 1 : 0, E); |
| 7924 | } |
| 7925 | |
| 7926 | case Builtin::BI__builtin_isnormal: { |
| 7927 | APFloat Val(0.0); |
| 7928 | return EvaluateFloat(E->getArg(0), Val, Info) && |
| 7929 | Success(Val.isNormal() ? 1 : 0, E); |
| 7930 | } |
| 7931 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 7932 | case Builtin::BI__builtin_parity: |
| 7933 | case Builtin::BI__builtin_parityl: |
| 7934 | case Builtin::BI__builtin_parityll: { |
| 7935 | APSInt Val; |
| 7936 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 7937 | return false; |
| 7938 | |
| 7939 | return Success(Val.countPopulation() % 2, E); |
| 7940 | } |
| 7941 | |
Richard Smith | 80b3c8e | 2013-06-13 05:04:16 +0000 | [diff] [blame] | 7942 | case Builtin::BI__builtin_popcount: |
| 7943 | case Builtin::BI__builtin_popcountl: |
| 7944 | case Builtin::BI__builtin_popcountll: { |
| 7945 | APSInt Val; |
| 7946 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
| 7947 | return false; |
| 7948 | |
| 7949 | return Success(Val.countPopulation(), E); |
| 7950 | } |
| 7951 | |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 7952 | case Builtin::BIstrlen: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 7953 | case Builtin::BIwcslen: |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 7954 | // A call to strlen is not a constant expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 7955 | if (Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 7956 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 7957 | << /*isConstexpr*/0 << /*isConstructor*/0 |
| 7958 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
Richard Smith | 9cf080f | 2012-01-18 03:06:12 +0000 | [diff] [blame] | 7959 | else |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 7960 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 7961 | LLVM_FALLTHROUGH; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 7962 | case Builtin::BI__builtin_strlen: |
| 7963 | case Builtin::BI__builtin_wcslen: { |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 7964 | // As an extension, we support __builtin_strlen() as a constant expression, |
| 7965 | // and support folding strlen() to a constant. |
| 7966 | LValue String; |
| 7967 | if (!EvaluatePointer(E->getArg(0), String, Info)) |
| 7968 | return false; |
| 7969 | |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 7970 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
| 7971 | |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 7972 | // Fast path: if it's a string literal, search the string value. |
| 7973 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
| 7974 | String.getLValueBase().dyn_cast<const Expr *>())) { |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 7975 | // The string literal may have embedded null characters. Find the first |
| 7976 | // one and truncate there. |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 7977 | StringRef Str = S->getBytes(); |
| 7978 | int64_t Off = String.Offset.getQuantity(); |
| 7979 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 7980 | S->getCharByteWidth() == 1 && |
| 7981 | // FIXME: Add fast-path for wchar_t too. |
| 7982 | Info.Ctx.hasSameUnqualifiedType(CharTy, Info.Ctx.CharTy)) { |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 7983 | Str = Str.substr(Off); |
| 7984 | |
| 7985 | StringRef::size_type Pos = Str.find(0); |
| 7986 | if (Pos != StringRef::npos) |
| 7987 | Str = Str.substr(0, Pos); |
| 7988 | |
| 7989 | return Success(Str.size(), E); |
| 7990 | } |
| 7991 | |
| 7992 | // Fall through to slow path to issue appropriate diagnostic. |
Douglas Gregor | 6a6dac2 | 2010-09-10 06:27:15 +0000 | [diff] [blame] | 7993 | } |
Richard Smith | e6c19f2 | 2013-11-15 02:10:04 +0000 | [diff] [blame] | 7994 | |
| 7995 | // 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] | 7996 | for (uint64_t Strlen = 0; /**/; ++Strlen) { |
| 7997 | APValue Char; |
| 7998 | if (!handleLValueToRValueConversion(Info, E, CharTy, String, Char) || |
| 7999 | !Char.isInt()) |
| 8000 | return false; |
| 8001 | if (!Char.getInt()) |
| 8002 | return Success(Strlen, E); |
| 8003 | if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1)) |
| 8004 | return false; |
| 8005 | } |
| 8006 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8007 | |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8008 | case Builtin::BIstrcmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8009 | case Builtin::BIwcscmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8010 | case Builtin::BIstrncmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8011 | case Builtin::BIwcsncmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8012 | case Builtin::BImemcmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8013 | case Builtin::BIwmemcmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8014 | // A call to strlen is not a constant expression. |
| 8015 | if (Info.getLangOpts().CPlusPlus11) |
| 8016 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
| 8017 | << /*isConstexpr*/0 << /*isConstructor*/0 |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8018 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8019 | else |
| 8020 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 8021 | LLVM_FALLTHROUGH; |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8022 | case Builtin::BI__builtin_strcmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8023 | case Builtin::BI__builtin_wcscmp: |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8024 | case Builtin::BI__builtin_strncmp: |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8025 | case Builtin::BI__builtin_wcsncmp: |
| 8026 | case Builtin::BI__builtin_memcmp: |
| 8027 | case Builtin::BI__builtin_wmemcmp: { |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8028 | LValue String1, String2; |
| 8029 | if (!EvaluatePointer(E->getArg(0), String1, Info) || |
| 8030 | !EvaluatePointer(E->getArg(1), String2, Info)) |
| 8031 | return false; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8032 | |
| 8033 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
| 8034 | |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8035 | uint64_t MaxLength = uint64_t(-1); |
| 8036 | if (BuiltinOp != Builtin::BIstrcmp && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8037 | BuiltinOp != Builtin::BIwcscmp && |
| 8038 | BuiltinOp != Builtin::BI__builtin_strcmp && |
| 8039 | BuiltinOp != Builtin::BI__builtin_wcscmp) { |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8040 | APSInt N; |
| 8041 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
| 8042 | return false; |
| 8043 | MaxLength = N.getExtValue(); |
| 8044 | } |
| 8045 | bool StopAtNull = (BuiltinOp != Builtin::BImemcmp && |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8046 | BuiltinOp != Builtin::BIwmemcmp && |
| 8047 | BuiltinOp != Builtin::BI__builtin_memcmp && |
| 8048 | BuiltinOp != Builtin::BI__builtin_wmemcmp); |
Richard Smith | e151bab | 2016-11-11 23:43:35 +0000 | [diff] [blame] | 8049 | for (; MaxLength; --MaxLength) { |
| 8050 | APValue Char1, Char2; |
| 8051 | if (!handleLValueToRValueConversion(Info, E, CharTy, String1, Char1) || |
| 8052 | !handleLValueToRValueConversion(Info, E, CharTy, String2, Char2) || |
| 8053 | !Char1.isInt() || !Char2.isInt()) |
| 8054 | return false; |
| 8055 | if (Char1.getInt() != Char2.getInt()) |
| 8056 | return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E); |
| 8057 | if (StopAtNull && !Char1.getInt()) |
| 8058 | return Success(0, E); |
| 8059 | assert(!(StopAtNull && !Char2.getInt())); |
| 8060 | if (!HandleLValueArrayAdjustment(Info, E, String1, CharTy, 1) || |
| 8061 | !HandleLValueArrayAdjustment(Info, E, String2, CharTy, 1)) |
| 8062 | return false; |
| 8063 | } |
| 8064 | // We hit the strncmp / memcmp limit. |
| 8065 | return Success(0, E); |
| 8066 | } |
| 8067 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8068 | case Builtin::BI__atomic_always_lock_free: |
Richard Smith | b1e36c6 | 2012-04-11 17:55:32 +0000 | [diff] [blame] | 8069 | case Builtin::BI__atomic_is_lock_free: |
| 8070 | case Builtin::BI__c11_atomic_is_lock_free: { |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8071 | APSInt SizeVal; |
| 8072 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
| 8073 | return false; |
| 8074 | |
| 8075 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
| 8076 | // of two less than the maximum inline atomic width, we know it is |
| 8077 | // lock-free. If the size isn't a power of two, or greater than the |
| 8078 | // maximum alignment where we promote atomics, we know it is not lock-free |
| 8079 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
| 8080 | // the answer can only be determined at runtime; for example, 16-byte |
| 8081 | // atomics have lock-free implementations on some, but not all, |
| 8082 | // x86-64 processors. |
| 8083 | |
| 8084 | // Check power-of-two. |
| 8085 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8086 | if (Size.isPowerOfTwo()) { |
| 8087 | // Check against inlining width. |
| 8088 | unsigned InlineWidthBits = |
| 8089 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
| 8090 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
| 8091 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
| 8092 | Size == CharUnits::One() || |
| 8093 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
| 8094 | Expr::NPC_NeverValueDependent)) |
| 8095 | // OK, we will inline appropriately-aligned operations of this size, |
| 8096 | // and _Atomic(T) is appropriately-aligned. |
| 8097 | return Success(1, E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8098 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8099 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
| 8100 | castAs<PointerType>()->getPointeeType(); |
| 8101 | if (!PointeeType->isIncompleteType() && |
| 8102 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
| 8103 | // OK, we will inline operations on this object. |
| 8104 | return Success(1, E); |
| 8105 | } |
| 8106 | } |
| 8107 | } |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8108 | |
Richard Smith | 01ba47d | 2012-04-13 00:45:38 +0000 | [diff] [blame] | 8109 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
| 8110 | Success(0, E) : Error(E); |
Eli Friedman | a4c2602 | 2011-10-17 21:44:23 +0000 | [diff] [blame] | 8111 | } |
Jonas Hahnfeld | 23604a8 | 2017-10-17 14:28:14 +0000 | [diff] [blame] | 8112 | case Builtin::BIomp_is_initial_device: |
| 8113 | // We can decide statically which value the runtime would return if called. |
| 8114 | return Success(Info.getLangOpts().OpenMPIsDevice ? 0 : 1, E); |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 8115 | } |
Chris Lattner | 7174bf3 | 2008-07-12 00:38:25 +0000 | [diff] [blame] | 8116 | } |
Anders Carlsson | 4a3585b | 2008-07-08 15:34:11 +0000 | [diff] [blame] | 8117 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8118 | static bool HasSameBase(const LValue &A, const LValue &B) { |
| 8119 | if (!A.getLValueBase()) |
| 8120 | return !B.getLValueBase(); |
| 8121 | if (!B.getLValueBase()) |
| 8122 | return false; |
| 8123 | |
Richard Smith | ce40ad6 | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 8124 | if (A.getLValueBase().getOpaqueValue() != |
| 8125 | B.getLValueBase().getOpaqueValue()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8126 | const Decl *ADecl = GetLValueBaseDecl(A); |
| 8127 | if (!ADecl) |
| 8128 | return false; |
| 8129 | const Decl *BDecl = GetLValueBaseDecl(B); |
Richard Smith | 8081560 | 2011-11-07 05:07:52 +0000 | [diff] [blame] | 8130 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8131 | return false; |
| 8132 | } |
| 8133 | |
| 8134 | return IsGlobalLValue(A.getLValueBase()) || |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 8135 | (A.getLValueCallIndex() == B.getLValueCallIndex() && |
| 8136 | A.getLValueVersion() == B.getLValueVersion()); |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 8139 | /// \brief Determine whether this is a pointer past the end of the complete |
| 8140 | /// object referred to by the lvalue. |
| 8141 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
| 8142 | const LValue &LV) { |
| 8143 | // A null pointer can be viewed as being "past the end" but we don't |
| 8144 | // choose to look at it that way here. |
| 8145 | if (!LV.getLValueBase()) |
| 8146 | return false; |
| 8147 | |
| 8148 | // If the designator is valid and refers to a subobject, we're not pointing |
| 8149 | // past the end. |
| 8150 | if (!LV.getLValueDesignator().Invalid && |
| 8151 | !LV.getLValueDesignator().isOnePastTheEnd()) |
| 8152 | return false; |
| 8153 | |
David Majnemer | c378ca5 | 2015-08-29 08:32:55 +0000 | [diff] [blame] | 8154 | // A pointer to an incomplete type might be past-the-end if the type's size is |
| 8155 | // zero. We cannot tell because the type is incomplete. |
| 8156 | QualType Ty = getType(LV.getLValueBase()); |
| 8157 | if (Ty->isIncompleteType()) |
| 8158 | return true; |
| 8159 | |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 8160 | // We're a past-the-end pointer if we point to the byte after the object, |
| 8161 | // no matter what our type or path is. |
David Majnemer | c378ca5 | 2015-08-29 08:32:55 +0000 | [diff] [blame] | 8162 | auto Size = Ctx.getTypeSizeInChars(Ty); |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 8163 | return LV.getLValueOffset() == Size; |
| 8164 | } |
| 8165 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8166 | namespace { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 8167 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8168 | /// \brief Data recursive integer evaluator of certain binary operators. |
| 8169 | /// |
| 8170 | /// We use a data recursive algorithm for binary operators so that we are able |
| 8171 | /// to handle extreme cases of chained binary operators without causing stack |
| 8172 | /// overflow. |
| 8173 | class DataRecursiveIntBinOpEvaluator { |
| 8174 | struct EvalResult { |
| 8175 | APValue Val; |
| 8176 | bool Failed; |
| 8177 | |
| 8178 | EvalResult() : Failed(false) { } |
| 8179 | |
| 8180 | void swap(EvalResult &RHS) { |
| 8181 | Val.swap(RHS.Val); |
| 8182 | Failed = RHS.Failed; |
| 8183 | RHS.Failed = false; |
| 8184 | } |
| 8185 | }; |
| 8186 | |
| 8187 | struct Job { |
| 8188 | const Expr *E; |
| 8189 | EvalResult LHSResult; // meaningful only for binary operator expression. |
| 8190 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 8191 | |
David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 8192 | Job() = default; |
Benjamin Kramer | 33e9760 | 2016-10-21 18:55:07 +0000 | [diff] [blame] | 8193 | Job(Job &&) = default; |
David Blaikie | 7372606 | 2015-08-12 23:09:24 +0000 | [diff] [blame] | 8194 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8195 | void startSpeculativeEval(EvalInfo &Info) { |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8196 | SpecEvalRAII = SpeculativeEvaluationRAII(Info); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8197 | } |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8198 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8199 | private: |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8200 | SpeculativeEvaluationRAII SpecEvalRAII; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8201 | }; |
| 8202 | |
| 8203 | SmallVector<Job, 16> Queue; |
| 8204 | |
| 8205 | IntExprEvaluator &IntEval; |
| 8206 | EvalInfo &Info; |
| 8207 | APValue &FinalResult; |
| 8208 | |
| 8209 | public: |
| 8210 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
| 8211 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
| 8212 | |
| 8213 | /// \brief True if \param E is a binary operator that we are going to handle |
| 8214 | /// data recursively. |
| 8215 | /// We handle binary operators that are comma, logical, or that have operands |
| 8216 | /// with integral or enumeration type. |
| 8217 | static bool shouldEnqueue(const BinaryOperator *E) { |
| 8218 | return E->getOpcode() == BO_Comma || |
| 8219 | E->isLogicalOp() || |
Richard Smith | 3a09d8b | 2016-06-04 00:22:31 +0000 | [diff] [blame] | 8220 | (E->isRValue() && |
| 8221 | E->getType()->isIntegralOrEnumerationType() && |
| 8222 | E->getLHS()->getType()->isIntegralOrEnumerationType() && |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8223 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 8224 | } |
| 8225 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8226 | bool Traverse(const BinaryOperator *E) { |
| 8227 | enqueue(E); |
| 8228 | EvalResult PrevResult; |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8229 | while (!Queue.empty()) |
| 8230 | process(PrevResult); |
| 8231 | |
| 8232 | if (PrevResult.Failed) return false; |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8233 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8234 | FinalResult.swap(PrevResult.Val); |
| 8235 | return true; |
| 8236 | } |
| 8237 | |
| 8238 | private: |
| 8239 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
| 8240 | return IntEval.Success(Value, E, Result); |
| 8241 | } |
| 8242 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
| 8243 | return IntEval.Success(Value, E, Result); |
| 8244 | } |
| 8245 | bool Error(const Expr *E) { |
| 8246 | return IntEval.Error(E); |
| 8247 | } |
| 8248 | bool Error(const Expr *E, diag::kind D) { |
| 8249 | return IntEval.Error(E, D); |
| 8250 | } |
| 8251 | |
| 8252 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
| 8253 | return Info.CCEDiag(E, D); |
| 8254 | } |
| 8255 | |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8256 | // \brief Returns true if visiting the RHS is necessary, false otherwise. |
| 8257 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8258 | bool &SuppressRHSDiags); |
| 8259 | |
| 8260 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 8261 | const BinaryOperator *E, APValue &Result); |
| 8262 | |
| 8263 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
| 8264 | Result.Failed = !Evaluate(Result.Val, Info, E); |
| 8265 | if (Result.Failed) |
| 8266 | Result.Val = APValue(); |
| 8267 | } |
| 8268 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8269 | void process(EvalResult &Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8270 | |
| 8271 | void enqueue(const Expr *E) { |
| 8272 | E = E->IgnoreParens(); |
| 8273 | Queue.resize(Queue.size()+1); |
| 8274 | Queue.back().E = E; |
| 8275 | Queue.back().Kind = Job::AnyExprKind; |
| 8276 | } |
| 8277 | }; |
| 8278 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 8279 | } |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8280 | |
| 8281 | bool DataRecursiveIntBinOpEvaluator:: |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8282 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8283 | bool &SuppressRHSDiags) { |
| 8284 | if (E->getOpcode() == BO_Comma) { |
| 8285 | // Ignore LHS but note if we could not evaluate it. |
| 8286 | if (LHSResult.Failed) |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8287 | return Info.noteSideEffect(); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8288 | return true; |
| 8289 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8290 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8291 | if (E->isLogicalOp()) { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8292 | bool LHSAsBool; |
| 8293 | if (!LHSResult.Failed && HandleConversionToBool(LHSResult.Val, LHSAsBool)) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8294 | // We were able to evaluate the LHS, see if we can get away with not |
| 8295 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8296 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
| 8297 | Success(LHSAsBool, E, LHSResult.Val); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8298 | return false; // Ignore RHS |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8299 | } |
| 8300 | } else { |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8301 | LHSResult.Failed = true; |
| 8302 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8303 | // 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] | 8304 | // might have had side effects. |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8305 | if (!Info.noteSideEffect()) |
| 8306 | return false; |
| 8307 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8308 | // We can't evaluate the LHS; however, sometimes the result |
| 8309 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 8310 | // Don't ignore RHS and suppress diagnostics from this arm. |
| 8311 | SuppressRHSDiags = true; |
| 8312 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8313 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8314 | return true; |
| 8315 | } |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8316 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8317 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 8318 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Richard Smith | 4e66f1f | 2013-11-06 02:19:10 +0000 | [diff] [blame] | 8319 | |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8320 | if (LHSResult.Failed && !Info.noteFailure()) |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8321 | return false; // Ignore RHS; |
| 8322 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8323 | return true; |
| 8324 | } |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8325 | |
Benjamin Kramer | f6021ec | 2017-03-21 21:35:04 +0000 | [diff] [blame] | 8326 | static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index, |
| 8327 | bool IsSub) { |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 8328 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
| 8329 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
| 8330 | // offsets. |
| 8331 | assert(!LVal.hasLValuePath() && "have designator for integer lvalue"); |
| 8332 | CharUnits &Offset = LVal.getLValueOffset(); |
| 8333 | uint64_t Offset64 = Offset.getQuantity(); |
| 8334 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
| 8335 | Offset = CharUnits::fromQuantity(IsSub ? Offset64 - Index64 |
| 8336 | : Offset64 + Index64); |
| 8337 | } |
| 8338 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8339 | bool DataRecursiveIntBinOpEvaluator:: |
| 8340 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
| 8341 | const BinaryOperator *E, APValue &Result) { |
| 8342 | if (E->getOpcode() == BO_Comma) { |
| 8343 | if (RHSResult.Failed) |
| 8344 | return false; |
| 8345 | Result = RHSResult.Val; |
| 8346 | return true; |
| 8347 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8348 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8349 | if (E->isLogicalOp()) { |
| 8350 | bool lhsResult, rhsResult; |
| 8351 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
| 8352 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8353 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8354 | if (LHSIsOK) { |
| 8355 | if (RHSIsOK) { |
| 8356 | if (E->getOpcode() == BO_LOr) |
| 8357 | return Success(lhsResult || rhsResult, E, Result); |
| 8358 | else |
| 8359 | return Success(lhsResult && rhsResult, E, Result); |
| 8360 | } |
| 8361 | } else { |
| 8362 | if (RHSIsOK) { |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8363 | // We can't evaluate the LHS; however, sometimes the result |
| 8364 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
| 8365 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8366 | return Success(rhsResult, E, Result); |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8367 | } |
| 8368 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8369 | |
Argyrios Kyrtzidis | 8d4677a | 2012-02-25 23:21:37 +0000 | [diff] [blame] | 8370 | return false; |
| 8371 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8372 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8373 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
| 8374 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8375 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8376 | if (LHSResult.Failed || RHSResult.Failed) |
| 8377 | return false; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8378 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8379 | const APValue &LHSVal = LHSResult.Val; |
| 8380 | const APValue &RHSVal = RHSResult.Val; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8381 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8382 | // Handle cases like (unsigned long)&a + 4. |
| 8383 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
| 8384 | Result = LHSVal; |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 8385 | addOrSubLValueAsInteger(Result, RHSVal.getInt(), E->getOpcode() == BO_Sub); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8386 | return true; |
| 8387 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8388 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8389 | // Handle cases like 4 + (unsigned long)&a |
| 8390 | if (E->getOpcode() == BO_Add && |
| 8391 | RHSVal.isLValue() && LHSVal.isInt()) { |
| 8392 | Result = RHSVal; |
Richard Smith | d6cc198 | 2017-01-31 02:23:02 +0000 | [diff] [blame] | 8393 | addOrSubLValueAsInteger(Result, LHSVal.getInt(), /*IsSub*/false); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8394 | return true; |
| 8395 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8396 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8397 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
| 8398 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
| 8399 | if (!LHSVal.getLValueOffset().isZero() || |
| 8400 | !RHSVal.getLValueOffset().isZero()) |
| 8401 | return false; |
| 8402 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 8403 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
| 8404 | if (!LHSExpr || !RHSExpr) |
| 8405 | return false; |
| 8406 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 8407 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 8408 | if (!LHSAddrExpr || !RHSAddrExpr) |
| 8409 | return false; |
| 8410 | // Make sure both labels come from the same function. |
| 8411 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 8412 | RHSAddrExpr->getLabel()->getDeclContext()) |
| 8413 | return false; |
| 8414 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
| 8415 | return true; |
| 8416 | } |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 8417 | |
| 8418 | // All the remaining cases expect both operands to be an integer |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8419 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
| 8420 | return Error(E); |
Richard Smith | 43e7773 | 2013-05-07 04:50:00 +0000 | [diff] [blame] | 8421 | |
| 8422 | // Set up the width and signedness manually, in case it can't be deduced |
| 8423 | // from the operation we're performing. |
| 8424 | // FIXME: Don't do this in the cases where we can deduce it. |
| 8425 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
| 8426 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
| 8427 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
| 8428 | RHSVal.getInt(), Value)) |
| 8429 | return false; |
| 8430 | return Success(Value, E, Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8431 | } |
| 8432 | |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8433 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8434 | Job &job = Queue.back(); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8435 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8436 | switch (job.Kind) { |
| 8437 | case Job::AnyExprKind: { |
| 8438 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
| 8439 | if (shouldEnqueue(Bop)) { |
| 8440 | job.Kind = Job::BinOpKind; |
| 8441 | enqueue(Bop->getLHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8442 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8443 | } |
| 8444 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8445 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8446 | EvaluateExpr(job.E, Result); |
| 8447 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8448 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8449 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8450 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8451 | case Job::BinOpKind: { |
| 8452 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8453 | bool SuppressRHSDiags = false; |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8454 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8455 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8456 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8457 | } |
| 8458 | if (SuppressRHSDiags) |
| 8459 | job.startSpeculativeEval(Info); |
Argyrios Kyrtzidis | 5957b70 | 2012-03-22 02:13:06 +0000 | [diff] [blame] | 8460 | job.LHSResult.swap(Result); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8461 | job.Kind = Job::BinOpVisitedLHSKind; |
| 8462 | enqueue(Bop->getRHS()); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8463 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8464 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8465 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8466 | case Job::BinOpVisitedLHSKind: { |
| 8467 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
| 8468 | EvalResult RHS; |
| 8469 | RHS.swap(Result); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8470 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8471 | Queue.pop_back(); |
Richard Trieu | ba4d087 | 2012-03-21 23:30:30 +0000 | [diff] [blame] | 8472 | return; |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8473 | } |
| 8474 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8475 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8476 | llvm_unreachable("Invalid Job::Kind!"); |
| 8477 | } |
| 8478 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8479 | namespace { |
| 8480 | /// Used when we determine that we should fail, but can keep evaluating prior to |
| 8481 | /// noting that we had a failure. |
| 8482 | class DelayedNoteFailureRAII { |
| 8483 | EvalInfo &Info; |
| 8484 | bool NoteFailure; |
| 8485 | |
| 8486 | public: |
| 8487 | DelayedNoteFailureRAII(EvalInfo &Info, bool NoteFailure = true) |
| 8488 | : Info(Info), NoteFailure(NoteFailure) {} |
| 8489 | ~DelayedNoteFailureRAII() { |
| 8490 | if (NoteFailure) { |
| 8491 | bool ContinueAfterFailure = Info.noteFailure(); |
| 8492 | (void)ContinueAfterFailure; |
| 8493 | assert(ContinueAfterFailure && |
| 8494 | "Shouldn't have kept evaluating on failure."); |
| 8495 | } |
| 8496 | } |
| 8497 | }; |
| 8498 | } |
| 8499 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8500 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8501 | // We don't call noteFailure immediately because the assignment happens after |
| 8502 | // we evaluate LHS and RHS. |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 8503 | if (!Info.keepEvaluatingAfterFailure() && E->isAssignmentOp()) |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8504 | return Error(E); |
| 8505 | |
George Burgess IV | 8c892b5 | 2016-05-25 22:31:54 +0000 | [diff] [blame] | 8506 | DelayedNoteFailureRAII MaybeNoteFailureLater(Info, E->isAssignmentOp()); |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8507 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
| 8508 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 8509 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8510 | QualType LHSTy = E->getLHS()->getType(); |
| 8511 | QualType RHSTy = E->getRHS()->getType(); |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8512 | |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 8513 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 8514 | ComplexValue LHS, RHS; |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 8515 | bool LHSOK; |
Josh Magee | 4d1a79b | 2015-02-04 21:50:20 +0000 | [diff] [blame] | 8516 | if (E->isAssignmentOp()) { |
| 8517 | LValue LV; |
| 8518 | EvaluateLValue(E->getLHS(), LV, Info); |
| 8519 | LHSOK = false; |
| 8520 | } else if (LHSTy->isRealFloatingType()) { |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 8521 | LHSOK = EvaluateFloat(E->getLHS(), LHS.FloatReal, Info); |
| 8522 | if (LHSOK) { |
| 8523 | LHS.makeComplexFloat(); |
| 8524 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
| 8525 | } |
| 8526 | } else { |
| 8527 | LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
| 8528 | } |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8529 | if (!LHSOK && !Info.noteFailure()) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8530 | return false; |
| 8531 | |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 8532 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 8533 | if (!EvaluateFloat(E->getRHS(), RHS.FloatReal, Info) || !LHSOK) |
| 8534 | return false; |
| 8535 | RHS.makeComplexFloat(); |
| 8536 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
| 8537 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8538 | return false; |
| 8539 | |
| 8540 | if (LHS.isComplexFloat()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8541 | APFloat::cmpResult CR_r = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8542 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8543 | APFloat::cmpResult CR_i = |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8544 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
| 8545 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8546 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 8547 | return Success((CR_r == APFloat::cmpEqual && |
| 8548 | CR_i == APFloat::cmpEqual), E); |
| 8549 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8550 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 8551 | "Invalid complex comparison."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8552 | return Success(((CR_r == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 8553 | CR_r == APFloat::cmpLessThan || |
| 8554 | CR_r == APFloat::cmpUnordered) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8555 | (CR_i == APFloat::cmpGreaterThan || |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 8556 | CR_i == APFloat::cmpLessThan || |
| 8557 | CR_i == APFloat::cmpUnordered)), E); |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 8558 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8559 | } else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8560 | if (E->getOpcode() == BO_EQ) |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 8561 | return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
| 8562 | LHS.getComplexIntImag() == RHS.getComplexIntImag()), E); |
| 8563 | else { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8564 | assert(E->getOpcode() == BO_NE && |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 8565 | "Invalid compex comparison."); |
| 8566 | return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() || |
| 8567 | LHS.getComplexIntImag() != RHS.getComplexIntImag()), E); |
| 8568 | } |
Daniel Dunbar | 74f2425b | 2009-01-29 06:43:41 +0000 | [diff] [blame] | 8569 | } |
| 8570 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8571 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8572 | if (LHSTy->isRealFloatingType() && |
| 8573 | RHSTy->isRealFloatingType()) { |
| 8574 | APFloat RHS(0.0), LHS(0.0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8575 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8576 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8577 | if (!LHSOK && !Info.noteFailure()) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8578 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8579 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8580 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8581 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8582 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8583 | APFloat::cmpResult CR = LHS.compare(RHS); |
Anders Carlsson | 899c705 | 2008-11-16 22:46:56 +0000 | [diff] [blame] | 8584 | |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8585 | switch (E->getOpcode()) { |
| 8586 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8587 | llvm_unreachable("Invalid binary operator!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8588 | case BO_LT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 8589 | return Success(CR == APFloat::cmpLessThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8590 | case BO_GT: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 8591 | return Success(CR == APFloat::cmpGreaterThan, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8592 | case BO_LE: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 8593 | return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8594 | case BO_GE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8595 | return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual, |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 8596 | E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8597 | case BO_EQ: |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 8598 | return Success(CR == APFloat::cmpEqual, E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8599 | case BO_NE: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8600 | return Success(CR == APFloat::cmpGreaterThan |
Mon P Wang | 75c645c | 2010-04-29 05:53:29 +0000 | [diff] [blame] | 8601 | || CR == APFloat::cmpLessThan |
| 8602 | || CR == APFloat::cmpUnordered, E); |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8603 | } |
Anders Carlsson | acc7981 | 2008-11-16 07:17:21 +0000 | [diff] [blame] | 8604 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8605 | |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 8606 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8607 | if (E->getOpcode() == BO_Sub || E->isComparisonOp()) { |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8608 | LValue LHSValue, RHSValue; |
| 8609 | |
| 8610 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8611 | if (!LHSOK && !Info.noteFailure()) |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 8612 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 8613 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 8614 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 8615 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 8616 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8617 | // Reject differing bases from the normal codepath; we special-case |
| 8618 | // comparisons to null. |
| 8619 | if (!HasSameBase(LHSValue, RHSValue)) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 8620 | if (E->getOpcode() == BO_Sub) { |
| 8621 | // Handle &&A - &&B. |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 8622 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 8623 | return Error(E); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 8624 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>(); |
Benjamin Kramer | daa09612 | 2012-10-03 14:15:39 +0000 | [diff] [blame] | 8625 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr*>(); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 8626 | if (!LHSExpr || !RHSExpr) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 8627 | return Error(E); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 8628 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
| 8629 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
| 8630 | if (!LHSAddrExpr || !RHSAddrExpr) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 8631 | return Error(E); |
Eli Friedman | b1bc368 | 2012-01-05 23:59:40 +0000 | [diff] [blame] | 8632 | // Make sure both labels come from the same function. |
| 8633 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
| 8634 | RHSAddrExpr->getLabel()->getDeclContext()) |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 8635 | return Error(E); |
| 8636 | return Success(APValue(LHSAddrExpr, RHSAddrExpr), E); |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 8637 | } |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 8638 | // Inequalities and subtractions between unrelated pointers have |
| 8639 | // unspecified or undefined behavior. |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 8640 | if (!E->isEqualityOp()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8641 | return Error(E); |
Eli Friedman | c6be94b | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 8642 | // A constant address may compare equal to the address of a symbol. |
| 8643 | // The one exception is that address of an object cannot compare equal |
Eli Friedman | 42fbd62 | 2011-10-31 22:54:30 +0000 | [diff] [blame] | 8644 | // to a null pointer constant. |
Eli Friedman | c6be94b | 2011-10-31 22:28:05 +0000 | [diff] [blame] | 8645 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
| 8646 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8647 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 8648 | // It's implementation-defined whether distinct literals will have |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 8649 | // distinct addresses. In clang, the result of such a comparison is |
| 8650 | // unspecified, so it is not a constant expression. However, we do know |
| 8651 | // that the address of a literal will be non-null. |
Richard Smith | e9e20dd3 | 2011-11-04 01:10:57 +0000 | [diff] [blame] | 8652 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
| 8653 | LHSValue.Base && RHSValue.Base) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8654 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 8655 | // We can't tell whether weak symbols will end up pointing to the same |
| 8656 | // object. |
| 8657 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8658 | return Error(E); |
Richard Smith | d20f1e6 | 2014-10-21 23:01:04 +0000 | [diff] [blame] | 8659 | // We can't compare the address of the start of one object with the |
| 8660 | // past-the-end address of another object, per C++ DR1652. |
| 8661 | if ((LHSValue.Base && LHSValue.Offset.isZero() && |
| 8662 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) || |
| 8663 | (RHSValue.Base && RHSValue.Offset.isZero() && |
| 8664 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue))) |
| 8665 | return Error(E); |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 8666 | // We can't tell whether an object is at the same address as another |
| 8667 | // zero sized object. |
David Majnemer | 27db358 | 2014-12-11 19:36:24 +0000 | [diff] [blame] | 8668 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
| 8669 | (LHSValue.Base && isZeroSized(RHSValue))) |
David Majnemer | b511603 | 2014-12-09 23:32:34 +0000 | [diff] [blame] | 8670 | return Error(E); |
Richard Smith | 83c6821 | 2011-10-31 05:11:32 +0000 | [diff] [blame] | 8671 | // Pointers with different bases cannot represent the same object. |
| 8672 | return Success(E->getOpcode() == BO_NE, E); |
Eli Friedman | 334046a | 2009-06-14 02:17:33 +0000 | [diff] [blame] | 8673 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 8674 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 8675 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
| 8676 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
| 8677 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 8678 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
| 8679 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
| 8680 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8681 | if (E->getOpcode() == BO_Sub) { |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 8682 | // C++11 [expr.add]p6: |
| 8683 | // Unless both pointers point to elements of the same array object, or |
| 8684 | // one past the last element of the array object, the behavior is |
| 8685 | // undefined. |
| 8686 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 8687 | !AreElementsOfSameArray(getType(LHSValue.Base), |
| 8688 | LHSDesignator, RHSDesignator)) |
| 8689 | CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
| 8690 | |
Chris Lattner | 882bdf2 | 2010-04-20 17:13:14 +0000 | [diff] [blame] | 8691 | QualType Type = E->getLHS()->getType(); |
| 8692 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 8693 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8694 | CharUnits ElementSize; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 8695 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8696 | return false; |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 8697 | |
Richard Smith | 84c6b3d | 2013-09-10 21:34:14 +0000 | [diff] [blame] | 8698 | // As an extension, a type may have zero size (empty struct or union in |
| 8699 | // C, array of zero length). Pointer subtraction in such cases has |
| 8700 | // undefined behavior, so is not constant. |
| 8701 | if (ElementSize.isZero()) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 8702 | Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
Richard Smith | 84c6b3d | 2013-09-10 21:34:14 +0000 | [diff] [blame] | 8703 | << ElementType; |
| 8704 | return false; |
| 8705 | } |
| 8706 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 8707 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
| 8708 | // and produce incorrect results when it overflows. Such behavior |
| 8709 | // appears to be non-conforming, but is common, so perhaps we should |
| 8710 | // assume the standard intended for such cases to be undefined behavior |
| 8711 | // and check for them. |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8712 | |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 8713 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
| 8714 | // overflow in the final conversion to ptrdiff_t. |
| 8715 | APSInt LHS( |
| 8716 | llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
| 8717 | APSInt RHS( |
| 8718 | llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
| 8719 | APSInt ElemSize( |
| 8720 | llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false); |
| 8721 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
| 8722 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
| 8723 | |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 8724 | if (Result.extend(65) != TrueResult && |
| 8725 | !HandleOverflow(Info, E, TrueResult, E->getType())) |
| 8726 | return false; |
Richard Smith | 1b47041 | 2012-02-01 08:10:20 +0000 | [diff] [blame] | 8727 | return Success(Result, E); |
| 8728 | } |
Richard Smith | de21b24 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 8729 | |
| 8730 | // C++11 [expr.rel]p3: |
| 8731 | // Pointers to void (after pointer conversions) can be compared, with a |
| 8732 | // result defined as follows: If both pointers represent the same |
| 8733 | // address or are both the null pointer value, the result is true if the |
| 8734 | // operator is <= or >= and false otherwise; otherwise the result is |
| 8735 | // unspecified. |
| 8736 | // We interpret this as applying to pointers to *cv* void. |
| 8737 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 8738 | E->isRelationalOp()) |
Richard Smith | de21b24 | 2012-01-31 06:41:30 +0000 | [diff] [blame] | 8739 | CCEDiag(E, diag::note_constexpr_void_comparison); |
| 8740 | |
Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 8741 | // C++11 [expr.rel]p2: |
| 8742 | // - If two pointers point to non-static data members of the same object, |
| 8743 | // or to subobjects or array elements fo such members, recursively, the |
| 8744 | // pointer to the later declared member compares greater provided the |
| 8745 | // two members have the same access control and provided their class is |
| 8746 | // not a union. |
| 8747 | // [...] |
| 8748 | // - Otherwise pointer comparisons are unspecified. |
| 8749 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
| 8750 | E->isRelationalOp()) { |
| 8751 | bool WasArrayIndex; |
| 8752 | unsigned Mismatch = |
| 8753 | FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator, |
| 8754 | RHSDesignator, WasArrayIndex); |
| 8755 | // At the point where the designators diverge, the comparison has a |
| 8756 | // specified value if: |
| 8757 | // - we are comparing array indices |
| 8758 | // - we are comparing fields of a union, or fields with the same access |
| 8759 | // Otherwise, the result is unspecified and thus the comparison is not a |
| 8760 | // constant expression. |
| 8761 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
| 8762 | Mismatch < RHSDesignator.Entries.size()) { |
| 8763 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
| 8764 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
| 8765 | if (!LF && !RF) |
| 8766 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
| 8767 | else if (!LF) |
| 8768 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 8769 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
| 8770 | << RF->getParent() << RF; |
| 8771 | else if (!RF) |
| 8772 | CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
| 8773 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
| 8774 | << LF->getParent() << LF; |
| 8775 | else if (!LF->getParent()->isUnion() && |
| 8776 | LF->getAccess() != RF->getAccess()) |
| 8777 | CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access) |
| 8778 | << LF << LF->getAccess() << RF << RF->getAccess() |
| 8779 | << LF->getParent(); |
| 8780 | } |
| 8781 | } |
| 8782 | |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 8783 | // The comparison here must be unsigned, and performed with the same |
| 8784 | // width as the pointer. |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 8785 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
| 8786 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
| 8787 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
| 8788 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
| 8789 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
| 8790 | CompareLHS &= Mask; |
| 8791 | CompareRHS &= Mask; |
| 8792 | |
Eli Friedman | 2f5b7c5 | 2012-04-16 19:23:57 +0000 | [diff] [blame] | 8793 | // If there is a base and this is a relational operator, we can only |
| 8794 | // compare pointers within the object in question; otherwise, the result |
| 8795 | // depends on where the object is located in memory. |
| 8796 | if (!LHSValue.Base.isNull() && E->isRelationalOp()) { |
| 8797 | QualType BaseTy = getType(LHSValue.Base); |
| 8798 | if (BaseTy->isIncompleteType()) |
| 8799 | return Error(E); |
| 8800 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
| 8801 | uint64_t OffsetLimit = Size.getQuantity(); |
| 8802 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
| 8803 | return Error(E); |
| 8804 | } |
| 8805 | |
Richard Smith | 8b3497e | 2011-10-31 01:37:14 +0000 | [diff] [blame] | 8806 | switch (E->getOpcode()) { |
| 8807 | default: llvm_unreachable("missing comparison operator"); |
Eli Friedman | 6c31cb4 | 2012-04-16 04:30:08 +0000 | [diff] [blame] | 8808 | case BO_LT: return Success(CompareLHS < CompareRHS, E); |
| 8809 | case BO_GT: return Success(CompareLHS > CompareRHS, E); |
| 8810 | case BO_LE: return Success(CompareLHS <= CompareRHS, E); |
| 8811 | case BO_GE: return Success(CompareLHS >= CompareRHS, E); |
| 8812 | case BO_EQ: return Success(CompareLHS == CompareRHS, E); |
| 8813 | case BO_NE: return Success(CompareLHS != CompareRHS, E); |
Eli Friedman | a38da57 | 2009-04-28 19:17:36 +0000 | [diff] [blame] | 8814 | } |
Anders Carlsson | 9f9e424 | 2008-11-16 19:01:22 +0000 | [diff] [blame] | 8815 | } |
| 8816 | } |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 8817 | |
| 8818 | if (LHSTy->isMemberPointerType()) { |
| 8819 | assert(E->isEqualityOp() && "unexpected member pointer operation"); |
| 8820 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
| 8821 | |
| 8822 | MemberPtr LHSValue, RHSValue; |
| 8823 | |
| 8824 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 8825 | if (!LHSOK && !Info.noteFailure()) |
Richard Smith | 7bb0067 | 2012-02-01 01:42:44 +0000 | [diff] [blame] | 8826 | return false; |
| 8827 | |
| 8828 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
| 8829 | return false; |
| 8830 | |
| 8831 | // C++11 [expr.eq]p2: |
| 8832 | // If both operands are null, they compare equal. Otherwise if only one is |
| 8833 | // null, they compare unequal. |
| 8834 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
| 8835 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
| 8836 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 8837 | } |
| 8838 | |
| 8839 | // Otherwise if either is a pointer to a virtual member function, the |
| 8840 | // result is unspecified. |
| 8841 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
| 8842 | if (MD->isVirtual()) |
| 8843 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 8844 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
| 8845 | if (MD->isVirtual()) |
| 8846 | CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
| 8847 | |
| 8848 | // Otherwise they compare equal if and only if they would refer to the |
| 8849 | // same member of the same most derived object or the same subobject if |
| 8850 | // they were dereferenced with a hypothetical object of the associated |
| 8851 | // class type. |
| 8852 | bool Equal = LHSValue == RHSValue; |
| 8853 | return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E); |
| 8854 | } |
| 8855 | |
Richard Smith | ab44d9b | 2012-02-14 22:35:28 +0000 | [diff] [blame] | 8856 | if (LHSTy->isNullPtrType()) { |
| 8857 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
| 8858 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
| 8859 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
| 8860 | // are compared, the result is true of the operator is <=, >= or ==, and |
| 8861 | // false otherwise. |
| 8862 | BinaryOperator::Opcode Opcode = E->getOpcode(); |
| 8863 | return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E); |
| 8864 | } |
| 8865 | |
Argyrios Kyrtzidis | 57595e4 | 2012-03-15 18:07:16 +0000 | [diff] [blame] | 8866 | assert((!LHSTy->isIntegralOrEnumerationType() || |
| 8867 | !RHSTy->isIntegralOrEnumerationType()) && |
| 8868 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
| 8869 | // We can't continue from here for non-integral types. |
| 8870 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 8871 | } |
| 8872 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8873 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
| 8874 | /// a result as the expression's type. |
| 8875 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
| 8876 | const UnaryExprOrTypeTraitExpr *E) { |
| 8877 | switch(E->getKind()) { |
| 8878 | case UETT_AlignOf: { |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 8879 | if (E->isArgumentType()) |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8880 | return Success(GetAlignOfType(Info, E->getArgumentType()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 8881 | else |
Hal Finkel | 0dd05d4 | 2014-10-03 17:18:37 +0000 | [diff] [blame] | 8882 | return Success(GetAlignOfExpr(Info, E->getArgumentExpr()), E); |
Chris Lattner | 24aeeab | 2009-01-24 21:09:06 +0000 | [diff] [blame] | 8883 | } |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 8884 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8885 | case UETT_VecStep: { |
| 8886 | QualType Ty = E->getTypeOfArgument(); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 8887 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8888 | if (Ty->isVectorType()) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 8889 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
Eli Friedman | 6400433 | 2009-03-23 04:38:34 +0000 | [diff] [blame] | 8890 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8891 | // The vec_step built-in functions that take a 3-component |
| 8892 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
| 8893 | if (n == 3) |
| 8894 | n = 4; |
Eli Friedman | 2aa38fe | 2009-01-24 22:19:05 +0000 | [diff] [blame] | 8895 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8896 | return Success(n, E); |
| 8897 | } else |
| 8898 | return Success(1, E); |
| 8899 | } |
| 8900 | |
| 8901 | case UETT_SizeOf: { |
| 8902 | QualType SrcTy = E->getTypeOfArgument(); |
| 8903 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 8904 | // the result is the size of the referenced type." |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8905 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
| 8906 | SrcTy = Ref->getPointeeType(); |
| 8907 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8908 | CharUnits Sizeof; |
Richard Smith | 17100ba | 2012-02-16 02:46:34 +0000 | [diff] [blame] | 8909 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8910 | return false; |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 8911 | return Success(Sizeof, E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8912 | } |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 8913 | case UETT_OpenMPRequiredSimdAlign: |
| 8914 | assert(E->isArgumentType()); |
| 8915 | return Success( |
| 8916 | Info.Ctx.toCharUnitsFromBits( |
| 8917 | Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType())) |
| 8918 | .getQuantity(), |
| 8919 | E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 8920 | } |
| 8921 | |
| 8922 | llvm_unreachable("unknown expr/type trait"); |
Chris Lattner | f8d7f72 | 2008-07-11 21:24:13 +0000 | [diff] [blame] | 8923 | } |
| 8924 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 8925 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8926 | CharUnits Result; |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 8927 | unsigned n = OOE->getNumComponents(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8928 | if (n == 0) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8929 | return Error(OOE); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 8930 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8931 | for (unsigned i = 0; i != n; ++i) { |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 8932 | OffsetOfNode ON = OOE->getComponent(i); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8933 | switch (ON.getKind()) { |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 8934 | case OffsetOfNode::Array: { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 8935 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8936 | APSInt IdxResult; |
| 8937 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
| 8938 | return false; |
| 8939 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
| 8940 | if (!AT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8941 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8942 | CurrentType = AT->getElementType(); |
| 8943 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
| 8944 | Result += IdxResult.getSExtValue() * ElementSize; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 8945 | break; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8946 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8947 | |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 8948 | case OffsetOfNode::Field: { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8949 | FieldDecl *MemberDecl = ON.getField(); |
| 8950 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8951 | if (!RT) |
| 8952 | return Error(OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8953 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 8954 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8955 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 8956 | unsigned i = MemberDecl->getFieldIndex(); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8957 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
Ken Dyck | 86a7fcc | 2011-01-18 01:56:16 +0000 | [diff] [blame] | 8958 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8959 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8960 | break; |
| 8961 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8962 | |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 8963 | case OffsetOfNode::Identifier: |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8964 | llvm_unreachable("dependent __builtin_offsetof"); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8965 | |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 8966 | case OffsetOfNode::Base: { |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8967 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
| 8968 | if (BaseSpec->isVirtual()) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8969 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8970 | |
| 8971 | // Find the layout of the class whose base we are looking into. |
| 8972 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8973 | if (!RT) |
| 8974 | return Error(OOE); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8975 | RecordDecl *RD = RT->getDecl(); |
John McCall | d7bca76 | 2012-05-01 00:38:49 +0000 | [diff] [blame] | 8976 | if (RD->isInvalidDecl()) return false; |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8977 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
| 8978 | |
| 8979 | // Find the base class itself. |
| 8980 | CurrentType = BaseSpec->getType(); |
| 8981 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
| 8982 | if (!BaseRT) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8983 | return Error(OOE); |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 8984 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8985 | // Add the offset to the base. |
Ken Dyck | 02155cb | 2011-01-26 02:17:08 +0000 | [diff] [blame] | 8986 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8987 | break; |
| 8988 | } |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8989 | } |
| 8990 | } |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 8991 | return Success(Result, OOE); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8992 | } |
| 8993 | |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 8994 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 8995 | switch (E->getOpcode()) { |
| 8996 | default: |
| 8997 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 8998 | // See C99 6.6p3. |
| 8999 | return Error(E); |
| 9000 | case UO_Extension: |
| 9001 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
| 9002 | // If so, we could clear the diagnostic ID. |
| 9003 | return Visit(E->getSubExpr()); |
| 9004 | case UO_Plus: |
| 9005 | // The result is just the value. |
| 9006 | return Visit(E->getSubExpr()); |
| 9007 | case UO_Minus: { |
| 9008 | if (!Visit(E->getSubExpr())) |
Malcolm Parsons | fab3680 | 2018-04-16 08:31:08 +0000 | [diff] [blame^] | 9009 | return false; |
| 9010 | if (!Result.isInt()) return Error(E); |
| 9011 | const APSInt &Value = Result.getInt(); |
| 9012 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() && |
| 9013 | !HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
| 9014 | E->getType())) |
| 9015 | return false; |
Richard Smith | fe80003 | 2012-01-31 04:08:20 +0000 | [diff] [blame] | 9016 | return Success(-Value, E); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9017 | } |
| 9018 | case UO_Not: { |
| 9019 | if (!Visit(E->getSubExpr())) |
| 9020 | return false; |
| 9021 | if (!Result.isInt()) return Error(E); |
| 9022 | return Success(~Result.getInt(), E); |
| 9023 | } |
| 9024 | case UO_LNot: { |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 9025 | bool bres; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9026 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 9027 | return false; |
Daniel Dunbar | 8aafc89 | 2009-02-19 09:06:44 +0000 | [diff] [blame] | 9028 | return Success(!bres, E); |
Eli Friedman | 5a332ea | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 9029 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9030 | } |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9031 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9032 | |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9033 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
| 9034 | /// result type is integer. |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9035 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 9036 | const Expr *SubExpr = E->getSubExpr(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 9037 | QualType DestType = E->getType(); |
Daniel Dunbar | cf04aa1 | 2009-02-19 22:16:29 +0000 | [diff] [blame] | 9038 | QualType SrcType = SubExpr->getType(); |
Anders Carlsson | 27b8c5c | 2008-11-30 18:14:57 +0000 | [diff] [blame] | 9039 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9040 | switch (E->getCastKind()) { |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9041 | case CK_BaseToDerived: |
| 9042 | case CK_DerivedToBase: |
| 9043 | case CK_UncheckedDerivedToBase: |
| 9044 | case CK_Dynamic: |
| 9045 | case CK_ToUnion: |
| 9046 | case CK_ArrayToPointerDecay: |
| 9047 | case CK_FunctionToPointerDecay: |
| 9048 | case CK_NullToPointer: |
| 9049 | case CK_NullToMemberPointer: |
| 9050 | case CK_BaseToDerivedMemberPointer: |
| 9051 | case CK_DerivedToBaseMemberPointer: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 9052 | case CK_ReinterpretMemberPointer: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9053 | case CK_ConstructorConversion: |
| 9054 | case CK_IntegralToPointer: |
| 9055 | case CK_ToVoid: |
| 9056 | case CK_VectorSplat: |
| 9057 | case CK_IntegralToFloating: |
| 9058 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 9059 | case CK_CPointerToObjCPointerCast: |
| 9060 | case CK_BlockPointerToObjCPointerCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9061 | case CK_AnyPointerToBlockPointerCast: |
| 9062 | case CK_ObjCObjectLValueCast: |
| 9063 | case CK_FloatingRealToComplex: |
| 9064 | case CK_FloatingComplexToReal: |
| 9065 | case CK_FloatingComplexCast: |
| 9066 | case CK_FloatingComplexToIntegralComplex: |
| 9067 | case CK_IntegralRealToComplex: |
| 9068 | case CK_IntegralComplexCast: |
| 9069 | case CK_IntegralComplexToFloatingComplex: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 9070 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 9071 | case CK_ZeroToOCLEvent: |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 9072 | case CK_ZeroToOCLQueue: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9073 | case CK_NonAtomicToAtomic: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 9074 | case CK_AddressSpaceConversion: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 9075 | case CK_IntToOCLSampler: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9076 | llvm_unreachable("invalid cast kind for integral value"); |
| 9077 | |
Eli Friedman | 9faf2f9 | 2011-03-25 19:07:11 +0000 | [diff] [blame] | 9078 | case CK_BitCast: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9079 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9080 | case CK_LValueBitCast: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 9081 | case CK_ARCProduceObject: |
| 9082 | case CK_ARCConsumeObject: |
| 9083 | case CK_ARCReclaimReturnedObject: |
| 9084 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9085 | case CK_CopyAndAutoreleaseBlockObject: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9086 | return Error(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9087 | |
Richard Smith | 4ef685b | 2012-01-17 21:17:26 +0000 | [diff] [blame] | 9088 | case CK_UserDefinedConversion: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9089 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 9090 | case CK_AtomicToNonAtomic: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9091 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9092 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9093 | |
| 9094 | case CK_MemberPointerToBoolean: |
| 9095 | case CK_PointerToBoolean: |
| 9096 | case CK_IntegralToBoolean: |
| 9097 | case CK_FloatingToBoolean: |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 9098 | case CK_BooleanToSignedIntegral: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9099 | case CK_FloatingComplexToBoolean: |
| 9100 | case CK_IntegralComplexToBoolean: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9101 | bool BoolResult; |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9102 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9103 | return false; |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 9104 | uint64_t IntResult = BoolResult; |
| 9105 | if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral) |
| 9106 | IntResult = (uint64_t)-1; |
| 9107 | return Success(IntResult, E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9108 | } |
| 9109 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9110 | case CK_IntegralCast: { |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9111 | if (!Visit(SubExpr)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 9112 | return false; |
Daniel Dunbar | b6f953e | 2009-01-29 06:16:07 +0000 | [diff] [blame] | 9113 | |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 9114 | if (!Result.isInt()) { |
Eli Friedman | fd5e54d | 2012-01-04 23:13:47 +0000 | [diff] [blame] | 9115 | // Allow casts of address-of-label differences if they are no-ops |
| 9116 | // or narrowing. (The narrowing case isn't actually guaranteed to |
| 9117 | // be constant-evaluatable except in some narrow cases which are hard |
| 9118 | // to detect here. We let it through on the assumption the user knows |
| 9119 | // what they are doing.) |
| 9120 | if (Result.isAddrLabelDiff()) |
| 9121 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
Eli Friedman | 742421e | 2009-02-20 01:15:07 +0000 | [diff] [blame] | 9122 | // Only allow casts of lvalues if they are lossless. |
| 9123 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
| 9124 | } |
Daniel Dunbar | ca097ad | 2009-02-19 20:17:33 +0000 | [diff] [blame] | 9125 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9126 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
| 9127 | Result.getInt()), E); |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9128 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9129 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9130 | case CK_PointerToIntegral: { |
Richard Smith | 6d6ecc3 | 2011-12-12 12:46:16 +0000 | [diff] [blame] | 9131 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
| 9132 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 9133 | LValue LV; |
Chris Lattner | cdf34e7 | 2008-07-11 22:52:41 +0000 | [diff] [blame] | 9134 | if (!EvaluatePointer(SubExpr, LV, Info)) |
Chris Lattner | e13042c | 2008-07-11 19:10:17 +0000 | [diff] [blame] | 9135 | return false; |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9136 | |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 9137 | if (LV.getLValueBase()) { |
| 9138 | // Only allow based lvalue casts if they are lossless. |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9139 | // FIXME: Allow a larger integer size than the pointer size, and allow |
| 9140 | // narrowing back down to pointer width in subsequent integral casts. |
| 9141 | // FIXME: Check integer type's active bits, not its type size. |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 9142 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9143 | return Error(E); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9144 | |
Richard Smith | cf74da7 | 2011-11-16 07:18:12 +0000 | [diff] [blame] | 9145 | LV.Designator.setInvalid(); |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 9146 | LV.moveInto(Result); |
Daniel Dunbar | 1c8560d | 2009-02-19 22:24:01 +0000 | [diff] [blame] | 9147 | return true; |
| 9148 | } |
| 9149 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 9150 | uint64_t V; |
| 9151 | if (LV.isNullPointer()) |
| 9152 | V = Info.Ctx.getTargetNullPointerValue(SrcType); |
| 9153 | else |
| 9154 | V = LV.getLValueOffset().getQuantity(); |
| 9155 | |
| 9156 | APSInt AsInt = Info.Ctx.MakeIntValue(V, SrcType); |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9157 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 9158 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9159 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9160 | case CK_IntegralComplexToReal: { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9161 | ComplexValue C; |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 9162 | if (!EvaluateComplex(SubExpr, C, Info)) |
| 9163 | return false; |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9164 | return Success(C.getComplexIntReal(), E); |
Eli Friedman | d3a5a9d | 2009-04-22 19:23:09 +0000 | [diff] [blame] | 9165 | } |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 9166 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9167 | case CK_FloatingToIntegral: { |
| 9168 | APFloat F(0.0); |
| 9169 | if (!EvaluateFloat(SubExpr, F, Info)) |
| 9170 | return false; |
Chris Lattner | 477c4be | 2008-07-12 01:15:53 +0000 | [diff] [blame] | 9171 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9172 | APSInt Value; |
| 9173 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
| 9174 | return false; |
| 9175 | return Success(Value, E); |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9176 | } |
| 9177 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9178 | |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9179 | llvm_unreachable("unknown cast resulting in integral value"); |
Anders Carlsson | 9c18165 | 2008-07-08 14:35:21 +0000 | [diff] [blame] | 9180 | } |
Anders Carlsson | b5ad021 | 2008-07-08 14:30:00 +0000 | [diff] [blame] | 9181 | |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9182 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
| 9183 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9184 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9185 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 9186 | return false; |
| 9187 | if (!LV.isComplexInt()) |
| 9188 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9189 | return Success(LV.getComplexIntReal(), E); |
| 9190 | } |
| 9191 | |
| 9192 | return Visit(E->getSubExpr()); |
| 9193 | } |
| 9194 | |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 9195 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9196 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9197 | ComplexValue LV; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9198 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
| 9199 | return false; |
| 9200 | if (!LV.isComplexInt()) |
| 9201 | return Error(E); |
Eli Friedman | a1c7b6c | 2009-02-28 03:59:05 +0000 | [diff] [blame] | 9202 | return Success(LV.getComplexIntImag(), E); |
| 9203 | } |
| 9204 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 9205 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 4e7a241 | 2009-02-27 04:45:43 +0000 | [diff] [blame] | 9206 | return Success(0, E); |
| 9207 | } |
| 9208 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 9209 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
| 9210 | return Success(E->getPackLength(), E); |
| 9211 | } |
| 9212 | |
Sebastian Redl | 5f0180d | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 9213 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
| 9214 | return Success(E->getValue(), E); |
| 9215 | } |
| 9216 | |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 9217 | //===----------------------------------------------------------------------===// |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9218 | // Float Evaluation |
| 9219 | //===----------------------------------------------------------------------===// |
| 9220 | |
| 9221 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 9222 | class FloatExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 9223 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9224 | APFloat &Result; |
| 9225 | public: |
| 9226 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9227 | : ExprEvaluatorBaseTy(info), Result(result) {} |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9228 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9229 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9230 | Result = V.getFloat(); |
| 9231 | return true; |
| 9232 | } |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9233 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9234 | bool ZeroInitialization(const Expr *E) { |
Richard Smith | 4ce706a | 2011-10-11 21:43:33 +0000 | [diff] [blame] | 9235 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
| 9236 | return true; |
| 9237 | } |
| 9238 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9239 | bool VisitCallExpr(const CallExpr *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9240 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9241 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9242 | bool VisitBinaryOperator(const BinaryOperator *E); |
| 9243 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9244 | bool VisitCastExpr(const CastExpr *E); |
Eli Friedman | c2b5017 | 2009-02-22 11:46:18 +0000 | [diff] [blame] | 9245 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 9246 | bool VisitUnaryReal(const UnaryOperator *E); |
| 9247 | bool VisitUnaryImag(const UnaryOperator *E); |
Eli Friedman | 449fe54 | 2009-03-23 04:56:01 +0000 | [diff] [blame] | 9248 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 9249 | // FIXME: Missing: array subscript of vector, member of vector |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9250 | }; |
| 9251 | } // end anonymous namespace |
| 9252 | |
| 9253 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9254 | assert(E->isRValue() && E->getType()->isRealFloatingType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9255 | return FloatExprEvaluator(Info, Result).Visit(E); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9256 | } |
| 9257 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 9258 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 9259 | QualType ResultTy, |
| 9260 | const Expr *Arg, |
| 9261 | bool SNaN, |
| 9262 | llvm::APFloat &Result) { |
| 9263 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
| 9264 | if (!S) return false; |
| 9265 | |
| 9266 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
| 9267 | |
| 9268 | llvm::APInt fill; |
| 9269 | |
| 9270 | // Treat empty strings as if they were zero. |
| 9271 | if (S->getString().empty()) |
| 9272 | fill = llvm::APInt(32, 0); |
| 9273 | else if (S->getString().getAsInteger(0, fill)) |
| 9274 | return false; |
| 9275 | |
Petar Jovanovic | d55ae6b | 2015-02-26 18:19:22 +0000 | [diff] [blame] | 9276 | if (Context.getTargetInfo().isNan2008()) { |
| 9277 | if (SNaN) |
| 9278 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 9279 | else |
| 9280 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 9281 | } else { |
| 9282 | // Prior to IEEE 754-2008, architectures were allowed to choose whether |
| 9283 | // the first bit of their significand was set for qNaN or sNaN. MIPS chose |
| 9284 | // a different encoding to what became a standard in 2008, and for pre- |
| 9285 | // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as |
| 9286 | // sNaN. This is now known as "legacy NaN" encoding. |
| 9287 | if (SNaN) |
| 9288 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
| 9289 | else |
| 9290 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
| 9291 | } |
| 9292 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 9293 | return true; |
| 9294 | } |
| 9295 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9296 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 9297 | switch (E->getBuiltinCallee()) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9298 | default: |
| 9299 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 9300 | |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9301 | case Builtin::BI__builtin_huge_val: |
| 9302 | case Builtin::BI__builtin_huge_valf: |
| 9303 | case Builtin::BI__builtin_huge_vall: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9304 | case Builtin::BI__builtin_huge_valf128: |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9305 | case Builtin::BI__builtin_inf: |
| 9306 | case Builtin::BI__builtin_inff: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9307 | case Builtin::BI__builtin_infl: |
| 9308 | case Builtin::BI__builtin_inff128: { |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 9309 | const llvm::fltSemantics &Sem = |
| 9310 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
Chris Lattner | 37346e0 | 2008-10-06 05:53:16 +0000 | [diff] [blame] | 9311 | Result = llvm::APFloat::getInf(Sem); |
| 9312 | return true; |
Daniel Dunbar | 1be9f88 | 2008-10-14 05:41:12 +0000 | [diff] [blame] | 9313 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9314 | |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 9315 | case Builtin::BI__builtin_nans: |
| 9316 | case Builtin::BI__builtin_nansf: |
| 9317 | case Builtin::BI__builtin_nansl: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9318 | case Builtin::BI__builtin_nansf128: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9319 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 9320 | true, Result)) |
| 9321 | return Error(E); |
| 9322 | return true; |
John McCall | 1629149 | 2010-02-28 13:00:19 +0000 | [diff] [blame] | 9323 | |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 9324 | case Builtin::BI__builtin_nan: |
| 9325 | case Builtin::BI__builtin_nanf: |
| 9326 | case Builtin::BI__builtin_nanl: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9327 | case Builtin::BI__builtin_nanf128: |
Mike Stump | 2346cd2 | 2009-05-30 03:56:50 +0000 | [diff] [blame] | 9328 | // If this is __builtin_nan() turn this into a nan, otherwise we |
Chris Lattner | 0b7282e | 2008-10-06 06:31:58 +0000 | [diff] [blame] | 9329 | // can't constant fold it. |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9330 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
| 9331 | false, Result)) |
| 9332 | return Error(E); |
| 9333 | return true; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9334 | |
| 9335 | case Builtin::BI__builtin_fabs: |
| 9336 | case Builtin::BI__builtin_fabsf: |
| 9337 | case Builtin::BI__builtin_fabsl: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9338 | case Builtin::BI__builtin_fabsf128: |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9339 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
| 9340 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9341 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9342 | if (Result.isNegative()) |
| 9343 | Result.changeSign(); |
| 9344 | return true; |
| 9345 | |
Richard Smith | 8889a3d | 2013-06-13 06:26:32 +0000 | [diff] [blame] | 9346 | // FIXME: Builtin::BI__builtin_powi |
| 9347 | // FIXME: Builtin::BI__builtin_powif |
| 9348 | // FIXME: Builtin::BI__builtin_powil |
| 9349 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9350 | case Builtin::BI__builtin_copysign: |
| 9351 | case Builtin::BI__builtin_copysignf: |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9352 | case Builtin::BI__builtin_copysignl: |
| 9353 | case Builtin::BI__builtin_copysignf128: { |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9354 | APFloat RHS(0.); |
| 9355 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
| 9356 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
| 9357 | return false; |
| 9358 | Result.copySign(RHS); |
| 9359 | return true; |
| 9360 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9361 | } |
| 9362 | } |
| 9363 | |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 9364 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 9365 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 9366 | ComplexValue CV; |
| 9367 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 9368 | return false; |
| 9369 | Result = CV.FloatReal; |
| 9370 | return true; |
| 9371 | } |
| 9372 | |
| 9373 | return Visit(E->getSubExpr()); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 9374 | } |
| 9375 | |
| 9376 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 9377 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
| 9378 | ComplexValue CV; |
| 9379 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
| 9380 | return false; |
| 9381 | Result = CV.FloatImag; |
| 9382 | return true; |
| 9383 | } |
| 9384 | |
Richard Smith | 4a67812 | 2011-10-24 18:44:57 +0000 | [diff] [blame] | 9385 | VisitIgnoredValue(E->getSubExpr()); |
Eli Friedman | 9571953 | 2010-08-14 20:52:13 +0000 | [diff] [blame] | 9386 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
| 9387 | Result = llvm::APFloat::getZero(Sem); |
John McCall | b1fb0d3 | 2010-05-07 22:08:54 +0000 | [diff] [blame] | 9388 | return true; |
| 9389 | } |
| 9390 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9391 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9392 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9393 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9394 | case UO_Plus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 9395 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9396 | case UO_Minus: |
Richard Smith | 390cd49 | 2011-10-30 23:17:09 +0000 | [diff] [blame] | 9397 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
| 9398 | return false; |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9399 | Result.changeSign(); |
| 9400 | return true; |
| 9401 | } |
| 9402 | } |
Chris Lattner | 4deaa4e | 2008-10-06 05:28:25 +0000 | [diff] [blame] | 9403 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9404 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9405 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
| 9406 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
Eli Friedman | 141fbf3 | 2009-11-16 04:25:37 +0000 | [diff] [blame] | 9407 | |
Daniel Dunbar | c3d79cf | 2008-10-16 03:51:50 +0000 | [diff] [blame] | 9408 | APFloat RHS(0.0); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 9409 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 9410 | if (!LHSOK && !Info.noteFailure()) |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9411 | return false; |
Richard Smith | 861b5b5 | 2013-05-07 23:34:45 +0000 | [diff] [blame] | 9412 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
| 9413 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9414 | } |
| 9415 | |
| 9416 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
| 9417 | Result = E->getValue(); |
| 9418 | return true; |
| 9419 | } |
| 9420 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9421 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
| 9422 | const Expr* SubExpr = E->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9423 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 9424 | switch (E->getCastKind()) { |
| 9425 | default: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9426 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 9427 | |
| 9428 | case CK_IntegralToFloating: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9429 | APSInt IntResult; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9430 | return EvaluateInteger(SubExpr, IntResult, Info) && |
| 9431 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
| 9432 | E->getType(), Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9433 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 9434 | |
| 9435 | case CK_FloatingCast: { |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9436 | if (!Visit(SubExpr)) |
| 9437 | return false; |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9438 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
| 9439 | Result); |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9440 | } |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 9441 | |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 9442 | case CK_FloatingComplexToReal: { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 9443 | ComplexValue V; |
| 9444 | if (!EvaluateComplex(SubExpr, V, Info)) |
| 9445 | return false; |
| 9446 | Result = V.getComplexFloatReal(); |
| 9447 | return true; |
| 9448 | } |
Eli Friedman | 8bfbe3a | 2011-03-25 00:54:52 +0000 | [diff] [blame] | 9449 | } |
Eli Friedman | 9a156e5 | 2008-11-12 09:44:48 +0000 | [diff] [blame] | 9450 | } |
| 9451 | |
Eli Friedman | 24c0154 | 2008-08-22 00:06:13 +0000 | [diff] [blame] | 9452 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 9453 | // Complex Evaluation (for float and integer) |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9454 | //===----------------------------------------------------------------------===// |
| 9455 | |
| 9456 | namespace { |
Benjamin Kramer | 26222b6 | 2009-11-28 19:03:38 +0000 | [diff] [blame] | 9457 | class ComplexExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 9458 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9459 | ComplexValue &Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9460 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9461 | public: |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9462 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9463 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
| 9464 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 9465 | bool Success(const APValue &V, const Expr *e) { |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9466 | Result.setFrom(V); |
| 9467 | return true; |
| 9468 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9469 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 9470 | bool ZeroInitialization(const Expr *E); |
| 9471 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9472 | //===--------------------------------------------------------------------===// |
| 9473 | // Visitor Methods |
| 9474 | //===--------------------------------------------------------------------===// |
| 9475 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9476 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9477 | bool VisitCastExpr(const CastExpr *E); |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9478 | bool VisitBinaryOperator(const BinaryOperator *E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 9479 | bool VisitUnaryOperator(const UnaryOperator *E); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 9480 | bool VisitInitListExpr(const InitListExpr *E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9481 | }; |
| 9482 | } // end anonymous namespace |
| 9483 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9484 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
| 9485 | EvalInfo &Info) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9486 | assert(E->isRValue() && E->getType()->isAnyComplexType()); |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9487 | return ComplexExprEvaluator(Info, Result).Visit(E); |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9488 | } |
| 9489 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 9490 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 9491 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 9492 | if (ElemTy->isRealFloatingType()) { |
| 9493 | Result.makeComplexFloat(); |
| 9494 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
| 9495 | Result.FloatReal = Zero; |
| 9496 | Result.FloatImag = Zero; |
| 9497 | } else { |
| 9498 | Result.makeComplexInt(); |
| 9499 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
| 9500 | Result.IntReal = Zero; |
| 9501 | Result.IntImag = Zero; |
| 9502 | } |
| 9503 | return true; |
| 9504 | } |
| 9505 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9506 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
| 9507 | const Expr* SubExpr = E->getSubExpr(); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9508 | |
| 9509 | if (SubExpr->getType()->isRealFloatingType()) { |
| 9510 | Result.makeComplexFloat(); |
| 9511 | APFloat &Imag = Result.FloatImag; |
| 9512 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
| 9513 | return false; |
| 9514 | |
| 9515 | Result.FloatReal = APFloat(Imag.getSemantics()); |
| 9516 | return true; |
| 9517 | } else { |
| 9518 | assert(SubExpr->getType()->isIntegerType() && |
| 9519 | "Unexpected imaginary literal."); |
| 9520 | |
| 9521 | Result.makeComplexInt(); |
| 9522 | APSInt &Imag = Result.IntImag; |
| 9523 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
| 9524 | return false; |
| 9525 | |
| 9526 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
| 9527 | return true; |
| 9528 | } |
| 9529 | } |
| 9530 | |
Peter Collingbourne | e920068 | 2011-05-13 03:29:01 +0000 | [diff] [blame] | 9531 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9532 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9533 | switch (E->getCastKind()) { |
| 9534 | case CK_BitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9535 | case CK_BaseToDerived: |
| 9536 | case CK_DerivedToBase: |
| 9537 | case CK_UncheckedDerivedToBase: |
| 9538 | case CK_Dynamic: |
| 9539 | case CK_ToUnion: |
| 9540 | case CK_ArrayToPointerDecay: |
| 9541 | case CK_FunctionToPointerDecay: |
| 9542 | case CK_NullToPointer: |
| 9543 | case CK_NullToMemberPointer: |
| 9544 | case CK_BaseToDerivedMemberPointer: |
| 9545 | case CK_DerivedToBaseMemberPointer: |
| 9546 | case CK_MemberPointerToBoolean: |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 9547 | case CK_ReinterpretMemberPointer: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9548 | case CK_ConstructorConversion: |
| 9549 | case CK_IntegralToPointer: |
| 9550 | case CK_PointerToIntegral: |
| 9551 | case CK_PointerToBoolean: |
| 9552 | case CK_ToVoid: |
| 9553 | case CK_VectorSplat: |
| 9554 | case CK_IntegralCast: |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 9555 | case CK_BooleanToSignedIntegral: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9556 | case CK_IntegralToBoolean: |
| 9557 | case CK_IntegralToFloating: |
| 9558 | case CK_FloatingToIntegral: |
| 9559 | case CK_FloatingToBoolean: |
| 9560 | case CK_FloatingCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 9561 | case CK_CPointerToObjCPointerCast: |
| 9562 | case CK_BlockPointerToObjCPointerCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9563 | case CK_AnyPointerToBlockPointerCast: |
| 9564 | case CK_ObjCObjectLValueCast: |
| 9565 | case CK_FloatingComplexToReal: |
| 9566 | case CK_FloatingComplexToBoolean: |
| 9567 | case CK_IntegralComplexToReal: |
| 9568 | case CK_IntegralComplexToBoolean: |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 9569 | case CK_ARCProduceObject: |
| 9570 | case CK_ARCConsumeObject: |
| 9571 | case CK_ARCReclaimReturnedObject: |
| 9572 | case CK_ARCExtendBlockObject: |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 9573 | case CK_CopyAndAutoreleaseBlockObject: |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 9574 | case CK_BuiltinFnToFnPtr: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 9575 | case CK_ZeroToOCLEvent: |
Egor Churaev | 8983142 | 2016-12-23 14:55:49 +0000 | [diff] [blame] | 9576 | case CK_ZeroToOCLQueue: |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9577 | case CK_NonAtomicToAtomic: |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 9578 | case CK_AddressSpaceConversion: |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 9579 | case CK_IntToOCLSampler: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9580 | llvm_unreachable("invalid cast kind for complex value"); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 9581 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9582 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 9583 | case CK_AtomicToNonAtomic: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9584 | case CK_NoOp: |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 9585 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9586 | |
| 9587 | case CK_Dependent: |
Eli Friedman | c757de2 | 2011-03-25 00:43:55 +0000 | [diff] [blame] | 9588 | case CK_LValueBitCast: |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9589 | case CK_UserDefinedConversion: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9590 | return Error(E); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9591 | |
| 9592 | case CK_FloatingRealToComplex: { |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9593 | APFloat &Real = Result.FloatReal; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9594 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9595 | return false; |
| 9596 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9597 | Result.makeComplexFloat(); |
| 9598 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 9599 | return true; |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9600 | } |
| 9601 | |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9602 | case CK_FloatingComplexCast: { |
| 9603 | if (!Visit(E->getSubExpr())) |
| 9604 | return false; |
| 9605 | |
| 9606 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 9607 | QualType From |
| 9608 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 9609 | |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9610 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
| 9611 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9612 | } |
| 9613 | |
| 9614 | case CK_FloatingComplexToIntegralComplex: { |
| 9615 | if (!Visit(E->getSubExpr())) |
| 9616 | return false; |
| 9617 | |
| 9618 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 9619 | QualType From |
| 9620 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 9621 | Result.makeComplexInt(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9622 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
| 9623 | To, Result.IntReal) && |
| 9624 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
| 9625 | To, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9626 | } |
| 9627 | |
| 9628 | case CK_IntegralRealToComplex: { |
| 9629 | APSInt &Real = Result.IntReal; |
| 9630 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
| 9631 | return false; |
| 9632 | |
| 9633 | Result.makeComplexInt(); |
| 9634 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
| 9635 | return true; |
| 9636 | } |
| 9637 | |
| 9638 | case CK_IntegralComplexCast: { |
| 9639 | if (!Visit(E->getSubExpr())) |
| 9640 | return false; |
| 9641 | |
| 9642 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
| 9643 | QualType From |
| 9644 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
| 9645 | |
Richard Smith | 911e142 | 2012-01-30 22:27:01 +0000 | [diff] [blame] | 9646 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
| 9647 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9648 | return true; |
| 9649 | } |
| 9650 | |
| 9651 | case CK_IntegralComplexToFloatingComplex: { |
| 9652 | if (!Visit(E->getSubExpr())) |
| 9653 | return false; |
| 9654 | |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 9655 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9656 | QualType From |
Ted Kremenek | 2883175 | 2012-08-23 20:46:57 +0000 | [diff] [blame] | 9657 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9658 | Result.makeComplexFloat(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 9659 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
| 9660 | To, Result.FloatReal) && |
| 9661 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
| 9662 | To, Result.FloatImag); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 9663 | } |
| 9664 | } |
| 9665 | |
| 9666 | llvm_unreachable("unknown cast resulting in complex value"); |
Eli Friedman | c3e9df3 | 2010-08-16 23:27:44 +0000 | [diff] [blame] | 9667 | } |
| 9668 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9669 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 9670 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
Richard Smith | 10f4d06 | 2011-11-16 17:22:48 +0000 | [diff] [blame] | 9671 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
| 9672 | |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9673 | // Track whether the LHS or RHS is real at the type system level. When this is |
| 9674 | // the case we can simplify our evaluation strategy. |
| 9675 | bool LHSReal = false, RHSReal = false; |
| 9676 | |
| 9677 | bool LHSOK; |
| 9678 | if (E->getLHS()->getType()->isRealFloatingType()) { |
| 9679 | LHSReal = true; |
| 9680 | APFloat &Real = Result.FloatReal; |
| 9681 | LHSOK = EvaluateFloat(E->getLHS(), Real, Info); |
| 9682 | if (LHSOK) { |
| 9683 | Result.makeComplexFloat(); |
| 9684 | Result.FloatImag = APFloat(Real.getSemantics()); |
| 9685 | } |
| 9686 | } else { |
| 9687 | LHSOK = Visit(E->getLHS()); |
| 9688 | } |
George Burgess IV | a145e25 | 2016-05-25 22:38:36 +0000 | [diff] [blame] | 9689 | if (!LHSOK && !Info.noteFailure()) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9690 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9691 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9692 | ComplexValue RHS; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9693 | if (E->getRHS()->getType()->isRealFloatingType()) { |
| 9694 | RHSReal = true; |
| 9695 | APFloat &Real = RHS.FloatReal; |
| 9696 | if (!EvaluateFloat(E->getRHS(), Real, Info) || !LHSOK) |
| 9697 | return false; |
| 9698 | RHS.makeComplexFloat(); |
| 9699 | RHS.FloatImag = APFloat(Real.getSemantics()); |
| 9700 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9701 | return false; |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 9702 | |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9703 | assert(!(LHSReal && RHSReal) && |
| 9704 | "Cannot have both operands of a complex operation be real."); |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 9705 | switch (E->getOpcode()) { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9706 | default: return Error(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9707 | case BO_Add: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 9708 | if (Result.isComplexFloat()) { |
| 9709 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
| 9710 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9711 | if (LHSReal) |
| 9712 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 9713 | else if (!RHSReal) |
| 9714 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
| 9715 | APFloat::rmNearestTiesToEven); |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 9716 | } else { |
| 9717 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
| 9718 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
| 9719 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 9720 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9721 | case BO_Sub: |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 9722 | if (Result.isComplexFloat()) { |
| 9723 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
| 9724 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9725 | if (LHSReal) { |
| 9726 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
| 9727 | Result.getComplexFloatImag().changeSign(); |
| 9728 | } else if (!RHSReal) { |
| 9729 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
| 9730 | APFloat::rmNearestTiesToEven); |
| 9731 | } |
Daniel Dunbar | f50e60b | 2009-01-28 22:24:07 +0000 | [diff] [blame] | 9732 | } else { |
| 9733 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
| 9734 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
| 9735 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 9736 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9737 | case BO_Mul: |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 9738 | if (Result.isComplexFloat()) { |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9739 | // This is an implementation of complex multiplication according to the |
Hiroshi Inoue | 0c2734f | 2017-07-05 05:37:45 +0000 | [diff] [blame] | 9740 | // constraints laid out in C11 Annex G. The implemention uses the |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9741 | // following naming scheme: |
| 9742 | // (a + ib) * (c + id) |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9743 | ComplexValue LHS = Result; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9744 | APFloat &A = LHS.getComplexFloatReal(); |
| 9745 | APFloat &B = LHS.getComplexFloatImag(); |
| 9746 | APFloat &C = RHS.getComplexFloatReal(); |
| 9747 | APFloat &D = RHS.getComplexFloatImag(); |
| 9748 | APFloat &ResR = Result.getComplexFloatReal(); |
| 9749 | APFloat &ResI = Result.getComplexFloatImag(); |
| 9750 | if (LHSReal) { |
| 9751 | assert(!RHSReal && "Cannot have two real operands for a complex op!"); |
| 9752 | ResR = A * C; |
| 9753 | ResI = A * D; |
| 9754 | } else if (RHSReal) { |
| 9755 | ResR = C * A; |
| 9756 | ResI = C * B; |
| 9757 | } else { |
| 9758 | // In the fully general case, we need to handle NaNs and infinities |
| 9759 | // robustly. |
| 9760 | APFloat AC = A * C; |
| 9761 | APFloat BD = B * D; |
| 9762 | APFloat AD = A * D; |
| 9763 | APFloat BC = B * C; |
| 9764 | ResR = AC - BD; |
| 9765 | ResI = AD + BC; |
| 9766 | if (ResR.isNaN() && ResI.isNaN()) { |
| 9767 | bool Recalc = false; |
| 9768 | if (A.isInfinity() || B.isInfinity()) { |
| 9769 | A = APFloat::copySign( |
| 9770 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 9771 | B = APFloat::copySign( |
| 9772 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 9773 | if (C.isNaN()) |
| 9774 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 9775 | if (D.isNaN()) |
| 9776 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 9777 | Recalc = true; |
| 9778 | } |
| 9779 | if (C.isInfinity() || D.isInfinity()) { |
| 9780 | C = APFloat::copySign( |
| 9781 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 9782 | D = APFloat::copySign( |
| 9783 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 9784 | if (A.isNaN()) |
| 9785 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 9786 | if (B.isNaN()) |
| 9787 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 9788 | Recalc = true; |
| 9789 | } |
| 9790 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
| 9791 | AD.isInfinity() || BC.isInfinity())) { |
| 9792 | if (A.isNaN()) |
| 9793 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
| 9794 | if (B.isNaN()) |
| 9795 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
| 9796 | if (C.isNaN()) |
| 9797 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
| 9798 | if (D.isNaN()) |
| 9799 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
| 9800 | Recalc = true; |
| 9801 | } |
| 9802 | if (Recalc) { |
| 9803 | ResR = APFloat::getInf(A.getSemantics()) * (A * C - B * D); |
| 9804 | ResI = APFloat::getInf(A.getSemantics()) * (A * D + B * C); |
| 9805 | } |
| 9806 | } |
| 9807 | } |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 9808 | } else { |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9809 | ComplexValue LHS = Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9810 | Result.getComplexIntReal() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 9811 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
| 9812 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9813 | Result.getComplexIntImag() = |
Daniel Dunbar | 0aa2606 | 2009-01-29 01:32:56 +0000 | [diff] [blame] | 9814 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
| 9815 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
| 9816 | } |
| 9817 | break; |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 9818 | case BO_Div: |
| 9819 | if (Result.isComplexFloat()) { |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9820 | // This is an implementation of complex division according to the |
Hiroshi Inoue | 0c2734f | 2017-07-05 05:37:45 +0000 | [diff] [blame] | 9821 | // constraints laid out in C11 Annex G. The implemention uses the |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9822 | // following naming scheme: |
| 9823 | // (a + ib) / (c + id) |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 9824 | ComplexValue LHS = Result; |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9825 | APFloat &A = LHS.getComplexFloatReal(); |
| 9826 | APFloat &B = LHS.getComplexFloatImag(); |
| 9827 | APFloat &C = RHS.getComplexFloatReal(); |
| 9828 | APFloat &D = RHS.getComplexFloatImag(); |
| 9829 | APFloat &ResR = Result.getComplexFloatReal(); |
| 9830 | APFloat &ResI = Result.getComplexFloatImag(); |
| 9831 | if (RHSReal) { |
| 9832 | ResR = A / C; |
| 9833 | ResI = B / C; |
| 9834 | } else { |
| 9835 | if (LHSReal) { |
| 9836 | // No real optimizations we can do here, stub out with zero. |
| 9837 | B = APFloat::getZero(A.getSemantics()); |
| 9838 | } |
| 9839 | int DenomLogB = 0; |
| 9840 | APFloat MaxCD = maxnum(abs(C), abs(D)); |
| 9841 | if (MaxCD.isFinite()) { |
| 9842 | DenomLogB = ilogb(MaxCD); |
Matt Arsenault | c477f48 | 2016-03-13 05:12:47 +0000 | [diff] [blame] | 9843 | C = scalbn(C, -DenomLogB, APFloat::rmNearestTiesToEven); |
| 9844 | D = scalbn(D, -DenomLogB, APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9845 | } |
| 9846 | APFloat Denom = C * C + D * D; |
Matt Arsenault | c477f48 | 2016-03-13 05:12:47 +0000 | [diff] [blame] | 9847 | ResR = scalbn((A * C + B * D) / Denom, -DenomLogB, |
| 9848 | APFloat::rmNearestTiesToEven); |
| 9849 | ResI = scalbn((B * C - A * D) / Denom, -DenomLogB, |
| 9850 | APFloat::rmNearestTiesToEven); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 9851 | if (ResR.isNaN() && ResI.isNaN()) { |
| 9852 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
| 9853 | ResR = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * A; |
| 9854 | ResI = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * B; |
| 9855 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
| 9856 | D.isFinite()) { |
| 9857 | A = APFloat::copySign( |
| 9858 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
| 9859 | B = APFloat::copySign( |
| 9860 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
| 9861 | ResR = APFloat::getInf(ResR.getSemantics()) * (A * C + B * D); |
| 9862 | ResI = APFloat::getInf(ResI.getSemantics()) * (B * C - A * D); |
| 9863 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
| 9864 | C = APFloat::copySign( |
| 9865 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
| 9866 | D = APFloat::copySign( |
| 9867 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
| 9868 | ResR = APFloat::getZero(ResR.getSemantics()) * (A * C + B * D); |
| 9869 | ResI = APFloat::getZero(ResI.getSemantics()) * (B * C - A * D); |
| 9870 | } |
| 9871 | } |
| 9872 | } |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 9873 | } else { |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9874 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
| 9875 | return Error(E, diag::note_expr_divide_by_zero); |
| 9876 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 9877 | ComplexValue LHS = Result; |
| 9878 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 9879 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
| 9880 | Result.getComplexIntReal() = |
| 9881 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
| 9882 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
| 9883 | Result.getComplexIntImag() = |
| 9884 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
| 9885 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
| 9886 | } |
| 9887 | break; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 9888 | } |
| 9889 | |
John McCall | 93d91dc | 2010-05-07 17:22:02 +0000 | [diff] [blame] | 9890 | return true; |
Anders Carlsson | 9ddf7be | 2008-11-16 21:51:21 +0000 | [diff] [blame] | 9891 | } |
| 9892 | |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 9893 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
| 9894 | // Get the operand value into 'Result'. |
| 9895 | if (!Visit(E->getSubExpr())) |
| 9896 | return false; |
| 9897 | |
| 9898 | switch (E->getOpcode()) { |
| 9899 | default: |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 9900 | return Error(E); |
Abramo Bagnara | 9e0e709 | 2010-12-11 16:05:48 +0000 | [diff] [blame] | 9901 | case UO_Extension: |
| 9902 | return true; |
| 9903 | case UO_Plus: |
| 9904 | // The result is always just the subexpr. |
| 9905 | return true; |
| 9906 | case UO_Minus: |
| 9907 | if (Result.isComplexFloat()) { |
| 9908 | Result.getComplexFloatReal().changeSign(); |
| 9909 | Result.getComplexFloatImag().changeSign(); |
| 9910 | } |
| 9911 | else { |
| 9912 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
| 9913 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 9914 | } |
| 9915 | return true; |
| 9916 | case UO_Not: |
| 9917 | if (Result.isComplexFloat()) |
| 9918 | Result.getComplexFloatImag().changeSign(); |
| 9919 | else |
| 9920 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
| 9921 | return true; |
| 9922 | } |
| 9923 | } |
| 9924 | |
Eli Friedman | c4b251d | 2012-01-10 04:58:17 +0000 | [diff] [blame] | 9925 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
| 9926 | if (E->getNumInits() == 2) { |
| 9927 | if (E->getType()->isComplexType()) { |
| 9928 | Result.makeComplexFloat(); |
| 9929 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
| 9930 | return false; |
| 9931 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
| 9932 | return false; |
| 9933 | } else { |
| 9934 | Result.makeComplexInt(); |
| 9935 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
| 9936 | return false; |
| 9937 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
| 9938 | return false; |
| 9939 | } |
| 9940 | return true; |
| 9941 | } |
| 9942 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
| 9943 | } |
| 9944 | |
Anders Carlsson | 537969c | 2008-11-16 20:27:53 +0000 | [diff] [blame] | 9945 | //===----------------------------------------------------------------------===// |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9946 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
| 9947 | // implicit conversion. |
| 9948 | //===----------------------------------------------------------------------===// |
| 9949 | |
| 9950 | namespace { |
| 9951 | class AtomicExprEvaluator : |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 9952 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 9953 | const LValue *This; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9954 | APValue &Result; |
| 9955 | public: |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 9956 | AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result) |
| 9957 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9958 | |
| 9959 | bool Success(const APValue &V, const Expr *E) { |
| 9960 | Result = V; |
| 9961 | return true; |
| 9962 | } |
| 9963 | |
| 9964 | bool ZeroInitialization(const Expr *E) { |
| 9965 | ImplicitValueInitExpr VIE( |
| 9966 | E->getType()->castAs<AtomicType>()->getValueType()); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 9967 | // For atomic-qualified class (and array) types in C++, initialize the |
| 9968 | // _Atomic-wrapped subobject directly, in-place. |
| 9969 | return This ? EvaluateInPlace(Result, Info, *This, &VIE) |
| 9970 | : Evaluate(Result, Info, &VIE); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9971 | } |
| 9972 | |
| 9973 | bool VisitCastExpr(const CastExpr *E) { |
| 9974 | switch (E->getCastKind()) { |
| 9975 | default: |
| 9976 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 9977 | case CK_NonAtomicToAtomic: |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 9978 | return This ? EvaluateInPlace(Result, Info, *This, E->getSubExpr()) |
| 9979 | : Evaluate(Result, Info, E->getSubExpr()); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9980 | } |
| 9981 | } |
| 9982 | }; |
| 9983 | } // end anonymous namespace |
| 9984 | |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 9985 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
| 9986 | EvalInfo &Info) { |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9987 | assert(E->isRValue() && E->getType()->isAtomicType()); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 9988 | return AtomicExprEvaluator(Info, This, Result).Visit(E); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 9989 | } |
| 9990 | |
| 9991 | //===----------------------------------------------------------------------===// |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 9992 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
| 9993 | // comma operator |
| 9994 | //===----------------------------------------------------------------------===// |
| 9995 | |
| 9996 | namespace { |
| 9997 | class VoidExprEvaluator |
Aaron Ballman | 68af21c | 2014-01-03 19:26:43 +0000 | [diff] [blame] | 9998 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 9999 | public: |
| 10000 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
| 10001 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10002 | bool Success(const APValue &V, const Expr *e) { return true; } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10003 | |
Richard Smith | 7cd577b | 2017-08-17 19:35:50 +0000 | [diff] [blame] | 10004 | bool ZeroInitialization(const Expr *E) { return true; } |
| 10005 | |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10006 | bool VisitCastExpr(const CastExpr *E) { |
| 10007 | switch (E->getCastKind()) { |
| 10008 | default: |
| 10009 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
| 10010 | case CK_ToVoid: |
| 10011 | VisitIgnoredValue(E->getSubExpr()); |
| 10012 | return true; |
| 10013 | } |
| 10014 | } |
Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 10015 | |
| 10016 | bool VisitCallExpr(const CallExpr *E) { |
| 10017 | switch (E->getBuiltinCallee()) { |
| 10018 | default: |
| 10019 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
| 10020 | case Builtin::BI__assume: |
Hal Finkel | bcc0608 | 2014-09-07 22:58:14 +0000 | [diff] [blame] | 10021 | case Builtin::BI__builtin_assume: |
Hal Finkel | a8443c3 | 2014-07-17 14:49:58 +0000 | [diff] [blame] | 10022 | // The argument is not evaluated! |
| 10023 | return true; |
| 10024 | } |
| 10025 | } |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10026 | }; |
| 10027 | } // end anonymous namespace |
| 10028 | |
| 10029 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
| 10030 | assert(E->isRValue() && E->getType()->isVoidType()); |
| 10031 | return VoidExprEvaluator(Info).Visit(E); |
| 10032 | } |
| 10033 | |
| 10034 | //===----------------------------------------------------------------------===// |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10035 | // Top level Expr::EvaluateAsRValue method. |
Chris Lattner | 05706e88 | 2008-07-11 18:11:29 +0000 | [diff] [blame] | 10036 | //===----------------------------------------------------------------------===// |
| 10037 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10038 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10039 | // In C, function designators are not lvalues, but we evaluate them as if they |
| 10040 | // are. |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10041 | QualType T = E->getType(); |
| 10042 | if (E->isGLValue() || T->isFunctionType()) { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10043 | LValue LV; |
| 10044 | if (!EvaluateLValue(E, LV, Info)) |
| 10045 | return false; |
| 10046 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10047 | } else if (T->isVectorType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10048 | if (!EvaluateVector(E, Result, Info)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 10049 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10050 | } else if (T->isIntegralOrEnumerationType()) { |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10051 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10052 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10053 | } else if (T->hasPointerRepresentation()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10054 | LValue LV; |
| 10055 | if (!EvaluatePointer(E, LV, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10056 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10057 | LV.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10058 | } else if (T->isRealFloatingType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10059 | llvm::APFloat F(0.0); |
| 10060 | if (!EvaluateFloat(E, F, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10061 | return false; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10062 | Result = APValue(F); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10063 | } else if (T->isAnyComplexType()) { |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10064 | ComplexValue C; |
| 10065 | if (!EvaluateComplex(E, C, Info)) |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10066 | return false; |
Richard Smith | 725810a | 2011-10-16 21:26:27 +0000 | [diff] [blame] | 10067 | C.moveInto(Result); |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10068 | } else if (T->isMemberPointerType()) { |
Richard Smith | 027bf11 | 2011-11-17 22:56:20 +0000 | [diff] [blame] | 10069 | MemberPtr P; |
| 10070 | if (!EvaluateMemberPointer(E, P, Info)) |
| 10071 | return false; |
| 10072 | P.moveInto(Result); |
| 10073 | return true; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10074 | } else if (T->isArrayType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10075 | LValue LV; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 10076 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10077 | if (!EvaluateArray(E, LV, Value, Info)) |
Richard Smith | f3e9e43 | 2011-11-07 09:22:26 +0000 | [diff] [blame] | 10078 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10079 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10080 | } else if (T->isRecordType()) { |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10081 | LValue LV; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 10082 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10083 | if (!EvaluateRecord(E, LV, Value, Info)) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10084 | return false; |
Richard Smith | 08d6a2c | 2013-07-24 07:11:57 +0000 | [diff] [blame] | 10085 | Result = Value; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10086 | } else if (T->isVoidType()) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10087 | if (!Info.getLangOpts().CPlusPlus11) |
Richard Smith | ce1ec5e | 2012-03-15 04:53:45 +0000 | [diff] [blame] | 10088 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10089 | << E->getType(); |
Richard Smith | 42d3af9 | 2011-12-07 00:43:50 +0000 | [diff] [blame] | 10090 | if (!EvaluateVoid(E, Info)) |
| 10091 | return false; |
Richard Smith | a23ab51 | 2013-05-23 00:30:41 +0000 | [diff] [blame] | 10092 | } else if (T->isAtomicType()) { |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10093 | QualType Unqual = T.getAtomicUnqualifiedType(); |
| 10094 | if (Unqual->isArrayType() || Unqual->isRecordType()) { |
| 10095 | LValue LV; |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 10096 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10097 | if (!EvaluateAtomic(E, &LV, Value, Info)) |
| 10098 | return false; |
| 10099 | } else { |
| 10100 | if (!EvaluateAtomic(E, nullptr, Result, Info)) |
| 10101 | return false; |
| 10102 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10103 | } else if (Info.getLangOpts().CPlusPlus11) { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 10104 | Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType(); |
Richard Smith | 357362d | 2011-12-13 06:39:58 +0000 | [diff] [blame] | 10105 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10106 | } else { |
Faisal Vali | e690b7a | 2016-07-02 22:34:24 +0000 | [diff] [blame] | 10107 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
Anders Carlsson | 7c282e4 | 2008-11-22 22:56:32 +0000 | [diff] [blame] | 10108 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10109 | } |
Anders Carlsson | 475f4bc | 2008-11-22 21:50:49 +0000 | [diff] [blame] | 10110 | |
Anders Carlsson | 7b6f0af | 2008-11-30 16:58:53 +0000 | [diff] [blame] | 10111 | return true; |
| 10112 | } |
| 10113 | |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10114 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
| 10115 | /// cases, the in-place evaluation is essential, since later initializers for |
| 10116 | /// an object can indirectly refer to subobjects which were initialized earlier. |
| 10117 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10118 | const Expr *E, bool AllowNonLiteralTypes) { |
Argyrios Kyrtzidis | 3d9e382 | 2014-02-20 04:00:01 +0000 | [diff] [blame] | 10119 | assert(!E->isValueDependent()); |
| 10120 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10121 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10122 | return false; |
| 10123 | |
| 10124 | if (E->isRValue()) { |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 10125 | // Evaluate arrays and record types in-place, so that later initializers can |
| 10126 | // refer to earlier-initialized members of the object. |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10127 | QualType T = E->getType(); |
| 10128 | if (T->isArrayType()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10129 | return EvaluateArray(E, This, Result, Info); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10130 | else if (T->isRecordType()) |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 10131 | return EvaluateRecord(E, This, Result, Info); |
Richard Smith | 64cb9ca | 2017-02-22 22:09:50 +0000 | [diff] [blame] | 10132 | else if (T->isAtomicType()) { |
| 10133 | QualType Unqual = T.getAtomicUnqualifiedType(); |
| 10134 | if (Unqual->isArrayType() || Unqual->isRecordType()) |
| 10135 | return EvaluateAtomic(E, &This, Result, Info); |
| 10136 | } |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 10137 | } |
| 10138 | |
| 10139 | // For any other type, in-place evaluation is unimportant. |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10140 | return Evaluate(Result, Info, E); |
Richard Smith | ed5165f | 2011-11-04 05:33:44 +0000 | [diff] [blame] | 10141 | } |
| 10142 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10143 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
| 10144 | /// lvalue-to-rvalue cast if it is an lvalue. |
| 10145 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 10146 | if (E->getType().isNull()) |
| 10147 | return false; |
| 10148 | |
Nick Lewycky | c190f96 | 2017-05-02 01:06:16 +0000 | [diff] [blame] | 10149 | if (!CheckLiteralType(Info, E)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10150 | return false; |
| 10151 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10152 | if (!::Evaluate(Result, Info, E)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10153 | return false; |
| 10154 | |
| 10155 | if (E->isGLValue()) { |
| 10156 | LValue LV; |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10157 | LV.setFrom(Info.Ctx, Result); |
Richard Smith | 243ef90 | 2013-05-05 23:31:59 +0000 | [diff] [blame] | 10158 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10159 | return false; |
| 10160 | } |
| 10161 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10162 | // Check this core constant expression is a constant expression. |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10163 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10164 | } |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10165 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10166 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 10167 | const ASTContext &Ctx, bool &IsConst) { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10168 | // Fast-path evaluations of integer literals, since we sometimes see files |
| 10169 | // containing vast quantities of these. |
| 10170 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
| 10171 | Result.Val = APValue(APSInt(L->getValue(), |
| 10172 | L->getType()->isUnsignedIntegerType())); |
| 10173 | IsConst = true; |
| 10174 | return true; |
| 10175 | } |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 10176 | |
| 10177 | // This case should be rare, but we need to check it before we check on |
| 10178 | // the type below. |
| 10179 | if (Exp->getType().isNull()) { |
| 10180 | IsConst = false; |
| 10181 | return true; |
| 10182 | } |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 10183 | |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10184 | // FIXME: Evaluating values of large array and record types can cause |
| 10185 | // performance problems. Only do so in C++11 for now. |
| 10186 | if (Exp->isRValue() && (Exp->getType()->isArrayType() || |
| 10187 | Exp->getType()->isRecordType()) && |
Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 10188 | !Ctx.getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10189 | IsConst = false; |
| 10190 | return true; |
| 10191 | } |
| 10192 | return false; |
| 10193 | } |
| 10194 | |
| 10195 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10196 | /// 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] | 10197 | /// any crazy technique (that has nothing to do with language standards) that |
| 10198 | /// 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] | 10199 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
| 10200 | /// will be applied to the result. |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10201 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10202 | bool IsConst; |
Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 10203 | if (FastEvaluateAsRValue(this, Result, Ctx, IsConst)) |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10204 | return IsConst; |
Daniel Jasper | ffdee09 | 2017-05-02 19:21:42 +0000 | [diff] [blame] | 10205 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 10206 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10207 | return ::EvaluateAsRValue(Info, this, Result.Val); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 10208 | } |
| 10209 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 10210 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
| 10211 | const ASTContext &Ctx) const { |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10212 | EvalResult Scratch; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10213 | return EvaluateAsRValue(Scratch, Ctx) && |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10214 | HandleConversionToBool(Scratch.Val, Result); |
John McCall | 1be1c63 | 2010-01-05 23:42:56 +0000 | [diff] [blame] | 10215 | } |
| 10216 | |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 10217 | static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result, |
| 10218 | Expr::SideEffectsKind SEK) { |
| 10219 | return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) || |
| 10220 | (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior); |
| 10221 | } |
| 10222 | |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10223 | bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx, |
| 10224 | SideEffectsKind AllowSideEffects) const { |
| 10225 | if (!getType()->isIntegralOrEnumerationType()) |
| 10226 | return false; |
| 10227 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10228 | EvalResult ExprResult; |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10229 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() || |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 10230 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10231 | return false; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10232 | |
Richard Smith | 11562c5 | 2011-10-28 17:51:58 +0000 | [diff] [blame] | 10233 | Result = ExprResult.Val.getInt(); |
| 10234 | return true; |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 10235 | } |
| 10236 | |
Richard Trieu | be234c3 | 2016-04-21 21:04:55 +0000 | [diff] [blame] | 10237 | bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx, |
| 10238 | SideEffectsKind AllowSideEffects) const { |
| 10239 | if (!getType()->isRealFloatingType()) |
| 10240 | return false; |
| 10241 | |
| 10242 | EvalResult ExprResult; |
| 10243 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isFloat() || |
| 10244 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
| 10245 | return false; |
| 10246 | |
| 10247 | Result = ExprResult.Val.getFloat(); |
| 10248 | return true; |
| 10249 | } |
| 10250 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 10251 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 10252 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
Anders Carlsson | 4316812 | 2009-04-10 04:54:13 +0000 | [diff] [blame] | 10253 | |
John McCall | 45d55e4 | 2010-05-07 21:00:08 +0000 | [diff] [blame] | 10254 | LValue LV; |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10255 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
| 10256 | !CheckLValueConstantExpression(Info, getExprLoc(), |
| 10257 | Ctx.getLValueReferenceType(getType()), LV)) |
| 10258 | return false; |
| 10259 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10260 | LV.moveInto(Result.Val); |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10261 | return true; |
Eli Friedman | 7d45c48 | 2009-09-13 10:17:44 +0000 | [diff] [blame] | 10262 | } |
| 10263 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 10264 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
| 10265 | const VarDecl *VD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10266 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 10267 | // FIXME: Evaluating initializers for large array and record types can cause |
| 10268 | // performance problems. Only do so in C++11 for now. |
| 10269 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10270 | !Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 10271 | return false; |
| 10272 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 10273 | Expr::EvalStatus EStatus; |
| 10274 | EStatus.Diag = &Notes; |
| 10275 | |
Richard Smith | 0c6124b | 2015-12-03 01:36:22 +0000 | [diff] [blame] | 10276 | EvalInfo InitInfo(Ctx, EStatus, VD->isConstexpr() |
| 10277 | ? EvalInfo::EM_ConstantExpression |
| 10278 | : EvalInfo::EM_ConstantFold); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 10279 | InitInfo.setEvaluatingDecl(VD, Value); |
| 10280 | |
| 10281 | LValue LVal; |
| 10282 | LVal.set(VD); |
| 10283 | |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10284 | // C++11 [basic.start.init]p2: |
| 10285 | // Variables with static storage duration or thread storage duration shall be |
| 10286 | // zero-initialized before any other initialization takes place. |
| 10287 | // This behavior is not present in C. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10288 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10289 | !VD->getType()->isReferenceType()) { |
| 10290 | ImplicitValueInitExpr VIE(VD->getType()); |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10291 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10292 | /*AllowNonLiteralTypes=*/true)) |
Richard Smith | fddd384 | 2011-12-30 21:15:51 +0000 | [diff] [blame] | 10293 | return false; |
| 10294 | } |
| 10295 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10296 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, |
| 10297 | /*AllowNonLiteralTypes=*/true) || |
Richard Smith | b228a86 | 2012-02-15 02:18:13 +0000 | [diff] [blame] | 10298 | EStatus.HasSideEffects) |
| 10299 | return false; |
| 10300 | |
| 10301 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
| 10302 | Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 10303 | } |
| 10304 | |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10305 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
| 10306 | /// constant folded, but discard the result. |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 10307 | bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const { |
Anders Carlsson | 5b3638b | 2008-12-01 06:44:05 +0000 | [diff] [blame] | 10308 | EvalResult Result; |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 10309 | return EvaluateAsRValue(Result, Ctx) && |
| 10310 | !hasUnacceptableSideEffect(Result, SEK); |
Chris Lattner | cb13691 | 2008-10-06 06:49:02 +0000 | [diff] [blame] | 10311 | } |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 10312 | |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 10313 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10314 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 10315 | EvalResult EvalResult; |
Fariborz Jahanian | 8b115b7 | 2013-01-09 23:04:56 +0000 | [diff] [blame] | 10316 | EvalResult.Diag = Diag; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10317 | bool Result = EvaluateAsRValue(EvalResult, Ctx); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 10318 | (void)Result; |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 10319 | assert(Result && "Could not evaluate expression"); |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 10320 | assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer"); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 10321 | |
Anders Carlsson | 6736d1a2 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 10322 | return EvalResult.Val.getInt(); |
Anders Carlsson | 59689ed | 2008-11-22 21:04:56 +0000 | [diff] [blame] | 10323 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10324 | |
Richard Smith | e9ff770 | 2013-11-05 22:23:30 +0000 | [diff] [blame] | 10325 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10326 | bool IsConst; |
| 10327 | EvalResult EvalResult; |
Richard Smith | 9f7df0c | 2017-06-26 23:19:32 +0000 | [diff] [blame] | 10328 | if (!FastEvaluateAsRValue(this, EvalResult, Ctx, IsConst)) { |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 10329 | EvalInfo Info(Ctx, EvalResult, EvalInfo::EM_EvaluateForOverflow); |
Fariborz Jahanian | e735ff9 | 2013-01-24 22:11:45 +0000 | [diff] [blame] | 10330 | (void)::EvaluateAsRValue(Info, this, EvalResult.Val); |
| 10331 | } |
| 10332 | } |
| 10333 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 10334 | bool Expr::EvalResult::isGlobalLValue() const { |
| 10335 | assert(Val.isLValue()); |
| 10336 | return IsGlobalLValue(Val.getLValueBase()); |
| 10337 | } |
Abramo Bagnara | f819945 | 2010-05-14 17:07:14 +0000 | [diff] [blame] | 10338 | |
| 10339 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10340 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 10341 | /// an integer constant expression. |
| 10342 | |
| 10343 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 10344 | /// comma, etc |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10345 | |
| 10346 | // CheckICE - This function does the fundamental ICE checking: the returned |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10347 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
| 10348 | // and a (possibly null) SourceLocation indicating the location of the problem. |
| 10349 | // |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10350 | // Note that to reduce code duplication, this helper does no evaluation |
| 10351 | // itself; the caller checks whether the expression is evaluatable, and |
| 10352 | // in the rare cases where CheckICE actually cares about the evaluated |
George Burgess IV | 5731707 | 2017-02-02 07:53:55 +0000 | [diff] [blame] | 10353 | // value, it calls into Evaluate. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10354 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 10355 | namespace { |
| 10356 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10357 | enum ICEKind { |
| 10358 | /// This expression is an ICE. |
| 10359 | IK_ICE, |
| 10360 | /// This expression is not an ICE, but if it isn't evaluated, it's |
| 10361 | /// a legal subexpression for an ICE. This return value is used to handle |
| 10362 | /// the comma operator in C99 mode, and non-constant subexpressions. |
| 10363 | IK_ICEIfUnevaluated, |
| 10364 | /// This expression is not an ICE, and is not a legal subexpression for one. |
| 10365 | IK_NotICE |
| 10366 | }; |
| 10367 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10368 | struct ICEDiag { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10369 | ICEKind Kind; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10370 | SourceLocation Loc; |
| 10371 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10372 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10373 | }; |
| 10374 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 10375 | } |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 10376 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10377 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
| 10378 | |
| 10379 | 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] | 10380 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 10381 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10382 | Expr::EvalResult EVResult; |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10383 | if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects || |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10384 | !EVResult.Val.isInt()) |
| 10385 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 10386 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10387 | return NoDiag(); |
| 10388 | } |
| 10389 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 10390 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10391 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10392 | if (!E->getType()->isIntegralOrEnumerationType()) |
| 10393 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10394 | |
| 10395 | switch (E->getStmtClass()) { |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 10396 | #define ABSTRACT_STMT(Node) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10397 | #define STMT(Node, Base) case Expr::Node##Class: |
| 10398 | #define EXPR(Node, Base) |
| 10399 | #include "clang/AST/StmtNodes.inc" |
| 10400 | case Expr::PredefinedExprClass: |
| 10401 | case Expr::FloatingLiteralClass: |
| 10402 | case Expr::ImaginaryLiteralClass: |
| 10403 | case Expr::StringLiteralClass: |
| 10404 | case Expr::ArraySubscriptExprClass: |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 10405 | case Expr::OMPArraySectionExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10406 | case Expr::MemberExprClass: |
| 10407 | case Expr::CompoundAssignOperatorClass: |
| 10408 | case Expr::CompoundLiteralExprClass: |
| 10409 | case Expr::ExtVectorElementExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10410 | case Expr::DesignatedInitExprClass: |
Richard Smith | 410306b | 2016-12-12 02:53:20 +0000 | [diff] [blame] | 10411 | case Expr::ArrayInitLoopExprClass: |
| 10412 | case Expr::ArrayInitIndexExprClass: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 10413 | case Expr::NoInitExprClass: |
| 10414 | case Expr::DesignatedInitUpdateExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10415 | case Expr::ImplicitValueInitExprClass: |
| 10416 | case Expr::ParenListExprClass: |
| 10417 | case Expr::VAArgExprClass: |
| 10418 | case Expr::AddrLabelExprClass: |
| 10419 | case Expr::StmtExprClass: |
| 10420 | case Expr::CXXMemberCallExprClass: |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 10421 | case Expr::CUDAKernelCallExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10422 | case Expr::CXXDynamicCastExprClass: |
| 10423 | case Expr::CXXTypeidExprClass: |
Francois Pichet | 5cc0a67 | 2010-09-08 23:47:05 +0000 | [diff] [blame] | 10424 | case Expr::CXXUuidofExprClass: |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 10425 | case Expr::MSPropertyRefExprClass: |
Alexey Bataev | f763027 | 2015-11-25 12:01:00 +0000 | [diff] [blame] | 10426 | case Expr::MSPropertySubscriptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10427 | case Expr::CXXNullPtrLiteralExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 10428 | case Expr::UserDefinedLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10429 | case Expr::CXXThisExprClass: |
| 10430 | case Expr::CXXThrowExprClass: |
| 10431 | case Expr::CXXNewExprClass: |
| 10432 | case Expr::CXXDeleteExprClass: |
| 10433 | case Expr::CXXPseudoDestructorExprClass: |
| 10434 | case Expr::UnresolvedLookupExprClass: |
Kaelyn Takata | e1f49d5 | 2014-10-27 18:07:20 +0000 | [diff] [blame] | 10435 | case Expr::TypoExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10436 | case Expr::DependentScopeDeclRefExprClass: |
| 10437 | case Expr::CXXConstructExprClass: |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 10438 | case Expr::CXXInheritedCtorInitExprClass: |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 10439 | case Expr::CXXStdInitializerListExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10440 | case Expr::CXXBindTemporaryExprClass: |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 10441 | case Expr::ExprWithCleanupsClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10442 | case Expr::CXXTemporaryObjectExprClass: |
| 10443 | case Expr::CXXUnresolvedConstructExprClass: |
| 10444 | case Expr::CXXDependentScopeMemberExprClass: |
| 10445 | case Expr::UnresolvedMemberExprClass: |
| 10446 | case Expr::ObjCStringLiteralClass: |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 10447 | case Expr::ObjCBoxedExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 10448 | case Expr::ObjCArrayLiteralClass: |
| 10449 | case Expr::ObjCDictionaryLiteralClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10450 | case Expr::ObjCEncodeExprClass: |
| 10451 | case Expr::ObjCMessageExprClass: |
| 10452 | case Expr::ObjCSelectorExprClass: |
| 10453 | case Expr::ObjCProtocolExprClass: |
| 10454 | case Expr::ObjCIvarRefExprClass: |
| 10455 | case Expr::ObjCPropertyRefExprClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 10456 | case Expr::ObjCSubscriptRefExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10457 | case Expr::ObjCIsaExprClass: |
Erik Pilkington | 29099de | 2016-07-16 00:35:23 +0000 | [diff] [blame] | 10458 | case Expr::ObjCAvailabilityCheckExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10459 | case Expr::ShuffleVectorExprClass: |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 10460 | case Expr::ConvertVectorExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10461 | case Expr::BlockExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10462 | case Expr::NoStmtClass: |
John McCall | 8d69a21 | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 10463 | case Expr::OpaqueValueExprClass: |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 10464 | case Expr::PackExpansionExprClass: |
Douglas Gregor | cdbc539 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 10465 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 10466 | case Expr::FunctionParmPackExprClass: |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 10467 | case Expr::AsTypeExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10468 | case Expr::ObjCIndirectCopyRestoreExprClass: |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 10469 | case Expr::MaterializeTemporaryExprClass: |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 10470 | case Expr::PseudoObjectExprClass: |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 10471 | case Expr::AtomicExprClass: |
Douglas Gregor | e31e606 | 2012-02-07 10:09:13 +0000 | [diff] [blame] | 10472 | case Expr::LambdaExprClass: |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 10473 | case Expr::CXXFoldExprClass: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 10474 | case Expr::CoawaitExprClass: |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 10475 | case Expr::DependentCoawaitExprClass: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 10476 | case Expr::CoyieldExprClass: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10477 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Sebastian Redl | 12757ab | 2011-09-24 17:48:14 +0000 | [diff] [blame] | 10478 | |
Richard Smith | f137f93 | 2014-01-25 20:50:08 +0000 | [diff] [blame] | 10479 | case Expr::InitListExprClass: { |
| 10480 | // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the |
| 10481 | // form "T x = { a };" is equivalent to "T x = a;". |
| 10482 | // Unless we're initializing a reference, T is a scalar as it is known to be |
| 10483 | // of integral or enumeration type. |
| 10484 | if (E->isRValue()) |
| 10485 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
| 10486 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
| 10487 | return ICEDiag(IK_NotICE, E->getLocStart()); |
| 10488 | } |
| 10489 | |
Douglas Gregor | 820ba7b | 2011-01-04 17:33:58 +0000 | [diff] [blame] | 10490 | case Expr::SizeOfPackExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10491 | case Expr::GNUNullExprClass: |
| 10492 | // GCC considers the GNU __null value to be an integral constant expression. |
| 10493 | return NoDiag(); |
| 10494 | |
John McCall | 7c454bb | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 10495 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 10496 | return |
| 10497 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
| 10498 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10499 | case Expr::ParenExprClass: |
| 10500 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 10501 | case Expr::GenericSelectionExprClass: |
| 10502 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10503 | case Expr::IntegerLiteralClass: |
| 10504 | case Expr::CharacterLiteralClass: |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 10505 | case Expr::ObjCBoolLiteralExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10506 | case Expr::CXXBoolLiteralExprClass: |
Douglas Gregor | 747eb78 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 10507 | case Expr::CXXScalarValueInitExprClass: |
Douglas Gregor | 29c42f2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 10508 | case Expr::TypeTraitExprClass: |
John Wiegley | 6242b6a | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 10509 | case Expr::ArrayTypeTraitExprClass: |
John Wiegley | f9f6584 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 10510 | case Expr::ExpressionTraitExprClass: |
Sebastian Redl | 4202c0f | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 10511 | case Expr::CXXNoexceptExprClass: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10512 | return NoDiag(); |
| 10513 | case Expr::CallExprClass: |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 10514 | case Expr::CXXOperatorCallExprClass: { |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 10515 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
| 10516 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 10517 | // contain an operand of (pointer to) function type. |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10518 | const CallExpr *CE = cast<CallExpr>(E); |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 10519 | if (CE->getBuiltinCallee()) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10520 | return CheckEvalInICE(E, Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10521 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10522 | } |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 10523 | case Expr::DeclRefExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10524 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 10525 | return NoDiag(); |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 10526 | const ValueDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10527 | if (Ctx.getLangOpts().CPlusPlus && |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 10528 | D && IsConstNonVolatile(D->getType())) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10529 | // Parameter variables are never constants. Without this check, |
| 10530 | // getAnyInitializer() can find a default argument, which leads |
| 10531 | // to chaos. |
| 10532 | if (isa<ParmVarDecl>(D)) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10533 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10534 | |
| 10535 | // C++ 7.1.5.1p2 |
| 10536 | // A variable of non-volatile const-qualified integral or enumeration |
| 10537 | // type initialized by an ICE can be used in ICEs. |
| 10538 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 10539 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10540 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
Richard Smith | ec8dcd2 | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 10541 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 10542 | const VarDecl *VD; |
| 10543 | // Look for a declaration of this variable that has an initializer, and |
| 10544 | // check whether it is an ICE. |
| 10545 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
| 10546 | return NoDiag(); |
| 10547 | else |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10548 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10549 | } |
| 10550 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10551 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 6365c91 | 2012-02-24 22:12:32 +0000 | [diff] [blame] | 10552 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10553 | case Expr::UnaryOperatorClass: { |
| 10554 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
| 10555 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10556 | case UO_PostInc: |
| 10557 | case UO_PostDec: |
| 10558 | case UO_PreInc: |
| 10559 | case UO_PreDec: |
| 10560 | case UO_AddrOf: |
| 10561 | case UO_Deref: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 10562 | case UO_Coawait: |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 10563 | // C99 6.6/3 allows increment and decrement within unevaluated |
| 10564 | // subexpressions of constant expressions, but they can never be ICEs |
| 10565 | // because an ICE cannot contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10566 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10567 | case UO_Extension: |
| 10568 | case UO_LNot: |
| 10569 | case UO_Plus: |
| 10570 | case UO_Minus: |
| 10571 | case UO_Not: |
| 10572 | case UO_Real: |
| 10573 | case UO_Imag: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10574 | return CheckICE(Exp->getSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10575 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10576 | |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10577 | // OffsetOf falls through here. |
Galina Kistanova | f87496d | 2017-06-03 06:31:42 +0000 | [diff] [blame] | 10578 | LLVM_FALLTHROUGH; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10579 | } |
| 10580 | case Expr::OffsetOfExprClass: { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10581 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 10582 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
| 10583 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
| 10584 | // compliance: we should warn earlier for offsetof expressions with |
| 10585 | // array subscripts that aren't ICEs, and if the array subscripts |
| 10586 | // are ICEs, the value of the offsetof must be an integer constant. |
| 10587 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10588 | } |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 10589 | case Expr::UnaryExprOrTypeTraitExprClass: { |
| 10590 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
| 10591 | if ((Exp->getKind() == UETT_SizeOf) && |
| 10592 | Exp->getTypeOfArgument()->isVariableArrayType()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10593 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10594 | return NoDiag(); |
| 10595 | } |
| 10596 | case Expr::BinaryOperatorClass: { |
| 10597 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
| 10598 | switch (Exp->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10599 | case BO_PtrMemD: |
| 10600 | case BO_PtrMemI: |
| 10601 | case BO_Assign: |
| 10602 | case BO_MulAssign: |
| 10603 | case BO_DivAssign: |
| 10604 | case BO_RemAssign: |
| 10605 | case BO_AddAssign: |
| 10606 | case BO_SubAssign: |
| 10607 | case BO_ShlAssign: |
| 10608 | case BO_ShrAssign: |
| 10609 | case BO_AndAssign: |
| 10610 | case BO_XorAssign: |
| 10611 | case BO_OrAssign: |
Richard Smith | c70f1d6 | 2017-12-14 15:16:18 +0000 | [diff] [blame] | 10612 | case BO_Cmp: // FIXME: Re-enable once we can evaluate this. |
Richard Smith | 62f6595 | 2011-10-24 22:35:48 +0000 | [diff] [blame] | 10613 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
| 10614 | // constant expressions, but they can never be ICEs because an ICE cannot |
| 10615 | // contain an lvalue operand. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10616 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10617 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10618 | case BO_Mul: |
| 10619 | case BO_Div: |
| 10620 | case BO_Rem: |
| 10621 | case BO_Add: |
| 10622 | case BO_Sub: |
| 10623 | case BO_Shl: |
| 10624 | case BO_Shr: |
| 10625 | case BO_LT: |
| 10626 | case BO_GT: |
| 10627 | case BO_LE: |
| 10628 | case BO_GE: |
| 10629 | case BO_EQ: |
| 10630 | case BO_NE: |
| 10631 | case BO_And: |
| 10632 | case BO_Xor: |
| 10633 | case BO_Or: |
| 10634 | case BO_Comma: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10635 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 10636 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10637 | if (Exp->getOpcode() == BO_Div || |
| 10638 | Exp->getOpcode() == BO_Rem) { |
Richard Smith | 7b553f1 | 2011-10-29 00:50:52 +0000 | [diff] [blame] | 10639 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10640 | // we don't evaluate one. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10641 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 10642 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10643 | if (REval == 0) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10644 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10645 | if (REval.isSigned() && REval.isAllOnesValue()) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 10646 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10647 | if (LEval.isMinSignedValue()) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10648 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10649 | } |
| 10650 | } |
| 10651 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10652 | if (Exp->getOpcode() == BO_Comma) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10653 | if (Ctx.getLangOpts().C99) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10654 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 10655 | // if it isn't evaluated. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10656 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
| 10657 | return ICEDiag(IK_ICEIfUnevaluated, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10658 | } else { |
| 10659 | // In both C89 and C++, commas in ICEs are illegal. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10660 | return ICEDiag(IK_NotICE, E->getLocStart()); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10661 | } |
| 10662 | } |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10663 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10664 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10665 | case BO_LAnd: |
| 10666 | case BO_LOr: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10667 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 10668 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10669 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10670 | // Rare case where the RHS has a comma "side-effect"; we need |
| 10671 | // to actually check the condition to see whether the side |
| 10672 | // with the comma is evaluated. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 10673 | if ((Exp->getOpcode() == BO_LAnd) != |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 10674 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10675 | return RHSResult; |
| 10676 | return NoDiag(); |
| 10677 | } |
| 10678 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10679 | return Worst(LHSResult, RHSResult); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10680 | } |
| 10681 | } |
Galina Kistanova | f87496d | 2017-06-03 06:31:42 +0000 | [diff] [blame] | 10682 | LLVM_FALLTHROUGH; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10683 | } |
| 10684 | case Expr::ImplicitCastExprClass: |
| 10685 | case Expr::CStyleCastExprClass: |
| 10686 | case Expr::CXXFunctionalCastExprClass: |
| 10687 | case Expr::CXXStaticCastExprClass: |
| 10688 | case Expr::CXXReinterpretCastExprClass: |
Richard Smith | c3e31e7 | 2011-10-24 18:26:35 +0000 | [diff] [blame] | 10689 | case Expr::CXXConstCastExprClass: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10690 | case Expr::ObjCBridgedCastExprClass: { |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10691 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 10692 | if (isa<ExplicitCastExpr>(E)) { |
| 10693 | if (const FloatingLiteral *FL |
| 10694 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
| 10695 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
| 10696 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
| 10697 | APSInt IgnoredVal(DestWidth, !DestSigned); |
| 10698 | bool Ignored; |
| 10699 | // If the value does not fit in the destination type, the behavior is |
| 10700 | // undefined, so we are not required to treat it as a constant |
| 10701 | // expression. |
| 10702 | if (FL->getValue().convertToInteger(IgnoredVal, |
| 10703 | llvm::APFloat::rmTowardZero, |
| 10704 | &Ignored) & APFloat::opInvalidOp) |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10705 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Richard Smith | 0b973d0 | 2011-12-18 02:33:09 +0000 | [diff] [blame] | 10706 | return NoDiag(); |
| 10707 | } |
| 10708 | } |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 10709 | switch (cast<CastExpr>(E)->getCastKind()) { |
| 10710 | case CK_LValueToRValue: |
David Chisnall | fa35df6 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 10711 | case CK_AtomicToNonAtomic: |
| 10712 | case CK_NonAtomicToAtomic: |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 10713 | case CK_NoOp: |
| 10714 | case CK_IntegralToBoolean: |
| 10715 | case CK_IntegralCast: |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10716 | return CheckICE(SubExpr, Ctx); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 10717 | default: |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10718 | return ICEDiag(IK_NotICE, E->getLocStart()); |
Eli Friedman | 76d4e43 | 2011-09-29 21:49:34 +0000 | [diff] [blame] | 10719 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10720 | } |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 10721 | case Expr::BinaryConditionalOperatorClass: { |
| 10722 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
| 10723 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10724 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 10725 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10726 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
| 10727 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
| 10728 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
Richard Smith | 74fc721 | 2012-12-28 12:53:55 +0000 | [diff] [blame] | 10729 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 10730 | return FalseResult; |
| 10731 | } |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10732 | case Expr::ConditionalOperatorClass: { |
| 10733 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
| 10734 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 10735 | // then only the true side is actually considered in an integer constant |
| 10736 | // expression, and it is fully evaluated. This is an important GNU |
| 10737 | // extension. See GCC PR38377 for discussion. |
| 10738 | if (const CallExpr *CallCE |
| 10739 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 10740 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
Richard Smith | 5fab0c9 | 2011-12-28 19:48:30 +0000 | [diff] [blame] | 10741 | return CheckEvalInICE(E, Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10742 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10743 | if (CondResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10744 | return CondResult; |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 10745 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10746 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 10747 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
Douglas Gregor | fcafc6e | 2011-05-24 16:02:01 +0000 | [diff] [blame] | 10748 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10749 | if (TrueResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10750 | return TrueResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10751 | if (FalseResult.Kind == IK_NotICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10752 | return FalseResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10753 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10754 | return CondResult; |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10755 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10756 | return NoDiag(); |
| 10757 | // Rare case where the diagnostics depend on which side is evaluated |
| 10758 | // Note that if we get here, CondResult is 0, and at least one of |
| 10759 | // TrueResult and FalseResult is non-zero. |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10760 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10761 | return FalseResult; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10762 | return TrueResult; |
| 10763 | } |
| 10764 | case Expr::CXXDefaultArgExprClass: |
| 10765 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 10766 | case Expr::CXXDefaultInitExprClass: |
| 10767 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10768 | case Expr::ChooseExprClass: { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 10769 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10770 | } |
| 10771 | } |
| 10772 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 10773 | llvm_unreachable("Invalid StmtClass!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10774 | } |
| 10775 | |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10776 | /// Evaluate an expression as a C++11 integral constant expression. |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 10777 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10778 | const Expr *E, |
| 10779 | llvm::APSInt *Value, |
| 10780 | SourceLocation *Loc) { |
| 10781 | if (!E->getType()->isIntegralOrEnumerationType()) { |
| 10782 | if (Loc) *Loc = E->getExprLoc(); |
| 10783 | return false; |
| 10784 | } |
| 10785 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 10786 | APValue Result; |
| 10787 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 10788 | return false; |
| 10789 | |
Richard Smith | 98710fc | 2014-11-13 23:03:19 +0000 | [diff] [blame] | 10790 | if (!Result.isInt()) { |
| 10791 | if (Loc) *Loc = E->getExprLoc(); |
| 10792 | return false; |
| 10793 | } |
| 10794 | |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 10795 | if (Value) *Value = Result.getInt(); |
Richard Smith | 92b1ce0 | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 10796 | return true; |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10797 | } |
| 10798 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 10799 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
| 10800 | SourceLocation *Loc) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10801 | if (Ctx.getLangOpts().CPlusPlus11) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 10802 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr, Loc); |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10803 | |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10804 | ICEDiag D = CheckICE(this, Ctx); |
| 10805 | if (D.Kind != IK_ICE) { |
| 10806 | if (Loc) *Loc = D.Loc; |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10807 | return false; |
| 10808 | } |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10809 | return true; |
| 10810 | } |
| 10811 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 10812 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx, |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10813 | SourceLocation *Loc, bool isEvaluated) const { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 10814 | if (Ctx.getLangOpts().CPlusPlus11) |
Richard Smith | f57d8cb | 2011-12-09 22:58:01 +0000 | [diff] [blame] | 10815 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
| 10816 | |
| 10817 | if (!isIntegerConstantExpr(Ctx, Loc)) |
| 10818 | return false; |
Richard Smith | 5c40f09 | 2015-12-04 03:00:44 +0000 | [diff] [blame] | 10819 | // The only possible side-effects here are due to UB discovered in the |
| 10820 | // evaluation (for instance, INT_MAX + 1). In such a case, we are still |
| 10821 | // required to treat the expression as an ICE, so we produce the folded |
| 10822 | // value. |
| 10823 | if (!EvaluateAsInt(Value, Ctx, SE_AllowSideEffects)) |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10824 | llvm_unreachable("ICE cannot be evaluated!"); |
John McCall | 864e396 | 2010-05-07 05:32:02 +0000 | [diff] [blame] | 10825 | return true; |
| 10826 | } |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 10827 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 10828 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
Richard Smith | 9e575da | 2012-12-28 13:25:52 +0000 | [diff] [blame] | 10829 | return CheckICE(this, Ctx).Kind == IK_ICE; |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 10830 | } |
| 10831 | |
Craig Topper | a31a882 | 2013-08-22 07:09:37 +0000 | [diff] [blame] | 10832 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 10833 | SourceLocation *Loc) const { |
| 10834 | // We support this checking in C++98 mode in order to diagnose compatibility |
| 10835 | // issues. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10836 | assert(Ctx.getLangOpts().CPlusPlus); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 10837 | |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 10838 | // Build evaluation settings. |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 10839 | Expr::EvalStatus Status; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10840 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 10841 | Status.Diag = &Diags; |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 10842 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
Richard Smith | 66e05fe | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 10843 | |
| 10844 | APValue Scratch; |
| 10845 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
| 10846 | |
| 10847 | if (!Diags.empty()) { |
| 10848 | IsConstExpr = false; |
| 10849 | if (Loc) *Loc = Diags[0].first; |
| 10850 | } else if (!IsConstExpr) { |
| 10851 | // FIXME: This shouldn't happen. |
| 10852 | if (Loc) *Loc = getExprLoc(); |
| 10853 | } |
| 10854 | |
| 10855 | return IsConstExpr; |
| 10856 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 10857 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10858 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
| 10859 | const FunctionDecl *Callee, |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 10860 | ArrayRef<const Expr*> Args, |
| 10861 | const Expr *This) const { |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10862 | Expr::EvalStatus Status; |
| 10863 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
| 10864 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 10865 | LValue ThisVal; |
| 10866 | const LValue *ThisPtr = nullptr; |
| 10867 | if (This) { |
| 10868 | #ifndef NDEBUG |
| 10869 | auto *MD = dyn_cast<CXXMethodDecl>(Callee); |
| 10870 | assert(MD && "Don't provide `this` for non-methods."); |
| 10871 | assert(!MD->isStatic() && "Don't provide `this` for static methods."); |
| 10872 | #endif |
| 10873 | if (EvaluateObjectArgument(Info, This, ThisVal)) |
| 10874 | ThisPtr = &ThisVal; |
| 10875 | if (Info.EvalStatus.HasSideEffects) |
| 10876 | return false; |
| 10877 | } |
| 10878 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10879 | ArgVector ArgValues(Args.size()); |
| 10880 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
| 10881 | I != E; ++I) { |
Nick Lewycky | f0202ca | 2014-12-16 06:12:01 +0000 | [diff] [blame] | 10882 | if ((*I)->isValueDependent() || |
| 10883 | !Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10884 | // If evaluation fails, throw away the argument entirely. |
| 10885 | ArgValues[I - Args.begin()] = APValue(); |
| 10886 | if (Info.EvalStatus.HasSideEffects) |
| 10887 | return false; |
| 10888 | } |
| 10889 | |
| 10890 | // Build fake call to Callee. |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 10891 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10892 | ArgValues.data()); |
| 10893 | return Evaluate(Value, Info, this) && !Info.EvalStatus.HasSideEffects; |
| 10894 | } |
| 10895 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 10896 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 10897 | SmallVectorImpl< |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 10898 | PartialDiagnosticAt> &Diags) { |
| 10899 | // FIXME: It would be useful to check constexpr function templates, but at the |
| 10900 | // moment the constant expression evaluator cannot cope with the non-rigorous |
| 10901 | // ASTs which we build for dependent expressions. |
| 10902 | if (FD->isDependentContext()) |
| 10903 | return true; |
| 10904 | |
| 10905 | Expr::EvalStatus Status; |
| 10906 | Status.Diag = &Diags; |
| 10907 | |
Richard Smith | 6d4c658 | 2013-11-05 22:18:15 +0000 | [diff] [blame] | 10908 | EvalInfo Info(FD->getASTContext(), Status, |
| 10909 | EvalInfo::EM_PotentialConstantExpression); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 10910 | |
| 10911 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 10912 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 10913 | |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10914 | // Fabricate an arbitrary expression on the stack and pretend that it |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 10915 | // is a temporary being used as the 'this' pointer. |
| 10916 | LValue This; |
| 10917 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
Akira Hatanaka | 4e2698c | 2018-04-10 05:15:01 +0000 | [diff] [blame] | 10918 | This.set({&VIE, Info.CurrentCall->Index}); |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 10919 | |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 10920 | ArrayRef<const Expr*> Args; |
| 10921 | |
Richard Smith | 2e312c8 | 2012-03-03 22:46:17 +0000 | [diff] [blame] | 10922 | APValue Scratch; |
Richard Smith | 7525ff6 | 2013-05-09 07:14:00 +0000 | [diff] [blame] | 10923 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 10924 | // Evaluate the call as a constant initializer, to allow the construction |
| 10925 | // of objects of non-literal types. |
| 10926 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 10927 | HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch); |
| 10928 | } else { |
| 10929 | SourceLocation Loc = FD->getLocation(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 10930 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : nullptr, |
Richard Smith | 52a980a | 2015-08-28 02:43:42 +0000 | [diff] [blame] | 10931 | Args, FD->getBody(), Info, Scratch, nullptr); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 10932 | } |
Richard Smith | 253c2a3 | 2012-01-27 01:14:48 +0000 | [diff] [blame] | 10933 | |
| 10934 | return Diags.empty(); |
| 10935 | } |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10936 | |
| 10937 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
| 10938 | const FunctionDecl *FD, |
| 10939 | SmallVectorImpl< |
| 10940 | PartialDiagnosticAt> &Diags) { |
| 10941 | Expr::EvalStatus Status; |
| 10942 | Status.Diag = &Diags; |
| 10943 | |
| 10944 | EvalInfo Info(FD->getASTContext(), Status, |
| 10945 | EvalInfo::EM_PotentialConstantExpressionUnevaluated); |
| 10946 | |
| 10947 | // Fabricate a call stack frame to give the arguments a plausible cover story. |
| 10948 | ArrayRef<const Expr*> Args; |
| 10949 | ArgVector ArgValues(0); |
| 10950 | bool Success = EvaluateArgs(Args, ArgValues, Info); |
| 10951 | (void)Success; |
| 10952 | assert(Success && |
| 10953 | "Failed to set up arguments for potential constant evaluation"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 10954 | CallStackFrame Frame(Info, SourceLocation(), FD, nullptr, ArgValues.data()); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 10955 | |
| 10956 | APValue ResultScratch; |
| 10957 | Evaluate(ResultScratch, Info, E); |
| 10958 | return Diags.empty(); |
| 10959 | } |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10960 | |
| 10961 | bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
| 10962 | unsigned Type) const { |
| 10963 | if (!getType()->isPointerType()) |
| 10964 | return false; |
| 10965 | |
| 10966 | Expr::EvalStatus Status; |
| 10967 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 10968 | return tryEvaluateBuiltinObjectSize(this, Type, Info, Result); |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 10969 | } |