1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file implements the Expr constant evaluator. |
10 | // |
11 | // Constant expression evaluation produces four main results: |
12 | // |
13 | // * A success/failure flag indicating whether constant folding was successful. |
14 | // This is the 'bool' return value used by most of the code in this file. A |
15 | // 'false' return value indicates that constant folding has failed, and any |
16 | // appropriate diagnostic has already been produced. |
17 | // |
18 | // * An evaluated result, valid only if constant folding has not failed. |
19 | // |
20 | // * A flag indicating if evaluation encountered (unevaluated) side-effects. |
21 | // These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1), |
22 | // where it is possible to determine the evaluated result regardless. |
23 | // |
24 | // * A set of notes indicating why the evaluation was not a constant expression |
25 | // (under the C++11 / C++1y rules only, at the moment), or, if folding failed |
26 | // too, why the expression could not be folded. |
27 | // |
28 | // If we are checking for a potential constant expression, failure to constant |
29 | // fold a potential constant sub-expression will be indicated by a 'false' |
30 | // return value (the expression could not be folded) and no diagnostic (the |
31 | // expression is not necessarily non-constant). |
32 | // |
33 | //===----------------------------------------------------------------------===// |
34 | |
35 | #include "ByteCode/Context.h" |
36 | #include "ByteCode/Frame.h" |
37 | #include "ByteCode/State.h" |
38 | #include "ExprConstShared.h" |
39 | #include "clang/AST/APValue.h" |
40 | #include "clang/AST/ASTContext.h" |
41 | #include "clang/AST/ASTLambda.h" |
42 | #include "clang/AST/Attr.h" |
43 | #include "clang/AST/CXXInheritance.h" |
44 | #include "clang/AST/CharUnits.h" |
45 | #include "clang/AST/CurrentSourceLocExprScope.h" |
46 | #include "clang/AST/Expr.h" |
47 | #include "clang/AST/OSLog.h" |
48 | #include "clang/AST/OptionalDiagnostic.h" |
49 | #include "clang/AST/RecordLayout.h" |
50 | #include "clang/AST/StmtVisitor.h" |
51 | #include "clang/AST/TypeLoc.h" |
52 | #include "clang/Basic/Builtins.h" |
53 | #include "clang/Basic/DiagnosticSema.h" |
54 | #include "clang/Basic/TargetBuiltins.h" |
55 | #include "clang/Basic/TargetInfo.h" |
56 | #include "llvm/ADT/APFixedPoint.h" |
57 | #include "llvm/ADT/Sequence.h" |
58 | #include "llvm/ADT/SmallBitVector.h" |
59 | #include "llvm/ADT/StringExtras.h" |
60 | #include "llvm/Support/Casting.h" |
61 | #include "llvm/Support/Debug.h" |
62 | #include "llvm/Support/SaveAndRestore.h" |
63 | #include "llvm/Support/SipHash.h" |
64 | #include "llvm/Support/TimeProfiler.h" |
65 | #include "llvm/Support/raw_ostream.h" |
66 | #include <cstring> |
67 | #include <functional> |
68 | #include <optional> |
69 | |
70 | #define DEBUG_TYPE "exprconstant" |
71 | |
72 | using namespace clang; |
73 | using llvm::APFixedPoint; |
74 | using llvm::APInt; |
75 | using llvm::APSInt; |
76 | using llvm::APFloat; |
77 | using llvm::FixedPointSemantics; |
78 | |
79 | namespace { |
80 | struct LValue; |
81 | class CallStackFrame; |
82 | class EvalInfo; |
83 | |
84 | using SourceLocExprScopeGuard = |
85 | CurrentSourceLocExprScope::SourceLocExprScopeGuard; |
86 | |
87 | static QualType getType(APValue::LValueBase B) { |
88 | return B.getType(); |
89 | } |
90 | |
91 | /// Get an LValue path entry, which is known to not be an array index, as a |
92 | /// field declaration. |
93 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
94 | return dyn_cast_or_null<FieldDecl>(Val: E.getAsBaseOrMember().getPointer()); |
95 | } |
96 | /// Get an LValue path entry, which is known to not be an array index, as a |
97 | /// base class declaration. |
98 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
99 | return dyn_cast_or_null<CXXRecordDecl>(Val: E.getAsBaseOrMember().getPointer()); |
100 | } |
101 | /// Determine whether this LValue path entry for a base class names a virtual |
102 | /// base class. |
103 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
104 | return E.getAsBaseOrMember().getInt(); |
105 | } |
106 | |
107 | /// Given an expression, determine the type used to store the result of |
108 | /// evaluating that expression. |
109 | static QualType getStorageType(const ASTContext &Ctx, const Expr *E) { |
110 | if (E->isPRValue()) |
111 | return E->getType(); |
112 | return Ctx.getLValueReferenceType(T: E->getType()); |
113 | } |
114 | |
115 | /// Given a CallExpr, try to get the alloc_size attribute. May return null. |
116 | static const AllocSizeAttr *getAllocSizeAttr(const CallExpr *CE) { |
117 | if (const FunctionDecl *DirectCallee = CE->getDirectCallee()) |
118 | return DirectCallee->getAttr<AllocSizeAttr>(); |
119 | if (const Decl *IndirectCallee = CE->getCalleeDecl()) |
120 | return IndirectCallee->getAttr<AllocSizeAttr>(); |
121 | return nullptr; |
122 | } |
123 | |
124 | /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr. |
125 | /// This will look through a single cast. |
126 | /// |
127 | /// Returns null if we couldn't unwrap a function with alloc_size. |
128 | static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) { |
129 | if (!E->getType()->isPointerType()) |
130 | return nullptr; |
131 | |
132 | E = E->IgnoreParens(); |
133 | // If we're doing a variable assignment from e.g. malloc(N), there will |
134 | // probably be a cast of some kind. In exotic cases, we might also see a |
135 | // top-level ExprWithCleanups. Ignore them either way. |
136 | if (const auto *FE = dyn_cast<FullExpr>(Val: E)) |
137 | E = FE->getSubExpr()->IgnoreParens(); |
138 | |
139 | if (const auto *Cast = dyn_cast<CastExpr>(Val: E)) |
140 | E = Cast->getSubExpr()->IgnoreParens(); |
141 | |
142 | if (const auto *CE = dyn_cast<CallExpr>(E)) |
143 | return getAllocSizeAttr(CE) ? CE : nullptr; |
144 | return nullptr; |
145 | } |
146 | |
147 | /// Determines whether or not the given Base contains a call to a function |
148 | /// with the alloc_size attribute. |
149 | static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) { |
150 | const auto *E = Base.dyn_cast<const Expr *>(); |
151 | return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E); |
152 | } |
153 | |
154 | /// Determines whether the given kind of constant expression is only ever |
155 | /// used for name mangling. If so, it's permitted to reference things that we |
156 | /// can't generate code for (in particular, dllimported functions). |
157 | static bool isForManglingOnly(ConstantExprKind Kind) { |
158 | switch (Kind) { |
159 | case ConstantExprKind::Normal: |
160 | case ConstantExprKind::ClassTemplateArgument: |
161 | case ConstantExprKind::ImmediateInvocation: |
162 | // Note that non-type template arguments of class type are emitted as |
163 | // template parameter objects. |
164 | return false; |
165 | |
166 | case ConstantExprKind::NonClassTemplateArgument: |
167 | return true; |
168 | } |
169 | llvm_unreachable("unknown ConstantExprKind"); |
170 | } |
171 | |
172 | static bool isTemplateArgument(ConstantExprKind Kind) { |
173 | switch (Kind) { |
174 | case ConstantExprKind::Normal: |
175 | case ConstantExprKind::ImmediateInvocation: |
176 | return false; |
177 | |
178 | case ConstantExprKind::ClassTemplateArgument: |
179 | case ConstantExprKind::NonClassTemplateArgument: |
180 | return true; |
181 | } |
182 | llvm_unreachable("unknown ConstantExprKind"); |
183 | } |
184 | |
185 | /// The bound to claim that an array of unknown bound has. |
186 | /// The value in MostDerivedArraySize is undefined in this case. So, set it |
187 | /// to an arbitrary value that's likely to loudly break things if it's used. |
188 | static const uint64_t AssumedSizeForUnsizedArray = |
189 | std::numeric_limits<uint64_t>::max() / 2; |
190 | |
191 | /// Determines if an LValue with the given LValueBase will have an unsized |
192 | /// array in its designator. |
193 | /// Find the path length and type of the most-derived subobject in the given |
194 | /// path, and find the size of the containing array, if any. |
195 | static unsigned |
196 | findMostDerivedSubobject(ASTContext &Ctx, APValue::LValueBase Base, |
197 | ArrayRef<APValue::LValuePathEntry> Path, |
198 | uint64_t &ArraySize, QualType &Type, bool &IsArray, |
199 | bool &FirstEntryIsUnsizedArray) { |
200 | // This only accepts LValueBases from APValues, and APValues don't support |
201 | // arrays that lack size info. |
202 | assert(!isBaseAnAllocSizeCall(Base) && |
203 | "Unsized arrays shouldn't appear here"); |
204 | unsigned MostDerivedLength = 0; |
205 | Type = getType(B: Base); |
206 | |
207 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
208 | if (Type->isArrayType()) { |
209 | const ArrayType *AT = Ctx.getAsArrayType(T: Type); |
210 | Type = AT->getElementType(); |
211 | MostDerivedLength = I + 1; |
212 | IsArray = true; |
213 | |
214 | if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) { |
215 | ArraySize = CAT->getZExtSize(); |
216 | } else { |
217 | assert(I == 0 && "unexpected unsized array designator"); |
218 | FirstEntryIsUnsizedArray = true; |
219 | ArraySize = AssumedSizeForUnsizedArray; |
220 | } |
221 | } else if (Type->isAnyComplexType()) { |
222 | const ComplexType *CT = Type->castAs<ComplexType>(); |
223 | Type = CT->getElementType(); |
224 | ArraySize = 2; |
225 | MostDerivedLength = I + 1; |
226 | IsArray = true; |
227 | } else if (const auto *VT = Type->getAs<VectorType>()) { |
228 | Type = VT->getElementType(); |
229 | ArraySize = VT->getNumElements(); |
230 | MostDerivedLength = I + 1; |
231 | IsArray = true; |
232 | } else if (const FieldDecl *FD = getAsField(E: Path[I])) { |
233 | Type = FD->getType(); |
234 | ArraySize = 0; |
235 | MostDerivedLength = I + 1; |
236 | IsArray = false; |
237 | } else { |
238 | // Path[I] describes a base class. |
239 | ArraySize = 0; |
240 | IsArray = false; |
241 | } |
242 | } |
243 | return MostDerivedLength; |
244 | } |
245 | |
246 | /// A path from a glvalue to a subobject of that glvalue. |
247 | struct SubobjectDesignator { |
248 | /// True if the subobject was named in a manner not supported by C++11. Such |
249 | /// lvalues can still be folded, but they are not core constant expressions |
250 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
251 | LLVM_PREFERRED_TYPE(bool) |
252 | unsigned Invalid : 1; |
253 | |
254 | /// Is this a pointer one past the end of an object? |
255 | LLVM_PREFERRED_TYPE(bool) |
256 | unsigned IsOnePastTheEnd : 1; |
257 | |
258 | /// Indicator of whether the first entry is an unsized array. |
259 | LLVM_PREFERRED_TYPE(bool) |
260 | unsigned FirstEntryIsAnUnsizedArray : 1; |
261 | |
262 | /// Indicator of whether the most-derived object is an array element. |
263 | LLVM_PREFERRED_TYPE(bool) |
264 | unsigned MostDerivedIsArrayElement : 1; |
265 | |
266 | /// The length of the path to the most-derived object of which this is a |
267 | /// subobject. |
268 | unsigned MostDerivedPathLength : 28; |
269 | |
270 | /// The size of the array of which the most-derived object is an element. |
271 | /// This will always be 0 if the most-derived object is not an array |
272 | /// element. 0 is not an indicator of whether or not the most-derived object |
273 | /// is an array, however, because 0-length arrays are allowed. |
274 | /// |
275 | /// If the current array is an unsized array, the value of this is |
276 | /// undefined. |
277 | uint64_t MostDerivedArraySize; |
278 | /// The type of the most derived object referred to by this address. |
279 | QualType MostDerivedType; |
280 | |
281 | typedef APValue::LValuePathEntry PathEntry; |
282 | |
283 | /// The entries on the path from the glvalue to the designated subobject. |
284 | SmallVector<PathEntry, 8> Entries; |
285 | |
286 | SubobjectDesignator() : Invalid(true) {} |
287 | |
288 | explicit SubobjectDesignator(QualType T) |
289 | : Invalid(false), IsOnePastTheEnd(false), |
290 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
291 | MostDerivedPathLength(0), MostDerivedArraySize(0), |
292 | MostDerivedType(T) {} |
293 | |
294 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
295 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
296 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
297 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
298 | assert(V.isLValue() && "Non-LValue used to make an LValue designator?"); |
299 | if (!Invalid) { |
300 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
301 | llvm::append_range(C&: Entries, R: V.getLValuePath()); |
302 | if (V.getLValueBase()) { |
303 | bool IsArray = false; |
304 | bool FirstIsUnsizedArray = false; |
305 | MostDerivedPathLength = findMostDerivedSubobject( |
306 | Ctx, V.getLValueBase(), V.getLValuePath(), MostDerivedArraySize, |
307 | MostDerivedType, IsArray, FirstIsUnsizedArray); |
308 | MostDerivedIsArrayElement = IsArray; |
309 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
310 | } |
311 | } |
312 | } |
313 | |
314 | void truncate(ASTContext &Ctx, APValue::LValueBase Base, |
315 | unsigned NewLength) { |
316 | if (Invalid) |
317 | return; |
318 | |
319 | assert(Base && "cannot truncate path for null pointer"); |
320 | assert(NewLength <= Entries.size() && "not a truncation"); |
321 | |
322 | if (NewLength == Entries.size()) |
323 | return; |
324 | Entries.resize(N: NewLength); |
325 | |
326 | bool IsArray = false; |
327 | bool FirstIsUnsizedArray = false; |
328 | MostDerivedPathLength = findMostDerivedSubobject( |
329 | Ctx, Base, Entries, MostDerivedArraySize, MostDerivedType, IsArray, |
330 | FirstIsUnsizedArray); |
331 | MostDerivedIsArrayElement = IsArray; |
332 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
333 | } |
334 | |
335 | void setInvalid() { |
336 | Invalid = true; |
337 | Entries.clear(); |
338 | } |
339 | |
340 | /// Determine whether the most derived subobject is an array without a |
341 | /// known bound. |
342 | bool isMostDerivedAnUnsizedArray() const { |
343 | assert(!Invalid && "Calling this makes no sense on invalid designators"); |
344 | return Entries.size() == 1 && FirstEntryIsAnUnsizedArray; |
345 | } |
346 | |
347 | /// Determine what the most derived array's size is. Results in an assertion |
348 | /// failure if the most derived array lacks a size. |
349 | uint64_t getMostDerivedArraySize() const { |
350 | assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size"); |
351 | return MostDerivedArraySize; |
352 | } |
353 | |
354 | /// Determine whether this is a one-past-the-end pointer. |
355 | bool isOnePastTheEnd() const { |
356 | assert(!Invalid); |
357 | if (IsOnePastTheEnd) |
358 | return true; |
359 | if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement && |
360 | Entries[MostDerivedPathLength - 1].getAsArrayIndex() == |
361 | MostDerivedArraySize) |
362 | return true; |
363 | return false; |
364 | } |
365 | |
366 | /// Get the range of valid index adjustments in the form |
367 | /// {maximum value that can be subtracted from this pointer, |
368 | /// maximum value that can be added to this pointer} |
369 | std::pair<uint64_t, uint64_t> validIndexAdjustments() { |
370 | if (Invalid || isMostDerivedAnUnsizedArray()) |
371 | return {0, 0}; |
372 | |
373 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
374 | // nonarray object behaves the same as a pointer to the first element of |
375 | // an array of length one with the type of the object as its element type. |
376 | bool IsArray = MostDerivedPathLength == Entries.size() && |
377 | MostDerivedIsArrayElement; |
378 | uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex() |
379 | : (uint64_t)IsOnePastTheEnd; |
380 | uint64_t ArraySize = |
381 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
382 | return {ArrayIndex, ArraySize - ArrayIndex}; |
383 | } |
384 | |
385 | /// Check that this refers to a valid subobject. |
386 | bool isValidSubobject() const { |
387 | if (Invalid) |
388 | return false; |
389 | return !isOnePastTheEnd(); |
390 | } |
391 | /// Check that this refers to a valid subobject, and if not, produce a |
392 | /// relevant diagnostic and set the designator as invalid. |
393 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
394 | |
395 | /// Get the type of the designated object. |
396 | QualType getType(ASTContext &Ctx) const { |
397 | assert(!Invalid && "invalid designator has no subobject type"); |
398 | return MostDerivedPathLength == Entries.size() |
399 | ? MostDerivedType |
400 | : Ctx.getRecordType(getAsBaseClass(Entries.back())); |
401 | } |
402 | |
403 | /// Update this designator to refer to the first element within this array. |
404 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
405 | Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0)); |
406 | |
407 | // This is a most-derived object. |
408 | MostDerivedType = CAT->getElementType(); |
409 | MostDerivedIsArrayElement = true; |
410 | MostDerivedArraySize = CAT->getZExtSize(); |
411 | MostDerivedPathLength = Entries.size(); |
412 | } |
413 | /// Update this designator to refer to the first element within the array of |
414 | /// elements of type T. This is an array of unknown size. |
415 | void addUnsizedArrayUnchecked(QualType ElemTy) { |
416 | Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0)); |
417 | |
418 | MostDerivedType = ElemTy; |
419 | MostDerivedIsArrayElement = true; |
420 | // The value in MostDerivedArraySize is undefined in this case. So, set it |
421 | // to an arbitrary value that's likely to loudly break things if it's |
422 | // used. |
423 | MostDerivedArraySize = AssumedSizeForUnsizedArray; |
424 | MostDerivedPathLength = Entries.size(); |
425 | } |
426 | /// Update this designator to refer to the given base or member of this |
427 | /// object. |
428 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
429 | Entries.push_back(Elt: APValue::BaseOrMemberType(D, Virtual)); |
430 | |
431 | // If this isn't a base class, it's a new most-derived object. |
432 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: D)) { |
433 | MostDerivedType = FD->getType(); |
434 | MostDerivedIsArrayElement = false; |
435 | MostDerivedArraySize = 0; |
436 | MostDerivedPathLength = Entries.size(); |
437 | } |
438 | } |
439 | /// Update this designator to refer to the given complex component. |
440 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
441 | Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Imag)); |
442 | |
443 | // This is technically a most-derived object, though in practice this |
444 | // is unlikely to matter. |
445 | MostDerivedType = EltTy; |
446 | MostDerivedIsArrayElement = true; |
447 | MostDerivedArraySize = 2; |
448 | MostDerivedPathLength = Entries.size(); |
449 | } |
450 | |
451 | void addVectorElementUnchecked(QualType EltTy, uint64_t Size, |
452 | uint64_t Idx) { |
453 | Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Idx)); |
454 | MostDerivedType = EltTy; |
455 | MostDerivedPathLength = Entries.size(); |
456 | MostDerivedArraySize = 0; |
457 | MostDerivedIsArrayElement = false; |
458 | } |
459 | |
460 | void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E); |
461 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, |
462 | const APSInt &N); |
463 | /// Add N to the address of this subobject. |
464 | void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N) { |
465 | if (Invalid || !N) return; |
466 | uint64_t TruncatedN = N.extOrTrunc(width: 64).getZExtValue(); |
467 | if (isMostDerivedAnUnsizedArray()) { |
468 | diagnoseUnsizedArrayPointerArithmetic(Info, E); |
469 | // Can't verify -- trust that the user is doing the right thing (or if |
470 | // not, trust that the caller will catch the bad behavior). |
471 | // FIXME: Should we reject if this overflows, at least? |
472 | Entries.back() = PathEntry::ArrayIndex( |
473 | Index: Entries.back().getAsArrayIndex() + TruncatedN); |
474 | return; |
475 | } |
476 | |
477 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
478 | // nonarray object behaves the same as a pointer to the first element of |
479 | // an array of length one with the type of the object as its element type. |
480 | bool IsArray = MostDerivedPathLength == Entries.size() && |
481 | MostDerivedIsArrayElement; |
482 | uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex() |
483 | : (uint64_t)IsOnePastTheEnd; |
484 | uint64_t ArraySize = |
485 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
486 | |
487 | if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) { |
488 | // Calculate the actual index in a wide enough type, so we can include |
489 | // it in the note. |
490 | N = N.extend(width: std::max<unsigned>(a: N.getBitWidth() + 1, b: 65)); |
491 | (llvm::APInt&)N += ArrayIndex; |
492 | assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index"); |
493 | diagnosePointerArithmetic(Info, E, N); |
494 | setInvalid(); |
495 | return; |
496 | } |
497 | |
498 | ArrayIndex += TruncatedN; |
499 | assert(ArrayIndex <= ArraySize && |
500 | "bounds check succeeded for out-of-bounds index"); |
501 | |
502 | if (IsArray) |
503 | Entries.back() = PathEntry::ArrayIndex(Index: ArrayIndex); |
504 | else |
505 | IsOnePastTheEnd = (ArrayIndex != 0); |
506 | } |
507 | }; |
508 | |
509 | /// A scope at the end of which an object can need to be destroyed. |
510 | enum class ScopeKind { |
511 | Block, |
512 | FullExpression, |
513 | Call |
514 | }; |
515 | |
516 | /// A reference to a particular call and its arguments. |
517 | struct CallRef { |
518 | CallRef() : OrigCallee(), CallIndex(0), Version() {} |
519 | CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version) |
520 | : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {} |
521 | |
522 | explicit operator bool() const { return OrigCallee; } |
523 | |
524 | /// Get the parameter that the caller initialized, corresponding to the |
525 | /// given parameter in the callee. |
526 | const ParmVarDecl *getOrigParam(const ParmVarDecl *PVD) const { |
527 | return OrigCallee ? OrigCallee->getParamDecl(i: PVD->getFunctionScopeIndex()) |
528 | : PVD; |
529 | } |
530 | |
531 | /// The callee at the point where the arguments were evaluated. This might |
532 | /// be different from the actual callee (a different redeclaration, or a |
533 | /// virtual override), but this function's parameters are the ones that |
534 | /// appear in the parameter map. |
535 | const FunctionDecl *OrigCallee; |
536 | /// The call index of the frame that holds the argument values. |
537 | unsigned CallIndex; |
538 | /// The version of the parameters corresponding to this call. |
539 | unsigned Version; |
540 | }; |
541 | |
542 | /// A stack frame in the constexpr call stack. |
543 | class CallStackFrame : public interp::Frame { |
544 | public: |
545 | EvalInfo &Info; |
546 | |
547 | /// Parent - The caller of this stack frame. |
548 | CallStackFrame *Caller; |
549 | |
550 | /// Callee - The function which was called. |
551 | const FunctionDecl *Callee; |
552 | |
553 | /// This - The binding for the this pointer in this call, if any. |
554 | const LValue *This; |
555 | |
556 | /// CallExpr - The syntactical structure of member function calls |
557 | const Expr *CallExpr; |
558 | |
559 | /// Information on how to find the arguments to this call. Our arguments |
560 | /// are stored in our parent's CallStackFrame, using the ParmVarDecl* as a |
561 | /// key and this value as the version. |
562 | CallRef Arguments; |
563 | |
564 | /// Source location information about the default argument or default |
565 | /// initializer expression we're evaluating, if any. |
566 | CurrentSourceLocExprScope CurSourceLocExprScope; |
567 | |
568 | // Note that we intentionally use std::map here so that references to |
569 | // values are stable. |
570 | typedef std::pair<const void *, unsigned> MapKeyTy; |
571 | typedef std::map<MapKeyTy, APValue> MapTy; |
572 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
573 | MapTy Temporaries; |
574 | MapTy ConstexprUnknownAPValues; |
575 | |
576 | /// CallRange - The source range of the call expression for this call. |
577 | SourceRange CallRange; |
578 | |
579 | /// Index - The call index of this call. |
580 | unsigned Index; |
581 | |
582 | /// The stack of integers for tracking version numbers for temporaries. |
583 | SmallVector<unsigned, 2> TempVersionStack = {1}; |
584 | unsigned CurTempVersion = TempVersionStack.back(); |
585 | |
586 | unsigned getTempVersion() const { return TempVersionStack.back(); } |
587 | |
588 | void pushTempVersion() { |
589 | TempVersionStack.push_back(Elt: ++CurTempVersion); |
590 | } |
591 | |
592 | void popTempVersion() { |
593 | TempVersionStack.pop_back(); |
594 | } |
595 | |
596 | CallRef createCall(const FunctionDecl *Callee) { |
597 | return {Callee, Index, ++CurTempVersion}; |
598 | } |
599 | |
600 | // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact |
601 | // on the overall stack usage of deeply-recursing constexpr evaluations. |
602 | // (We should cache this map rather than recomputing it repeatedly.) |
603 | // But let's try this and see how it goes; we can look into caching the map |
604 | // as a later change. |
605 | |
606 | /// LambdaCaptureFields - Mapping from captured variables/this to |
607 | /// corresponding data members in the closure class. |
608 | llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields; |
609 | FieldDecl *LambdaThisCaptureField = nullptr; |
610 | |
611 | CallStackFrame(EvalInfo &Info, SourceRange CallRange, |
612 | const FunctionDecl *Callee, const LValue *This, |
613 | const Expr *CallExpr, CallRef Arguments); |
614 | ~CallStackFrame(); |
615 | |
616 | // Return the temporary for Key whose version number is Version. |
617 | APValue *getTemporary(const void *Key, unsigned Version) { |
618 | MapKeyTy KV(Key, Version); |
619 | auto LB = Temporaries.lower_bound(x: KV); |
620 | if (LB != Temporaries.end() && LB->first == KV) |
621 | return &LB->second; |
622 | return nullptr; |
623 | } |
624 | |
625 | // Return the current temporary for Key in the map. |
626 | APValue *getCurrentTemporary(const void *Key) { |
627 | auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX)); |
628 | if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key) |
629 | return &std::prev(x: UB)->second; |
630 | return nullptr; |
631 | } |
632 | |
633 | // Return the version number of the current temporary for Key. |
634 | unsigned getCurrentTemporaryVersion(const void *Key) const { |
635 | auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX)); |
636 | if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key) |
637 | return std::prev(x: UB)->first.second; |
638 | return 0; |
639 | } |
640 | |
641 | /// Allocate storage for an object of type T in this stack frame. |
642 | /// Populates LV with a handle to the created object. Key identifies |
643 | /// the temporary within the stack frame, and must not be reused without |
644 | /// bumping the temporary version number. |
645 | template<typename KeyT> |
646 | APValue &createTemporary(const KeyT *Key, QualType T, |
647 | ScopeKind Scope, LValue &LV); |
648 | |
649 | APValue &createConstexprUnknownAPValues(const VarDecl *Key, |
650 | APValue::LValueBase Base); |
651 | |
652 | /// Allocate storage for a parameter of a function call made in this frame. |
653 | APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV); |
654 | |
655 | void describe(llvm::raw_ostream &OS) const override; |
656 | |
657 | Frame *getCaller() const override { return Caller; } |
658 | SourceRange getCallRange() const override { return CallRange; } |
659 | const FunctionDecl *getCallee() const override { return Callee; } |
660 | |
661 | bool isStdFunction() const { |
662 | for (const DeclContext *DC = Callee; DC; DC = DC->getParent()) |
663 | if (DC->isStdNamespace()) |
664 | return true; |
665 | return false; |
666 | } |
667 | |
668 | /// Whether we're in a context where [[msvc::constexpr]] evaluation is |
669 | /// permitted. See MSConstexprDocs for description of permitted contexts. |
670 | bool CanEvalMSConstexpr = false; |
671 | |
672 | private: |
673 | APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T, |
674 | ScopeKind Scope); |
675 | }; |
676 | |
677 | /// Temporarily override 'this'. |
678 | class ThisOverrideRAII { |
679 | public: |
680 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
681 | : Frame(Frame), OldThis(Frame.This) { |
682 | if (Enable) |
683 | Frame.This = NewThis; |
684 | } |
685 | ~ThisOverrideRAII() { |
686 | Frame.This = OldThis; |
687 | } |
688 | private: |
689 | CallStackFrame &Frame; |
690 | const LValue *OldThis; |
691 | }; |
692 | |
693 | // A shorthand time trace scope struct, prints source range, for example |
694 | // {"name":"EvaluateAsRValue","args":{"detail":"<test.cc:8:21, col:25>"}}} |
695 | class ExprTimeTraceScope { |
696 | public: |
697 | ExprTimeTraceScope(const Expr *E, const ASTContext &Ctx, StringRef Name) |
698 | : TimeScope(Name, [E, &Ctx] { |
699 | return E->getSourceRange().printToString(Ctx.getSourceManager()); |
700 | }) {} |
701 | |
702 | private: |
703 | llvm::TimeTraceScope TimeScope; |
704 | }; |
705 | |
706 | /// RAII object used to change the current ability of |
707 | /// [[msvc::constexpr]] evaulation. |
708 | struct MSConstexprContextRAII { |
709 | CallStackFrame &Frame; |
710 | bool OldValue; |
711 | explicit MSConstexprContextRAII(CallStackFrame &Frame, bool Value) |
712 | : Frame(Frame), OldValue(Frame.CanEvalMSConstexpr) { |
713 | Frame.CanEvalMSConstexpr = Value; |
714 | } |
715 | |
716 | ~MSConstexprContextRAII() { Frame.CanEvalMSConstexpr = OldValue; } |
717 | }; |
718 | } |
719 | |
720 | static bool HandleDestruction(EvalInfo &Info, const Expr *E, |
721 | const LValue &This, QualType ThisType); |
722 | static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc, |
723 | APValue::LValueBase LVBase, APValue &Value, |
724 | QualType T); |
725 | |
726 | namespace { |
727 | /// A cleanup, and a flag indicating whether it is lifetime-extended. |
728 | class Cleanup { |
729 | llvm::PointerIntPair<APValue*, 2, ScopeKind> Value; |
730 | APValue::LValueBase Base; |
731 | QualType T; |
732 | |
733 | public: |
734 | Cleanup(APValue *Val, APValue::LValueBase Base, QualType T, |
735 | ScopeKind Scope) |
736 | : Value(Val, Scope), Base(Base), T(T) {} |
737 | |
738 | /// Determine whether this cleanup should be performed at the end of the |
739 | /// given kind of scope. |
740 | bool isDestroyedAtEndOf(ScopeKind K) const { |
741 | return (int)Value.getInt() >= (int)K; |
742 | } |
743 | bool endLifetime(EvalInfo &Info, bool RunDestructors) { |
744 | if (RunDestructors) { |
745 | SourceLocation Loc; |
746 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) |
747 | Loc = VD->getLocation(); |
748 | else if (const Expr *E = Base.dyn_cast<const Expr*>()) |
749 | Loc = E->getExprLoc(); |
750 | return HandleDestruction(Info, Loc, Base, *Value.getPointer(), T); |
751 | } |
752 | *Value.getPointer() = APValue(); |
753 | return true; |
754 | } |
755 | |
756 | bool hasSideEffect() { |
757 | return T.isDestructedType(); |
758 | } |
759 | }; |
760 | |
761 | /// A reference to an object whose construction we are currently evaluating. |
762 | struct ObjectUnderConstruction { |
763 | APValue::LValueBase Base; |
764 | ArrayRef<APValue::LValuePathEntry> Path; |
765 | friend bool operator==(const ObjectUnderConstruction &LHS, |
766 | const ObjectUnderConstruction &RHS) { |
767 | return LHS.Base == RHS.Base && LHS.Path == RHS.Path; |
768 | } |
769 | friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) { |
770 | return llvm::hash_combine(args: Obj.Base, args: Obj.Path); |
771 | } |
772 | }; |
773 | enum class ConstructionPhase { |
774 | None, |
775 | Bases, |
776 | AfterBases, |
777 | AfterFields, |
778 | Destroying, |
779 | DestroyingBases |
780 | }; |
781 | } |
782 | |
783 | namespace llvm { |
784 | template<> struct DenseMapInfo<ObjectUnderConstruction> { |
785 | using Base = DenseMapInfo<APValue::LValueBase>; |
786 | static ObjectUnderConstruction getEmptyKey() { |
787 | return {.Base: Base::getEmptyKey(), .Path: {}}; } |
788 | static ObjectUnderConstruction getTombstoneKey() { |
789 | return {.Base: Base::getTombstoneKey(), .Path: {}}; |
790 | } |
791 | static unsigned getHashValue(const ObjectUnderConstruction &Object) { |
792 | return hash_value(Obj: Object); |
793 | } |
794 | static bool isEqual(const ObjectUnderConstruction &LHS, |
795 | const ObjectUnderConstruction &RHS) { |
796 | return LHS == RHS; |
797 | } |
798 | }; |
799 | } |
800 | |
801 | namespace { |
802 | /// A dynamically-allocated heap object. |
803 | struct DynAlloc { |
804 | /// The value of this heap-allocated object. |
805 | APValue Value; |
806 | /// The allocating expression; used for diagnostics. Either a CXXNewExpr |
807 | /// or a CallExpr (the latter is for direct calls to operator new inside |
808 | /// std::allocator<T>::allocate). |
809 | const Expr *AllocExpr = nullptr; |
810 | |
811 | enum Kind { |
812 | New, |
813 | ArrayNew, |
814 | StdAllocator |
815 | }; |
816 | |
817 | /// Get the kind of the allocation. This must match between allocation |
818 | /// and deallocation. |
819 | Kind getKind() const { |
820 | if (auto *NE = dyn_cast<CXXNewExpr>(Val: AllocExpr)) |
821 | return NE->isArray() ? ArrayNew : New; |
822 | assert(isa<CallExpr>(AllocExpr)); |
823 | return StdAllocator; |
824 | } |
825 | }; |
826 | |
827 | struct DynAllocOrder { |
828 | bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const { |
829 | return L.getIndex() < R.getIndex(); |
830 | } |
831 | }; |
832 | |
833 | /// EvalInfo - This is a private struct used by the evaluator to capture |
834 | /// information about a subexpression as it is folded. It retains information |
835 | /// about the AST context, but also maintains information about the folded |
836 | /// expression. |
837 | /// |
838 | /// If an expression could be evaluated, it is still possible it is not a C |
839 | /// "integer constant expression" or constant expression. If not, this struct |
840 | /// captures information about how and why not. |
841 | /// |
842 | /// One bit of information passed *into* the request for constant folding |
843 | /// indicates whether the subexpression is "evaluated" or not according to C |
844 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
845 | /// evaluate the expression regardless of what the RHS is, but C only allows |
846 | /// certain things in certain situations. |
847 | class EvalInfo : public interp::State { |
848 | public: |
849 | ASTContext &Ctx; |
850 | |
851 | /// EvalStatus - Contains information about the evaluation. |
852 | Expr::EvalStatus &EvalStatus; |
853 | |
854 | /// CurrentCall - The top of the constexpr call stack. |
855 | CallStackFrame *CurrentCall; |
856 | |
857 | /// CallStackDepth - The number of calls in the call stack right now. |
858 | unsigned CallStackDepth; |
859 | |
860 | /// NextCallIndex - The next call index to assign. |
861 | unsigned NextCallIndex; |
862 | |
863 | /// StepsLeft - The remaining number of evaluation steps we're permitted |
864 | /// to perform. This is essentially a limit for the number of statements |
865 | /// we will evaluate. |
866 | unsigned StepsLeft; |
867 | |
868 | /// Enable the experimental new constant interpreter. If an expression is |
869 | /// not supported by the interpreter, an error is triggered. |
870 | bool EnableNewConstInterp; |
871 | |
872 | /// BottomFrame - The frame in which evaluation started. This must be |
873 | /// initialized after CurrentCall and CallStackDepth. |
874 | CallStackFrame BottomFrame; |
875 | |
876 | /// A stack of values whose lifetimes end at the end of some surrounding |
877 | /// evaluation frame. |
878 | llvm::SmallVector<Cleanup, 16> CleanupStack; |
879 | |
880 | /// EvaluatingDecl - This is the declaration whose initializer is being |
881 | /// evaluated, if any. |
882 | APValue::LValueBase EvaluatingDecl; |
883 | |
884 | enum class EvaluatingDeclKind { |
885 | None, |
886 | /// We're evaluating the construction of EvaluatingDecl. |
887 | Ctor, |
888 | /// We're evaluating the destruction of EvaluatingDecl. |
889 | Dtor, |
890 | }; |
891 | EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None; |
892 | |
893 | /// EvaluatingDeclValue - This is the value being constructed for the |
894 | /// declaration whose initializer is being evaluated, if any. |
895 | APValue *EvaluatingDeclValue; |
896 | |
897 | /// Set of objects that are currently being constructed. |
898 | llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase> |
899 | ObjectsUnderConstruction; |
900 | |
901 | /// Current heap allocations, along with the location where each was |
902 | /// allocated. We use std::map here because we need stable addresses |
903 | /// for the stored APValues. |
904 | std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs; |
905 | |
906 | /// The number of heap allocations performed so far in this evaluation. |
907 | unsigned NumHeapAllocs = 0; |
908 | |
909 | struct EvaluatingConstructorRAII { |
910 | EvalInfo &EI; |
911 | ObjectUnderConstruction Object; |
912 | bool DidInsert; |
913 | EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object, |
914 | bool HasBases) |
915 | : EI(EI), Object(Object) { |
916 | DidInsert = |
917 | EI.ObjectsUnderConstruction |
918 | .insert(KV: {Object, HasBases ? ConstructionPhase::Bases |
919 | : ConstructionPhase::AfterBases}) |
920 | .second; |
921 | } |
922 | void finishedConstructingBases() { |
923 | EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases; |
924 | } |
925 | void finishedConstructingFields() { |
926 | EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterFields; |
927 | } |
928 | ~EvaluatingConstructorRAII() { |
929 | if (DidInsert) EI.ObjectsUnderConstruction.erase(Val: Object); |
930 | } |
931 | }; |
932 | |
933 | struct EvaluatingDestructorRAII { |
934 | EvalInfo &EI; |
935 | ObjectUnderConstruction Object; |
936 | bool DidInsert; |
937 | EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object) |
938 | : EI(EI), Object(Object) { |
939 | DidInsert = EI.ObjectsUnderConstruction |
940 | .insert(KV: {Object, ConstructionPhase::Destroying}) |
941 | .second; |
942 | } |
943 | void startedDestroyingBases() { |
944 | EI.ObjectsUnderConstruction[Object] = |
945 | ConstructionPhase::DestroyingBases; |
946 | } |
947 | ~EvaluatingDestructorRAII() { |
948 | if (DidInsert) |
949 | EI.ObjectsUnderConstruction.erase(Val: Object); |
950 | } |
951 | }; |
952 | |
953 | ConstructionPhase |
954 | isEvaluatingCtorDtor(APValue::LValueBase Base, |
955 | ArrayRef<APValue::LValuePathEntry> Path) { |
956 | return ObjectsUnderConstruction.lookup(Val: {.Base: Base, .Path: Path}); |
957 | } |
958 | |
959 | /// If we're currently speculatively evaluating, the outermost call stack |
960 | /// depth at which we can mutate state, otherwise 0. |
961 | unsigned SpeculativeEvaluationDepth = 0; |
962 | |
963 | /// The current array initialization index, if we're performing array |
964 | /// initialization. |
965 | uint64_t ArrayInitIndex = -1; |
966 | |
967 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
968 | /// notes attached to it will also be stored, otherwise they will not be. |
969 | bool HasActiveDiagnostic; |
970 | |
971 | /// Have we emitted a diagnostic explaining why we couldn't constant |
972 | /// fold (not just why it's not strictly a constant expression)? |
973 | bool HasFoldFailureDiagnostic; |
974 | |
975 | /// Whether we're checking that an expression is a potential constant |
976 | /// expression. If so, do not fail on constructs that could become constant |
977 | /// later on (such as a use of an undefined global). |
978 | bool CheckingPotentialConstantExpression = false; |
979 | |
980 | /// Whether we're checking for an expression that has undefined behavior. |
981 | /// If so, we will produce warnings if we encounter an operation that is |
982 | /// always undefined. |
983 | /// |
984 | /// Note that we still need to evaluate the expression normally when this |
985 | /// is set; this is used when evaluating ICEs in C. |
986 | bool CheckingForUndefinedBehavior = false; |
987 | |
988 | enum EvaluationMode { |
989 | /// Evaluate as a constant expression. Stop if we find that the expression |
990 | /// is not a constant expression. |
991 | EM_ConstantExpression, |
992 | |
993 | /// Evaluate as a constant expression. Stop if we find that the expression |
994 | /// is not a constant expression. Some expressions can be retried in the |
995 | /// optimizer if we don't constant fold them here, but in an unevaluated |
996 | /// context we try to fold them immediately since the optimizer never |
997 | /// gets a chance to look at it. |
998 | EM_ConstantExpressionUnevaluated, |
999 | |
1000 | /// Fold the expression to a constant. Stop if we hit a side-effect that |
1001 | /// we can't model. |
1002 | EM_ConstantFold, |
1003 | |
1004 | /// Evaluate in any way we know how. Don't worry about side-effects that |
1005 | /// can't be modeled. |
1006 | EM_IgnoreSideEffects, |
1007 | } EvalMode; |
1008 | |
1009 | /// Are we checking whether the expression is a potential constant |
1010 | /// expression? |
1011 | bool checkingPotentialConstantExpression() const override { |
1012 | return CheckingPotentialConstantExpression; |
1013 | } |
1014 | |
1015 | /// Are we checking an expression for overflow? |
1016 | // FIXME: We should check for any kind of undefined or suspicious behavior |
1017 | // in such constructs, not just overflow. |
1018 | bool checkingForUndefinedBehavior() const override { |
1019 | return CheckingForUndefinedBehavior; |
1020 | } |
1021 | |
1022 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode) |
1023 | : Ctx(const_cast<ASTContext &>(C)), EvalStatus(S), CurrentCall(nullptr), |
1024 | CallStackDepth(0), NextCallIndex(1), |
1025 | StepsLeft(C.getLangOpts().ConstexprStepLimit), |
1026 | EnableNewConstInterp(C.getLangOpts().EnableNewConstInterp), |
1027 | BottomFrame(*this, SourceLocation(), /*Callee=*/nullptr, |
1028 | /*This=*/nullptr, |
1029 | /*CallExpr=*/nullptr, CallRef()), |
1030 | EvaluatingDecl((const ValueDecl *)nullptr), |
1031 | EvaluatingDeclValue(nullptr), HasActiveDiagnostic(false), |
1032 | HasFoldFailureDiagnostic(false), EvalMode(Mode) {} |
1033 | |
1034 | ~EvalInfo() { |
1035 | discardCleanups(); |
1036 | } |
1037 | |
1038 | ASTContext &getASTContext() const override { return Ctx; } |
1039 | |
1040 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value, |
1041 | EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) { |
1042 | EvaluatingDecl = Base; |
1043 | IsEvaluatingDecl = EDK; |
1044 | EvaluatingDeclValue = &Value; |
1045 | } |
1046 | |
1047 | bool CheckCallLimit(SourceLocation Loc) { |
1048 | // Don't perform any constexpr calls (other than the call we're checking) |
1049 | // when checking a potential constant expression. |
1050 | if (checkingPotentialConstantExpression() && CallStackDepth > 1) |
1051 | return false; |
1052 | if (NextCallIndex == 0) { |
1053 | // NextCallIndex has wrapped around. |
1054 | FFDiag(Loc, diag::note_constexpr_call_limit_exceeded); |
1055 | return false; |
1056 | } |
1057 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
1058 | return true; |
1059 | FFDiag(Loc, diag::note_constexpr_depth_limit_exceeded) |
1060 | << getLangOpts().ConstexprCallDepth; |
1061 | return false; |
1062 | } |
1063 | |
1064 | bool CheckArraySize(SourceLocation Loc, unsigned BitWidth, |
1065 | uint64_t ElemCount, bool Diag) { |
1066 | // FIXME: GH63562 |
1067 | // APValue stores array extents as unsigned, |
1068 | // so anything that is greater that unsigned would overflow when |
1069 | // constructing the array, we catch this here. |
1070 | if (BitWidth > ConstantArrayType::getMaxSizeBits(Context: Ctx) || |
1071 | ElemCount > uint64_t(std::numeric_limits<unsigned>::max())) { |
1072 | if (Diag) |
1073 | FFDiag(Loc, diag::note_constexpr_new_too_large) << ElemCount; |
1074 | return false; |
1075 | } |
1076 | |
1077 | // FIXME: GH63562 |
1078 | // Arrays allocate an APValue per element. |
1079 | // We use the number of constexpr steps as a proxy for the maximum size |
1080 | // of arrays to avoid exhausting the system resources, as initialization |
1081 | // of each element is likely to take some number of steps anyway. |
1082 | uint64_t Limit = Ctx.getLangOpts().ConstexprStepLimit; |
1083 | if (ElemCount > Limit) { |
1084 | if (Diag) |
1085 | FFDiag(Loc, diag::note_constexpr_new_exceeds_limits) |
1086 | << ElemCount << Limit; |
1087 | return false; |
1088 | } |
1089 | return true; |
1090 | } |
1091 | |
1092 | std::pair<CallStackFrame *, unsigned> |
1093 | getCallFrameAndDepth(unsigned CallIndex) { |
1094 | assert(CallIndex && "no call index in getCallFrameAndDepth"); |
1095 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
1096 | // be null in this loop. |
1097 | unsigned Depth = CallStackDepth; |
1098 | CallStackFrame *Frame = CurrentCall; |
1099 | while (Frame->Index > CallIndex) { |
1100 | Frame = Frame->Caller; |
1101 | --Depth; |
1102 | } |
1103 | if (Frame->Index == CallIndex) |
1104 | return {Frame, Depth}; |
1105 | return {nullptr, 0}; |
1106 | } |
1107 | |
1108 | bool nextStep(const Stmt *S) { |
1109 | if (!StepsLeft) { |
1110 | FFDiag(S->getBeginLoc(), diag::note_constexpr_step_limit_exceeded); |
1111 | return false; |
1112 | } |
1113 | --StepsLeft; |
1114 | return true; |
1115 | } |
1116 | |
1117 | APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV); |
1118 | |
1119 | std::optional<DynAlloc *> lookupDynamicAlloc(DynamicAllocLValue DA) { |
1120 | std::optional<DynAlloc *> Result; |
1121 | auto It = HeapAllocs.find(x: DA); |
1122 | if (It != HeapAllocs.end()) |
1123 | Result = &It->second; |
1124 | return Result; |
1125 | } |
1126 | |
1127 | /// Get the allocated storage for the given parameter of the given call. |
1128 | APValue *getParamSlot(CallRef Call, const ParmVarDecl *PVD) { |
1129 | CallStackFrame *Frame = getCallFrameAndDepth(CallIndex: Call.CallIndex).first; |
1130 | return Frame ? Frame->getTemporary(Key: Call.getOrigParam(PVD), Version: Call.Version) |
1131 | : nullptr; |
1132 | } |
1133 | |
1134 | /// Information about a stack frame for std::allocator<T>::[de]allocate. |
1135 | struct StdAllocatorCaller { |
1136 | unsigned FrameIndex; |
1137 | QualType ElemType; |
1138 | const Expr *Call; |
1139 | explicit operator bool() const { return FrameIndex != 0; }; |
1140 | }; |
1141 | |
1142 | StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const { |
1143 | for (const CallStackFrame *Call = CurrentCall; Call != &BottomFrame; |
1144 | Call = Call->Caller) { |
1145 | const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Val: Call->Callee); |
1146 | if (!MD) |
1147 | continue; |
1148 | const IdentifierInfo *FnII = MD->getIdentifier(); |
1149 | if (!FnII || !FnII->isStr(Str: FnName)) |
1150 | continue; |
1151 | |
1152 | const auto *CTSD = |
1153 | dyn_cast<ClassTemplateSpecializationDecl>(Val: MD->getParent()); |
1154 | if (!CTSD) |
1155 | continue; |
1156 | |
1157 | const IdentifierInfo *ClassII = CTSD->getIdentifier(); |
1158 | const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); |
1159 | if (CTSD->isInStdNamespace() && ClassII && |
1160 | ClassII->isStr(Str: "allocator") && TAL.size() >= 1 && |
1161 | TAL[0].getKind() == TemplateArgument::Type) |
1162 | return {Call->Index, TAL[0].getAsType(), Call->CallExpr}; |
1163 | } |
1164 | |
1165 | return {}; |
1166 | } |
1167 | |
1168 | void performLifetimeExtension() { |
1169 | // Disable the cleanups for lifetime-extended temporaries. |
1170 | llvm::erase_if(C&: CleanupStack, P: [](Cleanup &C) { |
1171 | return !C.isDestroyedAtEndOf(K: ScopeKind::FullExpression); |
1172 | }); |
1173 | } |
1174 | |
1175 | /// Throw away any remaining cleanups at the end of evaluation. If any |
1176 | /// cleanups would have had a side-effect, note that as an unmodeled |
1177 | /// side-effect and return false. Otherwise, return true. |
1178 | bool discardCleanups() { |
1179 | for (Cleanup &C : CleanupStack) { |
1180 | if (C.hasSideEffect() && !noteSideEffect()) { |
1181 | CleanupStack.clear(); |
1182 | return false; |
1183 | } |
1184 | } |
1185 | CleanupStack.clear(); |
1186 | return true; |
1187 | } |
1188 | |
1189 | private: |
1190 | interp::Frame *getCurrentFrame() override { return CurrentCall; } |
1191 | const interp::Frame *getBottomFrame() const override { return &BottomFrame; } |
1192 | |
1193 | bool hasActiveDiagnostic() override { return HasActiveDiagnostic; } |
1194 | void setActiveDiagnostic(bool Flag) override { HasActiveDiagnostic = Flag; } |
1195 | |
1196 | void setFoldFailureDiagnostic(bool Flag) override { |
1197 | HasFoldFailureDiagnostic = Flag; |
1198 | } |
1199 | |
1200 | Expr::EvalStatus &getEvalStatus() const override { return EvalStatus; } |
1201 | |
1202 | // If we have a prior diagnostic, it will be noting that the expression |
1203 | // isn't a constant expression. This diagnostic is more important, |
1204 | // unless we require this evaluation to produce a constant expression. |
1205 | // |
1206 | // FIXME: We might want to show both diagnostics to the user in |
1207 | // EM_ConstantFold mode. |
1208 | bool hasPriorDiagnostic() override { |
1209 | if (!EvalStatus.Diag->empty()) { |
1210 | switch (EvalMode) { |
1211 | case EM_ConstantFold: |
1212 | case EM_IgnoreSideEffects: |
1213 | if (!HasFoldFailureDiagnostic) |
1214 | break; |
1215 | // We've already failed to fold something. Keep that diagnostic. |
1216 | [[fallthrough]]; |
1217 | case EM_ConstantExpression: |
1218 | case EM_ConstantExpressionUnevaluated: |
1219 | setActiveDiagnostic(false); |
1220 | return true; |
1221 | } |
1222 | } |
1223 | return false; |
1224 | } |
1225 | |
1226 | unsigned getCallStackDepth() override { return CallStackDepth; } |
1227 | |
1228 | public: |
1229 | /// Should we continue evaluation after encountering a side-effect that we |
1230 | /// couldn't model? |
1231 | bool keepEvaluatingAfterSideEffect() const override { |
1232 | switch (EvalMode) { |
1233 | case EM_IgnoreSideEffects: |
1234 | return true; |
1235 | |
1236 | case EM_ConstantExpression: |
1237 | case EM_ConstantExpressionUnevaluated: |
1238 | case EM_ConstantFold: |
1239 | // By default, assume any side effect might be valid in some other |
1240 | // evaluation of this expression from a different context. |
1241 | return checkingPotentialConstantExpression() || |
1242 | checkingForUndefinedBehavior(); |
1243 | } |
1244 | llvm_unreachable("Missed EvalMode case"); |
1245 | } |
1246 | |
1247 | /// Note that we have had a side-effect, and determine whether we should |
1248 | /// keep evaluating. |
1249 | bool noteSideEffect() override { |
1250 | EvalStatus.HasSideEffects = true; |
1251 | return keepEvaluatingAfterSideEffect(); |
1252 | } |
1253 | |
1254 | /// Should we continue evaluation after encountering undefined behavior? |
1255 | bool keepEvaluatingAfterUndefinedBehavior() { |
1256 | switch (EvalMode) { |
1257 | case EM_IgnoreSideEffects: |
1258 | case EM_ConstantFold: |
1259 | return true; |
1260 | |
1261 | case EM_ConstantExpression: |
1262 | case EM_ConstantExpressionUnevaluated: |
1263 | return checkingForUndefinedBehavior(); |
1264 | } |
1265 | llvm_unreachable("Missed EvalMode case"); |
1266 | } |
1267 | |
1268 | /// Note that we hit something that was technically undefined behavior, but |
1269 | /// that we can evaluate past it (such as signed overflow or floating-point |
1270 | /// division by zero.) |
1271 | bool noteUndefinedBehavior() override { |
1272 | EvalStatus.HasUndefinedBehavior = true; |
1273 | return keepEvaluatingAfterUndefinedBehavior(); |
1274 | } |
1275 | |
1276 | /// Should we continue evaluation as much as possible after encountering a |
1277 | /// construct which can't be reduced to a value? |
1278 | bool keepEvaluatingAfterFailure() const override { |
1279 | if (!StepsLeft) |
1280 | return false; |
1281 | |
1282 | switch (EvalMode) { |
1283 | case EM_ConstantExpression: |
1284 | case EM_ConstantExpressionUnevaluated: |
1285 | case EM_ConstantFold: |
1286 | case EM_IgnoreSideEffects: |
1287 | return checkingPotentialConstantExpression() || |
1288 | checkingForUndefinedBehavior(); |
1289 | } |
1290 | llvm_unreachable("Missed EvalMode case"); |
1291 | } |
1292 | |
1293 | /// Notes that we failed to evaluate an expression that other expressions |
1294 | /// directly depend on, and determine if we should keep evaluating. This |
1295 | /// should only be called if we actually intend to keep evaluating. |
1296 | /// |
1297 | /// Call noteSideEffect() instead if we may be able to ignore the value that |
1298 | /// we failed to evaluate, e.g. if we failed to evaluate Foo() in: |
1299 | /// |
1300 | /// (Foo(), 1) // use noteSideEffect |
1301 | /// (Foo() || true) // use noteSideEffect |
1302 | /// Foo() + 1 // use noteFailure |
1303 | [[nodiscard]] bool noteFailure() { |
1304 | // Failure when evaluating some expression often means there is some |
1305 | // subexpression whose evaluation was skipped. Therefore, (because we |
1306 | // don't track whether we skipped an expression when unwinding after an |
1307 | // evaluation failure) every evaluation failure that bubbles up from a |
1308 | // subexpression implies that a side-effect has potentially happened. We |
1309 | // skip setting the HasSideEffects flag to true until we decide to |
1310 | // continue evaluating after that point, which happens here. |
1311 | bool KeepGoing = keepEvaluatingAfterFailure(); |
1312 | EvalStatus.HasSideEffects |= KeepGoing; |
1313 | return KeepGoing; |
1314 | } |
1315 | |
1316 | class ArrayInitLoopIndex { |
1317 | EvalInfo &Info; |
1318 | uint64_t OuterIndex; |
1319 | |
1320 | public: |
1321 | ArrayInitLoopIndex(EvalInfo &Info) |
1322 | : Info(Info), OuterIndex(Info.ArrayInitIndex) { |
1323 | Info.ArrayInitIndex = 0; |
1324 | } |
1325 | ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; } |
1326 | |
1327 | operator uint64_t&() { return Info.ArrayInitIndex; } |
1328 | }; |
1329 | }; |
1330 | |
1331 | /// Object used to treat all foldable expressions as constant expressions. |
1332 | struct FoldConstant { |
1333 | EvalInfo &Info; |
1334 | bool Enabled; |
1335 | bool HadNoPriorDiags; |
1336 | EvalInfo::EvaluationMode OldMode; |
1337 | |
1338 | explicit FoldConstant(EvalInfo &Info, bool Enabled) |
1339 | : Info(Info), |
1340 | Enabled(Enabled), |
1341 | HadNoPriorDiags(Info.EvalStatus.Diag && |
1342 | Info.EvalStatus.Diag->empty() && |
1343 | !Info.EvalStatus.HasSideEffects), |
1344 | OldMode(Info.EvalMode) { |
1345 | if (Enabled) |
1346 | Info.EvalMode = EvalInfo::EM_ConstantFold; |
1347 | } |
1348 | void keepDiagnostics() { Enabled = false; } |
1349 | ~FoldConstant() { |
1350 | if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() && |
1351 | !Info.EvalStatus.HasSideEffects) |
1352 | Info.EvalStatus.Diag->clear(); |
1353 | Info.EvalMode = OldMode; |
1354 | } |
1355 | }; |
1356 | |
1357 | /// RAII object used to set the current evaluation mode to ignore |
1358 | /// side-effects. |
1359 | struct IgnoreSideEffectsRAII { |
1360 | EvalInfo &Info; |
1361 | EvalInfo::EvaluationMode OldMode; |
1362 | explicit IgnoreSideEffectsRAII(EvalInfo &Info) |
1363 | : Info(Info), OldMode(Info.EvalMode) { |
1364 | Info.EvalMode = EvalInfo::EM_IgnoreSideEffects; |
1365 | } |
1366 | |
1367 | ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; } |
1368 | }; |
1369 | |
1370 | /// RAII object used to optionally suppress diagnostics and side-effects from |
1371 | /// a speculative evaluation. |
1372 | class SpeculativeEvaluationRAII { |
1373 | EvalInfo *Info = nullptr; |
1374 | Expr::EvalStatus OldStatus; |
1375 | unsigned OldSpeculativeEvaluationDepth = 0; |
1376 | |
1377 | void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) { |
1378 | Info = Other.Info; |
1379 | OldStatus = Other.OldStatus; |
1380 | OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth; |
1381 | Other.Info = nullptr; |
1382 | } |
1383 | |
1384 | void maybeRestoreState() { |
1385 | if (!Info) |
1386 | return; |
1387 | |
1388 | Info->EvalStatus = OldStatus; |
1389 | Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth; |
1390 | } |
1391 | |
1392 | public: |
1393 | SpeculativeEvaluationRAII() = default; |
1394 | |
1395 | SpeculativeEvaluationRAII( |
1396 | EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr) |
1397 | : Info(&Info), OldStatus(Info.EvalStatus), |
1398 | OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) { |
1399 | Info.EvalStatus.Diag = NewDiag; |
1400 | Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1; |
1401 | } |
1402 | |
1403 | SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete; |
1404 | SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) { |
1405 | moveFromAndCancel(Other: std::move(Other)); |
1406 | } |
1407 | |
1408 | SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) { |
1409 | maybeRestoreState(); |
1410 | moveFromAndCancel(Other: std::move(Other)); |
1411 | return *this; |
1412 | } |
1413 | |
1414 | ~SpeculativeEvaluationRAII() { maybeRestoreState(); } |
1415 | }; |
1416 | |
1417 | /// RAII object wrapping a full-expression or block scope, and handling |
1418 | /// the ending of the lifetime of temporaries created within it. |
1419 | template<ScopeKind Kind> |
1420 | class ScopeRAII { |
1421 | EvalInfo &Info; |
1422 | unsigned OldStackSize; |
1423 | public: |
1424 | ScopeRAII(EvalInfo &Info) |
1425 | : Info(Info), OldStackSize(Info.CleanupStack.size()) { |
1426 | // Push a new temporary version. This is needed to distinguish between |
1427 | // temporaries created in different iterations of a loop. |
1428 | Info.CurrentCall->pushTempVersion(); |
1429 | } |
1430 | bool destroy(bool RunDestructors = true) { |
1431 | bool OK = cleanup(Info, RunDestructors, OldStackSize); |
1432 | OldStackSize = -1U; |
1433 | return OK; |
1434 | } |
1435 | ~ScopeRAII() { |
1436 | if (OldStackSize != -1U) |
1437 | destroy(RunDestructors: false); |
1438 | // Body moved to a static method to encourage the compiler to inline away |
1439 | // instances of this class. |
1440 | Info.CurrentCall->popTempVersion(); |
1441 | } |
1442 | private: |
1443 | static bool cleanup(EvalInfo &Info, bool RunDestructors, |
1444 | unsigned OldStackSize) { |
1445 | assert(OldStackSize <= Info.CleanupStack.size() && |
1446 | "running cleanups out of order?"); |
1447 | |
1448 | // Run all cleanups for a block scope, and non-lifetime-extended cleanups |
1449 | // for a full-expression scope. |
1450 | bool Success = true; |
1451 | for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) { |
1452 | if (Info.CleanupStack[I - 1].isDestroyedAtEndOf(K: Kind)) { |
1453 | if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) { |
1454 | Success = false; |
1455 | break; |
1456 | } |
1457 | } |
1458 | } |
1459 | |
1460 | // Compact any retained cleanups. |
1461 | auto NewEnd = Info.CleanupStack.begin() + OldStackSize; |
1462 | if (Kind != ScopeKind::Block) |
1463 | NewEnd = |
1464 | std::remove_if(NewEnd, Info.CleanupStack.end(), [](Cleanup &C) { |
1465 | return C.isDestroyedAtEndOf(K: Kind); |
1466 | }); |
1467 | Info.CleanupStack.erase(CS: NewEnd, CE: Info.CleanupStack.end()); |
1468 | return Success; |
1469 | } |
1470 | }; |
1471 | typedef ScopeRAII<ScopeKind::Block> BlockScopeRAII; |
1472 | typedef ScopeRAII<ScopeKind::FullExpression> FullExpressionRAII; |
1473 | typedef ScopeRAII<ScopeKind::Call> CallScopeRAII; |
1474 | } |
1475 | |
1476 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
1477 | CheckSubobjectKind CSK) { |
1478 | if (Invalid) |
1479 | return false; |
1480 | if (isOnePastTheEnd()) { |
1481 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
1482 | << CSK; |
1483 | setInvalid(); |
1484 | return false; |
1485 | } |
1486 | // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there |
1487 | // must actually be at least one array element; even a VLA cannot have a |
1488 | // bound of zero. And if our index is nonzero, we already had a CCEDiag. |
1489 | return true; |
1490 | } |
1491 | |
1492 | void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, |
1493 | const Expr *E) { |
1494 | Info.CCEDiag(E, diag::note_constexpr_unsized_array_indexed); |
1495 | // Do not set the designator as invalid: we can represent this situation, |
1496 | // and correct handling of __builtin_object_size requires us to do so. |
1497 | } |
1498 | |
1499 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
1500 | const Expr *E, |
1501 | const APSInt &N) { |
1502 | // If we're complaining, we must be able to statically determine the size of |
1503 | // the most derived array. |
1504 | if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement) |
1505 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
1506 | << N << /*array*/ 0 |
1507 | << static_cast<unsigned>(getMostDerivedArraySize()); |
1508 | else |
1509 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
1510 | << N << /*non-array*/ 1; |
1511 | setInvalid(); |
1512 | } |
1513 | |
1514 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceRange CallRange, |
1515 | const FunctionDecl *Callee, const LValue *This, |
1516 | const Expr *CallExpr, CallRef Call) |
1517 | : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This), |
1518 | CallExpr(CallExpr), Arguments(Call), CallRange(CallRange), |
1519 | Index(Info.NextCallIndex++) { |
1520 | Info.CurrentCall = this; |
1521 | ++Info.CallStackDepth; |
1522 | } |
1523 | |
1524 | CallStackFrame::~CallStackFrame() { |
1525 | assert(Info.CurrentCall == this && "calls retired out of order"); |
1526 | --Info.CallStackDepth; |
1527 | Info.CurrentCall = Caller; |
1528 | } |
1529 | |
1530 | static bool isRead(AccessKinds AK) { |
1531 | return AK == AK_Read || AK == AK_ReadObjectRepresentation || |
1532 | AK == AK_IsWithinLifetime; |
1533 | } |
1534 | |
1535 | static bool isModification(AccessKinds AK) { |
1536 | switch (AK) { |
1537 | case AK_Read: |
1538 | case AK_ReadObjectRepresentation: |
1539 | case AK_MemberCall: |
1540 | case AK_DynamicCast: |
1541 | case AK_TypeId: |
1542 | case AK_IsWithinLifetime: |
1543 | return false; |
1544 | case AK_Assign: |
1545 | case AK_Increment: |
1546 | case AK_Decrement: |
1547 | case AK_Construct: |
1548 | case AK_Destroy: |
1549 | return true; |
1550 | } |
1551 | llvm_unreachable("unknown access kind"); |
1552 | } |
1553 | |
1554 | static bool isAnyAccess(AccessKinds AK) { |
1555 | return isRead(AK) || isModification(AK); |
1556 | } |
1557 | |
1558 | /// Is this an access per the C++ definition? |
1559 | static bool isFormalAccess(AccessKinds AK) { |
1560 | return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy && |
1561 | AK != AK_IsWithinLifetime; |
1562 | } |
1563 | |
1564 | /// Is this kind of axcess valid on an indeterminate object value? |
1565 | static bool isValidIndeterminateAccess(AccessKinds AK) { |
1566 | switch (AK) { |
1567 | case AK_Read: |
1568 | case AK_Increment: |
1569 | case AK_Decrement: |
1570 | // These need the object's value. |
1571 | return false; |
1572 | |
1573 | case AK_IsWithinLifetime: |
1574 | case AK_ReadObjectRepresentation: |
1575 | case AK_Assign: |
1576 | case AK_Construct: |
1577 | case AK_Destroy: |
1578 | // Construction and destruction don't need the value. |
1579 | return true; |
1580 | |
1581 | case AK_MemberCall: |
1582 | case AK_DynamicCast: |
1583 | case AK_TypeId: |
1584 | // These aren't really meaningful on scalars. |
1585 | return true; |
1586 | } |
1587 | llvm_unreachable("unknown access kind"); |
1588 | } |
1589 | |
1590 | namespace { |
1591 | struct ComplexValue { |
1592 | private: |
1593 | bool IsInt; |
1594 | |
1595 | public: |
1596 | APSInt IntReal, IntImag; |
1597 | APFloat FloatReal, FloatImag; |
1598 | |
1599 | ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {} |
1600 | |
1601 | void makeComplexFloat() { IsInt = false; } |
1602 | bool isComplexFloat() const { return !IsInt; } |
1603 | APFloat &getComplexFloatReal() { return FloatReal; } |
1604 | APFloat &getComplexFloatImag() { return FloatImag; } |
1605 | |
1606 | void makeComplexInt() { IsInt = true; } |
1607 | bool isComplexInt() const { return IsInt; } |
1608 | APSInt &getComplexIntReal() { return IntReal; } |
1609 | APSInt &getComplexIntImag() { return IntImag; } |
1610 | |
1611 | void moveInto(APValue &v) const { |
1612 | if (isComplexFloat()) |
1613 | v = APValue(FloatReal, FloatImag); |
1614 | else |
1615 | v = APValue(IntReal, IntImag); |
1616 | } |
1617 | void setFrom(const APValue &v) { |
1618 | assert(v.isComplexFloat() || v.isComplexInt()); |
1619 | if (v.isComplexFloat()) { |
1620 | makeComplexFloat(); |
1621 | FloatReal = v.getComplexFloatReal(); |
1622 | FloatImag = v.getComplexFloatImag(); |
1623 | } else { |
1624 | makeComplexInt(); |
1625 | IntReal = v.getComplexIntReal(); |
1626 | IntImag = v.getComplexIntImag(); |
1627 | } |
1628 | } |
1629 | }; |
1630 | |
1631 | struct LValue { |
1632 | APValue::LValueBase Base; |
1633 | CharUnits Offset; |
1634 | SubobjectDesignator Designator; |
1635 | bool IsNullPtr : 1; |
1636 | bool InvalidBase : 1; |
1637 | // P2280R4 track if we have an unknown reference or pointer. |
1638 | bool AllowConstexprUnknown = false; |
1639 | |
1640 | const APValue::LValueBase getLValueBase() const { return Base; } |
1641 | bool allowConstexprUnknown() const { return AllowConstexprUnknown; } |
1642 | CharUnits &getLValueOffset() { return Offset; } |
1643 | const CharUnits &getLValueOffset() const { return Offset; } |
1644 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
1645 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
1646 | bool isNullPointer() const { return IsNullPtr;} |
1647 | |
1648 | unsigned getLValueCallIndex() const { return Base.getCallIndex(); } |
1649 | unsigned getLValueVersion() const { return Base.getVersion(); } |
1650 | |
1651 | void moveInto(APValue &V) const { |
1652 | if (Designator.Invalid) |
1653 | V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr); |
1654 | else { |
1655 | assert(!InvalidBase && "APValues can't handle invalid LValue bases"); |
1656 | V = APValue(Base, Offset, Designator.Entries, |
1657 | Designator.IsOnePastTheEnd, IsNullPtr); |
1658 | } |
1659 | if (AllowConstexprUnknown) |
1660 | V.setConstexprUnknown(); |
1661 | } |
1662 | void setFrom(ASTContext &Ctx, const APValue &V) { |
1663 | assert(V.isLValue() && "Setting LValue from a non-LValue?"); |
1664 | Base = V.getLValueBase(); |
1665 | Offset = V.getLValueOffset(); |
1666 | InvalidBase = false; |
1667 | Designator = SubobjectDesignator(Ctx, V); |
1668 | IsNullPtr = V.isNullPointer(); |
1669 | AllowConstexprUnknown = V.allowConstexprUnknown(); |
1670 | } |
1671 | |
1672 | void set(APValue::LValueBase B, bool BInvalid = false) { |
1673 | #ifndef NDEBUG |
1674 | // We only allow a few types of invalid bases. Enforce that here. |
1675 | if (BInvalid) { |
1676 | const auto *E = B.get<const Expr *>(); |
1677 | assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) && |
1678 | "Unexpected type of invalid base"); |
1679 | } |
1680 | #endif |
1681 | |
1682 | Base = B; |
1683 | Offset = CharUnits::fromQuantity(Quantity: 0); |
1684 | InvalidBase = BInvalid; |
1685 | Designator = SubobjectDesignator(getType(B)); |
1686 | IsNullPtr = false; |
1687 | AllowConstexprUnknown = false; |
1688 | } |
1689 | |
1690 | void setNull(ASTContext &Ctx, QualType PointerTy) { |
1691 | Base = (const ValueDecl *)nullptr; |
1692 | Offset = |
1693 | CharUnits::fromQuantity(Quantity: Ctx.getTargetNullPointerValue(QT: PointerTy)); |
1694 | InvalidBase = false; |
1695 | Designator = SubobjectDesignator(PointerTy->getPointeeType()); |
1696 | IsNullPtr = true; |
1697 | AllowConstexprUnknown = false; |
1698 | } |
1699 | |
1700 | void setInvalid(APValue::LValueBase B, unsigned I = 0) { |
1701 | set(B, BInvalid: true); |
1702 | } |
1703 | |
1704 | std::string toString(ASTContext &Ctx, QualType T) const { |
1705 | APValue Printable; |
1706 | moveInto(V&: Printable); |
1707 | return Printable.getAsString(Ctx, Ty: T); |
1708 | } |
1709 | |
1710 | private: |
1711 | // Check that this LValue is not based on a null pointer. If it is, produce |
1712 | // a diagnostic and mark the designator as invalid. |
1713 | template <typename GenDiagType> |
1714 | bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) { |
1715 | if (Designator.Invalid) |
1716 | return false; |
1717 | if (IsNullPtr) { |
1718 | GenDiag(); |
1719 | Designator.setInvalid(); |
1720 | return false; |
1721 | } |
1722 | return true; |
1723 | } |
1724 | |
1725 | public: |
1726 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
1727 | CheckSubobjectKind CSK) { |
1728 | return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, CSK] { |
1729 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) << CSK; |
1730 | }); |
1731 | } |
1732 | |
1733 | bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E, |
1734 | AccessKinds AK) { |
1735 | return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, AK] { |
1736 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
1737 | }); |
1738 | } |
1739 | |
1740 | // Check this LValue refers to an object. If not, set the designator to be |
1741 | // invalid and emit a diagnostic. |
1742 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
1743 | return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) && |
1744 | Designator.checkSubobject(Info, E, CSK); |
1745 | } |
1746 | |
1747 | void addDecl(EvalInfo &Info, const Expr *E, |
1748 | const Decl *D, bool Virtual = false) { |
1749 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
1750 | Designator.addDeclUnchecked(D, Virtual); |
1751 | } |
1752 | void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) { |
1753 | if (!Designator.Entries.empty()) { |
1754 | Info.CCEDiag(E, diag::note_constexpr_unsupported_unsized_array); |
1755 | Designator.setInvalid(); |
1756 | return; |
1757 | } |
1758 | if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer)) { |
1759 | assert(getType(Base)->isPointerType() || getType(Base)->isArrayType()); |
1760 | Designator.FirstEntryIsAnUnsizedArray = true; |
1761 | Designator.addUnsizedArrayUnchecked(ElemTy); |
1762 | } |
1763 | } |
1764 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
1765 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
1766 | Designator.addArrayUnchecked(CAT); |
1767 | } |
1768 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
1769 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
1770 | Designator.addComplexUnchecked(EltTy, Imag); |
1771 | } |
1772 | void addVectorElement(EvalInfo &Info, const Expr *E, QualType EltTy, |
1773 | uint64_t Size, uint64_t Idx) { |
1774 | if (checkSubobject(Info, E, CSK_VectorElement)) |
1775 | Designator.addVectorElementUnchecked(EltTy, Size, Idx); |
1776 | } |
1777 | void clearIsNullPointer() { |
1778 | IsNullPtr = false; |
1779 | } |
1780 | void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E, |
1781 | const APSInt &Index, CharUnits ElementSize) { |
1782 | // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB, |
1783 | // but we're not required to diagnose it and it's valid in C++.) |
1784 | if (!Index) |
1785 | return; |
1786 | |
1787 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
1788 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
1789 | // offsets. |
1790 | uint64_t Offset64 = Offset.getQuantity(); |
1791 | uint64_t ElemSize64 = ElementSize.getQuantity(); |
1792 | uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue(); |
1793 | Offset = CharUnits::fromQuantity(Quantity: Offset64 + ElemSize64 * Index64); |
1794 | |
1795 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
1796 | Designator.adjustIndex(Info, E, Index); |
1797 | clearIsNullPointer(); |
1798 | } |
1799 | void adjustOffset(CharUnits N) { |
1800 | Offset += N; |
1801 | if (N.getQuantity()) |
1802 | clearIsNullPointer(); |
1803 | } |
1804 | }; |
1805 | |
1806 | struct MemberPtr { |
1807 | MemberPtr() {} |
1808 | explicit MemberPtr(const ValueDecl *Decl) |
1809 | : DeclAndIsDerivedMember(Decl, false) {} |
1810 | |
1811 | /// The member or (direct or indirect) field referred to by this member |
1812 | /// pointer, or 0 if this is a null member pointer. |
1813 | const ValueDecl *getDecl() const { |
1814 | return DeclAndIsDerivedMember.getPointer(); |
1815 | } |
1816 | /// Is this actually a member of some type derived from the relevant class? |
1817 | bool isDerivedMember() const { |
1818 | return DeclAndIsDerivedMember.getInt(); |
1819 | } |
1820 | /// Get the class which the declaration actually lives in. |
1821 | const CXXRecordDecl *getContainingRecord() const { |
1822 | return cast<CXXRecordDecl>( |
1823 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
1824 | } |
1825 | |
1826 | void moveInto(APValue &V) const { |
1827 | V = APValue(getDecl(), isDerivedMember(), Path); |
1828 | } |
1829 | void setFrom(const APValue &V) { |
1830 | assert(V.isMemberPointer()); |
1831 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
1832 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
1833 | Path.clear(); |
1834 | llvm::append_range(C&: Path, R: V.getMemberPointerPath()); |
1835 | } |
1836 | |
1837 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
1838 | /// whether the member is a member of some class derived from the class type |
1839 | /// of the member pointer. |
1840 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
1841 | /// Path - The path of base/derived classes from the member declaration's |
1842 | /// class (exclusive) to the class type of the member pointer (inclusive). |
1843 | SmallVector<const CXXRecordDecl*, 4> Path; |
1844 | |
1845 | /// Perform a cast towards the class of the Decl (either up or down the |
1846 | /// hierarchy). |
1847 | bool castBack(const CXXRecordDecl *Class) { |
1848 | assert(!Path.empty()); |
1849 | const CXXRecordDecl *Expected; |
1850 | if (Path.size() >= 2) |
1851 | Expected = Path[Path.size() - 2]; |
1852 | else |
1853 | Expected = getContainingRecord(); |
1854 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
1855 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
1856 | // if B does not contain the original member and is not a base or |
1857 | // derived class of the class containing the original member, the result |
1858 | // of the cast is undefined. |
1859 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
1860 | // (D::*). We consider that to be a language defect. |
1861 | return false; |
1862 | } |
1863 | Path.pop_back(); |
1864 | return true; |
1865 | } |
1866 | /// Perform a base-to-derived member pointer cast. |
1867 | bool castToDerived(const CXXRecordDecl *Derived) { |
1868 | if (!getDecl()) |
1869 | return true; |
1870 | if (!isDerivedMember()) { |
1871 | Path.push_back(Elt: Derived); |
1872 | return true; |
1873 | } |
1874 | if (!castBack(Class: Derived)) |
1875 | return false; |
1876 | if (Path.empty()) |
1877 | DeclAndIsDerivedMember.setInt(false); |
1878 | return true; |
1879 | } |
1880 | /// Perform a derived-to-base member pointer cast. |
1881 | bool castToBase(const CXXRecordDecl *Base) { |
1882 | if (!getDecl()) |
1883 | return true; |
1884 | if (Path.empty()) |
1885 | DeclAndIsDerivedMember.setInt(true); |
1886 | if (isDerivedMember()) { |
1887 | Path.push_back(Elt: Base); |
1888 | return true; |
1889 | } |
1890 | return castBack(Class: Base); |
1891 | } |
1892 | }; |
1893 | |
1894 | /// Compare two member pointers, which are assumed to be of the same type. |
1895 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
1896 | if (!LHS.getDecl() || !RHS.getDecl()) |
1897 | return !LHS.getDecl() && !RHS.getDecl(); |
1898 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
1899 | return false; |
1900 | return LHS.Path == RHS.Path; |
1901 | } |
1902 | } |
1903 | |
1904 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
1905 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
1906 | const LValue &This, const Expr *E, |
1907 | bool AllowNonLiteralTypes = false); |
1908 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
1909 | bool InvalidBaseOK = false); |
1910 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info, |
1911 | bool InvalidBaseOK = false); |
1912 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
1913 | EvalInfo &Info); |
1914 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
1915 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
1916 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
1917 | EvalInfo &Info); |
1918 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
1919 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
1920 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
1921 | EvalInfo &Info); |
1922 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result); |
1923 | static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result, |
1924 | EvalInfo &Info, |
1925 | std::string *StringResult = nullptr); |
1926 | |
1927 | /// Evaluate an integer or fixed point expression into an APResult. |
1928 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
1929 | EvalInfo &Info); |
1930 | |
1931 | /// Evaluate only a fixed point expression into an APResult. |
1932 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
1933 | EvalInfo &Info); |
1934 | |
1935 | //===----------------------------------------------------------------------===// |
1936 | // Misc utilities |
1937 | //===----------------------------------------------------------------------===// |
1938 | |
1939 | /// Negate an APSInt in place, converting it to a signed form if necessary, and |
1940 | /// preserving its value (by extending by up to one bit as needed). |
1941 | static void negateAsSigned(APSInt &Int) { |
1942 | if (Int.isUnsigned() || Int.isMinSignedValue()) { |
1943 | Int = Int.extend(width: Int.getBitWidth() + 1); |
1944 | Int.setIsSigned(true); |
1945 | } |
1946 | Int = -Int; |
1947 | } |
1948 | |
1949 | template<typename KeyT> |
1950 | APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T, |
1951 | ScopeKind Scope, LValue &LV) { |
1952 | unsigned Version = getTempVersion(); |
1953 | APValue::LValueBase Base(Key, Index, Version); |
1954 | LV.set(B: Base); |
1955 | return createLocal(Base, Key, T, Scope); |
1956 | } |
1957 | |
1958 | APValue & |
1959 | CallStackFrame::createConstexprUnknownAPValues(const VarDecl *Key, |
1960 | APValue::LValueBase Base) { |
1961 | APValue &Result = ConstexprUnknownAPValues[MapKeyTy(Key, Base.getVersion())]; |
1962 | Result = APValue(Base, CharUnits::Zero(), APValue::ConstexprUnknown{}); |
1963 | |
1964 | return Result; |
1965 | } |
1966 | |
1967 | /// Allocate storage for a parameter of a function call made in this frame. |
1968 | APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD, |
1969 | LValue &LV) { |
1970 | assert(Args.CallIndex == Index && "creating parameter in wrong frame"); |
1971 | APValue::LValueBase Base(PVD, Index, Args.Version); |
1972 | LV.set(B: Base); |
1973 | // We always destroy parameters at the end of the call, even if we'd allow |
1974 | // them to live to the end of the full-expression at runtime, in order to |
1975 | // give portable results and match other compilers. |
1976 | return createLocal(Base, Key: PVD, T: PVD->getType(), Scope: ScopeKind::Call); |
1977 | } |
1978 | |
1979 | APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key, |
1980 | QualType T, ScopeKind Scope) { |
1981 | assert(Base.getCallIndex() == Index && "lvalue for wrong frame"); |
1982 | unsigned Version = Base.getVersion(); |
1983 | APValue &Result = Temporaries[MapKeyTy(Key, Version)]; |
1984 | assert(Result.isAbsent() && "local created multiple times"); |
1985 | |
1986 | // If we're creating a local immediately in the operand of a speculative |
1987 | // evaluation, don't register a cleanup to be run outside the speculative |
1988 | // evaluation context, since we won't actually be able to initialize this |
1989 | // object. |
1990 | if (Index <= Info.SpeculativeEvaluationDepth) { |
1991 | if (T.isDestructedType()) |
1992 | Info.noteSideEffect(); |
1993 | } else { |
1994 | Info.CleanupStack.push_back(Elt: Cleanup(&Result, Base, T, Scope)); |
1995 | } |
1996 | return Result; |
1997 | } |
1998 | |
1999 | APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) { |
2000 | if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) { |
2001 | FFDiag(E, diag::note_constexpr_heap_alloc_limit_exceeded); |
2002 | return nullptr; |
2003 | } |
2004 | |
2005 | DynamicAllocLValue DA(NumHeapAllocs++); |
2006 | LV.set(B: APValue::LValueBase::getDynamicAlloc(LV: DA, Type: T)); |
2007 | auto Result = HeapAllocs.emplace(args: std::piecewise_construct, |
2008 | args: std::forward_as_tuple(args&: DA), args: std::tuple<>()); |
2009 | assert(Result.second && "reused a heap alloc index?"); |
2010 | Result.first->second.AllocExpr = E; |
2011 | return &Result.first->second.Value; |
2012 | } |
2013 | |
2014 | /// Produce a string describing the given constexpr call. |
2015 | void CallStackFrame::describe(raw_ostream &Out) const { |
2016 | unsigned ArgIndex = 0; |
2017 | bool IsMemberCall = |
2018 | isa<CXXMethodDecl>(Val: Callee) && !isa<CXXConstructorDecl>(Val: Callee) && |
2019 | cast<CXXMethodDecl>(Val: Callee)->isImplicitObjectMemberFunction(); |
2020 | |
2021 | if (!IsMemberCall) |
2022 | Callee->getNameForDiagnostic(OS&: Out, Policy: Info.Ctx.getPrintingPolicy(), |
2023 | /*Qualified=*/false); |
2024 | |
2025 | if (This && IsMemberCall) { |
2026 | if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(Val: CallExpr)) { |
2027 | const Expr *Object = MCE->getImplicitObjectArgument(); |
2028 | Object->printPretty(Out, /*Helper=*/nullptr, Info.Ctx.getPrintingPolicy(), |
2029 | /*Indentation=*/0); |
2030 | if (Object->getType()->isPointerType()) |
2031 | Out << "->"; |
2032 | else |
2033 | Out << "."; |
2034 | } else if (const auto *OCE = |
2035 | dyn_cast_if_present<CXXOperatorCallExpr>(Val: CallExpr)) { |
2036 | OCE->getArg(0)->printPretty(Out, /*Helper=*/nullptr, |
2037 | Info.Ctx.getPrintingPolicy(), |
2038 | /*Indentation=*/0); |
2039 | Out << "."; |
2040 | } else { |
2041 | APValue Val; |
2042 | This->moveInto(V&: Val); |
2043 | Val.printPretty( |
2044 | Out, Info.Ctx, |
2045 | Info.Ctx.getLValueReferenceType(T: This->Designator.MostDerivedType)); |
2046 | Out << "."; |
2047 | } |
2048 | Callee->getNameForDiagnostic(OS&: Out, Policy: Info.Ctx.getPrintingPolicy(), |
2049 | /*Qualified=*/false); |
2050 | IsMemberCall = false; |
2051 | } |
2052 | |
2053 | Out << '('; |
2054 | |
2055 | for (FunctionDecl::param_const_iterator I = Callee->param_begin(), |
2056 | E = Callee->param_end(); I != E; ++I, ++ArgIndex) { |
2057 | if (ArgIndex > (unsigned)IsMemberCall) |
2058 | Out << ", "; |
2059 | |
2060 | const ParmVarDecl *Param = *I; |
2061 | APValue *V = Info.getParamSlot(Call: Arguments, PVD: Param); |
2062 | if (V) |
2063 | V->printPretty(Out, Info.Ctx, Param->getType()); |
2064 | else |
2065 | Out << "<...>"; |
2066 | |
2067 | if (ArgIndex == 0 && IsMemberCall) |
2068 | Out << "->"<< *Callee << '('; |
2069 | } |
2070 | |
2071 | Out << ')'; |
2072 | } |
2073 | |
2074 | /// Evaluate an expression to see if it had side-effects, and discard its |
2075 | /// result. |
2076 | /// \return \c true if the caller should keep evaluating. |
2077 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
2078 | assert(!E->isValueDependent()); |
2079 | APValue Scratch; |
2080 | if (!Evaluate(Result&: Scratch, Info, E)) |
2081 | // We don't need the value, but we might have skipped a side effect here. |
2082 | return Info.noteSideEffect(); |
2083 | return true; |
2084 | } |
2085 | |
2086 | /// Should this call expression be treated as forming an opaque constant? |
2087 | static bool IsOpaqueConstantCall(const CallExpr *E) { |
2088 | unsigned Builtin = E->getBuiltinCallee(); |
2089 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
2090 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString || |
2091 | Builtin == Builtin::BI__builtin_ptrauth_sign_constant || |
2092 | Builtin == Builtin::BI__builtin_function_start); |
2093 | } |
2094 | |
2095 | static bool IsOpaqueConstantCall(const LValue &LVal) { |
2096 | const auto *BaseExpr = |
2097 | llvm::dyn_cast_if_present<CallExpr>(Val: LVal.Base.dyn_cast<const Expr *>()); |
2098 | return BaseExpr && IsOpaqueConstantCall(E: BaseExpr); |
2099 | } |
2100 | |
2101 | static bool IsGlobalLValue(APValue::LValueBase B) { |
2102 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
2103 | // constant expression of pointer type that evaluates to... |
2104 | |
2105 | // ... a null pointer value, or a prvalue core constant expression of type |
2106 | // std::nullptr_t. |
2107 | if (!B) |
2108 | return true; |
2109 | |
2110 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
2111 | // ... the address of an object with static storage duration, |
2112 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
2113 | return VD->hasGlobalStorage(); |
2114 | if (isa<TemplateParamObjectDecl>(Val: D)) |
2115 | return true; |
2116 | // ... the address of a function, |
2117 | // ... the address of a GUID [MS extension], |
2118 | // ... the address of an unnamed global constant |
2119 | return isa<FunctionDecl, MSGuidDecl, UnnamedGlobalConstantDecl>(Val: D); |
2120 | } |
2121 | |
2122 | if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>()) |
2123 | return true; |
2124 | |
2125 | const Expr *E = B.get<const Expr*>(); |
2126 | switch (E->getStmtClass()) { |
2127 | default: |
2128 | return false; |
2129 | case Expr::CompoundLiteralExprClass: { |
2130 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(Val: E); |
2131 | return CLE->isFileScope() && CLE->isLValue(); |
2132 | } |
2133 | case Expr::MaterializeTemporaryExprClass: |
2134 | // A materialized temporary might have been lifetime-extended to static |
2135 | // storage duration. |
2136 | return cast<MaterializeTemporaryExpr>(Val: E)->getStorageDuration() == SD_Static; |
2137 | // A string literal has static storage duration. |
2138 | case Expr::StringLiteralClass: |
2139 | case Expr::PredefinedExprClass: |
2140 | case Expr::ObjCStringLiteralClass: |
2141 | case Expr::ObjCEncodeExprClass: |
2142 | return true; |
2143 | case Expr::ObjCBoxedExprClass: |
2144 | return cast<ObjCBoxedExpr>(Val: E)->isExpressibleAsConstantInitializer(); |
2145 | case Expr::CallExprClass: |
2146 | return IsOpaqueConstantCall(E: cast<CallExpr>(Val: E)); |
2147 | // For GCC compatibility, &&label has static storage duration. |
2148 | case Expr::AddrLabelExprClass: |
2149 | return true; |
2150 | // A Block literal expression may be used as the initialization value for |
2151 | // Block variables at global or local static scope. |
2152 | case Expr::BlockExprClass: |
2153 | return !cast<BlockExpr>(Val: E)->getBlockDecl()->hasCaptures(); |
2154 | // The APValue generated from a __builtin_source_location will be emitted as a |
2155 | // literal. |
2156 | case Expr::SourceLocExprClass: |
2157 | return true; |
2158 | case Expr::ImplicitValueInitExprClass: |
2159 | // FIXME: |
2160 | // We can never form an lvalue with an implicit value initialization as its |
2161 | // base through expression evaluation, so these only appear in one case: the |
2162 | // implicit variable declaration we invent when checking whether a constexpr |
2163 | // constructor can produce a constant expression. We must assume that such |
2164 | // an expression might be a global lvalue. |
2165 | return true; |
2166 | } |
2167 | } |
2168 | |
2169 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
2170 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
2171 | } |
2172 | |
2173 | // Information about an LValueBase that is some kind of string. |
2174 | struct LValueBaseString { |
2175 | std::string ObjCEncodeStorage; |
2176 | StringRef Bytes; |
2177 | int CharWidth; |
2178 | }; |
2179 | |
2180 | // Gets the lvalue base of LVal as a string. |
2181 | static bool GetLValueBaseAsString(const EvalInfo &Info, const LValue &LVal, |
2182 | LValueBaseString &AsString) { |
2183 | const auto *BaseExpr = LVal.Base.dyn_cast<const Expr *>(); |
2184 | if (!BaseExpr) |
2185 | return false; |
2186 | |
2187 | // For ObjCEncodeExpr, we need to compute and store the string. |
2188 | if (const auto *EE = dyn_cast<ObjCEncodeExpr>(Val: BaseExpr)) { |
2189 | Info.Ctx.getObjCEncodingForType(T: EE->getEncodedType(), |
2190 | S&: AsString.ObjCEncodeStorage); |
2191 | AsString.Bytes = AsString.ObjCEncodeStorage; |
2192 | AsString.CharWidth = 1; |
2193 | return true; |
2194 | } |
2195 | |
2196 | // Otherwise, we have a StringLiteral. |
2197 | const auto *Lit = dyn_cast<StringLiteral>(Val: BaseExpr); |
2198 | if (const auto *PE = dyn_cast<PredefinedExpr>(Val: BaseExpr)) |
2199 | Lit = PE->getFunctionName(); |
2200 | |
2201 | if (!Lit) |
2202 | return false; |
2203 | |
2204 | AsString.Bytes = Lit->getBytes(); |
2205 | AsString.CharWidth = Lit->getCharByteWidth(); |
2206 | return true; |
2207 | } |
2208 | |
2209 | // Determine whether two string literals potentially overlap. This will be the |
2210 | // case if they agree on the values of all the bytes on the overlapping region |
2211 | // between them. |
2212 | // |
2213 | // The overlapping region is the portion of the two string literals that must |
2214 | // overlap in memory if the pointers actually point to the same address at |
2215 | // runtime. For example, if LHS is "abcdef" + 3 and RHS is "cdef\0gh" + 1 then |
2216 | // the overlapping region is "cdef\0", which in this case does agree, so the |
2217 | // strings are potentially overlapping. Conversely, for "foobar" + 3 versus |
2218 | // "bazbar" + 3, the overlapping region contains all of both strings, so they |
2219 | // are not potentially overlapping, even though they agree from the given |
2220 | // addresses onwards. |
2221 | // |
2222 | // See open core issue CWG2765 which is discussing the desired rule here. |
2223 | static bool ArePotentiallyOverlappingStringLiterals(const EvalInfo &Info, |
2224 | const LValue &LHS, |
2225 | const LValue &RHS) { |
2226 | LValueBaseString LHSString, RHSString; |
2227 | if (!GetLValueBaseAsString(Info, LVal: LHS, AsString&: LHSString) || |
2228 | !GetLValueBaseAsString(Info, LVal: RHS, AsString&: RHSString)) |
2229 | return false; |
2230 | |
2231 | // This is the byte offset to the location of the first character of LHS |
2232 | // within RHS. We don't need to look at the characters of one string that |
2233 | // would appear before the start of the other string if they were merged. |
2234 | CharUnits Offset = RHS.Offset - LHS.Offset; |
2235 | if (Offset.isNegative()) { |
2236 | if (LHSString.Bytes.size() < (size_t)-Offset.getQuantity()) |
2237 | return false; |
2238 | LHSString.Bytes = LHSString.Bytes.drop_front(N: -Offset.getQuantity()); |
2239 | } else { |
2240 | if (RHSString.Bytes.size() < (size_t)Offset.getQuantity()) |
2241 | return false; |
2242 | RHSString.Bytes = RHSString.Bytes.drop_front(N: Offset.getQuantity()); |
2243 | } |
2244 | |
2245 | bool LHSIsLonger = LHSString.Bytes.size() > RHSString.Bytes.size(); |
2246 | StringRef Longer = LHSIsLonger ? LHSString.Bytes : RHSString.Bytes; |
2247 | StringRef Shorter = LHSIsLonger ? RHSString.Bytes : LHSString.Bytes; |
2248 | int ShorterCharWidth = (LHSIsLonger ? RHSString : LHSString).CharWidth; |
2249 | |
2250 | // The null terminator isn't included in the string data, so check for it |
2251 | // manually. If the longer string doesn't have a null terminator where the |
2252 | // shorter string ends, they aren't potentially overlapping. |
2253 | for (int NullByte : llvm::seq(Size: ShorterCharWidth)) { |
2254 | if (Shorter.size() + NullByte >= Longer.size()) |
2255 | break; |
2256 | if (Longer[Shorter.size() + NullByte]) |
2257 | return false; |
2258 | } |
2259 | |
2260 | // Otherwise, they're potentially overlapping if and only if the overlapping |
2261 | // region is the same. |
2262 | return Shorter == Longer.take_front(N: Shorter.size()); |
2263 | } |
2264 | |
2265 | static bool IsWeakLValue(const LValue &Value) { |
2266 | const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value); |
2267 | return Decl && Decl->isWeak(); |
2268 | } |
2269 | |
2270 | static bool isZeroSized(const LValue &Value) { |
2271 | const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value); |
2272 | if (isa_and_nonnull<VarDecl>(Val: Decl)) { |
2273 | QualType Ty = Decl->getType(); |
2274 | if (Ty->isArrayType()) |
2275 | return Ty->isIncompleteType() || |
2276 | Decl->getASTContext().getTypeSize(Ty) == 0; |
2277 | } |
2278 | return false; |
2279 | } |
2280 | |
2281 | static bool HasSameBase(const LValue &A, const LValue &B) { |
2282 | if (!A.getLValueBase()) |
2283 | return !B.getLValueBase(); |
2284 | if (!B.getLValueBase()) |
2285 | return false; |
2286 | |
2287 | if (A.getLValueBase().getOpaqueValue() != |
2288 | B.getLValueBase().getOpaqueValue()) |
2289 | return false; |
2290 | |
2291 | return A.getLValueCallIndex() == B.getLValueCallIndex() && |
2292 | A.getLValueVersion() == B.getLValueVersion(); |
2293 | } |
2294 | |
2295 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
2296 | assert(Base && "no location for a null lvalue"); |
2297 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
2298 | |
2299 | // For a parameter, find the corresponding call stack frame (if it still |
2300 | // exists), and point at the parameter of the function definition we actually |
2301 | // invoked. |
2302 | if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(Val: VD)) { |
2303 | unsigned Idx = PVD->getFunctionScopeIndex(); |
2304 | for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) { |
2305 | if (F->Arguments.CallIndex == Base.getCallIndex() && |
2306 | F->Arguments.Version == Base.getVersion() && F->Callee && |
2307 | Idx < F->Callee->getNumParams()) { |
2308 | VD = F->Callee->getParamDecl(i: Idx); |
2309 | break; |
2310 | } |
2311 | } |
2312 | } |
2313 | |
2314 | if (VD) |
2315 | Info.Note(VD->getLocation(), diag::note_declared_at); |
2316 | else if (const Expr *E = Base.dyn_cast<const Expr*>()) |
2317 | Info.Note(E->getExprLoc(), diag::note_constexpr_temporary_here); |
2318 | else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { |
2319 | // FIXME: Produce a note for dangling pointers too. |
2320 | if (std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA)) |
2321 | Info.Note((*Alloc)->AllocExpr->getExprLoc(), |
2322 | diag::note_constexpr_dynamic_alloc_here); |
2323 | } |
2324 | |
2325 | // We have no information to show for a typeid(T) object. |
2326 | } |
2327 | |
2328 | enum class CheckEvaluationResultKind { |
2329 | ConstantExpression, |
2330 | FullyInitialized, |
2331 | }; |
2332 | |
2333 | /// Materialized temporaries that we've already checked to determine if they're |
2334 | /// initializsed by a constant expression. |
2335 | using CheckedTemporaries = |
2336 | llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>; |
2337 | |
2338 | static bool CheckEvaluationResult(CheckEvaluationResultKind CERK, |
2339 | EvalInfo &Info, SourceLocation DiagLoc, |
2340 | QualType Type, const APValue &Value, |
2341 | ConstantExprKind Kind, |
2342 | const FieldDecl *SubobjectDecl, |
2343 | CheckedTemporaries &CheckedTemps); |
2344 | |
2345 | /// Check that this reference or pointer core constant expression is a valid |
2346 | /// value for an address or reference constant expression. Return true if we |
2347 | /// can fold this expression, whether or not it's a constant expression. |
2348 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
2349 | QualType Type, const LValue &LVal, |
2350 | ConstantExprKind Kind, |
2351 | CheckedTemporaries &CheckedTemps) { |
2352 | bool IsReferenceType = Type->isReferenceType(); |
2353 | |
2354 | APValue::LValueBase Base = LVal.getLValueBase(); |
2355 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
2356 | |
2357 | const Expr *BaseE = Base.dyn_cast<const Expr *>(); |
2358 | const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>(); |
2359 | |
2360 | // Additional restrictions apply in a template argument. We only enforce the |
2361 | // C++20 restrictions here; additional syntactic and semantic restrictions |
2362 | // are applied elsewhere. |
2363 | if (isTemplateArgument(Kind)) { |
2364 | int InvalidBaseKind = -1; |
2365 | StringRef Ident; |
2366 | if (Base.is<TypeInfoLValue>()) |
2367 | InvalidBaseKind = 0; |
2368 | else if (isa_and_nonnull<StringLiteral>(Val: BaseE)) |
2369 | InvalidBaseKind = 1; |
2370 | else if (isa_and_nonnull<MaterializeTemporaryExpr>(Val: BaseE) || |
2371 | isa_and_nonnull<LifetimeExtendedTemporaryDecl>(Val: BaseVD)) |
2372 | InvalidBaseKind = 2; |
2373 | else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(Val: BaseE)) { |
2374 | InvalidBaseKind = 3; |
2375 | Ident = PE->getIdentKindName(); |
2376 | } |
2377 | |
2378 | if (InvalidBaseKind != -1) { |
2379 | Info.FFDiag(Loc, diag::note_constexpr_invalid_template_arg) |
2380 | << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind |
2381 | << Ident; |
2382 | return false; |
2383 | } |
2384 | } |
2385 | |
2386 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(Val: BaseVD); |
2387 | FD && FD->isImmediateFunction()) { |
2388 | Info.FFDiag(Loc, diag::note_consteval_address_accessible) |
2389 | << !Type->isAnyPointerType(); |
2390 | Info.Note(FD->getLocation(), diag::note_declared_at); |
2391 | return false; |
2392 | } |
2393 | |
2394 | // Check that the object is a global. Note that the fake 'this' object we |
2395 | // manufacture when checking potential constant expressions is conservatively |
2396 | // assumed to be global here. |
2397 | if (!IsGlobalLValue(B: Base)) { |
2398 | if (Info.getLangOpts().CPlusPlus11) { |
2399 | Info.FFDiag(Loc, diag::note_constexpr_non_global, 1) |
2400 | << IsReferenceType << !Designator.Entries.empty() << !!BaseVD |
2401 | << BaseVD; |
2402 | auto *VarD = dyn_cast_or_null<VarDecl>(Val: BaseVD); |
2403 | if (VarD && VarD->isConstexpr()) { |
2404 | // Non-static local constexpr variables have unintuitive semantics: |
2405 | // constexpr int a = 1; |
2406 | // constexpr const int *p = &a; |
2407 | // ... is invalid because the address of 'a' is not constant. Suggest |
2408 | // adding a 'static' in this case. |
2409 | Info.Note(VarD->getLocation(), diag::note_constexpr_not_static) |
2410 | << VarD |
2411 | << FixItHint::CreateInsertion(VarD->getBeginLoc(), "static "); |
2412 | } else { |
2413 | NoteLValueLocation(Info, Base); |
2414 | } |
2415 | } else { |
2416 | Info.FFDiag(Loc); |
2417 | } |
2418 | // Don't allow references to temporaries to escape. |
2419 | return false; |
2420 | } |
2421 | assert((Info.checkingPotentialConstantExpression() || |
2422 | LVal.getLValueCallIndex() == 0) && |
2423 | "have call index for global lvalue"); |
2424 | |
2425 | if (LVal.allowConstexprUnknown()) { |
2426 | if (BaseVD) { |
2427 | Info.FFDiag(Loc, diag::note_constexpr_var_init_non_constant, 1) << BaseVD; |
2428 | NoteLValueLocation(Info, Base); |
2429 | } else { |
2430 | Info.FFDiag(Loc); |
2431 | } |
2432 | return false; |
2433 | } |
2434 | |
2435 | if (Base.is<DynamicAllocLValue>()) { |
2436 | Info.FFDiag(Loc, diag::note_constexpr_dynamic_alloc) |
2437 | << IsReferenceType << !Designator.Entries.empty(); |
2438 | NoteLValueLocation(Info, Base); |
2439 | return false; |
2440 | } |
2441 | |
2442 | if (BaseVD) { |
2443 | if (const VarDecl *Var = dyn_cast<const VarDecl>(Val: BaseVD)) { |
2444 | // Check if this is a thread-local variable. |
2445 | if (Var->getTLSKind()) |
2446 | // FIXME: Diagnostic! |
2447 | return false; |
2448 | |
2449 | // A dllimport variable never acts like a constant, unless we're |
2450 | // evaluating a value for use only in name mangling. |
2451 | if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>()) |
2452 | // FIXME: Diagnostic! |
2453 | return false; |
2454 | |
2455 | // In CUDA/HIP device compilation, only device side variables have |
2456 | // constant addresses. |
2457 | if (Info.getASTContext().getLangOpts().CUDA && |
2458 | Info.getASTContext().getLangOpts().CUDAIsDevice && |
2459 | Info.getASTContext().CUDAConstantEvalCtx.NoWrongSidedVars) { |
2460 | if ((!Var->hasAttr<CUDADeviceAttr>() && |
2461 | !Var->hasAttr<CUDAConstantAttr>() && |
2462 | !Var->getType()->isCUDADeviceBuiltinSurfaceType() && |
2463 | !Var->getType()->isCUDADeviceBuiltinTextureType()) || |
2464 | Var->hasAttr<HIPManagedAttr>()) |
2465 | return false; |
2466 | } |
2467 | } |
2468 | if (const auto *FD = dyn_cast<const FunctionDecl>(Val: BaseVD)) { |
2469 | // __declspec(dllimport) must be handled very carefully: |
2470 | // We must never initialize an expression with the thunk in C++. |
2471 | // Doing otherwise would allow the same id-expression to yield |
2472 | // different addresses for the same function in different translation |
2473 | // units. However, this means that we must dynamically initialize the |
2474 | // expression with the contents of the import address table at runtime. |
2475 | // |
2476 | // The C language has no notion of ODR; furthermore, it has no notion of |
2477 | // dynamic initialization. This means that we are permitted to |
2478 | // perform initialization with the address of the thunk. |
2479 | if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) && |
2480 | FD->hasAttr<DLLImportAttr>()) |
2481 | // FIXME: Diagnostic! |
2482 | return false; |
2483 | } |
2484 | } else if (const auto *MTE = |
2485 | dyn_cast_or_null<MaterializeTemporaryExpr>(Val: BaseE)) { |
2486 | if (CheckedTemps.insert(Ptr: MTE).second) { |
2487 | QualType TempType = getType(B: Base); |
2488 | if (TempType.isDestructedType()) { |
2489 | Info.FFDiag(MTE->getExprLoc(), |
2490 | diag::note_constexpr_unsupported_temporary_nontrivial_dtor) |
2491 | << TempType; |
2492 | return false; |
2493 | } |
2494 | |
2495 | APValue *V = MTE->getOrCreateValue(MayCreate: false); |
2496 | assert(V && "evasluation result refers to uninitialised temporary"); |
2497 | if (!CheckEvaluationResult(CheckEvaluationResultKind::ConstantExpression, |
2498 | Info, MTE->getExprLoc(), TempType, *V, Kind, |
2499 | /*SubobjectDecl=*/nullptr, CheckedTemps)) |
2500 | return false; |
2501 | } |
2502 | } |
2503 | |
2504 | // Allow address constant expressions to be past-the-end pointers. This is |
2505 | // an extension: the standard requires them to point to an object. |
2506 | if (!IsReferenceType) |
2507 | return true; |
2508 | |
2509 | // A reference constant expression must refer to an object. |
2510 | if (!Base) { |
2511 | // FIXME: diagnostic |
2512 | Info.CCEDiag(Loc); |
2513 | return true; |
2514 | } |
2515 | |
2516 | // Does this refer one past the end of some object? |
2517 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
2518 | Info.FFDiag(Loc, diag::note_constexpr_past_end, 1) |
2519 | << !Designator.Entries.empty() << !!BaseVD << BaseVD; |
2520 | NoteLValueLocation(Info, Base); |
2521 | } |
2522 | |
2523 | return true; |
2524 | } |
2525 | |
2526 | /// Member pointers are constant expressions unless they point to a |
2527 | /// non-virtual dllimport member function. |
2528 | static bool CheckMemberPointerConstantExpression(EvalInfo &Info, |
2529 | SourceLocation Loc, |
2530 | QualType Type, |
2531 | const APValue &Value, |
2532 | ConstantExprKind Kind) { |
2533 | const ValueDecl *Member = Value.getMemberPointerDecl(); |
2534 | const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Val: Member); |
2535 | if (!FD) |
2536 | return true; |
2537 | if (FD->isImmediateFunction()) { |
2538 | Info.FFDiag(Loc, diag::note_consteval_address_accessible) << /*pointer*/ 0; |
2539 | Info.Note(FD->getLocation(), diag::note_declared_at); |
2540 | return false; |
2541 | } |
2542 | return isForManglingOnly(Kind) || FD->isVirtual() || |
2543 | !FD->hasAttr<DLLImportAttr>(); |
2544 | } |
2545 | |
2546 | /// Check that this core constant expression is of literal type, and if not, |
2547 | /// produce an appropriate diagnostic. |
2548 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
2549 | const LValue *This = nullptr) { |
2550 | // The restriction to literal types does not exist in C++23 anymore. |
2551 | if (Info.getLangOpts().CPlusPlus23) |
2552 | return true; |
2553 | |
2554 | if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Info.Ctx)) |
2555 | return true; |
2556 | |
2557 | // C++1y: A constant initializer for an object o [...] may also invoke |
2558 | // constexpr constructors for o and its subobjects even if those objects |
2559 | // are of non-literal class types. |
2560 | // |
2561 | // C++11 missed this detail for aggregates, so classes like this: |
2562 | // struct foo_t { union { int i; volatile int j; } u; }; |
2563 | // are not (obviously) initializable like so: |
2564 | // __attribute__((__require_constant_initialization__)) |
2565 | // static const foo_t x = {{0}}; |
2566 | // because "i" is a subobject with non-literal initialization (due to the |
2567 | // volatile member of the union). See: |
2568 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 |
2569 | // Therefore, we use the C++1y behavior. |
2570 | if (This && Info.EvaluatingDecl == This->getLValueBase()) |
2571 | return true; |
2572 | |
2573 | // Prvalue constant expressions must be of literal types. |
2574 | if (Info.getLangOpts().CPlusPlus11) |
2575 | Info.FFDiag(E, diag::note_constexpr_nonliteral) |
2576 | << E->getType(); |
2577 | else |
2578 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2579 | return false; |
2580 | } |
2581 | |
2582 | static bool CheckEvaluationResult(CheckEvaluationResultKind CERK, |
2583 | EvalInfo &Info, SourceLocation DiagLoc, |
2584 | QualType Type, const APValue &Value, |
2585 | ConstantExprKind Kind, |
2586 | const FieldDecl *SubobjectDecl, |
2587 | CheckedTemporaries &CheckedTemps) { |
2588 | if (!Value.hasValue()) { |
2589 | if (SubobjectDecl) { |
2590 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
2591 | << /*(name)*/ 1 << SubobjectDecl; |
2592 | Info.Note(SubobjectDecl->getLocation(), |
2593 | diag::note_constexpr_subobject_declared_here); |
2594 | } else { |
2595 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
2596 | << /*of type*/ 0 << Type; |
2597 | } |
2598 | return false; |
2599 | } |
2600 | |
2601 | // We allow _Atomic(T) to be initialized from anything that T can be |
2602 | // initialized from. |
2603 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
2604 | Type = AT->getValueType(); |
2605 | |
2606 | // Core issue 1454: For a literal constant expression of array or class type, |
2607 | // each subobject of its value shall have been initialized by a constant |
2608 | // expression. |
2609 | if (Value.isArray()) { |
2610 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
2611 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
2612 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy, |
2613 | Value: Value.getArrayInitializedElt(I), Kind, |
2614 | SubobjectDecl, CheckedTemps)) |
2615 | return false; |
2616 | } |
2617 | if (!Value.hasArrayFiller()) |
2618 | return true; |
2619 | return CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy, |
2620 | Value: Value.getArrayFiller(), Kind, SubobjectDecl, |
2621 | CheckedTemps); |
2622 | } |
2623 | if (Value.isUnion() && Value.getUnionField()) { |
2624 | return CheckEvaluationResult( |
2625 | CERK, Info, DiagLoc, Value.getUnionField()->getType(), |
2626 | Value.getUnionValue(), Kind, Value.getUnionField(), CheckedTemps); |
2627 | } |
2628 | if (Value.isStruct()) { |
2629 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
2630 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) { |
2631 | unsigned BaseIndex = 0; |
2632 | for (const CXXBaseSpecifier &BS : CD->bases()) { |
2633 | const APValue &BaseValue = Value.getStructBase(i: BaseIndex); |
2634 | if (!BaseValue.hasValue()) { |
2635 | SourceLocation TypeBeginLoc = BS.getBaseTypeLoc(); |
2636 | Info.FFDiag(TypeBeginLoc, diag::note_constexpr_uninitialized_base) |
2637 | << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc()); |
2638 | return false; |
2639 | } |
2640 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(), Value: BaseValue, |
2641 | Kind, /*SubobjectDecl=*/nullptr, |
2642 | CheckedTemps)) |
2643 | return false; |
2644 | ++BaseIndex; |
2645 | } |
2646 | } |
2647 | for (const auto *I : RD->fields()) { |
2648 | if (I->isUnnamedBitField()) |
2649 | continue; |
2650 | |
2651 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, I->getType(), |
2652 | Value.getStructField(i: I->getFieldIndex()), Kind, |
2653 | I, CheckedTemps)) |
2654 | return false; |
2655 | } |
2656 | } |
2657 | |
2658 | if (Value.isLValue() && |
2659 | CERK == CheckEvaluationResultKind::ConstantExpression) { |
2660 | LValue LVal; |
2661 | LVal.setFrom(Ctx&: Info.Ctx, V: Value); |
2662 | return CheckLValueConstantExpression(Info, Loc: DiagLoc, Type, LVal, Kind, |
2663 | CheckedTemps); |
2664 | } |
2665 | |
2666 | if (Value.isMemberPointer() && |
2667 | CERK == CheckEvaluationResultKind::ConstantExpression) |
2668 | return CheckMemberPointerConstantExpression(Info, Loc: DiagLoc, Type, Value, Kind); |
2669 | |
2670 | // Everything else is fine. |
2671 | return true; |
2672 | } |
2673 | |
2674 | /// Check that this core constant expression value is a valid value for a |
2675 | /// constant expression. If not, report an appropriate diagnostic. Does not |
2676 | /// check that the expression is of literal type. |
2677 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
2678 | QualType Type, const APValue &Value, |
2679 | ConstantExprKind Kind) { |
2680 | // Nothing to check for a constant expression of type 'cv void'. |
2681 | if (Type->isVoidType()) |
2682 | return true; |
2683 | |
2684 | CheckedTemporaries CheckedTemps; |
2685 | return CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression, |
2686 | Info, DiagLoc, Type, Value, Kind, |
2687 | /*SubobjectDecl=*/nullptr, CheckedTemps); |
2688 | } |
2689 | |
2690 | /// Check that this evaluated value is fully-initialized and can be loaded by |
2691 | /// an lvalue-to-rvalue conversion. |
2692 | static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc, |
2693 | QualType Type, const APValue &Value) { |
2694 | CheckedTemporaries CheckedTemps; |
2695 | return CheckEvaluationResult( |
2696 | CERK: CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value, |
2697 | Kind: ConstantExprKind::Normal, /*SubobjectDecl=*/nullptr, CheckedTemps); |
2698 | } |
2699 | |
2700 | /// Enforce C++2a [expr.const]/4.17, which disallows new-expressions unless |
2701 | /// "the allocated storage is deallocated within the evaluation". |
2702 | static bool CheckMemoryLeaks(EvalInfo &Info) { |
2703 | if (!Info.HeapAllocs.empty()) { |
2704 | // We can still fold to a constant despite a compile-time memory leak, |
2705 | // so long as the heap allocation isn't referenced in the result (we check |
2706 | // that in CheckConstantExpression). |
2707 | Info.CCEDiag(Info.HeapAllocs.begin()->second.AllocExpr, |
2708 | diag::note_constexpr_memory_leak) |
2709 | << unsigned(Info.HeapAllocs.size() - 1); |
2710 | } |
2711 | return true; |
2712 | } |
2713 | |
2714 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
2715 | // A null base expression indicates a null pointer. These are always |
2716 | // evaluatable, and they are false unless the offset is zero. |
2717 | if (!Value.getLValueBase()) { |
2718 | // TODO: Should a non-null pointer with an offset of zero evaluate to true? |
2719 | Result = !Value.getLValueOffset().isZero(); |
2720 | return true; |
2721 | } |
2722 | |
2723 | // We have a non-null base. These are generally known to be true, but if it's |
2724 | // a weak declaration it can be null at runtime. |
2725 | Result = true; |
2726 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
2727 | return !Decl || !Decl->isWeak(); |
2728 | } |
2729 | |
2730 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
2731 | // TODO: This function should produce notes if it fails. |
2732 | switch (Val.getKind()) { |
2733 | case APValue::None: |
2734 | case APValue::Indeterminate: |
2735 | return false; |
2736 | case APValue::Int: |
2737 | Result = Val.getInt().getBoolValue(); |
2738 | return true; |
2739 | case APValue::FixedPoint: |
2740 | Result = Val.getFixedPoint().getBoolValue(); |
2741 | return true; |
2742 | case APValue::Float: |
2743 | Result = !Val.getFloat().isZero(); |
2744 | return true; |
2745 | case APValue::ComplexInt: |
2746 | Result = Val.getComplexIntReal().getBoolValue() || |
2747 | Val.getComplexIntImag().getBoolValue(); |
2748 | return true; |
2749 | case APValue::ComplexFloat: |
2750 | Result = !Val.getComplexFloatReal().isZero() || |
2751 | !Val.getComplexFloatImag().isZero(); |
2752 | return true; |
2753 | case APValue::LValue: |
2754 | return EvalPointerValueAsBool(Value: Val, Result); |
2755 | case APValue::MemberPointer: |
2756 | if (Val.getMemberPointerDecl() && Val.getMemberPointerDecl()->isWeak()) { |
2757 | return false; |
2758 | } |
2759 | Result = Val.getMemberPointerDecl(); |
2760 | return true; |
2761 | case APValue::Vector: |
2762 | case APValue::Array: |
2763 | case APValue::Struct: |
2764 | case APValue::Union: |
2765 | case APValue::AddrLabelDiff: |
2766 | return false; |
2767 | } |
2768 | |
2769 | llvm_unreachable("unknown APValue kind"); |
2770 | } |
2771 | |
2772 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
2773 | EvalInfo &Info) { |
2774 | assert(!E->isValueDependent()); |
2775 | assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
2776 | APValue Val; |
2777 | if (!Evaluate(Result&: Val, Info, E)) |
2778 | return false; |
2779 | return HandleConversionToBool(Val, Result); |
2780 | } |
2781 | |
2782 | template<typename T> |
2783 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
2784 | const T &SrcValue, QualType DestType) { |
2785 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
2786 | << SrcValue << DestType; |
2787 | return Info.noteUndefinedBehavior(); |
2788 | } |
2789 | |
2790 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
2791 | QualType SrcType, const APFloat &Value, |
2792 | QualType DestType, APSInt &Result) { |
2793 | unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType); |
2794 | // Determine whether we are converting to unsigned or signed. |
2795 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
2796 | |
2797 | Result = APSInt(DestWidth, !DestSigned); |
2798 | bool ignored; |
2799 | if (Value.convertToInteger(Result, RM: llvm::APFloat::rmTowardZero, IsExact: &ignored) |
2800 | & APFloat::opInvalidOp) |
2801 | return HandleOverflow(Info, E, SrcValue: Value, DestType); |
2802 | return true; |
2803 | } |
2804 | |
2805 | /// Get rounding mode to use in evaluation of the specified expression. |
2806 | /// |
2807 | /// If rounding mode is unknown at compile time, still try to evaluate the |
2808 | /// expression. If the result is exact, it does not depend on rounding mode. |
2809 | /// So return "tonearest" mode instead of "dynamic". |
2810 | static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E) { |
2811 | llvm::RoundingMode RM = |
2812 | E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).getRoundingMode(); |
2813 | if (RM == llvm::RoundingMode::Dynamic) |
2814 | RM = llvm::RoundingMode::NearestTiesToEven; |
2815 | return RM; |
2816 | } |
2817 | |
2818 | /// Check if the given evaluation result is allowed for constant evaluation. |
2819 | static bool checkFloatingPointResult(EvalInfo &Info, const Expr *E, |
2820 | APFloat::opStatus St) { |
2821 | // In a constant context, assume that any dynamic rounding mode or FP |
2822 | // exception state matches the default floating-point environment. |
2823 | if (Info.InConstantContext) |
2824 | return true; |
2825 | |
2826 | FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()); |
2827 | if ((St & APFloat::opInexact) && |
2828 | FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) { |
2829 | // Inexact result means that it depends on rounding mode. If the requested |
2830 | // mode is dynamic, the evaluation cannot be made in compile time. |
2831 | Info.FFDiag(E, diag::note_constexpr_dynamic_rounding); |
2832 | return false; |
2833 | } |
2834 | |
2835 | if ((St != APFloat::opOK) && |
2836 | (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic || |
2837 | FPO.getExceptionMode() != LangOptions::FPE_Ignore || |
2838 | FPO.getAllowFEnvAccess())) { |
2839 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
2840 | return false; |
2841 | } |
2842 | |
2843 | if ((St & APFloat::opStatus::opInvalidOp) && |
2844 | FPO.getExceptionMode() != LangOptions::FPE_Ignore) { |
2845 | // There is no usefully definable result. |
2846 | Info.FFDiag(E); |
2847 | return false; |
2848 | } |
2849 | |
2850 | // FIXME: if: |
2851 | // - evaluation triggered other FP exception, and |
2852 | // - exception mode is not "ignore", and |
2853 | // - the expression being evaluated is not a part of global variable |
2854 | // initializer, |
2855 | // the evaluation probably need to be rejected. |
2856 | return true; |
2857 | } |
2858 | |
2859 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
2860 | QualType SrcType, QualType DestType, |
2861 | APFloat &Result) { |
2862 | assert((isa<CastExpr>(E) || isa<CompoundAssignOperator>(E) || |
2863 | isa<ConvertVectorExpr>(E)) && |
2864 | "HandleFloatToFloatCast has been checked with only CastExpr, " |
2865 | "CompoundAssignOperator and ConvertVectorExpr. Please either validate " |
2866 | "the new expression or address the root cause of this usage."); |
2867 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E); |
2868 | APFloat::opStatus St; |
2869 | APFloat Value = Result; |
2870 | bool ignored; |
2871 | St = Result.convert(ToSemantics: Info.Ctx.getFloatTypeSemantics(T: DestType), RM, losesInfo: &ignored); |
2872 | return checkFloatingPointResult(Info, E, St); |
2873 | } |
2874 | |
2875 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
2876 | QualType DestType, QualType SrcType, |
2877 | const APSInt &Value) { |
2878 | unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType); |
2879 | // Figure out if this is a truncate, extend or noop cast. |
2880 | // If the input is signed, do a sign extend, noop, or truncate. |
2881 | APSInt Result = Value.extOrTrunc(width: DestWidth); |
2882 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
2883 | if (DestType->isBooleanType()) |
2884 | Result = Value.getBoolValue(); |
2885 | return Result; |
2886 | } |
2887 | |
2888 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
2889 | const FPOptions FPO, |
2890 | QualType SrcType, const APSInt &Value, |
2891 | QualType DestType, APFloat &Result) { |
2892 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(T: DestType), 1); |
2893 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E); |
2894 | APFloat::opStatus St = Result.convertFromAPInt(Input: Value, IsSigned: Value.isSigned(), RM); |
2895 | return checkFloatingPointResult(Info, E, St); |
2896 | } |
2897 | |
2898 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
2899 | APValue &Value, const FieldDecl *FD) { |
2900 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
2901 | |
2902 | if (!Value.isInt()) { |
2903 | // Trying to store a pointer-cast-to-integer into a bitfield. |
2904 | // FIXME: In this case, we should provide the diagnostic for casting |
2905 | // a pointer to an integer. |
2906 | assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
2907 | Info.FFDiag(E); |
2908 | return false; |
2909 | } |
2910 | |
2911 | APSInt &Int = Value.getInt(); |
2912 | unsigned OldBitWidth = Int.getBitWidth(); |
2913 | unsigned NewBitWidth = FD->getBitWidthValue(); |
2914 | if (NewBitWidth < OldBitWidth) |
2915 | Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth); |
2916 | return true; |
2917 | } |
2918 | |
2919 | /// Perform the given integer operation, which is known to need at most BitWidth |
2920 | /// bits, and check for overflow in the original type (if that type was not an |
2921 | /// unsigned type). |
2922 | template<typename Operation> |
2923 | static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
2924 | const APSInt &LHS, const APSInt &RHS, |
2925 | unsigned BitWidth, Operation Op, |
2926 | APSInt &Result) { |
2927 | if (LHS.isUnsigned()) { |
2928 | Result = Op(LHS, RHS); |
2929 | return true; |
2930 | } |
2931 | |
2932 | APSInt Value(Op(LHS.extend(width: BitWidth), RHS.extend(width: BitWidth)), false); |
2933 | Result = Value.trunc(width: LHS.getBitWidth()); |
2934 | if (Result.extend(width: BitWidth) != Value) { |
2935 | if (Info.checkingForUndefinedBehavior()) |
2936 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
2937 | diag::warn_integer_constant_overflow) |
2938 | << toString(Result, 10, Result.isSigned(), /*formatAsCLiteral=*/false, |
2939 | /*UpperCase=*/true, /*InsertSeparators=*/true) |
2940 | << E->getType() << E->getSourceRange(); |
2941 | return HandleOverflow(Info, E, SrcValue: Value, DestType: E->getType()); |
2942 | } |
2943 | return true; |
2944 | } |
2945 | |
2946 | /// Perform the given binary integer operation. |
2947 | static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E, |
2948 | const APSInt &LHS, BinaryOperatorKind Opcode, |
2949 | APSInt RHS, APSInt &Result) { |
2950 | bool HandleOverflowResult = true; |
2951 | switch (Opcode) { |
2952 | default: |
2953 | Info.FFDiag(E); |
2954 | return false; |
2955 | case BO_Mul: |
2956 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
2957 | std::multiplies<APSInt>(), Result); |
2958 | case BO_Add: |
2959 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2960 | std::plus<APSInt>(), Result); |
2961 | case BO_Sub: |
2962 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2963 | std::minus<APSInt>(), Result); |
2964 | case BO_And: Result = LHS & RHS; return true; |
2965 | case BO_Xor: Result = LHS ^ RHS; return true; |
2966 | case BO_Or: Result = LHS | RHS; return true; |
2967 | case BO_Div: |
2968 | case BO_Rem: |
2969 | if (RHS == 0) { |
2970 | Info.FFDiag(E, diag::note_expr_divide_by_zero) |
2971 | << E->getRHS()->getSourceRange(); |
2972 | return false; |
2973 | } |
2974 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports |
2975 | // this operation and gives the two's complement result. |
2976 | if (RHS.isNegative() && RHS.isAllOnes() && LHS.isSigned() && |
2977 | LHS.isMinSignedValue()) |
2978 | HandleOverflowResult = HandleOverflow( |
2979 | Info, E, -LHS.extend(width: LHS.getBitWidth() + 1), E->getType()); |
2980 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
2981 | return HandleOverflowResult; |
2982 | case BO_Shl: { |
2983 | if (Info.getLangOpts().OpenCL) |
2984 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
2985 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2986 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2987 | RHS.isUnsigned()); |
2988 | else if (RHS.isSigned() && RHS.isNegative()) { |
2989 | // During constant-folding, a negative shift is an opposite shift. Such |
2990 | // a shift is not a constant expression. |
2991 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2992 | if (!Info.noteUndefinedBehavior()) |
2993 | return false; |
2994 | RHS = -RHS; |
2995 | goto shift_right; |
2996 | } |
2997 | shift_left: |
2998 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
2999 | // the shifted type. |
3000 | unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1); |
3001 | if (SA != RHS) { |
3002 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
3003 | << RHS << E->getType() << LHS.getBitWidth(); |
3004 | if (!Info.noteUndefinedBehavior()) |
3005 | return false; |
3006 | } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) { |
3007 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
3008 | // operand, and must not overflow the corresponding unsigned type. |
3009 | // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to |
3010 | // E1 x 2^E2 module 2^N. |
3011 | if (LHS.isNegative()) { |
3012 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
3013 | if (!Info.noteUndefinedBehavior()) |
3014 | return false; |
3015 | } else if (LHS.countl_zero() < SA) { |
3016 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
3017 | if (!Info.noteUndefinedBehavior()) |
3018 | return false; |
3019 | } |
3020 | } |
3021 | Result = LHS << SA; |
3022 | return true; |
3023 | } |
3024 | case BO_Shr: { |
3025 | if (Info.getLangOpts().OpenCL) |
3026 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
3027 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
3028 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
3029 | RHS.isUnsigned()); |
3030 | else if (RHS.isSigned() && RHS.isNegative()) { |
3031 | // During constant-folding, a negative shift is an opposite shift. Such a |
3032 | // shift is not a constant expression. |
3033 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
3034 | if (!Info.noteUndefinedBehavior()) |
3035 | return false; |
3036 | RHS = -RHS; |
3037 | goto shift_left; |
3038 | } |
3039 | shift_right: |
3040 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
3041 | // shifted type. |
3042 | unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1); |
3043 | if (SA != RHS) { |
3044 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
3045 | << RHS << E->getType() << LHS.getBitWidth(); |
3046 | if (!Info.noteUndefinedBehavior()) |
3047 | return false; |
3048 | } |
3049 | |
3050 | Result = LHS >> SA; |
3051 | return true; |
3052 | } |
3053 | |
3054 | case BO_LT: Result = LHS < RHS; return true; |
3055 | case BO_GT: Result = LHS > RHS; return true; |
3056 | case BO_LE: Result = LHS <= RHS; return true; |
3057 | case BO_GE: Result = LHS >= RHS; return true; |
3058 | case BO_EQ: Result = LHS == RHS; return true; |
3059 | case BO_NE: Result = LHS != RHS; return true; |
3060 | case BO_Cmp: |
3061 | llvm_unreachable("BO_Cmp should be handled elsewhere"); |
3062 | } |
3063 | } |
3064 | |
3065 | /// Perform the given binary floating-point operation, in-place, on LHS. |
3066 | static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E, |
3067 | APFloat &LHS, BinaryOperatorKind Opcode, |
3068 | const APFloat &RHS) { |
3069 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E); |
3070 | APFloat::opStatus St; |
3071 | switch (Opcode) { |
3072 | default: |
3073 | Info.FFDiag(E); |
3074 | return false; |
3075 | case BO_Mul: |
3076 | St = LHS.multiply(RHS, RM); |
3077 | break; |
3078 | case BO_Add: |
3079 | St = LHS.add(RHS, RM); |
3080 | break; |
3081 | case BO_Sub: |
3082 | St = LHS.subtract(RHS, RM); |
3083 | break; |
3084 | case BO_Div: |
3085 | // [expr.mul]p4: |
3086 | // If the second operand of / or % is zero the behavior is undefined. |
3087 | if (RHS.isZero()) |
3088 | Info.CCEDiag(E, diag::note_expr_divide_by_zero); |
3089 | St = LHS.divide(RHS, RM); |
3090 | break; |
3091 | } |
3092 | |
3093 | // [expr.pre]p4: |
3094 | // If during the evaluation of an expression, the result is not |
3095 | // mathematically defined [...], the behavior is undefined. |
3096 | // FIXME: C++ rules require us to not conform to IEEE 754 here. |
3097 | if (LHS.isNaN()) { |
3098 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
3099 | return Info.noteUndefinedBehavior(); |
3100 | } |
3101 | |
3102 | return checkFloatingPointResult(Info, E, St); |
3103 | } |
3104 | |
3105 | static bool handleLogicalOpForVector(const APInt &LHSValue, |
3106 | BinaryOperatorKind Opcode, |
3107 | const APInt &RHSValue, APInt &Result) { |
3108 | bool LHS = (LHSValue != 0); |
3109 | bool RHS = (RHSValue != 0); |
3110 | |
3111 | if (Opcode == BO_LAnd) |
3112 | Result = LHS && RHS; |
3113 | else |
3114 | Result = LHS || RHS; |
3115 | return true; |
3116 | } |
3117 | static bool handleLogicalOpForVector(const APFloat &LHSValue, |
3118 | BinaryOperatorKind Opcode, |
3119 | const APFloat &RHSValue, APInt &Result) { |
3120 | bool LHS = !LHSValue.isZero(); |
3121 | bool RHS = !RHSValue.isZero(); |
3122 | |
3123 | if (Opcode == BO_LAnd) |
3124 | Result = LHS && RHS; |
3125 | else |
3126 | Result = LHS || RHS; |
3127 | return true; |
3128 | } |
3129 | |
3130 | static bool handleLogicalOpForVector(const APValue &LHSValue, |
3131 | BinaryOperatorKind Opcode, |
3132 | const APValue &RHSValue, APInt &Result) { |
3133 | // The result is always an int type, however operands match the first. |
3134 | if (LHSValue.getKind() == APValue::Int) |
3135 | return handleLogicalOpForVector(LHSValue: LHSValue.getInt(), Opcode, |
3136 | RHSValue: RHSValue.getInt(), Result); |
3137 | assert(LHSValue.getKind() == APValue::Float && "Should be no other options"); |
3138 | return handleLogicalOpForVector(LHSValue: LHSValue.getFloat(), Opcode, |
3139 | RHSValue: RHSValue.getFloat(), Result); |
3140 | } |
3141 | |
3142 | template <typename APTy> |
3143 | static bool |
3144 | handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode, |
3145 | const APTy &RHSValue, APInt &Result) { |
3146 | switch (Opcode) { |
3147 | default: |
3148 | llvm_unreachable("unsupported binary operator"); |
3149 | case BO_EQ: |
3150 | Result = (LHSValue == RHSValue); |
3151 | break; |
3152 | case BO_NE: |
3153 | Result = (LHSValue != RHSValue); |
3154 | break; |
3155 | case BO_LT: |
3156 | Result = (LHSValue < RHSValue); |
3157 | break; |
3158 | case BO_GT: |
3159 | Result = (LHSValue > RHSValue); |
3160 | break; |
3161 | case BO_LE: |
3162 | Result = (LHSValue <= RHSValue); |
3163 | break; |
3164 | case BO_GE: |
3165 | Result = (LHSValue >= RHSValue); |
3166 | break; |
3167 | } |
3168 | |
3169 | // The boolean operations on these vector types use an instruction that |
3170 | // results in a mask of '-1' for the 'truth' value. Ensure that we negate 1 |
3171 | // to -1 to make sure that we produce the correct value. |
3172 | Result.negate(); |
3173 | |
3174 | return true; |
3175 | } |
3176 | |
3177 | static bool handleCompareOpForVector(const APValue &LHSValue, |
3178 | BinaryOperatorKind Opcode, |
3179 | const APValue &RHSValue, APInt &Result) { |
3180 | // The result is always an int type, however operands match the first. |
3181 | if (LHSValue.getKind() == APValue::Int) |
3182 | return handleCompareOpForVectorHelper(LHSValue: LHSValue.getInt(), Opcode, |
3183 | RHSValue: RHSValue.getInt(), Result); |
3184 | assert(LHSValue.getKind() == APValue::Float && "Should be no other options"); |
3185 | return handleCompareOpForVectorHelper(LHSValue: LHSValue.getFloat(), Opcode, |
3186 | RHSValue: RHSValue.getFloat(), Result); |
3187 | } |
3188 | |
3189 | // Perform binary operations for vector types, in place on the LHS. |
3190 | static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E, |
3191 | BinaryOperatorKind Opcode, |
3192 | APValue &LHSValue, |
3193 | const APValue &RHSValue) { |
3194 | assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI && |
3195 | "Operation not supported on vector types"); |
3196 | |
3197 | const auto *VT = E->getType()->castAs<VectorType>(); |
3198 | unsigned NumElements = VT->getNumElements(); |
3199 | QualType EltTy = VT->getElementType(); |
3200 | |
3201 | // In the cases (typically C as I've observed) where we aren't evaluating |
3202 | // constexpr but are checking for cases where the LHS isn't yet evaluatable, |
3203 | // just give up. |
3204 | if (!LHSValue.isVector()) { |
3205 | assert(LHSValue.isLValue() && |
3206 | "A vector result that isn't a vector OR uncalculated LValue"); |
3207 | Info.FFDiag(E); |
3208 | return false; |
3209 | } |
3210 | |
3211 | assert(LHSValue.getVectorLength() == NumElements && |
3212 | RHSValue.getVectorLength() == NumElements && "Different vector sizes"); |
3213 | |
3214 | SmallVector<APValue, 4> ResultElements; |
3215 | |
3216 | for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) { |
3217 | APValue LHSElt = LHSValue.getVectorElt(I: EltNum); |
3218 | APValue RHSElt = RHSValue.getVectorElt(I: EltNum); |
3219 | |
3220 | if (EltTy->isIntegerType()) { |
3221 | APSInt EltResult{Info.Ctx.getIntWidth(T: EltTy), |
3222 | EltTy->isUnsignedIntegerType()}; |
3223 | bool Success = true; |
3224 | |
3225 | if (BinaryOperator::isLogicalOp(Opc: Opcode)) |
3226 | Success = handleLogicalOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult); |
3227 | else if (BinaryOperator::isComparisonOp(Opc: Opcode)) |
3228 | Success = handleCompareOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult); |
3229 | else |
3230 | Success = handleIntIntBinOp(Info, E, LHS: LHSElt.getInt(), Opcode, |
3231 | RHS: RHSElt.getInt(), Result&: EltResult); |
3232 | |
3233 | if (!Success) { |
3234 | Info.FFDiag(E); |
3235 | return false; |
3236 | } |
3237 | ResultElements.emplace_back(Args&: EltResult); |
3238 | |
3239 | } else if (EltTy->isFloatingType()) { |
3240 | assert(LHSElt.getKind() == APValue::Float && |
3241 | RHSElt.getKind() == APValue::Float && |
3242 | "Mismatched LHS/RHS/Result Type"); |
3243 | APFloat LHSFloat = LHSElt.getFloat(); |
3244 | |
3245 | if (!handleFloatFloatBinOp(Info, E, LHS&: LHSFloat, Opcode, |
3246 | RHS: RHSElt.getFloat())) { |
3247 | Info.FFDiag(E); |
3248 | return false; |
3249 | } |
3250 | |
3251 | ResultElements.emplace_back(Args&: LHSFloat); |
3252 | } |
3253 | } |
3254 | |
3255 | LHSValue = APValue(ResultElements.data(), ResultElements.size()); |
3256 | return true; |
3257 | } |
3258 | |
3259 | /// Cast an lvalue referring to a base subobject to a derived class, by |
3260 | /// truncating the lvalue's path to the given length. |
3261 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
3262 | const RecordDecl *TruncatedType, |
3263 | unsigned TruncatedElements) { |
3264 | SubobjectDesignator &D = Result.Designator; |
3265 | |
3266 | // Check we actually point to a derived class object. |
3267 | if (TruncatedElements == D.Entries.size()) |
3268 | return true; |
3269 | assert(TruncatedElements >= D.MostDerivedPathLength && |
3270 | "not casting to a derived class"); |
3271 | if (!Result.checkSubobject(Info, E, CSK: CSK_Derived)) |
3272 | return false; |
3273 | |
3274 | // Truncate the path to the subobject, and remove any derived-to-base offsets. |
3275 | const RecordDecl *RD = TruncatedType; |
3276 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
3277 | if (RD->isInvalidDecl()) return false; |
3278 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
3279 | const CXXRecordDecl *Base = getAsBaseClass(E: D.Entries[I]); |
3280 | if (isVirtualBaseClass(E: D.Entries[I])) |
3281 | Result.Offset -= Layout.getVBaseClassOffset(VBase: Base); |
3282 | else |
3283 | Result.Offset -= Layout.getBaseClassOffset(Base); |
3284 | RD = Base; |
3285 | } |
3286 | D.Entries.resize(N: TruncatedElements); |
3287 | return true; |
3288 | } |
3289 | |
3290 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
3291 | const CXXRecordDecl *Derived, |
3292 | const CXXRecordDecl *Base, |
3293 | const ASTRecordLayout *RL = nullptr) { |
3294 | if (!RL) { |
3295 | if (Derived->isInvalidDecl()) return false; |
3296 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
3297 | } |
3298 | |
3299 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
3300 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
3301 | return true; |
3302 | } |
3303 | |
3304 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
3305 | const CXXRecordDecl *DerivedDecl, |
3306 | const CXXBaseSpecifier *Base) { |
3307 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
3308 | |
3309 | if (!Base->isVirtual()) |
3310 | return HandleLValueDirectBase(Info, E, Obj, Derived: DerivedDecl, Base: BaseDecl); |
3311 | |
3312 | SubobjectDesignator &D = Obj.Designator; |
3313 | if (D.Invalid) |
3314 | return false; |
3315 | |
3316 | // Extract most-derived object and corresponding type. |
3317 | // FIXME: After implementing P2280R4 it became possible to get references |
3318 | // here. We do MostDerivedType->getAsCXXRecordDecl() in several other |
3319 | // locations and if we see crashes in those locations in the future |
3320 | // it may make more sense to move this fix into Lvalue::set. |
3321 | DerivedDecl = D.MostDerivedType.getNonReferenceType()->getAsCXXRecordDecl(); |
3322 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
3323 | return false; |
3324 | |
3325 | // Find the virtual base class. |
3326 | if (DerivedDecl->isInvalidDecl()) return false; |
3327 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
3328 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
3329 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(VBase: BaseDecl); |
3330 | return true; |
3331 | } |
3332 | |
3333 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
3334 | QualType Type, LValue &Result) { |
3335 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
3336 | PathE = E->path_end(); |
3337 | PathI != PathE; ++PathI) { |
3338 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
3339 | *PathI)) |
3340 | return false; |
3341 | Type = (*PathI)->getType(); |
3342 | } |
3343 | return true; |
3344 | } |
3345 | |
3346 | /// Cast an lvalue referring to a derived class to a known base subobject. |
3347 | static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result, |
3348 | const CXXRecordDecl *DerivedRD, |
3349 | const CXXRecordDecl *BaseRD) { |
3350 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
3351 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
3352 | if (!DerivedRD->isDerivedFrom(Base: BaseRD, Paths)) |
3353 | llvm_unreachable("Class must be derived from the passed in base class!"); |
3354 | |
3355 | for (CXXBasePathElement &Elem : Paths.front()) |
3356 | if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Elem.Class, Base: Elem.Base)) |
3357 | return false; |
3358 | return true; |
3359 | } |
3360 | |
3361 | /// Update LVal to refer to the given field, which must be a member of the type |
3362 | /// currently described by LVal. |
3363 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
3364 | const FieldDecl *FD, |
3365 | const ASTRecordLayout *RL = nullptr) { |
3366 | if (!RL) { |
3367 | if (FD->getParent()->isInvalidDecl()) return false; |
3368 | RL = &Info.Ctx.getASTRecordLayout(D: FD->getParent()); |
3369 | } |
3370 | |
3371 | unsigned I = FD->getFieldIndex(); |
3372 | LVal.addDecl(Info, E, FD); |
3373 | LVal.adjustOffset(N: Info.Ctx.toCharUnitsFromBits(BitSize: RL->getFieldOffset(FieldNo: I))); |
3374 | return true; |
3375 | } |
3376 | |
3377 | /// Update LVal to refer to the given indirect field. |
3378 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
3379 | LValue &LVal, |
3380 | const IndirectFieldDecl *IFD) { |
3381 | for (const auto *C : IFD->chain()) |
3382 | if (!HandleLValueMember(Info, E, LVal, FD: cast<FieldDecl>(Val: C))) |
3383 | return false; |
3384 | return true; |
3385 | } |
3386 | |
3387 | enum class SizeOfType { |
3388 | SizeOf, |
3389 | DataSizeOf, |
3390 | }; |
3391 | |
3392 | /// Get the size of the given type in char units. |
3393 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type, |
3394 | CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) { |
3395 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
3396 | // extension. |
3397 | if (Type->isVoidType() || Type->isFunctionType()) { |
3398 | Size = CharUnits::One(); |
3399 | return true; |
3400 | } |
3401 | |
3402 | if (Type->isDependentType()) { |
3403 | Info.FFDiag(Loc); |
3404 | return false; |
3405 | } |
3406 | |
3407 | if (!Type->isConstantSizeType()) { |
3408 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
3409 | // FIXME: Better diagnostic. |
3410 | Info.FFDiag(Loc); |
3411 | return false; |
3412 | } |
3413 | |
3414 | if (SOT == SizeOfType::SizeOf) |
3415 | Size = Info.Ctx.getTypeSizeInChars(T: Type); |
3416 | else |
3417 | Size = Info.Ctx.getTypeInfoDataSizeInChars(T: Type).Width; |
3418 | return true; |
3419 | } |
3420 | |
3421 | /// Update a pointer value to model pointer arithmetic. |
3422 | /// \param Info - Information about the ongoing evaluation. |
3423 | /// \param E - The expression being evaluated, for diagnostic purposes. |
3424 | /// \param LVal - The pointer value to be updated. |
3425 | /// \param EltTy - The pointee type represented by LVal. |
3426 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
3427 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
3428 | LValue &LVal, QualType EltTy, |
3429 | APSInt Adjustment) { |
3430 | CharUnits SizeOfPointee; |
3431 | if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfPointee)) |
3432 | return false; |
3433 | |
3434 | LVal.adjustOffsetAndIndex(Info, E, Index: Adjustment, ElementSize: SizeOfPointee); |
3435 | return true; |
3436 | } |
3437 | |
3438 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
3439 | LValue &LVal, QualType EltTy, |
3440 | int64_t Adjustment) { |
3441 | return HandleLValueArrayAdjustment(Info, E, LVal, EltTy, |
3442 | Adjustment: APSInt::get(X: Adjustment)); |
3443 | } |
3444 | |
3445 | /// Update an lvalue to refer to a component of a complex number. |
3446 | /// \param Info - Information about the ongoing evaluation. |
3447 | /// \param LVal - The lvalue to be updated. |
3448 | /// \param EltTy - The complex number's component type. |
3449 | /// \param Imag - False for the real component, true for the imaginary. |
3450 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
3451 | LValue &LVal, QualType EltTy, |
3452 | bool Imag) { |
3453 | if (Imag) { |
3454 | CharUnits SizeOfComponent; |
3455 | if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfComponent)) |
3456 | return false; |
3457 | LVal.Offset += SizeOfComponent; |
3458 | } |
3459 | LVal.addComplex(Info, E, EltTy, Imag); |
3460 | return true; |
3461 | } |
3462 | |
3463 | static bool HandleLValueVectorElement(EvalInfo &Info, const Expr *E, |
3464 | LValue &LVal, QualType EltTy, |
3465 | uint64_t Size, uint64_t Idx) { |
3466 | if (Idx) { |
3467 | CharUnits SizeOfElement; |
3468 | if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfElement)) |
3469 | return false; |
3470 | LVal.Offset += SizeOfElement * Idx; |
3471 | } |
3472 | LVal.addVectorElement(Info, E, EltTy, Size, Idx); |
3473 | return true; |
3474 | } |
3475 | |
3476 | /// Try to evaluate the initializer for a variable declaration. |
3477 | /// |
3478 | /// \param Info Information about the ongoing evaluation. |
3479 | /// \param E An expression to be used when printing diagnostics. |
3480 | /// \param VD The variable whose initializer should be obtained. |
3481 | /// \param Version The version of the variable within the frame. |
3482 | /// \param Frame The frame in which the variable was created. Must be null |
3483 | /// if this variable is not local to the evaluation. |
3484 | /// \param Result Filled in with a pointer to the value of the variable. |
3485 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
3486 | const VarDecl *VD, CallStackFrame *Frame, |
3487 | unsigned Version, APValue *&Result) { |
3488 | // C++23 [expr.const]p8 If we have a reference type allow unknown references |
3489 | // and pointers. |
3490 | bool AllowConstexprUnknown = |
3491 | Info.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType(); |
3492 | |
3493 | APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version); |
3494 | |
3495 | auto CheckUninitReference = [&](bool IsLocalVariable) { |
3496 | if (!Result->hasValue() && VD->getType()->isReferenceType()) { |
3497 | // C++23 [expr.const]p8 |
3498 | // ... For such an object that is not usable in constant expressions, the |
3499 | // dynamic type of the object is constexpr-unknown. For such a reference |
3500 | // that is not usable in constant expressions, the reference is treated |
3501 | // as binding to an unspecified object of the referenced type whose |
3502 | // lifetime and that of all subobjects includes the entire constant |
3503 | // evaluation and whose dynamic type is constexpr-unknown. |
3504 | // |
3505 | // Variables that are part of the current evaluation are not |
3506 | // constexpr-unknown. |
3507 | if (!AllowConstexprUnknown || IsLocalVariable) { |
3508 | if (!Info.checkingPotentialConstantExpression()) |
3509 | Info.FFDiag(E, diag::note_constexpr_use_uninit_reference); |
3510 | return false; |
3511 | } |
3512 | Result = &Info.CurrentCall->createConstexprUnknownAPValues(Key: VD, Base); |
3513 | } |
3514 | return true; |
3515 | }; |
3516 | |
3517 | // If this is a local variable, dig out its value. |
3518 | if (Frame) { |
3519 | Result = Frame->getTemporary(Key: VD, Version); |
3520 | if (Result) |
3521 | return CheckUninitReference(/*IsLocalVariable=*/true); |
3522 | |
3523 | if (!isa<ParmVarDecl>(Val: VD)) { |
3524 | // Assume variables referenced within a lambda's call operator that were |
3525 | // not declared within the call operator are captures and during checking |
3526 | // of a potential constant expression, assume they are unknown constant |
3527 | // expressions. |
3528 | assert(isLambdaCallOperator(Frame->Callee) && |
3529 | (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && |
3530 | "missing value for local variable"); |
3531 | if (Info.checkingPotentialConstantExpression()) |
3532 | return false; |
3533 | // FIXME: This diagnostic is bogus; we do support captures. Is this code |
3534 | // still reachable at all? |
3535 | Info.FFDiag(E->getBeginLoc(), |
3536 | diag::note_unimplemented_constexpr_lambda_feature_ast) |
3537 | << "captures not currently allowed"; |
3538 | return false; |
3539 | } |
3540 | } |
3541 | |
3542 | // If we're currently evaluating the initializer of this declaration, use that |
3543 | // in-flight value. |
3544 | if (Info.EvaluatingDecl == Base) { |
3545 | Result = Info.EvaluatingDeclValue; |
3546 | return CheckUninitReference(/*IsLocalVariable=*/false); |
3547 | } |
3548 | |
3549 | // P2280R4 struck the restriction that variable of reference type lifetime |
3550 | // should begin within the evaluation of E |
3551 | // Used to be C++20 [expr.const]p5.12.2: |
3552 | // ... its lifetime began within the evaluation of E; |
3553 | if (isa<ParmVarDecl>(Val: VD)) { |
3554 | if (AllowConstexprUnknown) { |
3555 | Result = &Info.CurrentCall->createConstexprUnknownAPValues(Key: VD, Base); |
3556 | return true; |
3557 | } |
3558 | |
3559 | // Assume parameters of a potential constant expression are usable in |
3560 | // constant expressions. |
3561 | if (!Info.checkingPotentialConstantExpression() || |
3562 | !Info.CurrentCall->Callee || |
3563 | !Info.CurrentCall->Callee->Equals(DC: VD->getDeclContext())) { |
3564 | if (Info.getLangOpts().CPlusPlus11) { |
3565 | Info.FFDiag(E, diag::note_constexpr_function_param_value_unknown) |
3566 | << VD; |
3567 | NoteLValueLocation(Info, Base); |
3568 | } else { |
3569 | Info.FFDiag(E); |
3570 | } |
3571 | } |
3572 | return false; |
3573 | } |
3574 | |
3575 | if (E->isValueDependent()) |
3576 | return false; |
3577 | |
3578 | // Dig out the initializer, and use the declaration which it's attached to. |
3579 | // FIXME: We should eventually check whether the variable has a reachable |
3580 | // initializing declaration. |
3581 | const Expr *Init = VD->getAnyInitializer(D&: VD); |
3582 | // P2280R4 struck the restriction that variable of reference type should have |
3583 | // a preceding initialization. |
3584 | // Used to be C++20 [expr.const]p5.12: |
3585 | // ... reference has a preceding initialization and either ... |
3586 | if (!Init && !AllowConstexprUnknown) { |
3587 | // Don't diagnose during potential constant expression checking; an |
3588 | // initializer might be added later. |
3589 | if (!Info.checkingPotentialConstantExpression()) { |
3590 | Info.FFDiag(E, diag::note_constexpr_var_init_unknown, 1) |
3591 | << VD; |
3592 | NoteLValueLocation(Info, Base); |
3593 | } |
3594 | return false; |
3595 | } |
3596 | |
3597 | // P2280R4 struck the initialization requirement for variables of reference |
3598 | // type so we can no longer assume we have an Init. |
3599 | // Used to be C++20 [expr.const]p5.12: |
3600 | // ... reference has a preceding initialization and either ... |
3601 | if (Init && Init->isValueDependent()) { |
3602 | // The DeclRefExpr is not value-dependent, but the variable it refers to |
3603 | // has a value-dependent initializer. This should only happen in |
3604 | // constant-folding cases, where the variable is not actually of a suitable |
3605 | // type for use in a constant expression (otherwise the DeclRefExpr would |
3606 | // have been value-dependent too), so diagnose that. |
3607 | assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx)); |
3608 | if (!Info.checkingPotentialConstantExpression()) { |
3609 | Info.FFDiag(E, Info.getLangOpts().CPlusPlus11 |
3610 | ? diag::note_constexpr_ltor_non_constexpr |
3611 | : diag::note_constexpr_ltor_non_integral, 1) |
3612 | << VD << VD->getType(); |
3613 | NoteLValueLocation(Info, Base); |
3614 | } |
3615 | return false; |
3616 | } |
3617 | |
3618 | // Check that we can fold the initializer. In C++, we will have already done |
3619 | // this in the cases where it matters for conformance. |
3620 | // P2280R4 struck the initialization requirement for variables of reference |
3621 | // type so we can no longer assume we have an Init. |
3622 | // Used to be C++20 [expr.const]p5.12: |
3623 | // ... reference has a preceding initialization and either ... |
3624 | if (Init && !VD->evaluateValue() && !AllowConstexprUnknown) { |
3625 | Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD; |
3626 | NoteLValueLocation(Info, Base); |
3627 | return false; |
3628 | } |
3629 | |
3630 | // Check that the variable is actually usable in constant expressions. For a |
3631 | // const integral variable or a reference, we might have a non-constant |
3632 | // initializer that we can nonetheless evaluate the initializer for. Such |
3633 | // variables are not usable in constant expressions. In C++98, the |
3634 | // initializer also syntactically needs to be an ICE. |
3635 | // |
3636 | // FIXME: We don't diagnose cases that aren't potentially usable in constant |
3637 | // expressions here; doing so would regress diagnostics for things like |
3638 | // reading from a volatile constexpr variable. |
3639 | if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() && |
3640 | VD->mightBeUsableInConstantExpressions(C: Info.Ctx) && |
3641 | !AllowConstexprUnknown) || |
3642 | ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) && |
3643 | !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Context: Info.Ctx))) { |
3644 | if (Init) { |
3645 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD; |
3646 | NoteLValueLocation(Info, Base); |
3647 | } else { |
3648 | Info.CCEDiag(E); |
3649 | } |
3650 | } |
3651 | |
3652 | // Never use the initializer of a weak variable, not even for constant |
3653 | // folding. We can't be sure that this is the definition that will be used. |
3654 | if (VD->isWeak()) { |
3655 | Info.FFDiag(E, diag::note_constexpr_var_init_weak) << VD; |
3656 | NoteLValueLocation(Info, Base); |
3657 | return false; |
3658 | } |
3659 | |
3660 | Result = VD->getEvaluatedValue(); |
3661 | |
3662 | if (!Result) { |
3663 | if (AllowConstexprUnknown) |
3664 | Result = &Info.CurrentCall->createConstexprUnknownAPValues(Key: VD, Base); |
3665 | else |
3666 | return false; |
3667 | } |
3668 | |
3669 | return CheckUninitReference(/*IsLocalVariable=*/false); |
3670 | } |
3671 | |
3672 | /// Get the base index of the given base class within an APValue representing |
3673 | /// the given derived class. |
3674 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
3675 | const CXXRecordDecl *Base) { |
3676 | Base = Base->getCanonicalDecl(); |
3677 | unsigned Index = 0; |
3678 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
3679 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
3680 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
3681 | return Index; |
3682 | } |
3683 | |
3684 | llvm_unreachable("base class missing from derived class's bases list"); |
3685 | } |
3686 | |
3687 | /// Extract the value of a character from a string literal. |
3688 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
3689 | uint64_t Index) { |
3690 | assert(!isa<SourceLocExpr>(Lit) && |
3691 | "SourceLocExpr should have already been converted to a StringLiteral"); |
3692 | |
3693 | // FIXME: Support MakeStringConstant |
3694 | if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Val: Lit)) { |
3695 | std::string Str; |
3696 | Info.Ctx.getObjCEncodingForType(T: ObjCEnc->getEncodedType(), S&: Str); |
3697 | assert(Index <= Str.size() && "Index too large"); |
3698 | return APSInt::getUnsigned(X: Str.c_str()[Index]); |
3699 | } |
3700 | |
3701 | if (auto PE = dyn_cast<PredefinedExpr>(Val: Lit)) |
3702 | Lit = PE->getFunctionName(); |
3703 | const StringLiteral *S = cast<StringLiteral>(Val: Lit); |
3704 | const ConstantArrayType *CAT = |
3705 | Info.Ctx.getAsConstantArrayType(T: S->getType()); |
3706 | assert(CAT && "string literal isn't an array"); |
3707 | QualType CharType = CAT->getElementType(); |
3708 | assert(CharType->isIntegerType() && "unexpected character type"); |
3709 | APSInt Value(Info.Ctx.getTypeSize(T: CharType), |
3710 | CharType->isUnsignedIntegerType()); |
3711 | if (Index < S->getLength()) |
3712 | Value = S->getCodeUnit(i: Index); |
3713 | return Value; |
3714 | } |
3715 | |
3716 | // Expand a string literal into an array of characters. |
3717 | // |
3718 | // FIXME: This is inefficient; we should probably introduce something similar |
3719 | // to the LLVM ConstantDataArray to make this cheaper. |
3720 | static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S, |
3721 | APValue &Result, |
3722 | QualType AllocType = QualType()) { |
3723 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
3724 | T: AllocType.isNull() ? S->getType() : AllocType); |
3725 | assert(CAT && "string literal isn't an array"); |
3726 | QualType CharType = CAT->getElementType(); |
3727 | assert(CharType->isIntegerType() && "unexpected character type"); |
3728 | |
3729 | unsigned Elts = CAT->getZExtSize(); |
3730 | Result = APValue(APValue::UninitArray(), |
3731 | std::min(a: S->getLength(), b: Elts), Elts); |
3732 | APSInt Value(Info.Ctx.getTypeSize(T: CharType), |
3733 | CharType->isUnsignedIntegerType()); |
3734 | if (Result.hasArrayFiller()) |
3735 | Result.getArrayFiller() = APValue(Value); |
3736 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
3737 | Value = S->getCodeUnit(i: I); |
3738 | Result.getArrayInitializedElt(I) = APValue(Value); |
3739 | } |
3740 | } |
3741 | |
3742 | // Expand an array so that it has more than Index filled elements. |
3743 | static void expandArray(APValue &Array, unsigned Index) { |
3744 | unsigned Size = Array.getArraySize(); |
3745 | assert(Index < Size); |
3746 | |
3747 | // Always at least double the number of elements for which we store a value. |
3748 | unsigned OldElts = Array.getArrayInitializedElts(); |
3749 | unsigned NewElts = std::max(a: Index+1, b: OldElts * 2); |
3750 | NewElts = std::min(a: Size, b: std::max(a: NewElts, b: 8u)); |
3751 | |
3752 | // Copy the data across. |
3753 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
3754 | for (unsigned I = 0; I != OldElts; ++I) |
3755 | NewValue.getArrayInitializedElt(I).swap(RHS&: Array.getArrayInitializedElt(I)); |
3756 | for (unsigned I = OldElts; I != NewElts; ++I) |
3757 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
3758 | if (NewValue.hasArrayFiller()) |
3759 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
3760 | Array.swap(RHS&: NewValue); |
3761 | } |
3762 | |
3763 | /// Determine whether a type would actually be read by an lvalue-to-rvalue |
3764 | /// conversion. If it's of class type, we may assume that the copy operation |
3765 | /// is trivial. Note that this is never true for a union type with fields |
3766 | /// (because the copy always "reads" the active member) and always true for |
3767 | /// a non-class type. |
3768 | static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD); |
3769 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
3770 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3771 | return !RD || isReadByLvalueToRvalueConversion(RD); |
3772 | } |
3773 | static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) { |
3774 | // FIXME: A trivial copy of a union copies the object representation, even if |
3775 | // the union is empty. |
3776 | if (RD->isUnion()) |
3777 | return !RD->field_empty(); |
3778 | if (RD->isEmpty()) |
3779 | return false; |
3780 | |
3781 | for (auto *Field : RD->fields()) |
3782 | if (!Field->isUnnamedBitField() && |
3783 | isReadByLvalueToRvalueConversion(Field->getType())) |
3784 | return true; |
3785 | |
3786 | for (auto &BaseSpec : RD->bases()) |
3787 | if (isReadByLvalueToRvalueConversion(T: BaseSpec.getType())) |
3788 | return true; |
3789 | |
3790 | return false; |
3791 | } |
3792 | |
3793 | /// Diagnose an attempt to read from any unreadable field within the specified |
3794 | /// type, which might be a class type. |
3795 | static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK, |
3796 | QualType T) { |
3797 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3798 | if (!RD) |
3799 | return false; |
3800 | |
3801 | if (!RD->hasMutableFields()) |
3802 | return false; |
3803 | |
3804 | for (auto *Field : RD->fields()) { |
3805 | // If we're actually going to read this field in some way, then it can't |
3806 | // be mutable. If we're in a union, then assigning to a mutable field |
3807 | // (even an empty one) can change the active member, so that's not OK. |
3808 | // FIXME: Add core issue number for the union case. |
3809 | if (Field->isMutable() && |
3810 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
3811 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) << AK << Field; |
3812 | Info.Note(Field->getLocation(), diag::note_declared_at); |
3813 | return true; |
3814 | } |
3815 | |
3816 | if (diagnoseMutableFields(Info, E, AK, Field->getType())) |
3817 | return true; |
3818 | } |
3819 | |
3820 | for (auto &BaseSpec : RD->bases()) |
3821 | if (diagnoseMutableFields(Info, E, AK, T: BaseSpec.getType())) |
3822 | return true; |
3823 | |
3824 | // All mutable fields were empty, and thus not actually read. |
3825 | return false; |
3826 | } |
3827 | |
3828 | static bool lifetimeStartedInEvaluation(EvalInfo &Info, |
3829 | APValue::LValueBase Base, |
3830 | bool MutableSubobject = false) { |
3831 | // A temporary or transient heap allocation we created. |
3832 | if (Base.getCallIndex() || Base.is<DynamicAllocLValue>()) |
3833 | return true; |
3834 | |
3835 | switch (Info.IsEvaluatingDecl) { |
3836 | case EvalInfo::EvaluatingDeclKind::None: |
3837 | return false; |
3838 | |
3839 | case EvalInfo::EvaluatingDeclKind::Ctor: |
3840 | // The variable whose initializer we're evaluating. |
3841 | if (Info.EvaluatingDecl == Base) |
3842 | return true; |
3843 | |
3844 | // A temporary lifetime-extended by the variable whose initializer we're |
3845 | // evaluating. |
3846 | if (auto *BaseE = Base.dyn_cast<const Expr *>()) |
3847 | if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(Val: BaseE)) |
3848 | return Info.EvaluatingDecl == BaseMTE->getExtendingDecl(); |
3849 | return false; |
3850 | |
3851 | case EvalInfo::EvaluatingDeclKind::Dtor: |
3852 | // C++2a [expr.const]p6: |
3853 | // [during constant destruction] the lifetime of a and its non-mutable |
3854 | // subobjects (but not its mutable subobjects) [are] considered to start |
3855 | // within e. |
3856 | if (MutableSubobject || Base != Info.EvaluatingDecl) |
3857 | return false; |
3858 | // FIXME: We can meaningfully extend this to cover non-const objects, but |
3859 | // we will need special handling: we should be able to access only |
3860 | // subobjects of such objects that are themselves declared const. |
3861 | QualType T = getType(B: Base); |
3862 | return T.isConstQualified() || T->isReferenceType(); |
3863 | } |
3864 | |
3865 | llvm_unreachable("unknown evaluating decl kind"); |
3866 | } |
3867 | |
3868 | static bool CheckArraySize(EvalInfo &Info, const ConstantArrayType *CAT, |
3869 | SourceLocation CallLoc = {}) { |
3870 | return Info.CheckArraySize( |
3871 | Loc: CAT->getSizeExpr() ? CAT->getSizeExpr()->getBeginLoc() : CallLoc, |
3872 | BitWidth: CAT->getNumAddressingBits(Context: Info.Ctx), ElemCount: CAT->getZExtSize(), |
3873 | /*Diag=*/true); |
3874 | } |
3875 | |
3876 | namespace { |
3877 | /// A handle to a complete object (an object that is not a subobject of |
3878 | /// another object). |
3879 | struct CompleteObject { |
3880 | /// The identity of the object. |
3881 | APValue::LValueBase Base; |
3882 | /// The value of the complete object. |
3883 | APValue *Value; |
3884 | /// The type of the complete object. |
3885 | QualType Type; |
3886 | |
3887 | CompleteObject() : Value(nullptr) {} |
3888 | CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type) |
3889 | : Base(Base), Value(Value), Type(Type) {} |
3890 | |
3891 | bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const { |
3892 | // If this isn't a "real" access (eg, if it's just accessing the type |
3893 | // info), allow it. We assume the type doesn't change dynamically for |
3894 | // subobjects of constexpr objects (even though we'd hit UB here if it |
3895 | // did). FIXME: Is this right? |
3896 | if (!isAnyAccess(AK)) |
3897 | return true; |
3898 | |
3899 | // In C++14 onwards, it is permitted to read a mutable member whose |
3900 | // lifetime began within the evaluation. |
3901 | // FIXME: Should we also allow this in C++11? |
3902 | if (!Info.getLangOpts().CPlusPlus14 && |
3903 | AK != AccessKinds::AK_IsWithinLifetime) |
3904 | return false; |
3905 | return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true); |
3906 | } |
3907 | |
3908 | explicit operator bool() const { return !Type.isNull(); } |
3909 | }; |
3910 | } // end anonymous namespace |
3911 | |
3912 | static QualType getSubobjectType(QualType ObjType, QualType SubobjType, |
3913 | bool IsMutable = false) { |
3914 | // C++ [basic.type.qualifier]p1: |
3915 | // - A const object is an object of type const T or a non-mutable subobject |
3916 | // of a const object. |
3917 | if (ObjType.isConstQualified() && !IsMutable) |
3918 | SubobjType.addConst(); |
3919 | // - A volatile object is an object of type const T or a subobject of a |
3920 | // volatile object. |
3921 | if (ObjType.isVolatileQualified()) |
3922 | SubobjType.addVolatile(); |
3923 | return SubobjType; |
3924 | } |
3925 | |
3926 | /// Find the designated sub-object of an rvalue. |
3927 | template <typename SubobjectHandler> |
3928 | static typename SubobjectHandler::result_type |
3929 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
3930 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
3931 | if (Sub.Invalid) |
3932 | // A diagnostic will have already been produced. |
3933 | return handler.failed(); |
3934 | if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) { |
3935 | if (Info.getLangOpts().CPlusPlus11) |
3936 | Info.FFDiag(E, Sub.isOnePastTheEnd() |
3937 | ? diag::note_constexpr_access_past_end |
3938 | : diag::note_constexpr_access_unsized_array) |
3939 | << handler.AccessKind; |
3940 | else |
3941 | Info.FFDiag(E); |
3942 | return handler.failed(); |
3943 | } |
3944 | |
3945 | APValue *O = Obj.Value; |
3946 | QualType ObjType = Obj.Type; |
3947 | const FieldDecl *LastField = nullptr; |
3948 | const FieldDecl *VolatileField = nullptr; |
3949 | |
3950 | // C++23 [expr.const]p8 If we have an unknown reference or pointers and it |
3951 | // does not have a value then bail out. |
3952 | if (O->allowConstexprUnknown() && !O->hasValue()) |
3953 | return false; |
3954 | |
3955 | // Walk the designator's path to find the subobject. |
3956 | for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) { |
3957 | // Reading an indeterminate value is undefined, but assigning over one is OK. |
3958 | if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) || |
3959 | (O->isIndeterminate() && |
3960 | !isValidIndeterminateAccess(handler.AccessKind))) { |
3961 | // Object has ended lifetime. |
3962 | // If I is non-zero, some subobject (member or array element) of a |
3963 | // complete object has ended its lifetime, so this is valid for |
3964 | // IsWithinLifetime, resulting in false. |
3965 | if (I != 0 && handler.AccessKind == AK_IsWithinLifetime) |
3966 | return false; |
3967 | if (!Info.checkingPotentialConstantExpression()) |
3968 | Info.FFDiag(E, diag::note_constexpr_access_uninit) |
3969 | << handler.AccessKind << O->isIndeterminate() |
3970 | << E->getSourceRange(); |
3971 | return handler.failed(); |
3972 | } |
3973 | |
3974 | // C++ [class.ctor]p5, C++ [class.dtor]p5: |
3975 | // const and volatile semantics are not applied on an object under |
3976 | // {con,de}struction. |
3977 | if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) && |
3978 | ObjType->isRecordType() && |
3979 | Info.isEvaluatingCtorDtor( |
3980 | Base: Obj.Base, |
3981 | Path: llvm::ArrayRef(Sub.Entries.begin(), Sub.Entries.begin() + I)) != |
3982 | ConstructionPhase::None) { |
3983 | ObjType = Info.Ctx.getCanonicalType(T: ObjType); |
3984 | ObjType.removeLocalConst(); |
3985 | ObjType.removeLocalVolatile(); |
3986 | } |
3987 | |
3988 | // If this is our last pass, check that the final object type is OK. |
3989 | if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) { |
3990 | // Accesses to volatile objects are prohibited. |
3991 | if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) { |
3992 | if (Info.getLangOpts().CPlusPlus) { |
3993 | int DiagKind; |
3994 | SourceLocation Loc; |
3995 | const NamedDecl *Decl = nullptr; |
3996 | if (VolatileField) { |
3997 | DiagKind = 2; |
3998 | Loc = VolatileField->getLocation(); |
3999 | Decl = VolatileField; |
4000 | } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) { |
4001 | DiagKind = 1; |
4002 | Loc = VD->getLocation(); |
4003 | Decl = VD; |
4004 | } else { |
4005 | DiagKind = 0; |
4006 | if (auto *E = Obj.Base.dyn_cast<const Expr *>()) |
4007 | Loc = E->getExprLoc(); |
4008 | } |
4009 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
4010 | << handler.AccessKind << DiagKind << Decl; |
4011 | Info.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind; |
4012 | } else { |
4013 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
4014 | } |
4015 | return handler.failed(); |
4016 | } |
4017 | |
4018 | // If we are reading an object of class type, there may still be more |
4019 | // things we need to check: if there are any mutable subobjects, we |
4020 | // cannot perform this read. (This only happens when performing a trivial |
4021 | // copy or assignment.) |
4022 | if (ObjType->isRecordType() && |
4023 | !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind) && |
4024 | diagnoseMutableFields(Info, E, handler.AccessKind, ObjType)) |
4025 | return handler.failed(); |
4026 | } |
4027 | |
4028 | if (I == N) { |
4029 | if (!handler.found(*O, ObjType)) |
4030 | return false; |
4031 | |
4032 | // If we modified a bit-field, truncate it to the right width. |
4033 | if (isModification(handler.AccessKind) && |
4034 | LastField && LastField->isBitField() && |
4035 | !truncateBitfieldValue(Info, E, Value&: *O, FD: LastField)) |
4036 | return false; |
4037 | |
4038 | return true; |
4039 | } |
4040 | |
4041 | LastField = nullptr; |
4042 | if (ObjType->isArrayType()) { |
4043 | // Next subobject is an array element. |
4044 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: ObjType); |
4045 | assert(CAT && "vla in literal type?"); |
4046 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
4047 | if (CAT->getSize().ule(RHS: Index)) { |
4048 | // Note, it should not be possible to form a pointer with a valid |
4049 | // designator which points more than one past the end of the array. |
4050 | if (Info.getLangOpts().CPlusPlus11) |
4051 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
4052 | << handler.AccessKind; |
4053 | else |
4054 | Info.FFDiag(E); |
4055 | return handler.failed(); |
4056 | } |
4057 | |
4058 | ObjType = CAT->getElementType(); |
4059 | |
4060 | if (O->getArrayInitializedElts() > Index) |
4061 | O = &O->getArrayInitializedElt(I: Index); |
4062 | else if (!isRead(handler.AccessKind)) { |
4063 | if (!CheckArraySize(Info, CAT, CallLoc: E->getExprLoc())) |
4064 | return handler.failed(); |
4065 | |
4066 | expandArray(Array&: *O, Index); |
4067 | O = &O->getArrayInitializedElt(I: Index); |
4068 | } else |
4069 | O = &O->getArrayFiller(); |
4070 | } else if (ObjType->isAnyComplexType()) { |
4071 | // Next subobject is a complex number. |
4072 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
4073 | if (Index > 1) { |
4074 | if (Info.getLangOpts().CPlusPlus11) |
4075 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
4076 | << handler.AccessKind; |
4077 | else |
4078 | Info.FFDiag(E); |
4079 | return handler.failed(); |
4080 | } |
4081 | |
4082 | ObjType = getSubobjectType( |
4083 | ObjType, SubobjType: ObjType->castAs<ComplexType>()->getElementType()); |
4084 | |
4085 | assert(I == N - 1 && "extracting subobject of scalar?"); |
4086 | if (O->isComplexInt()) { |
4087 | return handler.found(Index ? O->getComplexIntImag() |
4088 | : O->getComplexIntReal(), ObjType); |
4089 | } else { |
4090 | assert(O->isComplexFloat()); |
4091 | return handler.found(Index ? O->getComplexFloatImag() |
4092 | : O->getComplexFloatReal(), ObjType); |
4093 | } |
4094 | } else if (const auto *VT = ObjType->getAs<VectorType>()) { |
4095 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
4096 | unsigned NumElements = VT->getNumElements(); |
4097 | if (Index == NumElements) { |
4098 | if (Info.getLangOpts().CPlusPlus11) |
4099 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
4100 | << handler.AccessKind; |
4101 | else |
4102 | Info.FFDiag(E); |
4103 | return handler.failed(); |
4104 | } |
4105 | |
4106 | if (Index > NumElements) { |
4107 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
4108 | << Index << /*array*/ 0 << NumElements; |
4109 | return handler.failed(); |
4110 | } |
4111 | |
4112 | ObjType = VT->getElementType(); |
4113 | assert(I == N - 1 && "extracting subobject of scalar?"); |
4114 | return handler.found(O->getVectorElt(I: Index), ObjType); |
4115 | } else if (const FieldDecl *Field = getAsField(E: Sub.Entries[I])) { |
4116 | if (Field->isMutable() && |
4117 | !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind)) { |
4118 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) |
4119 | << handler.AccessKind << Field; |
4120 | Info.Note(Field->getLocation(), diag::note_declared_at); |
4121 | return handler.failed(); |
4122 | } |
4123 | |
4124 | // Next subobject is a class, struct or union field. |
4125 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
4126 | if (RD->isUnion()) { |
4127 | const FieldDecl *UnionField = O->getUnionField(); |
4128 | if (!UnionField || |
4129 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
4130 | if (I == N - 1 && handler.AccessKind == AK_Construct) { |
4131 | // Placement new onto an inactive union member makes it active. |
4132 | O->setUnion(Field, Value: APValue()); |
4133 | } else { |
4134 | // Pointer to/into inactive union member: Not within lifetime |
4135 | if (handler.AccessKind == AK_IsWithinLifetime) |
4136 | return false; |
4137 | // FIXME: If O->getUnionValue() is absent, report that there's no |
4138 | // active union member rather than reporting the prior active union |
4139 | // member. We'll need to fix nullptr_t to not use APValue() as its |
4140 | // representation first. |
4141 | Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member) |
4142 | << handler.AccessKind << Field << !UnionField << UnionField; |
4143 | return handler.failed(); |
4144 | } |
4145 | } |
4146 | O = &O->getUnionValue(); |
4147 | } else |
4148 | O = &O->getStructField(i: Field->getFieldIndex()); |
4149 | |
4150 | ObjType = getSubobjectType(ObjType, Field->getType(), Field->isMutable()); |
4151 | LastField = Field; |
4152 | if (Field->getType().isVolatileQualified()) |
4153 | VolatileField = Field; |
4154 | } else { |
4155 | // Next subobject is a base class. |
4156 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
4157 | const CXXRecordDecl *Base = getAsBaseClass(E: Sub.Entries[I]); |
4158 | O = &O->getStructBase(i: getBaseIndex(Derived, Base)); |
4159 | |
4160 | ObjType = getSubobjectType(ObjType, SubobjType: Info.Ctx.getRecordType(Base)); |
4161 | } |
4162 | } |
4163 | } |
4164 | |
4165 | namespace { |
4166 | struct ExtractSubobjectHandler { |
4167 | EvalInfo &Info; |
4168 | const Expr *E; |
4169 | APValue &Result; |
4170 | const AccessKinds AccessKind; |
4171 | |
4172 | typedef bool result_type; |
4173 | bool failed() { return false; } |
4174 | bool found(APValue &Subobj, QualType SubobjType) { |
4175 | Result = Subobj; |
4176 | if (AccessKind == AK_ReadObjectRepresentation) |
4177 | return true; |
4178 | return CheckFullyInitialized(Info, DiagLoc: E->getExprLoc(), Type: SubobjType, Value: Result); |
4179 | } |
4180 | bool found(APSInt &Value, QualType SubobjType) { |
4181 | Result = APValue(Value); |
4182 | return true; |
4183 | } |
4184 | bool found(APFloat &Value, QualType SubobjType) { |
4185 | Result = APValue(Value); |
4186 | return true; |
4187 | } |
4188 | }; |
4189 | } // end anonymous namespace |
4190 | |
4191 | /// Extract the designated sub-object of an rvalue. |
4192 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
4193 | const CompleteObject &Obj, |
4194 | const SubobjectDesignator &Sub, APValue &Result, |
4195 | AccessKinds AK = AK_Read) { |
4196 | assert(AK == AK_Read || AK == AK_ReadObjectRepresentation); |
4197 | ExtractSubobjectHandler Handler = {.Info: Info, .E: E, .Result: Result, .AccessKind: AK}; |
4198 | return findSubobject(Info, E, Obj, Sub, handler&: Handler); |
4199 | } |
4200 | |
4201 | namespace { |
4202 | struct ModifySubobjectHandler { |
4203 | EvalInfo &Info; |
4204 | APValue &NewVal; |
4205 | const Expr *E; |
4206 | |
4207 | typedef bool result_type; |
4208 | static const AccessKinds AccessKind = AK_Assign; |
4209 | |
4210 | bool checkConst(QualType QT) { |
4211 | // Assigning to a const object has undefined behavior. |
4212 | if (QT.isConstQualified()) { |
4213 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4214 | return false; |
4215 | } |
4216 | return true; |
4217 | } |
4218 | |
4219 | bool failed() { return false; } |
4220 | bool found(APValue &Subobj, QualType SubobjType) { |
4221 | if (!checkConst(QT: SubobjType)) |
4222 | return false; |
4223 | // We've been given ownership of NewVal, so just swap it in. |
4224 | Subobj.swap(RHS&: NewVal); |
4225 | return true; |
4226 | } |
4227 | bool found(APSInt &Value, QualType SubobjType) { |
4228 | if (!checkConst(QT: SubobjType)) |
4229 | return false; |
4230 | if (!NewVal.isInt()) { |
4231 | // Maybe trying to write a cast pointer value into a complex? |
4232 | Info.FFDiag(E); |
4233 | return false; |
4234 | } |
4235 | Value = NewVal.getInt(); |
4236 | return true; |
4237 | } |
4238 | bool found(APFloat &Value, QualType SubobjType) { |
4239 | if (!checkConst(QT: SubobjType)) |
4240 | return false; |
4241 | Value = NewVal.getFloat(); |
4242 | return true; |
4243 | } |
4244 | }; |
4245 | } // end anonymous namespace |
4246 | |
4247 | const AccessKinds ModifySubobjectHandler::AccessKind; |
4248 | |
4249 | /// Update the designated sub-object of an rvalue to the given value. |
4250 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
4251 | const CompleteObject &Obj, |
4252 | const SubobjectDesignator &Sub, |
4253 | APValue &NewVal) { |
4254 | ModifySubobjectHandler Handler = { .Info: Info, .NewVal: NewVal, .E: E }; |
4255 | return findSubobject(Info, E, Obj, Sub, handler&: Handler); |
4256 | } |
4257 | |
4258 | /// Find the position where two subobject designators diverge, or equivalently |
4259 | /// the length of the common initial subsequence. |
4260 | static unsigned FindDesignatorMismatch(QualType ObjType, |
4261 | const SubobjectDesignator &A, |
4262 | const SubobjectDesignator &B, |
4263 | bool &WasArrayIndex) { |
4264 | unsigned I = 0, N = std::min(a: A.Entries.size(), b: B.Entries.size()); |
4265 | for (/**/; I != N; ++I) { |
4266 | if (!ObjType.isNull() && |
4267 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
4268 | // Next subobject is an array element. |
4269 | if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) { |
4270 | WasArrayIndex = true; |
4271 | return I; |
4272 | } |
4273 | if (ObjType->isAnyComplexType()) |
4274 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
4275 | else |
4276 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
4277 | } else { |
4278 | if (A.Entries[I].getAsBaseOrMember() != |
4279 | B.Entries[I].getAsBaseOrMember()) { |
4280 | WasArrayIndex = false; |
4281 | return I; |
4282 | } |
4283 | if (const FieldDecl *FD = getAsField(E: A.Entries[I])) |
4284 | // Next subobject is a field. |
4285 | ObjType = FD->getType(); |
4286 | else |
4287 | // Next subobject is a base class. |
4288 | ObjType = QualType(); |
4289 | } |
4290 | } |
4291 | WasArrayIndex = false; |
4292 | return I; |
4293 | } |
4294 | |
4295 | /// Determine whether the given subobject designators refer to elements of the |
4296 | /// same array object. |
4297 | static bool AreElementsOfSameArray(QualType ObjType, |
4298 | const SubobjectDesignator &A, |
4299 | const SubobjectDesignator &B) { |
4300 | if (A.Entries.size() != B.Entries.size()) |
4301 | return false; |
4302 | |
4303 | bool IsArray = A.MostDerivedIsArrayElement; |
4304 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
4305 | // A is a subobject of the array element. |
4306 | return false; |
4307 | |
4308 | // If A (and B) designates an array element, the last entry will be the array |
4309 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
4310 | // of length 1' case, and the entire path must match. |
4311 | bool WasArrayIndex; |
4312 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
4313 | return CommonLength >= A.Entries.size() - IsArray; |
4314 | } |
4315 | |
4316 | /// Find the complete object to which an LValue refers. |
4317 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
4318 | AccessKinds AK, const LValue &LVal, |
4319 | QualType LValType) { |
4320 | if (LVal.InvalidBase) { |
4321 | Info.FFDiag(E); |
4322 | return CompleteObject(); |
4323 | } |
4324 | |
4325 | if (!LVal.Base) { |
4326 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
4327 | return CompleteObject(); |
4328 | } |
4329 | |
4330 | CallStackFrame *Frame = nullptr; |
4331 | unsigned Depth = 0; |
4332 | if (LVal.getLValueCallIndex()) { |
4333 | std::tie(args&: Frame, args&: Depth) = |
4334 | Info.getCallFrameAndDepth(CallIndex: LVal.getLValueCallIndex()); |
4335 | if (!Frame) { |
4336 | Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1) |
4337 | << AK << LVal.Base.is<const ValueDecl*>(); |
4338 | NoteLValueLocation(Info, Base: LVal.Base); |
4339 | return CompleteObject(); |
4340 | } |
4341 | } |
4342 | |
4343 | bool IsAccess = isAnyAccess(AK); |
4344 | |
4345 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
4346 | // is not a constant expression (even if the object is non-volatile). We also |
4347 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
4348 | // semantics. |
4349 | if (isFormalAccess(AK) && LValType.isVolatileQualified()) { |
4350 | if (Info.getLangOpts().CPlusPlus) |
4351 | Info.FFDiag(E, diag::note_constexpr_access_volatile_type) |
4352 | << AK << LValType; |
4353 | else |
4354 | Info.FFDiag(E); |
4355 | return CompleteObject(); |
4356 | } |
4357 | |
4358 | // Compute value storage location and type of base object. |
4359 | APValue *BaseVal = nullptr; |
4360 | QualType BaseType = getType(B: LVal.Base); |
4361 | |
4362 | if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl && |
4363 | lifetimeStartedInEvaluation(Info, Base: LVal.Base)) { |
4364 | // This is the object whose initializer we're evaluating, so its lifetime |
4365 | // started in the current evaluation. |
4366 | BaseVal = Info.EvaluatingDeclValue; |
4367 | } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) { |
4368 | // Allow reading from a GUID declaration. |
4369 | if (auto *GD = dyn_cast<MSGuidDecl>(Val: D)) { |
4370 | if (isModification(AK)) { |
4371 | // All the remaining cases do not permit modification of the object. |
4372 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4373 | return CompleteObject(); |
4374 | } |
4375 | APValue &V = GD->getAsAPValue(); |
4376 | if (V.isAbsent()) { |
4377 | Info.FFDiag(E, diag::note_constexpr_unsupported_layout) |
4378 | << GD->getType(); |
4379 | return CompleteObject(); |
4380 | } |
4381 | return CompleteObject(LVal.Base, &V, GD->getType()); |
4382 | } |
4383 | |
4384 | // Allow reading the APValue from an UnnamedGlobalConstantDecl. |
4385 | if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(Val: D)) { |
4386 | if (isModification(AK)) { |
4387 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4388 | return CompleteObject(); |
4389 | } |
4390 | return CompleteObject(LVal.Base, const_cast<APValue *>(&GCD->getValue()), |
4391 | GCD->getType()); |
4392 | } |
4393 | |
4394 | // Allow reading from template parameter objects. |
4395 | if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: D)) { |
4396 | if (isModification(AK)) { |
4397 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4398 | return CompleteObject(); |
4399 | } |
4400 | return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()), |
4401 | TPO->getType()); |
4402 | } |
4403 | |
4404 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
4405 | // In C++11, constexpr, non-volatile variables initialized with constant |
4406 | // expressions are constant expressions too. Inside constexpr functions, |
4407 | // parameters are constant expressions even if they're non-const. |
4408 | // In C++1y, objects local to a constant expression (those with a Frame) are |
4409 | // both readable and writable inside constant expressions. |
4410 | // In C, such things can also be folded, although they are not ICEs. |
4411 | const VarDecl *VD = dyn_cast<VarDecl>(Val: D); |
4412 | if (VD) { |
4413 | if (const VarDecl *VDef = VD->getDefinition(C&: Info.Ctx)) |
4414 | VD = VDef; |
4415 | } |
4416 | if (!VD || VD->isInvalidDecl()) { |
4417 | Info.FFDiag(E); |
4418 | return CompleteObject(); |
4419 | } |
4420 | |
4421 | bool IsConstant = BaseType.isConstant(Ctx: Info.Ctx); |
4422 | bool ConstexprVar = false; |
4423 | if (const auto *VD = dyn_cast_if_present<VarDecl>( |
4424 | Val: Info.EvaluatingDecl.dyn_cast<const ValueDecl *>())) |
4425 | ConstexprVar = VD->isConstexpr(); |
4426 | |
4427 | // Unless we're looking at a local variable or argument in a constexpr call, |
4428 | // the variable we're reading must be const. |
4429 | if (!Frame) { |
4430 | if (IsAccess && isa<ParmVarDecl>(Val: VD)) { |
4431 | // Access of a parameter that's not associated with a frame isn't going |
4432 | // to work out, but we can leave it to evaluateVarDeclInit to provide a |
4433 | // suitable diagnostic. |
4434 | } else if (Info.getLangOpts().CPlusPlus14 && |
4435 | lifetimeStartedInEvaluation(Info, Base: LVal.Base)) { |
4436 | // OK, we can read and modify an object if we're in the process of |
4437 | // evaluating its initializer, because its lifetime began in this |
4438 | // evaluation. |
4439 | } else if (isModification(AK)) { |
4440 | // All the remaining cases do not permit modification of the object. |
4441 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4442 | return CompleteObject(); |
4443 | } else if (VD->isConstexpr()) { |
4444 | // OK, we can read this variable. |
4445 | } else if (Info.getLangOpts().C23 && ConstexprVar) { |
4446 | Info.FFDiag(E); |
4447 | return CompleteObject(); |
4448 | } else if (BaseType->isIntegralOrEnumerationType()) { |
4449 | if (!IsConstant) { |
4450 | if (!IsAccess) |
4451 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4452 | if (Info.getLangOpts().CPlusPlus) { |
4453 | Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
4454 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4455 | } else { |
4456 | Info.FFDiag(E); |
4457 | } |
4458 | return CompleteObject(); |
4459 | } |
4460 | } else if (!IsAccess) { |
4461 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4462 | } else if (IsConstant && Info.checkingPotentialConstantExpression() && |
4463 | BaseType->isLiteralType(Ctx: Info.Ctx) && !VD->hasDefinition()) { |
4464 | // This variable might end up being constexpr. Don't diagnose it yet. |
4465 | } else if (IsConstant) { |
4466 | // Keep evaluating to see what we can do. In particular, we support |
4467 | // folding of const floating-point types, in order to make static const |
4468 | // data members of such types (supported as an extension) more useful. |
4469 | if (Info.getLangOpts().CPlusPlus) { |
4470 | Info.CCEDiag(E, Info.getLangOpts().CPlusPlus11 |
4471 | ? diag::note_constexpr_ltor_non_constexpr |
4472 | : diag::note_constexpr_ltor_non_integral, 1) |
4473 | << VD << BaseType; |
4474 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4475 | } else { |
4476 | Info.CCEDiag(E); |
4477 | } |
4478 | } else { |
4479 | // Never allow reading a non-const value. |
4480 | if (Info.getLangOpts().CPlusPlus) { |
4481 | Info.FFDiag(E, Info.getLangOpts().CPlusPlus11 |
4482 | ? diag::note_constexpr_ltor_non_constexpr |
4483 | : diag::note_constexpr_ltor_non_integral, 1) |
4484 | << VD << BaseType; |
4485 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4486 | } else { |
4487 | Info.FFDiag(E); |
4488 | } |
4489 | return CompleteObject(); |
4490 | } |
4491 | } |
4492 | |
4493 | if (!evaluateVarDeclInit(Info, E, VD, Frame, Version: LVal.getLValueVersion(), Result&: BaseVal)) |
4494 | return CompleteObject(); |
4495 | } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) { |
4496 | std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA); |
4497 | if (!Alloc) { |
4498 | Info.FFDiag(E, diag::note_constexpr_access_deleted_object) << AK; |
4499 | return CompleteObject(); |
4500 | } |
4501 | return CompleteObject(LVal.Base, &(*Alloc)->Value, |
4502 | LVal.Base.getDynamicAllocType()); |
4503 | } else { |
4504 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
4505 | |
4506 | if (!Frame) { |
4507 | if (const MaterializeTemporaryExpr *MTE = |
4508 | dyn_cast_or_null<MaterializeTemporaryExpr>(Val: Base)) { |
4509 | assert(MTE->getStorageDuration() == SD_Static && |
4510 | "should have a frame for a non-global materialized temporary"); |
4511 | |
4512 | // C++20 [expr.const]p4: [DR2126] |
4513 | // An object or reference is usable in constant expressions if it is |
4514 | // - a temporary object of non-volatile const-qualified literal type |
4515 | // whose lifetime is extended to that of a variable that is usable |
4516 | // in constant expressions |
4517 | // |
4518 | // C++20 [expr.const]p5: |
4519 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
4520 | // - a non-volatile glvalue that refers to an object that is usable |
4521 | // in constant expressions, or |
4522 | // - a non-volatile glvalue of literal type that refers to a |
4523 | // non-volatile object whose lifetime began within the evaluation |
4524 | // of E; |
4525 | // |
4526 | // C++11 misses the 'began within the evaluation of e' check and |
4527 | // instead allows all temporaries, including things like: |
4528 | // int &&r = 1; |
4529 | // int x = ++r; |
4530 | // constexpr int k = r; |
4531 | // Therefore we use the C++14-onwards rules in C++11 too. |
4532 | // |
4533 | // Note that temporaries whose lifetimes began while evaluating a |
4534 | // variable's constructor are not usable while evaluating the |
4535 | // corresponding destructor, not even if they're of const-qualified |
4536 | // types. |
4537 | if (!MTE->isUsableInConstantExpressions(Context: Info.Ctx) && |
4538 | !lifetimeStartedInEvaluation(Info, Base: LVal.Base)) { |
4539 | if (!IsAccess) |
4540 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4541 | Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
4542 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
4543 | return CompleteObject(); |
4544 | } |
4545 | |
4546 | BaseVal = MTE->getOrCreateValue(MayCreate: false); |
4547 | assert(BaseVal && "got reference to unevaluated temporary"); |
4548 | } else { |
4549 | if (!IsAccess) |
4550 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4551 | APValue Val; |
4552 | LVal.moveInto(V&: Val); |
4553 | Info.FFDiag(E, diag::note_constexpr_access_unreadable_object) |
4554 | << AK |
4555 | << Val.getAsString(Info.Ctx, |
4556 | Info.Ctx.getLValueReferenceType(LValType)); |
4557 | NoteLValueLocation(Info, Base: LVal.Base); |
4558 | return CompleteObject(); |
4559 | } |
4560 | } else { |
4561 | BaseVal = Frame->getTemporary(Key: Base, Version: LVal.Base.getVersion()); |
4562 | assert(BaseVal && "missing value for temporary"); |
4563 | } |
4564 | } |
4565 | |
4566 | // In C++14, we can't safely access any mutable state when we might be |
4567 | // evaluating after an unmodeled side effect. Parameters are modeled as state |
4568 | // in the caller, but aren't visible once the call returns, so they can be |
4569 | // modified in a speculatively-evaluated call. |
4570 | // |
4571 | // FIXME: Not all local state is mutable. Allow local constant subobjects |
4572 | // to be read here (but take care with 'mutable' fields). |
4573 | unsigned VisibleDepth = Depth; |
4574 | if (llvm::isa_and_nonnull<ParmVarDecl>( |
4575 | Val: LVal.Base.dyn_cast<const ValueDecl *>())) |
4576 | ++VisibleDepth; |
4577 | if ((Frame && Info.getLangOpts().CPlusPlus14 && |
4578 | Info.EvalStatus.HasSideEffects) || |
4579 | (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth)) |
4580 | return CompleteObject(); |
4581 | |
4582 | return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType); |
4583 | } |
4584 | |
4585 | /// Perform an lvalue-to-rvalue conversion on the given glvalue. This |
4586 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
4587 | /// glvalue referred to by an entity of reference type. |
4588 | /// |
4589 | /// \param Info - Information about the ongoing evaluation. |
4590 | /// \param Conv - The expression for which we are performing the conversion. |
4591 | /// Used for diagnostics. |
4592 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
4593 | /// case of a non-class type). |
4594 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
4595 | /// \param RVal - The produced value will be placed here. |
4596 | /// \param WantObjectRepresentation - If true, we're looking for the object |
4597 | /// representation rather than the value, and in particular, |
4598 | /// there is no requirement that the result be fully initialized. |
4599 | static bool |
4600 | handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type, |
4601 | const LValue &LVal, APValue &RVal, |
4602 | bool WantObjectRepresentation = false) { |
4603 | if (LVal.Designator.Invalid) |
4604 | return false; |
4605 | |
4606 | // Check for special cases where there is no existing APValue to look at. |
4607 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
4608 | |
4609 | AccessKinds AK = |
4610 | WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read; |
4611 | |
4612 | if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) { |
4613 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Val: Base)) { |
4614 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
4615 | // initializer until now for such expressions. Such an expression can't be |
4616 | // an ICE in C, so this only matters for fold. |
4617 | if (Type.isVolatileQualified()) { |
4618 | Info.FFDiag(E: Conv); |
4619 | return false; |
4620 | } |
4621 | |
4622 | APValue Lit; |
4623 | if (!Evaluate(Result&: Lit, Info, E: CLE->getInitializer())) |
4624 | return false; |
4625 | |
4626 | // According to GCC info page: |
4627 | // |
4628 | // 6.28 Compound Literals |
4629 | // |
4630 | // As an optimization, G++ sometimes gives array compound literals longer |
4631 | // lifetimes: when the array either appears outside a function or has a |
4632 | // const-qualified type. If foo and its initializer had elements of type |
4633 | // char *const rather than char *, or if foo were a global variable, the |
4634 | // array would have static storage duration. But it is probably safest |
4635 | // just to avoid the use of array compound literals in C++ code. |
4636 | // |
4637 | // Obey that rule by checking constness for converted array types. |
4638 | |
4639 | QualType CLETy = CLE->getType(); |
4640 | if (CLETy->isArrayType() && !Type->isArrayType()) { |
4641 | if (!CLETy.isConstant(Ctx: Info.Ctx)) { |
4642 | Info.FFDiag(E: Conv); |
4643 | Info.Note(CLE->getExprLoc(), diag::note_declared_at); |
4644 | return false; |
4645 | } |
4646 | } |
4647 | |
4648 | CompleteObject LitObj(LVal.Base, &Lit, Base->getType()); |
4649 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal, AK); |
4650 | } else if (isa<StringLiteral>(Val: Base) || isa<PredefinedExpr>(Val: Base)) { |
4651 | // Special-case character extraction so we don't have to construct an |
4652 | // APValue for the whole string. |
4653 | assert(LVal.Designator.Entries.size() <= 1 && |
4654 | "Can only read characters from string literals"); |
4655 | if (LVal.Designator.Entries.empty()) { |
4656 | // Fail for now for LValue to RValue conversion of an array. |
4657 | // (This shouldn't show up in C/C++, but it could be triggered by a |
4658 | // weird EvaluateAsRValue call from a tool.) |
4659 | Info.FFDiag(E: Conv); |
4660 | return false; |
4661 | } |
4662 | if (LVal.Designator.isOnePastTheEnd()) { |
4663 | if (Info.getLangOpts().CPlusPlus11) |
4664 | Info.FFDiag(Conv, diag::note_constexpr_access_past_end) << AK; |
4665 | else |
4666 | Info.FFDiag(E: Conv); |
4667 | return false; |
4668 | } |
4669 | uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex(); |
4670 | RVal = APValue(extractStringLiteralCharacter(Info, Lit: Base, Index: CharIndex)); |
4671 | return true; |
4672 | } |
4673 | } |
4674 | |
4675 | CompleteObject Obj = findCompleteObject(Info, E: Conv, AK, LVal, LValType: Type); |
4676 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal, AK); |
4677 | } |
4678 | |
4679 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
4680 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
4681 | QualType LValType, APValue &Val) { |
4682 | if (LVal.Designator.Invalid) |
4683 | return false; |
4684 | |
4685 | if (!Info.getLangOpts().CPlusPlus14) { |
4686 | Info.FFDiag(E); |
4687 | return false; |
4688 | } |
4689 | |
4690 | CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType); |
4691 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
4692 | } |
4693 | |
4694 | namespace { |
4695 | struct CompoundAssignSubobjectHandler { |
4696 | EvalInfo &Info; |
4697 | const CompoundAssignOperator *E; |
4698 | QualType PromotedLHSType; |
4699 | BinaryOperatorKind Opcode; |
4700 | const APValue &RHS; |
4701 | |
4702 | static const AccessKinds AccessKind = AK_Assign; |
4703 | |
4704 | typedef bool result_type; |
4705 | |
4706 | bool checkConst(QualType QT) { |
4707 | // Assigning to a const object has undefined behavior. |
4708 | if (QT.isConstQualified()) { |
4709 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4710 | return false; |
4711 | } |
4712 | return true; |
4713 | } |
4714 | |
4715 | bool failed() { return false; } |
4716 | bool found(APValue &Subobj, QualType SubobjType) { |
4717 | switch (Subobj.getKind()) { |
4718 | case APValue::Int: |
4719 | return found(Value&: Subobj.getInt(), SubobjType); |
4720 | case APValue::Float: |
4721 | return found(Value&: Subobj.getFloat(), SubobjType); |
4722 | case APValue::ComplexInt: |
4723 | case APValue::ComplexFloat: |
4724 | // FIXME: Implement complex compound assignment. |
4725 | Info.FFDiag(E); |
4726 | return false; |
4727 | case APValue::LValue: |
4728 | return foundPointer(Subobj, SubobjType); |
4729 | case APValue::Vector: |
4730 | return foundVector(Value&: Subobj, SubobjType); |
4731 | case APValue::Indeterminate: |
4732 | Info.FFDiag(E, diag::note_constexpr_access_uninit) |
4733 | << /*read of=*/0 << /*uninitialized object=*/1 |
4734 | << E->getLHS()->getSourceRange(); |
4735 | return false; |
4736 | default: |
4737 | // FIXME: can this happen? |
4738 | Info.FFDiag(E); |
4739 | return false; |
4740 | } |
4741 | } |
4742 | |
4743 | bool foundVector(APValue &Value, QualType SubobjType) { |
4744 | if (!checkConst(QT: SubobjType)) |
4745 | return false; |
4746 | |
4747 | if (!SubobjType->isVectorType()) { |
4748 | Info.FFDiag(E); |
4749 | return false; |
4750 | } |
4751 | return handleVectorVectorBinOp(Info, E, Opcode, Value, RHS); |
4752 | } |
4753 | |
4754 | bool found(APSInt &Value, QualType SubobjType) { |
4755 | if (!checkConst(QT: SubobjType)) |
4756 | return false; |
4757 | |
4758 | if (!SubobjType->isIntegerType()) { |
4759 | // We don't support compound assignment on integer-cast-to-pointer |
4760 | // values. |
4761 | Info.FFDiag(E); |
4762 | return false; |
4763 | } |
4764 | |
4765 | if (RHS.isInt()) { |
4766 | APSInt LHS = |
4767 | HandleIntToIntCast(Info, E, PromotedLHSType, SubobjType, Value); |
4768 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
4769 | return false; |
4770 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
4771 | return true; |
4772 | } else if (RHS.isFloat()) { |
4773 | const FPOptions FPO = E->getFPFeaturesInEffect( |
4774 | Info.Ctx.getLangOpts()); |
4775 | APFloat FValue(0.0); |
4776 | return HandleIntToFloatCast(Info, E, FPO, SubobjType, Value, |
4777 | PromotedLHSType, FValue) && |
4778 | handleFloatFloatBinOp(Info, E, FValue, Opcode, RHS.getFloat()) && |
4779 | HandleFloatToIntCast(Info, E, PromotedLHSType, FValue, SubobjType, |
4780 | Value); |
4781 | } |
4782 | |
4783 | Info.FFDiag(E); |
4784 | return false; |
4785 | } |
4786 | bool found(APFloat &Value, QualType SubobjType) { |
4787 | return checkConst(SubobjType) && |
4788 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
4789 | Value) && |
4790 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
4791 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
4792 | } |
4793 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
4794 | if (!checkConst(QT: SubobjType)) |
4795 | return false; |
4796 | |
4797 | QualType PointeeType; |
4798 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
4799 | PointeeType = PT->getPointeeType(); |
4800 | |
4801 | if (PointeeType.isNull() || !RHS.isInt() || |
4802 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
4803 | Info.FFDiag(E); |
4804 | return false; |
4805 | } |
4806 | |
4807 | APSInt Offset = RHS.getInt(); |
4808 | if (Opcode == BO_Sub) |
4809 | negateAsSigned(Int&: Offset); |
4810 | |
4811 | LValue LVal; |
4812 | LVal.setFrom(Ctx&: Info.Ctx, V: Subobj); |
4813 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
4814 | return false; |
4815 | LVal.moveInto(V&: Subobj); |
4816 | return true; |
4817 | } |
4818 | }; |
4819 | } // end anonymous namespace |
4820 | |
4821 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
4822 | |
4823 | /// Perform a compound assignment of LVal <op>= RVal. |
4824 | static bool handleCompoundAssignment(EvalInfo &Info, |
4825 | const CompoundAssignOperator *E, |
4826 | const LValue &LVal, QualType LValType, |
4827 | QualType PromotedLValType, |
4828 | BinaryOperatorKind Opcode, |
4829 | const APValue &RVal) { |
4830 | if (LVal.Designator.Invalid) |
4831 | return false; |
4832 | |
4833 | if (!Info.getLangOpts().CPlusPlus14) { |
4834 | Info.FFDiag(E); |
4835 | return false; |
4836 | } |
4837 | |
4838 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
4839 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
4840 | RVal }; |
4841 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
4842 | } |
4843 | |
4844 | namespace { |
4845 | struct IncDecSubobjectHandler { |
4846 | EvalInfo &Info; |
4847 | const UnaryOperator *E; |
4848 | AccessKinds AccessKind; |
4849 | APValue *Old; |
4850 | |
4851 | typedef bool result_type; |
4852 | |
4853 | bool checkConst(QualType QT) { |
4854 | // Assigning to a const object has undefined behavior. |
4855 | if (QT.isConstQualified()) { |
4856 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4857 | return false; |
4858 | } |
4859 | return true; |
4860 | } |
4861 | |
4862 | bool failed() { return false; } |
4863 | bool found(APValue &Subobj, QualType SubobjType) { |
4864 | // Stash the old value. Also clear Old, so we don't clobber it later |
4865 | // if we're post-incrementing a complex. |
4866 | if (Old) { |
4867 | *Old = Subobj; |
4868 | Old = nullptr; |
4869 | } |
4870 | |
4871 | switch (Subobj.getKind()) { |
4872 | case APValue::Int: |
4873 | return found(Value&: Subobj.getInt(), SubobjType); |
4874 | case APValue::Float: |
4875 | return found(Value&: Subobj.getFloat(), SubobjType); |
4876 | case APValue::ComplexInt: |
4877 | return found(Value&: Subobj.getComplexIntReal(), |
4878 | SubobjType: SubobjType->castAs<ComplexType>()->getElementType() |
4879 | .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers())); |
4880 | case APValue::ComplexFloat: |
4881 | return found(Value&: Subobj.getComplexFloatReal(), |
4882 | SubobjType: SubobjType->castAs<ComplexType>()->getElementType() |
4883 | .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers())); |
4884 | case APValue::LValue: |
4885 | return foundPointer(Subobj, SubobjType); |
4886 | default: |
4887 | // FIXME: can this happen? |
4888 | Info.FFDiag(E); |
4889 | return false; |
4890 | } |
4891 | } |
4892 | bool found(APSInt &Value, QualType SubobjType) { |
4893 | if (!checkConst(QT: SubobjType)) |
4894 | return false; |
4895 | |
4896 | if (!SubobjType->isIntegerType()) { |
4897 | // We don't support increment / decrement on integer-cast-to-pointer |
4898 | // values. |
4899 | Info.FFDiag(E); |
4900 | return false; |
4901 | } |
4902 | |
4903 | if (Old) *Old = APValue(Value); |
4904 | |
4905 | // bool arithmetic promotes to int, and the conversion back to bool |
4906 | // doesn't reduce mod 2^n, so special-case it. |
4907 | if (SubobjType->isBooleanType()) { |
4908 | if (AccessKind == AK_Increment) |
4909 | Value = 1; |
4910 | else |
4911 | Value = !Value; |
4912 | return true; |
4913 | } |
4914 | |
4915 | bool WasNegative = Value.isNegative(); |
4916 | if (AccessKind == AK_Increment) { |
4917 | ++Value; |
4918 | |
4919 | if (!WasNegative && Value.isNegative() && E->canOverflow()) { |
4920 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
4921 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
4922 | } |
4923 | } else { |
4924 | --Value; |
4925 | |
4926 | if (WasNegative && !Value.isNegative() && E->canOverflow()) { |
4927 | unsigned BitWidth = Value.getBitWidth(); |
4928 | APSInt ActualValue(Value.sext(width: BitWidth + 1), /*IsUnsigned*/false); |
4929 | ActualValue.setBit(BitWidth); |
4930 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
4931 | } |
4932 | } |
4933 | return true; |
4934 | } |
4935 | bool found(APFloat &Value, QualType SubobjType) { |
4936 | if (!checkConst(QT: SubobjType)) |
4937 | return false; |
4938 | |
4939 | if (Old) *Old = APValue(Value); |
4940 | |
4941 | APFloat One(Value.getSemantics(), 1); |
4942 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E); |
4943 | APFloat::opStatus St; |
4944 | if (AccessKind == AK_Increment) |
4945 | St = Value.add(RHS: One, RM); |
4946 | else |
4947 | St = Value.subtract(RHS: One, RM); |
4948 | return checkFloatingPointResult(Info, E, St); |
4949 | } |
4950 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
4951 | if (!checkConst(QT: SubobjType)) |
4952 | return false; |
4953 | |
4954 | QualType PointeeType; |
4955 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
4956 | PointeeType = PT->getPointeeType(); |
4957 | else { |
4958 | Info.FFDiag(E); |
4959 | return false; |
4960 | } |
4961 | |
4962 | LValue LVal; |
4963 | LVal.setFrom(Ctx&: Info.Ctx, V: Subobj); |
4964 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
4965 | AccessKind == AK_Increment ? 1 : -1)) |
4966 | return false; |
4967 | LVal.moveInto(V&: Subobj); |
4968 | return true; |
4969 | } |
4970 | }; |
4971 | } // end anonymous namespace |
4972 | |
4973 | /// Perform an increment or decrement on LVal. |
4974 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
4975 | QualType LValType, bool IsIncrement, APValue *Old) { |
4976 | if (LVal.Designator.Invalid) |
4977 | return false; |
4978 | |
4979 | if (!Info.getLangOpts().CPlusPlus14) { |
4980 | Info.FFDiag(E); |
4981 | return false; |
4982 | } |
4983 | |
4984 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
4985 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
4986 | IncDecSubobjectHandler Handler = {.Info: Info, .E: cast<UnaryOperator>(Val: E), .AccessKind: AK, .Old: Old}; |
4987 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
4988 | } |
4989 | |
4990 | /// Build an lvalue for the object argument of a member function call. |
4991 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
4992 | LValue &This) { |
4993 | if (Object->getType()->isPointerType() && Object->isPRValue()) |
4994 | return EvaluatePointer(E: Object, Result&: This, Info); |
4995 | |
4996 | if (Object->isGLValue()) |
4997 | return EvaluateLValue(E: Object, Result&: This, Info); |
4998 | |
4999 | if (Object->getType()->isLiteralType(Ctx: Info.Ctx)) |
5000 | return EvaluateTemporary(E: Object, Result&: This, Info); |
5001 | |
5002 | if (Object->getType()->isRecordType() && Object->isPRValue()) |
5003 | return EvaluateTemporary(E: Object, Result&: This, Info); |
5004 | |
5005 | Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
5006 | return false; |
5007 | } |
5008 | |
5009 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
5010 | /// lvalue referring to the result. |
5011 | /// |
5012 | /// \param Info - Information about the ongoing evaluation. |
5013 | /// \param LV - An lvalue referring to the base of the member pointer. |
5014 | /// \param RHS - The member pointer expression. |
5015 | /// \param IncludeMember - Specifies whether the member itself is included in |
5016 | /// the resulting LValue subobject designator. This is not possible when |
5017 | /// creating a bound member function. |
5018 | /// \return The field or method declaration to which the member pointer refers, |
5019 | /// or 0 if evaluation fails. |
5020 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
5021 | QualType LVType, |
5022 | LValue &LV, |
5023 | const Expr *RHS, |
5024 | bool IncludeMember = true) { |
5025 | MemberPtr MemPtr; |
5026 | if (!EvaluateMemberPointer(E: RHS, Result&: MemPtr, Info)) |
5027 | return nullptr; |
5028 | |
5029 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
5030 | // member value, the behavior is undefined. |
5031 | if (!MemPtr.getDecl()) { |
5032 | // FIXME: Specific diagnostic. |
5033 | Info.FFDiag(E: RHS); |
5034 | return nullptr; |
5035 | } |
5036 | |
5037 | if (MemPtr.isDerivedMember()) { |
5038 | // This is a member of some derived class. Truncate LV appropriately. |
5039 | // The end of the derived-to-base path for the base object must match the |
5040 | // derived-to-base path for the member pointer. |
5041 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
5042 | LV.Designator.Entries.size()) { |
5043 | Info.FFDiag(E: RHS); |
5044 | return nullptr; |
5045 | } |
5046 | unsigned PathLengthToMember = |
5047 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
5048 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
5049 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
5050 | LV.Designator.Entries[PathLengthToMember + I]); |
5051 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
5052 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
5053 | Info.FFDiag(E: RHS); |
5054 | return nullptr; |
5055 | } |
5056 | } |
5057 | |
5058 | // Truncate the lvalue to the appropriate derived class. |
5059 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
5060 | PathLengthToMember)) |
5061 | return nullptr; |
5062 | } else if (!MemPtr.Path.empty()) { |
5063 | // Extend the LValue path with the member pointer's path. |
5064 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
5065 | MemPtr.Path.size() + IncludeMember); |
5066 | |
5067 | // Walk down to the appropriate base class. |
5068 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
5069 | LVType = PT->getPointeeType(); |
5070 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
5071 | assert(RD && "member pointer access on non-class-type expression"); |
5072 | // The first class in the path is that of the lvalue. |
5073 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
5074 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
5075 | if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, Base)) |
5076 | return nullptr; |
5077 | RD = Base; |
5078 | } |
5079 | // Finally cast to the class containing the member. |
5080 | if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, |
5081 | Base: MemPtr.getContainingRecord())) |
5082 | return nullptr; |
5083 | } |
5084 | |
5085 | // Add the member. Note that we cannot build bound member functions here. |
5086 | if (IncludeMember) { |
5087 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: MemPtr.getDecl())) { |
5088 | if (!HandleLValueMember(Info, E: RHS, LVal&: LV, FD)) |
5089 | return nullptr; |
5090 | } else if (const IndirectFieldDecl *IFD = |
5091 | dyn_cast<IndirectFieldDecl>(Val: MemPtr.getDecl())) { |
5092 | if (!HandleLValueIndirectMember(Info, E: RHS, LVal&: LV, IFD)) |
5093 | return nullptr; |
5094 | } else { |
5095 | llvm_unreachable("can't construct reference to bound member function"); |
5096 | } |
5097 | } |
5098 | |
5099 | return MemPtr.getDecl(); |
5100 | } |
5101 | |
5102 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
5103 | const BinaryOperator *BO, |
5104 | LValue &LV, |
5105 | bool IncludeMember = true) { |
5106 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
5107 | |
5108 | if (!EvaluateObjectArgument(Info, Object: BO->getLHS(), This&: LV)) { |
5109 | if (Info.noteFailure()) { |
5110 | MemberPtr MemPtr; |
5111 | EvaluateMemberPointer(E: BO->getRHS(), Result&: MemPtr, Info); |
5112 | } |
5113 | return nullptr; |
5114 | } |
5115 | |
5116 | return HandleMemberPointerAccess(Info, LVType: BO->getLHS()->getType(), LV, |
5117 | RHS: BO->getRHS(), IncludeMember); |
5118 | } |
5119 | |
5120 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
5121 | /// the provided lvalue, which currently refers to the base object. |
5122 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
5123 | LValue &Result) { |
5124 | SubobjectDesignator &D = Result.Designator; |
5125 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
5126 | return false; |
5127 | |
5128 | QualType TargetQT = E->getType(); |
5129 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
5130 | TargetQT = PT->getPointeeType(); |
5131 | |
5132 | // Check this cast lands within the final derived-to-base subobject path. |
5133 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
5134 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
5135 | << D.MostDerivedType << TargetQT; |
5136 | return false; |
5137 | } |
5138 | |
5139 | // Check the type of the final cast. We don't need to check the path, |
5140 | // since a cast can only be formed if the path is unique. |
5141 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
5142 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
5143 | const CXXRecordDecl *FinalType; |
5144 | if (NewEntriesSize == D.MostDerivedPathLength) |
5145 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
5146 | else |
5147 | FinalType = getAsBaseClass(E: D.Entries[NewEntriesSize - 1]); |
5148 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
5149 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
5150 | << D.MostDerivedType << TargetQT; |
5151 | return false; |
5152 | } |
5153 | |
5154 | // Truncate the lvalue to the appropriate derived class. |
5155 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
5156 | } |
5157 | |
5158 | /// Get the value to use for a default-initialized object of type T. |
5159 | /// Return false if it encounters something invalid. |
5160 | static bool handleDefaultInitValue(QualType T, APValue &Result) { |
5161 | bool Success = true; |
5162 | |
5163 | // If there is already a value present don't overwrite it. |
5164 | if (!Result.isAbsent()) |
5165 | return true; |
5166 | |
5167 | if (auto *RD = T->getAsCXXRecordDecl()) { |
5168 | if (RD->isInvalidDecl()) { |
5169 | Result = APValue(); |
5170 | return false; |
5171 | } |
5172 | if (RD->isUnion()) { |
5173 | Result = APValue((const FieldDecl *)nullptr); |
5174 | return true; |
5175 | } |
5176 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
5177 | std::distance(RD->field_begin(), RD->field_end())); |
5178 | |
5179 | unsigned Index = 0; |
5180 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
5181 | End = RD->bases_end(); |
5182 | I != End; ++I, ++Index) |
5183 | Success &= |
5184 | handleDefaultInitValue(T: I->getType(), Result&: Result.getStructBase(i: Index)); |
5185 | |
5186 | for (const auto *I : RD->fields()) { |
5187 | if (I->isUnnamedBitField()) |
5188 | continue; |
5189 | Success &= handleDefaultInitValue( |
5190 | I->getType(), Result.getStructField(I->getFieldIndex())); |
5191 | } |
5192 | return Success; |
5193 | } |
5194 | |
5195 | if (auto *AT = |
5196 | dyn_cast_or_null<ConstantArrayType>(T->getAsArrayTypeUnsafe())) { |
5197 | Result = APValue(APValue::UninitArray(), 0, AT->getZExtSize()); |
5198 | if (Result.hasArrayFiller()) |
5199 | Success &= |
5200 | handleDefaultInitValue(AT->getElementType(), Result.getArrayFiller()); |
5201 | |
5202 | return Success; |
5203 | } |
5204 | |
5205 | Result = APValue::IndeterminateValue(); |
5206 | return true; |
5207 | } |
5208 | |
5209 | namespace { |
5210 | enum EvalStmtResult { |
5211 | /// Evaluation failed. |
5212 | ESR_Failed, |
5213 | /// Hit a 'return' statement. |
5214 | ESR_Returned, |
5215 | /// Evaluation succeeded. |
5216 | ESR_Succeeded, |
5217 | /// Hit a 'continue' statement. |
5218 | ESR_Continue, |
5219 | /// Hit a 'break' statement. |
5220 | ESR_Break, |
5221 | /// Still scanning for 'case' or 'default' statement. |
5222 | ESR_CaseNotFound |
5223 | }; |
5224 | } |
5225 | |
5226 | static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) { |
5227 | if (VD->isInvalidDecl()) |
5228 | return false; |
5229 | // We don't need to evaluate the initializer for a static local. |
5230 | if (!VD->hasLocalStorage()) |
5231 | return true; |
5232 | |
5233 | LValue Result; |
5234 | APValue &Val = Info.CurrentCall->createTemporary(VD, VD->getType(), |
5235 | ScopeKind::Block, Result); |
5236 | |
5237 | const Expr *InitE = VD->getInit(); |
5238 | if (!InitE) { |
5239 | if (VD->getType()->isDependentType()) |
5240 | return Info.noteSideEffect(); |
5241 | return handleDefaultInitValue(VD->getType(), Val); |
5242 | } |
5243 | if (InitE->isValueDependent()) |
5244 | return false; |
5245 | |
5246 | if (!EvaluateInPlace(Result&: Val, Info, This: Result, E: InitE)) { |
5247 | // Wipe out any partially-computed value, to allow tracking that this |
5248 | // evaluation failed. |
5249 | Val = APValue(); |
5250 | return false; |
5251 | } |
5252 | |
5253 | return true; |
5254 | } |
5255 | |
5256 | static bool EvaluateDecompositionDeclInit(EvalInfo &Info, |
5257 | const DecompositionDecl *DD); |
5258 | |
5259 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D, |
5260 | bool EvaluateConditionDecl = false) { |
5261 | bool OK = true; |
5262 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
5263 | OK &= EvaluateVarDecl(Info, VD); |
5264 | |
5265 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(Val: D); |
5266 | EvaluateConditionDecl && DD) |
5267 | OK &= EvaluateDecompositionDeclInit(Info, DD); |
5268 | |
5269 | return OK; |
5270 | } |
5271 | |
5272 | static bool EvaluateDecompositionDeclInit(EvalInfo &Info, |
5273 | const DecompositionDecl *DD) { |
5274 | bool OK = true; |
5275 | for (auto *BD : DD->flat_bindings()) |
5276 | if (auto *VD = BD->getHoldingVar()) |
5277 | OK &= EvaluateDecl(Info, VD, /*EvaluateConditionDecl=*/true); |
5278 | |
5279 | return OK; |
5280 | } |
5281 | |
5282 | static bool MaybeEvaluateDeferredVarDeclInit(EvalInfo &Info, |
5283 | const VarDecl *VD) { |
5284 | if (auto *DD = dyn_cast_if_present<DecompositionDecl>(Val: VD)) { |
5285 | if (!EvaluateDecompositionDeclInit(Info, DD)) |
5286 | return false; |
5287 | } |
5288 | return true; |
5289 | } |
5290 | |
5291 | static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) { |
5292 | assert(E->isValueDependent()); |
5293 | if (Info.noteSideEffect()) |
5294 | return true; |
5295 | assert(E->containsErrors() && "valid value-dependent expression should never " |
5296 | "reach invalid code path."); |
5297 | return false; |
5298 | } |
5299 | |
5300 | /// Evaluate a condition (either a variable declaration or an expression). |
5301 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
5302 | const Expr *Cond, bool &Result) { |
5303 | if (Cond->isValueDependent()) |
5304 | return false; |
5305 | FullExpressionRAII Scope(Info); |
5306 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
5307 | return false; |
5308 | if (!EvaluateAsBooleanCondition(E: Cond, Result, Info)) |
5309 | return false; |
5310 | if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: CondDecl)) |
5311 | return false; |
5312 | return Scope.destroy(); |
5313 | } |
5314 | |
5315 | namespace { |
5316 | /// A location where the result (returned value) of evaluating a |
5317 | /// statement should be stored. |
5318 | struct StmtResult { |
5319 | /// The APValue that should be filled in with the returned value. |
5320 | APValue &Value; |
5321 | /// The location containing the result, if any (used to support RVO). |
5322 | const LValue *Slot; |
5323 | }; |
5324 | |
5325 | struct TempVersionRAII { |
5326 | CallStackFrame &Frame; |
5327 | |
5328 | TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) { |
5329 | Frame.pushTempVersion(); |
5330 | } |
5331 | |
5332 | ~TempVersionRAII() { |
5333 | Frame.popTempVersion(); |
5334 | } |
5335 | }; |
5336 | |
5337 | } |
5338 | |
5339 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
5340 | const Stmt *S, |
5341 | const SwitchCase *SC = nullptr); |
5342 | |
5343 | /// Evaluate the body of a loop, and translate the result as appropriate. |
5344 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
5345 | const Stmt *Body, |
5346 | const SwitchCase *Case = nullptr) { |
5347 | BlockScopeRAII Scope(Info); |
5348 | |
5349 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Body, SC: Case); |
5350 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
5351 | ESR = ESR_Failed; |
5352 | |
5353 | switch (ESR) { |
5354 | case ESR_Break: |
5355 | return ESR_Succeeded; |
5356 | case ESR_Succeeded: |
5357 | case ESR_Continue: |
5358 | return ESR_Continue; |
5359 | case ESR_Failed: |
5360 | case ESR_Returned: |
5361 | case ESR_CaseNotFound: |
5362 | return ESR; |
5363 | } |
5364 | llvm_unreachable("Invalid EvalStmtResult!"); |
5365 | } |
5366 | |
5367 | /// Evaluate a switch statement. |
5368 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
5369 | const SwitchStmt *SS) { |
5370 | BlockScopeRAII Scope(Info); |
5371 | |
5372 | // Evaluate the switch condition. |
5373 | APSInt Value; |
5374 | { |
5375 | if (const Stmt *Init = SS->getInit()) { |
5376 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init); |
5377 | if (ESR != ESR_Succeeded) { |
5378 | if (ESR != ESR_Failed && !Scope.destroy()) |
5379 | ESR = ESR_Failed; |
5380 | return ESR; |
5381 | } |
5382 | } |
5383 | |
5384 | FullExpressionRAII CondScope(Info); |
5385 | if (SS->getConditionVariable() && |
5386 | !EvaluateDecl(Info, SS->getConditionVariable())) |
5387 | return ESR_Failed; |
5388 | if (SS->getCond()->isValueDependent()) { |
5389 | // We don't know what the value is, and which branch should jump to. |
5390 | EvaluateDependentExpr(E: SS->getCond(), Info); |
5391 | return ESR_Failed; |
5392 | } |
5393 | if (!EvaluateInteger(E: SS->getCond(), Result&: Value, Info)) |
5394 | return ESR_Failed; |
5395 | |
5396 | if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: SS->getConditionVariable())) |
5397 | return ESR_Failed; |
5398 | |
5399 | if (!CondScope.destroy()) |
5400 | return ESR_Failed; |
5401 | } |
5402 | |
5403 | // Find the switch case corresponding to the value of the condition. |
5404 | // FIXME: Cache this lookup. |
5405 | const SwitchCase *Found = nullptr; |
5406 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
5407 | SC = SC->getNextSwitchCase()) { |
5408 | if (isa<DefaultStmt>(Val: SC)) { |
5409 | Found = SC; |
5410 | continue; |
5411 | } |
5412 | |
5413 | const CaseStmt *CS = cast<CaseStmt>(Val: SC); |
5414 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Ctx: Info.Ctx); |
5415 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Ctx: Info.Ctx) |
5416 | : LHS; |
5417 | if (LHS <= Value && Value <= RHS) { |
5418 | Found = SC; |
5419 | break; |
5420 | } |
5421 | } |
5422 | |
5423 | if (!Found) |
5424 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5425 | |
5426 | // Search the switch body for the switch case and evaluate it from there. |
5427 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SS->getBody(), SC: Found); |
5428 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
5429 | return ESR_Failed; |
5430 | |
5431 | switch (ESR) { |
5432 | case ESR_Break: |
5433 | return ESR_Succeeded; |
5434 | case ESR_Succeeded: |
5435 | case ESR_Continue: |
5436 | case ESR_Failed: |
5437 | case ESR_Returned: |
5438 | return ESR; |
5439 | case ESR_CaseNotFound: |
5440 | // This can only happen if the switch case is nested within a statement |
5441 | // expression. We have no intention of supporting that. |
5442 | Info.FFDiag(Found->getBeginLoc(), |
5443 | diag::note_constexpr_stmt_expr_unsupported); |
5444 | return ESR_Failed; |
5445 | } |
5446 | llvm_unreachable("Invalid EvalStmtResult!"); |
5447 | } |
5448 | |
5449 | static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) { |
5450 | // An expression E is a core constant expression unless the evaluation of E |
5451 | // would evaluate one of the following: [C++23] - a control flow that passes |
5452 | // through a declaration of a variable with static or thread storage duration |
5453 | // unless that variable is usable in constant expressions. |
5454 | if (VD->isLocalVarDecl() && VD->isStaticLocal() && |
5455 | !VD->isUsableInConstantExpressions(C: Info.Ctx)) { |
5456 | Info.CCEDiag(VD->getLocation(), diag::note_constexpr_static_local) |
5457 | << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD; |
5458 | return false; |
5459 | } |
5460 | return true; |
5461 | } |
5462 | |
5463 | // Evaluate a statement. |
5464 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
5465 | const Stmt *S, const SwitchCase *Case) { |
5466 | if (!Info.nextStep(S)) |
5467 | return ESR_Failed; |
5468 | |
5469 | // If we're hunting down a 'case' or 'default' label, recurse through |
5470 | // substatements until we hit the label. |
5471 | if (Case) { |
5472 | switch (S->getStmtClass()) { |
5473 | case Stmt::CompoundStmtClass: |
5474 | // FIXME: Precompute which substatement of a compound statement we |
5475 | // would jump to, and go straight there rather than performing a |
5476 | // linear scan each time. |
5477 | case Stmt::LabelStmtClass: |
5478 | case Stmt::AttributedStmtClass: |
5479 | case Stmt::DoStmtClass: |
5480 | break; |
5481 | |
5482 | case Stmt::CaseStmtClass: |
5483 | case Stmt::DefaultStmtClass: |
5484 | if (Case == S) |
5485 | Case = nullptr; |
5486 | break; |
5487 | |
5488 | case Stmt::IfStmtClass: { |
5489 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
5490 | // straight there rather than scanning both sides. |
5491 | const IfStmt *IS = cast<IfStmt>(Val: S); |
5492 | |
5493 | // Wrap the evaluation in a block scope, in case it's a DeclStmt |
5494 | // preceded by our switch label. |
5495 | BlockScopeRAII Scope(Info); |
5496 | |
5497 | // Step into the init statement in case it brings an (uninitialized) |
5498 | // variable into scope. |
5499 | if (const Stmt *Init = IS->getInit()) { |
5500 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case); |
5501 | if (ESR != ESR_CaseNotFound) { |
5502 | assert(ESR != ESR_Succeeded); |
5503 | return ESR; |
5504 | } |
5505 | } |
5506 | |
5507 | // Condition variable must be initialized if it exists. |
5508 | // FIXME: We can skip evaluating the body if there's a condition |
5509 | // variable, as there can't be any case labels within it. |
5510 | // (The same is true for 'for' statements.) |
5511 | |
5512 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: IS->getThen(), Case); |
5513 | if (ESR == ESR_Failed) |
5514 | return ESR; |
5515 | if (ESR != ESR_CaseNotFound) |
5516 | return Scope.destroy() ? ESR : ESR_Failed; |
5517 | if (!IS->getElse()) |
5518 | return ESR_CaseNotFound; |
5519 | |
5520 | ESR = EvaluateStmt(Result, Info, S: IS->getElse(), Case); |
5521 | if (ESR == ESR_Failed) |
5522 | return ESR; |
5523 | if (ESR != ESR_CaseNotFound) |
5524 | return Scope.destroy() ? ESR : ESR_Failed; |
5525 | return ESR_CaseNotFound; |
5526 | } |
5527 | |
5528 | case Stmt::WhileStmtClass: { |
5529 | EvalStmtResult ESR = |
5530 | EvaluateLoopBody(Result, Info, Body: cast<WhileStmt>(Val: S)->getBody(), Case); |
5531 | if (ESR != ESR_Continue) |
5532 | return ESR; |
5533 | break; |
5534 | } |
5535 | |
5536 | case Stmt::ForStmtClass: { |
5537 | const ForStmt *FS = cast<ForStmt>(Val: S); |
5538 | BlockScopeRAII Scope(Info); |
5539 | |
5540 | // Step into the init statement in case it brings an (uninitialized) |
5541 | // variable into scope. |
5542 | if (const Stmt *Init = FS->getInit()) { |
5543 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case); |
5544 | if (ESR != ESR_CaseNotFound) { |
5545 | assert(ESR != ESR_Succeeded); |
5546 | return ESR; |
5547 | } |
5548 | } |
5549 | |
5550 | EvalStmtResult ESR = |
5551 | EvaluateLoopBody(Result, Info, Body: FS->getBody(), Case); |
5552 | if (ESR != ESR_Continue) |
5553 | return ESR; |
5554 | if (const auto *Inc = FS->getInc()) { |
5555 | if (Inc->isValueDependent()) { |
5556 | if (!EvaluateDependentExpr(E: Inc, Info)) |
5557 | return ESR_Failed; |
5558 | } else { |
5559 | FullExpressionRAII IncScope(Info); |
5560 | if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy()) |
5561 | return ESR_Failed; |
5562 | } |
5563 | } |
5564 | break; |
5565 | } |
5566 | |
5567 | case Stmt::DeclStmtClass: { |
5568 | // Start the lifetime of any uninitialized variables we encounter. They |
5569 | // might be used by the selected branch of the switch. |
5570 | const DeclStmt *DS = cast<DeclStmt>(Val: S); |
5571 | for (const auto *D : DS->decls()) { |
5572 | if (const auto *VD = dyn_cast<VarDecl>(Val: D)) { |
5573 | if (!CheckLocalVariableDeclaration(Info, VD)) |
5574 | return ESR_Failed; |
5575 | if (VD->hasLocalStorage() && !VD->getInit()) |
5576 | if (!EvaluateVarDecl(Info, VD)) |
5577 | return ESR_Failed; |
5578 | // FIXME: If the variable has initialization that can't be jumped |
5579 | // over, bail out of any immediately-surrounding compound-statement |
5580 | // too. There can't be any case labels here. |
5581 | } |
5582 | } |
5583 | return ESR_CaseNotFound; |
5584 | } |
5585 | |
5586 | default: |
5587 | return ESR_CaseNotFound; |
5588 | } |
5589 | } |
5590 | |
5591 | switch (S->getStmtClass()) { |
5592 | default: |
5593 | if (const Expr *E = dyn_cast<Expr>(Val: S)) { |
5594 | if (E->isValueDependent()) { |
5595 | if (!EvaluateDependentExpr(E, Info)) |
5596 | return ESR_Failed; |
5597 | } else { |
5598 | // Don't bother evaluating beyond an expression-statement which couldn't |
5599 | // be evaluated. |
5600 | // FIXME: Do we need the FullExpressionRAII object here? |
5601 | // VisitExprWithCleanups should create one when necessary. |
5602 | FullExpressionRAII Scope(Info); |
5603 | if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy()) |
5604 | return ESR_Failed; |
5605 | } |
5606 | return ESR_Succeeded; |
5607 | } |
5608 | |
5609 | Info.FFDiag(Loc: S->getBeginLoc()) << S->getSourceRange(); |
5610 | return ESR_Failed; |
5611 | |
5612 | case Stmt::NullStmtClass: |
5613 | return ESR_Succeeded; |
5614 | |
5615 | case Stmt::DeclStmtClass: { |
5616 | const DeclStmt *DS = cast<DeclStmt>(Val: S); |
5617 | for (const auto *D : DS->decls()) { |
5618 | const VarDecl *VD = dyn_cast_or_null<VarDecl>(Val: D); |
5619 | if (VD && !CheckLocalVariableDeclaration(Info, VD)) |
5620 | return ESR_Failed; |
5621 | // Each declaration initialization is its own full-expression. |
5622 | FullExpressionRAII Scope(Info); |
5623 | if (!EvaluateDecl(Info, D, /*EvaluateConditionDecl=*/true) && |
5624 | !Info.noteFailure()) |
5625 | return ESR_Failed; |
5626 | if (!Scope.destroy()) |
5627 | return ESR_Failed; |
5628 | } |
5629 | return ESR_Succeeded; |
5630 | } |
5631 | |
5632 | case Stmt::ReturnStmtClass: { |
5633 | const Expr *RetExpr = cast<ReturnStmt>(Val: S)->getRetValue(); |
5634 | FullExpressionRAII Scope(Info); |
5635 | if (RetExpr && RetExpr->isValueDependent()) { |
5636 | EvaluateDependentExpr(E: RetExpr, Info); |
5637 | // We know we returned, but we don't know what the value is. |
5638 | return ESR_Failed; |
5639 | } |
5640 | if (RetExpr && |
5641 | !(Result.Slot |
5642 | ? EvaluateInPlace(Result&: Result.Value, Info, This: *Result.Slot, E: RetExpr) |
5643 | : Evaluate(Result&: Result.Value, Info, E: RetExpr))) |
5644 | return ESR_Failed; |
5645 | return Scope.destroy() ? ESR_Returned : ESR_Failed; |
5646 | } |
5647 | |
5648 | case Stmt::CompoundStmtClass: { |
5649 | BlockScopeRAII Scope(Info); |
5650 | |
5651 | const CompoundStmt *CS = cast<CompoundStmt>(Val: S); |
5652 | for (const auto *BI : CS->body()) { |
5653 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: BI, Case); |
5654 | if (ESR == ESR_Succeeded) |
5655 | Case = nullptr; |
5656 | else if (ESR != ESR_CaseNotFound) { |
5657 | if (ESR != ESR_Failed && !Scope.destroy()) |
5658 | return ESR_Failed; |
5659 | return ESR; |
5660 | } |
5661 | } |
5662 | if (Case) |
5663 | return ESR_CaseNotFound; |
5664 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5665 | } |
5666 | |
5667 | case Stmt::IfStmtClass: { |
5668 | const IfStmt *IS = cast<IfStmt>(Val: S); |
5669 | |
5670 | // Evaluate the condition, as either a var decl or as an expression. |
5671 | BlockScopeRAII Scope(Info); |
5672 | if (const Stmt *Init = IS->getInit()) { |
5673 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init); |
5674 | if (ESR != ESR_Succeeded) { |
5675 | if (ESR != ESR_Failed && !Scope.destroy()) |
5676 | return ESR_Failed; |
5677 | return ESR; |
5678 | } |
5679 | } |
5680 | bool Cond; |
5681 | if (IS->isConsteval()) { |
5682 | Cond = IS->isNonNegatedConsteval(); |
5683 | // If we are not in a constant context, if consteval should not evaluate |
5684 | // to true. |
5685 | if (!Info.InConstantContext) |
5686 | Cond = !Cond; |
5687 | } else if (!EvaluateCond(Info, CondDecl: IS->getConditionVariable(), Cond: IS->getCond(), |
5688 | Result&: Cond)) |
5689 | return ESR_Failed; |
5690 | |
5691 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
5692 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SubStmt); |
5693 | if (ESR != ESR_Succeeded) { |
5694 | if (ESR != ESR_Failed && !Scope.destroy()) |
5695 | return ESR_Failed; |
5696 | return ESR; |
5697 | } |
5698 | } |
5699 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5700 | } |
5701 | |
5702 | case Stmt::WhileStmtClass: { |
5703 | const WhileStmt *WS = cast<WhileStmt>(Val: S); |
5704 | while (true) { |
5705 | BlockScopeRAII Scope(Info); |
5706 | bool Continue; |
5707 | if (!EvaluateCond(Info, CondDecl: WS->getConditionVariable(), Cond: WS->getCond(), |
5708 | Result&: Continue)) |
5709 | return ESR_Failed; |
5710 | if (!Continue) |
5711 | break; |
5712 | |
5713 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: WS->getBody()); |
5714 | if (ESR != ESR_Continue) { |
5715 | if (ESR != ESR_Failed && !Scope.destroy()) |
5716 | return ESR_Failed; |
5717 | return ESR; |
5718 | } |
5719 | if (!Scope.destroy()) |
5720 | return ESR_Failed; |
5721 | } |
5722 | return ESR_Succeeded; |
5723 | } |
5724 | |
5725 | case Stmt::DoStmtClass: { |
5726 | const DoStmt *DS = cast<DoStmt>(Val: S); |
5727 | bool Continue; |
5728 | do { |
5729 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: DS->getBody(), Case); |
5730 | if (ESR != ESR_Continue) |
5731 | return ESR; |
5732 | Case = nullptr; |
5733 | |
5734 | if (DS->getCond()->isValueDependent()) { |
5735 | EvaluateDependentExpr(E: DS->getCond(), Info); |
5736 | // Bailout as we don't know whether to keep going or terminate the loop. |
5737 | return ESR_Failed; |
5738 | } |
5739 | FullExpressionRAII CondScope(Info); |
5740 | if (!EvaluateAsBooleanCondition(E: DS->getCond(), Result&: Continue, Info) || |
5741 | !CondScope.destroy()) |
5742 | return ESR_Failed; |
5743 | } while (Continue); |
5744 | return ESR_Succeeded; |
5745 | } |
5746 | |
5747 | case Stmt::ForStmtClass: { |
5748 | const ForStmt *FS = cast<ForStmt>(Val: S); |
5749 | BlockScopeRAII ForScope(Info); |
5750 | if (FS->getInit()) { |
5751 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit()); |
5752 | if (ESR != ESR_Succeeded) { |
5753 | if (ESR != ESR_Failed && !ForScope.destroy()) |
5754 | return ESR_Failed; |
5755 | return ESR; |
5756 | } |
5757 | } |
5758 | while (true) { |
5759 | BlockScopeRAII IterScope(Info); |
5760 | bool Continue = true; |
5761 | if (FS->getCond() && !EvaluateCond(Info, CondDecl: FS->getConditionVariable(), |
5762 | Cond: FS->getCond(), Result&: Continue)) |
5763 | return ESR_Failed; |
5764 | |
5765 | if (!Continue) { |
5766 | if (!IterScope.destroy()) |
5767 | return ESR_Failed; |
5768 | break; |
5769 | } |
5770 | |
5771 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody()); |
5772 | if (ESR != ESR_Continue) { |
5773 | if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy())) |
5774 | return ESR_Failed; |
5775 | return ESR; |
5776 | } |
5777 | |
5778 | if (const auto *Inc = FS->getInc()) { |
5779 | if (Inc->isValueDependent()) { |
5780 | if (!EvaluateDependentExpr(E: Inc, Info)) |
5781 | return ESR_Failed; |
5782 | } else { |
5783 | FullExpressionRAII IncScope(Info); |
5784 | if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy()) |
5785 | return ESR_Failed; |
5786 | } |
5787 | } |
5788 | |
5789 | if (!IterScope.destroy()) |
5790 | return ESR_Failed; |
5791 | } |
5792 | return ForScope.destroy() ? ESR_Succeeded : ESR_Failed; |
5793 | } |
5794 | |
5795 | case Stmt::CXXForRangeStmtClass: { |
5796 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(Val: S); |
5797 | BlockScopeRAII Scope(Info); |
5798 | |
5799 | // Evaluate the init-statement if present. |
5800 | if (FS->getInit()) { |
5801 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit()); |
5802 | if (ESR != ESR_Succeeded) { |
5803 | if (ESR != ESR_Failed && !Scope.destroy()) |
5804 | return ESR_Failed; |
5805 | return ESR; |
5806 | } |
5807 | } |
5808 | |
5809 | // Initialize the __range variable. |
5810 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getRangeStmt()); |
5811 | if (ESR != ESR_Succeeded) { |
5812 | if (ESR != ESR_Failed && !Scope.destroy()) |
5813 | return ESR_Failed; |
5814 | return ESR; |
5815 | } |
5816 | |
5817 | // In error-recovery cases it's possible to get here even if we failed to |
5818 | // synthesize the __begin and __end variables. |
5819 | if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond()) |
5820 | return ESR_Failed; |
5821 | |
5822 | // Create the __begin and __end iterators. |
5823 | ESR = EvaluateStmt(Result, Info, S: FS->getBeginStmt()); |
5824 | if (ESR != ESR_Succeeded) { |
5825 | if (ESR != ESR_Failed && !Scope.destroy()) |
5826 | return ESR_Failed; |
5827 | return ESR; |
5828 | } |
5829 | ESR = EvaluateStmt(Result, Info, S: FS->getEndStmt()); |
5830 | if (ESR != ESR_Succeeded) { |
5831 | if (ESR != ESR_Failed && !Scope.destroy()) |
5832 | return ESR_Failed; |
5833 | return ESR; |
5834 | } |
5835 | |
5836 | while (true) { |
5837 | // Condition: __begin != __end. |
5838 | { |
5839 | if (FS->getCond()->isValueDependent()) { |
5840 | EvaluateDependentExpr(E: FS->getCond(), Info); |
5841 | // We don't know whether to keep going or terminate the loop. |
5842 | return ESR_Failed; |
5843 | } |
5844 | bool Continue = true; |
5845 | FullExpressionRAII CondExpr(Info); |
5846 | if (!EvaluateAsBooleanCondition(E: FS->getCond(), Result&: Continue, Info)) |
5847 | return ESR_Failed; |
5848 | if (!Continue) |
5849 | break; |
5850 | } |
5851 | |
5852 | // User's variable declaration, initialized by *__begin. |
5853 | BlockScopeRAII InnerScope(Info); |
5854 | ESR = EvaluateStmt(Result, Info, S: FS->getLoopVarStmt()); |
5855 | if (ESR != ESR_Succeeded) { |
5856 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
5857 | return ESR_Failed; |
5858 | return ESR; |
5859 | } |
5860 | |
5861 | // Loop body. |
5862 | ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody()); |
5863 | if (ESR != ESR_Continue) { |
5864 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
5865 | return ESR_Failed; |
5866 | return ESR; |
5867 | } |
5868 | if (FS->getInc()->isValueDependent()) { |
5869 | if (!EvaluateDependentExpr(E: FS->getInc(), Info)) |
5870 | return ESR_Failed; |
5871 | } else { |
5872 | // Increment: ++__begin |
5873 | if (!EvaluateIgnoredValue(Info, E: FS->getInc())) |
5874 | return ESR_Failed; |
5875 | } |
5876 | |
5877 | if (!InnerScope.destroy()) |
5878 | return ESR_Failed; |
5879 | } |
5880 | |
5881 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5882 | } |
5883 | |
5884 | case Stmt::SwitchStmtClass: |
5885 | return EvaluateSwitch(Result, Info, SS: cast<SwitchStmt>(Val: S)); |
5886 | |
5887 | case Stmt::ContinueStmtClass: |
5888 | return ESR_Continue; |
5889 | |
5890 | case Stmt::BreakStmtClass: |
5891 | return ESR_Break; |
5892 | |
5893 | case Stmt::LabelStmtClass: |
5894 | return EvaluateStmt(Result, Info, S: cast<LabelStmt>(Val: S)->getSubStmt(), Case); |
5895 | |
5896 | case Stmt::AttributedStmtClass: { |
5897 | const auto *AS = cast<AttributedStmt>(Val: S); |
5898 | const auto *SS = AS->getSubStmt(); |
5899 | MSConstexprContextRAII ConstexprContext( |
5900 | *Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(AS->getAttrs()) && |
5901 | isa<ReturnStmt>(SS)); |
5902 | |
5903 | auto LO = Info.getASTContext().getLangOpts(); |
5904 | if (LO.CXXAssumptions && !LO.MSVCCompat) { |
5905 | for (auto *Attr : AS->getAttrs()) { |
5906 | auto *AA = dyn_cast<CXXAssumeAttr>(Attr); |
5907 | if (!AA) |
5908 | continue; |
5909 | |
5910 | auto *Assumption = AA->getAssumption(); |
5911 | if (Assumption->isValueDependent()) |
5912 | return ESR_Failed; |
5913 | |
5914 | if (Assumption->HasSideEffects(Info.getASTContext())) |
5915 | continue; |
5916 | |
5917 | bool Value; |
5918 | if (!EvaluateAsBooleanCondition(Assumption, Value, Info)) |
5919 | return ESR_Failed; |
5920 | if (!Value) { |
5921 | Info.CCEDiag(Assumption->getExprLoc(), |
5922 | diag::note_constexpr_assumption_failed); |
5923 | return ESR_Failed; |
5924 | } |
5925 | } |
5926 | } |
5927 | |
5928 | return EvaluateStmt(Result, Info, S: SS, Case); |
5929 | } |
5930 | |
5931 | case Stmt::CaseStmtClass: |
5932 | case Stmt::DefaultStmtClass: |
5933 | return EvaluateStmt(Result, Info, S: cast<SwitchCase>(Val: S)->getSubStmt(), Case); |
5934 | case Stmt::CXXTryStmtClass: |
5935 | // Evaluate try blocks by evaluating all sub statements. |
5936 | return EvaluateStmt(Result, Info, cast<CXXTryStmt>(Val: S)->getTryBlock(), Case); |
5937 | } |
5938 | } |
5939 | |
5940 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
5941 | /// default constructor. If so, we'll fold it whether or not it's marked as |
5942 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
5943 | /// so we need special handling. |
5944 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
5945 | const CXXConstructorDecl *CD, |
5946 | bool IsValueInitialization) { |
5947 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
5948 | return false; |
5949 | |
5950 | // Value-initialization does not call a trivial default constructor, so such a |
5951 | // call is a core constant expression whether or not the constructor is |
5952 | // constexpr. |
5953 | if (!CD->isConstexpr() && !IsValueInitialization) { |
5954 | if (Info.getLangOpts().CPlusPlus11) { |
5955 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
5956 | // we should be much more explicit about why it's not constexpr. |
5957 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
5958 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
5959 | Info.Note(CD->getLocation(), diag::note_declared_at); |
5960 | } else { |
5961 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
5962 | } |
5963 | } |
5964 | return true; |
5965 | } |
5966 | |
5967 | /// CheckConstexprFunction - Check that a function can be called in a constant |
5968 | /// expression. |
5969 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
5970 | const FunctionDecl *Declaration, |
5971 | const FunctionDecl *Definition, |
5972 | const Stmt *Body) { |
5973 | // Potential constant expressions can contain calls to declared, but not yet |
5974 | // defined, constexpr functions. |
5975 | if (Info.checkingPotentialConstantExpression() && !Definition && |
5976 | Declaration->isConstexpr()) |
5977 | return false; |
5978 | |
5979 | // Bail out if the function declaration itself is invalid. We will |
5980 | // have produced a relevant diagnostic while parsing it, so just |
5981 | // note the problematic sub-expression. |
5982 | if (Declaration->isInvalidDecl()) { |
5983 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5984 | return false; |
5985 | } |
5986 | |
5987 | // DR1872: An instantiated virtual constexpr function can't be called in a |
5988 | // constant expression (prior to C++20). We can still constant-fold such a |
5989 | // call. |
5990 | if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Declaration) && |
5991 | cast<CXXMethodDecl>(Declaration)->isVirtual()) |
5992 | Info.CCEDiag(CallLoc, diag::note_constexpr_virtual_call); |
5993 | |
5994 | if (Definition && Definition->isInvalidDecl()) { |
5995 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5996 | return false; |
5997 | } |
5998 | |
5999 | // Can we evaluate this function call? |
6000 | if (Definition && Body && |
6001 | (Definition->isConstexpr() || (Info.CurrentCall->CanEvalMSConstexpr && |
6002 | Definition->hasAttr<MSConstexprAttr>()))) |
6003 | return true; |
6004 | |
6005 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
6006 | // Special note for the assert() macro, as the normal error message falsely |
6007 | // implies we cannot use an assertion during constant evaluation. |
6008 | if (CallLoc.isMacroID() && DiagDecl->getIdentifier()) { |
6009 | // FIXME: Instead of checking for an implementation-defined function, |
6010 | // check and evaluate the assert() macro. |
6011 | StringRef Name = DiagDecl->getName(); |
6012 | bool AssertFailed = |
6013 | Name == "__assert_rtn"|| Name == "__assert_fail"|| Name == "_wassert"; |
6014 | if (AssertFailed) { |
6015 | Info.FFDiag(CallLoc, diag::note_constexpr_assert_failed); |
6016 | return false; |
6017 | } |
6018 | } |
6019 | |
6020 | if (Info.getLangOpts().CPlusPlus11) { |
6021 | // If this function is not constexpr because it is an inherited |
6022 | // non-constexpr constructor, diagnose that directly. |
6023 | auto *CD = dyn_cast<CXXConstructorDecl>(Val: DiagDecl); |
6024 | if (CD && CD->isInheritingConstructor()) { |
6025 | auto *Inherited = CD->getInheritedConstructor().getConstructor(); |
6026 | if (!Inherited->isConstexpr()) |
6027 | DiagDecl = CD = Inherited; |
6028 | } |
6029 | |
6030 | // FIXME: If DiagDecl is an implicitly-declared special member function |
6031 | // or an inheriting constructor, we should be much more explicit about why |
6032 | // it's not constexpr. |
6033 | if (CD && CD->isInheritingConstructor()) |
6034 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1) |
6035 | << CD->getInheritedConstructor().getConstructor()->getParent(); |
6036 | else |
6037 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1) |
6038 | << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; |
6039 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
6040 | } else { |
6041 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
6042 | } |
6043 | return false; |
6044 | } |
6045 | |
6046 | namespace { |
6047 | struct CheckDynamicTypeHandler { |
6048 | AccessKinds AccessKind; |
6049 | typedef bool result_type; |
6050 | bool failed() { return false; } |
6051 | bool found(APValue &Subobj, QualType SubobjType) { return true; } |
6052 | bool found(APSInt &Value, QualType SubobjType) { return true; } |
6053 | bool found(APFloat &Value, QualType SubobjType) { return true; } |
6054 | }; |
6055 | } // end anonymous namespace |
6056 | |
6057 | /// Check that we can access the notional vptr of an object / determine its |
6058 | /// dynamic type. |
6059 | static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This, |
6060 | AccessKinds AK, bool Polymorphic) { |
6061 | // We are not allowed to invoke a virtual function whose dynamic type |
6062 | // is constexpr-unknown, so stop early and let this fail later on if we |
6063 | // attempt to do so. |
6064 | // C++23 [expr.const]p5.6 |
6065 | // an invocation of a virtual function ([class.virtual]) for an object whose |
6066 | // dynamic type is constexpr-unknown; |
6067 | if (This.allowConstexprUnknown()) |
6068 | return true; |
6069 | |
6070 | if (This.Designator.Invalid) |
6071 | return false; |
6072 | |
6073 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: This, LValType: QualType()); |
6074 | |
6075 | if (!Obj) |
6076 | return false; |
6077 | |
6078 | if (!Obj.Value) { |
6079 | // The object is not usable in constant expressions, so we can't inspect |
6080 | // its value to see if it's in-lifetime or what the active union members |
6081 | // are. We can still check for a one-past-the-end lvalue. |
6082 | if (This.Designator.isOnePastTheEnd() || |
6083 | This.Designator.isMostDerivedAnUnsizedArray()) { |
6084 | Info.FFDiag(E, This.Designator.isOnePastTheEnd() |
6085 | ? diag::note_constexpr_access_past_end |
6086 | : diag::note_constexpr_access_unsized_array) |
6087 | << AK; |
6088 | return false; |
6089 | } else if (Polymorphic) { |
6090 | // Conservatively refuse to perform a polymorphic operation if we would |
6091 | // not be able to read a notional 'vptr' value. |
6092 | APValue Val; |
6093 | This.moveInto(V&: Val); |
6094 | QualType StarThisType = |
6095 | Info.Ctx.getLValueReferenceType(T: This.Designator.getType(Info.Ctx)); |
6096 | Info.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type) |
6097 | << AK << Val.getAsString(Info.Ctx, StarThisType); |
6098 | return false; |
6099 | } |
6100 | return true; |
6101 | } |
6102 | |
6103 | CheckDynamicTypeHandler Handler{.AccessKind: AK}; |
6104 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
6105 | } |
6106 | |
6107 | /// Check that the pointee of the 'this' pointer in a member function call is |
6108 | /// either within its lifetime or in its period of construction or destruction. |
6109 | static bool |
6110 | checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E, |
6111 | const LValue &This, |
6112 | const CXXMethodDecl *NamedMember) { |
6113 | return checkDynamicType( |
6114 | Info, E, This, |
6115 | AK: isa<CXXDestructorDecl>(Val: NamedMember) ? AK_Destroy : AK_MemberCall, Polymorphic: false); |
6116 | } |
6117 | |
6118 | struct DynamicType { |
6119 | /// The dynamic class type of the object. |
6120 | const CXXRecordDecl *Type; |
6121 | /// The corresponding path length in the lvalue. |
6122 | unsigned PathLength; |
6123 | }; |
6124 | |
6125 | static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator, |
6126 | unsigned PathLength) { |
6127 | assert(PathLength >= Designator.MostDerivedPathLength && PathLength <= |
6128 | Designator.Entries.size() && "invalid path length"); |
6129 | return (PathLength == Designator.MostDerivedPathLength) |
6130 | ? Designator.MostDerivedType->getAsCXXRecordDecl() |
6131 | : getAsBaseClass(E: Designator.Entries[PathLength - 1]); |
6132 | } |
6133 | |
6134 | /// Determine the dynamic type of an object. |
6135 | static std::optional<DynamicType> ComputeDynamicType(EvalInfo &Info, |
6136 | const Expr *E, |
6137 | LValue &This, |
6138 | AccessKinds AK) { |
6139 | // If we don't have an lvalue denoting an object of class type, there is no |
6140 | // meaningful dynamic type. (We consider objects of non-class type to have no |
6141 | // dynamic type.) |
6142 | if (!checkDynamicType(Info, E, This, AK, |
6143 | Polymorphic: (AK == AK_TypeId |
6144 | ? (E->getType()->isReferenceType() ? true : false) |
6145 | : true))) |
6146 | return std::nullopt; |
6147 | |
6148 | if (This.Designator.Invalid) |
6149 | return std::nullopt; |
6150 | |
6151 | // Refuse to compute a dynamic type in the presence of virtual bases. This |
6152 | // shouldn't happen other than in constant-folding situations, since literal |
6153 | // types can't have virtual bases. |
6154 | // |
6155 | // Note that consumers of DynamicType assume that the type has no virtual |
6156 | // bases, and will need modifications if this restriction is relaxed. |
6157 | const CXXRecordDecl *Class = |
6158 | This.Designator.MostDerivedType->getAsCXXRecordDecl(); |
6159 | if (!Class || Class->getNumVBases()) { |
6160 | Info.FFDiag(E); |
6161 | return std::nullopt; |
6162 | } |
6163 | |
6164 | // FIXME: For very deep class hierarchies, it might be beneficial to use a |
6165 | // binary search here instead. But the overwhelmingly common case is that |
6166 | // we're not in the middle of a constructor, so it probably doesn't matter |
6167 | // in practice. |
6168 | ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries; |
6169 | for (unsigned PathLength = This.Designator.MostDerivedPathLength; |
6170 | PathLength <= Path.size(); ++PathLength) { |
6171 | switch (Info.isEvaluatingCtorDtor(Base: This.getLValueBase(), |
6172 | Path: Path.slice(N: 0, M: PathLength))) { |
6173 | case ConstructionPhase::Bases: |
6174 | case ConstructionPhase::DestroyingBases: |
6175 | // We're constructing or destroying a base class. This is not the dynamic |
6176 | // type. |
6177 | break; |
6178 | |
6179 | case ConstructionPhase::None: |
6180 | case ConstructionPhase::AfterBases: |
6181 | case ConstructionPhase::AfterFields: |
6182 | case ConstructionPhase::Destroying: |
6183 | // We've finished constructing the base classes and not yet started |
6184 | // destroying them again, so this is the dynamic type. |
6185 | return DynamicType{getBaseClassType(This.Designator, PathLength), |
6186 | PathLength}; |
6187 | } |
6188 | } |
6189 | |
6190 | // CWG issue 1517: we're constructing a base class of the object described by |
6191 | // 'This', so that object has not yet begun its period of construction and |
6192 | // any polymorphic operation on it results in undefined behavior. |
6193 | Info.FFDiag(E); |
6194 | return std::nullopt; |
6195 | } |
6196 | |
6197 | /// Perform virtual dispatch. |
6198 | static const CXXMethodDecl *HandleVirtualDispatch( |
6199 | EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found, |
6200 | llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) { |
6201 | std::optional<DynamicType> DynType = ComputeDynamicType( |
6202 | Info, E, This, |
6203 | AK: isa<CXXDestructorDecl>(Val: Found) ? AK_Destroy : AK_MemberCall); |
6204 | if (!DynType) |
6205 | return nullptr; |
6206 | |
6207 | // Find the final overrider. It must be declared in one of the classes on the |
6208 | // path from the dynamic type to the static type. |
6209 | // FIXME: If we ever allow literal types to have virtual base classes, that |
6210 | // won't be true. |
6211 | const CXXMethodDecl *Callee = Found; |
6212 | unsigned PathLength = DynType->PathLength; |
6213 | for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) { |
6214 | const CXXRecordDecl *Class = getBaseClassType(This.Designator, PathLength); |
6215 | const CXXMethodDecl *Overrider = |
6216 | Found->getCorrespondingMethodDeclaredInClass(RD: Class, MayBeBase: false); |
6217 | if (Overrider) { |
6218 | Callee = Overrider; |
6219 | break; |
6220 | } |
6221 | } |
6222 | |
6223 | // C++2a [class.abstract]p6: |
6224 | // the effect of making a virtual call to a pure virtual function [...] is |
6225 | // undefined |
6226 | if (Callee->isPureVirtual()) { |
6227 | Info.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << Callee; |
6228 | Info.Note(Callee->getLocation(), diag::note_declared_at); |
6229 | return nullptr; |
6230 | } |
6231 | |
6232 | // If necessary, walk the rest of the path to determine the sequence of |
6233 | // covariant adjustment steps to apply. |
6234 | if (!Info.Ctx.hasSameUnqualifiedType(T1: Callee->getReturnType(), |
6235 | T2: Found->getReturnType())) { |
6236 | CovariantAdjustmentPath.push_back(Elt: Callee->getReturnType()); |
6237 | for (unsigned CovariantPathLength = PathLength + 1; |
6238 | CovariantPathLength != This.Designator.Entries.size(); |
6239 | ++CovariantPathLength) { |
6240 | const CXXRecordDecl *NextClass = |
6241 | getBaseClassType(This.Designator, CovariantPathLength); |
6242 | const CXXMethodDecl *Next = |
6243 | Found->getCorrespondingMethodDeclaredInClass(RD: NextClass, MayBeBase: false); |
6244 | if (Next && !Info.Ctx.hasSameUnqualifiedType( |
6245 | T1: Next->getReturnType(), T2: CovariantAdjustmentPath.back())) |
6246 | CovariantAdjustmentPath.push_back(Elt: Next->getReturnType()); |
6247 | } |
6248 | if (!Info.Ctx.hasSameUnqualifiedType(T1: Found->getReturnType(), |
6249 | T2: CovariantAdjustmentPath.back())) |
6250 | CovariantAdjustmentPath.push_back(Elt: Found->getReturnType()); |
6251 | } |
6252 | |
6253 | // Perform 'this' adjustment. |
6254 | if (!CastToDerivedClass(Info, E, This, Callee->getParent(), PathLength)) |
6255 | return nullptr; |
6256 | |
6257 | return Callee; |
6258 | } |
6259 | |
6260 | /// Perform the adjustment from a value returned by a virtual function to |
6261 | /// a value of the statically expected type, which may be a pointer or |
6262 | /// reference to a base class of the returned type. |
6263 | static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E, |
6264 | APValue &Result, |
6265 | ArrayRef<QualType> Path) { |
6266 | assert(Result.isLValue() && |
6267 | "unexpected kind of APValue for covariant return"); |
6268 | if (Result.isNullPointer()) |
6269 | return true; |
6270 | |
6271 | LValue LVal; |
6272 | LVal.setFrom(Ctx&: Info.Ctx, V: Result); |
6273 | |
6274 | const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl(); |
6275 | for (unsigned I = 1; I != Path.size(); ++I) { |
6276 | const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl(); |
6277 | assert(OldClass && NewClass && "unexpected kind of covariant return"); |
6278 | if (OldClass != NewClass && |
6279 | !CastToBaseClass(Info, E, Result&: LVal, DerivedRD: OldClass, BaseRD: NewClass)) |
6280 | return false; |
6281 | OldClass = NewClass; |
6282 | } |
6283 | |
6284 | LVal.moveInto(V&: Result); |
6285 | return true; |
6286 | } |
6287 | |
6288 | /// Determine whether \p Base, which is known to be a direct base class of |
6289 | /// \p Derived, is a public base class. |
6290 | static bool isBaseClassPublic(const CXXRecordDecl *Derived, |
6291 | const CXXRecordDecl *Base) { |
6292 | for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) { |
6293 | auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl(); |
6294 | if (BaseClass && declaresSameEntity(BaseClass, Base)) |
6295 | return BaseSpec.getAccessSpecifier() == AS_public; |
6296 | } |
6297 | llvm_unreachable("Base is not a direct base of Derived"); |
6298 | } |
6299 | |
6300 | /// Apply the given dynamic cast operation on the provided lvalue. |
6301 | /// |
6302 | /// This implements the hard case of dynamic_cast, requiring a "runtime check" |
6303 | /// to find a suitable target subobject. |
6304 | static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E, |
6305 | LValue &Ptr) { |
6306 | // We can't do anything with a non-symbolic pointer value. |
6307 | SubobjectDesignator &D = Ptr.Designator; |
6308 | if (D.Invalid) |
6309 | return false; |
6310 | |
6311 | // C++ [expr.dynamic.cast]p6: |
6312 | // If v is a null pointer value, the result is a null pointer value. |
6313 | if (Ptr.isNullPointer() && !E->isGLValue()) |
6314 | return true; |
6315 | |
6316 | // For all the other cases, we need the pointer to point to an object within |
6317 | // its lifetime / period of construction / destruction, and we need to know |
6318 | // its dynamic type. |
6319 | std::optional<DynamicType> DynType = |
6320 | ComputeDynamicType(Info, E, Ptr, AK_DynamicCast); |
6321 | if (!DynType) |
6322 | return false; |
6323 | |
6324 | // C++ [expr.dynamic.cast]p7: |
6325 | // If T is "pointer to cv void", then the result is a pointer to the most |
6326 | // derived object |
6327 | if (E->getType()->isVoidPointerType()) |
6328 | return CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength); |
6329 | |
6330 | const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl(); |
6331 | assert(C && "dynamic_cast target is not void pointer nor class"); |
6332 | CanQualType CQT = Info.Ctx.getCanonicalType(T: Info.Ctx.getRecordType(C)); |
6333 | |
6334 | auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) { |
6335 | // C++ [expr.dynamic.cast]p9: |
6336 | if (!E->isGLValue()) { |
6337 | // The value of a failed cast to pointer type is the null pointer value |
6338 | // of the required result type. |
6339 | Ptr.setNull(Ctx&: Info.Ctx, PointerTy: E->getType()); |
6340 | return true; |
6341 | } |
6342 | |
6343 | // A failed cast to reference type throws [...] std::bad_cast. |
6344 | unsigned DiagKind; |
6345 | if (!Paths && (declaresSameEntity(DynType->Type, C) || |
6346 | DynType->Type->isDerivedFrom(Base: C))) |
6347 | DiagKind = 0; |
6348 | else if (!Paths || Paths->begin() == Paths->end()) |
6349 | DiagKind = 1; |
6350 | else if (Paths->isAmbiguous(BaseType: CQT)) |
6351 | DiagKind = 2; |
6352 | else { |
6353 | assert(Paths->front().Access != AS_public && "why did the cast fail?"); |
6354 | DiagKind = 3; |
6355 | } |
6356 | Info.FFDiag(E, diag::note_constexpr_dynamic_cast_to_reference_failed) |
6357 | << DiagKind << Ptr.Designator.getType(Info.Ctx) |
6358 | << Info.Ctx.getRecordType(DynType->Type) |
6359 | << E->getType().getUnqualifiedType(); |
6360 | return false; |
6361 | }; |
6362 | |
6363 | // Runtime check, phase 1: |
6364 | // Walk from the base subobject towards the derived object looking for the |
6365 | // target type. |
6366 | for (int PathLength = Ptr.Designator.Entries.size(); |
6367 | PathLength >= (int)DynType->PathLength; --PathLength) { |
6368 | const CXXRecordDecl *Class = getBaseClassType(Ptr.Designator, PathLength); |
6369 | if (declaresSameEntity(Class, C)) |
6370 | return CastToDerivedClass(Info, E, Ptr, Class, PathLength); |
6371 | // We can only walk across public inheritance edges. |
6372 | if (PathLength > (int)DynType->PathLength && |
6373 | !isBaseClassPublic(getBaseClassType(Ptr.Designator, PathLength - 1), |
6374 | Class)) |
6375 | return RuntimeCheckFailed(nullptr); |
6376 | } |
6377 | |
6378 | // Runtime check, phase 2: |
6379 | // Search the dynamic type for an unambiguous public base of type C. |
6380 | CXXBasePaths Paths(/*FindAmbiguities=*/true, |
6381 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
6382 | if (DynType->Type->isDerivedFrom(Base: C, Paths) && !Paths.isAmbiguous(BaseType: CQT) && |
6383 | Paths.front().Access == AS_public) { |
6384 | // Downcast to the dynamic type... |
6385 | if (!CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength)) |
6386 | return false; |
6387 | // ... then upcast to the chosen base class subobject. |
6388 | for (CXXBasePathElement &Elem : Paths.front()) |
6389 | if (!HandleLValueBase(Info, E, Ptr, Elem.Class, Elem.Base)) |
6390 | return false; |
6391 | return true; |
6392 | } |
6393 | |
6394 | // Otherwise, the runtime check fails. |
6395 | return RuntimeCheckFailed(&Paths); |
6396 | } |
6397 | |
6398 | namespace { |
6399 | struct StartLifetimeOfUnionMemberHandler { |
6400 | EvalInfo &Info; |
6401 | const Expr *LHSExpr; |
6402 | const FieldDecl *Field; |
6403 | bool DuringInit; |
6404 | bool Failed = false; |
6405 | static const AccessKinds AccessKind = AK_Assign; |
6406 | |
6407 | typedef bool result_type; |
6408 | bool failed() { return Failed; } |
6409 | bool found(APValue &Subobj, QualType SubobjType) { |
6410 | // We are supposed to perform no initialization but begin the lifetime of |
6411 | // the object. We interpret that as meaning to do what default |
6412 | // initialization of the object would do if all constructors involved were |
6413 | // trivial: |
6414 | // * All base, non-variant member, and array element subobjects' lifetimes |
6415 | // begin |
6416 | // * No variant members' lifetimes begin |
6417 | // * All scalar subobjects whose lifetimes begin have indeterminate values |
6418 | assert(SubobjType->isUnionType()); |
6419 | if (declaresSameEntity(Subobj.getUnionField(), Field)) { |
6420 | // This union member is already active. If it's also in-lifetime, there's |
6421 | // nothing to do. |
6422 | if (Subobj.getUnionValue().hasValue()) |
6423 | return true; |
6424 | } else if (DuringInit) { |
6425 | // We're currently in the process of initializing a different union |
6426 | // member. If we carried on, that initialization would attempt to |
6427 | // store to an inactive union member, resulting in undefined behavior. |
6428 | Info.FFDiag(LHSExpr, |
6429 | diag::note_constexpr_union_member_change_during_init); |
6430 | return false; |
6431 | } |
6432 | APValue Result; |
6433 | Failed = !handleDefaultInitValue(Field->getType(), Result); |
6434 | Subobj.setUnion(Field, Value: Result); |
6435 | return true; |
6436 | } |
6437 | bool found(APSInt &Value, QualType SubobjType) { |
6438 | llvm_unreachable("wrong value kind for union object"); |
6439 | } |
6440 | bool found(APFloat &Value, QualType SubobjType) { |
6441 | llvm_unreachable("wrong value kind for union object"); |
6442 | } |
6443 | }; |
6444 | } // end anonymous namespace |
6445 | |
6446 | const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind; |
6447 | |
6448 | /// Handle a builtin simple-assignment or a call to a trivial assignment |
6449 | /// operator whose left-hand side might involve a union member access. If it |
6450 | /// does, implicitly start the lifetime of any accessed union elements per |
6451 | /// C++20 [class.union]5. |
6452 | static bool MaybeHandleUnionActiveMemberChange(EvalInfo &Info, |
6453 | const Expr *LHSExpr, |
6454 | const LValue &LHS) { |
6455 | if (LHS.InvalidBase || LHS.Designator.Invalid) |
6456 | return false; |
6457 | |
6458 | llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths; |
6459 | // C++ [class.union]p5: |
6460 | // define the set S(E) of subexpressions of E as follows: |
6461 | unsigned PathLength = LHS.Designator.Entries.size(); |
6462 | for (const Expr *E = LHSExpr; E != nullptr;) { |
6463 | // -- If E is of the form A.B, S(E) contains the elements of S(A)... |
6464 | if (auto *ME = dyn_cast<MemberExpr>(Val: E)) { |
6465 | auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl()); |
6466 | // Note that we can't implicitly start the lifetime of a reference, |
6467 | // so we don't need to proceed any further if we reach one. |
6468 | if (!FD || FD->getType()->isReferenceType()) |
6469 | break; |
6470 | |
6471 | // ... and also contains A.B if B names a union member ... |
6472 | if (FD->getParent()->isUnion()) { |
6473 | // ... of a non-class, non-array type, or of a class type with a |
6474 | // trivial default constructor that is not deleted, or an array of |
6475 | // such types. |
6476 | auto *RD = |
6477 | FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
6478 | if (!RD || RD->hasTrivialDefaultConstructor()) |
6479 | UnionPathLengths.push_back(Elt: {PathLength - 1, FD}); |
6480 | } |
6481 | |
6482 | E = ME->getBase(); |
6483 | --PathLength; |
6484 | assert(declaresSameEntity(FD, |
6485 | LHS.Designator.Entries[PathLength] |
6486 | .getAsBaseOrMember().getPointer())); |
6487 | |
6488 | // -- If E is of the form A[B] and is interpreted as a built-in array |
6489 | // subscripting operator, S(E) is [S(the array operand, if any)]. |
6490 | } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) { |
6491 | // Step over an ArrayToPointerDecay implicit cast. |
6492 | auto *Base = ASE->getBase()->IgnoreImplicit(); |
6493 | if (!Base->getType()->isArrayType()) |
6494 | break; |
6495 | |
6496 | E = Base; |
6497 | --PathLength; |
6498 | |
6499 | } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E)) { |
6500 | // Step over a derived-to-base conversion. |
6501 | E = ICE->getSubExpr(); |
6502 | if (ICE->getCastKind() == CK_NoOp) |
6503 | continue; |
6504 | if (ICE->getCastKind() != CK_DerivedToBase && |
6505 | ICE->getCastKind() != CK_UncheckedDerivedToBase) |
6506 | break; |
6507 | // Walk path backwards as we walk up from the base to the derived class. |
6508 | for (const CXXBaseSpecifier *Elt : llvm::reverse(ICE->path())) { |
6509 | if (Elt->isVirtual()) { |
6510 | // A class with virtual base classes never has a trivial default |
6511 | // constructor, so S(E) is empty in this case. |
6512 | E = nullptr; |
6513 | break; |
6514 | } |
6515 | |
6516 | --PathLength; |
6517 | assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(), |
6518 | LHS.Designator.Entries[PathLength] |
6519 | .getAsBaseOrMember().getPointer())); |
6520 | } |
6521 | |
6522 | // -- Otherwise, S(E) is empty. |
6523 | } else { |
6524 | break; |
6525 | } |
6526 | } |
6527 | |
6528 | // Common case: no unions' lifetimes are started. |
6529 | if (UnionPathLengths.empty()) |
6530 | return true; |
6531 | |
6532 | // if modification of X [would access an inactive union member], an object |
6533 | // of the type of X is implicitly created |
6534 | CompleteObject Obj = |
6535 | findCompleteObject(Info, E: LHSExpr, AK: AK_Assign, LVal: LHS, LValType: LHSExpr->getType()); |
6536 | if (!Obj) |
6537 | return false; |
6538 | for (std::pair<unsigned, const FieldDecl *> LengthAndField : |
6539 | llvm::reverse(C&: UnionPathLengths)) { |
6540 | // Form a designator for the union object. |
6541 | SubobjectDesignator D = LHS.Designator; |
6542 | D.truncate(Ctx&: Info.Ctx, Base: LHS.Base, NewLength: LengthAndField.first); |
6543 | |
6544 | bool DuringInit = Info.isEvaluatingCtorDtor(Base: LHS.Base, Path: D.Entries) == |
6545 | ConstructionPhase::AfterBases; |
6546 | StartLifetimeOfUnionMemberHandler StartLifetime{ |
6547 | .Info: Info, .LHSExpr: LHSExpr, .Field: LengthAndField.second, .DuringInit: DuringInit}; |
6548 | if (!findSubobject(Info, E: LHSExpr, Obj, Sub: D, handler&: StartLifetime)) |
6549 | return false; |
6550 | } |
6551 | |
6552 | return true; |
6553 | } |
6554 | |
6555 | static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg, |
6556 | CallRef Call, EvalInfo &Info, bool NonNull = false, |
6557 | APValue **EvaluatedArg = nullptr) { |
6558 | LValue LV; |
6559 | // Create the parameter slot and register its destruction. For a vararg |
6560 | // argument, create a temporary. |
6561 | // FIXME: For calling conventions that destroy parameters in the callee, |
6562 | // should we consider performing destruction when the function returns |
6563 | // instead? |
6564 | APValue &V = PVD ? Info.CurrentCall->createParam(Args: Call, PVD, LV) |
6565 | : Info.CurrentCall->createTemporary(Key: Arg, T: Arg->getType(), |
6566 | Scope: ScopeKind::Call, LV); |
6567 | if (!EvaluateInPlace(Result&: V, Info, This: LV, E: Arg)) |
6568 | return false; |
6569 | |
6570 | // Passing a null pointer to an __attribute__((nonnull)) parameter results in |
6571 | // undefined behavior, so is non-constant. |
6572 | if (NonNull && V.isLValue() && V.isNullPointer()) { |
6573 | Info.CCEDiag(Arg, diag::note_non_null_attribute_failed); |
6574 | return false; |
6575 | } |
6576 | |
6577 | if (EvaluatedArg) |
6578 | *EvaluatedArg = &V; |
6579 | |
6580 | return true; |
6581 | } |
6582 | |
6583 | /// Evaluate the arguments to a function call. |
6584 | static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call, |
6585 | EvalInfo &Info, const FunctionDecl *Callee, |
6586 | bool RightToLeft = false, |
6587 | LValue *ObjectArg = nullptr) { |
6588 | bool Success = true; |
6589 | llvm::SmallBitVector ForbiddenNullArgs; |
6590 | if (Callee->hasAttr<NonNullAttr>()) { |
6591 | ForbiddenNullArgs.resize(N: Args.size()); |
6592 | for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) { |
6593 | if (!Attr->args_size()) { |
6594 | ForbiddenNullArgs.set(); |
6595 | break; |
6596 | } else |
6597 | for (auto Idx : Attr->args()) { |
6598 | unsigned ASTIdx = Idx.getASTIndex(); |
6599 | if (ASTIdx >= Args.size()) |
6600 | continue; |
6601 | ForbiddenNullArgs[ASTIdx] = true; |
6602 | } |
6603 | } |
6604 | } |
6605 | for (unsigned I = 0; I < Args.size(); I++) { |
6606 | unsigned Idx = RightToLeft ? Args.size() - I - 1 : I; |
6607 | const ParmVarDecl *PVD = |
6608 | Idx < Callee->getNumParams() ? Callee->getParamDecl(i: Idx) : nullptr; |
6609 | bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx]; |
6610 | APValue *That = nullptr; |
6611 | if (!EvaluateCallArg(PVD, Arg: Args[Idx], Call, Info, NonNull, EvaluatedArg: &That)) { |
6612 | // If we're checking for a potential constant expression, evaluate all |
6613 | // initializers even if some of them fail. |
6614 | if (!Info.noteFailure()) |
6615 | return false; |
6616 | Success = false; |
6617 | } |
6618 | if (PVD && PVD->isExplicitObjectParameter() && That && That->isLValue()) |
6619 | ObjectArg->setFrom(Ctx&: Info.Ctx, V: *That); |
6620 | } |
6621 | return Success; |
6622 | } |
6623 | |
6624 | /// Perform a trivial copy from Param, which is the parameter of a copy or move |
6625 | /// constructor or assignment operator. |
6626 | static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param, |
6627 | const Expr *E, APValue &Result, |
6628 | bool CopyObjectRepresentation) { |
6629 | // Find the reference argument. |
6630 | CallStackFrame *Frame = Info.CurrentCall; |
6631 | APValue *RefValue = Info.getParamSlot(Call: Frame->Arguments, PVD: Param); |
6632 | if (!RefValue) { |
6633 | Info.FFDiag(E); |
6634 | return false; |
6635 | } |
6636 | |
6637 | // Copy out the contents of the RHS object. |
6638 | LValue RefLValue; |
6639 | RefLValue.setFrom(Ctx&: Info.Ctx, V: *RefValue); |
6640 | return handleLValueToRValueConversion( |
6641 | Info, E, Param->getType().getNonReferenceType(), RefLValue, Result, |
6642 | CopyObjectRepresentation); |
6643 | } |
6644 | |
6645 | /// Evaluate a function call. |
6646 | static bool HandleFunctionCall(SourceLocation CallLoc, |
6647 | const FunctionDecl *Callee, |
6648 | const LValue *ObjectArg, const Expr *E, |
6649 | ArrayRef<const Expr *> Args, CallRef Call, |
6650 | const Stmt *Body, EvalInfo &Info, |
6651 | APValue &Result, const LValue *ResultSlot) { |
6652 | if (!Info.CheckCallLimit(Loc: CallLoc)) |
6653 | return false; |
6654 | |
6655 | CallStackFrame Frame(Info, E->getSourceRange(), Callee, ObjectArg, E, Call); |
6656 | |
6657 | // For a trivial copy or move assignment, perform an APValue copy. This is |
6658 | // essential for unions, where the operations performed by the assignment |
6659 | // operator cannot be represented as statements. |
6660 | // |
6661 | // Skip this for non-union classes with no fields; in that case, the defaulted |
6662 | // copy/move does not actually read the object. |
6663 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: Callee); |
6664 | if (MD && MD->isDefaulted() && |
6665 | (MD->getParent()->isUnion() || |
6666 | (MD->isTrivial() && |
6667 | isReadByLvalueToRvalueConversion(RD: MD->getParent())))) { |
6668 | unsigned ExplicitOffset = MD->isExplicitObjectMemberFunction() ? 1 : 0; |
6669 | assert(ObjectArg && |
6670 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
6671 | APValue RHSValue; |
6672 | if (!handleTrivialCopy(Info, MD->getParamDecl(0), Args[0], RHSValue, |
6673 | MD->getParent()->isUnion())) |
6674 | return false; |
6675 | |
6676 | LValue Obj; |
6677 | if (!handleAssignment(Info, E: Args[ExplicitOffset], LVal: *ObjectArg, |
6678 | LValType: MD->getFunctionObjectParameterReferenceType(), |
6679 | Val&: RHSValue)) |
6680 | return false; |
6681 | ObjectArg->moveInto(V&: Result); |
6682 | return true; |
6683 | } else if (MD && isLambdaCallOperator(MD)) { |
6684 | // We're in a lambda; determine the lambda capture field maps unless we're |
6685 | // just constexpr checking a lambda's call operator. constexpr checking is |
6686 | // done before the captures have been added to the closure object (unless |
6687 | // we're inferring constexpr-ness), so we don't have access to them in this |
6688 | // case. But since we don't need the captures to constexpr check, we can |
6689 | // just ignore them. |
6690 | if (!Info.checkingPotentialConstantExpression()) |
6691 | MD->getParent()->getCaptureFields(Captures&: Frame.LambdaCaptureFields, |
6692 | ThisCapture&: Frame.LambdaThisCaptureField); |
6693 | } |
6694 | |
6695 | StmtResult Ret = {.Value: Result, .Slot: ResultSlot}; |
6696 | EvalStmtResult ESR = EvaluateStmt(Result&: Ret, Info, S: Body); |
6697 | if (ESR == ESR_Succeeded) { |
6698 | if (Callee->getReturnType()->isVoidType()) |
6699 | return true; |
6700 | Info.FFDiag(Callee->getEndLoc(), diag::note_constexpr_no_return); |
6701 | } |
6702 | return ESR == ESR_Returned; |
6703 | } |
6704 | |
6705 | /// Evaluate a constructor call. |
6706 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
6707 | CallRef Call, |
6708 | const CXXConstructorDecl *Definition, |
6709 | EvalInfo &Info, APValue &Result) { |
6710 | SourceLocation CallLoc = E->getExprLoc(); |
6711 | if (!Info.CheckCallLimit(Loc: CallLoc)) |
6712 | return false; |
6713 | |
6714 | const CXXRecordDecl *RD = Definition->getParent(); |
6715 | if (RD->getNumVBases()) { |
6716 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
6717 | return false; |
6718 | } |
6719 | |
6720 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
6721 | Info, |
6722 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
6723 | RD->getNumBases()); |
6724 | CallStackFrame Frame(Info, E->getSourceRange(), Definition, &This, E, Call); |
6725 | |
6726 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
6727 | // wasteful. |
6728 | APValue RetVal; |
6729 | StmtResult Ret = {.Value: RetVal, .Slot: nullptr}; |
6730 | |
6731 | // If it's a delegating constructor, delegate. |
6732 | if (Definition->isDelegatingConstructor()) { |
6733 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
6734 | if ((*I)->getInit()->isValueDependent()) { |
6735 | if (!EvaluateDependentExpr(E: (*I)->getInit(), Info)) |
6736 | return false; |
6737 | } else { |
6738 | FullExpressionRAII InitScope(Info); |
6739 | if (!EvaluateInPlace(Result, Info, This, E: (*I)->getInit()) || |
6740 | !InitScope.destroy()) |
6741 | return false; |
6742 | } |
6743 | return EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed; |
6744 | } |
6745 | |
6746 | // For a trivial copy or move constructor, perform an APValue copy. This is |
6747 | // essential for unions (or classes with anonymous union members), where the |
6748 | // operations performed by the constructor cannot be represented by |
6749 | // ctor-initializers. |
6750 | // |
6751 | // Skip this for empty non-union classes; we should not perform an |
6752 | // lvalue-to-rvalue conversion on them because their copy constructor does not |
6753 | // actually read them. |
6754 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
6755 | (Definition->getParent()->isUnion() || |
6756 | (Definition->isTrivial() && |
6757 | isReadByLvalueToRvalueConversion(Definition->getParent())))) { |
6758 | return handleTrivialCopy(Info, Definition->getParamDecl(0), E, Result, |
6759 | Definition->getParent()->isUnion()); |
6760 | } |
6761 | |
6762 | // Reserve space for the struct members. |
6763 | if (!Result.hasValue()) { |
6764 | if (!RD->isUnion()) |
6765 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
6766 | std::distance(RD->field_begin(), RD->field_end())); |
6767 | else |
6768 | // A union starts with no active member. |
6769 | Result = APValue((const FieldDecl*)nullptr); |
6770 | } |
6771 | |
6772 | if (RD->isInvalidDecl()) return false; |
6773 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6774 | |
6775 | // A scope for temporaries lifetime-extended by reference members. |
6776 | BlockScopeRAII LifetimeExtendedScope(Info); |
6777 | |
6778 | bool Success = true; |
6779 | unsigned BasesSeen = 0; |
6780 | #ifndef NDEBUG |
6781 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
6782 | #endif |
6783 | CXXRecordDecl::field_iterator FieldIt = RD->field_begin(); |
6784 | auto SkipToField = [&](FieldDecl *FD, bool Indirect) { |
6785 | // We might be initializing the same field again if this is an indirect |
6786 | // field initialization. |
6787 | if (FieldIt == RD->field_end() || |
6788 | FieldIt->getFieldIndex() > FD->getFieldIndex()) { |
6789 | assert(Indirect && "fields out of order?"); |
6790 | return; |
6791 | } |
6792 | |
6793 | // Default-initialize any fields with no explicit initializer. |
6794 | for (; !declaresSameEntity(*FieldIt, FD); ++FieldIt) { |
6795 | assert(FieldIt != RD->field_end() && "missing field?"); |
6796 | if (!FieldIt->isUnnamedBitField()) |
6797 | Success &= handleDefaultInitValue( |
6798 | FieldIt->getType(), |
6799 | Result.getStructField(i: FieldIt->getFieldIndex())); |
6800 | } |
6801 | ++FieldIt; |
6802 | }; |
6803 | for (const auto *I : Definition->inits()) { |
6804 | LValue Subobject = This; |
6805 | LValue SubobjectParent = This; |
6806 | APValue *Value = &Result; |
6807 | |
6808 | // Determine the subobject to initialize. |
6809 | FieldDecl *FD = nullptr; |
6810 | if (I->isBaseInitializer()) { |
6811 | QualType BaseType(I->getBaseClass(), 0); |
6812 | #ifndef NDEBUG |
6813 | // Non-virtual base classes are initialized in the order in the class |
6814 | // definition. We have already checked for virtual base classes. |
6815 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
6816 | assert(Info.Ctx.hasSameUnqualifiedType(BaseIt->getType(), BaseType) && |
6817 | "base class initializers not in expected order"); |
6818 | ++BaseIt; |
6819 | #endif |
6820 | if (!HandleLValueDirectBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD, |
6821 | Base: BaseType->getAsCXXRecordDecl(), RL: &Layout)) |
6822 | return false; |
6823 | Value = &Result.getStructBase(i: BasesSeen++); |
6824 | } else if ((FD = I->getMember())) { |
6825 | if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD, RL: &Layout)) |
6826 | return false; |
6827 | if (RD->isUnion()) { |
6828 | Result = APValue(FD); |
6829 | Value = &Result.getUnionValue(); |
6830 | } else { |
6831 | SkipToField(FD, false); |
6832 | Value = &Result.getStructField(i: FD->getFieldIndex()); |
6833 | } |
6834 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
6835 | // Walk the indirect field decl's chain to find the object to initialize, |
6836 | // and make sure we've initialized every step along it. |
6837 | auto IndirectFieldChain = IFD->chain(); |
6838 | for (auto *C : IndirectFieldChain) { |
6839 | FD = cast<FieldDecl>(Val: C); |
6840 | CXXRecordDecl *CD = cast<CXXRecordDecl>(Val: FD->getParent()); |
6841 | // Switch the union field if it differs. This happens if we had |
6842 | // preceding zero-initialization, and we're now initializing a union |
6843 | // subobject other than the first. |
6844 | // FIXME: In this case, the values of the other subobjects are |
6845 | // specified, since zero-initialization sets all padding bits to zero. |
6846 | if (!Value->hasValue() || |
6847 | (Value->isUnion() && |
6848 | !declaresSameEntity(Value->getUnionField(), FD))) { |
6849 | if (CD->isUnion()) |
6850 | *Value = APValue(FD); |
6851 | else |
6852 | // FIXME: This immediately starts the lifetime of all members of |
6853 | // an anonymous struct. It would be preferable to strictly start |
6854 | // member lifetime in initialization order. |
6855 | Success &= |
6856 | handleDefaultInitValue(T: Info.Ctx.getRecordType(CD), Result&: *Value); |
6857 | } |
6858 | // Store Subobject as its parent before updating it for the last element |
6859 | // in the chain. |
6860 | if (C == IndirectFieldChain.back()) |
6861 | SubobjectParent = Subobject; |
6862 | if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD)) |
6863 | return false; |
6864 | if (CD->isUnion()) |
6865 | Value = &Value->getUnionValue(); |
6866 | else { |
6867 | if (C == IndirectFieldChain.front() && !RD->isUnion()) |
6868 | SkipToField(FD, true); |
6869 | Value = &Value->getStructField(i: FD->getFieldIndex()); |
6870 | } |
6871 | } |
6872 | } else { |
6873 | llvm_unreachable("unknown base initializer kind"); |
6874 | } |
6875 | |
6876 | // Need to override This for implicit field initializers as in this case |
6877 | // This refers to innermost anonymous struct/union containing initializer, |
6878 | // not to currently constructed class. |
6879 | const Expr *Init = I->getInit(); |
6880 | if (Init->isValueDependent()) { |
6881 | if (!EvaluateDependentExpr(E: Init, Info)) |
6882 | return false; |
6883 | } else { |
6884 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent, |
6885 | isa<CXXDefaultInitExpr>(Val: Init)); |
6886 | FullExpressionRAII InitScope(Info); |
6887 | if (!EvaluateInPlace(Result&: *Value, Info, This: Subobject, E: Init) || |
6888 | (FD && FD->isBitField() && |
6889 | !truncateBitfieldValue(Info, E: Init, Value&: *Value, FD))) { |
6890 | // If we're checking for a potential constant expression, evaluate all |
6891 | // initializers even if some of them fail. |
6892 | if (!Info.noteFailure()) |
6893 | return false; |
6894 | Success = false; |
6895 | } |
6896 | } |
6897 | |
6898 | // This is the point at which the dynamic type of the object becomes this |
6899 | // class type. |
6900 | if (I->isBaseInitializer() && BasesSeen == RD->getNumBases()) |
6901 | EvalObj.finishedConstructingBases(); |
6902 | } |
6903 | |
6904 | // Default-initialize any remaining fields. |
6905 | if (!RD->isUnion()) { |
6906 | for (; FieldIt != RD->field_end(); ++FieldIt) { |
6907 | if (!FieldIt->isUnnamedBitField()) |
6908 | Success &= handleDefaultInitValue( |
6909 | FieldIt->getType(), |
6910 | Result.getStructField(i: FieldIt->getFieldIndex())); |
6911 | } |
6912 | } |
6913 | |
6914 | EvalObj.finishedConstructingFields(); |
6915 | |
6916 | return Success && |
6917 | EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed && |
6918 | LifetimeExtendedScope.destroy(); |
6919 | } |
6920 | |
6921 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
6922 | ArrayRef<const Expr*> Args, |
6923 | const CXXConstructorDecl *Definition, |
6924 | EvalInfo &Info, APValue &Result) { |
6925 | CallScopeRAII CallScope(Info); |
6926 | CallRef Call = Info.CurrentCall->createCall(Definition); |
6927 | if (!EvaluateArgs(Args, Call, Info, Definition)) |
6928 | return false; |
6929 | |
6930 | return HandleConstructorCall(E, This, Call, Definition, Info, Result) && |
6931 | CallScope.destroy(); |
6932 | } |
6933 | |
6934 | static bool HandleDestructionImpl(EvalInfo &Info, SourceRange CallRange, |
6935 | const LValue &This, APValue &Value, |
6936 | QualType T) { |
6937 | // Objects can only be destroyed while they're within their lifetimes. |
6938 | // FIXME: We have no representation for whether an object of type nullptr_t |
6939 | // is in its lifetime; it usually doesn't matter. Perhaps we should model it |
6940 | // as indeterminate instead? |
6941 | if (Value.isAbsent() && !T->isNullPtrType()) { |
6942 | APValue Printable; |
6943 | This.moveInto(V&: Printable); |
6944 | Info.FFDiag(CallRange.getBegin(), |
6945 | diag::note_constexpr_destroy_out_of_lifetime) |
6946 | << Printable.getAsString(Info.Ctx, Info.Ctx.getLValueReferenceType(T)); |
6947 | return false; |
6948 | } |
6949 | |
6950 | // Invent an expression for location purposes. |
6951 | // FIXME: We shouldn't need to do this. |
6952 | OpaqueValueExpr LocE(CallRange.getBegin(), Info.Ctx.IntTy, VK_PRValue); |
6953 | |
6954 | // For arrays, destroy elements right-to-left. |
6955 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) { |
6956 | uint64_t Size = CAT->getZExtSize(); |
6957 | QualType ElemT = CAT->getElementType(); |
6958 | |
6959 | if (!CheckArraySize(Info, CAT, CallLoc: CallRange.getBegin())) |
6960 | return false; |
6961 | |
6962 | LValue ElemLV = This; |
6963 | ElemLV.addArray(Info, &LocE, CAT); |
6964 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, Size)) |
6965 | return false; |
6966 | |
6967 | // Ensure that we have actual array elements available to destroy; the |
6968 | // destructors might mutate the value, so we can't run them on the array |
6969 | // filler. |
6970 | if (Size && Size > Value.getArrayInitializedElts()) |
6971 | expandArray(Array&: Value, Index: Value.getArraySize() - 1); |
6972 | |
6973 | // The size of the array might have been reduced by |
6974 | // a placement new. |
6975 | for (Size = Value.getArraySize(); Size != 0; --Size) { |
6976 | APValue &Elem = Value.getArrayInitializedElt(I: Size - 1); |
6977 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, -1) || |
6978 | !HandleDestructionImpl(Info, CallRange, This: ElemLV, Value&: Elem, T: ElemT)) |
6979 | return false; |
6980 | } |
6981 | |
6982 | // End the lifetime of this array now. |
6983 | Value = APValue(); |
6984 | return true; |
6985 | } |
6986 | |
6987 | const CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
6988 | if (!RD) { |
6989 | if (T.isDestructedType()) { |
6990 | Info.FFDiag(CallRange.getBegin(), |
6991 | diag::note_constexpr_unsupported_destruction) |
6992 | << T; |
6993 | return false; |
6994 | } |
6995 | |
6996 | Value = APValue(); |
6997 | return true; |
6998 | } |
6999 | |
7000 | if (RD->getNumVBases()) { |
7001 | Info.FFDiag(CallRange.getBegin(), diag::note_constexpr_virtual_base) << RD; |
7002 | return false; |
7003 | } |
7004 | |
7005 | const CXXDestructorDecl *DD = RD->getDestructor(); |
7006 | if (!DD && !RD->hasTrivialDestructor()) { |
7007 | Info.FFDiag(Loc: CallRange.getBegin()); |
7008 | return false; |
7009 | } |
7010 | |
7011 | if (!DD || DD->isTrivial() || |
7012 | (RD->isAnonymousStructOrUnion() && RD->isUnion())) { |
7013 | // A trivial destructor just ends the lifetime of the object. Check for |
7014 | // this case before checking for a body, because we might not bother |
7015 | // building a body for a trivial destructor. Note that it doesn't matter |
7016 | // whether the destructor is constexpr in this case; all trivial |
7017 | // destructors are constexpr. |
7018 | // |
7019 | // If an anonymous union would be destroyed, some enclosing destructor must |
7020 | // have been explicitly defined, and the anonymous union destruction should |
7021 | // have no effect. |
7022 | Value = APValue(); |
7023 | return true; |
7024 | } |
7025 | |
7026 | if (!Info.CheckCallLimit(Loc: CallRange.getBegin())) |
7027 | return false; |
7028 | |
7029 | const FunctionDecl *Definition = nullptr; |
7030 | const Stmt *Body = DD->getBody(Definition); |
7031 | |
7032 | if (!CheckConstexprFunction(Info, CallRange.getBegin(), DD, Definition, Body)) |
7033 | return false; |
7034 | |
7035 | CallStackFrame Frame(Info, CallRange, Definition, &This, /*CallExpr=*/nullptr, |
7036 | CallRef()); |
7037 | |
7038 | // We're now in the period of destruction of this object. |
7039 | unsigned BasesLeft = RD->getNumBases(); |
7040 | EvalInfo::EvaluatingDestructorRAII EvalObj( |
7041 | Info, |
7042 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}); |
7043 | if (!EvalObj.DidInsert) { |
7044 | // C++2a [class.dtor]p19: |
7045 | // the behavior is undefined if the destructor is invoked for an object |
7046 | // whose lifetime has ended |
7047 | // (Note that formally the lifetime ends when the period of destruction |
7048 | // begins, even though certain uses of the object remain valid until the |
7049 | // period of destruction ends.) |
7050 | Info.FFDiag(CallRange.getBegin(), diag::note_constexpr_double_destroy); |
7051 | return false; |
7052 | } |
7053 | |
7054 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
7055 | // wasteful. |
7056 | APValue RetVal; |
7057 | StmtResult Ret = {.Value: RetVal, .Slot: nullptr}; |
7058 | if (EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) == ESR_Failed) |
7059 | return false; |
7060 | |
7061 | // A union destructor does not implicitly destroy its members. |
7062 | if (RD->isUnion()) |
7063 | return true; |
7064 | |
7065 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
7066 | |
7067 | // We don't have a good way to iterate fields in reverse, so collect all the |
7068 | // fields first and then walk them backwards. |
7069 | SmallVector<FieldDecl*, 16> Fields(RD->fields()); |
7070 | for (const FieldDecl *FD : llvm::reverse(Fields)) { |
7071 | if (FD->isUnnamedBitField()) |
7072 | continue; |
7073 | |
7074 | LValue Subobject = This; |
7075 | if (!HandleLValueMember(Info, &LocE, Subobject, FD, &Layout)) |
7076 | return false; |
7077 | |
7078 | APValue *SubobjectValue = &Value.getStructField(FD->getFieldIndex()); |
7079 | if (!HandleDestructionImpl(Info, CallRange, Subobject, *SubobjectValue, |
7080 | FD->getType())) |
7081 | return false; |
7082 | } |
7083 | |
7084 | if (BasesLeft != 0) |
7085 | EvalObj.startedDestroyingBases(); |
7086 | |
7087 | // Destroy base classes in reverse order. |
7088 | for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->bases())) { |
7089 | --BasesLeft; |
7090 | |
7091 | QualType BaseType = Base.getType(); |
7092 | LValue Subobject = This; |
7093 | if (!HandleLValueDirectBase(Info, &LocE, Subobject, RD, |
7094 | BaseType->getAsCXXRecordDecl(), &Layout)) |
7095 | return false; |
7096 | |
7097 | APValue *SubobjectValue = &Value.getStructBase(i: BasesLeft); |
7098 | if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue, |
7099 | T: BaseType)) |
7100 | return false; |
7101 | } |
7102 | assert(BasesLeft == 0 && "NumBases was wrong?"); |
7103 | |
7104 | // The period of destruction ends now. The object is gone. |
7105 | Value = APValue(); |
7106 | return true; |
7107 | } |
7108 | |
7109 | namespace { |
7110 | struct DestroyObjectHandler { |
7111 | EvalInfo &Info; |
7112 | const Expr *E; |
7113 | const LValue &This; |
7114 | const AccessKinds AccessKind; |
7115 | |
7116 | typedef bool result_type; |
7117 | bool failed() { return false; } |
7118 | bool found(APValue &Subobj, QualType SubobjType) { |
7119 | return HandleDestructionImpl(Info, E->getSourceRange(), This, Subobj, |
7120 | SubobjType); |
7121 | } |
7122 | bool found(APSInt &Value, QualType SubobjType) { |
7123 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
7124 | return false; |
7125 | } |
7126 | bool found(APFloat &Value, QualType SubobjType) { |
7127 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
7128 | return false; |
7129 | } |
7130 | }; |
7131 | } |
7132 | |
7133 | /// Perform a destructor or pseudo-destructor call on the given object, which |
7134 | /// might in general not be a complete object. |
7135 | static bool HandleDestruction(EvalInfo &Info, const Expr *E, |
7136 | const LValue &This, QualType ThisType) { |
7137 | CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Destroy, LVal: This, LValType: ThisType); |
7138 | DestroyObjectHandler Handler = {.Info: Info, .E: E, .This: This, .AccessKind: AK_Destroy}; |
7139 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
7140 | } |
7141 | |
7142 | /// Destroy and end the lifetime of the given complete object. |
7143 | static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc, |
7144 | APValue::LValueBase LVBase, APValue &Value, |
7145 | QualType T) { |
7146 | // If we've had an unmodeled side-effect, we can't rely on mutable state |
7147 | // (such as the object we're about to destroy) being correct. |
7148 | if (Info.EvalStatus.HasSideEffects) |
7149 | return false; |
7150 | |
7151 | LValue LV; |
7152 | LV.set(B: {LVBase}); |
7153 | return HandleDestructionImpl(Info, CallRange: Loc, This: LV, Value, T); |
7154 | } |
7155 | |
7156 | /// Perform a call to 'operator new' or to `__builtin_operator_new'. |
7157 | static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E, |
7158 | LValue &Result) { |
7159 | if (Info.checkingPotentialConstantExpression() || |
7160 | Info.SpeculativeEvaluationDepth) |
7161 | return false; |
7162 | |
7163 | // This is permitted only within a call to std::allocator<T>::allocate. |
7164 | auto Caller = Info.getStdAllocatorCaller(FnName: "allocate"); |
7165 | if (!Caller) { |
7166 | Info.FFDiag(E->getExprLoc(), Info.getLangOpts().CPlusPlus20 |
7167 | ? diag::note_constexpr_new_untyped |
7168 | : diag::note_constexpr_new); |
7169 | return false; |
7170 | } |
7171 | |
7172 | QualType ElemType = Caller.ElemType; |
7173 | if (ElemType->isIncompleteType() || ElemType->isFunctionType()) { |
7174 | Info.FFDiag(E->getExprLoc(), |
7175 | diag::note_constexpr_new_not_complete_object_type) |
7176 | << (ElemType->isIncompleteType() ? 0 : 1) << ElemType; |
7177 | return false; |
7178 | } |
7179 | |
7180 | APSInt ByteSize; |
7181 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: ByteSize, Info)) |
7182 | return false; |
7183 | bool IsNothrow = false; |
7184 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) { |
7185 | EvaluateIgnoredValue(Info, E: E->getArg(Arg: I)); |
7186 | IsNothrow |= E->getType()->isNothrowT(); |
7187 | } |
7188 | |
7189 | CharUnits ElemSize; |
7190 | if (!HandleSizeof(Info, E->getExprLoc(), ElemType, ElemSize)) |
7191 | return false; |
7192 | APInt Size, Remainder; |
7193 | APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity()); |
7194 | APInt::udivrem(LHS: ByteSize, RHS: ElemSizeAP, Quotient&: Size, Remainder); |
7195 | if (Remainder != 0) { |
7196 | // This likely indicates a bug in the implementation of 'std::allocator'. |
7197 | Info.FFDiag(E->getExprLoc(), diag::note_constexpr_operator_new_bad_size) |
7198 | << ByteSize << APSInt(ElemSizeAP, true) << ElemType; |
7199 | return false; |
7200 | } |
7201 | |
7202 | if (!Info.CheckArraySize(Loc: E->getBeginLoc(), BitWidth: ByteSize.getActiveBits(), |
7203 | ElemCount: Size.getZExtValue(), /*Diag=*/!IsNothrow)) { |
7204 | if (IsNothrow) { |
7205 | Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType()); |
7206 | return true; |
7207 | } |
7208 | return false; |
7209 | } |
7210 | |
7211 | QualType AllocType = Info.Ctx.getConstantArrayType( |
7212 | EltTy: ElemType, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
7213 | APValue *Val = Info.createHeapAlloc(E: Caller.Call, T: AllocType, LV&: Result); |
7214 | *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue()); |
7215 | Result.addArray(Info, E, cast<ConstantArrayType>(Val&: AllocType)); |
7216 | return true; |
7217 | } |
7218 | |
7219 | static bool hasVirtualDestructor(QualType T) { |
7220 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
7221 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
7222 | return DD->isVirtual(); |
7223 | return false; |
7224 | } |
7225 | |
7226 | static const FunctionDecl *getVirtualOperatorDelete(QualType T) { |
7227 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
7228 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
7229 | return DD->isVirtual() ? DD->getOperatorDelete() : nullptr; |
7230 | return nullptr; |
7231 | } |
7232 | |
7233 | /// Check that the given object is a suitable pointer to a heap allocation that |
7234 | /// still exists and is of the right kind for the purpose of a deletion. |
7235 | /// |
7236 | /// On success, returns the heap allocation to deallocate. On failure, produces |
7237 | /// a diagnostic and returns std::nullopt. |
7238 | static std::optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E, |
7239 | const LValue &Pointer, |
7240 | DynAlloc::Kind DeallocKind) { |
7241 | auto PointerAsString = [&] { |
7242 | return Pointer.toString(Info.Ctx, Info.Ctx.VoidPtrTy); |
7243 | }; |
7244 | |
7245 | DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>(); |
7246 | if (!DA) { |
7247 | Info.FFDiag(E, diag::note_constexpr_delete_not_heap_alloc) |
7248 | << PointerAsString(); |
7249 | if (Pointer.Base) |
7250 | NoteLValueLocation(Info, Base: Pointer.Base); |
7251 | return std::nullopt; |
7252 | } |
7253 | |
7254 | std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA); |
7255 | if (!Alloc) { |
7256 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
7257 | return std::nullopt; |
7258 | } |
7259 | |
7260 | if (DeallocKind != (*Alloc)->getKind()) { |
7261 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
7262 | Info.FFDiag(E, diag::note_constexpr_new_delete_mismatch) |
7263 | << DeallocKind << (*Alloc)->getKind() << AllocType; |
7264 | NoteLValueLocation(Info, Base: Pointer.Base); |
7265 | return std::nullopt; |
7266 | } |
7267 | |
7268 | bool Subobject = false; |
7269 | if (DeallocKind == DynAlloc::New) { |
7270 | Subobject = Pointer.Designator.MostDerivedPathLength != 0 || |
7271 | Pointer.Designator.isOnePastTheEnd(); |
7272 | } else { |
7273 | Subobject = Pointer.Designator.Entries.size() != 1 || |
7274 | Pointer.Designator.Entries[0].getAsArrayIndex() != 0; |
7275 | } |
7276 | if (Subobject) { |
7277 | Info.FFDiag(E, diag::note_constexpr_delete_subobject) |
7278 | << PointerAsString() << Pointer.Designator.isOnePastTheEnd(); |
7279 | return std::nullopt; |
7280 | } |
7281 | |
7282 | return Alloc; |
7283 | } |
7284 | |
7285 | // Perform a call to 'operator delete' or '__builtin_operator_delete'. |
7286 | static bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) { |
7287 | if (Info.checkingPotentialConstantExpression() || |
7288 | Info.SpeculativeEvaluationDepth) |
7289 | return false; |
7290 | |
7291 | // This is permitted only within a call to std::allocator<T>::deallocate. |
7292 | if (!Info.getStdAllocatorCaller(FnName: "deallocate")) { |
7293 | Info.FFDiag(E->getExprLoc()); |
7294 | return true; |
7295 | } |
7296 | |
7297 | LValue Pointer; |
7298 | if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Pointer, Info)) |
7299 | return false; |
7300 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) |
7301 | EvaluateIgnoredValue(Info, E: E->getArg(Arg: I)); |
7302 | |
7303 | if (Pointer.Designator.Invalid) |
7304 | return false; |
7305 | |
7306 | // Deleting a null pointer would have no effect, but it's not permitted by |
7307 | // std::allocator<T>::deallocate's contract. |
7308 | if (Pointer.isNullPointer()) { |
7309 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_deallocate_null); |
7310 | return true; |
7311 | } |
7312 | |
7313 | if (!CheckDeleteKind(Info, E, Pointer, DynAlloc::StdAllocator)) |
7314 | return false; |
7315 | |
7316 | Info.HeapAllocs.erase(x: Pointer.Base.get<DynamicAllocLValue>()); |
7317 | return true; |
7318 | } |
7319 | |
7320 | //===----------------------------------------------------------------------===// |
7321 | // Generic Evaluation |
7322 | //===----------------------------------------------------------------------===// |
7323 | namespace { |
7324 | |
7325 | class BitCastBuffer { |
7326 | // FIXME: We're going to need bit-level granularity when we support |
7327 | // bit-fields. |
7328 | // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but |
7329 | // we don't support a host or target where that is the case. Still, we should |
7330 | // use a more generic type in case we ever do. |
7331 | SmallVector<std::optional<unsigned char>, 32> Bytes; |
7332 | |
7333 | static_assert(std::numeric_limits<unsigned char>::digits >= 8, |
7334 | "Need at least 8 bit unsigned char"); |
7335 | |
7336 | bool TargetIsLittleEndian; |
7337 | |
7338 | public: |
7339 | BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian) |
7340 | : Bytes(Width.getQuantity()), |
7341 | TargetIsLittleEndian(TargetIsLittleEndian) {} |
7342 | |
7343 | [[nodiscard]] bool readObject(CharUnits Offset, CharUnits Width, |
7344 | SmallVectorImpl<unsigned char> &Output) const { |
7345 | for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) { |
7346 | // If a byte of an integer is uninitialized, then the whole integer is |
7347 | // uninitialized. |
7348 | if (!Bytes[I.getQuantity()]) |
7349 | return false; |
7350 | Output.push_back(Elt: *Bytes[I.getQuantity()]); |
7351 | } |
7352 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
7353 | std::reverse(first: Output.begin(), last: Output.end()); |
7354 | return true; |
7355 | } |
7356 | |
7357 | void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) { |
7358 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
7359 | std::reverse(first: Input.begin(), last: Input.end()); |
7360 | |
7361 | size_t Index = 0; |
7362 | for (unsigned char Byte : Input) { |
7363 | assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?"); |
7364 | Bytes[Offset.getQuantity() + Index] = Byte; |
7365 | ++Index; |
7366 | } |
7367 | } |
7368 | |
7369 | size_t size() { return Bytes.size(); } |
7370 | }; |
7371 | |
7372 | /// Traverse an APValue to produce an BitCastBuffer, emulating how the current |
7373 | /// target would represent the value at runtime. |
7374 | class APValueToBufferConverter { |
7375 | EvalInfo &Info; |
7376 | BitCastBuffer Buffer; |
7377 | const CastExpr *BCE; |
7378 | |
7379 | APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth, |
7380 | const CastExpr *BCE) |
7381 | : Info(Info), |
7382 | Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()), |
7383 | BCE(BCE) {} |
7384 | |
7385 | bool visit(const APValue &Val, QualType Ty) { |
7386 | return visit(Val, Ty, Offset: CharUnits::fromQuantity(Quantity: 0)); |
7387 | } |
7388 | |
7389 | // Write out Val with type Ty into Buffer starting at Offset. |
7390 | bool visit(const APValue &Val, QualType Ty, CharUnits Offset) { |
7391 | assert((size_t)Offset.getQuantity() <= Buffer.size()); |
7392 | |
7393 | // As a special case, nullptr_t has an indeterminate value. |
7394 | if (Ty->isNullPtrType()) |
7395 | return true; |
7396 | |
7397 | // Dig through Src to find the byte at SrcOffset. |
7398 | switch (Val.getKind()) { |
7399 | case APValue::Indeterminate: |
7400 | case APValue::None: |
7401 | return true; |
7402 | |
7403 | case APValue::Int: |
7404 | return visitInt(Val: Val.getInt(), Ty, Offset); |
7405 | case APValue::Float: |
7406 | return visitFloat(Val: Val.getFloat(), Ty, Offset); |
7407 | case APValue::Array: |
7408 | return visitArray(Val, Ty, Offset); |
7409 | case APValue::Struct: |
7410 | return visitRecord(Val, Ty, Offset); |
7411 | case APValue::Vector: |
7412 | return visitVector(Val, Ty, Offset); |
7413 | |
7414 | case APValue::ComplexInt: |
7415 | case APValue::ComplexFloat: |
7416 | return visitComplex(Val, Ty, Offset); |
7417 | case APValue::FixedPoint: |
7418 | // FIXME: We should support these. |
7419 | |
7420 | case APValue::Union: |
7421 | case APValue::MemberPointer: |
7422 | case APValue::AddrLabelDiff: { |
7423 | Info.FFDiag(BCE->getBeginLoc(), |
7424 | diag::note_constexpr_bit_cast_unsupported_type) |
7425 | << Ty; |
7426 | return false; |
7427 | } |
7428 | |
7429 | case APValue::LValue: |
7430 | llvm_unreachable("LValue subobject in bit_cast?"); |
7431 | } |
7432 | llvm_unreachable("Unhandled APValue::ValueKind"); |
7433 | } |
7434 | |
7435 | bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) { |
7436 | const RecordDecl *RD = Ty->getAsRecordDecl(); |
7437 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
7438 | |
7439 | // Visit the base classes. |
7440 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) { |
7441 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
7442 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
7443 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
7444 | const APValue &Base = Val.getStructBase(i: I); |
7445 | |
7446 | // Can happen in error cases. |
7447 | if (!Base.isStruct()) |
7448 | return false; |
7449 | |
7450 | if (!visitRecord(Val: Base, Ty: BS.getType(), |
7451 | Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset)) |
7452 | return false; |
7453 | } |
7454 | } |
7455 | |
7456 | // Visit the fields. |
7457 | unsigned FieldIdx = 0; |
7458 | for (FieldDecl *FD : RD->fields()) { |
7459 | if (FD->isBitField()) { |
7460 | Info.FFDiag(BCE->getBeginLoc(), |
7461 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
7462 | return false; |
7463 | } |
7464 | |
7465 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx); |
7466 | |
7467 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 && |
7468 | "only bit-fields can have sub-char alignment"); |
7469 | CharUnits FieldOffset = |
7470 | Info.Ctx.toCharUnitsFromBits(BitSize: FieldOffsetBits) + Offset; |
7471 | QualType FieldTy = FD->getType(); |
7472 | if (!visit(Val: Val.getStructField(i: FieldIdx), Ty: FieldTy, Offset: FieldOffset)) |
7473 | return false; |
7474 | ++FieldIdx; |
7475 | } |
7476 | |
7477 | return true; |
7478 | } |
7479 | |
7480 | bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) { |
7481 | const auto *CAT = |
7482 | dyn_cast_or_null<ConstantArrayType>(Ty->getAsArrayTypeUnsafe()); |
7483 | if (!CAT) |
7484 | return false; |
7485 | |
7486 | CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(CAT->getElementType()); |
7487 | unsigned NumInitializedElts = Val.getArrayInitializedElts(); |
7488 | unsigned ArraySize = Val.getArraySize(); |
7489 | // First, initialize the initialized elements. |
7490 | for (unsigned I = 0; I != NumInitializedElts; ++I) { |
7491 | const APValue &SubObj = Val.getArrayInitializedElt(I); |
7492 | if (!visit(SubObj, CAT->getElementType(), Offset + I * ElemWidth)) |
7493 | return false; |
7494 | } |
7495 | |
7496 | // Next, initialize the rest of the array using the filler. |
7497 | if (Val.hasArrayFiller()) { |
7498 | const APValue &Filler = Val.getArrayFiller(); |
7499 | for (unsigned I = NumInitializedElts; I != ArraySize; ++I) { |
7500 | if (!visit(Filler, CAT->getElementType(), Offset + I * ElemWidth)) |
7501 | return false; |
7502 | } |
7503 | } |
7504 | |
7505 | return true; |
7506 | } |
7507 | |
7508 | bool visitComplex(const APValue &Val, QualType Ty, CharUnits Offset) { |
7509 | const ComplexType *ComplexTy = Ty->castAs<ComplexType>(); |
7510 | QualType EltTy = ComplexTy->getElementType(); |
7511 | CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy); |
7512 | bool IsInt = Val.isComplexInt(); |
7513 | |
7514 | if (IsInt) { |
7515 | if (!visitInt(Val: Val.getComplexIntReal(), Ty: EltTy, |
7516 | Offset: Offset + (0 * EltSizeChars))) |
7517 | return false; |
7518 | if (!visitInt(Val: Val.getComplexIntImag(), Ty: EltTy, |
7519 | Offset: Offset + (1 * EltSizeChars))) |
7520 | return false; |
7521 | } else { |
7522 | if (!visitFloat(Val: Val.getComplexFloatReal(), Ty: EltTy, |
7523 | Offset: Offset + (0 * EltSizeChars))) |
7524 | return false; |
7525 | if (!visitFloat(Val: Val.getComplexFloatImag(), Ty: EltTy, |
7526 | Offset: Offset + (1 * EltSizeChars))) |
7527 | return false; |
7528 | } |
7529 | |
7530 | return true; |
7531 | } |
7532 | |
7533 | bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) { |
7534 | const VectorType *VTy = Ty->castAs<VectorType>(); |
7535 | QualType EltTy = VTy->getElementType(); |
7536 | unsigned NElts = VTy->getNumElements(); |
7537 | |
7538 | if (VTy->isPackedVectorBoolType(Info.Ctx)) { |
7539 | // Special handling for OpenCL bool vectors: |
7540 | // Since these vectors are stored as packed bits, but we can't write |
7541 | // individual bits to the BitCastBuffer, we'll buffer all of the elements |
7542 | // together into an appropriately sized APInt and write them all out at |
7543 | // once. Because we don't accept vectors where NElts * EltSize isn't a |
7544 | // multiple of the char size, there will be no padding space, so we don't |
7545 | // have to worry about writing data which should have been left |
7546 | // uninitialized. |
7547 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
7548 | |
7549 | llvm::APInt Res = llvm::APInt::getZero(numBits: NElts); |
7550 | for (unsigned I = 0; I < NElts; ++I) { |
7551 | const llvm::APSInt &EltAsInt = Val.getVectorElt(I).getInt(); |
7552 | assert(EltAsInt.isUnsigned() && EltAsInt.getBitWidth() == 1 && |
7553 | "bool vector element must be 1-bit unsigned integer!"); |
7554 | |
7555 | Res.insertBits(SubBits: EltAsInt, bitPosition: BigEndian ? (NElts - I - 1) : I); |
7556 | } |
7557 | |
7558 | SmallVector<uint8_t, 8> Bytes(NElts / 8); |
7559 | llvm::StoreIntToMemory(IntVal: Res, Dst: &*Bytes.begin(), StoreBytes: NElts / 8); |
7560 | Buffer.writeObject(Offset, Input&: Bytes); |
7561 | } else { |
7562 | // Iterate over each of the elements and write them out to the buffer at |
7563 | // the appropriate offset. |
7564 | CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy); |
7565 | for (unsigned I = 0; I < NElts; ++I) { |
7566 | if (!visit(Val: Val.getVectorElt(I), Ty: EltTy, Offset: Offset + I * EltSizeChars)) |
7567 | return false; |
7568 | } |
7569 | } |
7570 | |
7571 | return true; |
7572 | } |
7573 | |
7574 | bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) { |
7575 | APSInt AdjustedVal = Val; |
7576 | unsigned Width = AdjustedVal.getBitWidth(); |
7577 | if (Ty->isBooleanType()) { |
7578 | Width = Info.Ctx.getTypeSize(T: Ty); |
7579 | AdjustedVal = AdjustedVal.extend(width: Width); |
7580 | } |
7581 | |
7582 | SmallVector<uint8_t, 8> Bytes(Width / 8); |
7583 | llvm::StoreIntToMemory(IntVal: AdjustedVal, Dst: &*Bytes.begin(), StoreBytes: Width / 8); |
7584 | Buffer.writeObject(Offset, Input&: Bytes); |
7585 | return true; |
7586 | } |
7587 | |
7588 | bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) { |
7589 | APSInt AsInt(Val.bitcastToAPInt()); |
7590 | return visitInt(Val: AsInt, Ty, Offset); |
7591 | } |
7592 | |
7593 | public: |
7594 | static std::optional<BitCastBuffer> |
7595 | convert(EvalInfo &Info, const APValue &Src, const CastExpr *BCE) { |
7596 | CharUnits DstSize = Info.Ctx.getTypeSizeInChars(BCE->getType()); |
7597 | APValueToBufferConverter Converter(Info, DstSize, BCE); |
7598 | if (!Converter.visit(Val: Src, Ty: BCE->getSubExpr()->getType())) |
7599 | return std::nullopt; |
7600 | return Converter.Buffer; |
7601 | } |
7602 | }; |
7603 | |
7604 | /// Write an BitCastBuffer into an APValue. |
7605 | class BufferToAPValueConverter { |
7606 | EvalInfo &Info; |
7607 | const BitCastBuffer &Buffer; |
7608 | const CastExpr *BCE; |
7609 | |
7610 | BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer, |
7611 | const CastExpr *BCE) |
7612 | : Info(Info), Buffer(Buffer), BCE(BCE) {} |
7613 | |
7614 | // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast |
7615 | // with an invalid type, so anything left is a deficiency on our part (FIXME). |
7616 | // Ideally this will be unreachable. |
7617 | std::nullopt_t unsupportedType(QualType Ty) { |
7618 | Info.FFDiag(BCE->getBeginLoc(), |
7619 | diag::note_constexpr_bit_cast_unsupported_type) |
7620 | << Ty; |
7621 | return std::nullopt; |
7622 | } |
7623 | |
7624 | std::nullopt_t unrepresentableValue(QualType Ty, const APSInt &Val) { |
7625 | Info.FFDiag(BCE->getBeginLoc(), |
7626 | diag::note_constexpr_bit_cast_unrepresentable_value) |
7627 | << Ty << toString(Val, /*Radix=*/10); |
7628 | return std::nullopt; |
7629 | } |
7630 | |
7631 | std::optional<APValue> visit(const BuiltinType *T, CharUnits Offset, |
7632 | const EnumType *EnumSugar = nullptr) { |
7633 | if (T->isNullPtrType()) { |
7634 | uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QT: QualType(T, 0)); |
7635 | return APValue((Expr *)nullptr, |
7636 | /*Offset=*/CharUnits::fromQuantity(Quantity: NullValue), |
7637 | APValue::NoLValuePath{}, /*IsNullPtr=*/true); |
7638 | } |
7639 | |
7640 | CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T); |
7641 | |
7642 | // Work around floating point types that contain unused padding bytes. This |
7643 | // is really just `long double` on x86, which is the only fundamental type |
7644 | // with padding bytes. |
7645 | if (T->isRealFloatingType()) { |
7646 | const llvm::fltSemantics &Semantics = |
7647 | Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0)); |
7648 | unsigned NumBits = llvm::APFloatBase::getSizeInBits(Sem: Semantics); |
7649 | assert(NumBits % 8 == 0); |
7650 | CharUnits NumBytes = CharUnits::fromQuantity(Quantity: NumBits / 8); |
7651 | if (NumBytes != SizeOf) |
7652 | SizeOf = NumBytes; |
7653 | } |
7654 | |
7655 | SmallVector<uint8_t, 8> Bytes; |
7656 | if (!Buffer.readObject(Offset, Width: SizeOf, Output&: Bytes)) { |
7657 | // If this is std::byte or unsigned char, then its okay to store an |
7658 | // indeterminate value. |
7659 | bool IsStdByte = EnumSugar && EnumSugar->isStdByteType(); |
7660 | bool IsUChar = |
7661 | !EnumSugar && (T->isSpecificBuiltinType(BuiltinType::UChar) || |
7662 | T->isSpecificBuiltinType(BuiltinType::Char_U)); |
7663 | if (!IsStdByte && !IsUChar) { |
7664 | QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0); |
7665 | Info.FFDiag(BCE->getExprLoc(), |
7666 | diag::note_constexpr_bit_cast_indet_dest) |
7667 | << DisplayType << Info.Ctx.getLangOpts().CharIsSigned; |
7668 | return std::nullopt; |
7669 | } |
7670 | |
7671 | return APValue::IndeterminateValue(); |
7672 | } |
7673 | |
7674 | APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true); |
7675 | llvm::LoadIntFromMemory(IntVal&: Val, Src: &*Bytes.begin(), LoadBytes: Bytes.size()); |
7676 | |
7677 | if (T->isIntegralOrEnumerationType()) { |
7678 | Val.setIsSigned(T->isSignedIntegerOrEnumerationType()); |
7679 | |
7680 | unsigned IntWidth = Info.Ctx.getIntWidth(T: QualType(T, 0)); |
7681 | if (IntWidth != Val.getBitWidth()) { |
7682 | APSInt Truncated = Val.trunc(width: IntWidth); |
7683 | if (Truncated.extend(width: Val.getBitWidth()) != Val) |
7684 | return unrepresentableValue(Ty: QualType(T, 0), Val); |
7685 | Val = Truncated; |
7686 | } |
7687 | |
7688 | return APValue(Val); |
7689 | } |
7690 | |
7691 | if (T->isRealFloatingType()) { |
7692 | const llvm::fltSemantics &Semantics = |
7693 | Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0)); |
7694 | return APValue(APFloat(Semantics, Val)); |
7695 | } |
7696 | |
7697 | return unsupportedType(Ty: QualType(T, 0)); |
7698 | } |
7699 | |
7700 | std::optional<APValue> visit(const RecordType *RTy, CharUnits Offset) { |
7701 | const RecordDecl *RD = RTy->getAsRecordDecl(); |
7702 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
7703 | |
7704 | unsigned NumBases = 0; |
7705 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
7706 | NumBases = CXXRD->getNumBases(); |
7707 | |
7708 | APValue ResultVal(APValue::UninitStruct(), NumBases, |
7709 | std::distance(RD->field_begin(), RD->field_end())); |
7710 | |
7711 | // Visit the base classes. |
7712 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
7713 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
7714 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
7715 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
7716 | |
7717 | std::optional<APValue> SubObj = visitType( |
7718 | BS.getType(), Layout.getBaseClassOffset(BaseDecl) + Offset); |
7719 | if (!SubObj) |
7720 | return std::nullopt; |
7721 | ResultVal.getStructBase(i: I) = *SubObj; |
7722 | } |
7723 | } |
7724 | |
7725 | // Visit the fields. |
7726 | unsigned FieldIdx = 0; |
7727 | for (FieldDecl *FD : RD->fields()) { |
7728 | // FIXME: We don't currently support bit-fields. A lot of the logic for |
7729 | // this is in CodeGen, so we need to factor it around. |
7730 | if (FD->isBitField()) { |
7731 | Info.FFDiag(BCE->getBeginLoc(), |
7732 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
7733 | return std::nullopt; |
7734 | } |
7735 | |
7736 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx); |
7737 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0); |
7738 | |
7739 | CharUnits FieldOffset = |
7740 | CharUnits::fromQuantity(FieldOffsetBits / Info.Ctx.getCharWidth()) + |
7741 | Offset; |
7742 | QualType FieldTy = FD->getType(); |
7743 | std::optional<APValue> SubObj = visitType(FieldTy, FieldOffset); |
7744 | if (!SubObj) |
7745 | return std::nullopt; |
7746 | ResultVal.getStructField(FieldIdx) = *SubObj; |
7747 | ++FieldIdx; |
7748 | } |
7749 | |
7750 | return ResultVal; |
7751 | } |
7752 | |
7753 | std::optional<APValue> visit(const EnumType *Ty, CharUnits Offset) { |
7754 | QualType RepresentationType = Ty->getDecl()->getIntegerType(); |
7755 | assert(!RepresentationType.isNull() && |
7756 | "enum forward decl should be caught by Sema"); |
7757 | const auto *AsBuiltin = |
7758 | RepresentationType.getCanonicalType()->castAs<BuiltinType>(); |
7759 | // Recurse into the underlying type. Treat std::byte transparently as |
7760 | // unsigned char. |
7761 | return visit(AsBuiltin, Offset, /*EnumTy=*/Ty); |
7762 | } |
7763 | |
7764 | std::optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) { |
7765 | size_t Size = Ty->getLimitedSize(); |
7766 | CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(Ty->getElementType()); |
7767 | |
7768 | APValue ArrayValue(APValue::UninitArray(), Size, Size); |
7769 | for (size_t I = 0; I != Size; ++I) { |
7770 | std::optional<APValue> ElementValue = |
7771 | visitType(Ty->getElementType(), Offset + I * ElementWidth); |
7772 | if (!ElementValue) |
7773 | return std::nullopt; |
7774 | ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue); |
7775 | } |
7776 | |
7777 | return ArrayValue; |
7778 | } |
7779 | |
7780 | std::optional<APValue> visit(const ComplexType *Ty, CharUnits Offset) { |
7781 | QualType ElementType = Ty->getElementType(); |
7782 | CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: ElementType); |
7783 | bool IsInt = ElementType->isIntegerType(); |
7784 | |
7785 | std::optional<APValue> Values[2]; |
7786 | for (unsigned I = 0; I != 2; ++I) { |
7787 | Values[I] = visitType(Ty->getElementType(), Offset + I * ElementWidth); |
7788 | if (!Values[I]) |
7789 | return std::nullopt; |
7790 | } |
7791 | |
7792 | if (IsInt) |
7793 | return APValue(Values[0]->getInt(), Values[1]->getInt()); |
7794 | return APValue(Values[0]->getFloat(), Values[1]->getFloat()); |
7795 | } |
7796 | |
7797 | std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) { |
7798 | QualType EltTy = VTy->getElementType(); |
7799 | unsigned NElts = VTy->getNumElements(); |
7800 | unsigned EltSize = |
7801 | VTy->isPackedVectorBoolType(Info.Ctx) ? 1 : Info.Ctx.getTypeSize(T: EltTy); |
7802 | |
7803 | SmallVector<APValue, 4> Elts; |
7804 | Elts.reserve(N: NElts); |
7805 | if (VTy->isPackedVectorBoolType(Info.Ctx)) { |
7806 | // Special handling for OpenCL bool vectors: |
7807 | // Since these vectors are stored as packed bits, but we can't read |
7808 | // individual bits from the BitCastBuffer, we'll buffer all of the |
7809 | // elements together into an appropriately sized APInt and write them all |
7810 | // out at once. Because we don't accept vectors where NElts * EltSize |
7811 | // isn't a multiple of the char size, there will be no padding space, so |
7812 | // we don't have to worry about reading any padding data which didn't |
7813 | // actually need to be accessed. |
7814 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
7815 | |
7816 | SmallVector<uint8_t, 8> Bytes; |
7817 | Bytes.reserve(N: NElts / 8); |
7818 | if (!Buffer.readObject(Offset, Width: CharUnits::fromQuantity(Quantity: NElts / 8), Output&: Bytes)) |
7819 | return std::nullopt; |
7820 | |
7821 | APSInt SValInt(NElts, true); |
7822 | llvm::LoadIntFromMemory(IntVal&: SValInt, Src: &*Bytes.begin(), LoadBytes: Bytes.size()); |
7823 | |
7824 | for (unsigned I = 0; I < NElts; ++I) { |
7825 | llvm::APInt Elt = |
7826 | SValInt.extractBits(numBits: 1, bitPosition: (BigEndian ? NElts - I - 1 : I) * EltSize); |
7827 | Elts.emplace_back( |
7828 | APSInt(std::move(Elt), !EltTy->isSignedIntegerType())); |
7829 | } |
7830 | } else { |
7831 | // Iterate over each of the elements and read them from the buffer at |
7832 | // the appropriate offset. |
7833 | CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy); |
7834 | for (unsigned I = 0; I < NElts; ++I) { |
7835 | std::optional<APValue> EltValue = |
7836 | visitType(EltTy, Offset + I * EltSizeChars); |
7837 | if (!EltValue) |
7838 | return std::nullopt; |
7839 | Elts.push_back(std::move(*EltValue)); |
7840 | } |
7841 | } |
7842 | |
7843 | return APValue(Elts.data(), Elts.size()); |
7844 | } |
7845 | |
7846 | std::optional<APValue> visit(const Type *Ty, CharUnits Offset) { |
7847 | return unsupportedType(Ty: QualType(Ty, 0)); |
7848 | } |
7849 | |
7850 | std::optional<APValue> visitType(QualType Ty, CharUnits Offset) { |
7851 | QualType Can = Ty.getCanonicalType(); |
7852 | |
7853 | switch (Can->getTypeClass()) { |
7854 | #define TYPE(Class, Base) \ |
7855 | case Type::Class: \ |
7856 | return visit(cast<Class##Type>(Can.getTypePtr()), Offset); |
7857 | #define ABSTRACT_TYPE(Class, Base) |
7858 | #define NON_CANONICAL_TYPE(Class, Base) \ |
7859 | case Type::Class: \ |
7860 | llvm_unreachable("non-canonical type should be impossible!"); |
7861 | #define DEPENDENT_TYPE(Class, Base) \ |
7862 | case Type::Class: \ |
7863 | llvm_unreachable( \ |
7864 | "dependent types aren't supported in the constant evaluator!"); |
7865 | #define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \ |
7866 | case Type::Class: \ |
7867 | llvm_unreachable("either dependent or not canonical!"); |
7868 | #include "clang/AST/TypeNodes.inc" |
7869 | } |
7870 | llvm_unreachable("Unhandled Type::TypeClass"); |
7871 | } |
7872 | |
7873 | public: |
7874 | // Pull out a full value of type DstType. |
7875 | static std::optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer, |
7876 | const CastExpr *BCE) { |
7877 | BufferToAPValueConverter Converter(Info, Buffer, BCE); |
7878 | return Converter.visitType(BCE->getType(), CharUnits::fromQuantity(Quantity: 0)); |
7879 | } |
7880 | }; |
7881 | |
7882 | static bool checkBitCastConstexprEligibilityType(SourceLocation Loc, |
7883 | QualType Ty, EvalInfo *Info, |
7884 | const ASTContext &Ctx, |
7885 | bool CheckingDest) { |
7886 | Ty = Ty.getCanonicalType(); |
7887 | |
7888 | auto diag = [&](int Reason) { |
7889 | if (Info) |
7890 | Info->FFDiag(Loc, diag::note_constexpr_bit_cast_invalid_type) |
7891 | << CheckingDest << (Reason == 4) << Reason; |
7892 | return false; |
7893 | }; |
7894 | auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) { |
7895 | if (Info) |
7896 | Info->Note(NoteLoc, diag::note_constexpr_bit_cast_invalid_subtype) |
7897 | << NoteTy << Construct << Ty; |
7898 | return false; |
7899 | }; |
7900 | |
7901 | if (Ty->isUnionType()) |
7902 | return diag(0); |
7903 | if (Ty->isPointerType()) |
7904 | return diag(1); |
7905 | if (Ty->isMemberPointerType()) |
7906 | return diag(2); |
7907 | if (Ty.isVolatileQualified()) |
7908 | return diag(3); |
7909 | |
7910 | if (RecordDecl *Record = Ty->getAsRecordDecl()) { |
7911 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Record)) { |
7912 | for (CXXBaseSpecifier &BS : CXXRD->bases()) |
7913 | if (!checkBitCastConstexprEligibilityType(Loc, BS.getType(), Info, Ctx, |
7914 | CheckingDest)) |
7915 | return note(1, BS.getType(), BS.getBeginLoc()); |
7916 | } |
7917 | for (FieldDecl *FD : Record->fields()) { |
7918 | if (FD->getType()->isReferenceType()) |
7919 | return diag(4); |
7920 | if (!checkBitCastConstexprEligibilityType(Loc, FD->getType(), Info, Ctx, |
7921 | CheckingDest)) |
7922 | return note(0, FD->getType(), FD->getBeginLoc()); |
7923 | } |
7924 | } |
7925 | |
7926 | if (Ty->isArrayType() && |
7927 | !checkBitCastConstexprEligibilityType(Loc, Ty: Ctx.getBaseElementType(QT: Ty), |
7928 | Info, Ctx, CheckingDest)) |
7929 | return false; |
7930 | |
7931 | if (const auto *VTy = Ty->getAs<VectorType>()) { |
7932 | QualType EltTy = VTy->getElementType(); |
7933 | unsigned NElts = VTy->getNumElements(); |
7934 | unsigned EltSize = |
7935 | VTy->isPackedVectorBoolType(Ctx) ? 1 : Ctx.getTypeSize(T: EltTy); |
7936 | |
7937 | if ((NElts * EltSize) % Ctx.getCharWidth() != 0) { |
7938 | // The vector's size in bits is not a multiple of the target's byte size, |
7939 | // so its layout is unspecified. For now, we'll simply treat these cases |
7940 | // as unsupported (this should only be possible with OpenCL bool vectors |
7941 | // whose element count isn't a multiple of the byte size). |
7942 | Info->FFDiag(Loc, diag::note_constexpr_bit_cast_invalid_vector) |
7943 | << QualType(VTy, 0) << EltSize << NElts << Ctx.getCharWidth(); |
7944 | return false; |
7945 | } |
7946 | |
7947 | if (EltTy->isRealFloatingType() && |
7948 | &Ctx.getFloatTypeSemantics(T: EltTy) == &APFloat::x87DoubleExtended()) { |
7949 | // The layout for x86_fp80 vectors seems to be handled very inconsistently |
7950 | // by both clang and LLVM, so for now we won't allow bit_casts involving |
7951 | // it in a constexpr context. |
7952 | Info->FFDiag(Loc, diag::note_constexpr_bit_cast_unsupported_type) |
7953 | << EltTy; |
7954 | return false; |
7955 | } |
7956 | } |
7957 | |
7958 | return true; |
7959 | } |
7960 | |
7961 | static bool checkBitCastConstexprEligibility(EvalInfo *Info, |
7962 | const ASTContext &Ctx, |
7963 | const CastExpr *BCE) { |
7964 | bool DestOK = checkBitCastConstexprEligibilityType( |
7965 | BCE->getBeginLoc(), BCE->getType(), Info, Ctx, true); |
7966 | bool SourceOK = DestOK && checkBitCastConstexprEligibilityType( |
7967 | BCE->getBeginLoc(), |
7968 | BCE->getSubExpr()->getType(), Info, Ctx, false); |
7969 | return SourceOK; |
7970 | } |
7971 | |
7972 | static bool handleRValueToRValueBitCast(EvalInfo &Info, APValue &DestValue, |
7973 | const APValue &SourceRValue, |
7974 | const CastExpr *BCE) { |
7975 | assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 && |
7976 | "no host or target supports non 8-bit chars"); |
7977 | |
7978 | if (!checkBitCastConstexprEligibility(Info: &Info, Ctx: Info.Ctx, BCE)) |
7979 | return false; |
7980 | |
7981 | // Read out SourceValue into a char buffer. |
7982 | std::optional<BitCastBuffer> Buffer = |
7983 | APValueToBufferConverter::convert(Info, Src: SourceRValue, BCE); |
7984 | if (!Buffer) |
7985 | return false; |
7986 | |
7987 | // Write out the buffer into a new APValue. |
7988 | std::optional<APValue> MaybeDestValue = |
7989 | BufferToAPValueConverter::convert(Info, *Buffer, BCE); |
7990 | if (!MaybeDestValue) |
7991 | return false; |
7992 | |
7993 | DestValue = std::move(*MaybeDestValue); |
7994 | return true; |
7995 | } |
7996 | |
7997 | static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue, |
7998 | APValue &SourceValue, |
7999 | const CastExpr *BCE) { |
8000 | assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 && |
8001 | "no host or target supports non 8-bit chars"); |
8002 | assert(SourceValue.isLValue() && |
8003 | "LValueToRValueBitcast requires an lvalue operand!"); |
8004 | |
8005 | LValue SourceLValue; |
8006 | APValue SourceRValue; |
8007 | SourceLValue.setFrom(Ctx&: Info.Ctx, V: SourceValue); |
8008 | if (!handleLValueToRValueConversion( |
8009 | Info, BCE, BCE->getSubExpr()->getType().withConst(), SourceLValue, |
8010 | SourceRValue, /*WantObjectRepresentation=*/true)) |
8011 | return false; |
8012 | |
8013 | return handleRValueToRValueBitCast(Info, DestValue, SourceRValue, BCE); |
8014 | } |
8015 | |
8016 | template <class Derived> |
8017 | class ExprEvaluatorBase |
8018 | : public ConstStmtVisitor<Derived, bool> { |
8019 | private: |
8020 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
8021 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
8022 | return getDerived().Success(V, E); |
8023 | } |
8024 | bool DerivedZeroInitialization(const Expr *E) { |
8025 | return getDerived().ZeroInitialization(E); |
8026 | } |
8027 | |
8028 | // Check whether a conditional operator with a non-constant condition is a |
8029 | // potential constant expression. If neither arm is a potential constant |
8030 | // expression, then the conditional operator is not either. |
8031 | template<typename ConditionalOperator> |
8032 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
8033 | assert(Info.checkingPotentialConstantExpression()); |
8034 | |
8035 | // Speculatively evaluate both arms. |
8036 | SmallVector<PartialDiagnosticAt, 8> Diag; |
8037 | { |
8038 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
8039 | StmtVisitorTy::Visit(E->getFalseExpr()); |
8040 | if (Diag.empty()) |
8041 | return; |
8042 | } |
8043 | |
8044 | { |
8045 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
8046 | Diag.clear(); |
8047 | StmtVisitorTy::Visit(E->getTrueExpr()); |
8048 | if (Diag.empty()) |
8049 | return; |
8050 | } |
8051 | |
8052 | Error(E, diag::note_constexpr_conditional_never_const); |
8053 | } |
8054 | |
8055 | |
8056 | template<typename ConditionalOperator> |
8057 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
8058 | bool BoolResult; |
8059 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
8060 | if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) { |
8061 | CheckPotentialConstantConditional(E); |
8062 | return false; |
8063 | } |
8064 | if (Info.noteFailure()) { |
8065 | StmtVisitorTy::Visit(E->getTrueExpr()); |
8066 | StmtVisitorTy::Visit(E->getFalseExpr()); |
8067 | } |
8068 | return false; |
8069 | } |
8070 | |
8071 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
8072 | return StmtVisitorTy::Visit(EvalExpr); |
8073 | } |
8074 | |
8075 | protected: |
8076 | EvalInfo &Info; |
8077 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
8078 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
8079 | |
8080 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
8081 | return Info.CCEDiag(E, DiagId: D); |
8082 | } |
8083 | |
8084 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
8085 | |
8086 | bool IsConstantEvaluatedBuiltinCall(const CallExpr *E) { |
8087 | unsigned BuiltinOp = E->getBuiltinCallee(); |
8088 | return BuiltinOp != 0 && |
8089 | Info.Ctx.BuiltinInfo.isConstantEvaluated(ID: BuiltinOp); |
8090 | } |
8091 | |
8092 | public: |
8093 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
8094 | |
8095 | EvalInfo &getEvalInfo() { return Info; } |
8096 | |
8097 | /// Report an evaluation error. This should only be called when an error is |
8098 | /// first discovered. When propagating an error, just return false. |
8099 | bool Error(const Expr *E, diag::kind D) { |
8100 | Info.FFDiag(E, DiagId: D) << E->getSourceRange(); |
8101 | return false; |
8102 | } |
8103 | bool Error(const Expr *E) { |
8104 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
8105 | } |
8106 | |
8107 | bool VisitStmt(const Stmt *) { |
8108 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
8109 | } |
8110 | bool VisitExpr(const Expr *E) { |
8111 | return Error(E); |
8112 | } |
8113 | |
8114 | bool VisitEmbedExpr(const EmbedExpr *E) { |
8115 | const auto It = E->begin(); |
8116 | return StmtVisitorTy::Visit(*It); |
8117 | } |
8118 | |
8119 | bool VisitPredefinedExpr(const PredefinedExpr *E) { |
8120 | return StmtVisitorTy::Visit(E->getFunctionName()); |
8121 | } |
8122 | bool VisitConstantExpr(const ConstantExpr *E) { |
8123 | if (E->hasAPValueResult()) |
8124 | return DerivedSuccess(V: E->getAPValueResult(), E); |
8125 | |
8126 | return StmtVisitorTy::Visit(E->getSubExpr()); |
8127 | } |
8128 | |
8129 | bool VisitParenExpr(const ParenExpr *E) |
8130 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
8131 | bool VisitUnaryExtension(const UnaryOperator *E) |
8132 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
8133 | bool VisitUnaryPlus(const UnaryOperator *E) |
8134 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
8135 | bool VisitChooseExpr(const ChooseExpr *E) |
8136 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
8137 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
8138 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
8139 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
8140 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
8141 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
8142 | TempVersionRAII RAII(*Info.CurrentCall); |
8143 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
8144 | return StmtVisitorTy::Visit(E->getExpr()); |
8145 | } |
8146 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
8147 | TempVersionRAII RAII(*Info.CurrentCall); |
8148 | // The initializer may not have been parsed yet, or might be erroneous. |
8149 | if (!E->getExpr()) |
8150 | return Error(E); |
8151 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
8152 | return StmtVisitorTy::Visit(E->getExpr()); |
8153 | } |
8154 | |
8155 | bool VisitExprWithCleanups(const ExprWithCleanups *E) { |
8156 | FullExpressionRAII Scope(Info); |
8157 | return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy(); |
8158 | } |
8159 | |
8160 | // Temporaries are registered when created, so we don't care about |
8161 | // CXXBindTemporaryExpr. |
8162 | bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) { |
8163 | return StmtVisitorTy::Visit(E->getSubExpr()); |
8164 | } |
8165 | |
8166 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
8167 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
8168 | << diag::ConstexprInvalidCastKind::Reinterpret; |
8169 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
8170 | } |
8171 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
8172 | if (!Info.Ctx.getLangOpts().CPlusPlus20) |
8173 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
8174 | << diag::ConstexprInvalidCastKind::Dynamic; |
8175 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
8176 | } |
8177 | bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) { |
8178 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
8179 | } |
8180 | |
8181 | bool VisitBinaryOperator(const BinaryOperator *E) { |
8182 | switch (E->getOpcode()) { |
8183 | default: |
8184 | return Error(E); |
8185 | |
8186 | case BO_Comma: |
8187 | VisitIgnoredValue(E: E->getLHS()); |
8188 | return StmtVisitorTy::Visit(E->getRHS()); |
8189 | |
8190 | case BO_PtrMemD: |
8191 | case BO_PtrMemI: { |
8192 | LValue Obj; |
8193 | if (!HandleMemberPointerAccess(Info, BO: E, LV&: Obj)) |
8194 | return false; |
8195 | APValue Result; |
8196 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
8197 | return false; |
8198 | return DerivedSuccess(V: Result, E); |
8199 | } |
8200 | } |
8201 | } |
8202 | |
8203 | bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) { |
8204 | return StmtVisitorTy::Visit(E->getSemanticForm()); |
8205 | } |
8206 | |
8207 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
8208 | // Evaluate and cache the common expression. We treat it as a temporary, |
8209 | // even though it's not quite the same thing. |
8210 | LValue CommonLV; |
8211 | if (!Evaluate(Result&: Info.CurrentCall->createTemporary( |
8212 | E->getOpaqueValue(), |
8213 | getStorageType(Info.Ctx, E->getOpaqueValue()), |
8214 | ScopeKind::FullExpression, CommonLV), |
8215 | Info, E: E->getCommon())) |
8216 | return false; |
8217 | |
8218 | return HandleConditionalOperator(E); |
8219 | } |
8220 | |
8221 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
8222 | bool IsBcpCall = false; |
8223 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
8224 | // the result is a constant expression if it can be folded without |
8225 | // side-effects. This is an important GNU extension. See GCC PR38377 |
8226 | // for discussion. |
8227 | if (const CallExpr *CallCE = |
8228 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
8229 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
8230 | IsBcpCall = true; |
8231 | |
8232 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
8233 | // constant expression; we can't check whether it's potentially foldable. |
8234 | // FIXME: We should instead treat __builtin_constant_p as non-constant if |
8235 | // it would return 'false' in this mode. |
8236 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
8237 | return false; |
8238 | |
8239 | FoldConstant Fold(Info, IsBcpCall); |
8240 | if (!HandleConditionalOperator(E)) { |
8241 | Fold.keepDiagnostics(); |
8242 | return false; |
8243 | } |
8244 | |
8245 | return true; |
8246 | } |
8247 | |
8248 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
8249 | if (APValue *Value = Info.CurrentCall->getCurrentTemporary(Key: E); |
8250 | Value && !Value->isAbsent()) |
8251 | return DerivedSuccess(V: *Value, E); |
8252 | |
8253 | const Expr *Source = E->getSourceExpr(); |
8254 | if (!Source) |
8255 | return Error(E); |
8256 | if (Source == E) { |
8257 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
8258 | return Error(E); |
8259 | } |
8260 | return StmtVisitorTy::Visit(Source); |
8261 | } |
8262 | |
8263 | bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
8264 | for (const Expr *SemE : E->semantics()) { |
8265 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) { |
8266 | // FIXME: We can't handle the case where an OpaqueValueExpr is also the |
8267 | // result expression: there could be two different LValues that would |
8268 | // refer to the same object in that case, and we can't model that. |
8269 | if (SemE == E->getResultExpr()) |
8270 | return Error(E); |
8271 | |
8272 | // Unique OVEs get evaluated if and when we encounter them when |
8273 | // emitting the rest of the semantic form, rather than eagerly. |
8274 | if (OVE->isUnique()) |
8275 | continue; |
8276 | |
8277 | LValue LV; |
8278 | if (!Evaluate(Info.CurrentCall->createTemporary( |
8279 | OVE, getStorageType(Info.Ctx, OVE), |
8280 | ScopeKind::FullExpression, LV), |
8281 | Info, OVE->getSourceExpr())) |
8282 | return false; |
8283 | } else if (SemE == E->getResultExpr()) { |
8284 | if (!StmtVisitorTy::Visit(SemE)) |
8285 | return false; |
8286 | } else { |
8287 | if (!EvaluateIgnoredValue(Info, E: SemE)) |
8288 | return false; |
8289 | } |
8290 | } |
8291 | return true; |
8292 | } |
8293 | |
8294 | bool VisitCallExpr(const CallExpr *E) { |
8295 | APValue Result; |
8296 | if (!handleCallExpr(E, Result, ResultSlot: nullptr)) |
8297 | return false; |
8298 | return DerivedSuccess(V: Result, E); |
8299 | } |
8300 | |
8301 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
8302 | const LValue *ResultSlot) { |
8303 | CallScopeRAII CallScope(Info); |
8304 | |
8305 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
8306 | QualType CalleeType = Callee->getType(); |
8307 | |
8308 | const FunctionDecl *FD = nullptr; |
8309 | LValue *This = nullptr, ObjectArg; |
8310 | auto Args = llvm::ArrayRef(E->getArgs(), E->getNumArgs()); |
8311 | bool HasQualifier = false; |
8312 | |
8313 | CallRef Call; |
8314 | |
8315 | // Extract function decl and 'this' pointer from the callee. |
8316 | if (CalleeType->isSpecificBuiltinType(K: BuiltinType::BoundMember)) { |
8317 | const CXXMethodDecl *Member = nullptr; |
8318 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
8319 | // Explicit bound member calls, such as x.f() or p->g(); |
8320 | if (!EvaluateObjectArgument(Info, Object: ME->getBase(), This&: ObjectArg)) |
8321 | return false; |
8322 | Member = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); |
8323 | if (!Member) |
8324 | return Error(Callee); |
8325 | This = &ObjectArg; |
8326 | HasQualifier = ME->hasQualifier(); |
8327 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
8328 | // Indirect bound member calls ('.*' or '->*'). |
8329 | const ValueDecl *D = |
8330 | HandleMemberPointerAccess(Info, BO: BE, LV&: ObjectArg, IncludeMember: false); |
8331 | if (!D) |
8332 | return false; |
8333 | Member = dyn_cast<CXXMethodDecl>(D); |
8334 | if (!Member) |
8335 | return Error(Callee); |
8336 | This = &ObjectArg; |
8337 | } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Callee)) { |
8338 | if (!Info.getLangOpts().CPlusPlus20) |
8339 | Info.CCEDiag(PDE, diag::note_constexpr_pseudo_destructor); |
8340 | return EvaluateObjectArgument(Info, PDE->getBase(), ObjectArg) && |
8341 | HandleDestruction(Info, PDE, ObjectArg, PDE->getDestroyedType()); |
8342 | } else |
8343 | return Error(Callee); |
8344 | FD = Member; |
8345 | } else if (CalleeType->isFunctionPointerType()) { |
8346 | LValue CalleeLV; |
8347 | if (!EvaluatePointer(E: Callee, Result&: CalleeLV, Info)) |
8348 | return false; |
8349 | |
8350 | if (!CalleeLV.getLValueOffset().isZero()) |
8351 | return Error(Callee); |
8352 | if (CalleeLV.isNullPointer()) { |
8353 | Info.FFDiag(Callee, diag::note_constexpr_null_callee) |
8354 | << const_cast<Expr *>(Callee); |
8355 | return false; |
8356 | } |
8357 | FD = dyn_cast_or_null<FunctionDecl>( |
8358 | CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>()); |
8359 | if (!FD) |
8360 | return Error(Callee); |
8361 | // Don't call function pointers which have been cast to some other type. |
8362 | // Per DR (no number yet), the caller and callee can differ in noexcept. |
8363 | if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec( |
8364 | T: CalleeType->getPointeeType(), U: FD->getType())) { |
8365 | return Error(E); |
8366 | } |
8367 | |
8368 | // For an (overloaded) assignment expression, evaluate the RHS before the |
8369 | // LHS. |
8370 | auto *OCE = dyn_cast<CXXOperatorCallExpr>(E); |
8371 | if (OCE && OCE->isAssignmentOp()) { |
8372 | assert(Args.size() == 2 && "wrong number of arguments in assignment"); |
8373 | Call = Info.CurrentCall->createCall(Callee: FD); |
8374 | bool HasThis = false; |
8375 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
8376 | HasThis = MD->isImplicitObjectMemberFunction(); |
8377 | if (!EvaluateArgs(HasThis ? Args.slice(1) : Args, Call, Info, FD, |
8378 | /*RightToLeft=*/true, &ObjectArg)) |
8379 | return false; |
8380 | } |
8381 | |
8382 | // Overloaded operator calls to member functions are represented as normal |
8383 | // calls with '*this' as the first argument. |
8384 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
8385 | if (MD && |
8386 | (MD->isImplicitObjectMemberFunction() || (OCE && MD->isStatic()))) { |
8387 | // FIXME: When selecting an implicit conversion for an overloaded |
8388 | // operator delete, we sometimes try to evaluate calls to conversion |
8389 | // operators without a 'this' parameter! |
8390 | if (Args.empty()) |
8391 | return Error(E); |
8392 | |
8393 | if (!EvaluateObjectArgument(Info, Args[0], ObjectArg)) |
8394 | return false; |
8395 | |
8396 | // If we are calling a static operator, the 'this' argument needs to be |
8397 | // ignored after being evaluated. |
8398 | if (MD->isInstance()) |
8399 | This = &ObjectArg; |
8400 | |
8401 | // If this is syntactically a simple assignment using a trivial |
8402 | // assignment operator, start the lifetimes of union members as needed, |
8403 | // per C++20 [class.union]5. |
8404 | if (Info.getLangOpts().CPlusPlus20 && OCE && |
8405 | OCE->getOperator() == OO_Equal && MD->isTrivial() && |
8406 | !MaybeHandleUnionActiveMemberChange(Info, Args[0], ObjectArg)) |
8407 | return false; |
8408 | |
8409 | Args = Args.slice(1); |
8410 | } else if (MD && MD->isLambdaStaticInvoker()) { |
8411 | // Map the static invoker for the lambda back to the call operator. |
8412 | // Conveniently, we don't have to slice out the 'this' argument (as is |
8413 | // being done for the non-static case), since a static member function |
8414 | // doesn't have an implicit argument passed in. |
8415 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
8416 | assert( |
8417 | ClosureClass->captures_begin() == ClosureClass->captures_end() && |
8418 | "Number of captures must be zero for conversion to function-ptr"); |
8419 | |
8420 | const CXXMethodDecl *LambdaCallOp = |
8421 | ClosureClass->getLambdaCallOperator(); |
8422 | |
8423 | // Set 'FD', the function that will be called below, to the call |
8424 | // operator. If the closure object represents a generic lambda, find |
8425 | // the corresponding specialization of the call operator. |
8426 | |
8427 | if (ClosureClass->isGenericLambda()) { |
8428 | assert(MD->isFunctionTemplateSpecialization() && |
8429 | "A generic lambda's static-invoker function must be a " |
8430 | "template specialization"); |
8431 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
8432 | FunctionTemplateDecl *CallOpTemplate = |
8433 | LambdaCallOp->getDescribedFunctionTemplate(); |
8434 | void *InsertPos = nullptr; |
8435 | FunctionDecl *CorrespondingCallOpSpecialization = |
8436 | CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertPos); |
8437 | assert(CorrespondingCallOpSpecialization && |
8438 | "We must always have a function call operator specialization " |
8439 | "that corresponds to our static invoker specialization"); |
8440 | assert(isa<CXXMethodDecl>(CorrespondingCallOpSpecialization)); |
8441 | FD = CorrespondingCallOpSpecialization; |
8442 | } else |
8443 | FD = LambdaCallOp; |
8444 | } else if (FD->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) { |
8445 | if (FD->getDeclName().isAnyOperatorNew()) { |
8446 | LValue Ptr; |
8447 | if (!HandleOperatorNewCall(Info, E, Result&: Ptr)) |
8448 | return false; |
8449 | Ptr.moveInto(V&: Result); |
8450 | return CallScope.destroy(); |
8451 | } else { |
8452 | return HandleOperatorDeleteCall(Info, E) && CallScope.destroy(); |
8453 | } |
8454 | } |
8455 | } else |
8456 | return Error(E); |
8457 | |
8458 | // Evaluate the arguments now if we've not already done so. |
8459 | if (!Call) { |
8460 | Call = Info.CurrentCall->createCall(Callee: FD); |
8461 | if (!EvaluateArgs(Args, Call, Info, FD, /*RightToLeft*/ false, |
8462 | &ObjectArg)) |
8463 | return false; |
8464 | } |
8465 | |
8466 | SmallVector<QualType, 4> CovariantAdjustmentPath; |
8467 | if (This) { |
8468 | auto *NamedMember = dyn_cast<CXXMethodDecl>(FD); |
8469 | if (NamedMember && NamedMember->isVirtual() && !HasQualifier) { |
8470 | // Perform virtual dispatch, if necessary. |
8471 | FD = HandleVirtualDispatch(Info, E, *This, NamedMember, |
8472 | CovariantAdjustmentPath); |
8473 | if (!FD) |
8474 | return false; |
8475 | } else if (NamedMember && NamedMember->isImplicitObjectMemberFunction()) { |
8476 | // Check that the 'this' pointer points to an object of the right type. |
8477 | // FIXME: If this is an assignment operator call, we may need to change |
8478 | // the active union member before we check this. |
8479 | if (!checkNonVirtualMemberCallThisPointer(Info, E, *This, NamedMember)) |
8480 | return false; |
8481 | } |
8482 | } |
8483 | |
8484 | // Destructor calls are different enough that they have their own codepath. |
8485 | if (auto *DD = dyn_cast<CXXDestructorDecl>(FD)) { |
8486 | assert(This && "no 'this' pointer for destructor call"); |
8487 | return HandleDestruction(Info, E, *This, |
8488 | Info.Ctx.getRecordType(Decl: DD->getParent())) && |
8489 | CallScope.destroy(); |
8490 | } |
8491 | |
8492 | const FunctionDecl *Definition = nullptr; |
8493 | Stmt *Body = FD->getBody(Definition); |
8494 | SourceLocation Loc = E->getExprLoc(); |
8495 | |
8496 | // Treat the object argument as `this` when evaluating defaulted |
8497 | // special menmber functions |
8498 | if (FD->hasCXXExplicitFunctionObjectParameter()) |
8499 | This = &ObjectArg; |
8500 | |
8501 | if (!CheckConstexprFunction(Info, CallLoc: Loc, Declaration: FD, Definition, Body) || |
8502 | !HandleFunctionCall(Loc, Definition, This, E, Args, Call, Body, Info, |
8503 | Result, ResultSlot)) |
8504 | return false; |
8505 | |
8506 | if (!CovariantAdjustmentPath.empty() && |
8507 | !HandleCovariantReturnAdjustment(Info, E, Result, |
8508 | CovariantAdjustmentPath)) |
8509 | return false; |
8510 | |
8511 | return CallScope.destroy(); |
8512 | } |
8513 | |
8514 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
8515 | return StmtVisitorTy::Visit(E->getInitializer()); |
8516 | } |
8517 | bool VisitInitListExpr(const InitListExpr *E) { |
8518 | if (E->getNumInits() == 0) |
8519 | return DerivedZeroInitialization(E); |
8520 | if (E->getNumInits() == 1) |
8521 | return StmtVisitorTy::Visit(E->getInit(Init: 0)); |
8522 | return Error(E); |
8523 | } |
8524 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
8525 | return DerivedZeroInitialization(E); |
8526 | } |
8527 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
8528 | return DerivedZeroInitialization(E); |
8529 | } |
8530 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
8531 | return DerivedZeroInitialization(E); |
8532 | } |
8533 | |
8534 | /// A member expression where the object is a prvalue is itself a prvalue. |
8535 | bool VisitMemberExpr(const MemberExpr *E) { |
8536 | assert(!Info.Ctx.getLangOpts().CPlusPlus11 && |
8537 | "missing temporary materialization conversion"); |
8538 | assert(!E->isArrow() && "missing call to bound member function?"); |
8539 | |
8540 | APValue Val; |
8541 | if (!Evaluate(Result&: Val, Info, E: E->getBase())) |
8542 | return false; |
8543 | |
8544 | QualType BaseTy = E->getBase()->getType(); |
8545 | |
8546 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
8547 | if (!FD) return Error(E); |
8548 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
8549 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
8550 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
8551 | |
8552 | // Note: there is no lvalue base here. But this case should only ever |
8553 | // happen in C or in C++98, where we cannot be evaluating a constexpr |
8554 | // constructor, which is the only case the base matters. |
8555 | CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy); |
8556 | SubobjectDesignator Designator(BaseTy); |
8557 | Designator.addDeclUnchecked(FD); |
8558 | |
8559 | APValue Result; |
8560 | return extractSubobject(Info, E, Obj, Designator, Result) && |
8561 | DerivedSuccess(V: Result, E); |
8562 | } |
8563 | |
8564 | bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
8565 | APValue Val; |
8566 | if (!Evaluate(Result&: Val, Info, E: E->getBase())) |
8567 | return false; |
8568 | |
8569 | if (Val.isVector()) { |
8570 | SmallVector<uint32_t, 4> Indices; |
8571 | E->getEncodedElementAccess(Elts&: Indices); |
8572 | if (Indices.size() == 1) { |
8573 | // Return scalar. |
8574 | return DerivedSuccess(V: Val.getVectorElt(Indices[0]), E); |
8575 | } else { |
8576 | // Construct new APValue vector. |
8577 | SmallVector<APValue, 4> Elts; |
8578 | for (unsigned I = 0; I < Indices.size(); ++I) { |
8579 | Elts.push_back(Val.getVectorElt(Indices[I])); |
8580 | } |
8581 | APValue VecResult(Elts.data(), Indices.size()); |
8582 | return DerivedSuccess(V: VecResult, E); |
8583 | } |
8584 | } |
8585 | |
8586 | return false; |
8587 | } |
8588 | |
8589 | bool VisitCastExpr(const CastExpr *E) { |
8590 | switch (E->getCastKind()) { |
8591 | default: |
8592 | break; |
8593 | |
8594 | case CK_AtomicToNonAtomic: { |
8595 | APValue AtomicVal; |
8596 | // This does not need to be done in place even for class/array types: |
8597 | // atomic-to-non-atomic conversion implies copying the object |
8598 | // representation. |
8599 | if (!Evaluate(Result&: AtomicVal, Info, E: E->getSubExpr())) |
8600 | return false; |
8601 | return DerivedSuccess(V: AtomicVal, E); |
8602 | } |
8603 | |
8604 | case CK_NoOp: |
8605 | case CK_UserDefinedConversion: |
8606 | return StmtVisitorTy::Visit(E->getSubExpr()); |
8607 | |
8608 | case CK_LValueToRValue: { |
8609 | LValue LVal; |
8610 | if (!EvaluateLValue(E: E->getSubExpr(), Result&: LVal, Info)) |
8611 | return false; |
8612 | APValue RVal; |
8613 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
8614 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
8615 | LVal, RVal)) |
8616 | return false; |
8617 | return DerivedSuccess(V: RVal, E); |
8618 | } |
8619 | case CK_LValueToRValueBitCast: { |
8620 | APValue DestValue, SourceValue; |
8621 | if (!Evaluate(Result&: SourceValue, Info, E: E->getSubExpr())) |
8622 | return false; |
8623 | if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, BCE: E)) |
8624 | return false; |
8625 | return DerivedSuccess(V: DestValue, E); |
8626 | } |
8627 | |
8628 | case CK_AddressSpaceConversion: { |
8629 | APValue Value; |
8630 | if (!Evaluate(Result&: Value, Info, E: E->getSubExpr())) |
8631 | return false; |
8632 | return DerivedSuccess(V: Value, E); |
8633 | } |
8634 | } |
8635 | |
8636 | return Error(E); |
8637 | } |
8638 | |
8639 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
8640 | return VisitUnaryPostIncDec(UO); |
8641 | } |
8642 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
8643 | return VisitUnaryPostIncDec(UO); |
8644 | } |
8645 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
8646 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8647 | return Error(UO); |
8648 | |
8649 | LValue LVal; |
8650 | if (!EvaluateLValue(E: UO->getSubExpr(), Result&: LVal, Info)) |
8651 | return false; |
8652 | APValue RVal; |
8653 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
8654 | UO->isIncrementOp(), &RVal)) |
8655 | return false; |
8656 | return DerivedSuccess(V: RVal, E: UO); |
8657 | } |
8658 | |
8659 | bool VisitStmtExpr(const StmtExpr *E) { |
8660 | // We will have checked the full-expressions inside the statement expression |
8661 | // when they were completed, and don't need to check them again now. |
8662 | llvm::SaveAndRestore NotCheckingForUB(Info.CheckingForUndefinedBehavior, |
8663 | false); |
8664 | |
8665 | const CompoundStmt *CS = E->getSubStmt(); |
8666 | if (CS->body_empty()) |
8667 | return true; |
8668 | |
8669 | BlockScopeRAII Scope(Info); |
8670 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
8671 | BE = CS->body_end(); |
8672 | /**/; ++BI) { |
8673 | if (BI + 1 == BE) { |
8674 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
8675 | if (!FinalExpr) { |
8676 | Info.FFDiag((*BI)->getBeginLoc(), |
8677 | diag::note_constexpr_stmt_expr_unsupported); |
8678 | return false; |
8679 | } |
8680 | return this->Visit(FinalExpr) && Scope.destroy(); |
8681 | } |
8682 | |
8683 | APValue ReturnValue; |
8684 | StmtResult Result = { .Value: ReturnValue, .Slot: nullptr }; |
8685 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: *BI); |
8686 | if (ESR != ESR_Succeeded) { |
8687 | // FIXME: If the statement-expression terminated due to 'return', |
8688 | // 'break', or 'continue', it would be nice to propagate that to |
8689 | // the outer statement evaluation rather than bailing out. |
8690 | if (ESR != ESR_Failed) |
8691 | Info.FFDiag((*BI)->getBeginLoc(), |
8692 | diag::note_constexpr_stmt_expr_unsupported); |
8693 | return false; |
8694 | } |
8695 | } |
8696 | |
8697 | llvm_unreachable("Return from function from the loop above."); |
8698 | } |
8699 | |
8700 | bool VisitPackIndexingExpr(const PackIndexingExpr *E) { |
8701 | return StmtVisitorTy::Visit(E->getSelectedExpr()); |
8702 | } |
8703 | |
8704 | /// Visit a value which is evaluated, but whose value is ignored. |
8705 | void VisitIgnoredValue(const Expr *E) { |
8706 | EvaluateIgnoredValue(Info, E); |
8707 | } |
8708 | |
8709 | /// Potentially visit a MemberExpr's base expression. |
8710 | void VisitIgnoredBaseExpression(const Expr *E) { |
8711 | // While MSVC doesn't evaluate the base expression, it does diagnose the |
8712 | // presence of side-effecting behavior. |
8713 | if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Ctx: Info.Ctx)) |
8714 | return; |
8715 | VisitIgnoredValue(E); |
8716 | } |
8717 | }; |
8718 | |
8719 | } // namespace |
8720 | |
8721 | //===----------------------------------------------------------------------===// |
8722 | // Common base class for lvalue and temporary evaluation. |
8723 | //===----------------------------------------------------------------------===// |
8724 | namespace { |
8725 | template<class Derived> |
8726 | class LValueExprEvaluatorBase |
8727 | : public ExprEvaluatorBase<Derived> { |
8728 | protected: |
8729 | LValue &Result; |
8730 | bool InvalidBaseOK; |
8731 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
8732 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
8733 | |
8734 | bool Success(APValue::LValueBase B) { |
8735 | Result.set(B); |
8736 | return true; |
8737 | } |
8738 | |
8739 | bool evaluatePointer(const Expr *E, LValue &Result) { |
8740 | return EvaluatePointer(E, Result, this->Info, InvalidBaseOK); |
8741 | } |
8742 | |
8743 | public: |
8744 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) |
8745 | : ExprEvaluatorBaseTy(Info), Result(Result), |
8746 | InvalidBaseOK(InvalidBaseOK) {} |
8747 | |
8748 | bool Success(const APValue &V, const Expr *E) { |
8749 | Result.setFrom(Ctx&: this->Info.Ctx, V); |
8750 | return true; |
8751 | } |
8752 | |
8753 | bool VisitMemberExpr(const MemberExpr *E) { |
8754 | // Handle non-static data members. |
8755 | QualType BaseTy; |
8756 | bool EvalOK; |
8757 | if (E->isArrow()) { |
8758 | EvalOK = evaluatePointer(E: E->getBase(), Result); |
8759 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
8760 | } else if (E->getBase()->isPRValue()) { |
8761 | assert(E->getBase()->getType()->isRecordType()); |
8762 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
8763 | BaseTy = E->getBase()->getType(); |
8764 | } else { |
8765 | EvalOK = this->Visit(E->getBase()); |
8766 | BaseTy = E->getBase()->getType(); |
8767 | } |
8768 | if (!EvalOK) { |
8769 | if (!InvalidBaseOK) |
8770 | return false; |
8771 | Result.setInvalid(E); |
8772 | return true; |
8773 | } |
8774 | |
8775 | const ValueDecl *MD = E->getMemberDecl(); |
8776 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl())) { |
8777 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
8778 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
8779 | (void)BaseTy; |
8780 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
8781 | return false; |
8782 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Val: MD)) { |
8783 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
8784 | return false; |
8785 | } else |
8786 | return this->Error(E); |
8787 | |
8788 | if (MD->getType()->isReferenceType()) { |
8789 | APValue RefValue; |
8790 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
8791 | RefValue)) |
8792 | return false; |
8793 | return Success(RefValue, E); |
8794 | } |
8795 | return true; |
8796 | } |
8797 | |
8798 | bool VisitBinaryOperator(const BinaryOperator *E) { |
8799 | switch (E->getOpcode()) { |
8800 | default: |
8801 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
8802 | |
8803 | case BO_PtrMemD: |
8804 | case BO_PtrMemI: |
8805 | return HandleMemberPointerAccess(this->Info, E, Result); |
8806 | } |
8807 | } |
8808 | |
8809 | bool VisitCastExpr(const CastExpr *E) { |
8810 | switch (E->getCastKind()) { |
8811 | default: |
8812 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
8813 | |
8814 | case CK_DerivedToBase: |
8815 | case CK_UncheckedDerivedToBase: |
8816 | if (!this->Visit(E->getSubExpr())) |
8817 | return false; |
8818 | |
8819 | // Now figure out the necessary offset to add to the base LV to get from |
8820 | // the derived class to the base class. |
8821 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
8822 | Result); |
8823 | } |
8824 | } |
8825 | }; |
8826 | } |
8827 | |
8828 | //===----------------------------------------------------------------------===// |
8829 | // LValue Evaluation |
8830 | // |
8831 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
8832 | // function designators (in C), decl references to void objects (in C), and |
8833 | // temporaries (if building with -Wno-address-of-temporary). |
8834 | // |
8835 | // LValue evaluation produces values comprising a base expression of one of the |
8836 | // following types: |
8837 | // - Declarations |
8838 | // * VarDecl |
8839 | // * FunctionDecl |
8840 | // - Literals |
8841 | // * CompoundLiteralExpr in C (and in global scope in C++) |
8842 | // * StringLiteral |
8843 | // * PredefinedExpr |
8844 | // * ObjCStringLiteralExpr |
8845 | // * ObjCEncodeExpr |
8846 | // * AddrLabelExpr |
8847 | // * BlockExpr |
8848 | // * CallExpr for a MakeStringConstant builtin |
8849 | // - typeid(T) expressions, as TypeInfoLValues |
8850 | // - Locals and temporaries |
8851 | // * MaterializeTemporaryExpr |
8852 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
8853 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
8854 | // from the AST (FIXME). |
8855 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
8856 | // CallIndex, for a lifetime-extended temporary. |
8857 | // * The ConstantExpr that is currently being evaluated during evaluation of an |
8858 | // immediate invocation. |
8859 | // plus an offset in bytes. |
8860 | //===----------------------------------------------------------------------===// |
8861 | namespace { |
8862 | class LValueExprEvaluator |
8863 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
8864 | public: |
8865 | LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) : |
8866 | LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {} |
8867 | |
8868 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
8869 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
8870 | |
8871 | bool VisitCallExpr(const CallExpr *E); |
8872 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
8873 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
8874 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
8875 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
8876 | bool VisitMemberExpr(const MemberExpr *E); |
8877 | bool VisitStringLiteral(const StringLiteral *E) { |
8878 | return Success(B: APValue::LValueBase( |
8879 | E, 0, Info.getASTContext().getNextStringLiteralVersion())); |
8880 | } |
8881 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
8882 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
8883 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
8884 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
8885 | bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E); |
8886 | bool VisitUnaryDeref(const UnaryOperator *E); |
8887 | bool VisitUnaryReal(const UnaryOperator *E); |
8888 | bool VisitUnaryImag(const UnaryOperator *E); |
8889 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
8890 | return VisitUnaryPreIncDec(UO); |
8891 | } |
8892 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
8893 | return VisitUnaryPreIncDec(UO); |
8894 | } |
8895 | bool VisitBinAssign(const BinaryOperator *BO); |
8896 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
8897 | |
8898 | bool VisitCastExpr(const CastExpr *E) { |
8899 | switch (E->getCastKind()) { |
8900 | default: |
8901 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
8902 | |
8903 | case CK_LValueBitCast: |
8904 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) |
8905 | << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret |
8906 | << Info.Ctx.getLangOpts().CPlusPlus; |
8907 | if (!Visit(E->getSubExpr())) |
8908 | return false; |
8909 | Result.Designator.setInvalid(); |
8910 | return true; |
8911 | |
8912 | case CK_BaseToDerived: |
8913 | if (!Visit(E->getSubExpr())) |
8914 | return false; |
8915 | return HandleBaseToDerivedCast(Info, E, Result); |
8916 | |
8917 | case CK_Dynamic: |
8918 | if (!Visit(E->getSubExpr())) |
8919 | return false; |
8920 | return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result); |
8921 | } |
8922 | } |
8923 | }; |
8924 | } // end anonymous namespace |
8925 | |
8926 | /// Get an lvalue to a field of a lambda's closure type. |
8927 | static bool HandleLambdaCapture(EvalInfo &Info, const Expr *E, LValue &Result, |
8928 | const CXXMethodDecl *MD, const FieldDecl *FD, |
8929 | bool LValueToRValueConversion) { |
8930 | // Static lambda function call operators can't have captures. We already |
8931 | // diagnosed this, so bail out here. |
8932 | if (MD->isStatic()) { |
8933 | assert(Info.CurrentCall->This == nullptr && |
8934 | "This should not be set for a static call operator"); |
8935 | return false; |
8936 | } |
8937 | |
8938 | // Start with 'Result' referring to the complete closure object... |
8939 | if (MD->isExplicitObjectMemberFunction()) { |
8940 | // Self may be passed by reference or by value. |
8941 | const ParmVarDecl *Self = MD->getParamDecl(0); |
8942 | if (Self->getType()->isReferenceType()) { |
8943 | APValue *RefValue = Info.getParamSlot(Call: Info.CurrentCall->Arguments, PVD: Self); |
8944 | if (!RefValue->allowConstexprUnknown() || RefValue->hasValue()) |
8945 | Result.setFrom(Ctx&: Info.Ctx, V: *RefValue); |
8946 | } else { |
8947 | const ParmVarDecl *VD = Info.CurrentCall->Arguments.getOrigParam(PVD: Self); |
8948 | CallStackFrame *Frame = |
8949 | Info.getCallFrameAndDepth(CallIndex: Info.CurrentCall->Arguments.CallIndex) |
8950 | .first; |
8951 | unsigned Version = Info.CurrentCall->Arguments.Version; |
8952 | Result.set({VD, Frame->Index, Version}); |
8953 | } |
8954 | } else |
8955 | Result = *Info.CurrentCall->This; |
8956 | |
8957 | // ... then update it to refer to the field of the closure object |
8958 | // that represents the capture. |
8959 | if (!HandleLValueMember(Info, E, LVal&: Result, FD)) |
8960 | return false; |
8961 | |
8962 | // And if the field is of reference type (or if we captured '*this' by |
8963 | // reference), update 'Result' to refer to what |
8964 | // the field refers to. |
8965 | if (LValueToRValueConversion) { |
8966 | APValue RVal; |
8967 | if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, RVal)) |
8968 | return false; |
8969 | Result.setFrom(Ctx&: Info.Ctx, V: RVal); |
8970 | } |
8971 | return true; |
8972 | } |
8973 | |
8974 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
8975 | /// expressions which are not glvalues, in three cases: |
8976 | /// * function designators in C, and |
8977 | /// * "extern void" objects |
8978 | /// * @selector() expressions in Objective-C |
8979 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
8980 | bool InvalidBaseOK) { |
8981 | assert(!E->isValueDependent()); |
8982 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
8983 | E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E->IgnoreParens())); |
8984 | return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
8985 | } |
8986 | |
8987 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
8988 | const NamedDecl *D = E->getDecl(); |
8989 | if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl, |
8990 | UnnamedGlobalConstantDecl>(Val: D)) |
8991 | return Success(B: cast<ValueDecl>(Val: D)); |
8992 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
8993 | return VisitVarDecl(E, VD); |
8994 | if (const BindingDecl *BD = dyn_cast<BindingDecl>(Val: D)) |
8995 | return Visit(BD->getBinding()); |
8996 | return Error(E); |
8997 | } |
8998 | |
8999 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
9000 | // If we are within a lambda's call operator, check whether the 'VD' referred |
9001 | // to within 'E' actually represents a lambda-capture that maps to a |
9002 | // data-member/field within the closure object, and if so, evaluate to the |
9003 | // field or what the field refers to. |
9004 | if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) && |
9005 | isa<DeclRefExpr>(Val: E) && |
9006 | cast<DeclRefExpr>(Val: E)->refersToEnclosingVariableOrCapture()) { |
9007 | // We don't always have a complete capture-map when checking or inferring if |
9008 | // the function call operator meets the requirements of a constexpr function |
9009 | // - but we don't need to evaluate the captures to determine constexprness |
9010 | // (dcl.constexpr C++17). |
9011 | if (Info.checkingPotentialConstantExpression()) |
9012 | return false; |
9013 | |
9014 | if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) { |
9015 | const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee); |
9016 | return HandleLambdaCapture(Info, E, Result, MD, FD, |
9017 | FD->getType()->isReferenceType()); |
9018 | } |
9019 | } |
9020 | |
9021 | CallStackFrame *Frame = nullptr; |
9022 | unsigned Version = 0; |
9023 | if (VD->hasLocalStorage()) { |
9024 | // Only if a local variable was declared in the function currently being |
9025 | // evaluated, do we expect to be able to find its value in the current |
9026 | // frame. (Otherwise it was likely declared in an enclosing context and |
9027 | // could either have a valid evaluatable value (for e.g. a constexpr |
9028 | // variable) or be ill-formed (and trigger an appropriate evaluation |
9029 | // diagnostic)). |
9030 | CallStackFrame *CurrFrame = Info.CurrentCall; |
9031 | if (CurrFrame->Callee && CurrFrame->Callee->Equals(DC: VD->getDeclContext())) { |
9032 | // Function parameters are stored in some caller's frame. (Usually the |
9033 | // immediate caller, but for an inherited constructor they may be more |
9034 | // distant.) |
9035 | if (auto *PVD = dyn_cast<ParmVarDecl>(Val: VD)) { |
9036 | if (CurrFrame->Arguments) { |
9037 | VD = CurrFrame->Arguments.getOrigParam(PVD); |
9038 | Frame = |
9039 | Info.getCallFrameAndDepth(CallIndex: CurrFrame->Arguments.CallIndex).first; |
9040 | Version = CurrFrame->Arguments.Version; |
9041 | } |
9042 | } else { |
9043 | Frame = CurrFrame; |
9044 | Version = CurrFrame->getCurrentTemporaryVersion(Key: VD); |
9045 | } |
9046 | } |
9047 | } |
9048 | |
9049 | if (!VD->getType()->isReferenceType()) { |
9050 | if (Frame) { |
9051 | Result.set({VD, Frame->Index, Version}); |
9052 | return true; |
9053 | } |
9054 | return Success(VD); |
9055 | } |
9056 | |
9057 | if (!Info.getLangOpts().CPlusPlus11) { |
9058 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_integral, 1) |
9059 | << VD << VD->getType(); |
9060 | Info.Note(VD->getLocation(), diag::note_declared_at); |
9061 | } |
9062 | |
9063 | APValue *V; |
9064 | if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, Result&: V)) |
9065 | return false; |
9066 | |
9067 | return Success(V: *V, E); |
9068 | } |
9069 | |
9070 | bool LValueExprEvaluator::VisitCallExpr(const CallExpr *E) { |
9071 | if (!IsConstantEvaluatedBuiltinCall(E)) |
9072 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
9073 | |
9074 | switch (E->getBuiltinCallee()) { |
9075 | default: |
9076 | return false; |
9077 | case Builtin::BIas_const: |
9078 | case Builtin::BIforward: |
9079 | case Builtin::BIforward_like: |
9080 | case Builtin::BImove: |
9081 | case Builtin::BImove_if_noexcept: |
9082 | if (cast<FunctionDecl>(Val: E->getCalleeDecl())->isConstexpr()) |
9083 | return Visit(E->getArg(Arg: 0)); |
9084 | break; |
9085 | } |
9086 | |
9087 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
9088 | } |
9089 | |
9090 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
9091 | const MaterializeTemporaryExpr *E) { |
9092 | // Walk through the expression to find the materialized temporary itself. |
9093 | SmallVector<const Expr *, 2> CommaLHSs; |
9094 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
9095 | const Expr *Inner = |
9096 | E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments); |
9097 | |
9098 | // If we passed any comma operators, evaluate their LHSs. |
9099 | for (const Expr *E : CommaLHSs) |
9100 | if (!EvaluateIgnoredValue(Info, E)) |
9101 | return false; |
9102 | |
9103 | // A materialized temporary with static storage duration can appear within the |
9104 | // result of a constant expression evaluation, so we need to preserve its |
9105 | // value for use outside this evaluation. |
9106 | APValue *Value; |
9107 | if (E->getStorageDuration() == SD_Static) { |
9108 | if (Info.EvalMode == EvalInfo::EM_ConstantFold) |
9109 | return false; |
9110 | // FIXME: What about SD_Thread? |
9111 | Value = E->getOrCreateValue(MayCreate: true); |
9112 | *Value = APValue(); |
9113 | Result.set(E); |
9114 | } else { |
9115 | Value = &Info.CurrentCall->createTemporary( |
9116 | Key: E, T: Inner->getType(), |
9117 | Scope: E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression |
9118 | : ScopeKind::Block, |
9119 | LV&: Result); |
9120 | } |
9121 | |
9122 | QualType Type = Inner->getType(); |
9123 | |
9124 | // Materialize the temporary itself. |
9125 | if (!EvaluateInPlace(Result&: *Value, Info, This: Result, E: Inner)) { |
9126 | *Value = APValue(); |
9127 | return false; |
9128 | } |
9129 | |
9130 | // Adjust our lvalue to refer to the desired subobject. |
9131 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
9132 | --I; |
9133 | switch (Adjustments[I].Kind) { |
9134 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
9135 | if (!HandleLValueBasePath(Info, E: Adjustments[I].DerivedToBase.BasePath, |
9136 | Type, Result)) |
9137 | return false; |
9138 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
9139 | break; |
9140 | |
9141 | case SubobjectAdjustment::FieldAdjustment: |
9142 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
9143 | return false; |
9144 | Type = Adjustments[I].Field->getType(); |
9145 | break; |
9146 | |
9147 | case SubobjectAdjustment::MemberPointerAdjustment: |
9148 | if (!HandleMemberPointerAccess(Info&: this->Info, LVType: Type, LV&: Result, |
9149 | RHS: Adjustments[I].Ptr.RHS)) |
9150 | return false; |
9151 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
9152 | break; |
9153 | } |
9154 | } |
9155 | |
9156 | return true; |
9157 | } |
9158 | |
9159 | bool |
9160 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
9161 | assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) && |
9162 | "lvalue compound literal in c++?"); |
9163 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
9164 | // only see this when folding in C, so there's no standard to follow here. |
9165 | return Success(E); |
9166 | } |
9167 | |
9168 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
9169 | TypeInfoLValue TypeInfo; |
9170 | |
9171 | if (!E->isPotentiallyEvaluated()) { |
9172 | if (E->isTypeOperand()) |
9173 | TypeInfo = TypeInfoLValue(E->getTypeOperand(Context: Info.Ctx).getTypePtr()); |
9174 | else |
9175 | TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr()); |
9176 | } else { |
9177 | if (!Info.Ctx.getLangOpts().CPlusPlus20) { |
9178 | Info.CCEDiag(E, diag::note_constexpr_typeid_polymorphic) |
9179 | << E->getExprOperand()->getType() |
9180 | << E->getExprOperand()->getSourceRange(); |
9181 | } |
9182 | |
9183 | if (!Visit(E->getExprOperand())) |
9184 | return false; |
9185 | |
9186 | std::optional<DynamicType> DynType = |
9187 | ComputeDynamicType(Info, E, Result, AK_TypeId); |
9188 | if (!DynType) |
9189 | return false; |
9190 | |
9191 | TypeInfo = |
9192 | TypeInfoLValue(Info.Ctx.getRecordType(DynType->Type).getTypePtr()); |
9193 | } |
9194 | |
9195 | return Success(APValue::LValueBase::getTypeInfo(LV: TypeInfo, TypeInfo: E->getType())); |
9196 | } |
9197 | |
9198 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
9199 | return Success(E->getGuidDecl()); |
9200 | } |
9201 | |
9202 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
9203 | // Handle static data members. |
9204 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: E->getMemberDecl())) { |
9205 | VisitIgnoredBaseExpression(E: E->getBase()); |
9206 | return VisitVarDecl(E, VD); |
9207 | } |
9208 | |
9209 | // Handle static member functions. |
9210 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: E->getMemberDecl())) { |
9211 | if (MD->isStatic()) { |
9212 | VisitIgnoredBaseExpression(E: E->getBase()); |
9213 | return Success(MD); |
9214 | } |
9215 | } |
9216 | |
9217 | // Handle non-static data members. |
9218 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
9219 | } |
9220 | |
9221 | bool LValueExprEvaluator::VisitExtVectorElementExpr( |
9222 | const ExtVectorElementExpr *E) { |
9223 | bool Success = true; |
9224 | |
9225 | APValue Val; |
9226 | if (!Evaluate(Result&: Val, Info, E: E->getBase())) { |
9227 | if (!Info.noteFailure()) |
9228 | return false; |
9229 | Success = false; |
9230 | } |
9231 | |
9232 | SmallVector<uint32_t, 4> Indices; |
9233 | E->getEncodedElementAccess(Elts&: Indices); |
9234 | // FIXME: support accessing more than one element |
9235 | if (Indices.size() > 1) |
9236 | return false; |
9237 | |
9238 | if (Success) { |
9239 | Result.setFrom(Ctx&: Info.Ctx, V: Val); |
9240 | QualType BaseType = E->getBase()->getType(); |
9241 | if (E->isArrow()) |
9242 | BaseType = BaseType->getPointeeType(); |
9243 | const auto *VT = BaseType->castAs<VectorType>(); |
9244 | HandleLValueVectorElement(Info, E, Result, VT->getElementType(), |
9245 | VT->getNumElements(), Indices[0]); |
9246 | } |
9247 | |
9248 | return Success; |
9249 | } |
9250 | |
9251 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
9252 | if (E->getBase()->getType()->isSveVLSBuiltinType()) |
9253 | return Error(E); |
9254 | |
9255 | APSInt Index; |
9256 | bool Success = true; |
9257 | |
9258 | if (const auto *VT = E->getBase()->getType()->getAs<VectorType>()) { |
9259 | APValue Val; |
9260 | if (!Evaluate(Result&: Val, Info, E: E->getBase())) { |
9261 | if (!Info.noteFailure()) |
9262 | return false; |
9263 | Success = false; |
9264 | } |
9265 | |
9266 | if (!EvaluateInteger(E: E->getIdx(), Result&: Index, Info)) { |
9267 | if (!Info.noteFailure()) |
9268 | return false; |
9269 | Success = false; |
9270 | } |
9271 | |
9272 | if (Success) { |
9273 | Result.setFrom(Ctx&: Info.Ctx, V: Val); |
9274 | HandleLValueVectorElement(Info, E, Result, VT->getElementType(), |
9275 | VT->getNumElements(), Index.getExtValue()); |
9276 | } |
9277 | |
9278 | return Success; |
9279 | } |
9280 | |
9281 | // C++17's rules require us to evaluate the LHS first, regardless of which |
9282 | // side is the base. |
9283 | for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) { |
9284 | if (SubExpr == E->getBase() ? !evaluatePointer(E: SubExpr, Result) |
9285 | : !EvaluateInteger(E: SubExpr, Result&: Index, Info)) { |
9286 | if (!Info.noteFailure()) |
9287 | return false; |
9288 | Success = false; |
9289 | } |
9290 | } |
9291 | |
9292 | return Success && |
9293 | HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index); |
9294 | } |
9295 | |
9296 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
9297 | return evaluatePointer(E: E->getSubExpr(), Result); |
9298 | } |
9299 | |
9300 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
9301 | if (!Visit(E->getSubExpr())) |
9302 | return false; |
9303 | // __real is a no-op on scalar lvalues. |
9304 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
9305 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
9306 | return true; |
9307 | } |
9308 | |
9309 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
9310 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
9311 | "lvalue __imag__ on scalar?"); |
9312 | if (!Visit(E->getSubExpr())) |
9313 | return false; |
9314 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
9315 | return true; |
9316 | } |
9317 | |
9318 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
9319 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
9320 | return Error(UO); |
9321 | |
9322 | if (!this->Visit(UO->getSubExpr())) |
9323 | return false; |
9324 | |
9325 | return handleIncDec( |
9326 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
9327 | UO->isIncrementOp(), nullptr); |
9328 | } |
9329 | |
9330 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
9331 | const CompoundAssignOperator *CAO) { |
9332 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
9333 | return Error(CAO); |
9334 | |
9335 | bool Success = true; |
9336 | |
9337 | // C++17 onwards require that we evaluate the RHS first. |
9338 | APValue RHS; |
9339 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) { |
9340 | if (!Info.noteFailure()) |
9341 | return false; |
9342 | Success = false; |
9343 | } |
9344 | |
9345 | // The overall lvalue result is the result of evaluating the LHS. |
9346 | if (!this->Visit(CAO->getLHS()) || !Success) |
9347 | return false; |
9348 | |
9349 | return handleCompoundAssignment( |
9350 | this->Info, CAO, |
9351 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
9352 | CAO->getOpForCompoundAssignment(Opc: CAO->getOpcode()), RHS); |
9353 | } |
9354 | |
9355 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
9356 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
9357 | return Error(E); |
9358 | |
9359 | bool Success = true; |
9360 | |
9361 | // C++17 onwards require that we evaluate the RHS first. |
9362 | APValue NewVal; |
9363 | if (!Evaluate(Result&: NewVal, Info&: this->Info, E: E->getRHS())) { |
9364 | if (!Info.noteFailure()) |
9365 | return false; |
9366 | Success = false; |
9367 | } |
9368 | |
9369 | if (!this->Visit(E->getLHS()) || !Success) |
9370 | return false; |
9371 | |
9372 | if (Info.getLangOpts().CPlusPlus20 && |
9373 | !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: E->getLHS(), LHS: Result)) |
9374 | return false; |
9375 | |
9376 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
9377 | NewVal); |
9378 | } |
9379 | |
9380 | //===----------------------------------------------------------------------===// |
9381 | // Pointer Evaluation |
9382 | //===----------------------------------------------------------------------===// |
9383 | |
9384 | /// Attempts to compute the number of bytes available at the pointer |
9385 | /// returned by a function with the alloc_size attribute. Returns true if we |
9386 | /// were successful. Places an unsigned number into `Result`. |
9387 | /// |
9388 | /// This expects the given CallExpr to be a call to a function with an |
9389 | /// alloc_size attribute. |
9390 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
9391 | const CallExpr *Call, |
9392 | llvm::APInt &Result) { |
9393 | const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call); |
9394 | |
9395 | assert(AllocSize && AllocSize->getElemSizeParam().isValid()); |
9396 | unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex(); |
9397 | unsigned BitsInSizeT = Ctx.getTypeSize(T: Ctx.getSizeType()); |
9398 | if (Call->getNumArgs() <= SizeArgNo) |
9399 | return false; |
9400 | |
9401 | auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) { |
9402 | Expr::EvalResult ExprResult; |
9403 | if (!E->EvaluateAsInt(Result&: ExprResult, Ctx, AllowSideEffects: Expr::SE_AllowSideEffects)) |
9404 | return false; |
9405 | Into = ExprResult.Val.getInt(); |
9406 | if (Into.isNegative() || !Into.isIntN(N: BitsInSizeT)) |
9407 | return false; |
9408 | Into = Into.zext(width: BitsInSizeT); |
9409 | return true; |
9410 | }; |
9411 | |
9412 | APSInt SizeOfElem; |
9413 | if (!EvaluateAsSizeT(Call->getArg(Arg: SizeArgNo), SizeOfElem)) |
9414 | return false; |
9415 | |
9416 | if (!AllocSize->getNumElemsParam().isValid()) { |
9417 | Result = std::move(SizeOfElem); |
9418 | return true; |
9419 | } |
9420 | |
9421 | APSInt NumberOfElems; |
9422 | unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex(); |
9423 | if (!EvaluateAsSizeT(Call->getArg(Arg: NumArgNo), NumberOfElems)) |
9424 | return false; |
9425 | |
9426 | bool Overflow; |
9427 | llvm::APInt BytesAvailable = SizeOfElem.umul_ov(RHS: NumberOfElems, Overflow); |
9428 | if (Overflow) |
9429 | return false; |
9430 | |
9431 | Result = std::move(BytesAvailable); |
9432 | return true; |
9433 | } |
9434 | |
9435 | /// Convenience function. LVal's base must be a call to an alloc_size |
9436 | /// function. |
9437 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
9438 | const LValue &LVal, |
9439 | llvm::APInt &Result) { |
9440 | assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
9441 | "Can't get the size of a non alloc_size function"); |
9442 | const auto *Base = LVal.getLValueBase().get<const Expr *>(); |
9443 | const CallExpr *CE = tryUnwrapAllocSizeCall(E: Base); |
9444 | return getBytesReturnedByAllocSizeCall(Ctx, Call: CE, Result); |
9445 | } |
9446 | |
9447 | /// Attempts to evaluate the given LValueBase as the result of a call to |
9448 | /// a function with the alloc_size attribute. If it was possible to do so, this |
9449 | /// function will return true, make Result's Base point to said function call, |
9450 | /// and mark Result's Base as invalid. |
9451 | static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base, |
9452 | LValue &Result) { |
9453 | if (Base.isNull()) |
9454 | return false; |
9455 | |
9456 | // Because we do no form of static analysis, we only support const variables. |
9457 | // |
9458 | // Additionally, we can't support parameters, nor can we support static |
9459 | // variables (in the latter case, use-before-assign isn't UB; in the former, |
9460 | // we have no clue what they'll be assigned to). |
9461 | const auto *VD = |
9462 | dyn_cast_or_null<VarDecl>(Val: Base.dyn_cast<const ValueDecl *>()); |
9463 | if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified()) |
9464 | return false; |
9465 | |
9466 | const Expr *Init = VD->getAnyInitializer(); |
9467 | if (!Init || Init->getType().isNull()) |
9468 | return false; |
9469 | |
9470 | const Expr *E = Init->IgnoreParens(); |
9471 | if (!tryUnwrapAllocSizeCall(E)) |
9472 | return false; |
9473 | |
9474 | // Store E instead of E unwrapped so that the type of the LValue's base is |
9475 | // what the user wanted. |
9476 | Result.setInvalid(B: E); |
9477 | |
9478 | QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType(); |
9479 | Result.addUnsizedArray(Info, E, ElemTy: Pointee); |
9480 | return true; |
9481 | } |
9482 | |
9483 | namespace { |
9484 | class PointerExprEvaluator |
9485 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
9486 | LValue &Result; |
9487 | bool InvalidBaseOK; |
9488 | |
9489 | bool Success(const Expr *E) { |
9490 | Result.set(B: E); |
9491 | return true; |
9492 | } |
9493 | |
9494 | bool evaluateLValue(const Expr *E, LValue &Result) { |
9495 | return EvaluateLValue(E, Result, Info, InvalidBaseOK); |
9496 | } |
9497 | |
9498 | bool evaluatePointer(const Expr *E, LValue &Result) { |
9499 | return EvaluatePointer(E, Result, Info, InvalidBaseOK); |
9500 | } |
9501 | |
9502 | bool visitNonBuiltinCallExpr(const CallExpr *E); |
9503 | public: |
9504 | |
9505 | PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK) |
9506 | : ExprEvaluatorBaseTy(info), Result(Result), |
9507 | InvalidBaseOK(InvalidBaseOK) {} |
9508 | |
9509 | bool Success(const APValue &V, const Expr *E) { |
9510 | Result.setFrom(Ctx&: Info.Ctx, V); |
9511 | return true; |
9512 | } |
9513 | bool ZeroInitialization(const Expr *E) { |
9514 | Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType()); |
9515 | return true; |
9516 | } |
9517 | |
9518 | bool VisitBinaryOperator(const BinaryOperator *E); |
9519 | bool VisitCastExpr(const CastExpr* E); |
9520 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
9521 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
9522 | { return Success(E); } |
9523 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
9524 | if (E->isExpressibleAsConstantInitializer()) |
9525 | return Success(E); |
9526 | if (Info.noteFailure()) |
9527 | EvaluateIgnoredValue(Info, E: E->getSubExpr()); |
9528 | return Error(E); |
9529 | } |
9530 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
9531 | { return Success(E); } |
9532 | bool VisitCallExpr(const CallExpr *E); |
9533 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
9534 | bool VisitBlockExpr(const BlockExpr *E) { |
9535 | if (!E->getBlockDecl()->hasCaptures()) |
9536 | return Success(E); |
9537 | return Error(E); |
9538 | } |
9539 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
9540 | auto DiagnoseInvalidUseOfThis = [&] { |
9541 | if (Info.getLangOpts().CPlusPlus11) |
9542 | Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit(); |
9543 | else |
9544 | Info.FFDiag(E); |
9545 | }; |
9546 | |
9547 | // Can't look at 'this' when checking a potential constant expression. |
9548 | if (Info.checkingPotentialConstantExpression()) |
9549 | return false; |
9550 | |
9551 | bool IsExplicitLambda = |
9552 | isLambdaCallWithExplicitObjectParameter(Info.CurrentCall->Callee); |
9553 | if (!IsExplicitLambda) { |
9554 | if (!Info.CurrentCall->This) { |
9555 | DiagnoseInvalidUseOfThis(); |
9556 | return false; |
9557 | } |
9558 | |
9559 | Result = *Info.CurrentCall->This; |
9560 | } |
9561 | |
9562 | if (isLambdaCallOperator(Info.CurrentCall->Callee)) { |
9563 | // Ensure we actually have captured 'this'. If something was wrong with |
9564 | // 'this' capture, the error would have been previously reported. |
9565 | // Otherwise we can be inside of a default initialization of an object |
9566 | // declared by lambda's body, so no need to return false. |
9567 | if (!Info.CurrentCall->LambdaThisCaptureField) { |
9568 | if (IsExplicitLambda && !Info.CurrentCall->This) { |
9569 | DiagnoseInvalidUseOfThis(); |
9570 | return false; |
9571 | } |
9572 | |
9573 | return true; |
9574 | } |
9575 | |
9576 | const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee); |
9577 | return HandleLambdaCapture( |
9578 | Info, E, Result, MD, Info.CurrentCall->LambdaThisCaptureField, |
9579 | Info.CurrentCall->LambdaThisCaptureField->getType()->isPointerType()); |
9580 | } |
9581 | return true; |
9582 | } |
9583 | |
9584 | bool VisitCXXNewExpr(const CXXNewExpr *E); |
9585 | |
9586 | bool VisitSourceLocExpr(const SourceLocExpr *E) { |
9587 | assert(!E->isIntType() && "SourceLocExpr isn't a pointer type?"); |
9588 | APValue LValResult = E->EvaluateInContext( |
9589 | Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
9590 | Result.setFrom(Ctx&: Info.Ctx, V: LValResult); |
9591 | return true; |
9592 | } |
9593 | |
9594 | bool VisitEmbedExpr(const EmbedExpr *E) { |
9595 | llvm::report_fatal_error(reason: "Not yet implemented for ExprConstant.cpp"); |
9596 | return true; |
9597 | } |
9598 | |
9599 | bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) { |
9600 | std::string ResultStr = E->ComputeName(Context&: Info.Ctx); |
9601 | |
9602 | QualType CharTy = Info.Ctx.CharTy.withConst(); |
9603 | APInt Size(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType()), |
9604 | ResultStr.size() + 1); |
9605 | QualType ArrayTy = Info.Ctx.getConstantArrayType( |
9606 | EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9607 | |
9608 | StringLiteral *SL = |
9609 | StringLiteral::Create(Ctx: Info.Ctx, Str: ResultStr, Kind: StringLiteralKind::Ordinary, |
9610 | /*Pascal*/ false, Ty: ArrayTy, Loc: E->getLocation()); |
9611 | |
9612 | evaluateLValue(SL, Result); |
9613 | Result.addArray(Info, E, cast<ConstantArrayType>(Val&: ArrayTy)); |
9614 | return true; |
9615 | } |
9616 | |
9617 | // FIXME: Missing: @protocol, @selector |
9618 | }; |
9619 | } // end anonymous namespace |
9620 | |
9621 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info, |
9622 | bool InvalidBaseOK) { |
9623 | assert(!E->isValueDependent()); |
9624 | assert(E->isPRValue() && E->getType()->hasPointerRepresentation()); |
9625 | return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
9626 | } |
9627 | |
9628 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
9629 | if (E->getOpcode() != BO_Add && |
9630 | E->getOpcode() != BO_Sub) |
9631 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
9632 | |
9633 | const Expr *PExp = E->getLHS(); |
9634 | const Expr *IExp = E->getRHS(); |
9635 | if (IExp->getType()->isPointerType()) |
9636 | std::swap(a&: PExp, b&: IExp); |
9637 | |
9638 | bool EvalPtrOK = evaluatePointer(E: PExp, Result); |
9639 | if (!EvalPtrOK && !Info.noteFailure()) |
9640 | return false; |
9641 | |
9642 | llvm::APSInt Offset; |
9643 | if (!EvaluateInteger(E: IExp, Result&: Offset, Info) || !EvalPtrOK) |
9644 | return false; |
9645 | |
9646 | if (E->getOpcode() == BO_Sub) |
9647 | negateAsSigned(Int&: Offset); |
9648 | |
9649 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
9650 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset); |
9651 | } |
9652 | |
9653 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
9654 | return evaluateLValue(E: E->getSubExpr(), Result); |
9655 | } |
9656 | |
9657 | // Is the provided decl 'std::source_location::current'? |
9658 | static bool IsDeclSourceLocationCurrent(const FunctionDecl *FD) { |
9659 | if (!FD) |
9660 | return false; |
9661 | const IdentifierInfo *FnII = FD->getIdentifier(); |
9662 | if (!FnII || !FnII->isStr(Str: "current")) |
9663 | return false; |
9664 | |
9665 | const auto *RD = dyn_cast<RecordDecl>(FD->getParent()); |
9666 | if (!RD) |
9667 | return false; |
9668 | |
9669 | const IdentifierInfo *ClassII = RD->getIdentifier(); |
9670 | return RD->isInStdNamespace() && ClassII && ClassII->isStr(Str: "source_location"); |
9671 | } |
9672 | |
9673 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
9674 | const Expr *SubExpr = E->getSubExpr(); |
9675 | |
9676 | switch (E->getCastKind()) { |
9677 | default: |
9678 | break; |
9679 | case CK_BitCast: |
9680 | case CK_CPointerToObjCPointerCast: |
9681 | case CK_BlockPointerToObjCPointerCast: |
9682 | case CK_AnyPointerToBlockPointerCast: |
9683 | case CK_AddressSpaceConversion: |
9684 | if (!Visit(SubExpr)) |
9685 | return false; |
9686 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
9687 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
9688 | // also static_casts, but we disallow them as a resolution to DR1312. |
9689 | if (!E->getType()->isVoidPointerType()) { |
9690 | // In some circumstances, we permit casting from void* to cv1 T*, when the |
9691 | // actual pointee object is actually a cv2 T. |
9692 | bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid && |
9693 | !Result.IsNullPtr; |
9694 | bool VoidPtrCastMaybeOK = |
9695 | Result.IsNullPtr || |
9696 | (HasValidResult && |
9697 | Info.Ctx.hasSimilarType(T1: Result.Designator.getType(Info.Ctx), |
9698 | T2: E->getType()->getPointeeType())); |
9699 | // 1. We'll allow it in std::allocator::allocate, and anything which that |
9700 | // calls. |
9701 | // 2. HACK 2022-03-28: Work around an issue with libstdc++'s |
9702 | // <source_location> header. Fixed in GCC 12 and later (2022-04-??). |
9703 | // We'll allow it in the body of std::source_location::current. GCC's |
9704 | // implementation had a parameter of type `void*`, and casts from |
9705 | // that back to `const __impl*` in its body. |
9706 | if (VoidPtrCastMaybeOK && |
9707 | (Info.getStdAllocatorCaller(FnName: "allocate") || |
9708 | IsDeclSourceLocationCurrent(FD: Info.CurrentCall->Callee) || |
9709 | Info.getLangOpts().CPlusPlus26)) { |
9710 | // Permitted. |
9711 | } else { |
9712 | if (SubExpr->getType()->isVoidPointerType() && |
9713 | Info.getLangOpts().CPlusPlus) { |
9714 | if (HasValidResult) |
9715 | CCEDiag(E, diag::note_constexpr_invalid_void_star_cast) |
9716 | << SubExpr->getType() << Info.getLangOpts().CPlusPlus26 |
9717 | << Result.Designator.getType(Info.Ctx).getCanonicalType() |
9718 | << E->getType()->getPointeeType(); |
9719 | else |
9720 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
9721 | << diag::ConstexprInvalidCastKind::CastFrom |
9722 | << SubExpr->getType(); |
9723 | } else |
9724 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
9725 | << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret |
9726 | << Info.Ctx.getLangOpts().CPlusPlus; |
9727 | Result.Designator.setInvalid(); |
9728 | } |
9729 | } |
9730 | if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr) |
9731 | ZeroInitialization(E); |
9732 | return true; |
9733 | |
9734 | case CK_DerivedToBase: |
9735 | case CK_UncheckedDerivedToBase: |
9736 | if (!evaluatePointer(E: E->getSubExpr(), Result)) |
9737 | return false; |
9738 | if (!Result.Base && Result.Offset.isZero()) |
9739 | return true; |
9740 | |
9741 | // Now figure out the necessary offset to add to the base LV to get from |
9742 | // the derived class to the base class. |
9743 | return HandleLValueBasePath(Info, E, Type: E->getSubExpr()->getType()-> |
9744 | castAs<PointerType>()->getPointeeType(), |
9745 | Result); |
9746 | |
9747 | case CK_BaseToDerived: |
9748 | if (!Visit(E->getSubExpr())) |
9749 | return false; |
9750 | if (!Result.Base && Result.Offset.isZero()) |
9751 | return true; |
9752 | return HandleBaseToDerivedCast(Info, E, Result); |
9753 | |
9754 | case CK_Dynamic: |
9755 | if (!Visit(E->getSubExpr())) |
9756 | return false; |
9757 | return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result); |
9758 | |
9759 | case CK_NullToPointer: |
9760 | VisitIgnoredValue(E: E->getSubExpr()); |
9761 | return ZeroInitialization(E); |
9762 | |
9763 | case CK_IntegralToPointer: { |
9764 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
9765 | << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret |
9766 | << Info.Ctx.getLangOpts().CPlusPlus; |
9767 | |
9768 | APValue Value; |
9769 | if (!EvaluateIntegerOrLValue(E: SubExpr, Result&: Value, Info)) |
9770 | break; |
9771 | |
9772 | if (Value.isInt()) { |
9773 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
9774 | uint64_t N = Value.getInt().extOrTrunc(width: Size).getZExtValue(); |
9775 | Result.Base = (Expr*)nullptr; |
9776 | Result.InvalidBase = false; |
9777 | Result.Offset = CharUnits::fromQuantity(Quantity: N); |
9778 | Result.Designator.setInvalid(); |
9779 | Result.IsNullPtr = false; |
9780 | return true; |
9781 | } else { |
9782 | // In rare instances, the value isn't an lvalue. |
9783 | // For example, when the value is the difference between the addresses of |
9784 | // two labels. We reject that as a constant expression because we can't |
9785 | // compute a valid offset to convert into a pointer. |
9786 | if (!Value.isLValue()) |
9787 | return false; |
9788 | |
9789 | // Cast is of an lvalue, no need to change value. |
9790 | Result.setFrom(Ctx&: Info.Ctx, V: Value); |
9791 | return true; |
9792 | } |
9793 | } |
9794 | |
9795 | case CK_ArrayToPointerDecay: { |
9796 | if (SubExpr->isGLValue()) { |
9797 | if (!evaluateLValue(E: SubExpr, Result)) |
9798 | return false; |
9799 | } else { |
9800 | APValue &Value = Info.CurrentCall->createTemporary( |
9801 | Key: SubExpr, T: SubExpr->getType(), Scope: ScopeKind::FullExpression, LV&: Result); |
9802 | if (!EvaluateInPlace(Result&: Value, Info, This: Result, E: SubExpr)) |
9803 | return false; |
9804 | } |
9805 | // The result is a pointer to the first element of the array. |
9806 | auto *AT = Info.Ctx.getAsArrayType(T: SubExpr->getType()); |
9807 | if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) |
9808 | Result.addArray(Info, E, CAT); |
9809 | else |
9810 | Result.addUnsizedArray(Info, E, AT->getElementType()); |
9811 | return true; |
9812 | } |
9813 | |
9814 | case CK_FunctionToPointerDecay: |
9815 | return evaluateLValue(E: SubExpr, Result); |
9816 | |
9817 | case CK_LValueToRValue: { |
9818 | LValue LVal; |
9819 | if (!evaluateLValue(E: E->getSubExpr(), Result&: LVal)) |
9820 | return false; |
9821 | |
9822 | APValue RVal; |
9823 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
9824 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
9825 | LVal, RVal)) |
9826 | return InvalidBaseOK && |
9827 | evaluateLValueAsAllocSize(Info, Base: LVal.Base, Result); |
9828 | return Success(RVal, E); |
9829 | } |
9830 | } |
9831 | |
9832 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
9833 | } |
9834 | |
9835 | static CharUnits GetAlignOfType(const ASTContext &Ctx, QualType T, |
9836 | UnaryExprOrTypeTrait ExprKind) { |
9837 | // C++ [expr.alignof]p3: |
9838 | // When alignof is applied to a reference type, the result is the |
9839 | // alignment of the referenced type. |
9840 | T = T.getNonReferenceType(); |
9841 | |
9842 | if (T.getQualifiers().hasUnaligned()) |
9843 | return CharUnits::One(); |
9844 | |
9845 | const bool AlignOfReturnsPreferred = |
9846 | Ctx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7; |
9847 | |
9848 | // __alignof is defined to return the preferred alignment. |
9849 | // Before 8, clang returned the preferred alignment for alignof and _Alignof |
9850 | // as well. |
9851 | if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) |
9852 | return Ctx.toCharUnitsFromBits(BitSize: Ctx.getPreferredTypeAlign(T: T.getTypePtr())); |
9853 | // alignof and _Alignof are defined to return the ABI alignment. |
9854 | else if (ExprKind == UETT_AlignOf) |
9855 | return Ctx.getTypeAlignInChars(T: T.getTypePtr()); |
9856 | else |
9857 | llvm_unreachable("GetAlignOfType on a non-alignment ExprKind"); |
9858 | } |
9859 | |
9860 | CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E, |
9861 | UnaryExprOrTypeTrait ExprKind) { |
9862 | E = E->IgnoreParens(); |
9863 | |
9864 | // The kinds of expressions that we have special-case logic here for |
9865 | // should be kept up to date with the special checks for those |
9866 | // expressions in Sema. |
9867 | |
9868 | // alignof decl is always accepted, even if it doesn't make sense: we default |
9869 | // to 1 in those cases. |
9870 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E)) |
9871 | return Ctx.getDeclAlign(DRE->getDecl(), |
9872 | /*RefAsPointee*/ true); |
9873 | |
9874 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: E)) |
9875 | return Ctx.getDeclAlign(ME->getMemberDecl(), |
9876 | /*RefAsPointee*/ true); |
9877 | |
9878 | return GetAlignOfType(Ctx, T: E->getType(), ExprKind); |
9879 | } |
9880 | |
9881 | static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) { |
9882 | if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>()) |
9883 | return Info.Ctx.getDeclAlign(VD); |
9884 | if (const auto *E = Value.Base.dyn_cast<const Expr *>()) |
9885 | return GetAlignOfExpr(Ctx: Info.Ctx, E, ExprKind: UETT_AlignOf); |
9886 | return GetAlignOfType(Ctx: Info.Ctx, T: Value.Base.getTypeInfoType(), ExprKind: UETT_AlignOf); |
9887 | } |
9888 | |
9889 | /// Evaluate the value of the alignment argument to __builtin_align_{up,down}, |
9890 | /// __builtin_is_aligned and __builtin_assume_aligned. |
9891 | static bool getAlignmentArgument(const Expr *E, QualType ForType, |
9892 | EvalInfo &Info, APSInt &Alignment) { |
9893 | if (!EvaluateInteger(E, Result&: Alignment, Info)) |
9894 | return false; |
9895 | if (Alignment < 0 || !Alignment.isPowerOf2()) { |
9896 | Info.FFDiag(E, diag::note_constexpr_invalid_alignment) << Alignment; |
9897 | return false; |
9898 | } |
9899 | unsigned SrcWidth = Info.Ctx.getIntWidth(T: ForType); |
9900 | APSInt MaxValue(APInt::getOneBitSet(numBits: SrcWidth, BitNo: SrcWidth - 1)); |
9901 | if (APSInt::compareValues(I1: Alignment, I2: MaxValue) > 0) { |
9902 | Info.FFDiag(E, diag::note_constexpr_alignment_too_big) |
9903 | << MaxValue << ForType << Alignment; |
9904 | return false; |
9905 | } |
9906 | // Ensure both alignment and source value have the same bit width so that we |
9907 | // don't assert when computing the resulting value. |
9908 | APSInt ExtAlignment = |
9909 | APSInt(Alignment.zextOrTrunc(width: SrcWidth), /*isUnsigned=*/true); |
9910 | assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 && |
9911 | "Alignment should not be changed by ext/trunc"); |
9912 | Alignment = ExtAlignment; |
9913 | assert(Alignment.getBitWidth() == SrcWidth); |
9914 | return true; |
9915 | } |
9916 | |
9917 | // To be clear: this happily visits unsupported builtins. Better name welcomed. |
9918 | bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) { |
9919 | if (ExprEvaluatorBaseTy::VisitCallExpr(E)) |
9920 | return true; |
9921 | |
9922 | if (!(InvalidBaseOK && getAllocSizeAttr(E))) |
9923 | return false; |
9924 | |
9925 | Result.setInvalid(E); |
9926 | QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType(); |
9927 | Result.addUnsizedArray(Info, E, PointeeTy); |
9928 | return true; |
9929 | } |
9930 | |
9931 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
9932 | if (!IsConstantEvaluatedBuiltinCall(E)) |
9933 | return visitNonBuiltinCallExpr(E); |
9934 | return VisitBuiltinCallExpr(E, BuiltinOp: E->getBuiltinCallee()); |
9935 | } |
9936 | |
9937 | // Determine if T is a character type for which we guarantee that |
9938 | // sizeof(T) == 1. |
9939 | static bool isOneByteCharacterType(QualType T) { |
9940 | return T->isCharType() || T->isChar8Type(); |
9941 | } |
9942 | |
9943 | bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
9944 | unsigned BuiltinOp) { |
9945 | if (IsOpaqueConstantCall(E)) |
9946 | return Success(E); |
9947 | |
9948 | switch (BuiltinOp) { |
9949 | case Builtin::BIaddressof: |
9950 | case Builtin::BI__addressof: |
9951 | case Builtin::BI__builtin_addressof: |
9952 | return evaluateLValue(E: E->getArg(Arg: 0), Result); |
9953 | case Builtin::BI__builtin_assume_aligned: { |
9954 | // We need to be very careful here because: if the pointer does not have the |
9955 | // asserted alignment, then the behavior is undefined, and undefined |
9956 | // behavior is non-constant. |
9957 | if (!evaluatePointer(E: E->getArg(Arg: 0), Result)) |
9958 | return false; |
9959 | |
9960 | LValue OffsetResult(Result); |
9961 | APSInt Alignment; |
9962 | if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info, |
9963 | Alignment)) |
9964 | return false; |
9965 | CharUnits Align = CharUnits::fromQuantity(Quantity: Alignment.getZExtValue()); |
9966 | |
9967 | if (E->getNumArgs() > 2) { |
9968 | APSInt Offset; |
9969 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Offset, Info)) |
9970 | return false; |
9971 | |
9972 | int64_t AdditionalOffset = -Offset.getZExtValue(); |
9973 | OffsetResult.Offset += CharUnits::fromQuantity(Quantity: AdditionalOffset); |
9974 | } |
9975 | |
9976 | // If there is a base object, then it must have the correct alignment. |
9977 | if (OffsetResult.Base) { |
9978 | CharUnits BaseAlignment = getBaseAlignment(Info, Value: OffsetResult); |
9979 | |
9980 | if (BaseAlignment < Align) { |
9981 | Result.Designator.setInvalid(); |
9982 | CCEDiag(E->getArg(0), diag::note_constexpr_baa_insufficient_alignment) |
9983 | << 0 << BaseAlignment.getQuantity() << Align.getQuantity(); |
9984 | return false; |
9985 | } |
9986 | } |
9987 | |
9988 | // The offset must also have the correct alignment. |
9989 | if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) { |
9990 | Result.Designator.setInvalid(); |
9991 | |
9992 | (OffsetResult.Base |
9993 | ? CCEDiag(E->getArg(0), |
9994 | diag::note_constexpr_baa_insufficient_alignment) |
9995 | << 1 |
9996 | : CCEDiag(E->getArg(0), |
9997 | diag::note_constexpr_baa_value_insufficient_alignment)) |
9998 | << OffsetResult.Offset.getQuantity() << Align.getQuantity(); |
9999 | return false; |
10000 | } |
10001 | |
10002 | return true; |
10003 | } |
10004 | case Builtin::BI__builtin_align_up: |
10005 | case Builtin::BI__builtin_align_down: { |
10006 | if (!evaluatePointer(E: E->getArg(Arg: 0), Result)) |
10007 | return false; |
10008 | APSInt Alignment; |
10009 | if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info, |
10010 | Alignment)) |
10011 | return false; |
10012 | CharUnits BaseAlignment = getBaseAlignment(Info, Value: Result); |
10013 | CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Result.Offset); |
10014 | // For align_up/align_down, we can return the same value if the alignment |
10015 | // is known to be greater or equal to the requested value. |
10016 | if (PtrAlign.getQuantity() >= Alignment) |
10017 | return true; |
10018 | |
10019 | // The alignment could be greater than the minimum at run-time, so we cannot |
10020 | // infer much about the resulting pointer value. One case is possible: |
10021 | // For `_Alignas(32) char buf[N]; __builtin_align_down(&buf[idx], 32)` we |
10022 | // can infer the correct index if the requested alignment is smaller than |
10023 | // the base alignment so we can perform the computation on the offset. |
10024 | if (BaseAlignment.getQuantity() >= Alignment) { |
10025 | assert(Alignment.getBitWidth() <= 64 && |
10026 | "Cannot handle > 64-bit address-space"); |
10027 | uint64_t Alignment64 = Alignment.getZExtValue(); |
10028 | CharUnits NewOffset = CharUnits::fromQuantity( |
10029 | BuiltinOp == Builtin::BI__builtin_align_down |
10030 | ? llvm::alignDown(Result.Offset.getQuantity(), Alignment64) |
10031 | : llvm::alignTo(Result.Offset.getQuantity(), Alignment64)); |
10032 | Result.adjustOffset(N: NewOffset - Result.Offset); |
10033 | // TODO: diagnose out-of-bounds values/only allow for arrays? |
10034 | return true; |
10035 | } |
10036 | // Otherwise, we cannot constant-evaluate the result. |
10037 | Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_adjust) |
10038 | << Alignment; |
10039 | return false; |
10040 | } |
10041 | case Builtin::BI__builtin_operator_new: |
10042 | return HandleOperatorNewCall(Info, E, Result); |
10043 | case Builtin::BI__builtin_launder: |
10044 | return evaluatePointer(E: E->getArg(Arg: 0), Result); |
10045 | case Builtin::BIstrchr: |
10046 | case Builtin::BIwcschr: |
10047 | case Builtin::BImemchr: |
10048 | case Builtin::BIwmemchr: |
10049 | if (Info.getLangOpts().CPlusPlus11) |
10050 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
10051 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
10052 | << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp); |
10053 | else |
10054 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
10055 | [[fallthrough]]; |
10056 | case Builtin::BI__builtin_strchr: |
10057 | case Builtin::BI__builtin_wcschr: |
10058 | case Builtin::BI__builtin_memchr: |
10059 | case Builtin::BI__builtin_char_memchr: |
10060 | case Builtin::BI__builtin_wmemchr: { |
10061 | if (!Visit(E->getArg(Arg: 0))) |
10062 | return false; |
10063 | APSInt Desired; |
10064 | if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Desired, Info)) |
10065 | return false; |
10066 | uint64_t MaxLength = uint64_t(-1); |
10067 | if (BuiltinOp != Builtin::BIstrchr && |
10068 | BuiltinOp != Builtin::BIwcschr && |
10069 | BuiltinOp != Builtin::BI__builtin_strchr && |
10070 | BuiltinOp != Builtin::BI__builtin_wcschr) { |
10071 | APSInt N; |
10072 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info)) |
10073 | return false; |
10074 | MaxLength = N.getZExtValue(); |
10075 | } |
10076 | // We cannot find the value if there are no candidates to match against. |
10077 | if (MaxLength == 0u) |
10078 | return ZeroInitialization(E); |
10079 | if (!Result.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
10080 | Result.Designator.Invalid) |
10081 | return false; |
10082 | QualType CharTy = Result.Designator.getType(Info.Ctx); |
10083 | bool IsRawByte = BuiltinOp == Builtin::BImemchr || |
10084 | BuiltinOp == Builtin::BI__builtin_memchr; |
10085 | assert(IsRawByte || |
10086 | Info.Ctx.hasSameUnqualifiedType( |
10087 | CharTy, E->getArg(0)->getType()->getPointeeType())); |
10088 | // Pointers to const void may point to objects of incomplete type. |
10089 | if (IsRawByte && CharTy->isIncompleteType()) { |
10090 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy; |
10091 | return false; |
10092 | } |
10093 | // Give up on byte-oriented matching against multibyte elements. |
10094 | // FIXME: We can compare the bytes in the correct order. |
10095 | if (IsRawByte && !isOneByteCharacterType(T: CharTy)) { |
10096 | Info.FFDiag(E, diag::note_constexpr_memchr_unsupported) |
10097 | << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp) << CharTy; |
10098 | return false; |
10099 | } |
10100 | // Figure out what value we're actually looking for (after converting to |
10101 | // the corresponding unsigned type if necessary). |
10102 | uint64_t DesiredVal; |
10103 | bool StopAtNull = false; |
10104 | switch (BuiltinOp) { |
10105 | case Builtin::BIstrchr: |
10106 | case Builtin::BI__builtin_strchr: |
10107 | // strchr compares directly to the passed integer, and therefore |
10108 | // always fails if given an int that is not a char. |
10109 | if (!APSInt::isSameValue(I1: HandleIntToIntCast(Info, E, CharTy, |
10110 | E->getArg(Arg: 1)->getType(), |
10111 | Desired), |
10112 | I2: Desired)) |
10113 | return ZeroInitialization(E); |
10114 | StopAtNull = true; |
10115 | [[fallthrough]]; |
10116 | case Builtin::BImemchr: |
10117 | case Builtin::BI__builtin_memchr: |
10118 | case Builtin::BI__builtin_char_memchr: |
10119 | // memchr compares by converting both sides to unsigned char. That's also |
10120 | // correct for strchr if we get this far (to cope with plain char being |
10121 | // unsigned in the strchr case). |
10122 | DesiredVal = Desired.trunc(width: Info.Ctx.getCharWidth()).getZExtValue(); |
10123 | break; |
10124 | |
10125 | case Builtin::BIwcschr: |
10126 | case Builtin::BI__builtin_wcschr: |
10127 | StopAtNull = true; |
10128 | [[fallthrough]]; |
10129 | case Builtin::BIwmemchr: |
10130 | case Builtin::BI__builtin_wmemchr: |
10131 | // wcschr and wmemchr are given a wchar_t to look for. Just use it. |
10132 | DesiredVal = Desired.getZExtValue(); |
10133 | break; |
10134 | } |
10135 | |
10136 | for (; MaxLength; --MaxLength) { |
10137 | APValue Char; |
10138 | if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) || |
10139 | !Char.isInt()) |
10140 | return false; |
10141 | if (Char.getInt().getZExtValue() == DesiredVal) |
10142 | return true; |
10143 | if (StopAtNull && !Char.getInt()) |
10144 | break; |
10145 | if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1)) |
10146 | return false; |
10147 | } |
10148 | // Not found: return nullptr. |
10149 | return ZeroInitialization(E); |
10150 | } |
10151 | |
10152 | case Builtin::BImemcpy: |
10153 | case Builtin::BImemmove: |
10154 | case Builtin::BIwmemcpy: |
10155 | case Builtin::BIwmemmove: |
10156 | if (Info.getLangOpts().CPlusPlus11) |
10157 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
10158 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
10159 | << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp); |
10160 | else |
10161 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
10162 | [[fallthrough]]; |
10163 | case Builtin::BI__builtin_memcpy: |
10164 | case Builtin::BI__builtin_memmove: |
10165 | case Builtin::BI__builtin_wmemcpy: |
10166 | case Builtin::BI__builtin_wmemmove: { |
10167 | bool WChar = BuiltinOp == Builtin::BIwmemcpy || |
10168 | BuiltinOp == Builtin::BIwmemmove || |
10169 | BuiltinOp == Builtin::BI__builtin_wmemcpy || |
10170 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
10171 | bool Move = BuiltinOp == Builtin::BImemmove || |
10172 | BuiltinOp == Builtin::BIwmemmove || |
10173 | BuiltinOp == Builtin::BI__builtin_memmove || |
10174 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
10175 | |
10176 | // The result of mem* is the first argument. |
10177 | if (!Visit(E->getArg(Arg: 0))) |
10178 | return false; |
10179 | LValue Dest = Result; |
10180 | |
10181 | LValue Src; |
10182 | if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Src, Info)) |
10183 | return false; |
10184 | |
10185 | APSInt N; |
10186 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info)) |
10187 | return false; |
10188 | assert(!N.isSigned() && "memcpy and friends take an unsigned size"); |
10189 | |
10190 | // If the size is zero, we treat this as always being a valid no-op. |
10191 | // (Even if one of the src and dest pointers is null.) |
10192 | if (!N) |
10193 | return true; |
10194 | |
10195 | // Otherwise, if either of the operands is null, we can't proceed. Don't |
10196 | // try to determine the type of the copied objects, because there aren't |
10197 | // any. |
10198 | if (!Src.Base || !Dest.Base) { |
10199 | APValue Val; |
10200 | (!Src.Base ? Src : Dest).moveInto(V&: Val); |
10201 | Info.FFDiag(E, diag::note_constexpr_memcpy_null) |
10202 | << Move << WChar << !!Src.Base |
10203 | << Val.getAsString(Info.Ctx, E->getArg(0)->getType()); |
10204 | return false; |
10205 | } |
10206 | if (Src.Designator.Invalid || Dest.Designator.Invalid) |
10207 | return false; |
10208 | |
10209 | // We require that Src and Dest are both pointers to arrays of |
10210 | // trivially-copyable type. (For the wide version, the designator will be |
10211 | // invalid if the designated object is not a wchar_t.) |
10212 | QualType T = Dest.Designator.getType(Info.Ctx); |
10213 | QualType SrcT = Src.Designator.getType(Info.Ctx); |
10214 | if (!Info.Ctx.hasSameUnqualifiedType(T1: T, T2: SrcT)) { |
10215 | // FIXME: Consider using our bit_cast implementation to support this. |
10216 | Info.FFDiag(E, diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T; |
10217 | return false; |
10218 | } |
10219 | if (T->isIncompleteType()) { |
10220 | Info.FFDiag(E, diag::note_constexpr_memcpy_incomplete_type) << Move << T; |
10221 | return false; |
10222 | } |
10223 | if (!T.isTriviallyCopyableType(Context: Info.Ctx)) { |
10224 | Info.FFDiag(E, diag::note_constexpr_memcpy_nontrivial) << Move << T; |
10225 | return false; |
10226 | } |
10227 | |
10228 | // Figure out how many T's we're copying. |
10229 | uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity(); |
10230 | if (TSize == 0) |
10231 | return false; |
10232 | if (!WChar) { |
10233 | uint64_t Remainder; |
10234 | llvm::APInt OrigN = N; |
10235 | llvm::APInt::udivrem(LHS: OrigN, RHS: TSize, Quotient&: N, Remainder); |
10236 | if (Remainder) { |
10237 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
10238 | << Move << WChar << 0 << T << toString(OrigN, 10, /*Signed*/false) |
10239 | << (unsigned)TSize; |
10240 | return false; |
10241 | } |
10242 | } |
10243 | |
10244 | // Check that the copying will remain within the arrays, just so that we |
10245 | // can give a more meaningful diagnostic. This implicitly also checks that |
10246 | // N fits into 64 bits. |
10247 | uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second; |
10248 | uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second; |
10249 | if (N.ugt(RHS: RemainingSrcSize) || N.ugt(RHS: RemainingDestSize)) { |
10250 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
10251 | << Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T |
10252 | << toString(N, 10, /*Signed*/false); |
10253 | return false; |
10254 | } |
10255 | uint64_t NElems = N.getZExtValue(); |
10256 | uint64_t NBytes = NElems * TSize; |
10257 | |
10258 | // Check for overlap. |
10259 | int Direction = 1; |
10260 | if (HasSameBase(A: Src, B: Dest)) { |
10261 | uint64_t SrcOffset = Src.getLValueOffset().getQuantity(); |
10262 | uint64_t DestOffset = Dest.getLValueOffset().getQuantity(); |
10263 | if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) { |
10264 | // Dest is inside the source region. |
10265 | if (!Move) { |
10266 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
10267 | return false; |
10268 | } |
10269 | // For memmove and friends, copy backwards. |
10270 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, NElems - 1) || |
10271 | !HandleLValueArrayAdjustment(Info, E, Dest, T, NElems - 1)) |
10272 | return false; |
10273 | Direction = -1; |
10274 | } else if (!Move && SrcOffset >= DestOffset && |
10275 | SrcOffset - DestOffset < NBytes) { |
10276 | // Src is inside the destination region for memcpy: invalid. |
10277 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
10278 | return false; |
10279 | } |
10280 | } |
10281 | |
10282 | while (true) { |
10283 | APValue Val; |
10284 | // FIXME: Set WantObjectRepresentation to true if we're copying a |
10285 | // char-like type? |
10286 | if (!handleLValueToRValueConversion(Info, E, T, Src, Val) || |
10287 | !handleAssignment(Info, E, Dest, T, Val)) |
10288 | return false; |
10289 | // Do not iterate past the last element; if we're copying backwards, that |
10290 | // might take us off the start of the array. |
10291 | if (--NElems == 0) |
10292 | return true; |
10293 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, Direction) || |
10294 | !HandleLValueArrayAdjustment(Info, E, Dest, T, Direction)) |
10295 | return false; |
10296 | } |
10297 | } |
10298 | |
10299 | default: |
10300 | return false; |
10301 | } |
10302 | } |
10303 | |
10304 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
10305 | APValue &Result, const InitListExpr *ILE, |
10306 | QualType AllocType); |
10307 | static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This, |
10308 | APValue &Result, |
10309 | const CXXConstructExpr *CCE, |
10310 | QualType AllocType); |
10311 | |
10312 | bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) { |
10313 | if (!Info.getLangOpts().CPlusPlus20) |
10314 | Info.CCEDiag(E, diag::note_constexpr_new); |
10315 | |
10316 | // We cannot speculatively evaluate a delete expression. |
10317 | if (Info.SpeculativeEvaluationDepth) |
10318 | return false; |
10319 | |
10320 | FunctionDecl *OperatorNew = E->getOperatorNew(); |
10321 | QualType AllocType = E->getAllocatedType(); |
10322 | QualType TargetType = AllocType; |
10323 | |
10324 | bool IsNothrow = false; |
10325 | bool IsPlacement = false; |
10326 | |
10327 | if (E->getNumPlacementArgs() == 1 && |
10328 | E->getPlacementArg(I: 0)->getType()->isNothrowT()) { |
10329 | // The only new-placement list we support is of the form (std::nothrow). |
10330 | // |
10331 | // FIXME: There is no restriction on this, but it's not clear that any |
10332 | // other form makes any sense. We get here for cases such as: |
10333 | // |
10334 | // new (std::align_val_t{N}) X(int) |
10335 | // |
10336 | // (which should presumably be valid only if N is a multiple of |
10337 | // alignof(int), and in any case can't be deallocated unless N is |
10338 | // alignof(X) and X has new-extended alignment). |
10339 | LValue Nothrow; |
10340 | if (!EvaluateLValue(E: E->getPlacementArg(I: 0), Result&: Nothrow, Info)) |
10341 | return false; |
10342 | IsNothrow = true; |
10343 | } else if (OperatorNew->isReservedGlobalPlacementOperator()) { |
10344 | if (Info.CurrentCall->isStdFunction() || Info.getLangOpts().CPlusPlus26 || |
10345 | (Info.CurrentCall->CanEvalMSConstexpr && |
10346 | OperatorNew->hasAttr<MSConstexprAttr>())) { |
10347 | if (!EvaluatePointer(E: E->getPlacementArg(I: 0), Result, Info)) |
10348 | return false; |
10349 | if (Result.Designator.Invalid) |
10350 | return false; |
10351 | TargetType = E->getPlacementArg(I: 0)->getType(); |
10352 | IsPlacement = true; |
10353 | } else { |
10354 | Info.FFDiag(E, diag::note_constexpr_new_placement) |
10355 | << /*C++26 feature*/ 1 << E->getSourceRange(); |
10356 | return false; |
10357 | } |
10358 | } else if (E->getNumPlacementArgs()) { |
10359 | Info.FFDiag(E, diag::note_constexpr_new_placement) |
10360 | << /*Unsupported*/ 0 << E->getSourceRange(); |
10361 | return false; |
10362 | } else if (!OperatorNew |
10363 | ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) { |
10364 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
10365 | << isa<CXXMethodDecl>(OperatorNew) << OperatorNew; |
10366 | return false; |
10367 | } |
10368 | |
10369 | const Expr *Init = E->getInitializer(); |
10370 | const InitListExpr *ResizedArrayILE = nullptr; |
10371 | const CXXConstructExpr *ResizedArrayCCE = nullptr; |
10372 | bool ValueInit = false; |
10373 | |
10374 | if (std::optional<const Expr *> ArraySize = E->getArraySize()) { |
10375 | const Expr *Stripped = *ArraySize; |
10376 | for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped); |
10377 | Stripped = ICE->getSubExpr()) |
10378 | if (ICE->getCastKind() != CK_NoOp && |
10379 | ICE->getCastKind() != CK_IntegralCast) |
10380 | break; |
10381 | |
10382 | llvm::APSInt ArrayBound; |
10383 | if (!EvaluateInteger(E: Stripped, Result&: ArrayBound, Info)) |
10384 | return false; |
10385 | |
10386 | // C++ [expr.new]p9: |
10387 | // The expression is erroneous if: |
10388 | // -- [...] its value before converting to size_t [or] applying the |
10389 | // second standard conversion sequence is less than zero |
10390 | if (ArrayBound.isSigned() && ArrayBound.isNegative()) { |
10391 | if (IsNothrow) |
10392 | return ZeroInitialization(E); |
10393 | |
10394 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_negative) |
10395 | << ArrayBound << (*ArraySize)->getSourceRange(); |
10396 | return false; |
10397 | } |
10398 | |
10399 | // -- its value is such that the size of the allocated object would |
10400 | // exceed the implementation-defined limit |
10401 | if (!Info.CheckArraySize(Loc: ArraySize.value()->getExprLoc(), |
10402 | BitWidth: ConstantArrayType::getNumAddressingBits( |
10403 | Context: Info.Ctx, ElementType: AllocType, NumElements: ArrayBound), |
10404 | ElemCount: ArrayBound.getZExtValue(), /*Diag=*/!IsNothrow)) { |
10405 | if (IsNothrow) |
10406 | return ZeroInitialization(E); |
10407 | return false; |
10408 | } |
10409 | |
10410 | // -- the new-initializer is a braced-init-list and the number of |
10411 | // array elements for which initializers are provided [...] |
10412 | // exceeds the number of elements to initialize |
10413 | if (!Init) { |
10414 | // No initialization is performed. |
10415 | } else if (isa<CXXScalarValueInitExpr>(Val: Init) || |
10416 | isa<ImplicitValueInitExpr>(Val: Init)) { |
10417 | ValueInit = true; |
10418 | } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Val: Init)) { |
10419 | ResizedArrayCCE = CCE; |
10420 | } else { |
10421 | auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType()); |
10422 | assert(CAT && "unexpected type for array initializer"); |
10423 | |
10424 | unsigned Bits = |
10425 | std::max(a: CAT->getSizeBitWidth(), b: ArrayBound.getBitWidth()); |
10426 | llvm::APInt InitBound = CAT->getSize().zext(width: Bits); |
10427 | llvm::APInt AllocBound = ArrayBound.zext(width: Bits); |
10428 | if (InitBound.ugt(RHS: AllocBound)) { |
10429 | if (IsNothrow) |
10430 | return ZeroInitialization(E); |
10431 | |
10432 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_small) |
10433 | << toString(AllocBound, 10, /*Signed=*/false) |
10434 | << toString(InitBound, 10, /*Signed=*/false) |
10435 | << (*ArraySize)->getSourceRange(); |
10436 | return false; |
10437 | } |
10438 | |
10439 | // If the sizes differ, we must have an initializer list, and we need |
10440 | // special handling for this case when we initialize. |
10441 | if (InitBound != AllocBound) |
10442 | ResizedArrayILE = cast<InitListExpr>(Val: Init); |
10443 | } |
10444 | |
10445 | AllocType = Info.Ctx.getConstantArrayType(EltTy: AllocType, ArySize: ArrayBound, SizeExpr: nullptr, |
10446 | ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
10447 | } else { |
10448 | assert(!AllocType->isArrayType() && |
10449 | "array allocation with non-array new"); |
10450 | } |
10451 | |
10452 | APValue *Val; |
10453 | if (IsPlacement) { |
10454 | AccessKinds AK = AK_Construct; |
10455 | struct FindObjectHandler { |
10456 | EvalInfo &Info; |
10457 | const Expr *E; |
10458 | QualType AllocType; |
10459 | const AccessKinds AccessKind; |
10460 | APValue *Value; |
10461 | |
10462 | typedef bool result_type; |
10463 | bool failed() { return false; } |
10464 | bool checkConst(QualType QT) { |
10465 | if (QT.isConstQualified()) { |
10466 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
10467 | return false; |
10468 | } |
10469 | return true; |
10470 | } |
10471 | bool found(APValue &Subobj, QualType SubobjType) { |
10472 | if (!checkConst(QT: SubobjType)) |
10473 | return false; |
10474 | // FIXME: Reject the cases where [basic.life]p8 would not permit the |
10475 | // old name of the object to be used to name the new object. |
10476 | unsigned SubobjectSize = 1; |
10477 | unsigned AllocSize = 1; |
10478 | if (auto *CAT = dyn_cast<ConstantArrayType>(AllocType)) |
10479 | AllocSize = CAT->getZExtSize(); |
10480 | if (auto *CAT = dyn_cast<ConstantArrayType>(Val&: SubobjType)) |
10481 | SubobjectSize = CAT->getZExtSize(); |
10482 | if (SubobjectSize < AllocSize || |
10483 | !Info.Ctx.hasSimilarType(Info.Ctx.getBaseElementType(SubobjType), |
10484 | Info.Ctx.getBaseElementType(AllocType))) { |
10485 | Info.FFDiag(E, diag::note_constexpr_placement_new_wrong_type) |
10486 | << SubobjType << AllocType; |
10487 | return false; |
10488 | } |
10489 | Value = &Subobj; |
10490 | return true; |
10491 | } |
10492 | bool found(APSInt &Value, QualType SubobjType) { |
10493 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
10494 | return false; |
10495 | } |
10496 | bool found(APFloat &Value, QualType SubobjType) { |
10497 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
10498 | return false; |
10499 | } |
10500 | } Handler = {Info, E, AllocType, AK, nullptr}; |
10501 | |
10502 | CompleteObject Obj = findCompleteObject(Info, E, AK, Result, AllocType); |
10503 | if (!Obj || !findSubobject(Info, E, Obj, Result.Designator, Handler)) |
10504 | return false; |
10505 | |
10506 | Val = Handler.Value; |
10507 | |
10508 | // [basic.life]p1: |
10509 | // The lifetime of an object o of type T ends when [...] the storage |
10510 | // which the object occupies is [...] reused by an object that is not |
10511 | // nested within o (6.6.2). |
10512 | *Val = APValue(); |
10513 | } else { |
10514 | // Perform the allocation and obtain a pointer to the resulting object. |
10515 | Val = Info.createHeapAlloc(E, AllocType, Result); |
10516 | if (!Val) |
10517 | return false; |
10518 | } |
10519 | |
10520 | if (ValueInit) { |
10521 | ImplicitValueInitExpr VIE(AllocType); |
10522 | if (!EvaluateInPlace(*Val, Info, Result, &VIE)) |
10523 | return false; |
10524 | } else if (ResizedArrayILE) { |
10525 | if (!EvaluateArrayNewInitList(Info, This&: Result, Result&: *Val, ILE: ResizedArrayILE, |
10526 | AllocType)) |
10527 | return false; |
10528 | } else if (ResizedArrayCCE) { |
10529 | if (!EvaluateArrayNewConstructExpr(Info, This&: Result, Result&: *Val, CCE: ResizedArrayCCE, |
10530 | AllocType)) |
10531 | return false; |
10532 | } else if (Init) { |
10533 | if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: Init)) |
10534 | return false; |
10535 | } else if (!handleDefaultInitValue(T: AllocType, Result&: *Val)) { |
10536 | return false; |
10537 | } |
10538 | |
10539 | // Array new returns a pointer to the first element, not a pointer to the |
10540 | // array. |
10541 | if (auto *AT = AllocType->getAsArrayTypeUnsafe()) |
10542 | Result.addArray(Info, E, CAT: cast<ConstantArrayType>(AT)); |
10543 | |
10544 | return true; |
10545 | } |
10546 | //===----------------------------------------------------------------------===// |
10547 | // Member Pointer Evaluation |
10548 | //===----------------------------------------------------------------------===// |
10549 | |
10550 | namespace { |
10551 | class MemberPointerExprEvaluator |
10552 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
10553 | MemberPtr &Result; |
10554 | |
10555 | bool Success(const ValueDecl *D) { |
10556 | Result = MemberPtr(D); |
10557 | return true; |
10558 | } |
10559 | public: |
10560 | |
10561 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
10562 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
10563 | |
10564 | bool Success(const APValue &V, const Expr *E) { |
10565 | Result.setFrom(V); |
10566 | return true; |
10567 | } |
10568 | bool ZeroInitialization(const Expr *E) { |
10569 | return Success(D: (const ValueDecl*)nullptr); |
10570 | } |
10571 | |
10572 | bool VisitCastExpr(const CastExpr *E); |
10573 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
10574 | }; |
10575 | } // end anonymous namespace |
10576 | |
10577 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
10578 | EvalInfo &Info) { |
10579 | assert(!E->isValueDependent()); |
10580 | assert(E->isPRValue() && E->getType()->isMemberPointerType()); |
10581 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
10582 | } |
10583 | |
10584 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10585 | switch (E->getCastKind()) { |
10586 | default: |
10587 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10588 | |
10589 | case CK_NullToMemberPointer: |
10590 | VisitIgnoredValue(E: E->getSubExpr()); |
10591 | return ZeroInitialization(E); |
10592 | |
10593 | case CK_BaseToDerivedMemberPointer: { |
10594 | if (!Visit(E->getSubExpr())) |
10595 | return false; |
10596 | if (E->path_empty()) |
10597 | return true; |
10598 | // Base-to-derived member pointer casts store the path in derived-to-base |
10599 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
10600 | // the wrong end of the derived->base arc, so stagger the path by one class. |
10601 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
10602 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
10603 | PathI != PathE; ++PathI) { |
10604 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
10605 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
10606 | if (!Result.castToDerived(Derived)) |
10607 | return Error(E); |
10608 | } |
10609 | if (!Result.castToDerived(Derived: E->getType() |
10610 | ->castAs<MemberPointerType>() |
10611 | ->getMostRecentCXXRecordDecl())) |
10612 | return Error(E); |
10613 | return true; |
10614 | } |
10615 | |
10616 | case CK_DerivedToBaseMemberPointer: |
10617 | if (!Visit(E->getSubExpr())) |
10618 | return false; |
10619 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
10620 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
10621 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
10622 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
10623 | if (!Result.castToBase(Base)) |
10624 | return Error(E); |
10625 | } |
10626 | return true; |
10627 | } |
10628 | } |
10629 | |
10630 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
10631 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
10632 | // member can be formed. |
10633 | return Success(D: cast<DeclRefExpr>(Val: E->getSubExpr())->getDecl()); |
10634 | } |
10635 | |
10636 | //===----------------------------------------------------------------------===// |
10637 | // Record Evaluation |
10638 | //===----------------------------------------------------------------------===// |
10639 | |
10640 | namespace { |
10641 | class RecordExprEvaluator |
10642 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
10643 | const LValue &This; |
10644 | APValue &Result; |
10645 | public: |
10646 | |
10647 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
10648 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
10649 | |
10650 | bool Success(const APValue &V, const Expr *E) { |
10651 | Result = V; |
10652 | return true; |
10653 | } |
10654 | bool ZeroInitialization(const Expr *E) { |
10655 | return ZeroInitialization(E, T: E->getType()); |
10656 | } |
10657 | bool ZeroInitialization(const Expr *E, QualType T); |
10658 | |
10659 | bool VisitCallExpr(const CallExpr *E) { |
10660 | return handleCallExpr(E, Result, ResultSlot: &This); |
10661 | } |
10662 | bool VisitCastExpr(const CastExpr *E); |
10663 | bool VisitInitListExpr(const InitListExpr *E); |
10664 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
10665 | return VisitCXXConstructExpr(E, E->getType()); |
10666 | } |
10667 | bool VisitLambdaExpr(const LambdaExpr *E); |
10668 | bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E); |
10669 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T); |
10670 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
10671 | bool VisitBinCmp(const BinaryOperator *E); |
10672 | bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E); |
10673 | bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit, |
10674 | ArrayRef<Expr *> Args); |
10675 | }; |
10676 | } |
10677 | |
10678 | /// Perform zero-initialization on an object of non-union class type. |
10679 | /// C++11 [dcl.init]p5: |
10680 | /// To zero-initialize an object or reference of type T means: |
10681 | /// [...] |
10682 | /// -- if T is a (possibly cv-qualified) non-union class type, |
10683 | /// each non-static data member and each base-class subobject is |
10684 | /// zero-initialized |
10685 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
10686 | const RecordDecl *RD, |
10687 | const LValue &This, APValue &Result) { |
10688 | assert(!RD->isUnion() && "Expected non-union class type"); |
10689 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD); |
10690 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
10691 | std::distance(first: RD->field_begin(), last: RD->field_end())); |
10692 | |
10693 | if (RD->isInvalidDecl()) return false; |
10694 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
10695 | |
10696 | if (CD) { |
10697 | unsigned Index = 0; |
10698 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
10699 | End = CD->bases_end(); I != End; ++I, ++Index) { |
10700 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
10701 | LValue Subobject = This; |
10702 | if (!HandleLValueDirectBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout)) |
10703 | return false; |
10704 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
10705 | Result.getStructBase(i: Index))) |
10706 | return false; |
10707 | } |
10708 | } |
10709 | |
10710 | for (const auto *I : RD->fields()) { |
10711 | // -- if T is a reference type, no initialization is performed. |
10712 | if (I->isUnnamedBitField() || I->getType()->isReferenceType()) |
10713 | continue; |
10714 | |
10715 | LValue Subobject = This; |
10716 | if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: I, RL: &Layout)) |
10717 | return false; |
10718 | |
10719 | ImplicitValueInitExpr VIE(I->getType()); |
10720 | if (!EvaluateInPlace( |
10721 | Result.getStructField(i: I->getFieldIndex()), Info, Subobject, &VIE)) |
10722 | return false; |
10723 | } |
10724 | |
10725 | return true; |
10726 | } |
10727 | |
10728 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) { |
10729 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
10730 | if (RD->isInvalidDecl()) return false; |
10731 | if (RD->isUnion()) { |
10732 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
10733 | // object's first non-static named data member is zero-initialized |
10734 | RecordDecl::field_iterator I = RD->field_begin(); |
10735 | while (I != RD->field_end() && (*I)->isUnnamedBitField()) |
10736 | ++I; |
10737 | if (I == RD->field_end()) { |
10738 | Result = APValue((const FieldDecl*)nullptr); |
10739 | return true; |
10740 | } |
10741 | |
10742 | LValue Subobject = This; |
10743 | if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: *I)) |
10744 | return false; |
10745 | Result = APValue(*I); |
10746 | ImplicitValueInitExpr VIE(I->getType()); |
10747 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
10748 | } |
10749 | |
10750 | if (isa<CXXRecordDecl>(Val: RD) && cast<CXXRecordDecl>(Val: RD)->getNumVBases()) { |
10751 | Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD; |
10752 | return false; |
10753 | } |
10754 | |
10755 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
10756 | } |
10757 | |
10758 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10759 | switch (E->getCastKind()) { |
10760 | default: |
10761 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10762 | |
10763 | case CK_ConstructorConversion: |
10764 | return Visit(E->getSubExpr()); |
10765 | |
10766 | case CK_DerivedToBase: |
10767 | case CK_UncheckedDerivedToBase: { |
10768 | APValue DerivedObject; |
10769 | if (!Evaluate(Result&: DerivedObject, Info, E: E->getSubExpr())) |
10770 | return false; |
10771 | if (!DerivedObject.isStruct()) |
10772 | return Error(E: E->getSubExpr()); |
10773 | |
10774 | // Derived-to-base rvalue conversion: just slice off the derived part. |
10775 | APValue *Value = &DerivedObject; |
10776 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
10777 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
10778 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
10779 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
10780 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
10781 | Value = &Value->getStructBase(i: getBaseIndex(Derived: RD, Base)); |
10782 | RD = Base; |
10783 | } |
10784 | Result = *Value; |
10785 | return true; |
10786 | } |
10787 | } |
10788 | } |
10789 | |
10790 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
10791 | if (E->isTransparent()) |
10792 | return Visit(E->getInit(Init: 0)); |
10793 | return VisitCXXParenListOrInitListExpr(E, E->inits()); |
10794 | } |
10795 | |
10796 | bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr( |
10797 | const Expr *ExprToVisit, ArrayRef<Expr *> Args) { |
10798 | const RecordDecl *RD = |
10799 | ExprToVisit->getType()->castAs<RecordType>()->getDecl(); |
10800 | if (RD->isInvalidDecl()) return false; |
10801 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
10802 | auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD); |
10803 | |
10804 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
10805 | Info, |
10806 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
10807 | CXXRD && CXXRD->getNumBases()); |
10808 | |
10809 | if (RD->isUnion()) { |
10810 | const FieldDecl *Field; |
10811 | if (auto *ILE = dyn_cast<InitListExpr>(Val: ExprToVisit)) { |
10812 | Field = ILE->getInitializedFieldInUnion(); |
10813 | } else if (auto *PLIE = dyn_cast<CXXParenListInitExpr>(Val: ExprToVisit)) { |
10814 | Field = PLIE->getInitializedFieldInUnion(); |
10815 | } else { |
10816 | llvm_unreachable( |
10817 | "Expression is neither an init list nor a C++ paren list"); |
10818 | } |
10819 | |
10820 | Result = APValue(Field); |
10821 | if (!Field) |
10822 | return true; |
10823 | |
10824 | // If the initializer list for a union does not contain any elements, the |
10825 | // first element of the union is value-initialized. |
10826 | // FIXME: The element should be initialized from an initializer list. |
10827 | // Is this difference ever observable for initializer lists which |
10828 | // we don't build? |
10829 | ImplicitValueInitExpr VIE(Field->getType()); |
10830 | const Expr *InitExpr = Args.empty() ? &VIE : Args[0]; |
10831 | |
10832 | LValue Subobject = This; |
10833 | if (!HandleLValueMember(Info, E: InitExpr, LVal&: Subobject, FD: Field, RL: &Layout)) |
10834 | return false; |
10835 | |
10836 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
10837 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
10838 | isa<CXXDefaultInitExpr>(Val: InitExpr)); |
10839 | |
10840 | if (EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: InitExpr)) { |
10841 | if (Field->isBitField()) |
10842 | return truncateBitfieldValue(Info, E: InitExpr, Value&: Result.getUnionValue(), |
10843 | FD: Field); |
10844 | return true; |
10845 | } |
10846 | |
10847 | return false; |
10848 | } |
10849 | |
10850 | if (!Result.hasValue()) |
10851 | Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0, |
10852 | std::distance(first: RD->field_begin(), last: RD->field_end())); |
10853 | unsigned ElementNo = 0; |
10854 | bool Success = true; |
10855 | |
10856 | // Initialize base classes. |
10857 | if (CXXRD && CXXRD->getNumBases()) { |
10858 | for (const auto &Base : CXXRD->bases()) { |
10859 | assert(ElementNo < Args.size() && "missing init for base class"); |
10860 | const Expr *Init = Args[ElementNo]; |
10861 | |
10862 | LValue Subobject = This; |
10863 | if (!HandleLValueBase(Info, E: Init, Obj&: Subobject, DerivedDecl: CXXRD, Base: &Base)) |
10864 | return false; |
10865 | |
10866 | APValue &FieldVal = Result.getStructBase(i: ElementNo); |
10867 | if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init)) { |
10868 | if (!Info.noteFailure()) |
10869 | return false; |
10870 | Success = false; |
10871 | } |
10872 | ++ElementNo; |
10873 | } |
10874 | |
10875 | EvalObj.finishedConstructingBases(); |
10876 | } |
10877 | |
10878 | // Initialize members. |
10879 | for (const auto *Field : RD->fields()) { |
10880 | // Anonymous bit-fields are not considered members of the class for |
10881 | // purposes of aggregate initialization. |
10882 | if (Field->isUnnamedBitField()) |
10883 | continue; |
10884 | |
10885 | LValue Subobject = This; |
10886 | |
10887 | bool HaveInit = ElementNo < Args.size(); |
10888 | |
10889 | // FIXME: Diagnostics here should point to the end of the initializer |
10890 | // list, not the start. |
10891 | if (!HandleLValueMember(Info, E: HaveInit ? Args[ElementNo] : ExprToVisit, |
10892 | LVal&: Subobject, FD: Field, RL: &Layout)) |
10893 | return false; |
10894 | |
10895 | // Perform an implicit value-initialization for members beyond the end of |
10896 | // the initializer list. |
10897 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
10898 | const Expr *Init = HaveInit ? Args[ElementNo++] : &VIE; |
10899 | |
10900 | if (Field->getType()->isIncompleteArrayType()) { |
10901 | if (auto *CAT = Info.Ctx.getAsConstantArrayType(Init->getType())) { |
10902 | if (!CAT->isZeroSize()) { |
10903 | // Bail out for now. This might sort of "work", but the rest of the |
10904 | // code isn't really prepared to handle it. |
10905 | Info.FFDiag(Init, diag::note_constexpr_unsupported_flexible_array); |
10906 | return false; |
10907 | } |
10908 | } |
10909 | } |
10910 | |
10911 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
10912 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
10913 | isa<CXXDefaultInitExpr>(Val: Init)); |
10914 | |
10915 | APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex()); |
10916 | if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init) || |
10917 | (Field->isBitField() && !truncateBitfieldValue(Info, E: Init, |
10918 | Value&: FieldVal, FD: Field))) { |
10919 | if (!Info.noteFailure()) |
10920 | return false; |
10921 | Success = false; |
10922 | } |
10923 | } |
10924 | |
10925 | EvalObj.finishedConstructingFields(); |
10926 | |
10927 | return Success; |
10928 | } |
10929 | |
10930 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
10931 | QualType T) { |
10932 | // Note that E's type is not necessarily the type of our class here; we might |
10933 | // be initializing an array element instead. |
10934 | const CXXConstructorDecl *FD = E->getConstructor(); |
10935 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
10936 | |
10937 | bool ZeroInit = E->requiresZeroInitialization(); |
10938 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
10939 | // If we've already performed zero-initialization, we're already done. |
10940 | if (Result.hasValue()) |
10941 | return true; |
10942 | |
10943 | if (ZeroInit) |
10944 | return ZeroInitialization(E, T); |
10945 | |
10946 | return handleDefaultInitValue(T, Result); |
10947 | } |
10948 | |
10949 | const FunctionDecl *Definition = nullptr; |
10950 | auto Body = FD->getBody(Definition); |
10951 | |
10952 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
10953 | return false; |
10954 | |
10955 | // Avoid materializing a temporary for an elidable copy/move constructor. |
10956 | if (E->isElidable() && !ZeroInit) { |
10957 | // FIXME: This only handles the simplest case, where the source object |
10958 | // is passed directly as the first argument to the constructor. |
10959 | // This should also handle stepping though implicit casts and |
10960 | // and conversion sequences which involve two steps, with a |
10961 | // conversion operator followed by a converting constructor. |
10962 | const Expr *SrcObj = E->getArg(Arg: 0); |
10963 | assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent())); |
10964 | assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType())); |
10965 | if (const MaterializeTemporaryExpr *ME = |
10966 | dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj)) |
10967 | return Visit(ME->getSubExpr()); |
10968 | } |
10969 | |
10970 | if (ZeroInit && !ZeroInitialization(E, T)) |
10971 | return false; |
10972 | |
10973 | auto Args = llvm::ArrayRef(E->getArgs(), E->getNumArgs()); |
10974 | return HandleConstructorCall(E, This, Args, |
10975 | cast<CXXConstructorDecl>(Val: Definition), Info, |
10976 | Result); |
10977 | } |
10978 | |
10979 | bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr( |
10980 | const CXXInheritedCtorInitExpr *E) { |
10981 | if (!Info.CurrentCall) { |
10982 | assert(Info.checkingPotentialConstantExpression()); |
10983 | return false; |
10984 | } |
10985 | |
10986 | const CXXConstructorDecl *FD = E->getConstructor(); |
10987 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) |
10988 | return false; |
10989 | |
10990 | const FunctionDecl *Definition = nullptr; |
10991 | auto Body = FD->getBody(Definition); |
10992 | |
10993 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
10994 | return false; |
10995 | |
10996 | return HandleConstructorCall(E, This, Info.CurrentCall->Arguments, |
10997 | cast<CXXConstructorDecl>(Val: Definition), Info, |
10998 | Result); |
10999 | } |
11000 | |
11001 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
11002 | const CXXStdInitializerListExpr *E) { |
11003 | const ConstantArrayType *ArrayType = |
11004 | Info.Ctx.getAsConstantArrayType(T: E->getSubExpr()->getType()); |
11005 | |
11006 | LValue Array; |
11007 | if (!EvaluateLValue(E: E->getSubExpr(), Result&: Array, Info)) |
11008 | return false; |
11009 | |
11010 | assert(ArrayType && "unexpected type for array initializer"); |
11011 | |
11012 | // Get a pointer to the first element of the array. |
11013 | Array.addArray(Info, E, ArrayType); |
11014 | |
11015 | // FIXME: What if the initializer_list type has base classes, etc? |
11016 | Result = APValue(APValue::UninitStruct(), 0, 2); |
11017 | Array.moveInto(V&: Result.getStructField(i: 0)); |
11018 | |
11019 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
11020 | RecordDecl::field_iterator Field = Record->field_begin(); |
11021 | assert(Field != Record->field_end() && |
11022 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
11023 | ArrayType->getElementType()) && |
11024 | "Expected std::initializer_list first field to be const E *"); |
11025 | ++Field; |
11026 | assert(Field != Record->field_end() && |
11027 | "Expected std::initializer_list to have two fields"); |
11028 | |
11029 | if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) { |
11030 | // Length. |
11031 | Result.getStructField(i: 1) = APValue(APSInt(ArrayType->getSize())); |
11032 | } else { |
11033 | // End pointer. |
11034 | assert(Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
11035 | ArrayType->getElementType()) && |
11036 | "Expected std::initializer_list second field to be const E *"); |
11037 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
11038 | ArrayType->getElementType(), |
11039 | ArrayType->getZExtSize())) |
11040 | return false; |
11041 | Array.moveInto(V&: Result.getStructField(i: 1)); |
11042 | } |
11043 | |
11044 | assert(++Field == Record->field_end() && |
11045 | "Expected std::initializer_list to only have two fields"); |
11046 | |
11047 | return true; |
11048 | } |
11049 | |
11050 | bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) { |
11051 | const CXXRecordDecl *ClosureClass = E->getLambdaClass(); |
11052 | if (ClosureClass->isInvalidDecl()) |
11053 | return false; |
11054 | |
11055 | const size_t NumFields = |
11056 | std::distance(ClosureClass->field_begin(), ClosureClass->field_end()); |
11057 | |
11058 | assert(NumFields == (size_t)std::distance(E->capture_init_begin(), |
11059 | E->capture_init_end()) && |
11060 | "The number of lambda capture initializers should equal the number of " |
11061 | "fields within the closure type"); |
11062 | |
11063 | Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields); |
11064 | // Iterate through all the lambda's closure object's fields and initialize |
11065 | // them. |
11066 | auto *CaptureInitIt = E->capture_init_begin(); |
11067 | bool Success = true; |
11068 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(ClosureClass); |
11069 | for (const auto *Field : ClosureClass->fields()) { |
11070 | assert(CaptureInitIt != E->capture_init_end()); |
11071 | // Get the initializer for this field |
11072 | Expr *const CurFieldInit = *CaptureInitIt++; |
11073 | |
11074 | // If there is no initializer, either this is a VLA or an error has |
11075 | // occurred. |
11076 | if (!CurFieldInit || CurFieldInit->containsErrors()) |
11077 | return Error(E); |
11078 | |
11079 | LValue Subobject = This; |
11080 | |
11081 | if (!HandleLValueMember(Info, E, Subobject, Field, &Layout)) |
11082 | return false; |
11083 | |
11084 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
11085 | if (!EvaluateInPlace(FieldVal, Info, Subobject, CurFieldInit)) { |
11086 | if (!Info.keepEvaluatingAfterFailure()) |
11087 | return false; |
11088 | Success = false; |
11089 | } |
11090 | } |
11091 | return Success; |
11092 | } |
11093 | |
11094 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
11095 | APValue &Result, EvalInfo &Info) { |
11096 | assert(!E->isValueDependent()); |
11097 | assert(E->isPRValue() && E->getType()->isRecordType() && |
11098 | "can't evaluate expression as a record rvalue"); |
11099 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
11100 | } |
11101 | |
11102 | //===----------------------------------------------------------------------===// |
11103 | // Temporary Evaluation |
11104 | // |
11105 | // Temporaries are represented in the AST as rvalues, but generally behave like |
11106 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
11107 | // materialized so that a reference can bind to it. |
11108 | //===----------------------------------------------------------------------===// |
11109 | namespace { |
11110 | class TemporaryExprEvaluator |
11111 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
11112 | public: |
11113 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
11114 | LValueExprEvaluatorBaseTy(Info, Result, false) {} |
11115 | |
11116 | /// Visit an expression which constructs the value of this temporary. |
11117 | bool VisitConstructExpr(const Expr *E) { |
11118 | APValue &Value = Info.CurrentCall->createTemporary( |
11119 | Key: E, T: E->getType(), Scope: ScopeKind::FullExpression, LV&: Result); |
11120 | return EvaluateInPlace(Result&: Value, Info, This: Result, E); |
11121 | } |
11122 | |
11123 | bool VisitCastExpr(const CastExpr *E) { |
11124 | switch (E->getCastKind()) { |
11125 | default: |
11126 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
11127 | |
11128 | case CK_ConstructorConversion: |
11129 | return VisitConstructExpr(E: E->getSubExpr()); |
11130 | } |
11131 | } |
11132 | bool VisitInitListExpr(const InitListExpr *E) { |
11133 | return VisitConstructExpr(E); |
11134 | } |
11135 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
11136 | return VisitConstructExpr(E); |
11137 | } |
11138 | bool VisitCallExpr(const CallExpr *E) { |
11139 | return VisitConstructExpr(E); |
11140 | } |
11141 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
11142 | return VisitConstructExpr(E); |
11143 | } |
11144 | bool VisitLambdaExpr(const LambdaExpr *E) { |
11145 | return VisitConstructExpr(E); |
11146 | } |
11147 | }; |
11148 | } // end anonymous namespace |
11149 | |
11150 | /// Evaluate an expression of record type as a temporary. |
11151 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
11152 | assert(!E->isValueDependent()); |
11153 | assert(E->isPRValue() && E->getType()->isRecordType()); |
11154 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
11155 | } |
11156 | |
11157 | //===----------------------------------------------------------------------===// |
11158 | // Vector Evaluation |
11159 | //===----------------------------------------------------------------------===// |
11160 | |
11161 | namespace { |
11162 | class VectorExprEvaluator |
11163 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
11164 | APValue &Result; |
11165 | public: |
11166 | |
11167 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
11168 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
11169 | |
11170 | bool Success(ArrayRef<APValue> V, const Expr *E) { |
11171 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
11172 | // FIXME: remove this APValue copy. |
11173 | Result = APValue(V.data(), V.size()); |
11174 | return true; |
11175 | } |
11176 | bool Success(const APValue &V, const Expr *E) { |
11177 | assert(V.isVector()); |
11178 | Result = V; |
11179 | return true; |
11180 | } |
11181 | bool ZeroInitialization(const Expr *E); |
11182 | |
11183 | bool VisitUnaryReal(const UnaryOperator *E) |
11184 | { return Visit(E->getSubExpr()); } |
11185 | bool VisitCastExpr(const CastExpr* E); |
11186 | bool VisitInitListExpr(const InitListExpr *E); |
11187 | bool VisitUnaryImag(const UnaryOperator *E); |
11188 | bool VisitBinaryOperator(const BinaryOperator *E); |
11189 | bool VisitUnaryOperator(const UnaryOperator *E); |
11190 | bool VisitCallExpr(const CallExpr *E); |
11191 | bool VisitConvertVectorExpr(const ConvertVectorExpr *E); |
11192 | bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E); |
11193 | |
11194 | // FIXME: Missing: conditional operator (for GNU |
11195 | // conditional select), ExtVectorElementExpr |
11196 | }; |
11197 | } // end anonymous namespace |
11198 | |
11199 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
11200 | assert(E->isPRValue() && E->getType()->isVectorType() && |
11201 | "not a vector prvalue"); |
11202 | return VectorExprEvaluator(Info, Result).Visit(E); |
11203 | } |
11204 | |
11205 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { |
11206 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
11207 | unsigned NElts = VTy->getNumElements(); |
11208 | |
11209 | const Expr *SE = E->getSubExpr(); |
11210 | QualType SETy = SE->getType(); |
11211 | |
11212 | switch (E->getCastKind()) { |
11213 | case CK_VectorSplat: { |
11214 | APValue Val = APValue(); |
11215 | if (SETy->isIntegerType()) { |
11216 | APSInt IntResult; |
11217 | if (!EvaluateInteger(E: SE, Result&: IntResult, Info)) |
11218 | return false; |
11219 | Val = APValue(std::move(IntResult)); |
11220 | } else if (SETy->isRealFloatingType()) { |
11221 | APFloat FloatResult(0.0); |
11222 | if (!EvaluateFloat(E: SE, Result&: FloatResult, Info)) |
11223 | return false; |
11224 | Val = APValue(std::move(FloatResult)); |
11225 | } else { |
11226 | return Error(E); |
11227 | } |
11228 | |
11229 | // Splat and create vector APValue. |
11230 | SmallVector<APValue, 4> Elts(NElts, Val); |
11231 | return Success(Elts, E); |
11232 | } |
11233 | case CK_BitCast: { |
11234 | APValue SVal; |
11235 | if (!Evaluate(Result&: SVal, Info, E: SE)) |
11236 | return false; |
11237 | |
11238 | if (!SVal.isInt() && !SVal.isFloat() && !SVal.isVector()) { |
11239 | // Give up if the input isn't an int, float, or vector. For example, we |
11240 | // reject "(v4i16)(intptr_t)&a". |
11241 | Info.FFDiag(E, diag::note_constexpr_invalid_cast) |
11242 | << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret |
11243 | << Info.Ctx.getLangOpts().CPlusPlus; |
11244 | return false; |
11245 | } |
11246 | |
11247 | if (!handleRValueToRValueBitCast(Info, DestValue&: Result, SourceRValue: SVal, BCE: E)) |
11248 | return false; |
11249 | |
11250 | return true; |
11251 | } |
11252 | case CK_HLSLVectorTruncation: { |
11253 | APValue Val; |
11254 | SmallVector<APValue, 4> Elements; |
11255 | if (!EvaluateVector(E: SE, Result&: Val, Info)) |
11256 | return Error(E); |
11257 | for (unsigned I = 0; I < NElts; I++) |
11258 | Elements.push_back(Elt: Val.getVectorElt(I)); |
11259 | return Success(Elements, E); |
11260 | } |
11261 | default: |
11262 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
11263 | } |
11264 | } |
11265 | |
11266 | bool |
11267 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
11268 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
11269 | unsigned NumInits = E->getNumInits(); |
11270 | unsigned NumElements = VT->getNumElements(); |
11271 | |
11272 | QualType EltTy = VT->getElementType(); |
11273 | SmallVector<APValue, 4> Elements; |
11274 | |
11275 | // MFloat8 type doesn't have constants and thus constant folding |
11276 | // is impossible. |
11277 | if (EltTy->isMFloat8Type()) |
11278 | return false; |
11279 | |
11280 | // The number of initializers can be less than the number of |
11281 | // vector elements. For OpenCL, this can be due to nested vector |
11282 | // initialization. For GCC compatibility, missing trailing elements |
11283 | // should be initialized with zeroes. |
11284 | unsigned CountInits = 0, CountElts = 0; |
11285 | while (CountElts < NumElements) { |
11286 | // Handle nested vector initialization. |
11287 | if (CountInits < NumInits |
11288 | && E->getInit(Init: CountInits)->getType()->isVectorType()) { |
11289 | APValue v; |
11290 | if (!EvaluateVector(E: E->getInit(Init: CountInits), Result&: v, Info)) |
11291 | return Error(E); |
11292 | unsigned vlen = v.getVectorLength(); |
11293 | for (unsigned j = 0; j < vlen; j++) |
11294 | Elements.push_back(Elt: v.getVectorElt(I: j)); |
11295 | CountElts += vlen; |
11296 | } else if (EltTy->isIntegerType()) { |
11297 | llvm::APSInt sInt(32); |
11298 | if (CountInits < NumInits) { |
11299 | if (!EvaluateInteger(E: E->getInit(Init: CountInits), Result&: sInt, Info)) |
11300 | return false; |
11301 | } else // trailing integer zero. |
11302 | sInt = Info.Ctx.MakeIntValue(Value: 0, Type: EltTy); |
11303 | Elements.push_back(Elt: APValue(sInt)); |
11304 | CountElts++; |
11305 | } else { |
11306 | llvm::APFloat f(0.0); |
11307 | if (CountInits < NumInits) { |
11308 | if (!EvaluateFloat(E: E->getInit(Init: CountInits), Result&: f, Info)) |
11309 | return false; |
11310 | } else // trailing float zero. |
11311 | f = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy)); |
11312 | Elements.push_back(Elt: APValue(f)); |
11313 | CountElts++; |
11314 | } |
11315 | CountInits++; |
11316 | } |
11317 | return Success(Elements, E); |
11318 | } |
11319 | |
11320 | bool |
11321 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
11322 | const auto *VT = E->getType()->castAs<VectorType>(); |
11323 | QualType EltTy = VT->getElementType(); |
11324 | APValue ZeroElement; |
11325 | if (EltTy->isIntegerType()) |
11326 | ZeroElement = APValue(Info.Ctx.MakeIntValue(Value: 0, Type: EltTy)); |
11327 | else |
11328 | ZeroElement = |
11329 | APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy))); |
11330 | |
11331 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
11332 | return Success(V: Elements, E); |
11333 | } |
11334 | |
11335 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
11336 | VisitIgnoredValue(E: E->getSubExpr()); |
11337 | return ZeroInitialization(E); |
11338 | } |
11339 | |
11340 | bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
11341 | BinaryOperatorKind Op = E->getOpcode(); |
11342 | assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp && |
11343 | "Operation not supported on vector types"); |
11344 | |
11345 | if (Op == BO_Comma) |
11346 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
11347 | |
11348 | Expr *LHS = E->getLHS(); |
11349 | Expr *RHS = E->getRHS(); |
11350 | |
11351 | assert(LHS->getType()->isVectorType() && RHS->getType()->isVectorType() && |
11352 | "Must both be vector types"); |
11353 | // Checking JUST the types are the same would be fine, except shifts don't |
11354 | // need to have their types be the same (since you always shift by an int). |
11355 | assert(LHS->getType()->castAs<VectorType>()->getNumElements() == |
11356 | E->getType()->castAs<VectorType>()->getNumElements() && |
11357 | RHS->getType()->castAs<VectorType>()->getNumElements() == |
11358 | E->getType()->castAs<VectorType>()->getNumElements() && |
11359 | "All operands must be the same size."); |
11360 | |
11361 | APValue LHSValue; |
11362 | APValue RHSValue; |
11363 | bool LHSOK = Evaluate(Result&: LHSValue, Info, E: LHS); |
11364 | if (!LHSOK && !Info.noteFailure()) |
11365 | return false; |
11366 | if (!Evaluate(Result&: RHSValue, Info, E: RHS) || !LHSOK) |
11367 | return false; |
11368 | |
11369 | if (!handleVectorVectorBinOp(Info, E, Opcode: Op, LHSValue, RHSValue)) |
11370 | return false; |
11371 | |
11372 | return Success(LHSValue, E); |
11373 | } |
11374 | |
11375 | static std::optional<APValue> handleVectorUnaryOperator(ASTContext &Ctx, |
11376 | QualType ResultTy, |
11377 | UnaryOperatorKind Op, |
11378 | APValue Elt) { |
11379 | switch (Op) { |
11380 | case UO_Plus: |
11381 | // Nothing to do here. |
11382 | return Elt; |
11383 | case UO_Minus: |
11384 | if (Elt.getKind() == APValue::Int) { |
11385 | Elt.getInt().negate(); |
11386 | } else { |
11387 | assert(Elt.getKind() == APValue::Float && |
11388 | "Vector can only be int or float type"); |
11389 | Elt.getFloat().changeSign(); |
11390 | } |
11391 | return Elt; |
11392 | case UO_Not: |
11393 | // This is only valid for integral types anyway, so we don't have to handle |
11394 | // float here. |
11395 | assert(Elt.getKind() == APValue::Int && |
11396 | "Vector operator ~ can only be int"); |
11397 | Elt.getInt().flipAllBits(); |
11398 | return Elt; |
11399 | case UO_LNot: { |
11400 | if (Elt.getKind() == APValue::Int) { |
11401 | Elt.getInt() = !Elt.getInt(); |
11402 | // operator ! on vectors returns -1 for 'truth', so negate it. |
11403 | Elt.getInt().negate(); |
11404 | return Elt; |
11405 | } |
11406 | assert(Elt.getKind() == APValue::Float && |
11407 | "Vector can only be int or float type"); |
11408 | // Float types result in an int of the same size, but -1 for true, or 0 for |
11409 | // false. |
11410 | APSInt EltResult{Ctx.getIntWidth(T: ResultTy), |
11411 | ResultTy->isUnsignedIntegerType()}; |
11412 | if (Elt.getFloat().isZero()) |
11413 | EltResult.setAllBits(); |
11414 | else |
11415 | EltResult.clearAllBits(); |
11416 | |
11417 | return APValue{EltResult}; |
11418 | } |
11419 | default: |
11420 | // FIXME: Implement the rest of the unary operators. |
11421 | return std::nullopt; |
11422 | } |
11423 | } |
11424 | |
11425 | bool VectorExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
11426 | Expr *SubExpr = E->getSubExpr(); |
11427 | const auto *VD = SubExpr->getType()->castAs<VectorType>(); |
11428 | // This result element type differs in the case of negating a floating point |
11429 | // vector, since the result type is the a vector of the equivilant sized |
11430 | // integer. |
11431 | const QualType ResultEltTy = VD->getElementType(); |
11432 | UnaryOperatorKind Op = E->getOpcode(); |
11433 | |
11434 | APValue SubExprValue; |
11435 | if (!Evaluate(Result&: SubExprValue, Info, E: SubExpr)) |
11436 | return false; |
11437 | |
11438 | // FIXME: This vector evaluator someday needs to be changed to be LValue |
11439 | // aware/keep LValue information around, rather than dealing with just vector |
11440 | // types directly. Until then, we cannot handle cases where the operand to |
11441 | // these unary operators is an LValue. The only case I've been able to see |
11442 | // cause this is operator++ assigning to a member expression (only valid in |
11443 | // altivec compilations) in C mode, so this shouldn't limit us too much. |
11444 | if (SubExprValue.isLValue()) |
11445 | return false; |
11446 | |
11447 | assert(SubExprValue.getVectorLength() == VD->getNumElements() && |
11448 | "Vector length doesn't match type?"); |
11449 | |
11450 | SmallVector<APValue, 4> ResultElements; |
11451 | for (unsigned EltNum = 0; EltNum < VD->getNumElements(); ++EltNum) { |
11452 | std::optional<APValue> Elt = handleVectorUnaryOperator( |
11453 | Ctx&: Info.Ctx, ResultTy: ResultEltTy, Op, Elt: SubExprValue.getVectorElt(I: EltNum)); |
11454 | if (!Elt) |
11455 | return false; |
11456 | ResultElements.push_back(Elt: *Elt); |
11457 | } |
11458 | return Success(APValue(ResultElements.data(), ResultElements.size()), E); |
11459 | } |
11460 | |
11461 | static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO, |
11462 | const Expr *E, QualType SourceTy, |
11463 | QualType DestTy, APValue const &Original, |
11464 | APValue &Result) { |
11465 | if (SourceTy->isIntegerType()) { |
11466 | if (DestTy->isRealFloatingType()) { |
11467 | Result = APValue(APFloat(0.0)); |
11468 | return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(), |
11469 | DestType: DestTy, Result&: Result.getFloat()); |
11470 | } |
11471 | if (DestTy->isIntegerType()) { |
11472 | Result = APValue( |
11473 | HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt())); |
11474 | return true; |
11475 | } |
11476 | } else if (SourceTy->isRealFloatingType()) { |
11477 | if (DestTy->isRealFloatingType()) { |
11478 | Result = Original; |
11479 | return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy, |
11480 | Result&: Result.getFloat()); |
11481 | } |
11482 | if (DestTy->isIntegerType()) { |
11483 | Result = APValue(APSInt()); |
11484 | return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(), |
11485 | DestType: DestTy, Result&: Result.getInt()); |
11486 | } |
11487 | } |
11488 | |
11489 | Info.FFDiag(E, diag::err_convertvector_constexpr_unsupported_vector_cast) |
11490 | << SourceTy << DestTy; |
11491 | return false; |
11492 | } |
11493 | |
11494 | bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) { |
11495 | if (!IsConstantEvaluatedBuiltinCall(E)) |
11496 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
11497 | |
11498 | switch (E->getBuiltinCallee()) { |
11499 | default: |
11500 | return false; |
11501 | case Builtin::BI__builtin_elementwise_popcount: |
11502 | case Builtin::BI__builtin_elementwise_bitreverse: { |
11503 | APValue Source; |
11504 | if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source)) |
11505 | return false; |
11506 | |
11507 | QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType(); |
11508 | unsigned SourceLen = Source.getVectorLength(); |
11509 | SmallVector<APValue, 4> ResultElements; |
11510 | ResultElements.reserve(N: SourceLen); |
11511 | |
11512 | for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) { |
11513 | APSInt Elt = Source.getVectorElt(I: EltNum).getInt(); |
11514 | switch (E->getBuiltinCallee()) { |
11515 | case Builtin::BI__builtin_elementwise_popcount: |
11516 | ResultElements.push_back(Elt: APValue( |
11517 | APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), Elt.popcount()), |
11518 | DestEltTy->isUnsignedIntegerOrEnumerationType()))); |
11519 | break; |
11520 | case Builtin::BI__builtin_elementwise_bitreverse: |
11521 | ResultElements.push_back( |
11522 | Elt: APValue(APSInt(Elt.reverseBits(), |
11523 | DestEltTy->isUnsignedIntegerOrEnumerationType()))); |
11524 | break; |
11525 | } |
11526 | } |
11527 | |
11528 | return Success(APValue(ResultElements.data(), ResultElements.size()), E); |
11529 | } |
11530 | case Builtin::BI__builtin_elementwise_add_sat: |
11531 | case Builtin::BI__builtin_elementwise_sub_sat: { |
11532 | APValue SourceLHS, SourceRHS; |
11533 | if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) || |
11534 | !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS)) |
11535 | return false; |
11536 | |
11537 | QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType(); |
11538 | unsigned SourceLen = SourceLHS.getVectorLength(); |
11539 | SmallVector<APValue, 4> ResultElements; |
11540 | ResultElements.reserve(N: SourceLen); |
11541 | |
11542 | for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) { |
11543 | APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt(); |
11544 | APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt(); |
11545 | switch (E->getBuiltinCallee()) { |
11546 | case Builtin::BI__builtin_elementwise_add_sat: |
11547 | ResultElements.push_back(Elt: APValue( |
11548 | APSInt(LHS.isSigned() ? LHS.sadd_sat(RHS) : RHS.uadd_sat(RHS), |
11549 | DestEltTy->isUnsignedIntegerOrEnumerationType()))); |
11550 | break; |
11551 | case Builtin::BI__builtin_elementwise_sub_sat: |
11552 | ResultElements.push_back(Elt: APValue( |
11553 | APSInt(LHS.isSigned() ? LHS.ssub_sat(RHS) : RHS.usub_sat(RHS), |
11554 | DestEltTy->isUnsignedIntegerOrEnumerationType()))); |
11555 | break; |
11556 | } |
11557 | } |
11558 | |
11559 | return Success(APValue(ResultElements.data(), ResultElements.size()), E); |
11560 | } |
11561 | } |
11562 | } |
11563 | |
11564 | bool VectorExprEvaluator::VisitConvertVectorExpr(const ConvertVectorExpr *E) { |
11565 | APValue Source; |
11566 | QualType SourceVecType = E->getSrcExpr()->getType(); |
11567 | if (!EvaluateAsRValue(Info, E: E->getSrcExpr(), Result&: Source)) |
11568 | return false; |
11569 | |
11570 | QualType DestTy = E->getType()->castAs<VectorType>()->getElementType(); |
11571 | QualType SourceTy = SourceVecType->castAs<VectorType>()->getElementType(); |
11572 | |
11573 | const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()); |
11574 | |
11575 | auto SourceLen = Source.getVectorLength(); |
11576 | SmallVector<APValue, 4> ResultElements; |
11577 | ResultElements.reserve(N: SourceLen); |
11578 | for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) { |
11579 | APValue Elt; |
11580 | if (!handleVectorElementCast(Info, FPO, E, SourceTy, DestTy, |
11581 | Source.getVectorElt(I: EltNum), Elt)) |
11582 | return false; |
11583 | ResultElements.push_back(Elt: std::move(Elt)); |
11584 | } |
11585 | |
11586 | return Success(APValue(ResultElements.data(), ResultElements.size()), E); |
11587 | } |
11588 | |
11589 | static bool handleVectorShuffle(EvalInfo &Info, const ShuffleVectorExpr *E, |
11590 | QualType ElemType, APValue const &VecVal1, |
11591 | APValue const &VecVal2, unsigned EltNum, |
11592 | APValue &Result) { |
11593 | unsigned const TotalElementsInInputVector1 = VecVal1.getVectorLength(); |
11594 | unsigned const TotalElementsInInputVector2 = VecVal2.getVectorLength(); |
11595 | |
11596 | APSInt IndexVal = E->getShuffleMaskIdx(N: EltNum); |
11597 | int64_t index = IndexVal.getExtValue(); |
11598 | // The spec says that -1 should be treated as undef for optimizations, |
11599 | // but in constexpr we'd have to produce an APValue::Indeterminate, |
11600 | // which is prohibited from being a top-level constant value. Emit a |
11601 | // diagnostic instead. |
11602 | if (index == -1) { |
11603 | Info.FFDiag( |
11604 | E, diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr) |
11605 | << EltNum; |
11606 | return false; |
11607 | } |
11608 | |
11609 | if (index < 0 || |
11610 | index >= TotalElementsInInputVector1 + TotalElementsInInputVector2) |
11611 | llvm_unreachable("Out of bounds shuffle index"); |
11612 | |
11613 | if (index >= TotalElementsInInputVector1) |
11614 | Result = VecVal2.getVectorElt(I: index - TotalElementsInInputVector1); |
11615 | else |
11616 | Result = VecVal1.getVectorElt(I: index); |
11617 | return true; |
11618 | } |
11619 | |
11620 | bool VectorExprEvaluator::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) { |
11621 | APValue VecVal1; |
11622 | const Expr *Vec1 = E->getExpr(Index: 0); |
11623 | if (!EvaluateAsRValue(Info, E: Vec1, Result&: VecVal1)) |
11624 | return false; |
11625 | APValue VecVal2; |
11626 | const Expr *Vec2 = E->getExpr(Index: 1); |
11627 | if (!EvaluateAsRValue(Info, E: Vec2, Result&: VecVal2)) |
11628 | return false; |
11629 | |
11630 | VectorType const *DestVecTy = E->getType()->castAs<VectorType>(); |
11631 | QualType DestElTy = DestVecTy->getElementType(); |
11632 | |
11633 | auto TotalElementsInOutputVector = DestVecTy->getNumElements(); |
11634 | |
11635 | SmallVector<APValue, 4> ResultElements; |
11636 | ResultElements.reserve(N: TotalElementsInOutputVector); |
11637 | for (unsigned EltNum = 0; EltNum < TotalElementsInOutputVector; ++EltNum) { |
11638 | APValue Elt; |
11639 | if (!handleVectorShuffle(Info, E, ElemType: DestElTy, VecVal1, VecVal2, EltNum, Result&: Elt)) |
11640 | return false; |
11641 | ResultElements.push_back(Elt: std::move(Elt)); |
11642 | } |
11643 | |
11644 | return Success(APValue(ResultElements.data(), ResultElements.size()), E); |
11645 | } |
11646 | |
11647 | //===----------------------------------------------------------------------===// |
11648 | // Array Evaluation |
11649 | //===----------------------------------------------------------------------===// |
11650 | |
11651 | namespace { |
11652 | class ArrayExprEvaluator |
11653 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
11654 | const LValue &This; |
11655 | APValue &Result; |
11656 | public: |
11657 | |
11658 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
11659 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
11660 | |
11661 | bool Success(const APValue &V, const Expr *E) { |
11662 | assert(V.isArray() && "expected array"); |
11663 | Result = V; |
11664 | return true; |
11665 | } |
11666 | |
11667 | bool ZeroInitialization(const Expr *E) { |
11668 | const ConstantArrayType *CAT = |
11669 | Info.Ctx.getAsConstantArrayType(T: E->getType()); |
11670 | if (!CAT) { |
11671 | if (E->getType()->isIncompleteArrayType()) { |
11672 | // We can be asked to zero-initialize a flexible array member; this |
11673 | // is represented as an ImplicitValueInitExpr of incomplete array |
11674 | // type. In this case, the array has zero elements. |
11675 | Result = APValue(APValue::UninitArray(), 0, 0); |
11676 | return true; |
11677 | } |
11678 | // FIXME: We could handle VLAs here. |
11679 | return Error(E); |
11680 | } |
11681 | |
11682 | Result = APValue(APValue::UninitArray(), 0, CAT->getZExtSize()); |
11683 | if (!Result.hasArrayFiller()) |
11684 | return true; |
11685 | |
11686 | // Zero-initialize all elements. |
11687 | LValue Subobject = This; |
11688 | Subobject.addArray(Info, E, CAT); |
11689 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
11690 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
11691 | } |
11692 | |
11693 | bool VisitCallExpr(const CallExpr *E) { |
11694 | return handleCallExpr(E, Result, ResultSlot: &This); |
11695 | } |
11696 | bool VisitInitListExpr(const InitListExpr *E, |
11697 | QualType AllocType = QualType()); |
11698 | bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E); |
11699 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
11700 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
11701 | const LValue &Subobject, |
11702 | APValue *Value, QualType Type); |
11703 | bool VisitStringLiteral(const StringLiteral *E, |
11704 | QualType AllocType = QualType()) { |
11705 | expandStringLiteral(Info, S: E, Result, AllocType); |
11706 | return true; |
11707 | } |
11708 | bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E); |
11709 | bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit, |
11710 | ArrayRef<Expr *> Args, |
11711 | const Expr *ArrayFiller, |
11712 | QualType AllocType = QualType()); |
11713 | }; |
11714 | } // end anonymous namespace |
11715 | |
11716 | static bool EvaluateArray(const Expr *E, const LValue &This, |
11717 | APValue &Result, EvalInfo &Info) { |
11718 | assert(!E->isValueDependent()); |
11719 | assert(E->isPRValue() && E->getType()->isArrayType() && |
11720 | "not an array prvalue"); |
11721 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
11722 | } |
11723 | |
11724 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
11725 | APValue &Result, const InitListExpr *ILE, |
11726 | QualType AllocType) { |
11727 | assert(!ILE->isValueDependent()); |
11728 | assert(ILE->isPRValue() && ILE->getType()->isArrayType() && |
11729 | "not an array prvalue"); |
11730 | return ArrayExprEvaluator(Info, This, Result) |
11731 | .VisitInitListExpr(E: ILE, AllocType); |
11732 | } |
11733 | |
11734 | static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This, |
11735 | APValue &Result, |
11736 | const CXXConstructExpr *CCE, |
11737 | QualType AllocType) { |
11738 | assert(!CCE->isValueDependent()); |
11739 | assert(CCE->isPRValue() && CCE->getType()->isArrayType() && |
11740 | "not an array prvalue"); |
11741 | return ArrayExprEvaluator(Info, This, Result) |
11742 | .VisitCXXConstructExpr(E: CCE, Subobject: This, Value: &Result, Type: AllocType); |
11743 | } |
11744 | |
11745 | // Return true iff the given array filler may depend on the element index. |
11746 | static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { |
11747 | // For now, just allow non-class value-initialization and initialization |
11748 | // lists comprised of them. |
11749 | if (isa<ImplicitValueInitExpr>(Val: FillerExpr)) |
11750 | return false; |
11751 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Val: FillerExpr)) { |
11752 | for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { |
11753 | if (MaybeElementDependentArrayFiller(FillerExpr: ILE->getInit(Init: I))) |
11754 | return true; |
11755 | } |
11756 | |
11757 | if (ILE->hasArrayFiller() && |
11758 | MaybeElementDependentArrayFiller(FillerExpr: ILE->getArrayFiller())) |
11759 | return true; |
11760 | |
11761 | return false; |
11762 | } |
11763 | return true; |
11764 | } |
11765 | |
11766 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E, |
11767 | QualType AllocType) { |
11768 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
11769 | T: AllocType.isNull() ? E->getType() : AllocType); |
11770 | if (!CAT) |
11771 | return Error(E); |
11772 | |
11773 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
11774 | // an appropriately-typed string literal enclosed in braces. |
11775 | if (E->isStringLiteralInit()) { |
11776 | auto *SL = dyn_cast<StringLiteral>(Val: E->getInit(Init: 0)->IgnoreParenImpCasts()); |
11777 | // FIXME: Support ObjCEncodeExpr here once we support it in |
11778 | // ArrayExprEvaluator generally. |
11779 | if (!SL) |
11780 | return Error(E); |
11781 | return VisitStringLiteral(E: SL, AllocType); |
11782 | } |
11783 | // Any other transparent list init will need proper handling of the |
11784 | // AllocType; we can't just recurse to the inner initializer. |
11785 | assert(!E->isTransparent() && |
11786 | "transparent array list initialization is not string literal init?"); |
11787 | |
11788 | return VisitCXXParenListOrInitListExpr(E, E->inits(), E->getArrayFiller(), |
11789 | AllocType); |
11790 | } |
11791 | |
11792 | bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr( |
11793 | const Expr *ExprToVisit, ArrayRef<Expr *> Args, const Expr *ArrayFiller, |
11794 | QualType AllocType) { |
11795 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
11796 | T: AllocType.isNull() ? ExprToVisit->getType() : AllocType); |
11797 | |
11798 | bool Success = true; |
11799 | |
11800 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
11801 | "zero-initialized array shouldn't have any initialized elts"); |
11802 | APValue Filler; |
11803 | if (Result.isArray() && Result.hasArrayFiller()) |
11804 | Filler = Result.getArrayFiller(); |
11805 | |
11806 | unsigned NumEltsToInit = Args.size(); |
11807 | unsigned NumElts = CAT->getZExtSize(); |
11808 | |
11809 | // If the initializer might depend on the array index, run it for each |
11810 | // array element. |
11811 | if (NumEltsToInit != NumElts && |
11812 | MaybeElementDependentArrayFiller(FillerExpr: ArrayFiller)) { |
11813 | NumEltsToInit = NumElts; |
11814 | } else { |
11815 | for (auto *Init : Args) { |
11816 | if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) |
11817 | NumEltsToInit += EmbedS->getDataElementCount() - 1; |
11818 | } |
11819 | if (NumEltsToInit > NumElts) |
11820 | NumEltsToInit = NumElts; |
11821 | } |
11822 | |
11823 | LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: " |
11824 | << NumEltsToInit << ".\n"); |
11825 | |
11826 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
11827 | |
11828 | // If the array was previously zero-initialized, preserve the |
11829 | // zero-initialized values. |
11830 | if (Filler.hasValue()) { |
11831 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
11832 | Result.getArrayInitializedElt(I) = Filler; |
11833 | if (Result.hasArrayFiller()) |
11834 | Result.getArrayFiller() = Filler; |
11835 | } |
11836 | |
11837 | LValue Subobject = This; |
11838 | Subobject.addArray(Info, E: ExprToVisit, CAT); |
11839 | auto Eval = [&](const Expr *Init, unsigned ArrayIndex) { |
11840 | if (Init->isValueDependent()) |
11841 | return EvaluateDependentExpr(E: Init, Info); |
11842 | |
11843 | if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: ArrayIndex), Info, |
11844 | This: Subobject, E: Init) || |
11845 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
11846 | CAT->getElementType(), 1)) { |
11847 | if (!Info.noteFailure()) |
11848 | return false; |
11849 | Success = false; |
11850 | } |
11851 | return true; |
11852 | }; |
11853 | unsigned ArrayIndex = 0; |
11854 | QualType DestTy = CAT->getElementType(); |
11855 | APSInt Value(Info.Ctx.getTypeSize(T: DestTy), DestTy->isUnsignedIntegerType()); |
11856 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
11857 | const Expr *Init = Index < Args.size() ? Args[Index] : ArrayFiller; |
11858 | if (ArrayIndex >= NumEltsToInit) |
11859 | break; |
11860 | if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) { |
11861 | StringLiteral *SL = EmbedS->getDataStringLiteral(); |
11862 | for (unsigned I = EmbedS->getStartingElementPos(), |
11863 | N = EmbedS->getDataElementCount(); |
11864 | I != EmbedS->getStartingElementPos() + N; ++I) { |
11865 | Value = SL->getCodeUnit(i: I); |
11866 | if (DestTy->isIntegerType()) { |
11867 | Result.getArrayInitializedElt(I: ArrayIndex) = APValue(Value); |
11868 | } else { |
11869 | assert(DestTy->isFloatingType() && "unexpected type"); |
11870 | const FPOptions FPO = |
11871 | Init->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()); |
11872 | APFloat FValue(0.0); |
11873 | if (!HandleIntToFloatCast(Info, Init, FPO, EmbedS->getType(), Value, |
11874 | DestTy, FValue)) |
11875 | return false; |
11876 | Result.getArrayInitializedElt(I: ArrayIndex) = APValue(FValue); |
11877 | } |
11878 | ArrayIndex++; |
11879 | } |
11880 | } else { |
11881 | if (!Eval(Init, ArrayIndex)) |
11882 | return false; |
11883 | ++ArrayIndex; |
11884 | } |
11885 | } |
11886 | |
11887 | if (!Result.hasArrayFiller()) |
11888 | return Success; |
11889 | |
11890 | // If we get here, we have a trivial filler, which we can just evaluate |
11891 | // once and splat over the rest of the array elements. |
11892 | assert(ArrayFiller && "no array filler for incomplete init list"); |
11893 | return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject, |
11894 | E: ArrayFiller) && |
11895 | Success; |
11896 | } |
11897 | |
11898 | bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
11899 | LValue CommonLV; |
11900 | if (E->getCommonExpr() && |
11901 | !Evaluate(Result&: Info.CurrentCall->createTemporary( |
11902 | Key: E->getCommonExpr(), |
11903 | T: getStorageType(Info.Ctx, E->getCommonExpr()), |
11904 | Scope: ScopeKind::FullExpression, LV&: CommonLV), |
11905 | Info, E: E->getCommonExpr()->getSourceExpr())) |
11906 | return false; |
11907 | |
11908 | auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe()); |
11909 | |
11910 | uint64_t Elements = CAT->getZExtSize(); |
11911 | Result = APValue(APValue::UninitArray(), Elements, Elements); |
11912 | |
11913 | LValue Subobject = This; |
11914 | Subobject.addArray(Info, E, CAT: CAT); |
11915 | |
11916 | bool Success = true; |
11917 | for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) { |
11918 | // C++ [class.temporary]/5 |
11919 | // There are four contexts in which temporaries are destroyed at a different |
11920 | // point than the end of the full-expression. [...] The second context is |
11921 | // when a copy constructor is called to copy an element of an array while |
11922 | // the entire array is copied [...]. In either case, if the constructor has |
11923 | // one or more default arguments, the destruction of every temporary created |
11924 | // in a default argument is sequenced before the construction of the next |
11925 | // array element, if any. |
11926 | FullExpressionRAII Scope(Info); |
11927 | |
11928 | if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index), |
11929 | Info, This: Subobject, E: E->getSubExpr()) || |
11930 | !HandleLValueArrayAdjustment(Info, E, Subobject, |
11931 | CAT->getElementType(), 1)) { |
11932 | if (!Info.noteFailure()) |
11933 | return false; |
11934 | Success = false; |
11935 | } |
11936 | |
11937 | // Make sure we run the destructors too. |
11938 | Scope.destroy(); |
11939 | } |
11940 | |
11941 | return Success; |
11942 | } |
11943 | |
11944 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
11945 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
11946 | } |
11947 | |
11948 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
11949 | const LValue &Subobject, |
11950 | APValue *Value, |
11951 | QualType Type) { |
11952 | bool HadZeroInit = Value->hasValue(); |
11953 | |
11954 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: Type)) { |
11955 | unsigned FinalSize = CAT->getZExtSize(); |
11956 | |
11957 | // Preserve the array filler if we had prior zero-initialization. |
11958 | APValue Filler = |
11959 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
11960 | : APValue(); |
11961 | |
11962 | *Value = APValue(APValue::UninitArray(), 0, FinalSize); |
11963 | if (FinalSize == 0) |
11964 | return true; |
11965 | |
11966 | bool HasTrivialConstructor = CheckTrivialDefaultConstructor( |
11967 | Info, E->getExprLoc(), E->getConstructor(), |
11968 | E->requiresZeroInitialization()); |
11969 | LValue ArrayElt = Subobject; |
11970 | ArrayElt.addArray(Info, E, CAT); |
11971 | // We do the whole initialization in two passes, first for just one element, |
11972 | // then for the whole array. It's possible we may find out we can't do const |
11973 | // init in the first pass, in which case we avoid allocating a potentially |
11974 | // large array. We don't do more passes because expanding array requires |
11975 | // copying the data, which is wasteful. |
11976 | for (const unsigned N : {1u, FinalSize}) { |
11977 | unsigned OldElts = Value->getArrayInitializedElts(); |
11978 | if (OldElts == N) |
11979 | break; |
11980 | |
11981 | // Expand the array to appropriate size. |
11982 | APValue NewValue(APValue::UninitArray(), N, FinalSize); |
11983 | for (unsigned I = 0; I < OldElts; ++I) |
11984 | NewValue.getArrayInitializedElt(I).swap( |
11985 | RHS&: Value->getArrayInitializedElt(I)); |
11986 | Value->swap(RHS&: NewValue); |
11987 | |
11988 | if (HadZeroInit) |
11989 | for (unsigned I = OldElts; I < N; ++I) |
11990 | Value->getArrayInitializedElt(I) = Filler; |
11991 | |
11992 | if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) { |
11993 | // If we have a trivial constructor, only evaluate it once and copy |
11994 | // the result into all the array elements. |
11995 | APValue &FirstResult = Value->getArrayInitializedElt(I: 0); |
11996 | for (unsigned I = OldElts; I < FinalSize; ++I) |
11997 | Value->getArrayInitializedElt(I) = FirstResult; |
11998 | } else { |
11999 | for (unsigned I = OldElts; I < N; ++I) { |
12000 | if (!VisitCXXConstructExpr(E, ArrayElt, |
12001 | &Value->getArrayInitializedElt(I), |
12002 | CAT->getElementType()) || |
12003 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
12004 | CAT->getElementType(), 1)) |
12005 | return false; |
12006 | // When checking for const initilization any diagnostic is considered |
12007 | // an error. |
12008 | if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() && |
12009 | !Info.keepEvaluatingAfterFailure()) |
12010 | return false; |
12011 | } |
12012 | } |
12013 | } |
12014 | |
12015 | return true; |
12016 | } |
12017 | |
12018 | if (!Type->isRecordType()) |
12019 | return Error(E); |
12020 | |
12021 | return RecordExprEvaluator(Info, Subobject, *Value) |
12022 | .VisitCXXConstructExpr(E, T: Type); |
12023 | } |
12024 | |
12025 | bool ArrayExprEvaluator::VisitCXXParenListInitExpr( |
12026 | const CXXParenListInitExpr *E) { |
12027 | assert(E->getType()->isConstantArrayType() && |
12028 | "Expression result is not a constant array type"); |
12029 | |
12030 | return VisitCXXParenListOrInitListExpr(E, E->getInitExprs(), |
12031 | E->getArrayFiller()); |
12032 | } |
12033 | |
12034 | //===----------------------------------------------------------------------===// |
12035 | // Integer Evaluation |
12036 | // |
12037 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
12038 | // types and back in constant folding. Integer values are thus represented |
12039 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
12040 | //===----------------------------------------------------------------------===// |
12041 | |
12042 | namespace { |
12043 | class IntExprEvaluator |
12044 | : public ExprEvaluatorBase<IntExprEvaluator> { |
12045 | APValue &Result; |
12046 | public: |
12047 | IntExprEvaluator(EvalInfo &info, APValue &result) |
12048 | : ExprEvaluatorBaseTy(info), Result(result) {} |
12049 | |
12050 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
12051 | assert(E->getType()->isIntegralOrEnumerationType() && |
12052 | "Invalid evaluation result."); |
12053 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
12054 | "Invalid evaluation result."); |
12055 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
12056 | "Invalid evaluation result."); |
12057 | Result = APValue(SI); |
12058 | return true; |
12059 | } |
12060 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
12061 | return Success(SI, E, Result); |
12062 | } |
12063 | |
12064 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
12065 | assert(E->getType()->isIntegralOrEnumerationType() && |
12066 | "Invalid evaluation result."); |
12067 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
12068 | "Invalid evaluation result."); |
12069 | Result = APValue(APSInt(I)); |
12070 | Result.getInt().setIsUnsigned( |
12071 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
12072 | return true; |
12073 | } |
12074 | bool Success(const llvm::APInt &I, const Expr *E) { |
12075 | return Success(I, E, Result); |
12076 | } |
12077 | |
12078 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
12079 | assert(E->getType()->isIntegralOrEnumerationType() && |
12080 | "Invalid evaluation result."); |
12081 | Result = APValue(Info.Ctx.MakeIntValue(Value, Type: E->getType())); |
12082 | return true; |
12083 | } |
12084 | bool Success(uint64_t Value, const Expr *E) { |
12085 | return Success(Value, E, Result); |
12086 | } |
12087 | |
12088 | bool Success(CharUnits Size, const Expr *E) { |
12089 | return Success(Value: Size.getQuantity(), E); |
12090 | } |
12091 | |
12092 | bool Success(const APValue &V, const Expr *E) { |
12093 | // C++23 [expr.const]p8 If we have a variable that is unknown reference or |
12094 | // pointer allow further evaluation of the value. |
12095 | if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate() || |
12096 | V.allowConstexprUnknown()) { |
12097 | Result = V; |
12098 | return true; |
12099 | } |
12100 | return Success(SI: V.getInt(), E); |
12101 | } |
12102 | |
12103 | bool ZeroInitialization(const Expr *E) { return Success(Value: 0, E); } |
12104 | |
12105 | friend std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &, |
12106 | const CallExpr *); |
12107 | |
12108 | //===--------------------------------------------------------------------===// |
12109 | // Visitor Methods |
12110 | //===--------------------------------------------------------------------===// |
12111 | |
12112 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
12113 | return Success(E->getValue(), E); |
12114 | } |
12115 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
12116 | return Success(E->getValue(), E); |
12117 | } |
12118 | |
12119 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
12120 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
12121 | if (CheckReferencedDecl(E, E->getDecl())) |
12122 | return true; |
12123 | |
12124 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
12125 | } |
12126 | bool VisitMemberExpr(const MemberExpr *E) { |
12127 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
12128 | VisitIgnoredBaseExpression(E: E->getBase()); |
12129 | return true; |
12130 | } |
12131 | |
12132 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
12133 | } |
12134 | |
12135 | bool VisitCallExpr(const CallExpr *E); |
12136 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
12137 | bool VisitBinaryOperator(const BinaryOperator *E); |
12138 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
12139 | bool VisitUnaryOperator(const UnaryOperator *E); |
12140 | |
12141 | bool VisitCastExpr(const CastExpr* E); |
12142 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
12143 | |
12144 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
12145 | return Success(E->getValue(), E); |
12146 | } |
12147 | |
12148 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
12149 | return Success(E->getValue(), E); |
12150 | } |
12151 | |
12152 | bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
12153 | if (Info.ArrayInitIndex == uint64_t(-1)) { |
12154 | // We were asked to evaluate this subexpression independent of the |
12155 | // enclosing ArrayInitLoopExpr. We can't do that. |
12156 | Info.FFDiag(E); |
12157 | return false; |
12158 | } |
12159 | return Success(Info.ArrayInitIndex, E); |
12160 | } |
12161 | |
12162 | // Note, GNU defines __null as an integer, not a pointer. |
12163 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
12164 | return ZeroInitialization(E); |
12165 | } |
12166 | |
12167 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
12168 | if (E->isStoredAsBoolean()) |
12169 | return Success(E->getBoolValue(), E); |
12170 | if (E->getAPValue().isAbsent()) |
12171 | return false; |
12172 | assert(E->getAPValue().isInt() && "APValue type not supported"); |
12173 | return Success(E->getAPValue().getInt(), E); |
12174 | } |
12175 | |
12176 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
12177 | return Success(E->getValue(), E); |
12178 | } |
12179 | |
12180 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
12181 | return Success(E->getValue(), E); |
12182 | } |
12183 | |
12184 | bool VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *E) { |
12185 | // This should not be evaluated during constant expr evaluation, as it |
12186 | // should always be in an unevaluated context (the args list of a 'gang' or |
12187 | // 'tile' clause). |
12188 | return Error(E); |
12189 | } |
12190 | |
12191 | bool VisitUnaryReal(const UnaryOperator *E); |
12192 | bool VisitUnaryImag(const UnaryOperator *E); |
12193 | |
12194 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
12195 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
12196 | bool VisitSourceLocExpr(const SourceLocExpr *E); |
12197 | bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E); |
12198 | bool VisitRequiresExpr(const RequiresExpr *E); |
12199 | // FIXME: Missing: array subscript of vector, member of vector |
12200 | }; |
12201 | |
12202 | class FixedPointExprEvaluator |
12203 | : public ExprEvaluatorBase<FixedPointExprEvaluator> { |
12204 | APValue &Result; |
12205 | |
12206 | public: |
12207 | FixedPointExprEvaluator(EvalInfo &info, APValue &result) |
12208 | : ExprEvaluatorBaseTy(info), Result(result) {} |
12209 | |
12210 | bool Success(const llvm::APInt &I, const Expr *E) { |
12211 | return Success( |
12212 | V: APFixedPoint(I, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E); |
12213 | } |
12214 | |
12215 | bool Success(uint64_t Value, const Expr *E) { |
12216 | return Success( |
12217 | V: APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E); |
12218 | } |
12219 | |
12220 | bool Success(const APValue &V, const Expr *E) { |
12221 | return Success(V: V.getFixedPoint(), E); |
12222 | } |
12223 | |
12224 | bool Success(const APFixedPoint &V, const Expr *E) { |
12225 | assert(E->getType()->isFixedPointType() && "Invalid evaluation result."); |
12226 | assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) && |
12227 | "Invalid evaluation result."); |
12228 | Result = APValue(V); |
12229 | return true; |
12230 | } |
12231 | |
12232 | bool ZeroInitialization(const Expr *E) { |
12233 | return Success(Value: 0, E); |
12234 | } |
12235 | |
12236 | //===--------------------------------------------------------------------===// |
12237 | // Visitor Methods |
12238 | //===--------------------------------------------------------------------===// |
12239 | |
12240 | bool VisitFixedPointLiteral(const FixedPointLiteral *E) { |
12241 | return Success(E->getValue(), E); |
12242 | } |
12243 | |
12244 | bool VisitCastExpr(const CastExpr *E); |
12245 | bool VisitUnaryOperator(const UnaryOperator *E); |
12246 | bool VisitBinaryOperator(const BinaryOperator *E); |
12247 | }; |
12248 | } // end anonymous namespace |
12249 | |
12250 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
12251 | /// produce either the integer value or a pointer. |
12252 | /// |
12253 | /// GCC has a heinous extension which folds casts between pointer types and |
12254 | /// pointer-sized integral types. We support this by allowing the evaluation of |
12255 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
12256 | /// Some simple arithmetic on such values is supported (they are treated much |
12257 | /// like char*). |
12258 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
12259 | EvalInfo &Info) { |
12260 | assert(!E->isValueDependent()); |
12261 | assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType()); |
12262 | return IntExprEvaluator(Info, Result).Visit(E); |
12263 | } |
12264 | |
12265 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
12266 | assert(!E->isValueDependent()); |
12267 | APValue Val; |
12268 | if (!EvaluateIntegerOrLValue(E, Result&: Val, Info)) |
12269 | return false; |
12270 | if (!Val.isInt()) { |
12271 | // FIXME: It would be better to produce the diagnostic for casting |
12272 | // a pointer to an integer. |
12273 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
12274 | return false; |
12275 | } |
12276 | Result = Val.getInt(); |
12277 | return true; |
12278 | } |
12279 | |
12280 | bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) { |
12281 | APValue Evaluated = E->EvaluateInContext( |
12282 | Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
12283 | return Success(Evaluated, E); |
12284 | } |
12285 | |
12286 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
12287 | EvalInfo &Info) { |
12288 | assert(!E->isValueDependent()); |
12289 | if (E->getType()->isFixedPointType()) { |
12290 | APValue Val; |
12291 | if (!FixedPointExprEvaluator(Info, Val).Visit(E)) |
12292 | return false; |
12293 | if (!Val.isFixedPoint()) |
12294 | return false; |
12295 | |
12296 | Result = Val.getFixedPoint(); |
12297 | return true; |
12298 | } |
12299 | return false; |
12300 | } |
12301 | |
12302 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
12303 | EvalInfo &Info) { |
12304 | assert(!E->isValueDependent()); |
12305 | if (E->getType()->isIntegerType()) { |
12306 | auto FXSema = Info.Ctx.getFixedPointSemantics(Ty: E->getType()); |
12307 | APSInt Val; |
12308 | if (!EvaluateInteger(E, Result&: Val, Info)) |
12309 | return false; |
12310 | Result = APFixedPoint(Val, FXSema); |
12311 | return true; |
12312 | } else if (E->getType()->isFixedPointType()) { |
12313 | return EvaluateFixedPoint(E, Result, Info); |
12314 | } |
12315 | return false; |
12316 | } |
12317 | |
12318 | /// Check whether the given declaration can be directly converted to an integral |
12319 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
12320 | /// try. |
12321 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
12322 | // Enums are integer constant exprs. |
12323 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(Val: D)) { |
12324 | // Check for signedness/width mismatches between E type and ECD value. |
12325 | bool SameSign = (ECD->getInitVal().isSigned() |
12326 | == E->getType()->isSignedIntegerOrEnumerationType()); |
12327 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
12328 | == Info.Ctx.getIntWidth(T: E->getType())); |
12329 | if (SameSign && SameWidth) |
12330 | return Success(SI: ECD->getInitVal(), E); |
12331 | else { |
12332 | // Get rid of mismatch (otherwise Success assertions will fail) |
12333 | // by computing a new value matching the type of E. |
12334 | llvm::APSInt Val = ECD->getInitVal(); |
12335 | if (!SameSign) |
12336 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
12337 | if (!SameWidth) |
12338 | Val = Val.extOrTrunc(width: Info.Ctx.getIntWidth(T: E->getType())); |
12339 | return Success(SI: Val, E); |
12340 | } |
12341 | } |
12342 | return false; |
12343 | } |
12344 | |
12345 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
12346 | /// as GCC. |
12347 | GCCTypeClass EvaluateBuiltinClassifyType(QualType T, |
12348 | const LangOptions &LangOpts) { |
12349 | assert(!T->isDependentType() && "unexpected dependent type"); |
12350 | |
12351 | QualType CanTy = T.getCanonicalType(); |
12352 | |
12353 | switch (CanTy->getTypeClass()) { |
12354 | #define TYPE(ID, BASE) |
12355 | #define DEPENDENT_TYPE(ID, BASE) case Type::ID: |
12356 | #define NON_CANONICAL_TYPE(ID, BASE) case Type::ID: |
12357 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID: |
12358 | #include "clang/AST/TypeNodes.inc" |
12359 | case Type::Auto: |
12360 | case Type::DeducedTemplateSpecialization: |
12361 | llvm_unreachable("unexpected non-canonical or dependent type"); |
12362 | |
12363 | case Type::Builtin: |
12364 | switch (cast<BuiltinType>(CanTy)->getKind()) { |
12365 | #define BUILTIN_TYPE(ID, SINGLETON_ID) |
12366 | #define SIGNED_TYPE(ID, SINGLETON_ID) \ |
12367 | case BuiltinType::ID: return GCCTypeClass::Integer; |
12368 | #define FLOATING_TYPE(ID, SINGLETON_ID) \ |
12369 | case BuiltinType::ID: return GCCTypeClass::RealFloat; |
12370 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \ |
12371 | case BuiltinType::ID: break; |
12372 | #include "clang/AST/BuiltinTypes.def" |
12373 | case BuiltinType::Void: |
12374 | return GCCTypeClass::Void; |
12375 | |
12376 | case BuiltinType::Bool: |
12377 | return GCCTypeClass::Bool; |
12378 | |
12379 | case BuiltinType::Char_U: |
12380 | case BuiltinType::UChar: |
12381 | case BuiltinType::WChar_U: |
12382 | case BuiltinType::Char8: |
12383 | case BuiltinType::Char16: |
12384 | case BuiltinType::Char32: |
12385 | case BuiltinType::UShort: |
12386 | case BuiltinType::UInt: |
12387 | case BuiltinType::ULong: |
12388 | case BuiltinType::ULongLong: |
12389 | case BuiltinType::UInt128: |
12390 | return GCCTypeClass::Integer; |
12391 | |
12392 | case BuiltinType::UShortAccum: |
12393 | case BuiltinType::UAccum: |
12394 | case BuiltinType::ULongAccum: |
12395 | case BuiltinType::UShortFract: |
12396 | case BuiltinType::UFract: |
12397 | case BuiltinType::ULongFract: |
12398 | case BuiltinType::SatUShortAccum: |
12399 | case BuiltinType::SatUAccum: |
12400 | case BuiltinType::SatULongAccum: |
12401 | case BuiltinType::SatUShortFract: |
12402 | case BuiltinType::SatUFract: |
12403 | case BuiltinType::SatULongFract: |
12404 | return GCCTypeClass::None; |
12405 | |
12406 | case BuiltinType::NullPtr: |
12407 | |
12408 | case BuiltinType::ObjCId: |
12409 | case BuiltinType::ObjCClass: |
12410 | case BuiltinType::ObjCSel: |
12411 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
12412 | case BuiltinType::Id: |
12413 | #include "clang/Basic/OpenCLImageTypes.def" |
12414 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
12415 | case BuiltinType::Id: |
12416 | #include "clang/Basic/OpenCLExtensionTypes.def" |
12417 | case BuiltinType::OCLSampler: |
12418 | case BuiltinType::OCLEvent: |
12419 | case BuiltinType::OCLClkEvent: |
12420 | case BuiltinType::OCLQueue: |
12421 | case BuiltinType::OCLReserveID: |
12422 | #define SVE_TYPE(Name, Id, SingletonId) \ |
12423 | case BuiltinType::Id: |
12424 | #include "clang/Basic/AArch64ACLETypes.def" |
12425 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
12426 | case BuiltinType::Id: |
12427 | #include "clang/Basic/PPCTypes.def" |
12428 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
12429 | #include "clang/Basic/RISCVVTypes.def" |
12430 | #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
12431 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
12432 | #define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id: |
12433 | #include "clang/Basic/AMDGPUTypes.def" |
12434 | #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
12435 | #include "clang/Basic/HLSLIntangibleTypes.def" |
12436 | return GCCTypeClass::None; |
12437 | |
12438 | case BuiltinType::Dependent: |
12439 | llvm_unreachable("unexpected dependent type"); |
12440 | }; |
12441 | llvm_unreachable("unexpected placeholder type"); |
12442 | |
12443 | case Type::Enum: |
12444 | return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer; |
12445 | |
12446 | case Type::Pointer: |
12447 | case Type::ConstantArray: |
12448 | case Type::VariableArray: |
12449 | case Type::IncompleteArray: |
12450 | case Type::FunctionNoProto: |
12451 | case Type::FunctionProto: |
12452 | case Type::ArrayParameter: |
12453 | return GCCTypeClass::Pointer; |
12454 | |
12455 | case Type::MemberPointer: |
12456 | return CanTy->isMemberDataPointerType() |
12457 | ? GCCTypeClass::PointerToDataMember |
12458 | : GCCTypeClass::PointerToMemberFunction; |
12459 | |
12460 | case Type::Complex: |
12461 | return GCCTypeClass::Complex; |
12462 | |
12463 | case Type::Record: |
12464 | return CanTy->isUnionType() ? GCCTypeClass::Union |
12465 | : GCCTypeClass::ClassOrStruct; |
12466 | |
12467 | case Type::Atomic: |
12468 | // GCC classifies _Atomic T the same as T. |
12469 | return EvaluateBuiltinClassifyType( |
12470 | CanTy->castAs<AtomicType>()->getValueType(), LangOpts); |
12471 | |
12472 | case Type::Vector: |
12473 | case Type::ExtVector: |
12474 | return GCCTypeClass::Vector; |
12475 | |
12476 | case Type::BlockPointer: |
12477 | case Type::ConstantMatrix: |
12478 | case Type::ObjCObject: |
12479 | case Type::ObjCInterface: |
12480 | case Type::ObjCObjectPointer: |
12481 | case Type::Pipe: |
12482 | case Type::HLSLAttributedResource: |
12483 | case Type::HLSLInlineSpirv: |
12484 | // Classify all other types that don't fit into the regular |
12485 | // classification the same way. |
12486 | return GCCTypeClass::None; |
12487 | |
12488 | case Type::BitInt: |
12489 | return GCCTypeClass::BitInt; |
12490 | |
12491 | case Type::LValueReference: |
12492 | case Type::RValueReference: |
12493 | llvm_unreachable("invalid type for expression"); |
12494 | } |
12495 | |
12496 | llvm_unreachable("unexpected type class"); |
12497 | } |
12498 | |
12499 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
12500 | /// as GCC. |
12501 | static GCCTypeClass |
12502 | EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) { |
12503 | // If no argument was supplied, default to None. This isn't |
12504 | // ideal, however it is what gcc does. |
12505 | if (E->getNumArgs() == 0) |
12506 | return GCCTypeClass::None; |
12507 | |
12508 | // FIXME: Bizarrely, GCC treats a call with more than one argument as not |
12509 | // being an ICE, but still folds it to a constant using the type of the first |
12510 | // argument. |
12511 | return EvaluateBuiltinClassifyType(T: E->getArg(Arg: 0)->getType(), LangOpts); |
12512 | } |
12513 | |
12514 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
12515 | /// __builtin_constant_p when applied to the given pointer. |
12516 | /// |
12517 | /// A pointer is only "constant" if it is null (or a pointer cast to integer) |
12518 | /// or it points to the first character of a string literal. |
12519 | static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) { |
12520 | APValue::LValueBase Base = LV.getLValueBase(); |
12521 | if (Base.isNull()) { |
12522 | // A null base is acceptable. |
12523 | return true; |
12524 | } else if (const Expr *E = Base.dyn_cast<const Expr *>()) { |
12525 | if (!isa<StringLiteral>(Val: E)) |
12526 | return false; |
12527 | return LV.getLValueOffset().isZero(); |
12528 | } else if (Base.is<TypeInfoLValue>()) { |
12529 | // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to |
12530 | // evaluate to true. |
12531 | return true; |
12532 | } else { |
12533 | // Any other base is not constant enough for GCC. |
12534 | return false; |
12535 | } |
12536 | } |
12537 | |
12538 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
12539 | /// GCC as we can manage. |
12540 | static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) { |
12541 | // This evaluation is not permitted to have side-effects, so evaluate it in |
12542 | // a speculative evaluation context. |
12543 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
12544 | |
12545 | // Constant-folding is always enabled for the operand of __builtin_constant_p |
12546 | // (even when the enclosing evaluation context otherwise requires a strict |
12547 | // language-specific constant expression). |
12548 | FoldConstant Fold(Info, true); |
12549 | |
12550 | QualType ArgType = Arg->getType(); |
12551 | |
12552 | // __builtin_constant_p always has one operand. The rules which gcc follows |
12553 | // are not precisely documented, but are as follows: |
12554 | // |
12555 | // - If the operand is of integral, floating, complex or enumeration type, |
12556 | // and can be folded to a known value of that type, it returns 1. |
12557 | // - If the operand can be folded to a pointer to the first character |
12558 | // of a string literal (or such a pointer cast to an integral type) |
12559 | // or to a null pointer or an integer cast to a pointer, it returns 1. |
12560 | // |
12561 | // Otherwise, it returns 0. |
12562 | // |
12563 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
12564 | // its support for this did not work prior to GCC 9 and is not yet well |
12565 | // understood. |
12566 | if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() || |
12567 | ArgType->isAnyComplexType() || ArgType->isPointerType() || |
12568 | ArgType->isNullPtrType()) { |
12569 | APValue V; |
12570 | if (!::EvaluateAsRValue(Info, E: Arg, Result&: V) || Info.EvalStatus.HasSideEffects) { |
12571 | Fold.keepDiagnostics(); |
12572 | return false; |
12573 | } |
12574 | |
12575 | // For a pointer (possibly cast to integer), there are special rules. |
12576 | if (V.getKind() == APValue::LValue) |
12577 | return EvaluateBuiltinConstantPForLValue(LV: V); |
12578 | |
12579 | // Otherwise, any constant value is good enough. |
12580 | return V.hasValue(); |
12581 | } |
12582 | |
12583 | // Anything else isn't considered to be sufficiently constant. |
12584 | return false; |
12585 | } |
12586 | |
12587 | /// Retrieves the "underlying object type" of the given expression, |
12588 | /// as used by __builtin_object_size. |
12589 | static QualType getObjectType(APValue::LValueBase B) { |
12590 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
12591 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
12592 | return VD->getType(); |
12593 | } else if (const Expr *E = B.dyn_cast<const Expr*>()) { |
12594 | if (isa<CompoundLiteralExpr>(Val: E)) |
12595 | return E->getType(); |
12596 | } else if (B.is<TypeInfoLValue>()) { |
12597 | return B.getTypeInfoType(); |
12598 | } else if (B.is<DynamicAllocLValue>()) { |
12599 | return B.getDynamicAllocType(); |
12600 | } |
12601 | |
12602 | return QualType(); |
12603 | } |
12604 | |
12605 | /// A more selective version of E->IgnoreParenCasts for |
12606 | /// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only |
12607 | /// to change the type of E. |
12608 | /// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo` |
12609 | /// |
12610 | /// Always returns an RValue with a pointer representation. |
12611 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
12612 | assert(E->isPRValue() && E->getType()->hasPointerRepresentation()); |
12613 | |
12614 | const Expr *NoParens = E->IgnoreParens(); |
12615 | const auto *Cast = dyn_cast<CastExpr>(Val: NoParens); |
12616 | if (Cast == nullptr) |
12617 | return NoParens; |
12618 | |
12619 | // We only conservatively allow a few kinds of casts, because this code is |
12620 | // inherently a simple solution that seeks to support the common case. |
12621 | auto CastKind = Cast->getCastKind(); |
12622 | if (CastKind != CK_NoOp && CastKind != CK_BitCast && |
12623 | CastKind != CK_AddressSpaceConversion) |
12624 | return NoParens; |
12625 | |
12626 | const auto *SubExpr = Cast->getSubExpr(); |
12627 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue()) |
12628 | return NoParens; |
12629 | return ignorePointerCastsAndParens(E: SubExpr); |
12630 | } |
12631 | |
12632 | /// Checks to see if the given LValue's Designator is at the end of the LValue's |
12633 | /// record layout. e.g. |
12634 | /// struct { struct { int a, b; } fst, snd; } obj; |
12635 | /// obj.fst // no |
12636 | /// obj.snd // yes |
12637 | /// obj.fst.a // no |
12638 | /// obj.fst.b // no |
12639 | /// obj.snd.a // no |
12640 | /// obj.snd.b // yes |
12641 | /// |
12642 | /// Please note: this function is specialized for how __builtin_object_size |
12643 | /// views "objects". |
12644 | /// |
12645 | /// If this encounters an invalid RecordDecl or otherwise cannot determine the |
12646 | /// correct result, it will always return true. |
12647 | static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) { |
12648 | assert(!LVal.Designator.Invalid); |
12649 | |
12650 | auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD) { |
12651 | const RecordDecl *Parent = FD->getParent(); |
12652 | if (Parent->isInvalidDecl() || Parent->isUnion()) |
12653 | return true; |
12654 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(D: Parent); |
12655 | return FD->getFieldIndex() + 1 == Layout.getFieldCount(); |
12656 | }; |
12657 | |
12658 | auto &Base = LVal.getLValueBase(); |
12659 | if (auto *ME = dyn_cast_or_null<MemberExpr>(Val: Base.dyn_cast<const Expr *>())) { |
12660 | if (auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl())) { |
12661 | if (!IsLastOrInvalidFieldDecl(FD)) |
12662 | return false; |
12663 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(Val: ME->getMemberDecl())) { |
12664 | for (auto *FD : IFD->chain()) { |
12665 | if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(Val: FD))) |
12666 | return false; |
12667 | } |
12668 | } |
12669 | } |
12670 | |
12671 | unsigned I = 0; |
12672 | QualType BaseType = getType(B: Base); |
12673 | if (LVal.Designator.FirstEntryIsAnUnsizedArray) { |
12674 | // If we don't know the array bound, conservatively assume we're looking at |
12675 | // the final array element. |
12676 | ++I; |
12677 | if (BaseType->isIncompleteArrayType()) |
12678 | BaseType = Ctx.getAsArrayType(T: BaseType)->getElementType(); |
12679 | else |
12680 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
12681 | } |
12682 | |
12683 | for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) { |
12684 | const auto &Entry = LVal.Designator.Entries[I]; |
12685 | if (BaseType->isArrayType()) { |
12686 | // Because __builtin_object_size treats arrays as objects, we can ignore |
12687 | // the index iff this is the last array in the Designator. |
12688 | if (I + 1 == E) |
12689 | return true; |
12690 | const auto *CAT = cast<ConstantArrayType>(Val: Ctx.getAsArrayType(T: BaseType)); |
12691 | uint64_t Index = Entry.getAsArrayIndex(); |
12692 | if (Index + 1 != CAT->getZExtSize()) |
12693 | return false; |
12694 | BaseType = CAT->getElementType(); |
12695 | } else if (BaseType->isAnyComplexType()) { |
12696 | const auto *CT = BaseType->castAs<ComplexType>(); |
12697 | uint64_t Index = Entry.getAsArrayIndex(); |
12698 | if (Index != 1) |
12699 | return false; |
12700 | BaseType = CT->getElementType(); |
12701 | } else if (auto *FD = getAsField(Entry)) { |
12702 | if (!IsLastOrInvalidFieldDecl(FD)) |
12703 | return false; |
12704 | BaseType = FD->getType(); |
12705 | } else { |
12706 | assert(getAsBaseClass(Entry) && "Expecting cast to a base class"); |
12707 | return false; |
12708 | } |
12709 | } |
12710 | return true; |
12711 | } |
12712 | |
12713 | /// Tests to see if the LValue has a user-specified designator (that isn't |
12714 | /// necessarily valid). Note that this always returns 'true' if the LValue has |
12715 | /// an unsized array as its first designator entry, because there's currently no |
12716 | /// way to tell if the user typed *foo or foo[0]. |
12717 | static bool refersToCompleteObject(const LValue &LVal) { |
12718 | if (LVal.Designator.Invalid) |
12719 | return false; |
12720 | |
12721 | if (!LVal.Designator.Entries.empty()) |
12722 | return LVal.Designator.isMostDerivedAnUnsizedArray(); |
12723 | |
12724 | if (!LVal.InvalidBase) |
12725 | return true; |
12726 | |
12727 | // If `E` is a MemberExpr, then the first part of the designator is hiding in |
12728 | // the LValueBase. |
12729 | const auto *E = LVal.Base.dyn_cast<const Expr *>(); |
12730 | return !E || !isa<MemberExpr>(Val: E); |
12731 | } |
12732 | |
12733 | /// Attempts to detect a user writing into a piece of memory that's impossible |
12734 | /// to figure out the size of by just using types. |
12735 | static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) { |
12736 | const SubobjectDesignator &Designator = LVal.Designator; |
12737 | // Notes: |
12738 | // - Users can only write off of the end when we have an invalid base. Invalid |
12739 | // bases imply we don't know where the memory came from. |
12740 | // - We used to be a bit more aggressive here; we'd only be conservative if |
12741 | // the array at the end was flexible, or if it had 0 or 1 elements. This |
12742 | // broke some common standard library extensions (PR30346), but was |
12743 | // otherwise seemingly fine. It may be useful to reintroduce this behavior |
12744 | // with some sort of list. OTOH, it seems that GCC is always |
12745 | // conservative with the last element in structs (if it's an array), so our |
12746 | // current behavior is more compatible than an explicit list approach would |
12747 | // be. |
12748 | auto isFlexibleArrayMember = [&] { |
12749 | using FAMKind = LangOptions::StrictFlexArraysLevelKind; |
12750 | FAMKind StrictFlexArraysLevel = |
12751 | Ctx.getLangOpts().getStrictFlexArraysLevel(); |
12752 | |
12753 | if (Designator.isMostDerivedAnUnsizedArray()) |
12754 | return true; |
12755 | |
12756 | if (StrictFlexArraysLevel == FAMKind::Default) |
12757 | return true; |
12758 | |
12759 | if (Designator.getMostDerivedArraySize() == 0 && |
12760 | StrictFlexArraysLevel != FAMKind::IncompleteOnly) |
12761 | return true; |
12762 | |
12763 | if (Designator.getMostDerivedArraySize() == 1 && |
12764 | StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete) |
12765 | return true; |
12766 | |
12767 | return false; |
12768 | }; |
12769 | |
12770 | return LVal.InvalidBase && |
12771 | Designator.Entries.size() == Designator.MostDerivedPathLength && |
12772 | Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() && |
12773 | isDesignatorAtObjectEnd(Ctx, LVal); |
12774 | } |
12775 | |
12776 | /// Converts the given APInt to CharUnits, assuming the APInt is unsigned. |
12777 | /// Fails if the conversion would cause loss of precision. |
12778 | static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int, |
12779 | CharUnits &Result) { |
12780 | auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max(); |
12781 | if (Int.ugt(RHS: CharUnitsMax)) |
12782 | return false; |
12783 | Result = CharUnits::fromQuantity(Quantity: Int.getZExtValue()); |
12784 | return true; |
12785 | } |
12786 | |
12787 | /// If we're evaluating the object size of an instance of a struct that |
12788 | /// contains a flexible array member, add the size of the initializer. |
12789 | static void addFlexibleArrayMemberInitSize(EvalInfo &Info, const QualType &T, |
12790 | const LValue &LV, CharUnits &Size) { |
12791 | if (!T.isNull() && T->isStructureType() && |
12792 | T->getAsStructureType()->getDecl()->hasFlexibleArrayMember()) |
12793 | if (const auto *V = LV.getLValueBase().dyn_cast<const ValueDecl *>()) |
12794 | if (const auto *VD = dyn_cast<VarDecl>(Val: V)) |
12795 | if (VD->hasInit()) |
12796 | Size += VD->getFlexibleArrayInitChars(Ctx: Info.Ctx); |
12797 | } |
12798 | |
12799 | /// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will |
12800 | /// determine how many bytes exist from the beginning of the object to either |
12801 | /// the end of the current subobject, or the end of the object itself, depending |
12802 | /// on what the LValue looks like + the value of Type. |
12803 | /// |
12804 | /// If this returns false, the value of Result is undefined. |
12805 | static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc, |
12806 | unsigned Type, const LValue &LVal, |
12807 | CharUnits &EndOffset) { |
12808 | bool DetermineForCompleteObject = refersToCompleteObject(LVal); |
12809 | |
12810 | auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { |
12811 | if (Ty.isNull()) |
12812 | return false; |
12813 | |
12814 | Ty = Ty.getNonReferenceType(); |
12815 | |
12816 | if (Ty->isIncompleteType() || Ty->isFunctionType()) |
12817 | return false; |
12818 | |
12819 | return HandleSizeof(Info, Loc: ExprLoc, Type: Ty, Size&: Result); |
12820 | }; |
12821 | |
12822 | // We want to evaluate the size of the entire object. This is a valid fallback |
12823 | // for when Type=1 and the designator is invalid, because we're asked for an |
12824 | // upper-bound. |
12825 | if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) { |
12826 | // Type=3 wants a lower bound, so we can't fall back to this. |
12827 | if (Type == 3 && !DetermineForCompleteObject) |
12828 | return false; |
12829 | |
12830 | llvm::APInt APEndOffset; |
12831 | if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) && |
12832 | getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset)) |
12833 | return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset); |
12834 | |
12835 | if (LVal.InvalidBase) |
12836 | return false; |
12837 | |
12838 | QualType BaseTy = getObjectType(B: LVal.getLValueBase()); |
12839 | const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset); |
12840 | addFlexibleArrayMemberInitSize(Info, T: BaseTy, LV: LVal, Size&: EndOffset); |
12841 | return Ret; |
12842 | } |
12843 | |
12844 | // We want to evaluate the size of a subobject. |
12845 | const SubobjectDesignator &Designator = LVal.Designator; |
12846 | |
12847 | // The following is a moderately common idiom in C: |
12848 | // |
12849 | // struct Foo { int a; char c[1]; }; |
12850 | // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar)); |
12851 | // strcpy(&F->c[0], Bar); |
12852 | // |
12853 | // In order to not break too much legacy code, we need to support it. |
12854 | if (isUserWritingOffTheEnd(Ctx: Info.Ctx, LVal)) { |
12855 | // If we can resolve this to an alloc_size call, we can hand that back, |
12856 | // because we know for certain how many bytes there are to write to. |
12857 | llvm::APInt APEndOffset; |
12858 | if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) && |
12859 | getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset)) |
12860 | return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset); |
12861 | |
12862 | // If we cannot determine the size of the initial allocation, then we can't |
12863 | // given an accurate upper-bound. However, we are still able to give |
12864 | // conservative lower-bounds for Type=3. |
12865 | if (Type == 1) |
12866 | return false; |
12867 | } |
12868 | |
12869 | CharUnits BytesPerElem; |
12870 | if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem)) |
12871 | return false; |
12872 | |
12873 | // According to the GCC documentation, we want the size of the subobject |
12874 | // denoted by the pointer. But that's not quite right -- what we actually |
12875 | // want is the size of the immediately-enclosing array, if there is one. |
12876 | int64_t ElemsRemaining; |
12877 | if (Designator.MostDerivedIsArrayElement && |
12878 | Designator.Entries.size() == Designator.MostDerivedPathLength) { |
12879 | uint64_t ArraySize = Designator.getMostDerivedArraySize(); |
12880 | uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex(); |
12881 | ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex; |
12882 | } else { |
12883 | ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1; |
12884 | } |
12885 | |
12886 | EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining; |
12887 | return true; |
12888 | } |
12889 | |
12890 | /// Tries to evaluate the __builtin_object_size for @p E. If successful, |
12891 | /// returns true and stores the result in @p Size. |
12892 | /// |
12893 | /// If @p WasError is non-null, this will report whether the failure to evaluate |
12894 | /// is to be treated as an Error in IntExprEvaluator. |
12895 | static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, |
12896 | EvalInfo &Info, uint64_t &Size) { |
12897 | // Determine the denoted object. |
12898 | LValue LVal; |
12899 | { |
12900 | // The operand of __builtin_object_size is never evaluated for side-effects. |
12901 | // If there are any, but we can determine the pointed-to object anyway, then |
12902 | // ignore the side-effects. |
12903 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
12904 | IgnoreSideEffectsRAII Fold(Info); |
12905 | |
12906 | if (E->isGLValue()) { |
12907 | // It's possible for us to be given GLValues if we're called via |
12908 | // Expr::tryEvaluateObjectSize. |
12909 | APValue RVal; |
12910 | if (!EvaluateAsRValue(Info, E, Result&: RVal)) |
12911 | return false; |
12912 | LVal.setFrom(Ctx&: Info.Ctx, V: RVal); |
12913 | } else if (!EvaluatePointer(E: ignorePointerCastsAndParens(E), Result&: LVal, Info, |
12914 | /*InvalidBaseOK=*/true)) |
12915 | return false; |
12916 | } |
12917 | |
12918 | // If we point to before the start of the object, there are no accessible |
12919 | // bytes. |
12920 | if (LVal.getLValueOffset().isNegative()) { |
12921 | Size = 0; |
12922 | return true; |
12923 | } |
12924 | |
12925 | CharUnits EndOffset; |
12926 | if (!determineEndOffset(Info, ExprLoc: E->getExprLoc(), Type, LVal, EndOffset)) |
12927 | return false; |
12928 | |
12929 | // If we've fallen outside of the end offset, just pretend there's nothing to |
12930 | // write to/read from. |
12931 | if (EndOffset <= LVal.getLValueOffset()) |
12932 | Size = 0; |
12933 | else |
12934 | Size = (EndOffset - LVal.getLValueOffset()).getQuantity(); |
12935 | return true; |
12936 | } |
12937 | |
12938 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
12939 | if (!IsConstantEvaluatedBuiltinCall(E)) |
12940 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
12941 | return VisitBuiltinCallExpr(E, BuiltinOp: E->getBuiltinCallee()); |
12942 | } |
12943 | |
12944 | static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info, |
12945 | APValue &Val, APSInt &Alignment) { |
12946 | QualType SrcTy = E->getArg(Arg: 0)->getType(); |
12947 | if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: SrcTy, Info, Alignment)) |
12948 | return false; |
12949 | // Even though we are evaluating integer expressions we could get a pointer |
12950 | // argument for the __builtin_is_aligned() case. |
12951 | if (SrcTy->isPointerType()) { |
12952 | LValue Ptr; |
12953 | if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Ptr, Info)) |
12954 | return false; |
12955 | Ptr.moveInto(V&: Val); |
12956 | } else if (!SrcTy->isIntegralOrEnumerationType()) { |
12957 | Info.FFDiag(E: E->getArg(Arg: 0)); |
12958 | return false; |
12959 | } else { |
12960 | APSInt SrcInt; |
12961 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SrcInt, Info)) |
12962 | return false; |
12963 | assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() && |
12964 | "Bit widths must be the same"); |
12965 | Val = APValue(SrcInt); |
12966 | } |
12967 | assert(Val.hasValue()); |
12968 | return true; |
12969 | } |
12970 | |
12971 | bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
12972 | unsigned BuiltinOp) { |
12973 | switch (BuiltinOp) { |
12974 | default: |
12975 | return false; |
12976 | |
12977 | case Builtin::BI__builtin_dynamic_object_size: |
12978 | case Builtin::BI__builtin_object_size: { |
12979 | // The type was checked when we built the expression. |
12980 | unsigned Type = |
12981 | E->getArg(Arg: 1)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue(); |
12982 | assert(Type <= 3 && "unexpected type"); |
12983 | |
12984 | uint64_t Size; |
12985 | if (tryEvaluateBuiltinObjectSize(E: E->getArg(Arg: 0), Type, Info, Size)) |
12986 | return Success(Size, E); |
12987 | |
12988 | if (E->getArg(Arg: 0)->HasSideEffects(Ctx: Info.Ctx)) |
12989 | return Success((Type & 2) ? 0 : -1, E); |
12990 | |
12991 | // Expression had no side effects, but we couldn't statically determine the |
12992 | // size of the referenced object. |
12993 | switch (Info.EvalMode) { |
12994 | case EvalInfo::EM_ConstantExpression: |
12995 | case EvalInfo::EM_ConstantFold: |
12996 | case EvalInfo::EM_IgnoreSideEffects: |
12997 | // Leave it to IR generation. |
12998 | return Error(E); |
12999 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
13000 | // Reduce it to a constant now. |
13001 | return Success((Type & 2) ? 0 : -1, E); |
13002 | } |
13003 | |
13004 | llvm_unreachable("unexpected EvalMode"); |
13005 | } |
13006 | |
13007 | case Builtin::BI__builtin_os_log_format_buffer_size: { |
13008 | analyze_os_log::OSLogBufferLayout Layout; |
13009 | analyze_os_log::computeOSLogBufferLayout(Ctx&: Info.Ctx, E, layout&: Layout); |
13010 | return Success(Layout.size().getQuantity(), E); |
13011 | } |
13012 | |
13013 | case Builtin::BI__builtin_is_aligned: { |
13014 | APValue Src; |
13015 | APSInt Alignment; |
13016 | if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment)) |
13017 | return false; |
13018 | if (Src.isLValue()) { |
13019 | // If we evaluated a pointer, check the minimum known alignment. |
13020 | LValue Ptr; |
13021 | Ptr.setFrom(Ctx&: Info.Ctx, V: Src); |
13022 | CharUnits BaseAlignment = getBaseAlignment(Info, Value: Ptr); |
13023 | CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Ptr.Offset); |
13024 | // We can return true if the known alignment at the computed offset is |
13025 | // greater than the requested alignment. |
13026 | assert(PtrAlign.isPowerOfTwo()); |
13027 | assert(Alignment.isPowerOf2()); |
13028 | if (PtrAlign.getQuantity() >= Alignment) |
13029 | return Success(1, E); |
13030 | // If the alignment is not known to be sufficient, some cases could still |
13031 | // be aligned at run time. However, if the requested alignment is less or |
13032 | // equal to the base alignment and the offset is not aligned, we know that |
13033 | // the run-time value can never be aligned. |
13034 | if (BaseAlignment.getQuantity() >= Alignment && |
13035 | PtrAlign.getQuantity() < Alignment) |
13036 | return Success(0, E); |
13037 | // Otherwise we can't infer whether the value is sufficiently aligned. |
13038 | // TODO: __builtin_is_aligned(__builtin_align_{down,up{(expr, N), N) |
13039 | // in cases where we can't fully evaluate the pointer. |
13040 | Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_compute) |
13041 | << Alignment; |
13042 | return false; |
13043 | } |
13044 | assert(Src.isInt()); |
13045 | return Success((Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E); |
13046 | } |
13047 | case Builtin::BI__builtin_align_up: { |
13048 | APValue Src; |
13049 | APSInt Alignment; |
13050 | if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment)) |
13051 | return false; |
13052 | if (!Src.isInt()) |
13053 | return Error(E); |
13054 | APSInt AlignedVal = |
13055 | APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1), |
13056 | Src.getInt().isUnsigned()); |
13057 | assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth()); |
13058 | return Success(AlignedVal, E); |
13059 | } |
13060 | case Builtin::BI__builtin_align_down: { |
13061 | APValue Src; |
13062 | APSInt Alignment; |
13063 | if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment)) |
13064 | return false; |
13065 | if (!Src.isInt()) |
13066 | return Error(E); |
13067 | APSInt AlignedVal = |
13068 | APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned()); |
13069 | assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth()); |
13070 | return Success(AlignedVal, E); |
13071 | } |
13072 | |
13073 | case Builtin::BI__builtin_bitreverse8: |
13074 | case Builtin::BI__builtin_bitreverse16: |
13075 | case Builtin::BI__builtin_bitreverse32: |
13076 | case Builtin::BI__builtin_bitreverse64: |
13077 | case Builtin::BI__builtin_elementwise_bitreverse: { |
13078 | APSInt Val; |
13079 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13080 | return false; |
13081 | |
13082 | return Success(Val.reverseBits(), E); |
13083 | } |
13084 | |
13085 | case Builtin::BI__builtin_bswap16: |
13086 | case Builtin::BI__builtin_bswap32: |
13087 | case Builtin::BI__builtin_bswap64: { |
13088 | APSInt Val; |
13089 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13090 | return false; |
13091 | |
13092 | return Success(Val.byteSwap(), E); |
13093 | } |
13094 | |
13095 | case Builtin::BI__builtin_classify_type: |
13096 | return Success((int)EvaluateBuiltinClassifyType(E, LangOpts: Info.getLangOpts()), E); |
13097 | |
13098 | case Builtin::BI__builtin_clrsb: |
13099 | case Builtin::BI__builtin_clrsbl: |
13100 | case Builtin::BI__builtin_clrsbll: { |
13101 | APSInt Val; |
13102 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13103 | return false; |
13104 | |
13105 | return Success(Val.getBitWidth() - Val.getSignificantBits(), E); |
13106 | } |
13107 | |
13108 | case Builtin::BI__builtin_clz: |
13109 | case Builtin::BI__builtin_clzl: |
13110 | case Builtin::BI__builtin_clzll: |
13111 | case Builtin::BI__builtin_clzs: |
13112 | case Builtin::BI__builtin_clzg: |
13113 | case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes |
13114 | case Builtin::BI__lzcnt: |
13115 | case Builtin::BI__lzcnt64: { |
13116 | APSInt Val; |
13117 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13118 | return false; |
13119 | |
13120 | std::optional<APSInt> Fallback; |
13121 | if (BuiltinOp == Builtin::BI__builtin_clzg && E->getNumArgs() > 1) { |
13122 | APSInt FallbackTemp; |
13123 | if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info)) |
13124 | return false; |
13125 | Fallback = FallbackTemp; |
13126 | } |
13127 | |
13128 | if (!Val) { |
13129 | if (Fallback) |
13130 | return Success(*Fallback, E); |
13131 | |
13132 | // When the argument is 0, the result of GCC builtins is undefined, |
13133 | // whereas for Microsoft intrinsics, the result is the bit-width of the |
13134 | // argument. |
13135 | bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 && |
13136 | BuiltinOp != Builtin::BI__lzcnt && |
13137 | BuiltinOp != Builtin::BI__lzcnt64; |
13138 | |
13139 | if (ZeroIsUndefined) |
13140 | return Error(E); |
13141 | } |
13142 | |
13143 | return Success(Val.countl_zero(), E); |
13144 | } |
13145 | |
13146 | case Builtin::BI__builtin_constant_p: { |
13147 | const Expr *Arg = E->getArg(Arg: 0); |
13148 | if (EvaluateBuiltinConstantP(Info, Arg)) |
13149 | return Success(true, E); |
13150 | if (Info.InConstantContext || Arg->HasSideEffects(Ctx: Info.Ctx)) { |
13151 | // Outside a constant context, eagerly evaluate to false in the presence |
13152 | // of side-effects in order to avoid -Wunsequenced false-positives in |
13153 | // a branch on __builtin_constant_p(expr). |
13154 | return Success(false, E); |
13155 | } |
13156 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
13157 | return false; |
13158 | } |
13159 | |
13160 | case Builtin::BI__noop: |
13161 | // __noop always evaluates successfully and returns 0. |
13162 | return Success(0, E); |
13163 | |
13164 | case Builtin::BI__builtin_is_constant_evaluated: { |
13165 | const auto *Callee = Info.CurrentCall->getCallee(); |
13166 | if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression && |
13167 | (Info.CallStackDepth == 1 || |
13168 | (Info.CallStackDepth == 2 && Callee->isInStdNamespace() && |
13169 | Callee->getIdentifier() && |
13170 | Callee->getIdentifier()->isStr("is_constant_evaluated")))) { |
13171 | // FIXME: Find a better way to avoid duplicated diagnostics. |
13172 | if (Info.EvalStatus.Diag) |
13173 | Info.report((Info.CallStackDepth == 1) |
13174 | ? E->getExprLoc() |
13175 | : Info.CurrentCall->getCallRange().getBegin(), |
13176 | diag::warn_is_constant_evaluated_always_true_constexpr) |
13177 | << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated" |
13178 | : "std::is_constant_evaluated"); |
13179 | } |
13180 | |
13181 | return Success(Info.InConstantContext, E); |
13182 | } |
13183 | |
13184 | case Builtin::BI__builtin_is_within_lifetime: |
13185 | if (auto result = EvaluateBuiltinIsWithinLifetime(*this, E)) |
13186 | return Success(*result, E); |
13187 | return false; |
13188 | |
13189 | case Builtin::BI__builtin_ctz: |
13190 | case Builtin::BI__builtin_ctzl: |
13191 | case Builtin::BI__builtin_ctzll: |
13192 | case Builtin::BI__builtin_ctzs: |
13193 | case Builtin::BI__builtin_ctzg: { |
13194 | APSInt Val; |
13195 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13196 | return false; |
13197 | |
13198 | std::optional<APSInt> Fallback; |
13199 | if (BuiltinOp == Builtin::BI__builtin_ctzg && E->getNumArgs() > 1) { |
13200 | APSInt FallbackTemp; |
13201 | if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info)) |
13202 | return false; |
13203 | Fallback = FallbackTemp; |
13204 | } |
13205 | |
13206 | if (!Val) { |
13207 | if (Fallback) |
13208 | return Success(*Fallback, E); |
13209 | |
13210 | return Error(E); |
13211 | } |
13212 | |
13213 | return Success(Val.countr_zero(), E); |
13214 | } |
13215 | |
13216 | case Builtin::BI__builtin_eh_return_data_regno: { |
13217 | int Operand = E->getArg(Arg: 0)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue(); |
13218 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(RegNo: Operand); |
13219 | return Success(Operand, E); |
13220 | } |
13221 | |
13222 | case Builtin::BI__builtin_expect: |
13223 | case Builtin::BI__builtin_expect_with_probability: |
13224 | return Visit(E->getArg(Arg: 0)); |
13225 | |
13226 | case Builtin::BI__builtin_ptrauth_string_discriminator: { |
13227 | const auto *Literal = |
13228 | cast<StringLiteral>(Val: E->getArg(Arg: 0)->IgnoreParenImpCasts()); |
13229 | uint64_t Result = getPointerAuthStableSipHash(S: Literal->getString()); |
13230 | return Success(Result, E); |
13231 | } |
13232 | |
13233 | case Builtin::BI__builtin_ffs: |
13234 | case Builtin::BI__builtin_ffsl: |
13235 | case Builtin::BI__builtin_ffsll: { |
13236 | APSInt Val; |
13237 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13238 | return false; |
13239 | |
13240 | unsigned N = Val.countr_zero(); |
13241 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
13242 | } |
13243 | |
13244 | case Builtin::BI__builtin_fpclassify: { |
13245 | APFloat Val(0.0); |
13246 | if (!EvaluateFloat(E: E->getArg(Arg: 5), Result&: Val, Info)) |
13247 | return false; |
13248 | unsigned Arg; |
13249 | switch (Val.getCategory()) { |
13250 | case APFloat::fcNaN: Arg = 0; break; |
13251 | case APFloat::fcInfinity: Arg = 1; break; |
13252 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
13253 | case APFloat::fcZero: Arg = 4; break; |
13254 | } |
13255 | return Visit(E->getArg(Arg)); |
13256 | } |
13257 | |
13258 | case Builtin::BI__builtin_isinf_sign: { |
13259 | APFloat Val(0.0); |
13260 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
13261 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
13262 | } |
13263 | |
13264 | case Builtin::BI__builtin_isinf: { |
13265 | APFloat Val(0.0); |
13266 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
13267 | Success(Val.isInfinity() ? 1 : 0, E); |
13268 | } |
13269 | |
13270 | case Builtin::BI__builtin_isfinite: { |
13271 | APFloat Val(0.0); |
13272 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
13273 | Success(Val.isFinite() ? 1 : 0, E); |
13274 | } |
13275 | |
13276 | case Builtin::BI__builtin_isnan: { |
13277 | APFloat Val(0.0); |
13278 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
13279 | Success(Val.isNaN() ? 1 : 0, E); |
13280 | } |
13281 | |
13282 | case Builtin::BI__builtin_isnormal: { |
13283 | APFloat Val(0.0); |
13284 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
13285 | Success(Val.isNormal() ? 1 : 0, E); |
13286 | } |
13287 | |
13288 | case Builtin::BI__builtin_issubnormal: { |
13289 | APFloat Val(0.0); |
13290 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
13291 | Success(Val.isDenormal() ? 1 : 0, E); |
13292 | } |
13293 | |
13294 | case Builtin::BI__builtin_iszero: { |
13295 | APFloat Val(0.0); |
13296 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
13297 | Success(Val.isZero() ? 1 : 0, E); |
13298 | } |
13299 | |
13300 | case Builtin::BI__builtin_signbit: |
13301 | case Builtin::BI__builtin_signbitf: |
13302 | case Builtin::BI__builtin_signbitl: { |
13303 | APFloat Val(0.0); |
13304 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
13305 | Success(Val.isNegative() ? 1 : 0, E); |
13306 | } |
13307 | |
13308 | case Builtin::BI__builtin_isgreater: |
13309 | case Builtin::BI__builtin_isgreaterequal: |
13310 | case Builtin::BI__builtin_isless: |
13311 | case Builtin::BI__builtin_islessequal: |
13312 | case Builtin::BI__builtin_islessgreater: |
13313 | case Builtin::BI__builtin_isunordered: { |
13314 | APFloat LHS(0.0); |
13315 | APFloat RHS(0.0); |
13316 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: LHS, Info) || |
13317 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
13318 | return false; |
13319 | |
13320 | return Success( |
13321 | [&] { |
13322 | switch (BuiltinOp) { |
13323 | case Builtin::BI__builtin_isgreater: |
13324 | return LHS > RHS; |
13325 | case Builtin::BI__builtin_isgreaterequal: |
13326 | return LHS >= RHS; |
13327 | case Builtin::BI__builtin_isless: |
13328 | return LHS < RHS; |
13329 | case Builtin::BI__builtin_islessequal: |
13330 | return LHS <= RHS; |
13331 | case Builtin::BI__builtin_islessgreater: { |
13332 | APFloat::cmpResult cmp = LHS.compare(RHS); |
13333 | return cmp == APFloat::cmpResult::cmpLessThan || |
13334 | cmp == APFloat::cmpResult::cmpGreaterThan; |
13335 | } |
13336 | case Builtin::BI__builtin_isunordered: |
13337 | return LHS.compare(RHS) == APFloat::cmpResult::cmpUnordered; |
13338 | default: |
13339 | llvm_unreachable("Unexpected builtin ID: Should be a floating " |
13340 | "point comparison function"); |
13341 | } |
13342 | }() |
13343 | ? 1 |
13344 | : 0, |
13345 | E); |
13346 | } |
13347 | |
13348 | case Builtin::BI__builtin_issignaling: { |
13349 | APFloat Val(0.0); |
13350 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
13351 | Success(Val.isSignaling() ? 1 : 0, E); |
13352 | } |
13353 | |
13354 | case Builtin::BI__builtin_isfpclass: { |
13355 | APSInt MaskVal; |
13356 | if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: MaskVal, Info)) |
13357 | return false; |
13358 | unsigned Test = static_cast<llvm::FPClassTest>(MaskVal.getZExtValue()); |
13359 | APFloat Val(0.0); |
13360 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
13361 | Success((Val.classify() & Test) ? 1 : 0, E); |
13362 | } |
13363 | |
13364 | case Builtin::BI__builtin_parity: |
13365 | case Builtin::BI__builtin_parityl: |
13366 | case Builtin::BI__builtin_parityll: { |
13367 | APSInt Val; |
13368 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13369 | return false; |
13370 | |
13371 | return Success(Val.popcount() % 2, E); |
13372 | } |
13373 | |
13374 | case Builtin::BI__builtin_abs: |
13375 | case Builtin::BI__builtin_labs: |
13376 | case Builtin::BI__builtin_llabs: { |
13377 | APSInt Val; |
13378 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13379 | return false; |
13380 | if (Val == APSInt(APInt::getSignedMinValue(numBits: Val.getBitWidth()), |
13381 | /*IsUnsigned=*/false)) |
13382 | return false; |
13383 | if (Val.isNegative()) |
13384 | Val.negate(); |
13385 | return Success(Val, E); |
13386 | } |
13387 | |
13388 | case Builtin::BI__builtin_popcount: |
13389 | case Builtin::BI__builtin_popcountl: |
13390 | case Builtin::BI__builtin_popcountll: |
13391 | case Builtin::BI__builtin_popcountg: |
13392 | case Builtin::BI__builtin_elementwise_popcount: |
13393 | case Builtin::BI__popcnt16: // Microsoft variants of popcount |
13394 | case Builtin::BI__popcnt: |
13395 | case Builtin::BI__popcnt64: { |
13396 | APSInt Val; |
13397 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13398 | return false; |
13399 | |
13400 | return Success(Val.popcount(), E); |
13401 | } |
13402 | |
13403 | case Builtin::BI__builtin_rotateleft8: |
13404 | case Builtin::BI__builtin_rotateleft16: |
13405 | case Builtin::BI__builtin_rotateleft32: |
13406 | case Builtin::BI__builtin_rotateleft64: |
13407 | case Builtin::BI_rotl8: // Microsoft variants of rotate right |
13408 | case Builtin::BI_rotl16: |
13409 | case Builtin::BI_rotl: |
13410 | case Builtin::BI_lrotl: |
13411 | case Builtin::BI_rotl64: { |
13412 | APSInt Val, Amt; |
13413 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) || |
13414 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amt, Info)) |
13415 | return false; |
13416 | |
13417 | return Success(Val.rotl(rotateAmt: Amt.urem(RHS: Val.getBitWidth())), E); |
13418 | } |
13419 | |
13420 | case Builtin::BI__builtin_rotateright8: |
13421 | case Builtin::BI__builtin_rotateright16: |
13422 | case Builtin::BI__builtin_rotateright32: |
13423 | case Builtin::BI__builtin_rotateright64: |
13424 | case Builtin::BI_rotr8: // Microsoft variants of rotate right |
13425 | case Builtin::BI_rotr16: |
13426 | case Builtin::BI_rotr: |
13427 | case Builtin::BI_lrotr: |
13428 | case Builtin::BI_rotr64: { |
13429 | APSInt Val, Amt; |
13430 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) || |
13431 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amt, Info)) |
13432 | return false; |
13433 | |
13434 | return Success(Val.rotr(rotateAmt: Amt.urem(RHS: Val.getBitWidth())), E); |
13435 | } |
13436 | |
13437 | case Builtin::BI__builtin_elementwise_add_sat: { |
13438 | APSInt LHS, RHS; |
13439 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) || |
13440 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
13441 | return false; |
13442 | |
13443 | APInt Result = LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS); |
13444 | return Success(APSInt(Result, !LHS.isSigned()), E); |
13445 | } |
13446 | case Builtin::BI__builtin_elementwise_sub_sat: { |
13447 | APSInt LHS, RHS; |
13448 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) || |
13449 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
13450 | return false; |
13451 | |
13452 | APInt Result = LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS); |
13453 | return Success(APSInt(Result, !LHS.isSigned()), E); |
13454 | } |
13455 | |
13456 | case Builtin::BIstrlen: |
13457 | case Builtin::BIwcslen: |
13458 | // A call to strlen is not a constant expression. |
13459 | if (Info.getLangOpts().CPlusPlus11) |
13460 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
13461 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
13462 | << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp); |
13463 | else |
13464 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
13465 | [[fallthrough]]; |
13466 | case Builtin::BI__builtin_strlen: |
13467 | case Builtin::BI__builtin_wcslen: { |
13468 | // As an extension, we support __builtin_strlen() as a constant expression, |
13469 | // and support folding strlen() to a constant. |
13470 | uint64_t StrLen; |
13471 | if (EvaluateBuiltinStrLen(E: E->getArg(Arg: 0), Result&: StrLen, Info)) |
13472 | return Success(StrLen, E); |
13473 | return false; |
13474 | } |
13475 | |
13476 | case Builtin::BIstrcmp: |
13477 | case Builtin::BIwcscmp: |
13478 | case Builtin::BIstrncmp: |
13479 | case Builtin::BIwcsncmp: |
13480 | case Builtin::BImemcmp: |
13481 | case Builtin::BIbcmp: |
13482 | case Builtin::BIwmemcmp: |
13483 | // A call to strlen is not a constant expression. |
13484 | if (Info.getLangOpts().CPlusPlus11) |
13485 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
13486 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
13487 | << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp); |
13488 | else |
13489 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
13490 | [[fallthrough]]; |
13491 | case Builtin::BI__builtin_strcmp: |
13492 | case Builtin::BI__builtin_wcscmp: |
13493 | case Builtin::BI__builtin_strncmp: |
13494 | case Builtin::BI__builtin_wcsncmp: |
13495 | case Builtin::BI__builtin_memcmp: |
13496 | case Builtin::BI__builtin_bcmp: |
13497 | case Builtin::BI__builtin_wmemcmp: { |
13498 | LValue String1, String2; |
13499 | if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: String1, Info) || |
13500 | !EvaluatePointer(E: E->getArg(Arg: 1), Result&: String2, Info)) |
13501 | return false; |
13502 | |
13503 | uint64_t MaxLength = uint64_t(-1); |
13504 | if (BuiltinOp != Builtin::BIstrcmp && |
13505 | BuiltinOp != Builtin::BIwcscmp && |
13506 | BuiltinOp != Builtin::BI__builtin_strcmp && |
13507 | BuiltinOp != Builtin::BI__builtin_wcscmp) { |
13508 | APSInt N; |
13509 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info)) |
13510 | return false; |
13511 | MaxLength = N.getZExtValue(); |
13512 | } |
13513 | |
13514 | // Empty substrings compare equal by definition. |
13515 | if (MaxLength == 0u) |
13516 | return Success(0, E); |
13517 | |
13518 | if (!String1.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
13519 | !String2.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
13520 | String1.Designator.Invalid || String2.Designator.Invalid) |
13521 | return false; |
13522 | |
13523 | QualType CharTy1 = String1.Designator.getType(Info.Ctx); |
13524 | QualType CharTy2 = String2.Designator.getType(Info.Ctx); |
13525 | |
13526 | bool IsRawByte = BuiltinOp == Builtin::BImemcmp || |
13527 | BuiltinOp == Builtin::BIbcmp || |
13528 | BuiltinOp == Builtin::BI__builtin_memcmp || |
13529 | BuiltinOp == Builtin::BI__builtin_bcmp; |
13530 | |
13531 | assert(IsRawByte || |
13532 | (Info.Ctx.hasSameUnqualifiedType( |
13533 | CharTy1, E->getArg(0)->getType()->getPointeeType()) && |
13534 | Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))); |
13535 | |
13536 | // For memcmp, allow comparing any arrays of '[[un]signed] char' or |
13537 | // 'char8_t', but no other types. |
13538 | if (IsRawByte && |
13539 | !(isOneByteCharacterType(T: CharTy1) && isOneByteCharacterType(T: CharTy2))) { |
13540 | // FIXME: Consider using our bit_cast implementation to support this. |
13541 | Info.FFDiag(E, diag::note_constexpr_memcmp_unsupported) |
13542 | << Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp) << CharTy1 |
13543 | << CharTy2; |
13544 | return false; |
13545 | } |
13546 | |
13547 | const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) { |
13548 | return handleLValueToRValueConversion(Info, E, CharTy1, String1, Char1) && |
13549 | handleLValueToRValueConversion(Info, E, CharTy2, String2, Char2) && |
13550 | Char1.isInt() && Char2.isInt(); |
13551 | }; |
13552 | const auto &AdvanceElems = [&] { |
13553 | return HandleLValueArrayAdjustment(Info, E, String1, CharTy1, 1) && |
13554 | HandleLValueArrayAdjustment(Info, E, String2, CharTy2, 1); |
13555 | }; |
13556 | |
13557 | bool StopAtNull = |
13558 | (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp && |
13559 | BuiltinOp != Builtin::BIwmemcmp && |
13560 | BuiltinOp != Builtin::BI__builtin_memcmp && |
13561 | BuiltinOp != Builtin::BI__builtin_bcmp && |
13562 | BuiltinOp != Builtin::BI__builtin_wmemcmp); |
13563 | bool IsWide = BuiltinOp == Builtin::BIwcscmp || |
13564 | BuiltinOp == Builtin::BIwcsncmp || |
13565 | BuiltinOp == Builtin::BIwmemcmp || |
13566 | BuiltinOp == Builtin::BI__builtin_wcscmp || |
13567 | BuiltinOp == Builtin::BI__builtin_wcsncmp || |
13568 | BuiltinOp == Builtin::BI__builtin_wmemcmp; |
13569 | |
13570 | for (; MaxLength; --MaxLength) { |
13571 | APValue Char1, Char2; |
13572 | if (!ReadCurElems(Char1, Char2)) |
13573 | return false; |
13574 | if (Char1.getInt().ne(RHS: Char2.getInt())) { |
13575 | if (IsWide) // wmemcmp compares with wchar_t signedness. |
13576 | return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E); |
13577 | // memcmp always compares unsigned chars. |
13578 | return Success(Char1.getInt().ult(RHS: Char2.getInt()) ? -1 : 1, E); |
13579 | } |
13580 | if (StopAtNull && !Char1.getInt()) |
13581 | return Success(0, E); |
13582 | assert(!(StopAtNull && !Char2.getInt())); |
13583 | if (!AdvanceElems()) |
13584 | return false; |
13585 | } |
13586 | // We hit the strncmp / memcmp limit. |
13587 | return Success(0, E); |
13588 | } |
13589 | |
13590 | case Builtin::BI__atomic_always_lock_free: |
13591 | case Builtin::BI__atomic_is_lock_free: |
13592 | case Builtin::BI__c11_atomic_is_lock_free: { |
13593 | APSInt SizeVal; |
13594 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SizeVal, Info)) |
13595 | return false; |
13596 | |
13597 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
13598 | // of two less than or equal to the maximum inline atomic width, we know it |
13599 | // is lock-free. If the size isn't a power of two, or greater than the |
13600 | // maximum alignment where we promote atomics, we know it is not lock-free |
13601 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
13602 | // the answer can only be determined at runtime; for example, 16-byte |
13603 | // atomics have lock-free implementations on some, but not all, |
13604 | // x86-64 processors. |
13605 | |
13606 | // Check power-of-two. |
13607 | CharUnits Size = CharUnits::fromQuantity(Quantity: SizeVal.getZExtValue()); |
13608 | if (Size.isPowerOfTwo()) { |
13609 | // Check against inlining width. |
13610 | unsigned InlineWidthBits = |
13611 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
13612 | if (Size <= Info.Ctx.toCharUnitsFromBits(BitSize: InlineWidthBits)) { |
13613 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
13614 | Size == CharUnits::One()) |
13615 | return Success(1, E); |
13616 | |
13617 | // If the pointer argument can be evaluated to a compile-time constant |
13618 | // integer (or nullptr), check if that value is appropriately aligned. |
13619 | const Expr *PtrArg = E->getArg(Arg: 1); |
13620 | Expr::EvalResult ExprResult; |
13621 | APSInt IntResult; |
13622 | if (PtrArg->EvaluateAsRValue(Result&: ExprResult, Ctx: Info.Ctx) && |
13623 | ExprResult.Val.toIntegralConstant(Result&: IntResult, SrcTy: PtrArg->getType(), |
13624 | Ctx: Info.Ctx) && |
13625 | IntResult.isAligned(A: Size.getAsAlign())) |
13626 | return Success(1, E); |
13627 | |
13628 | // Otherwise, check if the type's alignment against Size. |
13629 | if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: PtrArg)) { |
13630 | // Drop the potential implicit-cast to 'const volatile void*', getting |
13631 | // the underlying type. |
13632 | if (ICE->getCastKind() == CK_BitCast) |
13633 | PtrArg = ICE->getSubExpr(); |
13634 | } |
13635 | |
13636 | if (auto PtrTy = PtrArg->getType()->getAs<PointerType>()) { |
13637 | QualType PointeeType = PtrTy->getPointeeType(); |
13638 | if (!PointeeType->isIncompleteType() && |
13639 | Info.Ctx.getTypeAlignInChars(T: PointeeType) >= Size) { |
13640 | // OK, we will inline operations on this object. |
13641 | return Success(1, E); |
13642 | } |
13643 | } |
13644 | } |
13645 | } |
13646 | |
13647 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
13648 | Success(0, E) : Error(E); |
13649 | } |
13650 | case Builtin::BI__builtin_addcb: |
13651 | case Builtin::BI__builtin_addcs: |
13652 | case Builtin::BI__builtin_addc: |
13653 | case Builtin::BI__builtin_addcl: |
13654 | case Builtin::BI__builtin_addcll: |
13655 | case Builtin::BI__builtin_subcb: |
13656 | case Builtin::BI__builtin_subcs: |
13657 | case Builtin::BI__builtin_subc: |
13658 | case Builtin::BI__builtin_subcl: |
13659 | case Builtin::BI__builtin_subcll: { |
13660 | LValue CarryOutLValue; |
13661 | APSInt LHS, RHS, CarryIn, CarryOut, Result; |
13662 | QualType ResultType = E->getArg(Arg: 0)->getType(); |
13663 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) || |
13664 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) || |
13665 | !EvaluateInteger(E: E->getArg(Arg: 2), Result&: CarryIn, Info) || |
13666 | !EvaluatePointer(E: E->getArg(Arg: 3), Result&: CarryOutLValue, Info)) |
13667 | return false; |
13668 | // Copy the number of bits and sign. |
13669 | Result = LHS; |
13670 | CarryOut = LHS; |
13671 | |
13672 | bool FirstOverflowed = false; |
13673 | bool SecondOverflowed = false; |
13674 | switch (BuiltinOp) { |
13675 | default: |
13676 | llvm_unreachable("Invalid value for BuiltinOp"); |
13677 | case Builtin::BI__builtin_addcb: |
13678 | case Builtin::BI__builtin_addcs: |
13679 | case Builtin::BI__builtin_addc: |
13680 | case Builtin::BI__builtin_addcl: |
13681 | case Builtin::BI__builtin_addcll: |
13682 | Result = |
13683 | LHS.uadd_ov(RHS, Overflow&: FirstOverflowed).uadd_ov(RHS: CarryIn, Overflow&: SecondOverflowed); |
13684 | break; |
13685 | case Builtin::BI__builtin_subcb: |
13686 | case Builtin::BI__builtin_subcs: |
13687 | case Builtin::BI__builtin_subc: |
13688 | case Builtin::BI__builtin_subcl: |
13689 | case Builtin::BI__builtin_subcll: |
13690 | Result = |
13691 | LHS.usub_ov(RHS, Overflow&: FirstOverflowed).usub_ov(RHS: CarryIn, Overflow&: SecondOverflowed); |
13692 | break; |
13693 | } |
13694 | |
13695 | // It is possible for both overflows to happen but CGBuiltin uses an OR so |
13696 | // this is consistent. |
13697 | CarryOut = (uint64_t)(FirstOverflowed | SecondOverflowed); |
13698 | APValue APV{CarryOut}; |
13699 | if (!handleAssignment(Info, E, CarryOutLValue, ResultType, APV)) |
13700 | return false; |
13701 | return Success(Result, E); |
13702 | } |
13703 | case Builtin::BI__builtin_add_overflow: |
13704 | case Builtin::BI__builtin_sub_overflow: |
13705 | case Builtin::BI__builtin_mul_overflow: |
13706 | case Builtin::BI__builtin_sadd_overflow: |
13707 | case Builtin::BI__builtin_uadd_overflow: |
13708 | case Builtin::BI__builtin_uaddl_overflow: |
13709 | case Builtin::BI__builtin_uaddll_overflow: |
13710 | case Builtin::BI__builtin_usub_overflow: |
13711 | case Builtin::BI__builtin_usubl_overflow: |
13712 | case Builtin::BI__builtin_usubll_overflow: |
13713 | case Builtin::BI__builtin_umul_overflow: |
13714 | case Builtin::BI__builtin_umull_overflow: |
13715 | case Builtin::BI__builtin_umulll_overflow: |
13716 | case Builtin::BI__builtin_saddl_overflow: |
13717 | case Builtin::BI__builtin_saddll_overflow: |
13718 | case Builtin::BI__builtin_ssub_overflow: |
13719 | case Builtin::BI__builtin_ssubl_overflow: |
13720 | case Builtin::BI__builtin_ssubll_overflow: |
13721 | case Builtin::BI__builtin_smul_overflow: |
13722 | case Builtin::BI__builtin_smull_overflow: |
13723 | case Builtin::BI__builtin_smulll_overflow: { |
13724 | LValue ResultLValue; |
13725 | APSInt LHS, RHS; |
13726 | |
13727 | QualType ResultType = E->getArg(Arg: 2)->getType()->getPointeeType(); |
13728 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) || |
13729 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) || |
13730 | !EvaluatePointer(E: E->getArg(Arg: 2), Result&: ResultLValue, Info)) |
13731 | return false; |
13732 | |
13733 | APSInt Result; |
13734 | bool DidOverflow = false; |
13735 | |
13736 | // If the types don't have to match, enlarge all 3 to the largest of them. |
13737 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
13738 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
13739 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
13740 | bool IsSigned = LHS.isSigned() || RHS.isSigned() || |
13741 | ResultType->isSignedIntegerOrEnumerationType(); |
13742 | bool AllSigned = LHS.isSigned() && RHS.isSigned() && |
13743 | ResultType->isSignedIntegerOrEnumerationType(); |
13744 | uint64_t LHSSize = LHS.getBitWidth(); |
13745 | uint64_t RHSSize = RHS.getBitWidth(); |
13746 | uint64_t ResultSize = Info.Ctx.getTypeSize(T: ResultType); |
13747 | uint64_t MaxBits = std::max(a: std::max(a: LHSSize, b: RHSSize), b: ResultSize); |
13748 | |
13749 | // Add an additional bit if the signedness isn't uniformly agreed to. We |
13750 | // could do this ONLY if there is a signed and an unsigned that both have |
13751 | // MaxBits, but the code to check that is pretty nasty. The issue will be |
13752 | // caught in the shrink-to-result later anyway. |
13753 | if (IsSigned && !AllSigned) |
13754 | ++MaxBits; |
13755 | |
13756 | LHS = APSInt(LHS.extOrTrunc(width: MaxBits), !IsSigned); |
13757 | RHS = APSInt(RHS.extOrTrunc(width: MaxBits), !IsSigned); |
13758 | Result = APSInt(MaxBits, !IsSigned); |
13759 | } |
13760 | |
13761 | // Find largest int. |
13762 | switch (BuiltinOp) { |
13763 | default: |
13764 | llvm_unreachable("Invalid value for BuiltinOp"); |
13765 | case Builtin::BI__builtin_add_overflow: |
13766 | case Builtin::BI__builtin_sadd_overflow: |
13767 | case Builtin::BI__builtin_saddl_overflow: |
13768 | case Builtin::BI__builtin_saddll_overflow: |
13769 | case Builtin::BI__builtin_uadd_overflow: |
13770 | case Builtin::BI__builtin_uaddl_overflow: |
13771 | case Builtin::BI__builtin_uaddll_overflow: |
13772 | Result = LHS.isSigned() ? LHS.sadd_ov(RHS, Overflow&: DidOverflow) |
13773 | : LHS.uadd_ov(RHS, Overflow&: DidOverflow); |
13774 | break; |
13775 | case Builtin::BI__builtin_sub_overflow: |
13776 | case Builtin::BI__builtin_ssub_overflow: |
13777 | case Builtin::BI__builtin_ssubl_overflow: |
13778 | case Builtin::BI__builtin_ssubll_overflow: |
13779 | case Builtin::BI__builtin_usub_overflow: |
13780 | case Builtin::BI__builtin_usubl_overflow: |
13781 | case Builtin::BI__builtin_usubll_overflow: |
13782 | Result = LHS.isSigned() ? LHS.ssub_ov(RHS, Overflow&: DidOverflow) |
13783 | : LHS.usub_ov(RHS, Overflow&: DidOverflow); |
13784 | break; |
13785 | case Builtin::BI__builtin_mul_overflow: |
13786 | case Builtin::BI__builtin_smul_overflow: |
13787 | case Builtin::BI__builtin_smull_overflow: |
13788 | case Builtin::BI__builtin_smulll_overflow: |
13789 | case Builtin::BI__builtin_umul_overflow: |
13790 | case Builtin::BI__builtin_umull_overflow: |
13791 | case Builtin::BI__builtin_umulll_overflow: |
13792 | Result = LHS.isSigned() ? LHS.smul_ov(RHS, Overflow&: DidOverflow) |
13793 | : LHS.umul_ov(RHS, Overflow&: DidOverflow); |
13794 | break; |
13795 | } |
13796 | |
13797 | // In the case where multiple sizes are allowed, truncate and see if |
13798 | // the values are the same. |
13799 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
13800 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
13801 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
13802 | // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead, |
13803 | // since it will give us the behavior of a TruncOrSelf in the case where |
13804 | // its parameter <= its size. We previously set Result to be at least the |
13805 | // type-size of the result, so getTypeSize(ResultType) <= Result.BitWidth |
13806 | // will work exactly like TruncOrSelf. |
13807 | APSInt Temp = Result.extOrTrunc(width: Info.Ctx.getTypeSize(T: ResultType)); |
13808 | Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType()); |
13809 | |
13810 | if (!APSInt::isSameValue(I1: Temp, I2: Result)) |
13811 | DidOverflow = true; |
13812 | Result = Temp; |
13813 | } |
13814 | |
13815 | APValue APV{Result}; |
13816 | if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) |
13817 | return false; |
13818 | return Success(DidOverflow, E); |
13819 | } |
13820 | |
13821 | case Builtin::BI__builtin_reduce_add: |
13822 | case Builtin::BI__builtin_reduce_mul: |
13823 | case Builtin::BI__builtin_reduce_and: |
13824 | case Builtin::BI__builtin_reduce_or: |
13825 | case Builtin::BI__builtin_reduce_xor: |
13826 | case Builtin::BI__builtin_reduce_min: |
13827 | case Builtin::BI__builtin_reduce_max: { |
13828 | APValue Source; |
13829 | if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source)) |
13830 | return false; |
13831 | |
13832 | unsigned SourceLen = Source.getVectorLength(); |
13833 | APSInt Reduced = Source.getVectorElt(I: 0).getInt(); |
13834 | for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) { |
13835 | switch (BuiltinOp) { |
13836 | default: |
13837 | return false; |
13838 | case Builtin::BI__builtin_reduce_add: { |
13839 | if (!CheckedIntArithmetic( |
13840 | Info, E, Reduced, Source.getVectorElt(I: EltNum).getInt(), |
13841 | Reduced.getBitWidth() + 1, std::plus<APSInt>(), Reduced)) |
13842 | return false; |
13843 | break; |
13844 | } |
13845 | case Builtin::BI__builtin_reduce_mul: { |
13846 | if (!CheckedIntArithmetic( |
13847 | Info, E, Reduced, Source.getVectorElt(I: EltNum).getInt(), |
13848 | Reduced.getBitWidth() * 2, std::multiplies<APSInt>(), Reduced)) |
13849 | return false; |
13850 | break; |
13851 | } |
13852 | case Builtin::BI__builtin_reduce_and: { |
13853 | Reduced &= Source.getVectorElt(I: EltNum).getInt(); |
13854 | break; |
13855 | } |
13856 | case Builtin::BI__builtin_reduce_or: { |
13857 | Reduced |= Source.getVectorElt(I: EltNum).getInt(); |
13858 | break; |
13859 | } |
13860 | case Builtin::BI__builtin_reduce_xor: { |
13861 | Reduced ^= Source.getVectorElt(I: EltNum).getInt(); |
13862 | break; |
13863 | } |
13864 | case Builtin::BI__builtin_reduce_min: { |
13865 | Reduced = std::min(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt()); |
13866 | break; |
13867 | } |
13868 | case Builtin::BI__builtin_reduce_max: { |
13869 | Reduced = std::max(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt()); |
13870 | break; |
13871 | } |
13872 | } |
13873 | } |
13874 | |
13875 | return Success(Reduced, E); |
13876 | } |
13877 | |
13878 | case clang::X86::BI__builtin_ia32_addcarryx_u32: |
13879 | case clang::X86::BI__builtin_ia32_addcarryx_u64: |
13880 | case clang::X86::BI__builtin_ia32_subborrow_u32: |
13881 | case clang::X86::BI__builtin_ia32_subborrow_u64: { |
13882 | LValue ResultLValue; |
13883 | APSInt CarryIn, LHS, RHS; |
13884 | QualType ResultType = E->getArg(Arg: 3)->getType()->getPointeeType(); |
13885 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CarryIn, Info) || |
13886 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: LHS, Info) || |
13887 | !EvaluateInteger(E: E->getArg(Arg: 2), Result&: RHS, Info) || |
13888 | !EvaluatePointer(E: E->getArg(Arg: 3), Result&: ResultLValue, Info)) |
13889 | return false; |
13890 | |
13891 | bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 || |
13892 | BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64; |
13893 | |
13894 | unsigned BitWidth = LHS.getBitWidth(); |
13895 | unsigned CarryInBit = CarryIn.ugt(RHS: 0) ? 1 : 0; |
13896 | APInt ExResult = |
13897 | IsAdd |
13898 | ? (LHS.zext(width: BitWidth + 1) + (RHS.zext(width: BitWidth + 1) + CarryInBit)) |
13899 | : (LHS.zext(width: BitWidth + 1) - (RHS.zext(width: BitWidth + 1) + CarryInBit)); |
13900 | |
13901 | APInt Result = ExResult.extractBits(numBits: BitWidth, bitPosition: 0); |
13902 | uint64_t CarryOut = ExResult.extractBitsAsZExtValue(numBits: 1, bitPosition: BitWidth); |
13903 | |
13904 | APValue APV{APSInt(Result, /*isUnsigned=*/true)}; |
13905 | if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) |
13906 | return false; |
13907 | return Success(CarryOut, E); |
13908 | } |
13909 | |
13910 | case clang::X86::BI__builtin_ia32_bextr_u32: |
13911 | case clang::X86::BI__builtin_ia32_bextr_u64: |
13912 | case clang::X86::BI__builtin_ia32_bextri_u32: |
13913 | case clang::X86::BI__builtin_ia32_bextri_u64: { |
13914 | APSInt Val, Idx; |
13915 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) || |
13916 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info)) |
13917 | return false; |
13918 | |
13919 | unsigned BitWidth = Val.getBitWidth(); |
13920 | uint64_t Shift = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0); |
13921 | uint64_t Length = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 8); |
13922 | Length = Length > BitWidth ? BitWidth : Length; |
13923 | |
13924 | // Handle out of bounds cases. |
13925 | if (Length == 0 || Shift >= BitWidth) |
13926 | return Success(0, E); |
13927 | |
13928 | uint64_t Result = Val.getZExtValue() >> Shift; |
13929 | Result &= llvm::maskTrailingOnes<uint64_t>(N: Length); |
13930 | return Success(Result, E); |
13931 | } |
13932 | |
13933 | case clang::X86::BI__builtin_ia32_bzhi_si: |
13934 | case clang::X86::BI__builtin_ia32_bzhi_di: { |
13935 | APSInt Val, Idx; |
13936 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) || |
13937 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info)) |
13938 | return false; |
13939 | |
13940 | unsigned BitWidth = Val.getBitWidth(); |
13941 | unsigned Index = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0); |
13942 | if (Index < BitWidth) |
13943 | Val.clearHighBits(hiBits: BitWidth - Index); |
13944 | return Success(Val, E); |
13945 | } |
13946 | |
13947 | case clang::X86::BI__builtin_ia32_lzcnt_u16: |
13948 | case clang::X86::BI__builtin_ia32_lzcnt_u32: |
13949 | case clang::X86::BI__builtin_ia32_lzcnt_u64: { |
13950 | APSInt Val; |
13951 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13952 | return false; |
13953 | return Success(Val.countLeadingZeros(), E); |
13954 | } |
13955 | |
13956 | case clang::X86::BI__builtin_ia32_tzcnt_u16: |
13957 | case clang::X86::BI__builtin_ia32_tzcnt_u32: |
13958 | case clang::X86::BI__builtin_ia32_tzcnt_u64: { |
13959 | APSInt Val; |
13960 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
13961 | return false; |
13962 | return Success(Val.countTrailingZeros(), E); |
13963 | } |
13964 | |
13965 | case clang::X86::BI__builtin_ia32_pdep_si: |
13966 | case clang::X86::BI__builtin_ia32_pdep_di: { |
13967 | APSInt Val, Msk; |
13968 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) || |
13969 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info)) |
13970 | return false; |
13971 | |
13972 | unsigned BitWidth = Val.getBitWidth(); |
13973 | APInt Result = APInt::getZero(numBits: BitWidth); |
13974 | for (unsigned I = 0, P = 0; I != BitWidth; ++I) |
13975 | if (Msk[I]) |
13976 | Result.setBitVal(BitPosition: I, BitValue: Val[P++]); |
13977 | return Success(Result, E); |
13978 | } |
13979 | |
13980 | case clang::X86::BI__builtin_ia32_pext_si: |
13981 | case clang::X86::BI__builtin_ia32_pext_di: { |
13982 | APSInt Val, Msk; |
13983 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) || |
13984 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info)) |
13985 | return false; |
13986 | |
13987 | unsigned BitWidth = Val.getBitWidth(); |
13988 | APInt Result = APInt::getZero(numBits: BitWidth); |
13989 | for (unsigned I = 0, P = 0; I != BitWidth; ++I) |
13990 | if (Msk[I]) |
13991 | Result.setBitVal(BitPosition: P++, BitValue: Val[I]); |
13992 | return Success(Result, E); |
13993 | } |
13994 | } |
13995 | } |
13996 | |
13997 | /// Determine whether this is a pointer past the end of the complete |
13998 | /// object referred to by the lvalue. |
13999 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
14000 | const LValue &LV) { |
14001 | // A null pointer can be viewed as being "past the end" but we don't |
14002 | // choose to look at it that way here. |
14003 | if (!LV.getLValueBase()) |
14004 | return false; |
14005 | |
14006 | // If the designator is valid and refers to a subobject, we're not pointing |
14007 | // past the end. |
14008 | if (!LV.getLValueDesignator().Invalid && |
14009 | !LV.getLValueDesignator().isOnePastTheEnd()) |
14010 | return false; |
14011 | |
14012 | // A pointer to an incomplete type might be past-the-end if the type's size is |
14013 | // zero. We cannot tell because the type is incomplete. |
14014 | QualType Ty = getType(B: LV.getLValueBase()); |
14015 | if (Ty->isIncompleteType()) |
14016 | return true; |
14017 | |
14018 | // Can't be past the end of an invalid object. |
14019 | if (LV.getLValueDesignator().Invalid) |
14020 | return false; |
14021 | |
14022 | // We're a past-the-end pointer if we point to the byte after the object, |
14023 | // no matter what our type or path is. |
14024 | auto Size = Ctx.getTypeSizeInChars(T: Ty); |
14025 | return LV.getLValueOffset() == Size; |
14026 | } |
14027 | |
14028 | namespace { |
14029 | |
14030 | /// Data recursive integer evaluator of certain binary operators. |
14031 | /// |
14032 | /// We use a data recursive algorithm for binary operators so that we are able |
14033 | /// to handle extreme cases of chained binary operators without causing stack |
14034 | /// overflow. |
14035 | class DataRecursiveIntBinOpEvaluator { |
14036 | struct EvalResult { |
14037 | APValue Val; |
14038 | bool Failed = false; |
14039 | |
14040 | EvalResult() = default; |
14041 | |
14042 | void swap(EvalResult &RHS) { |
14043 | Val.swap(RHS&: RHS.Val); |
14044 | Failed = RHS.Failed; |
14045 | RHS.Failed = false; |
14046 | } |
14047 | }; |
14048 | |
14049 | struct Job { |
14050 | const Expr *E; |
14051 | EvalResult LHSResult; // meaningful only for binary operator expression. |
14052 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
14053 | |
14054 | Job() = default; |
14055 | Job(Job &&) = default; |
14056 | |
14057 | void startSpeculativeEval(EvalInfo &Info) { |
14058 | SpecEvalRAII = SpeculativeEvaluationRAII(Info); |
14059 | } |
14060 | |
14061 | private: |
14062 | SpeculativeEvaluationRAII SpecEvalRAII; |
14063 | }; |
14064 | |
14065 | SmallVector<Job, 16> Queue; |
14066 | |
14067 | IntExprEvaluator &IntEval; |
14068 | EvalInfo &Info; |
14069 | APValue &FinalResult; |
14070 | |
14071 | public: |
14072 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
14073 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
14074 | |
14075 | /// True if \param E is a binary operator that we are going to handle |
14076 | /// data recursively. |
14077 | /// We handle binary operators that are comma, logical, or that have operands |
14078 | /// with integral or enumeration type. |
14079 | static bool shouldEnqueue(const BinaryOperator *E) { |
14080 | return E->getOpcode() == BO_Comma || E->isLogicalOp() || |
14081 | (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() && |
14082 | E->getLHS()->getType()->isIntegralOrEnumerationType() && |
14083 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
14084 | } |
14085 | |
14086 | bool Traverse(const BinaryOperator *E) { |
14087 | enqueue(E); |
14088 | EvalResult PrevResult; |
14089 | while (!Queue.empty()) |
14090 | process(Result&: PrevResult); |
14091 | |
14092 | if (PrevResult.Failed) return false; |
14093 | |
14094 | FinalResult.swap(RHS&: PrevResult.Val); |
14095 | return true; |
14096 | } |
14097 | |
14098 | private: |
14099 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
14100 | return IntEval.Success(Value, E, Result); |
14101 | } |
14102 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
14103 | return IntEval.Success(SI: Value, E, Result); |
14104 | } |
14105 | bool Error(const Expr *E) { |
14106 | return IntEval.Error(E); |
14107 | } |
14108 | bool Error(const Expr *E, diag::kind D) { |
14109 | return IntEval.Error(E, D); |
14110 | } |
14111 | |
14112 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
14113 | return Info.CCEDiag(E, DiagId: D); |
14114 | } |
14115 | |
14116 | // Returns true if visiting the RHS is necessary, false otherwise. |
14117 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
14118 | bool &SuppressRHSDiags); |
14119 | |
14120 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
14121 | const BinaryOperator *E, APValue &Result); |
14122 | |
14123 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
14124 | Result.Failed = !Evaluate(Result&: Result.Val, Info, E); |
14125 | if (Result.Failed) |
14126 | Result.Val = APValue(); |
14127 | } |
14128 | |
14129 | void process(EvalResult &Result); |
14130 | |
14131 | void enqueue(const Expr *E) { |
14132 | E = E->IgnoreParens(); |
14133 | Queue.resize(N: Queue.size()+1); |
14134 | Queue.back().E = E; |
14135 | Queue.back().Kind = Job::AnyExprKind; |
14136 | } |
14137 | }; |
14138 | |
14139 | } |
14140 | |
14141 | bool DataRecursiveIntBinOpEvaluator:: |
14142 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
14143 | bool &SuppressRHSDiags) { |
14144 | if (E->getOpcode() == BO_Comma) { |
14145 | // Ignore LHS but note if we could not evaluate it. |
14146 | if (LHSResult.Failed) |
14147 | return Info.noteSideEffect(); |
14148 | return true; |
14149 | } |
14150 | |
14151 | if (E->isLogicalOp()) { |
14152 | bool LHSAsBool; |
14153 | if (!LHSResult.Failed && HandleConversionToBool(Val: LHSResult.Val, Result&: LHSAsBool)) { |
14154 | // We were able to evaluate the LHS, see if we can get away with not |
14155 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
14156 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
14157 | Success(LHSAsBool, E, LHSResult.Val); |
14158 | return false; // Ignore RHS |
14159 | } |
14160 | } else { |
14161 | LHSResult.Failed = true; |
14162 | |
14163 | // Since we weren't able to evaluate the left hand side, it |
14164 | // might have had side effects. |
14165 | if (!Info.noteSideEffect()) |
14166 | return false; |
14167 | |
14168 | // We can't evaluate the LHS; however, sometimes the result |
14169 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
14170 | // Don't ignore RHS and suppress diagnostics from this arm. |
14171 | SuppressRHSDiags = true; |
14172 | } |
14173 | |
14174 | return true; |
14175 | } |
14176 | |
14177 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
14178 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
14179 | |
14180 | if (LHSResult.Failed && !Info.noteFailure()) |
14181 | return false; // Ignore RHS; |
14182 | |
14183 | return true; |
14184 | } |
14185 | |
14186 | static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index, |
14187 | bool IsSub) { |
14188 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
14189 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
14190 | // offsets. |
14191 | assert(!LVal.hasLValuePath() && "have designator for integer lvalue"); |
14192 | CharUnits &Offset = LVal.getLValueOffset(); |
14193 | uint64_t Offset64 = Offset.getQuantity(); |
14194 | uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue(); |
14195 | Offset = CharUnits::fromQuantity(Quantity: IsSub ? Offset64 - Index64 |
14196 | : Offset64 + Index64); |
14197 | } |
14198 | |
14199 | bool DataRecursiveIntBinOpEvaluator:: |
14200 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
14201 | const BinaryOperator *E, APValue &Result) { |
14202 | if (E->getOpcode() == BO_Comma) { |
14203 | if (RHSResult.Failed) |
14204 | return false; |
14205 | Result = RHSResult.Val; |
14206 | return true; |
14207 | } |
14208 | |
14209 | if (E->isLogicalOp()) { |
14210 | bool lhsResult, rhsResult; |
14211 | bool LHSIsOK = HandleConversionToBool(Val: LHSResult.Val, Result&: lhsResult); |
14212 | bool RHSIsOK = HandleConversionToBool(Val: RHSResult.Val, Result&: rhsResult); |
14213 | |
14214 | if (LHSIsOK) { |
14215 | if (RHSIsOK) { |
14216 | if (E->getOpcode() == BO_LOr) |
14217 | return Success(lhsResult || rhsResult, E, Result); |
14218 | else |
14219 | return Success(lhsResult && rhsResult, E, Result); |
14220 | } |
14221 | } else { |
14222 | if (RHSIsOK) { |
14223 | // We can't evaluate the LHS; however, sometimes the result |
14224 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
14225 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
14226 | return Success(rhsResult, E, Result); |
14227 | } |
14228 | } |
14229 | |
14230 | return false; |
14231 | } |
14232 | |
14233 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
14234 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
14235 | |
14236 | if (LHSResult.Failed || RHSResult.Failed) |
14237 | return false; |
14238 | |
14239 | const APValue &LHSVal = LHSResult.Val; |
14240 | const APValue &RHSVal = RHSResult.Val; |
14241 | |
14242 | // Handle cases like (unsigned long)&a + 4. |
14243 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
14244 | Result = LHSVal; |
14245 | addOrSubLValueAsInteger(LVal&: Result, Index: RHSVal.getInt(), IsSub: E->getOpcode() == BO_Sub); |
14246 | return true; |
14247 | } |
14248 | |
14249 | // Handle cases like 4 + (unsigned long)&a |
14250 | if (E->getOpcode() == BO_Add && |
14251 | RHSVal.isLValue() && LHSVal.isInt()) { |
14252 | Result = RHSVal; |
14253 | addOrSubLValueAsInteger(LVal&: Result, Index: LHSVal.getInt(), /*IsSub*/false); |
14254 | return true; |
14255 | } |
14256 | |
14257 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
14258 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
14259 | if (!LHSVal.getLValueOffset().isZero() || |
14260 | !RHSVal.getLValueOffset().isZero()) |
14261 | return false; |
14262 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
14263 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
14264 | if (!LHSExpr || !RHSExpr) |
14265 | return false; |
14266 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr); |
14267 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr); |
14268 | if (!LHSAddrExpr || !RHSAddrExpr) |
14269 | return false; |
14270 | // Make sure both labels come from the same function. |
14271 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
14272 | RHSAddrExpr->getLabel()->getDeclContext()) |
14273 | return false; |
14274 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
14275 | return true; |
14276 | } |
14277 | |
14278 | // All the remaining cases expect both operands to be an integer |
14279 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
14280 | return Error(E); |
14281 | |
14282 | // Set up the width and signedness manually, in case it can't be deduced |
14283 | // from the operation we're performing. |
14284 | // FIXME: Don't do this in the cases where we can deduce it. |
14285 | APSInt Value(Info.Ctx.getIntWidth(T: E->getType()), |
14286 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
14287 | if (!handleIntIntBinOp(Info, E, LHS: LHSVal.getInt(), Opcode: E->getOpcode(), |
14288 | RHS: RHSVal.getInt(), Result&: Value)) |
14289 | return false; |
14290 | return Success(Value, E, Result); |
14291 | } |
14292 | |
14293 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
14294 | Job &job = Queue.back(); |
14295 | |
14296 | switch (job.Kind) { |
14297 | case Job::AnyExprKind: { |
14298 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(Val: job.E)) { |
14299 | if (shouldEnqueue(E: Bop)) { |
14300 | job.Kind = Job::BinOpKind; |
14301 | enqueue(E: Bop->getLHS()); |
14302 | return; |
14303 | } |
14304 | } |
14305 | |
14306 | EvaluateExpr(E: job.E, Result); |
14307 | Queue.pop_back(); |
14308 | return; |
14309 | } |
14310 | |
14311 | case Job::BinOpKind: { |
14312 | const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E); |
14313 | bool SuppressRHSDiags = false; |
14314 | if (!VisitBinOpLHSOnly(LHSResult&: Result, E: Bop, SuppressRHSDiags)) { |
14315 | Queue.pop_back(); |
14316 | return; |
14317 | } |
14318 | if (SuppressRHSDiags) |
14319 | job.startSpeculativeEval(Info); |
14320 | job.LHSResult.swap(RHS&: Result); |
14321 | job.Kind = Job::BinOpVisitedLHSKind; |
14322 | enqueue(E: Bop->getRHS()); |
14323 | return; |
14324 | } |
14325 | |
14326 | case Job::BinOpVisitedLHSKind: { |
14327 | const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E); |
14328 | EvalResult RHS; |
14329 | RHS.swap(RHS&: Result); |
14330 | Result.Failed = !VisitBinOp(LHSResult: job.LHSResult, RHSResult: RHS, E: Bop, Result&: Result.Val); |
14331 | Queue.pop_back(); |
14332 | return; |
14333 | } |
14334 | } |
14335 | |
14336 | llvm_unreachable("Invalid Job::Kind!"); |
14337 | } |
14338 | |
14339 | namespace { |
14340 | enum class CmpResult { |
14341 | Unequal, |
14342 | Less, |
14343 | Equal, |
14344 | Greater, |
14345 | Unordered, |
14346 | }; |
14347 | } |
14348 | |
14349 | template <class SuccessCB, class AfterCB> |
14350 | static bool |
14351 | EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, |
14352 | SuccessCB &&Success, AfterCB &&DoAfter) { |
14353 | assert(!E->isValueDependent()); |
14354 | assert(E->isComparisonOp() && "expected comparison operator"); |
14355 | assert((E->getOpcode() == BO_Cmp || |
14356 | E->getType()->isIntegralOrEnumerationType()) && |
14357 | "unsupported binary expression evaluation"); |
14358 | auto Error = [&](const Expr *E) { |
14359 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
14360 | return false; |
14361 | }; |
14362 | |
14363 | bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp; |
14364 | bool IsEquality = E->isEqualityOp(); |
14365 | |
14366 | QualType LHSTy = E->getLHS()->getType(); |
14367 | QualType RHSTy = E->getRHS()->getType(); |
14368 | |
14369 | if (LHSTy->isIntegralOrEnumerationType() && |
14370 | RHSTy->isIntegralOrEnumerationType()) { |
14371 | APSInt LHS, RHS; |
14372 | bool LHSOK = EvaluateInteger(E: E->getLHS(), Result&: LHS, Info); |
14373 | if (!LHSOK && !Info.noteFailure()) |
14374 | return false; |
14375 | if (!EvaluateInteger(E: E->getRHS(), Result&: RHS, Info) || !LHSOK) |
14376 | return false; |
14377 | if (LHS < RHS) |
14378 | return Success(CmpResult::Less, E); |
14379 | if (LHS > RHS) |
14380 | return Success(CmpResult::Greater, E); |
14381 | return Success(CmpResult::Equal, E); |
14382 | } |
14383 | |
14384 | if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) { |
14385 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHSTy)); |
14386 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHSTy)); |
14387 | |
14388 | bool LHSOK = EvaluateFixedPointOrInteger(E: E->getLHS(), Result&: LHSFX, Info); |
14389 | if (!LHSOK && !Info.noteFailure()) |
14390 | return false; |
14391 | if (!EvaluateFixedPointOrInteger(E: E->getRHS(), Result&: RHSFX, Info) || !LHSOK) |
14392 | return false; |
14393 | if (LHSFX < RHSFX) |
14394 | return Success(CmpResult::Less, E); |
14395 | if (LHSFX > RHSFX) |
14396 | return Success(CmpResult::Greater, E); |
14397 | return Success(CmpResult::Equal, E); |
14398 | } |
14399 | |
14400 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
14401 | ComplexValue LHS, RHS; |
14402 | bool LHSOK; |
14403 | if (E->isAssignmentOp()) { |
14404 | LValue LV; |
14405 | EvaluateLValue(E: E->getLHS(), Result&: LV, Info); |
14406 | LHSOK = false; |
14407 | } else if (LHSTy->isRealFloatingType()) { |
14408 | LHSOK = EvaluateFloat(E: E->getLHS(), Result&: LHS.FloatReal, Info); |
14409 | if (LHSOK) { |
14410 | LHS.makeComplexFloat(); |
14411 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
14412 | } |
14413 | } else { |
14414 | LHSOK = EvaluateComplex(E: E->getLHS(), Res&: LHS, Info); |
14415 | } |
14416 | if (!LHSOK && !Info.noteFailure()) |
14417 | return false; |
14418 | |
14419 | if (E->getRHS()->getType()->isRealFloatingType()) { |
14420 | if (!EvaluateFloat(E: E->getRHS(), Result&: RHS.FloatReal, Info) || !LHSOK) |
14421 | return false; |
14422 | RHS.makeComplexFloat(); |
14423 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
14424 | } else if (!EvaluateComplex(E: E->getRHS(), Res&: RHS, Info) || !LHSOK) |
14425 | return false; |
14426 | |
14427 | if (LHS.isComplexFloat()) { |
14428 | APFloat::cmpResult CR_r = |
14429 | LHS.getComplexFloatReal().compare(RHS: RHS.getComplexFloatReal()); |
14430 | APFloat::cmpResult CR_i = |
14431 | LHS.getComplexFloatImag().compare(RHS: RHS.getComplexFloatImag()); |
14432 | bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual; |
14433 | return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E); |
14434 | } else { |
14435 | assert(IsEquality && "invalid complex comparison"); |
14436 | bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
14437 | LHS.getComplexIntImag() == RHS.getComplexIntImag(); |
14438 | return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E); |
14439 | } |
14440 | } |
14441 | |
14442 | if (LHSTy->isRealFloatingType() && |
14443 | RHSTy->isRealFloatingType()) { |
14444 | APFloat RHS(0.0), LHS(0.0); |
14445 | |
14446 | bool LHSOK = EvaluateFloat(E: E->getRHS(), Result&: RHS, Info); |
14447 | if (!LHSOK && !Info.noteFailure()) |
14448 | return false; |
14449 | |
14450 | if (!EvaluateFloat(E: E->getLHS(), Result&: LHS, Info) || !LHSOK) |
14451 | return false; |
14452 | |
14453 | assert(E->isComparisonOp() && "Invalid binary operator!"); |
14454 | llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS); |
14455 | if (!Info.InConstantContext && |
14456 | APFloatCmpResult == APFloat::cmpUnordered && |
14457 | E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained()) { |
14458 | // Note: Compares may raise invalid in some cases involving NaN or sNaN. |
14459 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
14460 | return false; |
14461 | } |
14462 | auto GetCmpRes = [&]() { |
14463 | switch (APFloatCmpResult) { |
14464 | case APFloat::cmpEqual: |
14465 | return CmpResult::Equal; |
14466 | case APFloat::cmpLessThan: |
14467 | return CmpResult::Less; |
14468 | case APFloat::cmpGreaterThan: |
14469 | return CmpResult::Greater; |
14470 | case APFloat::cmpUnordered: |
14471 | return CmpResult::Unordered; |
14472 | } |
14473 | llvm_unreachable("Unrecognised APFloat::cmpResult enum"); |
14474 | }; |
14475 | return Success(GetCmpRes(), E); |
14476 | } |
14477 | |
14478 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
14479 | LValue LHSValue, RHSValue; |
14480 | |
14481 | bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info); |
14482 | if (!LHSOK && !Info.noteFailure()) |
14483 | return false; |
14484 | |
14485 | if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK) |
14486 | return false; |
14487 | |
14488 | // If we have Unknown pointers we should fail if they are not global values. |
14489 | if (!(IsGlobalLValue(B: LHSValue.getLValueBase()) && |
14490 | IsGlobalLValue(B: RHSValue.getLValueBase())) && |
14491 | (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)) |
14492 | return false; |
14493 | |
14494 | // Reject differing bases from the normal codepath; we special-case |
14495 | // comparisons to null. |
14496 | if (!HasSameBase(A: LHSValue, B: RHSValue)) { |
14497 | auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) { |
14498 | std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType()); |
14499 | std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType()); |
14500 | Info.FFDiag(E, DiagID) |
14501 | << (Reversed ? RHS : LHS) << (Reversed ? LHS : RHS); |
14502 | return false; |
14503 | }; |
14504 | // Inequalities and subtractions between unrelated pointers have |
14505 | // unspecified or undefined behavior. |
14506 | if (!IsEquality) |
14507 | return DiagComparison( |
14508 | diag::note_constexpr_pointer_comparison_unspecified); |
14509 | // A constant address may compare equal to the address of a symbol. |
14510 | // The one exception is that address of an object cannot compare equal |
14511 | // to a null pointer constant. |
14512 | // TODO: Should we restrict this to actual null pointers, and exclude the |
14513 | // case of zero cast to pointer type? |
14514 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
14515 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
14516 | return DiagComparison(diag::note_constexpr_pointer_constant_comparison, |
14517 | !RHSValue.Base); |
14518 | // C++2c [intro.object]/10: |
14519 | // Two objects [...] may have the same address if [...] they are both |
14520 | // potentially non-unique objects. |
14521 | // C++2c [intro.object]/9: |
14522 | // An object is potentially non-unique if it is a string literal object, |
14523 | // the backing array of an initializer list, or a subobject thereof. |
14524 | // |
14525 | // This makes the comparison result unspecified, so it's not a constant |
14526 | // expression. |
14527 | // |
14528 | // TODO: Do we need to handle the initializer list case here? |
14529 | if (ArePotentiallyOverlappingStringLiterals(Info, LHSValue, RHSValue)) |
14530 | return DiagComparison(diag::note_constexpr_literal_comparison); |
14531 | if (IsOpaqueConstantCall(LHSValue) || IsOpaqueConstantCall(RHSValue)) |
14532 | return DiagComparison(diag::note_constexpr_opaque_call_comparison, |
14533 | !IsOpaqueConstantCall(LHSValue)); |
14534 | // We can't tell whether weak symbols will end up pointing to the same |
14535 | // object. |
14536 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
14537 | return DiagComparison(diag::note_constexpr_pointer_weak_comparison, |
14538 | !IsWeakLValue(LHSValue)); |
14539 | // We can't compare the address of the start of one object with the |
14540 | // past-the-end address of another object, per C++ DR1652. |
14541 | if (LHSValue.Base && LHSValue.Offset.isZero() && |
14542 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) |
14543 | return DiagComparison(diag::note_constexpr_pointer_comparison_past_end, |
14544 | true); |
14545 | if (RHSValue.Base && RHSValue.Offset.isZero() && |
14546 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue)) |
14547 | return DiagComparison(diag::note_constexpr_pointer_comparison_past_end, |
14548 | false); |
14549 | // We can't tell whether an object is at the same address as another |
14550 | // zero sized object. |
14551 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
14552 | (LHSValue.Base && isZeroSized(RHSValue))) |
14553 | return DiagComparison( |
14554 | diag::note_constexpr_pointer_comparison_zero_sized); |
14555 | return Success(CmpResult::Unequal, E); |
14556 | } |
14557 | |
14558 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
14559 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
14560 | |
14561 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
14562 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
14563 | |
14564 | // C++11 [expr.rel]p2: |
14565 | // - If two pointers point to non-static data members of the same object, |
14566 | // or to subobjects or array elements fo such members, recursively, the |
14567 | // pointer to the later declared member compares greater provided the |
14568 | // two members have the same access control and provided their class is |
14569 | // not a union. |
14570 | // [...] |
14571 | // - Otherwise pointer comparisons are unspecified. |
14572 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) { |
14573 | bool WasArrayIndex; |
14574 | unsigned Mismatch = FindDesignatorMismatch( |
14575 | ObjType: getType(B: LHSValue.Base), A: LHSDesignator, B: RHSDesignator, WasArrayIndex); |
14576 | // At the point where the designators diverge, the comparison has a |
14577 | // specified value if: |
14578 | // - we are comparing array indices |
14579 | // - we are comparing fields of a union, or fields with the same access |
14580 | // Otherwise, the result is unspecified and thus the comparison is not a |
14581 | // constant expression. |
14582 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
14583 | Mismatch < RHSDesignator.Entries.size()) { |
14584 | const FieldDecl *LF = getAsField(E: LHSDesignator.Entries[Mismatch]); |
14585 | const FieldDecl *RF = getAsField(E: RHSDesignator.Entries[Mismatch]); |
14586 | if (!LF && !RF) |
14587 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
14588 | else if (!LF) |
14589 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
14590 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
14591 | << RF->getParent() << RF; |
14592 | else if (!RF) |
14593 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
14594 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
14595 | << LF->getParent() << LF; |
14596 | else if (!LF->getParent()->isUnion() && |
14597 | LF->getAccess() != RF->getAccess()) |
14598 | Info.CCEDiag(E, |
14599 | diag::note_constexpr_pointer_comparison_differing_access) |
14600 | << LF << LF->getAccess() << RF << RF->getAccess() |
14601 | << LF->getParent(); |
14602 | } |
14603 | } |
14604 | |
14605 | // The comparison here must be unsigned, and performed with the same |
14606 | // width as the pointer. |
14607 | unsigned PtrSize = Info.Ctx.getTypeSize(T: LHSTy); |
14608 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
14609 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
14610 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
14611 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
14612 | CompareLHS &= Mask; |
14613 | CompareRHS &= Mask; |
14614 | |
14615 | // If there is a base and this is a relational operator, we can only |
14616 | // compare pointers within the object in question; otherwise, the result |
14617 | // depends on where the object is located in memory. |
14618 | if (!LHSValue.Base.isNull() && IsRelational) { |
14619 | QualType BaseTy = getType(B: LHSValue.Base); |
14620 | if (BaseTy->isIncompleteType()) |
14621 | return Error(E); |
14622 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T: BaseTy); |
14623 | uint64_t OffsetLimit = Size.getQuantity(); |
14624 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
14625 | return Error(E); |
14626 | } |
14627 | |
14628 | if (CompareLHS < CompareRHS) |
14629 | return Success(CmpResult::Less, E); |
14630 | if (CompareLHS > CompareRHS) |
14631 | return Success(CmpResult::Greater, E); |
14632 | return Success(CmpResult::Equal, E); |
14633 | } |
14634 | |
14635 | if (LHSTy->isMemberPointerType()) { |
14636 | assert(IsEquality && "unexpected member pointer operation"); |
14637 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
14638 | |
14639 | MemberPtr LHSValue, RHSValue; |
14640 | |
14641 | bool LHSOK = EvaluateMemberPointer(E: E->getLHS(), Result&: LHSValue, Info); |
14642 | if (!LHSOK && !Info.noteFailure()) |
14643 | return false; |
14644 | |
14645 | if (!EvaluateMemberPointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK) |
14646 | return false; |
14647 | |
14648 | // If either operand is a pointer to a weak function, the comparison is not |
14649 | // constant. |
14650 | if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) { |
14651 | Info.FFDiag(E, diag::note_constexpr_mem_pointer_weak_comparison) |
14652 | << LHSValue.getDecl(); |
14653 | return false; |
14654 | } |
14655 | if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) { |
14656 | Info.FFDiag(E, diag::note_constexpr_mem_pointer_weak_comparison) |
14657 | << RHSValue.getDecl(); |
14658 | return false; |
14659 | } |
14660 | |
14661 | // C++11 [expr.eq]p2: |
14662 | // If both operands are null, they compare equal. Otherwise if only one is |
14663 | // null, they compare unequal. |
14664 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
14665 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
14666 | return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E); |
14667 | } |
14668 | |
14669 | // Otherwise if either is a pointer to a virtual member function, the |
14670 | // result is unspecified. |
14671 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
14672 | if (MD->isVirtual()) |
14673 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
14674 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
14675 | if (MD->isVirtual()) |
14676 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
14677 | |
14678 | // Otherwise they compare equal if and only if they would refer to the |
14679 | // same member of the same most derived object or the same subobject if |
14680 | // they were dereferenced with a hypothetical object of the associated |
14681 | // class type. |
14682 | bool Equal = LHSValue == RHSValue; |
14683 | return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E); |
14684 | } |
14685 | |
14686 | if (LHSTy->isNullPtrType()) { |
14687 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
14688 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
14689 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
14690 | // are compared, the result is true of the operator is <=, >= or ==, and |
14691 | // false otherwise. |
14692 | LValue Res; |
14693 | if (!EvaluatePointer(E: E->getLHS(), Result&: Res, Info) || |
14694 | !EvaluatePointer(E: E->getRHS(), Result&: Res, Info)) |
14695 | return false; |
14696 | return Success(CmpResult::Equal, E); |
14697 | } |
14698 | |
14699 | return DoAfter(); |
14700 | } |
14701 | |
14702 | bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) { |
14703 | if (!CheckLiteralType(Info, E)) |
14704 | return false; |
14705 | |
14706 | auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) { |
14707 | ComparisonCategoryResult CCR; |
14708 | switch (CR) { |
14709 | case CmpResult::Unequal: |
14710 | llvm_unreachable("should never produce Unequal for three-way comparison"); |
14711 | case CmpResult::Less: |
14712 | CCR = ComparisonCategoryResult::Less; |
14713 | break; |
14714 | case CmpResult::Equal: |
14715 | CCR = ComparisonCategoryResult::Equal; |
14716 | break; |
14717 | case CmpResult::Greater: |
14718 | CCR = ComparisonCategoryResult::Greater; |
14719 | break; |
14720 | case CmpResult::Unordered: |
14721 | CCR = ComparisonCategoryResult::Unordered; |
14722 | break; |
14723 | } |
14724 | // Evaluation succeeded. Lookup the information for the comparison category |
14725 | // type and fetch the VarDecl for the result. |
14726 | const ComparisonCategoryInfo &CmpInfo = |
14727 | Info.Ctx.CompCategories.getInfoForType(Ty: E->getType()); |
14728 | const VarDecl *VD = CmpInfo.getValueInfo(ValueKind: CmpInfo.makeWeakResult(Res: CCR))->VD; |
14729 | // Check and evaluate the result as a constant expression. |
14730 | LValue LV; |
14731 | LV.set(VD); |
14732 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
14733 | return false; |
14734 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result, |
14735 | ConstantExprKind::Normal); |
14736 | }; |
14737 | return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() { |
14738 | return ExprEvaluatorBaseTy::VisitBinCmp(E); |
14739 | }); |
14740 | } |
14741 | |
14742 | bool RecordExprEvaluator::VisitCXXParenListInitExpr( |
14743 | const CXXParenListInitExpr *E) { |
14744 | return VisitCXXParenListOrInitListExpr(E, E->getInitExprs()); |
14745 | } |
14746 | |
14747 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
14748 | // We don't support assignment in C. C++ assignments don't get here because |
14749 | // assignment is an lvalue in C++. |
14750 | if (E->isAssignmentOp()) { |
14751 | Error(E); |
14752 | if (!Info.noteFailure()) |
14753 | return false; |
14754 | } |
14755 | |
14756 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
14757 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
14758 | |
14759 | assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() || |
14760 | !E->getRHS()->getType()->isIntegralOrEnumerationType()) && |
14761 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
14762 | |
14763 | if (E->isComparisonOp()) { |
14764 | // Evaluate builtin binary comparisons by evaluating them as three-way |
14765 | // comparisons and then translating the result. |
14766 | auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) { |
14767 | assert((CR != CmpResult::Unequal || E->isEqualityOp()) && |
14768 | "should only produce Unequal for equality comparisons"); |
14769 | bool IsEqual = CR == CmpResult::Equal, |
14770 | IsLess = CR == CmpResult::Less, |
14771 | IsGreater = CR == CmpResult::Greater; |
14772 | auto Op = E->getOpcode(); |
14773 | switch (Op) { |
14774 | default: |
14775 | llvm_unreachable("unsupported binary operator"); |
14776 | case BO_EQ: |
14777 | case BO_NE: |
14778 | return Success(IsEqual == (Op == BO_EQ), E); |
14779 | case BO_LT: |
14780 | return Success(IsLess, E); |
14781 | case BO_GT: |
14782 | return Success(IsGreater, E); |
14783 | case BO_LE: |
14784 | return Success(IsEqual || IsLess, E); |
14785 | case BO_GE: |
14786 | return Success(IsEqual || IsGreater, E); |
14787 | } |
14788 | }; |
14789 | return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() { |
14790 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
14791 | }); |
14792 | } |
14793 | |
14794 | QualType LHSTy = E->getLHS()->getType(); |
14795 | QualType RHSTy = E->getRHS()->getType(); |
14796 | |
14797 | if (LHSTy->isPointerType() && RHSTy->isPointerType() && |
14798 | E->getOpcode() == BO_Sub) { |
14799 | LValue LHSValue, RHSValue; |
14800 | |
14801 | bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info); |
14802 | if (!LHSOK && !Info.noteFailure()) |
14803 | return false; |
14804 | |
14805 | if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK) |
14806 | return false; |
14807 | |
14808 | // Reject differing bases from the normal codepath; we special-case |
14809 | // comparisons to null. |
14810 | if (!HasSameBase(A: LHSValue, B: RHSValue)) { |
14811 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>(); |
14812 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>(); |
14813 | |
14814 | auto DiagArith = [&](unsigned DiagID) { |
14815 | std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType()); |
14816 | std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType()); |
14817 | Info.FFDiag(E, DiagID) << LHS << RHS; |
14818 | if (LHSExpr && LHSExpr == RHSExpr) |
14819 | Info.Note(LHSExpr->getExprLoc(), |
14820 | diag::note_constexpr_repeated_literal_eval) |
14821 | << LHSExpr->getSourceRange(); |
14822 | return false; |
14823 | }; |
14824 | |
14825 | if (!LHSExpr || !RHSExpr) |
14826 | return DiagArith(diag::note_constexpr_pointer_arith_unspecified); |
14827 | |
14828 | if (ArePotentiallyOverlappingStringLiterals(Info, LHSValue, RHSValue)) |
14829 | return DiagArith(diag::note_constexpr_literal_arith); |
14830 | |
14831 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr); |
14832 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr); |
14833 | if (!LHSAddrExpr || !RHSAddrExpr) |
14834 | return Error(E); |
14835 | // Make sure both labels come from the same function. |
14836 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
14837 | RHSAddrExpr->getLabel()->getDeclContext()) |
14838 | return Error(E); |
14839 | return Success(APValue(LHSAddrExpr, RHSAddrExpr), E); |
14840 | } |
14841 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
14842 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
14843 | |
14844 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
14845 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
14846 | |
14847 | // C++11 [expr.add]p6: |
14848 | // Unless both pointers point to elements of the same array object, or |
14849 | // one past the last element of the array object, the behavior is |
14850 | // undefined. |
14851 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
14852 | !AreElementsOfSameArray(getType(LHSValue.Base), LHSDesignator, |
14853 | RHSDesignator)) |
14854 | Info.CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
14855 | |
14856 | QualType Type = E->getLHS()->getType(); |
14857 | QualType ElementType = Type->castAs<PointerType>()->getPointeeType(); |
14858 | |
14859 | CharUnits ElementSize; |
14860 | if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElementType, Size&: ElementSize)) |
14861 | return false; |
14862 | |
14863 | // As an extension, a type may have zero size (empty struct or union in |
14864 | // C, array of zero length). Pointer subtraction in such cases has |
14865 | // undefined behavior, so is not constant. |
14866 | if (ElementSize.isZero()) { |
14867 | Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
14868 | << ElementType; |
14869 | return false; |
14870 | } |
14871 | |
14872 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
14873 | // and produce incorrect results when it overflows. Such behavior |
14874 | // appears to be non-conforming, but is common, so perhaps we should |
14875 | // assume the standard intended for such cases to be undefined behavior |
14876 | // and check for them. |
14877 | |
14878 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
14879 | // overflow in the final conversion to ptrdiff_t. |
14880 | APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
14881 | APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
14882 | APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), |
14883 | false); |
14884 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
14885 | APSInt Result = TrueResult.trunc(width: Info.Ctx.getIntWidth(T: E->getType())); |
14886 | |
14887 | if (Result.extend(width: 65) != TrueResult && |
14888 | !HandleOverflow(Info, E, TrueResult, E->getType())) |
14889 | return false; |
14890 | return Success(Result, E); |
14891 | } |
14892 | |
14893 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
14894 | } |
14895 | |
14896 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
14897 | /// a result as the expression's type. |
14898 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
14899 | const UnaryExprOrTypeTraitExpr *E) { |
14900 | switch(E->getKind()) { |
14901 | case UETT_PreferredAlignOf: |
14902 | case UETT_AlignOf: { |
14903 | if (E->isArgumentType()) |
14904 | return Success( |
14905 | GetAlignOfType(Ctx: Info.Ctx, T: E->getArgumentType(), ExprKind: E->getKind()), E); |
14906 | else |
14907 | return Success( |
14908 | GetAlignOfExpr(Ctx: Info.Ctx, E: E->getArgumentExpr(), ExprKind: E->getKind()), E); |
14909 | } |
14910 | |
14911 | case UETT_PtrAuthTypeDiscriminator: { |
14912 | if (E->getArgumentType()->isDependentType()) |
14913 | return false; |
14914 | return Success( |
14915 | Info.Ctx.getPointerAuthTypeDiscriminator(T: E->getArgumentType()), E); |
14916 | } |
14917 | case UETT_VecStep: { |
14918 | QualType Ty = E->getTypeOfArgument(); |
14919 | |
14920 | if (Ty->isVectorType()) { |
14921 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
14922 | |
14923 | // The vec_step built-in functions that take a 3-component |
14924 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
14925 | if (n == 3) |
14926 | n = 4; |
14927 | |
14928 | return Success(n, E); |
14929 | } else |
14930 | return Success(1, E); |
14931 | } |
14932 | |
14933 | case UETT_DataSizeOf: |
14934 | case UETT_SizeOf: { |
14935 | QualType SrcTy = E->getTypeOfArgument(); |
14936 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
14937 | // the result is the size of the referenced type." |
14938 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
14939 | SrcTy = Ref->getPointeeType(); |
14940 | |
14941 | CharUnits Sizeof; |
14942 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof, |
14943 | E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf |
14944 | : SizeOfType::SizeOf)) { |
14945 | return false; |
14946 | } |
14947 | return Success(Sizeof, E); |
14948 | } |
14949 | case UETT_OpenMPRequiredSimdAlign: |
14950 | assert(E->isArgumentType()); |
14951 | return Success( |
14952 | Info.Ctx.toCharUnitsFromBits( |
14953 | BitSize: Info.Ctx.getOpenMPDefaultSimdAlign(T: E->getArgumentType())) |
14954 | .getQuantity(), |
14955 | E); |
14956 | case UETT_VectorElements: { |
14957 | QualType Ty = E->getTypeOfArgument(); |
14958 | // If the vector has a fixed size, we can determine the number of elements |
14959 | // at compile time. |
14960 | if (const auto *VT = Ty->getAs<VectorType>()) |
14961 | return Success(VT->getNumElements(), E); |
14962 | |
14963 | assert(Ty->isSizelessVectorType()); |
14964 | if (Info.InConstantContext) |
14965 | Info.CCEDiag(E, diag::note_constexpr_non_const_vectorelements) |
14966 | << E->getSourceRange(); |
14967 | |
14968 | return false; |
14969 | } |
14970 | case UETT_CountOf: { |
14971 | QualType Ty = E->getTypeOfArgument(); |
14972 | assert(Ty->isArrayType()); |
14973 | |
14974 | // We don't need to worry about array element qualifiers, so getting the |
14975 | // unsafe array type is fine. |
14976 | if (const auto *CAT = |
14977 | dyn_cast<ConstantArrayType>(Ty->getAsArrayTypeUnsafe())) { |
14978 | return Success(CAT->getSize(), E); |
14979 | } |
14980 | |
14981 | assert(!Ty->isConstantSizeType()); |
14982 | |
14983 | // If it's a variable-length array type, we need to check whether it is a |
14984 | // multidimensional array. If so, we need to check the size expression of |
14985 | // the VLA to see if it's a constant size. If so, we can return that value. |
14986 | const auto *VAT = Info.Ctx.getAsVariableArrayType(T: Ty); |
14987 | assert(VAT); |
14988 | if (VAT->getElementType()->isArrayType()) { |
14989 | std::optional<APSInt> Res = |
14990 | VAT->getSizeExpr()->getIntegerConstantExpr(Ctx: Info.Ctx); |
14991 | if (Res) { |
14992 | // The resulting value always has type size_t, so we need to make the |
14993 | // returned APInt have the correct sign and bit-width. |
14994 | APInt Val{ |
14995 | static_cast<unsigned>(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType())), |
14996 | Res->getZExtValue()}; |
14997 | return Success(Val, E); |
14998 | } |
14999 | } |
15000 | |
15001 | // Definitely a variable-length type, which is not an ICE. |
15002 | // FIXME: Better diagnostic. |
15003 | Info.FFDiag(Loc: E->getBeginLoc()); |
15004 | return false; |
15005 | } |
15006 | } |
15007 | |
15008 | llvm_unreachable("unknown expr/type trait"); |
15009 | } |
15010 | |
15011 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
15012 | CharUnits Result; |
15013 | unsigned n = OOE->getNumComponents(); |
15014 | if (n == 0) |
15015 | return Error(OOE); |
15016 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
15017 | for (unsigned i = 0; i != n; ++i) { |
15018 | OffsetOfNode ON = OOE->getComponent(Idx: i); |
15019 | switch (ON.getKind()) { |
15020 | case OffsetOfNode::Array: { |
15021 | const Expr *Idx = OOE->getIndexExpr(Idx: ON.getArrayExprIndex()); |
15022 | APSInt IdxResult; |
15023 | if (!EvaluateInteger(E: Idx, Result&: IdxResult, Info)) |
15024 | return false; |
15025 | const ArrayType *AT = Info.Ctx.getAsArrayType(T: CurrentType); |
15026 | if (!AT) |
15027 | return Error(OOE); |
15028 | CurrentType = AT->getElementType(); |
15029 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(T: CurrentType); |
15030 | Result += IdxResult.getSExtValue() * ElementSize; |
15031 | break; |
15032 | } |
15033 | |
15034 | case OffsetOfNode::Field: { |
15035 | FieldDecl *MemberDecl = ON.getField(); |
15036 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
15037 | if (!RT) |
15038 | return Error(OOE); |
15039 | RecordDecl *RD = RT->getDecl(); |
15040 | if (RD->isInvalidDecl()) return false; |
15041 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD); |
15042 | unsigned i = MemberDecl->getFieldIndex(); |
15043 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
15044 | Result += Info.Ctx.toCharUnitsFromBits(BitSize: RL.getFieldOffset(FieldNo: i)); |
15045 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
15046 | break; |
15047 | } |
15048 | |
15049 | case OffsetOfNode::Identifier: |
15050 | llvm_unreachable("dependent __builtin_offsetof"); |
15051 | |
15052 | case OffsetOfNode::Base: { |
15053 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
15054 | if (BaseSpec->isVirtual()) |
15055 | return Error(OOE); |
15056 | |
15057 | // Find the layout of the class whose base we are looking into. |
15058 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
15059 | if (!RT) |
15060 | return Error(OOE); |
15061 | RecordDecl *RD = RT->getDecl(); |
15062 | if (RD->isInvalidDecl()) return false; |
15063 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD); |
15064 | |
15065 | // Find the base class itself. |
15066 | CurrentType = BaseSpec->getType(); |
15067 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
15068 | if (!BaseRT) |
15069 | return Error(OOE); |
15070 | |
15071 | // Add the offset to the base. |
15072 | Result += RL.getBaseClassOffset(Base: cast<CXXRecordDecl>(Val: BaseRT->getDecl())); |
15073 | break; |
15074 | } |
15075 | } |
15076 | } |
15077 | return Success(Result, OOE); |
15078 | } |
15079 | |
15080 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
15081 | switch (E->getOpcode()) { |
15082 | default: |
15083 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
15084 | // See C99 6.6p3. |
15085 | return Error(E); |
15086 | case UO_Extension: |
15087 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
15088 | // If so, we could clear the diagnostic ID. |
15089 | return Visit(E->getSubExpr()); |
15090 | case UO_Plus: |
15091 | // The result is just the value. |
15092 | return Visit(E->getSubExpr()); |
15093 | case UO_Minus: { |
15094 | if (!Visit(E->getSubExpr())) |
15095 | return false; |
15096 | if (!Result.isInt()) return Error(E); |
15097 | const APSInt &Value = Result.getInt(); |
15098 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow()) { |
15099 | if (Info.checkingForUndefinedBehavior()) |
15100 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
15101 | diag::warn_integer_constant_overflow) |
15102 | << toString(Value, 10, Value.isSigned(), /*formatAsCLiteral=*/false, |
15103 | /*UpperCase=*/true, /*InsertSeparators=*/true) |
15104 | << E->getType() << E->getSourceRange(); |
15105 | |
15106 | if (!HandleOverflow(Info, E, -Value.extend(width: Value.getBitWidth() + 1), |
15107 | E->getType())) |
15108 | return false; |
15109 | } |
15110 | return Success(-Value, E); |
15111 | } |
15112 | case UO_Not: { |
15113 | if (!Visit(E->getSubExpr())) |
15114 | return false; |
15115 | if (!Result.isInt()) return Error(E); |
15116 | return Success(~Result.getInt(), E); |
15117 | } |
15118 | case UO_LNot: { |
15119 | bool bres; |
15120 | if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info)) |
15121 | return false; |
15122 | return Success(!bres, E); |
15123 | } |
15124 | } |
15125 | } |
15126 | |
15127 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
15128 | /// result type is integer. |
15129 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
15130 | const Expr *SubExpr = E->getSubExpr(); |
15131 | QualType DestType = E->getType(); |
15132 | QualType SrcType = SubExpr->getType(); |
15133 | |
15134 | switch (E->getCastKind()) { |
15135 | case CK_BaseToDerived: |
15136 | case CK_DerivedToBase: |
15137 | case CK_UncheckedDerivedToBase: |
15138 | case CK_Dynamic: |
15139 | case CK_ToUnion: |
15140 | case CK_ArrayToPointerDecay: |
15141 | case CK_FunctionToPointerDecay: |
15142 | case CK_NullToPointer: |
15143 | case CK_NullToMemberPointer: |
15144 | case CK_BaseToDerivedMemberPointer: |
15145 | case CK_DerivedToBaseMemberPointer: |
15146 | case CK_ReinterpretMemberPointer: |
15147 | case CK_ConstructorConversion: |
15148 | case CK_IntegralToPointer: |
15149 | case CK_ToVoid: |
15150 | case CK_VectorSplat: |
15151 | case CK_IntegralToFloating: |
15152 | case CK_FloatingCast: |
15153 | case CK_CPointerToObjCPointerCast: |
15154 | case CK_BlockPointerToObjCPointerCast: |
15155 | case CK_AnyPointerToBlockPointerCast: |
15156 | case CK_ObjCObjectLValueCast: |
15157 | case CK_FloatingRealToComplex: |
15158 | case CK_FloatingComplexToReal: |
15159 | case CK_FloatingComplexCast: |
15160 | case CK_FloatingComplexToIntegralComplex: |
15161 | case CK_IntegralRealToComplex: |
15162 | case CK_IntegralComplexCast: |
15163 | case CK_IntegralComplexToFloatingComplex: |
15164 | case CK_BuiltinFnToFnPtr: |
15165 | case CK_ZeroToOCLOpaqueType: |
15166 | case CK_NonAtomicToAtomic: |
15167 | case CK_AddressSpaceConversion: |
15168 | case CK_IntToOCLSampler: |
15169 | case CK_FloatingToFixedPoint: |
15170 | case CK_FixedPointToFloating: |
15171 | case CK_FixedPointCast: |
15172 | case CK_IntegralToFixedPoint: |
15173 | case CK_MatrixCast: |
15174 | case CK_HLSLAggregateSplatCast: |
15175 | llvm_unreachable("invalid cast kind for integral value"); |
15176 | |
15177 | case CK_BitCast: |
15178 | case CK_Dependent: |
15179 | case CK_LValueBitCast: |
15180 | case CK_ARCProduceObject: |
15181 | case CK_ARCConsumeObject: |
15182 | case CK_ARCReclaimReturnedObject: |
15183 | case CK_ARCExtendBlockObject: |
15184 | case CK_CopyAndAutoreleaseBlockObject: |
15185 | return Error(E); |
15186 | |
15187 | case CK_UserDefinedConversion: |
15188 | case CK_LValueToRValue: |
15189 | case CK_AtomicToNonAtomic: |
15190 | case CK_NoOp: |
15191 | case CK_LValueToRValueBitCast: |
15192 | case CK_HLSLArrayRValue: |
15193 | case CK_HLSLElementwiseCast: |
15194 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
15195 | |
15196 | case CK_MemberPointerToBoolean: |
15197 | case CK_PointerToBoolean: |
15198 | case CK_IntegralToBoolean: |
15199 | case CK_FloatingToBoolean: |
15200 | case CK_BooleanToSignedIntegral: |
15201 | case CK_FloatingComplexToBoolean: |
15202 | case CK_IntegralComplexToBoolean: { |
15203 | bool BoolResult; |
15204 | if (!EvaluateAsBooleanCondition(E: SubExpr, Result&: BoolResult, Info)) |
15205 | return false; |
15206 | uint64_t IntResult = BoolResult; |
15207 | if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral) |
15208 | IntResult = (uint64_t)-1; |
15209 | return Success(IntResult, E); |
15210 | } |
15211 | |
15212 | case CK_FixedPointToIntegral: { |
15213 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SrcType)); |
15214 | if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info)) |
15215 | return false; |
15216 | bool Overflowed; |
15217 | llvm::APSInt Result = Src.convertToInt( |
15218 | DstWidth: Info.Ctx.getIntWidth(T: DestType), |
15219 | DstSign: DestType->isSignedIntegerOrEnumerationType(), Overflow: &Overflowed); |
15220 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
15221 | return false; |
15222 | return Success(Result, E); |
15223 | } |
15224 | |
15225 | case CK_FixedPointToBoolean: { |
15226 | // Unsigned padding does not affect this. |
15227 | APValue Val; |
15228 | if (!Evaluate(Result&: Val, Info, E: SubExpr)) |
15229 | return false; |
15230 | return Success(Val.getFixedPoint().getBoolValue(), E); |
15231 | } |
15232 | |
15233 | case CK_IntegralCast: { |
15234 | if (!Visit(SubExpr)) |
15235 | return false; |
15236 | |
15237 | if (!Result.isInt()) { |
15238 | // Allow casts of address-of-label differences if they are no-ops |
15239 | // or narrowing. (The narrowing case isn't actually guaranteed to |
15240 | // be constant-evaluatable except in some narrow cases which are hard |
15241 | // to detect here. We let it through on the assumption the user knows |
15242 | // what they are doing.) |
15243 | if (Result.isAddrLabelDiff()) |
15244 | return Info.Ctx.getTypeSize(T: DestType) <= Info.Ctx.getTypeSize(T: SrcType); |
15245 | // Only allow casts of lvalues if they are lossless. |
15246 | return Info.Ctx.getTypeSize(T: DestType) == Info.Ctx.getTypeSize(T: SrcType); |
15247 | } |
15248 | |
15249 | if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) { |
15250 | const EnumType *ET = dyn_cast<EnumType>(Val: DestType.getCanonicalType()); |
15251 | const EnumDecl *ED = ET->getDecl(); |
15252 | // Check that the value is within the range of the enumeration values. |
15253 | // |
15254 | // This corressponds to [expr.static.cast]p10 which says: |
15255 | // A value of integral or enumeration type can be explicitly converted |
15256 | // to a complete enumeration type ... If the enumeration type does not |
15257 | // have a fixed underlying type, the value is unchanged if the original |
15258 | // value is within the range of the enumeration values ([dcl.enum]), and |
15259 | // otherwise, the behavior is undefined. |
15260 | // |
15261 | // This was resolved as part of DR2338 which has CD5 status. |
15262 | if (!ED->isFixed()) { |
15263 | llvm::APInt Min; |
15264 | llvm::APInt Max; |
15265 | |
15266 | ED->getValueRange(Max, Min); |
15267 | --Max; |
15268 | |
15269 | if (ED->getNumNegativeBits() && |
15270 | (Max.slt(Result.getInt().getSExtValue()) || |
15271 | Min.sgt(Result.getInt().getSExtValue()))) |
15272 | Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range) |
15273 | << llvm::toString(Result.getInt(), 10) << Min.getSExtValue() |
15274 | << Max.getSExtValue() << ED; |
15275 | else if (!ED->getNumNegativeBits() && |
15276 | Max.ult(Result.getInt().getZExtValue())) |
15277 | Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range) |
15278 | << llvm::toString(Result.getInt(), 10) << Min.getZExtValue() |
15279 | << Max.getZExtValue() << ED; |
15280 | } |
15281 | } |
15282 | |
15283 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
15284 | Result.getInt()), E); |
15285 | } |
15286 | |
15287 | case CK_PointerToIntegral: { |
15288 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
15289 | << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret |
15290 | << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange(); |
15291 | |
15292 | LValue LV; |
15293 | if (!EvaluatePointer(E: SubExpr, Result&: LV, Info)) |
15294 | return false; |
15295 | |
15296 | if (LV.getLValueBase()) { |
15297 | // Only allow based lvalue casts if they are lossless. |
15298 | // FIXME: Allow a larger integer size than the pointer size, and allow |
15299 | // narrowing back down to pointer width in subsequent integral casts. |
15300 | // FIXME: Check integer type's active bits, not its type size. |
15301 | if (Info.Ctx.getTypeSize(T: DestType) != Info.Ctx.getTypeSize(T: SrcType)) |
15302 | return Error(E); |
15303 | |
15304 | LV.Designator.setInvalid(); |
15305 | LV.moveInto(V&: Result); |
15306 | return true; |
15307 | } |
15308 | |
15309 | APSInt AsInt; |
15310 | APValue V; |
15311 | LV.moveInto(V); |
15312 | if (!V.toIntegralConstant(Result&: AsInt, SrcTy: SrcType, Ctx: Info.Ctx)) |
15313 | llvm_unreachable("Can't cast this!"); |
15314 | |
15315 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
15316 | } |
15317 | |
15318 | case CK_IntegralComplexToReal: { |
15319 | ComplexValue C; |
15320 | if (!EvaluateComplex(E: SubExpr, Res&: C, Info)) |
15321 | return false; |
15322 | return Success(C.getComplexIntReal(), E); |
15323 | } |
15324 | |
15325 | case CK_FloatingToIntegral: { |
15326 | APFloat F(0.0); |
15327 | if (!EvaluateFloat(E: SubExpr, Result&: F, Info)) |
15328 | return false; |
15329 | |
15330 | APSInt Value; |
15331 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
15332 | return false; |
15333 | return Success(Value, E); |
15334 | } |
15335 | case CK_HLSLVectorTruncation: { |
15336 | APValue Val; |
15337 | if (!EvaluateVector(E: SubExpr, Result&: Val, Info)) |
15338 | return Error(E); |
15339 | return Success(Val.getVectorElt(I: 0), E); |
15340 | } |
15341 | } |
15342 | |
15343 | llvm_unreachable("unknown cast resulting in integral value"); |
15344 | } |
15345 | |
15346 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
15347 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
15348 | ComplexValue LV; |
15349 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info)) |
15350 | return false; |
15351 | if (!LV.isComplexInt()) |
15352 | return Error(E); |
15353 | return Success(LV.getComplexIntReal(), E); |
15354 | } |
15355 | |
15356 | return Visit(E->getSubExpr()); |
15357 | } |
15358 | |
15359 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
15360 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
15361 | ComplexValue LV; |
15362 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info)) |
15363 | return false; |
15364 | if (!LV.isComplexInt()) |
15365 | return Error(E); |
15366 | return Success(LV.getComplexIntImag(), E); |
15367 | } |
15368 | |
15369 | VisitIgnoredValue(E: E->getSubExpr()); |
15370 | return Success(0, E); |
15371 | } |
15372 | |
15373 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
15374 | return Success(E->getPackLength(), E); |
15375 | } |
15376 | |
15377 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
15378 | return Success(E->getValue(), E); |
15379 | } |
15380 | |
15381 | bool IntExprEvaluator::VisitConceptSpecializationExpr( |
15382 | const ConceptSpecializationExpr *E) { |
15383 | return Success(E->isSatisfied(), E); |
15384 | } |
15385 | |
15386 | bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) { |
15387 | return Success(E->isSatisfied(), E); |
15388 | } |
15389 | |
15390 | bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
15391 | switch (E->getOpcode()) { |
15392 | default: |
15393 | // Invalid unary operators |
15394 | return Error(E); |
15395 | case UO_Plus: |
15396 | // The result is just the value. |
15397 | return Visit(E->getSubExpr()); |
15398 | case UO_Minus: { |
15399 | if (!Visit(E->getSubExpr())) return false; |
15400 | if (!Result.isFixedPoint()) |
15401 | return Error(E); |
15402 | bool Overflowed; |
15403 | APFixedPoint Negated = Result.getFixedPoint().negate(Overflow: &Overflowed); |
15404 | if (Overflowed && !HandleOverflow(Info, E, Negated, E->getType())) |
15405 | return false; |
15406 | return Success(Negated, E); |
15407 | } |
15408 | case UO_LNot: { |
15409 | bool bres; |
15410 | if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info)) |
15411 | return false; |
15412 | return Success(!bres, E); |
15413 | } |
15414 | } |
15415 | } |
15416 | |
15417 | bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) { |
15418 | const Expr *SubExpr = E->getSubExpr(); |
15419 | QualType DestType = E->getType(); |
15420 | assert(DestType->isFixedPointType() && |
15421 | "Expected destination type to be a fixed point type"); |
15422 | auto DestFXSema = Info.Ctx.getFixedPointSemantics(Ty: DestType); |
15423 | |
15424 | switch (E->getCastKind()) { |
15425 | case CK_FixedPointCast: { |
15426 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType())); |
15427 | if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info)) |
15428 | return false; |
15429 | bool Overflowed; |
15430 | APFixedPoint Result = Src.convert(DstSema: DestFXSema, Overflow: &Overflowed); |
15431 | if (Overflowed) { |
15432 | if (Info.checkingForUndefinedBehavior()) |
15433 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
15434 | diag::warn_fixedpoint_constant_overflow) |
15435 | << Result.toString() << E->getType(); |
15436 | if (!HandleOverflow(Info, E, Result, E->getType())) |
15437 | return false; |
15438 | } |
15439 | return Success(Result, E); |
15440 | } |
15441 | case CK_IntegralToFixedPoint: { |
15442 | APSInt Src; |
15443 | if (!EvaluateInteger(E: SubExpr, Result&: Src, Info)) |
15444 | return false; |
15445 | |
15446 | bool Overflowed; |
15447 | APFixedPoint IntResult = APFixedPoint::getFromIntValue( |
15448 | Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed); |
15449 | |
15450 | if (Overflowed) { |
15451 | if (Info.checkingForUndefinedBehavior()) |
15452 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
15453 | diag::warn_fixedpoint_constant_overflow) |
15454 | << IntResult.toString() << E->getType(); |
15455 | if (!HandleOverflow(Info, E, IntResult, E->getType())) |
15456 | return false; |
15457 | } |
15458 | |
15459 | return Success(IntResult, E); |
15460 | } |
15461 | case CK_FloatingToFixedPoint: { |
15462 | APFloat Src(0.0); |
15463 | if (!EvaluateFloat(E: SubExpr, Result&: Src, Info)) |
15464 | return false; |
15465 | |
15466 | bool Overflowed; |
15467 | APFixedPoint Result = APFixedPoint::getFromFloatValue( |
15468 | Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed); |
15469 | |
15470 | if (Overflowed) { |
15471 | if (Info.checkingForUndefinedBehavior()) |
15472 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
15473 | diag::warn_fixedpoint_constant_overflow) |
15474 | << Result.toString() << E->getType(); |
15475 | if (!HandleOverflow(Info, E, Result, E->getType())) |
15476 | return false; |
15477 | } |
15478 | |
15479 | return Success(Result, E); |
15480 | } |
15481 | case CK_NoOp: |
15482 | case CK_LValueToRValue: |
15483 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
15484 | default: |
15485 | return Error(E); |
15486 | } |
15487 | } |
15488 | |
15489 | bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
15490 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
15491 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
15492 | |
15493 | const Expr *LHS = E->getLHS(); |
15494 | const Expr *RHS = E->getRHS(); |
15495 | FixedPointSemantics ResultFXSema = |
15496 | Info.Ctx.getFixedPointSemantics(Ty: E->getType()); |
15497 | |
15498 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHS->getType())); |
15499 | if (!EvaluateFixedPointOrInteger(E: LHS, Result&: LHSFX, Info)) |
15500 | return false; |
15501 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHS->getType())); |
15502 | if (!EvaluateFixedPointOrInteger(E: RHS, Result&: RHSFX, Info)) |
15503 | return false; |
15504 | |
15505 | bool OpOverflow = false, ConversionOverflow = false; |
15506 | APFixedPoint Result(LHSFX.getSemantics()); |
15507 | switch (E->getOpcode()) { |
15508 | case BO_Add: { |
15509 | Result = LHSFX.add(Other: RHSFX, Overflow: &OpOverflow) |
15510 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
15511 | break; |
15512 | } |
15513 | case BO_Sub: { |
15514 | Result = LHSFX.sub(Other: RHSFX, Overflow: &OpOverflow) |
15515 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
15516 | break; |
15517 | } |
15518 | case BO_Mul: { |
15519 | Result = LHSFX.mul(Other: RHSFX, Overflow: &OpOverflow) |
15520 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
15521 | break; |
15522 | } |
15523 | case BO_Div: { |
15524 | if (RHSFX.getValue() == 0) { |
15525 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
15526 | return false; |
15527 | } |
15528 | Result = LHSFX.div(Other: RHSFX, Overflow: &OpOverflow) |
15529 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
15530 | break; |
15531 | } |
15532 | case BO_Shl: |
15533 | case BO_Shr: { |
15534 | FixedPointSemantics LHSSema = LHSFX.getSemantics(); |
15535 | llvm::APSInt RHSVal = RHSFX.getValue(); |
15536 | |
15537 | unsigned ShiftBW = |
15538 | LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding(); |
15539 | unsigned Amt = RHSVal.getLimitedValue(Limit: ShiftBW - 1); |
15540 | // Embedded-C 4.1.6.2.2: |
15541 | // The right operand must be nonnegative and less than the total number |
15542 | // of (nonpadding) bits of the fixed-point operand ... |
15543 | if (RHSVal.isNegative()) |
15544 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHSVal; |
15545 | else if (Amt != RHSVal) |
15546 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
15547 | << RHSVal << E->getType() << ShiftBW; |
15548 | |
15549 | if (E->getOpcode() == BO_Shl) |
15550 | Result = LHSFX.shl(Amt, Overflow: &OpOverflow); |
15551 | else |
15552 | Result = LHSFX.shr(Amt, Overflow: &OpOverflow); |
15553 | break; |
15554 | } |
15555 | default: |
15556 | return false; |
15557 | } |
15558 | if (OpOverflow || ConversionOverflow) { |
15559 | if (Info.checkingForUndefinedBehavior()) |
15560 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
15561 | diag::warn_fixedpoint_constant_overflow) |
15562 | << Result.toString() << E->getType(); |
15563 | if (!HandleOverflow(Info, E, Result, E->getType())) |
15564 | return false; |
15565 | } |
15566 | return Success(Result, E); |
15567 | } |
15568 | |
15569 | //===----------------------------------------------------------------------===// |
15570 | // Float Evaluation |
15571 | //===----------------------------------------------------------------------===// |
15572 | |
15573 | namespace { |
15574 | class FloatExprEvaluator |
15575 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
15576 | APFloat &Result; |
15577 | public: |
15578 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
15579 | : ExprEvaluatorBaseTy(info), Result(result) {} |
15580 | |
15581 | bool Success(const APValue &V, const Expr *e) { |
15582 | Result = V.getFloat(); |
15583 | return true; |
15584 | } |
15585 | |
15586 | bool ZeroInitialization(const Expr *E) { |
15587 | Result = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: E->getType())); |
15588 | return true; |
15589 | } |
15590 | |
15591 | bool VisitCallExpr(const CallExpr *E); |
15592 | |
15593 | bool VisitUnaryOperator(const UnaryOperator *E); |
15594 | bool VisitBinaryOperator(const BinaryOperator *E); |
15595 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
15596 | bool VisitCastExpr(const CastExpr *E); |
15597 | |
15598 | bool VisitUnaryReal(const UnaryOperator *E); |
15599 | bool VisitUnaryImag(const UnaryOperator *E); |
15600 | |
15601 | // FIXME: Missing: array subscript of vector, member of vector |
15602 | }; |
15603 | } // end anonymous namespace |
15604 | |
15605 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
15606 | assert(!E->isValueDependent()); |
15607 | assert(E->isPRValue() && E->getType()->isRealFloatingType()); |
15608 | return FloatExprEvaluator(Info, Result).Visit(E); |
15609 | } |
15610 | |
15611 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
15612 | QualType ResultTy, |
15613 | const Expr *Arg, |
15614 | bool SNaN, |
15615 | llvm::APFloat &Result) { |
15616 | const StringLiteral *S = dyn_cast<StringLiteral>(Val: Arg->IgnoreParenCasts()); |
15617 | if (!S) return false; |
15618 | |
15619 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(T: ResultTy); |
15620 | |
15621 | llvm::APInt fill; |
15622 | |
15623 | // Treat empty strings as if they were zero. |
15624 | if (S->getString().empty()) |
15625 | fill = llvm::APInt(32, 0); |
15626 | else if (S->getString().getAsInteger(Radix: 0, Result&: fill)) |
15627 | return false; |
15628 | |
15629 | if (Context.getTargetInfo().isNan2008()) { |
15630 | if (SNaN) |
15631 | Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill); |
15632 | else |
15633 | Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill); |
15634 | } else { |
15635 | // Prior to IEEE 754-2008, architectures were allowed to choose whether |
15636 | // the first bit of their significand was set for qNaN or sNaN. MIPS chose |
15637 | // a different encoding to what became a standard in 2008, and for pre- |
15638 | // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as |
15639 | // sNaN. This is now known as "legacy NaN" encoding. |
15640 | if (SNaN) |
15641 | Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill); |
15642 | else |
15643 | Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill); |
15644 | } |
15645 | |
15646 | return true; |
15647 | } |
15648 | |
15649 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
15650 | if (!IsConstantEvaluatedBuiltinCall(E)) |
15651 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
15652 | |
15653 | switch (E->getBuiltinCallee()) { |
15654 | default: |
15655 | return false; |
15656 | |
15657 | case Builtin::BI__builtin_huge_val: |
15658 | case Builtin::BI__builtin_huge_valf: |
15659 | case Builtin::BI__builtin_huge_vall: |
15660 | case Builtin::BI__builtin_huge_valf16: |
15661 | case Builtin::BI__builtin_huge_valf128: |
15662 | case Builtin::BI__builtin_inf: |
15663 | case Builtin::BI__builtin_inff: |
15664 | case Builtin::BI__builtin_infl: |
15665 | case Builtin::BI__builtin_inff16: |
15666 | case Builtin::BI__builtin_inff128: { |
15667 | const llvm::fltSemantics &Sem = |
15668 | Info.Ctx.getFloatTypeSemantics(T: E->getType()); |
15669 | Result = llvm::APFloat::getInf(Sem); |
15670 | return true; |
15671 | } |
15672 | |
15673 | case Builtin::BI__builtin_nans: |
15674 | case Builtin::BI__builtin_nansf: |
15675 | case Builtin::BI__builtin_nansl: |
15676 | case Builtin::BI__builtin_nansf16: |
15677 | case Builtin::BI__builtin_nansf128: |
15678 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(Arg: 0), |
15679 | true, Result)) |
15680 | return Error(E); |
15681 | return true; |
15682 | |
15683 | case Builtin::BI__builtin_nan: |
15684 | case Builtin::BI__builtin_nanf: |
15685 | case Builtin::BI__builtin_nanl: |
15686 | case Builtin::BI__builtin_nanf16: |
15687 | case Builtin::BI__builtin_nanf128: |
15688 | // If this is __builtin_nan() turn this into a nan, otherwise we |
15689 | // can't constant fold it. |
15690 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(Arg: 0), |
15691 | false, Result)) |
15692 | return Error(E); |
15693 | return true; |
15694 | |
15695 | case Builtin::BI__builtin_fabs: |
15696 | case Builtin::BI__builtin_fabsf: |
15697 | case Builtin::BI__builtin_fabsl: |
15698 | case Builtin::BI__builtin_fabsf128: |
15699 | // The C standard says "fabs raises no floating-point exceptions, |
15700 | // even if x is a signaling NaN. The returned value is independent of |
15701 | // the current rounding direction mode." Therefore constant folding can |
15702 | // proceed without regard to the floating point settings. |
15703 | // Reference, WG14 N2478 F.10.4.3 |
15704 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info)) |
15705 | return false; |
15706 | |
15707 | if (Result.isNegative()) |
15708 | Result.changeSign(); |
15709 | return true; |
15710 | |
15711 | case Builtin::BI__arithmetic_fence: |
15712 | return EvaluateFloat(E: E->getArg(Arg: 0), Result, Info); |
15713 | |
15714 | // FIXME: Builtin::BI__builtin_powi |
15715 | // FIXME: Builtin::BI__builtin_powif |
15716 | // FIXME: Builtin::BI__builtin_powil |
15717 | |
15718 | case Builtin::BI__builtin_copysign: |
15719 | case Builtin::BI__builtin_copysignf: |
15720 | case Builtin::BI__builtin_copysignl: |
15721 | case Builtin::BI__builtin_copysignf128: { |
15722 | APFloat RHS(0.); |
15723 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
15724 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
15725 | return false; |
15726 | Result.copySign(RHS); |
15727 | return true; |
15728 | } |
15729 | |
15730 | case Builtin::BI__builtin_fmax: |
15731 | case Builtin::BI__builtin_fmaxf: |
15732 | case Builtin::BI__builtin_fmaxl: |
15733 | case Builtin::BI__builtin_fmaxf16: |
15734 | case Builtin::BI__builtin_fmaxf128: { |
15735 | APFloat RHS(0.); |
15736 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
15737 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
15738 | return false; |
15739 | Result = maxnum(A: Result, B: RHS); |
15740 | return true; |
15741 | } |
15742 | |
15743 | case Builtin::BI__builtin_fmin: |
15744 | case Builtin::BI__builtin_fminf: |
15745 | case Builtin::BI__builtin_fminl: |
15746 | case Builtin::BI__builtin_fminf16: |
15747 | case Builtin::BI__builtin_fminf128: { |
15748 | APFloat RHS(0.); |
15749 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
15750 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
15751 | return false; |
15752 | Result = minnum(A: Result, B: RHS); |
15753 | return true; |
15754 | } |
15755 | |
15756 | case Builtin::BI__builtin_fmaximum_num: |
15757 | case Builtin::BI__builtin_fmaximum_numf: |
15758 | case Builtin::BI__builtin_fmaximum_numl: |
15759 | case Builtin::BI__builtin_fmaximum_numf16: |
15760 | case Builtin::BI__builtin_fmaximum_numf128: { |
15761 | APFloat RHS(0.); |
15762 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
15763 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
15764 | return false; |
15765 | Result = maximumnum(A: Result, B: RHS); |
15766 | return true; |
15767 | } |
15768 | |
15769 | case Builtin::BI__builtin_fminimum_num: |
15770 | case Builtin::BI__builtin_fminimum_numf: |
15771 | case Builtin::BI__builtin_fminimum_numl: |
15772 | case Builtin::BI__builtin_fminimum_numf16: |
15773 | case Builtin::BI__builtin_fminimum_numf128: { |
15774 | APFloat RHS(0.); |
15775 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
15776 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
15777 | return false; |
15778 | Result = minimumnum(A: Result, B: RHS); |
15779 | return true; |
15780 | } |
15781 | } |
15782 | } |
15783 | |
15784 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
15785 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
15786 | ComplexValue CV; |
15787 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info)) |
15788 | return false; |
15789 | Result = CV.FloatReal; |
15790 | return true; |
15791 | } |
15792 | |
15793 | return Visit(E->getSubExpr()); |
15794 | } |
15795 | |
15796 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
15797 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
15798 | ComplexValue CV; |
15799 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info)) |
15800 | return false; |
15801 | Result = CV.FloatImag; |
15802 | return true; |
15803 | } |
15804 | |
15805 | VisitIgnoredValue(E: E->getSubExpr()); |
15806 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(T: E->getType()); |
15807 | Result = llvm::APFloat::getZero(Sem); |
15808 | return true; |
15809 | } |
15810 | |
15811 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
15812 | switch (E->getOpcode()) { |
15813 | default: return Error(E); |
15814 | case UO_Plus: |
15815 | return EvaluateFloat(E: E->getSubExpr(), Result, Info); |
15816 | case UO_Minus: |
15817 | // In C standard, WG14 N2478 F.3 p4 |
15818 | // "the unary - raises no floating point exceptions, |
15819 | // even if the operand is signalling." |
15820 | if (!EvaluateFloat(E: E->getSubExpr(), Result, Info)) |
15821 | return false; |
15822 | Result.changeSign(); |
15823 | return true; |
15824 | } |
15825 | } |
15826 | |
15827 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
15828 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
15829 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
15830 | |
15831 | APFloat RHS(0.0); |
15832 | bool LHSOK = EvaluateFloat(E: E->getLHS(), Result, Info); |
15833 | if (!LHSOK && !Info.noteFailure()) |
15834 | return false; |
15835 | return EvaluateFloat(E: E->getRHS(), Result&: RHS, Info) && LHSOK && |
15836 | handleFloatFloatBinOp(Info, E, LHS&: Result, Opcode: E->getOpcode(), RHS); |
15837 | } |
15838 | |
15839 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
15840 | Result = E->getValue(); |
15841 | return true; |
15842 | } |
15843 | |
15844 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
15845 | const Expr* SubExpr = E->getSubExpr(); |
15846 | |
15847 | switch (E->getCastKind()) { |
15848 | default: |
15849 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
15850 | |
15851 | case CK_IntegralToFloating: { |
15852 | APSInt IntResult; |
15853 | const FPOptions FPO = E->getFPFeaturesInEffect( |
15854 | LO: Info.Ctx.getLangOpts()); |
15855 | return EvaluateInteger(E: SubExpr, Result&: IntResult, Info) && |
15856 | HandleIntToFloatCast(Info, E, FPO, SubExpr->getType(), |
15857 | IntResult, E->getType(), Result); |
15858 | } |
15859 | |
15860 | case CK_FixedPointToFloating: { |
15861 | APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType())); |
15862 | if (!EvaluateFixedPoint(E: SubExpr, Result&: FixResult, Info)) |
15863 | return false; |
15864 | Result = |
15865 | FixResult.convertToFloat(FloatSema: Info.Ctx.getFloatTypeSemantics(T: E->getType())); |
15866 | return true; |
15867 | } |
15868 | |
15869 | case CK_FloatingCast: { |
15870 | if (!Visit(SubExpr)) |
15871 | return false; |
15872 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
15873 | Result); |
15874 | } |
15875 | |
15876 | case CK_FloatingComplexToReal: { |
15877 | ComplexValue V; |
15878 | if (!EvaluateComplex(E: SubExpr, Res&: V, Info)) |
15879 | return false; |
15880 | Result = V.getComplexFloatReal(); |
15881 | return true; |
15882 | } |
15883 | case CK_HLSLVectorTruncation: { |
15884 | APValue Val; |
15885 | if (!EvaluateVector(E: SubExpr, Result&: Val, Info)) |
15886 | return Error(E); |
15887 | return Success(Val.getVectorElt(I: 0), E); |
15888 | } |
15889 | } |
15890 | } |
15891 | |
15892 | //===----------------------------------------------------------------------===// |
15893 | // Complex Evaluation (for float and integer) |
15894 | //===----------------------------------------------------------------------===// |
15895 | |
15896 | namespace { |
15897 | class ComplexExprEvaluator |
15898 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
15899 | ComplexValue &Result; |
15900 | |
15901 | public: |
15902 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
15903 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
15904 | |
15905 | bool Success(const APValue &V, const Expr *e) { |
15906 | Result.setFrom(V); |
15907 | return true; |
15908 | } |
15909 | |
15910 | bool ZeroInitialization(const Expr *E); |
15911 | |
15912 | //===--------------------------------------------------------------------===// |
15913 | // Visitor Methods |
15914 | //===--------------------------------------------------------------------===// |
15915 | |
15916 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
15917 | bool VisitCastExpr(const CastExpr *E); |
15918 | bool VisitBinaryOperator(const BinaryOperator *E); |
15919 | bool VisitUnaryOperator(const UnaryOperator *E); |
15920 | bool VisitInitListExpr(const InitListExpr *E); |
15921 | bool VisitCallExpr(const CallExpr *E); |
15922 | }; |
15923 | } // end anonymous namespace |
15924 | |
15925 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
15926 | EvalInfo &Info) { |
15927 | assert(!E->isValueDependent()); |
15928 | assert(E->isPRValue() && E->getType()->isAnyComplexType()); |
15929 | return ComplexExprEvaluator(Info, Result).Visit(E); |
15930 | } |
15931 | |
15932 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
15933 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
15934 | if (ElemTy->isRealFloatingType()) { |
15935 | Result.makeComplexFloat(); |
15936 | APFloat Zero = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy)); |
15937 | Result.FloatReal = Zero; |
15938 | Result.FloatImag = Zero; |
15939 | } else { |
15940 | Result.makeComplexInt(); |
15941 | APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy); |
15942 | Result.IntReal = Zero; |
15943 | Result.IntImag = Zero; |
15944 | } |
15945 | return true; |
15946 | } |
15947 | |
15948 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
15949 | const Expr* SubExpr = E->getSubExpr(); |
15950 | |
15951 | if (SubExpr->getType()->isRealFloatingType()) { |
15952 | Result.makeComplexFloat(); |
15953 | APFloat &Imag = Result.FloatImag; |
15954 | if (!EvaluateFloat(E: SubExpr, Result&: Imag, Info)) |
15955 | return false; |
15956 | |
15957 | Result.FloatReal = APFloat(Imag.getSemantics()); |
15958 | return true; |
15959 | } else { |
15960 | assert(SubExpr->getType()->isIntegerType() && |
15961 | "Unexpected imaginary literal."); |
15962 | |
15963 | Result.makeComplexInt(); |
15964 | APSInt &Imag = Result.IntImag; |
15965 | if (!EvaluateInteger(E: SubExpr, Result&: Imag, Info)) |
15966 | return false; |
15967 | |
15968 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
15969 | return true; |
15970 | } |
15971 | } |
15972 | |
15973 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
15974 | |
15975 | switch (E->getCastKind()) { |
15976 | case CK_BitCast: |
15977 | case CK_BaseToDerived: |
15978 | case CK_DerivedToBase: |
15979 | case CK_UncheckedDerivedToBase: |
15980 | case CK_Dynamic: |
15981 | case CK_ToUnion: |
15982 | case CK_ArrayToPointerDecay: |
15983 | case CK_FunctionToPointerDecay: |
15984 | case CK_NullToPointer: |
15985 | case CK_NullToMemberPointer: |
15986 | case CK_BaseToDerivedMemberPointer: |
15987 | case CK_DerivedToBaseMemberPointer: |
15988 | case CK_MemberPointerToBoolean: |
15989 | case CK_ReinterpretMemberPointer: |
15990 | case CK_ConstructorConversion: |
15991 | case CK_IntegralToPointer: |
15992 | case CK_PointerToIntegral: |
15993 | case CK_PointerToBoolean: |
15994 | case CK_ToVoid: |
15995 | case CK_VectorSplat: |
15996 | case CK_IntegralCast: |
15997 | case CK_BooleanToSignedIntegral: |
15998 | case CK_IntegralToBoolean: |
15999 | case CK_IntegralToFloating: |
16000 | case CK_FloatingToIntegral: |
16001 | case CK_FloatingToBoolean: |
16002 | case CK_FloatingCast: |
16003 | case CK_CPointerToObjCPointerCast: |
16004 | case CK_BlockPointerToObjCPointerCast: |
16005 | case CK_AnyPointerToBlockPointerCast: |
16006 | case CK_ObjCObjectLValueCast: |
16007 | case CK_FloatingComplexToReal: |
16008 | case CK_FloatingComplexToBoolean: |
16009 | case CK_IntegralComplexToReal: |
16010 | case CK_IntegralComplexToBoolean: |
16011 | case CK_ARCProduceObject: |
16012 | case CK_ARCConsumeObject: |
16013 | case CK_ARCReclaimReturnedObject: |
16014 | case CK_ARCExtendBlockObject: |
16015 | case CK_CopyAndAutoreleaseBlockObject: |
16016 | case CK_BuiltinFnToFnPtr: |
16017 | case CK_ZeroToOCLOpaqueType: |
16018 | case CK_NonAtomicToAtomic: |
16019 | case CK_AddressSpaceConversion: |
16020 | case CK_IntToOCLSampler: |
16021 | case CK_FloatingToFixedPoint: |
16022 | case CK_FixedPointToFloating: |
16023 | case CK_FixedPointCast: |
16024 | case CK_FixedPointToBoolean: |
16025 | case CK_FixedPointToIntegral: |
16026 | case CK_IntegralToFixedPoint: |
16027 | case CK_MatrixCast: |
16028 | case CK_HLSLVectorTruncation: |
16029 | case CK_HLSLElementwiseCast: |
16030 | case CK_HLSLAggregateSplatCast: |
16031 | llvm_unreachable("invalid cast kind for complex value"); |
16032 | |
16033 | case CK_LValueToRValue: |
16034 | case CK_AtomicToNonAtomic: |
16035 | case CK_NoOp: |
16036 | case CK_LValueToRValueBitCast: |
16037 | case CK_HLSLArrayRValue: |
16038 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
16039 | |
16040 | case CK_Dependent: |
16041 | case CK_LValueBitCast: |
16042 | case CK_UserDefinedConversion: |
16043 | return Error(E); |
16044 | |
16045 | case CK_FloatingRealToComplex: { |
16046 | APFloat &Real = Result.FloatReal; |
16047 | if (!EvaluateFloat(E: E->getSubExpr(), Result&: Real, Info)) |
16048 | return false; |
16049 | |
16050 | Result.makeComplexFloat(); |
16051 | Result.FloatImag = APFloat(Real.getSemantics()); |
16052 | return true; |
16053 | } |
16054 | |
16055 | case CK_FloatingComplexCast: { |
16056 | if (!Visit(E->getSubExpr())) |
16057 | return false; |
16058 | |
16059 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
16060 | QualType From |
16061 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
16062 | |
16063 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
16064 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
16065 | } |
16066 | |
16067 | case CK_FloatingComplexToIntegralComplex: { |
16068 | if (!Visit(E->getSubExpr())) |
16069 | return false; |
16070 | |
16071 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
16072 | QualType From |
16073 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
16074 | Result.makeComplexInt(); |
16075 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
16076 | To, Result.IntReal) && |
16077 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
16078 | To, Result.IntImag); |
16079 | } |
16080 | |
16081 | case CK_IntegralRealToComplex: { |
16082 | APSInt &Real = Result.IntReal; |
16083 | if (!EvaluateInteger(E: E->getSubExpr(), Result&: Real, Info)) |
16084 | return false; |
16085 | |
16086 | Result.makeComplexInt(); |
16087 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
16088 | return true; |
16089 | } |
16090 | |
16091 | case CK_IntegralComplexCast: { |
16092 | if (!Visit(E->getSubExpr())) |
16093 | return false; |
16094 | |
16095 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
16096 | QualType From |
16097 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
16098 | |
16099 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
16100 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
16101 | return true; |
16102 | } |
16103 | |
16104 | case CK_IntegralComplexToFloatingComplex: { |
16105 | if (!Visit(E->getSubExpr())) |
16106 | return false; |
16107 | |
16108 | const FPOptions FPO = E->getFPFeaturesInEffect( |
16109 | LO: Info.Ctx.getLangOpts()); |
16110 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
16111 | QualType From |
16112 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
16113 | Result.makeComplexFloat(); |
16114 | return HandleIntToFloatCast(Info, E, FPO, From, Result.IntReal, |
16115 | To, Result.FloatReal) && |
16116 | HandleIntToFloatCast(Info, E, FPO, From, Result.IntImag, |
16117 | To, Result.FloatImag); |
16118 | } |
16119 | } |
16120 | |
16121 | llvm_unreachable("unknown cast resulting in complex value"); |
16122 | } |
16123 | |
16124 | void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D, |
16125 | APFloat &ResR, APFloat &ResI) { |
16126 | // This is an implementation of complex multiplication according to the |
16127 | // constraints laid out in C11 Annex G. The implementation uses the |
16128 | // following naming scheme: |
16129 | // (a + ib) * (c + id) |
16130 | |
16131 | APFloat AC = A * C; |
16132 | APFloat BD = B * D; |
16133 | APFloat AD = A * D; |
16134 | APFloat BC = B * C; |
16135 | ResR = AC - BD; |
16136 | ResI = AD + BC; |
16137 | if (ResR.isNaN() && ResI.isNaN()) { |
16138 | bool Recalc = false; |
16139 | if (A.isInfinity() || B.isInfinity()) { |
16140 | A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), |
16141 | Sign: A); |
16142 | B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), |
16143 | Sign: B); |
16144 | if (C.isNaN()) |
16145 | C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C); |
16146 | if (D.isNaN()) |
16147 | D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D); |
16148 | Recalc = true; |
16149 | } |
16150 | if (C.isInfinity() || D.isInfinity()) { |
16151 | C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), |
16152 | Sign: C); |
16153 | D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), |
16154 | Sign: D); |
16155 | if (A.isNaN()) |
16156 | A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A); |
16157 | if (B.isNaN()) |
16158 | B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B); |
16159 | Recalc = true; |
16160 | } |
16161 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || AD.isInfinity() || |
16162 | BC.isInfinity())) { |
16163 | if (A.isNaN()) |
16164 | A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A); |
16165 | if (B.isNaN()) |
16166 | B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B); |
16167 | if (C.isNaN()) |
16168 | C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C); |
16169 | if (D.isNaN()) |
16170 | D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D); |
16171 | Recalc = true; |
16172 | } |
16173 | if (Recalc) { |
16174 | ResR = APFloat::getInf(Sem: A.getSemantics()) * (A * C - B * D); |
16175 | ResI = APFloat::getInf(Sem: A.getSemantics()) * (A * D + B * C); |
16176 | } |
16177 | } |
16178 | } |
16179 | |
16180 | void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D, |
16181 | APFloat &ResR, APFloat &ResI) { |
16182 | // This is an implementation of complex division according to the |
16183 | // constraints laid out in C11 Annex G. The implementation uses the |
16184 | // following naming scheme: |
16185 | // (a + ib) / (c + id) |
16186 | |
16187 | int DenomLogB = 0; |
16188 | APFloat MaxCD = maxnum(A: abs(X: C), B: abs(X: D)); |
16189 | if (MaxCD.isFinite()) { |
16190 | DenomLogB = ilogb(Arg: MaxCD); |
16191 | C = scalbn(X: C, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven); |
16192 | D = scalbn(X: D, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven); |
16193 | } |
16194 | APFloat Denom = C * C + D * D; |
16195 | ResR = |
16196 | scalbn(X: (A * C + B * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven); |
16197 | ResI = |
16198 | scalbn(X: (B * C - A * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven); |
16199 | if (ResR.isNaN() && ResI.isNaN()) { |
16200 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
16201 | ResR = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * A; |
16202 | ResI = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * B; |
16203 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
16204 | D.isFinite()) { |
16205 | A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), |
16206 | Sign: A); |
16207 | B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), |
16208 | Sign: B); |
16209 | ResR = APFloat::getInf(Sem: ResR.getSemantics()) * (A * C + B * D); |
16210 | ResI = APFloat::getInf(Sem: ResI.getSemantics()) * (B * C - A * D); |
16211 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
16212 | C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), |
16213 | Sign: C); |
16214 | D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), |
16215 | Sign: D); |
16216 | ResR = APFloat::getZero(Sem: ResR.getSemantics()) * (A * C + B * D); |
16217 | ResI = APFloat::getZero(Sem: ResI.getSemantics()) * (B * C - A * D); |
16218 | } |
16219 | } |
16220 | } |
16221 | |
16222 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
16223 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
16224 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
16225 | |
16226 | // Track whether the LHS or RHS is real at the type system level. When this is |
16227 | // the case we can simplify our evaluation strategy. |
16228 | bool LHSReal = false, RHSReal = false; |
16229 | |
16230 | bool LHSOK; |
16231 | if (E->getLHS()->getType()->isRealFloatingType()) { |
16232 | LHSReal = true; |
16233 | APFloat &Real = Result.FloatReal; |
16234 | LHSOK = EvaluateFloat(E: E->getLHS(), Result&: Real, Info); |
16235 | if (LHSOK) { |
16236 | Result.makeComplexFloat(); |
16237 | Result.FloatImag = APFloat(Real.getSemantics()); |
16238 | } |
16239 | } else { |
16240 | LHSOK = Visit(E->getLHS()); |
16241 | } |
16242 | if (!LHSOK && !Info.noteFailure()) |
16243 | return false; |
16244 | |
16245 | ComplexValue RHS; |
16246 | if (E->getRHS()->getType()->isRealFloatingType()) { |
16247 | RHSReal = true; |
16248 | APFloat &Real = RHS.FloatReal; |
16249 | if (!EvaluateFloat(E: E->getRHS(), Result&: Real, Info) || !LHSOK) |
16250 | return false; |
16251 | RHS.makeComplexFloat(); |
16252 | RHS.FloatImag = APFloat(Real.getSemantics()); |
16253 | } else if (!EvaluateComplex(E: E->getRHS(), Result&: RHS, Info) || !LHSOK) |
16254 | return false; |
16255 | |
16256 | assert(!(LHSReal && RHSReal) && |
16257 | "Cannot have both operands of a complex operation be real."); |
16258 | switch (E->getOpcode()) { |
16259 | default: return Error(E); |
16260 | case BO_Add: |
16261 | if (Result.isComplexFloat()) { |
16262 | Result.getComplexFloatReal().add(RHS: RHS.getComplexFloatReal(), |
16263 | RM: APFloat::rmNearestTiesToEven); |
16264 | if (LHSReal) |
16265 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
16266 | else if (!RHSReal) |
16267 | Result.getComplexFloatImag().add(RHS: RHS.getComplexFloatImag(), |
16268 | RM: APFloat::rmNearestTiesToEven); |
16269 | } else { |
16270 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
16271 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
16272 | } |
16273 | break; |
16274 | case BO_Sub: |
16275 | if (Result.isComplexFloat()) { |
16276 | Result.getComplexFloatReal().subtract(RHS: RHS.getComplexFloatReal(), |
16277 | RM: APFloat::rmNearestTiesToEven); |
16278 | if (LHSReal) { |
16279 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
16280 | Result.getComplexFloatImag().changeSign(); |
16281 | } else if (!RHSReal) { |
16282 | Result.getComplexFloatImag().subtract(RHS: RHS.getComplexFloatImag(), |
16283 | RM: APFloat::rmNearestTiesToEven); |
16284 | } |
16285 | } else { |
16286 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
16287 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
16288 | } |
16289 | break; |
16290 | case BO_Mul: |
16291 | if (Result.isComplexFloat()) { |
16292 | // This is an implementation of complex multiplication according to the |
16293 | // constraints laid out in C11 Annex G. The implementation uses the |
16294 | // following naming scheme: |
16295 | // (a + ib) * (c + id) |
16296 | ComplexValue LHS = Result; |
16297 | APFloat &A = LHS.getComplexFloatReal(); |
16298 | APFloat &B = LHS.getComplexFloatImag(); |
16299 | APFloat &C = RHS.getComplexFloatReal(); |
16300 | APFloat &D = RHS.getComplexFloatImag(); |
16301 | APFloat &ResR = Result.getComplexFloatReal(); |
16302 | APFloat &ResI = Result.getComplexFloatImag(); |
16303 | if (LHSReal) { |
16304 | assert(!RHSReal && "Cannot have two real operands for a complex op!"); |
16305 | ResR = A; |
16306 | ResI = A; |
16307 | // ResR = A * C; |
16308 | // ResI = A * D; |
16309 | if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: C) || |
16310 | !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: D)) |
16311 | return false; |
16312 | } else if (RHSReal) { |
16313 | // ResR = C * A; |
16314 | // ResI = C * B; |
16315 | ResR = C; |
16316 | ResI = C; |
16317 | if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: A) || |
16318 | !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: B)) |
16319 | return false; |
16320 | } else { |
16321 | HandleComplexComplexMul(A, B, C, D, ResR, ResI); |
16322 | } |
16323 | } else { |
16324 | ComplexValue LHS = Result; |
16325 | Result.getComplexIntReal() = |
16326 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
16327 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
16328 | Result.getComplexIntImag() = |
16329 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
16330 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
16331 | } |
16332 | break; |
16333 | case BO_Div: |
16334 | if (Result.isComplexFloat()) { |
16335 | // This is an implementation of complex division according to the |
16336 | // constraints laid out in C11 Annex G. The implementation uses the |
16337 | // following naming scheme: |
16338 | // (a + ib) / (c + id) |
16339 | ComplexValue LHS = Result; |
16340 | APFloat &A = LHS.getComplexFloatReal(); |
16341 | APFloat &B = LHS.getComplexFloatImag(); |
16342 | APFloat &C = RHS.getComplexFloatReal(); |
16343 | APFloat &D = RHS.getComplexFloatImag(); |
16344 | APFloat &ResR = Result.getComplexFloatReal(); |
16345 | APFloat &ResI = Result.getComplexFloatImag(); |
16346 | if (RHSReal) { |
16347 | ResR = A; |
16348 | ResI = B; |
16349 | // ResR = A / C; |
16350 | // ResI = B / C; |
16351 | if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Div, RHS: C) || |
16352 | !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Div, RHS: C)) |
16353 | return false; |
16354 | } else { |
16355 | if (LHSReal) { |
16356 | // No real optimizations we can do here, stub out with zero. |
16357 | B = APFloat::getZero(Sem: A.getSemantics()); |
16358 | } |
16359 | HandleComplexComplexDiv(A, B, C, D, ResR, ResI); |
16360 | } |
16361 | } else { |
16362 | ComplexValue LHS = Result; |
16363 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
16364 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
16365 | if (Den.isZero()) |
16366 | return Error(E, diag::note_expr_divide_by_zero); |
16367 | |
16368 | Result.getComplexIntReal() = |
16369 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
16370 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
16371 | Result.getComplexIntImag() = |
16372 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
16373 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
16374 | } |
16375 | break; |
16376 | } |
16377 | |
16378 | return true; |
16379 | } |
16380 | |
16381 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
16382 | // Get the operand value into 'Result'. |
16383 | if (!Visit(E->getSubExpr())) |
16384 | return false; |
16385 | |
16386 | switch (E->getOpcode()) { |
16387 | default: |
16388 | return Error(E); |
16389 | case UO_Extension: |
16390 | return true; |
16391 | case UO_Plus: |
16392 | // The result is always just the subexpr. |
16393 | return true; |
16394 | case UO_Minus: |
16395 | if (Result.isComplexFloat()) { |
16396 | Result.getComplexFloatReal().changeSign(); |
16397 | Result.getComplexFloatImag().changeSign(); |
16398 | } |
16399 | else { |
16400 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
16401 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
16402 | } |
16403 | return true; |
16404 | case UO_Not: |
16405 | if (Result.isComplexFloat()) |
16406 | Result.getComplexFloatImag().changeSign(); |
16407 | else |
16408 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
16409 | return true; |
16410 | } |
16411 | } |
16412 | |
16413 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
16414 | if (E->getNumInits() == 2) { |
16415 | if (E->getType()->isComplexType()) { |
16416 | Result.makeComplexFloat(); |
16417 | if (!EvaluateFloat(E: E->getInit(Init: 0), Result&: Result.FloatReal, Info)) |
16418 | return false; |
16419 | if (!EvaluateFloat(E: E->getInit(Init: 1), Result&: Result.FloatImag, Info)) |
16420 | return false; |
16421 | } else { |
16422 | Result.makeComplexInt(); |
16423 | if (!EvaluateInteger(E: E->getInit(Init: 0), Result&: Result.IntReal, Info)) |
16424 | return false; |
16425 | if (!EvaluateInteger(E: E->getInit(Init: 1), Result&: Result.IntImag, Info)) |
16426 | return false; |
16427 | } |
16428 | return true; |
16429 | } |
16430 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
16431 | } |
16432 | |
16433 | bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) { |
16434 | if (!IsConstantEvaluatedBuiltinCall(E)) |
16435 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
16436 | |
16437 | switch (E->getBuiltinCallee()) { |
16438 | case Builtin::BI__builtin_complex: |
16439 | Result.makeComplexFloat(); |
16440 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: Result.FloatReal, Info)) |
16441 | return false; |
16442 | if (!EvaluateFloat(E: E->getArg(Arg: 1), Result&: Result.FloatImag, Info)) |
16443 | return false; |
16444 | return true; |
16445 | |
16446 | default: |
16447 | return false; |
16448 | } |
16449 | } |
16450 | |
16451 | //===----------------------------------------------------------------------===// |
16452 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
16453 | // implicit conversion. |
16454 | //===----------------------------------------------------------------------===// |
16455 | |
16456 | namespace { |
16457 | class AtomicExprEvaluator : |
16458 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
16459 | const LValue *This; |
16460 | APValue &Result; |
16461 | public: |
16462 | AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result) |
16463 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
16464 | |
16465 | bool Success(const APValue &V, const Expr *E) { |
16466 | Result = V; |
16467 | return true; |
16468 | } |
16469 | |
16470 | bool ZeroInitialization(const Expr *E) { |
16471 | ImplicitValueInitExpr VIE( |
16472 | E->getType()->castAs<AtomicType>()->getValueType()); |
16473 | // For atomic-qualified class (and array) types in C++, initialize the |
16474 | // _Atomic-wrapped subobject directly, in-place. |
16475 | return This ? EvaluateInPlace(Result, Info, *This, &VIE) |
16476 | : Evaluate(Result, Info, &VIE); |
16477 | } |
16478 | |
16479 | bool VisitCastExpr(const CastExpr *E) { |
16480 | switch (E->getCastKind()) { |
16481 | default: |
16482 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
16483 | case CK_NullToPointer: |
16484 | VisitIgnoredValue(E: E->getSubExpr()); |
16485 | return ZeroInitialization(E); |
16486 | case CK_NonAtomicToAtomic: |
16487 | return This ? EvaluateInPlace(Result, Info, This: *This, E: E->getSubExpr()) |
16488 | : Evaluate(Result, Info, E: E->getSubExpr()); |
16489 | } |
16490 | } |
16491 | }; |
16492 | } // end anonymous namespace |
16493 | |
16494 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
16495 | EvalInfo &Info) { |
16496 | assert(!E->isValueDependent()); |
16497 | assert(E->isPRValue() && E->getType()->isAtomicType()); |
16498 | return AtomicExprEvaluator(Info, This, Result).Visit(E); |
16499 | } |
16500 | |
16501 | //===----------------------------------------------------------------------===// |
16502 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
16503 | // comma operator |
16504 | //===----------------------------------------------------------------------===// |
16505 | |
16506 | namespace { |
16507 | class VoidExprEvaluator |
16508 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
16509 | public: |
16510 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
16511 | |
16512 | bool Success(const APValue &V, const Expr *e) { return true; } |
16513 | |
16514 | bool ZeroInitialization(const Expr *E) { return true; } |
16515 | |
16516 | bool VisitCastExpr(const CastExpr *E) { |
16517 | switch (E->getCastKind()) { |
16518 | default: |
16519 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
16520 | case CK_ToVoid: |
16521 | VisitIgnoredValue(E: E->getSubExpr()); |
16522 | return true; |
16523 | } |
16524 | } |
16525 | |
16526 | bool VisitCallExpr(const CallExpr *E) { |
16527 | if (!IsConstantEvaluatedBuiltinCall(E)) |
16528 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
16529 | |
16530 | switch (E->getBuiltinCallee()) { |
16531 | case Builtin::BI__assume: |
16532 | case Builtin::BI__builtin_assume: |
16533 | // The argument is not evaluated! |
16534 | return true; |
16535 | |
16536 | case Builtin::BI__builtin_operator_delete: |
16537 | return HandleOperatorDeleteCall(Info, E); |
16538 | |
16539 | default: |
16540 | return false; |
16541 | } |
16542 | } |
16543 | |
16544 | bool VisitCXXDeleteExpr(const CXXDeleteExpr *E); |
16545 | }; |
16546 | } // end anonymous namespace |
16547 | |
16548 | bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
16549 | // We cannot speculatively evaluate a delete expression. |
16550 | if (Info.SpeculativeEvaluationDepth) |
16551 | return false; |
16552 | |
16553 | FunctionDecl *OperatorDelete = E->getOperatorDelete(); |
16554 | if (!OperatorDelete |
16555 | ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) { |
16556 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
16557 | << isa<CXXMethodDecl>(OperatorDelete) << OperatorDelete; |
16558 | return false; |
16559 | } |
16560 | |
16561 | const Expr *Arg = E->getArgument(); |
16562 | |
16563 | LValue Pointer; |
16564 | if (!EvaluatePointer(E: Arg, Result&: Pointer, Info)) |
16565 | return false; |
16566 | if (Pointer.Designator.Invalid) |
16567 | return false; |
16568 | |
16569 | // Deleting a null pointer has no effect. |
16570 | if (Pointer.isNullPointer()) { |
16571 | // This is the only case where we need to produce an extension warning: |
16572 | // the only other way we can succeed is if we find a dynamic allocation, |
16573 | // and we will have warned when we allocated it in that case. |
16574 | if (!Info.getLangOpts().CPlusPlus20) |
16575 | Info.CCEDiag(E, diag::note_constexpr_new); |
16576 | return true; |
16577 | } |
16578 | |
16579 | std::optional<DynAlloc *> Alloc = CheckDeleteKind( |
16580 | Info, E, Pointer, E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New); |
16581 | if (!Alloc) |
16582 | return false; |
16583 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
16584 | |
16585 | // For the non-array case, the designator must be empty if the static type |
16586 | // does not have a virtual destructor. |
16587 | if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 && |
16588 | !hasVirtualDestructor(T: Arg->getType()->getPointeeType())) { |
16589 | Info.FFDiag(E, diag::note_constexpr_delete_base_nonvirt_dtor) |
16590 | << Arg->getType()->getPointeeType() << AllocType; |
16591 | return false; |
16592 | } |
16593 | |
16594 | // For a class type with a virtual destructor, the selected operator delete |
16595 | // is the one looked up when building the destructor. |
16596 | if (!E->isArrayForm() && !E->isGlobalDelete()) { |
16597 | const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(T: AllocType); |
16598 | if (VirtualDelete && |
16599 | !VirtualDelete |
16600 | ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) { |
16601 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
16602 | << isa<CXXMethodDecl>(VirtualDelete) << VirtualDelete; |
16603 | return false; |
16604 | } |
16605 | } |
16606 | |
16607 | if (!HandleDestruction(Info, E->getExprLoc(), Pointer.getLValueBase(), |
16608 | (*Alloc)->Value, AllocType)) |
16609 | return false; |
16610 | |
16611 | if (!Info.HeapAllocs.erase(x: Pointer.Base.dyn_cast<DynamicAllocLValue>())) { |
16612 | // The element was already erased. This means the destructor call also |
16613 | // deleted the object. |
16614 | // FIXME: This probably results in undefined behavior before we get this |
16615 | // far, and should be diagnosed elsewhere first. |
16616 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
16617 | return false; |
16618 | } |
16619 | |
16620 | return true; |
16621 | } |
16622 | |
16623 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
16624 | assert(!E->isValueDependent()); |
16625 | assert(E->isPRValue() && E->getType()->isVoidType()); |
16626 | return VoidExprEvaluator(Info).Visit(E); |
16627 | } |
16628 | |
16629 | //===----------------------------------------------------------------------===// |
16630 | // Top level Expr::EvaluateAsRValue method. |
16631 | //===----------------------------------------------------------------------===// |
16632 | |
16633 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
16634 | assert(!E->isValueDependent()); |
16635 | // In C, function designators are not lvalues, but we evaluate them as if they |
16636 | // are. |
16637 | QualType T = E->getType(); |
16638 | if (E->isGLValue() || T->isFunctionType()) { |
16639 | LValue LV; |
16640 | if (!EvaluateLValue(E, Result&: LV, Info)) |
16641 | return false; |
16642 | LV.moveInto(V&: Result); |
16643 | } else if (T->isVectorType()) { |
16644 | if (!EvaluateVector(E, Result, Info)) |
16645 | return false; |
16646 | } else if (T->isIntegralOrEnumerationType()) { |
16647 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
16648 | return false; |
16649 | } else if (T->hasPointerRepresentation()) { |
16650 | LValue LV; |
16651 | if (!EvaluatePointer(E, Result&: LV, Info)) |
16652 | return false; |
16653 | LV.moveInto(V&: Result); |
16654 | } else if (T->isRealFloatingType()) { |
16655 | llvm::APFloat F(0.0); |
16656 | if (!EvaluateFloat(E, Result&: F, Info)) |
16657 | return false; |
16658 | Result = APValue(F); |
16659 | } else if (T->isAnyComplexType()) { |
16660 | ComplexValue C; |
16661 | if (!EvaluateComplex(E, Result&: C, Info)) |
16662 | return false; |
16663 | C.moveInto(v&: Result); |
16664 | } else if (T->isFixedPointType()) { |
16665 | if (!FixedPointExprEvaluator(Info, Result).Visit(E)) return false; |
16666 | } else if (T->isMemberPointerType()) { |
16667 | MemberPtr P; |
16668 | if (!EvaluateMemberPointer(E, Result&: P, Info)) |
16669 | return false; |
16670 | P.moveInto(V&: Result); |
16671 | return true; |
16672 | } else if (T->isArrayType()) { |
16673 | LValue LV; |
16674 | APValue &Value = |
16675 | Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV); |
16676 | if (!EvaluateArray(E, This: LV, Result&: Value, Info)) |
16677 | return false; |
16678 | Result = Value; |
16679 | } else if (T->isRecordType()) { |
16680 | LValue LV; |
16681 | APValue &Value = |
16682 | Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV); |
16683 | if (!EvaluateRecord(E, This: LV, Result&: Value, Info)) |
16684 | return false; |
16685 | Result = Value; |
16686 | } else if (T->isVoidType()) { |
16687 | if (!Info.getLangOpts().CPlusPlus11) |
16688 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
16689 | << E->getType(); |
16690 | if (!EvaluateVoid(E, Info)) |
16691 | return false; |
16692 | } else if (T->isAtomicType()) { |
16693 | QualType Unqual = T.getAtomicUnqualifiedType(); |
16694 | if (Unqual->isArrayType() || Unqual->isRecordType()) { |
16695 | LValue LV; |
16696 | APValue &Value = Info.CurrentCall->createTemporary( |
16697 | Key: E, T: Unqual, Scope: ScopeKind::FullExpression, LV); |
16698 | if (!EvaluateAtomic(E, This: &LV, Result&: Value, Info)) |
16699 | return false; |
16700 | Result = Value; |
16701 | } else { |
16702 | if (!EvaluateAtomic(E, This: nullptr, Result, Info)) |
16703 | return false; |
16704 | } |
16705 | } else if (Info.getLangOpts().CPlusPlus11) { |
16706 | Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType(); |
16707 | return false; |
16708 | } else { |
16709 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
16710 | return false; |
16711 | } |
16712 | |
16713 | return true; |
16714 | } |
16715 | |
16716 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
16717 | /// cases, the in-place evaluation is essential, since later initializers for |
16718 | /// an object can indirectly refer to subobjects which were initialized earlier. |
16719 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
16720 | const Expr *E, bool AllowNonLiteralTypes) { |
16721 | assert(!E->isValueDependent()); |
16722 | |
16723 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, This: &This)) |
16724 | return false; |
16725 | |
16726 | if (E->isPRValue()) { |
16727 | // Evaluate arrays and record types in-place, so that later initializers can |
16728 | // refer to earlier-initialized members of the object. |
16729 | QualType T = E->getType(); |
16730 | if (T->isArrayType()) |
16731 | return EvaluateArray(E, This, Result, Info); |
16732 | else if (T->isRecordType()) |
16733 | return EvaluateRecord(E, This, Result, Info); |
16734 | else if (T->isAtomicType()) { |
16735 | QualType Unqual = T.getAtomicUnqualifiedType(); |
16736 | if (Unqual->isArrayType() || Unqual->isRecordType()) |
16737 | return EvaluateAtomic(E, This: &This, Result, Info); |
16738 | } |
16739 | } |
16740 | |
16741 | // For any other type, in-place evaluation is unimportant. |
16742 | return Evaluate(Result, Info, E); |
16743 | } |
16744 | |
16745 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
16746 | /// lvalue-to-rvalue cast if it is an lvalue. |
16747 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
16748 | assert(!E->isValueDependent()); |
16749 | |
16750 | if (E->getType().isNull()) |
16751 | return false; |
16752 | |
16753 | if (!CheckLiteralType(Info, E)) |
16754 | return false; |
16755 | |
16756 | if (Info.EnableNewConstInterp) { |
16757 | if (!Info.Ctx.getInterpContext().evaluateAsRValue(Parent&: Info, E, Result)) |
16758 | return false; |
16759 | return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result, |
16760 | Kind: ConstantExprKind::Normal); |
16761 | } |
16762 | |
16763 | if (!::Evaluate(Result, Info, E)) |
16764 | return false; |
16765 | |
16766 | // Implicit lvalue-to-rvalue cast. |
16767 | if (E->isGLValue()) { |
16768 | LValue LV; |
16769 | LV.setFrom(Ctx&: Info.Ctx, V: Result); |
16770 | if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result)) |
16771 | return false; |
16772 | } |
16773 | |
16774 | // Check this core constant expression is a constant expression. |
16775 | return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result, |
16776 | Kind: ConstantExprKind::Normal) && |
16777 | CheckMemoryLeaks(Info); |
16778 | } |
16779 | |
16780 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
16781 | const ASTContext &Ctx, bool &IsConst) { |
16782 | // Fast-path evaluations of integer literals, since we sometimes see files |
16783 | // containing vast quantities of these. |
16784 | if (const auto *L = dyn_cast<IntegerLiteral>(Val: Exp)) { |
16785 | Result.Val = APValue(APSInt(L->getValue(), |
16786 | L->getType()->isUnsignedIntegerType())); |
16787 | IsConst = true; |
16788 | return true; |
16789 | } |
16790 | |
16791 | if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Val: Exp)) { |
16792 | Result.Val = APValue(APSInt(APInt(1, L->getValue()))); |
16793 | IsConst = true; |
16794 | return true; |
16795 | } |
16796 | |
16797 | if (const auto *FL = dyn_cast<FloatingLiteral>(Val: Exp)) { |
16798 | Result.Val = APValue(FL->getValue()); |
16799 | IsConst = true; |
16800 | return true; |
16801 | } |
16802 | |
16803 | if (const auto *L = dyn_cast<CharacterLiteral>(Val: Exp)) { |
16804 | Result.Val = APValue(Ctx.MakeIntValue(Value: L->getValue(), Type: L->getType())); |
16805 | IsConst = true; |
16806 | return true; |
16807 | } |
16808 | |
16809 | if (const auto *CE = dyn_cast<ConstantExpr>(Val: Exp)) { |
16810 | if (CE->hasAPValueResult()) { |
16811 | APValue APV = CE->getAPValueResult(); |
16812 | if (!APV.isLValue()) { |
16813 | Result.Val = std::move(APV); |
16814 | IsConst = true; |
16815 | return true; |
16816 | } |
16817 | } |
16818 | |
16819 | // The SubExpr is usually just an IntegerLiteral. |
16820 | return FastEvaluateAsRValue(CE->getSubExpr(), Result, Ctx, IsConst); |
16821 | } |
16822 | |
16823 | // This case should be rare, but we need to check it before we check on |
16824 | // the type below. |
16825 | if (Exp->getType().isNull()) { |
16826 | IsConst = false; |
16827 | return true; |
16828 | } |
16829 | |
16830 | return false; |
16831 | } |
16832 | |
16833 | static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result, |
16834 | Expr::SideEffectsKind SEK) { |
16835 | return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) || |
16836 | (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior); |
16837 | } |
16838 | |
16839 | static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result, |
16840 | const ASTContext &Ctx, EvalInfo &Info) { |
16841 | assert(!E->isValueDependent()); |
16842 | bool IsConst; |
16843 | if (FastEvaluateAsRValue(Exp: E, Result, Ctx, IsConst)) |
16844 | return IsConst; |
16845 | |
16846 | return EvaluateAsRValue(Info, E, Result&: Result.Val); |
16847 | } |
16848 | |
16849 | static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult, |
16850 | const ASTContext &Ctx, |
16851 | Expr::SideEffectsKind AllowSideEffects, |
16852 | EvalInfo &Info) { |
16853 | assert(!E->isValueDependent()); |
16854 | if (!E->getType()->isIntegralOrEnumerationType()) |
16855 | return false; |
16856 | |
16857 | if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info) || |
16858 | !ExprResult.Val.isInt() || |
16859 | hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects)) |
16860 | return false; |
16861 | |
16862 | return true; |
16863 | } |
16864 | |
16865 | static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult, |
16866 | const ASTContext &Ctx, |
16867 | Expr::SideEffectsKind AllowSideEffects, |
16868 | EvalInfo &Info) { |
16869 | assert(!E->isValueDependent()); |
16870 | if (!E->getType()->isFixedPointType()) |
16871 | return false; |
16872 | |
16873 | if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info)) |
16874 | return false; |
16875 | |
16876 | if (!ExprResult.Val.isFixedPoint() || |
16877 | hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects)) |
16878 | return false; |
16879 | |
16880 | return true; |
16881 | } |
16882 | |
16883 | /// EvaluateAsRValue - Return true if this is a constant which we can fold using |
16884 | /// any crazy technique (that has nothing to do with language standards) that |
16885 | /// we want to. If this function returns true, it returns the folded constant |
16886 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
16887 | /// will be applied to the result. |
16888 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, |
16889 | bool InConstantContext) const { |
16890 | assert(!isValueDependent() && |
16891 | "Expression evaluator can't be called on a dependent expression."); |
16892 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsRValue"); |
16893 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
16894 | Info.InConstantContext = InConstantContext; |
16895 | return ::EvaluateAsRValue(E: this, Result, Ctx, Info); |
16896 | } |
16897 | |
16898 | bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, |
16899 | bool InConstantContext) const { |
16900 | assert(!isValueDependent() && |
16901 | "Expression evaluator can't be called on a dependent expression."); |
16902 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsBooleanCondition"); |
16903 | EvalResult Scratch; |
16904 | return EvaluateAsRValue(Result&: Scratch, Ctx, InConstantContext) && |
16905 | HandleConversionToBool(Val: Scratch.Val, Result); |
16906 | } |
16907 | |
16908 | bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, |
16909 | SideEffectsKind AllowSideEffects, |
16910 | bool InConstantContext) const { |
16911 | assert(!isValueDependent() && |
16912 | "Expression evaluator can't be called on a dependent expression."); |
16913 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsInt"); |
16914 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
16915 | Info.InConstantContext = InConstantContext; |
16916 | return ::EvaluateAsInt(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info); |
16917 | } |
16918 | |
16919 | bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx, |
16920 | SideEffectsKind AllowSideEffects, |
16921 | bool InConstantContext) const { |
16922 | assert(!isValueDependent() && |
16923 | "Expression evaluator can't be called on a dependent expression."); |
16924 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFixedPoint"); |
16925 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
16926 | Info.InConstantContext = InConstantContext; |
16927 | return ::EvaluateAsFixedPoint(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info); |
16928 | } |
16929 | |
16930 | bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx, |
16931 | SideEffectsKind AllowSideEffects, |
16932 | bool InConstantContext) const { |
16933 | assert(!isValueDependent() && |
16934 | "Expression evaluator can't be called on a dependent expression."); |
16935 | |
16936 | if (!getType()->isRealFloatingType()) |
16937 | return false; |
16938 | |
16939 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFloat"); |
16940 | EvalResult ExprResult; |
16941 | if (!EvaluateAsRValue(Result&: ExprResult, Ctx, InConstantContext) || |
16942 | !ExprResult.Val.isFloat() || |
16943 | hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects)) |
16944 | return false; |
16945 | |
16946 | Result = ExprResult.Val.getFloat(); |
16947 | return true; |
16948 | } |
16949 | |
16950 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, |
16951 | bool InConstantContext) const { |
16952 | assert(!isValueDependent() && |
16953 | "Expression evaluator can't be called on a dependent expression."); |
16954 | |
16955 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsLValue"); |
16956 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
16957 | Info.InConstantContext = InConstantContext; |
16958 | LValue LV; |
16959 | CheckedTemporaries CheckedTemps; |
16960 | if (!EvaluateLValue(E: this, Result&: LV, Info) || !Info.discardCleanups() || |
16961 | Result.HasSideEffects || |
16962 | !CheckLValueConstantExpression(Info, Loc: getExprLoc(), |
16963 | Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV, |
16964 | Kind: ConstantExprKind::Normal, CheckedTemps)) |
16965 | return false; |
16966 | |
16967 | LV.moveInto(V&: Result.Val); |
16968 | return true; |
16969 | } |
16970 | |
16971 | static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base, |
16972 | APValue DestroyedValue, QualType Type, |
16973 | SourceLocation Loc, Expr::EvalStatus &EStatus, |
16974 | bool IsConstantDestruction) { |
16975 | EvalInfo Info(Ctx, EStatus, |
16976 | IsConstantDestruction ? EvalInfo::EM_ConstantExpression |
16977 | : EvalInfo::EM_ConstantFold); |
16978 | Info.setEvaluatingDecl(Base, Value&: DestroyedValue, |
16979 | EDK: EvalInfo::EvaluatingDeclKind::Dtor); |
16980 | Info.InConstantContext = IsConstantDestruction; |
16981 | |
16982 | LValue LVal; |
16983 | LVal.set(B: Base); |
16984 | |
16985 | if (!HandleDestruction(Info, Loc, LVBase: Base, Value&: DestroyedValue, T: Type) || |
16986 | EStatus.HasSideEffects) |
16987 | return false; |
16988 | |
16989 | if (!Info.discardCleanups()) |
16990 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
16991 | |
16992 | return true; |
16993 | } |
16994 | |
16995 | bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx, |
16996 | ConstantExprKind Kind) const { |
16997 | assert(!isValueDependent() && |
16998 | "Expression evaluator can't be called on a dependent expression."); |
16999 | bool IsConst; |
17000 | if (FastEvaluateAsRValue(Exp: this, Result, Ctx, IsConst) && Result.Val.hasValue()) |
17001 | return true; |
17002 | |
17003 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr"); |
17004 | EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression; |
17005 | EvalInfo Info(Ctx, Result, EM); |
17006 | Info.InConstantContext = true; |
17007 | |
17008 | if (Info.EnableNewConstInterp) { |
17009 | if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val, Kind)) |
17010 | return false; |
17011 | return CheckConstantExpression(Info, DiagLoc: getExprLoc(), |
17012 | Type: getStorageType(Ctx, E: this), Value: Result.Val, Kind); |
17013 | } |
17014 | |
17015 | // The type of the object we're initializing is 'const T' for a class NTTP. |
17016 | QualType T = getType(); |
17017 | if (Kind == ConstantExprKind::ClassTemplateArgument) |
17018 | T.addConst(); |
17019 | |
17020 | // If we're evaluating a prvalue, fake up a MaterializeTemporaryExpr to |
17021 | // represent the result of the evaluation. CheckConstantExpression ensures |
17022 | // this doesn't escape. |
17023 | MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true); |
17024 | APValue::LValueBase Base(&BaseMTE); |
17025 | Info.setEvaluatingDecl(Base, Value&: Result.Val); |
17026 | |
17027 | LValue LVal; |
17028 | LVal.set(B: Base); |
17029 | // C++23 [intro.execution]/p5 |
17030 | // A full-expression is [...] a constant-expression |
17031 | // So we need to make sure temporary objects are destroyed after having |
17032 | // evaluating the expression (per C++23 [class.temporary]/p4). |
17033 | FullExpressionRAII Scope(Info); |
17034 | if (!::EvaluateInPlace(Result&: Result.Val, Info, This: LVal, E: this) || |
17035 | Result.HasSideEffects || !Scope.destroy()) |
17036 | return false; |
17037 | |
17038 | if (!Info.discardCleanups()) |
17039 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
17040 | |
17041 | if (!CheckConstantExpression(Info, DiagLoc: getExprLoc(), Type: getStorageType(Ctx, E: this), |
17042 | Value: Result.Val, Kind)) |
17043 | return false; |
17044 | if (!CheckMemoryLeaks(Info)) |
17045 | return false; |
17046 | |
17047 | // If this is a class template argument, it's required to have constant |
17048 | // destruction too. |
17049 | if (Kind == ConstantExprKind::ClassTemplateArgument && |
17050 | (!EvaluateDestruction(Ctx, Base, Result.Val, T, getBeginLoc(), Result, |
17051 | true) || |
17052 | Result.HasSideEffects)) { |
17053 | // FIXME: Prefix a note to indicate that the problem is lack of constant |
17054 | // destruction. |
17055 | return false; |
17056 | } |
17057 | |
17058 | return true; |
17059 | } |
17060 | |
17061 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
17062 | const VarDecl *VD, |
17063 | SmallVectorImpl<PartialDiagnosticAt> &Notes, |
17064 | bool IsConstantInitialization) const { |
17065 | assert(!isValueDependent() && |
17066 | "Expression evaluator can't be called on a dependent expression."); |
17067 | |
17068 | llvm::TimeTraceScope TimeScope("EvaluateAsInitializer", [&] { |
17069 | std::string Name; |
17070 | llvm::raw_string_ostream OS(Name); |
17071 | VD->printQualifiedName(OS); |
17072 | return Name; |
17073 | }); |
17074 | |
17075 | Expr::EvalStatus EStatus; |
17076 | EStatus.Diag = &Notes; |
17077 | |
17078 | EvalInfo Info(Ctx, EStatus, |
17079 | (IsConstantInitialization && |
17080 | (Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23)) |
17081 | ? EvalInfo::EM_ConstantExpression |
17082 | : EvalInfo::EM_ConstantFold); |
17083 | Info.setEvaluatingDecl(VD, Value); |
17084 | Info.InConstantContext = IsConstantInitialization; |
17085 | |
17086 | SourceLocation DeclLoc = VD->getLocation(); |
17087 | QualType DeclTy = VD->getType(); |
17088 | |
17089 | if (Info.EnableNewConstInterp) { |
17090 | auto &InterpCtx = const_cast<ASTContext &>(Ctx).getInterpContext(); |
17091 | if (!InterpCtx.evaluateAsInitializer(Parent&: Info, VD, Result&: Value)) |
17092 | return false; |
17093 | |
17094 | return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value, |
17095 | Kind: ConstantExprKind::Normal); |
17096 | } else { |
17097 | LValue LVal; |
17098 | LVal.set(VD); |
17099 | |
17100 | { |
17101 | // C++23 [intro.execution]/p5 |
17102 | // A full-expression is ... an init-declarator ([dcl.decl]) or a |
17103 | // mem-initializer. |
17104 | // So we need to make sure temporary objects are destroyed after having |
17105 | // evaluated the expression (per C++23 [class.temporary]/p4). |
17106 | // |
17107 | // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the |
17108 | // serialization code calls ParmVarDecl::getDefaultArg() which strips the |
17109 | // outermost FullExpr, such as ExprWithCleanups. |
17110 | FullExpressionRAII Scope(Info); |
17111 | if (!EvaluateInPlace(Result&: Value, Info, This: LVal, E: this, |
17112 | /*AllowNonLiteralTypes=*/true) || |
17113 | EStatus.HasSideEffects) |
17114 | return false; |
17115 | } |
17116 | |
17117 | // At this point, any lifetime-extended temporaries are completely |
17118 | // initialized. |
17119 | Info.performLifetimeExtension(); |
17120 | |
17121 | if (!Info.discardCleanups()) |
17122 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
17123 | } |
17124 | |
17125 | return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value, |
17126 | Kind: ConstantExprKind::Normal) && |
17127 | CheckMemoryLeaks(Info); |
17128 | } |
17129 | |
17130 | bool VarDecl::evaluateDestruction( |
17131 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
17132 | Expr::EvalStatus EStatus; |
17133 | EStatus.Diag = &Notes; |
17134 | |
17135 | // Only treat the destruction as constant destruction if we formally have |
17136 | // constant initialization (or are usable in a constant expression). |
17137 | bool IsConstantDestruction = hasConstantInitialization(); |
17138 | |
17139 | // Make a copy of the value for the destructor to mutate, if we know it. |
17140 | // Otherwise, treat the value as default-initialized; if the destructor works |
17141 | // anyway, then the destruction is constant (and must be essentially empty). |
17142 | APValue DestroyedValue; |
17143 | if (getEvaluatedValue() && !getEvaluatedValue()->isAbsent()) |
17144 | DestroyedValue = *getEvaluatedValue(); |
17145 | else if (!handleDefaultInitValue(getType(), DestroyedValue)) |
17146 | return false; |
17147 | |
17148 | if (!EvaluateDestruction(getASTContext(), this, std::move(DestroyedValue), |
17149 | getType(), getLocation(), EStatus, |
17150 | IsConstantDestruction) || |
17151 | EStatus.HasSideEffects) |
17152 | return false; |
17153 | |
17154 | ensureEvaluatedStmt()->HasConstantDestruction = true; |
17155 | return true; |
17156 | } |
17157 | |
17158 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
17159 | /// constant folded, but discard the result. |
17160 | bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const { |
17161 | assert(!isValueDependent() && |
17162 | "Expression evaluator can't be called on a dependent expression."); |
17163 | |
17164 | EvalResult Result; |
17165 | return EvaluateAsRValue(Result, Ctx, /* in constant context */ InConstantContext: true) && |
17166 | !hasUnacceptableSideEffect(Result, SEK); |
17167 | } |
17168 | |
17169 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
17170 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
17171 | assert(!isValueDependent() && |
17172 | "Expression evaluator can't be called on a dependent expression."); |
17173 | |
17174 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstInt"); |
17175 | EvalResult EVResult; |
17176 | EVResult.Diag = Diag; |
17177 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
17178 | Info.InConstantContext = true; |
17179 | |
17180 | bool Result = ::EvaluateAsRValue(E: this, Result&: EVResult, Ctx, Info); |
17181 | (void)Result; |
17182 | assert(Result && "Could not evaluate expression"); |
17183 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
17184 | |
17185 | return EVResult.Val.getInt(); |
17186 | } |
17187 | |
17188 | APSInt Expr::EvaluateKnownConstIntCheckOverflow( |
17189 | const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
17190 | assert(!isValueDependent() && |
17191 | "Expression evaluator can't be called on a dependent expression."); |
17192 | |
17193 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstIntCheckOverflow"); |
17194 | EvalResult EVResult; |
17195 | EVResult.Diag = Diag; |
17196 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
17197 | Info.InConstantContext = true; |
17198 | Info.CheckingForUndefinedBehavior = true; |
17199 | |
17200 | bool Result = ::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val); |
17201 | (void)Result; |
17202 | assert(Result && "Could not evaluate expression"); |
17203 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
17204 | |
17205 | return EVResult.Val.getInt(); |
17206 | } |
17207 | |
17208 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
17209 | assert(!isValueDependent() && |
17210 | "Expression evaluator can't be called on a dependent expression."); |
17211 | |
17212 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow"); |
17213 | bool IsConst; |
17214 | EvalResult EVResult; |
17215 | if (!FastEvaluateAsRValue(Exp: this, Result&: EVResult, Ctx, IsConst)) { |
17216 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
17217 | Info.CheckingForUndefinedBehavior = true; |
17218 | (void)::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val); |
17219 | } |
17220 | } |
17221 | |
17222 | bool Expr::EvalResult::isGlobalLValue() const { |
17223 | assert(Val.isLValue()); |
17224 | return IsGlobalLValue(B: Val.getLValueBase()); |
17225 | } |
17226 | |
17227 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
17228 | /// an integer constant expression. |
17229 | |
17230 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
17231 | /// comma, etc |
17232 | |
17233 | // CheckICE - This function does the fundamental ICE checking: the returned |
17234 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
17235 | // and a (possibly null) SourceLocation indicating the location of the problem. |
17236 | // |
17237 | // Note that to reduce code duplication, this helper does no evaluation |
17238 | // itself; the caller checks whether the expression is evaluatable, and |
17239 | // in the rare cases where CheckICE actually cares about the evaluated |
17240 | // value, it calls into Evaluate. |
17241 | |
17242 | namespace { |
17243 | |
17244 | enum ICEKind { |
17245 | /// This expression is an ICE. |
17246 | IK_ICE, |
17247 | /// This expression is not an ICE, but if it isn't evaluated, it's |
17248 | /// a legal subexpression for an ICE. This return value is used to handle |
17249 | /// the comma operator in C99 mode, and non-constant subexpressions. |
17250 | IK_ICEIfUnevaluated, |
17251 | /// This expression is not an ICE, and is not a legal subexpression for one. |
17252 | IK_NotICE |
17253 | }; |
17254 | |
17255 | struct ICEDiag { |
17256 | ICEKind Kind; |
17257 | SourceLocation Loc; |
17258 | |
17259 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
17260 | }; |
17261 | |
17262 | } |
17263 | |
17264 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
17265 | |
17266 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
17267 | |
17268 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
17269 | Expr::EvalResult EVResult; |
17270 | Expr::EvalStatus Status; |
17271 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
17272 | |
17273 | Info.InConstantContext = true; |
17274 | if (!::EvaluateAsRValue(E, Result&: EVResult, Ctx, Info) || EVResult.HasSideEffects || |
17275 | !EVResult.Val.isInt()) |
17276 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17277 | |
17278 | return NoDiag(); |
17279 | } |
17280 | |
17281 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
17282 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
17283 | if (!E->getType()->isIntegralOrEnumerationType()) |
17284 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17285 | |
17286 | switch (E->getStmtClass()) { |
17287 | #define ABSTRACT_STMT(Node) |
17288 | #define STMT(Node, Base) case Expr::Node##Class: |
17289 | #define EXPR(Node, Base) |
17290 | #include "clang/AST/StmtNodes.inc" |
17291 | case Expr::PredefinedExprClass: |
17292 | case Expr::FloatingLiteralClass: |
17293 | case Expr::ImaginaryLiteralClass: |
17294 | case Expr::StringLiteralClass: |
17295 | case Expr::ArraySubscriptExprClass: |
17296 | case Expr::MatrixSubscriptExprClass: |
17297 | case Expr::ArraySectionExprClass: |
17298 | case Expr::OMPArrayShapingExprClass: |
17299 | case Expr::OMPIteratorExprClass: |
17300 | case Expr::MemberExprClass: |
17301 | case Expr::CompoundAssignOperatorClass: |
17302 | case Expr::CompoundLiteralExprClass: |
17303 | case Expr::ExtVectorElementExprClass: |
17304 | case Expr::DesignatedInitExprClass: |
17305 | case Expr::ArrayInitLoopExprClass: |
17306 | case Expr::ArrayInitIndexExprClass: |
17307 | case Expr::NoInitExprClass: |
17308 | case Expr::DesignatedInitUpdateExprClass: |
17309 | case Expr::ImplicitValueInitExprClass: |
17310 | case Expr::ParenListExprClass: |
17311 | case Expr::VAArgExprClass: |
17312 | case Expr::AddrLabelExprClass: |
17313 | case Expr::StmtExprClass: |
17314 | case Expr::CXXMemberCallExprClass: |
17315 | case Expr::CUDAKernelCallExprClass: |
17316 | case Expr::CXXAddrspaceCastExprClass: |
17317 | case Expr::CXXDynamicCastExprClass: |
17318 | case Expr::CXXTypeidExprClass: |
17319 | case Expr::CXXUuidofExprClass: |
17320 | case Expr::MSPropertyRefExprClass: |
17321 | case Expr::MSPropertySubscriptExprClass: |
17322 | case Expr::CXXNullPtrLiteralExprClass: |
17323 | case Expr::UserDefinedLiteralClass: |
17324 | case Expr::CXXThisExprClass: |
17325 | case Expr::CXXThrowExprClass: |
17326 | case Expr::CXXNewExprClass: |
17327 | case Expr::CXXDeleteExprClass: |
17328 | case Expr::CXXPseudoDestructorExprClass: |
17329 | case Expr::UnresolvedLookupExprClass: |
17330 | case Expr::TypoExprClass: |
17331 | case Expr::RecoveryExprClass: |
17332 | case Expr::DependentScopeDeclRefExprClass: |
17333 | case Expr::CXXConstructExprClass: |
17334 | case Expr::CXXInheritedCtorInitExprClass: |
17335 | case Expr::CXXStdInitializerListExprClass: |
17336 | case Expr::CXXBindTemporaryExprClass: |
17337 | case Expr::ExprWithCleanupsClass: |
17338 | case Expr::CXXTemporaryObjectExprClass: |
17339 | case Expr::CXXUnresolvedConstructExprClass: |
17340 | case Expr::CXXDependentScopeMemberExprClass: |
17341 | case Expr::UnresolvedMemberExprClass: |
17342 | case Expr::ObjCStringLiteralClass: |
17343 | case Expr::ObjCBoxedExprClass: |
17344 | case Expr::ObjCArrayLiteralClass: |
17345 | case Expr::ObjCDictionaryLiteralClass: |
17346 | case Expr::ObjCEncodeExprClass: |
17347 | case Expr::ObjCMessageExprClass: |
17348 | case Expr::ObjCSelectorExprClass: |
17349 | case Expr::ObjCProtocolExprClass: |
17350 | case Expr::ObjCIvarRefExprClass: |
17351 | case Expr::ObjCPropertyRefExprClass: |
17352 | case Expr::ObjCSubscriptRefExprClass: |
17353 | case Expr::ObjCIsaExprClass: |
17354 | case Expr::ObjCAvailabilityCheckExprClass: |
17355 | case Expr::ShuffleVectorExprClass: |
17356 | case Expr::ConvertVectorExprClass: |
17357 | case Expr::BlockExprClass: |
17358 | case Expr::NoStmtClass: |
17359 | case Expr::OpaqueValueExprClass: |
17360 | case Expr::PackExpansionExprClass: |
17361 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
17362 | case Expr::FunctionParmPackExprClass: |
17363 | case Expr::AsTypeExprClass: |
17364 | case Expr::ObjCIndirectCopyRestoreExprClass: |
17365 | case Expr::MaterializeTemporaryExprClass: |
17366 | case Expr::PseudoObjectExprClass: |
17367 | case Expr::AtomicExprClass: |
17368 | case Expr::LambdaExprClass: |
17369 | case Expr::CXXFoldExprClass: |
17370 | case Expr::CoawaitExprClass: |
17371 | case Expr::DependentCoawaitExprClass: |
17372 | case Expr::CoyieldExprClass: |
17373 | case Expr::SYCLUniqueStableNameExprClass: |
17374 | case Expr::CXXParenListInitExprClass: |
17375 | case Expr::HLSLOutArgExprClass: |
17376 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17377 | |
17378 | case Expr::InitListExprClass: { |
17379 | // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the |
17380 | // form "T x = { a };" is equivalent to "T x = a;". |
17381 | // Unless we're initializing a reference, T is a scalar as it is known to be |
17382 | // of integral or enumeration type. |
17383 | if (E->isPRValue()) |
17384 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
17385 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
17386 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17387 | } |
17388 | |
17389 | case Expr::SizeOfPackExprClass: |
17390 | case Expr::GNUNullExprClass: |
17391 | case Expr::SourceLocExprClass: |
17392 | case Expr::EmbedExprClass: |
17393 | case Expr::OpenACCAsteriskSizeExprClass: |
17394 | return NoDiag(); |
17395 | |
17396 | case Expr::PackIndexingExprClass: |
17397 | return CheckICE(cast<PackIndexingExpr>(E)->getSelectedExpr(), Ctx); |
17398 | |
17399 | case Expr::SubstNonTypeTemplateParmExprClass: |
17400 | return |
17401 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
17402 | |
17403 | case Expr::ConstantExprClass: |
17404 | return CheckICE(cast<ConstantExpr>(E)->getSubExpr(), Ctx); |
17405 | |
17406 | case Expr::ParenExprClass: |
17407 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
17408 | case Expr::GenericSelectionExprClass: |
17409 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
17410 | case Expr::IntegerLiteralClass: |
17411 | case Expr::FixedPointLiteralClass: |
17412 | case Expr::CharacterLiteralClass: |
17413 | case Expr::ObjCBoolLiteralExprClass: |
17414 | case Expr::CXXBoolLiteralExprClass: |
17415 | case Expr::CXXScalarValueInitExprClass: |
17416 | case Expr::TypeTraitExprClass: |
17417 | case Expr::ConceptSpecializationExprClass: |
17418 | case Expr::RequiresExprClass: |
17419 | case Expr::ArrayTypeTraitExprClass: |
17420 | case Expr::ExpressionTraitExprClass: |
17421 | case Expr::CXXNoexceptExprClass: |
17422 | return NoDiag(); |
17423 | case Expr::CallExprClass: |
17424 | case Expr::CXXOperatorCallExprClass: { |
17425 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
17426 | // constant expressions, but they can never be ICEs because an ICE cannot |
17427 | // contain an operand of (pointer to) function type. |
17428 | const CallExpr *CE = cast<CallExpr>(E); |
17429 | if (CE->getBuiltinCallee()) |
17430 | return CheckEvalInICE(E, Ctx); |
17431 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17432 | } |
17433 | case Expr::CXXRewrittenBinaryOperatorClass: |
17434 | return CheckICE(cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm(), |
17435 | Ctx); |
17436 | case Expr::DeclRefExprClass: { |
17437 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
17438 | if (isa<EnumConstantDecl>(D)) |
17439 | return NoDiag(); |
17440 | |
17441 | // C++ and OpenCL (FIXME: spec reference?) allow reading const-qualified |
17442 | // integer variables in constant expressions: |
17443 | // |
17444 | // C++ 7.1.5.1p2 |
17445 | // A variable of non-volatile const-qualified integral or enumeration |
17446 | // type initialized by an ICE can be used in ICEs. |
17447 | // |
17448 | // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In |
17449 | // that mode, use of reference variables should not be allowed. |
17450 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
17451 | if (VD && VD->isUsableInConstantExpressions(C: Ctx) && |
17452 | !VD->getType()->isReferenceType()) |
17453 | return NoDiag(); |
17454 | |
17455 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17456 | } |
17457 | case Expr::UnaryOperatorClass: { |
17458 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
17459 | switch (Exp->getOpcode()) { |
17460 | case UO_PostInc: |
17461 | case UO_PostDec: |
17462 | case UO_PreInc: |
17463 | case UO_PreDec: |
17464 | case UO_AddrOf: |
17465 | case UO_Deref: |
17466 | case UO_Coawait: |
17467 | // C99 6.6/3 allows increment and decrement within unevaluated |
17468 | // subexpressions of constant expressions, but they can never be ICEs |
17469 | // because an ICE cannot contain an lvalue operand. |
17470 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17471 | case UO_Extension: |
17472 | case UO_LNot: |
17473 | case UO_Plus: |
17474 | case UO_Minus: |
17475 | case UO_Not: |
17476 | case UO_Real: |
17477 | case UO_Imag: |
17478 | return CheckICE(E: Exp->getSubExpr(), Ctx); |
17479 | } |
17480 | llvm_unreachable("invalid unary operator class"); |
17481 | } |
17482 | case Expr::OffsetOfExprClass: { |
17483 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
17484 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
17485 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
17486 | // compliance: we should warn earlier for offsetof expressions with |
17487 | // array subscripts that aren't ICEs, and if the array subscripts |
17488 | // are ICEs, the value of the offsetof must be an integer constant. |
17489 | return CheckEvalInICE(E, Ctx); |
17490 | } |
17491 | case Expr::UnaryExprOrTypeTraitExprClass: { |
17492 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
17493 | if ((Exp->getKind() == UETT_SizeOf) && |
17494 | Exp->getTypeOfArgument()->isVariableArrayType()) |
17495 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17496 | if (Exp->getKind() == UETT_CountOf) { |
17497 | QualType ArgTy = Exp->getTypeOfArgument(); |
17498 | if (ArgTy->isVariableArrayType()) { |
17499 | // We need to look whether the array is multidimensional. If it is, |
17500 | // then we want to check the size expression manually to see whether |
17501 | // it is an ICE or not. |
17502 | const auto *VAT = Ctx.getAsVariableArrayType(T: ArgTy); |
17503 | if (VAT->getElementType()->isArrayType()) |
17504 | return CheckICE(VAT->getSizeExpr(), Ctx); |
17505 | |
17506 | // Otherwise, this is a regular VLA, which is definitely not an ICE. |
17507 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17508 | } |
17509 | } |
17510 | return NoDiag(); |
17511 | } |
17512 | case Expr::BinaryOperatorClass: { |
17513 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
17514 | switch (Exp->getOpcode()) { |
17515 | case BO_PtrMemD: |
17516 | case BO_PtrMemI: |
17517 | case BO_Assign: |
17518 | case BO_MulAssign: |
17519 | case BO_DivAssign: |
17520 | case BO_RemAssign: |
17521 | case BO_AddAssign: |
17522 | case BO_SubAssign: |
17523 | case BO_ShlAssign: |
17524 | case BO_ShrAssign: |
17525 | case BO_AndAssign: |
17526 | case BO_XorAssign: |
17527 | case BO_OrAssign: |
17528 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
17529 | // constant expressions, but they can never be ICEs because an ICE cannot |
17530 | // contain an lvalue operand. |
17531 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17532 | |
17533 | case BO_Mul: |
17534 | case BO_Div: |
17535 | case BO_Rem: |
17536 | case BO_Add: |
17537 | case BO_Sub: |
17538 | case BO_Shl: |
17539 | case BO_Shr: |
17540 | case BO_LT: |
17541 | case BO_GT: |
17542 | case BO_LE: |
17543 | case BO_GE: |
17544 | case BO_EQ: |
17545 | case BO_NE: |
17546 | case BO_And: |
17547 | case BO_Xor: |
17548 | case BO_Or: |
17549 | case BO_Comma: |
17550 | case BO_Cmp: { |
17551 | ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx); |
17552 | ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx); |
17553 | if (Exp->getOpcode() == BO_Div || |
17554 | Exp->getOpcode() == BO_Rem) { |
17555 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
17556 | // we don't evaluate one. |
17557 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
17558 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
17559 | if (REval == 0) |
17560 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
17561 | if (REval.isSigned() && REval.isAllOnes()) { |
17562 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
17563 | if (LEval.isMinSignedValue()) |
17564 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
17565 | } |
17566 | } |
17567 | } |
17568 | if (Exp->getOpcode() == BO_Comma) { |
17569 | if (Ctx.getLangOpts().C99) { |
17570 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
17571 | // if it isn't evaluated. |
17572 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
17573 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
17574 | } else { |
17575 | // In both C89 and C++, commas in ICEs are illegal. |
17576 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17577 | } |
17578 | } |
17579 | return Worst(A: LHSResult, B: RHSResult); |
17580 | } |
17581 | case BO_LAnd: |
17582 | case BO_LOr: { |
17583 | ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx); |
17584 | ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx); |
17585 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
17586 | // Rare case where the RHS has a comma "side-effect"; we need |
17587 | // to actually check the condition to see whether the side |
17588 | // with the comma is evaluated. |
17589 | if ((Exp->getOpcode() == BO_LAnd) != |
17590 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
17591 | return RHSResult; |
17592 | return NoDiag(); |
17593 | } |
17594 | |
17595 | return Worst(A: LHSResult, B: RHSResult); |
17596 | } |
17597 | } |
17598 | llvm_unreachable("invalid binary operator kind"); |
17599 | } |
17600 | case Expr::ImplicitCastExprClass: |
17601 | case Expr::CStyleCastExprClass: |
17602 | case Expr::CXXFunctionalCastExprClass: |
17603 | case Expr::CXXStaticCastExprClass: |
17604 | case Expr::CXXReinterpretCastExprClass: |
17605 | case Expr::CXXConstCastExprClass: |
17606 | case Expr::ObjCBridgedCastExprClass: { |
17607 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
17608 | if (isa<ExplicitCastExpr>(E)) { |
17609 | if (const FloatingLiteral *FL |
17610 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
17611 | unsigned DestWidth = Ctx.getIntWidth(T: E->getType()); |
17612 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
17613 | APSInt IgnoredVal(DestWidth, !DestSigned); |
17614 | bool Ignored; |
17615 | // If the value does not fit in the destination type, the behavior is |
17616 | // undefined, so we are not required to treat it as a constant |
17617 | // expression. |
17618 | if (FL->getValue().convertToInteger(Result&: IgnoredVal, |
17619 | RM: llvm::APFloat::rmTowardZero, |
17620 | IsExact: &Ignored) & APFloat::opInvalidOp) |
17621 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17622 | return NoDiag(); |
17623 | } |
17624 | } |
17625 | switch (cast<CastExpr>(E)->getCastKind()) { |
17626 | case CK_LValueToRValue: |
17627 | case CK_AtomicToNonAtomic: |
17628 | case CK_NonAtomicToAtomic: |
17629 | case CK_NoOp: |
17630 | case CK_IntegralToBoolean: |
17631 | case CK_IntegralCast: |
17632 | return CheckICE(E: SubExpr, Ctx); |
17633 | default: |
17634 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17635 | } |
17636 | } |
17637 | case Expr::BinaryConditionalOperatorClass: { |
17638 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
17639 | ICEDiag CommonResult = CheckICE(E: Exp->getCommon(), Ctx); |
17640 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
17641 | ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx); |
17642 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
17643 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
17644 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
17645 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
17646 | return FalseResult; |
17647 | } |
17648 | case Expr::ConditionalOperatorClass: { |
17649 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
17650 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
17651 | // then only the true side is actually considered in an integer constant |
17652 | // expression, and it is fully evaluated. This is an important GNU |
17653 | // extension. See GCC PR38377 for discussion. |
17654 | if (const CallExpr *CallCE |
17655 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
17656 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
17657 | return CheckEvalInICE(E, Ctx); |
17658 | ICEDiag CondResult = CheckICE(E: Exp->getCond(), Ctx); |
17659 | if (CondResult.Kind == IK_NotICE) |
17660 | return CondResult; |
17661 | |
17662 | ICEDiag TrueResult = CheckICE(E: Exp->getTrueExpr(), Ctx); |
17663 | ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx); |
17664 | |
17665 | if (TrueResult.Kind == IK_NotICE) |
17666 | return TrueResult; |
17667 | if (FalseResult.Kind == IK_NotICE) |
17668 | return FalseResult; |
17669 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
17670 | return CondResult; |
17671 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
17672 | return NoDiag(); |
17673 | // Rare case where the diagnostics depend on which side is evaluated |
17674 | // Note that if we get here, CondResult is 0, and at least one of |
17675 | // TrueResult and FalseResult is non-zero. |
17676 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
17677 | return FalseResult; |
17678 | return TrueResult; |
17679 | } |
17680 | case Expr::CXXDefaultArgExprClass: |
17681 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
17682 | case Expr::CXXDefaultInitExprClass: |
17683 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
17684 | case Expr::ChooseExprClass: { |
17685 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
17686 | } |
17687 | case Expr::BuiltinBitCastExprClass: { |
17688 | if (!checkBitCastConstexprEligibility(nullptr, Ctx, cast<CastExpr>(E))) |
17689 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
17690 | return CheckICE(cast<CastExpr>(E)->getSubExpr(), Ctx); |
17691 | } |
17692 | } |
17693 | |
17694 | llvm_unreachable("Invalid StmtClass!"); |
17695 | } |
17696 | |
17697 | /// Evaluate an expression as a C++11 integral constant expression. |
17698 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
17699 | const Expr *E, |
17700 | llvm::APSInt *Value, |
17701 | SourceLocation *Loc) { |
17702 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
17703 | if (Loc) *Loc = E->getExprLoc(); |
17704 | return false; |
17705 | } |
17706 | |
17707 | APValue Result; |
17708 | if (!E->isCXX11ConstantExpr(Ctx, Result: &Result, Loc)) |
17709 | return false; |
17710 | |
17711 | if (!Result.isInt()) { |
17712 | if (Loc) *Loc = E->getExprLoc(); |
17713 | return false; |
17714 | } |
17715 | |
17716 | if (Value) *Value = Result.getInt(); |
17717 | return true; |
17718 | } |
17719 | |
17720 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
17721 | SourceLocation *Loc) const { |
17722 | assert(!isValueDependent() && |
17723 | "Expression evaluator can't be called on a dependent expression."); |
17724 | |
17725 | ExprTimeTraceScope TimeScope(this, Ctx, "isIntegerConstantExpr"); |
17726 | |
17727 | if (Ctx.getLangOpts().CPlusPlus11) |
17728 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: nullptr, Loc); |
17729 | |
17730 | ICEDiag D = CheckICE(E: this, Ctx); |
17731 | if (D.Kind != IK_ICE) { |
17732 | if (Loc) *Loc = D.Loc; |
17733 | return false; |
17734 | } |
17735 | return true; |
17736 | } |
17737 | |
17738 | std::optional<llvm::APSInt> |
17739 | Expr::getIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc) const { |
17740 | if (isValueDependent()) { |
17741 | // Expression evaluator can't succeed on a dependent expression. |
17742 | return std::nullopt; |
17743 | } |
17744 | |
17745 | APSInt Value; |
17746 | |
17747 | if (Ctx.getLangOpts().CPlusPlus11) { |
17748 | if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: &Value, Loc)) |
17749 | return Value; |
17750 | return std::nullopt; |
17751 | } |
17752 | |
17753 | if (!isIntegerConstantExpr(Ctx, Loc)) |
17754 | return std::nullopt; |
17755 | |
17756 | // The only possible side-effects here are due to UB discovered in the |
17757 | // evaluation (for instance, INT_MAX + 1). In such a case, we are still |
17758 | // required to treat the expression as an ICE, so we produce the folded |
17759 | // value. |
17760 | EvalResult ExprResult; |
17761 | Expr::EvalStatus Status; |
17762 | EvalInfo Info(Ctx, Status, EvalInfo::EM_IgnoreSideEffects); |
17763 | Info.InConstantContext = true; |
17764 | |
17765 | if (!::EvaluateAsInt(E: this, ExprResult, Ctx, AllowSideEffects: SE_AllowSideEffects, Info)) |
17766 | llvm_unreachable("ICE cannot be evaluated!"); |
17767 | |
17768 | return ExprResult.Val.getInt(); |
17769 | } |
17770 | |
17771 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
17772 | assert(!isValueDependent() && |
17773 | "Expression evaluator can't be called on a dependent expression."); |
17774 | |
17775 | return CheckICE(E: this, Ctx).Kind == IK_ICE; |
17776 | } |
17777 | |
17778 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
17779 | SourceLocation *Loc) const { |
17780 | assert(!isValueDependent() && |
17781 | "Expression evaluator can't be called on a dependent expression."); |
17782 | |
17783 | // We support this checking in C++98 mode in order to diagnose compatibility |
17784 | // issues. |
17785 | assert(Ctx.getLangOpts().CPlusPlus); |
17786 | |
17787 | // Build evaluation settings. |
17788 | Expr::EvalStatus Status; |
17789 | SmallVector<PartialDiagnosticAt, 8> Diags; |
17790 | Status.Diag = &Diags; |
17791 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
17792 | |
17793 | APValue Scratch; |
17794 | bool IsConstExpr = |
17795 | ::EvaluateAsRValue(Info, E: this, Result&: Result ? *Result : Scratch) && |
17796 | // FIXME: We don't produce a diagnostic for this, but the callers that |
17797 | // call us on arbitrary full-expressions should generally not care. |
17798 | Info.discardCleanups() && !Status.HasSideEffects; |
17799 | |
17800 | if (!Diags.empty()) { |
17801 | IsConstExpr = false; |
17802 | if (Loc) *Loc = Diags[0].first; |
17803 | } else if (!IsConstExpr) { |
17804 | // FIXME: This shouldn't happen. |
17805 | if (Loc) *Loc = getExprLoc(); |
17806 | } |
17807 | |
17808 | return IsConstExpr; |
17809 | } |
17810 | |
17811 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
17812 | const FunctionDecl *Callee, |
17813 | ArrayRef<const Expr*> Args, |
17814 | const Expr *This) const { |
17815 | assert(!isValueDependent() && |
17816 | "Expression evaluator can't be called on a dependent expression."); |
17817 | |
17818 | llvm::TimeTraceScope TimeScope("EvaluateWithSubstitution", [&] { |
17819 | std::string Name; |
17820 | llvm::raw_string_ostream OS(Name); |
17821 | Callee->getNameForDiagnostic(OS, Policy: Ctx.getPrintingPolicy(), |
17822 | /*Qualified=*/true); |
17823 | return Name; |
17824 | }); |
17825 | |
17826 | Expr::EvalStatus Status; |
17827 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
17828 | Info.InConstantContext = true; |
17829 | |
17830 | LValue ThisVal; |
17831 | const LValue *ThisPtr = nullptr; |
17832 | if (This) { |
17833 | #ifndef NDEBUG |
17834 | auto *MD = dyn_cast<CXXMethodDecl>(Val: Callee); |
17835 | assert(MD && "Don't provide `this` for non-methods."); |
17836 | assert(MD->isImplicitObjectMemberFunction() && |
17837 | "Don't provide `this` for methods without an implicit object."); |
17838 | #endif |
17839 | if (!This->isValueDependent() && |
17840 | EvaluateObjectArgument(Info, Object: This, This&: ThisVal) && |
17841 | !Info.EvalStatus.HasSideEffects) |
17842 | ThisPtr = &ThisVal; |
17843 | |
17844 | // Ignore any side-effects from a failed evaluation. This is safe because |
17845 | // they can't interfere with any other argument evaluation. |
17846 | Info.EvalStatus.HasSideEffects = false; |
17847 | } |
17848 | |
17849 | CallRef Call = Info.CurrentCall->createCall(Callee); |
17850 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
17851 | I != E; ++I) { |
17852 | unsigned Idx = I - Args.begin(); |
17853 | if (Idx >= Callee->getNumParams()) |
17854 | break; |
17855 | const ParmVarDecl *PVD = Callee->getParamDecl(i: Idx); |
17856 | if ((*I)->isValueDependent() || |
17857 | !EvaluateCallArg(PVD, Arg: *I, Call, Info) || |
17858 | Info.EvalStatus.HasSideEffects) { |
17859 | // If evaluation fails, throw away the argument entirely. |
17860 | if (APValue *Slot = Info.getParamSlot(Call, PVD)) |
17861 | *Slot = APValue(); |
17862 | } |
17863 | |
17864 | // Ignore any side-effects from a failed evaluation. This is safe because |
17865 | // they can't interfere with any other argument evaluation. |
17866 | Info.EvalStatus.HasSideEffects = false; |
17867 | } |
17868 | |
17869 | // Parameter cleanups happen in the caller and are not part of this |
17870 | // evaluation. |
17871 | Info.discardCleanups(); |
17872 | Info.EvalStatus.HasSideEffects = false; |
17873 | |
17874 | // Build fake call to Callee. |
17875 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, This, |
17876 | Call); |
17877 | // FIXME: Missing ExprWithCleanups in enable_if conditions? |
17878 | FullExpressionRAII Scope(Info); |
17879 | return Evaluate(Result&: Value, Info, E: this) && Scope.destroy() && |
17880 | !Info.EvalStatus.HasSideEffects; |
17881 | } |
17882 | |
17883 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
17884 | SmallVectorImpl< |
17885 | PartialDiagnosticAt> &Diags) { |
17886 | // FIXME: It would be useful to check constexpr function templates, but at the |
17887 | // moment the constant expression evaluator cannot cope with the non-rigorous |
17888 | // ASTs which we build for dependent expressions. |
17889 | if (FD->isDependentContext()) |
17890 | return true; |
17891 | |
17892 | llvm::TimeTraceScope TimeScope("isPotentialConstantExpr", [&] { |
17893 | std::string Name; |
17894 | llvm::raw_string_ostream OS(Name); |
17895 | FD->getNameForDiagnostic(OS, Policy: FD->getASTContext().getPrintingPolicy(), |
17896 | /*Qualified=*/true); |
17897 | return Name; |
17898 | }); |
17899 | |
17900 | Expr::EvalStatus Status; |
17901 | Status.Diag = &Diags; |
17902 | |
17903 | EvalInfo Info(FD->getASTContext(), Status, EvalInfo::EM_ConstantExpression); |
17904 | Info.InConstantContext = true; |
17905 | Info.CheckingPotentialConstantExpression = true; |
17906 | |
17907 | // The constexpr VM attempts to compile all methods to bytecode here. |
17908 | if (Info.EnableNewConstInterp) { |
17909 | Info.Ctx.getInterpContext().isPotentialConstantExpr(Parent&: Info, FnDecl: FD); |
17910 | return Diags.empty(); |
17911 | } |
17912 | |
17913 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD); |
17914 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
17915 | |
17916 | // Fabricate an arbitrary expression on the stack and pretend that it |
17917 | // is a temporary being used as the 'this' pointer. |
17918 | LValue This; |
17919 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
17920 | This.set({&VIE, Info.CurrentCall->Index}); |
17921 | |
17922 | ArrayRef<const Expr*> Args; |
17923 | |
17924 | APValue Scratch; |
17925 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Val: FD)) { |
17926 | // Evaluate the call as a constant initializer, to allow the construction |
17927 | // of objects of non-literal types. |
17928 | Info.setEvaluatingDecl(Base: This.getLValueBase(), Value&: Scratch); |
17929 | HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch); |
17930 | } else { |
17931 | SourceLocation Loc = FD->getLocation(); |
17932 | HandleFunctionCall( |
17933 | Loc, FD, (MD && MD->isImplicitObjectMemberFunction()) ? &This : nullptr, |
17934 | &VIE, Args, CallRef(), FD->getBody(), Info, Scratch, |
17935 | /*ResultSlot=*/nullptr); |
17936 | } |
17937 | |
17938 | return Diags.empty(); |
17939 | } |
17940 | |
17941 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
17942 | const FunctionDecl *FD, |
17943 | SmallVectorImpl< |
17944 | PartialDiagnosticAt> &Diags) { |
17945 | assert(!E->isValueDependent() && |
17946 | "Expression evaluator can't be called on a dependent expression."); |
17947 | |
17948 | Expr::EvalStatus Status; |
17949 | Status.Diag = &Diags; |
17950 | |
17951 | EvalInfo Info(FD->getASTContext(), Status, |
17952 | EvalInfo::EM_ConstantExpressionUnevaluated); |
17953 | Info.InConstantContext = true; |
17954 | Info.CheckingPotentialConstantExpression = true; |
17955 | |
17956 | // Fabricate a call stack frame to give the arguments a plausible cover story. |
17957 | CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr, |
17958 | /*CallExpr=*/nullptr, CallRef()); |
17959 | |
17960 | APValue ResultScratch; |
17961 | Evaluate(Result&: ResultScratch, Info, E); |
17962 | return Diags.empty(); |
17963 | } |
17964 | |
17965 | bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
17966 | unsigned Type) const { |
17967 | if (!getType()->isPointerType()) |
17968 | return false; |
17969 | |
17970 | Expr::EvalStatus Status; |
17971 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
17972 | return tryEvaluateBuiltinObjectSize(E: this, Type, Info, Size&: Result); |
17973 | } |
17974 | |
17975 | static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result, |
17976 | EvalInfo &Info, std::string *StringResult) { |
17977 | if (!E->getType()->hasPointerRepresentation() || !E->isPRValue()) |
17978 | return false; |
17979 | |
17980 | LValue String; |
17981 | |
17982 | if (!EvaluatePointer(E, Result&: String, Info)) |
17983 | return false; |
17984 | |
17985 | QualType CharTy = E->getType()->getPointeeType(); |
17986 | |
17987 | // Fast path: if it's a string literal, search the string value. |
17988 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
17989 | Val: String.getLValueBase().dyn_cast<const Expr *>())) { |
17990 | StringRef Str = S->getBytes(); |
17991 | int64_t Off = String.Offset.getQuantity(); |
17992 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
17993 | S->getCharByteWidth() == 1 && |
17994 | // FIXME: Add fast-path for wchar_t too. |
17995 | Info.Ctx.hasSameUnqualifiedType(T1: CharTy, T2: Info.Ctx.CharTy)) { |
17996 | Str = Str.substr(Start: Off); |
17997 | |
17998 | StringRef::size_type Pos = Str.find(C: 0); |
17999 | if (Pos != StringRef::npos) |
18000 | Str = Str.substr(Start: 0, N: Pos); |
18001 | |
18002 | Result = Str.size(); |
18003 | if (StringResult) |
18004 | *StringResult = Str; |
18005 | return true; |
18006 | } |
18007 | |
18008 | // Fall through to slow path. |
18009 | } |
18010 | |
18011 | // Slow path: scan the bytes of the string looking for the terminating 0. |
18012 | for (uint64_t Strlen = 0; /**/; ++Strlen) { |
18013 | APValue Char; |
18014 | if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: String, RVal&: Char) || |
18015 | !Char.isInt()) |
18016 | return false; |
18017 | if (!Char.getInt()) { |
18018 | Result = Strlen; |
18019 | return true; |
18020 | } else if (StringResult) |
18021 | StringResult->push_back(c: Char.getInt().getExtValue()); |
18022 | if (!HandleLValueArrayAdjustment(Info, E, LVal&: String, EltTy: CharTy, Adjustment: 1)) |
18023 | return false; |
18024 | } |
18025 | } |
18026 | |
18027 | std::optional<std::string> Expr::tryEvaluateString(ASTContext &Ctx) const { |
18028 | Expr::EvalStatus Status; |
18029 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
18030 | uint64_t Result; |
18031 | std::string StringResult; |
18032 | |
18033 | if (EvaluateBuiltinStrLen(E: this, Result, Info, StringResult: &StringResult)) |
18034 | return StringResult; |
18035 | return {}; |
18036 | } |
18037 | |
18038 | template <typename T> |
18039 | static bool EvaluateCharRangeAsStringImpl(const Expr *, T &Result, |
18040 | const Expr *SizeExpression, |
18041 | const Expr *PtrExpression, |
18042 | ASTContext &Ctx, |
18043 | Expr::EvalResult &Status) { |
18044 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
18045 | Info.InConstantContext = true; |
18046 | |
18047 | if (Info.EnableNewConstInterp) |
18048 | return Info.Ctx.getInterpContext().evaluateCharRange(Info, SizeExpression, |
18049 | PtrExpression, Result); |
18050 | |
18051 | LValue String; |
18052 | FullExpressionRAII Scope(Info); |
18053 | APSInt SizeValue; |
18054 | if (!::EvaluateInteger(E: SizeExpression, Result&: SizeValue, Info)) |
18055 | return false; |
18056 | |
18057 | uint64_t Size = SizeValue.getZExtValue(); |
18058 | |
18059 | // FIXME: better protect against invalid or excessive sizes |
18060 | if constexpr (std::is_same_v<APValue, T>) |
18061 | Result = APValue(APValue::UninitArray{}, Size, Size); |
18062 | else { |
18063 | if (Size < Result.max_size()) |
18064 | Result.reserve(Size); |
18065 | } |
18066 | if (!::EvaluatePointer(E: PtrExpression, Result&: String, Info)) |
18067 | return false; |
18068 | |
18069 | QualType CharTy = PtrExpression->getType()->getPointeeType(); |
18070 | for (uint64_t I = 0; I < Size; ++I) { |
18071 | APValue Char; |
18072 | if (!handleLValueToRValueConversion(Info, Conv: PtrExpression, Type: CharTy, LVal: String, |
18073 | RVal&: Char)) |
18074 | return false; |
18075 | |
18076 | if constexpr (std::is_same_v<APValue, T>) { |
18077 | Result.getArrayInitializedElt(I) = std::move(Char); |
18078 | } else { |
18079 | APSInt C = Char.getInt(); |
18080 | |
18081 | assert(C.getBitWidth() <= 8 && |
18082 | "string element not representable in char"); |
18083 | |
18084 | Result.push_back(static_cast<char>(C.getExtValue())); |
18085 | } |
18086 | |
18087 | if (!HandleLValueArrayAdjustment(Info, E: PtrExpression, LVal&: String, EltTy: CharTy, Adjustment: 1)) |
18088 | return false; |
18089 | } |
18090 | |
18091 | return Scope.destroy() && CheckMemoryLeaks(Info); |
18092 | } |
18093 | |
18094 | bool Expr::EvaluateCharRangeAsString(std::string &Result, |
18095 | const Expr *SizeExpression, |
18096 | const Expr *PtrExpression, ASTContext &Ctx, |
18097 | EvalResult &Status) const { |
18098 | return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression, |
18099 | PtrExpression, Ctx, Status); |
18100 | } |
18101 | |
18102 | bool Expr::EvaluateCharRangeAsString(APValue &Result, |
18103 | const Expr *SizeExpression, |
18104 | const Expr *PtrExpression, ASTContext &Ctx, |
18105 | EvalResult &Status) const { |
18106 | return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression, |
18107 | PtrExpression, Ctx, Status); |
18108 | } |
18109 | |
18110 | bool Expr::tryEvaluateStrLen(uint64_t &Result, ASTContext &Ctx) const { |
18111 | Expr::EvalStatus Status; |
18112 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
18113 | return EvaluateBuiltinStrLen(E: this, Result, Info); |
18114 | } |
18115 | |
18116 | namespace { |
18117 | struct IsWithinLifetimeHandler { |
18118 | EvalInfo &Info; |
18119 | static constexpr AccessKinds AccessKind = AccessKinds::AK_IsWithinLifetime; |
18120 | using result_type = std::optional<bool>; |
18121 | std::optional<bool> failed() { return std::nullopt; } |
18122 | template <typename T> |
18123 | std::optional<bool> found(T &Subobj, QualType SubobjType) { |
18124 | return true; |
18125 | } |
18126 | }; |
18127 | |
18128 | std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &IEE, |
18129 | const CallExpr *E) { |
18130 | EvalInfo &Info = IEE.Info; |
18131 | // Sometimes this is called during some sorts of constant folding / early |
18132 | // evaluation. These are meant for non-constant expressions and are not |
18133 | // necessary since this consteval builtin will never be evaluated at runtime. |
18134 | // Just fail to evaluate when not in a constant context. |
18135 | if (!Info.InConstantContext) |
18136 | return std::nullopt; |
18137 | assert(E->getBuiltinCallee() == Builtin::BI__builtin_is_within_lifetime); |
18138 | const Expr *Arg = E->getArg(Arg: 0); |
18139 | if (Arg->isValueDependent()) |
18140 | return std::nullopt; |
18141 | LValue Val; |
18142 | if (!EvaluatePointer(E: Arg, Result&: Val, Info)) |
18143 | return std::nullopt; |
18144 | |
18145 | if (Val.allowConstexprUnknown()) |
18146 | return true; |
18147 | |
18148 | auto Error = [&](int Diag) { |
18149 | bool CalledFromStd = false; |
18150 | const auto *Callee = Info.CurrentCall->getCallee(); |
18151 | if (Callee && Callee->isInStdNamespace()) { |
18152 | const IdentifierInfo *Identifier = Callee->getIdentifier(); |
18153 | CalledFromStd = Identifier && Identifier->isStr(Str: "is_within_lifetime"); |
18154 | } |
18155 | Info.CCEDiag(CalledFromStd ? Info.CurrentCall->getCallRange().getBegin() |
18156 | : E->getExprLoc(), |
18157 | diag::err_invalid_is_within_lifetime) |
18158 | << (CalledFromStd ? "std::is_within_lifetime" |
18159 | : "__builtin_is_within_lifetime") |
18160 | << Diag; |
18161 | return std::nullopt; |
18162 | }; |
18163 | // C++2c [meta.const.eval]p4: |
18164 | // During the evaluation of an expression E as a core constant expression, a |
18165 | // call to this function is ill-formed unless p points to an object that is |
18166 | // usable in constant expressions or whose complete object's lifetime began |
18167 | // within E. |
18168 | |
18169 | // Make sure it points to an object |
18170 | // nullptr does not point to an object |
18171 | if (Val.isNullPointer() || Val.getLValueBase().isNull()) |
18172 | return Error(0); |
18173 | QualType T = Val.getLValueBase().getType(); |
18174 | assert(!T->isFunctionType() && |
18175 | "Pointers to functions should have been typed as function pointers " |
18176 | "which would have been rejected earlier"); |
18177 | assert(T->isObjectType()); |
18178 | // Hypothetical array element is not an object |
18179 | if (Val.getLValueDesignator().isOnePastTheEnd()) |
18180 | return Error(1); |
18181 | assert(Val.getLValueDesignator().isValidSubobject() && |
18182 | "Unchecked case for valid subobject"); |
18183 | // All other ill-formed values should have failed EvaluatePointer, so the |
18184 | // object should be a pointer to an object that is usable in a constant |
18185 | // expression or whose complete lifetime began within the expression |
18186 | CompleteObject CO = |
18187 | findCompleteObject(Info, E, AccessKinds::AK_IsWithinLifetime, Val, T); |
18188 | // The lifetime hasn't begun yet if we are still evaluating the |
18189 | // initializer ([basic.life]p(1.2)) |
18190 | if (Info.EvaluatingDeclValue && CO.Value == Info.EvaluatingDeclValue) |
18191 | return Error(2); |
18192 | |
18193 | if (!CO) |
18194 | return false; |
18195 | IsWithinLifetimeHandler handler{.Info: Info}; |
18196 | return findSubobject(Info, E, CO, Val.getLValueDesignator(), handler); |
18197 | } |
18198 | } // namespace |
18199 |
Definitions
- getType
- getAsField
- getAsBaseClass
- isVirtualBaseClass
- getStorageType
- getAllocSizeAttr
- tryUnwrapAllocSizeCall
- isBaseAnAllocSizeCall
- isForManglingOnly
- isTemplateArgument
- AssumedSizeForUnsizedArray
- findMostDerivedSubobject
- SubobjectDesignator
- SubobjectDesignator
- SubobjectDesignator
- SubobjectDesignator
- truncate
- setInvalid
- isMostDerivedAnUnsizedArray
- getMostDerivedArraySize
- isOnePastTheEnd
- validIndexAdjustments
- isValidSubobject
- getType
- addArrayUnchecked
- addUnsizedArrayUnchecked
- addDeclUnchecked
- addComplexUnchecked
- addVectorElementUnchecked
- adjustIndex
- ScopeKind
- CallRef
- CallRef
- CallRef
- operator bool
- getOrigParam
- CallStackFrame
- getTempVersion
- pushTempVersion
- popTempVersion
- createCall
- getTemporary
- getCurrentTemporary
- getCurrentTemporaryVersion
- getCaller
- getCallRange
- getCallee
- isStdFunction
- ThisOverrideRAII
- ThisOverrideRAII
- ~ThisOverrideRAII
- ExprTimeTraceScope
- ExprTimeTraceScope
- MSConstexprContextRAII
- MSConstexprContextRAII
- ~MSConstexprContextRAII
- Cleanup
- Cleanup
- isDestroyedAtEndOf
- endLifetime
- hasSideEffect
- ObjectUnderConstruction
- operator==
- hash_value
- ConstructionPhase
- DenseMapInfo
- getEmptyKey
- getTombstoneKey
- getHashValue
- isEqual
- DynAlloc
- Kind
- getKind
- DynAllocOrder
- operator()
- EvalInfo
- EvaluatingDeclKind
- EvaluatingConstructorRAII
- EvaluatingConstructorRAII
- finishedConstructingBases
- finishedConstructingFields
- ~EvaluatingConstructorRAII
- EvaluatingDestructorRAII
- EvaluatingDestructorRAII
- startedDestroyingBases
- ~EvaluatingDestructorRAII
- isEvaluatingCtorDtor
- EvaluationMode
- checkingPotentialConstantExpression
- checkingForUndefinedBehavior
- EvalInfo
- ~EvalInfo
- getASTContext
- setEvaluatingDecl
- CheckCallLimit
- CheckArraySize
- getCallFrameAndDepth
- nextStep
- lookupDynamicAlloc
- getParamSlot
- StdAllocatorCaller
- operator bool
- getStdAllocatorCaller
- performLifetimeExtension
- discardCleanups
- getCurrentFrame
- getBottomFrame
- hasActiveDiagnostic
- setActiveDiagnostic
- setFoldFailureDiagnostic
- getEvalStatus
- hasPriorDiagnostic
- getCallStackDepth
- keepEvaluatingAfterSideEffect
- noteSideEffect
- keepEvaluatingAfterUndefinedBehavior
- noteUndefinedBehavior
- keepEvaluatingAfterFailure
- noteFailure
- ArrayInitLoopIndex
- ArrayInitLoopIndex
- ~ArrayInitLoopIndex
- operator uint64_t&
- FoldConstant
- FoldConstant
- keepDiagnostics
- ~FoldConstant
- IgnoreSideEffectsRAII
- IgnoreSideEffectsRAII
- ~IgnoreSideEffectsRAII
- SpeculativeEvaluationRAII
- moveFromAndCancel
- maybeRestoreState
- SpeculativeEvaluationRAII
- SpeculativeEvaluationRAII
- SpeculativeEvaluationRAII
- SpeculativeEvaluationRAII
- operator=
- ~SpeculativeEvaluationRAII
- ScopeRAII
- ScopeRAII
- destroy
- ~ScopeRAII
- cleanup
- checkSubobject
- diagnoseUnsizedArrayPointerArithmetic
- diagnosePointerArithmetic
- CallStackFrame
- ~CallStackFrame
- isRead
- isModification
- isAnyAccess
- isFormalAccess
- isValidIndeterminateAccess
- ComplexValue
- ComplexValue
- makeComplexFloat
- isComplexFloat
- getComplexFloatReal
- getComplexFloatImag
- makeComplexInt
- isComplexInt
- getComplexIntReal
- getComplexIntImag
- moveInto
- setFrom
- LValue
- getLValueBase
- allowConstexprUnknown
- getLValueOffset
- getLValueOffset
- getLValueDesignator
- getLValueDesignator
- isNullPointer
- getLValueCallIndex
- getLValueVersion
- moveInto
- setFrom
- set
- setNull
- setInvalid
- toString
- checkNullPointerDiagnosingWith
- checkNullPointer
- checkNullPointerForFoldAccess
- checkSubobject
- addDecl
- addUnsizedArray
- addArray
- addComplex
- addVectorElement
- clearIsNullPointer
- adjustOffsetAndIndex
- adjustOffset
- MemberPtr
- MemberPtr
- MemberPtr
- getDecl
- isDerivedMember
- getContainingRecord
- moveInto
- setFrom
- castBack
- castToDerived
- castToBase
- operator==
- negateAsSigned
- createTemporary
- createConstexprUnknownAPValues
- createParam
- createLocal
- createHeapAlloc
- describe
- EvaluateIgnoredValue
- IsOpaqueConstantCall
- IsOpaqueConstantCall
- IsGlobalLValue
- GetLValueBaseDecl
- LValueBaseString
- GetLValueBaseAsString
- ArePotentiallyOverlappingStringLiterals
- IsWeakLValue
- isZeroSized
- HasSameBase
- NoteLValueLocation
- CheckEvaluationResultKind
- CheckLValueConstantExpression
- CheckMemberPointerConstantExpression
- CheckLiteralType
- CheckEvaluationResult
- CheckConstantExpression
- CheckFullyInitialized
- CheckMemoryLeaks
- EvalPointerValueAsBool
- HandleConversionToBool
- EvaluateAsBooleanCondition
- HandleOverflow
- HandleFloatToIntCast
- getActiveRoundingMode
- checkFloatingPointResult
- HandleFloatToFloatCast
- HandleIntToIntCast
- HandleIntToFloatCast
- truncateBitfieldValue
- CheckedIntArithmetic
- handleIntIntBinOp
- handleFloatFloatBinOp
- handleLogicalOpForVector
- handleLogicalOpForVector
- handleLogicalOpForVector
- handleCompareOpForVectorHelper
- handleCompareOpForVector
- handleVectorVectorBinOp
- CastToDerivedClass
- HandleLValueDirectBase
- HandleLValueBase
- HandleLValueBasePath
- CastToBaseClass
- HandleLValueMember
- HandleLValueIndirectMember
- SizeOfType
- HandleSizeof
- HandleLValueArrayAdjustment
- HandleLValueArrayAdjustment
- HandleLValueComplexElement
- HandleLValueVectorElement
- evaluateVarDeclInit
- getBaseIndex
- extractStringLiteralCharacter
- expandStringLiteral
- expandArray
- isReadByLvalueToRvalueConversion
- isReadByLvalueToRvalueConversion
- diagnoseMutableFields
- lifetimeStartedInEvaluation
- CheckArraySize
- CompleteObject
- CompleteObject
- CompleteObject
- mayAccessMutableMembers
- operator bool
- getSubobjectType
- findSubobject
- ExtractSubobjectHandler
- failed
- found
- found
- found
- extractSubobject
- ModifySubobjectHandler
- checkConst
- failed
- found
- found
- found
- AccessKind
- modifySubobject
- FindDesignatorMismatch
- AreElementsOfSameArray
- findCompleteObject
- handleLValueToRValueConversion
- handleAssignment
- CompoundAssignSubobjectHandler
- checkConst
- failed
- found
- foundVector
- found
- found
- foundPointer
- AccessKind
- handleCompoundAssignment
- IncDecSubobjectHandler
- checkConst
- failed
- found
- found
- found
- foundPointer
- handleIncDec
- EvaluateObjectArgument
- HandleMemberPointerAccess
- HandleMemberPointerAccess
- HandleBaseToDerivedCast
- handleDefaultInitValue
- EvalStmtResult
- EvaluateVarDecl
- EvaluateDecl
- EvaluateDecompositionDeclInit
- MaybeEvaluateDeferredVarDeclInit
- EvaluateDependentExpr
- EvaluateCond
- StmtResult
- TempVersionRAII
- TempVersionRAII
- ~TempVersionRAII
- EvaluateLoopBody
- EvaluateSwitch
- CheckLocalVariableDeclaration
- EvaluateStmt
- CheckTrivialDefaultConstructor
- CheckConstexprFunction
- CheckDynamicTypeHandler
- failed
- found
- found
- found
- checkDynamicType
- checkNonVirtualMemberCallThisPointer
- DynamicType
- getBaseClassType
- ComputeDynamicType
- HandleVirtualDispatch
- HandleCovariantReturnAdjustment
- isBaseClassPublic
- HandleDynamicCast
- StartLifetimeOfUnionMemberHandler
- failed
- found
- found
- found
- AccessKind
- MaybeHandleUnionActiveMemberChange
- EvaluateCallArg
- EvaluateArgs
- handleTrivialCopy
- HandleFunctionCall
- HandleConstructorCall
- HandleConstructorCall
- HandleDestructionImpl
- DestroyObjectHandler
- failed
- found
- found
- found
- HandleDestruction
- HandleDestruction
- HandleOperatorNewCall
- hasVirtualDestructor
- getVirtualOperatorDelete
- CheckDeleteKind
- HandleOperatorDeleteCall
- BitCastBuffer
- BitCastBuffer
- readObject
- writeObject
- size
- APValueToBufferConverter
- APValueToBufferConverter
- visit
- visit
- visitRecord
- visitArray
- visitComplex
- visitVector
- visitInt
- visitFloat
- convert
- BufferToAPValueConverter
- BufferToAPValueConverter
- unsupportedType
- unrepresentableValue
- visit
- visit
- visit
- visit
- visit
- visit
- visit
- visitType
- convert
- checkBitCastConstexprEligibilityType
- checkBitCastConstexprEligibility
- handleRValueToRValueBitCast
- handleLValueToRValueBitCast
- ExprEvaluatorBase
- getDerived
- DerivedSuccess
- DerivedZeroInitialization
- CheckPotentialConstantConditional
- HandleConditionalOperator
- CCEDiag
- ZeroInitialization
- IsConstantEvaluatedBuiltinCall
- ExprEvaluatorBase
- getEvalInfo
- Error
- Error
- VisitStmt
- VisitExpr
- VisitEmbedExpr
- VisitPredefinedExpr
- VisitConstantExpr
- VisitParenExpr
- VisitUnaryExtension
- VisitUnaryPlus
- VisitChooseExpr
- VisitGenericSelectionExpr
- VisitSubstNonTypeTemplateParmExpr
- VisitCXXDefaultArgExpr
- VisitCXXDefaultInitExpr
- VisitExprWithCleanups
- VisitCXXBindTemporaryExpr
- VisitCXXReinterpretCastExpr
- VisitCXXDynamicCastExpr
- VisitBuiltinBitCastExpr
- VisitBinaryOperator
- VisitCXXRewrittenBinaryOperator
- VisitBinaryConditionalOperator
- VisitConditionalOperator
- VisitOpaqueValueExpr
- VisitPseudoObjectExpr
- VisitCallExpr
- handleCallExpr
- VisitCompoundLiteralExpr
- VisitInitListExpr
- VisitImplicitValueInitExpr
- VisitCXXScalarValueInitExpr
- VisitCXXNullPtrLiteralExpr
- VisitMemberExpr
- VisitExtVectorElementExpr
- VisitCastExpr
- VisitUnaryPostInc
- VisitUnaryPostDec
- VisitUnaryPostIncDec
- VisitStmtExpr
- VisitPackIndexingExpr
- VisitIgnoredValue
- VisitIgnoredBaseExpression
- LValueExprEvaluatorBase
- Success
- evaluatePointer
- LValueExprEvaluatorBase
- Success
- VisitMemberExpr
- VisitBinaryOperator
- VisitCastExpr
- LValueExprEvaluator
- LValueExprEvaluator
- VisitPredefinedExpr
- VisitStringLiteral
- VisitObjCEncodeExpr
- VisitUnaryPreInc
- VisitUnaryPreDec
- VisitCastExpr
- HandleLambdaCapture
- EvaluateLValue
- VisitDeclRefExpr
- VisitVarDecl
- VisitCallExpr
- VisitMaterializeTemporaryExpr
- VisitCompoundLiteralExpr
- VisitCXXTypeidExpr
- VisitCXXUuidofExpr
- VisitMemberExpr
- VisitExtVectorElementExpr
- VisitArraySubscriptExpr
- VisitUnaryDeref
- VisitUnaryReal
- VisitUnaryImag
- VisitUnaryPreIncDec
- VisitCompoundAssignOperator
- VisitBinAssign
- getBytesReturnedByAllocSizeCall
- getBytesReturnedByAllocSizeCall
- evaluateLValueAsAllocSize
- PointerExprEvaluator
- Success
- evaluateLValue
- evaluatePointer
- PointerExprEvaluator
- Success
- ZeroInitialization
- VisitObjCStringLiteral
- VisitObjCBoxedExpr
- VisitAddrLabelExpr
- VisitBlockExpr
- VisitCXXThisExpr
- VisitSourceLocExpr
- VisitEmbedExpr
- VisitSYCLUniqueStableNameExpr
- EvaluatePointer
- VisitBinaryOperator
- VisitUnaryAddrOf
- IsDeclSourceLocationCurrent
- VisitCastExpr
- GetAlignOfType
- GetAlignOfExpr
- getBaseAlignment
- getAlignmentArgument
- visitNonBuiltinCallExpr
- VisitCallExpr
- isOneByteCharacterType
- VisitBuiltinCallExpr
- VisitCXXNewExpr
- MemberPointerExprEvaluator
- Success
- MemberPointerExprEvaluator
- Success
- ZeroInitialization
- EvaluateMemberPointer
- VisitCastExpr
- VisitUnaryAddrOf
- RecordExprEvaluator
- RecordExprEvaluator
- Success
- ZeroInitialization
- VisitCallExpr
- VisitCXXConstructExpr
- HandleClassZeroInitialization
- ZeroInitialization
- VisitCastExpr
- VisitInitListExpr
- VisitCXXParenListOrInitListExpr
- VisitCXXConstructExpr
- VisitCXXInheritedCtorInitExpr
- VisitCXXStdInitializerListExpr
- VisitLambdaExpr
- EvaluateRecord
- TemporaryExprEvaluator
- TemporaryExprEvaluator
- VisitConstructExpr
- VisitCastExpr
- VisitInitListExpr
- VisitCXXConstructExpr
- VisitCallExpr
- VisitCXXStdInitializerListExpr
- VisitLambdaExpr
- EvaluateTemporary
- VectorExprEvaluator
- VectorExprEvaluator
- Success
- Success
- VisitUnaryReal
- EvaluateVector
- VisitCastExpr
- VisitInitListExpr
- ZeroInitialization
- VisitUnaryImag
- VisitBinaryOperator
- handleVectorUnaryOperator
- VisitUnaryOperator
- handleVectorElementCast
- VisitCallExpr
- VisitConvertVectorExpr
- handleVectorShuffle
- VisitShuffleVectorExpr
- ArrayExprEvaluator
- ArrayExprEvaluator
- Success
- ZeroInitialization
- VisitCallExpr
- VisitStringLiteral
- EvaluateArray
- EvaluateArrayNewInitList
- EvaluateArrayNewConstructExpr
- MaybeElementDependentArrayFiller
- VisitInitListExpr
- VisitCXXParenListOrInitListExpr
- VisitArrayInitLoopExpr
- VisitCXXConstructExpr
- VisitCXXConstructExpr
- VisitCXXParenListInitExpr
- IntExprEvaluator
- IntExprEvaluator
- Success
- Success
- Success
- Success
- Success
- Success
- Success
- Success
- ZeroInitialization
- VisitIntegerLiteral
- VisitCharacterLiteral
- VisitDeclRefExpr
- VisitMemberExpr
- VisitCXXBoolLiteralExpr
- VisitObjCBoolLiteralExpr
- VisitArrayInitIndexExpr
- VisitGNUNullExpr
- VisitTypeTraitExpr
- VisitArrayTypeTraitExpr
- VisitExpressionTraitExpr
- VisitOpenACCAsteriskSizeExpr
- FixedPointExprEvaluator
- FixedPointExprEvaluator
- Success
- Success
- Success
- Success
- ZeroInitialization
- VisitFixedPointLiteral
- EvaluateIntegerOrLValue
- EvaluateInteger
- VisitSourceLocExpr
- EvaluateFixedPoint
- EvaluateFixedPointOrInteger
- CheckReferencedDecl
- EvaluateBuiltinClassifyType
- EvaluateBuiltinClassifyType
- EvaluateBuiltinConstantPForLValue
- EvaluateBuiltinConstantP
- getObjectType
- ignorePointerCastsAndParens
- isDesignatorAtObjectEnd
- refersToCompleteObject
- isUserWritingOffTheEnd
- convertUnsignedAPIntToCharUnits
- addFlexibleArrayMemberInitSize
- determineEndOffset
- tryEvaluateBuiltinObjectSize
- VisitCallExpr
- getBuiltinAlignArguments
- VisitBuiltinCallExpr
- isOnePastTheEndOfCompleteObject
- DataRecursiveIntBinOpEvaluator
- EvalResult
- EvalResult
- swap
- Job
- Job
- Job
- startSpeculativeEval
- DataRecursiveIntBinOpEvaluator
- shouldEnqueue
- Traverse
- Success
- Success
- Error
- Error
- CCEDiag
- EvaluateExpr
- enqueue
- VisitBinOpLHSOnly
- addOrSubLValueAsInteger
- VisitBinOp
- process
- CmpResult
- EvaluateComparisonBinaryOperator
- VisitBinCmp
- VisitCXXParenListInitExpr
- VisitBinaryOperator
- VisitUnaryExprOrTypeTraitExpr
- VisitOffsetOfExpr
- VisitUnaryOperator
- VisitCastExpr
- VisitUnaryReal
- VisitUnaryImag
- VisitSizeOfPackExpr
- VisitCXXNoexceptExpr
- VisitConceptSpecializationExpr
- VisitRequiresExpr
- VisitUnaryOperator
- VisitCastExpr
- VisitBinaryOperator
- FloatExprEvaluator
- FloatExprEvaluator
- Success
- ZeroInitialization
- EvaluateFloat
- TryEvaluateBuiltinNaN
- VisitCallExpr
- VisitUnaryReal
- VisitUnaryImag
- VisitUnaryOperator
- VisitBinaryOperator
- VisitFloatingLiteral
- VisitCastExpr
- ComplexExprEvaluator
- ComplexExprEvaluator
- Success
- EvaluateComplex
- ZeroInitialization
- VisitImaginaryLiteral
- VisitCastExpr
- HandleComplexComplexMul
- HandleComplexComplexDiv
- VisitBinaryOperator
- VisitUnaryOperator
- VisitInitListExpr
- VisitCallExpr
- AtomicExprEvaluator
- AtomicExprEvaluator
- Success
- ZeroInitialization
- VisitCastExpr
- EvaluateAtomic
- VoidExprEvaluator
- VoidExprEvaluator
- Success
- ZeroInitialization
- VisitCastExpr
- VisitCallExpr
- VisitCXXDeleteExpr
- EvaluateVoid
- Evaluate
- EvaluateInPlace
- EvaluateAsRValue
- FastEvaluateAsRValue
- hasUnacceptableSideEffect
- EvaluateAsRValue
- EvaluateAsInt
- EvaluateAsFixedPoint
- EvaluateAsRValue
- EvaluateAsBooleanCondition
- EvaluateAsInt
- EvaluateAsFixedPoint
- EvaluateAsFloat
- EvaluateAsLValue
- EvaluateDestruction
- EvaluateAsConstantExpr
- EvaluateAsInitializer
- evaluateDestruction
- isEvaluatable
- EvaluateKnownConstInt
- EvaluateKnownConstIntCheckOverflow
- EvaluateForOverflow
- isGlobalLValue
- ICEKind
- ICEDiag
- ICEDiag
- NoDiag
- Worst
- CheckEvalInICE
- CheckICE
- EvaluateCPlusPlus11IntegralConstantExpr
- isIntegerConstantExpr
- getIntegerConstantExpr
- isCXX98IntegralConstantExpr
- isCXX11ConstantExpr
- EvaluateWithSubstitution
- isPotentialConstantExpr
- isPotentialConstantExprUnevaluated
- tryEvaluateObjectSize
- EvaluateBuiltinStrLen
- tryEvaluateString
- EvaluateCharRangeAsStringImpl
- EvaluateCharRangeAsString
- EvaluateCharRangeAsString
- tryEvaluateStrLen
- IsWithinLifetimeHandler
- AccessKind
- failed
- found
Improve your Profiling and Debugging skills
Find out more