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/EvalSettings.h"
37#include "ByteCode/Frame.h"
38#include "ByteCode/State.h"
39#include "ExprConstShared.h"
40#include "clang/AST/APValue.h"
41#include "clang/AST/ASTContext.h"
42#include "clang/AST/ASTLambda.h"
43#include "clang/AST/Attr.h"
44#include "clang/AST/CXXInheritance.h"
45#include "clang/AST/CharUnits.h"
46#include "clang/AST/ComparisonCategories.h"
47#include "clang/AST/CurrentSourceLocExprScope.h"
48#include "clang/AST/Expr.h"
49#include "clang/AST/InferAlloc.h"
50#include "clang/AST/OSLog.h"
51#include "clang/AST/OptionalDiagnostic.h"
52#include "clang/AST/RecordLayout.h"
53#include "clang/AST/Reflection.h"
54#include "clang/AST/StmtVisitor.h"
55#include "clang/AST/Type.h"
56#include "clang/AST/TypeLoc.h"
57#include "clang/Basic/Builtins.h"
58#include "clang/Basic/DiagnosticSema.h"
59#include "clang/Basic/TargetBuiltins.h"
60#include "clang/Basic/TargetInfo.h"
61#include "llvm/ADT/APFixedPoint.h"
62#include "llvm/ADT/Sequence.h"
63#include "llvm/ADT/SmallBitVector.h"
64#include "llvm/ADT/StringExtras.h"
65#include "llvm/ADT/bit.h"
66#include "llvm/Support/CRC.h"
67#include "llvm/Support/Casting.h"
68#include "llvm/Support/Debug.h"
69#include "llvm/Support/MathExtras.h"
70#include "llvm/Support/SaveAndRestore.h"
71#include "llvm/Support/SipHash.h"
72#include "llvm/Support/TimeProfiler.h"
73#include "llvm/Support/raw_ostream.h"
74#include <cstring>
75#include <functional>
76#include <limits>
77#include <optional>
78
79#define DEBUG_TYPE "exprconstant"
80
81using namespace clang;
82using llvm::APFixedPoint;
83using llvm::APInt;
84using llvm::APSInt;
85using llvm::APFloat;
86using llvm::FixedPointSemantics;
87
88namespace {
89 struct LValue;
90 class CallStackFrame;
91 class EvalInfo;
92
93 using SourceLocExprScopeGuard =
94 CurrentSourceLocExprScope::SourceLocExprScopeGuard;
95
96 static QualType getType(APValue::LValueBase B) {
97 return B.getType();
98 }
99
100 /// Get an LValue path entry, which is known to not be an array index, as a
101 /// field declaration.
102 static const FieldDecl *getAsField(APValue::LValuePathEntry E) {
103 return dyn_cast_or_null<FieldDecl>(Val: E.getAsBaseOrMember().getPointer());
104 }
105 /// Get an LValue path entry, which is known to not be an array index, as a
106 /// base class declaration.
107 static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) {
108 return dyn_cast_or_null<CXXRecordDecl>(Val: E.getAsBaseOrMember().getPointer());
109 }
110 /// Determine whether this LValue path entry for a base class names a virtual
111 /// base class.
112 static bool isVirtualBaseClass(APValue::LValuePathEntry E) {
113 return E.getAsBaseOrMember().getInt();
114 }
115
116 /// Given an expression, determine the type used to store the result of
117 /// evaluating that expression.
118 static QualType getStorageType(const ASTContext &Ctx, const Expr *E) {
119 if (E->isPRValue())
120 return E->getType();
121 return Ctx.getLValueReferenceType(T: E->getType());
122 }
123
124 static unsigned countNonVirtualBases(const CXXRecordDecl *RD) {
125 return llvm::count_if(Range: RD->bases(), P: [](auto &B) { return !B.isVirtual(); });
126 }
127
128 /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.
129 /// This will look through a single cast.
130 ///
131 /// Returns null if we couldn't unwrap a function with alloc_size.
132 static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) {
133 if (!E->getType()->isPointerType())
134 return nullptr;
135
136 E = E->IgnoreParens();
137 // If we're doing a variable assignment from e.g. malloc(N), there will
138 // probably be a cast of some kind. In exotic cases, we might also see a
139 // top-level ExprWithCleanups. Ignore them either way.
140 if (const auto *FE = dyn_cast<FullExpr>(Val: E))
141 E = FE->getSubExpr()->IgnoreParens();
142
143 if (const auto *Cast = dyn_cast<CastExpr>(Val: E))
144 E = Cast->getSubExpr()->IgnoreParens();
145
146 if (const auto *CE = dyn_cast<CallExpr>(Val: E))
147 return CE->getCalleeAllocSizeAttr() ? CE : nullptr;
148 return nullptr;
149 }
150
151 /// Determines whether or not the given Base contains a call to a function
152 /// with the alloc_size attribute.
153 static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) {
154 const auto *E = Base.dyn_cast<const Expr *>();
155 return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E);
156 }
157
158 /// The bound to claim that an array of unknown bound has.
159 /// The value in MostDerivedArraySize is undefined in this case. So, set it
160 /// to an arbitrary value that's likely to loudly break things if it's used.
161 static const uint64_t AssumedSizeForUnsizedArray =
162 std::numeric_limits<uint64_t>::max() / 2;
163
164 /// Determines if an LValue with the given LValueBase will have an unsized
165 /// array in its designator.
166 /// Find the path length and type of the most-derived subobject in the given
167 /// path, and find the size of the containing array, if any.
168 static unsigned
169 findMostDerivedSubobject(const ASTContext &Ctx, APValue::LValueBase Base,
170 ArrayRef<APValue::LValuePathEntry> Path,
171 uint64_t &ArraySize, QualType &Type, bool &IsArray,
172 bool &FirstEntryIsUnsizedArray) {
173 // This only accepts LValueBases from APValues, and APValues don't support
174 // arrays that lack size info.
175 assert(!isBaseAnAllocSizeCall(Base) &&
176 "Unsized arrays shouldn't appear here");
177 unsigned MostDerivedLength = 0;
178 // The type of Base is a reference type if the base is a constexpr-unknown
179 // variable. In that case, look through the reference type.
180 Type = getType(B: Base).getNonReferenceType();
181
182 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
183 if (Type->isArrayType()) {
184 const ArrayType *AT = Ctx.getAsArrayType(T: Type);
185 Type = AT->getElementType();
186 MostDerivedLength = I + 1;
187 IsArray = true;
188
189 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) {
190 ArraySize = CAT->getZExtSize();
191 } else {
192 assert(I == 0 && "unexpected unsized array designator");
193 FirstEntryIsUnsizedArray = true;
194 ArraySize = AssumedSizeForUnsizedArray;
195 }
196 } else if (Type->isAnyComplexType()) {
197 const ComplexType *CT = Type->castAs<ComplexType>();
198 Type = CT->getElementType();
199 ArraySize = 2;
200 MostDerivedLength = I + 1;
201 IsArray = true;
202 } else if (const auto *VT = Type->getAs<VectorType>()) {
203 Type = VT->getElementType();
204 ArraySize = VT->getNumElements();
205 MostDerivedLength = I + 1;
206 IsArray = true;
207 } else if (const FieldDecl *FD = getAsField(E: Path[I])) {
208 Type = FD->getType();
209 ArraySize = 0;
210 MostDerivedLength = I + 1;
211 IsArray = false;
212 } else {
213 // Path[I] describes a base class.
214 ArraySize = 0;
215 IsArray = false;
216 }
217 }
218 return MostDerivedLength;
219 }
220
221 /// A path from a glvalue to a subobject of that glvalue.
222 struct SubobjectDesignator {
223 /// True if the subobject was named in a manner not supported by C++11. Such
224 /// lvalues can still be folded, but they are not core constant expressions
225 /// and we cannot perform lvalue-to-rvalue conversions on them.
226 LLVM_PREFERRED_TYPE(bool)
227 unsigned Invalid : 1;
228
229 /// Is this a pointer one past the end of an object?
230 LLVM_PREFERRED_TYPE(bool)
231 unsigned IsOnePastTheEnd : 1;
232
233 /// Indicator of whether the first entry is an unsized array.
234 LLVM_PREFERRED_TYPE(bool)
235 unsigned FirstEntryIsAnUnsizedArray : 1;
236
237 /// Indicator of whether the most-derived object is an array element.
238 LLVM_PREFERRED_TYPE(bool)
239 unsigned MostDerivedIsArrayElement : 1;
240
241 /// The length of the path to the most-derived object of which this is a
242 /// subobject.
243 unsigned MostDerivedPathLength : 28;
244
245 /// The size of the array of which the most-derived object is an element.
246 /// This will always be 0 if the most-derived object is not an array
247 /// element. 0 is not an indicator of whether or not the most-derived object
248 /// is an array, however, because 0-length arrays are allowed.
249 ///
250 /// If the current array is an unsized array, the value of this is
251 /// undefined.
252 uint64_t MostDerivedArraySize;
253 /// The type of the most derived object referred to by this address.
254 QualType MostDerivedType;
255
256 typedef APValue::LValuePathEntry PathEntry;
257
258 /// The entries on the path from the glvalue to the designated subobject.
259 SmallVector<PathEntry, 8> Entries;
260
261 SubobjectDesignator() : Invalid(true) {}
262
263 explicit SubobjectDesignator(QualType T)
264 : Invalid(false), IsOnePastTheEnd(false),
265 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
266 MostDerivedPathLength(0), MostDerivedArraySize(0),
267 MostDerivedType(T.isNull() ? QualType() : T.getNonReferenceType()) {}
268
269 SubobjectDesignator(const ASTContext &Ctx, const APValue &V)
270 : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false),
271 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
272 MostDerivedPathLength(0), MostDerivedArraySize(0) {
273 assert(V.isLValue() && "Non-LValue used to make an LValue designator?");
274 if (!Invalid) {
275 IsOnePastTheEnd = V.isLValueOnePastTheEnd();
276 llvm::append_range(C&: Entries, R: V.getLValuePath());
277 if (V.getLValueBase()) {
278 bool IsArray = false;
279 bool FirstIsUnsizedArray = false;
280 MostDerivedPathLength = findMostDerivedSubobject(
281 Ctx, Base: V.getLValueBase(), Path: V.getLValuePath(), ArraySize&: MostDerivedArraySize,
282 Type&: MostDerivedType, IsArray, FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
283 MostDerivedIsArrayElement = IsArray;
284 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
285 }
286 }
287 }
288
289 void truncate(ASTContext &Ctx, APValue::LValueBase Base,
290 unsigned NewLength) {
291 if (Invalid)
292 return;
293
294 assert(Base && "cannot truncate path for null pointer");
295 assert(NewLength <= Entries.size() && "not a truncation");
296
297 if (NewLength == Entries.size())
298 return;
299 Entries.resize(N: NewLength);
300
301 bool IsArray = false;
302 bool FirstIsUnsizedArray = false;
303 MostDerivedPathLength = findMostDerivedSubobject(
304 Ctx, Base, Path: Entries, ArraySize&: MostDerivedArraySize, Type&: MostDerivedType, IsArray,
305 FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
306 MostDerivedIsArrayElement = IsArray;
307 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
308 }
309
310 void setInvalid() {
311 Invalid = true;
312 Entries.clear();
313 }
314
315 /// Determine whether the most derived subobject is an array without a
316 /// known bound.
317 bool isMostDerivedAnUnsizedArray() const {
318 assert(!Invalid && "Calling this makes no sense on invalid designators");
319 return Entries.size() == 1 && FirstEntryIsAnUnsizedArray;
320 }
321
322 /// Determine what the most derived array's size is. Results in an assertion
323 /// failure if the most derived array lacks a size.
324 uint64_t getMostDerivedArraySize() const {
325 assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size");
326 return MostDerivedArraySize;
327 }
328
329 /// Determine whether this is a one-past-the-end pointer.
330 bool isOnePastTheEnd() const {
331 assert(!Invalid);
332 if (IsOnePastTheEnd)
333 return true;
334 if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement &&
335 Entries[MostDerivedPathLength - 1].getAsArrayIndex() ==
336 MostDerivedArraySize)
337 return true;
338 return false;
339 }
340
341 /// Get the range of valid index adjustments in the form
342 /// {maximum value that can be subtracted from this pointer,
343 /// maximum value that can be added to this pointer}
344 std::pair<uint64_t, uint64_t> validIndexAdjustments() {
345 if (Invalid || isMostDerivedAnUnsizedArray())
346 return {0, 0};
347
348 // [expr.add]p4: For the purposes of these operators, a pointer to a
349 // nonarray object behaves the same as a pointer to the first element of
350 // an array of length one with the type of the object as its element type.
351 bool IsArray = MostDerivedPathLength == Entries.size() &&
352 MostDerivedIsArrayElement;
353 uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex()
354 : (uint64_t)IsOnePastTheEnd;
355 uint64_t ArraySize =
356 IsArray ? getMostDerivedArraySize() : (uint64_t)1;
357 return {ArrayIndex, ArraySize - ArrayIndex};
358 }
359
360 /// Check that this refers to a valid subobject.
361 bool isValidSubobject() const {
362 if (Invalid)
363 return false;
364 return !isOnePastTheEnd();
365 }
366 /// Check that this refers to a valid subobject, and if not, produce a
367 /// relevant diagnostic and set the designator as invalid.
368 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK);
369
370 /// Get the type of the designated object.
371 QualType getType(ASTContext &Ctx) const {
372 assert(!Invalid && "invalid designator has no subobject type");
373 return MostDerivedPathLength == Entries.size()
374 ? MostDerivedType
375 : Ctx.getCanonicalTagType(TD: getAsBaseClass(E: Entries.back()));
376 }
377
378 /// Update this designator to refer to the first element within this array.
379 void addArrayUnchecked(const ConstantArrayType *CAT) {
380 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
381
382 // This is a most-derived object.
383 MostDerivedType = CAT->getElementType();
384 MostDerivedIsArrayElement = true;
385 MostDerivedArraySize = CAT->getZExtSize();
386 MostDerivedPathLength = Entries.size();
387 }
388 /// Update this designator to refer to the first element within the array of
389 /// elements of type T. This is an array of unknown size.
390 void addUnsizedArrayUnchecked(QualType ElemTy) {
391 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
392
393 MostDerivedType = ElemTy;
394 MostDerivedIsArrayElement = true;
395 // The value in MostDerivedArraySize is undefined in this case. So, set it
396 // to an arbitrary value that's likely to loudly break things if it's
397 // used.
398 MostDerivedArraySize = AssumedSizeForUnsizedArray;
399 MostDerivedPathLength = Entries.size();
400 }
401 /// Update this designator to refer to the given base or member of this
402 /// object.
403 void addDeclUnchecked(const Decl *D, bool Virtual = false) {
404 Entries.push_back(Elt: APValue::BaseOrMemberType(D, Virtual));
405
406 // If this isn't a base class, it's a new most-derived object.
407 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: D)) {
408 MostDerivedType = FD->getType();
409 MostDerivedIsArrayElement = false;
410 MostDerivedArraySize = 0;
411 MostDerivedPathLength = Entries.size();
412 }
413 }
414 /// Update this designator to refer to the given complex component.
415 void addComplexUnchecked(QualType EltTy, bool Imag) {
416 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Imag));
417
418 // This is technically a most-derived object, though in practice this
419 // is unlikely to matter.
420 MostDerivedType = EltTy;
421 MostDerivedIsArrayElement = true;
422 MostDerivedArraySize = 2;
423 MostDerivedPathLength = Entries.size();
424 }
425
426 void addVectorElementUnchecked(QualType EltTy, uint64_t Size,
427 uint64_t Idx) {
428 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Idx));
429 MostDerivedType = EltTy;
430 MostDerivedPathLength = Entries.size();
431 MostDerivedArraySize = 0;
432 MostDerivedIsArrayElement = false;
433 }
434
435 void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E);
436 void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E,
437 const APSInt &N);
438 /// Add N to the address of this subobject.
439 void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N, const LValue &LV);
440 };
441
442 /// A scope at the end of which an object can need to be destroyed.
443 enum class ScopeKind {
444 Block,
445 FullExpression,
446 Call
447 };
448
449 /// A reference to a particular call and its arguments.
450 struct CallRef {
451 CallRef() : OrigCallee(), CallIndex(0), Version() {}
452 CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version)
453 : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {}
454
455 explicit operator bool() const { return OrigCallee; }
456
457 /// Get the parameter that the caller initialized, corresponding to the
458 /// given parameter in the callee.
459 const ParmVarDecl *getOrigParam(const ParmVarDecl *PVD) const {
460 return OrigCallee ? OrigCallee->getParamDecl(i: PVD->getFunctionScopeIndex())
461 : PVD;
462 }
463
464 /// The callee at the point where the arguments were evaluated. This might
465 /// be different from the actual callee (a different redeclaration, or a
466 /// virtual override), but this function's parameters are the ones that
467 /// appear in the parameter map.
468 const FunctionDecl *OrigCallee;
469 /// The call index of the frame that holds the argument values.
470 unsigned CallIndex;
471 /// The version of the parameters corresponding to this call.
472 unsigned Version;
473 };
474
475 /// A stack frame in the constexpr call stack.
476 class CallStackFrame : public interp::Frame {
477 public:
478 EvalInfo &Info;
479
480 /// Parent - The caller of this stack frame.
481 CallStackFrame *Caller;
482
483 /// Callee - The function which was called.
484 const FunctionDecl *Callee;
485
486 /// This - The binding for the this pointer in this call, if any.
487 const LValue *This;
488
489 /// CallExpr - The syntactical structure of member function calls
490 const Expr *CallExpr;
491
492 /// Information on how to find the arguments to this call. Our arguments
493 /// are stored in our parent's CallStackFrame, using the ParmVarDecl* as a
494 /// key and this value as the version.
495 CallRef Arguments;
496
497 /// Source location information about the default argument or default
498 /// initializer expression we're evaluating, if any.
499 CurrentSourceLocExprScope CurSourceLocExprScope;
500
501 // Note that we intentionally use std::map here so that references to
502 // values are stable.
503 typedef std::pair<const void *, unsigned> MapKeyTy;
504 typedef std::map<MapKeyTy, APValue> MapTy;
505 /// Temporaries - Temporary lvalues materialized within this stack frame.
506 MapTy Temporaries;
507
508 /// CallRange - The source range of the call expression for this call.
509 SourceRange CallRange;
510
511 /// Index - The call index of this call.
512 unsigned Index;
513
514 /// The stack of integers for tracking version numbers for temporaries.
515 SmallVector<unsigned, 2> TempVersionStack = {1};
516 unsigned CurTempVersion = TempVersionStack.back();
517
518 unsigned getTempVersion() const { return TempVersionStack.back(); }
519
520 void pushTempVersion() {
521 TempVersionStack.push_back(Elt: ++CurTempVersion);
522 }
523
524 void popTempVersion() {
525 TempVersionStack.pop_back();
526 }
527
528 CallRef createCall(const FunctionDecl *Callee) {
529 return {Callee, Index, ++CurTempVersion};
530 }
531
532 // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact
533 // on the overall stack usage of deeply-recursing constexpr evaluations.
534 // (We should cache this map rather than recomputing it repeatedly.)
535 // But let's try this and see how it goes; we can look into caching the map
536 // as a later change.
537
538 /// LambdaCaptureFields - Mapping from captured variables/this to
539 /// corresponding data members in the closure class.
540 llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;
541 FieldDecl *LambdaThisCaptureField = nullptr;
542
543 CallStackFrame(EvalInfo &Info, SourceRange CallRange,
544 const FunctionDecl *Callee, const LValue *This,
545 const Expr *CallExpr, CallRef Arguments);
546 ~CallStackFrame();
547
548 // Return the temporary for Key whose version number is Version.
549 APValue *getTemporary(const void *Key, unsigned Version) {
550 MapKeyTy KV(Key, Version);
551 auto LB = Temporaries.lower_bound(x: KV);
552 if (LB != Temporaries.end() && LB->first == KV)
553 return &LB->second;
554 return nullptr;
555 }
556
557 // Return the current temporary for Key in the map.
558 APValue *getCurrentTemporary(const void *Key) {
559 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
560 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
561 return &std::prev(x: UB)->second;
562 return nullptr;
563 }
564
565 // Return the version number of the current temporary for Key.
566 unsigned getCurrentTemporaryVersion(const void *Key) const {
567 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
568 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
569 return std::prev(x: UB)->first.second;
570 return 0;
571 }
572
573 /// Allocate storage for an object of type T in this stack frame.
574 /// Populates LV with a handle to the created object. Key identifies
575 /// the temporary within the stack frame, and must not be reused without
576 /// bumping the temporary version number.
577 template<typename KeyT>
578 APValue &createTemporary(const KeyT *Key, QualType T,
579 ScopeKind Scope, LValue &LV);
580
581 /// Allocate storage for a parameter of a function call made in this frame.
582 APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV);
583
584 void describe(llvm::raw_ostream &OS) const override;
585
586 Frame *getCaller() const override { return Caller; }
587 SourceRange getCallRange() const override { return CallRange; }
588 const FunctionDecl *getCallee() const override { return Callee; }
589
590 bool isStdFunction() const {
591 for (const DeclContext *DC = Callee; DC; DC = DC->getParent())
592 if (DC->isStdNamespace())
593 return true;
594 return false;
595 }
596
597 /// Whether we're in a context where [[msvc::constexpr]] evaluation is
598 /// permitted. See MSConstexprDocs for description of permitted contexts.
599 bool CanEvalMSConstexpr = false;
600
601 private:
602 APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T,
603 ScopeKind Scope);
604 };
605
606 /// Temporarily override 'this'.
607 class ThisOverrideRAII {
608 public:
609 ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable)
610 : Frame(Frame), OldThis(Frame.This) {
611 if (Enable)
612 Frame.This = NewThis;
613 }
614 ~ThisOverrideRAII() {
615 Frame.This = OldThis;
616 }
617 private:
618 CallStackFrame &Frame;
619 const LValue *OldThis;
620 };
621
622 // A shorthand time trace scope struct, prints source range, for example
623 // {"name":"EvaluateAsRValue","args":{"detail":"<test.cc:8:21, col:25>"}}}
624 class ExprTimeTraceScope {
625 public:
626 ExprTimeTraceScope(const Expr *E, const ASTContext &Ctx, StringRef Name)
627 : TimeScope(Name, [E, &Ctx] {
628 return E->getSourceRange().printToString(SM: Ctx.getSourceManager());
629 }) {}
630
631 private:
632 llvm::TimeTraceScope TimeScope;
633 };
634
635 /// RAII object used to change the current ability of
636 /// [[msvc::constexpr]] evaulation.
637 struct MSConstexprContextRAII {
638 CallStackFrame &Frame;
639 bool OldValue;
640 explicit MSConstexprContextRAII(CallStackFrame &Frame, bool Value)
641 : Frame(Frame), OldValue(Frame.CanEvalMSConstexpr) {
642 Frame.CanEvalMSConstexpr = Value;
643 }
644
645 ~MSConstexprContextRAII() { Frame.CanEvalMSConstexpr = OldValue; }
646 };
647}
648
649static bool HandleDestruction(EvalInfo &Info, const Expr *E,
650 const LValue &This, QualType ThisType);
651static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
652 APValue::LValueBase LVBase, APValue &Value,
653 QualType T);
654
655namespace {
656 /// A cleanup, and a flag indicating whether it is lifetime-extended.
657 class Cleanup {
658 llvm::PointerIntPair<APValue*, 2, ScopeKind> Value;
659 APValue::LValueBase Base;
660 QualType T;
661
662 public:
663 Cleanup(APValue *Val, APValue::LValueBase Base, QualType T,
664 ScopeKind Scope)
665 : Value(Val, Scope), Base(Base), T(T) {}
666
667 /// Determine whether this cleanup should be performed at the end of the
668 /// given kind of scope.
669 bool isDestroyedAtEndOf(ScopeKind K) const {
670 return (int)Value.getInt() >= (int)K;
671 }
672 bool endLifetime(EvalInfo &Info, bool RunDestructors) {
673 if (RunDestructors) {
674 SourceLocation Loc;
675 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
676 Loc = VD->getLocation();
677 else if (const Expr *E = Base.dyn_cast<const Expr*>())
678 Loc = E->getExprLoc();
679 return HandleDestruction(Info, Loc, LVBase: Base, Value&: *Value.getPointer(), T);
680 }
681 *Value.getPointer() = APValue();
682 return true;
683 }
684
685 bool hasSideEffect() {
686 return T.isDestructedType();
687 }
688 };
689
690 /// A reference to an object whose construction we are currently evaluating.
691 struct ObjectUnderConstruction {
692 APValue::LValueBase Base;
693 ArrayRef<APValue::LValuePathEntry> Path;
694 friend bool operator==(const ObjectUnderConstruction &LHS,
695 const ObjectUnderConstruction &RHS) {
696 return LHS.Base == RHS.Base && LHS.Path == RHS.Path;
697 }
698 friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) {
699 return llvm::hash_combine(args: Obj.Base, args: Obj.Path);
700 }
701 };
702 enum class ConstructionPhase {
703 None,
704 Bases,
705 AfterBases,
706 AfterFields,
707 Destroying,
708 DestroyingBases
709 };
710}
711
712namespace llvm {
713template<> struct DenseMapInfo<ObjectUnderConstruction> {
714 using Base = DenseMapInfo<APValue::LValueBase>;
715 static unsigned getHashValue(const ObjectUnderConstruction &Object) {
716 return hash_value(Obj: Object);
717 }
718 static bool isEqual(const ObjectUnderConstruction &LHS,
719 const ObjectUnderConstruction &RHS) {
720 return LHS == RHS;
721 }
722};
723}
724
725namespace {
726 /// A dynamically-allocated heap object.
727 struct DynAlloc {
728 /// The value of this heap-allocated object.
729 APValue Value;
730 /// The allocating expression; used for diagnostics. Either a CXXNewExpr
731 /// or a CallExpr (the latter is for direct calls to operator new inside
732 /// std::allocator<T>::allocate).
733 const Expr *AllocExpr = nullptr;
734
735 /// Get the kind of the allocation. This must match between allocation
736 /// and deallocation.
737 static DynAllocKind kindOfExpr(const Expr *AllocExpr) {
738 if (auto *NE = dyn_cast<CXXNewExpr>(Val: AllocExpr))
739 return NE->isArray() ? DynAllocKind::ArrayNew : DynAllocKind::New;
740 assert(isa<CallExpr>(AllocExpr));
741 return DynAllocKind::StdAllocator;
742 }
743
744 DynAllocKind getKind() const { return kindOfExpr(AllocExpr); }
745 };
746
747 struct DynAllocOrder {
748 bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const {
749 return L.getIndex() < R.getIndex();
750 }
751 };
752
753 /// EvalInfo - This is a private struct used by the evaluator to capture
754 /// information about a subexpression as it is folded. It retains information
755 /// about the AST context, but also maintains information about the folded
756 /// expression.
757 ///
758 /// If an expression could be evaluated, it is still possible it is not a C
759 /// "integer constant expression" or constant expression. If not, this struct
760 /// captures information about how and why not.
761 ///
762 /// One bit of information passed *into* the request for constant folding
763 /// indicates whether the subexpression is "evaluated" or not according to C
764 /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
765 /// evaluate the expression regardless of what the RHS is, but C only allows
766 /// certain things in certain situations.
767 class EvalInfo final : public interp::State {
768 public:
769 /// CurrentCall - The top of the constexpr call stack.
770 CallStackFrame *CurrentCall;
771
772 /// CallStackDepth - The number of calls in the call stack right now.
773 unsigned CallStackDepth;
774
775 /// NextCallIndex - The next call index to assign.
776 unsigned NextCallIndex;
777
778 /// StepsLeft - The remaining number of evaluation steps we're permitted
779 /// to perform. This is essentially a limit for the number of statements
780 /// we will evaluate.
781 unsigned StepsLeft;
782
783 /// BottomFrame - The frame in which evaluation started. This must be
784 /// initialized after CurrentCall and CallStackDepth.
785 CallStackFrame BottomFrame;
786
787 /// A stack of values whose lifetimes end at the end of some surrounding
788 /// evaluation frame.
789 llvm::SmallVector<Cleanup, 16> CleanupStack;
790
791 /// EvaluatingDecl - This is the declaration whose initializer is being
792 /// evaluated, if any.
793 APValue::LValueBase EvaluatingDecl;
794
795 enum class EvaluatingDeclKind {
796 None,
797 /// We're evaluating the construction of EvaluatingDecl.
798 Ctor,
799 /// We're evaluating the destruction of EvaluatingDecl.
800 Dtor,
801 };
802 EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None;
803
804 /// EvaluatingDeclValue - This is the value being constructed for the
805 /// declaration whose initializer is being evaluated, if any.
806 APValue *EvaluatingDeclValue;
807
808 /// Stack of loops and 'switch' statements which we're currently
809 /// breaking/continuing; null entries are used to mark unlabeled
810 /// break/continue.
811 SmallVector<const Stmt *> BreakContinueStack;
812
813 /// Set of objects that are currently being constructed.
814 llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase>
815 ObjectsUnderConstruction;
816
817 /// Current heap allocations, along with the location where each was
818 /// allocated. We use std::map here because we need stable addresses
819 /// for the stored APValues.
820 std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs;
821
822 /// The number of heap allocations performed so far in this evaluation.
823 uintptr_t NumHeapAllocs = 0;
824
825 struct EvaluatingConstructorRAII {
826 EvalInfo &EI;
827 ObjectUnderConstruction Object;
828 bool DidInsert;
829 EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object,
830 bool HasBases)
831 : EI(EI), Object(Object) {
832 DidInsert =
833 EI.ObjectsUnderConstruction
834 .insert(KV: {Object, HasBases ? ConstructionPhase::Bases
835 : ConstructionPhase::AfterBases})
836 .second;
837 }
838 void finishedConstructingBases() {
839 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases;
840 }
841 void finishedConstructingFields() {
842 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterFields;
843 }
844 ~EvaluatingConstructorRAII() {
845 if (DidInsert) EI.ObjectsUnderConstruction.erase(Val: Object);
846 }
847 };
848
849 struct EvaluatingDestructorRAII {
850 EvalInfo &EI;
851 ObjectUnderConstruction Object;
852 bool DidInsert;
853 EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object)
854 : EI(EI), Object(Object) {
855 DidInsert = EI.ObjectsUnderConstruction
856 .insert(KV: {Object, ConstructionPhase::Destroying})
857 .second;
858 }
859 void startedDestroyingBases() {
860 EI.ObjectsUnderConstruction[Object] =
861 ConstructionPhase::DestroyingBases;
862 }
863 ~EvaluatingDestructorRAII() {
864 if (DidInsert)
865 EI.ObjectsUnderConstruction.erase(Val: Object);
866 }
867 };
868
869 ConstructionPhase
870 isEvaluatingCtorDtor(APValue::LValueBase Base,
871 ArrayRef<APValue::LValuePathEntry> Path) {
872 return ObjectsUnderConstruction.lookup(Val: {.Base: Base, .Path: Path});
873 }
874
875 /// If we're currently speculatively evaluating, the outermost call stack
876 /// depth at which we can mutate state, otherwise 0.
877 unsigned SpeculativeEvaluationDepth = 0;
878
879 /// The current array initialization index, if we're performing array
880 /// initialization.
881 uint64_t ArrayInitIndex = -1;
882
883 EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode)
884 : State(const_cast<ASTContext &>(C), S), CurrentCall(nullptr),
885 CallStackDepth(0), NextCallIndex(1),
886 StepsLeft(C.getLangOpts().ConstexprStepLimit),
887 BottomFrame(*this, SourceLocation(), /*Callee=*/nullptr,
888 /*This=*/nullptr,
889 /*CallExpr=*/nullptr, CallRef()),
890 EvaluatingDecl((const ValueDecl *)nullptr),
891 EvaluatingDeclValue(nullptr) {
892 EvalMode = Mode;
893 }
894
895 ~EvalInfo() {
896 discardCleanups();
897 }
898
899 void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value,
900 EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) {
901 EvaluatingDecl = Base;
902 IsEvaluatingDecl = EDK;
903 EvaluatingDeclValue = &Value;
904 }
905
906 bool CheckCallLimit(SourceLocation Loc) {
907 // Don't perform any constexpr calls (other than the call we're checking)
908 // when checking a potential constant expression.
909 if (checkingPotentialConstantExpression() && CallStackDepth > 1)
910 return false;
911 if (NextCallIndex == 0) {
912 // NextCallIndex has wrapped around.
913 FFDiag(Loc, DiagId: diag::note_constexpr_call_limit_exceeded);
914 return false;
915 }
916 if (CallStackDepth <= getLangOpts().ConstexprCallDepth)
917 return true;
918 FFDiag(Loc, DiagId: diag::note_constexpr_depth_limit_exceeded)
919 << getLangOpts().ConstexprCallDepth;
920 return false;
921 }
922
923 bool CheckArraySize(SourceLocation Loc, unsigned BitWidth,
924 uint64_t ElemCount, bool Diag) {
925 // FIXME: GH63562
926 // APValue stores array extents as unsigned,
927 // so anything that is greater that unsigned would overflow when
928 // constructing the array, we catch this here.
929 if (BitWidth > ConstantArrayType::getMaxSizeBits(Context: Ctx) ||
930 ElemCount > uint64_t(std::numeric_limits<unsigned>::max())) {
931 if (Diag)
932 FFDiag(Loc, DiagId: diag::note_constexpr_new_too_large) << ElemCount;
933 return false;
934 }
935
936 // FIXME: GH63562
937 // Arrays allocate an APValue per element.
938 // We use the number of constexpr steps as a proxy for the maximum size
939 // of arrays to avoid exhausting the system resources, as initialization
940 // of each element is likely to take some number of steps anyway.
941 uint64_t Limit = getLangOpts().ConstexprStepLimit;
942 if (Limit != 0 && ElemCount > Limit) {
943 if (Diag) {
944 FFDiag(Loc, DiagId: diag::note_constexpr_new_exceeds_limits, ExtraNotes: 1)
945 << ElemCount << Limit;
946 Note(Loc, DiagId: diag::note_constexpr_steps);
947 }
948 return false;
949 }
950 return true;
951 }
952
953 std::pair<CallStackFrame *, unsigned>
954 getCallFrameAndDepth(unsigned CallIndex) {
955 assert(CallIndex && "no call index in getCallFrameAndDepth");
956 // We will eventually hit BottomFrame, which has Index 1, so Frame can't
957 // be null in this loop.
958 unsigned Depth = CallStackDepth;
959 CallStackFrame *Frame = CurrentCall;
960 while (Frame->Index > CallIndex) {
961 Frame = Frame->Caller;
962 --Depth;
963 }
964 if (Frame->Index == CallIndex)
965 return {Frame, Depth};
966 return {nullptr, 0};
967 }
968
969 bool nextStep(const Stmt *S) {
970 if (getLangOpts().ConstexprStepLimit == 0)
971 return true;
972
973 if (!StepsLeft) {
974 FFDiag(Loc: S->getBeginLoc(), DiagId: diag::note_constexpr_step_limit_exceeded, ExtraNotes: 1)
975 << getLangOpts().ConstexprStepLimit;
976 Note(Loc: S->getBeginLoc(), DiagId: diag::note_constexpr_steps);
977 return false;
978 }
979 --StepsLeft;
980 return true;
981 }
982
983 APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV);
984
985 std::optional<DynAlloc *> lookupDynamicAlloc(DynamicAllocLValue DA) {
986 std::optional<DynAlloc *> Result;
987 auto It = HeapAllocs.find(x: DA);
988 if (It != HeapAllocs.end())
989 Result = &It->second;
990 return Result;
991 }
992
993 /// Get the allocated storage for the given parameter of the given call.
994 APValue *getParamSlot(CallRef Call, const ParmVarDecl *PVD) {
995 CallStackFrame *Frame = getCallFrameAndDepth(CallIndex: Call.CallIndex).first;
996 return Frame ? Frame->getTemporary(Key: Call.getOrigParam(PVD), Version: Call.Version)
997 : nullptr;
998 }
999
1000 /// Information about a stack frame for std::allocator<T>::[de]allocate.
1001 struct StdAllocatorCaller {
1002 unsigned FrameIndex;
1003 QualType ElemType;
1004 const Expr *Call;
1005 explicit operator bool() const { return FrameIndex != 0; };
1006 };
1007
1008 StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const {
1009 for (const CallStackFrame *Call = CurrentCall; Call->Caller != nullptr;
1010 Call = Call->Caller) {
1011 const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Val: Call->Callee);
1012 if (!MD)
1013 continue;
1014 const IdentifierInfo *FnII = MD->getIdentifier();
1015 if (!FnII || !FnII->isStr(Str: FnName))
1016 continue;
1017
1018 const auto *CTSD =
1019 dyn_cast<ClassTemplateSpecializationDecl>(Val: MD->getParent());
1020 if (!CTSD)
1021 continue;
1022
1023 const IdentifierInfo *ClassII = CTSD->getIdentifier();
1024 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
1025 if (CTSD->isInStdNamespace() && ClassII &&
1026 ClassII->isStr(Str: "allocator") && TAL.size() >= 1 &&
1027 TAL[0].getKind() == TemplateArgument::Type)
1028 return {.FrameIndex: Call->Index, .ElemType: TAL[0].getAsType(), .Call: Call->CallExpr};
1029 }
1030
1031 return {};
1032 }
1033
1034 void performLifetimeExtension() {
1035 // Disable the cleanups for lifetime-extended temporaries.
1036 llvm::erase_if(C&: CleanupStack, P: [](Cleanup &C) {
1037 return !C.isDestroyedAtEndOf(K: ScopeKind::FullExpression);
1038 });
1039 }
1040
1041 /// Throw away any remaining cleanups at the end of evaluation. If any
1042 /// cleanups would have had a side-effect, note that as an unmodeled
1043 /// side-effect and return false. Otherwise, return true.
1044 bool discardCleanups() {
1045 for (Cleanup &C : CleanupStack) {
1046 if (C.hasSideEffect() && !noteSideEffect()) {
1047 CleanupStack.clear();
1048 return false;
1049 }
1050 }
1051 CleanupStack.clear();
1052 return true;
1053 }
1054
1055 private:
1056 const interp::Frame *getCurrentFrame() override { return CurrentCall; }
1057
1058 unsigned getCallStackDepth() override { return CallStackDepth; }
1059 bool stepsLeft() const override { return StepsLeft > 0; }
1060
1061 public:
1062 /// Notes that we failed to evaluate an expression that other expressions
1063 /// directly depend on, and determine if we should keep evaluating. This
1064 /// should only be called if we actually intend to keep evaluating.
1065 ///
1066 /// Call noteSideEffect() instead if we may be able to ignore the value that
1067 /// we failed to evaluate, e.g. if we failed to evaluate Foo() in:
1068 ///
1069 /// (Foo(), 1) // use noteSideEffect
1070 /// (Foo() || true) // use noteSideEffect
1071 /// Foo() + 1 // use noteFailure
1072 [[nodiscard]] bool noteFailure() {
1073 // Failure when evaluating some expression often means there is some
1074 // subexpression whose evaluation was skipped. Therefore, (because we
1075 // don't track whether we skipped an expression when unwinding after an
1076 // evaluation failure) every evaluation failure that bubbles up from a
1077 // subexpression implies that a side-effect has potentially happened. We
1078 // skip setting the HasSideEffects flag to true until we decide to
1079 // continue evaluating after that point, which happens here.
1080 bool KeepGoing = keepEvaluatingAfterFailure();
1081 EvalStatus.HasSideEffects |= KeepGoing;
1082 return KeepGoing;
1083 }
1084
1085 class ArrayInitLoopIndex {
1086 EvalInfo &Info;
1087 uint64_t OuterIndex;
1088
1089 public:
1090 ArrayInitLoopIndex(EvalInfo &Info)
1091 : Info(Info), OuterIndex(Info.ArrayInitIndex) {
1092 Info.ArrayInitIndex = 0;
1093 }
1094 ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; }
1095
1096 operator uint64_t&() { return Info.ArrayInitIndex; }
1097 };
1098 };
1099
1100 /// Object used to treat all foldable expressions as constant expressions.
1101 struct FoldConstant {
1102 EvalInfo &Info;
1103 bool Enabled;
1104 bool HadNoPriorDiags;
1105 EvaluationMode OldMode;
1106
1107 explicit FoldConstant(EvalInfo &Info, bool Enabled)
1108 : Info(Info),
1109 Enabled(Enabled),
1110 HadNoPriorDiags(Info.EvalStatus.Diag &&
1111 Info.EvalStatus.Diag->empty() &&
1112 !Info.EvalStatus.HasSideEffects),
1113 OldMode(Info.EvalMode) {
1114 if (Enabled)
1115 Info.EvalMode = EvaluationMode::ConstantFold;
1116 }
1117 void keepDiagnostics() { Enabled = false; }
1118 ~FoldConstant() {
1119 if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() &&
1120 !Info.EvalStatus.HasSideEffects) {
1121 Info.EvalStatus.Diag->clear();
1122 Info.EvalStatus.DiagEmitted = false;
1123 }
1124 Info.EvalMode = OldMode;
1125 }
1126 };
1127
1128 /// RAII object used to set the current evaluation mode to ignore
1129 /// side-effects.
1130 struct IgnoreSideEffectsRAII {
1131 EvalInfo &Info;
1132 EvaluationMode OldMode;
1133 explicit IgnoreSideEffectsRAII(EvalInfo &Info)
1134 : Info(Info), OldMode(Info.EvalMode) {
1135 Info.EvalMode = EvaluationMode::IgnoreSideEffects;
1136 }
1137
1138 ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; }
1139 };
1140
1141 /// RAII object used to optionally suppress diagnostics and side-effects from
1142 /// a speculative evaluation.
1143 class SpeculativeEvaluationRAII {
1144 EvalInfo *Info = nullptr;
1145 Expr::EvalStatus OldStatus;
1146 unsigned OldSpeculativeEvaluationDepth = 0;
1147
1148 void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) {
1149 Info = Other.Info;
1150 OldStatus = Other.OldStatus;
1151 OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth;
1152 Other.Info = nullptr;
1153 }
1154
1155 void maybeRestoreState() {
1156 if (!Info)
1157 return;
1158
1159 Info->EvalStatus = OldStatus;
1160 Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth;
1161 }
1162
1163 public:
1164 SpeculativeEvaluationRAII() = default;
1165
1166 SpeculativeEvaluationRAII(
1167 EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr)
1168 : Info(&Info), OldStatus(Info.EvalStatus),
1169 OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) {
1170 Info.EvalStatus.Diag = NewDiag;
1171 Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1;
1172 }
1173
1174 SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete;
1175 SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) {
1176 moveFromAndCancel(Other: std::move(Other));
1177 }
1178
1179 SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) {
1180 maybeRestoreState();
1181 moveFromAndCancel(Other: std::move(Other));
1182 return *this;
1183 }
1184
1185 ~SpeculativeEvaluationRAII() { maybeRestoreState(); }
1186 };
1187
1188 /// RAII object wrapping a full-expression or block scope, and handling
1189 /// the ending of the lifetime of temporaries created within it.
1190 template<ScopeKind Kind>
1191 class ScopeRAII {
1192 EvalInfo &Info;
1193 unsigned OldStackSize;
1194 public:
1195 ScopeRAII(EvalInfo &Info)
1196 : Info(Info), OldStackSize(Info.CleanupStack.size()) {
1197 // Push a new temporary version. This is needed to distinguish between
1198 // temporaries created in different iterations of a loop.
1199 Info.CurrentCall->pushTempVersion();
1200 }
1201 bool destroy(bool RunDestructors = true) {
1202 bool OK = cleanup(Info, RunDestructors, OldStackSize);
1203 OldStackSize = std::numeric_limits<unsigned>::max();
1204 return OK;
1205 }
1206 ~ScopeRAII() {
1207 if (OldStackSize != std::numeric_limits<unsigned>::max())
1208 destroy(RunDestructors: false);
1209 // Body moved to a static method to encourage the compiler to inline away
1210 // instances of this class.
1211 Info.CurrentCall->popTempVersion();
1212 }
1213 private:
1214 static bool cleanup(EvalInfo &Info, bool RunDestructors,
1215 unsigned OldStackSize) {
1216 assert(OldStackSize <= Info.CleanupStack.size() &&
1217 "running cleanups out of order?");
1218
1219 // Run all cleanups for a block scope, and non-lifetime-extended cleanups
1220 // for a full-expression scope.
1221 bool Success = true;
1222 for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) {
1223 if (Info.CleanupStack[I - 1].isDestroyedAtEndOf(K: Kind)) {
1224 if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) {
1225 Success = false;
1226 break;
1227 }
1228 }
1229 }
1230
1231 // Compact any retained cleanups.
1232 auto NewEnd = Info.CleanupStack.begin() + OldStackSize;
1233 if (Kind != ScopeKind::Block)
1234 NewEnd =
1235 std::remove_if(NewEnd, Info.CleanupStack.end(), [](Cleanup &C) {
1236 return C.isDestroyedAtEndOf(K: Kind);
1237 });
1238 Info.CleanupStack.erase(CS: NewEnd, CE: Info.CleanupStack.end());
1239 return Success;
1240 }
1241 };
1242 typedef ScopeRAII<ScopeKind::Block> BlockScopeRAII;
1243 typedef ScopeRAII<ScopeKind::FullExpression> FullExpressionRAII;
1244 typedef ScopeRAII<ScopeKind::Call> CallScopeRAII;
1245}
1246
1247bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,
1248 CheckSubobjectKind CSK) {
1249 if (Invalid)
1250 return false;
1251 if (isOnePastTheEnd()) {
1252 Info.CCEDiag(E, DiagId: diag::note_constexpr_past_end_subobject)
1253 << CSK;
1254 setInvalid();
1255 return false;
1256 }
1257 // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there
1258 // must actually be at least one array element; even a VLA cannot have a
1259 // bound of zero. And if our index is nonzero, we already had a CCEDiag.
1260 return true;
1261}
1262
1263void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info,
1264 const Expr *E) {
1265 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsized_array_indexed);
1266 // Do not set the designator as invalid: we can represent this situation,
1267 // and correct handling of __builtin_object_size requires us to do so.
1268}
1269
1270void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
1271 const Expr *E,
1272 const APSInt &N) {
1273 // If we're complaining, we must be able to statically determine the size of
1274 // the most derived array.
1275 if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement)
1276 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1277 << N << /*array*/ 0
1278 << static_cast<unsigned>(getMostDerivedArraySize());
1279 else
1280 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1281 << N << /*non-array*/ 1;
1282 setInvalid();
1283}
1284
1285CallStackFrame::CallStackFrame(EvalInfo &Info, SourceRange CallRange,
1286 const FunctionDecl *Callee, const LValue *This,
1287 const Expr *CallExpr, CallRef Call)
1288 : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This),
1289 CallExpr(CallExpr), Arguments(Call), CallRange(CallRange),
1290 Index(Info.NextCallIndex++) {
1291 Info.CurrentCall = this;
1292 ++Info.CallStackDepth;
1293}
1294
1295CallStackFrame::~CallStackFrame() {
1296 assert(Info.CurrentCall == this && "calls retired out of order");
1297 --Info.CallStackDepth;
1298 Info.CurrentCall = Caller;
1299}
1300
1301static bool isRead(AccessKinds AK) {
1302 return AK == AK_Read || AK == AK_ReadObjectRepresentation ||
1303 AK == AK_IsWithinLifetime || AK == AK_Dereference;
1304}
1305
1306static bool isModification(AccessKinds AK) {
1307 switch (AK) {
1308 case AK_Read:
1309 case AK_ReadObjectRepresentation:
1310 case AK_MemberCall:
1311 case AK_DynamicCast:
1312 case AK_TypeId:
1313 case AK_IsWithinLifetime:
1314 case AK_Dereference:
1315 return false;
1316 case AK_Assign:
1317 case AK_Increment:
1318 case AK_Decrement:
1319 case AK_Construct:
1320 case AK_Destroy:
1321 return true;
1322 }
1323 llvm_unreachable("unknown access kind");
1324}
1325
1326static bool isAnyAccess(AccessKinds AK) {
1327 return isRead(AK) || isModification(AK);
1328}
1329
1330/// Is this an access per the C++ definition?
1331static bool isFormalAccess(AccessKinds AK) {
1332 return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy &&
1333 AK != AK_IsWithinLifetime && AK != AK_Dereference;
1334}
1335
1336/// Is this kind of access valid on an indeterminate object value?
1337static bool isValidIndeterminateAccess(AccessKinds AK) {
1338 switch (AK) {
1339 case AK_Read:
1340 case AK_Increment:
1341 case AK_Decrement:
1342 case AK_Dereference:
1343 // These need the object's value.
1344 return false;
1345
1346 case AK_IsWithinLifetime:
1347 case AK_ReadObjectRepresentation:
1348 case AK_Assign:
1349 case AK_Construct:
1350 case AK_Destroy:
1351 // Construction and destruction don't need the value.
1352 return true;
1353
1354 case AK_MemberCall:
1355 case AK_DynamicCast:
1356 case AK_TypeId:
1357 // These aren't really meaningful on scalars.
1358 return true;
1359 }
1360 llvm_unreachable("unknown access kind");
1361}
1362
1363namespace {
1364 struct ComplexValue {
1365 private:
1366 bool IsInt;
1367
1368 public:
1369 APSInt IntReal, IntImag;
1370 APFloat FloatReal, FloatImag;
1371
1372 ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {}
1373
1374 void makeComplexFloat() { IsInt = false; }
1375 bool isComplexFloat() const { return !IsInt; }
1376 APFloat &getComplexFloatReal() { return FloatReal; }
1377 APFloat &getComplexFloatImag() { return FloatImag; }
1378
1379 void makeComplexInt() { IsInt = true; }
1380 bool isComplexInt() const { return IsInt; }
1381 APSInt &getComplexIntReal() { return IntReal; }
1382 APSInt &getComplexIntImag() { return IntImag; }
1383
1384 void moveInto(APValue &v) const {
1385 if (isComplexFloat())
1386 v = APValue(FloatReal, FloatImag);
1387 else
1388 v = APValue(IntReal, IntImag);
1389 }
1390 void setFrom(const APValue &v) {
1391 assert(v.isComplexFloat() || v.isComplexInt());
1392 if (v.isComplexFloat()) {
1393 makeComplexFloat();
1394 FloatReal = v.getComplexFloatReal();
1395 FloatImag = v.getComplexFloatImag();
1396 } else {
1397 makeComplexInt();
1398 IntReal = v.getComplexIntReal();
1399 IntImag = v.getComplexIntImag();
1400 }
1401 }
1402 };
1403
1404 struct LValue {
1405 APValue::LValueBase Base;
1406 CharUnits Offset;
1407 SubobjectDesignator Designator;
1408 bool IsNullPtr : 1;
1409 bool InvalidBase : 1;
1410 // P2280R4 track if we have an unknown reference or pointer.
1411 bool AllowConstexprUnknown = false;
1412
1413 const APValue::LValueBase getLValueBase() const { return Base; }
1414 bool allowConstexprUnknown() const { return AllowConstexprUnknown; }
1415 CharUnits &getLValueOffset() { return Offset; }
1416 CharUnits getLValueOffset() const { return Offset; }
1417 SubobjectDesignator &getLValueDesignator() { return Designator; }
1418 const SubobjectDesignator &getLValueDesignator() const { return Designator;}
1419 bool isNullPointer() const { return IsNullPtr;}
1420
1421 unsigned getLValueCallIndex() const { return Base.getCallIndex(); }
1422 unsigned getLValueVersion() const { return Base.getVersion(); }
1423
1424 bool pointsToCompleteClass(const CXXRecordDecl *D) const {
1425 if (Designator.Entries.empty())
1426 return true;
1427
1428 return Designator.MostDerivedType->getAsCXXRecordDecl() == D;
1429 }
1430
1431 void moveInto(APValue &V) const {
1432 if (Designator.Invalid)
1433 V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr);
1434 else {
1435 assert(!InvalidBase && "APValues can't handle invalid LValue bases");
1436 V = APValue(Base, Offset, Designator.Entries,
1437 Designator.IsOnePastTheEnd, IsNullPtr);
1438 }
1439 if (AllowConstexprUnknown)
1440 V.setConstexprUnknown();
1441 }
1442 void setFrom(const ASTContext &Ctx, const APValue &V) {
1443 assert(V.isLValue() && "Setting LValue from a non-LValue?");
1444 Base = V.getLValueBase();
1445 Offset = V.getLValueOffset();
1446 InvalidBase = false;
1447 Designator = SubobjectDesignator(Ctx, V);
1448 IsNullPtr = V.isNullPointer();
1449 AllowConstexprUnknown = V.allowConstexprUnknown();
1450 }
1451
1452 void set(APValue::LValueBase B, bool BInvalid = false) {
1453#ifndef NDEBUG
1454 // We only allow a few types of invalid bases. Enforce that here.
1455 if (BInvalid) {
1456 const auto *E = B.get<const Expr *>();
1457 assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) &&
1458 "Unexpected type of invalid base");
1459 }
1460#endif
1461
1462 Base = B;
1463 Offset = CharUnits::fromQuantity(Quantity: 0);
1464 InvalidBase = BInvalid;
1465 Designator = SubobjectDesignator(getType(B));
1466 IsNullPtr = false;
1467 AllowConstexprUnknown = false;
1468 }
1469
1470 void setNull(ASTContext &Ctx, QualType PointerTy) {
1471 Base = (const ValueDecl *)nullptr;
1472 Offset =
1473 CharUnits::fromQuantity(Quantity: Ctx.getTargetNullPointerValue(QT: PointerTy));
1474 InvalidBase = false;
1475 Designator = SubobjectDesignator(PointerTy->getPointeeType());
1476 IsNullPtr = true;
1477 AllowConstexprUnknown = false;
1478 }
1479
1480 void setInvalid(APValue::LValueBase B, unsigned I = 0) {
1481 set(B, BInvalid: true);
1482 }
1483
1484 std::string toString(ASTContext &Ctx, QualType T) const {
1485 APValue Printable;
1486 moveInto(V&: Printable);
1487 return Printable.getAsString(Ctx, Ty: T);
1488 }
1489
1490 private:
1491 // Check that this LValue is not based on a null pointer. If it is, produce
1492 // a diagnostic and mark the designator as invalid.
1493 template <typename GenDiagType>
1494 bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) {
1495 if (Designator.Invalid)
1496 return false;
1497 if (IsNullPtr) {
1498 GenDiag();
1499 Designator.setInvalid();
1500 return false;
1501 }
1502 return true;
1503 }
1504
1505 public:
1506 bool checkNullPointer(EvalInfo &Info, const Expr *E,
1507 CheckSubobjectKind CSK) {
1508 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, CSK] {
1509 Info.CCEDiag(E, DiagId: diag::note_constexpr_null_subobject) << CSK;
1510 });
1511 }
1512
1513 bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E,
1514 AccessKinds AK) {
1515 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, AK] {
1516 if (AK == AccessKinds::AK_Dereference)
1517 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
1518 else
1519 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
1520 });
1521 }
1522
1523 // Check this LValue refers to an object. If not, set the designator to be
1524 // invalid and emit a diagnostic.
1525 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {
1526 return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) &&
1527 Designator.checkSubobject(Info, E, CSK);
1528 }
1529
1530 void addDecl(EvalInfo &Info, const Expr *E,
1531 const Decl *D, bool Virtual = false) {
1532 if (checkSubobject(Info, E, CSK: isa<FieldDecl>(Val: D) ? CSK_Field : CSK_Base))
1533 Designator.addDeclUnchecked(D, Virtual);
1534 }
1535 void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) {
1536 if (!Designator.Entries.empty()) {
1537 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsupported_unsized_array);
1538 Designator.setInvalid();
1539 return;
1540 }
1541 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer)) {
1542 assert(!Base || getType(Base).getNonReferenceType()->isPointerType() ||
1543 getType(Base).getNonReferenceType()->isArrayType());
1544 Designator.FirstEntryIsAnUnsizedArray = true;
1545 Designator.addUnsizedArrayUnchecked(ElemTy);
1546 }
1547 }
1548 void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {
1549 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer))
1550 Designator.addArrayUnchecked(CAT);
1551 }
1552 void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) {
1553 if (checkSubobject(Info, E, CSK: Imag ? CSK_Imag : CSK_Real))
1554 Designator.addComplexUnchecked(EltTy, Imag);
1555 }
1556 void addVectorElement(EvalInfo &Info, const Expr *E, QualType EltTy,
1557 uint64_t Size, uint64_t Idx) {
1558 if (checkSubobject(Info, E, CSK: CSK_VectorElement))
1559 Designator.addVectorElementUnchecked(EltTy, Size, Idx);
1560 }
1561 void clearIsNullPointer() {
1562 IsNullPtr = false;
1563 }
1564 void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E,
1565 const APSInt &Index, CharUnits ElementSize) {
1566 // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB,
1567 // but we're not required to diagnose it and it's valid in C++.)
1568 if (!Index)
1569 return;
1570
1571 // Compute the new offset in the appropriate width, wrapping at 64 bits.
1572 // FIXME: When compiling for a 32-bit target, we should use 32-bit
1573 // offsets.
1574 uint64_t Offset64 = Offset.getQuantity();
1575 uint64_t ElemSize64 = ElementSize.getQuantity();
1576 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
1577 Offset = CharUnits::fromQuantity(Quantity: Offset64 + ElemSize64 * Index64);
1578
1579 if (checkNullPointer(Info, E, CSK: CSK_ArrayIndex))
1580 Designator.adjustIndex(Info, E, N: Index, LV: *this);
1581 clearIsNullPointer();
1582 }
1583 void adjustOffset(CharUnits N) {
1584 Offset += N;
1585 if (N.getQuantity())
1586 clearIsNullPointer();
1587 }
1588 };
1589
1590 struct MemberPtr {
1591 MemberPtr() {}
1592 explicit MemberPtr(const ValueDecl *Decl)
1593 : DeclAndIsDerivedMember(Decl, false) {}
1594
1595 /// The member or (direct or indirect) field referred to by this member
1596 /// pointer, or 0 if this is a null member pointer.
1597 const ValueDecl *getDecl() const {
1598 return DeclAndIsDerivedMember.getPointer();
1599 }
1600 /// Is this actually a member of some type derived from the relevant class?
1601 bool isDerivedMember() const {
1602 return DeclAndIsDerivedMember.getInt();
1603 }
1604 /// Get the class which the declaration actually lives in.
1605 const CXXRecordDecl *getContainingRecord() const {
1606 return cast<CXXRecordDecl>(
1607 Val: DeclAndIsDerivedMember.getPointer()->getDeclContext());
1608 }
1609
1610 void moveInto(APValue &V) const {
1611 V = APValue(getDecl(), isDerivedMember(), Path);
1612 }
1613 void setFrom(const APValue &V) {
1614 assert(V.isMemberPointer());
1615 DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());
1616 DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());
1617 Path.clear();
1618 llvm::append_range(C&: Path, R: V.getMemberPointerPath());
1619 }
1620
1621 /// DeclAndIsDerivedMember - The member declaration, and a flag indicating
1622 /// whether the member is a member of some class derived from the class type
1623 /// of the member pointer.
1624 llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;
1625 /// Path - The path of base/derived classes from the member declaration's
1626 /// class (exclusive) to the class type of the member pointer (inclusive).
1627 SmallVector<const CXXRecordDecl*, 4> Path;
1628
1629 /// Perform a cast towards the class of the Decl (either up or down the
1630 /// hierarchy).
1631 bool castBack(const CXXRecordDecl *Class) {
1632 assert(!Path.empty());
1633 const CXXRecordDecl *Expected;
1634 if (Path.size() >= 2)
1635 Expected = Path[Path.size() - 2];
1636 else
1637 Expected = getContainingRecord();
1638 if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {
1639 // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
1640 // if B does not contain the original member and is not a base or
1641 // derived class of the class containing the original member, the result
1642 // of the cast is undefined.
1643 // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
1644 // (D::*). We consider that to be a language defect.
1645 return false;
1646 }
1647 Path.pop_back();
1648 return true;
1649 }
1650 /// Perform a base-to-derived member pointer cast.
1651 bool castToDerived(const CXXRecordDecl *Derived) {
1652 if (!getDecl())
1653 return true;
1654 if (!isDerivedMember()) {
1655 Path.push_back(Elt: Derived);
1656 return true;
1657 }
1658 if (!castBack(Class: Derived))
1659 return false;
1660 if (Path.empty())
1661 DeclAndIsDerivedMember.setInt(false);
1662 return true;
1663 }
1664 /// Perform a derived-to-base member pointer cast.
1665 bool castToBase(const CXXRecordDecl *Base) {
1666 if (!getDecl())
1667 return true;
1668 if (Path.empty())
1669 DeclAndIsDerivedMember.setInt(true);
1670 if (isDerivedMember()) {
1671 Path.push_back(Elt: Base);
1672 return true;
1673 }
1674 return castBack(Class: Base);
1675 }
1676 };
1677
1678 /// Compare two member pointers, which are assumed to be of the same type.
1679 static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {
1680 if (!LHS.getDecl() || !RHS.getDecl())
1681 return !LHS.getDecl() && !RHS.getDecl();
1682 if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())
1683 return false;
1684 return LHS.Path == RHS.Path;
1685 }
1686}
1687
1688void SubobjectDesignator::adjustIndex(EvalInfo &Info, const Expr *E, APSInt N,
1689 const LValue &LV) {
1690 if (Invalid || !N)
1691 return;
1692 uint64_t TruncatedN = N.extOrTrunc(width: 64).getZExtValue();
1693 if (isMostDerivedAnUnsizedArray()) {
1694 diagnoseUnsizedArrayPointerArithmetic(Info, E);
1695 // Can't verify -- trust that the user is doing the right thing (or if
1696 // not, trust that the caller will catch the bad behavior).
1697 // FIXME: Should we reject if this overflows, at least?
1698 Entries.back() =
1699 PathEntry::ArrayIndex(Index: Entries.back().getAsArrayIndex() + TruncatedN);
1700 return;
1701 }
1702
1703 // [expr.add]p4: For the purposes of these operators, a pointer to a
1704 // nonarray object behaves the same as a pointer to the first element of
1705 // an array of length one with the type of the object as its element type.
1706 bool IsArray =
1707 MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement;
1708 uint64_t ArrayIndex =
1709 IsArray ? Entries.back().getAsArrayIndex() : (uint64_t)IsOnePastTheEnd;
1710 uint64_t ArraySize = IsArray ? getMostDerivedArraySize() : (uint64_t)1;
1711
1712 if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) {
1713 if (!Info.checkingPotentialConstantExpression() ||
1714 !LV.AllowConstexprUnknown) {
1715 // Calculate the actual index in a wide enough type, so we can include
1716 // it in the note.
1717 N = N.extend(width: std::max<unsigned>(a: N.getBitWidth() + 1, b: 65));
1718 (llvm::APInt &)N += ArrayIndex;
1719 assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index");
1720 diagnosePointerArithmetic(Info, E, N);
1721 }
1722 setInvalid();
1723 return;
1724 }
1725
1726 ArrayIndex += TruncatedN;
1727 assert(ArrayIndex <= ArraySize &&
1728 "bounds check succeeded for out-of-bounds index");
1729
1730 if (IsArray)
1731 Entries.back() = PathEntry::ArrayIndex(Index: ArrayIndex);
1732 else
1733 IsOnePastTheEnd = (ArrayIndex != 0);
1734}
1735
1736static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E);
1737static bool EvaluateInPlace(APValue &Result, EvalInfo &Info,
1738 const LValue &This, const Expr *E,
1739 bool AllowNonLiteralTypes = false);
1740static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
1741 bool InvalidBaseOK = false);
1742static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info,
1743 bool InvalidBaseOK = false);
1744static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
1745 EvalInfo &Info);
1746static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info);
1747static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
1748static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
1749 EvalInfo &Info);
1750static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
1751static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
1752static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info);
1753static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
1754 EvalInfo &Info);
1755static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result);
1756static std::optional<uint64_t>
1757EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
1758 std::string *StringResult = nullptr);
1759
1760/// Evaluate an integer or fixed point expression into an APResult.
1761static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
1762 EvalInfo &Info);
1763
1764/// Evaluate only a fixed point expression into an APResult.
1765static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
1766 EvalInfo &Info);
1767
1768//===----------------------------------------------------------------------===//
1769// Misc utilities
1770//===----------------------------------------------------------------------===//
1771
1772/// Negate an APSInt in place, converting it to a signed form if necessary, and
1773/// preserving its value (by extending by up to one bit as needed).
1774static void negateAsSigned(APSInt &Int) {
1775 if (Int.isUnsigned() || Int.isMinSignedValue()) {
1776 Int = Int.extend(width: Int.getBitWidth() + 1);
1777 Int.setIsSigned(true);
1778 }
1779 Int = -Int;
1780}
1781
1782template<typename KeyT>
1783APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T,
1784 ScopeKind Scope, LValue &LV) {
1785 unsigned Version = getTempVersion();
1786 APValue::LValueBase Base(Key, Index, Version);
1787 LV.set(B: Base);
1788 return createLocal(Base, Key, T, Scope);
1789}
1790
1791/// Allocate storage for a parameter of a function call made in this frame.
1792APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD,
1793 LValue &LV) {
1794 assert(Args.CallIndex == Index && "creating parameter in wrong frame");
1795 APValue::LValueBase Base(PVD, Index, Args.Version);
1796 LV.set(B: Base);
1797 // We always destroy parameters at the end of the call, even if we'd allow
1798 // them to live to the end of the full-expression at runtime, in order to
1799 // give portable results and match other compilers.
1800 return createLocal(Base, Key: PVD, T: PVD->getType(), Scope: ScopeKind::Call);
1801}
1802
1803APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key,
1804 QualType T, ScopeKind Scope) {
1805 assert(Base.getCallIndex() == Index && "lvalue for wrong frame");
1806 unsigned Version = Base.getVersion();
1807 APValue &Result = Temporaries[MapKeyTy(Key, Version)];
1808 assert(Result.isAbsent() && "local created multiple times");
1809
1810 // If we're creating a local immediately in the operand of a speculative
1811 // evaluation, don't register a cleanup to be run outside the speculative
1812 // evaluation context, since we won't actually be able to initialize this
1813 // object.
1814 if (Index <= Info.SpeculativeEvaluationDepth) {
1815 if (T.isDestructedType())
1816 Info.noteSideEffect();
1817 } else {
1818 Info.CleanupStack.push_back(Elt: Cleanup(&Result, Base, T, Scope));
1819 }
1820 return Result;
1821}
1822
1823CharUnits clang::GetAlignOfDynamicAlloc(const ASTContext &Ctx,
1824 QualType AllocType,
1825 DynAllocKind AllocKind) {
1826 assert((AllocKind != DynAllocKind::None) &&
1827 "should only be called on dynamically allocated blocks");
1828 assert((AllocKind != DynAllocKind::BuiltinOperatorNew) &&
1829 "__builtin_operator_new should have been allowed only from "
1830 "std::allocator::allocate");
1831
1832 const TargetInfo &TI = Ctx.getTargetInfo();
1833 uint64_t DefaultNewAlign = TI.getNewAlign();
1834
1835 uint64_t TypeAlignment = Ctx.getTypeAlign(T: AllocType);
1836 assert(TypeAlignment > 0 && "Unknown alignment for allocated type!");
1837
1838 uint64_t AllocSize = Ctx.getTypeSize(T: AllocType);
1839
1840 if (AllocSize == 0) {
1841 switch (AllocKind) {
1842 // Allocating a zero-sized array is allowed, however it doesn't have
1843 // any alignment guarantees.
1844 case DynAllocKind::ArrayNew:
1845 case DynAllocKind::StdAllocator:
1846 return CharUnits::One();
1847
1848 // Flexible array members are allowed as only member as an extension.
1849 // In this case the size of the type will be zero, but the allocation
1850 // should still be suitable for the array element type.
1851 case DynAllocKind::New:
1852 return Ctx.toCharUnitsFromBits(BitSize: TypeAlignment);
1853
1854 default:
1855 llvm_unreachable("Unhandled DynAllocKind");
1856 }
1857 }
1858
1859 assert(TypeAlignment <= AllocSize && "Invalid alignment/size for type!");
1860 assert(AllocSize % TypeAlignment == 0 && "Invalid alignment/size for type!");
1861
1862 // For new-extended alignment the ::operator new overload with
1863 // std::align_val_t parameter is used. According to C++
1864 // [basic.stc.dynamic.allocation]p3.1 this overload returns memory
1865 // according to the requested alignment. No stricter guarantees are
1866 // made.
1867 if (TypeAlignment > DefaultNewAlign)
1868 return Ctx.toCharUnitsFromBits(BitSize: TypeAlignment);
1869
1870 switch (AllocKind) {
1871 // According to C++ [allocator.members]p5 it is unspecified how the
1872 // memory obtained from ::operator new is used by
1873 // std::allocator::allocate, therefore be conservative here.
1874 case DynAllocKind::StdAllocator:
1875 return Ctx.toCharUnitsFromBits(BitSize: TypeAlignment);
1876
1877 // The non-array form of new does not permit allocation overhead and
1878 // therefore provides alignment as guaranteed by ::operator new.
1879 // According to C++ [basic.stc.dynamic.allocation]p3.3 the allocation
1880 // is suitably aligned for all objects without new-extended alignment
1881 // with the exact size of the allocation. An object of the exact size
1882 // AllocSize can have alignment of at most the lowest bit set in
1883 // AllocSize.
1884 case DynAllocKind::New:
1885 return Ctx.toCharUnitsFromBits(
1886 BitSize: std::min(a: DefaultNewAlign, b: uint64_t(1) << llvm::countr_zero(Val: AllocSize)));
1887
1888 case DynAllocKind::ArrayNew: {
1889 const Type *ET = AllocType.getTypePtr()
1890 ->getArrayElementTypeNoTypeQual()
1891 ->getCanonicalTypeUnqualified()
1892 .getTypePtr();
1893 // According to C++ [expr.new]p17, unless the element type of an
1894 // array new expression is char, unsigned char or std::byte, the
1895 // allocation may be offset into the allocation returned by
1896 // ::operator new[]. Therefore no stricter alignment than the type's
1897 // alignment is guaranteed. For char, unsigned char and std::byte
1898 if (!ET->isSpecificBuiltinType(K: BuiltinType::UChar) &&
1899 !ET->isSpecificBuiltinType(K: BuiltinType::Char_U) &&
1900 !ET->isSpecificBuiltinType(K: BuiltinType::Char_S) && !ET->isStdByteType())
1901 return Ctx.toCharUnitsFromBits(BitSize: TypeAlignment);
1902
1903 // Otherwise, the allocation is offset from the result of ::operator
1904 // new[] by a multiple of the strictest fundamental alignment.
1905 // According C++ [basic.stc.dynamic.allocation]p3.2 the allocation
1906 // returned by ::operator new[] is suitably aligned for all objects
1907 // without new-extended alignment and size up to the allocated size.
1908 uint64_t MaxFundamentalAlign =
1909 std::max(a: TI.getLongLongAlign(), b: TI.getLongDoubleAlign());
1910 return Ctx.toCharUnitsFromBits(BitSize: std::min(
1911 l: {DefaultNewAlign, MaxFundamentalAlign, llvm::bit_floor(Value: AllocSize)}));
1912 }
1913
1914 default:
1915 llvm_unreachable("Unhandled DynAllocKind");
1916 }
1917}
1918
1919APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) {
1920 if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) {
1921 FFDiag(E, DiagId: diag::note_constexpr_heap_alloc_limit_exceeded);
1922 return nullptr;
1923 }
1924
1925 DynamicAllocLValue DA(NumHeapAllocs++, DynAlloc::kindOfExpr(AllocExpr: E));
1926 LV.set(B: APValue::LValueBase::getDynamicAlloc(LV: DA, Type: T));
1927 auto Result = HeapAllocs.emplace(args: std::piecewise_construct,
1928 args: std::forward_as_tuple(args&: DA), args: std::tuple<>());
1929 assert(Result.second && "reused a heap alloc index?");
1930 Result.first->second.AllocExpr = E;
1931 return &Result.first->second.Value;
1932}
1933
1934/// Produce a string describing the given constexpr call.
1935void CallStackFrame::describe(raw_ostream &Out) const {
1936 bool IsMemberCall = false;
1937 bool ExplicitInstanceParam = false;
1938 clang::PrintingPolicy PrintingPolicy = Info.Ctx.getPrintingPolicy();
1939 PrintingPolicy.SuppressLambdaBody = true;
1940
1941 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: Callee)) {
1942 IsMemberCall = !isa<CXXConstructorDecl>(Val: MD) && !MD->isStatic();
1943 ExplicitInstanceParam = MD->isExplicitObjectMemberFunction();
1944 }
1945
1946 if (!IsMemberCall)
1947 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1948 /*Qualified=*/false);
1949
1950 if (This && IsMemberCall) {
1951 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(Val: CallExpr)) {
1952 const Expr *Object = MCE->getImplicitObjectArgument();
1953 Object->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1954 /*Indentation=*/0);
1955 if (Object->getType()->isPointerType())
1956 Out << "->";
1957 else
1958 Out << ".";
1959 } else if (const auto *OCE =
1960 dyn_cast_if_present<CXXOperatorCallExpr>(Val: CallExpr)) {
1961 OCE->getArg(Arg: 0)->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1962 /*Indentation=*/0);
1963 Out << ".";
1964 } else {
1965 APValue Val;
1966 This->moveInto(V&: Val);
1967 Val.printPretty(
1968 OS&: Out, Ctx: Info.Ctx,
1969 Ty: Info.Ctx.getLValueReferenceType(T: This->Designator.MostDerivedType));
1970 Out << ".";
1971 }
1972 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1973 /*Qualified=*/false);
1974 }
1975
1976 Out << '(';
1977
1978 llvm::ListSeparator Comma;
1979 for (const ParmVarDecl *Param :
1980 Callee->parameters().slice(N: ExplicitInstanceParam)) {
1981 Out << Comma;
1982 const APValue *V = Info.getParamSlot(Call: Arguments, PVD: Param);
1983 if (V)
1984 V->printPretty(OS&: Out, Ctx: Info.Ctx, Ty: Param->getType());
1985 else
1986 Out << "<...>";
1987 }
1988
1989 Out << ')';
1990}
1991
1992/// Evaluate an expression to see if it had side-effects, and discard its
1993/// result.
1994/// \return \c true if the caller should keep evaluating.
1995static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) {
1996 assert(!E->isValueDependent());
1997 APValue Scratch;
1998 if (!Evaluate(Result&: Scratch, Info, E))
1999 // We don't need the value, but we might have skipped a side effect here.
2000 return Info.noteSideEffect();
2001 return true;
2002}
2003
2004static bool IsOpaqueConstantCall(const LValue &LVal) {
2005 const auto *BaseExpr =
2006 llvm::dyn_cast_if_present<CallExpr>(Val: LVal.Base.dyn_cast<const Expr *>());
2007 return BaseExpr && isOpaqueConstantCall(E: BaseExpr);
2008}
2009
2010static bool IsGlobalLValue(APValue::LValueBase B) {
2011 if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>())
2012 return true;
2013
2014 return isGlobalLValue(D: B.dyn_cast<const ValueDecl *>(),
2015 E: B.dyn_cast<const Expr *>());
2016}
2017
2018bool isGlobalLValue(const ValueDecl *D, const Expr *E) {
2019 // C++11 [expr.const]p3 An address constant expression is a prvalue core
2020 // constant expression of pointer type that evaluates to...
2021
2022 // ... a null pointer value, or a prvalue core constant expression of type
2023 // std::nullptr_t.
2024 if (!D && !E)
2025 return true;
2026
2027 if (D) {
2028 // ... the address of an object with static storage duration,
2029 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
2030 return VD->hasGlobalStorage();
2031 if (isa<TemplateParamObjectDecl>(Val: D))
2032 return true;
2033 // ... the address of a function,
2034 // ... the address of a GUID [MS extension],
2035 // ... the address of an unnamed global constant
2036 return isa<FunctionDecl, MSGuidDecl, UnnamedGlobalConstantDecl>(Val: D);
2037 }
2038
2039 assert(E);
2040
2041 switch (E->getStmtClass()) {
2042 default:
2043 return false;
2044 case Expr::CompoundLiteralExprClass: {
2045 const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(Val: E);
2046 return CLE->isFileScope() && CLE->isLValue();
2047 }
2048 case Expr::MaterializeTemporaryExprClass:
2049 // A materialized temporary might have been lifetime-extended to static
2050 // storage duration.
2051 return cast<MaterializeTemporaryExpr>(Val: E)->getStorageDuration() == SD_Static;
2052 // A string literal has static storage duration.
2053 case Expr::StringLiteralClass:
2054 case Expr::PredefinedExprClass:
2055 case Expr::ObjCStringLiteralClass:
2056 case Expr::ObjCEncodeExprClass:
2057 return true;
2058 case Expr::ObjCBoxedExprClass:
2059 case Expr::ObjCArrayLiteralClass:
2060 case Expr::ObjCDictionaryLiteralClass:
2061 return cast<ObjCObjectLiteral>(Val: E)->isExpressibleAsConstantInitializer();
2062 case Expr::CallExprClass:
2063 return isOpaqueConstantCall(E: cast<CallExpr>(Val: E));
2064 // For GCC compatibility, &&label has static storage duration.
2065 case Expr::AddrLabelExprClass:
2066 return true;
2067 // A Block literal expression may be used as the initialization value for
2068 // Block variables at global or local static scope.
2069 case Expr::BlockExprClass:
2070 return !cast<BlockExpr>(Val: E)->getBlockDecl()->hasCaptures();
2071 // The APValue generated from a __builtin_source_location will be emitted as a
2072 // literal.
2073 case Expr::SourceLocExprClass:
2074 return true;
2075 case Expr::ImplicitValueInitExprClass:
2076 // FIXME:
2077 // We can never form an lvalue with an implicit value initialization as its
2078 // base through expression evaluation, so these only appear in one case: the
2079 // implicit variable declaration we invent when checking whether a constexpr
2080 // constructor can produce a constant expression. We must assume that such
2081 // an expression might be a global lvalue.
2082 return true;
2083 }
2084
2085 llvm_unreachable("Unhandled stmt kind in switch with default?");
2086}
2087
2088static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
2089 return LVal.Base.dyn_cast<const ValueDecl*>();
2090}
2091
2092// Information about an LValueBase that is some kind of string.
2093struct LValueBaseString {
2094 std::string ObjCEncodeStorage;
2095 StringRef Bytes;
2096 int CharWidth;
2097};
2098
2099// Gets the lvalue base of LVal as a string.
2100static bool GetLValueBaseAsString(const EvalInfo &Info, const LValue &LVal,
2101 LValueBaseString &AsString) {
2102 const auto *BaseExpr = LVal.Base.dyn_cast<const Expr *>();
2103 if (!BaseExpr)
2104 return false;
2105
2106 // For ObjCEncodeExpr, we need to compute and store the string.
2107 if (const auto *EE = dyn_cast<ObjCEncodeExpr>(Val: BaseExpr)) {
2108 Info.Ctx.getObjCEncodingForType(T: EE->getEncodedType(),
2109 S&: AsString.ObjCEncodeStorage);
2110 AsString.Bytes = AsString.ObjCEncodeStorage;
2111 AsString.CharWidth = 1;
2112 return true;
2113 }
2114
2115 // Otherwise, we have a StringLiteral.
2116 const auto *Lit = dyn_cast<StringLiteral>(Val: BaseExpr);
2117 if (const auto *PE = dyn_cast<PredefinedExpr>(Val: BaseExpr))
2118 Lit = PE->getFunctionName();
2119
2120 if (!Lit)
2121 return false;
2122
2123 AsString.Bytes = Lit->getBytes();
2124 AsString.CharWidth = Lit->getCharByteWidth();
2125 return true;
2126}
2127
2128// Determine whether two string literals potentially overlap. This will be the
2129// case if they agree on the values of all the bytes on the overlapping region
2130// between them.
2131//
2132// The overlapping region is the portion of the two string literals that must
2133// overlap in memory if the pointers actually point to the same address at
2134// runtime. For example, if LHS is "abcdef" + 3 and RHS is "cdef\0gh" + 1 then
2135// the overlapping region is "cdef\0", which in this case does agree, so the
2136// strings are potentially overlapping. Conversely, for "foobar" + 3 versus
2137// "bazbar" + 3, the overlapping region contains all of both strings, so they
2138// are not potentially overlapping, even though they agree from the given
2139// addresses onwards.
2140//
2141// See open core issue CWG2765 which is discussing the desired rule here.
2142static bool ArePotentiallyOverlappingStringLiterals(const EvalInfo &Info,
2143 const LValue &LHS,
2144 const LValue &RHS) {
2145 LValueBaseString LHSString, RHSString;
2146 if (!GetLValueBaseAsString(Info, LVal: LHS, AsString&: LHSString) ||
2147 !GetLValueBaseAsString(Info, LVal: RHS, AsString&: RHSString))
2148 return false;
2149
2150 // This is the byte offset to the location of the first character of LHS
2151 // within RHS. We don't need to look at the characters of one string that
2152 // would appear before the start of the other string if they were merged.
2153 CharUnits Offset = RHS.Offset - LHS.Offset;
2154 if (Offset.isNegative()) {
2155 if (LHSString.Bytes.size() < (size_t)-Offset.getQuantity())
2156 return false;
2157 LHSString.Bytes = LHSString.Bytes.drop_front(N: -Offset.getQuantity());
2158 } else {
2159 if (RHSString.Bytes.size() < (size_t)Offset.getQuantity())
2160 return false;
2161 RHSString.Bytes = RHSString.Bytes.drop_front(N: Offset.getQuantity());
2162 }
2163
2164 bool LHSIsLonger = LHSString.Bytes.size() > RHSString.Bytes.size();
2165 StringRef Longer = LHSIsLonger ? LHSString.Bytes : RHSString.Bytes;
2166 StringRef Shorter = LHSIsLonger ? RHSString.Bytes : LHSString.Bytes;
2167 int ShorterCharWidth = (LHSIsLonger ? RHSString : LHSString).CharWidth;
2168
2169 // The null terminator isn't included in the string data, so check for it
2170 // manually. If the longer string doesn't have a null terminator where the
2171 // shorter string ends, they aren't potentially overlapping.
2172 for (int NullByte : llvm::seq(Size: ShorterCharWidth)) {
2173 if (Shorter.size() + NullByte >= Longer.size())
2174 break;
2175 if (Longer[Shorter.size() + NullByte])
2176 return false;
2177 }
2178
2179 // Otherwise, they're potentially overlapping if and only if the overlapping
2180 // region is the same.
2181 return Shorter == Longer.take_front(N: Shorter.size());
2182}
2183
2184static bool IsWeakLValue(const LValue &Value) {
2185 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2186 return Decl && Decl->isWeak();
2187}
2188
2189static bool isZeroSized(const LValue &Value) {
2190 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2191 if (isa_and_nonnull<VarDecl>(Val: Decl)) {
2192 QualType Ty = Decl->getType();
2193 if (Ty->isArrayType())
2194 return Ty->isIncompleteType() ||
2195 Decl->getASTContext().getTypeSize(T: Ty) == 0;
2196 }
2197 return false;
2198}
2199
2200static bool HasSameBase(const LValue &A, const LValue &B) {
2201 if (!A.getLValueBase())
2202 return !B.getLValueBase();
2203 if (!B.getLValueBase())
2204 return false;
2205
2206 if (A.getLValueBase().getOpaqueValue() !=
2207 B.getLValueBase().getOpaqueValue())
2208 return false;
2209
2210 return A.getLValueCallIndex() == B.getLValueCallIndex() &&
2211 A.getLValueVersion() == B.getLValueVersion();
2212}
2213
2214static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) {
2215 assert(Base && "no location for a null lvalue");
2216 const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
2217
2218 // For a parameter, find the corresponding call stack frame (if it still
2219 // exists), and point at the parameter of the function definition we actually
2220 // invoked.
2221 if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(Val: VD)) {
2222 unsigned Idx = PVD->getFunctionScopeIndex();
2223 for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) {
2224 if (F->Arguments.CallIndex == Base.getCallIndex() &&
2225 F->Arguments.Version == Base.getVersion() && F->Callee &&
2226 Idx < F->Callee->getNumParams()) {
2227 VD = F->Callee->getParamDecl(i: Idx);
2228 break;
2229 }
2230 }
2231 }
2232
2233 if (VD)
2234 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
2235 else if (const Expr *E = Base.dyn_cast<const Expr*>())
2236 Info.Note(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
2237 else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
2238 // FIXME: Produce a note for dangling pointers too.
2239 if (std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA))
2240 Info.Note(Loc: (*Alloc)->AllocExpr->getExprLoc(),
2241 DiagId: diag::note_constexpr_dynamic_alloc_here);
2242 }
2243
2244 // We have no information to show for a typeid(T) object.
2245}
2246
2247enum class CheckEvaluationResultKind {
2248 ConstantExpression,
2249 FullyInitialized,
2250};
2251
2252/// Materialized temporaries that we've already checked to determine if they're
2253/// initializsed by a constant expression.
2254using CheckedTemporaries =
2255 llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>;
2256
2257static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2258 EvalInfo &Info, SourceLocation DiagLoc,
2259 QualType Type, const APValue &Value,
2260 ConstantExprKind Kind,
2261 const FieldDecl *SubobjectDecl,
2262 CheckedTemporaries &CheckedTemps,
2263 bool IsCompleteClass = true);
2264
2265/// Check that this reference or pointer core constant expression is a valid
2266/// value for an address or reference constant expression. Return true if we
2267/// can fold this expression, whether or not it's a constant expression.
2268static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
2269 QualType Type, const LValue &LVal,
2270 ConstantExprKind Kind,
2271 CheckedTemporaries &CheckedTemps) {
2272 bool IsReferenceType = Type->isReferenceType();
2273
2274 APValue::LValueBase Base = LVal.getLValueBase();
2275 const SubobjectDesignator &Designator = LVal.getLValueDesignator();
2276
2277 const Expr *BaseE = Base.dyn_cast<const Expr *>();
2278 const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>();
2279
2280 // Additional restrictions apply in a template argument. We only enforce the
2281 // C++20 restrictions here; additional syntactic and semantic restrictions
2282 // are applied elsewhere.
2283 if (isTemplateArgument(Kind)) {
2284 int InvalidBaseKind = -1;
2285 StringRef Ident;
2286 if (Base.is<TypeInfoLValue>())
2287 InvalidBaseKind = 0;
2288 else if (isa_and_nonnull<StringLiteral>(Val: BaseE))
2289 InvalidBaseKind = 1;
2290 else if (isa_and_nonnull<MaterializeTemporaryExpr>(Val: BaseE) ||
2291 isa_and_nonnull<LifetimeExtendedTemporaryDecl>(Val: BaseVD))
2292 InvalidBaseKind = 2;
2293 else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(Val: BaseE)) {
2294 InvalidBaseKind = 3;
2295 Ident = PE->getIdentKindName();
2296 }
2297
2298 if (InvalidBaseKind != -1) {
2299 Info.FFDiag(Loc, DiagId: diag::note_constexpr_invalid_template_arg)
2300 << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind
2301 << Ident;
2302 return false;
2303 }
2304 }
2305
2306 if (auto *FD = dyn_cast_or_null<FunctionDecl>(Val: BaseVD);
2307 FD && FD->isImmediateFunction()) {
2308 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible)
2309 << !Type->isAnyPointerType();
2310 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2311 return false;
2312 }
2313
2314 // Check that the object is a global. Note that the fake 'this' object we
2315 // manufacture when checking potential constant expressions is conservatively
2316 // assumed to be global here.
2317 if (!IsGlobalLValue(B: Base)) {
2318 if (Info.getLangOpts().CPlusPlus11) {
2319 Info.FFDiag(Loc, DiagId: diag::note_constexpr_non_global, ExtraNotes: 1)
2320 << IsReferenceType << !Designator.Entries.empty() << !!BaseVD
2321 << BaseVD;
2322 auto *VarD = dyn_cast_or_null<VarDecl>(Val: BaseVD);
2323 if (VarD && VarD->isConstexpr()) {
2324 // Non-static local constexpr variables have unintuitive semantics:
2325 // constexpr int a = 1;
2326 // constexpr const int *p = &a;
2327 // ... is invalid because the address of 'a' is not constant. Suggest
2328 // adding a 'static' in this case.
2329 Info.Note(Loc: VarD->getLocation(), DiagId: diag::note_constexpr_not_static)
2330 << VarD
2331 << FixItHint::CreateInsertion(InsertionLoc: VarD->getBeginLoc(), Code: "static ");
2332 } else {
2333 NoteLValueLocation(Info, Base);
2334 }
2335 } else {
2336 Info.FFDiag(Loc);
2337 }
2338 // Don't allow references to temporaries to escape.
2339 return false;
2340 }
2341 assert((Info.checkingPotentialConstantExpression() ||
2342 LVal.getLValueCallIndex() == 0) &&
2343 "have call index for global lvalue");
2344
2345 if (LVal.allowConstexprUnknown()) {
2346 if (BaseVD) {
2347 Info.FFDiag(Loc, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << BaseVD;
2348 NoteLValueLocation(Info, Base);
2349 } else {
2350 Info.FFDiag(Loc);
2351 }
2352 return false;
2353 }
2354
2355 if (Base.is<DynamicAllocLValue>()) {
2356 Info.FFDiag(Loc, DiagId: diag::note_constexpr_dynamic_alloc)
2357 << IsReferenceType << !Designator.Entries.empty();
2358 NoteLValueLocation(Info, Base);
2359 return false;
2360 }
2361
2362 if (BaseVD) {
2363 if (const VarDecl *Var = dyn_cast<const VarDecl>(Val: BaseVD)) {
2364 // Check if this is a thread-local variable.
2365 if (Var->getTLSKind())
2366 // FIXME: Diagnostic!
2367 return false;
2368
2369 // A dllimport variable never acts like a constant, unless we're
2370 // evaluating a value for use only in name mangling, and unless it's a
2371 // static local. For the latter case, we'd still need to evaluate the
2372 // constant expression in case we're inside a (inlined) function.
2373 if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>() &&
2374 !Var->isStaticLocal())
2375 return false;
2376
2377 // Address of a managed variable is never a constant expression.
2378 if (Info.getLangOpts().CUDA && Var->hasAttr<HIPManagedAttr>())
2379 return false;
2380
2381 // In CUDA/HIP device compilation, only device side variables have
2382 // constant addresses.
2383 if (Info.getLangOpts().CUDA && Info.getLangOpts().CUDAIsDevice &&
2384 Info.Ctx.CUDAConstantEvalCtx.NoWrongSidedVars) {
2385 if ((!Var->hasAttr<CUDADeviceAttr>() &&
2386 !Var->hasAttr<CUDAConstantAttr>() &&
2387 !Var->getType()->isCUDADeviceBuiltinSurfaceType() &&
2388 !Var->getType()->isCUDADeviceBuiltinTextureType()))
2389 return false;
2390 }
2391 }
2392 if (const auto *FD = dyn_cast<const FunctionDecl>(Val: BaseVD)) {
2393 // __declspec(dllimport) must be handled very carefully:
2394 // We must never initialize an expression with the thunk in C++.
2395 // Doing otherwise would allow the same id-expression to yield
2396 // different addresses for the same function in different translation
2397 // units. However, this means that we must dynamically initialize the
2398 // expression with the contents of the import address table at runtime.
2399 //
2400 // The C language has no notion of ODR; furthermore, it has no notion of
2401 // dynamic initialization. This means that we are permitted to
2402 // perform initialization with the address of the thunk.
2403 if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) &&
2404 FD->hasAttr<DLLImportAttr>())
2405 // FIXME: Diagnostic!
2406 return false;
2407 }
2408 } else if (const auto *MTE =
2409 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: BaseE)) {
2410 if (CheckedTemps.insert(Ptr: MTE).second) {
2411 QualType TempType = getType(B: Base);
2412 if (TempType.isDestructedType()) {
2413 Info.FFDiag(Loc: MTE->getExprLoc(),
2414 DiagId: diag::note_constexpr_unsupported_temporary_nontrivial_dtor)
2415 << TempType;
2416 return false;
2417 }
2418
2419 APValue *V = MTE->getOrCreateValue(MayCreate: false);
2420 assert(V && "evasluation result refers to uninitialised temporary");
2421 if (!CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2422 Info, DiagLoc: MTE->getExprLoc(), Type: TempType, Value: *V, Kind,
2423 /*SubobjectDecl=*/nullptr, CheckedTemps))
2424 return false;
2425 }
2426 }
2427
2428 // Allow address constant expressions to be past-the-end pointers. This is
2429 // an extension: the standard requires them to point to an object.
2430 if (!IsReferenceType)
2431 return true;
2432
2433 // A reference constant expression must refer to an object.
2434 if (!Base) {
2435 // FIXME: diagnostic
2436 Info.CCEDiag(Loc);
2437 return true;
2438 }
2439
2440 // Does this refer one past the end of some object?
2441 if (!Designator.Invalid && Designator.isOnePastTheEnd()) {
2442 Info.FFDiag(Loc, DiagId: diag::note_constexpr_past_end, ExtraNotes: 1)
2443 << !Designator.Entries.empty() << !!BaseVD << BaseVD;
2444 NoteLValueLocation(Info, Base);
2445 }
2446
2447 return true;
2448}
2449
2450/// Member pointers are constant expressions unless they point to a
2451/// non-virtual dllimport member function.
2452static bool CheckMemberPointerConstantExpression(EvalInfo &Info,
2453 SourceLocation Loc,
2454 QualType Type,
2455 const APValue &Value,
2456 ConstantExprKind Kind) {
2457 const ValueDecl *Member = Value.getMemberPointerDecl();
2458 const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Val: Member);
2459 if (!FD)
2460 return true;
2461 if (FD->isImmediateFunction()) {
2462 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible) << /*pointer*/ 0;
2463 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2464 return false;
2465 }
2466 return isForManglingOnly(Kind) || FD->isVirtual() ||
2467 !FD->hasAttr<DLLImportAttr>();
2468}
2469
2470/// Check that this core constant expression is of literal type, and if not,
2471/// produce an appropriate diagnostic.
2472static bool CheckLiteralType(EvalInfo &Info, const Expr *E,
2473 const LValue *This = nullptr) {
2474 // The restriction to literal types does not exist in C++23 anymore.
2475 if (Info.getLangOpts().CPlusPlus23)
2476 return true;
2477
2478 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Info.Ctx))
2479 return true;
2480
2481 // C++1y: A constant initializer for an object o [...] may also invoke
2482 // constexpr constructors for o and its subobjects even if those objects
2483 // are of non-literal class types.
2484 //
2485 // C++11 missed this detail for aggregates, so classes like this:
2486 // struct foo_t { union { int i; volatile int j; } u; };
2487 // are not (obviously) initializable like so:
2488 // __attribute__((__require_constant_initialization__))
2489 // static const foo_t x = {{0}};
2490 // because "i" is a subobject with non-literal initialization (due to the
2491 // volatile member of the union). See:
2492 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677
2493 // Therefore, we use the C++1y behavior.
2494 if (This && Info.EvaluatingDecl == This->getLValueBase())
2495 return true;
2496
2497 // Prvalue constant expressions must be of literal types.
2498 if (Info.getLangOpts().CPlusPlus11)
2499 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral)
2500 << E->getType();
2501 else
2502 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
2503 return false;
2504}
2505
2506static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2507 EvalInfo &Info, SourceLocation DiagLoc,
2508 QualType Type, const APValue &Value,
2509 ConstantExprKind Kind,
2510 const FieldDecl *SubobjectDecl,
2511 CheckedTemporaries &CheckedTemps,
2512 bool IsCompleteClass) {
2513 if (!Value.hasValue()) {
2514 if (SubobjectDecl) {
2515 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2516 << /*(name)*/ 1 << SubobjectDecl;
2517 Info.Note(Loc: SubobjectDecl->getLocation(),
2518 DiagId: diag::note_constexpr_subobject_declared_here);
2519 } else {
2520 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2521 << /*of type*/ 0 << Type;
2522 }
2523 return false;
2524 }
2525
2526 // We allow _Atomic(T) to be initialized from anything that T can be
2527 // initialized from.
2528 if (const AtomicType *AT = Type->getAs<AtomicType>())
2529 Type = AT->getValueType();
2530
2531 // Core issue 1454: For a literal constant expression of array or class type,
2532 // each subobject of its value shall have been initialized by a constant
2533 // expression.
2534 if (Value.isArray()) {
2535 QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();
2536 for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {
2537 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2538 Value: Value.getArrayInitializedElt(I), Kind,
2539 SubobjectDecl, CheckedTemps))
2540 return false;
2541 }
2542 if (!Value.hasArrayFiller())
2543 return true;
2544 return CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2545 Value: Value.getArrayFiller(), Kind, SubobjectDecl,
2546 CheckedTemps);
2547 }
2548 if (Value.isUnion() && Value.getUnionField()) {
2549 return CheckEvaluationResult(
2550 CERK, Info, DiagLoc, Type: Value.getUnionField()->getType(),
2551 Value: Value.getUnionValue(), Kind, SubobjectDecl: Value.getUnionField(), CheckedTemps);
2552 }
2553 if (Value.isStruct()) {
2554 auto *RD = Type->castAsRecordDecl();
2555 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2556 unsigned BaseIndex = 0;
2557 for (const CXXBaseSpecifier &BS : CD->bases()) {
2558 if (BS.isVirtual())
2559 continue;
2560 const APValue &BaseValue = Value.getStructBase(i: BaseIndex);
2561 if (!BaseValue.hasValue()) {
2562 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2563 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2564 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2565 return false;
2566 }
2567 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(), Value: BaseValue,
2568 Kind, /*SubobjectDecl=*/nullptr,
2569 CheckedTemps, /*IsCompleteClass=*/false))
2570 return false;
2571 ++BaseIndex;
2572 }
2573 }
2574 for (const auto *I : RD->fields()) {
2575 if (I->isUnnamedBitField())
2576 continue;
2577
2578 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: I->getType(),
2579 Value: Value.getStructField(i: I->getFieldIndex()), Kind,
2580 SubobjectDecl: I, CheckedTemps))
2581 return false;
2582 }
2583
2584 if (IsCompleteClass) {
2585 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2586 unsigned BaseIndex = 0;
2587 for (const CXXBaseSpecifier &BS : CD->vbases()) {
2588 assert(BS.isVirtual());
2589 const APValue &BaseValue = Value.getStructVirtualBase(i: BaseIndex);
2590 if (!BaseValue.hasValue()) {
2591 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2592 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2593 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2594 return false;
2595 }
2596 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(),
2597 Value: BaseValue, Kind, /*SubobjectDecl=*/nullptr,
2598 CheckedTemps, /*IsCompleteClass=*/false))
2599 return false;
2600 ++BaseIndex;
2601 }
2602 }
2603 }
2604 }
2605
2606 if (Value.isLValue() &&
2607 CERK == CheckEvaluationResultKind::ConstantExpression) {
2608 LValue LVal;
2609 LVal.setFrom(Ctx: Info.Ctx, V: Value);
2610 return CheckLValueConstantExpression(Info, Loc: DiagLoc, Type, LVal, Kind,
2611 CheckedTemps);
2612 }
2613
2614 if (Value.isMemberPointer() &&
2615 CERK == CheckEvaluationResultKind::ConstantExpression)
2616 return CheckMemberPointerConstantExpression(Info, Loc: DiagLoc, Type, Value, Kind);
2617
2618 // Everything else is fine.
2619 return true;
2620}
2621
2622/// Check that this core constant expression value is a valid value for a
2623/// constant expression. If not, report an appropriate diagnostic. Does not
2624/// check that the expression is of literal type.
2625static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
2626 QualType Type, const APValue &Value,
2627 ConstantExprKind Kind) {
2628 // Nothing to check for a constant expression of type 'cv void'.
2629 if (Type->isVoidType())
2630 return true;
2631
2632 CheckedTemporaries CheckedTemps;
2633 return CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2634 Info, DiagLoc, Type, Value, Kind,
2635 /*SubobjectDecl=*/nullptr, CheckedTemps);
2636}
2637
2638/// Check that this evaluated value is fully-initialized and can be loaded by
2639/// an lvalue-to-rvalue conversion.
2640static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc,
2641 QualType Type, const APValue &Value) {
2642 CheckedTemporaries CheckedTemps;
2643 return CheckEvaluationResult(
2644 CERK: CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value,
2645 Kind: ConstantExprKind::Normal, /*SubobjectDecl=*/nullptr, CheckedTemps);
2646}
2647
2648/// Enforce C++2a [expr.const]/4.17, which disallows new-expressions unless
2649/// "the allocated storage is deallocated within the evaluation".
2650static bool CheckMemoryLeaks(EvalInfo &Info) {
2651 if (!Info.HeapAllocs.empty()) {
2652 // We can still fold to a constant despite a compile-time memory leak,
2653 // so long as the heap allocation isn't referenced in the result (we check
2654 // that in CheckConstantExpression).
2655 Info.CCEDiag(E: Info.HeapAllocs.begin()->second.AllocExpr,
2656 DiagId: diag::note_constexpr_memory_leak)
2657 << unsigned(Info.HeapAllocs.size() - 1);
2658 }
2659 return true;
2660}
2661
2662static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {
2663 // A null base expression indicates a null pointer. These are always
2664 // evaluatable, and they are false unless the offset is zero.
2665 if (!Value.getLValueBase()) {
2666 // TODO: Should a non-null pointer with an offset of zero evaluate to true?
2667 Result = !Value.getLValueOffset().isZero();
2668 return true;
2669 }
2670
2671 // We have a non-null base. These are generally known to be true, but if it's
2672 // a weak declaration it can be null at runtime.
2673 Result = true;
2674 const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
2675 return !Decl || !Decl->isWeak();
2676}
2677
2678static bool HandleConversionToBool(const APValue &Val, bool &Result) {
2679 // TODO: This function should produce notes if it fails.
2680 switch (Val.getKind()) {
2681 case APValue::None:
2682 case APValue::Indeterminate:
2683 return false;
2684 case APValue::Int:
2685 Result = Val.getInt().getBoolValue();
2686 return true;
2687 case APValue::FixedPoint:
2688 Result = Val.getFixedPoint().getBoolValue();
2689 return true;
2690 case APValue::Float:
2691 Result = !Val.getFloat().isZero();
2692 return true;
2693 case APValue::ComplexInt:
2694 Result = Val.getComplexIntReal().getBoolValue() ||
2695 Val.getComplexIntImag().getBoolValue();
2696 return true;
2697 case APValue::ComplexFloat:
2698 Result = !Val.getComplexFloatReal().isZero() ||
2699 !Val.getComplexFloatImag().isZero();
2700 return true;
2701 case APValue::LValue:
2702 return EvalPointerValueAsBool(Value: Val, Result);
2703 case APValue::MemberPointer:
2704 if (Val.getMemberPointerDecl() && Val.getMemberPointerDecl()->isWeak()) {
2705 return false;
2706 }
2707 Result = Val.getMemberPointerDecl();
2708 return true;
2709 case APValue::Vector:
2710 case APValue::Matrix:
2711 case APValue::Array:
2712 case APValue::Struct:
2713 case APValue::Union:
2714 case APValue::AddrLabelDiff:
2715 case APValue::Reflection:
2716 return false;
2717 }
2718
2719 llvm_unreachable("unknown APValue kind");
2720}
2721
2722static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
2723 EvalInfo &Info) {
2724 assert(!E->isValueDependent());
2725 assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition");
2726 APValue Val;
2727 if (!Evaluate(Result&: Val, Info, E))
2728 return false;
2729 return HandleConversionToBool(Val, Result);
2730}
2731
2732template<typename T>
2733static bool HandleOverflow(EvalInfo &Info, const Expr *E,
2734 const T &SrcValue, QualType DestType) {
2735 Info.CCEDiag(E, DiagId: diag::note_constexpr_overflow) << SrcValue << DestType;
2736 if (const auto *OBT = DestType->getAs<OverflowBehaviorType>();
2737 OBT && OBT->isTrapKind()) {
2738 return false;
2739 }
2740 return Info.noteUndefinedBehavior();
2741}
2742
2743static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
2744 QualType SrcType, const APFloat &Value,
2745 QualType DestType, APSInt &Result) {
2746 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2747 // Determine whether we are converting to unsigned or signed.
2748 bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
2749
2750 Result = APSInt(DestWidth, !DestSigned);
2751 bool ignored;
2752 if (Value.convertToInteger(Result, RM: llvm::APFloat::rmTowardZero, IsExact: &ignored)
2753 & APFloat::opInvalidOp)
2754 return HandleOverflow(Info, E, SrcValue: Value, DestType);
2755 return true;
2756}
2757
2758/// Get rounding mode to use in evaluation of the specified expression.
2759///
2760/// If rounding mode is unknown at compile time, still try to evaluate the
2761/// expression. If the result is exact, it does not depend on rounding mode.
2762/// So return "tonearest" mode instead of "dynamic".
2763static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E) {
2764 llvm::RoundingMode RM =
2765 E->getFPFeaturesInEffect(LO: Info.getLangOpts()).getRoundingMode();
2766 if (RM == llvm::RoundingMode::Dynamic)
2767 RM = llvm::RoundingMode::NearestTiesToEven;
2768 return RM;
2769}
2770
2771/// Check if the given floating-point evaluation result is allowed for
2772/// compile-time constant folding during translation (as opposed to mandatory
2773/// constant expression evaluation).
2774static bool checkFloatingPointResultForConstantFolding(EvalInfo &Info,
2775 const Expr *E,
2776 APFloat::opStatus St) {
2777 // In a constant context, assume that any dynamic rounding mode or FP
2778 // exception state matches the default floating-point environment.
2779 if (Info.InConstantContext)
2780 return true;
2781
2782 FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.getLangOpts());
2783 if ((St & APFloat::opInexact) &&
2784 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
2785 // Inexact result means that it depends on rounding mode. If the requested
2786 // mode is dynamic, the evaluation cannot be made in compile time.
2787 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
2788 return false;
2789 }
2790
2791 if ((St != APFloat::opOK) &&
2792 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||
2793 FPO.getExceptionMode() != LangOptions::FPE_Ignore ||
2794 FPO.getAllowFEnvAccess())) {
2795 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
2796 return false;
2797 }
2798
2799 if ((St & APFloat::opStatus::opInvalidOp) &&
2800 FPO.getExceptionMode() != LangOptions::FPE_Ignore) {
2801 // There is no usefully definable result.
2802 Info.FFDiag(E);
2803 return false;
2804 }
2805
2806 // FIXME: if:
2807 // - evaluation triggered other FP exception, and
2808 // - exception mode is not "ignore", and
2809 // - the expression being evaluated is not a part of global variable
2810 // initializer,
2811 // the evaluation probably need to be rejected.
2812 return true;
2813}
2814
2815static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
2816 QualType SrcType, QualType DestType,
2817 APFloat &Result) {
2818 assert((isa<CastExpr>(E) || isa<CompoundAssignOperator>(E) ||
2819 isa<ConvertVectorExpr>(E)) &&
2820 "HandleFloatToFloatCast has been checked with only CastExpr, "
2821 "CompoundAssignOperator and ConvertVectorExpr. Please either validate "
2822 "the new expression or address the root cause of this usage.");
2823 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2824 APFloat::opStatus St;
2825 APFloat Value = Result;
2826 bool ignored;
2827 St = Result.convert(ToSemantics: Info.Ctx.getFloatTypeSemantics(T: DestType), RM, losesInfo: &ignored);
2828 return checkFloatingPointResultForConstantFolding(Info, E, St);
2829}
2830
2831static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
2832 QualType DestType, QualType SrcType,
2833 const APSInt &Value) {
2834 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2835 // Figure out if this is a truncate, extend or noop cast.
2836 // If the input is signed, do a sign extend, noop, or truncate.
2837 APSInt Result = Value.extOrTrunc(width: DestWidth);
2838 Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
2839 if (DestType->isBooleanType())
2840 Result = Value.getBoolValue();
2841 return Result;
2842}
2843
2844static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
2845 const FPOptions FPO,
2846 QualType SrcType, const APSInt &Value,
2847 QualType DestType, APFloat &Result) {
2848 Result = APFloat(Info.Ctx.getFloatTypeSemantics(T: DestType), 1);
2849 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2850 APFloat::opStatus St = Result.convertFromAPInt(Input: Value, IsSigned: Value.isSigned(), RM);
2851 return checkFloatingPointResultForConstantFolding(Info, E, St);
2852}
2853
2854static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E,
2855 APValue &Value, const FieldDecl *FD) {
2856 assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield");
2857
2858 if (!Value.isInt()) {
2859 // Trying to store a pointer-cast-to-integer into a bitfield.
2860 // FIXME: In this case, we should provide the diagnostic for casting
2861 // a pointer to an integer.
2862 assert(Value.isLValue() && "integral value neither int nor lvalue?");
2863 Info.FFDiag(E);
2864 return false;
2865 }
2866
2867 APSInt &Int = Value.getInt();
2868 unsigned OldBitWidth = Int.getBitWidth();
2869 unsigned NewBitWidth = FD->getBitWidthValue();
2870 if (NewBitWidth < OldBitWidth)
2871 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
2872 return true;
2873}
2874
2875/// Perform the given integer operation, which is known to need at most BitWidth
2876/// bits, and check for overflow in the original type (if that type was not an
2877/// unsigned type).
2878template<typename Operation>
2879static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
2880 const APSInt &LHS, const APSInt &RHS,
2881 unsigned BitWidth, Operation Op,
2882 APSInt &Result) {
2883 if (LHS.isUnsigned()) {
2884 Result = Op(LHS, RHS);
2885 return true;
2886 }
2887
2888 APSInt Value(Op(LHS.extend(width: BitWidth), RHS.extend(width: BitWidth)), false);
2889 Result = Value.trunc(width: LHS.getBitWidth());
2890 if (Result.extend(width: BitWidth) != Value && !E->getType().isWrapType()) {
2891 if (Info.checkingForUndefinedBehavior())
2892 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
2893 DiagID: diag::warn_integer_constant_overflow)
2894 << toString(I: Result, Radix: 10, Signed: Result.isSigned(), /*formatAsCLiteral=*/false,
2895 /*UpperCase=*/true, /*InsertSeparators=*/true)
2896 << E->getType() << E->getSourceRange();
2897 return HandleOverflow(Info, E, SrcValue: Value, DestType: E->getType());
2898 }
2899 return true;
2900}
2901
2902/// Perform the given binary integer operation.
2903static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E,
2904 const APSInt &LHS, BinaryOperatorKind Opcode,
2905 APSInt RHS, APSInt &Result) {
2906 bool HandleOverflowResult = true;
2907 switch (Opcode) {
2908 default:
2909 Info.FFDiag(E);
2910 return false;
2911 case BO_Mul:
2912 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() * 2,
2913 Op: std::multiplies<APSInt>(), Result);
2914 case BO_Add:
2915 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2916 Op: std::plus<APSInt>(), Result);
2917 case BO_Sub:
2918 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2919 Op: std::minus<APSInt>(), Result);
2920 case BO_And: Result = LHS & RHS; return true;
2921 case BO_Xor: Result = LHS ^ RHS; return true;
2922 case BO_Or: Result = LHS | RHS; return true;
2923 case BO_Div:
2924 case BO_Rem:
2925 if (RHS == 0) {
2926 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero)
2927 << E->getRHS()->getSourceRange();
2928 return false;
2929 }
2930 // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports
2931 // this operation and gives the two's complement result.
2932 if (RHS.isNegative() && RHS.isAllOnes() && LHS.isSigned() &&
2933 LHS.isMinSignedValue())
2934 HandleOverflowResult = HandleOverflow(
2935 Info, E, SrcValue: -LHS.extend(width: LHS.getBitWidth() + 1), DestType: E->getType());
2936 Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS);
2937 return HandleOverflowResult;
2938 case BO_Shl: {
2939 if (Info.getLangOpts().OpenCL)
2940 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2941 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2942 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2943 RHS.isUnsigned());
2944 else if (RHS.isSigned() && RHS.isNegative()) {
2945 // During constant-folding, a negative shift is an opposite shift. Such
2946 // a shift is not a constant expression.
2947 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2948 if (!Info.noteUndefinedBehavior())
2949 return false;
2950 RHS = -RHS;
2951 goto shift_right;
2952 }
2953 shift_left:
2954 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
2955 // the shifted type.
2956 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2957 if (SA != RHS) {
2958 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
2959 << RHS << E->getType() << LHS.getBitWidth();
2960 if (!Info.noteUndefinedBehavior())
2961 return false;
2962 } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) {
2963 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
2964 // operand, and must not overflow the corresponding unsigned type.
2965 // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to
2966 // E1 x 2^E2 module 2^N.
2967 if (LHS.isNegative()) {
2968 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_of_negative) << LHS;
2969 if (!Info.noteUndefinedBehavior())
2970 return false;
2971 } else if (LHS.countl_zero() < SA) {
2972 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_discards);
2973 if (!Info.noteUndefinedBehavior())
2974 return false;
2975 }
2976 }
2977 Result = LHS << SA;
2978 return true;
2979 }
2980 case BO_Shr: {
2981 if (Info.getLangOpts().OpenCL)
2982 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2983 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2984 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2985 RHS.isUnsigned());
2986 else if (RHS.isSigned() && RHS.isNegative()) {
2987 // During constant-folding, a negative shift is an opposite shift. Such a
2988 // shift is not a constant expression.
2989 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2990 if (!Info.noteUndefinedBehavior())
2991 return false;
2992 RHS = -RHS;
2993 goto shift_left;
2994 }
2995 shift_right:
2996 // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
2997 // shifted type.
2998 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2999 if (SA != RHS) {
3000 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
3001 << RHS << E->getType() << LHS.getBitWidth();
3002 if (!Info.noteUndefinedBehavior())
3003 return false;
3004 }
3005
3006 Result = LHS >> SA;
3007 return true;
3008 }
3009
3010 case BO_LT: Result = LHS < RHS; return true;
3011 case BO_GT: Result = LHS > RHS; return true;
3012 case BO_LE: Result = LHS <= RHS; return true;
3013 case BO_GE: Result = LHS >= RHS; return true;
3014 case BO_EQ: Result = LHS == RHS; return true;
3015 case BO_NE: Result = LHS != RHS; return true;
3016 case BO_Cmp:
3017 llvm_unreachable("BO_Cmp should be handled elsewhere");
3018 }
3019}
3020
3021/// Perform the given binary floating-point operation, in-place, on LHS.
3022static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E,
3023 APFloat &LHS, BinaryOperatorKind Opcode,
3024 const APFloat &RHS) {
3025 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
3026 APFloat::opStatus St;
3027 switch (Opcode) {
3028 default:
3029 Info.FFDiag(E);
3030 return false;
3031 case BO_Mul:
3032 St = LHS.multiply(RHS, RM);
3033 break;
3034 case BO_Add:
3035 St = LHS.add(RHS, RM);
3036 break;
3037 case BO_Sub:
3038 St = LHS.subtract(RHS, RM);
3039 break;
3040 case BO_Div:
3041 // [expr.mul]p4:
3042 // If the second operand of / or % is zero the behavior is undefined.
3043 if (RHS.isZero())
3044 Info.CCEDiag(E, DiagId: diag::note_expr_divide_by_zero);
3045 St = LHS.divide(RHS, RM);
3046 break;
3047 }
3048
3049 // FIXME: The standard quote below is deleted by P3899R3.
3050 // [expr.pre]p4:
3051 // If during the evaluation of an expression, the result is not
3052 // mathematically defined [...], the behavior is undefined.
3053 // FIXME: C++ rules require us to not conform to IEEE 754 here.
3054 // FIXME: The NaN check should not be applied outside of "constant contexts"
3055 // because it prevents NaN propagation and the "invalid" status is the
3056 // responsibility of checkFloatingPointResultForConstantFolding.
3057 if (LHS.isNaN()) {
3058 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << LHS.isNaN();
3059 return Info.noteUndefinedBehavior();
3060 }
3061
3062 return checkFloatingPointResultForConstantFolding(Info, E, St);
3063}
3064
3065static bool handleLogicalOpForVector(const APInt &LHSValue,
3066 BinaryOperatorKind Opcode,
3067 const APInt &RHSValue, APInt &Result) {
3068 bool LHS = (LHSValue != 0);
3069 bool RHS = (RHSValue != 0);
3070
3071 if (Opcode == BO_LAnd)
3072 Result = LHS && RHS;
3073 else
3074 Result = LHS || RHS;
3075 return true;
3076}
3077static bool handleLogicalOpForVector(const APFloat &LHSValue,
3078 BinaryOperatorKind Opcode,
3079 const APFloat &RHSValue, APInt &Result) {
3080 bool LHS = !LHSValue.isZero();
3081 bool RHS = !RHSValue.isZero();
3082
3083 if (Opcode == BO_LAnd)
3084 Result = LHS && RHS;
3085 else
3086 Result = LHS || RHS;
3087 return true;
3088}
3089
3090static bool handleLogicalOpForVector(const APValue &LHSValue,
3091 BinaryOperatorKind Opcode,
3092 const APValue &RHSValue, APInt &Result) {
3093 // The result is always an int type, however operands match the first.
3094 if (LHSValue.getKind() == APValue::Int)
3095 return handleLogicalOpForVector(LHSValue: LHSValue.getInt(), Opcode,
3096 RHSValue: RHSValue.getInt(), Result);
3097 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3098 return handleLogicalOpForVector(LHSValue: LHSValue.getFloat(), Opcode,
3099 RHSValue: RHSValue.getFloat(), Result);
3100}
3101
3102template <typename APTy>
3103static bool
3104handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode,
3105 const APTy &RHSValue, APInt &Result) {
3106 switch (Opcode) {
3107 default:
3108 llvm_unreachable("unsupported binary operator");
3109 case BO_EQ:
3110 Result = (LHSValue == RHSValue);
3111 break;
3112 case BO_NE:
3113 Result = (LHSValue != RHSValue);
3114 break;
3115 case BO_LT:
3116 Result = (LHSValue < RHSValue);
3117 break;
3118 case BO_GT:
3119 Result = (LHSValue > RHSValue);
3120 break;
3121 case BO_LE:
3122 Result = (LHSValue <= RHSValue);
3123 break;
3124 case BO_GE:
3125 Result = (LHSValue >= RHSValue);
3126 break;
3127 }
3128
3129 // The boolean operations on these vector types use an instruction that
3130 // results in a mask of '-1' for the 'truth' value. Ensure that we negate 1
3131 // to -1 to make sure that we produce the correct value.
3132 Result.negate();
3133
3134 return true;
3135}
3136
3137static bool handleCompareOpForVector(const APValue &LHSValue,
3138 BinaryOperatorKind Opcode,
3139 const APValue &RHSValue, APInt &Result) {
3140 // The result is always an int type, however operands match the first.
3141 if (LHSValue.getKind() == APValue::Int)
3142 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getInt(), Opcode,
3143 RHSValue: RHSValue.getInt(), Result);
3144 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3145 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getFloat(), Opcode,
3146 RHSValue: RHSValue.getFloat(), Result);
3147}
3148
3149// Perform binary operations for vector types, in place on the LHS.
3150static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E,
3151 BinaryOperatorKind Opcode,
3152 APValue &LHSValue,
3153 const APValue &RHSValue) {
3154 assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI &&
3155 "Operation not supported on vector types");
3156
3157 const auto *VT = E->getType()->castAs<VectorType>();
3158 unsigned NumElements = VT->getNumElements();
3159 QualType EltTy = VT->getElementType();
3160
3161 // In the cases (typically C as I've observed) where we aren't evaluating
3162 // constexpr but are checking for cases where the LHS isn't yet evaluatable,
3163 // just give up.
3164 if (!LHSValue.isVector()) {
3165 assert(LHSValue.isLValue() &&
3166 "A vector result that isn't a vector OR uncalculated LValue");
3167 Info.FFDiag(E);
3168 return false;
3169 }
3170
3171 assert(LHSValue.getVectorLength() == NumElements &&
3172 RHSValue.getVectorLength() == NumElements && "Different vector sizes");
3173
3174 SmallVector<APValue, 4> ResultElements;
3175
3176 for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) {
3177 APValue LHSElt = LHSValue.getVectorElt(I: EltNum);
3178 APValue RHSElt = RHSValue.getVectorElt(I: EltNum);
3179
3180 if (EltTy->isIntegerType()) {
3181 APSInt EltResult{Info.Ctx.getIntWidth(T: EltTy),
3182 EltTy->isUnsignedIntegerType()};
3183 bool Success = true;
3184
3185 if (BinaryOperator::isLogicalOp(Opc: Opcode))
3186 Success = handleLogicalOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3187 else if (BinaryOperator::isComparisonOp(Opc: Opcode))
3188 Success = handleCompareOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3189 else
3190 Success = handleIntIntBinOp(Info, E, LHS: LHSElt.getInt(), Opcode,
3191 RHS: RHSElt.getInt(), Result&: EltResult);
3192
3193 if (!Success) {
3194 Info.FFDiag(E);
3195 return false;
3196 }
3197 ResultElements.emplace_back(Args&: EltResult);
3198
3199 } else if (EltTy->isFloatingType()) {
3200 assert(LHSElt.getKind() == APValue::Float &&
3201 RHSElt.getKind() == APValue::Float &&
3202 "Mismatched LHS/RHS/Result Type");
3203 APFloat LHSFloat = LHSElt.getFloat();
3204
3205 if (!handleFloatFloatBinOp(Info, E, LHS&: LHSFloat, Opcode,
3206 RHS: RHSElt.getFloat())) {
3207 Info.FFDiag(E);
3208 return false;
3209 }
3210
3211 ResultElements.emplace_back(Args&: LHSFloat);
3212 }
3213 }
3214
3215 LHSValue = APValue(ResultElements.data(), ResultElements.size());
3216 return true;
3217}
3218
3219/// Cast an lvalue referring to a base subobject to a derived class, by
3220/// truncating the lvalue's path to the given length.
3221static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
3222 const RecordDecl *TruncatedType,
3223 unsigned TruncatedElements) {
3224 SubobjectDesignator &D = Result.Designator;
3225
3226 // Check we actually point to a derived class object.
3227 if (TruncatedElements == D.Entries.size())
3228 return true;
3229 assert(TruncatedElements >= D.MostDerivedPathLength &&
3230 "not casting to a derived class");
3231 if (!Result.checkSubobject(Info, E, CSK: CSK_Derived))
3232 return false;
3233
3234 // Truncate the path to the subobject, and remove any derived-to-base offsets.
3235 const RecordDecl *RD = TruncatedType;
3236 for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
3237 if (RD->isInvalidDecl()) return false;
3238 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
3239 const CXXRecordDecl *Base = getAsBaseClass(E: D.Entries[I]);
3240 if (isVirtualBaseClass(E: D.Entries[I]))
3241 Result.Offset -= Layout.getVBaseClassOffset(VBase: Base);
3242 else
3243 Result.Offset -= Layout.getBaseClassOffset(Base);
3244 RD = Base;
3245 }
3246 D.Entries.resize(N: TruncatedElements);
3247 return true;
3248}
3249
3250static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3251 const CXXRecordDecl *Derived,
3252 const CXXRecordDecl *Base,
3253 const ASTRecordLayout *RL = nullptr) {
3254 if (!RL) {
3255 if (Derived->isInvalidDecl()) return false;
3256 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3257 }
3258
3259 Obj.addDecl(Info, E, D: Base, /*Virtual=*/false);
3260 Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
3261 return true;
3262}
3263
3264static bool HandleLValueDirectVirtualBase(EvalInfo &Info, const Expr *E,
3265 LValue &Obj,
3266 const CXXRecordDecl *Derived,
3267 const CXXRecordDecl *Base,
3268 const ASTRecordLayout *RL = nullptr) {
3269 if (!RL) {
3270 if (Derived->isInvalidDecl())
3271 return false;
3272 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3273 }
3274
3275 Obj.addDecl(Info, E, D: Base, /*Virtual=*/true);
3276 Obj.getLValueOffset() += RL->getVBaseClassOffset(VBase: Base);
3277 return true;
3278}
3279
3280static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3281 const CXXRecordDecl *DerivedDecl,
3282 const CXXBaseSpecifier *Base) {
3283 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
3284
3285 if (!Base->isVirtual())
3286 return HandleLValueDirectBase(Info, E, Obj, Derived: DerivedDecl, Base: BaseDecl);
3287
3288 SubobjectDesignator &D = Obj.Designator;
3289 if (D.Invalid)
3290 return false;
3291
3292 // Extract most-derived object and corresponding type.
3293 // FIXME: After implementing P2280R4 it became possible to get references
3294 // here. We do MostDerivedType->getAsCXXRecordDecl() in several other
3295 // locations and if we see crashes in those locations in the future
3296 // it may make more sense to move this fix into Lvalue::set.
3297 DerivedDecl = D.MostDerivedType.getNonReferenceType()->getAsCXXRecordDecl();
3298 if (!CastToDerivedClass(Info, E, Result&: Obj, TruncatedType: DerivedDecl, TruncatedElements: D.MostDerivedPathLength))
3299 return false;
3300
3301 // Find the virtual base class.
3302 if (DerivedDecl->isInvalidDecl()) return false;
3303 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: DerivedDecl);
3304 Obj.addDecl(Info, E, D: BaseDecl, /*Virtual*/ true);
3305 Obj.getLValueOffset() += Layout.getVBaseClassOffset(VBase: BaseDecl);
3306 return true;
3307}
3308
3309static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E,
3310 QualType Type, LValue &Result) {
3311 for (CastExpr::path_const_iterator PathI = E->path_begin(),
3312 PathE = E->path_end();
3313 PathI != PathE; ++PathI) {
3314 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Type->getAsCXXRecordDecl(),
3315 Base: *PathI))
3316 return false;
3317 Type = (*PathI)->getType();
3318 }
3319 return true;
3320}
3321
3322/// Cast an lvalue referring to a derived class to a known base subobject.
3323static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result,
3324 const CXXRecordDecl *DerivedRD,
3325 const CXXRecordDecl *BaseRD) {
3326 CXXBasePaths Paths(/*FindAmbiguities=*/false,
3327 /*RecordPaths=*/true, /*DetectVirtual=*/false);
3328 if (!DerivedRD->isDerivedFrom(Base: BaseRD, Paths))
3329 llvm_unreachable("Class must be derived from the passed in base class!");
3330
3331 for (CXXBasePathElement &Elem : Paths.front())
3332 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Elem.Class, Base: Elem.Base))
3333 return false;
3334 return true;
3335}
3336
3337/// Update LVal to refer to the given field, which must be a member of the type
3338/// currently described by LVal.
3339static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
3340 const FieldDecl *FD,
3341 const ASTRecordLayout *RL = nullptr) {
3342 if (!RL) {
3343 const RecordDecl *RD = FD->getParent();
3344 if (RD->isInvalidDecl())
3345 return false;
3346 // There are some cases where the base is not yet complete but we haven't
3347 // disagnosed (such as in a template instantation of an attribute that
3348 // references the expression, ala enable_if). These aren't necessarily
3349 // constant expressions so we return 'false', but they might be, so we don't
3350 // diagnose.
3351 if (!RD->isCompleteDefinition())
3352 return false;
3353 RL = &Info.Ctx.getASTRecordLayout(D: RD);
3354 }
3355
3356 unsigned I = FD->getFieldIndex();
3357 LVal.addDecl(Info, E, D: FD);
3358 LVal.adjustOffset(N: Info.Ctx.toCharUnitsFromBits(BitSize: RL->getFieldOffset(FieldNo: I)));
3359 return true;
3360}
3361
3362/// Update LVal to refer to the given indirect field.
3363static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
3364 LValue &LVal,
3365 const IndirectFieldDecl *IFD) {
3366 for (const auto *C : IFD->chain())
3367 if (!HandleLValueMember(Info, E, LVal, FD: cast<FieldDecl>(Val: C)))
3368 return false;
3369 return true;
3370}
3371
3372enum class SizeOfType {
3373 SizeOf,
3374 DataSizeOf,
3375};
3376
3377/// Get the size of the given type in char units.
3378static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type,
3379 CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) {
3380 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
3381 // extension.
3382 if (Type->isVoidType() || Type->isFunctionType()) {
3383 Size = CharUnits::One();
3384 return true;
3385 }
3386
3387 if (Type->isDependentType()) {
3388 Info.FFDiag(Loc);
3389 return false;
3390 }
3391
3392 if (!Type->isConstantSizeType()) {
3393 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
3394 // FIXME: Better diagnostic.
3395 Info.FFDiag(Loc);
3396 return false;
3397 }
3398
3399 if (SOT == SizeOfType::SizeOf)
3400 Size = Info.Ctx.getTypeSizeInChars(T: Type);
3401 else
3402 Size = Info.Ctx.getTypeInfoDataSizeInChars(T: Type).Width;
3403 return true;
3404}
3405
3406/// Update a pointer value to model pointer arithmetic.
3407/// \param Info - Information about the ongoing evaluation.
3408/// \param E - The expression being evaluated, for diagnostic purposes.
3409/// \param LVal - The pointer value to be updated.
3410/// \param EltTy - The pointee type represented by LVal.
3411/// \param Adjustment - The adjustment, in objects of type EltTy, to add.
3412static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3413 LValue &LVal, QualType EltTy,
3414 APSInt Adjustment) {
3415 CharUnits SizeOfPointee;
3416 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfPointee))
3417 return false;
3418
3419 LVal.adjustOffsetAndIndex(Info, E, Index: Adjustment, ElementSize: SizeOfPointee);
3420 return true;
3421}
3422
3423static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3424 LValue &LVal, QualType EltTy,
3425 int64_t Adjustment) {
3426 return HandleLValueArrayAdjustment(Info, E, LVal, EltTy,
3427 Adjustment: APSInt::get(X: Adjustment));
3428}
3429
3430/// Update an lvalue to refer to a component of a complex number.
3431/// \param Info - Information about the ongoing evaluation.
3432/// \param LVal - The lvalue to be updated.
3433/// \param EltTy - The complex number's component type.
3434/// \param Imag - False for the real component, true for the imaginary.
3435static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E,
3436 LValue &LVal, QualType EltTy,
3437 bool Imag) {
3438 if (Imag) {
3439 CharUnits SizeOfComponent;
3440 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfComponent))
3441 return false;
3442 LVal.Offset += SizeOfComponent;
3443 }
3444 LVal.addComplex(Info, E, EltTy, Imag);
3445 return true;
3446}
3447
3448static bool HandleLValueVectorElement(EvalInfo &Info, const Expr *E,
3449 LValue &LVal, QualType EltTy,
3450 uint64_t Size, uint64_t Idx) {
3451 if (Idx) {
3452 CharUnits SizeOfElement;
3453 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfElement))
3454 return false;
3455 LVal.Offset += SizeOfElement * Idx;
3456 }
3457 LVal.addVectorElement(Info, E, EltTy, Size, Idx);
3458 return true;
3459}
3460
3461/// Try to evaluate the initializer for a variable declaration.
3462///
3463/// \param Info Information about the ongoing evaluation.
3464/// \param E An expression to be used when printing diagnostics.
3465/// \param VD The variable whose initializer should be obtained.
3466/// \param Version The version of the variable within the frame.
3467/// \param Frame The frame in which the variable was created. Must be null
3468/// if this variable is not local to the evaluation.
3469/// \param Result Filled in with a pointer to the value of the variable.
3470static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
3471 const VarDecl *VD, CallStackFrame *Frame,
3472 unsigned Version, APValue *&Result) {
3473 // C++23 [expr.const]p8 If we have a reference type allow unknown references
3474 // and pointers.
3475 bool AllowConstexprUnknown =
3476 Info.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType();
3477
3478 APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version);
3479
3480 auto CheckUninitReference = [&](bool IsLocalVariable) {
3481 if (!Result || (!Result->hasValue() && VD->getType()->isReferenceType())) {
3482 // C++23 [expr.const]p8
3483 // ... For such an object that is not usable in constant expressions, the
3484 // dynamic type of the object is constexpr-unknown. For such a reference
3485 // that is not usable in constant expressions, the reference is treated
3486 // as binding to an unspecified object of the referenced type whose
3487 // lifetime and that of all subobjects includes the entire constant
3488 // evaluation and whose dynamic type is constexpr-unknown.
3489 //
3490 // Variables that are part of the current evaluation are not
3491 // constexpr-unknown.
3492 if (!AllowConstexprUnknown || IsLocalVariable) {
3493 if (!Info.checkingPotentialConstantExpression())
3494 Info.FFDiag(E, DiagId: diag::note_constexpr_use_uninit_reference);
3495 return false;
3496 }
3497 Result = nullptr;
3498 }
3499 return true;
3500 };
3501
3502 // If this is a local variable, dig out its value.
3503 if (Frame) {
3504 Result = Frame->getTemporary(Key: VD, Version);
3505 if (Result)
3506 return CheckUninitReference(/*IsLocalVariable=*/true);
3507
3508 if (!isa<ParmVarDecl>(Val: VD)) {
3509 // Assume variables referenced within a lambda's call operator that were
3510 // not declared within the call operator are captures and during checking
3511 // of a potential constant expression, assume they are unknown constant
3512 // expressions.
3513 assert(isLambdaCallOperator(Frame->Callee) &&
3514 (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) &&
3515 "missing value for local variable");
3516 if (Info.checkingPotentialConstantExpression())
3517 return false;
3518
3519 llvm_unreachable(
3520 "A variable in a frame should either be a local or a parameter");
3521 }
3522 }
3523
3524 // If we're currently evaluating the initializer of this declaration, use that
3525 // in-flight value.
3526 if (Info.EvaluatingDecl == Base) {
3527 Result = Info.EvaluatingDeclValue;
3528 return CheckUninitReference(/*IsLocalVariable=*/false);
3529 }
3530
3531 // P2280R4 struck the restriction that variable of reference type lifetime
3532 // should begin within the evaluation of E
3533 // Used to be C++20 [expr.const]p5.12.2:
3534 // ... its lifetime began within the evaluation of E;
3535 if (isa<ParmVarDecl>(Val: VD)) {
3536 if (AllowConstexprUnknown) {
3537 Result = nullptr;
3538 return true;
3539 }
3540
3541 // Assume parameters of a potential constant expression are usable in
3542 // constant expressions.
3543 if (!Info.checkingPotentialConstantExpression() ||
3544 !Info.CurrentCall->Callee ||
3545 !Info.CurrentCall->Callee->Equals(DC: VD->getDeclContext())) {
3546 if (Info.getLangOpts().CPlusPlus11) {
3547 Info.FFDiag(E, DiagId: diag::note_constexpr_function_param_value_unknown)
3548 << VD;
3549 NoteLValueLocation(Info, Base);
3550 } else {
3551 Info.FFDiag(E);
3552 }
3553 }
3554 return false;
3555 }
3556
3557 if (E->isValueDependent())
3558 return false;
3559
3560 // Dig out the initializer, and use the declaration which it's attached to.
3561 // FIXME: We should eventually check whether the variable has a reachable
3562 // initializing declaration.
3563 const Expr *Init = VD->getAnyInitializer(D&: VD);
3564 // P2280R4 struck the restriction that variable of reference type should have
3565 // a preceding initialization.
3566 // Used to be C++20 [expr.const]p5.12:
3567 // ... reference has a preceding initialization and either ...
3568 if (!Init && !AllowConstexprUnknown) {
3569 // Don't diagnose during potential constant expression checking; an
3570 // initializer might be added later.
3571 if (!Info.checkingPotentialConstantExpression()) {
3572 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_unknown, ExtraNotes: 1)
3573 << VD;
3574 NoteLValueLocation(Info, Base);
3575 }
3576 return false;
3577 }
3578
3579 // P2280R4 struck the initialization requirement for variables of reference
3580 // type so we can no longer assume we have an Init.
3581 // Used to be C++20 [expr.const]p5.12:
3582 // ... reference has a preceding initialization and either ...
3583 if (Init && Init->isValueDependent()) {
3584 if (!Info.checkingPotentialConstantExpression()) {
3585 Info.FFDiag(E,
3586 DiagId: Info.getLangOpts().CPlusPlus11
3587 ? diag::note_constexpr_ltor_non_constexpr
3588 : diag::note_constexpr_ltor_non_integral,
3589 ExtraNotes: 1)
3590 << VD << VD->getType();
3591 NoteLValueLocation(Info, Base);
3592 }
3593
3594 // A recovery initializer can be value-dependent even when the expression
3595 // referring to the variable is not.
3596 if (Init->containsErrors())
3597 return false;
3598
3599 // The DeclRefExpr is not value-dependent, but the variable it refers to
3600 // has a value-dependent initializer. This should only happen in
3601 // constant-folding cases, where the variable is not actually of a suitable
3602 // type for use in a constant expression (otherwise the DeclRefExpr would
3603 // have been value-dependent too), so diagnose that.
3604 assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx));
3605 return false;
3606 }
3607
3608 // Check that we can fold the initializer. In C++, we will have already done
3609 // this in the cases where it matters for conformance.
3610 // P2280R4 struck the initialization requirement for variables of reference
3611 // type so we can no longer assume we have an Init.
3612 // Used to be C++20 [expr.const]p5.12:
3613 // ... reference has a preceding initialization and either ...
3614 if (Init && !VD->evaluateValue() && !AllowConstexprUnknown) {
3615 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3616 NoteLValueLocation(Info, Base);
3617 return false;
3618 }
3619
3620 // Check that the variable is actually usable in constant expressions. For a
3621 // const integral variable or a reference, we might have a non-constant
3622 // initializer that we can nonetheless evaluate the initializer for. Such
3623 // variables are not usable in constant expressions. In C++98, the
3624 // initializer also syntactically needs to be an ICE.
3625 //
3626 // FIXME: We don't diagnose cases that aren't potentially usable in constant
3627 // expressions here; doing so would regress diagnostics for things like
3628 // reading from a volatile constexpr variable.
3629 if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() &&
3630 VD->mightBeUsableInConstantExpressions(C: Info.Ctx) &&
3631 !AllowConstexprUnknown) ||
3632 ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) &&
3633 !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Context: Info.Ctx))) {
3634 if (Init) {
3635 Info.CCEDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3636 NoteLValueLocation(Info, Base);
3637 } else {
3638 Info.CCEDiag(E);
3639 }
3640 }
3641
3642 // Never use the initializer of a weak variable, not even for constant
3643 // folding. We can't be sure that this is the definition that will be used.
3644 if (VD->isWeak()) {
3645 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_weak) << VD;
3646 NoteLValueLocation(Info, Base);
3647 return false;
3648 }
3649
3650 Result = const_cast<APValue *>(VD->getEvaluatedValue());
3651
3652 if (!Result && !AllowConstexprUnknown)
3653 return false;
3654
3655 return CheckUninitReference(/*IsLocalVariable=*/false);
3656}
3657
3658/// Get the base index of the given base class within an APValue representing
3659/// the given derived class.
3660static unsigned getBaseIndex(const CXXRecordDecl *Derived,
3661 const CXXRecordDecl *Base) {
3662 Base = Base->getCanonicalDecl();
3663 unsigned Index = 0;
3664 for (const CXXBaseSpecifier &B : Derived->bases()) {
3665 if (B.isVirtual())
3666 continue;
3667 if (B.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3668 return Index;
3669 ++Index;
3670 }
3671
3672 for (const CXXBaseSpecifier &B : Derived->vbases()) {
3673 if (B.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3674 return Index;
3675 ++Index;
3676 }
3677
3678 llvm_unreachable("base class missing from derived class's bases list");
3679}
3680
3681/// Extract the value of a character from a string literal.
3682static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
3683 uint64_t Index) {
3684 assert(!isa<SourceLocExpr>(Lit) &&
3685 "SourceLocExpr should have already been converted to a StringLiteral");
3686
3687 // FIXME: Support MakeStringConstant
3688 if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Val: Lit)) {
3689 std::string Str;
3690 Info.Ctx.getObjCEncodingForType(T: ObjCEnc->getEncodedType(), S&: Str);
3691 assert(Index <= Str.size() && "Index too large");
3692 return APSInt::getUnsigned(X: Str.c_str()[Index]);
3693 }
3694
3695 if (auto PE = dyn_cast<PredefinedExpr>(Val: Lit))
3696 Lit = PE->getFunctionName();
3697 const StringLiteral *S = cast<StringLiteral>(Val: Lit);
3698 const ConstantArrayType *CAT =
3699 Info.Ctx.getAsConstantArrayType(T: S->getType());
3700 assert(CAT && "string literal isn't an array");
3701 QualType CharType = CAT->getElementType();
3702 assert(CharType->isIntegerType() && "unexpected character type");
3703 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3704 CharType->isUnsignedIntegerType());
3705 if (Index < S->getLength())
3706 Value = S->getCodeUnit(I: Index);
3707 return Value;
3708}
3709
3710// Expand a string literal into an array of characters.
3711//
3712// FIXME: This is inefficient; we should probably introduce something similar
3713// to the LLVM ConstantDataArray to make this cheaper.
3714static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S,
3715 APValue &Result,
3716 QualType AllocType = QualType()) {
3717 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
3718 T: AllocType.isNull() ? S->getType() : AllocType);
3719 assert(CAT && "string literal isn't an array");
3720 QualType CharType = CAT->getElementType();
3721 assert(CharType->isIntegerType() && "unexpected character type");
3722
3723 unsigned Elts = CAT->getZExtSize();
3724 Result = APValue(APValue::UninitArray(),
3725 std::min(a: S->getLength(), b: Elts), Elts);
3726 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3727 CharType->isUnsignedIntegerType());
3728 if (Result.hasArrayFiller())
3729 Result.getArrayFiller() = APValue(Value);
3730 for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) {
3731 Value = S->getCodeUnit(I);
3732 Result.getArrayInitializedElt(I) = APValue(Value);
3733 }
3734}
3735
3736// Expand an array so that it has more than Index filled elements.
3737static void expandArray(APValue &Array, unsigned Index) {
3738 unsigned Size = Array.getArraySize();
3739 assert(Index < Size);
3740
3741 // Always at least double the number of elements for which we store a value.
3742 unsigned OldElts = Array.getArrayInitializedElts();
3743 unsigned NewElts = std::max(a: Index+1, b: OldElts * 2);
3744 NewElts = std::min(a: Size, b: std::max(a: NewElts, b: 8u));
3745
3746 // Copy the data across.
3747 APValue NewValue(APValue::UninitArray(), NewElts, Size);
3748 for (unsigned I = 0; I != OldElts; ++I)
3749 NewValue.getArrayInitializedElt(I).swap(RHS&: Array.getArrayInitializedElt(I));
3750 for (unsigned I = OldElts; I != NewElts; ++I)
3751 NewValue.getArrayInitializedElt(I) = Array.getArrayFiller();
3752 if (NewValue.hasArrayFiller())
3753 NewValue.getArrayFiller() = Array.getArrayFiller();
3754 Array.swap(RHS&: NewValue);
3755}
3756
3757// Expand an indeterminate vector to materialize all elements.
3758static void expandVector(APValue &Vec, unsigned NumElements) {
3759 assert(Vec.isIndeterminate());
3760 SmallVector<APValue, 4> Elts(NumElements, APValue::IndeterminateValue());
3761 Vec = APValue(Elts.data(), Elts.size());
3762}
3763
3764/// Determine whether a type would actually be read by an lvalue-to-rvalue
3765/// conversion. If it's of class type, we may assume that the copy operation
3766/// is trivial. Note that this is never true for a union type with fields
3767/// (because the copy always "reads" the active member) and always true for
3768/// a non-class type.
3769bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD);
3770bool isReadByLvalueToRvalueConversion(QualType T) {
3771 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3772 return !RD || isReadByLvalueToRvalueConversion(RD);
3773}
3774bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) {
3775 // FIXME: A trivial copy of a union copies the object representation, even if
3776 // the union is empty.
3777 if (RD->isUnion())
3778 return !RD->field_empty();
3779 if (RD->isEmpty())
3780 return false;
3781
3782 for (auto *Field : RD->fields())
3783 if (!Field->isUnnamedBitField() &&
3784 isReadByLvalueToRvalueConversion(T: Field->getType()))
3785 return true;
3786
3787 for (auto &BaseSpec : RD->bases())
3788 if (isReadByLvalueToRvalueConversion(T: BaseSpec.getType()))
3789 return true;
3790
3791 return false;
3792}
3793
3794/// Diagnose an attempt to read from any unreadable field within the specified
3795/// type, which might be a class type.
3796static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK,
3797 QualType T) {
3798 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3799 if (!RD)
3800 return false;
3801
3802 if (!RD->hasMutableFields())
3803 return false;
3804
3805 for (auto *Field : RD->fields()) {
3806 // If we're actually going to read this field in some way, then it can't
3807 // be mutable. If we're in a union, then assigning to a mutable field
3808 // (even an empty one) can change the active member, so that's not OK.
3809 // FIXME: Add core issue number for the union case.
3810 if (Field->isMutable() &&
3811 (RD->isUnion() || isReadByLvalueToRvalueConversion(T: Field->getType()))) {
3812 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1) << AK << Field;
3813 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
3814 return true;
3815 }
3816
3817 if (diagnoseMutableFields(Info, E, AK, T: Field->getType()))
3818 return true;
3819 }
3820
3821 for (auto &BaseSpec : RD->bases())
3822 if (diagnoseMutableFields(Info, E, AK, T: BaseSpec.getType()))
3823 return true;
3824
3825 // All mutable fields were empty, and thus not actually read.
3826 return false;
3827}
3828
3829static bool lifetimeStartedInEvaluation(EvalInfo &Info,
3830 APValue::LValueBase Base,
3831 bool MutableSubobject = false) {
3832 // A temporary or transient heap allocation we created.
3833 if (Base.getCallIndex() || Base.is<DynamicAllocLValue>())
3834 return true;
3835
3836 switch (Info.IsEvaluatingDecl) {
3837 case EvalInfo::EvaluatingDeclKind::None:
3838 return false;
3839
3840 case EvalInfo::EvaluatingDeclKind::Ctor:
3841 // The variable whose initializer we're evaluating.
3842 if (Info.EvaluatingDecl == Base)
3843 return true;
3844
3845 // A temporary lifetime-extended by the variable whose initializer we're
3846 // evaluating.
3847 if (auto *BaseE = Base.dyn_cast<const Expr *>())
3848 if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(Val: BaseE))
3849 return Info.EvaluatingDecl == BaseMTE->getExtendingDecl();
3850 return false;
3851
3852 case EvalInfo::EvaluatingDeclKind::Dtor:
3853 // C++2a [expr.const]p6:
3854 // [during constant destruction] the lifetime of a and its non-mutable
3855 // subobjects (but not its mutable subobjects) [are] considered to start
3856 // within e.
3857 if (MutableSubobject || Base != Info.EvaluatingDecl)
3858 return false;
3859 // FIXME: We can meaningfully extend this to cover non-const objects, but
3860 // we will need special handling: we should be able to access only
3861 // subobjects of such objects that are themselves declared const.
3862 QualType T = getType(B: Base);
3863 return T.isConstQualified() || T->isReferenceType();
3864 }
3865
3866 llvm_unreachable("unknown evaluating decl kind");
3867}
3868
3869static bool CheckArraySize(EvalInfo &Info, const ConstantArrayType *CAT,
3870 SourceLocation CallLoc = {}) {
3871 return Info.CheckArraySize(
3872 Loc: CAT->getSizeExpr() ? CAT->getSizeExpr()->getBeginLoc() : CallLoc,
3873 BitWidth: CAT->getNumAddressingBits(Context: Info.Ctx), ElemCount: CAT->getZExtSize(),
3874 /*Diag=*/true);
3875}
3876
3877static bool handleScalarCast(EvalInfo &Info, const FPOptions FPO, const Expr *E,
3878 QualType SourceTy, QualType DestTy,
3879 APValue const &Original, APValue &Result) {
3880 // boolean must be checked before integer
3881 // since IsIntegerType() is true for bool
3882 if (SourceTy->isBooleanType()) {
3883 if (DestTy->isBooleanType()) {
3884 Result = Original;
3885 return true;
3886 }
3887 if (DestTy->isIntegerType() || DestTy->isRealFloatingType()) {
3888 bool BoolResult;
3889 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3890 return false;
3891 uint64_t IntResult = BoolResult;
3892 QualType IntType = DestTy->isIntegerType()
3893 ? DestTy
3894 : Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false);
3895 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: IntType));
3896 }
3897 if (DestTy->isRealFloatingType()) {
3898 APValue Result2 = APValue(APFloat(0.0));
3899 if (!HandleIntToFloatCast(Info, E, FPO,
3900 SrcType: Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false),
3901 Value: Result.getInt(), DestType: DestTy, Result&: Result2.getFloat()))
3902 return false;
3903 Result = std::move(Result2);
3904 }
3905 return true;
3906 }
3907 if (SourceTy->isIntegerType()) {
3908 if (DestTy->isRealFloatingType()) {
3909 Result = APValue(APFloat(0.0));
3910 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
3911 DestType: DestTy, Result&: Result.getFloat());
3912 }
3913 if (DestTy->isBooleanType()) {
3914 bool BoolResult;
3915 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3916 return false;
3917 uint64_t IntResult = BoolResult;
3918 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3919 return true;
3920 }
3921 if (DestTy->isIntegerType()) {
3922 Result = APValue(
3923 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
3924 return true;
3925 }
3926 } else if (SourceTy->isRealFloatingType()) {
3927 if (DestTy->isRealFloatingType()) {
3928 Result = Original;
3929 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
3930 Result&: Result.getFloat());
3931 }
3932 if (DestTy->isBooleanType()) {
3933 bool BoolResult;
3934 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3935 return false;
3936 uint64_t IntResult = BoolResult;
3937 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3938 return true;
3939 }
3940 if (DestTy->isIntegerType()) {
3941 Result = APValue(APSInt());
3942 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
3943 DestType: DestTy, Result&: Result.getInt());
3944 }
3945 }
3946
3947 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
3948 return false;
3949}
3950
3951// do the heavy lifting for casting to aggregate types
3952// because we have to deal with bitfields specially
3953static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,
3954 const Expr *E, APValue &Result,
3955 QualType ResultType,
3956 SmallVectorImpl<APValue> &Elements,
3957 SmallVectorImpl<QualType> &ElTypes) {
3958
3959 SmallVector<std::tuple<APValue *, QualType, unsigned>> WorkList = {
3960 {&Result, ResultType, 0}};
3961
3962 unsigned ElI = 0;
3963 while (!WorkList.empty() && ElI < Elements.size()) {
3964 auto [Res, Type, BitWidth] = WorkList.pop_back_val();
3965
3966 if (Type->isRealFloatingType()) {
3967 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3968 Result&: *Res))
3969 return false;
3970 ElI++;
3971 continue;
3972 }
3973 if (Type->isIntegerType()) {
3974 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3975 Result&: *Res))
3976 return false;
3977 if (BitWidth > 0) {
3978 if (!Res->isInt())
3979 return false;
3980 APSInt &Int = Res->getInt();
3981 unsigned OldBitWidth = Int.getBitWidth();
3982 unsigned NewBitWidth = BitWidth;
3983 if (NewBitWidth < OldBitWidth)
3984 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
3985 }
3986 ElI++;
3987 continue;
3988 }
3989 if (Type->isVectorType()) {
3990 QualType ElTy = Type->castAs<VectorType>()->getElementType();
3991 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
3992 SmallVector<APValue> Vals(NumEl);
3993 for (unsigned I = 0; I < NumEl; ++I) {
3994 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: ElTy, Original: Elements[ElI],
3995 Result&: Vals[I]))
3996 return false;
3997 ElI++;
3998 }
3999 *Res = APValue(Vals.data(), NumEl);
4000 continue;
4001 }
4002 if (Type->isConstantArrayType()) {
4003 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4004 ->getElementType();
4005 uint64_t Size =
4006 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
4007 *Res = APValue(APValue::UninitArray(), Size, Size);
4008 for (int64_t I = Size - 1; I > -1; --I)
4009 WorkList.emplace_back(Args: &Res->getArrayInitializedElt(I), Args&: ElTy, Args: 0u);
4010 continue;
4011 }
4012 if (Type->isRecordType()) {
4013 const RecordDecl *RD = Type->getAsRecordDecl();
4014
4015 unsigned NumBases = 0;
4016 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
4017 NumBases = CXXRD->getNumBases();
4018
4019 *Res = APValue(APValue::UninitStruct(), NumBases, RD->getNumFields());
4020
4021 SmallVector<std::tuple<APValue *, QualType, unsigned>> ReverseList;
4022 // we need to traverse backwards
4023 // Visit the base classes.
4024 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4025 if (CXXRD->getNumBases() > 0) {
4026 assert(CXXRD->getNumBases() == 1);
4027 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4028 ReverseList.emplace_back(Args: &Res->getStructBase(i: 0), Args: BS.getType(), Args: 0u);
4029 }
4030 }
4031
4032 // Visit the fields.
4033 for (FieldDecl *FD : RD->fields()) {
4034 unsigned FDBW = 0;
4035 if (FD->isUnnamedBitField())
4036 continue;
4037 if (FD->isBitField()) {
4038 FDBW = FD->getBitWidthValue();
4039 }
4040
4041 ReverseList.emplace_back(Args: &Res->getStructField(i: FD->getFieldIndex()),
4042 Args: FD->getType(), Args&: FDBW);
4043 }
4044
4045 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
4046 llvm::append_range(C&: WorkList, R&: ReverseList);
4047 continue;
4048 }
4049 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4050 return false;
4051 }
4052 return true;
4053}
4054
4055static bool handleElementwiseCast(EvalInfo &Info, const Expr *E,
4056 const FPOptions FPO,
4057 SmallVectorImpl<APValue> &Elements,
4058 SmallVectorImpl<QualType> &SrcTypes,
4059 SmallVectorImpl<QualType> &DestTypes,
4060 SmallVectorImpl<APValue> &Results) {
4061
4062 assert((Elements.size() == SrcTypes.size()) &&
4063 (Elements.size() == DestTypes.size()));
4064
4065 for (unsigned I = 0, ESz = Elements.size(); I < ESz; ++I) {
4066 APValue Original = Elements[I];
4067 QualType SourceTy = SrcTypes[I];
4068 QualType DestTy = DestTypes[I];
4069
4070 if (!handleScalarCast(Info, FPO, E, SourceTy, DestTy, Original, Result&: Results[I]))
4071 return false;
4072 }
4073 return true;
4074}
4075
4076static unsigned elementwiseSize(EvalInfo &Info, QualType BaseTy) {
4077
4078 SmallVector<QualType> WorkList = {BaseTy};
4079
4080 unsigned Size = 0;
4081 while (!WorkList.empty()) {
4082 QualType Type = WorkList.pop_back_val();
4083 if (Type->isRealFloatingType() || Type->isIntegerType() ||
4084 Type->isBooleanType()) {
4085 ++Size;
4086 continue;
4087 }
4088 if (Type->isVectorType()) {
4089 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
4090 Size += NumEl;
4091 continue;
4092 }
4093 if (Type->isConstantMatrixType()) {
4094 unsigned NumEl =
4095 Type->castAs<ConstantMatrixType>()->getNumElementsFlattened();
4096 Size += NumEl;
4097 continue;
4098 }
4099 if (Type->isConstantArrayType()) {
4100 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4101 ->getElementType();
4102 uint64_t ArrSize =
4103 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
4104 for (uint64_t I = 0; I < ArrSize; ++I) {
4105 WorkList.push_back(Elt: ElTy);
4106 }
4107 continue;
4108 }
4109 if (Type->isRecordType()) {
4110 const RecordDecl *RD = Type->getAsRecordDecl();
4111
4112 // Visit the base classes.
4113 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4114 if (CXXRD->getNumBases() > 0) {
4115 assert(CXXRD->getNumBases() == 1);
4116 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4117 WorkList.push_back(Elt: BS.getType());
4118 }
4119 }
4120
4121 // visit the fields.
4122 for (FieldDecl *FD : RD->fields()) {
4123 if (FD->isUnnamedBitField())
4124 continue;
4125 WorkList.push_back(Elt: FD->getType());
4126 }
4127 continue;
4128 }
4129 }
4130 return Size;
4131}
4132
4133static bool hlslAggSplatHelper(EvalInfo &Info, const Expr *E, APValue &SrcVal,
4134 QualType &SrcTy) {
4135 SrcTy = E->getType();
4136
4137 if (!Evaluate(Result&: SrcVal, Info, E))
4138 return false;
4139
4140 assert((SrcVal.isFloat() || SrcVal.isInt() ||
4141 (SrcVal.isVector() && SrcVal.getVectorLength() == 1)) &&
4142 "Not a valid HLSLAggregateSplatCast.");
4143
4144 if (SrcVal.isVector()) {
4145 assert(SrcTy->isVectorType() && "Type mismatch.");
4146 SrcTy = SrcTy->castAs<VectorType>()->getElementType();
4147 SrcVal = SrcVal.getVectorElt(I: 0);
4148 }
4149 if (SrcVal.isMatrix()) {
4150 assert(SrcTy->isConstantMatrixType() && "Type mismatch.");
4151 SrcTy = SrcTy->castAs<ConstantMatrixType>()->getElementType();
4152 SrcVal = SrcVal.getMatrixElt(Row: 0, Col: 0);
4153 }
4154 return true;
4155}
4156
4157static bool flattenAPValue(EvalInfo &Info, const Expr *E, APValue Value,
4158 QualType BaseTy, SmallVectorImpl<APValue> &Elements,
4159 SmallVectorImpl<QualType> &Types, unsigned Size) {
4160
4161 SmallVector<std::pair<APValue, QualType>> WorkList = {{Value, BaseTy}};
4162 unsigned Populated = 0;
4163 while (!WorkList.empty() && Populated < Size) {
4164 auto [Work, Type] = WorkList.pop_back_val();
4165
4166 if (Work.isFloat() || Work.isInt()) {
4167 Elements.push_back(Elt: Work);
4168 Types.push_back(Elt: Type);
4169 Populated++;
4170 continue;
4171 }
4172 if (Work.isVector()) {
4173 assert(Type->isVectorType() && "Type mismatch.");
4174 QualType ElTy = Type->castAs<VectorType>()->getElementType();
4175 for (unsigned I = 0; I < Work.getVectorLength() && Populated < Size;
4176 I++) {
4177 Elements.push_back(Elt: Work.getVectorElt(I));
4178 Types.push_back(Elt: ElTy);
4179 Populated++;
4180 }
4181 continue;
4182 }
4183 if (Work.isMatrix()) {
4184 assert(Type->isConstantMatrixType() && "Type mismatch.");
4185 const auto *MT = Type->castAs<ConstantMatrixType>();
4186 QualType ElTy = MT->getElementType();
4187 // Matrix elements are flattened in row-major order.
4188 for (unsigned Row = 0; Row < Work.getMatrixNumRows() && Populated < Size;
4189 Row++) {
4190 for (unsigned Col = 0;
4191 Col < Work.getMatrixNumColumns() && Populated < Size; Col++) {
4192 Elements.push_back(Elt: Work.getMatrixElt(Row, Col));
4193 Types.push_back(Elt: ElTy);
4194 Populated++;
4195 }
4196 }
4197 continue;
4198 }
4199 if (Work.isArray()) {
4200 assert(Type->isConstantArrayType() && "Type mismatch.");
4201 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4202 ->getElementType();
4203 for (int64_t I = Work.getArraySize() - 1; I > -1; --I) {
4204 WorkList.emplace_back(Args&: Work.getArrayInitializedElt(I), Args&: ElTy);
4205 }
4206 continue;
4207 }
4208
4209 if (Work.isStruct()) {
4210 assert(Type->isRecordType() && "Type mismatch.");
4211
4212 const RecordDecl *RD = Type->getAsRecordDecl();
4213
4214 SmallVector<std::pair<APValue, QualType>> ReverseList;
4215 // Visit the fields.
4216 for (FieldDecl *FD : RD->fields()) {
4217 if (FD->isUnnamedBitField())
4218 continue;
4219 ReverseList.emplace_back(Args&: Work.getStructField(i: FD->getFieldIndex()),
4220 Args: FD->getType());
4221 }
4222
4223 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
4224 llvm::append_range(C&: WorkList, R&: ReverseList);
4225
4226 // Visit the base classes.
4227 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4228 if (CXXRD->getNumBases() > 0) {
4229 assert(CXXRD->getNumBases() == 1);
4230 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4231 const APValue &Base = Work.getStructBase(i: 0);
4232
4233 // Can happen in error cases.
4234 if (!Base.isStruct())
4235 return false;
4236
4237 WorkList.emplace_back(Args: Base, Args: BS.getType());
4238 }
4239 }
4240 continue;
4241 }
4242 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4243 return false;
4244 }
4245 return true;
4246}
4247
4248namespace {
4249/// A handle to a complete object (an object that is not a subobject of
4250/// another object).
4251struct CompleteObject {
4252 /// The identity of the object.
4253 APValue::LValueBase Base;
4254 /// The value of the complete object.
4255 APValue *Value;
4256 /// The type of the complete object.
4257 QualType Type;
4258
4259 CompleteObject() : Value(nullptr) {}
4260 CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type)
4261 : Base(Base), Value(Value), Type(Type) {}
4262
4263 bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const {
4264 // If this isn't a "real" access (eg, if it's just accessing the type
4265 // info), allow it. We assume the type doesn't change dynamically for
4266 // subobjects of constexpr objects (even though we'd hit UB here if it
4267 // did). FIXME: Is this right?
4268 if (!isAnyAccess(AK))
4269 return true;
4270
4271 // In C++14 onwards, it is permitted to read a mutable member whose
4272 // lifetime began within the evaluation.
4273 // FIXME: Should we also allow this in C++11?
4274 if (!Info.getLangOpts().CPlusPlus14 &&
4275 AK != AccessKinds::AK_IsWithinLifetime)
4276 return false;
4277 return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true);
4278 }
4279
4280 explicit operator bool() const { return !Type.isNull(); }
4281};
4282} // end anonymous namespace
4283
4284static QualType getSubobjectType(QualType ObjType, QualType SubobjType,
4285 bool IsMutable = false) {
4286 // C++ [basic.type.qualifier]p1:
4287 // - A const object is an object of type const T or a non-mutable subobject
4288 // of a const object.
4289 if (ObjType.isConstQualified() && !IsMutable)
4290 SubobjType.addConst();
4291 // - A volatile object is an object of type const T or a subobject of a
4292 // volatile object.
4293 if (ObjType.isVolatileQualified())
4294 SubobjType.addVolatile();
4295 return SubobjType;
4296}
4297
4298/// Find the designated sub-object of an rvalue.
4299template <typename SubobjectHandler>
4300static typename SubobjectHandler::result_type
4301findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
4302 const SubobjectDesignator &Sub, SubobjectHandler &handler) {
4303 if (Sub.Invalid)
4304 // A diagnostic will have already been produced.
4305 return handler.failed();
4306 if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) {
4307 if (Info.getLangOpts().CPlusPlus11)
4308 Info.FFDiag(E, DiagId: Sub.isOnePastTheEnd()
4309 ? diag::note_constexpr_access_past_end
4310 : diag::note_constexpr_access_unsized_array)
4311 << handler.AccessKind;
4312 else
4313 Info.FFDiag(E);
4314 return handler.failed();
4315 }
4316
4317 APValue *O = Obj.Value;
4318 QualType ObjType = Obj.Type;
4319 const FieldDecl *LastField = nullptr;
4320 const FieldDecl *VolatileField = nullptr;
4321
4322 // Walk the designator's path to find the subobject.
4323 for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) {
4324 // Reading an indeterminate value is undefined, but assigning over one is OK.
4325 if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) ||
4326 (O->isIndeterminate() &&
4327 !isValidIndeterminateAccess(handler.AccessKind))) {
4328 // Object has ended lifetime.
4329 // If I is non-zero, some subobject (member or array element) of a
4330 // complete object has ended its lifetime, so this is valid for
4331 // IsWithinLifetime, resulting in false.
4332 if (I != 0 && handler.AccessKind == AK_IsWithinLifetime)
4333 return false;
4334 if (!Info.checkingPotentialConstantExpression()) {
4335 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
4336 << handler.AccessKind << O->isIndeterminate()
4337 << E->getSourceRange();
4338 NoteLValueLocation(Info, Base: Obj.Base);
4339 }
4340 return handler.failed();
4341 }
4342
4343 // C++ [class.ctor]p5, C++ [class.dtor]p5:
4344 // const and volatile semantics are not applied on an object under
4345 // {con,de}struction.
4346 if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) &&
4347 ObjType->isRecordType() &&
4348 Info.isEvaluatingCtorDtor(
4349 Base: Obj.Base, Path: ArrayRef(Sub.Entries.begin(), Sub.Entries.begin() + I)) !=
4350 ConstructionPhase::None) {
4351 ObjType = Info.Ctx.getCanonicalType(T: ObjType);
4352 ObjType.removeLocalConst();
4353 ObjType.removeLocalVolatile();
4354 }
4355
4356 // If this is our last pass, check that the final object type is OK.
4357 if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) {
4358 // Accesses to volatile objects are prohibited.
4359 if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) {
4360 if (Info.getLangOpts().CPlusPlus) {
4361 int DiagKind;
4362 SourceLocation Loc;
4363 const NamedDecl *Decl = nullptr;
4364 if (VolatileField) {
4365 DiagKind = 2;
4366 Loc = VolatileField->getLocation();
4367 Decl = VolatileField;
4368 } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) {
4369 DiagKind = 1;
4370 Loc = VD->getLocation();
4371 Decl = VD;
4372 } else {
4373 DiagKind = 0;
4374 if (auto *E = Obj.Base.dyn_cast<const Expr *>())
4375 Loc = E->getExprLoc();
4376 }
4377 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_obj, ExtraNotes: 1)
4378 << handler.AccessKind << DiagKind << Decl;
4379 Info.Note(Loc, DiagId: diag::note_constexpr_volatile_here) << DiagKind;
4380 } else {
4381 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4382 }
4383 return handler.failed();
4384 }
4385
4386 // If we are reading an object of class type, there may still be more
4387 // things we need to check: if there are any mutable subobjects, we
4388 // cannot perform this read. (This only happens when performing a trivial
4389 // copy or assignment.)
4390 if (ObjType->isRecordType() &&
4391 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind) &&
4392 diagnoseMutableFields(Info, E, handler.AccessKind, ObjType))
4393 return handler.failed();
4394 }
4395
4396 if (I == N) {
4397 if (!handler.found(*O, ObjType, Obj.Base))
4398 return false;
4399
4400 // If we modified a bit-field, truncate it to the right width.
4401 if (isModification(handler.AccessKind) &&
4402 LastField && LastField->isBitField() &&
4403 !truncateBitfieldValue(Info, E, Value&: *O, FD: LastField))
4404 return false;
4405
4406 return true;
4407 }
4408
4409 LastField = nullptr;
4410
4411 // The value of an atomic object is represented like a value of the
4412 // underlying type, so look through the _Atomic wrapper.
4413 if (const AtomicType *AT = ObjType->getAs<AtomicType>())
4414 ObjType = Info.Ctx.getQualifiedType(T: AT->getValueType(),
4415 Qs: ObjType.getQualifiers());
4416
4417 if (ObjType->isArrayType()) {
4418 // Next subobject is an array element.
4419 const ArrayType *AT = Info.Ctx.getAsArrayType(T: ObjType);
4420 assert((isa<ConstantArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
4421 "vla in literal type?");
4422 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4423 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4424 CAT && CAT->getSize().ule(RHS: Index)) {
4425 // Note, it should not be possible to form a pointer with a valid
4426 // designator which points more than one past the end of the array.
4427 if (Info.getLangOpts().CPlusPlus11)
4428 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4429 << handler.AccessKind;
4430 else
4431 Info.FFDiag(E);
4432 return handler.failed();
4433 }
4434
4435 ObjType = AT->getElementType();
4436
4437 if (O->getArrayInitializedElts() > Index)
4438 O = &O->getArrayInitializedElt(I: Index);
4439 else if (!isRead(handler.AccessKind)) {
4440 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4441 CAT && !CheckArraySize(Info, CAT, CallLoc: E->getExprLoc()))
4442 return handler.failed();
4443
4444 expandArray(Array&: *O, Index);
4445 O = &O->getArrayInitializedElt(I: Index);
4446 } else
4447 O = &O->getArrayFiller();
4448 } else if (ObjType->isAnyComplexType()) {
4449 // Next subobject is a complex number.
4450 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4451 if (Index > 1) {
4452 if (Info.getLangOpts().CPlusPlus11)
4453 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4454 << handler.AccessKind;
4455 else
4456 Info.FFDiag(E);
4457 return handler.failed();
4458 }
4459
4460 ObjType = getSubobjectType(
4461 ObjType, SubobjType: ObjType->castAs<ComplexType>()->getElementType());
4462
4463 assert(I == N - 1 && "extracting subobject of scalar?");
4464 if (O->isComplexInt()) {
4465 return handler.found(Index ? O->getComplexIntImag()
4466 : O->getComplexIntReal(), ObjType);
4467 } else {
4468 assert(O->isComplexFloat());
4469 return handler.found(Index ? O->getComplexFloatImag()
4470 : O->getComplexFloatReal(), ObjType);
4471 }
4472 } else if (const auto *VT = ObjType->getAs<VectorType>()) {
4473 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4474 unsigned NumElements = VT->getNumElements();
4475 if (Index == NumElements) {
4476 if (Info.getLangOpts().CPlusPlus11)
4477 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4478 << handler.AccessKind;
4479 else
4480 Info.FFDiag(E);
4481 return handler.failed();
4482 }
4483
4484 if (Index > NumElements) {
4485 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
4486 << Index << /*array*/ 0 << NumElements;
4487 return handler.failed();
4488 }
4489
4490 ObjType = VT->getElementType();
4491 assert(I == N - 1 && "extracting subobject of scalar?");
4492
4493 if (O->isIndeterminate()) {
4494 if (isRead(handler.AccessKind)) {
4495 Info.FFDiag(E);
4496 return handler.failed();
4497 }
4498 expandVector(Vec&: *O, NumElements);
4499 }
4500 assert(O->isVector() && "unexpected object during vector element access");
4501 return handler.found(O->getVectorElt(I: Index), ObjType, Obj.Base);
4502 } else if (const FieldDecl *Field = getAsField(E: Sub.Entries[I])) {
4503 if (Field->isMutable() &&
4504 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind)) {
4505 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1)
4506 << handler.AccessKind << Field;
4507 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
4508 return handler.failed();
4509 }
4510
4511 // Next subobject is a class, struct or union field.
4512 RecordDecl *RD = ObjType->castAsCanonical<RecordType>()->getDecl();
4513 if (RD->isUnion()) {
4514 const FieldDecl *UnionField = O->getUnionField();
4515 if (!UnionField ||
4516 UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {
4517 if (I == N - 1 && handler.AccessKind == AK_Construct) {
4518 // Placement new onto an inactive union member makes it active.
4519 O->setUnion(Field, Value: APValue());
4520 } else {
4521 // Pointer to/into inactive union member: Not within lifetime
4522 if (handler.AccessKind == AK_IsWithinLifetime)
4523 return false;
4524 // FIXME: If O->getUnionValue() is absent, report that there's no
4525 // active union member rather than reporting the prior active union
4526 // member. We'll need to fix nullptr_t to not use APValue() as its
4527 // representation first.
4528 Info.FFDiag(E, DiagId: diag::note_constexpr_access_inactive_union_member)
4529 << handler.AccessKind << Field << !UnionField << UnionField;
4530 return handler.failed();
4531 }
4532 }
4533 O = &O->getUnionValue();
4534 } else
4535 O = &O->getStructField(i: Field->getFieldIndex());
4536
4537 ObjType = getSubobjectType(ObjType, SubobjType: Field->getType(), IsMutable: Field->isMutable());
4538 LastField = Field;
4539 if (Field->getType().isVolatileQualified())
4540 VolatileField = Field;
4541 } else {
4542 // Next subobject is a base class.
4543 const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();
4544 const CXXRecordDecl *Base = getAsBaseClass(E: Sub.Entries[I]);
4545
4546 unsigned BaseIndex = getBaseIndex(Derived, Base);
4547 unsigned NumNonVirtualBases = O->getStructNumBases();
4548 if (BaseIndex >= NumNonVirtualBases) {
4549 O = &O->getStructVirtualBase(i: BaseIndex - NumNonVirtualBases);
4550 } else
4551 O = &O->getStructBase(i: BaseIndex);
4552
4553 ObjType = getSubobjectType(ObjType, SubobjType: Info.Ctx.getCanonicalTagType(TD: Base));
4554 }
4555 }
4556}
4557
4558namespace {
4559struct ExtractSubobjectHandler {
4560 EvalInfo &Info;
4561 const Expr *E;
4562 APValue &Result;
4563 const AccessKinds AccessKind;
4564
4565 typedef bool result_type;
4566 bool failed() { return false; }
4567 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4568 Result = Subobj;
4569 if (AccessKind == AK_ReadObjectRepresentation)
4570 return true;
4571 return CheckFullyInitialized(Info, DiagLoc: E->getExprLoc(), Type: SubobjType, Value: Result);
4572 }
4573 bool found(APSInt &Value, QualType SubobjType) {
4574 Result = APValue(Value);
4575 return true;
4576 }
4577 bool found(APFloat &Value, QualType SubobjType) {
4578 Result = APValue(Value);
4579 return true;
4580 }
4581};
4582} // end anonymous namespace
4583
4584/// Extract the designated sub-object of an rvalue.
4585static bool extractSubobject(EvalInfo &Info, const Expr *E,
4586 const CompleteObject &Obj,
4587 const SubobjectDesignator &Sub, APValue &Result,
4588 AccessKinds AK = AK_Read) {
4589 assert(AK == AK_Read || AK == AK_ReadObjectRepresentation);
4590 ExtractSubobjectHandler Handler = {.Info: Info, .E: E, .Result: Result, .AccessKind: AK};
4591 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4592}
4593
4594namespace {
4595struct ModifySubobjectHandler {
4596 EvalInfo &Info;
4597 APValue &NewVal;
4598 const Expr *E;
4599
4600 typedef bool result_type;
4601 static const AccessKinds AccessKind = AK_Assign;
4602
4603 bool checkConst(QualType QT) {
4604 // Assigning to a const object has undefined behavior.
4605 if (QT.isConstQualified()) {
4606 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
4607 return false;
4608 }
4609 return true;
4610 }
4611
4612 bool failed() { return false; }
4613 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4614 if (!checkConst(QT: SubobjType))
4615 return false;
4616 // We've been given ownership of NewVal, so just swap it in.
4617 Subobj.swap(RHS&: NewVal);
4618 return true;
4619 }
4620 bool found(APSInt &Value, QualType SubobjType) {
4621 if (!checkConst(QT: SubobjType))
4622 return false;
4623 if (!NewVal.isInt()) {
4624 // Maybe trying to write a cast pointer value into a complex?
4625 Info.FFDiag(E);
4626 return false;
4627 }
4628 Value = NewVal.getInt();
4629 return true;
4630 }
4631 bool found(APFloat &Value, QualType SubobjType) {
4632 if (!checkConst(QT: SubobjType))
4633 return false;
4634 Value = NewVal.getFloat();
4635 return true;
4636 }
4637};
4638} // end anonymous namespace
4639
4640const AccessKinds ModifySubobjectHandler::AccessKind;
4641
4642/// Update the designated sub-object of an rvalue to the given value.
4643static bool modifySubobject(EvalInfo &Info, const Expr *E,
4644 const CompleteObject &Obj,
4645 const SubobjectDesignator &Sub,
4646 APValue &NewVal) {
4647 ModifySubobjectHandler Handler = { .Info: Info, .NewVal: NewVal, .E: E };
4648 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4649}
4650
4651/// Find the position where two subobject designators diverge, or equivalently
4652/// the length of the common initial subsequence.
4653static unsigned FindDesignatorMismatch(QualType ObjType,
4654 const SubobjectDesignator &A,
4655 const SubobjectDesignator &B,
4656 bool &WasArrayIndex) {
4657 unsigned I = 0, N = std::min(a: A.Entries.size(), b: B.Entries.size());
4658 for (/**/; I != N; ++I) {
4659 if (!ObjType.isNull() &&
4660 (ObjType->isArrayType() || ObjType->isAnyComplexType())) {
4661 // Next subobject is an array element.
4662 if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) {
4663 WasArrayIndex = true;
4664 return I;
4665 }
4666 if (ObjType->isAnyComplexType())
4667 ObjType = ObjType->castAs<ComplexType>()->getElementType();
4668 else
4669 ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();
4670 } else {
4671 if (A.Entries[I].getAsBaseOrMember() !=
4672 B.Entries[I].getAsBaseOrMember()) {
4673 WasArrayIndex = false;
4674 return I;
4675 }
4676 if (const FieldDecl *FD = getAsField(E: A.Entries[I]))
4677 // Next subobject is a field.
4678 ObjType = FD->getType();
4679 else
4680 // Next subobject is a base class.
4681 ObjType = QualType();
4682 }
4683 }
4684 WasArrayIndex = false;
4685 return I;
4686}
4687
4688/// Determine whether the given subobject designators refer to elements of the
4689/// same array object.
4690static bool AreElementsOfSameArray(QualType ObjType,
4691 const SubobjectDesignator &A,
4692 const SubobjectDesignator &B) {
4693 if (A.Entries.size() != B.Entries.size())
4694 return false;
4695
4696 bool IsArray = A.MostDerivedIsArrayElement;
4697 if (IsArray && A.MostDerivedPathLength != A.Entries.size())
4698 // A is a subobject of the array element.
4699 return false;
4700
4701 // If A (and B) designates an array element, the last entry will be the array
4702 // index. That doesn't have to match. Otherwise, we're in the 'implicit array
4703 // of length 1' case, and the entire path must match.
4704 bool WasArrayIndex;
4705 unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);
4706 return CommonLength >= A.Entries.size() - IsArray;
4707}
4708
4709/// Find the complete object to which an LValue refers.
4710static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
4711 AccessKinds AK, const LValue &LVal,
4712 QualType LValType) {
4713 if (LVal.InvalidBase) {
4714 Info.FFDiag(E);
4715 return CompleteObject();
4716 }
4717
4718 if (!LVal.Base) {
4719 if (AK == AccessKinds::AK_Dereference)
4720 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
4721 else
4722 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
4723 return CompleteObject();
4724 }
4725
4726 CallStackFrame *Frame = nullptr;
4727 unsigned Depth = 0;
4728 if (LVal.getLValueCallIndex()) {
4729 std::tie(args&: Frame, args&: Depth) =
4730 Info.getCallFrameAndDepth(CallIndex: LVal.getLValueCallIndex());
4731 if (!Frame) {
4732 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit, ExtraNotes: 1)
4733 << AK << /*Indeterminate=*/false << E->getSourceRange();
4734 NoteLValueLocation(Info, Base: LVal.Base);
4735 return CompleteObject();
4736 }
4737 }
4738
4739 bool IsAccess = isAnyAccess(AK);
4740
4741 // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type
4742 // is not a constant expression (even if the object is non-volatile). We also
4743 // apply this rule to C++98, in order to conform to the expected 'volatile'
4744 // semantics.
4745 if (isFormalAccess(AK) && LValType.isVolatileQualified()) {
4746 if (Info.getLangOpts().CPlusPlus)
4747 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_type)
4748 << AK << LValType;
4749 else
4750 Info.FFDiag(E);
4751 return CompleteObject();
4752 }
4753
4754 // Compute value storage location and type of base object.
4755 APValue *BaseVal = nullptr;
4756 QualType BaseType = getType(B: LVal.Base);
4757
4758 if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl &&
4759 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4760 // This is the object whose initializer we're evaluating, so its lifetime
4761 // started in the current evaluation.
4762 BaseVal = Info.EvaluatingDeclValue;
4763 } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) {
4764 // Allow reading from a GUID declaration.
4765 if (auto *GD = dyn_cast<MSGuidDecl>(Val: D)) {
4766 if (isModification(AK)) {
4767 // All the remaining cases do not permit modification of the object.
4768 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4769 return CompleteObject();
4770 }
4771 APValue &V = GD->getAsAPValue();
4772 if (V.isAbsent()) {
4773 Info.FFDiag(E, DiagId: diag::note_constexpr_unsupported_layout)
4774 << GD->getType();
4775 return CompleteObject();
4776 }
4777 return CompleteObject(LVal.Base, &V, GD->getType());
4778 }
4779
4780 // Allow reading the APValue from an UnnamedGlobalConstantDecl.
4781 if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(Val: D)) {
4782 if (isModification(AK)) {
4783 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4784 return CompleteObject();
4785 }
4786 return CompleteObject(LVal.Base, const_cast<APValue *>(&GCD->getValue()),
4787 GCD->getType());
4788 }
4789
4790 // Allow reading from template parameter objects.
4791 if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: D)) {
4792 if (isModification(AK)) {
4793 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4794 return CompleteObject();
4795 }
4796 return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()),
4797 TPO->getType());
4798 }
4799
4800 // In C++98, const, non-volatile integers initialized with ICEs are ICEs.
4801 // In C++11, constexpr, non-volatile variables initialized with constant
4802 // expressions are constant expressions too. Inside constexpr functions,
4803 // parameters are constant expressions even if they're non-const.
4804 // In C++1y, objects local to a constant expression (those with a Frame) are
4805 // both readable and writable inside constant expressions.
4806 // In C, such things can also be folded, although they are not ICEs.
4807 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
4808 if (VD) {
4809 if (const VarDecl *VDef = VD->getDefinition(C&: Info.Ctx))
4810 VD = VDef;
4811 }
4812 if (!VD || VD->isInvalidDecl()) {
4813 Info.FFDiag(E);
4814 return CompleteObject();
4815 }
4816
4817 bool IsConstant = BaseType.isConstant(Ctx: Info.Ctx);
4818 bool ConstexprVar = false;
4819 if (const auto *VD = dyn_cast_if_present<VarDecl>(
4820 Val: Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()))
4821 ConstexprVar = VD->isConstexpr();
4822
4823 // Unless we're looking at a local variable or argument in a constexpr call,
4824 // the variable we're reading must be const (unless we are binding to a
4825 // reference).
4826 if (AK != clang::AK_Dereference && !Frame) {
4827 if (IsAccess && isa<ParmVarDecl>(Val: VD)) {
4828 // Access of a parameter that's not associated with a frame isn't going
4829 // to work out, but we can leave it to evaluateVarDeclInit to provide a
4830 // suitable diagnostic.
4831 } else if (Info.getLangOpts().CPlusPlus14 &&
4832 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4833 // OK, we can read and modify an object if we're in the process of
4834 // evaluating its initializer, because its lifetime began in this
4835 // evaluation.
4836 } else if (isModification(AK)) {
4837 // All the remaining cases do not permit modification of the object.
4838 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4839 return CompleteObject();
4840 } else if (VD->isConstexpr()) {
4841 // OK, we can read this variable.
4842 } else if (Info.getLangOpts().C23 && ConstexprVar) {
4843 Info.FFDiag(E);
4844 return CompleteObject();
4845 } else if (VD->isCXXForRangeImplicitVar()) {
4846 if (!IsAccess)
4847 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4848 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_for_range_var) << VD;
4849 return CompleteObject();
4850 } else if (BaseType->isIntegralOrEnumerationType()) {
4851 if (!IsConstant) {
4852 if (!IsAccess)
4853 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4854 if (Info.getLangOpts().CPlusPlus) {
4855 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_non_const_int, ExtraNotes: 1) << VD;
4856 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4857 } else {
4858 Info.FFDiag(E);
4859 }
4860 return CompleteObject();
4861 }
4862 } else if (!IsAccess) {
4863 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4864 } else if ((IsConstant || BaseType->isReferenceType()) &&
4865 Info.checkingPotentialConstantExpression() &&
4866 BaseType->isLiteralType(Ctx: Info.Ctx) && !VD->hasDefinition()) {
4867 // This variable might end up being constexpr. Don't diagnose it yet.
4868 } else if (IsConstant) {
4869 // Keep evaluating to see what we can do. In particular, we support
4870 // folding of const floating-point types, in order to make static const
4871 // data members of such types (supported as an extension) more useful.
4872 if (Info.getLangOpts().CPlusPlus) {
4873 Info.CCEDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4874 ? diag::note_constexpr_ltor_non_constexpr
4875 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4876 << VD << BaseType;
4877 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4878 } else {
4879 Info.CCEDiag(E);
4880 }
4881 } else {
4882 // Never allow reading a non-const value.
4883 if (Info.getLangOpts().CPlusPlus) {
4884 Info.FFDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4885 ? diag::note_constexpr_ltor_non_constexpr
4886 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4887 << VD << BaseType;
4888 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4889 } else {
4890 Info.FFDiag(E);
4891 }
4892 return CompleteObject();
4893 }
4894 }
4895
4896 // When binding to a reference, the variable does not need to be constexpr
4897 // or have constant initalization.
4898 if (AK != clang::AK_Dereference &&
4899 !evaluateVarDeclInit(Info, E, VD, Frame, Version: LVal.getLValueVersion(),
4900 Result&: BaseVal))
4901 return CompleteObject();
4902 // If evaluateVarDeclInit sees a constexpr-unknown variable, it returns
4903 // a null BaseVal. Any constexpr-unknown variable seen here is an error:
4904 // we can't access a constexpr-unknown object.
4905 if (AK != clang::AK_Dereference && !BaseVal) {
4906 if (!Info.checkingPotentialConstantExpression()) {
4907 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unknown_variable, ExtraNotes: 1)
4908 << AK << VD;
4909 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4910 }
4911 return CompleteObject();
4912 }
4913 } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) {
4914 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
4915 if (!Alloc) {
4916 Info.FFDiag(E, DiagId: diag::note_constexpr_access_deleted_object) << AK;
4917 return CompleteObject();
4918 }
4919 return CompleteObject(LVal.Base, &(*Alloc)->Value,
4920 LVal.Base.getDynamicAllocType());
4921 }
4922 // When binding to a reference, the variable does not need to be
4923 // within its lifetime.
4924 else if (AK != clang::AK_Dereference) {
4925 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
4926
4927 if (!Frame) {
4928 if (const MaterializeTemporaryExpr *MTE =
4929 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: Base)) {
4930 assert(MTE->getStorageDuration() == SD_Static &&
4931 "should have a frame for a non-global materialized temporary");
4932
4933 // C++20 [expr.const]p4: [DR2126]
4934 // An object or reference is usable in constant expressions if it is
4935 // - a temporary object of non-volatile const-qualified literal type
4936 // whose lifetime is extended to that of a variable that is usable
4937 // in constant expressions
4938 //
4939 // C++20 [expr.const]p5:
4940 // an lvalue-to-rvalue conversion [is not allowed unless it applies to]
4941 // - a non-volatile glvalue that refers to an object that is usable
4942 // in constant expressions, or
4943 // - a non-volatile glvalue of literal type that refers to a
4944 // non-volatile object whose lifetime began within the evaluation
4945 // of E;
4946 //
4947 // C++11 misses the 'began within the evaluation of e' check and
4948 // instead allows all temporaries, including things like:
4949 // int &&r = 1;
4950 // int x = ++r;
4951 // constexpr int k = r;
4952 // Therefore we use the C++14-onwards rules in C++11 too.
4953 //
4954 // Note that temporaries whose lifetimes began while evaluating a
4955 // variable's constructor are not usable while evaluating the
4956 // corresponding destructor, not even if they're of const-qualified
4957 // types.
4958 if (!MTE->isUsableInConstantExpressions(Context: Info.Ctx) &&
4959 !lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4960 if (!IsAccess)
4961 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4962 Info.FFDiag(E, DiagId: diag::note_constexpr_access_static_temporary, ExtraNotes: 1) << AK;
4963 Info.Note(Loc: MTE->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
4964 return CompleteObject();
4965 }
4966
4967 BaseVal = MTE->getOrCreateValue(MayCreate: false);
4968 assert(BaseVal && "got reference to unevaluated temporary");
4969 } else if (const CompoundLiteralExpr *CLE =
4970 dyn_cast_or_null<CompoundLiteralExpr>(Val: Base)) {
4971 // According to GCC info page:
4972 //
4973 // 6.28 Compound Literals
4974 //
4975 // As an optimization, G++ sometimes gives array compound literals
4976 // longer lifetimes: when the array either appears outside a function or
4977 // has a const-qualified type. If foo and its initializer had elements
4978 // of type char *const rather than char *, or if foo were a global
4979 // variable, the array would have static storage duration. But it is
4980 // probably safest just to avoid the use of array compound literals in
4981 // C++ code.
4982 //
4983 // Obey that rule by checking constness for converted array types.
4984 if (QualType CLETy = CLE->getType(); CLETy->isArrayType() &&
4985 !LValType->isArrayType() &&
4986 !CLETy.isConstant(Ctx: Info.Ctx)) {
4987 Info.FFDiag(E);
4988 Info.Note(Loc: CLE->getExprLoc(), DiagId: diag::note_declared_at);
4989 return CompleteObject();
4990 }
4991
4992 BaseVal = &CLE->getStaticValue();
4993 } else {
4994 if (!IsAccess)
4995 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4996 APValue Val;
4997 LVal.moveInto(V&: Val);
4998 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unreadable_object)
4999 << AK
5000 << Val.getAsString(Ctx: Info.Ctx,
5001 Ty: Info.Ctx.getLValueReferenceType(T: LValType));
5002 NoteLValueLocation(Info, Base: LVal.Base);
5003 return CompleteObject();
5004 }
5005 } else if (AK != clang::AK_Dereference) {
5006 BaseVal = Frame->getTemporary(Key: Base, Version: LVal.Base.getVersion());
5007 assert(BaseVal && "missing value for temporary");
5008 }
5009 }
5010
5011 // In C++14, we can't safely access any mutable state when we might be
5012 // evaluating after an unmodeled side effect. Parameters are modeled as state
5013 // in the caller, but aren't visible once the call returns, so they can be
5014 // modified in a speculatively-evaluated call.
5015 //
5016 // FIXME: Not all local state is mutable. Allow local constant subobjects
5017 // to be read here (but take care with 'mutable' fields).
5018 unsigned VisibleDepth = Depth;
5019 if (llvm::isa_and_nonnull<ParmVarDecl>(
5020 Val: LVal.Base.dyn_cast<const ValueDecl *>()))
5021 ++VisibleDepth;
5022 if ((Frame && Info.getLangOpts().CPlusPlus14 &&
5023 Info.EvalStatus.HasSideEffects) ||
5024 (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth))
5025 return CompleteObject();
5026
5027 return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType);
5028}
5029
5030/// Perform an lvalue-to-rvalue conversion on the given glvalue. This
5031/// can also be used for 'lvalue-to-lvalue' conversions for looking up the
5032/// glvalue referred to by an entity of reference type.
5033///
5034/// \param Info - Information about the ongoing evaluation.
5035/// \param Conv - The expression for which we are performing the conversion.
5036/// Used for diagnostics.
5037/// \param Type - The type of the glvalue (before stripping cv-qualifiers in the
5038/// case of a non-class type).
5039/// \param LVal - The glvalue on which we are attempting to perform this action.
5040/// \param RVal - The produced value will be placed here.
5041/// \param WantObjectRepresentation - If true, we're looking for the object
5042/// representation rather than the value, and in particular,
5043/// there is no requirement that the result be fully initialized.
5044static bool
5045handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type,
5046 const LValue &LVal, APValue &RVal,
5047 bool WantObjectRepresentation = false) {
5048 if (LVal.Designator.Invalid)
5049 return false;
5050
5051 // Check for special cases where there is no existing APValue to look at.
5052 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
5053
5054 AccessKinds AK =
5055 WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read;
5056
5057 if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) {
5058 if (isa<StringLiteral>(Val: Base) || isa<PredefinedExpr>(Val: Base)) {
5059 // Special-case character extraction so we don't have to construct an
5060 // APValue for the whole string.
5061 assert(LVal.Designator.Entries.size() <= 1 &&
5062 "Can only read characters from string literals");
5063 if (LVal.Designator.Entries.empty()) {
5064 // Fail for now for LValue to RValue conversion of an array.
5065 // (This shouldn't show up in C/C++, but it could be triggered by a
5066 // weird EvaluateAsRValue call from a tool.)
5067 Info.FFDiag(E: Conv);
5068 return false;
5069 }
5070 if (LVal.Designator.isOnePastTheEnd()) {
5071 if (Info.getLangOpts().CPlusPlus11)
5072 Info.FFDiag(E: Conv, DiagId: diag::note_constexpr_access_past_end) << AK;
5073 else
5074 Info.FFDiag(E: Conv);
5075 return false;
5076 }
5077 uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex();
5078 RVal = APValue(extractStringLiteralCharacter(Info, Lit: Base, Index: CharIndex));
5079 return true;
5080 }
5081 }
5082
5083 CompleteObject Obj = findCompleteObject(Info, E: Conv, AK, LVal, LValType: Type);
5084 return Obj && extractSubobject(Info, E: Conv, Obj, Sub: LVal.Designator, Result&: RVal, AK);
5085}
5086
5087static bool hlslElementwiseCastHelper(EvalInfo &Info, const Expr *E,
5088 QualType DestTy,
5089 SmallVectorImpl<APValue> &SrcVals,
5090 SmallVectorImpl<QualType> &SrcTypes) {
5091 APValue Val;
5092 if (!Evaluate(Result&: Val, Info, E))
5093 return false;
5094
5095 // must be dealing with a record
5096 if (Val.isLValue()) {
5097 LValue LVal;
5098 LVal.setFrom(Ctx: Info.Ctx, V: Val);
5099 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal, RVal&: Val))
5100 return false;
5101 }
5102
5103 unsigned NEls = elementwiseSize(Info, BaseTy: DestTy);
5104 // flatten the source
5105 if (!flattenAPValue(Info, E, Value: Val, BaseTy: E->getType(), Elements&: SrcVals, Types&: SrcTypes, Size: NEls))
5106 return false;
5107
5108 return true;
5109}
5110
5111/// Perform an assignment of Val to LVal. Takes ownership of Val.
5112static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal,
5113 QualType LValType, APValue &Val) {
5114 if (LVal.Designator.Invalid)
5115 return false;
5116
5117 if (!Info.getLangOpts().CPlusPlus14) {
5118 Info.FFDiag(E);
5119 return false;
5120 }
5121
5122 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5123 return Obj && modifySubobject(Info, E, Obj, Sub: LVal.Designator, NewVal&: Val);
5124}
5125
5126namespace {
5127struct CompoundAssignSubobjectHandler {
5128 EvalInfo &Info;
5129 const CompoundAssignOperator *E;
5130 QualType PromotedLHSType;
5131 BinaryOperatorKind Opcode;
5132 const APValue &RHS;
5133
5134 static const AccessKinds AccessKind = AK_Assign;
5135
5136 typedef bool result_type;
5137
5138 bool checkConst(QualType QT) {
5139 // Assigning to a const object has undefined behavior.
5140 if (QT.isConstQualified()) {
5141 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5142 return false;
5143 }
5144 return true;
5145 }
5146
5147 bool failed() { return false; }
5148 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5149 switch (Subobj.getKind()) {
5150 case APValue::Int:
5151 return found(Value&: Subobj.getInt(), SubobjType);
5152 case APValue::Float:
5153 return found(Value&: Subobj.getFloat(), SubobjType);
5154 case APValue::ComplexInt:
5155 case APValue::ComplexFloat:
5156 // FIXME: Implement complex compound assignment.
5157 Info.FFDiag(E);
5158 return false;
5159 case APValue::LValue:
5160 return foundPointer(Subobj, SubobjType);
5161 case APValue::Vector:
5162 return foundVector(Value&: Subobj, SubobjType);
5163 case APValue::Indeterminate:
5164 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
5165 << /*read of=*/0 << /*uninitialized object=*/1
5166 << E->getLHS()->getSourceRange();
5167 NoteLValueLocation(Info, Base);
5168 return false;
5169 default:
5170 // FIXME: can this happen?
5171 Info.FFDiag(E);
5172 return false;
5173 }
5174 }
5175
5176 bool foundVector(APValue &Value, QualType SubobjType) {
5177 if (!checkConst(QT: SubobjType))
5178 return false;
5179
5180 if (!SubobjType->isVectorType()) {
5181 Info.FFDiag(E);
5182 return false;
5183 }
5184 return handleVectorVectorBinOp(Info, E, Opcode, LHSValue&: Value, RHSValue: RHS);
5185 }
5186
5187 bool found(APSInt &Value, QualType SubobjType) {
5188 if (!checkConst(QT: SubobjType))
5189 return false;
5190
5191 if (!SubobjType->isIntegerType()) {
5192 // We don't support compound assignment on integer-cast-to-pointer
5193 // values.
5194 Info.FFDiag(E);
5195 return false;
5196 }
5197
5198 if (RHS.isInt()) {
5199 APSInt LHS =
5200 HandleIntToIntCast(Info, E, DestType: PromotedLHSType, SrcType: SubobjType, Value);
5201 if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS: RHS.getInt(), Result&: LHS))
5202 return false;
5203 Value = HandleIntToIntCast(Info, E, DestType: SubobjType, SrcType: PromotedLHSType, Value: LHS);
5204 return true;
5205 } else if (RHS.isFloat()) {
5206 const FPOptions FPO = E->getFPFeaturesInEffect(
5207 LO: Info.Ctx.getLangOpts());
5208 APFloat FValue(0.0);
5209 return HandleIntToFloatCast(Info, E, FPO, SrcType: SubobjType, Value,
5210 DestType: PromotedLHSType, Result&: FValue) &&
5211 handleFloatFloatBinOp(Info, E, LHS&: FValue, Opcode, RHS: RHS.getFloat()) &&
5212 HandleFloatToIntCast(Info, E, SrcType: PromotedLHSType, Value: FValue, DestType: SubobjType,
5213 Result&: Value);
5214 }
5215
5216 Info.FFDiag(E);
5217 return false;
5218 }
5219 bool found(APFloat &Value, QualType SubobjType) {
5220 return checkConst(QT: SubobjType) &&
5221 HandleFloatToFloatCast(Info, E, SrcType: SubobjType, DestType: PromotedLHSType,
5222 Result&: Value) &&
5223 handleFloatFloatBinOp(Info, E, LHS&: Value, Opcode, RHS: RHS.getFloat()) &&
5224 HandleFloatToFloatCast(Info, E, SrcType: PromotedLHSType, DestType: SubobjType, Result&: Value);
5225 }
5226 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5227 if (!checkConst(QT: SubobjType))
5228 return false;
5229
5230 QualType PointeeType;
5231 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5232 PointeeType = PT->getPointeeType();
5233
5234 if (PointeeType.isNull() || !RHS.isInt() ||
5235 (Opcode != BO_Add && Opcode != BO_Sub)) {
5236 Info.FFDiag(E);
5237 return false;
5238 }
5239
5240 APSInt Offset = RHS.getInt();
5241 if (Opcode == BO_Sub)
5242 negateAsSigned(Int&: Offset);
5243
5244 LValue LVal;
5245 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5246 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType, Adjustment: Offset))
5247 return false;
5248 LVal.moveInto(V&: Subobj);
5249 return true;
5250 }
5251};
5252} // end anonymous namespace
5253
5254const AccessKinds CompoundAssignSubobjectHandler::AccessKind;
5255
5256/// Perform a compound assignment of LVal <op>= RVal.
5257static bool handleCompoundAssignment(EvalInfo &Info,
5258 const CompoundAssignOperator *E,
5259 const LValue &LVal, QualType LValType,
5260 QualType PromotedLValType,
5261 BinaryOperatorKind Opcode,
5262 const APValue &RVal) {
5263 if (LVal.Designator.Invalid)
5264 return false;
5265
5266 if (!Info.getLangOpts().CPlusPlus14) {
5267 Info.FFDiag(E);
5268 return false;
5269 }
5270
5271 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5272 CompoundAssignSubobjectHandler Handler = { .Info: Info, .E: E, .PromotedLHSType: PromotedLValType, .Opcode: Opcode,
5273 .RHS: RVal };
5274 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5275}
5276
5277namespace {
5278struct IncDecSubobjectHandler {
5279 EvalInfo &Info;
5280 const UnaryOperator *E;
5281 AccessKinds AccessKind;
5282 APValue *Old;
5283
5284 typedef bool result_type;
5285
5286 bool checkConst(QualType QT) {
5287 // Assigning to a const object has undefined behavior.
5288 if (QT.isConstQualified()) {
5289 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5290 return false;
5291 }
5292 return true;
5293 }
5294
5295 bool failed() { return false; }
5296 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5297 // Stash the old value. Also clear Old, so we don't clobber it later
5298 // if we're post-incrementing a complex.
5299 if (Old) {
5300 *Old = Subobj;
5301 Old = nullptr;
5302 }
5303
5304 switch (Subobj.getKind()) {
5305 case APValue::Int:
5306 return found(Value&: Subobj.getInt(), SubobjType);
5307 case APValue::Float:
5308 return found(Value&: Subobj.getFloat(), SubobjType);
5309 case APValue::ComplexInt:
5310 return found(Value&: Subobj.getComplexIntReal(),
5311 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5312 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5313 case APValue::ComplexFloat:
5314 return found(Value&: Subobj.getComplexFloatReal(),
5315 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5316 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5317 case APValue::LValue:
5318 return foundPointer(Subobj, SubobjType);
5319 default:
5320 // FIXME: can this happen?
5321 Info.FFDiag(E);
5322 return false;
5323 }
5324 }
5325 bool found(APSInt &Value, QualType SubobjType) {
5326 if (!checkConst(QT: SubobjType))
5327 return false;
5328
5329 if (!SubobjType->isIntegerType()) {
5330 // We don't support increment / decrement on integer-cast-to-pointer
5331 // values.
5332 Info.FFDiag(E);
5333 return false;
5334 }
5335
5336 if (Old) *Old = APValue(Value);
5337
5338 // bool arithmetic promotes to int, and the conversion back to bool
5339 // doesn't reduce mod 2^n, so special-case it.
5340 if (SubobjType->isBooleanType()) {
5341 if (AccessKind == AK_Increment)
5342 Value = 1;
5343 else
5344 Value = !Value;
5345 return true;
5346 }
5347
5348 bool WasNegative = Value.isNegative();
5349 if (AccessKind == AK_Increment) {
5350 ++Value;
5351
5352 if (!WasNegative && Value.isNegative() && E->canOverflow() &&
5353 !SubobjType.isWrapType()) {
5354 APSInt ActualValue(Value, /*IsUnsigned*/true);
5355 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5356 }
5357 } else {
5358 --Value;
5359
5360 if (WasNegative && !Value.isNegative() && E->canOverflow() &&
5361 !SubobjType.isWrapType()) {
5362 unsigned BitWidth = Value.getBitWidth();
5363 APSInt ActualValue(Value.sext(width: BitWidth + 1), /*IsUnsigned*/false);
5364 ActualValue.setBit(BitWidth);
5365 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5366 }
5367 }
5368 return true;
5369 }
5370 bool found(APFloat &Value, QualType SubobjType) {
5371 if (!checkConst(QT: SubobjType))
5372 return false;
5373
5374 if (Old) *Old = APValue(Value);
5375
5376 APFloat One(Value.getSemantics(), 1);
5377 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
5378 APFloat::opStatus St;
5379 if (AccessKind == AK_Increment)
5380 St = Value.add(RHS: One, RM);
5381 else
5382 St = Value.subtract(RHS: One, RM);
5383 return checkFloatingPointResultForConstantFolding(Info, E, St);
5384 }
5385 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5386 if (!checkConst(QT: SubobjType))
5387 return false;
5388
5389 QualType PointeeType;
5390 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5391 PointeeType = PT->getPointeeType();
5392 else {
5393 Info.FFDiag(E);
5394 return false;
5395 }
5396
5397 LValue LVal;
5398 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5399 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType,
5400 Adjustment: AccessKind == AK_Increment ? 1 : -1))
5401 return false;
5402 LVal.moveInto(V&: Subobj);
5403 return true;
5404 }
5405};
5406} // end anonymous namespace
5407
5408/// Perform an increment or decrement on LVal.
5409static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal,
5410 QualType LValType, bool IsIncrement, APValue *Old) {
5411 if (LVal.Designator.Invalid)
5412 return false;
5413
5414 if (!Info.getLangOpts().CPlusPlus14) {
5415 Info.FFDiag(E);
5416 return false;
5417 }
5418
5419 AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement;
5420 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType);
5421 IncDecSubobjectHandler Handler = {.Info: Info, .E: cast<UnaryOperator>(Val: E), .AccessKind: AK, .Old: Old};
5422 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5423}
5424
5425/// Build an lvalue for the object argument of a member function call.
5426static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,
5427 LValue &This) {
5428 if (Object->getType()->isPointerType() && Object->isPRValue())
5429 return EvaluatePointer(E: Object, Result&: This, Info);
5430
5431 if (Object->isGLValue())
5432 return EvaluateLValue(E: Object, Result&: This, Info);
5433
5434 if (Object->getType()->isLiteralType(Ctx: Info.Ctx))
5435 return EvaluateTemporary(E: Object, Result&: This, Info);
5436
5437 if (Object->getType()->isRecordType() && Object->isPRValue())
5438 return EvaluateTemporary(E: Object, Result&: This, Info);
5439
5440 Info.FFDiag(E: Object, DiagId: diag::note_constexpr_nonliteral) << Object->getType();
5441 return false;
5442}
5443
5444/// HandleMemberPointerAccess - Evaluate a member access operation and build an
5445/// lvalue referring to the result.
5446///
5447/// \param Info - Information about the ongoing evaluation.
5448/// \param LV - An lvalue referring to the base of the member pointer.
5449/// \param RHS - The member pointer expression.
5450/// \param IncludeMember - Specifies whether the member itself is included in
5451/// the resulting LValue subobject designator. This is not possible when
5452/// creating a bound member function.
5453/// \return The field or method declaration to which the member pointer refers,
5454/// or 0 if evaluation fails.
5455static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5456 QualType LVType,
5457 LValue &LV,
5458 const Expr *RHS,
5459 bool IncludeMember = true) {
5460 MemberPtr MemPtr;
5461 if (!EvaluateMemberPointer(E: RHS, Result&: MemPtr, Info))
5462 return nullptr;
5463
5464 // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to
5465 // member value, the behavior is undefined.
5466 if (!MemPtr.getDecl()) {
5467 // FIXME: Specific diagnostic.
5468 Info.FFDiag(E: RHS);
5469 return nullptr;
5470 }
5471
5472 if (MemPtr.isDerivedMember()) {
5473 // This is a member of some derived class. Truncate LV appropriately.
5474 // The end of the derived-to-base path for the base object must match the
5475 // derived-to-base path for the member pointer.
5476 // C++23 [expr.mptr.oper]p4:
5477 // If the result of E1 is an object [...] whose most derived object does
5478 // not contain the member to which E2 refers, the behavior is undefined.
5479 if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >
5480 LV.Designator.Entries.size()) {
5481 Info.FFDiag(E: RHS);
5482 return nullptr;
5483 }
5484 unsigned PathLengthToMember =
5485 LV.Designator.Entries.size() - MemPtr.Path.size();
5486 for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {
5487 const CXXRecordDecl *LVDecl = getAsBaseClass(
5488 E: LV.Designator.Entries[PathLengthToMember + I]);
5489 const CXXRecordDecl *MPDecl = MemPtr.Path[I];
5490 if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) {
5491 Info.FFDiag(E: RHS);
5492 return nullptr;
5493 }
5494 }
5495 // MemPtr.Path only contains the base classes of the class directly
5496 // containing the member E2. It is still necessary to check that the class
5497 // directly containing the member E2 lies on the derived-to-base path of E1
5498 // to avoid incorrectly permitting member pointer access into a sibling
5499 // class of the class containing the member E2. If this class would
5500 // correspond to the most-derived class of E1, it either isn't contained in
5501 // LV.Designator.Entries or the corresponding entry refers to an array
5502 // element instead. Therefore get the most derived class directly in this
5503 // case. Otherwise the previous entry should correpond to this class.
5504 const CXXRecordDecl *LastLVDecl =
5505 (PathLengthToMember > LV.Designator.MostDerivedPathLength)
5506 ? getAsBaseClass(E: LV.Designator.Entries[PathLengthToMember - 1])
5507 : LV.Designator.MostDerivedType->getAsCXXRecordDecl();
5508 const CXXRecordDecl *LastMPDecl = MemPtr.getContainingRecord();
5509 if (LastLVDecl->getCanonicalDecl() != LastMPDecl->getCanonicalDecl()) {
5510 Info.FFDiag(E: RHS);
5511 return nullptr;
5512 }
5513
5514 // Truncate the lvalue to the appropriate derived class.
5515 if (!CastToDerivedClass(Info, E: RHS, Result&: LV, TruncatedType: MemPtr.getContainingRecord(),
5516 TruncatedElements: PathLengthToMember))
5517 return nullptr;
5518 } else if (!MemPtr.Path.empty()) {
5519 // Extend the LValue path with the member pointer's path.
5520 LV.Designator.Entries.reserve(N: LV.Designator.Entries.size() +
5521 MemPtr.Path.size() + IncludeMember);
5522
5523 // Walk down to the appropriate base class.
5524 if (const PointerType *PT = LVType->getAs<PointerType>())
5525 LVType = PT->getPointeeType();
5526 const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();
5527 assert(RD && "member pointer access on non-class-type expression");
5528 // The first class in the path is that of the lvalue.
5529 for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {
5530 const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];
5531 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, Base))
5532 return nullptr;
5533 RD = Base;
5534 }
5535 // Finally cast to the class containing the member.
5536 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD,
5537 Base: MemPtr.getContainingRecord()))
5538 return nullptr;
5539 }
5540
5541 // Add the member. Note that we cannot build bound member functions here.
5542 if (IncludeMember) {
5543 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: MemPtr.getDecl())) {
5544 if (!HandleLValueMember(Info, E: RHS, LVal&: LV, FD))
5545 return nullptr;
5546 } else if (const IndirectFieldDecl *IFD =
5547 dyn_cast<IndirectFieldDecl>(Val: MemPtr.getDecl())) {
5548 if (!HandleLValueIndirectMember(Info, E: RHS, LVal&: LV, IFD))
5549 return nullptr;
5550 } else {
5551 llvm_unreachable("can't construct reference to bound member function");
5552 }
5553 }
5554
5555 return MemPtr.getDecl();
5556}
5557
5558static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5559 const BinaryOperator *BO,
5560 LValue &LV,
5561 bool IncludeMember = true) {
5562 assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);
5563
5564 if (!EvaluateObjectArgument(Info, Object: BO->getLHS(), This&: LV)) {
5565 if (Info.noteFailure()) {
5566 MemberPtr MemPtr;
5567 EvaluateMemberPointer(E: BO->getRHS(), Result&: MemPtr, Info);
5568 }
5569 return nullptr;
5570 }
5571
5572 return HandleMemberPointerAccess(Info, LVType: BO->getLHS()->getType(), LV,
5573 RHS: BO->getRHS(), IncludeMember);
5574}
5575
5576/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
5577/// the provided lvalue, which currently refers to the base object.
5578static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
5579 LValue &Result) {
5580 SubobjectDesignator &D = Result.Designator;
5581 if (D.Invalid || !Result.checkNullPointer(Info, E, CSK: CSK_Derived))
5582 return false;
5583
5584 QualType TargetQT = E->getType();
5585 if (const PointerType *PT = TargetQT->getAs<PointerType>())
5586 TargetQT = PT->getPointeeType();
5587
5588 auto InvalidCast = [&]() {
5589 if (!Info.checkingPotentialConstantExpression() ||
5590 !Result.AllowConstexprUnknown) {
5591 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_downcast)
5592 << D.MostDerivedType << TargetQT;
5593 }
5594 return false;
5595 };
5596
5597 // Check this cast lands within the final derived-to-base subobject path.
5598 if (D.MostDerivedPathLength + E->path_size() > D.Entries.size())
5599 return InvalidCast();
5600
5601 // Check the type of the final cast. We don't need to check the path,
5602 // since a cast can only be formed if the path is unique.
5603 unsigned NewEntriesSize = D.Entries.size() - E->path_size();
5604 const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
5605 const CXXRecordDecl *FinalType;
5606 if (NewEntriesSize == D.MostDerivedPathLength)
5607 FinalType = D.MostDerivedType->getAsCXXRecordDecl();
5608 else
5609 FinalType = getAsBaseClass(E: D.Entries[NewEntriesSize - 1]);
5610 if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl())
5611 return InvalidCast();
5612
5613 // Truncate the lvalue to the appropriate derived class.
5614 return CastToDerivedClass(Info, E, Result, TruncatedType: TargetType, TruncatedElements: NewEntriesSize);
5615}
5616
5617/// Get the value to use for a default-initialized object of type T.
5618/// Return false if it encounters something invalid.
5619static bool handleDefaultInitValue(QualType T, APValue &Result,
5620 bool IsCompleteClass = true) {
5621 bool Success = true;
5622
5623 // If there is already a value present don't overwrite it.
5624 if (!Result.isAbsent())
5625 return true;
5626
5627 if (auto *RD = T->getAsCXXRecordDecl()) {
5628 if (RD->isInvalidDecl()) {
5629 Result = APValue();
5630 return false;
5631 }
5632 if (RD->isUnion()) {
5633 Result = APValue((const FieldDecl *)nullptr);
5634 return true;
5635 }
5636
5637 // bases() includes directly specified virtual bases as well.
5638 unsigned NonVirtualBases = countNonVirtualBases(RD);
5639 Result =
5640 APValue(APValue::UninitStruct(), NonVirtualBases, RD->getNumFields(),
5641 IsCompleteClass ? RD->getNumVBases() : 0);
5642
5643 unsigned Index = 0;
5644 for (const CXXBaseSpecifier &B : RD->bases()) {
5645 if (B.isVirtual())
5646 continue;
5647 Success &= handleDefaultInitValue(
5648 T: B.getType(), Result&: Result.getStructBase(i: Index), /*IsCompleteClass=*/false);
5649 ++Index;
5650 }
5651
5652 for (const auto *I : RD->fields()) {
5653 if (I->isUnnamedBitField())
5654 continue;
5655 Success &= handleDefaultInitValue(
5656 T: I->getType(), Result&: Result.getStructField(i: I->getFieldIndex()));
5657 }
5658
5659 if (IsCompleteClass) {
5660 Index = 0;
5661
5662 for (const auto &B : RD->vbases()) {
5663 Success &= handleDefaultInitValue(T: B.getType(),
5664 Result&: Result.getStructVirtualBase(i: Index),
5665 /*IsCompleteClass=*/false);
5666 ++Index;
5667 }
5668 } else {
5669 // Virtual bases should only exist at the top level of an APValue.
5670 assert(Result.getStructNumVirtualBases() == 0);
5671 }
5672
5673 return Success;
5674 }
5675
5676 if (auto *AT =
5677 dyn_cast_or_null<ConstantArrayType>(Val: T->getAsArrayTypeUnsafe())) {
5678 Result = APValue(APValue::UninitArray(), 0, AT->getZExtSize());
5679 if (Result.hasArrayFiller())
5680 Success &=
5681 handleDefaultInitValue(T: AT->getElementType(), Result&: Result.getArrayFiller());
5682 return Success;
5683 }
5684
5685 Result = APValue::IndeterminateValue();
5686 return true;
5687}
5688
5689namespace {
5690enum EvalStmtResult {
5691 /// Evaluation failed.
5692 ESR_Failed,
5693 /// Hit a 'return' statement.
5694 ESR_Returned,
5695 /// Evaluation succeeded.
5696 ESR_Succeeded,
5697 /// Hit a 'continue' statement.
5698 ESR_Continue,
5699 /// Hit a 'break' statement.
5700 ESR_Break,
5701 /// Still scanning for 'case' or 'default' statement.
5702 ESR_CaseNotFound
5703};
5704}
5705/// Evaluates the initializer of a reference.
5706static bool EvaluateInitForDeclOfReferenceType(EvalInfo &Info,
5707 const ValueDecl *D,
5708 const Expr *Init, LValue &Result,
5709 APValue &Val) {
5710 assert(Init->isGLValue() && D->getType()->isReferenceType());
5711 // A reference is an lvalue.
5712 if (!EvaluateLValue(E: Init, Result, Info))
5713 return false;
5714 // [C++26][decl.ref]
5715 // The object designated by such a glvalue can be outside its lifetime
5716 // Because a null pointer value or a pointer past the end of an object
5717 // does not point to an object, a reference in a well-defined program cannot
5718 // refer to such things;
5719 if (!Result.Designator.Invalid && Result.Designator.isOnePastTheEnd()) {
5720 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_access_past_end) << AK_Dereference;
5721 return false;
5722 }
5723
5724 // Save the result.
5725 Result.moveInto(V&: Val);
5726 return true;
5727}
5728
5729static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) {
5730 if (VD->isInvalidDecl())
5731 return false;
5732 // We don't need to evaluate the initializer for a static local.
5733 if (!VD->hasLocalStorage())
5734 return true;
5735
5736 LValue Result;
5737 APValue &Val = Info.CurrentCall->createTemporary(Key: VD, T: VD->getType(),
5738 Scope: ScopeKind::Block, LV&: Result);
5739
5740 const Expr *InitE = VD->getInit();
5741 if (!InitE) {
5742 if (VD->getType()->isDependentType())
5743 return Info.noteSideEffect();
5744 return handleDefaultInitValue(T: VD->getType(), Result&: Val);
5745 }
5746 if (InitE->isValueDependent())
5747 return false;
5748
5749 // For references to objects, check they do not designate a one-past-the-end
5750 // object.
5751 if (VD->getType()->isReferenceType()) {
5752 return EvaluateInitForDeclOfReferenceType(Info, D: VD, Init: InitE, Result, Val);
5753 } else if (!EvaluateInPlace(Result&: Val, Info, This: Result, E: InitE)) {
5754 // Wipe out any partially-computed value, to allow tracking that this
5755 // evaluation failed.
5756 Val = APValue();
5757 return false;
5758 }
5759
5760 return true;
5761}
5762
5763static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5764 const DecompositionDecl *DD);
5765
5766static bool EvaluateDecl(EvalInfo &Info, const Decl *D,
5767 bool EvaluateConditionDecl = false) {
5768 bool OK = true;
5769 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
5770 OK &= EvaluateVarDecl(Info, VD);
5771
5772 if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(Val: D);
5773 EvaluateConditionDecl && DD)
5774 OK &= EvaluateDecompositionDeclInit(Info, DD);
5775
5776 return OK;
5777}
5778
5779static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5780 const DecompositionDecl *DD) {
5781 bool OK = true;
5782 for (auto *BD : DD->flat_bindings())
5783 if (auto *VD = BD->getHoldingVar())
5784 OK &= EvaluateDecl(Info, D: VD, /*EvaluateConditionDecl=*/true);
5785
5786 return OK;
5787}
5788
5789static bool MaybeEvaluateDeferredVarDeclInit(EvalInfo &Info,
5790 const VarDecl *VD) {
5791 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(Val: VD)) {
5792 if (!EvaluateDecompositionDeclInit(Info, DD))
5793 return false;
5794 }
5795 return true;
5796}
5797
5798static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) {
5799 assert(E->isValueDependent());
5800 if (Info.noteSideEffect())
5801 return true;
5802 assert(E->containsErrors() && "valid value-dependent expression should never "
5803 "reach invalid code path.");
5804 return false;
5805}
5806
5807/// Evaluate a condition (either a variable declaration or an expression).
5808static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl,
5809 const Expr *Cond, bool &Result) {
5810 if (Cond->isValueDependent())
5811 return false;
5812 FullExpressionRAII Scope(Info);
5813 if (CondDecl && !EvaluateDecl(Info, D: CondDecl))
5814 return false;
5815 if (!EvaluateAsBooleanCondition(E: Cond, Result, Info))
5816 return false;
5817 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: CondDecl))
5818 return false;
5819 return Scope.destroy();
5820}
5821
5822namespace {
5823/// A location where the result (returned value) of evaluating a
5824/// statement should be stored.
5825struct StmtResult {
5826 /// The APValue that should be filled in with the returned value.
5827 APValue &Value;
5828 /// The location containing the result, if any (used to support RVO).
5829 const LValue *Slot;
5830};
5831
5832struct TempVersionRAII {
5833 CallStackFrame &Frame;
5834
5835 TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) {
5836 Frame.pushTempVersion();
5837 }
5838
5839 ~TempVersionRAII() {
5840 Frame.popTempVersion();
5841 }
5842};
5843
5844}
5845
5846static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
5847 const Stmt *S,
5848 const SwitchCase *SC = nullptr);
5849
5850/// Helper to implement named break/continue. Returns 'true' if the evaluation
5851/// result should be propagated up. Otherwise, it sets the evaluation result
5852/// to either Continue to continue the current loop, or Succeeded to break it.
5853static bool ShouldPropagateBreakContinue(EvalInfo &Info,
5854 const Stmt *LoopOrSwitch,
5855 ArrayRef<BlockScopeRAII *> Scopes,
5856 EvalStmtResult &ESR) {
5857 bool IsSwitch = isa<SwitchStmt>(Val: LoopOrSwitch);
5858
5859 // For loops, map Succeeded to Continue so we don't have to check for both.
5860 if (!IsSwitch && ESR == ESR_Succeeded) {
5861 ESR = ESR_Continue;
5862 return false;
5863 }
5864
5865 if (ESR != ESR_Break && ESR != ESR_Continue)
5866 return false;
5867
5868 // Are we breaking out of or continuing this statement?
5869 bool CanBreakOrContinue = !IsSwitch || ESR == ESR_Break;
5870 const Stmt *StackTop = Info.BreakContinueStack.back();
5871 if (CanBreakOrContinue && (StackTop == nullptr || StackTop == LoopOrSwitch)) {
5872 Info.BreakContinueStack.pop_back();
5873 if (ESR == ESR_Break)
5874 ESR = ESR_Succeeded;
5875 return false;
5876 }
5877
5878 // We're not. Propagate the result up.
5879 for (BlockScopeRAII *S : Scopes) {
5880 if (!S->destroy()) {
5881 ESR = ESR_Failed;
5882 break;
5883 }
5884 }
5885 return true;
5886}
5887
5888/// Evaluate the body of a loop, and translate the result as appropriate.
5889static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info,
5890 const Stmt *Body,
5891 const SwitchCase *Case = nullptr) {
5892 BlockScopeRAII Scope(Info);
5893
5894 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Body, SC: Case);
5895 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5896 ESR = ESR_Failed;
5897
5898 return ESR;
5899}
5900
5901/// Evaluate a switch statement.
5902static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
5903 const SwitchStmt *SS) {
5904 BlockScopeRAII Scope(Info);
5905
5906 // Evaluate the switch condition.
5907 APSInt Value;
5908 {
5909 if (const Stmt *Init = SS->getInit()) {
5910 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
5911 if (ESR != ESR_Succeeded) {
5912 if (ESR != ESR_Failed && !Scope.destroy())
5913 ESR = ESR_Failed;
5914 return ESR;
5915 }
5916 }
5917
5918 FullExpressionRAII CondScope(Info);
5919 if (SS->getConditionVariable() &&
5920 !EvaluateDecl(Info, D: SS->getConditionVariable()))
5921 return ESR_Failed;
5922 if (SS->getCond()->isValueDependent()) {
5923 // We don't know what the value is, and which branch should jump to.
5924 EvaluateDependentExpr(E: SS->getCond(), Info);
5925 return ESR_Failed;
5926 }
5927 if (!EvaluateInteger(E: SS->getCond(), Result&: Value, Info))
5928 return ESR_Failed;
5929
5930 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: SS->getConditionVariable()))
5931 return ESR_Failed;
5932
5933 if (!CondScope.destroy())
5934 return ESR_Failed;
5935 }
5936
5937 // Find the switch case corresponding to the value of the condition.
5938 // FIXME: Cache this lookup.
5939 const SwitchCase *Found = nullptr;
5940 for (const SwitchCase *SC = SS->getSwitchCaseList(); SC;
5941 SC = SC->getNextSwitchCase()) {
5942 if (isa<DefaultStmt>(Val: SC)) {
5943 Found = SC;
5944 continue;
5945 }
5946
5947 const CaseStmt *CS = cast<CaseStmt>(Val: SC);
5948 const Expr *LHS = CS->getLHS();
5949 const Expr *RHS = CS->getRHS();
5950 if (LHS->isValueDependent() || (RHS && RHS->isValueDependent()))
5951 return ESR_Failed;
5952 APSInt LHSValue = LHS->EvaluateKnownConstInt(Ctx: Info.Ctx);
5953 APSInt RHSValue = RHS ? RHS->EvaluateKnownConstInt(Ctx: Info.Ctx) : LHSValue;
5954 if (LHSValue <= Value && Value <= RHSValue) {
5955 Found = SC;
5956 break;
5957 }
5958 }
5959
5960 if (!Found)
5961 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
5962
5963 // Search the switch body for the switch case and evaluate it from there.
5964 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SS->getBody(), SC: Found);
5965 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5966 return ESR_Failed;
5967 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: SS, /*Scopes=*/{}, ESR))
5968 return ESR;
5969
5970 switch (ESR) {
5971 case ESR_Break:
5972 llvm_unreachable("Should have been converted to Succeeded");
5973 case ESR_Succeeded:
5974 case ESR_Continue:
5975 case ESR_Failed:
5976 case ESR_Returned:
5977 return ESR;
5978 case ESR_CaseNotFound:
5979 // This can only happen if the switch case is nested within a statement
5980 // expression. We have no intention of supporting that.
5981 Info.FFDiag(Loc: Found->getBeginLoc(),
5982 DiagId: diag::note_constexpr_stmt_expr_unsupported);
5983 return ESR_Failed;
5984 }
5985 llvm_unreachable("Invalid EvalStmtResult!");
5986}
5987
5988static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) {
5989 // An expression E is a core constant expression unless the evaluation of E
5990 // would evaluate one of the following: [C++23] - a control flow that passes
5991 // through a declaration of a variable with static or thread storage duration
5992 // unless that variable is usable in constant expressions.
5993 if (VD->isLocalVarDecl() && VD->isStaticLocal() &&
5994 !VD->isUsableInConstantExpressions(C: Info.Ctx)) {
5995 Info.CCEDiag(Loc: VD->getLocation(), DiagId: diag::note_constexpr_static_local)
5996 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
5997 return false;
5998 }
5999 return true;
6000}
6001
6002// Evaluate a statement.
6003static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
6004 const Stmt *S, const SwitchCase *Case) {
6005 if (!Info.nextStep(S))
6006 return ESR_Failed;
6007
6008 // If we're hunting down a 'case' or 'default' label, recurse through
6009 // substatements until we hit the label.
6010 if (Case) {
6011 switch (S->getStmtClass()) {
6012 case Stmt::CompoundStmtClass:
6013 // FIXME: Precompute which substatement of a compound statement we
6014 // would jump to, and go straight there rather than performing a
6015 // linear scan each time.
6016 case Stmt::LabelStmtClass:
6017 case Stmt::AttributedStmtClass:
6018 case Stmt::DoStmtClass:
6019 break;
6020
6021 case Stmt::CaseStmtClass:
6022 case Stmt::DefaultStmtClass:
6023 if (Case == S)
6024 Case = nullptr;
6025 break;
6026
6027 case Stmt::IfStmtClass: {
6028 // FIXME: Precompute which side of an 'if' we would jump to, and go
6029 // straight there rather than scanning both sides.
6030 const IfStmt *IS = cast<IfStmt>(Val: S);
6031
6032 // Wrap the evaluation in a block scope, in case it's a DeclStmt
6033 // preceded by our switch label.
6034 BlockScopeRAII Scope(Info);
6035
6036 // Step into the init statement in case it brings an (uninitialized)
6037 // variable into scope.
6038 if (const Stmt *Init = IS->getInit()) {
6039 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
6040 if (ESR != ESR_CaseNotFound) {
6041 assert(ESR != ESR_Succeeded);
6042 return ESR;
6043 }
6044 }
6045
6046 // Condition variable must be initialized if it exists.
6047 // FIXME: We can skip evaluating the body if there's a condition
6048 // variable, as there can't be any case labels within it.
6049 // (The same is true for 'for' statements.)
6050
6051 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: IS->getThen(), Case);
6052 if (ESR == ESR_Failed)
6053 return ESR;
6054 if (ESR != ESR_CaseNotFound)
6055 return Scope.destroy() ? ESR : ESR_Failed;
6056 if (!IS->getElse())
6057 return ESR_CaseNotFound;
6058
6059 ESR = EvaluateStmt(Result, Info, S: IS->getElse(), Case);
6060 if (ESR == ESR_Failed)
6061 return ESR;
6062 if (ESR != ESR_CaseNotFound)
6063 return Scope.destroy() ? ESR : ESR_Failed;
6064 return ESR_CaseNotFound;
6065 }
6066
6067 case Stmt::WhileStmtClass: {
6068 EvalStmtResult ESR =
6069 EvaluateLoopBody(Result, Info, Body: cast<WhileStmt>(Val: S)->getBody(), Case);
6070 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: S, /*Scopes=*/{}, ESR))
6071 return ESR;
6072 if (ESR != ESR_Continue)
6073 return ESR;
6074 break;
6075 }
6076
6077 case Stmt::ForStmtClass: {
6078 const ForStmt *FS = cast<ForStmt>(Val: S);
6079 BlockScopeRAII Scope(Info);
6080
6081 // Step into the init statement in case it brings an (uninitialized)
6082 // variable into scope.
6083 if (const Stmt *Init = FS->getInit()) {
6084 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
6085 if (ESR != ESR_CaseNotFound) {
6086 assert(ESR != ESR_Succeeded);
6087 return ESR;
6088 }
6089 }
6090
6091 EvalStmtResult ESR =
6092 EvaluateLoopBody(Result, Info, Body: FS->getBody(), Case);
6093 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, /*Scopes=*/{}, ESR))
6094 return ESR;
6095 if (ESR != ESR_Continue)
6096 return ESR;
6097 if (const auto *Inc = FS->getInc()) {
6098 if (Inc->isValueDependent()) {
6099 if (!EvaluateDependentExpr(E: Inc, Info))
6100 return ESR_Failed;
6101 } else {
6102 FullExpressionRAII IncScope(Info);
6103 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6104 return ESR_Failed;
6105 }
6106 }
6107 break;
6108 }
6109
6110 case Stmt::DeclStmtClass: {
6111 // Start the lifetime of any uninitialized variables we encounter. They
6112 // might be used by the selected branch of the switch.
6113 const DeclStmt *DS = cast<DeclStmt>(Val: S);
6114 for (const auto *D : DS->decls()) {
6115 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
6116 if (!CheckLocalVariableDeclaration(Info, VD))
6117 return ESR_Failed;
6118 if (VD->hasLocalStorage() && !VD->getInit())
6119 if (!EvaluateVarDecl(Info, VD))
6120 return ESR_Failed;
6121 // FIXME: If the variable has initialization that can't be jumped
6122 // over, bail out of any immediately-surrounding compound-statement
6123 // too. There can't be any case labels here.
6124 }
6125 }
6126 return ESR_CaseNotFound;
6127 }
6128
6129 default:
6130 return ESR_CaseNotFound;
6131 }
6132 }
6133
6134 switch (S->getStmtClass()) {
6135 default:
6136 if (const Expr *E = dyn_cast<Expr>(Val: S)) {
6137 if (E->isValueDependent()) {
6138 if (!EvaluateDependentExpr(E, Info))
6139 return ESR_Failed;
6140 } else {
6141 // Don't bother evaluating beyond an expression-statement which couldn't
6142 // be evaluated.
6143 // FIXME: Do we need the FullExpressionRAII object here?
6144 // VisitExprWithCleanups should create one when necessary.
6145 FullExpressionRAII Scope(Info);
6146 if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy())
6147 return ESR_Failed;
6148 }
6149 return ESR_Succeeded;
6150 }
6151
6152 Info.FFDiag(Loc: S->getBeginLoc()) << S->getSourceRange();
6153 return ESR_Failed;
6154
6155 case Stmt::NullStmtClass:
6156 return ESR_Succeeded;
6157
6158 case Stmt::DeclStmtClass: {
6159 const DeclStmt *DS = cast<DeclStmt>(Val: S);
6160 for (const auto *D : DS->decls()) {
6161 const VarDecl *VD = dyn_cast_or_null<VarDecl>(Val: D);
6162 if (VD && !CheckLocalVariableDeclaration(Info, VD))
6163 return ESR_Failed;
6164
6165 if (const auto *ESD = dyn_cast<CXXExpansionStmtDecl>(Val: D)) {
6166 assert(ESD->getInstantiations() && "not expanded?");
6167 return EvaluateStmt(Result, Info, S: ESD->getInstantiations(), Case);
6168 }
6169
6170 // Each declaration initialization is its own full-expression.
6171 FullExpressionRAII Scope(Info);
6172 if (!EvaluateDecl(Info, D, /*EvaluateConditionDecl=*/true) &&
6173 !Info.noteFailure())
6174 return ESR_Failed;
6175 if (!Scope.destroy())
6176 return ESR_Failed;
6177 }
6178 return ESR_Succeeded;
6179 }
6180
6181 case Stmt::ReturnStmtClass: {
6182 const Expr *RetExpr = cast<ReturnStmt>(Val: S)->getRetValue();
6183 FullExpressionRAII Scope(Info);
6184 if (RetExpr && RetExpr->isValueDependent()) {
6185 EvaluateDependentExpr(E: RetExpr, Info);
6186 // We know we returned, but we don't know what the value is.
6187 return ESR_Failed;
6188 }
6189 if (RetExpr &&
6190 !(Result.Slot
6191 ? EvaluateInPlace(Result&: Result.Value, Info, This: *Result.Slot, E: RetExpr)
6192 : Evaluate(Result&: Result.Value, Info, E: RetExpr)))
6193 return ESR_Failed;
6194 return Scope.destroy() ? ESR_Returned : ESR_Failed;
6195 }
6196
6197 case Stmt::CompoundStmtClass: {
6198 BlockScopeRAII Scope(Info);
6199
6200 const CompoundStmt *CS = cast<CompoundStmt>(Val: S);
6201 for (const auto *BI : CS->body()) {
6202 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: BI, Case);
6203 if (ESR == ESR_Succeeded)
6204 Case = nullptr;
6205 else if (ESR != ESR_CaseNotFound) {
6206 if (ESR != ESR_Failed && !Scope.destroy())
6207 return ESR_Failed;
6208 return ESR;
6209 }
6210 }
6211 if (Case)
6212 return ESR_CaseNotFound;
6213 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6214 }
6215
6216 case Stmt::IfStmtClass: {
6217 const IfStmt *IS = cast<IfStmt>(Val: S);
6218
6219 // Evaluate the condition, as either a var decl or as an expression.
6220 BlockScopeRAII Scope(Info);
6221 if (const Stmt *Init = IS->getInit()) {
6222 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
6223 if (ESR != ESR_Succeeded) {
6224 if (ESR != ESR_Failed && !Scope.destroy())
6225 return ESR_Failed;
6226 return ESR;
6227 }
6228 }
6229 bool Cond;
6230 if (IS->isConsteval()) {
6231 Cond = IS->isNonNegatedConsteval();
6232 // If we are not in a constant context, if consteval should not evaluate
6233 // to true.
6234 if (!Info.InConstantContext)
6235 Cond = !Cond;
6236 } else if (!EvaluateCond(Info, CondDecl: IS->getConditionVariable(), Cond: IS->getCond(),
6237 Result&: Cond))
6238 return ESR_Failed;
6239
6240 if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) {
6241 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SubStmt);
6242 if (ESR != ESR_Succeeded) {
6243 if (ESR != ESR_Failed && !Scope.destroy())
6244 return ESR_Failed;
6245 return ESR;
6246 }
6247 }
6248 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6249 }
6250
6251 case Stmt::WhileStmtClass: {
6252 const WhileStmt *WS = cast<WhileStmt>(Val: S);
6253 while (true) {
6254 BlockScopeRAII Scope(Info);
6255 bool Continue;
6256 if (!EvaluateCond(Info, CondDecl: WS->getConditionVariable(), Cond: WS->getCond(),
6257 Result&: Continue))
6258 return ESR_Failed;
6259 if (!Continue)
6260 break;
6261
6262 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: WS->getBody());
6263 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: WS, Scopes: &Scope, ESR))
6264 return ESR;
6265
6266 if (ESR != ESR_Continue) {
6267 if (ESR != ESR_Failed && !Scope.destroy())
6268 return ESR_Failed;
6269 return ESR;
6270 }
6271 if (!Scope.destroy())
6272 return ESR_Failed;
6273 }
6274 return ESR_Succeeded;
6275 }
6276
6277 case Stmt::DoStmtClass: {
6278 const DoStmt *DS = cast<DoStmt>(Val: S);
6279 bool Continue;
6280 do {
6281 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: DS->getBody(), Case);
6282 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: DS, /*Scopes=*/{}, ESR))
6283 return ESR;
6284 if (ESR != ESR_Continue)
6285 return ESR;
6286 Case = nullptr;
6287
6288 if (DS->getCond()->isValueDependent()) {
6289 EvaluateDependentExpr(E: DS->getCond(), Info);
6290 // Bailout as we don't know whether to keep going or terminate the loop.
6291 return ESR_Failed;
6292 }
6293 FullExpressionRAII CondScope(Info);
6294 if (!EvaluateAsBooleanCondition(E: DS->getCond(), Result&: Continue, Info) ||
6295 !CondScope.destroy())
6296 return ESR_Failed;
6297 } while (Continue);
6298 return ESR_Succeeded;
6299 }
6300
6301 case Stmt::ForStmtClass: {
6302 const ForStmt *FS = cast<ForStmt>(Val: S);
6303 BlockScopeRAII ForScope(Info);
6304 if (FS->getInit()) {
6305 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6306 if (ESR != ESR_Succeeded) {
6307 if (ESR != ESR_Failed && !ForScope.destroy())
6308 return ESR_Failed;
6309 return ESR;
6310 }
6311 }
6312 while (true) {
6313 BlockScopeRAII IterScope(Info);
6314 bool Continue = true;
6315 if (FS->getCond() && !EvaluateCond(Info, CondDecl: FS->getConditionVariable(),
6316 Cond: FS->getCond(), Result&: Continue))
6317 return ESR_Failed;
6318
6319 if (!Continue) {
6320 if (!IterScope.destroy())
6321 return ESR_Failed;
6322 break;
6323 }
6324
6325 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6326 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&IterScope, &ForScope}, ESR))
6327 return ESR;
6328 if (ESR != ESR_Continue) {
6329 if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy()))
6330 return ESR_Failed;
6331 return ESR;
6332 }
6333
6334 if (const auto *Inc = FS->getInc()) {
6335 if (Inc->isValueDependent()) {
6336 if (!EvaluateDependentExpr(E: Inc, Info))
6337 return ESR_Failed;
6338 } else {
6339 FullExpressionRAII IncScope(Info);
6340 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6341 return ESR_Failed;
6342 }
6343 }
6344
6345 if (!IterScope.destroy())
6346 return ESR_Failed;
6347 }
6348 return ForScope.destroy() ? ESR_Succeeded : ESR_Failed;
6349 }
6350
6351 case Stmt::CXXForRangeStmtClass: {
6352 const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(Val: S);
6353 BlockScopeRAII Scope(Info);
6354
6355 // Evaluate the init-statement if present.
6356 if (FS->getInit()) {
6357 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6358 if (ESR != ESR_Succeeded) {
6359 if (ESR != ESR_Failed && !Scope.destroy())
6360 return ESR_Failed;
6361 return ESR;
6362 }
6363 }
6364
6365 // Initialize the __range variable.
6366 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getRangeStmt());
6367 if (ESR != ESR_Succeeded) {
6368 if (ESR != ESR_Failed && !Scope.destroy())
6369 return ESR_Failed;
6370 return ESR;
6371 }
6372
6373 // In error-recovery cases it's possible to get here even if we failed to
6374 // synthesize the __begin and __end variables.
6375 if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond())
6376 return ESR_Failed;
6377
6378 // Create the __begin and __end iterators.
6379 ESR = EvaluateStmt(Result, Info, S: FS->getBeginStmt());
6380 if (ESR != ESR_Succeeded) {
6381 if (ESR != ESR_Failed && !Scope.destroy())
6382 return ESR_Failed;
6383 return ESR;
6384 }
6385 ESR = EvaluateStmt(Result, Info, S: FS->getEndStmt());
6386 if (ESR != ESR_Succeeded) {
6387 if (ESR != ESR_Failed && !Scope.destroy())
6388 return ESR_Failed;
6389 return ESR;
6390 }
6391
6392 while (true) {
6393 // Condition: __begin != __end.
6394 {
6395 if (FS->getCond()->isValueDependent()) {
6396 EvaluateDependentExpr(E: FS->getCond(), Info);
6397 // We don't know whether to keep going or terminate the loop.
6398 return ESR_Failed;
6399 }
6400 bool Continue = true;
6401 FullExpressionRAII CondExpr(Info);
6402 if (!EvaluateAsBooleanCondition(E: FS->getCond(), Result&: Continue, Info))
6403 return ESR_Failed;
6404 if (!Continue)
6405 break;
6406 }
6407
6408 // User's variable declaration, initialized by *__begin.
6409 BlockScopeRAII InnerScope(Info);
6410 ESR = EvaluateStmt(Result, Info, S: FS->getLoopVarStmt());
6411 if (ESR != ESR_Succeeded) {
6412 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6413 return ESR_Failed;
6414 return ESR;
6415 }
6416
6417 // Loop body.
6418 ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6419 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&InnerScope, &Scope}, ESR))
6420 return ESR;
6421 if (ESR != ESR_Continue) {
6422 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6423 return ESR_Failed;
6424 return ESR;
6425 }
6426 if (FS->getInc()->isValueDependent()) {
6427 if (!EvaluateDependentExpr(E: FS->getInc(), Info))
6428 return ESR_Failed;
6429 } else {
6430 // Increment: ++__begin
6431 if (!EvaluateIgnoredValue(Info, E: FS->getInc()))
6432 return ESR_Failed;
6433 }
6434
6435 if (!InnerScope.destroy())
6436 return ESR_Failed;
6437 }
6438
6439 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6440 }
6441
6442 case Stmt::CXXExpansionStmtInstantiationClass: {
6443 BlockScopeRAII Scope(Info);
6444 const auto *Expansion = cast<CXXExpansionStmtInstantiation>(Val: S);
6445 for (const Stmt *PreambleStmt : Expansion->getPreambleStmts()) {
6446 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: PreambleStmt);
6447 if (ESR != ESR_Succeeded) {
6448 if (ESR != ESR_Failed && !Scope.destroy())
6449 return ESR_Failed;
6450 return ESR;
6451 }
6452 }
6453
6454 // No need to push an extra scope for these since they're already
6455 // CompoundStmts.
6456 EvalStmtResult ESR = ESR_Succeeded;
6457 for (const Stmt *Instantiation : Expansion->getInstantiations()) {
6458 ESR = EvaluateStmt(Result, Info, S: Instantiation);
6459 if (ESR == ESR_Failed ||
6460 ShouldPropagateBreakContinue(Info, LoopOrSwitch: Expansion, Scopes: &Scope, ESR))
6461 return ESR;
6462 if (ESR != ESR_Continue) {
6463 // Succeeded here actually means we encountered a 'break'.
6464 assert(ESR == ESR_Succeeded || ESR == ESR_Returned);
6465 break;
6466 }
6467 }
6468
6469 // Map Continue back to Succeeded if we fell off the end of the loop.
6470 if (ESR == ESR_Continue)
6471 ESR = ESR_Succeeded;
6472
6473 return Scope.destroy() ? ESR : ESR_Failed;
6474 }
6475
6476 case Stmt::SwitchStmtClass:
6477 return EvaluateSwitch(Result, Info, SS: cast<SwitchStmt>(Val: S));
6478
6479 case Stmt::ContinueStmtClass:
6480 case Stmt::BreakStmtClass: {
6481 auto *B = cast<LoopControlStmt>(Val: S);
6482 Info.BreakContinueStack.push_back(Elt: B->getNamedLoopOrSwitch());
6483 return isa<ContinueStmt>(Val: S) ? ESR_Continue : ESR_Break;
6484 }
6485
6486 case Stmt::LabelStmtClass:
6487 return EvaluateStmt(Result, Info, S: cast<LabelStmt>(Val: S)->getSubStmt(), Case);
6488
6489 case Stmt::AttributedStmtClass: {
6490 const auto *AS = cast<AttributedStmt>(Val: S);
6491 const auto *SS = AS->getSubStmt();
6492 MSConstexprContextRAII ConstexprContext(
6493 *Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(container: AS->getAttrs()) &&
6494 isa<ReturnStmt>(Val: SS));
6495
6496 auto LO = Info.Ctx.getLangOpts();
6497 if (LO.CXXAssumptions && !LO.MSVCCompat) {
6498 for (auto *Attr : AS->getAttrs()) {
6499 auto *AA = dyn_cast<CXXAssumeAttr>(Val: Attr);
6500 if (!AA)
6501 continue;
6502
6503 auto *Assumption = AA->getAssumption();
6504 if (Assumption->isValueDependent())
6505 return ESR_Failed;
6506
6507 if (Assumption->HasSideEffects(Ctx: Info.Ctx))
6508 continue;
6509
6510 bool Value;
6511 if (!EvaluateAsBooleanCondition(E: Assumption, Result&: Value, Info))
6512 return ESR_Failed;
6513 if (!Value) {
6514 Info.CCEDiag(Loc: Assumption->getExprLoc(),
6515 DiagId: diag::note_constexpr_assumption_failed);
6516 return ESR_Failed;
6517 }
6518 }
6519 }
6520
6521 return EvaluateStmt(Result, Info, S: SS, Case);
6522 }
6523
6524 case Stmt::CaseStmtClass:
6525 case Stmt::DefaultStmtClass:
6526 return EvaluateStmt(Result, Info, S: cast<SwitchCase>(Val: S)->getSubStmt(), Case);
6527 case Stmt::CXXTryStmtClass:
6528 // Evaluate try blocks by evaluating all sub statements.
6529 return EvaluateStmt(Result, Info, S: cast<CXXTryStmt>(Val: S)->getTryBlock(), Case);
6530 }
6531}
6532
6533/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
6534/// default constructor. If so, we'll fold it whether or not it's marked as
6535/// constexpr. If it is marked as constexpr, we will never implicitly define it,
6536/// so we need special handling.
6537static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
6538 const CXXConstructorDecl *CD,
6539 bool IsValueInitialization) {
6540 if (!CD->isTrivial() || !CD->isDefaultConstructor())
6541 return false;
6542
6543 // Value-initialization does not call a trivial default constructor, so such a
6544 // call is a core constant expression whether or not the constructor is
6545 // constexpr.
6546 if (!CD->isConstexpr() && !IsValueInitialization) {
6547 if (Info.getLangOpts().CPlusPlus11) {
6548 // FIXME: If DiagDecl is an implicitly-declared special member function,
6549 // we should be much more explicit about why it's not constexpr.
6550 Info.CCEDiag(Loc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6551 << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
6552 Info.Note(Loc: CD->getLocation(), DiagId: diag::note_declared_at);
6553 } else {
6554 Info.CCEDiag(Loc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6555 }
6556 }
6557 return true;
6558}
6559
6560/// CheckConstexprFunction - Check that a function can be called in a constant
6561/// expression.
6562static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
6563 const FunctionDecl *Declaration,
6564 const FunctionDecl *Definition,
6565 const Stmt *Body) {
6566 // Potential constant expressions can contain calls to declared, but not yet
6567 // defined, constexpr functions.
6568 if (Info.checkingPotentialConstantExpression() && !Definition &&
6569 Declaration->isConstexpr())
6570 return false;
6571
6572 // Bail out if the function declaration itself is invalid. We will
6573 // have produced a relevant diagnostic while parsing it, so just
6574 // note the problematic sub-expression.
6575 if (Declaration->isInvalidDecl()) {
6576 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6577 return false;
6578 }
6579
6580 // DR1872: An instantiated virtual constexpr function can't be called in a
6581 // constant expression (prior to C++20). We can still constant-fold such a
6582 // call.
6583 if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Val: Declaration) &&
6584 cast<CXXMethodDecl>(Val: Declaration)->isVirtual())
6585 Info.CCEDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_call);
6586
6587 if (Definition && Definition->isInvalidDecl()) {
6588 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6589 return false;
6590 }
6591
6592 // Can we evaluate this function call?
6593 if (Definition && Body &&
6594 (Definition->isConstexpr() || (Info.CurrentCall->CanEvalMSConstexpr &&
6595 Definition->hasAttr<MSConstexprAttr>())))
6596 return true;
6597
6598 const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
6599 // Special note for the assert() macro, as the normal error message falsely
6600 // implies we cannot use an assertion during constant evaluation.
6601 if (CallLoc.isMacroID() && DiagDecl->getIdentifier()) {
6602 // FIXME: Instead of checking for an implementation-defined function,
6603 // check and evaluate the assert() macro.
6604 StringRef Name = DiagDecl->getName();
6605 bool AssertFailed =
6606 Name == "__assert_rtn" || Name == "__assert_fail" || Name == "_wassert";
6607 if (AssertFailed) {
6608 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_assert_failed);
6609 return false;
6610 }
6611 }
6612
6613 if (Info.getLangOpts().CPlusPlus11) {
6614 // If this function is not constexpr because it is an inherited
6615 // non-constexpr constructor, diagnose that directly.
6616 auto *CD = dyn_cast<CXXConstructorDecl>(Val: DiagDecl);
6617 if (CD && CD->isInheritingConstructor()) {
6618 auto *Inherited = CD->getInheritedConstructor().getConstructor();
6619 if (!Inherited->isConstexpr())
6620 DiagDecl = CD = Inherited;
6621 }
6622
6623 // FIXME: If DiagDecl is an implicitly-declared special member function
6624 // or an inheriting constructor, we should be much more explicit about why
6625 // it's not constexpr.
6626 if (CD && CD->isInheritingConstructor())
6627 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_inhctor, ExtraNotes: 1)
6628 << CD->getInheritedConstructor().getConstructor()->getParent();
6629 else
6630 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6631 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
6632 Info.Note(Loc: DiagDecl->getLocation(), DiagId: diag::note_declared_at);
6633 } else {
6634 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6635 }
6636 return false;
6637}
6638
6639namespace {
6640struct CheckDynamicTypeHandler {
6641 AccessKinds AccessKind;
6642 typedef bool result_type;
6643 bool failed() { return false; }
6644 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
6645 return true;
6646 }
6647 bool found(APSInt &Value, QualType SubobjType) { return true; }
6648 bool found(APFloat &Value, QualType SubobjType) { return true; }
6649};
6650} // end anonymous namespace
6651
6652/// Check that we can access the notional vptr of an object / determine its
6653/// dynamic type.
6654static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This,
6655 AccessKinds AK, bool Polymorphic) {
6656 if (This.Designator.Invalid)
6657 return false;
6658
6659 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: This, LValType: QualType());
6660
6661 if (!Obj)
6662 return false;
6663
6664 if (!Obj.Value) {
6665 // The object is not usable in constant expressions, so we can't inspect
6666 // its value to see if it's in-lifetime or what the active union members
6667 // are. We can still check for a one-past-the-end lvalue.
6668 if (This.Designator.isOnePastTheEnd() ||
6669 This.Designator.isMostDerivedAnUnsizedArray()) {
6670 Info.FFDiag(E, DiagId: This.Designator.isOnePastTheEnd()
6671 ? diag::note_constexpr_access_past_end
6672 : diag::note_constexpr_access_unsized_array)
6673 << AK;
6674 return false;
6675 } else if (Polymorphic) {
6676 // Conservatively refuse to perform a polymorphic operation if we would
6677 // not be able to read a notional 'vptr' value.
6678 if (!Info.checkingPotentialConstantExpression() ||
6679 !This.AllowConstexprUnknown) {
6680 APValue Val;
6681 This.moveInto(V&: Val);
6682 QualType StarThisType =
6683 Info.Ctx.getLValueReferenceType(T: This.Designator.getType(Ctx&: Info.Ctx));
6684 Info.FFDiag(E, DiagId: diag::note_constexpr_polymorphic_unknown_dynamic_type)
6685 << AK << Val.getAsString(Ctx: Info.Ctx, Ty: StarThisType);
6686 }
6687 return false;
6688 }
6689 return true;
6690 }
6691
6692 CheckDynamicTypeHandler Handler{.AccessKind: AK};
6693 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
6694}
6695
6696/// Check that the pointee of the 'this' pointer in a member function call is
6697/// either within its lifetime or in its period of construction or destruction.
6698static bool
6699checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E,
6700 const LValue &This,
6701 const CXXMethodDecl *NamedMember) {
6702 return checkDynamicType(
6703 Info, E, This,
6704 AK: isa<CXXDestructorDecl>(Val: NamedMember) ? AK_Destroy : AK_MemberCall, Polymorphic: false);
6705}
6706
6707struct DynamicType {
6708 /// The dynamic class type of the object.
6709 const CXXRecordDecl *Type;
6710 /// The corresponding path length in the lvalue.
6711 unsigned PathLength;
6712};
6713
6714static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator,
6715 unsigned PathLength) {
6716 assert(PathLength >= Designator.MostDerivedPathLength && PathLength <=
6717 Designator.Entries.size() && "invalid path length");
6718 return (PathLength == Designator.MostDerivedPathLength)
6719 ? Designator.MostDerivedType->getAsCXXRecordDecl()
6720 : getAsBaseClass(E: Designator.Entries[PathLength - 1]);
6721}
6722
6723/// Determine the dynamic type of an object.
6724static std::optional<DynamicType> ComputeDynamicType(EvalInfo &Info,
6725 const Expr *E,
6726 LValue &This,
6727 AccessKinds AK) {
6728 // If we don't have an lvalue denoting an object of class type, there is no
6729 // meaningful dynamic type. (We consider objects of non-class type to have no
6730 // dynamic type.)
6731 if (!checkDynamicType(Info, E, This, AK,
6732 Polymorphic: AK != AK_TypeId || This.AllowConstexprUnknown))
6733 return std::nullopt;
6734
6735 if (This.Designator.Invalid)
6736 return std::nullopt;
6737
6738 // Refuse to compute a dynamic type in the presence of virtual bases
6739 // before C++26. This shouldn't happen other than in constant-folding
6740 // situations, since literal types can't have virtual bases.
6741 const CXXRecordDecl *Class =
6742 This.Designator.MostDerivedType->getAsCXXRecordDecl();
6743 if (!Class || (!Info.getLangOpts().CPlusPlus26 && Class->getNumVBases())) {
6744 Info.FFDiag(E);
6745 return std::nullopt;
6746 }
6747
6748 // FIXME: For very deep class hierarchies, it might be beneficial to use a
6749 // binary search here instead. But the overwhelmingly common case is that
6750 // we're not in the middle of a constructor, so it probably doesn't matter
6751 // in practice.
6752 ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries;
6753 for (unsigned PathLength = This.Designator.MostDerivedPathLength;
6754 PathLength <= Path.size(); ++PathLength) {
6755 switch (Info.isEvaluatingCtorDtor(Base: This.getLValueBase(),
6756 Path: Path.slice(N: 0, M: PathLength))) {
6757 case ConstructionPhase::Bases:
6758 case ConstructionPhase::DestroyingBases:
6759 // We're constructing or destroying a base class. This is not the dynamic
6760 // type.
6761 break;
6762
6763 case ConstructionPhase::None:
6764 case ConstructionPhase::AfterBases:
6765 case ConstructionPhase::AfterFields:
6766 case ConstructionPhase::Destroying:
6767 // We've finished constructing the base classes and not yet started
6768 // destroying them again, so this is the dynamic type.
6769 return DynamicType{.Type: getBaseClassType(Designator&: This.Designator, PathLength),
6770 .PathLength: PathLength};
6771 }
6772 }
6773
6774 // CWG issue 1517: we're constructing a base class of the object described by
6775 // 'This', so that object has not yet begun its period of construction and
6776 // any polymorphic operation on it results in undefined behavior.
6777 Info.FFDiag(E);
6778 return std::nullopt;
6779}
6780
6781/// Perform virtual dispatch.
6782static const CXXMethodDecl *HandleVirtualDispatch(
6783 EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found,
6784 llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) {
6785 std::optional<DynamicType> DynType = ComputeDynamicType(
6786 Info, E, This,
6787 AK: isa<CXXDestructorDecl>(Val: Found) ? AK_Destroy : AK_MemberCall);
6788 if (!DynType)
6789 return nullptr;
6790
6791 // Find the final overrider. It must be declared in one of the classes on the
6792 // path from the dynamic type to the static type.
6793 // FIXME: If we ever allow literal types to have virtual base classes, that
6794 // won't be true.
6795 const CXXMethodDecl *Callee = Found;
6796 unsigned PathLength = DynType->PathLength;
6797 for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) {
6798 const CXXRecordDecl *Class = getBaseClassType(Designator&: This.Designator, PathLength);
6799 const CXXMethodDecl *Overrider =
6800 Found->getCorrespondingMethodDeclaredInClass(RD: Class, MayBeBase: false);
6801 if (Overrider) {
6802 Callee = Overrider;
6803 break;
6804 }
6805 }
6806
6807 // C++2a [class.abstract]p6:
6808 // the effect of making a virtual call to a pure virtual function [...] is
6809 // undefined
6810 if (Callee->isPureVirtual()) {
6811 Info.FFDiag(E, DiagId: diag::note_constexpr_pure_virtual_call, ExtraNotes: 1) << Callee;
6812 Info.Note(Loc: Callee->getLocation(), DiagId: diag::note_declared_at);
6813 return nullptr;
6814 }
6815
6816 // If necessary, walk the rest of the path to determine the sequence of
6817 // covariant adjustment steps to apply.
6818 if (!Info.Ctx.hasSameUnqualifiedType(T1: Callee->getReturnType(),
6819 T2: Found->getReturnType())) {
6820 CovariantAdjustmentPath.push_back(Elt: Callee->getReturnType());
6821 for (unsigned CovariantPathLength = PathLength + 1;
6822 CovariantPathLength != This.Designator.Entries.size();
6823 ++CovariantPathLength) {
6824 const CXXRecordDecl *NextClass =
6825 getBaseClassType(Designator&: This.Designator, PathLength: CovariantPathLength);
6826 const CXXMethodDecl *Next =
6827 Found->getCorrespondingMethodDeclaredInClass(RD: NextClass, MayBeBase: false);
6828 if (Next && !Info.Ctx.hasSameUnqualifiedType(
6829 T1: Next->getReturnType(), T2: CovariantAdjustmentPath.back()))
6830 CovariantAdjustmentPath.push_back(Elt: Next->getReturnType());
6831 }
6832 if (!Info.Ctx.hasSameUnqualifiedType(T1: Found->getReturnType(),
6833 T2: CovariantAdjustmentPath.back()))
6834 CovariantAdjustmentPath.push_back(Elt: Found->getReturnType());
6835 }
6836
6837 // Perform 'this' adjustment.
6838 if (!CastToDerivedClass(Info, E, Result&: This, TruncatedType: Callee->getParent(), TruncatedElements: PathLength))
6839 return nullptr;
6840
6841 return Callee;
6842}
6843
6844/// Perform the adjustment from a value returned by a virtual function to
6845/// a value of the statically expected type, which may be a pointer or
6846/// reference to a base class of the returned type.
6847static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E,
6848 APValue &Result,
6849 ArrayRef<QualType> Path) {
6850 assert(Result.isLValue() &&
6851 "unexpected kind of APValue for covariant return");
6852 if (Result.isNullPointer())
6853 return true;
6854
6855 LValue LVal;
6856 LVal.setFrom(Ctx: Info.Ctx, V: Result);
6857
6858 const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl();
6859 for (unsigned I = 1; I != Path.size(); ++I) {
6860 const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl();
6861 assert(OldClass && NewClass && "unexpected kind of covariant return");
6862 if (OldClass != NewClass &&
6863 !CastToBaseClass(Info, E, Result&: LVal, DerivedRD: OldClass, BaseRD: NewClass))
6864 return false;
6865 OldClass = NewClass;
6866 }
6867
6868 LVal.moveInto(V&: Result);
6869 return true;
6870}
6871
6872/// Determine whether \p Base, which is known to be a direct base class of
6873/// \p Derived, is a public base class.
6874static bool isBaseClassPublic(const CXXRecordDecl *Derived,
6875 const CXXRecordDecl *Base) {
6876 for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) {
6877 if (BaseSpec.isVirtual())
6878 continue;
6879 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6880 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6881 return BaseSpec.getAccessSpecifier() == AS_public;
6882 }
6883 for (const CXXBaseSpecifier &BaseSpec : Derived->vbases()) {
6884 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6885 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6886 return BaseSpec.getAccessSpecifier() == AS_public;
6887 }
6888
6889 llvm_unreachable("Base is not a direct base of Derived");
6890}
6891
6892/// Apply the given dynamic cast operation on the provided lvalue.
6893///
6894/// This implements the hard case of dynamic_cast, requiring a "runtime check"
6895/// to find a suitable target subobject.
6896static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E,
6897 LValue &Ptr) {
6898 // We can't do anything with a non-symbolic pointer value.
6899 SubobjectDesignator &D = Ptr.Designator;
6900 if (D.Invalid)
6901 return false;
6902
6903 // C++ [expr.dynamic.cast]p6:
6904 // If v is a null pointer value, the result is a null pointer value.
6905 if (Ptr.isNullPointer() && !E->isGLValue())
6906 return true;
6907
6908 // For all the other cases, we need the pointer to point to an object within
6909 // its lifetime / period of construction / destruction, and we need to know
6910 // its dynamic type.
6911 std::optional<DynamicType> DynType =
6912 ComputeDynamicType(Info, E, This&: Ptr, AK: AK_DynamicCast);
6913 if (!DynType)
6914 return false;
6915
6916 // C++ [expr.dynamic.cast]p7:
6917 // If T is "pointer to cv void", then the result is a pointer to the most
6918 // derived object
6919 if (E->getType()->isVoidPointerType())
6920 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength);
6921
6922 const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl();
6923 assert(C && "dynamic_cast target is not void pointer nor class");
6924 CanQualType CQT = Info.Ctx.getCanonicalTagType(TD: C);
6925
6926 auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) {
6927 // C++ [expr.dynamic.cast]p9:
6928 if (!E->isGLValue()) {
6929 // The value of a failed cast to pointer type is the null pointer value
6930 // of the required result type.
6931 Ptr.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
6932 return true;
6933 }
6934
6935 // A failed cast to reference type throws [...] std::bad_cast.
6936 unsigned DiagKind;
6937 if (!Paths && (declaresSameEntity(D1: DynType->Type, D2: C) ||
6938 DynType->Type->isDerivedFrom(Base: C)))
6939 DiagKind = 0;
6940 else if (!Paths || Paths->begin() == Paths->end())
6941 DiagKind = 1;
6942 else if (Paths->isAmbiguous(BaseType: CQT))
6943 DiagKind = 2;
6944 else {
6945 assert(Paths->front().Access != AS_public && "why did the cast fail?");
6946 DiagKind = 3;
6947 }
6948 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_cast_to_reference_failed)
6949 << DiagKind << Ptr.Designator.getType(Ctx&: Info.Ctx)
6950 << Info.Ctx.getCanonicalTagType(TD: DynType->Type)
6951 << E->getType().getUnqualifiedType();
6952 return false;
6953 };
6954
6955 // Runtime check, phase 1:
6956 // Walk from the base subobject towards the derived object looking for the
6957 // target type.
6958 for (int PathLength = Ptr.Designator.Entries.size();
6959 PathLength >= (int)DynType->PathLength; --PathLength) {
6960 const CXXRecordDecl *Class = getBaseClassType(Designator&: Ptr.Designator, PathLength);
6961 if (declaresSameEntity(D1: Class, D2: C))
6962 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: Class, TruncatedElements: PathLength);
6963 // We can only walk across public inheritance edges.
6964 if (PathLength > (int)DynType->PathLength &&
6965 !isBaseClassPublic(Derived: getBaseClassType(Designator&: Ptr.Designator, PathLength: PathLength - 1),
6966 Base: Class))
6967 return RuntimeCheckFailed(nullptr);
6968 }
6969
6970 // Runtime check, phase 2:
6971 // Search the dynamic type for an unambiguous public base of type C.
6972 CXXBasePaths Paths(/*FindAmbiguities=*/true,
6973 /*RecordPaths=*/true, /*DetectVirtual=*/false);
6974 if (DynType->Type->isDerivedFrom(Base: C, Paths) && !Paths.isAmbiguous(BaseType: CQT) &&
6975 Paths.front().Access == AS_public) {
6976 // Downcast to the dynamic type...
6977 if (!CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength))
6978 return false;
6979 // ... then upcast to the chosen base class subobject.
6980 for (CXXBasePathElement &Elem : Paths.front())
6981 if (!HandleLValueBase(Info, E, Obj&: Ptr, DerivedDecl: Elem.Class, Base: Elem.Base))
6982 return false;
6983 return true;
6984 }
6985
6986 // Otherwise, the runtime check fails.
6987 return RuntimeCheckFailed(&Paths);
6988}
6989
6990namespace {
6991struct StartLifetimeOfUnionMemberHandler {
6992 EvalInfo &Info;
6993 const Expr *LHSExpr;
6994 const FieldDecl *Field;
6995 bool DuringInit;
6996 bool Failed = false;
6997 static const AccessKinds AccessKind = AK_Assign;
6998
6999 typedef bool result_type;
7000 bool failed() { return Failed; }
7001 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
7002 // We are supposed to perform no initialization but begin the lifetime of
7003 // the object. We interpret that as meaning to do what default
7004 // initialization of the object would do if all constructors involved were
7005 // trivial:
7006 // * All base, non-variant member, and array element subobjects' lifetimes
7007 // begin
7008 // * No variant members' lifetimes begin
7009 // * All scalar subobjects whose lifetimes begin have indeterminate values
7010 assert(SubobjType->isUnionType());
7011 if (declaresSameEntity(D1: Subobj.getUnionField(), D2: Field)) {
7012 // This union member is already active. If it's also in-lifetime, there's
7013 // nothing to do.
7014 if (Subobj.getUnionValue().hasValue())
7015 return true;
7016 } else if (DuringInit) {
7017 // We're currently in the process of initializing a different union
7018 // member. If we carried on, that initialization would attempt to
7019 // store to an inactive union member, resulting in undefined behavior.
7020 Info.FFDiag(E: LHSExpr,
7021 DiagId: diag::note_constexpr_union_member_change_during_init);
7022 return false;
7023 }
7024 APValue Result;
7025 Failed = !handleDefaultInitValue(T: Field->getType(), Result);
7026 Subobj.setUnion(Field, Value: Result);
7027 return true;
7028 }
7029 bool found(APSInt &Value, QualType SubobjType) {
7030 llvm_unreachable("wrong value kind for union object");
7031 }
7032 bool found(APFloat &Value, QualType SubobjType) {
7033 llvm_unreachable("wrong value kind for union object");
7034 }
7035};
7036} // end anonymous namespace
7037
7038const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind;
7039
7040/// Handle a builtin simple-assignment or a call to a trivial assignment
7041/// operator whose left-hand side might involve a union member access. If it
7042/// does, implicitly start the lifetime of any accessed union elements per
7043/// C++20 [class.union]5.
7044static bool MaybeHandleUnionActiveMemberChange(EvalInfo &Info,
7045 const Expr *LHSExpr,
7046 const LValue &LHS) {
7047 if (LHS.InvalidBase || LHS.Designator.Invalid)
7048 return false;
7049
7050 llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths;
7051 // C++ [class.union]p5:
7052 // define the set S(E) of subexpressions of E as follows:
7053 unsigned PathLength = LHS.Designator.Entries.size();
7054 for (const Expr *E = LHSExpr; E != nullptr;) {
7055 // -- If E is of the form A.B, S(E) contains the elements of S(A)...
7056 if (auto *ME = dyn_cast<MemberExpr>(Val: E)) {
7057 auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl());
7058 // Note that we can't implicitly start the lifetime of a reference,
7059 // so we don't need to proceed any further if we reach one.
7060 if (!FD || FD->getType()->isReferenceType())
7061 break;
7062
7063 // ... and also contains A.B if B names a union member ...
7064 if (FD->getParent()->isUnion()) {
7065 // ... of a non-class, non-array type, or of a class type with a
7066 // trivial default constructor that is not deleted, or an array of
7067 // such types.
7068 auto *RD =
7069 FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
7070 if (!RD || RD->hasTrivialDefaultConstructor())
7071 UnionPathLengths.push_back(Elt: {PathLength - 1, FD});
7072 }
7073
7074 E = ME->getBase();
7075 --PathLength;
7076 assert(declaresSameEntity(FD,
7077 LHS.Designator.Entries[PathLength]
7078 .getAsBaseOrMember().getPointer()));
7079
7080 // -- If E is of the form A[B] and is interpreted as a built-in array
7081 // subscripting operator, S(E) is [S(the array operand, if any)].
7082 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) {
7083 // Step over an ArrayToPointerDecay implicit cast.
7084 auto *Base = ASE->getBase()->IgnoreImplicit();
7085 if (!Base->getType()->isArrayType())
7086 break;
7087
7088 E = Base;
7089 --PathLength;
7090
7091 } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E)) {
7092 // Step over a derived-to-base conversion.
7093 E = ICE->getSubExpr();
7094 if (ICE->getCastKind() == CK_NoOp)
7095 continue;
7096 if (ICE->getCastKind() != CK_DerivedToBase &&
7097 ICE->getCastKind() != CK_UncheckedDerivedToBase)
7098 break;
7099 // Walk path backwards as we walk up from the base to the derived class.
7100 for (const CXXBaseSpecifier *Elt : llvm::reverse(C: ICE->path())) {
7101 if (Elt->isVirtual()) {
7102 // A class with virtual base classes never has a trivial default
7103 // constructor, so S(E) is empty in this case.
7104 E = nullptr;
7105 break;
7106 }
7107
7108 --PathLength;
7109 assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(),
7110 LHS.Designator.Entries[PathLength]
7111 .getAsBaseOrMember().getPointer()));
7112 }
7113
7114 // -- Otherwise, S(E) is empty.
7115 } else {
7116 break;
7117 }
7118 }
7119
7120 // Common case: no unions' lifetimes are started.
7121 if (UnionPathLengths.empty())
7122 return true;
7123
7124 // if modification of X [would access an inactive union member], an object
7125 // of the type of X is implicitly created
7126 CompleteObject Obj =
7127 findCompleteObject(Info, E: LHSExpr, AK: AK_Assign, LVal: LHS, LValType: LHSExpr->getType());
7128 if (!Obj)
7129 return false;
7130 for (std::pair<unsigned, const FieldDecl *> LengthAndField :
7131 llvm::reverse(C&: UnionPathLengths)) {
7132 // Form a designator for the union object.
7133 SubobjectDesignator D = LHS.Designator;
7134 D.truncate(Ctx&: Info.Ctx, Base: LHS.Base, NewLength: LengthAndField.first);
7135
7136 bool DuringInit = Info.isEvaluatingCtorDtor(Base: LHS.Base, Path: D.Entries) ==
7137 ConstructionPhase::AfterBases;
7138 StartLifetimeOfUnionMemberHandler StartLifetime{
7139 .Info: Info, .LHSExpr: LHSExpr, .Field: LengthAndField.second, .DuringInit: DuringInit};
7140 if (!findSubobject(Info, E: LHSExpr, Obj, Sub: D, handler&: StartLifetime))
7141 return false;
7142 }
7143
7144 return true;
7145}
7146
7147static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg,
7148 CallRef Call, EvalInfo &Info, bool NonNull = false,
7149 APValue **EvaluatedArg = nullptr) {
7150 LValue LV;
7151 // Create the parameter slot and register its destruction. For a vararg
7152 // argument, create a temporary.
7153 // FIXME: For calling conventions that destroy parameters in the callee,
7154 // should we consider performing destruction when the function returns
7155 // instead?
7156 APValue &V = PVD ? Info.CurrentCall->createParam(Args: Call, PVD, LV)
7157 : Info.CurrentCall->createTemporary(Key: Arg, T: Arg->getType(),
7158 Scope: ScopeKind::Call, LV);
7159 if (!EvaluateInPlace(Result&: V, Info, This: LV, E: Arg))
7160 return false;
7161
7162 // Passing a null pointer to an __attribute__((nonnull)) parameter results in
7163 // undefined behavior, so is non-constant.
7164 if (NonNull && V.isLValue() && V.isNullPointer()) {
7165 Info.CCEDiag(E: Arg, DiagId: diag::note_non_null_attribute_failed);
7166 return false;
7167 }
7168
7169 if (EvaluatedArg)
7170 *EvaluatedArg = &V;
7171
7172 return true;
7173}
7174
7175/// Evaluate the arguments to a function call.
7176static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call,
7177 EvalInfo &Info, const FunctionDecl *Callee,
7178 bool RightToLeft = false,
7179 LValue *ObjectArg = nullptr) {
7180 bool Success = true;
7181 llvm::SmallBitVector ForbiddenNullArgs;
7182 if (Callee->hasAttr<NonNullAttr>()) {
7183 ForbiddenNullArgs.resize(N: Args.size());
7184 for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) {
7185 if (!Attr->args_size()) {
7186 ForbiddenNullArgs.set();
7187 break;
7188 } else
7189 for (auto Idx : Attr->args()) {
7190 unsigned ASTIdx = Idx.getASTIndex();
7191 if (ASTIdx >= Args.size())
7192 continue;
7193 ForbiddenNullArgs[ASTIdx] = true;
7194 }
7195 }
7196 }
7197 for (unsigned I = 0; I < Args.size(); I++) {
7198 unsigned Idx = RightToLeft ? Args.size() - I - 1 : I;
7199 const ParmVarDecl *PVD =
7200 Idx < Callee->getNumParams() ? Callee->getParamDecl(i: Idx) : nullptr;
7201 bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx];
7202 APValue *That = nullptr;
7203 if (!EvaluateCallArg(PVD, Arg: Args[Idx], Call, Info, NonNull, EvaluatedArg: &That)) {
7204 // If we're checking for a potential constant expression, evaluate all
7205 // initializers even if some of them fail.
7206 if (!Info.noteFailure())
7207 return false;
7208 Success = false;
7209 }
7210 if (PVD && PVD->isExplicitObjectParameter() && That && That->isLValue())
7211 ObjectArg->setFrom(Ctx: Info.Ctx, V: *That);
7212 }
7213 return Success;
7214}
7215
7216/// Perform a trivial copy from Param, which is the parameter of a copy or move
7217/// constructor or assignment operator.
7218static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param,
7219 const Expr *E, APValue &Result,
7220 bool CopyObjectRepresentation) {
7221 // Find the reference argument.
7222 CallStackFrame *Frame = Info.CurrentCall;
7223 APValue *RefValue = Info.getParamSlot(Call: Frame->Arguments, PVD: Param);
7224 if (!RefValue) {
7225 Info.FFDiag(E);
7226 return false;
7227 }
7228
7229 // Copy out the contents of the RHS object.
7230 LValue RefLValue;
7231 RefLValue.setFrom(Ctx: Info.Ctx, V: *RefValue);
7232 return handleLValueToRValueConversion(
7233 Info, Conv: E, Type: Param->getType().getNonReferenceType(), LVal: RefLValue, RVal&: Result,
7234 WantObjectRepresentation: CopyObjectRepresentation);
7235}
7236
7237/// Evaluate a function call.
7238static bool HandleFunctionCall(SourceLocation CallLoc,
7239 const FunctionDecl *Callee,
7240 const LValue *ObjectArg, const Expr *E,
7241 ArrayRef<const Expr *> Args, CallRef Call,
7242 const Stmt *Body, EvalInfo &Info,
7243 APValue &Result, const LValue *ResultSlot) {
7244 if (!Info.CheckCallLimit(Loc: CallLoc))
7245 return false;
7246
7247 CallStackFrame Frame(Info, E->getSourceRange(), Callee, ObjectArg, E, Call);
7248
7249 // For a trivial copy or move assignment, perform an APValue copy. This is
7250 // essential for unions, where the operations performed by the assignment
7251 // operator cannot be represented as statements.
7252 //
7253 // Skip this for non-union classes with no fields; in that case, the defaulted
7254 // copy/move does not actually read the object.
7255 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: Callee);
7256
7257 auto IsTrivialMemoryOperation = [&](const CXXMethodDecl *MD) {
7258 if (!MD || !MD->isDefaulted())
7259 return false;
7260 if (!MD->isCopyAssignmentOperator() && !MD->isMoveAssignmentOperator())
7261 return false;
7262 return MD->getParent()->isUnion() ||
7263 (MD->isTrivial() &&
7264 isReadByLvalueToRvalueConversion(RD: MD->getParent()));
7265 };
7266
7267 if (IsTrivialMemoryOperation(MD)) {
7268 unsigned ExplicitOffset = MD->isExplicitObjectMemberFunction() ? 1 : 0;
7269 assert(ObjectArg);
7270 APValue RHSValue;
7271 if (!handleTrivialCopy(Info, Param: MD->getParamDecl(i: 0), E: Args[0], Result&: RHSValue,
7272 CopyObjectRepresentation: MD->getParent()->isUnion()))
7273 return false;
7274
7275 LValue Obj;
7276 if (!handleAssignment(Info, E: Args[ExplicitOffset], LVal: *ObjectArg,
7277 LValType: MD->getFunctionObjectParameterReferenceType(),
7278 Val&: RHSValue))
7279 return false;
7280 ObjectArg->moveInto(V&: Result);
7281 return true;
7282 } else if (MD && isLambdaCallOperator(MD)) {
7283 // We're in a lambda; determine the lambda capture field maps unless we're
7284 // just constexpr checking a lambda's call operator. constexpr checking is
7285 // done before the captures have been added to the closure object (unless
7286 // we're inferring constexpr-ness), so we don't have access to them in this
7287 // case. But since we don't need the captures to constexpr check, we can
7288 // just ignore them.
7289 if (!Info.checkingPotentialConstantExpression())
7290 MD->getParent()->getCaptureFields(Captures&: Frame.LambdaCaptureFields,
7291 ThisCapture&: Frame.LambdaThisCaptureField);
7292 }
7293
7294 StmtResult Ret = {.Value: Result, .Slot: ResultSlot};
7295 EvalStmtResult ESR = EvaluateStmt(Result&: Ret, Info, S: Body);
7296 if (ESR == ESR_Succeeded) {
7297 if (Callee->getReturnType()->isVoidType())
7298 return true;
7299 Info.FFDiag(Loc: Callee->getEndLoc(), DiagId: diag::note_constexpr_no_return);
7300 }
7301 return ESR == ESR_Returned;
7302}
7303
7304static bool HandleConstructorCall(const Expr *E, const LValue &This,
7305 CallRef Call,
7306 const CXXConstructorDecl *Definition,
7307 EvalInfo &Info, APValue &Result,
7308 bool IsCompleteClass = true);
7309
7310static bool HandleConstructorCall(const Expr *E, const LValue &This,
7311 ArrayRef<const Expr *> Args,
7312 const CXXConstructorDecl *Definition,
7313 EvalInfo &Info, APValue &Result,
7314 bool IsCompleteClass = true) {
7315 CallScopeRAII CallScope(Info);
7316 CallRef Call = Info.CurrentCall->createCall(Callee: Definition);
7317 if (!EvaluateArgs(Args, Call, Info, Callee: Definition))
7318 return false;
7319
7320 return HandleConstructorCall(E, This, Call, Definition, Info, Result,
7321 IsCompleteClass) &&
7322 CallScope.destroy();
7323}
7324
7325/// Evaluate a constructor call.
7326static bool HandleConstructorCall(const Expr *E, const LValue &This,
7327 CallRef Call,
7328 const CXXConstructorDecl *Definition,
7329 EvalInfo &Info, APValue &Result,
7330 bool IsCompleteClass) {
7331
7332 SourceLocation CallLoc = E->getExprLoc();
7333 if (!Info.CheckCallLimit(Loc: CallLoc))
7334 return false;
7335
7336 const CXXRecordDecl *RD = Definition->getParent();
7337 if (!Info.getLangOpts().CPlusPlus26 && RD->getNumVBases()) {
7338 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_base) << RD;
7339 return false;
7340 }
7341
7342 EvalInfo::EvaluatingConstructorRAII EvalObj(
7343 Info,
7344 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
7345 RD->getNumBases());
7346 CallStackFrame Frame(Info, E->getSourceRange(), Definition, &This, E, Call);
7347
7348 // FIXME: Creating an APValue just to hold a nonexistent return value is
7349 // wasteful.
7350 APValue RetVal;
7351 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7352
7353 // If it's a delegating constructor, delegate.
7354 if (Definition->isDelegatingConstructor()) {
7355 CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
7356 if ((*I)->getInit()->isValueDependent()) {
7357 if (!EvaluateDependentExpr(E: (*I)->getInit(), Info))
7358 return false;
7359 } else {
7360 FullExpressionRAII InitScope(Info);
7361 if (!EvaluateInPlace(Result, Info, This, E: (*I)->getInit()) ||
7362 !InitScope.destroy())
7363 return false;
7364 }
7365 return EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed;
7366 }
7367
7368 // For a trivial copy or move constructor, perform an APValue copy. This is
7369 // essential for unions (or classes with anonymous union members), where the
7370 // operations performed by the constructor cannot be represented by
7371 // ctor-initializers.
7372 //
7373 // Skip this for empty non-union classes; we should not perform an
7374 // lvalue-to-rvalue conversion on them because their copy constructor does not
7375 // actually read them.
7376 if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() &&
7377 (Definition->getParent()->isUnion() ||
7378 (Definition->isTrivial() &&
7379 isReadByLvalueToRvalueConversion(RD: Definition->getParent())))) {
7380 return handleTrivialCopy(Info, Param: Definition->getParamDecl(i: 0), E, Result,
7381 CopyObjectRepresentation: Definition->getParent()->isUnion());
7382 }
7383
7384 // Reserve space for the struct members.
7385 if (!Result.hasValue()) {
7386 if (!RD->isUnion()) {
7387 unsigned NonVirtualBases = countNonVirtualBases(RD);
7388 Result = APValue(APValue::UninitStruct(), NonVirtualBases,
7389 RD->getNumFields(), RD->getNumVBases());
7390 } else
7391 // A union starts with no active member.
7392 Result = APValue((const FieldDecl*)nullptr);
7393 }
7394
7395 if (RD->isInvalidDecl()) return false;
7396 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7397
7398 // A scope for temporaries lifetime-extended by reference members.
7399 BlockScopeRAII LifetimeExtendedScope(Info);
7400
7401 bool Success = true;
7402 unsigned BasesSeen = 0;
7403 unsigned VirtualBasesSeen = 0;
7404 unsigned NonVirtualBases = countNonVirtualBases(RD);
7405
7406 CXXRecordDecl::field_iterator FieldIt = RD->field_begin();
7407 auto SkipToField = [&](FieldDecl *FD, bool Indirect) {
7408 // We might be initializing the same field again if this is an indirect
7409 // field initialization.
7410 if (FieldIt == RD->field_end() ||
7411 FieldIt->getFieldIndex() > FD->getFieldIndex()) {
7412 assert(Indirect && "fields out of order?");
7413 return;
7414 }
7415
7416 // Default-initialize any fields with no explicit initializer.
7417 for (; !declaresSameEntity(D1: *FieldIt, D2: FD); ++FieldIt) {
7418 assert(FieldIt != RD->field_end() && "missing field?");
7419 if (!FieldIt->isUnnamedBitField())
7420 Success &= handleDefaultInitValue(
7421 T: FieldIt->getType(),
7422 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7423 }
7424 ++FieldIt;
7425 };
7426 for (const auto *I : Definition->inits()) {
7427 LValue Subobject = This;
7428 LValue SubobjectParent = This;
7429 APValue *Value = &Result;
7430
7431 // Determine the subobject to initialize.
7432 FieldDecl *FD = nullptr;
7433 if (I->isBaseInitializer()) {
7434 QualType BaseType(I->getBaseClass(), 0);
7435 if (I->isBaseVirtual()) {
7436 if (This.pointsToCompleteClass(D: RD)) {
7437 if (!HandleLValueDirectVirtualBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7438 Base: BaseType->getAsCXXRecordDecl(),
7439 RL: &Layout))
7440 return false;
7441 Value = &Result.getStructVirtualBase(i: VirtualBasesSeen++);
7442 } else {
7443 continue;
7444 }
7445
7446 } else {
7447 if (!HandleLValueDirectBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7448 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7449 return false;
7450 Value = &Result.getStructBase(i: BasesSeen++);
7451 }
7452 } else if ((FD = I->getMember())) {
7453 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD, RL: &Layout))
7454 return false;
7455 if (RD->isUnion()) {
7456 Result = APValue(FD);
7457 Value = &Result.getUnionValue();
7458 } else {
7459 SkipToField(FD, false);
7460 Value = &Result.getStructField(i: FD->getFieldIndex());
7461 }
7462 } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) {
7463 // Walk the indirect field decl's chain to find the object to initialize,
7464 // and make sure we've initialized every step along it.
7465 auto IndirectFieldChain = IFD->chain();
7466 for (auto *C : IndirectFieldChain) {
7467 FD = cast<FieldDecl>(Val: C);
7468 CXXRecordDecl *CD = cast<CXXRecordDecl>(Val: FD->getParent());
7469 // Switch the union field if it differs. This happens if we had
7470 // preceding zero-initialization, and we're now initializing a union
7471 // subobject other than the first.
7472 // FIXME: In this case, the values of the other subobjects are
7473 // specified, since zero-initialization sets all padding bits to zero.
7474 if (!Value->hasValue() ||
7475 (Value->isUnion() &&
7476 !declaresSameEntity(D1: Value->getUnionField(), D2: FD))) {
7477 if (CD->isUnion())
7478 *Value = APValue(FD);
7479 else
7480 // FIXME: This immediately starts the lifetime of all members of
7481 // an anonymous struct. It would be preferable to strictly start
7482 // member lifetime in initialization order.
7483 Success &= handleDefaultInitValue(T: Info.Ctx.getCanonicalTagType(TD: CD),
7484 Result&: *Value);
7485 }
7486 // Store Subobject as its parent before updating it for the last element
7487 // in the chain.
7488 if (C == IndirectFieldChain.back())
7489 SubobjectParent = Subobject;
7490 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD))
7491 return false;
7492 if (CD->isUnion())
7493 Value = &Value->getUnionValue();
7494 else {
7495 if (C == IndirectFieldChain.front() && !RD->isUnion())
7496 SkipToField(FD, true);
7497 Value = &Value->getStructField(i: FD->getFieldIndex());
7498 }
7499 }
7500 } else {
7501 llvm_unreachable("unknown base initializer kind");
7502 }
7503
7504 // Need to override This for implicit field initializers as in this case
7505 // This refers to innermost anonymous struct/union containing initializer,
7506 // not to currently constructed class.
7507 const Expr *Init = I->getInit();
7508 if (Init->isValueDependent()) {
7509 if (!EvaluateDependentExpr(E: Init, Info))
7510 return false;
7511 } else {
7512 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent,
7513 isa<CXXDefaultInitExpr>(Val: Init));
7514 FullExpressionRAII InitScope(Info);
7515 if (FD && FD->getType()->isReferenceType() &&
7516 !FD->getType()->isFunctionReferenceType()) {
7517 LValue Result;
7518 if (!EvaluateInitForDeclOfReferenceType(Info, D: FD, Init, Result,
7519 Val&: *Value)) {
7520 if (!Info.noteFailure())
7521 return false;
7522 Success = false;
7523 }
7524 } else if (!EvaluateInPlace(Result&: *Value, Info, This: Subobject, E: Init) ||
7525 (FD && FD->isBitField() &&
7526 !truncateBitfieldValue(Info, E: Init, Value&: *Value, FD))) {
7527 // If we're checking for a potential constant expression, evaluate all
7528 // initializers even if some of them fail.
7529 if (!Info.noteFailure())
7530 return false;
7531 Success = false;
7532 }
7533 }
7534
7535 // This is the point at which the dynamic type of the object becomes this
7536 // class type.
7537 if (I->isBaseInitializer() && BasesSeen == NonVirtualBases)
7538 EvalObj.finishedConstructingBases();
7539 }
7540
7541 // Default-initialize any remaining fields.
7542 if (!RD->isUnion()) {
7543 for (; FieldIt != RD->field_end(); ++FieldIt) {
7544 if (!FieldIt->isUnnamedBitField())
7545 Success &= handleDefaultInitValue(
7546 T: FieldIt->getType(),
7547 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7548 }
7549 }
7550
7551 EvalObj.finishedConstructingFields();
7552
7553 return Success &&
7554 EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed &&
7555 LifetimeExtendedScope.destroy();
7556}
7557
7558static bool HandleDestructionImpl(EvalInfo &Info, SourceRange CallRange,
7559 const LValue &This, APValue &Value,
7560 QualType T, bool IsCompleteClass = true) {
7561 // Objects can only be destroyed while they're within their lifetimes.
7562 // FIXME: We have no representation for whether an object of type nullptr_t
7563 // is in its lifetime; it usually doesn't matter. Perhaps we should model it
7564 // as indeterminate instead?
7565 if (Value.isAbsent() && !T->isNullPtrType()) {
7566 APValue Printable;
7567 This.moveInto(V&: Printable);
7568 Info.FFDiag(Loc: CallRange.getBegin(),
7569 DiagId: diag::note_constexpr_destroy_out_of_lifetime)
7570 << Printable.getAsString(Ctx: Info.Ctx, Ty: Info.Ctx.getLValueReferenceType(T));
7571 return false;
7572 }
7573
7574 // Invent an expression for location purposes.
7575 // FIXME: We shouldn't need to do this.
7576 OpaqueValueExpr LocE(CallRange.getBegin(), Info.Ctx.IntTy, VK_PRValue);
7577
7578 // For arrays, destroy elements right-to-left.
7579 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) {
7580 uint64_t Size = CAT->getZExtSize();
7581 QualType ElemT = CAT->getElementType();
7582
7583 if (!CheckArraySize(Info, CAT, CallLoc: CallRange.getBegin()))
7584 return false;
7585
7586 LValue ElemLV = This;
7587 ElemLV.addArray(Info, E: &LocE, CAT);
7588 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: Size))
7589 return false;
7590
7591 // Ensure that we have actual array elements available to destroy; the
7592 // destructors might mutate the value, so we can't run them on the array
7593 // filler.
7594 if (Size && Size > Value.getArrayInitializedElts())
7595 expandArray(Array&: Value, Index: Value.getArraySize() - 1);
7596
7597 // The size of the array might have been reduced by
7598 // a placement new.
7599 for (Size = Value.getArraySize(); Size != 0; --Size) {
7600 APValue &Elem = Value.getArrayInitializedElt(I: Size - 1);
7601 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: -1) ||
7602 !HandleDestructionImpl(Info, CallRange, This: ElemLV, Value&: Elem, T: ElemT))
7603 return false;
7604 }
7605
7606 // End the lifetime of this array now.
7607 Value = APValue();
7608 return true;
7609 }
7610
7611 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
7612 if (!RD) {
7613 if (T.isDestructedType()) {
7614 Info.FFDiag(Loc: CallRange.getBegin(),
7615 DiagId: diag::note_constexpr_unsupported_destruction)
7616 << T;
7617 return false;
7618 }
7619
7620 Value = APValue();
7621 return true;
7622 }
7623
7624 if (!Info.getLangOpts().CPlusPlus26 && RD->getNumVBases()) {
7625 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_virtual_base) << RD;
7626 return false;
7627 }
7628
7629 // If an anonymous union would be destroyed, some enclosing destructor must
7630 // have been explicitly defined, and the anonymous union destruction should
7631 // have no effect.
7632 if (RD->isAnonymousStructOrUnion() && RD->isUnion()) {
7633 Value = APValue();
7634 return true;
7635 }
7636
7637 const CXXDestructorDecl *DD = RD->getDestructor();
7638 if (!DD && !RD->hasTrivialDestructor()) {
7639 Info.FFDiag(Loc: CallRange.getBegin());
7640 return false;
7641 }
7642
7643 if (!DD || DD->isTrivial()) {
7644 // A trivial destructor just ends the lifetime of the object. Check for
7645 // this case before checking for a body, because we might not bother
7646 // building a body for a trivial destructor. Note that it doesn't matter
7647 // whether the destructor is constexpr in this case; all trivial
7648 // destructors are constexpr.
7649 Value = APValue();
7650 return true;
7651 }
7652
7653 if (!Info.CheckCallLimit(Loc: CallRange.getBegin()))
7654 return false;
7655
7656 const FunctionDecl *Definition = nullptr;
7657 const Stmt *Body = DD->getBody(Definition);
7658
7659 if (!CheckConstexprFunction(Info, CallLoc: CallRange.getBegin(), Declaration: DD, Definition, Body))
7660 return false;
7661
7662 CallStackFrame Frame(Info, CallRange, Definition, &This, /*CallExpr=*/nullptr,
7663 CallRef());
7664
7665 // We're now in the period of destruction of this object.
7666 EvalInfo::EvaluatingDestructorRAII EvalObj(
7667 Info,
7668 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries});
7669 unsigned NonVirtualBases = countNonVirtualBases(RD);
7670 unsigned NumVirtualBases = RD->getNumVBases();
7671 unsigned BasesLeft = NonVirtualBases;
7672 if (!EvalObj.DidInsert) {
7673 // C++2a [class.dtor]p19:
7674 // the behavior is undefined if the destructor is invoked for an object
7675 // whose lifetime has ended
7676 // (Note that formally the lifetime ends when the period of destruction
7677 // begins, even though certain uses of the object remain valid until the
7678 // period of destruction ends.)
7679 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_double_destroy);
7680 return false;
7681 }
7682
7683 // FIXME: Creating an APValue just to hold a nonexistent return value is
7684 // wasteful.
7685 APValue RetVal;
7686 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7687 if (EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) == ESR_Failed)
7688 return false;
7689
7690 // A union destructor does not implicitly destroy its members.
7691 if (RD->isUnion())
7692 return true;
7693
7694 if (!ASTContext::hasLayout(D: RD))
7695 return false;
7696 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7697
7698 // We don't have a good way to iterate fields in reverse, so collect all the
7699 // fields first and then walk them backwards.
7700 SmallVector<FieldDecl*, 16> Fields(RD->fields());
7701 for (const FieldDecl *FD : llvm::reverse(C&: Fields)) {
7702 if (FD->isUnnamedBitField())
7703 continue;
7704
7705 LValue Subobject = This;
7706 if (!HandleLValueMember(Info, E: &LocE, LVal&: Subobject, FD, RL: &Layout))
7707 return false;
7708
7709 APValue *SubobjectValue = &Value.getStructField(i: FD->getFieldIndex());
7710 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7711 T: FD->getType()))
7712 return false;
7713 }
7714
7715 if (BasesLeft != 0 || NumVirtualBases != 0)
7716 EvalObj.startedDestroyingBases();
7717
7718 // Destroy base classes in reverse order.
7719 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->bases())) {
7720 if (Base.isVirtual())
7721 continue;
7722 --BasesLeft;
7723
7724 QualType BaseType = Base.getType();
7725 LValue Subobject = This;
7726 if (!HandleLValueDirectBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7727 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7728 return false;
7729
7730 APValue *SubobjectValue = &Value.getStructBase(i: BasesLeft);
7731 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7732 T: BaseType, /*IsCompleteClass=*/false))
7733 return false;
7734 }
7735 assert(BasesLeft == 0 && "NumBases was wrong?");
7736
7737 // Virtual bases.
7738 if (IsCompleteClass) {
7739 unsigned VirtualBasesLeft = NumVirtualBases;
7740 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->vbases())) {
7741 --VirtualBasesLeft;
7742
7743 QualType BaseType = Base.getType();
7744 LValue Subobject = This;
7745 if (!HandleLValueDirectVirtualBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7746 Base: BaseType->getAsCXXRecordDecl(),
7747 RL: &Layout))
7748 return false;
7749
7750 APValue *SubobjectValue = &Value.getStructVirtualBase(i: VirtualBasesLeft);
7751 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7752 T: BaseType, /*IsCompleteClass=*/false))
7753 return false;
7754 }
7755 assert(VirtualBasesLeft == 0 && "NumVirtualBases was wrong?");
7756 }
7757
7758 // The period of destruction ends now. The object is gone.
7759 Value = APValue();
7760 return true;
7761}
7762
7763namespace {
7764struct DestroyObjectHandler {
7765 EvalInfo &Info;
7766 const Expr *E;
7767 const LValue &This;
7768 const AccessKinds AccessKind;
7769
7770 typedef bool result_type;
7771 bool failed() { return false; }
7772 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
7773 return HandleDestructionImpl(Info, CallRange: E->getSourceRange(), This, Value&: Subobj,
7774 T: SubobjType);
7775 }
7776 bool found(APSInt &Value, QualType SubobjType) {
7777 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7778 return false;
7779 }
7780 bool found(APFloat &Value, QualType SubobjType) {
7781 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7782 return false;
7783 }
7784};
7785}
7786
7787/// Perform a destructor or pseudo-destructor call on the given object, which
7788/// might in general not be a complete object.
7789static bool HandleDestruction(EvalInfo &Info, const Expr *E,
7790 const LValue &This, QualType ThisType) {
7791 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Destroy, LVal: This, LValType: ThisType);
7792 DestroyObjectHandler Handler = {.Info: Info, .E: E, .This: This, .AccessKind: AK_Destroy};
7793 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
7794}
7795
7796/// Destroy and end the lifetime of the given complete object.
7797static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
7798 APValue::LValueBase LVBase, APValue &Value,
7799 QualType T) {
7800 // If we've had an unmodeled side-effect, we can't rely on mutable state
7801 // (such as the object we're about to destroy) being correct.
7802 if (Info.EvalStatus.HasSideEffects)
7803 return false;
7804
7805 LValue LV;
7806 LV.set(B: {LVBase});
7807 return HandleDestructionImpl(Info, CallRange: Loc, This: LV, Value, T);
7808}
7809
7810/// Perform a call to 'operator new' or to `__builtin_operator_new'.
7811static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E,
7812 LValue &Result) {
7813 if (Info.checkingPotentialConstantExpression() ||
7814 Info.SpeculativeEvaluationDepth)
7815 return false;
7816
7817 // This is permitted only within a call to std::allocator<T>::allocate.
7818 auto Caller = Info.getStdAllocatorCaller(FnName: "allocate");
7819 if (!Caller) {
7820 Info.FFDiag(Loc: E->getExprLoc(), DiagId: Info.getLangOpts().CPlusPlus20
7821 ? diag::note_constexpr_new_untyped
7822 : diag::note_constexpr_new);
7823 return false;
7824 }
7825
7826 QualType ElemType = Caller.ElemType;
7827 if (ElemType->isIncompleteType() || ElemType->isFunctionType()) {
7828 Info.FFDiag(Loc: E->getExprLoc(),
7829 DiagId: diag::note_constexpr_new_not_complete_object_type)
7830 << (ElemType->isIncompleteType() ? 0 : 1) << ElemType;
7831 return false;
7832 }
7833
7834 APSInt ByteSize;
7835 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: ByteSize, Info))
7836 return false;
7837 bool IsNothrow = false;
7838 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) {
7839 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7840 IsNothrow |= E->getType()->isNothrowT();
7841 }
7842
7843 CharUnits ElemSize;
7844 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElemType, Size&: ElemSize))
7845 return false;
7846 APInt Size, Remainder;
7847 APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity());
7848 APInt::udivrem(LHS: ByteSize, RHS: ElemSizeAP, Quotient&: Size, Remainder);
7849 if (Remainder != 0) {
7850 // This likely indicates a bug in the implementation of 'std::allocator'.
7851 Info.FFDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_operator_new_bad_size)
7852 << ByteSize << APSInt(ElemSizeAP, true) << ElemType;
7853 return false;
7854 }
7855
7856 if (!Info.CheckArraySize(Loc: E->getBeginLoc(), BitWidth: ByteSize.getActiveBits(),
7857 ElemCount: Size.getZExtValue(), /*Diag=*/!IsNothrow)) {
7858 if (IsNothrow) {
7859 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
7860 return true;
7861 }
7862 return false;
7863 }
7864
7865 QualType AllocType = Info.Ctx.getConstantArrayType(
7866 EltTy: ElemType, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
7867 APValue *Val = Info.createHeapAlloc(E: Caller.Call, T: AllocType, LV&: Result);
7868 *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue());
7869 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: AllocType));
7870 return true;
7871}
7872
7873static bool hasVirtualDestructor(QualType T) {
7874 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7875 if (CXXDestructorDecl *DD = RD->getDestructor())
7876 return DD->isVirtual();
7877 return false;
7878}
7879
7880static const FunctionDecl *getVirtualOperatorDelete(QualType T) {
7881 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7882 if (CXXDestructorDecl *DD = RD->getDestructor())
7883 return DD->isVirtual() ? DD->getOperatorDelete() : nullptr;
7884 return nullptr;
7885}
7886
7887/// Check that the given object is a suitable pointer to a heap allocation that
7888/// still exists and is of the right kind for the purpose of a deletion.
7889///
7890/// On success, returns the heap allocation to deallocate. On failure, produces
7891/// a diagnostic and returns std::nullopt.
7892static std::optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E,
7893 const LValue &Pointer,
7894 DynAllocKind DeallocKind) {
7895 auto PointerAsString = [&] {
7896 return Pointer.toString(Ctx&: Info.Ctx, T: Info.Ctx.VoidPtrTy);
7897 };
7898
7899 DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>();
7900 if (!DA) {
7901 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_not_heap_alloc)
7902 << PointerAsString();
7903 if (Pointer.Base)
7904 NoteLValueLocation(Info, Base: Pointer.Base);
7905 return std::nullopt;
7906 }
7907
7908 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
7909 if (!Alloc) {
7910 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
7911 return std::nullopt;
7912 }
7913
7914 if (DeallocKind != (*Alloc)->getKind()) {
7915 QualType AllocType = Pointer.Base.getDynamicAllocType();
7916 Info.FFDiag(E, DiagId: diag::note_constexpr_new_delete_mismatch)
7917 << DeallocKind << (*Alloc)->getKind() << AllocType;
7918 NoteLValueLocation(Info, Base: Pointer.Base);
7919 return std::nullopt;
7920 }
7921
7922 bool Subobject = false;
7923 if (DeallocKind == DynAllocKind::New) {
7924 Subobject = Pointer.Designator.MostDerivedPathLength != 0 ||
7925 Pointer.Designator.isOnePastTheEnd();
7926 } else {
7927 Subobject = Pointer.Designator.Entries.size() != 1 ||
7928 Pointer.Designator.Entries[0].getAsArrayIndex() != 0;
7929 }
7930 if (Subobject) {
7931 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_subobject)
7932 << PointerAsString() << Pointer.Designator.isOnePastTheEnd();
7933 return std::nullopt;
7934 }
7935
7936 return Alloc;
7937}
7938
7939// Perform a call to 'operator delete' or '__builtin_operator_delete'.
7940static bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) {
7941 if (Info.checkingPotentialConstantExpression() ||
7942 Info.SpeculativeEvaluationDepth)
7943 return false;
7944
7945 // This is permitted only within a call to std::allocator<T>::deallocate.
7946 if (!Info.getStdAllocatorCaller(FnName: "deallocate")) {
7947 Info.FFDiag(Loc: E->getExprLoc());
7948 return true;
7949 }
7950
7951 LValue Pointer;
7952 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Pointer, Info))
7953 return false;
7954 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I)
7955 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7956
7957 if (Pointer.Designator.Invalid)
7958 return false;
7959
7960 // Deleting a null pointer would have no effect, but it's not permitted by
7961 // std::allocator<T>::deallocate's contract.
7962 if (Pointer.isNullPointer()) {
7963 Info.CCEDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_deallocate_null);
7964 return true;
7965 }
7966
7967 if (!CheckDeleteKind(Info, E, Pointer, DeallocKind: DynAllocKind::StdAllocator))
7968 return false;
7969
7970 Info.HeapAllocs.erase(x: Pointer.Base.get<DynamicAllocLValue>());
7971 return true;
7972}
7973
7974//===----------------------------------------------------------------------===//
7975// Generic Evaluation
7976//===----------------------------------------------------------------------===//
7977namespace {
7978
7979class BitCastBuffer {
7980 // FIXME: We're going to need bit-level granularity when we support
7981 // bit-fields.
7982 // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but
7983 // we don't support a host or target where that is the case. Still, we should
7984 // use a more generic type in case we ever do.
7985 SmallVector<std::optional<unsigned char>, 32> Bytes;
7986
7987 static_assert(std::numeric_limits<unsigned char>::digits >= 8,
7988 "Need at least 8 bit unsigned char");
7989
7990 bool TargetIsLittleEndian;
7991
7992public:
7993 BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian)
7994 : Bytes(Width.getQuantity()),
7995 TargetIsLittleEndian(TargetIsLittleEndian) {}
7996
7997 [[nodiscard]] bool readObject(CharUnits Offset, CharUnits Width,
7998 SmallVectorImpl<unsigned char> &Output) const {
7999 for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) {
8000 // If a byte of an integer is uninitialized, then the whole integer is
8001 // uninitialized.
8002 if (!Bytes[I.getQuantity()])
8003 return false;
8004 Output.push_back(Elt: *Bytes[I.getQuantity()]);
8005 }
8006 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
8007 std::reverse(first: Output.begin(), last: Output.end());
8008 return true;
8009 }
8010
8011 void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) {
8012 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
8013 std::reverse(first: Input.begin(), last: Input.end());
8014
8015 size_t Index = 0;
8016 for (unsigned char Byte : Input) {
8017 assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?");
8018 Bytes[Offset.getQuantity() + Index] = Byte;
8019 ++Index;
8020 }
8021 }
8022
8023 size_t size() { return Bytes.size(); }
8024};
8025
8026/// Traverse an APValue to produce an BitCastBuffer, emulating how the current
8027/// target would represent the value at runtime.
8028class APValueToBufferConverter {
8029 EvalInfo &Info;
8030 BitCastBuffer Buffer;
8031 const CastExpr *BCE;
8032
8033 APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth,
8034 const CastExpr *BCE)
8035 : Info(Info),
8036 Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()),
8037 BCE(BCE) {}
8038
8039 bool visit(const APValue &Val, QualType Ty) {
8040 return visit(Val, Ty, Offset: CharUnits::fromQuantity(Quantity: 0));
8041 }
8042
8043 // Write out Val with type Ty into Buffer starting at Offset.
8044 bool visit(const APValue &Val, QualType Ty, CharUnits Offset) {
8045 assert((size_t)Offset.getQuantity() <= Buffer.size());
8046
8047 // As a special case, nullptr_t has an indeterminate value.
8048 if (Ty->isNullPtrType())
8049 return true;
8050
8051 // Dig through Src to find the byte at SrcOffset.
8052 switch (Val.getKind()) {
8053 case APValue::Indeterminate:
8054 case APValue::None:
8055 return true;
8056
8057 case APValue::Int:
8058 return visitInt(Val: Val.getInt(), Ty, Offset);
8059 case APValue::Float:
8060 return visitFloat(Val: Val.getFloat(), Ty, Offset);
8061 case APValue::Array:
8062 return visitArray(Val, Ty, Offset);
8063 case APValue::Struct:
8064 return visitRecord(Val, Ty, Offset);
8065 case APValue::Vector:
8066 return visitVector(Val, Ty, Offset);
8067
8068 case APValue::ComplexInt:
8069 case APValue::ComplexFloat:
8070 return visitComplex(Val, Ty, Offset);
8071 case APValue::FixedPoint:
8072 // FIXME: We should support these.
8073
8074 case APValue::LValue:
8075 case APValue::Matrix:
8076 case APValue::Union:
8077 case APValue::MemberPointer:
8078 case APValue::AddrLabelDiff:
8079 case APValue::Reflection: {
8080 Info.FFDiag(Loc: BCE->getBeginLoc(),
8081 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8082 << Ty;
8083 return false;
8084 }
8085 }
8086 llvm_unreachable("Unhandled APValue::ValueKind");
8087 }
8088
8089 bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) {
8090 const RecordDecl *RD = Ty->getAsRecordDecl();
8091 if (!ASTContext::hasLayout(D: RD))
8092 return false;
8093 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8094
8095 // Visit the base classes.
8096 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8097 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8098 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8099 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8100 const APValue &Base = Val.getStructBase(i: I);
8101
8102 // Can happen in error cases.
8103 if (!Base.isStruct())
8104 return false;
8105
8106 if (!visitRecord(Val: Base, Ty: BS.getType(),
8107 Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset))
8108 return false;
8109 }
8110 }
8111
8112 // Visit the fields.
8113 unsigned FieldIdx = 0;
8114 for (FieldDecl *FD : RD->fields()) {
8115 if (FD->isBitField()) {
8116 Info.FFDiag(Loc: BCE->getBeginLoc(),
8117 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8118 return false;
8119 }
8120
8121 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8122
8123 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 &&
8124 "only bit-fields can have sub-char alignment");
8125 CharUnits FieldOffset =
8126 Info.Ctx.toCharUnitsFromBits(BitSize: FieldOffsetBits) + Offset;
8127 QualType FieldTy = FD->getType();
8128 if (!visit(Val: Val.getStructField(i: FieldIdx), Ty: FieldTy, Offset: FieldOffset))
8129 return false;
8130 ++FieldIdx;
8131 }
8132
8133 return true;
8134 }
8135
8136 bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) {
8137 const auto *CAT =
8138 dyn_cast_or_null<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe());
8139 if (!CAT)
8140 return false;
8141
8142 CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(T: CAT->getElementType());
8143 unsigned NumInitializedElts = Val.getArrayInitializedElts();
8144 unsigned ArraySize = Val.getArraySize();
8145 // First, initialize the initialized elements.
8146 for (unsigned I = 0; I != NumInitializedElts; ++I) {
8147 const APValue &SubObj = Val.getArrayInitializedElt(I);
8148 if (!visit(Val: SubObj, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
8149 return false;
8150 }
8151
8152 // Next, initialize the rest of the array using the filler.
8153 if (Val.hasArrayFiller()) {
8154 const APValue &Filler = Val.getArrayFiller();
8155 for (unsigned I = NumInitializedElts; I != ArraySize; ++I) {
8156 if (!visit(Val: Filler, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
8157 return false;
8158 }
8159 }
8160
8161 return true;
8162 }
8163
8164 bool visitComplex(const APValue &Val, QualType Ty, CharUnits Offset) {
8165 const ComplexType *ComplexTy = Ty->castAs<ComplexType>();
8166 QualType EltTy = ComplexTy->getElementType();
8167 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8168 bool IsInt = Val.isComplexInt();
8169
8170 if (IsInt) {
8171 if (!visitInt(Val: Val.getComplexIntReal(), Ty: EltTy,
8172 Offset: Offset + (0 * EltSizeChars)))
8173 return false;
8174 if (!visitInt(Val: Val.getComplexIntImag(), Ty: EltTy,
8175 Offset: Offset + (1 * EltSizeChars)))
8176 return false;
8177 } else {
8178 if (!visitFloat(Val: Val.getComplexFloatReal(), Ty: EltTy,
8179 Offset: Offset + (0 * EltSizeChars)))
8180 return false;
8181 if (!visitFloat(Val: Val.getComplexFloatImag(), Ty: EltTy,
8182 Offset: Offset + (1 * EltSizeChars)))
8183 return false;
8184 }
8185
8186 return true;
8187 }
8188
8189 bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) {
8190 const VectorType *VTy = Ty->castAs<VectorType>();
8191 QualType EltTy = VTy->getElementType();
8192 unsigned NElts = VTy->getNumElements();
8193
8194 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8195 // Special handling for OpenCL bool vectors:
8196 // Since these vectors are stored as packed bits, but we can't write
8197 // individual bits to the BitCastBuffer, we'll buffer all of the elements
8198 // together into an appropriately sized APInt and write them all out at
8199 // once. Because we don't accept vectors where NElts * EltSize isn't a
8200 // multiple of the char size, there will be no padding space, so we don't
8201 // have to worry about writing data which should have been left
8202 // uninitialized.
8203 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8204
8205 llvm::APInt Res = llvm::APInt::getZero(numBits: NElts);
8206 for (unsigned I = 0; I < NElts; ++I) {
8207 const llvm::APSInt &EltAsInt = Val.getVectorElt(I).getInt();
8208 assert(EltAsInt.isUnsigned() && EltAsInt.getBitWidth() == 1 &&
8209 "bool vector element must be 1-bit unsigned integer!");
8210
8211 Res.insertBits(SubBits: EltAsInt, bitPosition: BigEndian ? (NElts - I - 1) : I);
8212 }
8213
8214 SmallVector<uint8_t, 8> Bytes(NElts / 8);
8215 llvm::StoreIntToMemory(IntVal: Res, Dst: &*Bytes.begin(), StoreBytes: NElts / 8);
8216 Buffer.writeObject(Offset, Input&: Bytes);
8217 } else {
8218 // Iterate over each of the elements and write them out to the buffer at
8219 // the appropriate offset.
8220 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8221 for (unsigned I = 0; I < NElts; ++I) {
8222 if (!visit(Val: Val.getVectorElt(I), Ty: EltTy, Offset: Offset + I * EltSizeChars))
8223 return false;
8224 }
8225 }
8226
8227 return true;
8228 }
8229
8230 bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) {
8231 APSInt AdjustedVal = Val;
8232 unsigned Width = AdjustedVal.getBitWidth();
8233 if (Ty->isBooleanType()) {
8234 Width = Info.Ctx.getTypeSize(T: Ty);
8235 AdjustedVal = AdjustedVal.extend(width: Width);
8236 }
8237
8238 SmallVector<uint8_t, 8> Bytes(Width / 8);
8239 llvm::StoreIntToMemory(IntVal: AdjustedVal, Dst: &*Bytes.begin(), StoreBytes: Width / 8);
8240 Buffer.writeObject(Offset, Input&: Bytes);
8241 return true;
8242 }
8243
8244 bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) {
8245 APSInt AsInt(Val.bitcastToAPInt());
8246 return visitInt(Val: AsInt, Ty, Offset);
8247 }
8248
8249public:
8250 static std::optional<BitCastBuffer>
8251 convert(EvalInfo &Info, const APValue &Src, const CastExpr *BCE) {
8252 CharUnits DstSize = Info.Ctx.getTypeSizeInChars(T: BCE->getType());
8253 APValueToBufferConverter Converter(Info, DstSize, BCE);
8254 if (!Converter.visit(Val: Src, Ty: BCE->getSubExpr()->getType()))
8255 return std::nullopt;
8256 return Converter.Buffer;
8257 }
8258};
8259
8260/// Write an BitCastBuffer into an APValue.
8261class BufferToAPValueConverter {
8262 EvalInfo &Info;
8263 const BitCastBuffer &Buffer;
8264 const CastExpr *BCE;
8265
8266 BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer,
8267 const CastExpr *BCE)
8268 : Info(Info), Buffer(Buffer), BCE(BCE) {}
8269
8270 // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast
8271 // with an invalid type, so anything left is a deficiency on our part (FIXME).
8272 // Ideally this will be unreachable.
8273 std::nullopt_t unsupportedType(QualType Ty) {
8274 Info.FFDiag(Loc: BCE->getBeginLoc(),
8275 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8276 << Ty;
8277 return std::nullopt;
8278 }
8279
8280 std::nullopt_t unrepresentableValue(QualType Ty, const APSInt &Val) {
8281 Info.FFDiag(Loc: BCE->getBeginLoc(),
8282 DiagId: diag::note_constexpr_bit_cast_unrepresentable_value)
8283 << Ty << toString(I: Val, /*Radix=*/10);
8284 return std::nullopt;
8285 }
8286
8287 std::optional<APValue> visit(const BuiltinType *T, CharUnits Offset,
8288 const EnumType *EnumSugar = nullptr) {
8289 if (T->isNullPtrType()) {
8290 uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QT: QualType(T, 0));
8291 return APValue((Expr *)nullptr,
8292 /*Offset=*/CharUnits::fromQuantity(Quantity: NullValue),
8293 APValue::NoLValuePath{}, /*IsNullPtr=*/true);
8294 }
8295
8296 CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T);
8297
8298 // Work around floating point types that contain unused padding bytes. This
8299 // is really just `long double` on x86, which is the only fundamental type
8300 // with padding bytes.
8301 if (T->isRealFloatingType()) {
8302 const llvm::fltSemantics &Semantics =
8303 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8304 unsigned NumBits = llvm::APFloatBase::getSizeInBits(Sem: Semantics);
8305 assert(NumBits % 8 == 0);
8306 CharUnits NumBytes = CharUnits::fromQuantity(Quantity: NumBits / 8);
8307 if (NumBytes != SizeOf)
8308 SizeOf = NumBytes;
8309 }
8310
8311 SmallVector<uint8_t, 8> Bytes;
8312 if (!Buffer.readObject(Offset, Width: SizeOf, Output&: Bytes)) {
8313 // If this is std::byte or unsigned char, then its okay to store an
8314 // indeterminate value.
8315 bool IsStdByte = EnumSugar && EnumSugar->isStdByteType();
8316 bool IsUChar =
8317 !EnumSugar && (T->isSpecificBuiltinType(K: BuiltinType::UChar) ||
8318 T->isSpecificBuiltinType(K: BuiltinType::Char_U));
8319 if (!IsStdByte && !IsUChar) {
8320 QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0);
8321 Info.FFDiag(Loc: BCE->getExprLoc(),
8322 DiagId: diag::note_constexpr_bit_cast_indet_dest)
8323 << DisplayType << Info.Ctx.getLangOpts().CharIsSigned;
8324 return std::nullopt;
8325 }
8326
8327 return APValue::IndeterminateValue();
8328 }
8329
8330 APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true);
8331 llvm::LoadIntFromMemory(IntVal&: Val, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8332
8333 if (T->isIntegralOrEnumerationType()) {
8334 Val.setIsSigned(T->isSignedIntegerOrEnumerationType());
8335
8336 unsigned IntWidth = Info.Ctx.getIntWidth(T: QualType(T, 0));
8337 if (IntWidth != Val.getBitWidth()) {
8338 APSInt Truncated = Val.trunc(width: IntWidth);
8339 if (Truncated.extend(width: Val.getBitWidth()) != Val)
8340 return unrepresentableValue(Ty: QualType(T, 0), Val);
8341 Val = Truncated;
8342 }
8343
8344 return APValue(Val);
8345 }
8346
8347 if (T->isRealFloatingType()) {
8348 const llvm::fltSemantics &Semantics =
8349 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8350 return APValue(APFloat(Semantics, Val));
8351 }
8352
8353 return unsupportedType(Ty: QualType(T, 0));
8354 }
8355
8356 std::optional<APValue> visit(const RecordType *RTy, CharUnits Offset) {
8357 const RecordDecl *RD = RTy->getAsRecordDecl();
8358 if (RD->isInvalidDecl())
8359 return std::nullopt;
8360 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8361
8362 unsigned NumBases = 0;
8363 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
8364 NumBases = CXXRD->getNumBases();
8365
8366 APValue ResultVal(APValue::UninitStruct(), NumBases, RD->getNumFields());
8367
8368 // Visit the base classes.
8369 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8370 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8371 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8372 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8373
8374 std::optional<APValue> SubObj = visitType(
8375 Ty: BS.getType(), Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset);
8376 if (!SubObj)
8377 return std::nullopt;
8378 ResultVal.getStructBase(i: I) = *SubObj;
8379 }
8380 }
8381
8382 // Visit the fields.
8383 unsigned FieldIdx = 0;
8384 for (FieldDecl *FD : RD->fields()) {
8385 // FIXME: We don't currently support bit-fields. A lot of the logic for
8386 // this is in CodeGen, so we need to factor it around.
8387 if (FD->isBitField()) {
8388 Info.FFDiag(Loc: BCE->getBeginLoc(),
8389 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8390 return std::nullopt;
8391 }
8392
8393 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8394 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0);
8395
8396 CharUnits FieldOffset =
8397 CharUnits::fromQuantity(Quantity: FieldOffsetBits / Info.Ctx.getCharWidth()) +
8398 Offset;
8399 QualType FieldTy = FD->getType();
8400 std::optional<APValue> SubObj = visitType(Ty: FieldTy, Offset: FieldOffset);
8401 if (!SubObj)
8402 return std::nullopt;
8403 ResultVal.getStructField(i: FieldIdx) = *SubObj;
8404 ++FieldIdx;
8405 }
8406
8407 return ResultVal;
8408 }
8409
8410 std::optional<APValue> visit(const EnumType *Ty, CharUnits Offset) {
8411 QualType RepresentationType =
8412 Ty->getDecl()->getDefinitionOrSelf()->getIntegerType();
8413 assert(!RepresentationType.isNull() &&
8414 "enum forward decl should be caught by Sema");
8415 const auto *AsBuiltin =
8416 RepresentationType.getCanonicalType()->castAs<BuiltinType>();
8417 // Recurse into the underlying type. Treat std::byte transparently as
8418 // unsigned char.
8419 return visit(T: AsBuiltin, Offset, /*EnumTy=*/EnumSugar: Ty);
8420 }
8421
8422 std::optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) {
8423 size_t Size = Ty->getLimitedSize();
8424 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: Ty->getElementType());
8425
8426 APValue ArrayValue(APValue::UninitArray(), Size, Size);
8427 for (size_t I = 0; I != Size; ++I) {
8428 std::optional<APValue> ElementValue =
8429 visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8430 if (!ElementValue)
8431 return std::nullopt;
8432 ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue);
8433 }
8434
8435 return ArrayValue;
8436 }
8437
8438 std::optional<APValue> visit(const ComplexType *Ty, CharUnits Offset) {
8439 QualType ElementType = Ty->getElementType();
8440 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: ElementType);
8441 bool IsInt = ElementType->isIntegerType();
8442
8443 std::optional<APValue> Values[2];
8444 for (unsigned I = 0; I != 2; ++I) {
8445 Values[I] = visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8446 if (!Values[I])
8447 return std::nullopt;
8448 }
8449
8450 if (IsInt)
8451 return APValue(Values[0]->getInt(), Values[1]->getInt());
8452 return APValue(Values[0]->getFloat(), Values[1]->getFloat());
8453 }
8454
8455 std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) {
8456 QualType EltTy = VTy->getElementType();
8457 unsigned NElts = VTy->getNumElements();
8458 unsigned EltSize =
8459 VTy->isPackedVectorBoolType(ctx: Info.Ctx) ? 1 : Info.Ctx.getTypeSize(T: EltTy);
8460
8461 SmallVector<APValue, 4> Elts;
8462 Elts.reserve(N: NElts);
8463 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8464 // Special handling for OpenCL bool vectors:
8465 // Since these vectors are stored as packed bits, but we can't read
8466 // individual bits from the BitCastBuffer, we'll buffer all of the
8467 // elements together into an appropriately sized APInt and write them all
8468 // out at once. Because we don't accept vectors where NElts * EltSize
8469 // isn't a multiple of the char size, there will be no padding space, so
8470 // we don't have to worry about reading any padding data which didn't
8471 // actually need to be accessed.
8472 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8473
8474 SmallVector<uint8_t, 8> Bytes;
8475 Bytes.reserve(N: NElts / 8);
8476 if (!Buffer.readObject(Offset, Width: CharUnits::fromQuantity(Quantity: NElts / 8), Output&: Bytes))
8477 return std::nullopt;
8478
8479 APSInt SValInt(NElts, true);
8480 llvm::LoadIntFromMemory(IntVal&: SValInt, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8481
8482 for (unsigned I = 0; I < NElts; ++I) {
8483 llvm::APInt Elt =
8484 SValInt.extractBits(numBits: 1, bitPosition: (BigEndian ? NElts - I - 1 : I) * EltSize);
8485 Elts.emplace_back(
8486 Args: APSInt(std::move(Elt), !EltTy->isSignedIntegerType()));
8487 }
8488 } else {
8489 // Iterate over each of the elements and read them from the buffer at
8490 // the appropriate offset.
8491 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8492 for (unsigned I = 0; I < NElts; ++I) {
8493 std::optional<APValue> EltValue =
8494 visitType(Ty: EltTy, Offset: Offset + I * EltSizeChars);
8495 if (!EltValue)
8496 return std::nullopt;
8497 Elts.push_back(Elt: std::move(*EltValue));
8498 }
8499 }
8500
8501 return APValue(Elts.data(), Elts.size());
8502 }
8503
8504 std::optional<APValue> visit(const Type *Ty, CharUnits Offset) {
8505 return unsupportedType(Ty: QualType(Ty, 0));
8506 }
8507
8508 std::optional<APValue> visitType(QualType Ty, CharUnits Offset) {
8509 QualType Can = Ty.getCanonicalType();
8510
8511 switch (Can->getTypeClass()) {
8512#define TYPE(Class, Base) \
8513 case Type::Class: \
8514 return visit(cast<Class##Type>(Can.getTypePtr()), Offset);
8515#define ABSTRACT_TYPE(Class, Base)
8516#define NON_CANONICAL_TYPE(Class, Base) \
8517 case Type::Class: \
8518 llvm_unreachable("non-canonical type should be impossible!");
8519#define DEPENDENT_TYPE(Class, Base) \
8520 case Type::Class: \
8521 llvm_unreachable( \
8522 "dependent types aren't supported in the constant evaluator!");
8523#define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \
8524 case Type::Class: \
8525 llvm_unreachable("either dependent or not canonical!");
8526#include "clang/AST/TypeNodes.inc"
8527 }
8528 llvm_unreachable("Unhandled Type::TypeClass");
8529 }
8530
8531public:
8532 // Pull out a full value of type DstType.
8533 static std::optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer,
8534 const CastExpr *BCE) {
8535 BufferToAPValueConverter Converter(Info, Buffer, BCE);
8536 return Converter.visitType(Ty: BCE->getType(), Offset: CharUnits::fromQuantity(Quantity: 0));
8537 }
8538};
8539
8540static bool checkBitCastConstexprEligibilityType(SourceLocation Loc,
8541 QualType Ty, EvalInfo *Info,
8542 const ASTContext &Ctx,
8543 bool CheckingDest) {
8544 Ty = Ty.getCanonicalType();
8545
8546 auto diag = [&](int Reason) {
8547 if (Info)
8548 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_type)
8549 << CheckingDest << (Reason == 4) << Reason;
8550 return false;
8551 };
8552 auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) {
8553 if (Info)
8554 Info->Note(Loc: NoteLoc, DiagId: diag::note_constexpr_bit_cast_invalid_subtype)
8555 << NoteTy << Construct << Ty;
8556 return false;
8557 };
8558
8559 if (Ty->isUnionType())
8560 return diag(0);
8561 if (Ty->isPointerType())
8562 return diag(1);
8563 if (Ty->isMemberPointerType())
8564 return diag(2);
8565 if (Ty.isVolatileQualified())
8566 return diag(3);
8567
8568 if (RecordDecl *Record = Ty->getAsRecordDecl()) {
8569 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: Record)) {
8570 for (CXXBaseSpecifier &BS : CXXRD->bases())
8571 if (!checkBitCastConstexprEligibilityType(Loc, Ty: BS.getType(), Info, Ctx,
8572 CheckingDest))
8573 return note(1, BS.getType(), BS.getBeginLoc());
8574 }
8575 for (FieldDecl *FD : Record->fields()) {
8576 if (FD->getType()->isReferenceType())
8577 return diag(4);
8578 if (!checkBitCastConstexprEligibilityType(Loc, Ty: FD->getType(), Info, Ctx,
8579 CheckingDest))
8580 return note(0, FD->getType(), FD->getBeginLoc());
8581 }
8582 }
8583
8584 if (Ty->isArrayType() &&
8585 !checkBitCastConstexprEligibilityType(Loc, Ty: Ctx.getBaseElementType(QT: Ty),
8586 Info, Ctx, CheckingDest))
8587 return false;
8588
8589 if (const auto *VTy = Ty->getAs<VectorType>()) {
8590 QualType EltTy = VTy->getElementType();
8591 unsigned NElts = VTy->getNumElements();
8592 unsigned EltSize =
8593 VTy->isPackedVectorBoolType(ctx: Ctx) ? 1 : Ctx.getTypeSize(T: EltTy);
8594
8595 if ((NElts * EltSize) % Ctx.getCharWidth() != 0) {
8596 // The vector's size in bits is not a multiple of the target's byte size,
8597 // so its layout is unspecified. For now, we'll simply treat these cases
8598 // as unsupported (this should only be possible with OpenCL bool vectors
8599 // whose element count isn't a multiple of the byte size).
8600 if (Info)
8601 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_vector)
8602 << QualType(VTy, 0) << EltSize << NElts << Ctx.getCharWidth();
8603 return false;
8604 }
8605
8606 if (EltTy->isRealFloatingType() &&
8607 &Ctx.getFloatTypeSemantics(T: EltTy) == &APFloat::x87DoubleExtended()) {
8608 // The layout for x86_fp80 vectors seems to be handled very inconsistently
8609 // by both clang and LLVM, so for now we won't allow bit_casts involving
8610 // it in a constexpr context.
8611 if (Info)
8612 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8613 << EltTy;
8614 return false;
8615 }
8616 }
8617
8618 return true;
8619}
8620
8621static bool checkBitCastConstexprEligibility(EvalInfo *Info,
8622 const ASTContext &Ctx,
8623 const CastExpr *BCE) {
8624 bool DestOK = checkBitCastConstexprEligibilityType(
8625 Loc: BCE->getBeginLoc(), Ty: BCE->getType(), Info, Ctx, CheckingDest: true);
8626 bool SourceOK = DestOK && checkBitCastConstexprEligibilityType(
8627 Loc: BCE->getBeginLoc(),
8628 Ty: BCE->getSubExpr()->getType(), Info, Ctx, CheckingDest: false);
8629 return SourceOK;
8630}
8631
8632static bool handleRValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8633 const APValue &SourceRValue,
8634 const CastExpr *BCE) {
8635 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8636 "no host or target supports non 8-bit chars");
8637
8638 if (!checkBitCastConstexprEligibility(Info: &Info, Ctx: Info.Ctx, BCE))
8639 return false;
8640
8641 // Read out SourceValue into a char buffer.
8642 std::optional<BitCastBuffer> Buffer =
8643 APValueToBufferConverter::convert(Info, Src: SourceRValue, BCE);
8644 if (!Buffer)
8645 return false;
8646
8647 // Write out the buffer into a new APValue.
8648 std::optional<APValue> MaybeDestValue =
8649 BufferToAPValueConverter::convert(Info, Buffer&: *Buffer, BCE);
8650 if (!MaybeDestValue)
8651 return false;
8652
8653 DestValue = std::move(*MaybeDestValue);
8654 return true;
8655}
8656
8657static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8658 APValue &SourceValue,
8659 const CastExpr *BCE) {
8660 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8661 "no host or target supports non 8-bit chars");
8662 assert(SourceValue.isLValue() &&
8663 "LValueToRValueBitcast requires an lvalue operand!");
8664
8665 LValue SourceLValue;
8666 APValue SourceRValue;
8667 SourceLValue.setFrom(Ctx: Info.Ctx, V: SourceValue);
8668 if (!handleLValueToRValueConversion(
8669 Info, Conv: BCE, Type: BCE->getSubExpr()->getType().withConst(), LVal: SourceLValue,
8670 RVal&: SourceRValue, /*WantObjectRepresentation=*/true))
8671 return false;
8672
8673 return handleRValueToRValueBitCast(Info, DestValue, SourceRValue, BCE);
8674}
8675
8676template <class Derived>
8677class ExprEvaluatorBase
8678 : public ConstStmtVisitor<Derived, bool> {
8679private:
8680 Derived &getDerived() { return static_cast<Derived&>(*this); }
8681 bool DerivedSuccess(const APValue &V, const Expr *E) {
8682 return getDerived().Success(V, E);
8683 }
8684 bool DerivedZeroInitialization(const Expr *E) {
8685 return getDerived().ZeroInitialization(E);
8686 }
8687
8688 // Check whether a conditional operator with a non-constant condition is a
8689 // potential constant expression. If neither arm is a potential constant
8690 // expression, then the conditional operator is not either.
8691 template<typename ConditionalOperator>
8692 void CheckPotentialConstantConditional(const ConditionalOperator *E) {
8693 assert(Info.checkingPotentialConstantExpression());
8694
8695 // Speculatively evaluate both arms.
8696 SmallVector<PartialDiagnosticAt, 8> Diag;
8697 {
8698 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8699 StmtVisitorTy::Visit(E->getFalseExpr());
8700 if (Diag.empty())
8701 return;
8702 }
8703
8704 {
8705 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8706 Diag.clear();
8707 Info.EvalStatus.DiagEmitted = false;
8708 StmtVisitorTy::Visit(E->getTrueExpr());
8709 if (Diag.empty())
8710 return;
8711 }
8712
8713 Error(E, diag::note_constexpr_conditional_never_const);
8714 }
8715
8716
8717 template<typename ConditionalOperator>
8718 bool HandleConditionalOperator(const ConditionalOperator *E) {
8719 bool BoolResult;
8720 if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
8721 if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) {
8722 CheckPotentialConstantConditional(E);
8723 return false;
8724 }
8725 if (Info.noteFailure()) {
8726 StmtVisitorTy::Visit(E->getTrueExpr());
8727 StmtVisitorTy::Visit(E->getFalseExpr());
8728 }
8729 return false;
8730 }
8731
8732 Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
8733 return StmtVisitorTy::Visit(EvalExpr);
8734 }
8735
8736protected:
8737 EvalInfo &Info;
8738 typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy;
8739 typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
8740
8741 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
8742 return Info.CCEDiag(E, DiagId: D);
8743 }
8744
8745 bool ZeroInitialization(const Expr *E) { return Error(E); }
8746
8747 bool IsConstantEvaluatedBuiltinCall(const CallExpr *E) {
8748 unsigned BuiltinOp = E->getBuiltinCallee();
8749 return BuiltinOp != 0 &&
8750 Info.Ctx.BuiltinInfo.isConstantEvaluated(ID: BuiltinOp);
8751 }
8752
8753public:
8754 ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
8755
8756 EvalInfo &getEvalInfo() { return Info; }
8757
8758 /// Report an evaluation error. This should only be called when an error is
8759 /// first discovered. When propagating an error, just return false.
8760 bool Error(const Expr *E, diag::kind D) {
8761 Info.FFDiag(E, DiagId: D) << E->getSourceRange();
8762 return false;
8763 }
8764 bool Error(const Expr *E) {
8765 return Error(E, diag::note_invalid_subexpr_in_const_expr);
8766 }
8767
8768 bool VisitStmt(const Stmt *) {
8769 llvm_unreachable("Expression evaluator should not be called on stmts");
8770 }
8771 bool VisitExpr(const Expr *E) {
8772 return Error(E);
8773 }
8774
8775 bool VisitEmbedExpr(const EmbedExpr *E) {
8776 const auto It = E->begin();
8777 return StmtVisitorTy::Visit(*It);
8778 }
8779
8780 bool VisitPredefinedExpr(const PredefinedExpr *E) {
8781 return StmtVisitorTy::Visit(E->getFunctionName());
8782 }
8783 bool VisitConstantExpr(const ConstantExpr *E) {
8784 if (E->hasAPValueResult())
8785 return DerivedSuccess(V: E->getAPValueResult(), E);
8786
8787 return StmtVisitorTy::Visit(E->getSubExpr());
8788 }
8789
8790 bool VisitParenExpr(const ParenExpr *E)
8791 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8792 bool VisitUnaryExtension(const UnaryOperator *E)
8793 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8794 bool VisitUnaryPlus(const UnaryOperator *E)
8795 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8796 bool VisitChooseExpr(const ChooseExpr *E)
8797 { return StmtVisitorTy::Visit(E->getChosenSubExpr()); }
8798 bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
8799 { return StmtVisitorTy::Visit(E->getResultExpr()); }
8800 bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
8801 { return StmtVisitorTy::Visit(E->getReplacement()); }
8802 bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
8803 TempVersionRAII RAII(*Info.CurrentCall);
8804 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8805 return StmtVisitorTy::Visit(E->getExpr());
8806 }
8807 bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
8808 TempVersionRAII RAII(*Info.CurrentCall);
8809 // The initializer may not have been parsed yet, or might be erroneous.
8810 if (!E->getExpr())
8811 return Error(E);
8812 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8813 return StmtVisitorTy::Visit(E->getExpr());
8814 }
8815
8816 bool VisitExprWithCleanups(const ExprWithCleanups *E) {
8817 FullExpressionRAII Scope(Info);
8818 return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy();
8819 }
8820
8821 // Temporaries are registered when created, so we don't care about
8822 // CXXBindTemporaryExpr.
8823 bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) {
8824 return StmtVisitorTy::Visit(E->getSubExpr());
8825 }
8826
8827 bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
8828 if (E->getCastKind() != CK_PointerToIntegral)
8829 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8830 << diag::ConstexprInvalidCastKind::Reinterpret;
8831 return static_cast<Derived*>(this)->VisitCastExpr(E);
8832 }
8833 bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
8834 if (!Info.Ctx.getLangOpts().CPlusPlus20)
8835 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8836 << diag::ConstexprInvalidCastKind::Dynamic;
8837 return static_cast<Derived*>(this)->VisitCastExpr(E);
8838 }
8839 bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
8840 return static_cast<Derived*>(this)->VisitCastExpr(E);
8841 }
8842
8843 bool VisitBinaryOperator(const BinaryOperator *E) {
8844 switch (E->getOpcode()) {
8845 default:
8846 return Error(E);
8847
8848 case BO_Comma:
8849 VisitIgnoredValue(E: E->getLHS());
8850 return StmtVisitorTy::Visit(E->getRHS());
8851
8852 case BO_PtrMemD:
8853 case BO_PtrMemI: {
8854 LValue Obj;
8855 if (!HandleMemberPointerAccess(Info, BO: E, LV&: Obj))
8856 return false;
8857 APValue Result;
8858 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: Obj, RVal&: Result))
8859 return false;
8860 return DerivedSuccess(V: Result, E);
8861 }
8862 }
8863 }
8864
8865 bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) {
8866 return StmtVisitorTy::Visit(E->getSemanticForm());
8867 }
8868
8869 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
8870 // Evaluate and cache the common expression. We treat it as a temporary,
8871 // even though it's not quite the same thing.
8872 LValue CommonLV;
8873 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8874 Key: E->getOpaqueValue(),
8875 T: getStorageType(Ctx: Info.Ctx, E: E->getOpaqueValue()),
8876 Scope: ScopeKind::FullExpression, LV&: CommonLV),
8877 Info, E: E->getCommon()))
8878 return false;
8879
8880 return HandleConditionalOperator(E);
8881 }
8882
8883 bool VisitConditionalOperator(const ConditionalOperator *E) {
8884 bool IsBcpCall = false;
8885 // If the condition (ignoring parens) is a __builtin_constant_p call,
8886 // the result is a constant expression if it can be folded without
8887 // side-effects. This is an important GNU extension. See GCC PR38377
8888 // for discussion.
8889 if (const CallExpr *CallCE =
8890 dyn_cast<CallExpr>(Val: E->getCond()->IgnoreParenCasts()))
8891 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
8892 IsBcpCall = true;
8893
8894 // Always assume __builtin_constant_p(...) ? ... : ... is a potential
8895 // constant expression; we can't check whether it's potentially foldable.
8896 // FIXME: We should instead treat __builtin_constant_p as non-constant if
8897 // it would return 'false' in this mode.
8898 if (Info.checkingPotentialConstantExpression() && IsBcpCall)
8899 return false;
8900
8901 FoldConstant Fold(Info, IsBcpCall);
8902 if (!HandleConditionalOperator(E)) {
8903 Fold.keepDiagnostics();
8904 return false;
8905 }
8906
8907 return true;
8908 }
8909
8910 bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
8911 if (APValue *Value = Info.CurrentCall->getCurrentTemporary(Key: E);
8912 Value && !Value->isAbsent())
8913 return DerivedSuccess(V: *Value, E);
8914
8915 const Expr *Source = E->getSourceExpr();
8916 if (!Source)
8917 return Error(E);
8918 if (Source == E) {
8919 assert(0 && "OpaqueValueExpr recursively refers to itself");
8920 return Error(E);
8921 }
8922 return StmtVisitorTy::Visit(Source);
8923 }
8924
8925 bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
8926 for (const Expr *SemE : E->semantics()) {
8927 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: SemE)) {
8928 // FIXME: We can't handle the case where an OpaqueValueExpr is also the
8929 // result expression: there could be two different LValues that would
8930 // refer to the same object in that case, and we can't model that.
8931 if (SemE == E->getResultExpr())
8932 return Error(E);
8933
8934 // Unique OVEs get evaluated if and when we encounter them when
8935 // emitting the rest of the semantic form, rather than eagerly.
8936 if (OVE->isUnique())
8937 continue;
8938
8939 LValue LV;
8940 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8941 Key: OVE, T: getStorageType(Ctx: Info.Ctx, E: OVE),
8942 Scope: ScopeKind::FullExpression, LV),
8943 Info, E: OVE->getSourceExpr()))
8944 return false;
8945 } else if (SemE == E->getResultExpr()) {
8946 if (!StmtVisitorTy::Visit(SemE))
8947 return false;
8948 } else {
8949 if (!EvaluateIgnoredValue(Info, E: SemE))
8950 return false;
8951 }
8952 }
8953 return true;
8954 }
8955
8956 bool VisitCallExpr(const CallExpr *E) {
8957 APValue Result;
8958 if (!handleCallExpr(E, Result, ResultSlot: nullptr))
8959 return false;
8960 return DerivedSuccess(V: Result, E);
8961 }
8962
8963 bool handleCallExpr(const CallExpr *E, APValue &Result,
8964 const LValue *ResultSlot) {
8965 CallScopeRAII CallScope(Info);
8966
8967 const Expr *Callee = E->getCallee()->IgnoreParens();
8968 QualType CalleeType = Callee->getType();
8969
8970 const FunctionDecl *FD = nullptr;
8971 LValue *This = nullptr, ObjectArg;
8972 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
8973 bool HasQualifier = false;
8974
8975 CallRef Call;
8976
8977 // Extract function decl and 'this' pointer from the callee.
8978 if (CalleeType->isSpecificBuiltinType(K: BuiltinType::BoundMember)) {
8979 const CXXMethodDecl *Member = nullptr;
8980 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: Callee)) {
8981 // Explicit bound member calls, such as x.f() or p->g();
8982 if (!EvaluateObjectArgument(Info, Object: ME->getBase(), This&: ObjectArg))
8983 return false;
8984 Member = dyn_cast<CXXMethodDecl>(Val: ME->getMemberDecl());
8985 if (!Member)
8986 return Error(Callee);
8987 This = &ObjectArg;
8988 HasQualifier = ME->hasQualifier();
8989 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Val: Callee)) {
8990 // Indirect bound member calls ('.*' or '->*').
8991 const ValueDecl *D =
8992 HandleMemberPointerAccess(Info, BO: BE, LV&: ObjectArg, IncludeMember: false);
8993 if (!D)
8994 return false;
8995 Member = dyn_cast<CXXMethodDecl>(Val: D);
8996 if (!Member)
8997 return Error(Callee);
8998 This = &ObjectArg;
8999 } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Val: Callee)) {
9000 if (!Info.getLangOpts().CPlusPlus20)
9001 Info.CCEDiag(E: PDE, DiagId: diag::note_constexpr_pseudo_destructor);
9002 return EvaluateObjectArgument(Info, Object: PDE->getBase(), This&: ObjectArg) &&
9003 HandleDestruction(Info, E: PDE, This: ObjectArg, ThisType: PDE->getDestroyedType());
9004 } else
9005 return Error(Callee);
9006 FD = Member;
9007 } else if (CalleeType->isFunctionPointerType()) {
9008 LValue CalleeLV;
9009 if (!EvaluatePointer(E: Callee, Result&: CalleeLV, Info))
9010 return false;
9011
9012 if (!CalleeLV.getLValueOffset().isZero())
9013 return Error(Callee);
9014 if (CalleeLV.isNullPointer()) {
9015 Info.FFDiag(E: Callee, DiagId: diag::note_constexpr_null_callee)
9016 << const_cast<Expr *>(Callee);
9017 return false;
9018 }
9019 FD = dyn_cast_or_null<FunctionDecl>(
9020 Val: CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>());
9021 if (!FD)
9022 return Error(Callee);
9023 // Don't call function pointers which have been cast to some other type.
9024 // Per DR (no number yet), the caller and callee can differ in noexcept.
9025 if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec(
9026 T: CalleeType->getPointeeType(), U: FD->getType())) {
9027 return Error(E);
9028 }
9029
9030 // For an (overloaded) assignment expression, evaluate the RHS before the
9031 // LHS.
9032 auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E);
9033 if (OCE && OCE->isAssignmentOp()) {
9034 assert(Args.size() == 2 && "wrong number of arguments in assignment");
9035 Call = Info.CurrentCall->createCall(Callee: FD);
9036 bool HasThis = false;
9037 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FD))
9038 HasThis = MD->isImplicitObjectMemberFunction();
9039 if (!EvaluateArgs(Args: HasThis ? Args.slice(N: 1) : Args, Call, Info, Callee: FD,
9040 /*RightToLeft=*/true, ObjectArg: &ObjectArg))
9041 return false;
9042 }
9043
9044 // Overloaded operator calls to member functions are represented as normal
9045 // calls with '*this' as the first argument.
9046 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
9047 if (MD &&
9048 (MD->isImplicitObjectMemberFunction() || (OCE && MD->isStatic()))) {
9049 // FIXME: When selecting an implicit conversion for an overloaded
9050 // operator delete, we sometimes try to evaluate calls to conversion
9051 // operators without a 'this' parameter!
9052 if (Args.empty())
9053 return Error(E);
9054
9055 if (!EvaluateObjectArgument(Info, Object: Args[0], This&: ObjectArg))
9056 return false;
9057
9058 // If we are calling a static operator, the 'this' argument needs to be
9059 // ignored after being evaluated.
9060 if (MD->isInstance())
9061 This = &ObjectArg;
9062
9063 // If this is syntactically a simple assignment using a trivial
9064 // assignment operator, start the lifetimes of union members as needed,
9065 // per C++20 [class.union]5.
9066 if (Info.getLangOpts().CPlusPlus20 && OCE &&
9067 OCE->getOperator() == OO_Equal && MD->isTrivial() &&
9068 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: Args[0], LHS: ObjectArg))
9069 return false;
9070
9071 Args = Args.slice(N: 1);
9072 } else if (MD && MD->isLambdaStaticInvoker()) {
9073 // Map the static invoker for the lambda back to the call operator.
9074 // Conveniently, we don't have to slice out the 'this' argument (as is
9075 // being done for the non-static case), since a static member function
9076 // doesn't have an implicit argument passed in.
9077 const CXXRecordDecl *ClosureClass = MD->getParent();
9078 assert(
9079 ClosureClass->captures().empty() &&
9080 "Number of captures must be zero for conversion to function-ptr");
9081
9082 const CXXMethodDecl *LambdaCallOp =
9083 ClosureClass->getLambdaCallOperator();
9084
9085 // Set 'FD', the function that will be called below, to the call
9086 // operator. If the closure object represents a generic lambda, find
9087 // the corresponding specialization of the call operator.
9088
9089 if (ClosureClass->isGenericLambda()) {
9090 assert(MD->isFunctionTemplateSpecialization() &&
9091 "A generic lambda's static-invoker function must be a "
9092 "template specialization");
9093 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
9094 FunctionTemplateDecl *CallOpTemplate =
9095 LambdaCallOp->getDescribedFunctionTemplate();
9096 llvm::FoldingSetInsertToken InsertToken;
9097 FunctionDecl *CorrespondingCallOpSpecialization =
9098 CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertToken);
9099 assert(CorrespondingCallOpSpecialization &&
9100 "We must always have a function call operator specialization "
9101 "that corresponds to our static invoker specialization");
9102 assert(isa<CXXMethodDecl>(CorrespondingCallOpSpecialization));
9103 FD = CorrespondingCallOpSpecialization;
9104 } else
9105 FD = LambdaCallOp;
9106 } else if (FD->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
9107 if (FD->getDeclName().isAnyOperatorNew()) {
9108 LValue Ptr;
9109 if (!HandleOperatorNewCall(Info, E, Result&: Ptr))
9110 return false;
9111 Ptr.moveInto(V&: Result);
9112 return CallScope.destroy();
9113 } else {
9114 return HandleOperatorDeleteCall(Info, E) && CallScope.destroy();
9115 }
9116 }
9117 } else
9118 return Error(E);
9119
9120 // Evaluate the arguments now if we've not already done so.
9121 if (!Call) {
9122 Call = Info.CurrentCall->createCall(Callee: FD);
9123 if (!EvaluateArgs(Args, Call, Info, Callee: FD, /*RightToLeft*/ false,
9124 ObjectArg: &ObjectArg))
9125 return false;
9126 }
9127
9128 SmallVector<QualType, 4> CovariantAdjustmentPath;
9129 if (This) {
9130 auto *NamedMember = dyn_cast<CXXMethodDecl>(Val: FD);
9131 if (NamedMember && NamedMember->isVirtual() && !HasQualifier) {
9132 // Perform virtual dispatch, if necessary.
9133 FD = HandleVirtualDispatch(Info, E, This&: *This, Found: NamedMember,
9134 CovariantAdjustmentPath);
9135 if (!FD)
9136 return false;
9137 } else if (NamedMember && NamedMember->isImplicitObjectMemberFunction()) {
9138 // Check that the 'this' pointer points to an object of the right type.
9139 // FIXME: If this is an assignment operator call, we may need to change
9140 // the active union member before we check this.
9141 if (!checkNonVirtualMemberCallThisPointer(Info, E, This: *This, NamedMember))
9142 return false;
9143 }
9144 }
9145
9146 // Destructor calls are different enough that they have their own codepath.
9147 if (auto *DD = dyn_cast<CXXDestructorDecl>(Val: FD)) {
9148 assert(This && "no 'this' pointer for destructor call");
9149 return HandleDestruction(Info, E, This: *This,
9150 ThisType: Info.Ctx.getCanonicalTagType(TD: DD->getParent())) &&
9151 CallScope.destroy();
9152 }
9153
9154 const FunctionDecl *Definition = nullptr;
9155 Stmt *Body = FD->getBody(Definition);
9156 SourceLocation Loc = E->getExprLoc();
9157
9158 // Treat the object argument as `this` when evaluating defaulted
9159 // special menmber functions
9160 if (FD->hasCXXExplicitFunctionObjectParameter())
9161 This = &ObjectArg;
9162
9163 if (!CheckConstexprFunction(Info, CallLoc: Loc, Declaration: FD, Definition, Body) ||
9164 !HandleFunctionCall(CallLoc: Loc, Callee: Definition, ObjectArg: This, E, Args, Call, Body, Info,
9165 Result, ResultSlot))
9166 return false;
9167
9168 if (!CovariantAdjustmentPath.empty() &&
9169 !HandleCovariantReturnAdjustment(Info, E, Result,
9170 Path: CovariantAdjustmentPath))
9171 return false;
9172
9173 return CallScope.destroy();
9174 }
9175
9176 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9177 return StmtVisitorTy::Visit(E->getInitializer());
9178 }
9179 bool VisitInitListExpr(const InitListExpr *E) {
9180 if (E->getNumInits() == 0)
9181 return DerivedZeroInitialization(E);
9182 if (E->getNumInits() == 1)
9183 return StmtVisitorTy::Visit(E->getInit(Init: 0));
9184 return Error(E);
9185 }
9186 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
9187 return DerivedZeroInitialization(E);
9188 }
9189 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
9190 return DerivedZeroInitialization(E);
9191 }
9192 bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
9193 return DerivedZeroInitialization(E);
9194 }
9195
9196 /// A member expression where the object is a prvalue is itself a prvalue.
9197 bool VisitMemberExpr(const MemberExpr *E) {
9198 assert(!Info.Ctx.getLangOpts().CPlusPlus11 &&
9199 "missing temporary materialization conversion");
9200 assert(!E->isArrow() && "missing call to bound member function?");
9201
9202 APValue Val;
9203 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
9204 return false;
9205
9206 QualType BaseTy = E->getBase()->getType();
9207
9208 const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl());
9209 if (!FD) return Error(E);
9210 assert(!FD->getType()->isReferenceType() && "prvalue reference?");
9211 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9212 FD->getParent()->getCanonicalDecl() &&
9213 "record / field mismatch");
9214
9215 // Note: there is no lvalue base here. But this case should only ever
9216 // happen in C or in C++98, where we cannot be evaluating a constexpr
9217 // constructor, which is the only case the base matters.
9218 CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy);
9219 SubobjectDesignator Designator(BaseTy);
9220 Designator.addDeclUnchecked(D: FD);
9221
9222 APValue Result;
9223 return extractSubobject(Info, E, Obj, Sub: Designator, Result) &&
9224 DerivedSuccess(V: Result, E);
9225 }
9226
9227 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) {
9228 APValue Val;
9229 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
9230 return false;
9231
9232 if (Val.isVector()) {
9233 SmallVector<uint32_t, 4> Indices;
9234 E->getEncodedElementAccess(Elts&: Indices);
9235 if (Indices.size() == 1) {
9236 // Return scalar.
9237 return DerivedSuccess(V: Val.getVectorElt(I: Indices[0]), E);
9238 } else {
9239 // Construct new APValue vector.
9240 SmallVector<APValue, 4> Elts;
9241 for (unsigned I = 0; I < Indices.size(); ++I) {
9242 Elts.push_back(Elt: Val.getVectorElt(I: Indices[I]));
9243 }
9244 APValue VecResult(Elts.data(), Indices.size());
9245 return DerivedSuccess(V: VecResult, E);
9246 }
9247 }
9248
9249 return false;
9250 }
9251
9252 bool VisitCastExpr(const CastExpr *E) {
9253 switch (E->getCastKind()) {
9254 default:
9255 break;
9256
9257 case CK_AtomicToNonAtomic: {
9258 APValue AtomicVal;
9259 // This does not need to be done in place even for class/array types:
9260 // atomic-to-non-atomic conversion implies copying the object
9261 // representation.
9262 if (!Evaluate(Result&: AtomicVal, Info, E: E->getSubExpr()))
9263 return false;
9264 return DerivedSuccess(V: AtomicVal, E);
9265 }
9266
9267 case CK_NoOp:
9268 case CK_UserDefinedConversion:
9269 return StmtVisitorTy::Visit(E->getSubExpr());
9270
9271 case CK_HLSLArrayRValue: {
9272 const Expr *SubExpr = E->getSubExpr();
9273 if (!SubExpr->isGLValue()) {
9274 APValue Val;
9275 if (!Evaluate(Result&: Val, Info, E: SubExpr))
9276 return false;
9277 return DerivedSuccess(V: Val, E);
9278 }
9279
9280 LValue LVal;
9281 if (!EvaluateLValue(E: SubExpr, Result&: LVal, Info))
9282 return false;
9283 APValue RVal;
9284 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9285 if (!handleLValueToRValueConversion(Info, Conv: E, Type: SubExpr->getType(), LVal,
9286 RVal))
9287 return false;
9288 return DerivedSuccess(V: RVal, E);
9289 }
9290 case CK_LValueToRValue: {
9291 LValue LVal;
9292 if (!EvaluateLValue(E: E->getSubExpr(), Result&: LVal, Info))
9293 return false;
9294 APValue RVal;
9295 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9296 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
9297 LVal, RVal))
9298 return false;
9299 return DerivedSuccess(V: RVal, E);
9300 }
9301 case CK_LValueToRValueBitCast: {
9302 APValue DestValue, SourceValue;
9303 if (!Evaluate(Result&: SourceValue, Info, E: E->getSubExpr()))
9304 return false;
9305 if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, BCE: E))
9306 return false;
9307 return DerivedSuccess(V: DestValue, E);
9308 }
9309
9310 case CK_AddressSpaceConversion: {
9311 APValue Value;
9312 if (!Evaluate(Result&: Value, Info, E: E->getSubExpr()))
9313 return false;
9314 return DerivedSuccess(V: Value, E);
9315 }
9316 }
9317
9318 return Error(E);
9319 }
9320
9321 bool VisitUnaryPostInc(const UnaryOperator *UO) {
9322 return VisitUnaryPostIncDec(UO);
9323 }
9324 bool VisitUnaryPostDec(const UnaryOperator *UO) {
9325 return VisitUnaryPostIncDec(UO);
9326 }
9327 bool VisitUnaryPostIncDec(const UnaryOperator *UO) {
9328 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9329 return Error(UO);
9330
9331 LValue LVal;
9332 if (!EvaluateLValue(E: UO->getSubExpr(), Result&: LVal, Info))
9333 return false;
9334 APValue RVal;
9335 if (!handleIncDec(Info&: this->Info, E: UO, LVal, LValType: UO->getSubExpr()->getType(),
9336 IsIncrement: UO->isIncrementOp(), Old: &RVal))
9337 return false;
9338 return DerivedSuccess(V: RVal, E: UO);
9339 }
9340
9341 bool VisitStmtExpr(const StmtExpr *E) {
9342 // We will have checked the full-expressions inside the statement expression
9343 // when they were completed, and don't need to check them again now.
9344 llvm::SaveAndRestore NotCheckingForUB(Info.CheckingForUndefinedBehavior,
9345 false);
9346
9347 const CompoundStmt *CS = E->getSubStmt();
9348 if (CS->body_empty())
9349 return true;
9350
9351 BlockScopeRAII Scope(Info);
9352 for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
9353 BE = CS->body_end();
9354 /**/; ++BI) {
9355 if (BI + 1 == BE) {
9356 const Expr *FinalExpr = dyn_cast<Expr>(Val: *BI);
9357 if (!FinalExpr) {
9358 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9359 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9360 return false;
9361 }
9362 return this->Visit(FinalExpr) && Scope.destroy();
9363 }
9364
9365 APValue ReturnValue;
9366 StmtResult Result = { .Value: ReturnValue, .Slot: nullptr };
9367 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: *BI);
9368 if (ESR != ESR_Succeeded) {
9369 // FIXME: If the statement-expression terminated due to 'return',
9370 // 'break', or 'continue', it would be nice to propagate that to
9371 // the outer statement evaluation rather than bailing out.
9372 if (ESR != ESR_Failed)
9373 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9374 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9375 return false;
9376 }
9377 }
9378
9379 llvm_unreachable("Return from function from the loop above.");
9380 }
9381
9382 bool VisitPackIndexingExpr(const PackIndexingExpr *E) {
9383 return StmtVisitorTy::Visit(E->getSelectedExpr());
9384 }
9385
9386 /// Visit a value which is evaluated, but whose value is ignored.
9387 void VisitIgnoredValue(const Expr *E) {
9388 EvaluateIgnoredValue(Info, E);
9389 }
9390
9391 /// Potentially visit a MemberExpr's base expression.
9392 void VisitIgnoredBaseExpression(const Expr *E) {
9393 // While MSVC doesn't evaluate the base expression, it does diagnose the
9394 // presence of side-effecting behavior.
9395 if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Ctx: Info.Ctx))
9396 return;
9397 VisitIgnoredValue(E);
9398 }
9399};
9400
9401} // namespace
9402
9403//===----------------------------------------------------------------------===//
9404// Common base class for lvalue and temporary evaluation.
9405//===----------------------------------------------------------------------===//
9406namespace {
9407template<class Derived>
9408class LValueExprEvaluatorBase
9409 : public ExprEvaluatorBase<Derived> {
9410protected:
9411 LValue &Result;
9412 bool InvalidBaseOK;
9413 typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
9414 typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy;
9415
9416 bool Success(APValue::LValueBase B) {
9417 Result.set(B);
9418 return true;
9419 }
9420
9421 bool evaluatePointer(const Expr *E, LValue &Result) {
9422 return EvaluatePointer(E, Result, this->Info, InvalidBaseOK);
9423 }
9424
9425public:
9426 LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK)
9427 : ExprEvaluatorBaseTy(Info), Result(Result),
9428 InvalidBaseOK(InvalidBaseOK) {}
9429
9430 bool Success(const APValue &V, const Expr *E) {
9431 Result.setFrom(Ctx: this->Info.Ctx, V);
9432 return true;
9433 }
9434
9435 bool VisitMemberExpr(const MemberExpr *E) {
9436 // Handle non-static data members.
9437 QualType BaseTy;
9438 bool EvalOK;
9439 if (E->isArrow()) {
9440 EvalOK = evaluatePointer(E: E->getBase(), Result);
9441 BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType();
9442 } else if (E->getBase()->isPRValue()) {
9443 assert(E->getBase()->getType()->isRecordType());
9444 EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info);
9445 BaseTy = E->getBase()->getType();
9446 } else {
9447 EvalOK = this->Visit(E->getBase());
9448 BaseTy = E->getBase()->getType();
9449 }
9450 if (!EvalOK) {
9451 if (!InvalidBaseOK)
9452 return false;
9453 Result.setInvalid(B: E);
9454 return true;
9455 }
9456
9457 const ValueDecl *MD = E->getMemberDecl();
9458 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl())) {
9459 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9460 FD->getParent()->getCanonicalDecl() &&
9461 "record / field mismatch");
9462 (void)BaseTy;
9463 if (!HandleLValueMember(this->Info, E, Result, FD))
9464 return false;
9465 } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Val: MD)) {
9466 if (!HandleLValueIndirectMember(this->Info, E, Result, IFD))
9467 return false;
9468 } else
9469 return this->Error(E);
9470
9471 if (MD->getType()->isReferenceType()) {
9472 APValue RefValue;
9473 if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
9474 RefValue))
9475 return false;
9476 return Success(RefValue, E);
9477 }
9478 return true;
9479 }
9480
9481 bool VisitBinaryOperator(const BinaryOperator *E) {
9482 switch (E->getOpcode()) {
9483 default:
9484 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
9485
9486 case BO_PtrMemD:
9487 case BO_PtrMemI:
9488 return HandleMemberPointerAccess(this->Info, E, Result);
9489 }
9490 }
9491
9492 bool VisitCastExpr(const CastExpr *E) {
9493 switch (E->getCastKind()) {
9494 default:
9495 return ExprEvaluatorBaseTy::VisitCastExpr(E);
9496
9497 case CK_DerivedToBase:
9498 case CK_UncheckedDerivedToBase:
9499 if (!this->Visit(E->getSubExpr()))
9500 return false;
9501
9502 // Now figure out the necessary offset to add to the base LV to get from
9503 // the derived class to the base class.
9504 return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(),
9505 Result);
9506 }
9507 }
9508};
9509}
9510
9511//===----------------------------------------------------------------------===//
9512// LValue Evaluation
9513//
9514// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
9515// function designators (in C), decl references to void objects (in C), and
9516// temporaries (if building with -Wno-address-of-temporary).
9517//
9518// LValue evaluation produces values comprising a base expression of one of the
9519// following types:
9520// - Declarations
9521// * VarDecl
9522// * FunctionDecl
9523// - Literals
9524// * CompoundLiteralExpr in C (and in global scope in C++)
9525// * StringLiteral
9526// * PredefinedExpr
9527// * ObjCStringLiteralExpr
9528// * ObjCEncodeExpr
9529// * AddrLabelExpr
9530// * BlockExpr
9531// * CallExpr for a MakeStringConstant builtin
9532// - typeid(T) expressions, as TypeInfoLValues
9533// - Locals and temporaries
9534// * MaterializeTemporaryExpr
9535// * Any Expr, with a CallIndex indicating the function in which the temporary
9536// was evaluated, for cases where the MaterializeTemporaryExpr is missing
9537// from the AST (FIXME).
9538// * A MaterializeTemporaryExpr that has static storage duration, with no
9539// CallIndex, for a lifetime-extended temporary.
9540// * The ConstantExpr that is currently being evaluated during evaluation of an
9541// immediate invocation.
9542// plus an offset in bytes.
9543//===----------------------------------------------------------------------===//
9544namespace {
9545class LValueExprEvaluator
9546 : public LValueExprEvaluatorBase<LValueExprEvaluator> {
9547public:
9548 LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) :
9549 LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {}
9550
9551 bool VisitVarDecl(const Expr *E, const VarDecl *VD);
9552 bool VisitUnaryPreIncDec(const UnaryOperator *UO);
9553
9554 bool VisitCallExpr(const CallExpr *E);
9555 bool VisitDeclRefExpr(const DeclRefExpr *E);
9556 bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(B: E); }
9557 bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
9558 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
9559 bool VisitMemberExpr(const MemberExpr *E);
9560 bool VisitStringLiteral(const StringLiteral *E) {
9561 return Success(
9562 B: APValue::LValueBase(E, 0, Info.Ctx.getNextStringLiteralVersion()));
9563 }
9564 bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(B: E); }
9565 bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
9566 bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
9567 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
9568 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E);
9569 bool VisitUnaryDeref(const UnaryOperator *E);
9570 bool VisitUnaryReal(const UnaryOperator *E);
9571 bool VisitUnaryImag(const UnaryOperator *E);
9572 bool VisitUnaryPreInc(const UnaryOperator *UO) {
9573 return VisitUnaryPreIncDec(UO);
9574 }
9575 bool VisitUnaryPreDec(const UnaryOperator *UO) {
9576 return VisitUnaryPreIncDec(UO);
9577 }
9578 bool VisitBinAssign(const BinaryOperator *BO);
9579 bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO);
9580
9581 bool VisitCastExpr(const CastExpr *E) {
9582 switch (E->getCastKind()) {
9583 default:
9584 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
9585
9586 case CK_LValueBitCast:
9587 this->CCEDiag(E, D: diag::note_constexpr_invalid_cast)
9588 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
9589 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
9590 if (!Visit(S: E->getSubExpr()))
9591 return false;
9592 Result.Designator.setInvalid();
9593 return true;
9594
9595 case CK_BaseToDerived:
9596 if (!Visit(S: E->getSubExpr()))
9597 return false;
9598 return HandleBaseToDerivedCast(Info, E, Result);
9599
9600 case CK_Dynamic:
9601 if (!Visit(S: E->getSubExpr()))
9602 return false;
9603 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
9604 }
9605 }
9606};
9607} // end anonymous namespace
9608
9609/// Get an lvalue to a field of a lambda's closure type.
9610static bool HandleLambdaCapture(EvalInfo &Info, const Expr *E, LValue &Result,
9611 const CXXMethodDecl *MD, const FieldDecl *FD,
9612 bool LValueToRValueConversion) {
9613 // Static lambda function call operators can't have captures. We already
9614 // diagnosed this, so bail out here.
9615 if (MD->isStatic()) {
9616 assert(Info.CurrentCall->This == nullptr &&
9617 "This should not be set for a static call operator");
9618 return false;
9619 }
9620
9621 // Start with 'Result' referring to the complete closure object...
9622 if (MD->isExplicitObjectMemberFunction()) {
9623 // Self may be passed by reference or by value.
9624 const ParmVarDecl *Self = MD->getParamDecl(i: 0);
9625 if (Self->getType()->isReferenceType()) {
9626 APValue *RefValue = Info.getParamSlot(Call: Info.CurrentCall->Arguments, PVD: Self);
9627 if (!RefValue->allowConstexprUnknown() || RefValue->hasValue())
9628 Result.setFrom(Ctx: Info.Ctx, V: *RefValue);
9629 } else {
9630 const ParmVarDecl *VD = Info.CurrentCall->Arguments.getOrigParam(PVD: Self);
9631 CallStackFrame *Frame =
9632 Info.getCallFrameAndDepth(CallIndex: Info.CurrentCall->Arguments.CallIndex)
9633 .first;
9634 unsigned Version = Info.CurrentCall->Arguments.Version;
9635 Result.set(B: {VD, Frame->Index, Version});
9636 }
9637 } else
9638 Result = *Info.CurrentCall->This;
9639
9640 // ... then update it to refer to the field of the closure object
9641 // that represents the capture.
9642 if (!HandleLValueMember(Info, E, LVal&: Result, FD))
9643 return false;
9644
9645 // And if the field is of reference type (or if we captured '*this' by
9646 // reference), update 'Result' to refer to what
9647 // the field refers to.
9648 if (LValueToRValueConversion) {
9649 APValue RVal;
9650 if (!handleLValueToRValueConversion(Info, Conv: E, Type: FD->getType(), LVal: Result, RVal))
9651 return false;
9652 Result.setFrom(Ctx: Info.Ctx, V: RVal);
9653 }
9654 return true;
9655}
9656
9657/// Evaluate an expression as an lvalue. This can be legitimately called on
9658/// expressions which are not glvalues, in three cases:
9659/// * function designators in C, and
9660/// * "extern void" objects
9661/// * @selector() expressions in Objective-C
9662static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
9663 bool InvalidBaseOK) {
9664 assert(!E->isValueDependent());
9665 assert(E->isGLValue() || E->getType()->isFunctionType() ||
9666 E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E->IgnoreParens()));
9667 return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
9668}
9669
9670bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
9671 const ValueDecl *D = E->getDecl();
9672
9673 // If we are within a lambda's call operator, check whether the 'VD' referred
9674 // to within 'E' actually represents a lambda-capture that maps to a
9675 // data-member/field within the closure object, and if so, evaluate to the
9676 // field or what the field refers to.
9677 if (Info.CurrentCall && isLambdaCallOperator(DC: Info.CurrentCall->Callee) &&
9678 E->refersToEnclosingVariableOrCapture()) {
9679 // We don't always have a complete capture-map when checking or inferring if
9680 // the function call operator meets the requirements of a constexpr function
9681 // - but we don't need to evaluate the captures to determine constexprness
9682 // (dcl.constexpr C++17).
9683 if (Info.checkingPotentialConstantExpression())
9684 return false;
9685
9686 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(Val: D)) {
9687 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
9688 return HandleLambdaCapture(Info, E, Result, MD, FD,
9689 LValueToRValueConversion: FD->getType()->isReferenceType());
9690 }
9691 }
9692
9693 if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl,
9694 UnnamedGlobalConstantDecl>(Val: D))
9695 return Success(B: cast<ValueDecl>(Val: D));
9696 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
9697 return VisitVarDecl(E, VD);
9698 if (const BindingDecl *BD = dyn_cast<BindingDecl>(Val: D))
9699 return Visit(S: BD->getBinding());
9700 return Error(E);
9701}
9702
9703bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
9704 CallStackFrame *Frame = nullptr;
9705 unsigned Version = 0;
9706 if (VD->hasLocalStorage()) {
9707 // Only if a local variable was declared in the function currently being
9708 // evaluated, do we expect to be able to find its value in the current
9709 // frame. (Otherwise it was likely declared in an enclosing context and
9710 // could either have a valid evaluatable value (for e.g. a constexpr
9711 // variable) or be ill-formed (and trigger an appropriate evaluation
9712 // diagnostic)).
9713 CallStackFrame *CurrFrame = Info.CurrentCall;
9714 if (CurrFrame->Callee && CurrFrame->Callee->Equals(DC: VD->getDeclContext())) {
9715 // Function parameters are stored in some caller's frame. (Usually the
9716 // immediate caller, but for an inherited constructor they may be more
9717 // distant.)
9718 if (auto *PVD = dyn_cast<ParmVarDecl>(Val: VD)) {
9719 if (CurrFrame->Arguments) {
9720 VD = CurrFrame->Arguments.getOrigParam(PVD);
9721 Frame =
9722 Info.getCallFrameAndDepth(CallIndex: CurrFrame->Arguments.CallIndex).first;
9723 Version = CurrFrame->Arguments.Version;
9724 }
9725 } else {
9726 Frame = CurrFrame;
9727 Version = CurrFrame->getCurrentTemporaryVersion(Key: VD);
9728 }
9729 }
9730 }
9731
9732 if (!VD->getType()->isReferenceType()) {
9733 if (Frame) {
9734 Result.set(B: {VD, Frame->Index, Version});
9735 return true;
9736 }
9737 return Success(B: VD);
9738 }
9739
9740 if (!Info.getLangOpts().CPlusPlus11) {
9741 Info.CCEDiag(E, DiagId: diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
9742 << VD << VD->getType();
9743 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
9744 }
9745
9746 APValue *V;
9747 if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, Result&: V))
9748 return false;
9749
9750 if (!V) {
9751 Result.set(B: VD);
9752 Result.AllowConstexprUnknown = true;
9753 return true;
9754 }
9755
9756 return Success(V: *V, E);
9757}
9758
9759bool LValueExprEvaluator::VisitCallExpr(const CallExpr *E) {
9760 if (!IsConstantEvaluatedBuiltinCall(E))
9761 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9762
9763 switch (E->getBuiltinCallee()) {
9764 default:
9765 return false;
9766 case Builtin::BIas_const:
9767 case Builtin::BIforward:
9768 case Builtin::BIforward_like:
9769 case Builtin::BImove:
9770 case Builtin::BImove_if_noexcept:
9771 if (cast<FunctionDecl>(Val: E->getCalleeDecl())->isConstexpr())
9772 return Visit(S: E->getArg(Arg: 0));
9773 break;
9774 }
9775
9776 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9777}
9778
9779bool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
9780 const MaterializeTemporaryExpr *E) {
9781 // Walk through the expression to find the materialized temporary itself.
9782 SmallVector<const Expr *, 2> CommaLHSs;
9783 SmallVector<SubobjectAdjustment, 2> Adjustments;
9784 const Expr *Inner =
9785 E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments);
9786
9787 // If we passed any comma operators, evaluate their LHSs.
9788 for (const Expr *E : CommaLHSs)
9789 if (!EvaluateIgnoredValue(Info, E))
9790 return false;
9791
9792 // A materialized temporary with static storage duration can appear within the
9793 // result of a constant expression evaluation, so we need to preserve its
9794 // value for use outside this evaluation.
9795 APValue *Value;
9796 if (E->getStorageDuration() == SD_Static) {
9797 if (Info.EvalMode == EvaluationMode::ConstantFold)
9798 return false;
9799 // FIXME: What about SD_Thread?
9800 Value = E->getOrCreateValue(MayCreate: true);
9801 *Value = APValue();
9802 Result.set(B: E);
9803 } else {
9804 Value = &Info.CurrentCall->createTemporary(
9805 Key: E, T: Inner->getType(),
9806 Scope: E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression
9807 : ScopeKind::Block,
9808 LV&: Result);
9809 }
9810
9811 QualType Type = Inner->getType();
9812
9813 // Materialize the temporary itself.
9814 if (!EvaluateInPlace(Result&: *Value, Info, This: Result, E: Inner)) {
9815 *Value = APValue();
9816 return false;
9817 }
9818
9819 // Adjust our lvalue to refer to the desired subobject.
9820 for (unsigned I = Adjustments.size(); I != 0; /**/) {
9821 --I;
9822 switch (Adjustments[I].Kind) {
9823 case SubobjectAdjustment::DerivedToBaseAdjustment:
9824 if (!HandleLValueBasePath(Info, E: Adjustments[I].DerivedToBase.BasePath,
9825 Type, Result))
9826 return false;
9827 Type = Adjustments[I].DerivedToBase.BasePath->getType();
9828 break;
9829
9830 case SubobjectAdjustment::FieldAdjustment:
9831 if (!HandleLValueMember(Info, E, LVal&: Result, FD: Adjustments[I].Field))
9832 return false;
9833 Type = Adjustments[I].Field->getType();
9834 break;
9835
9836 case SubobjectAdjustment::MemberPointerAdjustment:
9837 if (!HandleMemberPointerAccess(Info&: this->Info, LVType: Type, LV&: Result,
9838 RHS: Adjustments[I].Ptr.RHS))
9839 return false;
9840 Type = Adjustments[I].Ptr.MPT->getPointeeType();
9841 break;
9842 }
9843 }
9844
9845 return true;
9846}
9847
9848bool
9849LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9850 assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) &&
9851 "lvalue compound literal in c++?");
9852 APValue *Lit;
9853 // If CompountLiteral has static storage, its value can be used outside
9854 // this expression. So evaluate it once and store it in ASTContext.
9855 if (E->hasStaticStorage()) {
9856 Lit = &E->getOrCreateStaticValue(Ctx&: Info.Ctx);
9857 Result.set(B: E);
9858 // Reset any previously evaluated state, otherwise evaluation below might
9859 // fail.
9860 // FIXME: Should we just re-use the previously evaluated value instead?
9861 *Lit = APValue();
9862 } else {
9863 assert(!Info.getLangOpts().CPlusPlus);
9864 Lit = &Info.CurrentCall->createTemporary(Key: E, T: E->getInitializer()->getType(),
9865 Scope: ScopeKind::Block, LV&: Result);
9866 }
9867 // FIXME: Evaluating in place isn't always right. We should figure out how to
9868 // use appropriate evaluation context here, see
9869 // clang/test/AST/static-compound-literals-reeval.cpp for a failure.
9870 if (!EvaluateInPlace(Result&: *Lit, Info, This: Result, E: E->getInitializer())) {
9871 *Lit = APValue();
9872 return false;
9873 }
9874 return true;
9875}
9876
9877bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
9878 TypeInfoLValue TypeInfo;
9879
9880 if (!E->isPotentiallyEvaluated()) {
9881 if (E->isTypeOperand())
9882 TypeInfo = TypeInfoLValue(E->getTypeOperand(Context: Info.Ctx).getTypePtr());
9883 else
9884 TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr());
9885 } else {
9886 if (!Info.Ctx.getLangOpts().CPlusPlus20) {
9887 Info.CCEDiag(E, DiagId: diag::note_constexpr_typeid_polymorphic)
9888 << E->getExprOperand()->getType()
9889 << E->getExprOperand()->getSourceRange();
9890 }
9891
9892 if (!Visit(S: E->getExprOperand()))
9893 return false;
9894
9895 std::optional<DynamicType> DynType =
9896 ComputeDynamicType(Info, E, This&: Result, AK: AK_TypeId);
9897 if (!DynType)
9898 return false;
9899
9900 TypeInfo = TypeInfoLValue(
9901 Info.Ctx.getCanonicalTagType(TD: DynType->Type).getTypePtr());
9902 }
9903
9904 return Success(B: APValue::LValueBase::getTypeInfo(LV: TypeInfo, TypeInfo: E->getType()));
9905}
9906
9907bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
9908 return Success(B: E->getGuidDecl());
9909}
9910
9911bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
9912 // Handle static data members.
9913 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: E->getMemberDecl())) {
9914 VisitIgnoredBaseExpression(E: E->getBase());
9915 return VisitVarDecl(E, VD);
9916 }
9917
9918 // Handle static member functions.
9919 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: E->getMemberDecl())) {
9920 if (MD->isStatic()) {
9921 VisitIgnoredBaseExpression(E: E->getBase());
9922 return Success(B: MD);
9923 }
9924 }
9925
9926 // Handle non-static data members.
9927 return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
9928}
9929
9930bool LValueExprEvaluator::VisitExtVectorElementExpr(
9931 const ExtVectorElementExpr *E) {
9932 bool Success = true;
9933
9934 APValue Val;
9935 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
9936 if (!Info.noteFailure())
9937 return false;
9938 Success = false;
9939 }
9940
9941 SmallVector<uint32_t, 4> Indices;
9942 E->getEncodedElementAccess(Elts&: Indices);
9943 // FIXME: support accessing more than one element
9944 if (Indices.size() > 1)
9945 return false;
9946
9947 if (Success) {
9948 Result.setFrom(Ctx: Info.Ctx, V: Val);
9949 QualType BaseType = E->getBase()->getType();
9950 if (E->isArrow())
9951 BaseType = BaseType->getPointeeType();
9952 const auto *VT = BaseType->castAs<VectorType>();
9953 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
9954 Size: VT->getNumElements(), Idx: Indices[0]);
9955 }
9956
9957 return Success;
9958}
9959
9960bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
9961 if (E->getBase()->getType()->isSveVLSBuiltinType())
9962 return Error(E);
9963
9964 APSInt Index;
9965 bool Success = true;
9966
9967 if (const auto *VT = E->getBase()->getType()->getAs<VectorType>()) {
9968 APValue Val;
9969 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
9970 if (!Info.noteFailure())
9971 return false;
9972 Success = false;
9973 }
9974
9975 if (!EvaluateInteger(E: E->getIdx(), Result&: Index, Info)) {
9976 if (!Info.noteFailure())
9977 return false;
9978 Success = false;
9979 }
9980
9981 if (Success) {
9982 Result.setFrom(Ctx: Info.Ctx, V: Val);
9983 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
9984 Size: VT->getNumElements(), Idx: Index.getZExtValue());
9985 }
9986
9987 return Success;
9988 }
9989
9990 // C++17's rules require us to evaluate the LHS first, regardless of which
9991 // side is the base.
9992 for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) {
9993 if (SubExpr == E->getBase() ? !evaluatePointer(E: SubExpr, Result)
9994 : !EvaluateInteger(E: SubExpr, Result&: Index, Info)) {
9995 if (!Info.noteFailure())
9996 return false;
9997 Success = false;
9998 }
9999 }
10000
10001 return Success &&
10002 HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: E->getType(), Adjustment: Index);
10003}
10004
10005bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
10006 bool Success = evaluatePointer(E: E->getSubExpr(), Result);
10007 // [C++26][expr.unary.op]
10008 // If the operand points to an object or function, the result
10009 // denotes that object or function; otherwise, the behavior is undefined.
10010 // Because &(*(type*)0) is a common pattern, we do not fail the evaluation
10011 // immediately.
10012 if (!Success || !E->getType().getNonReferenceType()->isObjectType())
10013 return Success;
10014 return bool(findCompleteObject(Info, E, AK: AK_Dereference, LVal: Result,
10015 LValType: E->getType())) ||
10016 Info.noteUndefinedBehavior();
10017}
10018
10019bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
10020 if (!Visit(S: E->getSubExpr()))
10021 return false;
10022 // __real is a no-op on scalar lvalues.
10023 if (E->getSubExpr()->getType()->isAnyComplexType())
10024 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: false);
10025 return true;
10026}
10027
10028bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
10029 assert(E->getSubExpr()->getType()->isAnyComplexType() &&
10030 "lvalue __imag__ on scalar?");
10031 if (!Visit(S: E->getSubExpr()))
10032 return false;
10033 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: true);
10034 return true;
10035}
10036
10037bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) {
10038 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
10039 return Error(E: UO);
10040
10041 if (!this->Visit(S: UO->getSubExpr()))
10042 return false;
10043
10044 return handleIncDec(
10045 Info&: this->Info, E: UO, LVal: Result, LValType: UO->getSubExpr()->getType(),
10046 IsIncrement: UO->isIncrementOp(), Old: nullptr);
10047}
10048
10049bool LValueExprEvaluator::VisitCompoundAssignOperator(
10050 const CompoundAssignOperator *CAO) {
10051 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
10052 return Error(E: CAO);
10053
10054 bool Success = true;
10055
10056 // C++17 onwards require that we evaluate the RHS first.
10057 APValue RHS;
10058 if (!Evaluate(Result&: RHS, Info&: this->Info, E: CAO->getRHS())) {
10059 if (!Info.noteFailure())
10060 return false;
10061 Success = false;
10062 }
10063
10064 // The overall lvalue result is the result of evaluating the LHS.
10065 if (!this->Visit(S: CAO->getLHS()) || !Success)
10066 return false;
10067
10068 return handleCompoundAssignment(
10069 Info&: this->Info, E: CAO,
10070 LVal: Result, LValType: CAO->getLHS()->getType(), PromotedLValType: CAO->getComputationLHSType(),
10071 Opcode: CAO->getOpForCompoundAssignment(Opc: CAO->getOpcode()), RVal: RHS);
10072}
10073
10074bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
10075 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
10076 return Error(E);
10077
10078 bool Success = true;
10079
10080 // C++17 onwards require that we evaluate the RHS first.
10081 APValue NewVal;
10082 if (!Evaluate(Result&: NewVal, Info&: this->Info, E: E->getRHS())) {
10083 if (!Info.noteFailure())
10084 return false;
10085 Success = false;
10086 }
10087
10088 if (!this->Visit(S: E->getLHS()) || !Success)
10089 return false;
10090
10091 if (Info.getLangOpts().CPlusPlus20 &&
10092 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: E->getLHS(), LHS: Result))
10093 return false;
10094
10095 return handleAssignment(Info&: this->Info, E, LVal: Result, LValType: E->getLHS()->getType(),
10096 Val&: NewVal);
10097}
10098
10099//===----------------------------------------------------------------------===//
10100// Pointer Evaluation
10101//===----------------------------------------------------------------------===//
10102
10103/// Convenience function. LVal's base must be a call to an alloc_size
10104/// function.
10105static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
10106 const LValue &LVal,
10107 llvm::APInt &Result) {
10108 assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) &&
10109 "Can't get the size of a non alloc_size function");
10110 const auto *Base = LVal.getLValueBase().get<const Expr *>();
10111 const CallExpr *CE = tryUnwrapAllocSizeCall(E: Base);
10112 std::optional<llvm::APInt> Size =
10113 CE->evaluateBytesReturnedByAllocSizeCall(Ctx);
10114 if (!Size)
10115 return false;
10116
10117 Result = std::move(*Size);
10118 return true;
10119}
10120
10121/// Attempts to evaluate the given LValueBase as the result of a call to
10122/// a function with the alloc_size attribute. If it was possible to do so, this
10123/// function will return true, make Result's Base point to said function call,
10124/// and mark Result's Base as invalid.
10125static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base,
10126 LValue &Result) {
10127 if (Base.isNull())
10128 return false;
10129
10130 // Because we do no form of static analysis, we only support const variables.
10131 //
10132 // Additionally, we can't support parameters, nor can we support static
10133 // variables (in the latter case, use-before-assign isn't UB; in the former,
10134 // we have no clue what they'll be assigned to).
10135 const auto *VD =
10136 dyn_cast_or_null<VarDecl>(Val: Base.dyn_cast<const ValueDecl *>());
10137 if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified())
10138 return false;
10139
10140 const Expr *Init = VD->getAnyInitializer();
10141 if (!Init || Init->getType().isNull())
10142 return false;
10143
10144 const Expr *E = Init->IgnoreParens();
10145 if (!tryUnwrapAllocSizeCall(E))
10146 return false;
10147
10148 // Store E instead of E unwrapped so that the type of the LValue's base is
10149 // what the user wanted.
10150 Result.setInvalid(B: E);
10151
10152 QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType();
10153 Result.addUnsizedArray(Info, E, ElemTy: Pointee);
10154 return true;
10155}
10156
10157namespace {
10158class PointerExprEvaluator
10159 : public ExprEvaluatorBase<PointerExprEvaluator> {
10160 LValue &Result;
10161 bool InvalidBaseOK;
10162
10163 bool Success(const Expr *E) {
10164 Result.set(B: E);
10165 return true;
10166 }
10167
10168 bool evaluateLValue(const Expr *E, LValue &Result) {
10169 return EvaluateLValue(E, Result, Info, InvalidBaseOK);
10170 }
10171
10172 bool evaluatePointer(const Expr *E, LValue &Result) {
10173 return EvaluatePointer(E, Result, Info, InvalidBaseOK);
10174 }
10175
10176 bool visitNonBuiltinCallExpr(const CallExpr *E);
10177public:
10178
10179 PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK)
10180 : ExprEvaluatorBaseTy(info), Result(Result),
10181 InvalidBaseOK(InvalidBaseOK) {}
10182
10183 bool Success(const APValue &V, const Expr *E) {
10184 Result.setFrom(Ctx: Info.Ctx, V);
10185 return true;
10186 }
10187 bool ZeroInitialization(const Expr *E) {
10188 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10189 return true;
10190 }
10191
10192 bool VisitBinaryOperator(const BinaryOperator *E);
10193 bool VisitCastExpr(const CastExpr* E);
10194 bool VisitUnaryAddrOf(const UnaryOperator *E);
10195 bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
10196 { return Success(E); }
10197 bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
10198 if (E->isExpressibleAsConstantInitializer())
10199 return Success(E);
10200 if (Info.noteFailure())
10201 EvaluateIgnoredValue(Info, E: E->getSubExpr());
10202 return Error(E);
10203 }
10204 bool VisitObjCArrayLiteral(const ObjCArrayLiteral *E) {
10205 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
10206 }
10207 bool VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) {
10208 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
10209 }
10210 bool VisitAddrLabelExpr(const AddrLabelExpr *E)
10211 { return Success(E); }
10212 bool VisitCallExpr(const CallExpr *E);
10213 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
10214 bool VisitBlockExpr(const BlockExpr *E) {
10215 if (!E->getBlockDecl()->hasCaptures())
10216 return Success(E);
10217 return Error(E);
10218 }
10219 bool VisitCXXThisExpr(const CXXThisExpr *E) {
10220 auto DiagnoseInvalidUseOfThis = [&] {
10221 if (Info.getLangOpts().CPlusPlus11)
10222 Info.FFDiag(E, DiagId: diag::note_constexpr_this) << E->isImplicit();
10223 else
10224 Info.FFDiag(E);
10225 };
10226
10227 // Can't look at 'this' when checking a potential constant expression.
10228 if (Info.checkingPotentialConstantExpression())
10229 return false;
10230
10231 bool IsExplicitLambda =
10232 isLambdaCallWithExplicitObjectParameter(DC: Info.CurrentCall->Callee);
10233 if (!IsExplicitLambda) {
10234 if (!Info.CurrentCall->This) {
10235 DiagnoseInvalidUseOfThis();
10236 return false;
10237 }
10238
10239 Result = *Info.CurrentCall->This;
10240 }
10241
10242 if (isLambdaCallOperator(DC: Info.CurrentCall->Callee)) {
10243 // Ensure we actually have captured 'this'. If something was wrong with
10244 // 'this' capture, the error would have been previously reported.
10245 // Otherwise we can be inside of a default initialization of an object
10246 // declared by lambda's body, so no need to return false.
10247 if (!Info.CurrentCall->LambdaThisCaptureField) {
10248 if (IsExplicitLambda && !Info.CurrentCall->This) {
10249 DiagnoseInvalidUseOfThis();
10250 return false;
10251 }
10252
10253 return true;
10254 }
10255
10256 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
10257 return HandleLambdaCapture(
10258 Info, E, Result, MD, FD: Info.CurrentCall->LambdaThisCaptureField,
10259 LValueToRValueConversion: Info.CurrentCall->LambdaThisCaptureField->getType()->isPointerType());
10260 }
10261 return true;
10262 }
10263
10264 bool VisitCXXNewExpr(const CXXNewExpr *E);
10265
10266 bool VisitSourceLocExpr(const SourceLocExpr *E) {
10267 assert(!E->isIntType() && "SourceLocExpr isn't a pointer type?");
10268 APValue LValResult = E->EvaluateInContext(
10269 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
10270 Result.setFrom(Ctx: Info.Ctx, V: LValResult);
10271 return true;
10272 }
10273
10274 bool VisitEmbedExpr(const EmbedExpr *E) {
10275 llvm::report_fatal_error(reason: "Not yet implemented for ExprConstant.cpp");
10276 return true;
10277 }
10278
10279 bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) {
10280 std::string ResultStr = E->ComputeName(Context&: Info.Ctx);
10281
10282 QualType CharTy = Info.Ctx.CharTy.withConst();
10283 APInt Size(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType()),
10284 ResultStr.size() + 1);
10285 QualType ArrayTy = Info.Ctx.getConstantArrayType(
10286 EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
10287
10288 StringLiteral *SL =
10289 StringLiteral::Create(Ctx: Info.Ctx, Str: ResultStr, Kind: StringLiteralKind::Ordinary,
10290 /*Pascal*/ false, Ty: ArrayTy, Locs: E->getLocation());
10291
10292 evaluateLValue(E: SL, Result);
10293 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: ArrayTy));
10294 return true;
10295 }
10296
10297 // FIXME: Missing: @protocol, @selector
10298};
10299} // end anonymous namespace
10300
10301static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info,
10302 bool InvalidBaseOK) {
10303 assert(!E->isValueDependent());
10304 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
10305 return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
10306}
10307
10308bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
10309 if (E->getOpcode() != BO_Add &&
10310 E->getOpcode() != BO_Sub)
10311 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
10312
10313 const Expr *PExp = E->getLHS();
10314 const Expr *IExp = E->getRHS();
10315 if (IExp->getType()->isPointerType())
10316 std::swap(a&: PExp, b&: IExp);
10317
10318 bool EvalPtrOK = evaluatePointer(E: PExp, Result);
10319 if (!EvalPtrOK && !Info.noteFailure())
10320 return false;
10321
10322 llvm::APSInt Offset;
10323 if (!EvaluateInteger(E: IExp, Result&: Offset, Info) || !EvalPtrOK)
10324 return false;
10325
10326 if (E->getOpcode() == BO_Sub)
10327 negateAsSigned(Int&: Offset);
10328
10329 QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType();
10330 return HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: Pointee, Adjustment: Offset);
10331}
10332
10333bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
10334 // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*'
10335 // operator, neither operator is evaluated and the result is as if both were
10336 // omitted (except that the operators' constraints, already enforced by Sema,
10337 // still apply, and the result is not an lvalue). So '&*p' is just the pointer
10338 // value 'p' with no dereference, and forming it is therefore not undefined
10339 // behavior even when 'p' is null, e.g. '&*(int *)0'. Evaluate the pointer
10340 // operand directly so we don't spuriously diagnose a null dereference.
10341 if (!Info.getLangOpts().CPlusPlus) {
10342 const Expr *Sub = E->getSubExpr()->IgnoreParens();
10343 if (const auto *Deref = dyn_cast<UnaryOperator>(Val: Sub);
10344 Deref && Deref->getOpcode() == UO_Deref)
10345 return evaluatePointer(E: Deref->getSubExpr(), Result);
10346 }
10347 return evaluateLValue(E: E->getSubExpr(), Result);
10348}
10349
10350// Is the provided decl 'std::source_location::current'?
10351static bool IsDeclSourceLocationCurrent(const FunctionDecl *FD) {
10352 if (!FD)
10353 return false;
10354 const IdentifierInfo *FnII = FD->getIdentifier();
10355 if (!FnII || !FnII->isStr(Str: "current"))
10356 return false;
10357
10358 const auto *RD = dyn_cast<RecordDecl>(Val: FD->getParent());
10359 if (!RD)
10360 return false;
10361
10362 const IdentifierInfo *ClassII = RD->getIdentifier();
10363 return RD->isInStdNamespace() && ClassII && ClassII->isStr(Str: "source_location");
10364}
10365
10366bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
10367 const Expr *SubExpr = E->getSubExpr();
10368
10369 switch (E->getCastKind()) {
10370 default:
10371 break;
10372 case CK_BitCast:
10373 case CK_CPointerToObjCPointerCast:
10374 case CK_BlockPointerToObjCPointerCast:
10375 case CK_AnyPointerToBlockPointerCast:
10376 case CK_AddressSpaceConversion:
10377 if (!Visit(S: SubExpr))
10378 return false;
10379 if (E->getType()->isFunctionPointerType() ||
10380 SubExpr->getType()->isFunctionPointerType()) {
10381 // Casting between two function pointer types, or between a function
10382 // pointer and an object pointer, is always a reinterpret_cast.
10383 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10384 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10385 << Info.Ctx.getLangOpts().CPlusPlus;
10386 Result.Designator.setInvalid();
10387 } else if (!E->getType()->isVoidPointerType()) {
10388 // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
10389 // permitted in constant expressions in C++11. Bitcasts from cv void* are
10390 // also static_casts, but we disallow them as a resolution to DR1312.
10391 //
10392 // In some circumstances, we permit casting from void* to cv1 T*, when the
10393 // actual pointee object is actually a cv2 T.
10394 bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&
10395 !Result.IsNullPtr;
10396 bool VoidPtrCastMaybeOK =
10397 Result.IsNullPtr ||
10398 (HasValidResult &&
10399 Info.Ctx.hasSimilarType(T1: Result.Designator.getType(Ctx&: Info.Ctx),
10400 T2: E->getType()->getPointeeType()));
10401 // 1. We'll allow it in std::allocator::allocate, and anything which that
10402 // calls.
10403 // 2. HACK 2022-03-28: Work around an issue with libstdc++'s
10404 // <source_location> header. Fixed in GCC 12 and later (2022-04-??).
10405 // We'll allow it in the body of std::source_location::current. GCC's
10406 // implementation had a parameter of type `void*`, and casts from
10407 // that back to `const __impl*` in its body.
10408 if (VoidPtrCastMaybeOK &&
10409 (Info.getStdAllocatorCaller(FnName: "allocate") ||
10410 IsDeclSourceLocationCurrent(FD: Info.CurrentCall->Callee) ||
10411 Info.getLangOpts().CPlusPlus26)) {
10412 // Permitted.
10413 } else {
10414 if (SubExpr->getType()->isVoidPointerType() &&
10415 Info.getLangOpts().CPlusPlus) {
10416 if (HasValidResult)
10417 CCEDiag(E, D: diag::note_constexpr_invalid_void_star_cast)
10418 << SubExpr->getType() << Info.getLangOpts().CPlusPlus26
10419 << Result.Designator.getType(Ctx&: Info.Ctx).getCanonicalType()
10420 << E->getType()->getPointeeType();
10421 else
10422 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10423 << diag::ConstexprInvalidCastKind::CastFrom
10424 << SubExpr->getType();
10425 } else
10426 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10427 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10428 << Info.Ctx.getLangOpts().CPlusPlus;
10429 Result.Designator.setInvalid();
10430 }
10431 }
10432 if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr)
10433 ZeroInitialization(E);
10434 return true;
10435
10436 case CK_DerivedToBase:
10437 case CK_UncheckedDerivedToBase:
10438 if (!evaluatePointer(E: E->getSubExpr(), Result))
10439 return false;
10440 if (!Result.Base && Result.Offset.isZero())
10441 return true;
10442
10443 // Now figure out the necessary offset to add to the base LV to get from
10444 // the derived class to the base class.
10445 return HandleLValueBasePath(Info, E, Type: E->getSubExpr()->getType()->
10446 castAs<PointerType>()->getPointeeType(),
10447 Result);
10448
10449 case CK_BaseToDerived:
10450 if (!Visit(S: E->getSubExpr()))
10451 return false;
10452 if (!Result.Base && Result.Offset.isZero())
10453 return true;
10454 return HandleBaseToDerivedCast(Info, E, Result);
10455
10456 case CK_Dynamic:
10457 if (!Visit(S: E->getSubExpr()))
10458 return false;
10459 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
10460
10461 case CK_NullToPointer:
10462 VisitIgnoredValue(E: E->getSubExpr());
10463 return ZeroInitialization(E);
10464
10465 case CK_IntegralToPointer: {
10466 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10467 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10468 << Info.Ctx.getLangOpts().CPlusPlus;
10469
10470 APValue Value;
10471 if (!EvaluateIntegerOrLValue(E: SubExpr, Result&: Value, Info))
10472 break;
10473
10474 if (Value.isInt()) {
10475 unsigned Size = Info.Ctx.getTypeSize(T: E->getType());
10476 uint64_t N = Value.getInt().extOrTrunc(width: Size).getZExtValue();
10477 if (N == Info.Ctx.getTargetNullPointerValue(QT: E->getType())) {
10478 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10479 } else {
10480 Result.Base = (Expr *)nullptr;
10481 Result.InvalidBase = false;
10482 Result.Offset = CharUnits::fromQuantity(Quantity: N);
10483 Result.Designator.setInvalid();
10484 Result.IsNullPtr = false;
10485 }
10486 return true;
10487 } else {
10488 // In rare instances, the value isn't an lvalue.
10489 // For example, when the value is the difference between the addresses of
10490 // two labels. We reject that as a constant expression because we can't
10491 // compute a valid offset to convert into a pointer.
10492 if (!Value.isLValue())
10493 return false;
10494
10495 // Cast is of an lvalue, no need to change value.
10496 Result.setFrom(Ctx: Info.Ctx, V: Value);
10497 return true;
10498 }
10499 }
10500
10501 case CK_ArrayToPointerDecay: {
10502 if (SubExpr->isGLValue()) {
10503 if (!evaluateLValue(E: SubExpr, Result))
10504 return false;
10505 } else {
10506 APValue &Value = Info.CurrentCall->createTemporary(
10507 Key: SubExpr, T: SubExpr->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
10508 if (!EvaluateInPlace(Result&: Value, Info, This: Result, E: SubExpr))
10509 return false;
10510 }
10511 // The result is a pointer to the first element of the array.
10512 auto *AT = Info.Ctx.getAsArrayType(T: SubExpr->getType());
10513 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT))
10514 Result.addArray(Info, E, CAT);
10515 else
10516 Result.addUnsizedArray(Info, E, ElemTy: AT->getElementType());
10517 return true;
10518 }
10519
10520 case CK_FunctionToPointerDecay:
10521 return evaluateLValue(E: SubExpr, Result);
10522
10523 case CK_LValueToRValue: {
10524 LValue LVal;
10525 if (!evaluateLValue(E: E->getSubExpr(), Result&: LVal))
10526 return false;
10527
10528 APValue RVal;
10529 // Note, we use the subexpression's type in order to retain cv-qualifiers.
10530 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
10531 LVal, RVal))
10532 return InvalidBaseOK &&
10533 evaluateLValueAsAllocSize(Info, Base: LVal.Base, Result);
10534 return Success(V: RVal, E);
10535 }
10536 }
10537
10538 return ExprEvaluatorBaseTy::VisitCastExpr(E);
10539}
10540
10541static CharUnits GetAlignOfType(const ASTContext &Ctx, QualType T,
10542 UnaryExprOrTypeTrait ExprKind) {
10543 // C++ [expr.alignof]p3:
10544 // When alignof is applied to a reference type, the result is the
10545 // alignment of the referenced type.
10546 T = T.getNonReferenceType();
10547
10548 if (T.getQualifiers().hasUnaligned())
10549 return CharUnits::One();
10550
10551 const bool AlignOfReturnsPreferred =
10552 Ctx.getLangOpts().isCompatibleWith(Version: LangOptions::ClangABI::Ver7);
10553
10554 // __alignof is defined to return the preferred alignment.
10555 // Before 8, clang returned the preferred alignment for alignof and _Alignof
10556 // as well.
10557 if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
10558 return Ctx.toCharUnitsFromBits(BitSize: Ctx.getPreferredTypeAlign(T: T.getTypePtr()));
10559 // alignof and _Alignof are defined to return the ABI alignment.
10560 else if (ExprKind == UETT_AlignOf)
10561 return Ctx.getTypeAlignInChars(T: T.getTypePtr());
10562 else
10563 llvm_unreachable("GetAlignOfType on a non-alignment ExprKind");
10564}
10565
10566// Convert a builtin ID to the canonical x86 builtin ID the constant evaluators
10567// dispatch on in their x86 target-specific cases, or 0 if \p BuiltinOp is a
10568// target builtin those cases should not handle.
10569//
10570// Target-independent builtins are returned unchanged. Target builtin IDs of
10571// different targets overlap (each target numbers its builtins from
10572// Builtin::FirstTSBuiltin), so a target builtin ID is only meaningful for the
10573// target that owns it. Determine the owning target (translating an auxiliary ID
10574// back to its canonical value) and only return the ID when x86 owns it;
10575// otherwise an overlapping ID could be misinterpreted as an unrelated x86
10576// builtin.
10577unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
10578 unsigned BuiltinOp) {
10579 // Target-independent builtins have the same ID regardless of the target, so
10580 // they can be dispatched as-is. This is the common case and is intentionally
10581 // kept to a single comparison so callers can use this on hot paths (e.g. the
10582 // bytecode interpreter's builtin dispatch) without re-deriving the ID from
10583 // the call expression.
10584 if (BuiltinOp < Builtin::FirstTSBuiltin)
10585 return BuiltinOp;
10586
10587 // Determine the target that owns this builtin, translating an auxiliary ID
10588 // back to its canonical value.
10589 const TargetInfo *OwningTarget;
10590 if (Ctx.BuiltinInfo.isAuxBuiltinID(ID: BuiltinOp)) {
10591 OwningTarget = Ctx.getAuxTargetInfo();
10592 BuiltinOp = Ctx.BuiltinInfo.getAuxBuiltinID(ID: BuiltinOp);
10593 } else {
10594 OwningTarget = &Ctx.getTargetInfo();
10595 }
10596
10597 if (!OwningTarget)
10598 return 0;
10599
10600 // x86 and x86_64 share a single builtin set and are the only architectures
10601 // whose target-specific builtins the constant evaluators currently fold.
10602 switch (OwningTarget->getTriple().getArch()) {
10603 case llvm::Triple::x86:
10604 case llvm::Triple::x86_64:
10605 return BuiltinOp;
10606 default:
10607 return 0;
10608 }
10609}
10610
10611unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
10612 const CallExpr *E) {
10613 return ConvertBuiltinIDToX86BuiltinID(Ctx, BuiltinOp: E->getBuiltinCallee());
10614}
10615
10616CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E,
10617 UnaryExprOrTypeTrait ExprKind) {
10618 E = E->IgnoreParens();
10619
10620 // The kinds of expressions that we have special-case logic here for
10621 // should be kept up to date with the special checks for those
10622 // expressions in Sema.
10623
10624 // alignof decl is always accepted, even if it doesn't make sense: we default
10625 // to 1 in those cases.
10626 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E))
10627 return Ctx.getDeclAlign(D: DRE->getDecl(),
10628 /*RefAsPointee*/ ForAlignof: true);
10629
10630 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: E))
10631 return Ctx.getDeclAlign(D: ME->getMemberDecl(),
10632 /*RefAsPointee*/ ForAlignof: true);
10633
10634 return GetAlignOfType(Ctx, T: E->getType(), ExprKind);
10635}
10636
10637static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) {
10638 if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>())
10639 return Info.Ctx.getDeclAlign(D: VD);
10640 if (const auto *E = Value.Base.dyn_cast<const Expr *>())
10641 return GetAlignOfExpr(Ctx: Info.Ctx, E, ExprKind: UETT_AlignOf);
10642 if (const auto &DA = Value.Base.dyn_cast<DynamicAllocLValue>())
10643 return GetAlignOfDynamicAlloc(Ctx: Info.getASTContext(), AllocType: Value.Base.getType(),
10644 AllocKind: DA.getAllocKind());
10645 return GetAlignOfType(Ctx: Info.Ctx, T: Value.Base.getTypeInfoType(), ExprKind: UETT_AlignOf);
10646}
10647
10648/// Evaluate the value of the alignment argument to __builtin_align_{up,down},
10649/// __builtin_is_aligned and __builtin_assume_aligned.
10650static bool getAlignmentArgument(const Expr *E, QualType ForType,
10651 EvalInfo &Info, APSInt &Alignment) {
10652 if (!EvaluateInteger(E, Result&: Alignment, Info))
10653 return false;
10654 if (Alignment < 0 || !Alignment.isPowerOf2()) {
10655 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_alignment) << Alignment;
10656 return false;
10657 }
10658 unsigned SrcWidth = Info.Ctx.getIntWidth(T: ForType);
10659 APSInt MaxValue(APInt::getOneBitSet(numBits: SrcWidth, BitNo: SrcWidth - 1));
10660 if (APSInt::compareValues(I1: Alignment, I2: MaxValue) > 0) {
10661 Info.FFDiag(E, DiagId: diag::note_constexpr_alignment_too_big)
10662 << MaxValue << ForType << Alignment;
10663 return false;
10664 }
10665 // Ensure both alignment and source value have the same bit width so that we
10666 // don't assert when computing the resulting value.
10667 APSInt ExtAlignment =
10668 APSInt(Alignment.zextOrTrunc(width: SrcWidth), /*isUnsigned=*/true);
10669 assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 &&
10670 "Alignment should not be changed by ext/trunc");
10671 Alignment = ExtAlignment;
10672 assert(Alignment.getBitWidth() == SrcWidth);
10673 return true;
10674}
10675
10676// To be clear: this happily visits unsupported builtins. Better name welcomed.
10677bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {
10678 if (ExprEvaluatorBaseTy::VisitCallExpr(E))
10679 return true;
10680
10681 if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr()))
10682 return false;
10683
10684 Result.setInvalid(B: E);
10685 QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType();
10686 Result.addUnsizedArray(Info, E, ElemTy: PointeeTy);
10687 return true;
10688}
10689
10690bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
10691 if (!IsConstantEvaluatedBuiltinCall(E))
10692 return visitNonBuiltinCallExpr(E);
10693 return VisitBuiltinCallExpr(E, BuiltinOp: ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E));
10694}
10695
10696// Determine if T is a character type for which we guarantee that
10697// sizeof(T) == 1.
10698static bool isOneByteCharacterType(QualType T) {
10699 return T->isCharType() || T->isChar8Type();
10700}
10701
10702bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
10703 unsigned BuiltinOp) {
10704 if (isOpaqueConstantCall(E))
10705 return Success(E);
10706
10707 switch (BuiltinOp) {
10708 case Builtin::BIaddressof:
10709 case Builtin::BI__addressof:
10710 case Builtin::BI__builtin_addressof:
10711 return evaluateLValue(E: E->getArg(Arg: 0), Result);
10712 case Builtin::BI__builtin_assume_aligned: {
10713 // We need to be very careful here because: if the pointer does not have the
10714 // asserted alignment, then the behavior is undefined, and undefined
10715 // behavior is non-constant.
10716 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10717 return false;
10718
10719 LValue OffsetResult(Result);
10720 APSInt Alignment;
10721 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10722 Alignment))
10723 return false;
10724 CharUnits Align = CharUnits::fromQuantity(Quantity: Alignment.getZExtValue());
10725
10726 if (E->getNumArgs() > 2) {
10727 APSInt Offset;
10728 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Offset, Info))
10729 return false;
10730
10731 int64_t AdditionalOffset = -Offset.getZExtValue();
10732 OffsetResult.Offset += CharUnits::fromQuantity(Quantity: AdditionalOffset);
10733 }
10734
10735 // If there is a base object, then it must have the correct alignment.
10736 if (OffsetResult.Base) {
10737 CharUnits BaseAlignment = getBaseAlignment(Info, Value: OffsetResult);
10738
10739 if (BaseAlignment < Align) {
10740 Result.Designator.setInvalid();
10741 CCEDiag(E: E->getArg(Arg: 0), D: diag::note_constexpr_baa_insufficient_alignment)
10742 << 0 << BaseAlignment.getQuantity() << Align.getQuantity();
10743 return false;
10744 }
10745 }
10746
10747 // The offset must also have the correct alignment.
10748 if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) {
10749 Result.Designator.setInvalid();
10750
10751 (OffsetResult.Base
10752 ? CCEDiag(E: E->getArg(Arg: 0),
10753 D: diag::note_constexpr_baa_insufficient_alignment)
10754 << 1
10755 : CCEDiag(E: E->getArg(Arg: 0),
10756 D: diag::note_constexpr_baa_value_insufficient_alignment))
10757 << OffsetResult.Offset.getQuantity() << Align.getQuantity();
10758 return false;
10759 }
10760
10761 return true;
10762 }
10763 case Builtin::BI__builtin_align_up:
10764 case Builtin::BI__builtin_align_down: {
10765 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10766 return false;
10767 APSInt Alignment;
10768 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10769 Alignment))
10770 return false;
10771
10772 if (!Result.Base) {
10773 // Null pointers are always aligned and align_up/align_down preserve null.
10774 if (Result.Offset.isZero())
10775 return true;
10776
10777 // Non-null pointers without a base (for example, integer-to-pointer
10778 // casts such as (void *)32) do not have enough information to perform
10779 // pointer arithmetic during constant evaluation.
10780 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_adjust)
10781 << Alignment;
10782 return false;
10783 }
10784
10785 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Result);
10786 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Result.Offset);
10787 // For align_up/align_down, we can return the same value if the alignment
10788 // is known to be greater or equal to the requested value.
10789 if (PtrAlign.getQuantity() >= Alignment)
10790 return true;
10791
10792 // The alignment could be greater than the minimum at run-time, so we cannot
10793 // infer much about the resulting pointer value. One case is possible:
10794 // For `_Alignas(32) char buf[N]; __builtin_align_down(&buf[idx], 32)` we
10795 // can infer the correct index if the requested alignment is smaller than
10796 // the base alignment so we can perform the computation on the offset.
10797 if (BaseAlignment.getQuantity() >= Alignment) {
10798 assert(Alignment.getBitWidth() <= 64 &&
10799 "Cannot handle > 64-bit address-space");
10800 uint64_t Alignment64 = Alignment.getZExtValue();
10801 CharUnits NewOffset = CharUnits::fromQuantity(
10802 Quantity: BuiltinOp == Builtin::BI__builtin_align_down
10803 ? llvm::alignDown(Value: Result.Offset.getQuantity(), Align: Alignment64)
10804 : llvm::alignTo(Value: Result.Offset.getQuantity(), Align: Alignment64));
10805 Result.adjustOffset(N: NewOffset - Result.Offset);
10806 // TODO: diagnose out-of-bounds values/only allow for arrays?
10807 return true;
10808 }
10809 // Otherwise, we cannot constant-evaluate the result.
10810 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_adjust)
10811 << Alignment;
10812 return false;
10813 }
10814 case Builtin::BI__builtin_operator_new:
10815 return HandleOperatorNewCall(Info, E, Result);
10816 case Builtin::BI__builtin_launder:
10817 return evaluatePointer(E: E->getArg(Arg: 0), Result);
10818 case Builtin::BIstrchr:
10819 case Builtin::BIwcschr:
10820 case Builtin::BImemchr:
10821 case Builtin::BIwmemchr:
10822 if (Info.getLangOpts().CPlusPlus11)
10823 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10824 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10825 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10826 else
10827 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10828 [[fallthrough]];
10829 case Builtin::BI__builtin_strchr:
10830 case Builtin::BI__builtin_wcschr:
10831 case Builtin::BI__builtin_memchr:
10832 case Builtin::BI__builtin_char_memchr:
10833 case Builtin::BI__builtin_wmemchr: {
10834 if (!Visit(S: E->getArg(Arg: 0)))
10835 return false;
10836 APSInt Desired;
10837 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Desired, Info))
10838 return false;
10839 uint64_t MaxLength = uint64_t(-1);
10840 if (BuiltinOp != Builtin::BIstrchr &&
10841 BuiltinOp != Builtin::BIwcschr &&
10842 BuiltinOp != Builtin::BI__builtin_strchr &&
10843 BuiltinOp != Builtin::BI__builtin_wcschr) {
10844 APSInt N;
10845 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10846 return false;
10847 MaxLength = N.getZExtValue();
10848 }
10849 // We cannot find the value if there are no candidates to match against.
10850 if (MaxLength == 0u)
10851 return ZeroInitialization(E);
10852 if (!Result.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
10853 Result.Designator.Invalid)
10854 return false;
10855 QualType CharTy = Result.Designator.getType(Ctx&: Info.Ctx);
10856 bool IsRawByte = BuiltinOp == Builtin::BImemchr ||
10857 BuiltinOp == Builtin::BI__builtin_memchr;
10858 assert(IsRawByte ||
10859 Info.Ctx.hasSameUnqualifiedType(
10860 CharTy, E->getArg(0)->getType()->getPointeeType()));
10861 // Pointers to const void may point to objects of incomplete type.
10862 if (IsRawByte && CharTy->isIncompleteType()) {
10863 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_incomplete_type) << CharTy;
10864 return false;
10865 }
10866 // Give up on byte-oriented matching against multibyte elements.
10867 // FIXME: We can compare the bytes in the correct order.
10868 if (IsRawByte && !isOneByteCharacterType(T: CharTy)) {
10869 Info.FFDiag(E, DiagId: diag::note_constexpr_memchr_unsupported)
10870 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy;
10871 return false;
10872 }
10873 // Figure out what value we're actually looking for (after converting to
10874 // the corresponding unsigned type if necessary).
10875 uint64_t DesiredVal;
10876 bool StopAtNull = false;
10877 switch (BuiltinOp) {
10878 case Builtin::BIstrchr:
10879 case Builtin::BI__builtin_strchr:
10880 // strchr compares directly to the passed integer, and therefore
10881 // always fails if given an int that is not a char.
10882 if (!APSInt::isSameValue(I1: HandleIntToIntCast(Info, E, DestType: CharTy,
10883 SrcType: E->getArg(Arg: 1)->getType(),
10884 Value: Desired),
10885 I2: Desired))
10886 return ZeroInitialization(E);
10887 StopAtNull = true;
10888 [[fallthrough]];
10889 case Builtin::BImemchr:
10890 case Builtin::BI__builtin_memchr:
10891 case Builtin::BI__builtin_char_memchr:
10892 // memchr compares by converting both sides to unsigned char. That's also
10893 // correct for strchr if we get this far (to cope with plain char being
10894 // unsigned in the strchr case).
10895 DesiredVal = Desired.trunc(width: Info.Ctx.getCharWidth()).getZExtValue();
10896 break;
10897
10898 case Builtin::BIwcschr:
10899 case Builtin::BI__builtin_wcschr:
10900 StopAtNull = true;
10901 [[fallthrough]];
10902 case Builtin::BIwmemchr:
10903 case Builtin::BI__builtin_wmemchr:
10904 // wcschr and wmemchr are given a wchar_t to look for. Just use it.
10905 DesiredVal = Desired.getZExtValue();
10906 break;
10907 }
10908
10909 for (; MaxLength; --MaxLength) {
10910 APValue Char;
10911 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: Result, RVal&: Char) ||
10912 !Char.isInt())
10913 return false;
10914 if (Char.getInt().getZExtValue() == DesiredVal)
10915 return true;
10916 if (StopAtNull && !Char.getInt())
10917 break;
10918 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: CharTy, Adjustment: 1))
10919 return false;
10920 }
10921 // Not found: return nullptr.
10922 return ZeroInitialization(E);
10923 }
10924
10925 case Builtin::BImemcpy:
10926 case Builtin::BImemmove:
10927 case Builtin::BIwmemcpy:
10928 case Builtin::BIwmemmove:
10929 if (Info.getLangOpts().CPlusPlus11)
10930 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10931 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10932 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10933 else
10934 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10935 [[fallthrough]];
10936 case Builtin::BI__builtin_memcpy:
10937 case Builtin::BI__builtin_memmove:
10938 case Builtin::BI__builtin_wmemcpy:
10939 case Builtin::BI__builtin_wmemmove: {
10940 bool WChar = BuiltinOp == Builtin::BIwmemcpy ||
10941 BuiltinOp == Builtin::BIwmemmove ||
10942 BuiltinOp == Builtin::BI__builtin_wmemcpy ||
10943 BuiltinOp == Builtin::BI__builtin_wmemmove;
10944 bool Move = BuiltinOp == Builtin::BImemmove ||
10945 BuiltinOp == Builtin::BIwmemmove ||
10946 BuiltinOp == Builtin::BI__builtin_memmove ||
10947 BuiltinOp == Builtin::BI__builtin_wmemmove;
10948
10949 // The result of mem* is the first argument.
10950 if (!Visit(S: E->getArg(Arg: 0)))
10951 return false;
10952 LValue Dest = Result;
10953
10954 LValue Src;
10955 if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Src, Info))
10956 return false;
10957
10958 APSInt N;
10959 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10960 return false;
10961 assert(!N.isSigned() && "memcpy and friends take an unsigned size");
10962
10963 // If the size is zero, we treat this as always being a valid no-op.
10964 // (Even if one of the src and dest pointers is null.)
10965 if (!N)
10966 return true;
10967
10968 // Otherwise, if either of the operands is null, we can't proceed. Don't
10969 // try to determine the type of the copied objects, because there aren't
10970 // any.
10971 if (!Src.Base || !Dest.Base) {
10972 APValue Val;
10973 (!Src.Base ? Src : Dest).moveInto(V&: Val);
10974 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_null)
10975 << Move << WChar << !!Src.Base
10976 << Val.getAsString(Ctx: Info.Ctx, Ty: E->getArg(Arg: 0)->getType());
10977 return false;
10978 }
10979 if (Src.Designator.Invalid || Dest.Designator.Invalid)
10980 return false;
10981
10982 // We require that Src and Dest are both pointers to arrays of
10983 // trivially-copyable type. (For the wide version, the designator will be
10984 // invalid if the designated object is not a wchar_t.)
10985 QualType T = Dest.Designator.getType(Ctx&: Info.Ctx);
10986 QualType SrcT = Src.Designator.getType(Ctx&: Info.Ctx);
10987 if (!Info.Ctx.hasSameUnqualifiedType(T1: T, T2: SrcT)) {
10988 // FIXME: Consider using our bit_cast implementation to support this.
10989 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T;
10990 return false;
10991 }
10992 if (T->isIncompleteType()) {
10993 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_incomplete_type) << Move << T;
10994 return false;
10995 }
10996 if (!T.isTriviallyCopyableType(Context: Info.Ctx)) {
10997 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_nontrivial) << Move << T;
10998 return false;
10999 }
11000
11001 // Figure out how many T's we're copying.
11002 uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity();
11003 if (TSize == 0)
11004 return false;
11005 if (!WChar) {
11006 uint64_t Remainder;
11007 llvm::APInt OrigN = N;
11008 llvm::APInt::udivrem(LHS: OrigN, RHS: TSize, Quotient&: N, Remainder);
11009 if (Remainder) {
11010 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
11011 << Move << WChar << 0 << T << toString(I: OrigN, Radix: 10, /*Signed*/false)
11012 << (unsigned)TSize;
11013 return false;
11014 }
11015 }
11016
11017 // Check that the copying will remain within the arrays, just so that we
11018 // can give a more meaningful diagnostic. This implicitly also checks that
11019 // N fits into 64 bits.
11020 uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second;
11021 uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second;
11022 if (N.ugt(RHS: RemainingSrcSize) || N.ugt(RHS: RemainingDestSize)) {
11023 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
11024 << Move << WChar << (N.ugt(RHS: RemainingSrcSize) ? 1 : 2) << T
11025 << toString(I: N, Radix: 10, /*Signed*/false);
11026 return false;
11027 }
11028 uint64_t NElems = N.getZExtValue();
11029 uint64_t NBytes = NElems * TSize;
11030
11031 // Check for overlap.
11032 int Direction = 1;
11033 if (HasSameBase(A: Src, B: Dest)) {
11034 uint64_t SrcOffset = Src.getLValueOffset().getQuantity();
11035 uint64_t DestOffset = Dest.getLValueOffset().getQuantity();
11036 if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) {
11037 // Dest is inside the source region.
11038 if (!Move) {
11039 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
11040 return false;
11041 }
11042 // For memmove and friends, copy backwards.
11043 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: NElems - 1) ||
11044 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: NElems - 1))
11045 return false;
11046 Direction = -1;
11047 } else if (!Move && SrcOffset >= DestOffset &&
11048 SrcOffset - DestOffset < NBytes) {
11049 // Src is inside the destination region for memcpy: invalid.
11050 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
11051 return false;
11052 }
11053 }
11054
11055 while (true) {
11056 APValue Val;
11057 // FIXME: Set WantObjectRepresentation to true if we're copying a
11058 // char-like type?
11059 if (!handleLValueToRValueConversion(Info, Conv: E, Type: T, LVal: Src, RVal&: Val) ||
11060 !handleAssignment(Info, E, LVal: Dest, LValType: T, Val))
11061 return false;
11062 // Do not iterate past the last element; if we're copying backwards, that
11063 // might take us off the start of the array.
11064 if (--NElems == 0)
11065 return true;
11066 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: Direction) ||
11067 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: Direction))
11068 return false;
11069 }
11070 }
11071
11072 default:
11073 return false;
11074 }
11075}
11076
11077static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
11078 APValue &Result, const InitListExpr *ILE,
11079 QualType AllocType);
11080static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
11081 APValue &Result,
11082 const CXXConstructExpr *CCE,
11083 QualType AllocType);
11084
11085bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {
11086 if (!Info.getLangOpts().CPlusPlus20)
11087 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
11088
11089 // We cannot speculatively evaluate a delete expression.
11090 if (Info.SpeculativeEvaluationDepth)
11091 return false;
11092
11093 FunctionDecl *OperatorNew = E->getOperatorNew();
11094 QualType AllocType = E->getAllocatedType();
11095 QualType TargetType = AllocType;
11096
11097 bool IsNothrow = false;
11098 bool IsPlacement = false;
11099
11100 // The only new-placement list we support (other than the reserved placement
11101 // form) is of the form (std::nothrow).
11102 //
11103 // FIXME: There is no restriction on this, but it's not clear that any
11104 // other form makes any sense. We get here for cases such as:
11105 //
11106 // new (std::align_val_t{N}) X(int)
11107 //
11108 // (which should presumably be valid only if N is a multiple of
11109 // alignof(int), and in any case can't be deallocated unless N is
11110 // alignof(X) and X has new-extended alignment).
11111 bool HasNothrowArg = E->getNumPlacementArgs() == 1 &&
11112 E->getPlacementArg(I: 0)->getType()->isNothrowT();
11113
11114 if (OperatorNew->isReservedGlobalPlacementOperator()) {
11115 if (Info.CurrentCall->isStdFunction() || Info.getLangOpts().CPlusPlus26 ||
11116 (Info.CurrentCall->CanEvalMSConstexpr &&
11117 OperatorNew->hasAttr<MSConstexprAttr>())) {
11118 if (!EvaluatePointer(E: E->getPlacementArg(I: 0), Result, Info))
11119 return false;
11120 if (Result.Designator.Invalid)
11121 return false;
11122 TargetType = E->getPlacementArg(I: 0)->getType();
11123 IsPlacement = true;
11124 } else {
11125 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
11126 << /*C++26 feature*/ 1 << E->getSourceRange();
11127 return false;
11128 }
11129 } else if (E->getNumPlacementArgs() && !HasNothrowArg) {
11130 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
11131 << /*Unsupported*/ 0 << E->getSourceRange();
11132 return false;
11133 } else if (!OperatorNew
11134 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
11135 // [expr.const] only permits new-expressions that select a replaceable
11136 // global allocation function. Check this before evaluating a
11137 // (std::nothrow) placement argument.
11138 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
11139 << isa<CXXMethodDecl>(Val: OperatorNew) << OperatorNew;
11140 return false;
11141 } else if (HasNothrowArg) {
11142 LValue Nothrow;
11143 if (!EvaluateLValue(E: E->getPlacementArg(I: 0), Result&: Nothrow, Info))
11144 return false;
11145 IsNothrow = true;
11146 }
11147
11148 const Expr *Init = E->getInitializer();
11149 const InitListExpr *ResizedArrayILE = nullptr;
11150 const CXXConstructExpr *ResizedArrayCCE = nullptr;
11151 bool ValueInit = false;
11152
11153 if (std::optional<const Expr *> ArraySize = E->getArraySize()) {
11154 const Expr *Stripped = *ArraySize;
11155 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped);
11156 Stripped = ICE->getSubExpr())
11157 if (ICE->getCastKind() != CK_NoOp &&
11158 ICE->getCastKind() != CK_IntegralCast)
11159 break;
11160
11161 llvm::APSInt ArrayBound;
11162 if (!EvaluateInteger(E: Stripped, Result&: ArrayBound, Info))
11163 return false;
11164
11165 // C++ [expr.new]p9:
11166 // The expression is erroneous if:
11167 // -- [...] its value before converting to size_t [or] applying the
11168 // second standard conversion sequence is less than zero
11169 if (ArrayBound.isSigned() && ArrayBound.isNegative()) {
11170 if (IsNothrow)
11171 return ZeroInitialization(E);
11172
11173 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_negative)
11174 << ArrayBound << (*ArraySize)->getSourceRange();
11175 return false;
11176 }
11177
11178 // -- its value is such that the size of the allocated object would
11179 // exceed the implementation-defined limit
11180 if (!Info.CheckArraySize(Loc: ArraySize.value()->getExprLoc(),
11181 BitWidth: ConstantArrayType::getNumAddressingBits(
11182 Context: Info.Ctx, ElementType: AllocType, NumElements: ArrayBound),
11183 ElemCount: ArrayBound.getZExtValue(), /*Diag=*/!IsNothrow)) {
11184 if (IsNothrow)
11185 return ZeroInitialization(E);
11186 return false;
11187 }
11188
11189 // -- the new-initializer is a braced-init-list and the number of
11190 // array elements for which initializers are provided [...]
11191 // exceeds the number of elements to initialize
11192 if (!Init) {
11193 // No initialization is performed.
11194 } else if (isa<CXXScalarValueInitExpr>(Val: Init) ||
11195 isa<ImplicitValueInitExpr>(Val: Init)) {
11196 ValueInit = true;
11197 } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Val: Init)) {
11198 ResizedArrayCCE = CCE;
11199 } else {
11200 auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType());
11201 assert(CAT && "unexpected type for array initializer");
11202
11203 unsigned Bits =
11204 std::max(a: CAT->getSizeBitWidth(), b: ArrayBound.getBitWidth());
11205 llvm::APInt InitBound = CAT->getSize().zext(width: Bits);
11206 llvm::APInt AllocBound = ArrayBound.zext(width: Bits);
11207 if (InitBound.ugt(RHS: AllocBound)) {
11208 if (IsNothrow)
11209 return ZeroInitialization(E);
11210
11211 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_too_small)
11212 << toString(I: AllocBound, Radix: 10, /*Signed=*/false)
11213 << toString(I: InitBound, Radix: 10, /*Signed=*/false)
11214 << (*ArraySize)->getSourceRange();
11215 return false;
11216 }
11217
11218 // If the sizes differ, we must have an initializer list, and we need
11219 // special handling for this case when we initialize.
11220 if (InitBound != AllocBound)
11221 ResizedArrayILE = cast<InitListExpr>(Val: Init);
11222 }
11223
11224 AllocType = Info.Ctx.getConstantArrayType(EltTy: AllocType, ArySize: ArrayBound, SizeExpr: nullptr,
11225 ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
11226 } else if (E->isArray()) {
11227 // We have an array new-expression whose array size could not be
11228 // determined, e.g. 'new int[]()', where the bound is neither given nor
11229 // deducible from the initializer. This is ill-formed and already
11230 // diagnosed, so bail out rather than mis-evaluating a scalar allocation
11231 // as an array (which would later crash the evaluator).
11232 return false;
11233 } else {
11234 assert(!AllocType->isArrayType() &&
11235 "array allocation with non-array new");
11236 }
11237
11238 APValue *Val;
11239 if (IsPlacement) {
11240 AccessKinds AK = AK_Construct;
11241 struct FindObjectHandler {
11242 EvalInfo &Info;
11243 const Expr *E;
11244 QualType AllocType;
11245 const AccessKinds AccessKind;
11246 APValue *Value;
11247
11248 typedef bool result_type;
11249 bool failed() { return false; }
11250 bool checkConst(QualType QT) {
11251 if (QT.isConstQualified()) {
11252 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
11253 return false;
11254 }
11255 return true;
11256 }
11257 bool found(APValue &Subobj, QualType SubobjType,
11258 APValue::LValueBase Base) {
11259 if (!checkConst(QT: SubobjType))
11260 return false;
11261 // FIXME: Reject the cases where [basic.life]p8 would not permit the
11262 // old name of the object to be used to name the new object.
11263 if (!Info.Ctx.hasSimilarType(T1: SubobjType, T2: AllocType)) {
11264 Info.FFDiag(E, DiagId: diag::note_constexpr_placement_new_wrong_type)
11265 << SubobjType << AllocType;
11266 return false;
11267 }
11268 Value = &Subobj;
11269 return true;
11270 }
11271 bool found(APSInt &Value, QualType SubobjType) {
11272 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
11273 return false;
11274 }
11275 bool found(APFloat &Value, QualType SubobjType) {
11276 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
11277 return false;
11278 }
11279 } Handler = {.Info: Info, .E: E, .AllocType: AllocType, .AccessKind: AK, .Value: nullptr};
11280
11281 if (AllocType->isArrayType() &&
11282 Result.Designator.MostDerivedIsArrayElement &&
11283 Result.Designator.Entries.back().getAsArrayIndex() == 0) {
11284 // The destination of placement new is pointing to the first element
11285 // of an array. There's a special case in [expr.const]: "[...] if T is an
11286 // array type, to the first element of such an object [...]". Handle
11287 // that case here by dropping the last entry in the designator list.
11288 QualType AllocElementType =
11289 Info.Ctx.getAsArrayType(T: AllocType)->getElementType();
11290 if (Info.Ctx.hasSimilarType(T1: AllocElementType,
11291 T2: Result.Designator.MostDerivedType)) {
11292 Result.Designator.truncate(Ctx&: Info.Ctx, Base: Result.Base,
11293 NewLength: Result.Designator.MostDerivedPathLength - 1);
11294 }
11295 }
11296
11297 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: Result, LValType: AllocType);
11298 if (!Obj || !findSubobject(Info, E, Obj, Sub: Result.Designator, handler&: Handler))
11299 return false;
11300
11301 Val = Handler.Value;
11302
11303 // [basic.life]p1:
11304 // The lifetime of an object o of type T ends when [...] the storage
11305 // which the object occupies is [...] reused by an object that is not
11306 // nested within o (6.6.2).
11307 *Val = APValue();
11308 } else {
11309 // Perform the allocation and obtain a pointer to the resulting object.
11310 Val = Info.createHeapAlloc(E, T: AllocType, LV&: Result);
11311 if (!Val)
11312 return false;
11313 }
11314
11315 if (ValueInit) {
11316 ImplicitValueInitExpr VIE(AllocType);
11317 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: &VIE))
11318 return false;
11319 } else if (ResizedArrayILE) {
11320 if (!EvaluateArrayNewInitList(Info, This&: Result, Result&: *Val, ILE: ResizedArrayILE,
11321 AllocType))
11322 return false;
11323 } else if (ResizedArrayCCE) {
11324 if (!EvaluateArrayNewConstructExpr(Info, This&: Result, Result&: *Val, CCE: ResizedArrayCCE,
11325 AllocType))
11326 return false;
11327 } else if (Init) {
11328 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: Init))
11329 return false;
11330 } else if (!handleDefaultInitValue(T: AllocType, Result&: *Val)) {
11331 return false;
11332 }
11333
11334 // Array new returns a pointer to the first element, not a pointer to the
11335 // array.
11336 if (auto *AT = AllocType->getAsArrayTypeUnsafe())
11337 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val: AT));
11338
11339 return true;
11340}
11341
11342//===----------------------------------------------------------------------===//
11343// Reflection expression evaluation
11344//===----------------------------------------------------------------------===//
11345
11346namespace {
11347class ReflectionEvaluator : public ExprEvaluatorBase<ReflectionEvaluator> {
11348
11349 using BaseType = ExprEvaluatorBase<ReflectionEvaluator>;
11350
11351 APValue &Result;
11352
11353public:
11354 ReflectionEvaluator(EvalInfo &E, APValue &Result)
11355 : ExprEvaluatorBaseTy(E), Result(Result) {}
11356
11357 bool Success(const APValue &V, const Expr *E) {
11358 Result = V;
11359 return true;
11360 }
11361
11362 bool VisitCXXReflectExpr(const CXXReflectExpr *E);
11363 bool ZeroInitialization(const Expr *E);
11364};
11365
11366bool ReflectionEvaluator::VisitCXXReflectExpr(const CXXReflectExpr *E) {
11367 switch (E->getKind()) {
11368 case ReflectionKind::Null: {
11369 assert(false && "null reflection can't be constructed from parsing a "
11370 "reflection operand");
11371 return false;
11372 }
11373 case ReflectionKind::Type: {
11374 APValue ReflectionValue(ReflectionKind::Type, E->getOpaqueValue());
11375 return Success(V: ReflectionValue, E);
11376 }
11377 }
11378 assert(false && "unknown or unimplemented reflection entities");
11379 return false;
11380}
11381
11382bool ReflectionEvaluator::ZeroInitialization(const Expr *E) {
11383 Result = APValue(ReflectionKind::Null, /*Operand=*/nullptr);
11384 return true;
11385}
11386
11387} // end anonymous namespace
11388
11389static bool EvaluateReflection(const Expr *E, APValue &Result, EvalInfo &Info) {
11390 assert(E->isPRValue() && E->getType()->isMetaInfoType());
11391 return ReflectionEvaluator(Info, Result).Visit(S: E);
11392}
11393
11394//===----------------------------------------------------------------------===//
11395// Member Pointer Evaluation
11396//===----------------------------------------------------------------------===//
11397
11398namespace {
11399class MemberPointerExprEvaluator
11400 : public ExprEvaluatorBase<MemberPointerExprEvaluator> {
11401 MemberPtr &Result;
11402
11403 bool Success(const ValueDecl *D) {
11404 Result = MemberPtr(D);
11405 return true;
11406 }
11407public:
11408
11409 MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
11410 : ExprEvaluatorBaseTy(Info), Result(Result) {}
11411
11412 bool Success(const APValue &V, const Expr *E) {
11413 Result.setFrom(V);
11414 return true;
11415 }
11416 bool ZeroInitialization(const Expr *E) {
11417 return Success(D: (const ValueDecl*)nullptr);
11418 }
11419
11420 bool VisitCastExpr(const CastExpr *E);
11421 bool VisitUnaryAddrOf(const UnaryOperator *E);
11422};
11423} // end anonymous namespace
11424
11425static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
11426 EvalInfo &Info) {
11427 assert(!E->isValueDependent());
11428 assert(E->isPRValue() && E->getType()->isMemberPointerType());
11429 return MemberPointerExprEvaluator(Info, Result).Visit(S: E);
11430}
11431
11432bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
11433 switch (E->getCastKind()) {
11434 default:
11435 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11436
11437 case CK_NullToMemberPointer:
11438 VisitIgnoredValue(E: E->getSubExpr());
11439 return ZeroInitialization(E);
11440
11441 case CK_BaseToDerivedMemberPointer: {
11442 if (!Visit(S: E->getSubExpr()))
11443 return false;
11444 if (E->path_empty())
11445 return true;
11446 // Base-to-derived member pointer casts store the path in derived-to-base
11447 // order, so iterate backwards. The CXXBaseSpecifier also provides us with
11448 // the wrong end of the derived->base arc, so stagger the path by one class.
11449 typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
11450 for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
11451 PathI != PathE; ++PathI) {
11452 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11453 const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
11454 if (!Result.castToDerived(Derived))
11455 return Error(E);
11456 }
11457 if (!Result.castToDerived(Derived: E->getType()
11458 ->castAs<MemberPointerType>()
11459 ->getMostRecentCXXRecordDecl()))
11460 return Error(E);
11461 return true;
11462 }
11463
11464 case CK_DerivedToBaseMemberPointer:
11465 if (!Visit(S: E->getSubExpr()))
11466 return false;
11467 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11468 PathE = E->path_end(); PathI != PathE; ++PathI) {
11469 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11470 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11471 if (!Result.castToBase(Base))
11472 return Error(E);
11473 }
11474 return true;
11475 }
11476}
11477
11478bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
11479 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
11480 // member can be formed.
11481 return Success(D: cast<DeclRefExpr>(Val: E->getSubExpr())->getDecl());
11482}
11483
11484//===----------------------------------------------------------------------===//
11485// Record Evaluation
11486//===----------------------------------------------------------------------===//
11487
11488namespace {
11489 class RecordExprEvaluator
11490 : public ExprEvaluatorBase<RecordExprEvaluator> {
11491 const LValue &This;
11492 APValue &Result;
11493 public:
11494
11495 RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
11496 : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
11497
11498 bool Success(const APValue &V, const Expr *E) {
11499 Result = V;
11500 return true;
11501 }
11502 bool ZeroInitialization(const Expr *E) {
11503 return ZeroInitialization(E, T: E->getType());
11504 }
11505 bool ZeroInitialization(const Expr *E, QualType T);
11506
11507 bool VisitCallExpr(const CallExpr *E) {
11508 return handleCallExpr(E, Result, ResultSlot: &This);
11509 }
11510 bool VisitCastExpr(const CastExpr *E);
11511 bool VisitInitListExpr(const InitListExpr *E);
11512 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
11513 return VisitCXXConstructExpr(E, T: E->getType());
11514 }
11515 bool VisitLambdaExpr(const LambdaExpr *E);
11516 bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E);
11517 bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T);
11518 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E);
11519 bool VisitBinCmp(const BinaryOperator *E);
11520 bool VisitTypeTraitExpr(const TypeTraitExpr *E);
11521 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
11522 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
11523 ArrayRef<Expr *> Args);
11524 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
11525 };
11526}
11527
11528/// Perform zero-initialization on an object of non-union class type.
11529/// C++11 [dcl.init]p5:
11530/// To zero-initialize an object or reference of type T means:
11531/// [...]
11532/// -- if T is a (possibly cv-qualified) non-union class type,
11533/// each non-static data member and each base-class subobject is
11534/// zero-initialized
11535static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
11536 const RecordDecl *RD,
11537 const LValue &This, APValue &Result,
11538 bool IsCompleteClass = true) {
11539 assert(!RD->isUnion() && "Expected non-union class type");
11540 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD);
11541
11542 if (CD) {
11543 unsigned NonVirtualBases = countNonVirtualBases(RD: CD);
11544 Result =
11545 APValue(APValue::UninitStruct(), NonVirtualBases, RD->getNumFields(),
11546 IsCompleteClass ? CD->getNumVBases() : 0);
11547 } else {
11548 Result = APValue(APValue::UninitStruct(), 0, RD->getNumFields());
11549 }
11550
11551 if (RD->isInvalidDecl()) return false;
11552 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11553
11554 if (CD) {
11555 unsigned Index = 0;
11556
11557 for (const auto &B : CD->bases()) {
11558 if (B.isVirtual())
11559 continue;
11560 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
11561 LValue Subobject = This;
11562 if (!HandleLValueDirectBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11563 return false;
11564 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11565 Result&: Result.getStructBase(i: Index),
11566 /*IsCompleteClass=*/false))
11567 return false;
11568 ++Index;
11569 }
11570 }
11571
11572 for (const auto *I : RD->fields()) {
11573 // -- if T is a reference type, no initialization is performed.
11574 if (I->isUnnamedBitField() || I->getType()->isReferenceType())
11575 continue;
11576
11577 LValue Subobject = This;
11578 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: I, RL: &Layout))
11579 return false;
11580
11581 ImplicitValueInitExpr VIE(I->getType());
11582 if (!EvaluateInPlace(
11583 Result&: Result.getStructField(i: I->getFieldIndex()), Info, This: Subobject, E: &VIE))
11584 return false;
11585 }
11586
11587 if (CD && This.pointsToCompleteClass(D: CD)) {
11588 unsigned Index = 0;
11589 for (const auto &B : CD->vbases()) {
11590 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
11591 LValue Subobject = This;
11592 if (!HandleLValueDirectVirtualBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11593 return false;
11594 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11595 Result&: Result.getStructVirtualBase(i: Index),
11596 /*IsCompleteClass=*/false))
11597 return false;
11598 ++Index;
11599 }
11600 }
11601
11602 return true;
11603}
11604
11605bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) {
11606 const auto *RD = T->castAsRecordDecl();
11607 if (RD->isInvalidDecl()) return false;
11608 if (RD->isUnion()) {
11609 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
11610 // object's first non-static named data member is zero-initialized
11611 RecordDecl::field_iterator I = RD->field_begin();
11612 while (I != RD->field_end() && (*I)->isUnnamedBitField())
11613 ++I;
11614 if (I == RD->field_end()) {
11615 Result = APValue((const FieldDecl*)nullptr);
11616 return true;
11617 }
11618
11619 LValue Subobject = This;
11620 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: *I))
11621 return false;
11622 Result = APValue(*I);
11623 ImplicitValueInitExpr VIE(I->getType());
11624 return EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: &VIE);
11625 }
11626
11627 if (!Info.getLangOpts().CPlusPlus26) {
11628 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11629 CXXRD && CXXRD->getNumVBases()) {
11630 Info.FFDiag(E, DiagId: diag::note_constexpr_virtual_base) << RD;
11631 return false;
11632 }
11633 }
11634
11635 return HandleClassZeroInitialization(Info, E, RD, This, Result);
11636}
11637
11638bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
11639 switch (E->getCastKind()) {
11640 default:
11641 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11642
11643 case CK_ConstructorConversion:
11644 return Visit(S: E->getSubExpr());
11645
11646 case CK_DerivedToBase:
11647 case CK_UncheckedDerivedToBase: {
11648 APValue DerivedObject;
11649 if (!Evaluate(Result&: DerivedObject, Info, E: E->getSubExpr()))
11650 return false;
11651 if (!DerivedObject.isStruct())
11652 return Error(E: E->getSubExpr());
11653
11654 // Derived-to-base rvalue conversion: just slice off the derived part.
11655 APValue *Value = &DerivedObject;
11656 const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
11657 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11658 PathE = E->path_end(); PathI != PathE; ++PathI) {
11659 assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
11660 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11661 Value = &Value->getStructBase(i: getBaseIndex(Derived: RD, Base));
11662 RD = Base;
11663 }
11664 Result = *Value;
11665 return true;
11666 }
11667 case CK_HLSLAggregateSplatCast: {
11668 APValue Val;
11669 QualType ValTy;
11670
11671 if (!hlslAggSplatHelper(Info, E: E->getSubExpr(), SrcVal&: Val, SrcTy&: ValTy))
11672 return false;
11673
11674 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
11675 // splat our Val
11676 SmallVector<APValue> SplatEls(NEls, Val);
11677 SmallVector<QualType> SplatType(NEls, ValTy);
11678
11679 // cast the elements and construct our struct result
11680 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11681 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
11682 ElTypes&: SplatType))
11683 return false;
11684
11685 return true;
11686 }
11687 case CK_HLSLElementwiseCast: {
11688 SmallVector<APValue> SrcEls;
11689 SmallVector<QualType> SrcTypes;
11690
11691 if (!hlslElementwiseCastHelper(Info, E: E->getSubExpr(), DestTy: E->getType(), SrcVals&: SrcEls,
11692 SrcTypes))
11693 return false;
11694
11695 // cast the elements and construct our struct result
11696 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11697 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
11698 ElTypes&: SrcTypes))
11699 return false;
11700
11701 return true;
11702 }
11703 case CK_ToUnion: {
11704 const FieldDecl *Field = E->getTargetUnionField();
11705 LValue Subobject = This;
11706 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field))
11707 return false;
11708 Result = APValue(Field);
11709 if (!EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject,
11710 E: E->getSubExpr()))
11711 return false;
11712 if (Field->isBitField()) {
11713 if (!truncateBitfieldValue(Info, E: E->getSubExpr(), Value&: Result.getUnionValue(),
11714 FD: Field))
11715 return false;
11716 }
11717 return true;
11718 }
11719 }
11720}
11721
11722bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
11723 if (E->isTransparent())
11724 return Visit(S: E->getInit(Init: 0));
11725 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits());
11726}
11727
11728bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr(
11729 const Expr *ExprToVisit, ArrayRef<Expr *> Args) {
11730 const auto *RD = ExprToVisit->getType()->castAsRecordDecl();
11731 if (RD->isInvalidDecl()) return false;
11732 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11733 auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11734
11735 EvalInfo::EvaluatingConstructorRAII EvalObj(
11736 Info,
11737 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
11738 CXXRD && CXXRD->getNumBases());
11739
11740 if (RD->isUnion()) {
11741 const FieldDecl *Field;
11742 if (auto *ILE = dyn_cast<InitListExpr>(Val: ExprToVisit)) {
11743 Field = ILE->getInitializedFieldInUnion();
11744 } else if (auto *PLIE = dyn_cast<CXXParenListInitExpr>(Val: ExprToVisit)) {
11745 Field = PLIE->getInitializedFieldInUnion();
11746 } else {
11747 llvm_unreachable(
11748 "Expression is neither an init list nor a C++ paren list");
11749 }
11750
11751 Result = APValue(Field);
11752 if (!Field)
11753 return true;
11754
11755 // If the initializer list for a union does not contain any elements, the
11756 // first element of the union is value-initialized.
11757 // FIXME: The element should be initialized from an initializer list.
11758 // Is this difference ever observable for initializer lists which
11759 // we don't build?
11760 ImplicitValueInitExpr VIE(Field->getType());
11761 const Expr *InitExpr = Args.empty() ? &VIE : Args[0];
11762
11763 LValue Subobject = This;
11764 if (!HandleLValueMember(Info, E: InitExpr, LVal&: Subobject, FD: Field, RL: &Layout))
11765 return false;
11766
11767 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11768 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11769 isa<CXXDefaultInitExpr>(Val: InitExpr));
11770
11771 if (EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: InitExpr)) {
11772 if (Field->isBitField())
11773 return truncateBitfieldValue(Info, E: InitExpr, Value&: Result.getUnionValue(),
11774 FD: Field);
11775 return true;
11776 }
11777
11778 return false;
11779 }
11780
11781 if (!Result.hasValue())
11782 Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0,
11783 RD->getNumFields());
11784 unsigned ElementNo = 0;
11785 bool Success = true;
11786
11787 // Initialize base classes.
11788 if (CXXRD && CXXRD->getNumBases()) {
11789 for (const auto &Base : CXXRD->bases()) {
11790 assert(ElementNo < Args.size() && "missing init for base class");
11791 const Expr *Init = Args[ElementNo];
11792
11793 LValue Subobject = This;
11794 if (!HandleLValueBase(Info, E: Init, Obj&: Subobject, DerivedDecl: CXXRD, Base: &Base))
11795 return false;
11796
11797 APValue &FieldVal = Result.getStructBase(i: ElementNo);
11798 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init)) {
11799 if (!Info.noteFailure())
11800 return false;
11801 Success = false;
11802 }
11803 ++ElementNo;
11804 }
11805
11806 EvalObj.finishedConstructingBases();
11807 }
11808
11809 // Initialize members.
11810 for (const auto *Field : RD->fields()) {
11811 // Anonymous bit-fields are not considered members of the class for
11812 // purposes of aggregate initialization.
11813 if (Field->isUnnamedBitField())
11814 continue;
11815
11816 LValue Subobject = This;
11817
11818 bool HaveInit = ElementNo < Args.size();
11819
11820 // FIXME: Diagnostics here should point to the end of the initializer
11821 // list, not the start.
11822 if (!HandleLValueMember(Info, E: HaveInit ? Args[ElementNo] : ExprToVisit,
11823 LVal&: Subobject, FD: Field, RL: &Layout))
11824 return false;
11825
11826 // Perform an implicit value-initialization for members beyond the end of
11827 // the initializer list.
11828 ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
11829 const Expr *Init = HaveInit ? Args[ElementNo++] : &VIE;
11830
11831 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
11832 // aren't supposed to be modified.
11833 if (isa<NoInitExpr>(Val: Init))
11834 continue;
11835
11836 if (Field->getType()->isIncompleteArrayType()) {
11837 if (auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType())) {
11838 if (!CAT->isZeroSize()) {
11839 // Bail out for now. This might sort of "work", but the rest of the
11840 // code isn't really prepared to handle it.
11841 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_unsupported_flexible_array);
11842 return false;
11843 }
11844 }
11845 }
11846
11847 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11848 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11849 isa<CXXDefaultInitExpr>(Val: Init));
11850
11851 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
11852 if (Field->getType()->isReferenceType()) {
11853 LValue Result;
11854 if (!EvaluateInitForDeclOfReferenceType(Info, D: Field, Init, Result,
11855 Val&: FieldVal)) {
11856 if (!Info.noteFailure())
11857 return false;
11858 Success = false;
11859 }
11860 } else if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init) ||
11861 (Field->isBitField() &&
11862 !truncateBitfieldValue(Info, E: Init, Value&: FieldVal, FD: Field))) {
11863 if (!Info.noteFailure())
11864 return false;
11865 Success = false;
11866 }
11867 }
11868
11869 EvalObj.finishedConstructingFields();
11870
11871 return Success;
11872}
11873
11874bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
11875 QualType T) {
11876 // Note that E's type is not necessarily the type of our class here; we might
11877 // be initializing an array element instead.
11878 const CXXConstructorDecl *FD = E->getConstructor();
11879 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false;
11880
11881 bool ZeroInit = E->requiresZeroInitialization();
11882 if (CheckTrivialDefaultConstructor(Info, Loc: E->getExprLoc(), CD: FD, IsValueInitialization: ZeroInit)) {
11883 if (ZeroInit)
11884 return ZeroInitialization(E, T);
11885
11886 return handleDefaultInitValue(T, Result);
11887 }
11888
11889 const FunctionDecl *Definition = nullptr;
11890 auto Body = FD->getBody(Definition);
11891
11892 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11893 return false;
11894
11895 // Avoid materializing a temporary for an elidable copy/move constructor.
11896 if (E->isElidable() && !ZeroInit) {
11897 // FIXME: This only handles the simplest case, where the source object
11898 // is passed directly as the first argument to the constructor.
11899 // This should also handle stepping though implicit casts and
11900 // and conversion sequences which involve two steps, with a
11901 // conversion operator followed by a converting constructor.
11902 const Expr *SrcObj = E->getArg(Arg: 0);
11903 assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent()));
11904 assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType()));
11905 if (const MaterializeTemporaryExpr *ME =
11906 dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj))
11907 return Visit(S: ME->getSubExpr());
11908 }
11909
11910 if (ZeroInit && !ZeroInitialization(E, T))
11911 return false;
11912
11913 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
11914 return HandleConstructorCall(E, This, Args,
11915 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
11916 Result);
11917}
11918
11919bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr(
11920 const CXXInheritedCtorInitExpr *E) {
11921 if (!Info.CurrentCall) {
11922 assert(Info.checkingPotentialConstantExpression());
11923 return false;
11924 }
11925
11926 const CXXConstructorDecl *FD = E->getConstructor();
11927 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl())
11928 return false;
11929
11930 const FunctionDecl *Definition = nullptr;
11931 auto Body = FD->getBody(Definition);
11932
11933 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11934 return false;
11935
11936 return HandleConstructorCall(E, This, Call: Info.CurrentCall->Arguments,
11937 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
11938 Result);
11939}
11940
11941bool RecordExprEvaluator::VisitCXXStdInitializerListExpr(
11942 const CXXStdInitializerListExpr *E) {
11943 const ConstantArrayType *ArrayType =
11944 Info.Ctx.getAsConstantArrayType(T: E->getSubExpr()->getType());
11945
11946 LValue Array;
11947 if (!EvaluateLValue(E: E->getSubExpr(), Result&: Array, Info))
11948 return false;
11949
11950 assert(ArrayType && "unexpected type for array initializer");
11951
11952 // Get a pointer to the first element of the array.
11953 Array.addArray(Info, E, CAT: ArrayType);
11954
11955 // FIXME: What if the initializer_list type has base classes, etc?
11956 Result = APValue(APValue::UninitStruct(), 0, 2);
11957 Array.moveInto(V&: Result.getStructField(i: 0));
11958
11959 auto *Record = E->getType()->castAsRecordDecl();
11960 RecordDecl::field_iterator Field = Record->field_begin();
11961 assert(Field != Record->field_end() &&
11962 Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
11963 ArrayType->getElementType()) &&
11964 "Expected std::initializer_list first field to be const E *");
11965 ++Field;
11966 assert(Field != Record->field_end() &&
11967 "Expected std::initializer_list to have two fields");
11968
11969 if (Info.Ctx.hasSameType(T1: Field->getType(), T2: Info.Ctx.getSizeType())) {
11970 // Length.
11971 Result.getStructField(i: 1) = APValue(APSInt(ArrayType->getSize()));
11972 } else {
11973 // End pointer.
11974 assert(Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
11975 ArrayType->getElementType()) &&
11976 "Expected std::initializer_list second field to be const E *");
11977 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Array,
11978 EltTy: ArrayType->getElementType(),
11979 Adjustment: ArrayType->getZExtSize()))
11980 return false;
11981 Array.moveInto(V&: Result.getStructField(i: 1));
11982 }
11983
11984 assert(++Field == Record->field_end() &&
11985 "Expected std::initializer_list to only have two fields");
11986
11987 return true;
11988}
11989
11990bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {
11991 const CXXRecordDecl *ClosureClass = E->getLambdaClass();
11992 if (ClosureClass->isInvalidDecl())
11993 return false;
11994
11995 const size_t NumFields = ClosureClass->getNumFields();
11996
11997 assert(NumFields == (size_t)std::distance(E->capture_init_begin(),
11998 E->capture_init_end()) &&
11999 "The number of lambda capture initializers should equal the number of "
12000 "fields within the closure type");
12001
12002 Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields);
12003 // Iterate through all the lambda's closure object's fields and initialize
12004 // them.
12005 auto *CaptureInitIt = E->capture_init_begin();
12006 bool Success = true;
12007 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: ClosureClass);
12008 for (const auto *Field : ClosureClass->fields()) {
12009 assert(CaptureInitIt != E->capture_init_end());
12010 // Get the initializer for this field
12011 Expr *const CurFieldInit = *CaptureInitIt++;
12012
12013 // If there is no initializer, either this is a VLA or an error has
12014 // occurred.
12015 if (!CurFieldInit || CurFieldInit->containsErrors())
12016 return Error(E);
12017
12018 LValue Subobject = This;
12019
12020 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field, RL: &Layout))
12021 return false;
12022
12023 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
12024 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: CurFieldInit)) {
12025 if (!Info.keepEvaluatingAfterFailure())
12026 return false;
12027 Success = false;
12028 }
12029 }
12030 return Success;
12031}
12032
12033bool RecordExprEvaluator::VisitDesignatedInitUpdateExpr(
12034 const DesignatedInitUpdateExpr *E) {
12035 if (!Visit(S: E->getBase()))
12036 return false;
12037 return Visit(S: E->getUpdater());
12038}
12039
12040static bool EvaluateRecord(const Expr *E, const LValue &This,
12041 APValue &Result, EvalInfo &Info) {
12042 assert(!E->isValueDependent());
12043 assert(E->isPRValue() && E->getType()->isRecordType() &&
12044 "can't evaluate expression as a record rvalue");
12045 return RecordExprEvaluator(Info, This, Result).Visit(S: E);
12046}
12047
12048//===----------------------------------------------------------------------===//
12049// Temporary Evaluation
12050//
12051// Temporaries are represented in the AST as rvalues, but generally behave like
12052// lvalues. The full-object of which the temporary is a subobject is implicitly
12053// materialized so that a reference can bind to it.
12054//===----------------------------------------------------------------------===//
12055namespace {
12056class TemporaryExprEvaluator
12057 : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
12058public:
12059 TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
12060 LValueExprEvaluatorBaseTy(Info, Result, false) {}
12061
12062 /// Visit an expression which constructs the value of this temporary.
12063 bool VisitConstructExpr(const Expr *E) {
12064 APValue &Value = Info.CurrentCall->createTemporary(
12065 Key: E, T: E->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
12066 return EvaluateInPlace(Result&: Value, Info, This: Result, E);
12067 }
12068
12069 bool VisitCastExpr(const CastExpr *E) {
12070 switch (E->getCastKind()) {
12071 default:
12072 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
12073
12074 case CK_ConstructorConversion:
12075 return VisitConstructExpr(E: E->getSubExpr());
12076 }
12077 }
12078 bool VisitInitListExpr(const InitListExpr *E) {
12079 return VisitConstructExpr(E);
12080 }
12081 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
12082 return VisitConstructExpr(E);
12083 }
12084 bool VisitCallExpr(const CallExpr *E) {
12085 return VisitConstructExpr(E);
12086 }
12087 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) {
12088 return VisitConstructExpr(E);
12089 }
12090 bool VisitLambdaExpr(const LambdaExpr *E) {
12091 return VisitConstructExpr(E);
12092 }
12093};
12094} // end anonymous namespace
12095
12096/// Evaluate an expression of record type as a temporary.
12097static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
12098 assert(!E->isValueDependent());
12099 assert(E->isPRValue() && E->getType()->isRecordType());
12100 return TemporaryExprEvaluator(Info, Result).Visit(S: E);
12101}
12102
12103//===----------------------------------------------------------------------===//
12104// Vector Evaluation
12105//===----------------------------------------------------------------------===//
12106
12107namespace {
12108 class VectorExprEvaluator
12109 : public ExprEvaluatorBase<VectorExprEvaluator> {
12110 APValue &Result;
12111 public:
12112
12113 VectorExprEvaluator(EvalInfo &info, APValue &Result)
12114 : ExprEvaluatorBaseTy(info), Result(Result) {}
12115
12116 bool Success(ArrayRef<APValue> V, const Expr *E) {
12117 assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
12118 // FIXME: remove this APValue copy.
12119 Result = APValue(V.data(), V.size());
12120 return true;
12121 }
12122 bool Success(const APValue &V, const Expr *E) {
12123 assert(V.isVector());
12124 Result = V;
12125 return true;
12126 }
12127 bool ZeroInitialization(const Expr *E);
12128
12129 bool VisitUnaryReal(const UnaryOperator *E)
12130 { return Visit(S: E->getSubExpr()); }
12131 bool VisitCastExpr(const CastExpr* E);
12132 bool VisitInitListExpr(const InitListExpr *E);
12133 bool VisitUnaryImag(const UnaryOperator *E);
12134 bool VisitBinaryOperator(const BinaryOperator *E);
12135 bool VisitUnaryOperator(const UnaryOperator *E);
12136 bool VisitCallExpr(const CallExpr *E);
12137 bool VisitConvertVectorExpr(const ConvertVectorExpr *E);
12138 bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E);
12139
12140 // FIXME: Missing: conditional operator (for GNU
12141 // conditional select), ExtVectorElementExpr
12142 };
12143} // end anonymous namespace
12144
12145static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
12146 assert(E->isPRValue() && E->getType()->isVectorType() &&
12147 "not a vector prvalue");
12148 return VectorExprEvaluator(Info, Result).Visit(S: E);
12149}
12150
12151static llvm::APInt ConvertBoolVectorToInt(const APValue &Val) {
12152 assert(Val.isVector() && "expected vector APValue");
12153 unsigned NumElts = Val.getVectorLength();
12154
12155 // Each element is one bit, so create an integer with NumElts bits.
12156 llvm::APInt Result(NumElts, 0);
12157
12158 for (unsigned I = 0; I < NumElts; ++I) {
12159 const APValue &Elt = Val.getVectorElt(I);
12160 assert(Elt.isInt() && "expected integer element in bool vector");
12161
12162 if (Elt.getInt().getBoolValue())
12163 Result.setBit(I);
12164 }
12165
12166 return Result;
12167}
12168
12169bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) {
12170 const VectorType *VTy = E->getType()->castAs<VectorType>();
12171 unsigned NElts = VTy->getNumElements();
12172
12173 const Expr *SE = E->getSubExpr();
12174 QualType SETy = SE->getType();
12175
12176 switch (E->getCastKind()) {
12177 case CK_VectorSplat: {
12178 APValue Val = APValue();
12179 if (SETy->isIntegerType()) {
12180 APSInt IntResult;
12181 if (!EvaluateInteger(E: SE, Result&: IntResult, Info))
12182 return false;
12183 Val = APValue(std::move(IntResult));
12184 } else if (SETy->isRealFloatingType()) {
12185 APFloat FloatResult(0.0);
12186 if (!EvaluateFloat(E: SE, Result&: FloatResult, Info))
12187 return false;
12188 Val = APValue(std::move(FloatResult));
12189 } else {
12190 return Error(E);
12191 }
12192
12193 // Splat and create vector APValue.
12194 SmallVector<APValue, 4> Elts(NElts, Val);
12195 return Success(V: Elts, E);
12196 }
12197 case CK_BitCast: {
12198 APValue SVal;
12199 if (!Evaluate(Result&: SVal, Info, E: SE))
12200 return false;
12201
12202 if (!SVal.isInt() && !SVal.isFloat() && !SVal.isVector()) {
12203 // Give up if the input isn't an int, float, or vector. For example, we
12204 // reject "(v4i16)(intptr_t)&a".
12205 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_cast)
12206 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
12207 << Info.Ctx.getLangOpts().CPlusPlus;
12208 return false;
12209 }
12210
12211 if (!handleRValueToRValueBitCast(Info, DestValue&: Result, SourceRValue: SVal, BCE: E))
12212 return false;
12213
12214 return true;
12215 }
12216 case CK_HLSLVectorTruncation: {
12217 APValue Val;
12218 SmallVector<APValue, 4> Elements;
12219 if (!EvaluateVector(E: SE, Result&: Val, Info))
12220 return Error(E);
12221 for (unsigned I = 0; I < NElts; I++)
12222 Elements.push_back(Elt: Val.getVectorElt(I));
12223 return Success(V: Elements, E);
12224 }
12225 case CK_HLSLMatrixTruncation: {
12226 // Matrix truncation occurs in row-major order.
12227 APValue Val;
12228 if (!EvaluateMatrix(E: SE, Result&: Val, Info))
12229 return Error(E);
12230 SmallVector<APValue, 16> Elements;
12231 for (unsigned Row = 0;
12232 Row < Val.getMatrixNumRows() && Elements.size() < NElts; Row++)
12233 for (unsigned Col = 0;
12234 Col < Val.getMatrixNumColumns() && Elements.size() < NElts; Col++)
12235 Elements.push_back(Elt: Val.getMatrixElt(Row, Col));
12236 return Success(V: Elements, E);
12237 }
12238 case CK_HLSLAggregateSplatCast: {
12239 APValue Val;
12240 QualType ValTy;
12241
12242 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
12243 return false;
12244
12245 // cast our Val once.
12246 APValue Result;
12247 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12248 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: VTy->getElementType(), Original: Val,
12249 Result))
12250 return false;
12251
12252 SmallVector<APValue, 4> SplatEls(NElts, Result);
12253 return Success(V: SplatEls, E);
12254 }
12255 case CK_HLSLElementwiseCast: {
12256 SmallVector<APValue> SrcVals;
12257 SmallVector<QualType> SrcTypes;
12258
12259 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
12260 return false;
12261
12262 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12263 SmallVector<QualType, 4> DestTypes(NElts, VTy->getElementType());
12264 SmallVector<APValue, 4> ResultEls(NElts);
12265 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
12266 Results&: ResultEls))
12267 return false;
12268 return Success(V: ResultEls, E);
12269 }
12270 case CK_IntegralToFloating:
12271 case CK_FloatingToIntegral:
12272 case CK_IntegralCast:
12273 case CK_FloatingCast:
12274 case CK_FloatingToBoolean:
12275 case CK_IntegralToBoolean: {
12276 // These casts apply element-wise when the source is a vector type.
12277 assert(SETy->isVectorType() && "expected vector source type");
12278 APValue SrcVal;
12279 if (!EvaluateVector(E: SE, Result&: SrcVal, Info))
12280 return Error(E);
12281
12282 assert(SrcVal.getVectorLength() == NElts);
12283 QualType SrcEltTy = SETy->castAs<VectorType>()->getElementType();
12284 QualType DstEltTy = VTy->getElementType();
12285 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12286
12287 SmallVector<APValue, 4> ResultEls(NElts);
12288 for (unsigned I = 0; I < NElts; ++I) {
12289 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcEltTy, DestTy: DstEltTy,
12290 Original: SrcVal.getVectorElt(I), Result&: ResultEls[I]))
12291 return Error(E);
12292 }
12293 return Success(V: ResultEls, E);
12294 }
12295 default:
12296 return ExprEvaluatorBaseTy::VisitCastExpr(E);
12297 }
12298}
12299
12300bool
12301VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
12302 const VectorType *VT = E->getType()->castAs<VectorType>();
12303 unsigned NumInits = E->getNumInits();
12304 unsigned NumElements = VT->getNumElements();
12305
12306 QualType EltTy = VT->getElementType();
12307 SmallVector<APValue, 4> Elements;
12308
12309 // MFloat8 type doesn't have constants and thus constant folding
12310 // is impossible.
12311 if (EltTy->isMFloat8Type())
12312 return false;
12313
12314 // The number of initializers can be less than the number of
12315 // vector elements. For OpenCL, this can be due to nested vector
12316 // initialization. For GCC compatibility, missing trailing elements
12317 // should be initialized with zeroes.
12318 unsigned CountInits = 0, CountElts = 0;
12319 while (CountElts < NumElements) {
12320 // Handle nested vector initialization.
12321 if (CountInits < NumInits
12322 && E->getInit(Init: CountInits)->getType()->isVectorType()) {
12323 APValue v;
12324 if (!EvaluateVector(E: E->getInit(Init: CountInits), Result&: v, Info))
12325 return Error(E);
12326 unsigned vlen = v.getVectorLength();
12327 for (unsigned j = 0; j < vlen; j++)
12328 Elements.push_back(Elt: v.getVectorElt(I: j));
12329 CountElts += vlen;
12330 } else if (EltTy->isIntegerType()) {
12331 llvm::APSInt sInt(32);
12332 if (CountInits < NumInits) {
12333 if (!EvaluateInteger(E: E->getInit(Init: CountInits), Result&: sInt, Info))
12334 return false;
12335 } else // trailing integer zero.
12336 sInt = Info.Ctx.MakeIntValue(Value: 0, Type: EltTy);
12337 Elements.push_back(Elt: APValue(sInt));
12338 CountElts++;
12339 } else {
12340 llvm::APFloat f(0.0);
12341 if (CountInits < NumInits) {
12342 if (!EvaluateFloat(E: E->getInit(Init: CountInits), Result&: f, Info))
12343 return false;
12344 } else // trailing float zero.
12345 f = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy));
12346 Elements.push_back(Elt: APValue(f));
12347 CountElts++;
12348 }
12349 CountInits++;
12350 }
12351 return Success(V: Elements, E);
12352}
12353
12354bool
12355VectorExprEvaluator::ZeroInitialization(const Expr *E) {
12356 const auto *VT = E->getType()->castAs<VectorType>();
12357 QualType EltTy = VT->getElementType();
12358 APValue ZeroElement;
12359 if (EltTy->isIntegerType())
12360 ZeroElement = APValue(Info.Ctx.MakeIntValue(Value: 0, Type: EltTy));
12361 else
12362 ZeroElement =
12363 APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy)));
12364
12365 SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
12366 return Success(V: Elements, E);
12367}
12368
12369bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
12370 VisitIgnoredValue(E: E->getSubExpr());
12371 return ZeroInitialization(E);
12372}
12373
12374bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
12375 BinaryOperatorKind Op = E->getOpcode();
12376 assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp &&
12377 "Operation not supported on vector types");
12378
12379 if (Op == BO_Comma)
12380 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
12381
12382 Expr *LHS = E->getLHS();
12383 Expr *RHS = E->getRHS();
12384
12385 [[maybe_unused]] QualType LHSType = LHS->getType().getAtomicUnqualifiedType();
12386 [[maybe_unused]] QualType RHSType = RHS->getType().getAtomicUnqualifiedType();
12387 assert(LHSType->isVectorType() && RHSType->isVectorType() &&
12388 "Must both be vector types");
12389 // Checking JUST the types are the same would be fine, except shifts don't
12390 // need to have their types be the same (since you always shift by an int).
12391 assert(LHSType->castAs<VectorType>()->getNumElements() ==
12392 E->getType()->castAs<VectorType>()->getNumElements() &&
12393 RHSType->castAs<VectorType>()->getNumElements() ==
12394 E->getType()->castAs<VectorType>()->getNumElements() &&
12395 "All operands must be the same size.");
12396
12397 APValue LHSValue;
12398 APValue RHSValue;
12399 bool LHSOK = Evaluate(Result&: LHSValue, Info, E: LHS);
12400 if (!LHSOK && !Info.noteFailure())
12401 return false;
12402 if (!Evaluate(Result&: RHSValue, Info, E: RHS) || !LHSOK)
12403 return false;
12404
12405 if (!handleVectorVectorBinOp(Info, E, Opcode: Op, LHSValue, RHSValue))
12406 return false;
12407
12408 return Success(V: LHSValue, E);
12409}
12410
12411static std::optional<APValue> handleVectorUnaryOperator(ASTContext &Ctx,
12412 QualType ResultTy,
12413 UnaryOperatorKind Op,
12414 APValue Elt) {
12415 switch (Op) {
12416 case UO_Plus:
12417 // Nothing to do here.
12418 return Elt;
12419 case UO_Minus:
12420 if (Elt.getKind() == APValue::Int) {
12421 Elt.getInt().negate();
12422 } else {
12423 assert(Elt.getKind() == APValue::Float &&
12424 "Vector can only be int or float type");
12425 Elt.getFloat().changeSign();
12426 }
12427 return Elt;
12428 case UO_Not:
12429 // This is only valid for integral types anyway, so we don't have to handle
12430 // float here.
12431 assert(Elt.getKind() == APValue::Int &&
12432 "Vector operator ~ can only be int");
12433 Elt.getInt().flipAllBits();
12434 return Elt;
12435 case UO_LNot: {
12436 if (Elt.getKind() == APValue::Int) {
12437 Elt.getInt() = !Elt.getInt();
12438 // operator ! on vectors returns -1 for 'truth', so negate it.
12439 Elt.getInt().negate();
12440 return Elt;
12441 }
12442 assert(Elt.getKind() == APValue::Float &&
12443 "Vector can only be int or float type");
12444 // Float types result in an int of the same size, but -1 for true, or 0 for
12445 // false.
12446 APSInt EltResult{Ctx.getIntWidth(T: ResultTy),
12447 ResultTy->isUnsignedIntegerType()};
12448 if (Elt.getFloat().isZero())
12449 EltResult.setAllBits();
12450 else
12451 EltResult.clearAllBits();
12452
12453 return APValue{EltResult};
12454 }
12455 default:
12456 // FIXME: Implement the rest of the unary operators.
12457 return std::nullopt;
12458 }
12459}
12460
12461bool VectorExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
12462 Expr *SubExpr = E->getSubExpr();
12463 const auto *VD = SubExpr->getType()->castAs<VectorType>();
12464 // This result element type differs in the case of negating a floating point
12465 // vector, since the result type is the a vector of the equivilant sized
12466 // integer.
12467 const QualType ResultEltTy = VD->getElementType();
12468 UnaryOperatorKind Op = E->getOpcode();
12469
12470 APValue SubExprValue;
12471 if (!Evaluate(Result&: SubExprValue, Info, E: SubExpr))
12472 return false;
12473
12474 // FIXME: This vector evaluator someday needs to be changed to be LValue
12475 // aware/keep LValue information around, rather than dealing with just vector
12476 // types directly. Until then, we cannot handle cases where the operand to
12477 // these unary operators is an LValue. The only case I've been able to see
12478 // cause this is operator++ assigning to a member expression (only valid in
12479 // altivec compilations) in C mode, so this shouldn't limit us too much.
12480 if (SubExprValue.isLValue())
12481 return false;
12482
12483 assert(SubExprValue.getVectorLength() == VD->getNumElements() &&
12484 "Vector length doesn't match type?");
12485
12486 SmallVector<APValue, 4> ResultElements;
12487 for (unsigned EltNum = 0; EltNum < VD->getNumElements(); ++EltNum) {
12488 std::optional<APValue> Elt = handleVectorUnaryOperator(
12489 Ctx&: Info.Ctx, ResultTy: ResultEltTy, Op, Elt: SubExprValue.getVectorElt(I: EltNum));
12490 if (!Elt)
12491 return false;
12492 ResultElements.push_back(Elt: *Elt);
12493 }
12494 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12495}
12496
12497static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO,
12498 const Expr *E, QualType SourceTy,
12499 QualType DestTy, APValue const &Original,
12500 APValue &Result) {
12501 if (SourceTy->isIntegerType()) {
12502 if (DestTy->isRealFloatingType()) {
12503 Result = APValue(APFloat(0.0));
12504 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
12505 DestType: DestTy, Result&: Result.getFloat());
12506 }
12507 if (DestTy->isIntegerType()) {
12508 Result = APValue(
12509 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
12510 return true;
12511 }
12512 } else if (SourceTy->isRealFloatingType()) {
12513 if (DestTy->isRealFloatingType()) {
12514 Result = Original;
12515 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
12516 Result&: Result.getFloat());
12517 }
12518 if (DestTy->isIntegerType()) {
12519 Result = APValue(APSInt());
12520 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
12521 DestType: DestTy, Result&: Result.getInt());
12522 }
12523 }
12524
12525 Info.FFDiag(E, DiagId: diag::err_convertvector_constexpr_unsupported_vector_cast)
12526 << SourceTy << DestTy;
12527 return false;
12528}
12529
12530static bool evalPackBuiltin(const CallExpr *E, EvalInfo &Info, APValue &Result,
12531 llvm::function_ref<APInt(const APSInt &)> PackFn) {
12532 APValue LHS, RHS;
12533 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: LHS) ||
12534 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: RHS))
12535 return false;
12536
12537 unsigned LHSVecLen = LHS.getVectorLength();
12538 unsigned RHSVecLen = RHS.getVectorLength();
12539
12540 assert(LHSVecLen != 0 && LHSVecLen == RHSVecLen &&
12541 "pack builtin LHSVecLen must equal to RHSVecLen");
12542
12543 const VectorType *VT0 = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
12544 const unsigned SrcBits = Info.Ctx.getIntWidth(T: VT0->getElementType());
12545
12546 const VectorType *DstVT = E->getType()->castAs<VectorType>();
12547 QualType DstElemTy = DstVT->getElementType();
12548 const bool DstIsUnsigned = DstElemTy->isUnsignedIntegerType();
12549
12550 const unsigned SrcPerLane = 128 / SrcBits;
12551 const unsigned Lanes = LHSVecLen * SrcBits / 128;
12552
12553 SmallVector<APValue, 64> Out;
12554 Out.reserve(N: LHSVecLen + RHSVecLen);
12555
12556 for (unsigned Lane = 0; Lane != Lanes; ++Lane) {
12557 unsigned base = Lane * SrcPerLane;
12558 for (unsigned I = 0; I != SrcPerLane; ++I)
12559 Out.emplace_back(Args: APValue(
12560 APSInt(PackFn(LHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12561 for (unsigned I = 0; I != SrcPerLane; ++I)
12562 Out.emplace_back(Args: APValue(
12563 APSInt(PackFn(RHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12564 }
12565
12566 Result = APValue(Out.data(), Out.size());
12567 return true;
12568}
12569
12570static bool evalShuffleGeneric(
12571 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12572 llvm::function_ref<std::pair<unsigned, int>(unsigned, unsigned)>
12573 GetSourceIndex) {
12574
12575 const auto *VT = Call->getType()->getAs<VectorType>();
12576 if (!VT)
12577 return false;
12578
12579 unsigned ShuffleMask = 0;
12580 APValue A, MaskVector, B;
12581 bool IsVectorMask = false;
12582 bool IsSingleOperand = (Call->getNumArgs() == 2);
12583
12584 if (IsSingleOperand) {
12585 QualType MaskType = Call->getArg(Arg: 1)->getType();
12586 if (MaskType->isVectorType()) {
12587 IsVectorMask = true;
12588 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12589 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector))
12590 return false;
12591 B = A;
12592 } else if (MaskType->isIntegerType()) {
12593 APSInt MaskImm;
12594 if (!EvaluateInteger(E: Call->getArg(Arg: 1), Result&: MaskImm, Info))
12595 return false;
12596 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12597 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A))
12598 return false;
12599 B = A;
12600 } else {
12601 return false;
12602 }
12603 } else {
12604 QualType Arg2Type = Call->getArg(Arg: 2)->getType();
12605 if (Arg2Type->isVectorType()) {
12606 IsVectorMask = true;
12607 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12608 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector) ||
12609 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 2), Result&: B))
12610 return false;
12611 } else if (Arg2Type->isIntegerType()) {
12612 APSInt MaskImm;
12613 if (!EvaluateInteger(E: Call->getArg(Arg: 2), Result&: MaskImm, Info))
12614 return false;
12615 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12616 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12617 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: B))
12618 return false;
12619 } else {
12620 return false;
12621 }
12622 }
12623
12624 unsigned NumElts = VT->getNumElements();
12625 SmallVector<APValue, 64> ResultElements;
12626 ResultElements.reserve(N: NumElts);
12627
12628 for (unsigned DstIdx = 0; DstIdx != NumElts; ++DstIdx) {
12629 if (IsVectorMask) {
12630 ShuffleMask = static_cast<unsigned>(
12631 MaskVector.getVectorElt(I: DstIdx).getInt().getZExtValue());
12632 }
12633 auto [SrcVecIdx, SrcIdx] = GetSourceIndex(DstIdx, ShuffleMask);
12634
12635 if (SrcIdx < 0) {
12636 // Zero out this element
12637 QualType ElemTy = VT->getElementType();
12638 if (ElemTy->isRealFloatingType()) {
12639 ResultElements.push_back(
12640 Elt: APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy))));
12641 } else if (ElemTy->isIntegerType()) {
12642 APValue Zero(Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy));
12643 ResultElements.push_back(Elt: APValue(Zero));
12644 } else {
12645 // Other types of fallback logic
12646 ResultElements.push_back(Elt: APValue());
12647 }
12648 } else {
12649 const APValue &Src = (SrcVecIdx == 0) ? A : B;
12650 ResultElements.push_back(Elt: Src.getVectorElt(I: SrcIdx));
12651 }
12652 }
12653
12654 Out = APValue(ResultElements.data(), ResultElements.size());
12655 return true;
12656}
12657static bool ConvertDoubleToFloatStrict(EvalInfo &Info, const Expr *E,
12658 APFloat OrigVal, APValue &Result) {
12659
12660 if (OrigVal.isInfinity()) {
12661 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 0;
12662 return false;
12663 }
12664 if (OrigVal.isNaN()) {
12665 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 1;
12666 return false;
12667 }
12668
12669 APFloat Val = OrigVal;
12670 bool LosesInfo = false;
12671 APFloat::opStatus Status = Val.convert(
12672 ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven, losesInfo: &LosesInfo);
12673
12674 if (LosesInfo || Val.isDenormal()) {
12675 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
12676 return false;
12677 }
12678
12679 if (Status != APFloat::opOK) {
12680 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
12681 return false;
12682 }
12683
12684 Result = APValue(Val);
12685 return true;
12686}
12687static bool evalShiftWithCount(
12688 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12689 llvm::function_ref<APInt(const APInt &, uint64_t)> ShiftOp,
12690 llvm::function_ref<APInt(const APInt &, unsigned)> OverflowOp) {
12691
12692 APValue Source, Count;
12693 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: Source) ||
12694 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: Count))
12695 return false;
12696
12697 assert(Call->getNumArgs() == 2);
12698
12699 QualType SourceTy = Call->getArg(Arg: 0)->getType();
12700 assert(SourceTy->isVectorType() &&
12701 Call->getArg(1)->getType()->isVectorType());
12702
12703 QualType DestEltTy = SourceTy->castAs<VectorType>()->getElementType();
12704 unsigned DestEltWidth = Source.getVectorElt(I: 0).getInt().getBitWidth();
12705 unsigned DestLen = Source.getVectorLength();
12706 bool IsDestUnsigned = DestEltTy->isUnsignedIntegerType();
12707 unsigned CountEltWidth = Count.getVectorElt(I: 0).getInt().getBitWidth();
12708 unsigned NumBitsInQWord = 64;
12709 unsigned NumCountElts = NumBitsInQWord / CountEltWidth;
12710 SmallVector<APValue, 64> Result;
12711 Result.reserve(N: DestLen);
12712
12713 uint64_t CountLQWord = 0;
12714 for (unsigned EltIdx = 0; EltIdx != NumCountElts; ++EltIdx) {
12715 uint64_t Elt = Count.getVectorElt(I: EltIdx).getInt().getZExtValue();
12716 CountLQWord |= (Elt << (EltIdx * CountEltWidth));
12717 }
12718
12719 for (unsigned EltIdx = 0; EltIdx != DestLen; ++EltIdx) {
12720 APInt Elt = Source.getVectorElt(I: EltIdx).getInt();
12721 if (CountLQWord < DestEltWidth) {
12722 Result.push_back(
12723 Elt: APValue(APSInt(ShiftOp(Elt, CountLQWord), IsDestUnsigned)));
12724 } else {
12725 Result.push_back(
12726 Elt: APValue(APSInt(OverflowOp(Elt, DestEltWidth), IsDestUnsigned)));
12727 }
12728 }
12729 Out = APValue(Result.data(), Result.size());
12730 return true;
12731}
12732
12733std::optional<APFloat> EvalScalarMinMaxFp(const APFloat &A, const APFloat &B,
12734 std::optional<APSInt> RoundingMode,
12735 bool IsMin) {
12736 APSInt DefaultMode(APInt(32, 4), /*isUnsigned=*/true);
12737 if (RoundingMode.value_or(u&: DefaultMode) != 4)
12738 return std::nullopt;
12739 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
12740 B.isInfinity() || B.isDenormal())
12741 return std::nullopt;
12742 if (A.isZero() && B.isZero())
12743 return B;
12744 return IsMin ? llvm::minimum(A, B) : llvm::maximum(A, B);
12745}
12746
12747bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
12748 if (!IsConstantEvaluatedBuiltinCall(E))
12749 return ExprEvaluatorBaseTy::VisitCallExpr(E);
12750
12751 unsigned BuiltinOp = ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E);
12752
12753 auto EvaluateBinOpExpr =
12754 [&](llvm::function_ref<APInt(const APSInt &, const APSInt &)> Fn) {
12755 APValue SourceLHS, SourceRHS;
12756 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12757 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12758 return false;
12759
12760 auto *DestTy = E->getType()->castAs<VectorType>();
12761 QualType DestEltTy = DestTy->getElementType();
12762 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12763 unsigned SourceLen = SourceLHS.getVectorLength();
12764 SmallVector<APValue, 4> ResultElements;
12765 ResultElements.reserve(N: SourceLen);
12766
12767 if (SourceRHS.isInt()) {
12768 const APSInt &RHS = SourceRHS.getInt();
12769 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12770 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12771 ResultElements.push_back(
12772 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12773 }
12774 } else {
12775 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12776 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12777 const APSInt &RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
12778 ResultElements.push_back(
12779 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12780 }
12781 }
12782 return Success(V: APValue(ResultElements.data(), SourceLen), E);
12783 };
12784
12785 auto EvaluateFpBinOpExpr =
12786 [&](llvm::function_ref<std::optional<APFloat>(
12787 const APFloat &, const APFloat &, std::optional<APSInt>)>
12788 Fn,
12789 bool IsScalar = false) {
12790 assert(E->getNumArgs() == 2 || E->getNumArgs() == 3);
12791 APValue A, B;
12792 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
12793 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
12794 return false;
12795
12796 assert(A.isVector() && B.isVector());
12797 assert(A.getVectorLength() == B.getVectorLength());
12798
12799 std::optional<APSInt> RoundingMode;
12800 if (E->getNumArgs() == 3) {
12801 APSInt Imm;
12802 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
12803 return false;
12804 RoundingMode = Imm;
12805 }
12806
12807 unsigned NumElems = A.getVectorLength();
12808 SmallVector<APValue, 4> ResultElements;
12809 ResultElements.reserve(N: NumElems);
12810
12811 for (unsigned EltNum = 0; EltNum < NumElems; ++EltNum) {
12812 if (IsScalar && EltNum > 0) {
12813 ResultElements.push_back(Elt: A.getVectorElt(I: EltNum));
12814 continue;
12815 }
12816 const APFloat &EltA = A.getVectorElt(I: EltNum).getFloat();
12817 const APFloat &EltB = B.getVectorElt(I: EltNum).getFloat();
12818 std::optional<APFloat> Result = Fn(EltA, EltB, RoundingMode);
12819 if (!Result)
12820 return false;
12821 ResultElements.push_back(Elt: APValue(*Result));
12822 }
12823 return Success(V: APValue(ResultElements.data(), NumElems), E);
12824 };
12825
12826 auto EvaluateScalarFpRoundMaskBinOp =
12827 [&](llvm::function_ref<std::optional<APFloat>(
12828 const APFloat &, const APFloat &, std::optional<APSInt>)>
12829 Fn) {
12830 assert(E->getNumArgs() == 5);
12831 APValue VecA, VecB, VecSrc;
12832 APSInt MaskVal, Rounding;
12833
12834 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
12835 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
12836 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
12837 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskVal, Info) ||
12838 !EvaluateInteger(E: E->getArg(Arg: 4), Result&: Rounding, Info))
12839 return false;
12840
12841 unsigned NumElems = VecA.getVectorLength();
12842 SmallVector<APValue, 8> ResultElements;
12843 ResultElements.reserve(N: NumElems);
12844
12845 if (MaskVal.getZExtValue() & 1) {
12846 const APFloat &EltA = VecA.getVectorElt(I: 0).getFloat();
12847 const APFloat &EltB = VecB.getVectorElt(I: 0).getFloat();
12848 std::optional<APFloat> Result = Fn(EltA, EltB, Rounding);
12849 if (!Result)
12850 return false;
12851 ResultElements.push_back(Elt: APValue(*Result));
12852 } else {
12853 ResultElements.push_back(Elt: VecSrc.getVectorElt(I: 0));
12854 }
12855
12856 for (unsigned I = 1; I < NumElems; ++I)
12857 ResultElements.push_back(Elt: VecA.getVectorElt(I));
12858
12859 return Success(V: APValue(ResultElements.data(), NumElems), E);
12860 };
12861
12862 auto EvalSelectScalar = [&](unsigned Len) -> bool {
12863 APSInt Mask;
12864 APValue AVal, WVal;
12865 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info) ||
12866 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: AVal) ||
12867 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: WVal))
12868 return false;
12869
12870 bool TakeA0 = (Mask.getZExtValue() & 1u) != 0;
12871 SmallVector<APValue, 4> Res;
12872 Res.reserve(N: Len);
12873 Res.push_back(Elt: TakeA0 ? AVal.getVectorElt(I: 0) : WVal.getVectorElt(I: 0));
12874 for (unsigned I = 1; I < Len; ++I)
12875 Res.push_back(Elt: WVal.getVectorElt(I));
12876 APValue V(Res.data(), Res.size());
12877 return Success(V, E);
12878 };
12879
12880 auto EvalVectorDotProduct = [&](bool IsSaturating) -> bool {
12881 APValue Source, OperandA, OperandB;
12882 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
12883 !EvaluateVector(E: E->getArg(Arg: 1), Result&: OperandA, Info) ||
12884 !EvaluateVector(E: E->getArg(Arg: 2), Result&: OperandB, Info)) {
12885 return false;
12886 }
12887
12888 unsigned NumSrcElems = Source.getVectorLength();
12889 unsigned NumOperandElems = OperandA.getVectorLength();
12890 unsigned ElemsPerLane = NumOperandElems / NumSrcElems;
12891
12892 assert(OperandA.getVectorLength() == OperandB.getVectorLength());
12893
12894 SmallVector<APValue, 16> Result;
12895 Result.reserve(N: NumSrcElems);
12896 for (unsigned I = 0; I != NumSrcElems; ++I) {
12897 APSInt DotProduct = Source.getVectorElt(I).getInt();
12898 DotProduct = DotProduct.extend(width: 64);
12899 for (unsigned J = 0; J != ElemsPerLane; ++J) {
12900 APSInt OpA = APSInt(
12901 OperandA.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
12902 false);
12903 APSInt OpB = APSInt(
12904 OperandB.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
12905 false);
12906 DotProduct += OpA * OpB;
12907 }
12908 if (IsSaturating) {
12909 DotProduct = APSInt(DotProduct.truncSSat(width: 32), false);
12910 } else {
12911 DotProduct = APSInt(DotProduct.trunc(width: 32), false);
12912 }
12913 Result.push_back(Elt: APValue(DotProduct));
12914 }
12915
12916 return Success(V: APValue(Result.data(), Result.size()), E);
12917 };
12918
12919 switch (BuiltinOp) {
12920 default:
12921 return false;
12922 case Builtin::BI__builtin_elementwise_popcount:
12923 case Builtin::BI__builtin_elementwise_bitreverse: {
12924 APValue Source;
12925 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
12926 return false;
12927
12928 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12929 unsigned SourceLen = Source.getVectorLength();
12930 SmallVector<APValue, 4> ResultElements;
12931 ResultElements.reserve(N: SourceLen);
12932
12933 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12934 APSInt Elt = Source.getVectorElt(I: EltNum).getInt();
12935 switch (BuiltinOp) {
12936 case Builtin::BI__builtin_elementwise_popcount:
12937 ResultElements.push_back(Elt: APValue(
12938 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), Elt.popcount()),
12939 DestEltTy->isUnsignedIntegerOrEnumerationType())));
12940 break;
12941 case Builtin::BI__builtin_elementwise_bitreverse:
12942 ResultElements.push_back(
12943 Elt: APValue(APSInt(Elt.reverseBits(),
12944 DestEltTy->isUnsignedIntegerOrEnumerationType())));
12945 break;
12946 }
12947 }
12948
12949 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12950 }
12951 case Builtin::BI__builtin_elementwise_abs: {
12952 APValue Source;
12953 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
12954 return false;
12955
12956 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12957 unsigned SourceLen = Source.getVectorLength();
12958 SmallVector<APValue, 4> ResultElements;
12959 ResultElements.reserve(N: SourceLen);
12960
12961 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12962 APValue CurrentEle = Source.getVectorElt(I: EltNum);
12963 APValue Val = DestEltTy->isFloatingType()
12964 ? APValue(llvm::abs(X: CurrentEle.getFloat()))
12965 : APValue(APSInt(
12966 CurrentEle.getInt().abs(),
12967 DestEltTy->isUnsignedIntegerOrEnumerationType()));
12968 ResultElements.push_back(Elt: Val);
12969 }
12970
12971 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12972 }
12973
12974 case Builtin::BI__builtin_elementwise_add_sat:
12975 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12976 return LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
12977 });
12978
12979 case Builtin::BI__builtin_elementwise_sub_sat:
12980 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12981 return LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
12982 });
12983
12984 case X86::BI__builtin_ia32_extract128i256:
12985 case X86::BI__builtin_ia32_vextractf128_pd256:
12986 case X86::BI__builtin_ia32_vextractf128_ps256:
12987 case X86::BI__builtin_ia32_vextractf128_si256: {
12988 APValue SourceVec, SourceImm;
12989 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
12990 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceImm))
12991 return false;
12992
12993 if (!SourceVec.isVector())
12994 return false;
12995
12996 const auto *RetVT = E->getType()->castAs<VectorType>();
12997 unsigned RetLen = RetVT->getNumElements();
12998 unsigned Idx = SourceImm.getInt().getZExtValue() & 1;
12999
13000 SmallVector<APValue, 32> ResultElements;
13001 ResultElements.reserve(N: RetLen);
13002
13003 for (unsigned I = 0; I < RetLen; I++)
13004 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Idx * RetLen + I));
13005
13006 return Success(V: APValue(ResultElements.data(), RetLen), E);
13007 }
13008
13009 case clang::X86::BI__builtin_ia32_cvtmask2b128:
13010 case clang::X86::BI__builtin_ia32_cvtmask2b256:
13011 case clang::X86::BI__builtin_ia32_cvtmask2b512:
13012 case clang::X86::BI__builtin_ia32_cvtmask2w128:
13013 case clang::X86::BI__builtin_ia32_cvtmask2w256:
13014 case clang::X86::BI__builtin_ia32_cvtmask2w512:
13015 case clang::X86::BI__builtin_ia32_cvtmask2d128:
13016 case clang::X86::BI__builtin_ia32_cvtmask2d256:
13017 case clang::X86::BI__builtin_ia32_cvtmask2d512:
13018 case clang::X86::BI__builtin_ia32_cvtmask2q128:
13019 case clang::X86::BI__builtin_ia32_cvtmask2q256:
13020 case clang::X86::BI__builtin_ia32_cvtmask2q512: {
13021 assert(E->getNumArgs() == 1);
13022 APSInt Mask;
13023 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info))
13024 return false;
13025
13026 QualType VecTy = E->getType();
13027 const VectorType *VT = VecTy->castAs<VectorType>();
13028 unsigned VectorLen = VT->getNumElements();
13029 QualType ElemTy = VT->getElementType();
13030 unsigned ElemWidth = Info.Ctx.getTypeSize(T: ElemTy);
13031
13032 SmallVector<APValue, 16> Elems;
13033 for (unsigned I = 0; I != VectorLen; ++I) {
13034 bool BitSet = Mask[I];
13035 APSInt ElemVal(ElemWidth, /*isUnsigned=*/false);
13036 if (BitSet) {
13037 ElemVal.setAllBits();
13038 }
13039 Elems.push_back(Elt: APValue(ElemVal));
13040 }
13041 return Success(V: APValue(Elems.data(), VectorLen), E);
13042 }
13043
13044 case X86::BI__builtin_ia32_extracti32x4_256_mask:
13045 case X86::BI__builtin_ia32_extractf32x4_256_mask:
13046 case X86::BI__builtin_ia32_extracti32x4_mask:
13047 case X86::BI__builtin_ia32_extractf32x4_mask:
13048 case X86::BI__builtin_ia32_extracti32x8_mask:
13049 case X86::BI__builtin_ia32_extractf32x8_mask:
13050 case X86::BI__builtin_ia32_extracti64x2_256_mask:
13051 case X86::BI__builtin_ia32_extractf64x2_256_mask:
13052 case X86::BI__builtin_ia32_extracti64x2_512_mask:
13053 case X86::BI__builtin_ia32_extractf64x2_512_mask:
13054 case X86::BI__builtin_ia32_extracti64x4_mask:
13055 case X86::BI__builtin_ia32_extractf64x4_mask: {
13056 APValue SourceVec, MergeVec;
13057 APSInt Imm, MaskImm;
13058
13059 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
13060 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info) ||
13061 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MergeVec) ||
13062 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskImm, Info))
13063 return false;
13064
13065 const auto *RetVT = E->getType()->castAs<VectorType>();
13066 unsigned RetLen = RetVT->getNumElements();
13067
13068 if (!SourceVec.isVector() || !MergeVec.isVector())
13069 return false;
13070 unsigned SrcLen = SourceVec.getVectorLength();
13071 unsigned Lanes = SrcLen / RetLen;
13072 unsigned Lane = static_cast<unsigned>(Imm.getZExtValue() % Lanes);
13073 unsigned Base = Lane * RetLen;
13074
13075 SmallVector<APValue, 32> ResultElements;
13076 ResultElements.reserve(N: RetLen);
13077 for (unsigned I = 0; I < RetLen; ++I) {
13078 if (MaskImm[I])
13079 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Base + I));
13080 else
13081 ResultElements.push_back(Elt: MergeVec.getVectorElt(I));
13082 }
13083 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13084 }
13085
13086 case clang::X86::BI__builtin_ia32_pavgb128:
13087 case clang::X86::BI__builtin_ia32_pavgw128:
13088 case clang::X86::BI__builtin_ia32_pavgb256:
13089 case clang::X86::BI__builtin_ia32_pavgw256:
13090 case clang::X86::BI__builtin_ia32_pavgb512:
13091 case clang::X86::BI__builtin_ia32_pavgw512:
13092 return EvaluateBinOpExpr(llvm::APIntOps::avgCeilU);
13093
13094 case clang::X86::BI__builtin_ia32_pmulhrsw128:
13095 case clang::X86::BI__builtin_ia32_pmulhrsw256:
13096 case clang::X86::BI__builtin_ia32_pmulhrsw512:
13097 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13098 return (llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS).ashr(ShiftAmt: 14) + 1)
13099 .extractBits(numBits: 16, bitPosition: 1);
13100 });
13101
13102 case clang::X86::BI__builtin_ia32_psadbw128:
13103 case clang::X86::BI__builtin_ia32_psadbw256:
13104 case clang::X86::BI__builtin_ia32_psadbw512: {
13105 APValue SourceLHS, SourceRHS;
13106 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13107 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13108 return false;
13109
13110 assert(SourceLHS.isVector() && SourceRHS.isVector());
13111 unsigned SourceLen = SourceLHS.getVectorLength();
13112 assert(SourceLen == SourceRHS.getVectorLength());
13113 assert((SourceLen % 8) == 0);
13114
13115 auto *DestTy = E->getType()->castAs<VectorType>();
13116 QualType DestEltTy = DestTy->getElementType();
13117 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13118 SmallVector<APValue, 8> ResultElements;
13119 ResultElements.reserve(N: SourceLen / 8);
13120
13121 for (unsigned Lane = 0; Lane != SourceLen; Lane += 8) {
13122 APInt Sum(64, 0);
13123 for (unsigned I = 0; I != 8; ++I) {
13124 APInt LHS = SourceLHS.getVectorElt(I: Lane + I).getInt().extOrTrunc(width: 8);
13125 APInt RHS = SourceRHS.getVectorElt(I: Lane + I).getInt().extOrTrunc(width: 8);
13126 Sum += llvm::APIntOps::abdu(A: LHS, B: RHS).zext(width: 64);
13127 }
13128 ResultElements.push_back(Elt: APValue(APSInt(Sum, DestUnsigned)));
13129 }
13130
13131 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13132 }
13133
13134 case clang::X86::BI__builtin_ia32_pmaddubsw128:
13135 case clang::X86::BI__builtin_ia32_pmaddubsw256:
13136 case clang::X86::BI__builtin_ia32_pmaddubsw512:
13137 case clang::X86::BI__builtin_ia32_pmaddwd128:
13138 case clang::X86::BI__builtin_ia32_pmaddwd256:
13139 case clang::X86::BI__builtin_ia32_pmaddwd512: {
13140 APValue SourceLHS, SourceRHS;
13141 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13142 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13143 return false;
13144
13145 auto *DestTy = E->getType()->castAs<VectorType>();
13146 QualType DestEltTy = DestTy->getElementType();
13147 unsigned SourceLen = SourceLHS.getVectorLength();
13148 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13149 SmallVector<APValue, 4> ResultElements;
13150 ResultElements.reserve(N: SourceLen / 2);
13151
13152 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
13153 const APSInt &LoLHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13154 const APSInt &HiLHS = SourceLHS.getVectorElt(I: EltNum + 1).getInt();
13155 const APSInt &LoRHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13156 const APSInt &HiRHS = SourceRHS.getVectorElt(I: EltNum + 1).getInt();
13157 unsigned BitWidth = 2 * LoLHS.getBitWidth();
13158
13159 switch (BuiltinOp) {
13160 case clang::X86::BI__builtin_ia32_pmaddubsw128:
13161 case clang::X86::BI__builtin_ia32_pmaddubsw256:
13162 case clang::X86::BI__builtin_ia32_pmaddubsw512:
13163 ResultElements.push_back(Elt: APValue(
13164 APSInt((LoLHS.zext(width: BitWidth) * LoRHS.sext(width: BitWidth))
13165 .sadd_sat(RHS: (HiLHS.zext(width: BitWidth) * HiRHS.sext(width: BitWidth))),
13166 DestUnsigned)));
13167 break;
13168 case clang::X86::BI__builtin_ia32_pmaddwd128:
13169 case clang::X86::BI__builtin_ia32_pmaddwd256:
13170 case clang::X86::BI__builtin_ia32_pmaddwd512:
13171 ResultElements.push_back(
13172 Elt: APValue(APSInt((LoLHS.sext(width: BitWidth) * LoRHS.sext(width: BitWidth)) +
13173 (HiLHS.sext(width: BitWidth) * HiRHS.sext(width: BitWidth)),
13174 DestUnsigned)));
13175 break;
13176 }
13177 }
13178
13179 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13180 }
13181
13182 case clang::X86::BI__builtin_ia32_bmacor16x16x16_v16hi:
13183 case clang::X86::BI__builtin_ia32_bmacor16x16x16_v32hi:
13184 case clang::X86::BI__builtin_ia32_bmacxor16x16x16_v16hi:
13185 case clang::X86::BI__builtin_ia32_bmacxor16x16x16_v32hi: {
13186 // Bit Matrix Multiply and Accumulate (AVX512BMM). Each 256-bit lane holds
13187 // a 16x16 bit matrix as 16 x i16 elements; element i is row i and bit j of
13188 // that element is entry [i][j]. The accumulator (third argument, src1 in
13189 // the AMD ISA) provides the initial value of each result bit, into which
13190 // the bit-matrix product of the first two arguments (src2 * src3) is
13191 // reduced with OR (vbmacor) or XOR (vbmacxor):
13192 // for i in 0..15, j in 0..15:
13193 // bit = C[16*i+j]
13194 // for k in 0..15: bit OP= A[16*i+k] & B[16*k+j]
13195 // dest[16*i+j] = bit
13196 APValue SourceA, SourceB, SourceC;
13197 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
13198 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
13199 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13200 return false;
13201
13202 bool IsXor = E->getBuiltinCallee() ==
13203 clang::X86::BI__builtin_ia32_bmacxor16x16x16_v16hi ||
13204 E->getBuiltinCallee() ==
13205 clang::X86::BI__builtin_ia32_bmacxor16x16x16_v32hi;
13206
13207 unsigned SourceLen = SourceA.getVectorLength();
13208 assert(SourceLen % 16 == 0 && "BMM operates on 256-bit lanes of 16 x i16");
13209 auto *DestTy = E->getType()->castAs<VectorType>();
13210 QualType DestEltTy = DestTy->getElementType();
13211 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13212
13213 SmallVector<APValue, 32> ResultElements(SourceLen);
13214 for (unsigned Lane = 0; Lane != SourceLen; Lane += 16) {
13215 for (unsigned I = 0; I != 16; ++I) {
13216 uint16_t A =
13217 (uint16_t)SourceA.getVectorElt(I: Lane + I).getInt().getZExtValue();
13218 uint16_t Dst =
13219 (uint16_t)SourceC.getVectorElt(I: Lane + I).getInt().getZExtValue();
13220 for (unsigned J = 0; J != 16; ++J) {
13221 // Seed the reduction with the accumulator bit, then fold in each
13222 // product term with the same operator (OR for vbmacor, XOR for
13223 // vbmacxor).
13224 unsigned Bit = (Dst >> J) & 1u;
13225 for (unsigned K = 0; K != 16; ++K) {
13226 uint16_t B = (uint16_t)SourceB.getVectorElt(I: Lane + K)
13227 .getInt()
13228 .getZExtValue();
13229 unsigned Product = ((A >> K) & 1u) & ((B >> J) & 1u);
13230 Bit = IsXor ? (Bit ^ Product) : (Bit | Product);
13231 }
13232 Dst = (Dst & ~(uint16_t(1) << J)) | (uint16_t(Bit) << J);
13233 }
13234 ResultElements[Lane + I] =
13235 APValue(APSInt(APInt(16, Dst), DestUnsigned));
13236 }
13237 }
13238 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13239 }
13240
13241 case clang::X86::BI__builtin_ia32_dbpsadbw128:
13242 case clang::X86::BI__builtin_ia32_dbpsadbw256:
13243 case clang::X86::BI__builtin_ia32_dbpsadbw512: {
13244 APValue SourceA, SourceB, SourceImm;
13245 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
13246 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
13247 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceImm))
13248 return false;
13249
13250 unsigned SourceLen = SourceA.getVectorLength();
13251 constexpr unsigned LaneSize = 16; // 128-bit lane = 16 bytes
13252 unsigned Imm = SourceImm.getInt().getZExtValue();
13253
13254 auto *DestTy = E->getType()->castAs<VectorType>();
13255 QualType DestEltTy = DestTy->getElementType();
13256 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13257 SmallVector<APValue, 32> ResultElements;
13258 ResultElements.reserve(N: SourceLen / 2);
13259
13260 // Phase 1: Shuffle SourceB using all four 2-bit fields of imm8.
13261 // Within each 128-bit lane, for group j (0..3), select a 4-byte block
13262 // from SourceB based on bits [2*j+1:2*j] of imm8.
13263 SmallVector<uint8_t, 64> Shuffled(SourceLen);
13264 for (unsigned I = 0; I < SourceLen; I += LaneSize) {
13265 for (unsigned J = 0; J < 4; ++J) {
13266 unsigned Part = (Imm >> (2 * J)) & 3;
13267 for (unsigned K = 0; K < 4; ++K) {
13268 Shuffled[I + 4 * J + K] = static_cast<uint8_t>(
13269 SourceB.getVectorElt(I: I + 4 * Part + K).getInt().getZExtValue());
13270 }
13271 }
13272 }
13273
13274 // Phase 2: Sliding SAD computation.
13275 // For every group of 4 output u16 values, compute absolute differences
13276 // using overlapping windows into SourceA and the shuffled array.
13277 unsigned Size = SourceLen / 2; // number of output u16 elements
13278 for (unsigned I = 0; I < Size; I += 4) {
13279 unsigned Sad[4] = {0, 0, 0, 0};
13280 for (unsigned J = 0; J < 4; ++J) {
13281 uint8_t A1 = static_cast<uint8_t>(
13282 SourceA.getVectorElt(I: 2 * I + J).getInt().getZExtValue());
13283 uint8_t A2 = static_cast<uint8_t>(
13284 SourceA.getVectorElt(I: 2 * I + J + 4).getInt().getZExtValue());
13285 uint8_t B0 = Shuffled[2 * I + J];
13286 uint8_t B1 = Shuffled[2 * I + J + 1];
13287 uint8_t B2 = Shuffled[2 * I + J + 2];
13288 uint8_t B3 = Shuffled[2 * I + J + 3];
13289 Sad[0] += (A1 > B0) ? (A1 - B0) : (B0 - A1);
13290 Sad[1] += (A1 > B1) ? (A1 - B1) : (B1 - A1);
13291 Sad[2] += (A2 > B2) ? (A2 - B2) : (B2 - A2);
13292 Sad[3] += (A2 > B3) ? (A2 - B3) : (B3 - A2);
13293 }
13294 for (unsigned R = 0; R < 4; ++R)
13295 ResultElements.push_back(
13296 Elt: APValue(APSInt(APInt(16, Sad[R]), DestUnsigned)));
13297 }
13298
13299 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13300 }
13301
13302 case clang::X86::BI__builtin_ia32_mpsadbw128:
13303 case clang::X86::BI__builtin_ia32_mpsadbw256: {
13304 APValue SourceA, SourceB;
13305 APSInt SourceImm;
13306 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: SourceA, Info) ||
13307 !EvaluateVector(E: E->getArg(Arg: 1), Result&: SourceB, Info) ||
13308 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: SourceImm, Info))
13309 return false;
13310 unsigned SourceLen = SourceA.getVectorLength();
13311 constexpr unsigned LaneSize = 16;
13312 assert((SourceLen == LaneSize || SourceLen == 2 * LaneSize) &&
13313 "MPSADBW operates on 128-bit or 256-bit vectors");
13314 unsigned NumLanes = SourceLen / LaneSize;
13315 unsigned Imm = SourceImm.getZExtValue();
13316
13317 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13318 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13319 SmallVector<APValue, 16> ResultElements;
13320 ResultElements.reserve(N: SourceLen / 2);
13321
13322 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
13323 unsigned Ctrl = (Imm >> (3 * Lane)) & 0x7;
13324 unsigned AOff = ((Ctrl >> 2) & 1) * 4;
13325 unsigned BOff = (Ctrl & 3) * 4;
13326 for (unsigned J = 0; J != 8; ++J) {
13327 uint16_t Sad = 0;
13328 for (unsigned K = 0; K != 4; ++K) {
13329 uint8_t A = static_cast<uint8_t>(
13330 SourceA.getVectorElt(I: Lane * LaneSize + AOff + J + K)
13331 .getInt()
13332 .getZExtValue());
13333 uint8_t B = static_cast<uint8_t>(
13334 SourceB.getVectorElt(I: Lane * LaneSize + BOff + K)
13335 .getInt()
13336 .getZExtValue());
13337 Sad += (A > B) ? (A - B) : (B - A);
13338 }
13339 ResultElements.push_back(Elt: APValue(APSInt(APInt(16, Sad), DestUnsigned)));
13340 }
13341 }
13342 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13343 }
13344
13345 case clang::X86::BI__builtin_ia32_pmulhuw128:
13346 case clang::X86::BI__builtin_ia32_pmulhuw256:
13347 case clang::X86::BI__builtin_ia32_pmulhuw512:
13348 return EvaluateBinOpExpr(llvm::APIntOps::mulhu);
13349
13350 case clang::X86::BI__builtin_ia32_pmulhw128:
13351 case clang::X86::BI__builtin_ia32_pmulhw256:
13352 case clang::X86::BI__builtin_ia32_pmulhw512:
13353 return EvaluateBinOpExpr(llvm::APIntOps::mulhs);
13354
13355 case clang::X86::BI__builtin_ia32_psllv2di:
13356 case clang::X86::BI__builtin_ia32_psllv4di:
13357 case clang::X86::BI__builtin_ia32_psllv4si:
13358 case clang::X86::BI__builtin_ia32_psllv8di:
13359 case clang::X86::BI__builtin_ia32_psllv8hi:
13360 case clang::X86::BI__builtin_ia32_psllv8si:
13361 case clang::X86::BI__builtin_ia32_psllv16hi:
13362 case clang::X86::BI__builtin_ia32_psllv16si:
13363 case clang::X86::BI__builtin_ia32_psllv32hi:
13364 case clang::X86::BI__builtin_ia32_psllwi128:
13365 case clang::X86::BI__builtin_ia32_pslldi128:
13366 case clang::X86::BI__builtin_ia32_psllqi128:
13367 case clang::X86::BI__builtin_ia32_psllwi256:
13368 case clang::X86::BI__builtin_ia32_pslldi256:
13369 case clang::X86::BI__builtin_ia32_psllqi256:
13370 case clang::X86::BI__builtin_ia32_psllwi512:
13371 case clang::X86::BI__builtin_ia32_pslldi512:
13372 case clang::X86::BI__builtin_ia32_psllqi512:
13373 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13374 if (RHS.uge(RHS: LHS.getBitWidth())) {
13375 return APInt::getZero(numBits: LHS.getBitWidth());
13376 }
13377 return LHS.shl(shiftAmt: RHS.getZExtValue());
13378 });
13379
13380 case clang::X86::BI__builtin_ia32_psrav4si:
13381 case clang::X86::BI__builtin_ia32_psrav8di:
13382 case clang::X86::BI__builtin_ia32_psrav8hi:
13383 case clang::X86::BI__builtin_ia32_psrav8si:
13384 case clang::X86::BI__builtin_ia32_psrav16hi:
13385 case clang::X86::BI__builtin_ia32_psrav16si:
13386 case clang::X86::BI__builtin_ia32_psrav32hi:
13387 case clang::X86::BI__builtin_ia32_psravq128:
13388 case clang::X86::BI__builtin_ia32_psravq256:
13389 case clang::X86::BI__builtin_ia32_psrawi128:
13390 case clang::X86::BI__builtin_ia32_psradi128:
13391 case clang::X86::BI__builtin_ia32_psraqi128:
13392 case clang::X86::BI__builtin_ia32_psrawi256:
13393 case clang::X86::BI__builtin_ia32_psradi256:
13394 case clang::X86::BI__builtin_ia32_psraqi256:
13395 case clang::X86::BI__builtin_ia32_psrawi512:
13396 case clang::X86::BI__builtin_ia32_psradi512:
13397 case clang::X86::BI__builtin_ia32_psraqi512:
13398 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13399 if (RHS.uge(RHS: LHS.getBitWidth())) {
13400 return LHS.ashr(ShiftAmt: LHS.getBitWidth() - 1);
13401 }
13402 return LHS.ashr(ShiftAmt: RHS.getZExtValue());
13403 });
13404
13405 case clang::X86::BI__builtin_ia32_psrlv2di:
13406 case clang::X86::BI__builtin_ia32_psrlv4di:
13407 case clang::X86::BI__builtin_ia32_psrlv4si:
13408 case clang::X86::BI__builtin_ia32_psrlv8di:
13409 case clang::X86::BI__builtin_ia32_psrlv8hi:
13410 case clang::X86::BI__builtin_ia32_psrlv8si:
13411 case clang::X86::BI__builtin_ia32_psrlv16hi:
13412 case clang::X86::BI__builtin_ia32_psrlv16si:
13413 case clang::X86::BI__builtin_ia32_psrlv32hi:
13414 case clang::X86::BI__builtin_ia32_psrlwi128:
13415 case clang::X86::BI__builtin_ia32_psrldi128:
13416 case clang::X86::BI__builtin_ia32_psrlqi128:
13417 case clang::X86::BI__builtin_ia32_psrlwi256:
13418 case clang::X86::BI__builtin_ia32_psrldi256:
13419 case clang::X86::BI__builtin_ia32_psrlqi256:
13420 case clang::X86::BI__builtin_ia32_psrlwi512:
13421 case clang::X86::BI__builtin_ia32_psrldi512:
13422 case clang::X86::BI__builtin_ia32_psrlqi512:
13423 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13424 if (RHS.uge(RHS: LHS.getBitWidth())) {
13425 return APInt::getZero(numBits: LHS.getBitWidth());
13426 }
13427 return LHS.lshr(shiftAmt: RHS.getZExtValue());
13428 });
13429 case X86::BI__builtin_ia32_packsswb128:
13430 case X86::BI__builtin_ia32_packsswb256:
13431 case X86::BI__builtin_ia32_packsswb512:
13432 case X86::BI__builtin_ia32_packssdw128:
13433 case X86::BI__builtin_ia32_packssdw256:
13434 case X86::BI__builtin_ia32_packssdw512:
13435 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
13436 return APSInt(Src).truncSSat(width: Src.getBitWidth() / 2);
13437 });
13438 case X86::BI__builtin_ia32_packusdw128:
13439 case X86::BI__builtin_ia32_packusdw256:
13440 case X86::BI__builtin_ia32_packusdw512:
13441 case X86::BI__builtin_ia32_packuswb128:
13442 case X86::BI__builtin_ia32_packuswb256:
13443 case X86::BI__builtin_ia32_packuswb512:
13444 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
13445 return APSInt(Src).truncSSatU(width: Src.getBitWidth() / 2);
13446 });
13447 case clang::X86::BI__builtin_ia32_selectss_128:
13448 return EvalSelectScalar(4);
13449 case clang::X86::BI__builtin_ia32_selectsd_128:
13450 return EvalSelectScalar(2);
13451 case clang::X86::BI__builtin_ia32_selectsh_128:
13452 case clang::X86::BI__builtin_ia32_selectsbf_128:
13453 return EvalSelectScalar(8);
13454 case clang::X86::BI__builtin_ia32_pmuldq128:
13455 case clang::X86::BI__builtin_ia32_pmuldq256:
13456 case clang::X86::BI__builtin_ia32_pmuldq512:
13457 case clang::X86::BI__builtin_ia32_pmuludq128:
13458 case clang::X86::BI__builtin_ia32_pmuludq256:
13459 case clang::X86::BI__builtin_ia32_pmuludq512: {
13460 APValue SourceLHS, SourceRHS;
13461 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13462 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13463 return false;
13464
13465 unsigned SourceLen = SourceLHS.getVectorLength();
13466 SmallVector<APValue, 4> ResultElements;
13467 ResultElements.reserve(N: SourceLen / 2);
13468
13469 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
13470 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13471 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13472
13473 switch (BuiltinOp) {
13474 case clang::X86::BI__builtin_ia32_pmuludq128:
13475 case clang::X86::BI__builtin_ia32_pmuludq256:
13476 case clang::X86::BI__builtin_ia32_pmuludq512:
13477 ResultElements.push_back(
13478 Elt: APValue(APSInt(llvm::APIntOps::muluExtended(C1: LHS, C2: RHS), true)));
13479 break;
13480 case clang::X86::BI__builtin_ia32_pmuldq128:
13481 case clang::X86::BI__builtin_ia32_pmuldq256:
13482 case clang::X86::BI__builtin_ia32_pmuldq512:
13483 ResultElements.push_back(
13484 Elt: APValue(APSInt(llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS), false)));
13485 break;
13486 }
13487 }
13488
13489 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13490 }
13491
13492 case X86::BI__builtin_ia32_vpmadd52luq128:
13493 case X86::BI__builtin_ia32_vpmadd52luq256:
13494 case X86::BI__builtin_ia32_vpmadd52luq512: {
13495 APValue A, B, C;
13496 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
13497 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
13498 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
13499 return false;
13500
13501 unsigned ALen = A.getVectorLength();
13502 SmallVector<APValue, 4> ResultElements;
13503 ResultElements.reserve(N: ALen);
13504
13505 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
13506 APInt AElt = A.getVectorElt(I: EltNum).getInt();
13507 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13508 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13509 APSInt ResElt(AElt + (BElt * CElt).zext(width: 64), false);
13510 ResultElements.push_back(Elt: APValue(ResElt));
13511 }
13512
13513 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13514 }
13515 case X86::BI__builtin_ia32_vpmadd52huq128:
13516 case X86::BI__builtin_ia32_vpmadd52huq256:
13517 case X86::BI__builtin_ia32_vpmadd52huq512: {
13518 APValue A, B, C;
13519 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
13520 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
13521 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
13522 return false;
13523
13524 unsigned ALen = A.getVectorLength();
13525 SmallVector<APValue, 4> ResultElements;
13526 ResultElements.reserve(N: ALen);
13527
13528 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
13529 APInt AElt = A.getVectorElt(I: EltNum).getInt();
13530 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13531 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13532 APSInt ResElt(AElt + llvm::APIntOps::mulhu(C1: BElt, C2: CElt).zext(width: 64), false);
13533 ResultElements.push_back(Elt: APValue(ResElt));
13534 }
13535
13536 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13537 }
13538
13539 case clang::X86::BI__builtin_ia32_vprotbi:
13540 case clang::X86::BI__builtin_ia32_vprotdi:
13541 case clang::X86::BI__builtin_ia32_vprotqi:
13542 case clang::X86::BI__builtin_ia32_vprotwi:
13543 case clang::X86::BI__builtin_ia32_prold128:
13544 case clang::X86::BI__builtin_ia32_prold256:
13545 case clang::X86::BI__builtin_ia32_prold512:
13546 case clang::X86::BI__builtin_ia32_prolq128:
13547 case clang::X86::BI__builtin_ia32_prolq256:
13548 case clang::X86::BI__builtin_ia32_prolq512:
13549 return EvaluateBinOpExpr(
13550 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotl(rotateAmt: RHS); });
13551
13552 case clang::X86::BI__builtin_ia32_prord128:
13553 case clang::X86::BI__builtin_ia32_prord256:
13554 case clang::X86::BI__builtin_ia32_prord512:
13555 case clang::X86::BI__builtin_ia32_prorq128:
13556 case clang::X86::BI__builtin_ia32_prorq256:
13557 case clang::X86::BI__builtin_ia32_prorq512:
13558 return EvaluateBinOpExpr(
13559 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotr(rotateAmt: RHS); });
13560
13561 case Builtin::BI__builtin_elementwise_max:
13562 case Builtin::BI__builtin_elementwise_min: {
13563 APValue SourceLHS, SourceRHS;
13564 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13565 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13566 return false;
13567
13568 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13569
13570 if (!DestEltTy->isIntegerType())
13571 return false;
13572
13573 unsigned SourceLen = SourceLHS.getVectorLength();
13574 SmallVector<APValue, 4> ResultElements;
13575 ResultElements.reserve(N: SourceLen);
13576
13577 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13578 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13579 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13580 switch (BuiltinOp) {
13581 case Builtin::BI__builtin_elementwise_max:
13582 ResultElements.push_back(
13583 Elt: APValue(APSInt(std::max(a: LHS, b: RHS),
13584 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13585 break;
13586 case Builtin::BI__builtin_elementwise_min:
13587 ResultElements.push_back(
13588 Elt: APValue(APSInt(std::min(a: LHS, b: RHS),
13589 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13590 break;
13591 }
13592 }
13593
13594 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13595 }
13596 case X86::BI__builtin_ia32_vpshldd128:
13597 case X86::BI__builtin_ia32_vpshldd256:
13598 case X86::BI__builtin_ia32_vpshldd512:
13599 case X86::BI__builtin_ia32_vpshldq128:
13600 case X86::BI__builtin_ia32_vpshldq256:
13601 case X86::BI__builtin_ia32_vpshldq512:
13602 case X86::BI__builtin_ia32_vpshldw128:
13603 case X86::BI__builtin_ia32_vpshldw256:
13604 case X86::BI__builtin_ia32_vpshldw512: {
13605 APValue SourceHi, SourceLo, SourceAmt;
13606 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
13607 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
13608 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13609 return false;
13610
13611 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13612 unsigned SourceLen = SourceHi.getVectorLength();
13613 SmallVector<APValue, 32> ResultElements;
13614 ResultElements.reserve(N: SourceLen);
13615
13616 APInt Amt = SourceAmt.getInt();
13617 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13618 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13619 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13620 APInt R = llvm::APIntOps::fshl(Hi, Lo, Shift: Amt);
13621 ResultElements.push_back(
13622 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13623 }
13624
13625 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13626 }
13627 case X86::BI__builtin_ia32_vpshrdd128:
13628 case X86::BI__builtin_ia32_vpshrdd256:
13629 case X86::BI__builtin_ia32_vpshrdd512:
13630 case X86::BI__builtin_ia32_vpshrdq128:
13631 case X86::BI__builtin_ia32_vpshrdq256:
13632 case X86::BI__builtin_ia32_vpshrdq512:
13633 case X86::BI__builtin_ia32_vpshrdw128:
13634 case X86::BI__builtin_ia32_vpshrdw256:
13635 case X86::BI__builtin_ia32_vpshrdw512: {
13636 // NOTE: Reversed Hi/Lo operands.
13637 APValue SourceHi, SourceLo, SourceAmt;
13638 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLo) ||
13639 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceHi) ||
13640 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13641 return false;
13642
13643 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13644 unsigned SourceLen = SourceHi.getVectorLength();
13645 SmallVector<APValue, 32> ResultElements;
13646 ResultElements.reserve(N: SourceLen);
13647
13648 APInt Amt = SourceAmt.getInt();
13649 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13650 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13651 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13652 APInt R = llvm::APIntOps::fshr(Hi, Lo, Shift: Amt);
13653 ResultElements.push_back(
13654 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13655 }
13656
13657 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13658 }
13659 case X86::BI__builtin_ia32_compressdf128_mask:
13660 case X86::BI__builtin_ia32_compressdf256_mask:
13661 case X86::BI__builtin_ia32_compressdf512_mask:
13662 case X86::BI__builtin_ia32_compressdi128_mask:
13663 case X86::BI__builtin_ia32_compressdi256_mask:
13664 case X86::BI__builtin_ia32_compressdi512_mask:
13665 case X86::BI__builtin_ia32_compresshi128_mask:
13666 case X86::BI__builtin_ia32_compresshi256_mask:
13667 case X86::BI__builtin_ia32_compresshi512_mask:
13668 case X86::BI__builtin_ia32_compressqi128_mask:
13669 case X86::BI__builtin_ia32_compressqi256_mask:
13670 case X86::BI__builtin_ia32_compressqi512_mask:
13671 case X86::BI__builtin_ia32_compresssf128_mask:
13672 case X86::BI__builtin_ia32_compresssf256_mask:
13673 case X86::BI__builtin_ia32_compresssf512_mask:
13674 case X86::BI__builtin_ia32_compresssi128_mask:
13675 case X86::BI__builtin_ia32_compresssi256_mask:
13676 case X86::BI__builtin_ia32_compresssi512_mask: {
13677 APValue Source, Passthru;
13678 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13679 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13680 return false;
13681 APSInt Mask;
13682 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13683 return false;
13684
13685 unsigned NumElts = Source.getVectorLength();
13686 SmallVector<APValue, 64> ResultElements;
13687 ResultElements.reserve(N: NumElts);
13688
13689 for (unsigned I = 0; I != NumElts; ++I) {
13690 if (Mask[I])
13691 ResultElements.push_back(Elt: Source.getVectorElt(I));
13692 }
13693 for (unsigned I = ResultElements.size(); I != NumElts; ++I) {
13694 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13695 }
13696
13697 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13698 }
13699 case X86::BI__builtin_ia32_expanddf128_mask:
13700 case X86::BI__builtin_ia32_expanddf256_mask:
13701 case X86::BI__builtin_ia32_expanddf512_mask:
13702 case X86::BI__builtin_ia32_expanddi128_mask:
13703 case X86::BI__builtin_ia32_expanddi256_mask:
13704 case X86::BI__builtin_ia32_expanddi512_mask:
13705 case X86::BI__builtin_ia32_expandhi128_mask:
13706 case X86::BI__builtin_ia32_expandhi256_mask:
13707 case X86::BI__builtin_ia32_expandhi512_mask:
13708 case X86::BI__builtin_ia32_expandqi128_mask:
13709 case X86::BI__builtin_ia32_expandqi256_mask:
13710 case X86::BI__builtin_ia32_expandqi512_mask:
13711 case X86::BI__builtin_ia32_expandsf128_mask:
13712 case X86::BI__builtin_ia32_expandsf256_mask:
13713 case X86::BI__builtin_ia32_expandsf512_mask:
13714 case X86::BI__builtin_ia32_expandsi128_mask:
13715 case X86::BI__builtin_ia32_expandsi256_mask:
13716 case X86::BI__builtin_ia32_expandsi512_mask: {
13717 APValue Source, Passthru;
13718 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13719 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13720 return false;
13721 APSInt Mask;
13722 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13723 return false;
13724
13725 unsigned NumElts = Source.getVectorLength();
13726 SmallVector<APValue, 64> ResultElements;
13727 ResultElements.reserve(N: NumElts);
13728
13729 unsigned SourceIdx = 0;
13730 for (unsigned I = 0; I != NumElts; ++I) {
13731 if (Mask[I])
13732 ResultElements.push_back(Elt: Source.getVectorElt(I: SourceIdx++));
13733 else
13734 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13735 }
13736 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13737 }
13738 case X86::BI__builtin_ia32_vpconflictsi_128:
13739 case X86::BI__builtin_ia32_vpconflictsi_256:
13740 case X86::BI__builtin_ia32_vpconflictsi_512:
13741 case X86::BI__builtin_ia32_vpconflictdi_128:
13742 case X86::BI__builtin_ia32_vpconflictdi_256:
13743 case X86::BI__builtin_ia32_vpconflictdi_512: {
13744 APValue Source;
13745
13746 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
13747 return false;
13748
13749 unsigned SourceLen = Source.getVectorLength();
13750 SmallVector<APValue, 32> ResultElements;
13751 ResultElements.reserve(N: SourceLen);
13752
13753 const auto *VecT = E->getType()->castAs<VectorType>();
13754 bool DestUnsigned =
13755 VecT->getElementType()->isUnsignedIntegerOrEnumerationType();
13756
13757 for (unsigned I = 0; I != SourceLen; ++I) {
13758 const APValue &EltI = Source.getVectorElt(I);
13759
13760 APInt ConflictMask(EltI.getInt().getBitWidth(), 0);
13761 for (unsigned J = 0; J != I; ++J) {
13762 const APValue &EltJ = Source.getVectorElt(I: J);
13763 ConflictMask.setBitVal(BitPosition: J, BitValue: EltI.getInt() == EltJ.getInt());
13764 }
13765 ResultElements.push_back(Elt: APValue(APSInt(ConflictMask, DestUnsigned)));
13766 }
13767 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13768 }
13769 case X86::BI__builtin_ia32_blendpd:
13770 case X86::BI__builtin_ia32_blendpd256:
13771 case X86::BI__builtin_ia32_blendps:
13772 case X86::BI__builtin_ia32_blendps256:
13773 case X86::BI__builtin_ia32_pblendw128:
13774 case X86::BI__builtin_ia32_pblendw256:
13775 case X86::BI__builtin_ia32_pblendd128:
13776 case X86::BI__builtin_ia32_pblendd256: {
13777 APValue SourceF, SourceT, SourceC;
13778 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13779 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13780 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13781 return false;
13782
13783 const APInt &C = SourceC.getInt();
13784 unsigned SourceLen = SourceF.getVectorLength();
13785 SmallVector<APValue, 32> ResultElements;
13786 ResultElements.reserve(N: SourceLen);
13787 for (unsigned EltNum = 0; EltNum != SourceLen; ++EltNum) {
13788 const APValue &F = SourceF.getVectorElt(I: EltNum);
13789 const APValue &T = SourceT.getVectorElt(I: EltNum);
13790 ResultElements.push_back(Elt: C[EltNum % 8] ? T : F);
13791 }
13792
13793 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13794 }
13795
13796 case X86::BI__builtin_ia32_psignb128:
13797 case X86::BI__builtin_ia32_psignb256:
13798 case X86::BI__builtin_ia32_psignw128:
13799 case X86::BI__builtin_ia32_psignw256:
13800 case X86::BI__builtin_ia32_psignd128:
13801 case X86::BI__builtin_ia32_psignd256:
13802 return EvaluateBinOpExpr([](const APInt &AElem, const APInt &BElem) {
13803 if (BElem.isZero())
13804 return APInt::getZero(numBits: AElem.getBitWidth());
13805 if (BElem.isNegative())
13806 return -AElem;
13807 return AElem;
13808 });
13809
13810 case X86::BI__builtin_ia32_blendvpd:
13811 case X86::BI__builtin_ia32_blendvpd256:
13812 case X86::BI__builtin_ia32_blendvps:
13813 case X86::BI__builtin_ia32_blendvps256:
13814 case X86::BI__builtin_ia32_pblendvb128:
13815 case X86::BI__builtin_ia32_pblendvb256: {
13816 // SSE blendv by mask signbit: "Result = C[] < 0 ? T[] : F[]".
13817 APValue SourceF, SourceT, SourceC;
13818 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13819 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13820 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13821 return false;
13822
13823 unsigned SourceLen = SourceF.getVectorLength();
13824 SmallVector<APValue, 32> ResultElements;
13825 ResultElements.reserve(N: SourceLen);
13826
13827 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13828 const APValue &F = SourceF.getVectorElt(I: EltNum);
13829 const APValue &T = SourceT.getVectorElt(I: EltNum);
13830 const APValue &C = SourceC.getVectorElt(I: EltNum);
13831 APInt M = C.isInt() ? (APInt)C.getInt() : C.getFloat().bitcastToAPInt();
13832 ResultElements.push_back(Elt: M.isNegative() ? T : F);
13833 }
13834
13835 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13836 }
13837 case X86::BI__builtin_ia32_selectb_128:
13838 case X86::BI__builtin_ia32_selectb_256:
13839 case X86::BI__builtin_ia32_selectb_512:
13840 case X86::BI__builtin_ia32_selectw_128:
13841 case X86::BI__builtin_ia32_selectw_256:
13842 case X86::BI__builtin_ia32_selectw_512:
13843 case X86::BI__builtin_ia32_selectd_128:
13844 case X86::BI__builtin_ia32_selectd_256:
13845 case X86::BI__builtin_ia32_selectd_512:
13846 case X86::BI__builtin_ia32_selectq_128:
13847 case X86::BI__builtin_ia32_selectq_256:
13848 case X86::BI__builtin_ia32_selectq_512:
13849 case X86::BI__builtin_ia32_selectph_128:
13850 case X86::BI__builtin_ia32_selectph_256:
13851 case X86::BI__builtin_ia32_selectph_512:
13852 case X86::BI__builtin_ia32_selectpbf_128:
13853 case X86::BI__builtin_ia32_selectpbf_256:
13854 case X86::BI__builtin_ia32_selectpbf_512:
13855 case X86::BI__builtin_ia32_selectps_128:
13856 case X86::BI__builtin_ia32_selectps_256:
13857 case X86::BI__builtin_ia32_selectps_512:
13858 case X86::BI__builtin_ia32_selectpd_128:
13859 case X86::BI__builtin_ia32_selectpd_256:
13860 case X86::BI__builtin_ia32_selectpd_512: {
13861 // AVX512 predicated move: "Result = Mask[] ? LHS[] : RHS[]".
13862 APValue SourceMask, SourceLHS, SourceRHS;
13863 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceMask) ||
13864 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLHS) ||
13865 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceRHS))
13866 return false;
13867
13868 APSInt Mask = SourceMask.getInt();
13869 unsigned SourceLen = SourceLHS.getVectorLength();
13870 SmallVector<APValue, 4> ResultElements;
13871 ResultElements.reserve(N: SourceLen);
13872
13873 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13874 const APValue &LHS = SourceLHS.getVectorElt(I: EltNum);
13875 const APValue &RHS = SourceRHS.getVectorElt(I: EltNum);
13876 ResultElements.push_back(Elt: Mask[EltNum] ? LHS : RHS);
13877 }
13878
13879 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13880 }
13881
13882 case X86::BI__builtin_ia32_cvtsd2ss: {
13883 APValue VecA, VecB;
13884 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13885 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB))
13886 return false;
13887
13888 SmallVector<APValue, 4> Elements;
13889
13890 APValue ResultVal;
13891 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13892 Result&: ResultVal))
13893 return false;
13894
13895 Elements.push_back(Elt: ResultVal);
13896
13897 unsigned NumEltsA = VecA.getVectorLength();
13898 for (unsigned I = 1; I < NumEltsA; ++I) {
13899 Elements.push_back(Elt: VecA.getVectorElt(I));
13900 }
13901
13902 return Success(V: Elements, E);
13903 }
13904 case X86::BI__builtin_ia32_cvtsd2ss_round_mask: {
13905 APValue VecA, VecB, VecSrc, MaskValue;
13906
13907 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13908 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
13909 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
13910 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: MaskValue))
13911 return false;
13912
13913 unsigned Mask = MaskValue.getInt().getZExtValue();
13914 SmallVector<APValue, 4> Elements;
13915
13916 if (Mask & 1) {
13917 APValue ResultVal;
13918 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13919 Result&: ResultVal))
13920 return false;
13921 Elements.push_back(Elt: ResultVal);
13922 } else {
13923 Elements.push_back(Elt: VecSrc.getVectorElt(I: 0));
13924 }
13925
13926 unsigned NumEltsA = VecA.getVectorLength();
13927 for (unsigned I = 1; I < NumEltsA; ++I) {
13928 Elements.push_back(Elt: VecA.getVectorElt(I));
13929 }
13930
13931 return Success(V: Elements, E);
13932 }
13933 case X86::BI__builtin_ia32_cvtpd2ps:
13934 case X86::BI__builtin_ia32_cvtpd2ps256:
13935 case X86::BI__builtin_ia32_cvtpd2ps_mask:
13936 case X86::BI__builtin_ia32_cvtpd2ps512_mask: {
13937
13938 const auto BuiltinID = BuiltinOp;
13939 bool IsMasked = (BuiltinID == X86::BI__builtin_ia32_cvtpd2ps_mask ||
13940 BuiltinID == X86::BI__builtin_ia32_cvtpd2ps512_mask);
13941
13942 APValue InputValue;
13943 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: InputValue))
13944 return false;
13945
13946 APValue MergeValue;
13947 unsigned Mask = 0xFFFFFFFF;
13948 bool NeedsMerge = false;
13949 if (IsMasked) {
13950 APValue MaskValue;
13951 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MaskValue))
13952 return false;
13953 Mask = MaskValue.getInt().getZExtValue();
13954 auto NumEltsResult = E->getType()->getAs<VectorType>()->getNumElements();
13955 for (unsigned I = 0; I < NumEltsResult; ++I) {
13956 if (!((Mask >> I) & 1)) {
13957 NeedsMerge = true;
13958 break;
13959 }
13960 }
13961 if (NeedsMerge) {
13962 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: MergeValue))
13963 return false;
13964 }
13965 }
13966
13967 unsigned NumEltsResult =
13968 E->getType()->getAs<VectorType>()->getNumElements();
13969 unsigned NumEltsInput = InputValue.getVectorLength();
13970 SmallVector<APValue, 8> Elements;
13971 for (unsigned I = 0; I < NumEltsResult; ++I) {
13972 if (IsMasked && !((Mask >> I) & 1)) {
13973 if (!NeedsMerge) {
13974 return false;
13975 }
13976 Elements.push_back(Elt: MergeValue.getVectorElt(I));
13977 continue;
13978 }
13979
13980 if (I >= NumEltsInput) {
13981 Elements.push_back(Elt: APValue(APFloat::getZero(Sem: APFloat::IEEEsingle())));
13982 continue;
13983 }
13984
13985 APValue ResultVal;
13986 if (!ConvertDoubleToFloatStrict(
13987 Info, E, OrigVal: InputValue.getVectorElt(I).getFloat(), Result&: ResultVal))
13988 return false;
13989
13990 Elements.push_back(Elt: ResultVal);
13991 }
13992 return Success(V: Elements, E);
13993 }
13994
13995 case X86::BI__builtin_ia32_shufps:
13996 case X86::BI__builtin_ia32_shufps256:
13997 case X86::BI__builtin_ia32_shufps512: {
13998 APValue R;
13999 if (!evalShuffleGeneric(
14000 Info, Call: E, Out&: R,
14001 GetSourceIndex: [](unsigned DstIdx,
14002 unsigned ShuffleMask) -> std::pair<unsigned, int> {
14003 constexpr unsigned LaneBits = 128u;
14004 unsigned NumElemPerLane = LaneBits / 32;
14005 unsigned NumSelectableElems = NumElemPerLane / 2;
14006 unsigned BitsPerElem = 2;
14007 unsigned IndexMask = (1u << BitsPerElem) - 1;
14008 unsigned MaskBits = 8;
14009 unsigned Lane = DstIdx / NumElemPerLane;
14010 unsigned ElemInLane = DstIdx % NumElemPerLane;
14011 unsigned LaneOffset = Lane * NumElemPerLane;
14012 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
14013 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
14014 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
14015 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
14016 }))
14017 return false;
14018 return Success(V: R, E);
14019 }
14020 case X86::BI__builtin_ia32_shufpd:
14021 case X86::BI__builtin_ia32_shufpd256:
14022 case X86::BI__builtin_ia32_shufpd512: {
14023 APValue R;
14024 if (!evalShuffleGeneric(
14025 Info, Call: E, Out&: R,
14026 GetSourceIndex: [](unsigned DstIdx,
14027 unsigned ShuffleMask) -> std::pair<unsigned, int> {
14028 constexpr unsigned LaneBits = 128u;
14029 unsigned NumElemPerLane = LaneBits / 64;
14030 unsigned NumSelectableElems = NumElemPerLane / 2;
14031 unsigned BitsPerElem = 1;
14032 unsigned IndexMask = (1u << BitsPerElem) - 1;
14033 unsigned MaskBits = 8;
14034 unsigned Lane = DstIdx / NumElemPerLane;
14035 unsigned ElemInLane = DstIdx % NumElemPerLane;
14036 unsigned LaneOffset = Lane * NumElemPerLane;
14037 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
14038 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
14039 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
14040 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
14041 }))
14042 return false;
14043 return Success(V: R, E);
14044 }
14045 case X86::BI__builtin_ia32_insertps128: {
14046 APValue R;
14047 if (!evalShuffleGeneric(
14048 Info, Call: E, Out&: R,
14049 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14050 // Bits [3:0]: zero mask - if bit is set, zero this element
14051 if ((Mask & (1 << DstIdx)) != 0) {
14052 return {0, -1};
14053 }
14054 // Bits [7:6]: select element from source vector Y (0-3)
14055 // Bits [5:4]: select destination position (0-3)
14056 unsigned SrcElem = (Mask >> 6) & 0x3;
14057 unsigned DstElem = (Mask >> 4) & 0x3;
14058 if (DstIdx == DstElem) {
14059 // Insert element from source vector (B) at this position
14060 return {1, static_cast<int>(SrcElem)};
14061 } else {
14062 // Copy from destination vector (A)
14063 return {0, static_cast<int>(DstIdx)};
14064 }
14065 }))
14066 return false;
14067 return Success(V: R, E);
14068 }
14069 case X86::BI__builtin_ia32_pshufb128:
14070 case X86::BI__builtin_ia32_pshufb256:
14071 case X86::BI__builtin_ia32_pshufb512: {
14072 APValue R;
14073 if (!evalShuffleGeneric(
14074 Info, Call: E, Out&: R,
14075 GetSourceIndex: [](unsigned DstIdx,
14076 unsigned ShuffleMask) -> std::pair<unsigned, int> {
14077 uint8_t Ctlb = static_cast<uint8_t>(ShuffleMask);
14078 if (Ctlb & 0x80)
14079 return std::make_pair(x: 0, y: -1);
14080
14081 unsigned LaneBase = (DstIdx / 16) * 16;
14082 unsigned SrcOffset = Ctlb & 0x0F;
14083 unsigned SrcIdx = LaneBase + SrcOffset;
14084 return std::make_pair(x: 0, y: static_cast<int>(SrcIdx));
14085 }))
14086 return false;
14087 return Success(V: R, E);
14088 }
14089
14090 case X86::BI__builtin_ia32_pshuflw:
14091 case X86::BI__builtin_ia32_pshuflw256:
14092 case X86::BI__builtin_ia32_pshuflw512: {
14093 APValue R;
14094 if (!evalShuffleGeneric(
14095 Info, Call: E, Out&: R,
14096 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14097 constexpr unsigned LaneBits = 128u;
14098 constexpr unsigned ElemBits = 16u;
14099 constexpr unsigned LaneElts = LaneBits / ElemBits;
14100 constexpr unsigned HalfSize = 4;
14101 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
14102 unsigned LaneIdx = DstIdx % LaneElts;
14103 if (LaneIdx < HalfSize) {
14104 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
14105 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
14106 }
14107 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
14108 }))
14109 return false;
14110 return Success(V: R, E);
14111 }
14112
14113 case X86::BI__builtin_ia32_pshufhw:
14114 case X86::BI__builtin_ia32_pshufhw256:
14115 case X86::BI__builtin_ia32_pshufhw512: {
14116 APValue R;
14117 if (!evalShuffleGeneric(
14118 Info, Call: E, Out&: R,
14119 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14120 constexpr unsigned LaneBits = 128u;
14121 constexpr unsigned ElemBits = 16u;
14122 constexpr unsigned LaneElts = LaneBits / ElemBits;
14123 constexpr unsigned HalfSize = 4;
14124 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
14125 unsigned LaneIdx = DstIdx % LaneElts;
14126 if (LaneIdx >= HalfSize) {
14127 unsigned Rel = LaneIdx - HalfSize;
14128 unsigned Sel = (Mask >> (2 * Rel)) & 0x3;
14129 return std::make_pair(
14130 x: 0, y: static_cast<int>(LaneBase + HalfSize + Sel));
14131 }
14132 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
14133 }))
14134 return false;
14135 return Success(V: R, E);
14136 }
14137
14138 case X86::BI__builtin_ia32_pshufd:
14139 case X86::BI__builtin_ia32_pshufd256:
14140 case X86::BI__builtin_ia32_pshufd512:
14141 case X86::BI__builtin_ia32_vpermilps:
14142 case X86::BI__builtin_ia32_vpermilps256:
14143 case X86::BI__builtin_ia32_vpermilps512: {
14144 APValue R;
14145 if (!evalShuffleGeneric(
14146 Info, Call: E, Out&: R,
14147 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14148 constexpr unsigned LaneBits = 128u;
14149 constexpr unsigned ElemBits = 32u;
14150 constexpr unsigned LaneElts = LaneBits / ElemBits;
14151 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
14152 unsigned LaneIdx = DstIdx % LaneElts;
14153 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
14154 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
14155 }))
14156 return false;
14157 return Success(V: R, E);
14158 }
14159
14160 case X86::BI__builtin_ia32_vpermilvarpd:
14161 case X86::BI__builtin_ia32_vpermilvarpd256:
14162 case X86::BI__builtin_ia32_vpermilvarpd512: {
14163 APValue R;
14164 if (!evalShuffleGeneric(
14165 Info, Call: E, Out&: R,
14166 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14167 unsigned NumElemPerLane = 2;
14168 unsigned Lane = DstIdx / NumElemPerLane;
14169 unsigned Offset = Mask & 0b10 ? 1 : 0;
14170 return std::make_pair(
14171 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
14172 }))
14173 return false;
14174 return Success(V: R, E);
14175 }
14176
14177 case X86::BI__builtin_ia32_vpermilpd:
14178 case X86::BI__builtin_ia32_vpermilpd256:
14179 case X86::BI__builtin_ia32_vpermilpd512: {
14180 APValue R;
14181 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
14182 unsigned NumElemPerLane = 2;
14183 unsigned BitsPerElem = 1;
14184 unsigned MaskBits = 8;
14185 unsigned IndexMask = 0x1;
14186 unsigned Lane = DstIdx / NumElemPerLane;
14187 unsigned LaneOffset = Lane * NumElemPerLane;
14188 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
14189 unsigned Index = (Control >> BitIndex) & IndexMask;
14190 return std::make_pair(x: 0, y: static_cast<int>(LaneOffset + Index));
14191 }))
14192 return false;
14193 return Success(V: R, E);
14194 }
14195
14196 case X86::BI__builtin_ia32_permdf256:
14197 case X86::BI__builtin_ia32_permdi256: {
14198 APValue R;
14199 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
14200 // permute4x64 operates on 4 64-bit elements
14201 // For element i (0-3), extract bits [2*i+1:2*i] from Control
14202 unsigned Index = (Control >> (2 * DstIdx)) & 0x3;
14203 return std::make_pair(x: 0, y: static_cast<int>(Index));
14204 }))
14205 return false;
14206 return Success(V: R, E);
14207 }
14208
14209 case X86::BI__builtin_ia32_vpermilvarps:
14210 case X86::BI__builtin_ia32_vpermilvarps256:
14211 case X86::BI__builtin_ia32_vpermilvarps512: {
14212 APValue R;
14213 if (!evalShuffleGeneric(
14214 Info, Call: E, Out&: R,
14215 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14216 unsigned NumElemPerLane = 4;
14217 unsigned Lane = DstIdx / NumElemPerLane;
14218 unsigned Offset = Mask & 0b11;
14219 return std::make_pair(
14220 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
14221 }))
14222 return false;
14223 return Success(V: R, E);
14224 }
14225
14226 case X86::BI__builtin_ia32_vpmultishiftqb128:
14227 case X86::BI__builtin_ia32_vpmultishiftqb256:
14228 case X86::BI__builtin_ia32_vpmultishiftqb512: {
14229 assert(E->getNumArgs() == 2);
14230
14231 APValue A, B;
14232 if (!Evaluate(Result&: A, Info, E: E->getArg(Arg: 0)) || !Evaluate(Result&: B, Info, E: E->getArg(Arg: 1)))
14233 return false;
14234
14235 assert(A.getVectorLength() == B.getVectorLength());
14236 unsigned NumBytesInQWord = 8;
14237 unsigned NumBitsInByte = 8;
14238 unsigned NumBytes = A.getVectorLength();
14239 unsigned NumQWords = NumBytes / NumBytesInQWord;
14240 SmallVector<APValue, 64> Result;
14241 Result.reserve(N: NumBytes);
14242
14243 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
14244 APInt BQWord(64, 0);
14245 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14246 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
14247 uint64_t Byte = B.getVectorElt(I: Idx).getInt().getZExtValue();
14248 BQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
14249 }
14250
14251 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14252 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
14253 uint64_t Ctrl = A.getVectorElt(I: Idx).getInt().getZExtValue() & 0x3F;
14254
14255 APInt Byte(8, 0);
14256 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
14257 Byte.setBitVal(BitPosition: BitIdx, BitValue: BQWord[(Ctrl + BitIdx) & 0x3F]);
14258 }
14259 Result.push_back(Elt: APValue(APSInt(Byte, /*isUnsigned*/ true)));
14260 }
14261 }
14262 return Success(V: APValue(Result.data(), Result.size()), E);
14263 }
14264
14265 case X86::BI__builtin_ia32_phminposuw128: {
14266 APValue Source;
14267 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
14268 return false;
14269 unsigned SourceLen = Source.getVectorLength();
14270 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14271 QualType ElemQT = VT->getElementType();
14272 unsigned ElemBitWidth = Info.Ctx.getTypeSize(T: ElemQT);
14273
14274 APInt MinIndex(ElemBitWidth, 0);
14275 APInt MinVal = Source.getVectorElt(I: 0).getInt();
14276 for (unsigned I = 1; I != SourceLen; ++I) {
14277 APInt Val = Source.getVectorElt(I).getInt();
14278 if (MinVal.ugt(RHS: Val)) {
14279 MinVal = Val;
14280 MinIndex = I;
14281 }
14282 }
14283
14284 bool ResultUnsigned = E->getCallReturnType(Ctx: Info.Ctx)
14285 ->castAs<VectorType>()
14286 ->getElementType()
14287 ->isUnsignedIntegerOrEnumerationType();
14288
14289 SmallVector<APValue, 8> Result;
14290 Result.reserve(N: SourceLen);
14291 Result.emplace_back(Args: APSInt(MinVal, ResultUnsigned));
14292 Result.emplace_back(Args: APSInt(MinIndex, ResultUnsigned));
14293 for (unsigned I = 0; I != SourceLen - 2; ++I) {
14294 Result.emplace_back(Args: APSInt(APInt(ElemBitWidth, 0), ResultUnsigned));
14295 }
14296 return Success(V: APValue(Result.data(), Result.size()), E);
14297 }
14298
14299 case X86::BI__builtin_ia32_psraq128:
14300 case X86::BI__builtin_ia32_psraq256:
14301 case X86::BI__builtin_ia32_psraq512:
14302 case X86::BI__builtin_ia32_psrad128:
14303 case X86::BI__builtin_ia32_psrad256:
14304 case X86::BI__builtin_ia32_psrad512:
14305 case X86::BI__builtin_ia32_psraw128:
14306 case X86::BI__builtin_ia32_psraw256:
14307 case X86::BI__builtin_ia32_psraw512: {
14308 APValue R;
14309 if (!evalShiftWithCount(
14310 Info, Call: E, Out&: R,
14311 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.ashr(ShiftAmt: Count); },
14312 OverflowOp: [](const APInt &Elt, unsigned Width) {
14313 return Elt.ashr(ShiftAmt: Width - 1);
14314 }))
14315 return false;
14316 return Success(V: R, E);
14317 }
14318
14319 case X86::BI__builtin_ia32_psllq128:
14320 case X86::BI__builtin_ia32_psllq256:
14321 case X86::BI__builtin_ia32_psllq512:
14322 case X86::BI__builtin_ia32_pslld128:
14323 case X86::BI__builtin_ia32_pslld256:
14324 case X86::BI__builtin_ia32_pslld512:
14325 case X86::BI__builtin_ia32_psllw128:
14326 case X86::BI__builtin_ia32_psllw256:
14327 case X86::BI__builtin_ia32_psllw512: {
14328 APValue R;
14329 if (!evalShiftWithCount(
14330 Info, Call: E, Out&: R,
14331 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.shl(shiftAmt: Count); },
14332 OverflowOp: [](const APInt &Elt, unsigned Width) {
14333 return APInt::getZero(numBits: Width);
14334 }))
14335 return false;
14336 return Success(V: R, E);
14337 }
14338
14339 case X86::BI__builtin_ia32_psrlq128:
14340 case X86::BI__builtin_ia32_psrlq256:
14341 case X86::BI__builtin_ia32_psrlq512:
14342 case X86::BI__builtin_ia32_psrld128:
14343 case X86::BI__builtin_ia32_psrld256:
14344 case X86::BI__builtin_ia32_psrld512:
14345 case X86::BI__builtin_ia32_psrlw128:
14346 case X86::BI__builtin_ia32_psrlw256:
14347 case X86::BI__builtin_ia32_psrlw512: {
14348 APValue R;
14349 if (!evalShiftWithCount(
14350 Info, Call: E, Out&: R,
14351 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.lshr(shiftAmt: Count); },
14352 OverflowOp: [](const APInt &Elt, unsigned Width) {
14353 return APInt::getZero(numBits: Width);
14354 }))
14355 return false;
14356 return Success(V: R, E);
14357 }
14358
14359 case X86::BI__builtin_ia32_pternlogd128_mask:
14360 case X86::BI__builtin_ia32_pternlogd256_mask:
14361 case X86::BI__builtin_ia32_pternlogd512_mask:
14362 case X86::BI__builtin_ia32_pternlogq128_mask:
14363 case X86::BI__builtin_ia32_pternlogq256_mask:
14364 case X86::BI__builtin_ia32_pternlogq512_mask: {
14365 APValue AValue, BValue, CValue, ImmValue, UValue;
14366 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
14367 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
14368 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
14369 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
14370 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
14371 return false;
14372
14373 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14374 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14375 APInt Imm = ImmValue.getInt();
14376 APInt U = UValue.getInt();
14377 unsigned ResultLen = AValue.getVectorLength();
14378 SmallVector<APValue, 16> ResultElements;
14379 ResultElements.reserve(N: ResultLen);
14380
14381 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
14382 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
14383 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
14384 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
14385
14386 if (U[EltNum]) {
14387 unsigned BitWidth = ALane.getBitWidth();
14388 APInt ResLane(BitWidth, 0);
14389
14390 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
14391 unsigned ABit = ALane[Bit];
14392 unsigned BBit = BLane[Bit];
14393 unsigned CBit = CLane[Bit];
14394
14395 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
14396 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
14397 }
14398 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
14399 } else {
14400 ResultElements.push_back(Elt: APValue(APSInt(ALane, DestUnsigned)));
14401 }
14402 }
14403 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14404 }
14405 case X86::BI__builtin_ia32_pternlogd128_maskz:
14406 case X86::BI__builtin_ia32_pternlogd256_maskz:
14407 case X86::BI__builtin_ia32_pternlogd512_maskz:
14408 case X86::BI__builtin_ia32_pternlogq128_maskz:
14409 case X86::BI__builtin_ia32_pternlogq256_maskz:
14410 case X86::BI__builtin_ia32_pternlogq512_maskz: {
14411 APValue AValue, BValue, CValue, ImmValue, UValue;
14412 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
14413 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
14414 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
14415 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
14416 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
14417 return false;
14418
14419 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14420 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14421 APInt Imm = ImmValue.getInt();
14422 APInt U = UValue.getInt();
14423 unsigned ResultLen = AValue.getVectorLength();
14424 SmallVector<APValue, 16> ResultElements;
14425 ResultElements.reserve(N: ResultLen);
14426
14427 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
14428 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
14429 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
14430 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
14431
14432 unsigned BitWidth = ALane.getBitWidth();
14433 APInt ResLane(BitWidth, 0);
14434
14435 if (U[EltNum]) {
14436 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
14437 unsigned ABit = ALane[Bit];
14438 unsigned BBit = BLane[Bit];
14439 unsigned CBit = CLane[Bit];
14440
14441 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
14442 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
14443 }
14444 }
14445 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
14446 }
14447 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14448 }
14449
14450 case Builtin::BI__builtin_elementwise_clzg:
14451 case Builtin::BI__builtin_elementwise_ctzg: {
14452 APValue SourceLHS;
14453 std::optional<APValue> Fallback;
14454 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS))
14455 return false;
14456 if (E->getNumArgs() > 1) {
14457 APValue FallbackTmp;
14458 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: FallbackTmp))
14459 return false;
14460 Fallback = FallbackTmp;
14461 }
14462
14463 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14464 unsigned SourceLen = SourceLHS.getVectorLength();
14465 SmallVector<APValue, 4> ResultElements;
14466 ResultElements.reserve(N: SourceLen);
14467
14468 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14469 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
14470 if (!LHS) {
14471 // Without a fallback, a zero element is undefined
14472 if (!Fallback) {
14473 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
14474 << /*IsTrailing=*/(BuiltinOp ==
14475 Builtin::BI__builtin_elementwise_ctzg);
14476 return false;
14477 }
14478 ResultElements.push_back(Elt: Fallback->getVectorElt(I: EltNum));
14479 continue;
14480 }
14481 switch (BuiltinOp) {
14482 case Builtin::BI__builtin_elementwise_clzg:
14483 ResultElements.push_back(Elt: APValue(
14484 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countl_zero()),
14485 DestEltTy->isUnsignedIntegerOrEnumerationType())));
14486 break;
14487 case Builtin::BI__builtin_elementwise_ctzg:
14488 ResultElements.push_back(Elt: APValue(
14489 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countr_zero()),
14490 DestEltTy->isUnsignedIntegerOrEnumerationType())));
14491 break;
14492 }
14493 }
14494
14495 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14496 }
14497
14498 case Builtin::BI__builtin_elementwise_fma: {
14499 APValue SourceX, SourceY, SourceZ;
14500 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceX) ||
14501 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceY) ||
14502 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceZ))
14503 return false;
14504
14505 unsigned SourceLen = SourceX.getVectorLength();
14506 SmallVector<APValue> ResultElements;
14507 ResultElements.reserve(N: SourceLen);
14508 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14509 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14510 const APFloat &X = SourceX.getVectorElt(I: EltNum).getFloat();
14511 const APFloat &Y = SourceY.getVectorElt(I: EltNum).getFloat();
14512 const APFloat &Z = SourceZ.getVectorElt(I: EltNum).getFloat();
14513 APFloat Result(X);
14514 (void)Result.fusedMultiplyAdd(Multiplicand: Y, Addend: Z, RM);
14515 ResultElements.push_back(Elt: APValue(Result));
14516 }
14517 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14518 }
14519
14520 case clang::X86::BI__builtin_ia32_phaddw128:
14521 case clang::X86::BI__builtin_ia32_phaddw256:
14522 case clang::X86::BI__builtin_ia32_phaddd128:
14523 case clang::X86::BI__builtin_ia32_phaddd256:
14524 case clang::X86::BI__builtin_ia32_phaddsw128:
14525 case clang::X86::BI__builtin_ia32_phaddsw256:
14526
14527 case clang::X86::BI__builtin_ia32_phsubw128:
14528 case clang::X86::BI__builtin_ia32_phsubw256:
14529 case clang::X86::BI__builtin_ia32_phsubd128:
14530 case clang::X86::BI__builtin_ia32_phsubd256:
14531 case clang::X86::BI__builtin_ia32_phsubsw128:
14532 case clang::X86::BI__builtin_ia32_phsubsw256: {
14533 APValue SourceLHS, SourceRHS;
14534 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14535 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14536 return false;
14537 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14538 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14539
14540 unsigned NumElts = SourceLHS.getVectorLength();
14541 unsigned EltBits = Info.Ctx.getIntWidth(T: DestEltTy);
14542 unsigned EltsPerLane = 128 / EltBits;
14543 SmallVector<APValue, 4> ResultElements;
14544 ResultElements.reserve(N: NumElts);
14545
14546 for (unsigned LaneStart = 0; LaneStart != NumElts;
14547 LaneStart += EltsPerLane) {
14548 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14549 APSInt LHSA = SourceLHS.getVectorElt(I: LaneStart + I).getInt();
14550 APSInt LHSB = SourceLHS.getVectorElt(I: LaneStart + I + 1).getInt();
14551 switch (BuiltinOp) {
14552 case clang::X86::BI__builtin_ia32_phaddw128:
14553 case clang::X86::BI__builtin_ia32_phaddw256:
14554 case clang::X86::BI__builtin_ia32_phaddd128:
14555 case clang::X86::BI__builtin_ia32_phaddd256: {
14556 APSInt Res(LHSA + LHSB, DestUnsigned);
14557 ResultElements.push_back(Elt: APValue(Res));
14558 break;
14559 }
14560 case clang::X86::BI__builtin_ia32_phaddsw128:
14561 case clang::X86::BI__builtin_ia32_phaddsw256: {
14562 APSInt Res(LHSA.sadd_sat(RHS: LHSB));
14563 ResultElements.push_back(Elt: APValue(Res));
14564 break;
14565 }
14566 case clang::X86::BI__builtin_ia32_phsubw128:
14567 case clang::X86::BI__builtin_ia32_phsubw256:
14568 case clang::X86::BI__builtin_ia32_phsubd128:
14569 case clang::X86::BI__builtin_ia32_phsubd256: {
14570 APSInt Res(LHSA - LHSB, DestUnsigned);
14571 ResultElements.push_back(Elt: APValue(Res));
14572 break;
14573 }
14574 case clang::X86::BI__builtin_ia32_phsubsw128:
14575 case clang::X86::BI__builtin_ia32_phsubsw256: {
14576 APSInt Res(LHSA.ssub_sat(RHS: LHSB));
14577 ResultElements.push_back(Elt: APValue(Res));
14578 break;
14579 }
14580 }
14581 }
14582 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14583 APSInt RHSA = SourceRHS.getVectorElt(I: LaneStart + I).getInt();
14584 APSInt RHSB = SourceRHS.getVectorElt(I: LaneStart + I + 1).getInt();
14585 switch (BuiltinOp) {
14586 case clang::X86::BI__builtin_ia32_phaddw128:
14587 case clang::X86::BI__builtin_ia32_phaddw256:
14588 case clang::X86::BI__builtin_ia32_phaddd128:
14589 case clang::X86::BI__builtin_ia32_phaddd256: {
14590 APSInt Res(RHSA + RHSB, DestUnsigned);
14591 ResultElements.push_back(Elt: APValue(Res));
14592 break;
14593 }
14594 case clang::X86::BI__builtin_ia32_phaddsw128:
14595 case clang::X86::BI__builtin_ia32_phaddsw256: {
14596 APSInt Res(RHSA.sadd_sat(RHS: RHSB));
14597 ResultElements.push_back(Elt: APValue(Res));
14598 break;
14599 }
14600 case clang::X86::BI__builtin_ia32_phsubw128:
14601 case clang::X86::BI__builtin_ia32_phsubw256:
14602 case clang::X86::BI__builtin_ia32_phsubd128:
14603 case clang::X86::BI__builtin_ia32_phsubd256: {
14604 APSInt Res(RHSA - RHSB, DestUnsigned);
14605 ResultElements.push_back(Elt: APValue(Res));
14606 break;
14607 }
14608 case clang::X86::BI__builtin_ia32_phsubsw128:
14609 case clang::X86::BI__builtin_ia32_phsubsw256: {
14610 APSInt Res(RHSA.ssub_sat(RHS: RHSB));
14611 ResultElements.push_back(Elt: APValue(Res));
14612 break;
14613 }
14614 }
14615 }
14616 }
14617 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14618 }
14619 case clang::X86::BI__builtin_ia32_haddpd:
14620 case clang::X86::BI__builtin_ia32_haddps:
14621 case clang::X86::BI__builtin_ia32_haddps256:
14622 case clang::X86::BI__builtin_ia32_haddpd256:
14623 case clang::X86::BI__builtin_ia32_hsubpd:
14624 case clang::X86::BI__builtin_ia32_hsubps:
14625 case clang::X86::BI__builtin_ia32_hsubps256:
14626 case clang::X86::BI__builtin_ia32_hsubpd256: {
14627 APValue SourceLHS, SourceRHS;
14628 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14629 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14630 return false;
14631 unsigned NumElts = SourceLHS.getVectorLength();
14632 SmallVector<APValue, 4> ResultElements;
14633 ResultElements.reserve(N: NumElts);
14634 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14635 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14636 unsigned EltBits = Info.Ctx.getTypeSize(T: DestEltTy);
14637 unsigned NumLanes = NumElts * EltBits / 128;
14638 unsigned NumElemsPerLane = NumElts / NumLanes;
14639 unsigned HalfElemsPerLane = NumElemsPerLane / 2;
14640
14641 for (unsigned L = 0; L != NumElts; L += NumElemsPerLane) {
14642 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14643 APFloat LHSA = SourceLHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14644 APFloat LHSB = SourceLHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14645 switch (BuiltinOp) {
14646 case clang::X86::BI__builtin_ia32_haddpd:
14647 case clang::X86::BI__builtin_ia32_haddps:
14648 case clang::X86::BI__builtin_ia32_haddps256:
14649 case clang::X86::BI__builtin_ia32_haddpd256:
14650 LHSA.add(RHS: LHSB, RM);
14651 break;
14652 case clang::X86::BI__builtin_ia32_hsubpd:
14653 case clang::X86::BI__builtin_ia32_hsubps:
14654 case clang::X86::BI__builtin_ia32_hsubps256:
14655 case clang::X86::BI__builtin_ia32_hsubpd256:
14656 LHSA.subtract(RHS: LHSB, RM);
14657 break;
14658 }
14659 ResultElements.push_back(Elt: APValue(LHSA));
14660 }
14661 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14662 APFloat RHSA = SourceRHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14663 APFloat RHSB = SourceRHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14664 switch (BuiltinOp) {
14665 case clang::X86::BI__builtin_ia32_haddpd:
14666 case clang::X86::BI__builtin_ia32_haddps:
14667 case clang::X86::BI__builtin_ia32_haddps256:
14668 case clang::X86::BI__builtin_ia32_haddpd256:
14669 RHSA.add(RHS: RHSB, RM);
14670 break;
14671 case clang::X86::BI__builtin_ia32_hsubpd:
14672 case clang::X86::BI__builtin_ia32_hsubps:
14673 case clang::X86::BI__builtin_ia32_hsubps256:
14674 case clang::X86::BI__builtin_ia32_hsubpd256:
14675 RHSA.subtract(RHS: RHSB, RM);
14676 break;
14677 }
14678 ResultElements.push_back(Elt: APValue(RHSA));
14679 }
14680 }
14681 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14682 }
14683 case clang::X86::BI__builtin_ia32_addsubpd:
14684 case clang::X86::BI__builtin_ia32_addsubps:
14685 case clang::X86::BI__builtin_ia32_addsubpd256:
14686 case clang::X86::BI__builtin_ia32_addsubps256: {
14687 // Addsub: alternates between subtraction and addition
14688 // Result[i] = (i % 2 == 0) ? (a[i] - b[i]) : (a[i] + b[i])
14689 APValue SourceLHS, SourceRHS;
14690 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14691 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14692 return false;
14693 unsigned NumElems = SourceLHS.getVectorLength();
14694 SmallVector<APValue, 8> ResultElements;
14695 ResultElements.reserve(N: NumElems);
14696 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14697
14698 for (unsigned I = 0; I != NumElems; ++I) {
14699 APFloat LHS = SourceLHS.getVectorElt(I).getFloat();
14700 APFloat RHS = SourceRHS.getVectorElt(I).getFloat();
14701 if (I % 2 == 0) {
14702 // Even indices: subtract
14703 LHS.subtract(RHS, RM);
14704 } else {
14705 // Odd indices: add
14706 LHS.add(RHS, RM);
14707 }
14708 ResultElements.push_back(Elt: APValue(LHS));
14709 }
14710 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14711 }
14712 case clang::X86::BI__builtin_ia32_pclmulqdq128:
14713 case clang::X86::BI__builtin_ia32_pclmulqdq256:
14714 case clang::X86::BI__builtin_ia32_pclmulqdq512: {
14715 // PCLMULQDQ: carry-less multiplication of selected 64-bit halves
14716 // imm8 bit 0: selects lower (0) or upper (1) 64 bits of first operand
14717 // imm8 bit 4: selects lower (0) or upper (1) 64 bits of second operand
14718 APValue SourceLHS, SourceRHS;
14719 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14720 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14721 return false;
14722
14723 APSInt Imm8;
14724 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm8, Info))
14725 return false;
14726
14727 // Extract bits 0 and 4 from imm8
14728 bool SelectUpperA = (Imm8 & 0x01) != 0;
14729 bool SelectUpperB = (Imm8 & 0x10) != 0;
14730
14731 unsigned NumElems = SourceLHS.getVectorLength();
14732 SmallVector<APValue, 8> ResultElements;
14733 ResultElements.reserve(N: NumElems);
14734 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14735 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14736
14737 // Process each 128-bit lane
14738 for (unsigned Lane = 0; Lane < NumElems; Lane += 2) {
14739 // Get the two 64-bit halves of the first operand
14740 APSInt A0 = SourceLHS.getVectorElt(I: Lane + 0).getInt();
14741 APSInt A1 = SourceLHS.getVectorElt(I: Lane + 1).getInt();
14742 // Get the two 64-bit halves of the second operand
14743 APSInt B0 = SourceRHS.getVectorElt(I: Lane + 0).getInt();
14744 APSInt B1 = SourceRHS.getVectorElt(I: Lane + 1).getInt();
14745
14746 // Select the appropriate 64-bit values based on imm8
14747 APInt A = SelectUpperA ? A1 : A0;
14748 APInt B = SelectUpperB ? B1 : B0;
14749
14750 // Extend both operands to 128 bits for carry-less multiplication
14751 APInt A128 = A.zext(width: 128);
14752 APInt B128 = B.zext(width: 128);
14753
14754 // Use APIntOps::clmul for carry-less multiplication
14755 APInt Result = llvm::APIntOps::clmul(LHS: A128, RHS: B128);
14756
14757 // Split the 128-bit result into two 64-bit halves
14758 APSInt ResultLow(Result.extractBits(numBits: 64, bitPosition: 0), DestUnsigned);
14759 APSInt ResultHigh(Result.extractBits(numBits: 64, bitPosition: 64), DestUnsigned);
14760
14761 ResultElements.push_back(Elt: APValue(ResultLow));
14762 ResultElements.push_back(Elt: APValue(ResultHigh));
14763 }
14764
14765 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14766 }
14767 case Builtin::BI__builtin_elementwise_clmul:
14768 return EvaluateBinOpExpr(llvm::APIntOps::clmul);
14769 case Builtin::BI__builtin_elementwise_pext:
14770 return EvaluateBinOpExpr(llvm::APIntOps::pext);
14771 case Builtin::BI__builtin_elementwise_pdep:
14772 return EvaluateBinOpExpr(llvm::APIntOps::pdep);
14773 case Builtin::BI__builtin_elementwise_fshl:
14774 case Builtin::BI__builtin_elementwise_fshr: {
14775 APValue SourceHi, SourceLo, SourceShift;
14776 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
14777 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
14778 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceShift))
14779 return false;
14780
14781 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14782 if (!DestEltTy->isIntegerType())
14783 return false;
14784
14785 unsigned SourceLen = SourceHi.getVectorLength();
14786 SmallVector<APValue> ResultElements;
14787 ResultElements.reserve(N: SourceLen);
14788 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14789 const APSInt &Hi = SourceHi.getVectorElt(I: EltNum).getInt();
14790 const APSInt &Lo = SourceLo.getVectorElt(I: EltNum).getInt();
14791 const APSInt &Shift = SourceShift.getVectorElt(I: EltNum).getInt();
14792 switch (BuiltinOp) {
14793 case Builtin::BI__builtin_elementwise_fshl:
14794 ResultElements.push_back(Elt: APValue(
14795 APSInt(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned())));
14796 break;
14797 case Builtin::BI__builtin_elementwise_fshr:
14798 ResultElements.push_back(Elt: APValue(
14799 APSInt(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned())));
14800 break;
14801 }
14802 }
14803
14804 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14805 }
14806
14807 case X86::BI__builtin_ia32_shuf_f32x4_256:
14808 case X86::BI__builtin_ia32_shuf_i32x4_256:
14809 case X86::BI__builtin_ia32_shuf_f64x2_256:
14810 case X86::BI__builtin_ia32_shuf_i64x2_256:
14811 case X86::BI__builtin_ia32_shuf_f32x4:
14812 case X86::BI__builtin_ia32_shuf_i32x4:
14813 case X86::BI__builtin_ia32_shuf_f64x2:
14814 case X86::BI__builtin_ia32_shuf_i64x2: {
14815 APValue SourceA, SourceB;
14816 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
14817 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB))
14818 return false;
14819
14820 APSInt Imm;
14821 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14822 return false;
14823
14824 // Destination and sources A, B all have the same type.
14825 unsigned NumElems = SourceA.getVectorLength();
14826 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14827 QualType ElemQT = VT->getElementType();
14828 unsigned ElemBits = Info.Ctx.getTypeSize(T: ElemQT);
14829 unsigned LaneBits = 128u;
14830 unsigned NumLanes = (NumElems * ElemBits) / LaneBits;
14831 unsigned NumElemsPerLane = LaneBits / ElemBits;
14832
14833 unsigned DstLen = SourceA.getVectorLength();
14834 SmallVector<APValue, 16> ResultElements;
14835 ResultElements.reserve(N: DstLen);
14836
14837 APValue R;
14838 if (!evalShuffleGeneric(
14839 Info, Call: E, Out&: R,
14840 GetSourceIndex: [NumLanes, NumElemsPerLane](unsigned DstIdx, unsigned ShuffleMask)
14841 -> std::pair<unsigned, int> {
14842 // DstIdx determines source. ShuffleMask selects lane in source.
14843 unsigned BitsPerElem = NumLanes / 2;
14844 unsigned IndexMask = (1u << BitsPerElem) - 1;
14845 unsigned Lane = DstIdx / NumElemsPerLane;
14846 unsigned SrcIdx = (Lane < NumLanes / 2) ? 0 : 1;
14847 unsigned BitIdx = BitsPerElem * Lane;
14848 unsigned SrcLaneIdx = (ShuffleMask >> BitIdx) & IndexMask;
14849 unsigned ElemInLane = DstIdx % NumElemsPerLane;
14850 unsigned IdxToPick = SrcLaneIdx * NumElemsPerLane + ElemInLane;
14851 return {SrcIdx, IdxToPick};
14852 }))
14853 return false;
14854 return Success(V: R, E);
14855 }
14856
14857 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14858 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14859 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi:
14860 case X86::BI__builtin_ia32_vgf2p8affineqb_v16qi:
14861 case X86::BI__builtin_ia32_vgf2p8affineqb_v32qi:
14862 case X86::BI__builtin_ia32_vgf2p8affineqb_v64qi: {
14863
14864 APValue X, A;
14865 APSInt Imm;
14866 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: X) ||
14867 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: A) ||
14868 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14869 return false;
14870
14871 assert(X.isVector() && A.isVector());
14872 assert(X.getVectorLength() == A.getVectorLength());
14873
14874 bool IsInverse = false;
14875 switch (BuiltinOp) {
14876 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14877 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14878 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi: {
14879 IsInverse = true;
14880 }
14881 }
14882
14883 unsigned NumBitsInByte = 8;
14884 unsigned NumBytesInQWord = 8;
14885 unsigned NumBitsInQWord = 64;
14886 unsigned NumBytes = A.getVectorLength();
14887 unsigned NumQWords = NumBytes / NumBytesInQWord;
14888 SmallVector<APValue, 64> Result;
14889 Result.reserve(N: NumBytes);
14890
14891 // computing A*X + Imm
14892 for (unsigned QWordIdx = 0; QWordIdx != NumQWords; ++QWordIdx) {
14893 // Extract the QWords from X, A
14894 APInt XQWord(NumBitsInQWord, 0);
14895 APInt AQWord(NumBitsInQWord, 0);
14896 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14897 unsigned Idx = QWordIdx * NumBytesInQWord + ByteIdx;
14898 APInt XByte = X.getVectorElt(I: Idx).getInt();
14899 APInt AByte = A.getVectorElt(I: Idx).getInt();
14900 XQWord.insertBits(SubBits: XByte, bitPosition: ByteIdx * NumBitsInByte);
14901 AQWord.insertBits(SubBits: AByte, bitPosition: ByteIdx * NumBitsInByte);
14902 }
14903
14904 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14905 uint8_t XByte =
14906 XQWord.lshr(shiftAmt: ByteIdx * NumBitsInByte).getLoBits(numBits: 8).getZExtValue();
14907 Result.push_back(Elt: APValue(APSInt(
14908 APInt(8, GFNIAffine(XByte, AQword: AQWord, Imm, Inverse: IsInverse)), false)));
14909 }
14910 }
14911
14912 return Success(V: APValue(Result.data(), Result.size()), E);
14913 }
14914
14915 case X86::BI__builtin_ia32_vgf2p8mulb_v16qi:
14916 case X86::BI__builtin_ia32_vgf2p8mulb_v32qi:
14917 case X86::BI__builtin_ia32_vgf2p8mulb_v64qi: {
14918 APValue A, B;
14919 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
14920 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
14921 return false;
14922
14923 assert(A.isVector() && B.isVector());
14924 assert(A.getVectorLength() == B.getVectorLength());
14925
14926 unsigned NumBytes = A.getVectorLength();
14927 SmallVector<APValue, 64> Result;
14928 Result.reserve(N: NumBytes);
14929
14930 for (unsigned ByteIdx = 0; ByteIdx != NumBytes; ++ByteIdx) {
14931 uint8_t AByte = A.getVectorElt(I: ByteIdx).getInt().getZExtValue();
14932 uint8_t BByte = B.getVectorElt(I: ByteIdx).getInt().getZExtValue();
14933 Result.push_back(Elt: APValue(
14934 APSInt(APInt(8, GFNIMul(AByte, BByte)), /*IsUnsigned=*/false)));
14935 }
14936
14937 return Success(V: APValue(Result.data(), Result.size()), E);
14938 }
14939
14940 case X86::BI__builtin_ia32_insertf32x4_256:
14941 case X86::BI__builtin_ia32_inserti32x4_256:
14942 case X86::BI__builtin_ia32_insertf64x2_256:
14943 case X86::BI__builtin_ia32_inserti64x2_256:
14944 case X86::BI__builtin_ia32_insertf32x4:
14945 case X86::BI__builtin_ia32_inserti32x4:
14946 case X86::BI__builtin_ia32_insertf64x2_512:
14947 case X86::BI__builtin_ia32_inserti64x2_512:
14948 case X86::BI__builtin_ia32_insertf32x8:
14949 case X86::BI__builtin_ia32_inserti32x8:
14950 case X86::BI__builtin_ia32_insertf64x4:
14951 case X86::BI__builtin_ia32_inserti64x4:
14952 case X86::BI__builtin_ia32_vinsertf128_ps256:
14953 case X86::BI__builtin_ia32_vinsertf128_pd256:
14954 case X86::BI__builtin_ia32_vinsertf128_si256:
14955 case X86::BI__builtin_ia32_insert128i256: {
14956 APValue SourceDst, SourceSub;
14957 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceDst) ||
14958 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceSub))
14959 return false;
14960
14961 APSInt Imm;
14962 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14963 return false;
14964
14965 assert(SourceDst.isVector() && SourceSub.isVector());
14966 unsigned DstLen = SourceDst.getVectorLength();
14967 unsigned SubLen = SourceSub.getVectorLength();
14968 assert(SubLen != 0 && DstLen != 0 && (DstLen % SubLen) == 0);
14969 unsigned NumLanes = DstLen / SubLen;
14970 unsigned LaneIdx = (Imm.getZExtValue() % NumLanes) * SubLen;
14971
14972 SmallVector<APValue, 16> ResultElements;
14973 ResultElements.reserve(N: DstLen);
14974
14975 for (unsigned EltNum = 0; EltNum < DstLen; ++EltNum) {
14976 if (EltNum >= LaneIdx && EltNum < LaneIdx + SubLen)
14977 ResultElements.push_back(Elt: SourceSub.getVectorElt(I: EltNum - LaneIdx));
14978 else
14979 ResultElements.push_back(Elt: SourceDst.getVectorElt(I: EltNum));
14980 }
14981
14982 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14983 }
14984
14985 case clang::X86::BI__builtin_ia32_vec_set_v4hi:
14986 case clang::X86::BI__builtin_ia32_vec_set_v16qi:
14987 case clang::X86::BI__builtin_ia32_vec_set_v8hi:
14988 case clang::X86::BI__builtin_ia32_vec_set_v4si:
14989 case clang::X86::BI__builtin_ia32_vec_set_v2di:
14990 case clang::X86::BI__builtin_ia32_vec_set_v32qi:
14991 case clang::X86::BI__builtin_ia32_vec_set_v16hi:
14992 case clang::X86::BI__builtin_ia32_vec_set_v8si:
14993 case clang::X86::BI__builtin_ia32_vec_set_v4di: {
14994 APValue VecVal;
14995 APSInt Scalar, IndexAPS;
14996 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: VecVal, Info) ||
14997 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Scalar, Info) ||
14998 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: IndexAPS, Info))
14999 return false;
15000
15001 QualType ElemTy = E->getType()->castAs<VectorType>()->getElementType();
15002 unsigned ElemWidth = Info.Ctx.getIntWidth(T: ElemTy);
15003 bool ElemUnsigned = ElemTy->isUnsignedIntegerOrEnumerationType();
15004 Scalar.setIsUnsigned(ElemUnsigned);
15005 APSInt ElemAPS = Scalar.extOrTrunc(width: ElemWidth);
15006 APValue ElemAV(ElemAPS);
15007
15008 unsigned NumElems = VecVal.getVectorLength();
15009 unsigned Index =
15010 static_cast<unsigned>(IndexAPS.getZExtValue() & (NumElems - 1));
15011
15012 SmallVector<APValue, 4> Elems;
15013 Elems.reserve(N: NumElems);
15014 for (unsigned ElemNum = 0; ElemNum != NumElems; ++ElemNum)
15015 Elems.push_back(Elt: ElemNum == Index ? ElemAV : VecVal.getVectorElt(I: ElemNum));
15016
15017 return Success(V: APValue(Elems.data(), NumElems), E);
15018 }
15019
15020 case X86::BI__builtin_ia32_pslldqi128_byteshift:
15021 case X86::BI__builtin_ia32_pslldqi256_byteshift:
15022 case X86::BI__builtin_ia32_pslldqi512_byteshift: {
15023 APValue R;
15024 if (!evalShuffleGeneric(
15025 Info, Call: E, Out&: R,
15026 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
15027 unsigned LaneBase = (DstIdx / 16) * 16;
15028 unsigned LaneIdx = DstIdx % 16;
15029 if (LaneIdx < Shift)
15030 return std::make_pair(x: 0, y: -1);
15031
15032 return std::make_pair(
15033 x: 0, y: static_cast<int>(LaneBase + LaneIdx - Shift));
15034 }))
15035 return false;
15036 return Success(V: R, E);
15037 }
15038
15039 case X86::BI__builtin_ia32_psrldqi128_byteshift:
15040 case X86::BI__builtin_ia32_psrldqi256_byteshift:
15041 case X86::BI__builtin_ia32_psrldqi512_byteshift: {
15042 APValue R;
15043 if (!evalShuffleGeneric(
15044 Info, Call: E, Out&: R,
15045 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
15046 unsigned LaneBase = (DstIdx / 16) * 16;
15047 unsigned LaneIdx = DstIdx % 16;
15048 if (LaneIdx + Shift < 16)
15049 return std::make_pair(
15050 x: 0, y: static_cast<int>(LaneBase + LaneIdx + Shift));
15051
15052 return std::make_pair(x: 0, y: -1);
15053 }))
15054 return false;
15055 return Success(V: R, E);
15056 }
15057
15058 case X86::BI__builtin_ia32_palignr128:
15059 case X86::BI__builtin_ia32_palignr256:
15060 case X86::BI__builtin_ia32_palignr512: {
15061 APValue R;
15062 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Shift) {
15063 // Default to -1 → zero-fill this destination element
15064 unsigned VecIdx = 1;
15065 int ElemIdx = -1;
15066
15067 int Lane = DstIdx / 16;
15068 int Offset = DstIdx % 16;
15069
15070 // Elements come from VecB first, then VecA after the shift boundary
15071 unsigned ShiftedIdx = Offset + (Shift & 0xFF);
15072 if (ShiftedIdx < 16) { // from VecB
15073 ElemIdx = ShiftedIdx + (Lane * 16);
15074 } else if (ShiftedIdx < 32) { // from VecA
15075 VecIdx = 0;
15076 ElemIdx = (ShiftedIdx - 16) + (Lane * 16);
15077 }
15078
15079 return std::pair<unsigned, int>{VecIdx, ElemIdx};
15080 }))
15081 return false;
15082 return Success(V: R, E);
15083 }
15084 case X86::BI__builtin_ia32_alignd128:
15085 case X86::BI__builtin_ia32_alignd256:
15086 case X86::BI__builtin_ia32_alignd512:
15087 case X86::BI__builtin_ia32_alignq128:
15088 case X86::BI__builtin_ia32_alignq256:
15089 case X86::BI__builtin_ia32_alignq512: {
15090 APValue R;
15091 unsigned NumElems = E->getType()->castAs<VectorType>()->getNumElements();
15092 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15093 GetSourceIndex: [NumElems](unsigned DstIdx, unsigned Shift) {
15094 unsigned Imm = Shift & 0xFF;
15095 unsigned EffectiveShift = Imm & (NumElems - 1);
15096 unsigned SourcePos = DstIdx + EffectiveShift;
15097 unsigned VecIdx = SourcePos < NumElems ? 1 : 0;
15098 unsigned ElemIdx = SourcePos & (NumElems - 1);
15099
15100 return std::pair<unsigned, int>{
15101 VecIdx, static_cast<int>(ElemIdx)};
15102 }))
15103 return false;
15104 return Success(V: R, E);
15105 }
15106 case X86::BI__builtin_ia32_permvarsi256:
15107 case X86::BI__builtin_ia32_permvarsf256:
15108 case X86::BI__builtin_ia32_permvardf512:
15109 case X86::BI__builtin_ia32_permvardi512:
15110 case X86::BI__builtin_ia32_permvarhi128: {
15111 APValue R;
15112 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15113 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15114 int Offset = ShuffleMask & 0x7;
15115 return std::pair<unsigned, int>{0, Offset};
15116 }))
15117 return false;
15118 return Success(V: R, E);
15119 }
15120 case X86::BI__builtin_ia32_permvarqi128:
15121 case X86::BI__builtin_ia32_permvarhi256:
15122 case X86::BI__builtin_ia32_permvarsi512:
15123 case X86::BI__builtin_ia32_permvarsf512: {
15124 APValue R;
15125 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15126 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15127 int Offset = ShuffleMask & 0xF;
15128 return std::pair<unsigned, int>{0, Offset};
15129 }))
15130 return false;
15131 return Success(V: R, E);
15132 }
15133 case X86::BI__builtin_ia32_permvardi256:
15134 case X86::BI__builtin_ia32_permvardf256: {
15135 APValue R;
15136 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15137 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15138 int Offset = ShuffleMask & 0x3;
15139 return std::pair<unsigned, int>{0, Offset};
15140 }))
15141 return false;
15142 return Success(V: R, E);
15143 }
15144 case X86::BI__builtin_ia32_permvarqi256:
15145 case X86::BI__builtin_ia32_permvarhi512: {
15146 APValue R;
15147 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15148 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15149 int Offset = ShuffleMask & 0x1F;
15150 return std::pair<unsigned, int>{0, Offset};
15151 }))
15152 return false;
15153 return Success(V: R, E);
15154 }
15155 case X86::BI__builtin_ia32_permvarqi512: {
15156 APValue R;
15157 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15158 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15159 int Offset = ShuffleMask & 0x3F;
15160 return std::pair<unsigned, int>{0, Offset};
15161 }))
15162 return false;
15163 return Success(V: R, E);
15164 }
15165 case X86::BI__builtin_ia32_vpermi2varq128:
15166 case X86::BI__builtin_ia32_vpermi2varpd128: {
15167 APValue R;
15168 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15169 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15170 int Offset = ShuffleMask & 0x1;
15171 unsigned SrcIdx = (ShuffleMask >> 1) & 0x1;
15172 return std::pair<unsigned, int>{SrcIdx, Offset};
15173 }))
15174 return false;
15175 return Success(V: R, E);
15176 }
15177 case X86::BI__builtin_ia32_vpermi2vard128:
15178 case X86::BI__builtin_ia32_vpermi2varps128:
15179 case X86::BI__builtin_ia32_vpermi2varq256:
15180 case X86::BI__builtin_ia32_vpermi2varpd256: {
15181 APValue R;
15182 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15183 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15184 int Offset = ShuffleMask & 0x3;
15185 unsigned SrcIdx = (ShuffleMask >> 2) & 0x1;
15186 return std::pair<unsigned, int>{SrcIdx, Offset};
15187 }))
15188 return false;
15189 return Success(V: R, E);
15190 }
15191 case X86::BI__builtin_ia32_vpermi2varhi128:
15192 case X86::BI__builtin_ia32_vpermi2vard256:
15193 case X86::BI__builtin_ia32_vpermi2varps256:
15194 case X86::BI__builtin_ia32_vpermi2varq512:
15195 case X86::BI__builtin_ia32_vpermi2varpd512: {
15196 APValue R;
15197 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15198 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15199 int Offset = ShuffleMask & 0x7;
15200 unsigned SrcIdx = (ShuffleMask >> 3) & 0x1;
15201 return std::pair<unsigned, int>{SrcIdx, Offset};
15202 }))
15203 return false;
15204 return Success(V: R, E);
15205 }
15206 case X86::BI__builtin_ia32_vpermi2varqi128:
15207 case X86::BI__builtin_ia32_vpermi2varhi256:
15208 case X86::BI__builtin_ia32_vpermi2vard512:
15209 case X86::BI__builtin_ia32_vpermi2varps512: {
15210 APValue R;
15211 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15212 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15213 int Offset = ShuffleMask & 0xF;
15214 unsigned SrcIdx = (ShuffleMask >> 4) & 0x1;
15215 return std::pair<unsigned, int>{SrcIdx, Offset};
15216 }))
15217 return false;
15218 return Success(V: R, E);
15219 }
15220 case X86::BI__builtin_ia32_vpermi2varqi256:
15221 case X86::BI__builtin_ia32_vpermi2varhi512: {
15222 APValue R;
15223 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15224 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15225 int Offset = ShuffleMask & 0x1F;
15226 unsigned SrcIdx = (ShuffleMask >> 5) & 0x1;
15227 return std::pair<unsigned, int>{SrcIdx, Offset};
15228 }))
15229 return false;
15230 return Success(V: R, E);
15231 }
15232 case X86::BI__builtin_ia32_vpermi2varqi512: {
15233 APValue R;
15234 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15235 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15236 int Offset = ShuffleMask & 0x3F;
15237 unsigned SrcIdx = (ShuffleMask >> 6) & 0x1;
15238 return std::pair<unsigned, int>{SrcIdx, Offset};
15239 }))
15240 return false;
15241 return Success(V: R, E);
15242 }
15243
15244 case clang::X86::BI__builtin_ia32_minps:
15245 case clang::X86::BI__builtin_ia32_minpd:
15246 case clang::X86::BI__builtin_ia32_minps256:
15247 case clang::X86::BI__builtin_ia32_minpd256:
15248 case clang::X86::BI__builtin_ia32_minps512:
15249 case clang::X86::BI__builtin_ia32_minpd512:
15250 case clang::X86::BI__builtin_ia32_minph128:
15251 case clang::X86::BI__builtin_ia32_minph256:
15252 case clang::X86::BI__builtin_ia32_minph512:
15253 return EvaluateFpBinOpExpr(
15254 [](const APFloat &A, const APFloat &B,
15255 std::optional<APSInt>) -> std::optional<APFloat> {
15256 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
15257 B.isInfinity() || B.isDenormal())
15258 return std::nullopt;
15259 if (A.isZero() && B.isZero())
15260 return B;
15261 return llvm::minimum(A, B);
15262 });
15263
15264 case clang::X86::BI__builtin_ia32_minss:
15265 case clang::X86::BI__builtin_ia32_minsd:
15266 return EvaluateFpBinOpExpr(
15267 [](const APFloat &A, const APFloat &B,
15268 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15269 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/true);
15270 },
15271 /*IsScalar=*/true);
15272
15273 case clang::X86::BI__builtin_ia32_minsd_round_mask:
15274 case clang::X86::BI__builtin_ia32_minss_round_mask:
15275 case clang::X86::BI__builtin_ia32_minsh_round_mask:
15276 case clang::X86::BI__builtin_ia32_maxsd_round_mask:
15277 case clang::X86::BI__builtin_ia32_maxss_round_mask:
15278 case clang::X86::BI__builtin_ia32_maxsh_round_mask: {
15279 bool IsMin = BuiltinOp == clang::X86::BI__builtin_ia32_minsd_round_mask ||
15280 BuiltinOp == clang::X86::BI__builtin_ia32_minss_round_mask ||
15281 BuiltinOp == clang::X86::BI__builtin_ia32_minsh_round_mask;
15282 return EvaluateScalarFpRoundMaskBinOp(
15283 [IsMin](const APFloat &A, const APFloat &B,
15284 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15285 return EvalScalarMinMaxFp(A, B, RoundingMode, IsMin);
15286 });
15287 }
15288
15289 case clang::X86::BI__builtin_ia32_maxps:
15290 case clang::X86::BI__builtin_ia32_maxpd:
15291 case clang::X86::BI__builtin_ia32_maxps256:
15292 case clang::X86::BI__builtin_ia32_maxpd256:
15293 case clang::X86::BI__builtin_ia32_maxps512:
15294 case clang::X86::BI__builtin_ia32_maxpd512:
15295 case clang::X86::BI__builtin_ia32_maxph128:
15296 case clang::X86::BI__builtin_ia32_maxph256:
15297 case clang::X86::BI__builtin_ia32_maxph512:
15298 return EvaluateFpBinOpExpr(
15299 [](const APFloat &A, const APFloat &B,
15300 std::optional<APSInt>) -> std::optional<APFloat> {
15301 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
15302 B.isInfinity() || B.isDenormal())
15303 return std::nullopt;
15304 if (A.isZero() && B.isZero())
15305 return B;
15306 return llvm::maximum(A, B);
15307 });
15308
15309 case clang::X86::BI__builtin_ia32_maxss:
15310 case clang::X86::BI__builtin_ia32_maxsd:
15311 return EvaluateFpBinOpExpr(
15312 [](const APFloat &A, const APFloat &B,
15313 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15314 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/false);
15315 },
15316 /*IsScalar=*/true);
15317
15318 case clang::X86::BI__builtin_ia32_vcvtps2ph:
15319 case clang::X86::BI__builtin_ia32_vcvtps2ph256: {
15320 APValue SrcVec;
15321 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SrcVec))
15322 return false;
15323
15324 APSInt Imm;
15325 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info))
15326 return false;
15327
15328 const auto *SrcVTy = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
15329 unsigned SrcNumElems = SrcVTy->getNumElements();
15330 const auto *DstVTy = E->getType()->castAs<VectorType>();
15331 unsigned DstNumElems = DstVTy->getNumElements();
15332 QualType DstElemTy = DstVTy->getElementType();
15333
15334 const llvm::fltSemantics &HalfSem =
15335 Info.Ctx.getFloatTypeSemantics(T: Info.Ctx.HalfTy);
15336
15337 int ImmVal = Imm.getZExtValue();
15338 bool UseMXCSR = (ImmVal & 4) != 0;
15339 bool IsFPConstrained =
15340 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained();
15341
15342 llvm::RoundingMode RM;
15343 if (!UseMXCSR) {
15344 switch (ImmVal & 3) {
15345 case 0:
15346 RM = llvm::RoundingMode::NearestTiesToEven;
15347 break;
15348 case 1:
15349 RM = llvm::RoundingMode::TowardNegative;
15350 break;
15351 case 2:
15352 RM = llvm::RoundingMode::TowardPositive;
15353 break;
15354 case 3:
15355 RM = llvm::RoundingMode::TowardZero;
15356 break;
15357 default:
15358 llvm_unreachable("Invalid immediate rounding mode");
15359 }
15360 } else {
15361 RM = llvm::RoundingMode::NearestTiesToEven;
15362 }
15363
15364 SmallVector<APValue, 8> ResultElements;
15365 ResultElements.reserve(N: DstNumElems);
15366
15367 for (unsigned I = 0; I < SrcNumElems; ++I) {
15368 APFloat SrcVal = SrcVec.getVectorElt(I).getFloat();
15369
15370 bool LostInfo;
15371 APFloat::opStatus St = SrcVal.convert(ToSemantics: HalfSem, RM, losesInfo: &LostInfo);
15372
15373 if (UseMXCSR && IsFPConstrained && St != APFloat::opOK) {
15374 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
15375 return false;
15376 }
15377
15378 APSInt DstInt(SrcVal.bitcastToAPInt(),
15379 DstElemTy->isUnsignedIntegerOrEnumerationType());
15380 ResultElements.push_back(Elt: APValue(DstInt));
15381 }
15382
15383 if (DstNumElems > SrcNumElems) {
15384 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: DstElemTy);
15385 for (unsigned I = SrcNumElems; I < DstNumElems; ++I) {
15386 ResultElements.push_back(Elt: APValue(Zero));
15387 }
15388 }
15389
15390 return Success(V: ResultElements, E);
15391 }
15392 case X86::BI__builtin_ia32_vperm2f128_pd256:
15393 case X86::BI__builtin_ia32_vperm2f128_ps256:
15394 case X86::BI__builtin_ia32_vperm2f128_si256:
15395 case X86::BI__builtin_ia32_permti256: {
15396 unsigned NumElements =
15397 E->getArg(Arg: 0)->getType()->getAs<VectorType>()->getNumElements();
15398 unsigned PreservedBitsCnt = NumElements >> 2;
15399 APValue R;
15400 if (!evalShuffleGeneric(
15401 Info, Call: E, Out&: R,
15402 GetSourceIndex: [PreservedBitsCnt](unsigned DstIdx, unsigned ShuffleMask) {
15403 unsigned ControlBitsCnt = DstIdx >> PreservedBitsCnt << 2;
15404 unsigned ControlBits = ShuffleMask >> ControlBitsCnt;
15405
15406 if (ControlBits & 0b1000)
15407 return std::make_pair(x: 0u, y: -1);
15408
15409 unsigned SrcVecIdx = (ControlBits & 0b10) >> 1;
15410 unsigned PreservedBitsMask = (1 << PreservedBitsCnt) - 1;
15411 int SrcIdx = ((ControlBits & 0b1) << PreservedBitsCnt) |
15412 (DstIdx & PreservedBitsMask);
15413 return std::make_pair(x&: SrcVecIdx, y&: SrcIdx);
15414 }))
15415 return false;
15416 return Success(V: R, E);
15417 }
15418 case X86::BI__builtin_ia32_vpdpwssd128:
15419 case X86::BI__builtin_ia32_vpdpwssd256:
15420 case X86::BI__builtin_ia32_vpdpwssd512:
15421 case X86::BI__builtin_ia32_vpdpbusd128:
15422 case X86::BI__builtin_ia32_vpdpbusd256:
15423 case X86::BI__builtin_ia32_vpdpbusd512:
15424 return EvalVectorDotProduct(false);
15425 case X86::BI__builtin_ia32_vpdpwssds128:
15426 case X86::BI__builtin_ia32_vpdpwssds256:
15427 case X86::BI__builtin_ia32_vpdpwssds512:
15428 case X86::BI__builtin_ia32_vpdpbusds128:
15429 case X86::BI__builtin_ia32_vpdpbusds256:
15430 case X86::BI__builtin_ia32_vpdpbusds512:
15431 return EvalVectorDotProduct(true);
15432 case X86::BI__builtin_ia32_cvtpd2dq:
15433 case X86::BI__builtin_ia32_cvtps2dq:
15434 case X86::BI__builtin_ia32_cvttpd2dq:
15435 case X86::BI__builtin_ia32_cvttps2dq:
15436 case X86::BI__builtin_ia32_cvtpd2dq256:
15437 case X86::BI__builtin_ia32_cvtps2dq256:
15438 case X86::BI__builtin_ia32_cvttpd2dq256:
15439 case X86::BI__builtin_ia32_cvttps2dq256: {
15440 APValue SrcVec;
15441 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SrcVec) || !SrcVec.isVector())
15442 return false;
15443
15444 const auto *VT = E->getType()->castAs<VectorType>();
15445 QualType EltTy = VT->getElementType();
15446 bool isUnsigned = EltTy->isUnsignedIntegerType();
15447 unsigned BitWidth = Info.Ctx.getIntWidth(T: EltTy);
15448
15449 unsigned NumSrcElems = SrcVec.getVectorLength();
15450 unsigned NumDstElems = VT->getNumElements();
15451
15452 SmallVector<APValue, 8> ResultElts;
15453 for (unsigned i = 0; i != NumDstElems; ++i) {
15454 if (i < NumSrcElems) {
15455 llvm::APFloat FloatElem = SrcVec.getVectorElt(I: i).getFloat();
15456 llvm::APSInt IntResult(BitWidth, isUnsigned);
15457 bool IsExact = false;
15458 // We only allow exact conversions so rounding mode does not matter for
15459 // cvt* and cvtt* builtins
15460 FloatElem.convertToInteger(Result&: IntResult, RM: llvm::APFloat::rmTowardZero,
15461 IsExact: &IsExact);
15462 if (!IsExact)
15463 return false;
15464 ResultElts.push_back(Elt: APValue(IntResult));
15465 } else
15466 // Pad remaining lanes with zero
15467 ResultElts.push_back(Elt: APValue(llvm::APSInt(BitWidth, isUnsigned)));
15468 }
15469 return Success(V: ResultElts, E);
15470 }
15471 }
15472}
15473
15474bool VectorExprEvaluator::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
15475 APValue Source;
15476 QualType SourceVecType = E->getSrcExpr()->getType();
15477 if (!EvaluateAsRValue(Info, E: E->getSrcExpr(), Result&: Source))
15478 return false;
15479
15480 QualType DestTy = E->getType()->castAs<VectorType>()->getElementType();
15481 QualType SourceTy = SourceVecType->castAs<VectorType>()->getElementType();
15482
15483 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15484
15485 auto SourceLen = Source.getVectorLength();
15486 SmallVector<APValue, 4> ResultElements;
15487 ResultElements.reserve(N: SourceLen);
15488 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
15489 APValue Elt;
15490 if (!handleVectorElementCast(Info, FPO, E, SourceTy, DestTy,
15491 Original: Source.getVectorElt(I: EltNum), Result&: Elt))
15492 return false;
15493 ResultElements.push_back(Elt: std::move(Elt));
15494 }
15495
15496 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15497}
15498
15499static bool handleVectorShuffle(EvalInfo &Info, const ShuffleVectorExpr *E,
15500 QualType ElemType, APValue const &VecVal1,
15501 APValue const &VecVal2, unsigned EltNum,
15502 APValue &Result) {
15503 unsigned const TotalElementsInInputVector1 = VecVal1.getVectorLength();
15504 unsigned const TotalElementsInInputVector2 = VecVal2.getVectorLength();
15505
15506 APSInt IndexVal = E->getShuffleMaskIdx(N: EltNum);
15507 int64_t index = IndexVal.getExtValue();
15508 // The spec says that -1 should be treated as undef for optimizations,
15509 // but in constexpr we'd have to produce an APValue::Indeterminate,
15510 // which is prohibited from being a top-level constant value. Emit a
15511 // diagnostic instead.
15512 if (index == -1) {
15513 Info.FFDiag(
15514 E, DiagId: diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
15515 << EltNum;
15516 return false;
15517 }
15518
15519 if (index < 0 ||
15520 index >= TotalElementsInInputVector1 + TotalElementsInInputVector2)
15521 llvm_unreachable("Out of bounds shuffle index");
15522
15523 if (index >= TotalElementsInInputVector1)
15524 Result = VecVal2.getVectorElt(I: index - TotalElementsInInputVector1);
15525 else
15526 Result = VecVal1.getVectorElt(I: index);
15527 return true;
15528}
15529
15530bool VectorExprEvaluator::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
15531 // FIXME: Unary shuffle with mask not currently supported.
15532 if (E->getNumSubExprs() == 2)
15533 return Error(E);
15534 APValue VecVal1;
15535 const Expr *Vec1 = E->getExpr(Index: 0);
15536 if (!EvaluateAsRValue(Info, E: Vec1, Result&: VecVal1))
15537 return false;
15538 APValue VecVal2;
15539 const Expr *Vec2 = E->getExpr(Index: 1);
15540 if (!EvaluateAsRValue(Info, E: Vec2, Result&: VecVal2))
15541 return false;
15542
15543 VectorType const *DestVecTy = E->getType()->castAs<VectorType>();
15544 QualType DestElTy = DestVecTy->getElementType();
15545
15546 auto TotalElementsInOutputVector = DestVecTy->getNumElements();
15547
15548 SmallVector<APValue, 4> ResultElements;
15549 ResultElements.reserve(N: TotalElementsInOutputVector);
15550 for (unsigned EltNum = 0; EltNum < TotalElementsInOutputVector; ++EltNum) {
15551 APValue Elt;
15552 if (!handleVectorShuffle(Info, E, ElemType: DestElTy, VecVal1, VecVal2, EltNum, Result&: Elt))
15553 return false;
15554 ResultElements.push_back(Elt: std::move(Elt));
15555 }
15556
15557 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15558}
15559
15560//===----------------------------------------------------------------------===//
15561// Matrix Evaluation
15562//===----------------------------------------------------------------------===//
15563
15564namespace {
15565class MatrixExprEvaluator : public ExprEvaluatorBase<MatrixExprEvaluator> {
15566 APValue &Result;
15567
15568public:
15569 MatrixExprEvaluator(EvalInfo &Info, APValue &Result)
15570 : ExprEvaluatorBaseTy(Info), Result(Result) {}
15571
15572 bool Success(ArrayRef<APValue> M, const Expr *E) {
15573 auto *CMTy = E->getType()->castAs<ConstantMatrixType>();
15574 assert(M.size() == CMTy->getNumElementsFlattened());
15575 // FIXME: remove this APValue copy.
15576 Result = APValue(M.data(), CMTy->getNumRows(), CMTy->getNumColumns());
15577 return true;
15578 }
15579 bool Success(const APValue &M, const Expr *E) {
15580 assert(M.isMatrix() && "expected matrix");
15581 Result = M;
15582 return true;
15583 }
15584
15585 bool VisitCastExpr(const CastExpr *E);
15586 bool VisitInitListExpr(const InitListExpr *E);
15587};
15588} // end anonymous namespace
15589
15590static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info) {
15591 assert(E->isPRValue() && E->getType()->isConstantMatrixType() &&
15592 "not a matrix prvalue");
15593 return MatrixExprEvaluator(Info, Result).Visit(S: E);
15594}
15595
15596bool MatrixExprEvaluator::VisitCastExpr(const CastExpr *E) {
15597 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15598 unsigned NumRows = MT->getNumRows();
15599 unsigned NumCols = MT->getNumColumns();
15600 unsigned NElts = NumRows * NumCols;
15601 QualType EltTy = MT->getElementType();
15602 const Expr *SE = E->getSubExpr();
15603
15604 switch (E->getCastKind()) {
15605 case CK_HLSLAggregateSplatCast: {
15606 APValue Val;
15607 QualType ValTy;
15608
15609 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15610 return false;
15611
15612 APValue CastedVal;
15613 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15614 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: EltTy, Original: Val, Result&: CastedVal))
15615 return false;
15616
15617 SmallVector<APValue, 16> SplatEls(NElts, CastedVal);
15618 return Success(M: SplatEls, E);
15619 }
15620 case CK_HLSLElementwiseCast: {
15621 SmallVector<APValue> SrcVals;
15622 SmallVector<QualType> SrcTypes;
15623
15624 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
15625 return false;
15626
15627 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15628 SmallVector<QualType, 16> DestTypes(NElts, EltTy);
15629 SmallVector<APValue, 16> ResultEls(NElts);
15630 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
15631 Results&: ResultEls))
15632 return false;
15633 return Success(M: ResultEls, E);
15634 }
15635 default:
15636 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15637 }
15638}
15639
15640bool MatrixExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
15641 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15642 QualType EltTy = MT->getElementType();
15643
15644 assert(E->getNumInits() == MT->getNumElementsFlattened() &&
15645 "Expected number of elements in initializer list to match the number "
15646 "of matrix elements");
15647
15648 SmallVector<APValue, 16> Elements;
15649 Elements.reserve(N: MT->getNumElementsFlattened());
15650
15651 // The following loop assumes the elements of the matrix InitListExpr are in
15652 // row-major order, which matches the row-major ordering assumption of the
15653 // matrix APValue.
15654 for (unsigned I = 0, N = MT->getNumElementsFlattened(); I < N; ++I) {
15655 if (EltTy->isIntegerType()) {
15656 llvm::APSInt IntVal;
15657 if (!EvaluateInteger(E: E->getInit(Init: I), Result&: IntVal, Info))
15658 return false;
15659 Elements.push_back(Elt: APValue(IntVal));
15660 } else {
15661 llvm::APFloat FloatVal(0.0);
15662 if (!EvaluateFloat(E: E->getInit(Init: I), Result&: FloatVal, Info))
15663 return false;
15664 Elements.push_back(Elt: APValue(FloatVal));
15665 }
15666 }
15667
15668 return Success(M: Elements, E);
15669}
15670
15671//===----------------------------------------------------------------------===//
15672// Array Evaluation
15673//===----------------------------------------------------------------------===//
15674
15675namespace {
15676 class ArrayExprEvaluator
15677 : public ExprEvaluatorBase<ArrayExprEvaluator> {
15678 const LValue &This;
15679 APValue &Result;
15680 public:
15681
15682 ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
15683 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
15684
15685 bool Success(const APValue &V, const Expr *E) {
15686 assert(V.isArray() && "expected array");
15687 Result = V;
15688 return true;
15689 }
15690
15691 bool ZeroInitialization(const Expr *E) {
15692 const ConstantArrayType *CAT =
15693 Info.Ctx.getAsConstantArrayType(T: E->getType());
15694 if (!CAT) {
15695 if (E->getType()->isIncompleteArrayType()) {
15696 // We can be asked to zero-initialize a flexible array member; this
15697 // is represented as an ImplicitValueInitExpr of incomplete array
15698 // type. In this case, the array has zero elements.
15699 Result = APValue(APValue::UninitArray(), 0, 0);
15700 return true;
15701 }
15702 // FIXME: We could handle VLAs here.
15703 return Error(E);
15704 }
15705
15706 Result = APValue(APValue::UninitArray(), 0, CAT->getZExtSize());
15707 if (!Result.hasArrayFiller())
15708 return true;
15709
15710 // Zero-initialize all elements.
15711 LValue Subobject = This;
15712 Subobject.addArray(Info, E, CAT);
15713 ImplicitValueInitExpr VIE(CAT->getElementType());
15714 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject, E: &VIE);
15715 }
15716
15717 bool VisitCallExpr(const CallExpr *E) {
15718 return handleCallExpr(E, Result, ResultSlot: &This);
15719 }
15720 bool VisitCastExpr(const CastExpr *E);
15721 bool VisitInitListExpr(const InitListExpr *E,
15722 QualType AllocType = QualType());
15723 bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E);
15724 bool VisitCXXConstructExpr(const CXXConstructExpr *E);
15725 bool VisitCXXConstructExpr(const CXXConstructExpr *E,
15726 const LValue &Subobject,
15727 APValue *Value, QualType Type);
15728 bool VisitStringLiteral(const StringLiteral *E,
15729 QualType AllocType = QualType()) {
15730 expandStringLiteral(Info, S: E, Result, AllocType);
15731 return true;
15732 }
15733 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
15734 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
15735 ArrayRef<Expr *> Args,
15736 const Expr *ArrayFiller,
15737 QualType AllocType = QualType());
15738 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
15739 };
15740} // end anonymous namespace
15741
15742static bool EvaluateArray(const Expr *E, const LValue &This,
15743 APValue &Result, EvalInfo &Info) {
15744 assert(!E->isValueDependent());
15745 assert(E->isPRValue() && E->getType()->isArrayType() &&
15746 "not an array prvalue");
15747 return ArrayExprEvaluator(Info, This, Result).Visit(S: E);
15748}
15749
15750static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
15751 APValue &Result, const InitListExpr *ILE,
15752 QualType AllocType) {
15753 assert(!ILE->isValueDependent());
15754 assert(ILE->isPRValue() && ILE->getType()->isArrayType() &&
15755 "not an array prvalue");
15756 return ArrayExprEvaluator(Info, This, Result)
15757 .VisitInitListExpr(E: ILE, AllocType);
15758}
15759
15760static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
15761 APValue &Result,
15762 const CXXConstructExpr *CCE,
15763 QualType AllocType) {
15764 assert(!CCE->isValueDependent());
15765 assert(CCE->isPRValue() && CCE->getType()->isArrayType() &&
15766 "not an array prvalue");
15767 return ArrayExprEvaluator(Info, This, Result)
15768 .VisitCXXConstructExpr(E: CCE, Subobject: This, Value: &Result, Type: AllocType);
15769}
15770
15771// Return true iff the given array filler may depend on the element index.
15772static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) {
15773 // For now, just allow non-class value-initialization and initialization
15774 // lists comprised of them.
15775 if (isa<ImplicitValueInitExpr>(Val: FillerExpr))
15776 return false;
15777 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Val: FillerExpr)) {
15778 for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) {
15779 if (MaybeElementDependentArrayFiller(FillerExpr: ILE->getInit(Init: I)))
15780 return true;
15781 }
15782
15783 if (ILE->hasArrayFiller() &&
15784 MaybeElementDependentArrayFiller(FillerExpr: ILE->getArrayFiller()))
15785 return true;
15786
15787 return false;
15788 }
15789 return true;
15790}
15791
15792bool ArrayExprEvaluator::VisitCastExpr(const CastExpr *E) {
15793 const Expr *SE = E->getSubExpr();
15794
15795 switch (E->getCastKind()) {
15796 default:
15797 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15798 case CK_HLSLAggregateSplatCast: {
15799 APValue Val;
15800 QualType ValTy;
15801
15802 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15803 return false;
15804
15805 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
15806
15807 SmallVector<APValue> SplatEls(NEls, Val);
15808 SmallVector<QualType> SplatType(NEls, ValTy);
15809
15810 // cast the elements
15811 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15812 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
15813 ElTypes&: SplatType))
15814 return false;
15815
15816 return true;
15817 }
15818 case CK_HLSLElementwiseCast: {
15819 SmallVector<APValue> SrcEls;
15820 SmallVector<QualType> SrcTypes;
15821
15822 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals&: SrcEls, SrcTypes))
15823 return false;
15824
15825 // cast the elements
15826 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15827 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
15828 ElTypes&: SrcTypes))
15829 return false;
15830 return true;
15831 }
15832 }
15833}
15834
15835bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E,
15836 QualType AllocType) {
15837 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15838 T: AllocType.isNull() ? E->getType() : AllocType);
15839 if (!CAT)
15840 return Error(E);
15841
15842 // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
15843 // an appropriately-typed string literal enclosed in braces.
15844 if (E->isStringLiteralInit()) {
15845 auto *SL = dyn_cast<StringLiteral>(Val: E->getInit(Init: 0)->IgnoreParenImpCasts());
15846 // FIXME: Support ObjCEncodeExpr here once we support it in
15847 // ArrayExprEvaluator generally.
15848 if (!SL)
15849 return Error(E);
15850 return VisitStringLiteral(E: SL, AllocType);
15851 }
15852 // Any other transparent list init will need proper handling of the
15853 // AllocType; we can't just recurse to the inner initializer.
15854 assert(!E->isTransparent() &&
15855 "transparent array list initialization is not string literal init?");
15856
15857 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits(), ArrayFiller: E->getArrayFiller(),
15858 AllocType);
15859}
15860
15861bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
15862 const Expr *ExprToVisit, ArrayRef<Expr *> Args, const Expr *ArrayFiller,
15863 QualType AllocType) {
15864 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15865 T: AllocType.isNull() ? ExprToVisit->getType() : AllocType);
15866
15867 bool Success = true;
15868
15869 unsigned NumEltsToInit = Args.size();
15870 unsigned NumElts = CAT->getZExtSize();
15871
15872 // If the initializer might depend on the array index, run it for each
15873 // array element.
15874 if (NumEltsToInit != NumElts &&
15875 MaybeElementDependentArrayFiller(FillerExpr: ArrayFiller)) {
15876 NumEltsToInit = NumElts;
15877 } else {
15878 // Add additional elements represented by EmbedExpr.
15879 for (auto *Init : Args) {
15880 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts()))
15881 NumEltsToInit += EmbedS->getDataElementCount() - 1;
15882 }
15883 // If we have extra elements in the list, they will be discarded.
15884 if (NumEltsToInit > NumElts)
15885 NumEltsToInit = NumElts;
15886 // If we're overwriting memory which already has an object, make sure we
15887 // don't reduce the number of non-filler elements. (It's possible to
15888 // optimize this in some cases, but the logic gets really complicated.)
15889 if (Result.hasValue() && NumEltsToInit < Result.getArrayInitializedElts())
15890 NumEltsToInit = Result.getArrayInitializedElts();
15891 }
15892
15893 LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
15894 << NumEltsToInit << ".\n");
15895
15896 if (!Result.hasValue()) {
15897 Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15898 } else if (Result.getArrayInitializedElts() != NumEltsToInit) {
15899 // Number of inititalized elts changed. Recreate the APValue, and copy over
15900 // the relevant elements. (This is essentially just fixing the internal
15901 // representation of the value, because it's tied to the number of
15902 // non-filler elements.)
15903 //
15904 // This should be hit rarely, but there are some edge cases:
15905 //
15906 // - The array could be zero-initialized.
15907 // - There could be a DesignatedInitListExpr.
15908 // - operator new[] can be used to start the lifetime early.
15909 APValue NewResult = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15910 // First copy existing elements.
15911 unsigned NumOldElts = Result.getArrayInitializedElts();
15912 for (unsigned I = 0; I < NumOldElts; ++I) {
15913 NewResult.getArrayInitializedElt(I) =
15914 std::move(Result.getArrayInitializedElt(I));
15915 }
15916 // Then copy the array filler over the remaining elements.
15917 for (unsigned I = Result.getArrayInitializedElts(); I < NumEltsToInit; ++I)
15918 NewResult.getArrayInitializedElt(I) = Result.getArrayFiller();
15919 if (NewResult.hasArrayFiller() && Result.hasArrayFiller())
15920 NewResult.getArrayFiller() = Result.getArrayFiller();
15921 Result = std::move(NewResult);
15922 }
15923
15924 LValue Subobject = This;
15925 Subobject.addArray(Info, E: ExprToVisit, CAT);
15926 auto Eval = [&](const Expr *Init, unsigned ArrayIndex) {
15927 if (Init->isValueDependent())
15928 return EvaluateDependentExpr(E: Init, Info);
15929
15930 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
15931 // aren't supposed to be modified.
15932 if (isa<NoInitExpr>(Val: Init))
15933 return true;
15934
15935 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: ArrayIndex), Info,
15936 This: Subobject, E: Init) ||
15937 !HandleLValueArrayAdjustment(Info, E: Init, LVal&: Subobject,
15938 EltTy: CAT->getElementType(), Adjustment: 1)) {
15939 if (!Info.noteFailure())
15940 return false;
15941 Success = false;
15942 }
15943 return true;
15944 };
15945 unsigned ArrayIndex = 0;
15946 QualType DestTy = CAT->getElementType();
15947 APSInt Value(Info.Ctx.getTypeSize(T: DestTy), DestTy->isUnsignedIntegerType());
15948 for (unsigned Index = 0; Index != NumEltsToInit; ++Index) {
15949 const Expr *Init = Index < Args.size() ? Args[Index] : ArrayFiller;
15950 if (ArrayIndex >= NumEltsToInit)
15951 break;
15952 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) {
15953 StringLiteral *SL = EmbedS->getDataStringLiteral();
15954 for (unsigned I = EmbedS->getStartingElementPos(),
15955 N = EmbedS->getDataElementCount();
15956 I != EmbedS->getStartingElementPos() + N; ++I) {
15957 Value = SL->getCodeUnit(I);
15958 if (DestTy->isIntegerType()) {
15959 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(Value);
15960 } else {
15961 assert(DestTy->isFloatingType() && "unexpected type");
15962 const FPOptions FPO =
15963 Init->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15964 APFloat FValue(0.0);
15965 if (!HandleIntToFloatCast(Info, E: Init, FPO, SrcType: EmbedS->getType(), Value,
15966 DestType: DestTy, Result&: FValue))
15967 return false;
15968 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(FValue);
15969 }
15970 ArrayIndex++;
15971 }
15972 } else {
15973 if (!Eval(Init, ArrayIndex))
15974 return false;
15975 ++ArrayIndex;
15976 }
15977 }
15978
15979 if (!Result.hasArrayFiller())
15980 return Success;
15981
15982 // If we get here, we have a trivial filler, which we can just evaluate
15983 // once and splat over the rest of the array elements.
15984 assert(ArrayFiller && "no array filler for incomplete init list");
15985 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject,
15986 E: ArrayFiller) &&
15987 Success;
15988}
15989
15990bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
15991 LValue CommonLV;
15992 if (E->getCommonExpr() &&
15993 !Evaluate(Result&: Info.CurrentCall->createTemporary(
15994 Key: E->getCommonExpr(),
15995 T: getStorageType(Ctx: Info.Ctx, E: E->getCommonExpr()),
15996 Scope: ScopeKind::FullExpression, LV&: CommonLV),
15997 Info, E: E->getCommonExpr()->getSourceExpr()))
15998 return false;
15999
16000 auto *CAT = cast<ConstantArrayType>(Val: E->getType()->castAsArrayTypeUnsafe());
16001 if (!CheckArraySize(Info, CAT, CallLoc: E->getExprLoc()))
16002 return false;
16003
16004 uint64_t Elements = CAT->getZExtSize();
16005 Result = APValue(APValue::UninitArray(), Elements, Elements);
16006
16007 LValue Subobject = This;
16008 Subobject.addArray(Info, E, CAT);
16009
16010 bool Success = true;
16011 for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) {
16012 // C++ [class.temporary]/5
16013 // There are four contexts in which temporaries are destroyed at a different
16014 // point than the end of the full-expression. [...] The second context is
16015 // when a copy constructor is called to copy an element of an array while
16016 // the entire array is copied [...]. In either case, if the constructor has
16017 // one or more default arguments, the destruction of every temporary created
16018 // in a default argument is sequenced before the construction of the next
16019 // array element, if any.
16020 FullExpressionRAII Scope(Info);
16021
16022 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index),
16023 Info, This: Subobject, E: E->getSubExpr()) ||
16024 !HandleLValueArrayAdjustment(Info, E, LVal&: Subobject,
16025 EltTy: CAT->getElementType(), Adjustment: 1)) {
16026 if (!Info.noteFailure())
16027 return false;
16028 Success = false;
16029 }
16030
16031 // Make sure we run the destructors too.
16032 Scope.destroy();
16033 }
16034
16035 return Success;
16036}
16037
16038bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
16039 return VisitCXXConstructExpr(E, Subobject: This, Value: &Result, Type: E->getType());
16040}
16041
16042bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
16043 const LValue &Subobject,
16044 APValue *Value,
16045 QualType Type) {
16046 bool HadZeroInit = Value->hasValue();
16047
16048 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: Type)) {
16049 if (!CheckArraySize(Info, CAT, CallLoc: E->getExprLoc()))
16050 return false;
16051 unsigned FinalSize = CAT->getZExtSize();
16052
16053 // Preserve the array filler if we had prior zero-initialization.
16054 APValue Filler =
16055 HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller()
16056 : APValue();
16057
16058 *Value = APValue(APValue::UninitArray(), 0, FinalSize);
16059 if (FinalSize == 0)
16060 return true;
16061
16062 bool HasTrivialConstructor = CheckTrivialDefaultConstructor(
16063 Info, Loc: E->getExprLoc(), CD: E->getConstructor(),
16064 IsValueInitialization: E->requiresZeroInitialization());
16065 LValue ArrayElt = Subobject;
16066 ArrayElt.addArray(Info, E, CAT);
16067 // We do the whole initialization in two passes, first for just one element,
16068 // then for the whole array. It's possible we may find out we can't do const
16069 // init in the first pass, in which case we avoid allocating a potentially
16070 // large array. We don't do more passes because expanding array requires
16071 // copying the data, which is wasteful.
16072 for (const unsigned N : {1u, FinalSize}) {
16073 unsigned OldElts = Value->getArrayInitializedElts();
16074 if (OldElts == N)
16075 break;
16076
16077 // Expand the array to appropriate size.
16078 APValue NewValue(APValue::UninitArray(), N, FinalSize);
16079 for (unsigned I = 0; I < OldElts; ++I)
16080 NewValue.getArrayInitializedElt(I).swap(
16081 RHS&: Value->getArrayInitializedElt(I));
16082 Value->swap(RHS&: NewValue);
16083
16084 if (HadZeroInit)
16085 for (unsigned I = OldElts; I < N; ++I)
16086 Value->getArrayInitializedElt(I) = Filler;
16087
16088 if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) {
16089 // If we have a trivial constructor, only evaluate it once and copy
16090 // the result into all the array elements.
16091 APValue &FirstResult = Value->getArrayInitializedElt(I: 0);
16092 for (unsigned I = OldElts; I < FinalSize; ++I)
16093 Value->getArrayInitializedElt(I) = FirstResult;
16094 } else {
16095 for (unsigned I = OldElts; I < N; ++I) {
16096 if (!VisitCXXConstructExpr(E, Subobject: ArrayElt,
16097 Value: &Value->getArrayInitializedElt(I),
16098 Type: CAT->getElementType()) ||
16099 !HandleLValueArrayAdjustment(Info, E, LVal&: ArrayElt,
16100 EltTy: CAT->getElementType(), Adjustment: 1))
16101 return false;
16102 // When checking for const initilization any diagnostic is considered
16103 // an error.
16104 if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&
16105 !Info.keepEvaluatingAfterFailure())
16106 return false;
16107 }
16108 }
16109 }
16110
16111 return true;
16112 }
16113
16114 if (!Type->isRecordType())
16115 return Error(E);
16116
16117 return RecordExprEvaluator(Info, Subobject, *Value)
16118 .VisitCXXConstructExpr(E, T: Type);
16119}
16120
16121bool ArrayExprEvaluator::VisitCXXParenListInitExpr(
16122 const CXXParenListInitExpr *E) {
16123 assert(E->getType()->isConstantArrayType() &&
16124 "Expression result is not a constant array type");
16125
16126 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs(),
16127 ArrayFiller: E->getArrayFiller());
16128}
16129
16130bool ArrayExprEvaluator::VisitDesignatedInitUpdateExpr(
16131 const DesignatedInitUpdateExpr *E) {
16132 if (!Visit(S: E->getBase()))
16133 return false;
16134 return Visit(S: E->getUpdater());
16135}
16136
16137//===----------------------------------------------------------------------===//
16138// Integer Evaluation
16139//
16140// As a GNU extension, we support casting pointers to sufficiently-wide integer
16141// types and back in constant folding. Integer values are thus represented
16142// either as an integer-valued APValue, or as an lvalue-valued APValue.
16143//===----------------------------------------------------------------------===//
16144
16145namespace {
16146class IntExprEvaluator
16147 : public ExprEvaluatorBase<IntExprEvaluator> {
16148 APValue &Result;
16149public:
16150 IntExprEvaluator(EvalInfo &info, APValue &result)
16151 : ExprEvaluatorBaseTy(info), Result(result) {}
16152
16153 bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) {
16154 assert(E->getType()->isIntegralOrEnumerationType() &&
16155 "Invalid evaluation result.");
16156 assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
16157 "Invalid evaluation result.");
16158 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16159 "Invalid evaluation result.");
16160 Result = APValue(SI);
16161 return true;
16162 }
16163 bool Success(const llvm::APSInt &SI, const Expr *E) {
16164 return Success(SI, E, Result);
16165 }
16166
16167 bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) {
16168 assert(E->getType()->isIntegralOrEnumerationType() &&
16169 "Invalid evaluation result.");
16170 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16171 "Invalid evaluation result.");
16172 Result = APValue(APSInt(I));
16173 Result.getInt().setIsUnsigned(
16174 E->getType()->isUnsignedIntegerOrEnumerationType());
16175 return true;
16176 }
16177 bool Success(const llvm::APInt &I, const Expr *E) {
16178 return Success(I, E, Result);
16179 }
16180
16181 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
16182 assert(E->getType()->isIntegralOrEnumerationType() &&
16183 "Invalid evaluation result.");
16184 Result = APValue(Info.Ctx.MakeIntValue(Value, Type: E->getType()));
16185 return true;
16186 }
16187 bool Success(uint64_t Value, const Expr *E) {
16188 return Success(Value, E, Result);
16189 }
16190
16191 bool Success(CharUnits Size, const Expr *E) {
16192 return Success(Value: Size.getQuantity(), E);
16193 }
16194
16195 bool Success(const APValue &V, const Expr *E) {
16196 // C++23 [expr.const]p8 If we have a variable that is unknown reference or
16197 // pointer allow further evaluation of the value.
16198 if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate() ||
16199 V.allowConstexprUnknown()) {
16200 Result = V;
16201 return true;
16202 }
16203 return Success(SI: V.getInt(), E);
16204 }
16205
16206 bool ZeroInitialization(const Expr *E) { return Success(Value: 0, E); }
16207
16208 friend std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &,
16209 const CallExpr *);
16210
16211 //===--------------------------------------------------------------------===//
16212 // Visitor Methods
16213 //===--------------------------------------------------------------------===//
16214
16215 bool VisitIntegerLiteral(const IntegerLiteral *E) {
16216 return Success(I: E->getValue(), E);
16217 }
16218 bool VisitCharacterLiteral(const CharacterLiteral *E) {
16219 return Success(Value: E->getValue(), E);
16220 }
16221
16222 bool CheckReferencedDecl(const Expr *E, const Decl *D);
16223 bool VisitDeclRefExpr(const DeclRefExpr *E) {
16224 if (CheckReferencedDecl(E, D: E->getDecl()))
16225 return true;
16226
16227 return ExprEvaluatorBaseTy::VisitDeclRefExpr(S: E);
16228 }
16229 bool VisitMemberExpr(const MemberExpr *E) {
16230 if (CheckReferencedDecl(E, D: E->getMemberDecl())) {
16231 VisitIgnoredBaseExpression(E: E->getBase());
16232 return true;
16233 }
16234
16235 return ExprEvaluatorBaseTy::VisitMemberExpr(E);
16236 }
16237
16238 bool VisitCallExpr(const CallExpr *E);
16239 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
16240 bool VisitBinaryOperator(const BinaryOperator *E);
16241 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
16242 bool VisitUnaryOperator(const UnaryOperator *E);
16243
16244 bool VisitCastExpr(const CastExpr* E);
16245 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
16246
16247 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
16248 return Success(Value: E->getValue(), E);
16249 }
16250
16251 bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
16252 return Success(Value: E->getValue(), E);
16253 }
16254
16255 bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
16256 if (Info.ArrayInitIndex == uint64_t(-1)) {
16257 // We were asked to evaluate this subexpression independent of the
16258 // enclosing ArrayInitLoopExpr. We can't do that.
16259 Info.FFDiag(E);
16260 return false;
16261 }
16262 return Success(Value: Info.ArrayInitIndex, E);
16263 }
16264
16265 // Note, GNU defines __null as an integer, not a pointer.
16266 bool VisitGNUNullExpr(const GNUNullExpr *E) {
16267 return ZeroInitialization(E);
16268 }
16269
16270 bool VisitTypeTraitExpr(const TypeTraitExpr *E) {
16271 if (E->isStoredAsBoolean())
16272 return Success(Value: E->getBoolValue(), E);
16273 if (E->getAPValue().isAbsent())
16274 return false;
16275 assert(E->getAPValue().isInt() && "APValue type not supported");
16276 return Success(SI: E->getAPValue().getInt(), E);
16277 }
16278
16279 bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
16280 return Success(Value: E->getValue(), E);
16281 }
16282
16283 bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
16284 return Success(Value: E->getValue(), E);
16285 }
16286
16287 bool VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *E) {
16288 // This should not be evaluated during constant expr evaluation, as it
16289 // should always be in an unevaluated context (the args list of a 'gang' or
16290 // 'tile' clause).
16291 return Error(E);
16292 }
16293
16294 bool VisitUnaryReal(const UnaryOperator *E);
16295 bool VisitUnaryImag(const UnaryOperator *E);
16296
16297 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
16298 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
16299 bool VisitSourceLocExpr(const SourceLocExpr *E);
16300 bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);
16301 bool VisitRequiresExpr(const RequiresExpr *E);
16302 // FIXME: Missing: array subscript of vector, member of vector
16303};
16304
16305class FixedPointExprEvaluator
16306 : public ExprEvaluatorBase<FixedPointExprEvaluator> {
16307 APValue &Result;
16308
16309 public:
16310 FixedPointExprEvaluator(EvalInfo &info, APValue &result)
16311 : ExprEvaluatorBaseTy(info), Result(result) {}
16312
16313 bool Success(const llvm::APInt &I, const Expr *E) {
16314 return Success(
16315 V: APFixedPoint(I, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
16316 }
16317
16318 bool Success(uint64_t Value, const Expr *E) {
16319 return Success(
16320 V: APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
16321 }
16322
16323 bool Success(const APValue &V, const Expr *E) {
16324 return Success(V: V.getFixedPoint(), E);
16325 }
16326
16327 bool Success(const APFixedPoint &V, const Expr *E) {
16328 assert(E->getType()->isFixedPointType() && "Invalid evaluation result.");
16329 assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16330 "Invalid evaluation result.");
16331 Result = APValue(V);
16332 return true;
16333 }
16334
16335 bool ZeroInitialization(const Expr *E) {
16336 return Success(Value: 0, E);
16337 }
16338
16339 //===--------------------------------------------------------------------===//
16340 // Visitor Methods
16341 //===--------------------------------------------------------------------===//
16342
16343 bool VisitFixedPointLiteral(const FixedPointLiteral *E) {
16344 return Success(I: E->getValue(), E);
16345 }
16346
16347 bool VisitCastExpr(const CastExpr *E);
16348 bool VisitUnaryOperator(const UnaryOperator *E);
16349 bool VisitBinaryOperator(const BinaryOperator *E);
16350};
16351} // end anonymous namespace
16352
16353/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
16354/// produce either the integer value or a pointer.
16355///
16356/// GCC has a heinous extension which folds casts between pointer types and
16357/// pointer-sized integral types. We support this by allowing the evaluation of
16358/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
16359/// Some simple arithmetic on such values is supported (they are treated much
16360/// like char*).
16361static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
16362 EvalInfo &Info) {
16363 assert(!E->isValueDependent());
16364 assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType());
16365 return IntExprEvaluator(Info, Result).Visit(S: E);
16366}
16367
16368static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
16369 assert(!E->isValueDependent());
16370 APValue Val;
16371 if (!EvaluateIntegerOrLValue(E, Result&: Val, Info))
16372 return false;
16373 if (!Val.isInt()) {
16374 // FIXME: It would be better to produce the diagnostic for casting
16375 // a pointer to an integer.
16376 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
16377 return false;
16378 }
16379 Result = Val.getInt();
16380 return true;
16381}
16382
16383bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) {
16384 APValue Evaluated = E->EvaluateInContext(
16385 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
16386 return Success(V: Evaluated, E);
16387}
16388
16389static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
16390 EvalInfo &Info) {
16391 assert(!E->isValueDependent());
16392 if (E->getType()->isFixedPointType()) {
16393 APValue Val;
16394 if (!FixedPointExprEvaluator(Info, Val).Visit(S: E))
16395 return false;
16396 if (!Val.isFixedPoint())
16397 return false;
16398
16399 Result = Val.getFixedPoint();
16400 return true;
16401 }
16402 return false;
16403}
16404
16405static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
16406 EvalInfo &Info) {
16407 assert(!E->isValueDependent());
16408 if (E->getType()->isIntegerType()) {
16409 auto FXSema = Info.Ctx.getFixedPointSemantics(Ty: E->getType());
16410 APSInt Val;
16411 if (!EvaluateInteger(E, Result&: Val, Info))
16412 return false;
16413 Result = APFixedPoint(Val, FXSema);
16414 return true;
16415 } else if (E->getType()->isFixedPointType()) {
16416 return EvaluateFixedPoint(E, Result, Info);
16417 }
16418 return false;
16419}
16420
16421/// Check whether the given declaration can be directly converted to an integral
16422/// rvalue. If not, no diagnostic is produced; there are other things we can
16423/// try.
16424bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
16425 // Enums are integer constant exprs.
16426 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(Val: D)) {
16427 // Check for signedness/width mismatches between E type and ECD value.
16428 bool SameSign = (ECD->getInitVal().isSigned()
16429 == E->getType()->isSignedIntegerOrEnumerationType());
16430 bool SameWidth = (ECD->getInitVal().getBitWidth()
16431 == Info.Ctx.getIntWidth(T: E->getType()));
16432 if (SameSign && SameWidth)
16433 return Success(SI: ECD->getInitVal(), E);
16434 else {
16435 // Get rid of mismatch (otherwise Success assertions will fail)
16436 // by computing a new value matching the type of E.
16437 llvm::APSInt Val = ECD->getInitVal();
16438 if (!SameSign)
16439 Val.setIsSigned(!ECD->getInitVal().isSigned());
16440 if (!SameWidth)
16441 Val = Val.extOrTrunc(width: Info.Ctx.getIntWidth(T: E->getType()));
16442 return Success(SI: Val, E);
16443 }
16444 }
16445 return false;
16446}
16447
16448/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16449/// as GCC.
16450GCCTypeClass EvaluateBuiltinClassifyType(QualType T,
16451 const LangOptions &LangOpts) {
16452 assert(!T->isDependentType() && "unexpected dependent type");
16453
16454 QualType CanTy = T.getCanonicalType();
16455
16456 switch (CanTy->getTypeClass()) {
16457#define TYPE(ID, BASE)
16458#define DEPENDENT_TYPE(ID, BASE) case Type::ID:
16459#define NON_CANONICAL_TYPE(ID, BASE) case Type::ID:
16460#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID:
16461#include "clang/AST/TypeNodes.inc"
16462 case Type::Auto:
16463 case Type::DeducedTemplateSpecialization:
16464 llvm_unreachable("unexpected non-canonical or dependent type");
16465
16466 case Type::Builtin:
16467 switch (cast<BuiltinType>(Val&: CanTy)->getKind()) {
16468#define BUILTIN_TYPE(ID, SINGLETON_ID)
16469#define SIGNED_TYPE(ID, SINGLETON_ID) \
16470 case BuiltinType::ID: return GCCTypeClass::Integer;
16471#define FLOATING_TYPE(ID, SINGLETON_ID) \
16472 case BuiltinType::ID: return GCCTypeClass::RealFloat;
16473#define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \
16474 case BuiltinType::ID: break;
16475#include "clang/AST/BuiltinTypes.def"
16476 case BuiltinType::Void:
16477 return GCCTypeClass::Void;
16478
16479 case BuiltinType::Bool:
16480 return GCCTypeClass::Bool;
16481
16482 case BuiltinType::Char_U:
16483 case BuiltinType::UChar:
16484 case BuiltinType::WChar_U:
16485 case BuiltinType::Char8:
16486 case BuiltinType::Char16:
16487 case BuiltinType::Char32:
16488 case BuiltinType::UShort:
16489 case BuiltinType::UInt:
16490 case BuiltinType::ULong:
16491 case BuiltinType::ULongLong:
16492 case BuiltinType::UInt128:
16493 return GCCTypeClass::Integer;
16494
16495 case BuiltinType::UShortAccum:
16496 case BuiltinType::UAccum:
16497 case BuiltinType::ULongAccum:
16498 case BuiltinType::UShortFract:
16499 case BuiltinType::UFract:
16500 case BuiltinType::ULongFract:
16501 case BuiltinType::SatUShortAccum:
16502 case BuiltinType::SatUAccum:
16503 case BuiltinType::SatULongAccum:
16504 case BuiltinType::SatUShortFract:
16505 case BuiltinType::SatUFract:
16506 case BuiltinType::SatULongFract:
16507 return GCCTypeClass::None;
16508
16509 case BuiltinType::NullPtr:
16510
16511 case BuiltinType::ObjCId:
16512 case BuiltinType::ObjCClass:
16513 case BuiltinType::ObjCSel:
16514#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
16515 case BuiltinType::Id:
16516#include "clang/Basic/OpenCLImageTypes.def"
16517#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
16518 case BuiltinType::Id:
16519#include "clang/Basic/OpenCLExtensionTypes.def"
16520 case BuiltinType::OCLSampler:
16521 case BuiltinType::OCLEvent:
16522 case BuiltinType::OCLClkEvent:
16523 case BuiltinType::OCLQueue:
16524 case BuiltinType::OCLReserveID:
16525#define SVE_TYPE(Name, Id, SingletonId) \
16526 case BuiltinType::Id:
16527#include "clang/Basic/AArch64ACLETypes.def"
16528#define PPC_VECTOR_TYPE(Name, Id, Size) \
16529 case BuiltinType::Id:
16530#include "clang/Basic/PPCTypes.def"
16531#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16532#include "clang/Basic/RISCVVTypes.def"
16533#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16534#include "clang/Basic/WebAssemblyReferenceTypes.def"
16535#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
16536#include "clang/Basic/AMDGPUTypes.def"
16537#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16538#include "clang/Basic/HLSLIntangibleTypes.def"
16539#define HLSL_PACKED_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16540#include "clang/Basic/HLSLPackedTypes.def"
16541#define SPIRV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16542#include "clang/Basic/SPIRVTypes.def"
16543 case BuiltinType::MetaInfo:
16544 return GCCTypeClass::None;
16545
16546 case BuiltinType::Dependent:
16547 llvm_unreachable("unexpected dependent type");
16548 };
16549 llvm_unreachable("unexpected placeholder type");
16550
16551 case Type::Enum:
16552 return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer;
16553
16554 case Type::Pointer:
16555 case Type::ConstantArray:
16556 case Type::VariableArray:
16557 case Type::IncompleteArray:
16558 case Type::FunctionNoProto:
16559 case Type::FunctionProto:
16560 case Type::ArrayParameter:
16561 return GCCTypeClass::Pointer;
16562
16563 case Type::MemberPointer:
16564 return CanTy->isMemberDataPointerType()
16565 ? GCCTypeClass::PointerToDataMember
16566 : GCCTypeClass::PointerToMemberFunction;
16567
16568 case Type::Complex:
16569 return GCCTypeClass::Complex;
16570
16571 case Type::Record:
16572 return CanTy->isUnionType() ? GCCTypeClass::Union
16573 : GCCTypeClass::ClassOrStruct;
16574
16575 case Type::Atomic:
16576 // GCC classifies _Atomic T the same as T.
16577 return EvaluateBuiltinClassifyType(
16578 T: CanTy->castAs<AtomicType>()->getValueType(), LangOpts);
16579
16580 case Type::Vector:
16581 case Type::ExtVector:
16582 return GCCTypeClass::Vector;
16583
16584 case Type::BlockPointer:
16585 case Type::ConstantMatrix:
16586 case Type::ObjCObject:
16587 case Type::ObjCInterface:
16588 case Type::ObjCObjectPointer:
16589 case Type::Pipe:
16590 case Type::HLSLAttributedResource:
16591 case Type::HLSLInlineSpirv:
16592 case Type::OverflowBehavior:
16593 // Classify all other types that don't fit into the regular
16594 // classification the same way.
16595 return GCCTypeClass::None;
16596
16597 case Type::BitInt:
16598 return GCCTypeClass::BitInt;
16599
16600 case Type::LValueReference:
16601 case Type::RValueReference:
16602 llvm_unreachable("invalid type for expression");
16603 }
16604
16605 llvm_unreachable("unexpected type class");
16606}
16607
16608/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16609/// as GCC.
16610static GCCTypeClass
16611EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) {
16612 // If no argument was supplied, default to None. This isn't
16613 // ideal, however it is what gcc does.
16614 if (E->getNumArgs() == 0)
16615 return GCCTypeClass::None;
16616
16617 // FIXME: Bizarrely, GCC treats a call with more than one argument as not
16618 // being an ICE, but still folds it to a constant using the type of the first
16619 // argument.
16620 return EvaluateBuiltinClassifyType(T: E->getArg(Arg: 0)->getType(), LangOpts);
16621}
16622
16623/// EvaluateBuiltinConstantPForLValue - Determine the result of
16624/// __builtin_constant_p when applied to the given pointer.
16625///
16626/// A pointer is only "constant" if it is null (or a pointer cast to integer)
16627/// or it points to the first character of a string literal.
16628static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) {
16629 APValue::LValueBase Base = LV.getLValueBase();
16630 if (Base.isNull()) {
16631 // A null base is acceptable.
16632 return true;
16633 } else if (const Expr *E = Base.dyn_cast<const Expr *>()) {
16634 if (!isa<StringLiteral>(Val: E))
16635 return false;
16636 return LV.getLValueOffset().isZero();
16637 } else if (Base.is<TypeInfoLValue>()) {
16638 // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to
16639 // evaluate to true.
16640 return true;
16641 } else {
16642 // Any other base is not constant enough for GCC.
16643 return false;
16644 }
16645}
16646
16647/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
16648/// GCC as we can manage.
16649static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) {
16650 // This evaluation is not permitted to have side-effects, so evaluate it in
16651 // a speculative evaluation context.
16652 SpeculativeEvaluationRAII SpeculativeEval(Info);
16653
16654 // Constant-folding is always enabled for the operand of __builtin_constant_p
16655 // (even when the enclosing evaluation context otherwise requires a strict
16656 // language-specific constant expression).
16657 FoldConstant Fold(Info, true);
16658
16659 QualType ArgType = Arg->getType();
16660
16661 // __builtin_constant_p always has one operand. The rules which gcc follows
16662 // are not precisely documented, but are as follows:
16663 //
16664 // - If the operand is of integral, floating, complex or enumeration type,
16665 // and can be folded to a known value of that type, it returns 1.
16666 // - If the operand can be folded to a pointer to the first character
16667 // of a string literal (or such a pointer cast to an integral type)
16668 // or to a null pointer or an integer cast to a pointer, it returns 1.
16669 //
16670 // Otherwise, it returns 0.
16671 //
16672 // FIXME: GCC also intends to return 1 for literals of aggregate types, but
16673 // its support for this did not work prior to GCC 9 and is not yet well
16674 // understood.
16675 if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() ||
16676 ArgType->isAnyComplexType() || ArgType->isPointerType() ||
16677 ArgType->isNullPtrType()) {
16678 APValue V;
16679 if (!::EvaluateAsRValue(Info, E: Arg, Result&: V) || Info.EvalStatus.HasSideEffects) {
16680 Fold.keepDiagnostics();
16681 return false;
16682 }
16683
16684 // For a pointer (possibly cast to integer), there are special rules.
16685 if (V.getKind() == APValue::LValue)
16686 return EvaluateBuiltinConstantPForLValue(LV: V);
16687
16688 // Otherwise, any constant value is good enough.
16689 return V.hasValue();
16690 }
16691
16692 // Anything else isn't considered to be sufficiently constant.
16693 return false;
16694}
16695
16696/// Retrieves the "underlying object type" of the given expression,
16697/// as used by __builtin_object_size.
16698static QualType getObjectType(APValue::LValueBase B) {
16699 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
16700 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
16701 return VD->getType();
16702 } else if (const Expr *E = B.dyn_cast<const Expr*>()) {
16703 if (isa<CompoundLiteralExpr>(Val: E))
16704 return E->getType();
16705 } else if (B.is<TypeInfoLValue>()) {
16706 return B.getTypeInfoType();
16707 } else if (B.is<DynamicAllocLValue>()) {
16708 return B.getDynamicAllocType();
16709 }
16710
16711 return QualType();
16712}
16713
16714/// A more selective version of E->IgnoreParenCasts for
16715/// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only
16716/// to change the type of E.
16717/// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo`
16718///
16719/// Always returns an RValue with a pointer representation.
16720const Expr *ignorePointerCastsAndParens(const Expr *E) {
16721 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
16722
16723 const Expr *NoParens = E->IgnoreParens();
16724 const auto *Cast = dyn_cast<CastExpr>(Val: NoParens);
16725 if (Cast == nullptr)
16726 return NoParens;
16727
16728 // We only conservatively allow a few kinds of casts, because this code is
16729 // inherently a simple solution that seeks to support the common case.
16730 auto CastKind = Cast->getCastKind();
16731 if (CastKind != CK_NoOp && CastKind != CK_BitCast &&
16732 CastKind != CK_AddressSpaceConversion)
16733 return NoParens;
16734
16735 const auto *SubExpr = Cast->getSubExpr();
16736 if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue())
16737 return NoParens;
16738 return ignorePointerCastsAndParens(E: SubExpr);
16739}
16740
16741/// Checks to see if the given LValue's Designator is at the end of the LValue's
16742/// record layout. e.g.
16743/// struct { struct { int a, b; } fst, snd; } obj;
16744/// obj.fst // no
16745/// obj.snd // yes
16746/// obj.fst.a // no
16747/// obj.fst.b // no
16748/// obj.snd.a // no
16749/// obj.snd.b // yes
16750///
16751/// Please note: this function is specialized for how __builtin_object_size
16752/// views "objects".
16753///
16754/// If this encounters an invalid RecordDecl or otherwise cannot determine the
16755/// correct result, it will always return true.
16756static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) {
16757 assert(!LVal.Designator.Invalid);
16758
16759 auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD) {
16760 const RecordDecl *Parent = FD->getParent();
16761 if (Parent->isInvalidDecl() || Parent->isUnion())
16762 return true;
16763 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(D: Parent);
16764 return FD->getFieldIndex() + 1 == Layout.getFieldCount();
16765 };
16766
16767 auto &Base = LVal.getLValueBase();
16768 if (auto *ME = dyn_cast_or_null<MemberExpr>(Val: Base.dyn_cast<const Expr *>())) {
16769 if (auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl())) {
16770 if (!IsLastOrInvalidFieldDecl(FD))
16771 return false;
16772 } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(Val: ME->getMemberDecl())) {
16773 for (auto *FD : IFD->chain()) {
16774 if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(Val: FD)))
16775 return false;
16776 }
16777 }
16778 }
16779
16780 unsigned I = 0;
16781 QualType BaseType = getType(B: Base);
16782 if (LVal.Designator.FirstEntryIsAnUnsizedArray) {
16783 // If we don't know the array bound, conservatively assume we're looking at
16784 // the final array element.
16785 ++I;
16786 if (BaseType->isIncompleteArrayType())
16787 BaseType = Ctx.getAsArrayType(T: BaseType)->getElementType();
16788 else
16789 BaseType = BaseType->castAs<PointerType>()->getPointeeType();
16790 }
16791
16792 for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) {
16793 const auto &Entry = LVal.Designator.Entries[I];
16794 if (BaseType->isArrayType()) {
16795 // Because __builtin_object_size treats arrays as objects, we can ignore
16796 // the index iff this is the last array in the Designator.
16797 if (I + 1 == E)
16798 return true;
16799 const auto *CAT = cast<ConstantArrayType>(Val: Ctx.getAsArrayType(T: BaseType));
16800 uint64_t Index = Entry.getAsArrayIndex();
16801 if (Index + 1 != CAT->getZExtSize())
16802 return false;
16803 BaseType = CAT->getElementType();
16804 } else if (BaseType->isAnyComplexType()) {
16805 const auto *CT = BaseType->castAs<ComplexType>();
16806 uint64_t Index = Entry.getAsArrayIndex();
16807 if (Index != 1)
16808 return false;
16809 BaseType = CT->getElementType();
16810 } else if (auto *FD = getAsField(E: Entry)) {
16811 if (!IsLastOrInvalidFieldDecl(FD))
16812 return false;
16813 BaseType = FD->getType();
16814 } else {
16815 assert(getAsBaseClass(Entry) && "Expecting cast to a base class");
16816 return false;
16817 }
16818 }
16819 return true;
16820}
16821
16822/// Tests to see if the LValue has a user-specified designator (that isn't
16823/// necessarily valid). Note that this always returns 'true' if the LValue has
16824/// an unsized array as its first designator entry, because there's currently no
16825/// way to tell if the user typed *foo or foo[0].
16826static bool refersToCompleteObject(const LValue &LVal) {
16827 if (LVal.Designator.Invalid)
16828 return false;
16829
16830 if (!LVal.Designator.Entries.empty())
16831 return LVal.Designator.isMostDerivedAnUnsizedArray();
16832
16833 if (!LVal.InvalidBase)
16834 return true;
16835
16836 // If `E` is a MemberExpr, then the first part of the designator is hiding in
16837 // the LValueBase.
16838 const auto *E = LVal.Base.dyn_cast<const Expr *>();
16839 return !E || !isa<MemberExpr>(Val: E);
16840}
16841
16842/// Attempts to detect a user writing into a piece of memory that's impossible
16843/// to figure out the size of by just using types.
16844static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) {
16845 const SubobjectDesignator &Designator = LVal.Designator;
16846 // Notes:
16847 // - Users can only write off of the end when we have an invalid base. Invalid
16848 // bases imply we don't know where the memory came from.
16849 // - We used to be a bit more aggressive here; we'd only be conservative if
16850 // the array at the end was flexible, or if it had 0 or 1 elements. This
16851 // broke some common standard library extensions (PR30346), but was
16852 // otherwise seemingly fine. It may be useful to reintroduce this behavior
16853 // with some sort of list. OTOH, it seems that GCC is always
16854 // conservative with the last element in structs (if it's an array), so our
16855 // current behavior is more compatible than an explicit list approach would
16856 // be.
16857 auto isFlexibleArrayMember = [&] {
16858 using FAMKind = LangOptions::StrictFlexArraysLevelKind;
16859 FAMKind StrictFlexArraysLevel =
16860 Ctx.getLangOpts().getStrictFlexArraysLevel();
16861
16862 if (Designator.isMostDerivedAnUnsizedArray())
16863 return true;
16864
16865 if (StrictFlexArraysLevel == FAMKind::Default)
16866 return true;
16867
16868 if (Designator.getMostDerivedArraySize() == 0 &&
16869 StrictFlexArraysLevel != FAMKind::IncompleteOnly)
16870 return true;
16871
16872 if (Designator.getMostDerivedArraySize() == 1 &&
16873 StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete)
16874 return true;
16875
16876 return false;
16877 };
16878
16879 return LVal.InvalidBase &&
16880 Designator.Entries.size() == Designator.MostDerivedPathLength &&
16881 Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() &&
16882 isDesignatorAtObjectEnd(Ctx, LVal);
16883}
16884
16885/// Converts the given APInt to CharUnits, assuming the APInt is unsigned.
16886/// Fails if the conversion would cause loss of precision.
16887static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int,
16888 CharUnits &Result) {
16889 auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max();
16890 if (Int.ugt(RHS: CharUnitsMax))
16891 return false;
16892 Result = CharUnits::fromQuantity(Quantity: Int.getZExtValue());
16893 return true;
16894}
16895
16896/// If we're evaluating the object size of an instance of a struct that
16897/// contains a flexible array member, add the size of the initializer.
16898static void addFlexibleArrayMemberInitSize(EvalInfo &Info, const QualType &T,
16899 const LValue &LV, CharUnits &Size) {
16900 if (!T.isNull() && T->isStructureType() &&
16901 T->castAsRecordDecl()->hasFlexibleArrayMember())
16902 if (const auto *V = LV.getLValueBase().dyn_cast<const ValueDecl *>())
16903 if (const auto *VD = dyn_cast<VarDecl>(Val: V))
16904 if (VD->hasInit())
16905 Size += VD->getFlexibleArrayInitChars(Ctx: Info.Ctx);
16906}
16907
16908/// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will
16909/// determine how many bytes exist from the beginning of the object to either
16910/// the end of the current subobject, or the end of the object itself, depending
16911/// on what the LValue looks like + the value of Type.
16912///
16913/// If this returns false, the value of Result is undefined.
16914static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc,
16915 unsigned Type, const LValue &LVal,
16916 CharUnits &EndOffset) {
16917 bool DetermineForCompleteObject = refersToCompleteObject(LVal);
16918
16919 auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) {
16920 if (Ty.isNull())
16921 return false;
16922
16923 Ty = Ty.getNonReferenceType();
16924
16925 if (Ty->isIncompleteType() || Ty->isFunctionType())
16926 return false;
16927
16928 return HandleSizeof(Info, Loc: ExprLoc, Type: Ty, Size&: Result);
16929 };
16930
16931 // We want to evaluate the size of the entire object. This is a valid fallback
16932 // for when Type=1 and the designator is invalid, because we're asked for an
16933 // upper-bound.
16934 if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) {
16935 // Type=3 wants a lower bound, so we can't fall back to this.
16936 if (Type == 3 && !DetermineForCompleteObject)
16937 return false;
16938
16939 llvm::APInt APEndOffset;
16940 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
16941 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
16942 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
16943
16944 if (LVal.InvalidBase)
16945 return false;
16946
16947 QualType BaseTy = getObjectType(B: LVal.getLValueBase());
16948 const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset);
16949 addFlexibleArrayMemberInitSize(Info, T: BaseTy, LV: LVal, Size&: EndOffset);
16950 return Ret;
16951 }
16952
16953 // We want to evaluate the size of a subobject.
16954 const SubobjectDesignator &Designator = LVal.Designator;
16955
16956 // The following is a moderately common idiom in C:
16957 //
16958 // struct Foo { int a; char c[1]; };
16959 // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar));
16960 // strcpy(&F->c[0], Bar);
16961 //
16962 // In order to not break too much legacy code, we need to support it.
16963 if (isUserWritingOffTheEnd(Ctx: Info.Ctx, LVal)) {
16964 // If we can resolve this to an alloc_size call, we can hand that back,
16965 // because we know for certain how many bytes there are to write to.
16966 llvm::APInt APEndOffset;
16967 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
16968 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
16969 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
16970
16971 // If we cannot determine the size of the initial allocation, then we can't
16972 // given an accurate upper-bound. However, we are still able to give
16973 // conservative lower-bounds for Type=3.
16974 if (Type == 1)
16975 return false;
16976 }
16977
16978 CharUnits BytesPerElem;
16979 if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem))
16980 return false;
16981
16982 // According to the GCC documentation, we want the size of the subobject
16983 // denoted by the pointer. But that's not quite right -- what we actually
16984 // want is the size of the immediately-enclosing array, if there is one.
16985 int64_t ElemsRemaining;
16986 if (Designator.MostDerivedIsArrayElement &&
16987 Designator.Entries.size() == Designator.MostDerivedPathLength) {
16988 uint64_t ArraySize = Designator.getMostDerivedArraySize();
16989 uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex();
16990 ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex;
16991 } else {
16992 ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1;
16993 }
16994
16995 EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining;
16996 return true;
16997}
16998
16999/// Tries to evaluate the __builtin_object_size for @p E.
17000///
17001/// If @p IsDynamic is true (i.e. we're evaluating
17002/// __builtin_dynamic_object_size) and the operand designates a flexible array
17003/// member annotated with 'counted_by', we refuse to fold so that IR generation
17004/// can emit the count-based runtime size computation.
17005static std::optional<uint64_t>
17006tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, EvalInfo &Info,
17007 bool IsDynamic = false) {
17008
17009 // Determine the denoted object.
17010 LValue LVal;
17011 {
17012 // The operand of __builtin_object_size is never evaluated for side-effects.
17013 // If there are any, but we can determine the pointed-to object anyway, then
17014 // ignore the side-effects.
17015 SpeculativeEvaluationRAII SpeculativeEval(Info);
17016 IgnoreSideEffectsRAII Fold(Info);
17017
17018 if (E->isGLValue()) {
17019 // It's possible for us to be given GLValues if we're called via
17020 // Expr::tryEvaluateObjectSize.
17021 APValue RVal;
17022 if (!EvaluateAsRValue(Info, E, Result&: RVal))
17023 return std::nullopt;
17024 LVal.setFrom(Ctx: Info.Ctx, V: RVal);
17025 } else if (!EvaluatePointer(E: ignorePointerCastsAndParens(E), Result&: LVal, Info,
17026 /*InvalidBaseOK=*/true))
17027 return std::nullopt;
17028 }
17029
17030 // If we point to before the start of the object, there are no accessible
17031 // bytes.
17032 if (LVal.getLValueOffset().isNegative())
17033 return 0;
17034
17035 // For __builtin_dynamic_object_size on a counted_by-annotated flexible
17036 // array member, defer to IR generation (emitCountedBySize in CGBuiltin):
17037 // its runtime computation uses the live 'count' field and is more accurate
17038 // than the layout/initializer-derived size we'd produce here. Use the same
17039 // findStructFieldAccess form-recognition CGBuiltin does, so we refuse to
17040 // fold on exactly the shapes that path handles (and, importantly, *not*
17041 // on '&af.fam' which designates the array-as-a-whole and stays on the
17042 // layout-derived path to match GCC). Checked after the negative-offset
17043 // early return above so that obviously out-of-bounds operands still fold
17044 // to 0, preserving existing behavior.
17045 if (IsDynamic) {
17046 const auto *ME = dyn_cast_or_null<MemberExpr>(Val: findStructFieldAccess(E));
17047 const auto *FD = ME ? dyn_cast<FieldDecl>(Val: ME->getMemberDecl()) : nullptr;
17048 if (FD && FD->getType()->isCountAttributedType())
17049 return std::nullopt;
17050 }
17051
17052 CharUnits EndOffset;
17053 if (!determineEndOffset(Info, ExprLoc: E->getExprLoc(), Type, LVal, EndOffset))
17054 return std::nullopt;
17055
17056 // If we've fallen outside of the end offset, just pretend there's nothing to
17057 // write to/read from.
17058 if (EndOffset <= LVal.getLValueOffset())
17059 return 0;
17060 return (EndOffset - LVal.getLValueOffset()).getQuantity();
17061}
17062
17063bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
17064 if (!IsConstantEvaluatedBuiltinCall(E))
17065 return ExprEvaluatorBaseTy::VisitCallExpr(E);
17066 return VisitBuiltinCallExpr(E, BuiltinOp: ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E));
17067}
17068
17069static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info,
17070 APValue &Val, APSInt &Alignment) {
17071 QualType SrcTy = E->getArg(Arg: 0)->getType();
17072 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: SrcTy, Info, Alignment))
17073 return false;
17074 // Even though we are evaluating integer expressions we could get a pointer
17075 // argument for the __builtin_is_aligned() case.
17076 if (SrcTy->isPointerType()) {
17077 LValue Ptr;
17078 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Ptr, Info))
17079 return false;
17080 Ptr.moveInto(V&: Val);
17081 } else if (!SrcTy->isIntegralOrEnumerationType()) {
17082 Info.FFDiag(E: E->getArg(Arg: 0));
17083 return false;
17084 } else {
17085 APSInt SrcInt;
17086 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SrcInt, Info))
17087 return false;
17088 assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() &&
17089 "Bit widths must be the same");
17090 Val = APValue(SrcInt);
17091 }
17092 assert(Val.hasValue());
17093 return true;
17094}
17095
17096bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
17097 unsigned BuiltinOp) {
17098 auto EvalTestOp = [&](llvm::function_ref<bool(const APInt &, const APInt &)>
17099 Fn) {
17100 APValue SourceLHS, SourceRHS;
17101 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
17102 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
17103 return false;
17104
17105 unsigned SourceLen = SourceLHS.getVectorLength();
17106 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
17107 QualType ElemQT = VT->getElementType();
17108 unsigned LaneWidth = Info.Ctx.getTypeSize(T: ElemQT);
17109
17110 APInt AWide(LaneWidth * SourceLen, 0);
17111 APInt BWide(LaneWidth * SourceLen, 0);
17112
17113 for (unsigned I = 0; I != SourceLen; ++I) {
17114 APInt ALane;
17115 APInt BLane;
17116 if (ElemQT->isIntegerType()) { // Get value.
17117 ALane = SourceLHS.getVectorElt(I).getInt();
17118 BLane = SourceRHS.getVectorElt(I).getInt();
17119 } else if (ElemQT->isFloatingType()) { // Get only sign bit.
17120 ALane =
17121 SourceLHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
17122 BLane =
17123 SourceRHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
17124 } else { // Must be integer or floating type.
17125 return false;
17126 }
17127 AWide.insertBits(SubBits: ALane, bitPosition: I * LaneWidth);
17128 BWide.insertBits(SubBits: BLane, bitPosition: I * LaneWidth);
17129 }
17130 return Success(Value: Fn(AWide, BWide), E);
17131 };
17132
17133 auto HandleMaskBinOp =
17134 [&](llvm::function_ref<APSInt(const APSInt &, const APSInt &)> Fn)
17135 -> bool {
17136 APValue LHS, RHS;
17137 if (!Evaluate(Result&: LHS, Info, E: E->getArg(Arg: 0)) ||
17138 !Evaluate(Result&: RHS, Info, E: E->getArg(Arg: 1)))
17139 return false;
17140
17141 APSInt ResultInt = Fn(LHS.getInt(), RHS.getInt());
17142
17143 return Success(V: APValue(ResultInt), E);
17144 };
17145
17146 auto HandleCRC32 = [&](unsigned DataBytes) -> bool {
17147 APSInt CRC, Data;
17148 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CRC, Info) ||
17149 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Data, Info))
17150 return false;
17151
17152 uint64_t CRCVal = CRC.getZExtValue();
17153 uint64_t DataVal = Data.getZExtValue();
17154
17155 // CRC32C polynomial (iSCSI polynomial, bit-reversed)
17156 static const uint32_t CRC32C_POLY = 0x82F63B78;
17157
17158 uint32_t Result = llvm::calculateReflectedCRC32(
17159 Crc: static_cast<uint32_t>(CRCVal), Data: DataVal, DataBytes, Poly: CRC32C_POLY);
17160
17161 return Success(Value: Result, E);
17162 };
17163
17164 switch (BuiltinOp) {
17165 default:
17166 return false;
17167
17168 case X86::BI__builtin_ia32_crc32qi:
17169 return HandleCRC32(1);
17170 case X86::BI__builtin_ia32_crc32hi:
17171 return HandleCRC32(2);
17172 case X86::BI__builtin_ia32_crc32si:
17173 return HandleCRC32(4);
17174 case X86::BI__builtin_ia32_crc32di:
17175 return HandleCRC32(8);
17176
17177 case Builtin::BI__builtin_dynamic_object_size:
17178 case Builtin::BI__builtin_object_size: {
17179 // The type was checked when we built the expression.
17180 unsigned Type =
17181 E->getArg(Arg: 1)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
17182 assert(Type <= 3 && "unexpected type");
17183
17184 bool IsDynamic = BuiltinOp == Builtin::BI__builtin_dynamic_object_size;
17185 if (std::optional<uint64_t> Size =
17186 tryEvaluateBuiltinObjectSize(E: E->getArg(Arg: 0), Type, Info, IsDynamic))
17187 return Success(Value: *Size, E);
17188
17189 if (E->getArg(Arg: 0)->HasSideEffects(Ctx: Info.Ctx))
17190 return Success(Value: (Type & 2) ? 0 : -1, E);
17191
17192 // Expression had no side effects, but we couldn't statically determine the
17193 // size of the referenced object.
17194 switch (Info.EvalMode) {
17195 case EvaluationMode::ConstantExpression:
17196 case EvaluationMode::ConstantFold:
17197 case EvaluationMode::IgnoreSideEffects:
17198 // Leave it to IR generation.
17199 return Error(E);
17200 case EvaluationMode::ConstantExpressionUnevaluated:
17201 // Reduce it to a constant now.
17202 return Success(Value: (Type & 2) ? 0 : -1, E);
17203 }
17204
17205 llvm_unreachable("unexpected EvalMode");
17206 }
17207
17208 case Builtin::BI__builtin_os_log_format_buffer_size: {
17209 analyze_os_log::OSLogBufferLayout Layout;
17210 analyze_os_log::computeOSLogBufferLayout(Ctx&: Info.Ctx, E, layout&: Layout);
17211 return Success(Value: Layout.size().getQuantity(), E);
17212 }
17213
17214 case Builtin::BI__builtin_is_aligned: {
17215 APValue Src;
17216 APSInt Alignment;
17217 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17218 return false;
17219 if (Src.isLValue()) {
17220 // If we evaluated a pointer, check the minimum known alignment.
17221 LValue Ptr;
17222 Ptr.setFrom(Ctx: Info.Ctx, V: Src);
17223 if (!Ptr.Base) {
17224 // Null pointers are always aligned.
17225 if (Ptr.Offset.isZero())
17226 return Success(Value: 1, E);
17227
17228 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_compute)
17229 << Alignment;
17230 // Reject non-null pointers without an underlying object.
17231 // Do not interpret the pointer offset as an integer address.
17232 return false;
17233 }
17234
17235 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Ptr);
17236 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Ptr.Offset);
17237 // We can return true if the known alignment at the computed offset is
17238 // greater than the requested alignment.
17239 assert(PtrAlign.isPowerOfTwo());
17240 assert(Alignment.isPowerOf2());
17241 if (PtrAlign.getQuantity() >= Alignment)
17242 return Success(Value: 1, E);
17243 // If the alignment is not known to be sufficient, some cases could still
17244 // be aligned at run time. However, if the requested alignment is less or
17245 // equal to the base alignment and the offset is not aligned, we know that
17246 // the run-time value can never be aligned.
17247 if (BaseAlignment.getQuantity() >= Alignment &&
17248 PtrAlign.getQuantity() < Alignment)
17249 return Success(Value: 0, E);
17250 // Otherwise we can't infer whether the value is sufficiently aligned.
17251 // TODO: __builtin_is_aligned(__builtin_align_{down,up{(expr, N), N)
17252 // in cases where we can't fully evaluate the pointer.
17253 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_compute)
17254 << Alignment;
17255 return false;
17256 }
17257 assert(Src.isInt());
17258 return Success(Value: (Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E);
17259 }
17260 case Builtin::BI__builtin_align_up: {
17261 APValue Src;
17262 APSInt Alignment;
17263 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17264 return false;
17265 if (!Src.isInt())
17266 return Error(E);
17267 APSInt AlignedVal =
17268 APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1),
17269 Src.getInt().isUnsigned());
17270 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
17271 return Success(SI: AlignedVal, E);
17272 }
17273 case Builtin::BI__builtin_align_down: {
17274 APValue Src;
17275 APSInt Alignment;
17276 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17277 return false;
17278 if (!Src.isInt())
17279 return Error(E);
17280 APSInt AlignedVal =
17281 APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned());
17282 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
17283 return Success(SI: AlignedVal, E);
17284 }
17285
17286 case Builtin::BI__builtin_bitreverseg:
17287 case Builtin::BI__builtin_bitreverse8:
17288 case Builtin::BI__builtin_bitreverse16:
17289 case Builtin::BI__builtin_bitreverse32:
17290 case Builtin::BI__builtin_bitreverse64:
17291 case Builtin::BI__builtin_elementwise_bitreverse: {
17292 APSInt Val;
17293 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17294 return false;
17295
17296 return Success(I: Val.reverseBits(), E);
17297 }
17298 case Builtin::BI__builtin_bswapg:
17299 case Builtin::BI__builtin_bswap16:
17300 case Builtin::BI__builtin_bswap32:
17301 case Builtin::BI__builtin_bswap64:
17302 case Builtin::BIstdc_memreverse8u8:
17303 case Builtin::BIstdc_memreverse8u16:
17304 case Builtin::BIstdc_memreverse8u32:
17305 case Builtin::BIstdc_memreverse8u64: {
17306 APSInt Val;
17307 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17308 return false;
17309 if (Val.getBitWidth() == 8 || Val.getBitWidth() == 1)
17310 return Success(SI: Val, E);
17311
17312 return Success(I: Val.byteSwap(), E);
17313 }
17314
17315 case Builtin::BI__builtin_classify_type:
17316 return Success(Value: (int)EvaluateBuiltinClassifyType(E, LangOpts: Info.getLangOpts()), E);
17317
17318 case Builtin::BI__builtin_clrsb:
17319 case Builtin::BI__builtin_clrsbl:
17320 case Builtin::BI__builtin_clrsbll: {
17321 APSInt Val;
17322 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17323 return false;
17324
17325 return Success(Value: Val.getBitWidth() - Val.getSignificantBits(), E);
17326 }
17327
17328 case Builtin::BI__builtin_clz:
17329 case Builtin::BI__builtin_clzl:
17330 case Builtin::BI__builtin_clzll:
17331 case Builtin::BI__builtin_clzs:
17332 case Builtin::BI__builtin_clzg:
17333 case Builtin::BI__builtin_elementwise_clzg:
17334 case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes
17335 case Builtin::BI__lzcnt:
17336 case Builtin::BI__lzcnt64: {
17337 APSInt Val;
17338 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17339 APValue Vec;
17340 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17341 return false;
17342 Val = ConvertBoolVectorToInt(Val: Vec);
17343 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17344 return false;
17345 }
17346
17347 std::optional<APSInt> Fallback;
17348 if ((BuiltinOp == Builtin::BI__builtin_clzg ||
17349 BuiltinOp == Builtin::BI__builtin_elementwise_clzg) &&
17350 E->getNumArgs() > 1) {
17351 APSInt FallbackTemp;
17352 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
17353 return false;
17354 Fallback = FallbackTemp;
17355 }
17356
17357 if (!Val) {
17358 if (Fallback)
17359 return Success(SI: *Fallback, E);
17360
17361 // When the argument is 0, the result of GCC builtins is undefined,
17362 // whereas for Microsoft intrinsics, the result is the bit-width of the
17363 // argument.
17364 bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 &&
17365 BuiltinOp != Builtin::BI__lzcnt &&
17366 BuiltinOp != Builtin::BI__lzcnt64;
17367
17368 if (BuiltinOp == Builtin::BI__builtin_elementwise_clzg) {
17369 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
17370 << /*IsTrailing=*/false;
17371 }
17372
17373 if (ZeroIsUndefined)
17374 return Error(E);
17375 }
17376
17377 return Success(Value: Val.countl_zero(), E);
17378 }
17379
17380 case Builtin::BI__builtin_constant_p: {
17381 const Expr *Arg = E->getArg(Arg: 0);
17382 if (EvaluateBuiltinConstantP(Info, Arg))
17383 return Success(Value: true, E);
17384 if (Info.InConstantContext || Arg->HasSideEffects(Ctx: Info.Ctx)) {
17385 // Outside a constant context, eagerly evaluate to false in the presence
17386 // of side-effects in order to avoid -Wunsequenced false-positives in
17387 // a branch on __builtin_constant_p(expr).
17388 return Success(Value: false, E);
17389 }
17390 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17391 return false;
17392 }
17393
17394 case Builtin::BI__noop:
17395 // __noop always evaluates successfully and returns 0.
17396 return Success(Value: 0, E);
17397
17398 case Builtin::BI__builtin_is_constant_evaluated: {
17399 const auto *Callee = Info.CurrentCall->getCallee();
17400 if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression &&
17401 (Info.CallStackDepth == 1 ||
17402 (Info.CallStackDepth == 2 && Callee->isInStdNamespace() &&
17403 Callee->getIdentifier() &&
17404 Callee->getIdentifier()->isStr(Str: "is_constant_evaluated")))) {
17405 // FIXME: Find a better way to avoid duplicated diagnostics.
17406 if (Info.EvalStatus.Diag)
17407 Info.report(Loc: (Info.CallStackDepth == 1)
17408 ? E->getExprLoc()
17409 : Info.CurrentCall->getCallRange().getBegin(),
17410 DiagId: diag::warn_is_constant_evaluated_always_true_constexpr)
17411 << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated"
17412 : "std::is_constant_evaluated");
17413 }
17414
17415 return Success(Value: Info.InConstantContext, E);
17416 }
17417
17418 case Builtin::BI__builtin_is_within_lifetime:
17419 if (auto result = EvaluateBuiltinIsWithinLifetime(*this, E))
17420 return Success(Value: *result, E);
17421 return false;
17422
17423 case Builtin::BI__builtin_ctz:
17424 case Builtin::BI__builtin_ctzl:
17425 case Builtin::BI__builtin_ctzll:
17426 case Builtin::BI__builtin_ctzs:
17427 case Builtin::BI__builtin_ctzg:
17428 case Builtin::BI__builtin_elementwise_ctzg: {
17429 APSInt Val;
17430 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17431 APValue Vec;
17432 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17433 return false;
17434 Val = ConvertBoolVectorToInt(Val: Vec);
17435 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17436 return false;
17437 }
17438
17439 std::optional<APSInt> Fallback;
17440 if ((BuiltinOp == Builtin::BI__builtin_ctzg ||
17441 BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) &&
17442 E->getNumArgs() > 1) {
17443 APSInt FallbackTemp;
17444 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
17445 return false;
17446 Fallback = FallbackTemp;
17447 }
17448
17449 if (!Val) {
17450 if (Fallback)
17451 return Success(SI: *Fallback, E);
17452
17453 if (BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) {
17454 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
17455 << /*IsTrailing=*/true;
17456 }
17457 return Error(E);
17458 }
17459
17460 return Success(Value: Val.countr_zero(), E);
17461 }
17462
17463 case Builtin::BI__builtin_eh_return_data_regno: {
17464 int Operand = E->getArg(Arg: 0)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
17465 Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(RegNo: Operand);
17466 return Success(Value: Operand, E);
17467 }
17468
17469 case Builtin::BI__builtin_elementwise_abs: {
17470 APSInt Val;
17471 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17472 return false;
17473
17474 return Success(I: Val.abs(), E);
17475 }
17476
17477 case Builtin::BI__builtin_expect:
17478 case Builtin::BI__builtin_expect_with_probability:
17479 return Visit(S: E->getArg(Arg: 0));
17480
17481 case Builtin::BI__builtin_ptrauth_string_discriminator: {
17482 const auto *Literal =
17483 cast<StringLiteral>(Val: E->getArg(Arg: 0)->IgnoreParenImpCasts());
17484 uint64_t Result = getPointerAuthStableSipHash(S: Literal->getString());
17485 return Success(Value: Result, E);
17486 }
17487
17488 case Builtin::BI__builtin_infer_alloc_token: {
17489 // If we fail to infer a type, this fails to be a constant expression; this
17490 // can be checked with __builtin_constant_p(...).
17491 QualType AllocType = infer_alloc::inferPossibleType(E, Ctx: Info.Ctx, CastE: nullptr);
17492 if (AllocType.isNull())
17493 return Error(
17494 E, D: diag::note_constexpr_infer_alloc_token_type_inference_failed);
17495 auto ATMD = infer_alloc::getAllocTokenMetadata(T: AllocType, Ctx: Info.Ctx);
17496 if (!ATMD)
17497 return Error(E, D: diag::note_constexpr_infer_alloc_token_no_metadata);
17498 auto Mode =
17499 Info.getLangOpts().AllocTokenMode.value_or(u: llvm::DefaultAllocTokenMode);
17500 uint64_t BitWidth = Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType());
17501 auto MaxTokensOpt = Info.getLangOpts().AllocTokenMax;
17502 uint64_t MaxTokens =
17503 MaxTokensOpt.value_or(u: 0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
17504 auto MaybeToken = llvm::getAllocToken(Mode, Metadata: *ATMD, MaxTokens);
17505 if (!MaybeToken)
17506 return Error(E, D: diag::note_constexpr_infer_alloc_token_stateful_mode);
17507 return Success(I: llvm::APInt(BitWidth, *MaybeToken), E);
17508 }
17509
17510 case Builtin::BI__builtin_ffs:
17511 case Builtin::BI__builtin_ffsl:
17512 case Builtin::BI__builtin_ffsll: {
17513 APSInt Val;
17514 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17515 return false;
17516
17517 unsigned N = Val.countr_zero();
17518 return Success(Value: N == Val.getBitWidth() ? 0 : N + 1, E);
17519 }
17520
17521 case Builtin::BI__builtin_fpclassify: {
17522 APFloat Val(0.0);
17523 if (!EvaluateFloat(E: E->getArg(Arg: 5), Result&: Val, Info))
17524 return false;
17525 unsigned Arg;
17526 switch (Val.getCategory()) {
17527 case APFloat::fcNaN: Arg = 0; break;
17528 case APFloat::fcInfinity: Arg = 1; break;
17529 case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break;
17530 case APFloat::fcZero: Arg = 4; break;
17531 }
17532 return Visit(S: E->getArg(Arg));
17533 }
17534
17535 case Builtin::BI__builtin_isinf_sign: {
17536 APFloat Val(0.0);
17537 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17538 Success(Value: Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E);
17539 }
17540
17541 case Builtin::BI__builtin_isinf: {
17542 APFloat Val(0.0);
17543 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17544 Success(Value: Val.isInfinity() ? 1 : 0, E);
17545 }
17546
17547 case Builtin::BI__builtin_isfinite: {
17548 APFloat Val(0.0);
17549 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17550 Success(Value: Val.isFinite() ? 1 : 0, E);
17551 }
17552
17553 case Builtin::BI__builtin_isnan: {
17554 APFloat Val(0.0);
17555 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17556 Success(Value: Val.isNaN() ? 1 : 0, E);
17557 }
17558
17559 case Builtin::BI__builtin_isnormal: {
17560 APFloat Val(0.0);
17561 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17562 Success(Value: Val.isNormal() ? 1 : 0, E);
17563 }
17564
17565 case Builtin::BI__builtin_issubnormal: {
17566 APFloat Val(0.0);
17567 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17568 Success(Value: Val.isDenormal() ? 1 : 0, E);
17569 }
17570
17571 case Builtin::BI__builtin_iszero: {
17572 APFloat Val(0.0);
17573 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17574 Success(Value: Val.isZero() ? 1 : 0, E);
17575 }
17576
17577 case Builtin::BI__builtin_signbit:
17578 case Builtin::BI__builtin_signbitf:
17579 case Builtin::BI__builtin_signbitl: {
17580 APFloat Val(0.0);
17581 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17582 Success(Value: Val.isNegative() ? 1 : 0, E);
17583 }
17584
17585 case Builtin::BI__builtin_isgreater:
17586 case Builtin::BI__builtin_isgreaterequal:
17587 case Builtin::BI__builtin_isless:
17588 case Builtin::BI__builtin_islessequal:
17589 case Builtin::BI__builtin_islessgreater:
17590 case Builtin::BI__builtin_isunordered: {
17591 APFloat LHS(0.0);
17592 APFloat RHS(0.0);
17593 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17594 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
17595 return false;
17596
17597 return Success(
17598 Value: [&] {
17599 switch (BuiltinOp) {
17600 case Builtin::BI__builtin_isgreater:
17601 return LHS > RHS;
17602 case Builtin::BI__builtin_isgreaterequal:
17603 return LHS >= RHS;
17604 case Builtin::BI__builtin_isless:
17605 return LHS < RHS;
17606 case Builtin::BI__builtin_islessequal:
17607 return LHS <= RHS;
17608 case Builtin::BI__builtin_islessgreater: {
17609 APFloat::cmpResult cmp = LHS.compare(RHS);
17610 return cmp == APFloat::cmpResult::cmpLessThan ||
17611 cmp == APFloat::cmpResult::cmpGreaterThan;
17612 }
17613 case Builtin::BI__builtin_isunordered:
17614 return LHS.compare(RHS) == APFloat::cmpResult::cmpUnordered;
17615 default:
17616 llvm_unreachable("Unexpected builtin ID: Should be a floating "
17617 "point comparison function");
17618 }
17619 }()
17620 ? 1
17621 : 0,
17622 E);
17623 }
17624
17625 case Builtin::BI__builtin_issignaling: {
17626 APFloat Val(0.0);
17627 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17628 Success(Value: Val.isSignaling() ? 1 : 0, E);
17629 }
17630
17631 case Builtin::BI__builtin_isfpclass: {
17632 APSInt MaskVal;
17633 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: MaskVal, Info))
17634 return false;
17635 unsigned Test = static_cast<llvm::FPClassTest>(MaskVal.getZExtValue());
17636 APFloat Val(0.0);
17637 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17638 Success(Value: (Val.classify() & Test) ? 1 : 0, E);
17639 }
17640
17641 case Builtin::BI__builtin_parity:
17642 case Builtin::BI__builtin_parityl:
17643 case Builtin::BI__builtin_parityll: {
17644 APSInt Val;
17645 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17646 return false;
17647
17648 return Success(Value: Val.popcount() % 2, E);
17649 }
17650
17651 case Builtin::BI__builtin_abs:
17652 case Builtin::BI__builtin_labs:
17653 case Builtin::BI__builtin_llabs: {
17654 APSInt Val;
17655 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17656 return false;
17657 if (Val == APSInt(APInt::getSignedMinValue(numBits: Val.getBitWidth()),
17658 /*IsUnsigned=*/false))
17659 return false;
17660 if (Val.isNegative())
17661 Val.negate();
17662 return Success(SI: Val, E);
17663 }
17664
17665 case Builtin::BI__builtin_popcount:
17666 case Builtin::BI__builtin_popcountl:
17667 case Builtin::BI__builtin_popcountll:
17668 case Builtin::BI__builtin_popcountg:
17669 case Builtin::BI__builtin_elementwise_popcount:
17670 case Builtin::BI__popcnt16: // Microsoft variants of popcount
17671 case Builtin::BI__popcnt:
17672 case Builtin::BI__popcnt64: {
17673 APSInt Val;
17674 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17675 APValue Vec;
17676 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17677 return false;
17678 Val = ConvertBoolVectorToInt(Val: Vec);
17679 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17680 return false;
17681 }
17682
17683 return Success(Value: Val.popcount(), E);
17684 }
17685
17686 case Builtin::BI__builtin_rotateleft8:
17687 case Builtin::BI__builtin_rotateleft16:
17688 case Builtin::BI__builtin_rotateleft32:
17689 case Builtin::BI__builtin_rotateleft64:
17690 case Builtin::BI__builtin_rotateright8:
17691 case Builtin::BI__builtin_rotateright16:
17692 case Builtin::BI__builtin_rotateright32:
17693 case Builtin::BI__builtin_rotateright64:
17694 case Builtin::BI__builtin_stdc_rotate_left:
17695 case Builtin::BI__builtin_stdc_rotate_right:
17696 case Builtin::BIstdc_rotate_left_uc:
17697 case Builtin::BIstdc_rotate_left_us:
17698 case Builtin::BIstdc_rotate_left_ui:
17699 case Builtin::BIstdc_rotate_left_ul:
17700 case Builtin::BIstdc_rotate_left_ull:
17701 case Builtin::BIstdc_rotate_right_uc:
17702 case Builtin::BIstdc_rotate_right_us:
17703 case Builtin::BIstdc_rotate_right_ui:
17704 case Builtin::BIstdc_rotate_right_ul:
17705 case Builtin::BIstdc_rotate_right_ull:
17706 case Builtin::BI_rotl8: // Microsoft variants of rotate left
17707 case Builtin::BI_rotl16:
17708 case Builtin::BI_rotl:
17709 case Builtin::BI_lrotl:
17710 case Builtin::BI_rotl64:
17711 case Builtin::BI_rotr8: // Microsoft variants of rotate right
17712 case Builtin::BI_rotr16:
17713 case Builtin::BI_rotr:
17714 case Builtin::BI_lrotr:
17715 case Builtin::BI_rotr64: {
17716 APSInt Value, Amount;
17717 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Value, Info) ||
17718 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amount, Info))
17719 return false;
17720
17721 Amount = NormalizeRotateAmount(Value, Amount);
17722
17723 switch (BuiltinOp) {
17724 case Builtin::BI__builtin_rotateright8:
17725 case Builtin::BI__builtin_rotateright16:
17726 case Builtin::BI__builtin_rotateright32:
17727 case Builtin::BI__builtin_rotateright64:
17728 case Builtin::BI__builtin_stdc_rotate_right:
17729 case Builtin::BIstdc_rotate_right_uc:
17730 case Builtin::BIstdc_rotate_right_us:
17731 case Builtin::BIstdc_rotate_right_ui:
17732 case Builtin::BIstdc_rotate_right_ul:
17733 case Builtin::BIstdc_rotate_right_ull:
17734 case Builtin::BI_rotr8:
17735 case Builtin::BI_rotr16:
17736 case Builtin::BI_rotr:
17737 case Builtin::BI_lrotr:
17738 case Builtin::BI_rotr64:
17739 return Success(
17740 SI: APSInt(Value.rotr(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17741 default:
17742 return Success(
17743 SI: APSInt(Value.rotl(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17744 }
17745 }
17746
17747 case Builtin::BIstdc_leading_zeros_uc:
17748 case Builtin::BIstdc_leading_zeros_us:
17749 case Builtin::BIstdc_leading_zeros_ui:
17750 case Builtin::BIstdc_leading_zeros_ul:
17751 case Builtin::BIstdc_leading_zeros_ull:
17752 case Builtin::BIstdc_leading_ones_uc:
17753 case Builtin::BIstdc_leading_ones_us:
17754 case Builtin::BIstdc_leading_ones_ui:
17755 case Builtin::BIstdc_leading_ones_ul:
17756 case Builtin::BIstdc_leading_ones_ull:
17757 case Builtin::BIstdc_trailing_zeros_uc:
17758 case Builtin::BIstdc_trailing_zeros_us:
17759 case Builtin::BIstdc_trailing_zeros_ui:
17760 case Builtin::BIstdc_trailing_zeros_ul:
17761 case Builtin::BIstdc_trailing_zeros_ull:
17762 case Builtin::BIstdc_trailing_ones_uc:
17763 case Builtin::BIstdc_trailing_ones_us:
17764 case Builtin::BIstdc_trailing_ones_ui:
17765 case Builtin::BIstdc_trailing_ones_ul:
17766 case Builtin::BIstdc_trailing_ones_ull:
17767 case Builtin::BIstdc_first_leading_zero_uc:
17768 case Builtin::BIstdc_first_leading_zero_us:
17769 case Builtin::BIstdc_first_leading_zero_ui:
17770 case Builtin::BIstdc_first_leading_zero_ul:
17771 case Builtin::BIstdc_first_leading_zero_ull:
17772 case Builtin::BIstdc_first_leading_one_uc:
17773 case Builtin::BIstdc_first_leading_one_us:
17774 case Builtin::BIstdc_first_leading_one_ui:
17775 case Builtin::BIstdc_first_leading_one_ul:
17776 case Builtin::BIstdc_first_leading_one_ull:
17777 case Builtin::BIstdc_first_trailing_zero_uc:
17778 case Builtin::BIstdc_first_trailing_zero_us:
17779 case Builtin::BIstdc_first_trailing_zero_ui:
17780 case Builtin::BIstdc_first_trailing_zero_ul:
17781 case Builtin::BIstdc_first_trailing_zero_ull:
17782 case Builtin::BIstdc_first_trailing_one_uc:
17783 case Builtin::BIstdc_first_trailing_one_us:
17784 case Builtin::BIstdc_first_trailing_one_ui:
17785 case Builtin::BIstdc_first_trailing_one_ul:
17786 case Builtin::BIstdc_first_trailing_one_ull:
17787 case Builtin::BIstdc_count_zeros_uc:
17788 case Builtin::BIstdc_count_zeros_us:
17789 case Builtin::BIstdc_count_zeros_ui:
17790 case Builtin::BIstdc_count_zeros_ul:
17791 case Builtin::BIstdc_count_zeros_ull:
17792 case Builtin::BIstdc_count_ones_uc:
17793 case Builtin::BIstdc_count_ones_us:
17794 case Builtin::BIstdc_count_ones_ui:
17795 case Builtin::BIstdc_count_ones_ul:
17796 case Builtin::BIstdc_count_ones_ull:
17797 case Builtin::BIstdc_has_single_bit_uc:
17798 case Builtin::BIstdc_has_single_bit_us:
17799 case Builtin::BIstdc_has_single_bit_ui:
17800 case Builtin::BIstdc_has_single_bit_ul:
17801 case Builtin::BIstdc_has_single_bit_ull:
17802 case Builtin::BIstdc_bit_width_uc:
17803 case Builtin::BIstdc_bit_width_us:
17804 case Builtin::BIstdc_bit_width_ui:
17805 case Builtin::BIstdc_bit_width_ul:
17806 case Builtin::BIstdc_bit_width_ull:
17807 case Builtin::BIstdc_bit_floor_uc:
17808 case Builtin::BIstdc_bit_floor_us:
17809 case Builtin::BIstdc_bit_floor_ui:
17810 case Builtin::BIstdc_bit_floor_ul:
17811 case Builtin::BIstdc_bit_floor_ull:
17812 case Builtin::BIstdc_bit_ceil_uc:
17813 case Builtin::BIstdc_bit_ceil_us:
17814 case Builtin::BIstdc_bit_ceil_ui:
17815 case Builtin::BIstdc_bit_ceil_ul:
17816 case Builtin::BIstdc_bit_ceil_ull:
17817 case Builtin::BI__builtin_stdc_leading_zeros:
17818 case Builtin::BI__builtin_stdc_leading_ones:
17819 case Builtin::BI__builtin_stdc_trailing_zeros:
17820 case Builtin::BI__builtin_stdc_trailing_ones:
17821 case Builtin::BI__builtin_stdc_first_leading_zero:
17822 case Builtin::BI__builtin_stdc_first_leading_one:
17823 case Builtin::BI__builtin_stdc_first_trailing_zero:
17824 case Builtin::BI__builtin_stdc_first_trailing_one:
17825 case Builtin::BI__builtin_stdc_count_zeros:
17826 case Builtin::BI__builtin_stdc_count_ones:
17827 case Builtin::BI__builtin_stdc_has_single_bit:
17828 case Builtin::BI__builtin_stdc_bit_width:
17829 case Builtin::BI__builtin_stdc_bit_floor:
17830 case Builtin::BI__builtin_stdc_bit_ceil: {
17831 APSInt Val;
17832 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17833 return false;
17834
17835 unsigned BitWidth = Val.getBitWidth();
17836 const unsigned ResBitWidth = Info.Ctx.getIntWidth(T: E->getType());
17837
17838 switch (BuiltinOp) {
17839 case Builtin::BIstdc_leading_zeros_uc:
17840 case Builtin::BIstdc_leading_zeros_us:
17841 case Builtin::BIstdc_leading_zeros_ui:
17842 case Builtin::BIstdc_leading_zeros_ul:
17843 case Builtin::BIstdc_leading_zeros_ull:
17844 case Builtin::BI__builtin_stdc_leading_zeros:
17845 return Success(I: APInt(ResBitWidth, Val.countl_zero()), E);
17846 case Builtin::BIstdc_leading_ones_uc:
17847 case Builtin::BIstdc_leading_ones_us:
17848 case Builtin::BIstdc_leading_ones_ui:
17849 case Builtin::BIstdc_leading_ones_ul:
17850 case Builtin::BIstdc_leading_ones_ull:
17851 case Builtin::BI__builtin_stdc_leading_ones:
17852 return Success(I: APInt(ResBitWidth, Val.countl_one()), E);
17853 case Builtin::BIstdc_trailing_zeros_uc:
17854 case Builtin::BIstdc_trailing_zeros_us:
17855 case Builtin::BIstdc_trailing_zeros_ui:
17856 case Builtin::BIstdc_trailing_zeros_ul:
17857 case Builtin::BIstdc_trailing_zeros_ull:
17858 case Builtin::BI__builtin_stdc_trailing_zeros:
17859 return Success(I: APInt(ResBitWidth, Val.countr_zero()), E);
17860 case Builtin::BIstdc_trailing_ones_uc:
17861 case Builtin::BIstdc_trailing_ones_us:
17862 case Builtin::BIstdc_trailing_ones_ui:
17863 case Builtin::BIstdc_trailing_ones_ul:
17864 case Builtin::BIstdc_trailing_ones_ull:
17865 case Builtin::BI__builtin_stdc_trailing_ones:
17866 return Success(I: APInt(ResBitWidth, Val.countr_one()), E);
17867 case Builtin::BIstdc_first_leading_zero_uc:
17868 case Builtin::BIstdc_first_leading_zero_us:
17869 case Builtin::BIstdc_first_leading_zero_ui:
17870 case Builtin::BIstdc_first_leading_zero_ul:
17871 case Builtin::BIstdc_first_leading_zero_ull:
17872 case Builtin::BI__builtin_stdc_first_leading_zero:
17873 return Success(
17874 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countl_one() + 1), E);
17875 case Builtin::BIstdc_first_leading_one_uc:
17876 case Builtin::BIstdc_first_leading_one_us:
17877 case Builtin::BIstdc_first_leading_one_ui:
17878 case Builtin::BIstdc_first_leading_one_ul:
17879 case Builtin::BIstdc_first_leading_one_ull:
17880 case Builtin::BI__builtin_stdc_first_leading_one:
17881 return Success(
17882 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countl_zero() + 1), E);
17883 case Builtin::BIstdc_first_trailing_zero_uc:
17884 case Builtin::BIstdc_first_trailing_zero_us:
17885 case Builtin::BIstdc_first_trailing_zero_ui:
17886 case Builtin::BIstdc_first_trailing_zero_ul:
17887 case Builtin::BIstdc_first_trailing_zero_ull:
17888 case Builtin::BI__builtin_stdc_first_trailing_zero:
17889 return Success(
17890 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countr_one() + 1), E);
17891 case Builtin::BIstdc_first_trailing_one_uc:
17892 case Builtin::BIstdc_first_trailing_one_us:
17893 case Builtin::BIstdc_first_trailing_one_ui:
17894 case Builtin::BIstdc_first_trailing_one_ul:
17895 case Builtin::BIstdc_first_trailing_one_ull:
17896 case Builtin::BI__builtin_stdc_first_trailing_one:
17897 return Success(
17898 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countr_zero() + 1), E);
17899 case Builtin::BIstdc_count_zeros_uc:
17900 case Builtin::BIstdc_count_zeros_us:
17901 case Builtin::BIstdc_count_zeros_ui:
17902 case Builtin::BIstdc_count_zeros_ul:
17903 case Builtin::BIstdc_count_zeros_ull:
17904 case Builtin::BI__builtin_stdc_count_zeros: {
17905 APInt Cnt(ResBitWidth, BitWidth - Val.popcount());
17906 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
17907 }
17908 case Builtin::BIstdc_count_ones_uc:
17909 case Builtin::BIstdc_count_ones_us:
17910 case Builtin::BIstdc_count_ones_ui:
17911 case Builtin::BIstdc_count_ones_ul:
17912 case Builtin::BIstdc_count_ones_ull:
17913 case Builtin::BI__builtin_stdc_count_ones: {
17914 APInt Cnt(ResBitWidth, Val.popcount());
17915 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
17916 }
17917 case Builtin::BIstdc_has_single_bit_uc:
17918 case Builtin::BIstdc_has_single_bit_us:
17919 case Builtin::BIstdc_has_single_bit_ui:
17920 case Builtin::BIstdc_has_single_bit_ul:
17921 case Builtin::BIstdc_has_single_bit_ull:
17922 case Builtin::BI__builtin_stdc_has_single_bit: {
17923 APInt Res(ResBitWidth, Val.popcount() == 1 ? 1 : 0);
17924 return Success(SI: APSInt(Res, /*IsUnsigned*/ true), E);
17925 }
17926 case Builtin::BIstdc_bit_width_uc:
17927 case Builtin::BIstdc_bit_width_us:
17928 case Builtin::BIstdc_bit_width_ui:
17929 case Builtin::BIstdc_bit_width_ul:
17930 case Builtin::BIstdc_bit_width_ull:
17931 case Builtin::BI__builtin_stdc_bit_width:
17932 return Success(I: APInt(ResBitWidth, BitWidth - Val.countl_zero()), E);
17933 case Builtin::BIstdc_bit_floor_uc:
17934 case Builtin::BIstdc_bit_floor_us:
17935 case Builtin::BIstdc_bit_floor_ui:
17936 case Builtin::BIstdc_bit_floor_ul:
17937 case Builtin::BIstdc_bit_floor_ull:
17938 case Builtin::BI__builtin_stdc_bit_floor: {
17939 if (Val.isZero())
17940 return Success(I: APInt(BitWidth, 0), E);
17941 unsigned Exp = BitWidth - Val.countl_zero() - 1;
17942 return Success(
17943 SI: APSInt(APInt::getOneBitSet(numBits: BitWidth, BitNo: Exp), /*IsUnsigned*/ true), E);
17944 }
17945 case Builtin::BIstdc_bit_ceil_uc:
17946 case Builtin::BIstdc_bit_ceil_us:
17947 case Builtin::BIstdc_bit_ceil_ui:
17948 case Builtin::BIstdc_bit_ceil_ul:
17949 case Builtin::BIstdc_bit_ceil_ull:
17950 case Builtin::BI__builtin_stdc_bit_ceil: {
17951 if (Val.ule(RHS: 1))
17952 return Success(SI: APSInt(APInt(BitWidth, 1), /*IsUnsigned*/ true), E);
17953 APInt ValMinusOne = Val - 1;
17954 unsigned LZ = ValMinusOne.countl_zero();
17955 if (LZ == 0)
17956 return Success(SI: APSInt(APInt(BitWidth, 0), /*IsUnsigned*/ true),
17957 E); // overflows; wrap to 0
17958 APInt Result = APInt::getOneBitSet(numBits: BitWidth, BitNo: BitWidth - LZ);
17959 return Success(SI: APSInt(Result, /*IsUnsigned*/ true), E);
17960 }
17961 default:
17962 llvm_unreachable("Unknown stdc builtin");
17963 }
17964 }
17965
17966 case Builtin::BI__builtin_elementwise_add_sat: {
17967 APSInt LHS, RHS;
17968 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17969 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17970 return false;
17971
17972 APInt Result = LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
17973 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17974 }
17975 case Builtin::BI__builtin_elementwise_sub_sat: {
17976 APSInt LHS, RHS;
17977 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17978 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17979 return false;
17980
17981 APInt Result = LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
17982 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17983 }
17984 case Builtin::BI__builtin_elementwise_max: {
17985 APSInt LHS, RHS;
17986 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17987 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17988 return false;
17989
17990 APInt Result = std::max(a: LHS, b: RHS);
17991 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17992 }
17993 case Builtin::BI__builtin_elementwise_min: {
17994 APSInt LHS, RHS;
17995 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17996 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17997 return false;
17998
17999 APInt Result = std::min(a: LHS, b: RHS);
18000 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
18001 }
18002 case Builtin::BI__builtin_elementwise_clmul: {
18003 APSInt LHS, RHS;
18004 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18005 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
18006 return false;
18007
18008 APInt Result = llvm::APIntOps::clmul(LHS, RHS);
18009 return Success(SI: APSInt(Result, LHS.isUnsigned()), E);
18010 }
18011 case Builtin::BI__builtin_elementwise_fshl:
18012 case Builtin::BI__builtin_elementwise_fshr: {
18013 APSInt Hi, Lo, Shift;
18014 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Hi, Info) ||
18015 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Lo, Info) ||
18016 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Shift, Info))
18017 return false;
18018
18019 switch (BuiltinOp) {
18020 case Builtin::BI__builtin_elementwise_fshl: {
18021 APSInt Result(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned());
18022 return Success(SI: Result, E);
18023 }
18024 case Builtin::BI__builtin_elementwise_fshr: {
18025 APSInt Result(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned());
18026 return Success(SI: Result, E);
18027 }
18028 }
18029 llvm_unreachable("Fully covered switch above");
18030 }
18031 case Builtin::BIstrlen:
18032 case Builtin::BIwcslen:
18033 // A call to strlen is not a constant expression.
18034 if (Info.getLangOpts().CPlusPlus11)
18035 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
18036 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
18037 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
18038 else
18039 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
18040 [[fallthrough]];
18041 case Builtin::BI__builtin_strlen:
18042 case Builtin::BI__builtin_wcslen: {
18043 // As an extension, we support __builtin_strlen() as a constant expression,
18044 // and support folding strlen() to a constant.
18045 if (std::optional<uint64_t> StrLen =
18046 EvaluateBuiltinStrLen(E: E->getArg(Arg: 0), Info))
18047 return Success(Value: *StrLen, E);
18048 return false;
18049 }
18050
18051 case Builtin::BIstrcmp:
18052 case Builtin::BIwcscmp:
18053 case Builtin::BIstrncmp:
18054 case Builtin::BIwcsncmp:
18055 case Builtin::BImemcmp:
18056 case Builtin::BIbcmp:
18057 case Builtin::BIwmemcmp:
18058 // A call to strlen is not a constant expression.
18059 if (Info.getLangOpts().CPlusPlus11)
18060 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
18061 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
18062 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
18063 else
18064 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
18065 [[fallthrough]];
18066 case Builtin::BI__builtin_strcmp:
18067 case Builtin::BI__builtin_wcscmp:
18068 case Builtin::BI__builtin_strncmp:
18069 case Builtin::BI__builtin_wcsncmp:
18070 case Builtin::BI__builtin_memcmp:
18071 case Builtin::BI__builtin_bcmp:
18072 case Builtin::BI__builtin_wmemcmp: {
18073 LValue String1, String2;
18074 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: String1, Info) ||
18075 !EvaluatePointer(E: E->getArg(Arg: 1), Result&: String2, Info))
18076 return false;
18077
18078 uint64_t MaxLength = uint64_t(-1);
18079 if (BuiltinOp != Builtin::BIstrcmp &&
18080 BuiltinOp != Builtin::BIwcscmp &&
18081 BuiltinOp != Builtin::BI__builtin_strcmp &&
18082 BuiltinOp != Builtin::BI__builtin_wcscmp) {
18083 APSInt N;
18084 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
18085 return false;
18086 MaxLength = N.getZExtValue();
18087 }
18088
18089 // Empty substrings compare equal by definition.
18090 if (MaxLength == 0u)
18091 return Success(Value: 0, E);
18092
18093 if (!String1.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
18094 !String2.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
18095 String1.Designator.Invalid || String2.Designator.Invalid)
18096 return false;
18097
18098 QualType CharTy1 = String1.Designator.getType(Ctx&: Info.Ctx);
18099 QualType CharTy2 = String2.Designator.getType(Ctx&: Info.Ctx);
18100
18101 bool IsRawByte = BuiltinOp == Builtin::BImemcmp ||
18102 BuiltinOp == Builtin::BIbcmp ||
18103 BuiltinOp == Builtin::BI__builtin_memcmp ||
18104 BuiltinOp == Builtin::BI__builtin_bcmp;
18105
18106 assert(IsRawByte ||
18107 (Info.Ctx.hasSameUnqualifiedType(
18108 CharTy1, E->getArg(0)->getType()->getPointeeType()) &&
18109 Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2)));
18110
18111 // For memcmp, allow comparing any arrays of '[[un]signed] char' or
18112 // 'char8_t', but no other types.
18113 if (IsRawByte &&
18114 !(isOneByteCharacterType(T: CharTy1) && isOneByteCharacterType(T: CharTy2))) {
18115 // FIXME: Consider using our bit_cast implementation to support this.
18116 Info.FFDiag(E, DiagId: diag::note_constexpr_memcmp_unsupported)
18117 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy1
18118 << CharTy2;
18119 return false;
18120 }
18121
18122 const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) {
18123 return handleLValueToRValueConversion(Info, Conv: E, Type: CharTy1, LVal: String1, RVal&: Char1) &&
18124 handleLValueToRValueConversion(Info, Conv: E, Type: CharTy2, LVal: String2, RVal&: Char2) &&
18125 Char1.isInt() && Char2.isInt();
18126 };
18127 const auto &AdvanceElems = [&] {
18128 return HandleLValueArrayAdjustment(Info, E, LVal&: String1, EltTy: CharTy1, Adjustment: 1) &&
18129 HandleLValueArrayAdjustment(Info, E, LVal&: String2, EltTy: CharTy2, Adjustment: 1);
18130 };
18131
18132 bool StopAtNull =
18133 (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp &&
18134 BuiltinOp != Builtin::BIwmemcmp &&
18135 BuiltinOp != Builtin::BI__builtin_memcmp &&
18136 BuiltinOp != Builtin::BI__builtin_bcmp &&
18137 BuiltinOp != Builtin::BI__builtin_wmemcmp);
18138 bool IsWide = BuiltinOp == Builtin::BIwcscmp ||
18139 BuiltinOp == Builtin::BIwcsncmp ||
18140 BuiltinOp == Builtin::BIwmemcmp ||
18141 BuiltinOp == Builtin::BI__builtin_wcscmp ||
18142 BuiltinOp == Builtin::BI__builtin_wcsncmp ||
18143 BuiltinOp == Builtin::BI__builtin_wmemcmp;
18144
18145 for (; MaxLength; --MaxLength) {
18146 APValue Char1, Char2;
18147 if (!ReadCurElems(Char1, Char2))
18148 return false;
18149 if (Char1.getInt().ne(RHS: Char2.getInt())) {
18150 if (IsWide) // wmemcmp compares with wchar_t signedness.
18151 return Success(Value: Char1.getInt() < Char2.getInt() ? -1 : 1, E);
18152 // memcmp always compares unsigned chars.
18153 return Success(Value: Char1.getInt().ult(RHS: Char2.getInt()) ? -1 : 1, E);
18154 }
18155 if (StopAtNull && !Char1.getInt())
18156 return Success(Value: 0, E);
18157 assert(!(StopAtNull && !Char2.getInt()));
18158 if (!AdvanceElems())
18159 return false;
18160 }
18161 // We hit the strncmp / memcmp limit.
18162 return Success(Value: 0, E);
18163 }
18164
18165 case Builtin::BI__atomic_always_lock_free:
18166 case Builtin::BI__atomic_is_lock_free:
18167 case Builtin::BI__c11_atomic_is_lock_free: {
18168 APSInt SizeVal;
18169 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SizeVal, Info))
18170 return false;
18171
18172 // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
18173 // of two less than or equal to the maximum inline atomic width, we know it
18174 // is lock-free. If the size isn't a power of two, or greater than the
18175 // maximum alignment where we promote atomics, we know it is not lock-free
18176 // (at least not in the sense of atomic_is_lock_free). Otherwise,
18177 // the answer can only be determined at runtime; for example, 16-byte
18178 // atomics have lock-free implementations on some, but not all,
18179 // x86-64 processors.
18180
18181 // Check power-of-two.
18182 CharUnits Size = CharUnits::fromQuantity(Quantity: SizeVal.getZExtValue());
18183 if (Size.isPowerOfTwo()) {
18184 // Check against inlining width.
18185 unsigned InlineWidthBits =
18186 Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
18187 if (Size <= Info.Ctx.toCharUnitsFromBits(BitSize: InlineWidthBits)) {
18188 if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||
18189 Size == CharUnits::One())
18190 return Success(Value: 1, E);
18191
18192 // If the pointer argument can be evaluated to a compile-time constant
18193 // integer (or nullptr), check if that value is appropriately aligned.
18194 const Expr *PtrArg = E->getArg(Arg: 1);
18195 Expr::EvalResult ExprResult;
18196 APSInt IntResult;
18197 if (PtrArg->EvaluateAsRValue(Result&: ExprResult, Ctx: Info.Ctx) &&
18198 ExprResult.Val.toIntegralConstant(Result&: IntResult, SrcTy: PtrArg->getType(),
18199 Ctx: Info.Ctx) &&
18200 IntResult.isAligned(A: Size.getAsAlign()))
18201 return Success(Value: 1, E);
18202
18203 // Otherwise, check if the type's alignment against Size.
18204 if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: PtrArg)) {
18205 // Drop the potential implicit-cast to 'const volatile void*', getting
18206 // the underlying type.
18207 if (ICE->getCastKind() == CK_BitCast)
18208 PtrArg = ICE->getSubExpr();
18209 }
18210
18211 if (auto PtrTy = PtrArg->getType()->getAs<PointerType>()) {
18212 QualType PointeeType = PtrTy->getPointeeType();
18213 if (!PointeeType->isIncompleteType() &&
18214 Info.Ctx.getTypeAlignInChars(T: PointeeType) >= Size) {
18215 // OK, we will inline operations on this object.
18216 return Success(Value: 1, E);
18217 }
18218 }
18219 }
18220 }
18221
18222 return BuiltinOp == Builtin::BI__atomic_always_lock_free ?
18223 Success(Value: 0, E) : Error(E);
18224 }
18225 case Builtin::BI__builtin_addcb:
18226 case Builtin::BI__builtin_addcs:
18227 case Builtin::BI__builtin_addc:
18228 case Builtin::BI__builtin_addcl:
18229 case Builtin::BI__builtin_addcll:
18230 case Builtin::BI__builtin_subcb:
18231 case Builtin::BI__builtin_subcs:
18232 case Builtin::BI__builtin_subc:
18233 case Builtin::BI__builtin_subcl:
18234 case Builtin::BI__builtin_subcll: {
18235 LValue CarryOutLValue;
18236 APSInt LHS, RHS, CarryIn, CarryOut, Result;
18237 QualType ResultType = E->getArg(Arg: 0)->getType();
18238 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18239 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18240 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: CarryIn, Info) ||
18241 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: CarryOutLValue, Info))
18242 return false;
18243 // Copy the number of bits and sign.
18244 Result = LHS;
18245 CarryOut = LHS;
18246
18247 bool FirstOverflowed = false;
18248 bool SecondOverflowed = false;
18249 switch (BuiltinOp) {
18250 default:
18251 llvm_unreachable("Invalid value for BuiltinOp");
18252 case Builtin::BI__builtin_addcb:
18253 case Builtin::BI__builtin_addcs:
18254 case Builtin::BI__builtin_addc:
18255 case Builtin::BI__builtin_addcl:
18256 case Builtin::BI__builtin_addcll:
18257 Result =
18258 LHS.uadd_ov(RHS, Overflow&: FirstOverflowed).uadd_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
18259 break;
18260 case Builtin::BI__builtin_subcb:
18261 case Builtin::BI__builtin_subcs:
18262 case Builtin::BI__builtin_subc:
18263 case Builtin::BI__builtin_subcl:
18264 case Builtin::BI__builtin_subcll:
18265 Result =
18266 LHS.usub_ov(RHS, Overflow&: FirstOverflowed).usub_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
18267 break;
18268 }
18269
18270 // It is possible for both overflows to happen but CGBuiltin uses an OR so
18271 // this is consistent.
18272 CarryOut = (uint64_t)(FirstOverflowed | SecondOverflowed);
18273 APValue APV{CarryOut};
18274 if (!handleAssignment(Info, E, LVal: CarryOutLValue, LValType: ResultType, Val&: APV))
18275 return false;
18276 return Success(SI: Result, E);
18277 }
18278 case Builtin::BI__builtin_add_overflow:
18279 case Builtin::BI__builtin_sub_overflow:
18280 case Builtin::BI__builtin_mul_overflow:
18281 case Builtin::BI__builtin_sadd_overflow:
18282 case Builtin::BI__builtin_uadd_overflow:
18283 case Builtin::BI__builtin_uaddl_overflow:
18284 case Builtin::BI__builtin_uaddll_overflow:
18285 case Builtin::BI__builtin_usub_overflow:
18286 case Builtin::BI__builtin_usubl_overflow:
18287 case Builtin::BI__builtin_usubll_overflow:
18288 case Builtin::BI__builtin_umul_overflow:
18289 case Builtin::BI__builtin_umull_overflow:
18290 case Builtin::BI__builtin_umulll_overflow:
18291 case Builtin::BI__builtin_saddl_overflow:
18292 case Builtin::BI__builtin_saddll_overflow:
18293 case Builtin::BI__builtin_ssub_overflow:
18294 case Builtin::BI__builtin_ssubl_overflow:
18295 case Builtin::BI__builtin_ssubll_overflow:
18296 case Builtin::BI__builtin_smul_overflow:
18297 case Builtin::BI__builtin_smull_overflow:
18298 case Builtin::BI__builtin_smulll_overflow: {
18299 LValue ResultLValue;
18300 APSInt LHS, RHS;
18301
18302 QualType ResultType = E->getArg(Arg: 2)->getType()->getPointeeType();
18303 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18304 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18305 !EvaluatePointer(E: E->getArg(Arg: 2), Result&: ResultLValue, Info))
18306 return false;
18307
18308 APSInt Result;
18309 bool DidOverflow = false;
18310
18311 // If the types don't have to match, enlarge all 3 to the largest of them.
18312 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
18313 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
18314 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
18315 bool IsSigned = LHS.isSigned() || RHS.isSigned() ||
18316 ResultType->isSignedIntegerOrEnumerationType();
18317 bool AllSigned = LHS.isSigned() && RHS.isSigned() &&
18318 ResultType->isSignedIntegerOrEnumerationType();
18319 uint64_t LHSSize = LHS.getBitWidth();
18320 uint64_t RHSSize = RHS.getBitWidth();
18321 uint64_t ResultSize = Info.Ctx.getIntWidth(T: ResultType);
18322 uint64_t MaxBits = std::max(a: std::max(a: LHSSize, b: RHSSize), b: ResultSize);
18323
18324 // Add an additional bit if the signedness isn't uniformly agreed to. We
18325 // could do this ONLY if there is a signed and an unsigned that both have
18326 // MaxBits, but the code to check that is pretty nasty. The issue will be
18327 // caught in the shrink-to-result later anyway.
18328 if (IsSigned && !AllSigned)
18329 ++MaxBits;
18330
18331 LHS = APSInt(LHS.extOrTrunc(width: MaxBits), !IsSigned);
18332 RHS = APSInt(RHS.extOrTrunc(width: MaxBits), !IsSigned);
18333 Result = APSInt(MaxBits, !IsSigned);
18334 }
18335
18336 // Find largest int.
18337 switch (BuiltinOp) {
18338 default:
18339 llvm_unreachable("Invalid value for BuiltinOp");
18340 case Builtin::BI__builtin_add_overflow:
18341 case Builtin::BI__builtin_sadd_overflow:
18342 case Builtin::BI__builtin_saddl_overflow:
18343 case Builtin::BI__builtin_saddll_overflow:
18344 case Builtin::BI__builtin_uadd_overflow:
18345 case Builtin::BI__builtin_uaddl_overflow:
18346 case Builtin::BI__builtin_uaddll_overflow:
18347 Result = LHS.isSigned() ? LHS.sadd_ov(RHS, Overflow&: DidOverflow)
18348 : LHS.uadd_ov(RHS, Overflow&: DidOverflow);
18349 break;
18350 case Builtin::BI__builtin_sub_overflow:
18351 case Builtin::BI__builtin_ssub_overflow:
18352 case Builtin::BI__builtin_ssubl_overflow:
18353 case Builtin::BI__builtin_ssubll_overflow:
18354 case Builtin::BI__builtin_usub_overflow:
18355 case Builtin::BI__builtin_usubl_overflow:
18356 case Builtin::BI__builtin_usubll_overflow:
18357 Result = LHS.isSigned() ? LHS.ssub_ov(RHS, Overflow&: DidOverflow)
18358 : LHS.usub_ov(RHS, Overflow&: DidOverflow);
18359 break;
18360 case Builtin::BI__builtin_mul_overflow:
18361 case Builtin::BI__builtin_smul_overflow:
18362 case Builtin::BI__builtin_smull_overflow:
18363 case Builtin::BI__builtin_smulll_overflow:
18364 case Builtin::BI__builtin_umul_overflow:
18365 case Builtin::BI__builtin_umull_overflow:
18366 case Builtin::BI__builtin_umulll_overflow:
18367 Result = LHS.isSigned() ? LHS.smul_ov(RHS, Overflow&: DidOverflow)
18368 : LHS.umul_ov(RHS, Overflow&: DidOverflow);
18369 break;
18370 }
18371
18372 // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead,
18373 // since it will give us the behavior of a TruncOrSelf in the case where
18374 // its parameter <= its size. We previously set Result to be at least the
18375 // integer width of the result, so getIntWidth(ResultType) <=
18376 // Result.BitWidth will work exactly like TruncOrSelf.
18377 APSInt Temp = Result.extOrTrunc(width: Info.Ctx.getIntWidth(T: ResultType));
18378 Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType());
18379
18380 // In the case where multiple sizes are allowed, truncate and see if
18381 // the values are the same.
18382 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
18383 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
18384 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
18385 if (!APSInt::isSameValue(I1: Temp, I2: Result))
18386 DidOverflow = true;
18387 }
18388 Result = Temp;
18389
18390 APValue APV{Result};
18391 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
18392 return false;
18393 return Success(Value: DidOverflow, E);
18394 }
18395
18396 case Builtin::BI__builtin_reduce_add:
18397 case Builtin::BI__builtin_reduce_mul:
18398 case Builtin::BI__builtin_reduce_and:
18399 case Builtin::BI__builtin_reduce_or:
18400 case Builtin::BI__builtin_reduce_xor:
18401 case Builtin::BI__builtin_reduce_min:
18402 case Builtin::BI__builtin_reduce_max: {
18403 APValue Source;
18404 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
18405 return false;
18406
18407 unsigned SourceLen = Source.getVectorLength();
18408 APSInt Reduced = Source.getVectorElt(I: 0).getInt();
18409 for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) {
18410 switch (BuiltinOp) {
18411 default:
18412 return false;
18413 case Builtin::BI__builtin_reduce_add: {
18414 if (!CheckedIntArithmetic(
18415 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
18416 BitWidth: Reduced.getBitWidth() + 1, Op: std::plus<APSInt>(), Result&: Reduced))
18417 return false;
18418 break;
18419 }
18420 case Builtin::BI__builtin_reduce_mul: {
18421 if (!CheckedIntArithmetic(
18422 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
18423 BitWidth: Reduced.getBitWidth() * 2, Op: std::multiplies<APSInt>(), Result&: Reduced))
18424 return false;
18425 break;
18426 }
18427 case Builtin::BI__builtin_reduce_and: {
18428 Reduced &= Source.getVectorElt(I: EltNum).getInt();
18429 break;
18430 }
18431 case Builtin::BI__builtin_reduce_or: {
18432 Reduced |= Source.getVectorElt(I: EltNum).getInt();
18433 break;
18434 }
18435 case Builtin::BI__builtin_reduce_xor: {
18436 Reduced ^= Source.getVectorElt(I: EltNum).getInt();
18437 break;
18438 }
18439 case Builtin::BI__builtin_reduce_min: {
18440 Reduced = std::min(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
18441 break;
18442 }
18443 case Builtin::BI__builtin_reduce_max: {
18444 Reduced = std::max(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
18445 break;
18446 }
18447 }
18448 }
18449
18450 return Success(SI: Reduced, E);
18451 }
18452
18453 case clang::X86::BI__builtin_ia32_addcarryx_u32:
18454 case clang::X86::BI__builtin_ia32_addcarryx_u64:
18455 case clang::X86::BI__builtin_ia32_subborrow_u32:
18456 case clang::X86::BI__builtin_ia32_subborrow_u64: {
18457 LValue ResultLValue;
18458 APSInt CarryIn, LHS, RHS;
18459 QualType ResultType = E->getArg(Arg: 3)->getType()->getPointeeType();
18460 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CarryIn, Info) ||
18461 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: LHS, Info) ||
18462 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: RHS, Info) ||
18463 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: ResultLValue, Info))
18464 return false;
18465
18466 bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||
18467 BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;
18468
18469 unsigned BitWidth = LHS.getBitWidth();
18470 unsigned CarryInBit = CarryIn.ugt(RHS: 0) ? 1 : 0;
18471 APInt ExResult =
18472 IsAdd
18473 ? (LHS.zext(width: BitWidth + 1) + (RHS.zext(width: BitWidth + 1) + CarryInBit))
18474 : (LHS.zext(width: BitWidth + 1) - (RHS.zext(width: BitWidth + 1) + CarryInBit));
18475
18476 APInt Result = ExResult.extractBits(numBits: BitWidth, bitPosition: 0);
18477 uint64_t CarryOut = ExResult.extractBitsAsZExtValue(numBits: 1, bitPosition: BitWidth);
18478
18479 APValue APV{APSInt(Result, /*isUnsigned=*/true)};
18480 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
18481 return false;
18482 return Success(Value: CarryOut, E);
18483 }
18484
18485 case clang::X86::BI__builtin_ia32_movmskps:
18486 case clang::X86::BI__builtin_ia32_movmskpd:
18487 case clang::X86::BI__builtin_ia32_pmovmskb128:
18488 case clang::X86::BI__builtin_ia32_pmovmskb256:
18489 case clang::X86::BI__builtin_ia32_movmskps256:
18490 case clang::X86::BI__builtin_ia32_movmskpd256: {
18491 APValue Source;
18492 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
18493 return false;
18494 unsigned SourceLen = Source.getVectorLength();
18495 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
18496 QualType ElemQT = VT->getElementType();
18497 unsigned ResultLen = Info.Ctx.getTypeSize(
18498 T: E->getCallReturnType(Ctx: Info.Ctx)); // Always 32-bit integer.
18499 APInt Result(ResultLen, 0);
18500
18501 for (unsigned I = 0; I != SourceLen; ++I) {
18502 APInt Elem;
18503 if (ElemQT->isIntegerType()) {
18504 Elem = Source.getVectorElt(I).getInt();
18505 } else if (ElemQT->isRealFloatingType()) {
18506 Elem = Source.getVectorElt(I).getFloat().bitcastToAPInt();
18507 } else {
18508 return false;
18509 }
18510 Result.setBitVal(BitPosition: I, BitValue: Elem.isNegative());
18511 }
18512 return Success(I: Result, E);
18513 }
18514
18515 case clang::X86::BI__builtin_ia32_bextr_u32:
18516 case clang::X86::BI__builtin_ia32_bextr_u64:
18517 case clang::X86::BI__builtin_ia32_bextri_u32:
18518 case clang::X86::BI__builtin_ia32_bextri_u64: {
18519 APSInt Val, Idx;
18520 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18521 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
18522 return false;
18523
18524 unsigned BitWidth = Val.getBitWidth();
18525 uint64_t Shift = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
18526 uint64_t Length = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 8);
18527 Length = Length > BitWidth ? BitWidth : Length;
18528
18529 // Handle out of bounds cases.
18530 if (Length == 0 || Shift >= BitWidth)
18531 return Success(Value: 0, E);
18532
18533 uint64_t Result = Val.getZExtValue() >> Shift;
18534 Result &= llvm::maskTrailingOnes<uint64_t>(N: Length);
18535 return Success(Value: Result, E);
18536 }
18537
18538 case clang::X86::BI__builtin_ia32_bzhi_si:
18539 case clang::X86::BI__builtin_ia32_bzhi_di: {
18540 APSInt Val, Idx;
18541 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18542 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
18543 return false;
18544
18545 unsigned BitWidth = Val.getBitWidth();
18546 unsigned Index = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
18547 if (Index < BitWidth)
18548 Val.clearHighBits(hiBits: BitWidth - Index);
18549 return Success(SI: Val, E);
18550 }
18551
18552 case clang::X86::BI__builtin_ia32_ktestcqi:
18553 case clang::X86::BI__builtin_ia32_ktestchi:
18554 case clang::X86::BI__builtin_ia32_ktestcsi:
18555 case clang::X86::BI__builtin_ia32_ktestcdi: {
18556 APSInt A, B;
18557 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18558 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18559 return false;
18560
18561 return Success(Value: (~A & B) == 0, E);
18562 }
18563
18564 case clang::X86::BI__builtin_ia32_ktestzqi:
18565 case clang::X86::BI__builtin_ia32_ktestzhi:
18566 case clang::X86::BI__builtin_ia32_ktestzsi:
18567 case clang::X86::BI__builtin_ia32_ktestzdi: {
18568 APSInt A, B;
18569 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18570 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18571 return false;
18572
18573 return Success(Value: (A & B) == 0, E);
18574 }
18575
18576 case clang::X86::BI__builtin_ia32_kortestcqi:
18577 case clang::X86::BI__builtin_ia32_kortestchi:
18578 case clang::X86::BI__builtin_ia32_kortestcsi:
18579 case clang::X86::BI__builtin_ia32_kortestcdi: {
18580 APSInt A, B;
18581 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18582 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18583 return false;
18584
18585 return Success(Value: ~(A | B) == 0, E);
18586 }
18587
18588 case clang::X86::BI__builtin_ia32_kortestzqi:
18589 case clang::X86::BI__builtin_ia32_kortestzhi:
18590 case clang::X86::BI__builtin_ia32_kortestzsi:
18591 case clang::X86::BI__builtin_ia32_kortestzdi: {
18592 APSInt A, B;
18593 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18594 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18595 return false;
18596
18597 return Success(Value: (A | B) == 0, E);
18598 }
18599
18600 case clang::X86::BI__builtin_ia32_kunpckhi:
18601 case clang::X86::BI__builtin_ia32_kunpckdi:
18602 case clang::X86::BI__builtin_ia32_kunpcksi: {
18603 APSInt A, B;
18604 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18605 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18606 return false;
18607
18608 // Generic kunpack: extract lower half of each operand and concatenate
18609 // Result = A[HalfWidth-1:0] concat B[HalfWidth-1:0]
18610 unsigned BW = A.getBitWidth();
18611 APSInt Result(A.trunc(width: BW / 2).concat(NewLSB: B.trunc(width: BW / 2)), A.isUnsigned());
18612 return Success(SI: Result, E);
18613 }
18614
18615 case clang::X86::BI__builtin_ia32_lzcnt_u16:
18616 case clang::X86::BI__builtin_ia32_lzcnt_u32:
18617 case clang::X86::BI__builtin_ia32_lzcnt_u64: {
18618 APSInt Val;
18619 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18620 return false;
18621 return Success(Value: Val.countLeadingZeros(), E);
18622 }
18623
18624 case clang::X86::BI__builtin_ia32_tzcnt_u16:
18625 case clang::X86::BI__builtin_ia32_tzcnt_u32:
18626 case clang::X86::BI__builtin_ia32_tzcnt_u64: {
18627 APSInt Val;
18628 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18629 return false;
18630 return Success(Value: Val.countTrailingZeros(), E);
18631 }
18632
18633 case Builtin::BI__builtin_elementwise_pdep: {
18634 APSInt Val, Msk;
18635 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18636 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18637 return false;
18638 return Success(I: llvm::APIntOps::pdep(Val, Mask: Msk), E);
18639 }
18640
18641 case Builtin::BI__builtin_elementwise_pext: {
18642 APSInt Val, Msk;
18643 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18644 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18645 return false;
18646 return Success(I: llvm::APIntOps::pext(Val, Mask: Msk), E);
18647 }
18648
18649 case X86::BI__builtin_ia32_ptestz128:
18650 case X86::BI__builtin_ia32_ptestz256:
18651 case X86::BI__builtin_ia32_vtestzps:
18652 case X86::BI__builtin_ia32_vtestzps256:
18653 case X86::BI__builtin_ia32_vtestzpd:
18654 case X86::BI__builtin_ia32_vtestzpd256: {
18655 return EvalTestOp(
18656 [](const APInt &A, const APInt &B) { return (A & B) == 0; });
18657 }
18658 case X86::BI__builtin_ia32_ptestc128:
18659 case X86::BI__builtin_ia32_ptestc256:
18660 case X86::BI__builtin_ia32_vtestcps:
18661 case X86::BI__builtin_ia32_vtestcps256:
18662 case X86::BI__builtin_ia32_vtestcpd:
18663 case X86::BI__builtin_ia32_vtestcpd256: {
18664 return EvalTestOp(
18665 [](const APInt &A, const APInt &B) { return (~A & B) == 0; });
18666 }
18667 case X86::BI__builtin_ia32_ptestnzc128:
18668 case X86::BI__builtin_ia32_ptestnzc256:
18669 case X86::BI__builtin_ia32_vtestnzcps:
18670 case X86::BI__builtin_ia32_vtestnzcps256:
18671 case X86::BI__builtin_ia32_vtestnzcpd:
18672 case X86::BI__builtin_ia32_vtestnzcpd256: {
18673 return EvalTestOp([](const APInt &A, const APInt &B) {
18674 return ((A & B) != 0) && ((~A & B) != 0);
18675 });
18676 }
18677 case X86::BI__builtin_ia32_kandqi:
18678 case X86::BI__builtin_ia32_kandhi:
18679 case X86::BI__builtin_ia32_kandsi:
18680 case X86::BI__builtin_ia32_kanddi: {
18681 return HandleMaskBinOp(
18682 [](const APSInt &LHS, const APSInt &RHS) { return LHS & RHS; });
18683 }
18684
18685 case X86::BI__builtin_ia32_kandnqi:
18686 case X86::BI__builtin_ia32_kandnhi:
18687 case X86::BI__builtin_ia32_kandnsi:
18688 case X86::BI__builtin_ia32_kandndi: {
18689 return HandleMaskBinOp(
18690 [](const APSInt &LHS, const APSInt &RHS) { return ~LHS & RHS; });
18691 }
18692
18693 case X86::BI__builtin_ia32_korqi:
18694 case X86::BI__builtin_ia32_korhi:
18695 case X86::BI__builtin_ia32_korsi:
18696 case X86::BI__builtin_ia32_kordi: {
18697 return HandleMaskBinOp(
18698 [](const APSInt &LHS, const APSInt &RHS) { return LHS | RHS; });
18699 }
18700
18701 case X86::BI__builtin_ia32_kxnorqi:
18702 case X86::BI__builtin_ia32_kxnorhi:
18703 case X86::BI__builtin_ia32_kxnorsi:
18704 case X86::BI__builtin_ia32_kxnordi: {
18705 return HandleMaskBinOp(
18706 [](const APSInt &LHS, const APSInt &RHS) { return ~(LHS ^ RHS); });
18707 }
18708
18709 case X86::BI__builtin_ia32_kxorqi:
18710 case X86::BI__builtin_ia32_kxorhi:
18711 case X86::BI__builtin_ia32_kxorsi:
18712 case X86::BI__builtin_ia32_kxordi: {
18713 return HandleMaskBinOp(
18714 [](const APSInt &LHS, const APSInt &RHS) { return LHS ^ RHS; });
18715 }
18716
18717 case X86::BI__builtin_ia32_knotqi:
18718 case X86::BI__builtin_ia32_knothi:
18719 case X86::BI__builtin_ia32_knotsi:
18720 case X86::BI__builtin_ia32_knotdi: {
18721 APSInt Val;
18722 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18723 return false;
18724 APSInt Result = ~Val;
18725 return Success(V: APValue(Result), E);
18726 }
18727
18728 case X86::BI__builtin_ia32_kaddqi:
18729 case X86::BI__builtin_ia32_kaddhi:
18730 case X86::BI__builtin_ia32_kaddsi:
18731 case X86::BI__builtin_ia32_kadddi: {
18732 return HandleMaskBinOp(
18733 [](const APSInt &LHS, const APSInt &RHS) { return LHS + RHS; });
18734 }
18735
18736 case X86::BI__builtin_ia32_kmovb:
18737 case X86::BI__builtin_ia32_kmovw:
18738 case X86::BI__builtin_ia32_kmovd:
18739 case X86::BI__builtin_ia32_kmovq: {
18740 APSInt Val;
18741 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18742 return false;
18743 return Success(SI: Val, E);
18744 }
18745
18746 case X86::BI__builtin_ia32_kshiftliqi:
18747 case X86::BI__builtin_ia32_kshiftlihi:
18748 case X86::BI__builtin_ia32_kshiftlisi:
18749 case X86::BI__builtin_ia32_kshiftlidi: {
18750 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18751 unsigned Amt = RHS.getZExtValue() & 0xFF;
18752 if (Amt >= LHS.getBitWidth())
18753 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18754 return APSInt(LHS.shl(shiftAmt: Amt), LHS.isUnsigned());
18755 });
18756 }
18757
18758 case X86::BI__builtin_ia32_kshiftriqi:
18759 case X86::BI__builtin_ia32_kshiftrihi:
18760 case X86::BI__builtin_ia32_kshiftrisi:
18761 case X86::BI__builtin_ia32_kshiftridi: {
18762 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18763 unsigned Amt = RHS.getZExtValue() & 0xFF;
18764 if (Amt >= LHS.getBitWidth())
18765 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18766 return APSInt(LHS.lshr(shiftAmt: Amt), LHS.isUnsigned());
18767 });
18768 }
18769
18770 case clang::X86::BI__builtin_ia32_vec_ext_v4hi:
18771 case clang::X86::BI__builtin_ia32_vec_ext_v16qi:
18772 case clang::X86::BI__builtin_ia32_vec_ext_v8hi:
18773 case clang::X86::BI__builtin_ia32_vec_ext_v4si:
18774 case clang::X86::BI__builtin_ia32_vec_ext_v2di:
18775 case clang::X86::BI__builtin_ia32_vec_ext_v32qi:
18776 case clang::X86::BI__builtin_ia32_vec_ext_v16hi:
18777 case clang::X86::BI__builtin_ia32_vec_ext_v8si:
18778 case clang::X86::BI__builtin_ia32_vec_ext_v4di: {
18779 APValue Vec;
18780 APSInt IdxAPS;
18781 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
18782 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
18783 return false;
18784 unsigned N = Vec.getVectorLength();
18785 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
18786 return Success(SI: Vec.getVectorElt(I: Idx).getInt(), E);
18787 }
18788
18789 case clang::X86::BI__builtin_ia32_cvtb2mask128:
18790 case clang::X86::BI__builtin_ia32_cvtb2mask256:
18791 case clang::X86::BI__builtin_ia32_cvtb2mask512:
18792 case clang::X86::BI__builtin_ia32_cvtw2mask128:
18793 case clang::X86::BI__builtin_ia32_cvtw2mask256:
18794 case clang::X86::BI__builtin_ia32_cvtw2mask512:
18795 case clang::X86::BI__builtin_ia32_cvtd2mask128:
18796 case clang::X86::BI__builtin_ia32_cvtd2mask256:
18797 case clang::X86::BI__builtin_ia32_cvtd2mask512:
18798 case clang::X86::BI__builtin_ia32_cvtq2mask128:
18799 case clang::X86::BI__builtin_ia32_cvtq2mask256:
18800 case clang::X86::BI__builtin_ia32_cvtq2mask512: {
18801 assert(E->getNumArgs() == 1);
18802 APValue Vec;
18803 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
18804 return false;
18805
18806 unsigned VectorLen = Vec.getVectorLength();
18807 unsigned RetWidth = Info.Ctx.getIntWidth(T: E->getType());
18808 llvm::APInt Bits(RetWidth, 0);
18809
18810 for (unsigned ElemNum = 0; ElemNum != VectorLen; ++ElemNum) {
18811 const APSInt &A = Vec.getVectorElt(I: ElemNum).getInt();
18812 unsigned MSB = A[A.getBitWidth() - 1];
18813 Bits.setBitVal(BitPosition: ElemNum, BitValue: MSB);
18814 }
18815
18816 APSInt RetMask(Bits, /*isUnsigned=*/true);
18817 return Success(V: APValue(RetMask), E);
18818 }
18819
18820 case clang::X86::BI__builtin_ia32_cmpb128_mask:
18821 case clang::X86::BI__builtin_ia32_cmpw128_mask:
18822 case clang::X86::BI__builtin_ia32_cmpd128_mask:
18823 case clang::X86::BI__builtin_ia32_cmpq128_mask:
18824 case clang::X86::BI__builtin_ia32_cmpb256_mask:
18825 case clang::X86::BI__builtin_ia32_cmpw256_mask:
18826 case clang::X86::BI__builtin_ia32_cmpd256_mask:
18827 case clang::X86::BI__builtin_ia32_cmpq256_mask:
18828 case clang::X86::BI__builtin_ia32_cmpb512_mask:
18829 case clang::X86::BI__builtin_ia32_cmpw512_mask:
18830 case clang::X86::BI__builtin_ia32_cmpd512_mask:
18831 case clang::X86::BI__builtin_ia32_cmpq512_mask:
18832 case clang::X86::BI__builtin_ia32_ucmpb128_mask:
18833 case clang::X86::BI__builtin_ia32_ucmpw128_mask:
18834 case clang::X86::BI__builtin_ia32_ucmpd128_mask:
18835 case clang::X86::BI__builtin_ia32_ucmpq128_mask:
18836 case clang::X86::BI__builtin_ia32_ucmpb256_mask:
18837 case clang::X86::BI__builtin_ia32_ucmpw256_mask:
18838 case clang::X86::BI__builtin_ia32_ucmpd256_mask:
18839 case clang::X86::BI__builtin_ia32_ucmpq256_mask:
18840 case clang::X86::BI__builtin_ia32_ucmpb512_mask:
18841 case clang::X86::BI__builtin_ia32_ucmpw512_mask:
18842 case clang::X86::BI__builtin_ia32_ucmpd512_mask:
18843 case clang::X86::BI__builtin_ia32_ucmpq512_mask: {
18844 assert(E->getNumArgs() == 4);
18845
18846 bool IsUnsigned =
18847 (BuiltinOp >= clang::X86::BI__builtin_ia32_ucmpb128_mask &&
18848 BuiltinOp <= clang::X86::BI__builtin_ia32_ucmpw512_mask);
18849
18850 APValue LHS, RHS;
18851 APSInt Mask, Opcode;
18852 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18853 !EvaluateVector(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18854 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Opcode, Info) ||
18855 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: Mask, Info))
18856 return false;
18857
18858 assert(LHS.getVectorLength() == RHS.getVectorLength());
18859
18860 unsigned VectorLen = LHS.getVectorLength();
18861 unsigned RetWidth = Mask.getBitWidth();
18862
18863 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
18864
18865 for (unsigned ElemNum = 0; ElemNum < VectorLen; ++ElemNum) {
18866 const APSInt &A = LHS.getVectorElt(I: ElemNum).getInt();
18867 const APSInt &B = RHS.getVectorElt(I: ElemNum).getInt();
18868 bool Result = false;
18869
18870 switch (Opcode.getExtValue() & 0x7) {
18871 case 0: // _MM_CMPINT_EQ
18872 Result = (A == B);
18873 break;
18874 case 1: // _MM_CMPINT_LT
18875 Result = IsUnsigned ? A.ult(RHS: B) : A.slt(RHS: B);
18876 break;
18877 case 2: // _MM_CMPINT_LE
18878 Result = IsUnsigned ? A.ule(RHS: B) : A.sle(RHS: B);
18879 break;
18880 case 3: // _MM_CMPINT_FALSE
18881 Result = false;
18882 break;
18883 case 4: // _MM_CMPINT_NE
18884 Result = (A != B);
18885 break;
18886 case 5: // _MM_CMPINT_NLT (>=)
18887 Result = IsUnsigned ? A.uge(RHS: B) : A.sge(RHS: B);
18888 break;
18889 case 6: // _MM_CMPINT_NLE (>)
18890 Result = IsUnsigned ? A.ugt(RHS: B) : A.sgt(RHS: B);
18891 break;
18892 case 7: // _MM_CMPINT_TRUE
18893 Result = true;
18894 break;
18895 }
18896
18897 RetMask.setBitVal(BitPosition: ElemNum, BitValue: Mask[ElemNum] && Result);
18898 }
18899
18900 return Success(V: APValue(RetMask), E);
18901 }
18902 case X86::BI__builtin_ia32_cvtss2si:
18903 case X86::BI__builtin_ia32_cvtsd2si:
18904 case X86::BI__builtin_ia32_cvttss2si:
18905 case X86::BI__builtin_ia32_cvttsd2si:
18906 case X86::BI__builtin_ia32_cvtss2si64:
18907 case X86::BI__builtin_ia32_cvtsd2si64:
18908 case X86::BI__builtin_ia32_cvttss2si64:
18909 case X86::BI__builtin_ia32_cvttsd2si64: {
18910 APValue ArgVal;
18911 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: ArgVal))
18912 return false;
18913
18914 assert(ArgVal.isVector() && "Expected a vector argument");
18915 llvm::APFloat FloatElem = ArgVal.getVectorElt(I: 0).getFloat();
18916 unsigned BitWidth = Info.Ctx.getIntWidth(T: E->getType());
18917 bool isUnsigned = E->getType()->isUnsignedIntegerType();
18918
18919 llvm::APSInt IntResult(BitWidth, isUnsigned);
18920 bool IsExact = false;
18921 // We only allow exact conversions so rounding mode does not matter for cvt*
18922 // and cvtt* builtins
18923 FloatElem.convertToInteger(Result&: IntResult, RM: llvm::APFloat::rmTowardZero,
18924 IsExact: &IsExact);
18925 if (!IsExact)
18926 return false;
18927
18928 return Success(SI: IntResult, E);
18929 }
18930 case X86::BI__builtin_ia32_vpshufbitqmb128_mask:
18931 case X86::BI__builtin_ia32_vpshufbitqmb256_mask:
18932 case X86::BI__builtin_ia32_vpshufbitqmb512_mask: {
18933 assert(E->getNumArgs() == 3);
18934
18935 APValue Source, ShuffleMask;
18936 APSInt ZeroMask;
18937 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
18938 !EvaluateVector(E: E->getArg(Arg: 1), Result&: ShuffleMask, Info) ||
18939 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: ZeroMask, Info))
18940 return false;
18941
18942 assert(Source.getVectorLength() == ShuffleMask.getVectorLength());
18943 assert(ZeroMask.getBitWidth() == Source.getVectorLength());
18944
18945 unsigned NumBytesInQWord = 8;
18946 unsigned NumBitsInByte = 8;
18947 unsigned NumBytes = Source.getVectorLength();
18948 unsigned NumQWords = NumBytes / NumBytesInQWord;
18949 unsigned RetWidth = ZeroMask.getBitWidth();
18950 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
18951
18952 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
18953 APInt SourceQWord(64, 0);
18954 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
18955 uint64_t Byte = Source.getVectorElt(I: QWordId * NumBytesInQWord + ByteIdx)
18956 .getInt()
18957 .getZExtValue();
18958 SourceQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
18959 }
18960
18961 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
18962 unsigned SelIdx = QWordId * NumBytesInQWord + ByteIdx;
18963 unsigned M =
18964 ShuffleMask.getVectorElt(I: SelIdx).getInt().getZExtValue() & 0x3F;
18965 if (ZeroMask[SelIdx]) {
18966 RetMask.setBitVal(BitPosition: SelIdx, BitValue: SourceQWord[M]);
18967 }
18968 }
18969 }
18970 return Success(V: APValue(RetMask), E);
18971 }
18972 }
18973}
18974
18975/// Determine whether this is a pointer past the end of the complete
18976/// object referred to by the lvalue.
18977static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx,
18978 const LValue &LV) {
18979 // A null pointer can be viewed as being "past the end" but we don't
18980 // choose to look at it that way here.
18981 if (!LV.getLValueBase())
18982 return false;
18983
18984 // If the designator is valid and refers to a subobject, we're not pointing
18985 // past the end.
18986 if (!LV.getLValueDesignator().Invalid &&
18987 !LV.getLValueDesignator().isOnePastTheEnd())
18988 return false;
18989
18990 // A pointer to an incomplete type might be past-the-end if the type's size is
18991 // zero. We cannot tell because the type is incomplete.
18992 QualType Ty = getType(B: LV.getLValueBase());
18993 if (Ty->isIncompleteType())
18994 return true;
18995
18996 // Can't be past the end of an invalid object.
18997 if (LV.getLValueDesignator().Invalid)
18998 return false;
18999
19000 // We're a past-the-end pointer if we point to the byte after the object,
19001 // no matter what our type or path is.
19002 auto Size = Ctx.getTypeSizeInChars(T: Ty);
19003 return LV.getLValueOffset() == Size;
19004}
19005
19006namespace {
19007
19008/// Data recursive integer evaluator of certain binary operators.
19009///
19010/// We use a data recursive algorithm for binary operators so that we are able
19011/// to handle extreme cases of chained binary operators without causing stack
19012/// overflow.
19013class DataRecursiveIntBinOpEvaluator {
19014 struct EvalResult {
19015 APValue Val;
19016 bool Failed = false;
19017
19018 EvalResult() = default;
19019
19020 void swap(EvalResult &RHS) {
19021 Val.swap(RHS&: RHS.Val);
19022 Failed = RHS.Failed;
19023 RHS.Failed = false;
19024 }
19025 };
19026
19027 struct Job {
19028 const Expr *E;
19029 EvalResult LHSResult; // meaningful only for binary operator expression.
19030 enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind;
19031
19032 Job() = default;
19033 Job(Job &&) = default;
19034
19035 void startSpeculativeEval(EvalInfo &Info) {
19036 SpecEvalRAII = SpeculativeEvaluationRAII(Info);
19037 }
19038
19039 private:
19040 SpeculativeEvaluationRAII SpecEvalRAII;
19041 };
19042
19043 SmallVector<Job, 16> Queue;
19044
19045 IntExprEvaluator &IntEval;
19046 EvalInfo &Info;
19047 APValue &FinalResult;
19048
19049public:
19050 DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result)
19051 : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { }
19052
19053 /// True if \param E is a binary operator that we are going to handle
19054 /// data recursively.
19055 /// We handle binary operators that are comma, logical, or that have operands
19056 /// with integral or enumeration type.
19057 static bool shouldEnqueue(const BinaryOperator *E) {
19058 return E->getOpcode() == BO_Comma || E->isLogicalOp() ||
19059 (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() &&
19060 E->getLHS()->getType()->isIntegralOrEnumerationType() &&
19061 E->getRHS()->getType()->isIntegralOrEnumerationType());
19062 }
19063
19064 bool Traverse(const BinaryOperator *E) {
19065 enqueue(E);
19066 EvalResult PrevResult;
19067 while (!Queue.empty())
19068 process(Result&: PrevResult);
19069
19070 if (PrevResult.Failed) return false;
19071
19072 FinalResult.swap(RHS&: PrevResult.Val);
19073 return true;
19074 }
19075
19076private:
19077 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
19078 return IntEval.Success(Value, E, Result);
19079 }
19080 bool Success(const APSInt &Value, const Expr *E, APValue &Result) {
19081 return IntEval.Success(SI: Value, E, Result);
19082 }
19083 bool Error(const Expr *E) {
19084 return IntEval.Error(E);
19085 }
19086 bool Error(const Expr *E, diag::kind D) {
19087 return IntEval.Error(E, D);
19088 }
19089
19090 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
19091 return Info.CCEDiag(E, DiagId: D);
19092 }
19093
19094 // Returns true if visiting the RHS is necessary, false otherwise.
19095 bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
19096 bool &SuppressRHSDiags);
19097
19098 bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
19099 const BinaryOperator *E, APValue &Result);
19100
19101 void EvaluateExpr(const Expr *E, EvalResult &Result) {
19102 Result.Failed = !Evaluate(Result&: Result.Val, Info, E);
19103 if (Result.Failed)
19104 Result.Val = APValue();
19105 }
19106
19107 void process(EvalResult &Result);
19108
19109 void enqueue(const Expr *E) {
19110 E = E->IgnoreParens();
19111 Queue.resize(N: Queue.size()+1);
19112 Queue.back().E = E;
19113 Queue.back().Kind = Job::AnyExprKind;
19114 }
19115};
19116
19117}
19118
19119bool DataRecursiveIntBinOpEvaluator::
19120 VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
19121 bool &SuppressRHSDiags) {
19122 if (E->getOpcode() == BO_Comma) {
19123 // Ignore LHS but note if we could not evaluate it.
19124 if (LHSResult.Failed)
19125 return Info.noteSideEffect();
19126 return true;
19127 }
19128
19129 if (E->isLogicalOp()) {
19130 bool LHSAsBool;
19131 if (!LHSResult.Failed && HandleConversionToBool(Val: LHSResult.Val, Result&: LHSAsBool)) {
19132 // We were able to evaluate the LHS, see if we can get away with not
19133 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
19134 if (LHSAsBool == (E->getOpcode() == BO_LOr)) {
19135 Success(Value: LHSAsBool, E, Result&: LHSResult.Val);
19136 return false; // Ignore RHS
19137 }
19138 } else {
19139 LHSResult.Failed = true;
19140
19141 // Since we weren't able to evaluate the left hand side, it
19142 // might have had side effects.
19143 if (!Info.noteSideEffect())
19144 return false;
19145
19146 // We can't evaluate the LHS; however, sometimes the result
19147 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
19148 // Don't ignore RHS and suppress diagnostics from this arm.
19149 SuppressRHSDiags = true;
19150 }
19151
19152 return true;
19153 }
19154
19155 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
19156 E->getRHS()->getType()->isIntegralOrEnumerationType());
19157
19158 if (LHSResult.Failed && !Info.noteFailure())
19159 return false; // Ignore RHS;
19160
19161 return true;
19162}
19163
19164static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index,
19165 bool IsSub) {
19166 // Compute the new offset in the appropriate width, wrapping at 64 bits.
19167 // FIXME: When compiling for a 32-bit target, we should use 32-bit
19168 // offsets.
19169 assert(!LVal.hasLValuePath() && "have designator for integer lvalue");
19170 CharUnits &Offset = LVal.getLValueOffset();
19171 uint64_t Offset64 = Offset.getQuantity();
19172 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
19173 Offset = CharUnits::fromQuantity(Quantity: IsSub ? Offset64 - Index64
19174 : Offset64 + Index64);
19175}
19176
19177bool DataRecursiveIntBinOpEvaluator::
19178 VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
19179 const BinaryOperator *E, APValue &Result) {
19180 if (E->getOpcode() == BO_Comma) {
19181 if (RHSResult.Failed)
19182 return false;
19183 Result = RHSResult.Val;
19184 return true;
19185 }
19186
19187 if (E->isLogicalOp()) {
19188 bool lhsResult, rhsResult;
19189 bool LHSIsOK = HandleConversionToBool(Val: LHSResult.Val, Result&: lhsResult);
19190 bool RHSIsOK = HandleConversionToBool(Val: RHSResult.Val, Result&: rhsResult);
19191
19192 if (LHSIsOK) {
19193 if (RHSIsOK) {
19194 if (E->getOpcode() == BO_LOr)
19195 return Success(Value: lhsResult || rhsResult, E, Result);
19196 else
19197 return Success(Value: lhsResult && rhsResult, E, Result);
19198 }
19199 } else {
19200 if (RHSIsOK) {
19201 // We can't evaluate the LHS; however, sometimes the result
19202 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
19203 if (rhsResult == (E->getOpcode() == BO_LOr))
19204 return Success(Value: rhsResult, E, Result);
19205 }
19206 }
19207
19208 return false;
19209 }
19210
19211 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
19212 E->getRHS()->getType()->isIntegralOrEnumerationType());
19213
19214 if (LHSResult.Failed || RHSResult.Failed)
19215 return false;
19216
19217 const APValue &LHSVal = LHSResult.Val;
19218 const APValue &RHSVal = RHSResult.Val;
19219
19220 // Handle cases like (unsigned long)&a + 4.
19221 if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
19222 Result = LHSVal;
19223 addOrSubLValueAsInteger(LVal&: Result, Index: RHSVal.getInt(), IsSub: E->getOpcode() == BO_Sub);
19224 return true;
19225 }
19226
19227 // Handle cases like 4 + (unsigned long)&a
19228 if (E->getOpcode() == BO_Add &&
19229 RHSVal.isLValue() && LHSVal.isInt()) {
19230 Result = RHSVal;
19231 addOrSubLValueAsInteger(LVal&: Result, Index: LHSVal.getInt(), /*IsSub*/false);
19232 return true;
19233 }
19234
19235 if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
19236 // Handle (intptr_t)&&A - (intptr_t)&&B.
19237 if (!LHSVal.getLValueOffset().isZero() ||
19238 !RHSVal.getLValueOffset().isZero())
19239 return false;
19240 const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
19241 const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
19242 if (!LHSExpr || !RHSExpr)
19243 return false;
19244 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
19245 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
19246 if (!LHSAddrExpr || !RHSAddrExpr)
19247 return false;
19248 // Make sure both labels come from the same function.
19249 if (LHSAddrExpr->getLabel()->getDeclContext() !=
19250 RHSAddrExpr->getLabel()->getDeclContext())
19251 return false;
19252 Result = APValue(LHSAddrExpr, RHSAddrExpr);
19253 return true;
19254 }
19255
19256 // All the remaining cases expect both operands to be an integer
19257 if (!LHSVal.isInt() || !RHSVal.isInt())
19258 return Error(E);
19259
19260 // Set up the width and signedness manually, in case it can't be deduced
19261 // from the operation we're performing.
19262 // FIXME: Don't do this in the cases where we can deduce it.
19263 APSInt Value(Info.Ctx.getIntWidth(T: E->getType()),
19264 E->getType()->isUnsignedIntegerOrEnumerationType());
19265 if (!handleIntIntBinOp(Info, E, LHS: LHSVal.getInt(), Opcode: E->getOpcode(),
19266 RHS: RHSVal.getInt(), Result&: Value))
19267 return false;
19268 return Success(Value, E, Result);
19269}
19270
19271void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) {
19272 Job &job = Queue.back();
19273
19274 switch (job.Kind) {
19275 case Job::AnyExprKind: {
19276 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(Val: job.E)) {
19277 if (shouldEnqueue(E: Bop)) {
19278 job.Kind = Job::BinOpKind;
19279 enqueue(E: Bop->getLHS());
19280 return;
19281 }
19282 }
19283
19284 EvaluateExpr(E: job.E, Result);
19285 Queue.pop_back();
19286 return;
19287 }
19288
19289 case Job::BinOpKind: {
19290 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
19291 bool SuppressRHSDiags = false;
19292 if (!VisitBinOpLHSOnly(LHSResult&: Result, E: Bop, SuppressRHSDiags)) {
19293 Queue.pop_back();
19294 return;
19295 }
19296 if (SuppressRHSDiags)
19297 job.startSpeculativeEval(Info);
19298 job.LHSResult.swap(RHS&: Result);
19299 job.Kind = Job::BinOpVisitedLHSKind;
19300 enqueue(E: Bop->getRHS());
19301 return;
19302 }
19303
19304 case Job::BinOpVisitedLHSKind: {
19305 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
19306 EvalResult RHS;
19307 RHS.swap(RHS&: Result);
19308 Result.Failed = !VisitBinOp(LHSResult: job.LHSResult, RHSResult: RHS, E: Bop, Result&: Result.Val);
19309 Queue.pop_back();
19310 return;
19311 }
19312 }
19313
19314 llvm_unreachable("Invalid Job::Kind!");
19315}
19316
19317namespace {
19318enum class CmpResult {
19319 Unequal,
19320 Less,
19321 Equal,
19322 Greater,
19323 Unordered,
19324};
19325}
19326
19327template <class SuccessCB, class AfterCB>
19328static bool
19329EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
19330 SuccessCB &&Success, AfterCB &&DoAfter) {
19331 assert(!E->isValueDependent());
19332 assert(E->isComparisonOp() && "expected comparison operator");
19333 assert((E->getOpcode() == BO_Cmp ||
19334 E->getType()->isIntegralOrEnumerationType()) &&
19335 "unsupported binary expression evaluation");
19336 auto Error = [&](const Expr *E) {
19337 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
19338 return false;
19339 };
19340
19341 bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp;
19342 bool IsEquality = E->isEqualityOp();
19343
19344 QualType LHSTy = E->getLHS()->getType();
19345 QualType RHSTy = E->getRHS()->getType();
19346
19347 if (LHSTy->isIntegralOrEnumerationType() &&
19348 RHSTy->isIntegralOrEnumerationType()) {
19349 APSInt LHS, RHS;
19350 bool LHSOK = EvaluateInteger(E: E->getLHS(), Result&: LHS, Info);
19351 if (!LHSOK && !Info.noteFailure())
19352 return false;
19353 if (!EvaluateInteger(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
19354 return false;
19355 if (LHS < RHS)
19356 return Success(CmpResult::Less, E);
19357 if (LHS > RHS)
19358 return Success(CmpResult::Greater, E);
19359 return Success(CmpResult::Equal, E);
19360 }
19361
19362 if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) {
19363 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHSTy));
19364 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHSTy));
19365
19366 bool LHSOK = EvaluateFixedPointOrInteger(E: E->getLHS(), Result&: LHSFX, Info);
19367 if (!LHSOK && !Info.noteFailure())
19368 return false;
19369 if (!EvaluateFixedPointOrInteger(E: E->getRHS(), Result&: RHSFX, Info) || !LHSOK)
19370 return false;
19371 if (LHSFX < RHSFX)
19372 return Success(CmpResult::Less, E);
19373 if (LHSFX > RHSFX)
19374 return Success(CmpResult::Greater, E);
19375 return Success(CmpResult::Equal, E);
19376 }
19377
19378 if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) {
19379 ComplexValue LHS, RHS;
19380 bool LHSOK;
19381 if (E->isAssignmentOp()) {
19382 LValue LV;
19383 EvaluateLValue(E: E->getLHS(), Result&: LV, Info);
19384 LHSOK = false;
19385 } else if (LHSTy->isRealFloatingType()) {
19386 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: LHS.FloatReal, Info);
19387 if (LHSOK) {
19388 LHS.makeComplexFloat();
19389 LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics());
19390 }
19391 } else {
19392 LHSOK = EvaluateComplex(E: E->getLHS(), Res&: LHS, Info);
19393 }
19394 if (!LHSOK && !Info.noteFailure())
19395 return false;
19396
19397 if (E->getRHS()->getType()->isRealFloatingType()) {
19398 if (!EvaluateFloat(E: E->getRHS(), Result&: RHS.FloatReal, Info) || !LHSOK)
19399 return false;
19400 RHS.makeComplexFloat();
19401 RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics());
19402 } else if (!EvaluateComplex(E: E->getRHS(), Res&: RHS, Info) || !LHSOK)
19403 return false;
19404
19405 if (LHS.isComplexFloat()) {
19406 APFloat::cmpResult CR_r =
19407 LHS.getComplexFloatReal().compare(RHS: RHS.getComplexFloatReal());
19408 APFloat::cmpResult CR_i =
19409 LHS.getComplexFloatImag().compare(RHS: RHS.getComplexFloatImag());
19410 bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual;
19411 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
19412 } else {
19413 assert(IsEquality && "invalid complex comparison");
19414 bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
19415 LHS.getComplexIntImag() == RHS.getComplexIntImag();
19416 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
19417 }
19418 }
19419
19420 if (LHSTy->isRealFloatingType() &&
19421 RHSTy->isRealFloatingType()) {
19422 APFloat RHS(0.0), LHS(0.0);
19423
19424 bool LHSOK = EvaluateFloat(E: E->getRHS(), Result&: RHS, Info);
19425 if (!LHSOK && !Info.noteFailure())
19426 return false;
19427
19428 if (!EvaluateFloat(E: E->getLHS(), Result&: LHS, Info) || !LHSOK)
19429 return false;
19430
19431 assert(E->isComparisonOp() && "Invalid binary operator!");
19432 llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS);
19433 if (!Info.InConstantContext &&
19434 APFloatCmpResult == APFloat::cmpUnordered &&
19435 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained()) {
19436 // Note: Compares may raise invalid in some cases involving NaN or sNaN.
19437 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
19438 return false;
19439 }
19440 auto GetCmpRes = [&]() {
19441 switch (APFloatCmpResult) {
19442 case APFloat::cmpEqual:
19443 return CmpResult::Equal;
19444 case APFloat::cmpLessThan:
19445 return CmpResult::Less;
19446 case APFloat::cmpGreaterThan:
19447 return CmpResult::Greater;
19448 case APFloat::cmpUnordered:
19449 return CmpResult::Unordered;
19450 }
19451 llvm_unreachable("Unrecognised APFloat::cmpResult enum");
19452 };
19453 return Success(GetCmpRes(), E);
19454 }
19455
19456 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
19457 LValue LHSValue, RHSValue;
19458
19459 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
19460 if (!LHSOK && !Info.noteFailure())
19461 return false;
19462
19463 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19464 return false;
19465
19466 // Reject differing bases from the normal codepath; we special-case
19467 // comparisons to null.
19468 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
19469 // Bail out early if we're checking potential constant expression.
19470 // Otherwise, prefer to diagnose other issues.
19471 if (Info.checkingPotentialConstantExpression() &&
19472 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
19473 return false;
19474 auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) {
19475 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
19476 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
19477 Info.FFDiag(E, DiagId: DiagID)
19478 << (Reversed ? RHS : LHS) << (Reversed ? LHS : RHS);
19479 return false;
19480 };
19481 // Inequalities and subtractions between unrelated pointers have
19482 // unspecified or undefined behavior.
19483 if (!IsEquality)
19484 return DiagComparison(
19485 diag::note_constexpr_pointer_comparison_unspecified);
19486 // A constant address may compare equal to the address of a symbol.
19487 // The one exception is that address of an object cannot compare equal
19488 // to a null pointer constant.
19489 // TODO: Should we restrict this to actual null pointers, and exclude the
19490 // case of zero cast to pointer type?
19491 if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
19492 (!RHSValue.Base && !RHSValue.Offset.isZero()))
19493 return DiagComparison(diag::note_constexpr_pointer_constant_comparison,
19494 !RHSValue.Base);
19495 // C++2c [intro.object]/10:
19496 // Two objects [...] may have the same address if [...] they are both
19497 // potentially non-unique objects.
19498 // C++2c [intro.object]/9:
19499 // An object is potentially non-unique if it is a string literal object,
19500 // the backing array of an initializer list, or a subobject thereof.
19501 //
19502 // This makes the comparison result unspecified, so it's not a constant
19503 // expression.
19504 //
19505 // TODO: Do we need to handle the initializer list case here?
19506 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
19507 return DiagComparison(diag::note_constexpr_literal_comparison);
19508 if (IsOpaqueConstantCall(LVal: LHSValue) || IsOpaqueConstantCall(LVal: RHSValue))
19509 return DiagComparison(diag::note_constexpr_opaque_call_comparison,
19510 !IsOpaqueConstantCall(LVal: LHSValue));
19511 // We can't tell whether weak symbols will end up pointing to the same
19512 // object.
19513 if (IsWeakLValue(Value: LHSValue) || IsWeakLValue(Value: RHSValue))
19514 return DiagComparison(diag::note_constexpr_pointer_weak_comparison,
19515 !IsWeakLValue(Value: LHSValue));
19516 // We can't compare the address of the start of one object with the
19517 // past-the-end address of another object, per C++ DR1652.
19518 if (LHSValue.Base && LHSValue.Offset.isZero() &&
19519 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: RHSValue))
19520 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
19521 true);
19522 if (RHSValue.Base && RHSValue.Offset.isZero() &&
19523 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: LHSValue))
19524 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
19525 false);
19526 // We can't tell whether an object is at the same address as another
19527 // zero sized object.
19528 if ((RHSValue.Base && isZeroSized(Value: LHSValue)) ||
19529 (LHSValue.Base && isZeroSized(Value: RHSValue)))
19530 return DiagComparison(
19531 diag::note_constexpr_pointer_comparison_zero_sized);
19532 if (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)
19533 return DiagComparison(
19534 diag::note_constexpr_pointer_comparison_unspecified);
19535 // FIXME: Verify both variables are live.
19536 return Success(CmpResult::Unequal, E);
19537 }
19538
19539 CharUnits LHSOffset = LHSValue.getLValueOffset();
19540 CharUnits RHSOffset = RHSValue.getLValueOffset();
19541
19542 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
19543 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
19544
19545 // C++11 [expr.rel]p2:
19546 // - If two pointers point to non-static data members of the same object,
19547 // or to subobjects or array elements fo such members, recursively, the
19548 // pointer to the later declared member compares greater provided the
19549 // two members have the same access control and provided their class is
19550 // not a union.
19551 // [...]
19552 // - Otherwise pointer comparisons are unspecified.
19553 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {
19554 bool WasArrayIndex;
19555 unsigned Mismatch = FindDesignatorMismatch(
19556 ObjType: LHSValue.Base.isNull() ? QualType()
19557 : getType(B: LHSValue.Base).getNonReferenceType(),
19558 A: LHSDesignator, B: RHSDesignator, WasArrayIndex);
19559 // At the point where the designators diverge, the comparison has a
19560 // specified value if:
19561 // - we are comparing array indices
19562 // - we are comparing fields of a union, or fields with the same access
19563 // Otherwise, the result is unspecified and thus the comparison is not a
19564 // constant expression.
19565 if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
19566 Mismatch < RHSDesignator.Entries.size()) {
19567 const FieldDecl *LF = getAsField(E: LHSDesignator.Entries[Mismatch]);
19568 const FieldDecl *RF = getAsField(E: RHSDesignator.Entries[Mismatch]);
19569 if (!LF && !RF)
19570 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_classes);
19571 else if (!LF)
19572 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
19573 << getAsBaseClass(E: LHSDesignator.Entries[Mismatch])
19574 << RF->getParent() << RF;
19575 else if (!RF)
19576 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
19577 << getAsBaseClass(E: RHSDesignator.Entries[Mismatch])
19578 << LF->getParent() << LF;
19579 else if (!LF->getParent()->isUnion() &&
19580 LF->getAccess() != RF->getAccess())
19581 Info.CCEDiag(E,
19582 DiagId: diag::note_constexpr_pointer_comparison_differing_access)
19583 << LF << LF->getAccess() << RF << RF->getAccess()
19584 << LF->getParent();
19585 }
19586 }
19587
19588 // The comparison here must be unsigned, and performed with the same
19589 // width as the pointer.
19590 unsigned PtrSize = Info.Ctx.getTypeSize(T: LHSTy);
19591 uint64_t CompareLHS = LHSOffset.getQuantity();
19592 uint64_t CompareRHS = RHSOffset.getQuantity();
19593 assert(PtrSize <= 64 && "Unexpected pointer width");
19594 uint64_t Mask = ~0ULL >> (64 - PtrSize);
19595 CompareLHS &= Mask;
19596 CompareRHS &= Mask;
19597
19598 // If there is a base and this is a relational operator, we can only
19599 // compare pointers within the object in question; otherwise, the result
19600 // depends on where the object is located in memory.
19601 if (!LHSValue.Base.isNull() && IsRelational) {
19602 QualType BaseTy = getType(B: LHSValue.Base).getNonReferenceType();
19603 if (BaseTy->isIncompleteType())
19604 return Error(E);
19605 CharUnits Size = Info.Ctx.getTypeSizeInChars(T: BaseTy);
19606 uint64_t OffsetLimit = Size.getQuantity();
19607 if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)
19608 return Error(E);
19609 }
19610
19611 if (CompareLHS < CompareRHS)
19612 return Success(CmpResult::Less, E);
19613 if (CompareLHS > CompareRHS)
19614 return Success(CmpResult::Greater, E);
19615 return Success(CmpResult::Equal, E);
19616 }
19617
19618 if (LHSTy->isMemberPointerType()) {
19619 assert(IsEquality && "unexpected member pointer operation");
19620 assert(RHSTy->isMemberPointerType() && "invalid comparison");
19621
19622 MemberPtr LHSValue, RHSValue;
19623
19624 bool LHSOK = EvaluateMemberPointer(E: E->getLHS(), Result&: LHSValue, Info);
19625 if (!LHSOK && !Info.noteFailure())
19626 return false;
19627
19628 if (!EvaluateMemberPointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19629 return false;
19630
19631 // If either operand is a pointer to a weak function, the comparison is not
19632 // constant.
19633 if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) {
19634 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19635 << LHSValue.getDecl();
19636 return false;
19637 }
19638 if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) {
19639 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19640 << RHSValue.getDecl();
19641 return false;
19642 }
19643
19644 // C++11 [expr.eq]p2:
19645 // If both operands are null, they compare equal. Otherwise if only one is
19646 // null, they compare unequal.
19647 if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
19648 bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
19649 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19650 }
19651
19652 // Otherwise if either is a pointer to a virtual member function, the
19653 // result is unspecified.
19654 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: LHSValue.getDecl()))
19655 if (MD->isVirtual())
19656 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19657 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: RHSValue.getDecl()))
19658 if (MD->isVirtual())
19659 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19660
19661 // Otherwise they compare equal if and only if they would refer to the
19662 // same member of the same most derived object or the same subobject if
19663 // they were dereferenced with a hypothetical object of the associated
19664 // class type.
19665 bool Equal = LHSValue == RHSValue;
19666 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19667 }
19668
19669 if (LHSTy->isNullPtrType()) {
19670 assert(E->isComparisonOp() && "unexpected nullptr operation");
19671 assert(RHSTy->isNullPtrType() && "missing pointer conversion");
19672 // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
19673 // are compared, the result is true of the operator is <=, >= or ==, and
19674 // false otherwise.
19675 LValue Res;
19676 if (!EvaluatePointer(E: E->getLHS(), Result&: Res, Info) ||
19677 !EvaluatePointer(E: E->getRHS(), Result&: Res, Info))
19678 return false;
19679 return Success(CmpResult::Equal, E);
19680 }
19681
19682 if (LHSTy->isMetaInfoType() && RHSTy->isMetaInfoType()) {
19683 APValue LHSValue, RHSValue;
19684 llvm::FoldingSetNodeID LID, RID;
19685 if (!Evaluate(Result&: LHSValue, Info, E: E->getLHS()))
19686 return false;
19687 LHSValue.Profile(ID&: LID);
19688
19689 if (!Evaluate(Result&: RHSValue, Info, E: E->getRHS()))
19690 return false;
19691 RHSValue.Profile(ID&: RID);
19692
19693 if (LID == RID)
19694 return Success(CmpResult::Equal, E);
19695 else
19696 return Success(CmpResult::Unequal, E);
19697 }
19698
19699 return DoAfter();
19700}
19701
19702static bool EvaluateComparisonResult(EvalInfo &Info, const Expr *E,
19703 ComparisonCategoryResult CCR,
19704 APValue &Result) {
19705 const ComparisonCategoryInfo &CmpInfo =
19706 Info.Ctx.CompCategories.getInfoForType(Ty: E->getType());
19707 const VarDecl *VD = CmpInfo.getValueInfo(ValueKind: CmpInfo.makeWeakResult(Res: CCR))->VD;
19708
19709 // Check and evaluate the result as a constant expression.
19710 LValue LV;
19711 LV.set(B: VD);
19712 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
19713 return false;
19714 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
19715 Kind: ConstantExprKind::Normal);
19716}
19717
19718bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) {
19719 if (!CheckLiteralType(Info, E))
19720 return false;
19721
19722 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19723 ComparisonCategoryResult CCR;
19724 switch (CR) {
19725 case CmpResult::Unequal:
19726 llvm_unreachable("should never produce Unequal for three-way comparison");
19727 case CmpResult::Less:
19728 CCR = ComparisonCategoryResult::Less;
19729 break;
19730 case CmpResult::Equal:
19731 CCR = ComparisonCategoryResult::Equal;
19732 break;
19733 case CmpResult::Greater:
19734 CCR = ComparisonCategoryResult::Greater;
19735 break;
19736 case CmpResult::Unordered:
19737 CCR = ComparisonCategoryResult::Unordered;
19738 break;
19739 }
19740 return EvaluateComparisonResult(Info, E, CCR, Result);
19741 };
19742 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19743 return ExprEvaluatorBaseTy::VisitBinCmp(S: E);
19744 });
19745}
19746
19747bool RecordExprEvaluator::VisitTypeTraitExpr(const TypeTraitExpr *E) {
19748 if (!CheckLiteralType(Info, E))
19749 return false;
19750
19751 assert(E->isStoredAsComparisonResult() &&
19752 "expected a strong_ordering type trait with a stored value");
19753
19754 ComparisonCategoryResult CCR = static_cast<ComparisonCategoryResult>(
19755 E->getAPValue().getInt().getZExtValue());
19756 return EvaluateComparisonResult(Info, E, CCR, Result);
19757}
19758
19759bool RecordExprEvaluator::VisitCXXParenListInitExpr(
19760 const CXXParenListInitExpr *E) {
19761 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs());
19762}
19763
19764bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
19765 // We don't support assignment in C. C++ assignments don't get here because
19766 // assignment is an lvalue in C++.
19767 if (E->isAssignmentOp()) {
19768 Error(E);
19769 if (!Info.noteFailure())
19770 return false;
19771 }
19772
19773 if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E))
19774 return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E);
19775
19776 assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() ||
19777 !E->getRHS()->getType()->isIntegralOrEnumerationType()) &&
19778 "DataRecursiveIntBinOpEvaluator should have handled integral types");
19779
19780 if (E->isComparisonOp()) {
19781 // Evaluate builtin binary comparisons by evaluating them as three-way
19782 // comparisons and then translating the result.
19783 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19784 assert((CR != CmpResult::Unequal || E->isEqualityOp()) &&
19785 "should only produce Unequal for equality comparisons");
19786 bool IsEqual = CR == CmpResult::Equal,
19787 IsLess = CR == CmpResult::Less,
19788 IsGreater = CR == CmpResult::Greater;
19789 auto Op = E->getOpcode();
19790 switch (Op) {
19791 default:
19792 llvm_unreachable("unsupported binary operator");
19793 case BO_EQ:
19794 case BO_NE:
19795 return Success(Value: IsEqual == (Op == BO_EQ), E);
19796 case BO_LT:
19797 return Success(Value: IsLess, E);
19798 case BO_GT:
19799 return Success(Value: IsGreater, E);
19800 case BO_LE:
19801 return Success(Value: IsEqual || IsLess, E);
19802 case BO_GE:
19803 return Success(Value: IsEqual || IsGreater, E);
19804 }
19805 };
19806 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19807 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19808 });
19809 }
19810
19811 QualType LHSTy = E->getLHS()->getType();
19812 QualType RHSTy = E->getRHS()->getType();
19813
19814 if (LHSTy->isPointerType() && RHSTy->isPointerType() &&
19815 E->getOpcode() == BO_Sub) {
19816 LValue LHSValue, RHSValue;
19817
19818 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
19819 if (!LHSOK && !Info.noteFailure())
19820 return false;
19821
19822 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19823 return false;
19824
19825 // Reject differing bases from the normal codepath; we special-case
19826 // comparisons to null.
19827 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
19828 if (Info.checkingPotentialConstantExpression() &&
19829 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
19830 return false;
19831
19832 const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>();
19833 const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>();
19834
19835 auto DiagArith = [&](unsigned DiagID) {
19836 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
19837 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
19838 Info.FFDiag(E, DiagId: DiagID) << LHS << RHS;
19839 if (LHSExpr && LHSExpr == RHSExpr)
19840 Info.Note(Loc: LHSExpr->getExprLoc(),
19841 DiagId: diag::note_constexpr_repeated_literal_eval)
19842 << LHSExpr->getSourceRange();
19843 return false;
19844 };
19845
19846 if (!LHSExpr || !RHSExpr)
19847 return DiagArith(diag::note_constexpr_pointer_arith_unspecified);
19848
19849 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
19850 return DiagArith(diag::note_constexpr_literal_arith);
19851
19852 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
19853 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
19854 if (!LHSAddrExpr || !RHSAddrExpr)
19855 return Error(E);
19856 // Make sure both labels come from the same function.
19857 if (LHSAddrExpr->getLabel()->getDeclContext() !=
19858 RHSAddrExpr->getLabel()->getDeclContext())
19859 return Error(E);
19860 return Success(V: APValue(LHSAddrExpr, RHSAddrExpr), E);
19861 }
19862 CharUnits LHSOffset = LHSValue.getLValueOffset();
19863 CharUnits RHSOffset = RHSValue.getLValueOffset();
19864
19865 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
19866 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
19867
19868 // C++11 [expr.add]p6:
19869 // Unless both pointers point to elements of the same array object, or
19870 // one past the last element of the array object, the behavior is
19871 // undefined.
19872 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
19873 !AreElementsOfSameArray(ObjType: getType(B: LHSValue.Base), A: LHSDesignator,
19874 B: RHSDesignator))
19875 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_not_same_array);
19876
19877 QualType Type = E->getLHS()->getType();
19878 QualType ElementType = Type->castAs<PointerType>()->getPointeeType();
19879
19880 CharUnits ElementSize;
19881 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElementType, Size&: ElementSize))
19882 return false;
19883
19884 // As an extension, a type may have zero size (empty struct or union in
19885 // C, array of zero length). Pointer subtraction in such cases has
19886 // undefined behavior, so is not constant.
19887 if (ElementSize.isZero()) {
19888 Info.FFDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_zero_size)
19889 << ElementType;
19890 return false;
19891 }
19892
19893 // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
19894 // and produce incorrect results when it overflows. Such behavior
19895 // appears to be non-conforming, but is common, so perhaps we should
19896 // assume the standard intended for such cases to be undefined behavior
19897 // and check for them.
19898
19899 // Compute (LHSOffset - RHSOffset) / Size carefully, checking for
19900 // overflow in the final conversion to ptrdiff_t.
19901 APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
19902 APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
19903 APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true),
19904 false);
19905 APSInt TrueResult = (LHS - RHS) / ElemSize;
19906 APSInt Result = TrueResult.trunc(width: Info.Ctx.getIntWidth(T: E->getType()));
19907
19908 if (Result.extend(width: 65) != TrueResult &&
19909 !HandleOverflow(Info, E, SrcValue: TrueResult, DestType: E->getType()))
19910 return false;
19911 return Success(SI: Result, E);
19912 }
19913
19914 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19915}
19916
19917/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
19918/// a result as the expression's type.
19919bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
19920 const UnaryExprOrTypeTraitExpr *E) {
19921 switch(E->getKind()) {
19922 case UETT_PreferredAlignOf:
19923 case UETT_AlignOf: {
19924 if (E->isArgumentType())
19925 return Success(
19926 Size: GetAlignOfType(Ctx: Info.Ctx, T: E->getArgumentType(), ExprKind: E->getKind()), E);
19927 else
19928 return Success(
19929 Size: GetAlignOfExpr(Ctx: Info.Ctx, E: E->getArgumentExpr(), ExprKind: E->getKind()), E);
19930 }
19931
19932 case UETT_PtrAuthTypeDiscriminator: {
19933 if (E->getArgumentType()->isDependentType())
19934 return false;
19935 return Success(
19936 Value: Info.Ctx.getPointerAuthTypeDiscriminator(T: E->getArgumentType()), E);
19937 }
19938 case UETT_VecStep: {
19939 QualType Ty = E->getTypeOfArgument();
19940
19941 if (Ty->isVectorType()) {
19942 unsigned n = Ty->castAs<VectorType>()->getNumElements();
19943
19944 // The vec_step built-in functions that take a 3-component
19945 // vector return 4. (OpenCL 1.1 spec 6.11.12)
19946 if (n == 3)
19947 n = 4;
19948
19949 return Success(Value: n, E);
19950 } else
19951 return Success(Value: 1, E);
19952 }
19953
19954 case UETT_DataSizeOf:
19955 case UETT_SizeOf: {
19956 QualType SrcTy = E->getTypeOfArgument();
19957 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
19958 // the result is the size of the referenced type."
19959 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
19960 SrcTy = Ref->getPointeeType();
19961
19962 CharUnits Sizeof;
19963 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: SrcTy, Size&: Sizeof,
19964 SOT: E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf
19965 : SizeOfType::SizeOf)) {
19966 return false;
19967 }
19968 return Success(Size: Sizeof, E);
19969 }
19970 case UETT_OpenMPRequiredSimdAlign:
19971 assert(E->isArgumentType());
19972 return Success(
19973 Value: Info.Ctx.toCharUnitsFromBits(
19974 BitSize: Info.Ctx.getOpenMPDefaultSimdAlign(T: E->getArgumentType()))
19975 .getQuantity(),
19976 E);
19977 case UETT_VectorElements: {
19978 QualType Ty = E->getTypeOfArgument();
19979 // If the vector has a fixed size, we can determine the number of elements
19980 // at compile time.
19981 if (const auto *VT = Ty->getAs<VectorType>())
19982 return Success(Value: VT->getNumElements(), E);
19983
19984 assert(Ty->isSizelessVectorType());
19985 if (Info.InConstantContext)
19986 Info.CCEDiag(E, DiagId: diag::note_constexpr_non_const_vectorelements)
19987 << E->getSourceRange();
19988
19989 return false;
19990 }
19991 case UETT_CountOf: {
19992 QualType Ty = E->getTypeOfArgument();
19993 assert(Ty->isArrayType());
19994
19995 // We don't need to worry about array element qualifiers, so getting the
19996 // unsafe array type is fine.
19997 if (const auto *CAT =
19998 dyn_cast<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe())) {
19999 return Success(I: CAT->getSize(), E);
20000 }
20001
20002 assert(!Ty->isConstantSizeType());
20003
20004 // If it's a variable-length array type, we need to check whether it is a
20005 // multidimensional array. If so, we need to check the size expression of
20006 // the VLA to see if it's a constant size. If so, we can return that value.
20007 const auto *VAT = Info.Ctx.getAsVariableArrayType(T: Ty);
20008 assert(VAT);
20009 if (VAT->getElementType()->isArrayType()) {
20010 // Variable array size expression could be missing (e.g. int a[*][10]) In
20011 // that case, it can't be a constant expression.
20012 if (!VAT->getSizeExpr()) {
20013 Info.FFDiag(Loc: E->getBeginLoc());
20014 return false;
20015 }
20016
20017 std::optional<APSInt> Res =
20018 VAT->getSizeExpr()->getIntegerConstantExpr(Ctx: Info.Ctx);
20019 if (Res) {
20020 // The resulting value always has type size_t, so we need to make the
20021 // returned APInt have the correct sign and bit-width.
20022 APInt Val{
20023 static_cast<unsigned>(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType())),
20024 Res->getZExtValue()};
20025 return Success(I: Val, E);
20026 }
20027 }
20028
20029 // Definitely a variable-length type, which is not an ICE.
20030 // FIXME: Better diagnostic.
20031 Info.FFDiag(Loc: E->getBeginLoc());
20032 return false;
20033 }
20034 }
20035
20036 llvm_unreachable("unknown expr/type trait");
20037}
20038
20039bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
20040 Info.Ctx.recordOffsetOfEvaluation(E: OOE);
20041 CharUnits Result;
20042 unsigned n = OOE->getNumComponents();
20043 if (n == 0)
20044 return Error(E: OOE);
20045 QualType CurrentType = OOE->getTypeSourceInfo()->getType();
20046 for (unsigned i = 0; i != n; ++i) {
20047 OffsetOfNode ON = OOE->getComponent(Idx: i);
20048 switch (ON.getKind()) {
20049 case OffsetOfNode::Array: {
20050 const Expr *Idx = OOE->getIndexExpr(Idx: ON.getArrayExprIndex());
20051 APSInt IdxResult;
20052 if (!EvaluateInteger(E: Idx, Result&: IdxResult, Info))
20053 return false;
20054 const ArrayType *AT = Info.Ctx.getAsArrayType(T: CurrentType);
20055 if (!AT)
20056 return Error(E: OOE);
20057 CurrentType = AT->getElementType();
20058 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(T: CurrentType);
20059 // Reject negative indices, indices too large to fit in int64_t,
20060 // and overflow in the offset computation.
20061 if (IdxResult.isNegative() || IdxResult.getActiveBits() > 63)
20062 return Error(E: OOE);
20063 int64_t IdxVal = IdxResult.getExtValue();
20064 int64_t ElemSize = ElementSize.getQuantity();
20065 if (IdxVal != 0 &&
20066 ElemSize > std::numeric_limits<int64_t>::max() / IdxVal)
20067 return Error(E: OOE, D: diag::note_constexpr_offsetof_overflow);
20068 int64_t Offset = IdxVal * ElemSize;
20069 if (Result.getQuantity() > std::numeric_limits<int64_t>::max() - Offset)
20070 return Error(E: OOE, D: diag::note_constexpr_offsetof_overflow);
20071 Result += CharUnits::fromQuantity(Quantity: Offset);
20072 break;
20073 }
20074
20075 case OffsetOfNode::Field: {
20076 FieldDecl *MemberDecl = ON.getField();
20077 const auto *RD = CurrentType->getAsRecordDecl();
20078 if (!RD)
20079 return Error(E: OOE);
20080 if (RD->isInvalidDecl()) return false;
20081 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
20082 unsigned i = MemberDecl->getFieldIndex();
20083 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
20084 Result += Info.Ctx.toCharUnitsFromBits(BitSize: RL.getFieldOffset(FieldNo: i));
20085 CurrentType = MemberDecl->getType().getNonReferenceType();
20086 break;
20087 }
20088
20089 case OffsetOfNode::Identifier:
20090 llvm_unreachable("dependent __builtin_offsetof");
20091
20092 case OffsetOfNode::Base: {
20093 CXXBaseSpecifier *BaseSpec = ON.getBase();
20094 if (BaseSpec->isVirtual())
20095 return Error(E: OOE);
20096
20097 // Find the layout of the class whose base we are looking into.
20098 const auto *RD = CurrentType->getAsCXXRecordDecl();
20099 if (!RD)
20100 return Error(E: OOE);
20101 if (RD->isInvalidDecl()) return false;
20102 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
20103
20104 // Find the base class itself.
20105 CurrentType = BaseSpec->getType();
20106 const auto *BaseRD = CurrentType->getAsCXXRecordDecl();
20107 if (!BaseRD)
20108 return Error(E: OOE);
20109
20110 // Add the offset to the base.
20111 Result += RL.getBaseClassOffset(Base: BaseRD);
20112 break;
20113 }
20114 }
20115 }
20116 return Success(Size: Result, E: OOE);
20117}
20118
20119bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20120 switch (E->getOpcode()) {
20121 default:
20122 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
20123 // See C99 6.6p3.
20124 return Error(E);
20125 case UO_Extension:
20126 // FIXME: Should extension allow i-c-e extension expressions in its scope?
20127 // If so, we could clear the diagnostic ID.
20128 return Visit(S: E->getSubExpr());
20129 case UO_Plus:
20130 // The result is just the value.
20131 return Visit(S: E->getSubExpr());
20132 case UO_Minus: {
20133 if (!Visit(S: E->getSubExpr()))
20134 return false;
20135 if (!Result.isInt()) return Error(E);
20136 const APSInt &Value = Result.getInt();
20137 if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() &&
20138 !E->getType().isWrapType()) {
20139 if (Info.checkingForUndefinedBehavior())
20140 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20141 DiagID: diag::warn_integer_constant_overflow)
20142 << toString(I: Value, Radix: 10, Signed: Value.isSigned(), /*formatAsCLiteral=*/false,
20143 /*UpperCase=*/true, /*InsertSeparators=*/true)
20144 << E->getType() << E->getSourceRange();
20145
20146 if (!HandleOverflow(Info, E, SrcValue: -Value.extend(width: Value.getBitWidth() + 1),
20147 DestType: E->getType()))
20148 return false;
20149 }
20150 return Success(SI: -Value, E);
20151 }
20152 case UO_Not: {
20153 if (!Visit(S: E->getSubExpr()))
20154 return false;
20155 if (!Result.isInt()) return Error(E);
20156 return Success(SI: ~Result.getInt(), E);
20157 }
20158 case UO_LNot: {
20159 bool bres;
20160 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
20161 return false;
20162 return Success(Value: !bres, E);
20163 }
20164 }
20165}
20166
20167/// HandleCast - This is used to evaluate implicit or explicit casts where the
20168/// result type is integer.
20169bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
20170 const Expr *SubExpr = E->getSubExpr();
20171 QualType DestType = E->getType();
20172 QualType SrcType = SubExpr->getType();
20173
20174 switch (E->getCastKind()) {
20175 case CK_BaseToDerived:
20176 case CK_DerivedToBase:
20177 case CK_UncheckedDerivedToBase:
20178 case CK_Dynamic:
20179 case CK_ToUnion:
20180 case CK_ArrayToPointerDecay:
20181 case CK_FunctionToPointerDecay:
20182 case CK_NullToPointer:
20183 case CK_NullToMemberPointer:
20184 case CK_BaseToDerivedMemberPointer:
20185 case CK_DerivedToBaseMemberPointer:
20186 case CK_ReinterpretMemberPointer:
20187 case CK_ConstructorConversion:
20188 case CK_IntegralToPointer:
20189 case CK_ToVoid:
20190 case CK_VectorSplat:
20191 case CK_IntegralToFloating:
20192 case CK_FloatingCast:
20193 case CK_CPointerToObjCPointerCast:
20194 case CK_BlockPointerToObjCPointerCast:
20195 case CK_AnyPointerToBlockPointerCast:
20196 case CK_ObjCObjectLValueCast:
20197 case CK_FloatingRealToComplex:
20198 case CK_FloatingComplexToReal:
20199 case CK_FloatingComplexCast:
20200 case CK_FloatingComplexToIntegralComplex:
20201 case CK_IntegralRealToComplex:
20202 case CK_IntegralComplexCast:
20203 case CK_IntegralComplexToFloatingComplex:
20204 case CK_BuiltinFnToFnPtr:
20205 case CK_ZeroToOCLOpaqueType:
20206 case CK_NonAtomicToAtomic:
20207 case CK_AddressSpaceConversion:
20208 case CK_IntToOCLSampler:
20209 case CK_FloatingToFixedPoint:
20210 case CK_FixedPointToFloating:
20211 case CK_FixedPointCast:
20212 case CK_IntegralToFixedPoint:
20213 case CK_MatrixCast:
20214 case CK_HLSLAggregateSplatCast:
20215 llvm_unreachable("invalid cast kind for integral value");
20216
20217 case CK_BitCast:
20218 case CK_Dependent:
20219 case CK_LValueBitCast:
20220 case CK_ARCProduceObject:
20221 case CK_ARCConsumeObject:
20222 case CK_ARCReclaimReturnedObject:
20223 case CK_ARCExtendBlockObject:
20224 case CK_CopyAndAutoreleaseBlockObject:
20225 return Error(E);
20226
20227 case CK_UserDefinedConversion:
20228 case CK_LValueToRValue:
20229 case CK_AtomicToNonAtomic:
20230 case CK_NoOp:
20231 case CK_LValueToRValueBitCast:
20232 case CK_HLSLArrayRValue:
20233 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20234
20235 case CK_MemberPointerToBoolean:
20236 case CK_PointerToBoolean:
20237 case CK_IntegralToBoolean:
20238 case CK_FloatingToBoolean:
20239 case CK_BooleanToSignedIntegral:
20240 case CK_FloatingComplexToBoolean:
20241 case CK_IntegralComplexToBoolean: {
20242 bool BoolResult;
20243 if (!EvaluateAsBooleanCondition(E: SubExpr, Result&: BoolResult, Info))
20244 return false;
20245 uint64_t IntResult = BoolResult;
20246 if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral)
20247 IntResult = (uint64_t)-1;
20248 return Success(Value: IntResult, E);
20249 }
20250
20251 case CK_FixedPointToIntegral: {
20252 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SrcType));
20253 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
20254 return false;
20255 bool Overflowed;
20256 llvm::APSInt Result = Src.convertToInt(
20257 DstWidth: Info.Ctx.getIntWidth(T: DestType),
20258 DstSign: DestType->isSignedIntegerOrEnumerationType(), Overflow: &Overflowed);
20259 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Result, DestType))
20260 return false;
20261 return Success(SI: Result, E);
20262 }
20263
20264 case CK_FixedPointToBoolean: {
20265 // Unsigned padding does not affect this.
20266 APValue Val;
20267 if (!Evaluate(Result&: Val, Info, E: SubExpr))
20268 return false;
20269 return Success(Value: Val.getFixedPoint().getBoolValue(), E);
20270 }
20271
20272 case CK_IntegralCast: {
20273 if (!Visit(S: SubExpr))
20274 return false;
20275
20276 if (!Result.isInt()) {
20277 // Allow casts of address-of-label differences if they are no-ops
20278 // or narrowing, if the result is at least 32 bits wide.
20279 // (The narrowing case isn't actually guaranteed to
20280 // be constant-evaluatable except in some narrow cases which are hard
20281 // to detect here. We let it through on the assumption the user knows
20282 // what they are doing.)
20283 if (Result.isAddrLabelDiff()) {
20284 unsigned DestBits = Info.Ctx.getTypeSize(T: DestType);
20285 return DestBits >= 32 && DestBits <= Info.Ctx.getTypeSize(T: SrcType);
20286 }
20287 // Only allow casts of lvalues if they are lossless.
20288 return Info.Ctx.getTypeSize(T: DestType) == Info.Ctx.getTypeSize(T: SrcType);
20289 }
20290
20291 if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) {
20292 const auto *ED = DestType->getAsEnumDecl();
20293 // Check that the value is within the range of the enumeration values.
20294 //
20295 // This corressponds to [expr.static.cast]p10 which says:
20296 // A value of integral or enumeration type can be explicitly converted
20297 // to a complete enumeration type ... If the enumeration type does not
20298 // have a fixed underlying type, the value is unchanged if the original
20299 // value is within the range of the enumeration values ([dcl.enum]), and
20300 // otherwise, the behavior is undefined.
20301 //
20302 // This was resolved as part of DR2338 which has CD5 status.
20303 if (!ED->isFixed()) {
20304 llvm::APInt Min;
20305 llvm::APInt Max;
20306
20307 ED->getValueRange(Max, Min);
20308 --Max;
20309
20310 if (ED->getNumNegativeBits() &&
20311 (Max.slt(RHS: Result.getInt().getSExtValue()) ||
20312 Min.sgt(RHS: Result.getInt().getSExtValue())))
20313 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
20314 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getSExtValue()
20315 << Max.getSExtValue() << ED;
20316 else if (!ED->getNumNegativeBits() &&
20317 Max.ult(RHS: Result.getInt().getZExtValue()))
20318 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
20319 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getZExtValue()
20320 << Max.getZExtValue() << ED;
20321 }
20322 }
20323
20324 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType,
20325 Value: Result.getInt()), E);
20326 }
20327
20328 case CK_PointerToIntegral: {
20329 CCEDiag(E, D: diag::note_constexpr_invalid_cast_ptrtoint)
20330 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
20331 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
20332
20333 LValue LV;
20334 if (!EvaluatePointer(E: SubExpr, Result&: LV, Info))
20335 return false;
20336
20337 if (LV.getLValueBase()) {
20338 CCEDiag(E, D: diag::note_constexpr_has_lvalue) << E->getSourceRange();
20339 // Only allow based lvalue casts if they are lossless.
20340 // FIXME: Allow a larger integer size than the pointer size, and allow
20341 // narrowing back down to pointer width in subsequent integral casts.
20342 // FIXME: Check integer type's active bits, not its type size.
20343 if (Info.Ctx.getTypeSize(T: DestType) != Info.Ctx.getTypeSize(T: SrcType))
20344 return Error(E);
20345
20346 LV.Designator.setInvalid();
20347 LV.moveInto(V&: Result);
20348 return true;
20349 }
20350
20351 APSInt AsInt;
20352 APValue V;
20353 LV.moveInto(V);
20354 if (!V.toIntegralConstant(Result&: AsInt, SrcTy: SrcType, Ctx: Info.Ctx))
20355 llvm_unreachable("Can't cast this!");
20356
20357 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType, Value: AsInt), E);
20358 }
20359
20360 case CK_IntegralComplexToReal: {
20361 ComplexValue C;
20362 if (!EvaluateComplex(E: SubExpr, Res&: C, Info))
20363 return false;
20364 return Success(SI: C.getComplexIntReal(), E);
20365 }
20366
20367 case CK_FloatingToIntegral: {
20368 APFloat F(0.0);
20369 if (!EvaluateFloat(E: SubExpr, Result&: F, Info))
20370 return false;
20371
20372 APSInt Value;
20373 if (!HandleFloatToIntCast(Info, E, SrcType, Value: F, DestType, Result&: Value))
20374 return false;
20375 return Success(SI: Value, E);
20376 }
20377 case CK_HLSLVectorTruncation: {
20378 APValue Val;
20379 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
20380 return Error(E);
20381 return Success(V: Val.getVectorElt(I: 0), E);
20382 }
20383 case CK_HLSLMatrixTruncation: {
20384 APValue Val;
20385 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
20386 return Error(E);
20387 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), E);
20388 }
20389 case CK_HLSLElementwiseCast: {
20390 SmallVector<APValue> SrcVals;
20391 SmallVector<QualType> SrcTypes;
20392
20393 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: DestType, SrcVals, SrcTypes))
20394 return false;
20395
20396 // cast our single element
20397 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
20398 APValue ResultVal;
20399 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: DestType, Original: SrcVals[0],
20400 Result&: ResultVal))
20401 return false;
20402 return Success(V: ResultVal, E);
20403 }
20404 }
20405
20406 llvm_unreachable("unknown cast resulting in integral value");
20407}
20408
20409bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
20410 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20411 ComplexValue LV;
20412 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
20413 return false;
20414 if (!LV.isComplexInt())
20415 return Error(E);
20416 return Success(SI: LV.getComplexIntReal(), E);
20417 }
20418
20419 return Visit(S: E->getSubExpr());
20420}
20421
20422bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
20423 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
20424 ComplexValue LV;
20425 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
20426 return false;
20427 if (!LV.isComplexInt())
20428 return Error(E);
20429 return Success(SI: LV.getComplexIntImag(), E);
20430 }
20431
20432 VisitIgnoredValue(E: E->getSubExpr());
20433 return Success(Value: 0, E);
20434}
20435
20436bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
20437 return Success(Value: E->getPackLength(), E);
20438}
20439
20440bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
20441 return Success(Value: E->getValue(), E);
20442}
20443
20444bool IntExprEvaluator::VisitConceptSpecializationExpr(
20445 const ConceptSpecializationExpr *E) {
20446 return Success(Value: E->isSatisfied(), E);
20447}
20448
20449bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) {
20450 return Success(Value: E->isSatisfied(), E);
20451}
20452
20453bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20454 switch (E->getOpcode()) {
20455 default:
20456 // Invalid unary operators
20457 return Error(E);
20458 case UO_Plus:
20459 // The result is just the value.
20460 return Visit(S: E->getSubExpr());
20461 case UO_Minus: {
20462 if (!Visit(S: E->getSubExpr())) return false;
20463 if (!Result.isFixedPoint())
20464 return Error(E);
20465 bool Overflowed;
20466 APFixedPoint Negated = Result.getFixedPoint().negate(Overflow: &Overflowed);
20467 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Negated, DestType: E->getType()))
20468 return false;
20469 return Success(V: Negated, E);
20470 }
20471 case UO_LNot: {
20472 bool bres;
20473 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
20474 return false;
20475 return Success(Value: !bres, E);
20476 }
20477 }
20478}
20479
20480bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) {
20481 const Expr *SubExpr = E->getSubExpr();
20482 QualType DestType = E->getType();
20483 assert(DestType->isFixedPointType() &&
20484 "Expected destination type to be a fixed point type");
20485 auto DestFXSema = Info.Ctx.getFixedPointSemantics(Ty: DestType);
20486
20487 switch (E->getCastKind()) {
20488 case CK_FixedPointCast: {
20489 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
20490 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
20491 return false;
20492 bool Overflowed;
20493 APFixedPoint Result = Src.convert(DstSema: DestFXSema, Overflow: &Overflowed);
20494 if (Overflowed) {
20495 if (Info.checkingForUndefinedBehavior())
20496 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20497 DiagID: diag::warn_fixedpoint_constant_overflow)
20498 << Result.toString() << E->getType();
20499 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20500 return false;
20501 }
20502 return Success(V: Result, E);
20503 }
20504 case CK_IntegralToFixedPoint: {
20505 APSInt Src;
20506 if (!EvaluateInteger(E: SubExpr, Result&: Src, Info))
20507 return false;
20508
20509 bool Overflowed;
20510 APFixedPoint IntResult = APFixedPoint::getFromIntValue(
20511 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
20512
20513 if (Overflowed) {
20514 if (Info.checkingForUndefinedBehavior())
20515 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20516 DiagID: diag::warn_fixedpoint_constant_overflow)
20517 << IntResult.toString() << E->getType();
20518 if (!HandleOverflow(Info, E, SrcValue: IntResult, DestType: E->getType()))
20519 return false;
20520 }
20521
20522 return Success(V: IntResult, E);
20523 }
20524 case CK_FloatingToFixedPoint: {
20525 APFloat Src(0.0);
20526 if (!EvaluateFloat(E: SubExpr, Result&: Src, Info))
20527 return false;
20528
20529 bool Overflowed;
20530 APFixedPoint Result = APFixedPoint::getFromFloatValue(
20531 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
20532
20533 if (Overflowed) {
20534 if (Info.checkingForUndefinedBehavior())
20535 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20536 DiagID: diag::warn_fixedpoint_constant_overflow)
20537 << Result.toString() << E->getType();
20538 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20539 return false;
20540 }
20541
20542 return Success(V: Result, E);
20543 }
20544 case CK_NoOp:
20545 case CK_LValueToRValue:
20546 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20547 default:
20548 return Error(E);
20549 }
20550}
20551
20552bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20553 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20554 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20555
20556 const Expr *LHS = E->getLHS();
20557 const Expr *RHS = E->getRHS();
20558 FixedPointSemantics ResultFXSema =
20559 Info.Ctx.getFixedPointSemantics(Ty: E->getType());
20560
20561 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHS->getType()));
20562 if (!EvaluateFixedPointOrInteger(E: LHS, Result&: LHSFX, Info))
20563 return false;
20564 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHS->getType()));
20565 if (!EvaluateFixedPointOrInteger(E: RHS, Result&: RHSFX, Info))
20566 return false;
20567
20568 bool OpOverflow = false, ConversionOverflow = false;
20569 APFixedPoint Result(LHSFX.getSemantics());
20570 switch (E->getOpcode()) {
20571 case BO_Add: {
20572 Result = LHSFX.add(Other: RHSFX, Overflow: &OpOverflow)
20573 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20574 break;
20575 }
20576 case BO_Sub: {
20577 Result = LHSFX.sub(Other: RHSFX, Overflow: &OpOverflow)
20578 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20579 break;
20580 }
20581 case BO_Mul: {
20582 Result = LHSFX.mul(Other: RHSFX, Overflow: &OpOverflow)
20583 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20584 break;
20585 }
20586 case BO_Div: {
20587 if (RHSFX.getValue() == 0) {
20588 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero);
20589 return false;
20590 }
20591 Result = LHSFX.div(Other: RHSFX, Overflow: &OpOverflow)
20592 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20593 break;
20594 }
20595 case BO_Shl:
20596 case BO_Shr: {
20597 FixedPointSemantics LHSSema = LHSFX.getSemantics();
20598 llvm::APSInt RHSVal = RHSFX.getValue();
20599
20600 unsigned ShiftBW =
20601 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding();
20602 unsigned Amt = RHSVal.getLimitedValue(Limit: ShiftBW - 1);
20603 // Embedded-C 4.1.6.2.2:
20604 // The right operand must be nonnegative and less than the total number
20605 // of (nonpadding) bits of the fixed-point operand ...
20606 if (RHSVal.isNegative())
20607 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHSVal;
20608 else if (Amt != RHSVal)
20609 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
20610 << RHSVal << E->getType() << ShiftBW;
20611
20612 if (E->getOpcode() == BO_Shl)
20613 Result = LHSFX.shl(Amt, Overflow: &OpOverflow);
20614 else
20615 Result = LHSFX.shr(Amt, Overflow: &OpOverflow);
20616 break;
20617 }
20618 default:
20619 return false;
20620 }
20621 if (OpOverflow || ConversionOverflow) {
20622 if (Info.checkingForUndefinedBehavior())
20623 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20624 DiagID: diag::warn_fixedpoint_constant_overflow)
20625 << Result.toString() << E->getType();
20626 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20627 return false;
20628 }
20629 return Success(V: Result, E);
20630}
20631
20632//===----------------------------------------------------------------------===//
20633// Float Evaluation
20634//===----------------------------------------------------------------------===//
20635
20636namespace {
20637class FloatExprEvaluator
20638 : public ExprEvaluatorBase<FloatExprEvaluator> {
20639 APFloat &Result;
20640public:
20641 FloatExprEvaluator(EvalInfo &info, APFloat &result)
20642 : ExprEvaluatorBaseTy(info), Result(result) {}
20643
20644 bool Success(const APValue &V, const Expr *e) {
20645 Result = V.getFloat();
20646 return true;
20647 }
20648
20649 bool ZeroInitialization(const Expr *E) {
20650 Result = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
20651 return true;
20652 }
20653
20654 bool VisitCallExpr(const CallExpr *E);
20655
20656 bool VisitUnaryOperator(const UnaryOperator *E);
20657 bool VisitBinaryOperator(const BinaryOperator *E);
20658 bool VisitFloatingLiteral(const FloatingLiteral *E);
20659 bool VisitCastExpr(const CastExpr *E);
20660
20661 bool VisitUnaryReal(const UnaryOperator *E);
20662 bool VisitUnaryImag(const UnaryOperator *E);
20663
20664 // FIXME: Missing: array subscript of vector, member of vector
20665};
20666} // end anonymous namespace
20667
20668static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
20669 assert(!E->isValueDependent());
20670 assert(E->isPRValue() && E->getType()->isRealFloatingType());
20671 return FloatExprEvaluator(Info, Result).Visit(S: E);
20672}
20673
20674static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
20675 QualType ResultTy,
20676 const Expr *Arg,
20677 bool SNaN,
20678 llvm::APFloat &Result) {
20679 const StringLiteral *S = dyn_cast<StringLiteral>(Val: Arg->IgnoreParenCasts());
20680 if (!S || !S->isOrdinary())
20681 return false;
20682
20683 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(T: ResultTy);
20684
20685 llvm::APInt fill;
20686
20687 // Treat empty strings as if they were zero.
20688 if (S->getString().empty())
20689 fill = llvm::APInt(32, 0);
20690 else if (S->getString().getAsInteger(Radix: 0, Result&: fill))
20691 return false;
20692
20693 if (Context.getTargetInfo().isNan2008()) {
20694 if (SNaN)
20695 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20696 else
20697 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20698 } else {
20699 // Prior to IEEE 754-2008, architectures were allowed to choose whether
20700 // the first bit of their significand was set for qNaN or sNaN. MIPS chose
20701 // a different encoding to what became a standard in 2008, and for pre-
20702 // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as
20703 // sNaN. This is now known as "legacy NaN" encoding.
20704 if (SNaN)
20705 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20706 else
20707 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20708 }
20709
20710 return true;
20711}
20712
20713bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
20714 if (!IsConstantEvaluatedBuiltinCall(E))
20715 return ExprEvaluatorBaseTy::VisitCallExpr(E);
20716
20717 unsigned BuiltinOp = ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E);
20718
20719 switch (BuiltinOp) {
20720 default:
20721 return false;
20722
20723 case Builtin::BI__builtin_huge_val:
20724 case Builtin::BI__builtin_huge_valf:
20725 case Builtin::BI__builtin_huge_vall:
20726 case Builtin::BI__builtin_huge_valf16:
20727 case Builtin::BI__builtin_huge_valf128:
20728 case Builtin::BI__builtin_inf:
20729 case Builtin::BI__builtin_inff:
20730 case Builtin::BI__builtin_infl:
20731 case Builtin::BI__builtin_inff16:
20732 case Builtin::BI__builtin_inff128: {
20733 const llvm::fltSemantics &Sem =
20734 Info.Ctx.getFloatTypeSemantics(T: E->getType());
20735 Result = llvm::APFloat::getInf(Sem);
20736 return true;
20737 }
20738
20739 case Builtin::BI__builtin_nans:
20740 case Builtin::BI__builtin_nansf:
20741 case Builtin::BI__builtin_nansl:
20742 case Builtin::BI__builtin_nansf16:
20743 case Builtin::BI__builtin_nansf128:
20744 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20745 SNaN: true, Result))
20746 return Error(E);
20747 return true;
20748
20749 case Builtin::BI__builtin_nan:
20750 case Builtin::BI__builtin_nanf:
20751 case Builtin::BI__builtin_nanl:
20752 case Builtin::BI__builtin_nanf16:
20753 case Builtin::BI__builtin_nanf128:
20754 // If this is __builtin_nan() turn this into a nan, otherwise we
20755 // can't constant fold it.
20756 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20757 SNaN: false, Result))
20758 return Error(E);
20759 return true;
20760
20761 case Builtin::BI__builtin_elementwise_abs:
20762 case Builtin::BI__builtin_fabs:
20763 case Builtin::BI__builtin_fabsf:
20764 case Builtin::BI__builtin_fabsl:
20765 case Builtin::BI__builtin_fabsf128:
20766 // The C standard says "fabs raises no floating-point exceptions,
20767 // even if x is a signaling NaN. The returned value is independent of
20768 // the current rounding direction mode." Therefore constant folding can
20769 // proceed without regard to the floating point settings.
20770 // Reference, WG14 N2478 F.10.4.3
20771 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info))
20772 return false;
20773
20774 if (Result.isNegative())
20775 Result.changeSign();
20776 return true;
20777
20778 case Builtin::BI__arithmetic_fence:
20779 return EvaluateFloat(E: E->getArg(Arg: 0), Result, Info);
20780
20781 // FIXME: Builtin::BI__builtin_powi
20782 // FIXME: Builtin::BI__builtin_powif
20783 // FIXME: Builtin::BI__builtin_powil
20784
20785 case Builtin::BI__builtin_copysign:
20786 case Builtin::BI__builtin_copysignf:
20787 case Builtin::BI__builtin_copysignl:
20788 case Builtin::BI__builtin_copysignf128: {
20789 APFloat RHS(0.);
20790 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20791 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20792 return false;
20793 Result.copySign(RHS);
20794 return true;
20795 }
20796
20797 case Builtin::BI__builtin_fmax:
20798 case Builtin::BI__builtin_fmaxf:
20799 case Builtin::BI__builtin_fmaxl:
20800 case Builtin::BI__builtin_fmaxf16:
20801 case Builtin::BI__builtin_fmaxf128: {
20802 APFloat RHS(0.);
20803 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20804 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20805 return false;
20806 Result = maxnum(A: Result, B: RHS);
20807 return true;
20808 }
20809
20810 case Builtin::BI__builtin_fmin:
20811 case Builtin::BI__builtin_fminf:
20812 case Builtin::BI__builtin_fminl:
20813 case Builtin::BI__builtin_fminf16:
20814 case Builtin::BI__builtin_fminf128: {
20815 APFloat RHS(0.);
20816 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20817 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20818 return false;
20819 Result = minnum(A: Result, B: RHS);
20820 return true;
20821 }
20822
20823 case Builtin::BI__builtin_fmaximum_num:
20824 case Builtin::BI__builtin_fmaximum_numf:
20825 case Builtin::BI__builtin_fmaximum_numl:
20826 case Builtin::BI__builtin_fmaximum_numf16:
20827 case Builtin::BI__builtin_fmaximum_numf128: {
20828 APFloat RHS(0.);
20829 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20830 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20831 return false;
20832 Result = maximumnum(A: Result, B: RHS);
20833 return true;
20834 }
20835
20836 case Builtin::BI__builtin_fminimum_num:
20837 case Builtin::BI__builtin_fminimum_numf:
20838 case Builtin::BI__builtin_fminimum_numl:
20839 case Builtin::BI__builtin_fminimum_numf16:
20840 case Builtin::BI__builtin_fminimum_numf128: {
20841 APFloat RHS(0.);
20842 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20843 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20844 return false;
20845 Result = minimumnum(A: Result, B: RHS);
20846 return true;
20847 }
20848
20849 case Builtin::BI__builtin_elementwise_fma: {
20850 if (!E->getArg(Arg: 0)->isPRValue() || !E->getArg(Arg: 1)->isPRValue() ||
20851 !E->getArg(Arg: 2)->isPRValue()) {
20852 return false;
20853 }
20854 APFloat SourceY(0.), SourceZ(0.);
20855 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20856 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: SourceY, Info) ||
20857 !EvaluateFloat(E: E->getArg(Arg: 2), Result&: SourceZ, Info))
20858 return false;
20859 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
20860 (void)Result.fusedMultiplyAdd(Multiplicand: SourceY, Addend: SourceZ, RM);
20861 return true;
20862 }
20863
20864 case clang::X86::BI__builtin_ia32_vec_ext_v4sf: {
20865 APValue Vec;
20866 APSInt IdxAPS;
20867 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
20868 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
20869 return false;
20870 unsigned N = Vec.getVectorLength();
20871 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
20872 return Success(V: Vec.getVectorElt(I: Idx), e: E);
20873 }
20874 }
20875}
20876
20877bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
20878 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20879 ComplexValue CV;
20880 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
20881 return false;
20882 Result = CV.FloatReal;
20883 return true;
20884 }
20885
20886 return Visit(S: E->getSubExpr());
20887}
20888
20889bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
20890 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20891 ComplexValue CV;
20892 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
20893 return false;
20894 Result = CV.FloatImag;
20895 return true;
20896 }
20897
20898 VisitIgnoredValue(E: E->getSubExpr());
20899 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(T: E->getType());
20900 Result = llvm::APFloat::getZero(Sem);
20901 return true;
20902}
20903
20904bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20905 switch (E->getOpcode()) {
20906 default: return Error(E);
20907 case UO_Plus:
20908 return EvaluateFloat(E: E->getSubExpr(), Result, Info);
20909 case UO_Minus:
20910 // In C standard, WG14 N2478 F.3 p4
20911 // "the unary - raises no floating point exceptions,
20912 // even if the operand is signalling."
20913 if (!EvaluateFloat(E: E->getSubExpr(), Result, Info))
20914 return false;
20915 Result.changeSign();
20916 return true;
20917 }
20918}
20919
20920bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20921 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20922 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20923
20924 APFloat RHS(0.0);
20925 bool LHSOK = EvaluateFloat(E: E->getLHS(), Result, Info);
20926 if (!LHSOK && !Info.noteFailure())
20927 return false;
20928 return EvaluateFloat(E: E->getRHS(), Result&: RHS, Info) && LHSOK &&
20929 handleFloatFloatBinOp(Info, E, LHS&: Result, Opcode: E->getOpcode(), RHS);
20930}
20931
20932bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
20933 Result = E->getValue();
20934 return true;
20935}
20936
20937bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
20938 const Expr* SubExpr = E->getSubExpr();
20939
20940 switch (E->getCastKind()) {
20941 default:
20942 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20943
20944 case CK_HLSLAggregateSplatCast:
20945 llvm_unreachable("invalid cast kind for floating value");
20946
20947 case CK_IntegralToFloating: {
20948 APSInt IntResult;
20949 const FPOptions FPO = E->getFPFeaturesInEffect(
20950 LO: Info.Ctx.getLangOpts());
20951 return EvaluateInteger(E: SubExpr, Result&: IntResult, Info) &&
20952 HandleIntToFloatCast(Info, E, FPO, SrcType: SubExpr->getType(),
20953 Value: IntResult, DestType: E->getType(), Result);
20954 }
20955
20956 case CK_FixedPointToFloating: {
20957 APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
20958 if (!EvaluateFixedPoint(E: SubExpr, Result&: FixResult, Info))
20959 return false;
20960 Result =
20961 FixResult.convertToFloat(FloatSema: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
20962 return true;
20963 }
20964
20965 case CK_FloatingCast: {
20966 if (!Visit(S: SubExpr))
20967 return false;
20968 return HandleFloatToFloatCast(Info, E, SrcType: SubExpr->getType(), DestType: E->getType(),
20969 Result);
20970 }
20971
20972 case CK_FloatingComplexToReal: {
20973 ComplexValue V;
20974 if (!EvaluateComplex(E: SubExpr, Res&: V, Info))
20975 return false;
20976 Result = V.getComplexFloatReal();
20977 return true;
20978 }
20979 case CK_HLSLVectorTruncation: {
20980 APValue Val;
20981 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
20982 return Error(E);
20983 return Success(V: Val.getVectorElt(I: 0), e: E);
20984 }
20985 case CK_HLSLMatrixTruncation: {
20986 APValue Val;
20987 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
20988 return Error(E);
20989 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), e: E);
20990 }
20991 case CK_HLSLElementwiseCast: {
20992 SmallVector<APValue> SrcVals;
20993 SmallVector<QualType> SrcTypes;
20994
20995 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: E->getType(), SrcVals,
20996 SrcTypes))
20997 return false;
20998
20999 // Cast our single element.
21000 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
21001 APValue ResultVal;
21002 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: E->getType(), Original: SrcVals[0],
21003 Result&: ResultVal))
21004 return false;
21005 return Success(V: ResultVal, e: E);
21006 }
21007 }
21008}
21009
21010//===----------------------------------------------------------------------===//
21011// Complex Evaluation (for float and integer)
21012//===----------------------------------------------------------------------===//
21013
21014namespace {
21015class ComplexExprEvaluator
21016 : public ExprEvaluatorBase<ComplexExprEvaluator> {
21017 ComplexValue &Result;
21018
21019public:
21020 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
21021 : ExprEvaluatorBaseTy(info), Result(Result) {}
21022
21023 bool Success(const APValue &V, const Expr *e) {
21024 Result.setFrom(V);
21025 return true;
21026 }
21027
21028 bool ZeroInitialization(const Expr *E);
21029
21030 //===--------------------------------------------------------------------===//
21031 // Visitor Methods
21032 //===--------------------------------------------------------------------===//
21033
21034 bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
21035 bool VisitCastExpr(const CastExpr *E);
21036 bool VisitBinaryOperator(const BinaryOperator *E);
21037 bool VisitUnaryOperator(const UnaryOperator *E);
21038 bool VisitInitListExpr(const InitListExpr *E);
21039 bool VisitCallExpr(const CallExpr *E);
21040};
21041} // end anonymous namespace
21042
21043static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
21044 EvalInfo &Info) {
21045 assert(!E->isValueDependent());
21046 assert(E->isPRValue() && E->getType()->isAnyComplexType());
21047 return ComplexExprEvaluator(Info, Result).Visit(S: E);
21048}
21049
21050bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
21051 QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();
21052 if (ElemTy->isRealFloatingType()) {
21053 Result.makeComplexFloat();
21054 APFloat Zero = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy));
21055 Result.FloatReal = Zero;
21056 Result.FloatImag = Zero;
21057 } else {
21058 Result.makeComplexInt();
21059 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy);
21060 Result.IntReal = Zero;
21061 Result.IntImag = Zero;
21062 }
21063 return true;
21064}
21065
21066bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
21067 const Expr* SubExpr = E->getSubExpr();
21068
21069 if (SubExpr->getType()->isRealFloatingType()) {
21070 Result.makeComplexFloat();
21071 APFloat &Imag = Result.FloatImag;
21072 if (!EvaluateFloat(E: SubExpr, Result&: Imag, Info))
21073 return false;
21074
21075 Result.FloatReal = APFloat(Imag.getSemantics());
21076 return true;
21077 } else {
21078 assert(SubExpr->getType()->isIntegerType() &&
21079 "Unexpected imaginary literal.");
21080
21081 Result.makeComplexInt();
21082 APSInt &Imag = Result.IntImag;
21083 if (!EvaluateInteger(E: SubExpr, Result&: Imag, Info))
21084 return false;
21085
21086 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
21087 return true;
21088 }
21089}
21090
21091bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
21092
21093 switch (E->getCastKind()) {
21094 case CK_BitCast:
21095 case CK_BaseToDerived:
21096 case CK_DerivedToBase:
21097 case CK_UncheckedDerivedToBase:
21098 case CK_Dynamic:
21099 case CK_ToUnion:
21100 case CK_ArrayToPointerDecay:
21101 case CK_FunctionToPointerDecay:
21102 case CK_NullToPointer:
21103 case CK_NullToMemberPointer:
21104 case CK_BaseToDerivedMemberPointer:
21105 case CK_DerivedToBaseMemberPointer:
21106 case CK_MemberPointerToBoolean:
21107 case CK_ReinterpretMemberPointer:
21108 case CK_ConstructorConversion:
21109 case CK_IntegralToPointer:
21110 case CK_PointerToIntegral:
21111 case CK_PointerToBoolean:
21112 case CK_ToVoid:
21113 case CK_VectorSplat:
21114 case CK_IntegralCast:
21115 case CK_BooleanToSignedIntegral:
21116 case CK_IntegralToBoolean:
21117 case CK_IntegralToFloating:
21118 case CK_FloatingToIntegral:
21119 case CK_FloatingToBoolean:
21120 case CK_FloatingCast:
21121 case CK_CPointerToObjCPointerCast:
21122 case CK_BlockPointerToObjCPointerCast:
21123 case CK_AnyPointerToBlockPointerCast:
21124 case CK_ObjCObjectLValueCast:
21125 case CK_FloatingComplexToReal:
21126 case CK_FloatingComplexToBoolean:
21127 case CK_IntegralComplexToReal:
21128 case CK_IntegralComplexToBoolean:
21129 case CK_ARCProduceObject:
21130 case CK_ARCConsumeObject:
21131 case CK_ARCReclaimReturnedObject:
21132 case CK_ARCExtendBlockObject:
21133 case CK_CopyAndAutoreleaseBlockObject:
21134 case CK_BuiltinFnToFnPtr:
21135 case CK_ZeroToOCLOpaqueType:
21136 case CK_NonAtomicToAtomic:
21137 case CK_AddressSpaceConversion:
21138 case CK_IntToOCLSampler:
21139 case CK_FloatingToFixedPoint:
21140 case CK_FixedPointToFloating:
21141 case CK_FixedPointCast:
21142 case CK_FixedPointToBoolean:
21143 case CK_FixedPointToIntegral:
21144 case CK_IntegralToFixedPoint:
21145 case CK_MatrixCast:
21146 case CK_HLSLVectorTruncation:
21147 case CK_HLSLMatrixTruncation:
21148 case CK_HLSLElementwiseCast:
21149 case CK_HLSLAggregateSplatCast:
21150 llvm_unreachable("invalid cast kind for complex value");
21151
21152 case CK_LValueToRValue:
21153 case CK_AtomicToNonAtomic:
21154 case CK_NoOp:
21155 case CK_LValueToRValueBitCast:
21156 case CK_HLSLArrayRValue:
21157 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21158
21159 case CK_Dependent:
21160 case CK_LValueBitCast:
21161 case CK_UserDefinedConversion:
21162 return Error(E);
21163
21164 case CK_FloatingRealToComplex: {
21165 APFloat &Real = Result.FloatReal;
21166 if (!EvaluateFloat(E: E->getSubExpr(), Result&: Real, Info))
21167 return false;
21168
21169 Result.makeComplexFloat();
21170 Result.FloatImag = APFloat(Real.getSemantics());
21171 return true;
21172 }
21173
21174 case CK_FloatingComplexCast: {
21175 if (!Visit(S: E->getSubExpr()))
21176 return false;
21177
21178 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21179 QualType From
21180 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21181
21182 return HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatReal) &&
21183 HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatImag);
21184 }
21185
21186 case CK_FloatingComplexToIntegralComplex: {
21187 if (!Visit(S: E->getSubExpr()))
21188 return false;
21189
21190 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21191 QualType From
21192 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21193 Result.makeComplexInt();
21194 return HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatReal,
21195 DestType: To, Result&: Result.IntReal) &&
21196 HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatImag,
21197 DestType: To, Result&: Result.IntImag);
21198 }
21199
21200 case CK_IntegralRealToComplex: {
21201 APSInt &Real = Result.IntReal;
21202 if (!EvaluateInteger(E: E->getSubExpr(), Result&: Real, Info))
21203 return false;
21204
21205 Result.makeComplexInt();
21206 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
21207 return true;
21208 }
21209
21210 case CK_IntegralComplexCast: {
21211 if (!Visit(S: E->getSubExpr()))
21212 return false;
21213
21214 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21215 QualType From
21216 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21217
21218 Result.IntReal = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntReal);
21219 Result.IntImag = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntImag);
21220 return true;
21221 }
21222
21223 case CK_IntegralComplexToFloatingComplex: {
21224 if (!Visit(S: E->getSubExpr()))
21225 return false;
21226
21227 const FPOptions FPO = E->getFPFeaturesInEffect(
21228 LO: Info.Ctx.getLangOpts());
21229 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21230 QualType From
21231 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21232 Result.makeComplexFloat();
21233 return HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntReal,
21234 DestType: To, Result&: Result.FloatReal) &&
21235 HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntImag,
21236 DestType: To, Result&: Result.FloatImag);
21237 }
21238 }
21239
21240 llvm_unreachable("unknown cast resulting in complex value");
21241}
21242
21243uint8_t GFNIMultiplicativeInverse(uint8_t Byte) {
21244 // Lookup Table for Multiplicative Inverse in GF(2^8)
21245 const uint8_t GFInv[256] = {
21246 0x00, 0x01, 0x8d, 0xf6, 0xcb, 0x52, 0x7b, 0xd1, 0xe8, 0x4f, 0x29, 0xc0,
21247 0xb0, 0xe1, 0xe5, 0xc7, 0x74, 0xb4, 0xaa, 0x4b, 0x99, 0x2b, 0x60, 0x5f,
21248 0x58, 0x3f, 0xfd, 0xcc, 0xff, 0x40, 0xee, 0xb2, 0x3a, 0x6e, 0x5a, 0xf1,
21249 0x55, 0x4d, 0xa8, 0xc9, 0xc1, 0x0a, 0x98, 0x15, 0x30, 0x44, 0xa2, 0xc2,
21250 0x2c, 0x45, 0x92, 0x6c, 0xf3, 0x39, 0x66, 0x42, 0xf2, 0x35, 0x20, 0x6f,
21251 0x77, 0xbb, 0x59, 0x19, 0x1d, 0xfe, 0x37, 0x67, 0x2d, 0x31, 0xf5, 0x69,
21252 0xa7, 0x64, 0xab, 0x13, 0x54, 0x25, 0xe9, 0x09, 0xed, 0x5c, 0x05, 0xca,
21253 0x4c, 0x24, 0x87, 0xbf, 0x18, 0x3e, 0x22, 0xf0, 0x51, 0xec, 0x61, 0x17,
21254 0x16, 0x5e, 0xaf, 0xd3, 0x49, 0xa6, 0x36, 0x43, 0xf4, 0x47, 0x91, 0xdf,
21255 0x33, 0x93, 0x21, 0x3b, 0x79, 0xb7, 0x97, 0x85, 0x10, 0xb5, 0xba, 0x3c,
21256 0xb6, 0x70, 0xd0, 0x06, 0xa1, 0xfa, 0x81, 0x82, 0x83, 0x7e, 0x7f, 0x80,
21257 0x96, 0x73, 0xbe, 0x56, 0x9b, 0x9e, 0x95, 0xd9, 0xf7, 0x02, 0xb9, 0xa4,
21258 0xde, 0x6a, 0x32, 0x6d, 0xd8, 0x8a, 0x84, 0x72, 0x2a, 0x14, 0x9f, 0x88,
21259 0xf9, 0xdc, 0x89, 0x9a, 0xfb, 0x7c, 0x2e, 0xc3, 0x8f, 0xb8, 0x65, 0x48,
21260 0x26, 0xc8, 0x12, 0x4a, 0xce, 0xe7, 0xd2, 0x62, 0x0c, 0xe0, 0x1f, 0xef,
21261 0x11, 0x75, 0x78, 0x71, 0xa5, 0x8e, 0x76, 0x3d, 0xbd, 0xbc, 0x86, 0x57,
21262 0x0b, 0x28, 0x2f, 0xa3, 0xda, 0xd4, 0xe4, 0x0f, 0xa9, 0x27, 0x53, 0x04,
21263 0x1b, 0xfc, 0xac, 0xe6, 0x7a, 0x07, 0xae, 0x63, 0xc5, 0xdb, 0xe2, 0xea,
21264 0x94, 0x8b, 0xc4, 0xd5, 0x9d, 0xf8, 0x90, 0x6b, 0xb1, 0x0d, 0xd6, 0xeb,
21265 0xc6, 0x0e, 0xcf, 0xad, 0x08, 0x4e, 0xd7, 0xe3, 0x5d, 0x50, 0x1e, 0xb3,
21266 0x5b, 0x23, 0x38, 0x34, 0x68, 0x46, 0x03, 0x8c, 0xdd, 0x9c, 0x7d, 0xa0,
21267 0xcd, 0x1a, 0x41, 0x1c};
21268
21269 return GFInv[Byte];
21270}
21271
21272uint8_t GFNIAffine(uint8_t XByte, const APInt &AQword, const APSInt &Imm,
21273 bool Inverse) {
21274 unsigned NumBitsInByte = 8;
21275 // Computing the affine transformation
21276 uint8_t RetByte = 0;
21277 for (uint32_t BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
21278 uint8_t AByte =
21279 AQword.lshr(shiftAmt: (7 - static_cast<int32_t>(BitIdx)) * NumBitsInByte)
21280 .getLoBits(numBits: 8)
21281 .getZExtValue();
21282 uint8_t Product;
21283 if (Inverse) {
21284 Product = AByte & GFNIMultiplicativeInverse(Byte: XByte);
21285 } else {
21286 Product = AByte & XByte;
21287 }
21288 uint8_t Parity = 0;
21289
21290 // Dot product in GF(2) uses XOR instead of addition
21291 for (unsigned PBitIdx = 0; PBitIdx != NumBitsInByte; ++PBitIdx) {
21292 Parity = Parity ^ ((Product >> PBitIdx) & 0x1);
21293 }
21294
21295 uint8_t Temp = Imm[BitIdx] ? 1 : 0;
21296 RetByte |= (Temp ^ Parity) << BitIdx;
21297 }
21298 return RetByte;
21299}
21300
21301uint8_t GFNIMul(uint8_t AByte, uint8_t BByte) {
21302 // Multiplying two polynomials of degree 7
21303 // Polynomial of degree 7
21304 // x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
21305 uint16_t TWord = 0;
21306 unsigned NumBitsInByte = 8;
21307 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
21308 if ((BByte >> BitIdx) & 0x1) {
21309 TWord = TWord ^ (AByte << BitIdx);
21310 }
21311 }
21312
21313 // When multiplying two polynomials of degree 7
21314 // results in a polynomial of degree 14
21315 // so the result has to be reduced to 7
21316 // Reduction polynomial is x^8 + x^4 + x^3 + x + 1 i.e. 0x11B
21317 for (int32_t BitIdx = 14; BitIdx > 7; --BitIdx) {
21318 if ((TWord >> BitIdx) & 0x1) {
21319 TWord = TWord ^ (0x11B << (BitIdx - 8));
21320 }
21321 }
21322 return (TWord & 0xFF);
21323}
21324
21325void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D,
21326 APFloat &ResR, APFloat &ResI) {
21327 // This is an implementation of complex multiplication according to the
21328 // constraints laid out in C11 Annex G. The implementation uses the
21329 // following naming scheme:
21330 // (a + ib) * (c + id)
21331
21332 APFloat AC = A * C;
21333 APFloat BD = B * D;
21334 APFloat AD = A * D;
21335 APFloat BC = B * C;
21336 ResR = AC - BD;
21337 ResI = AD + BC;
21338 if (ResR.isNaN() && ResI.isNaN()) {
21339 bool Recalc = false;
21340 if (A.isInfinity() || B.isInfinity()) {
21341 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
21342 Sign: A);
21343 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
21344 Sign: B);
21345 if (C.isNaN())
21346 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
21347 if (D.isNaN())
21348 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
21349 Recalc = true;
21350 }
21351 if (C.isInfinity() || D.isInfinity()) {
21352 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
21353 Sign: C);
21354 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
21355 Sign: D);
21356 if (A.isNaN())
21357 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
21358 if (B.isNaN())
21359 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
21360 Recalc = true;
21361 }
21362 if (!Recalc && (AC.isInfinity() || BD.isInfinity() || AD.isInfinity() ||
21363 BC.isInfinity())) {
21364 if (A.isNaN())
21365 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
21366 if (B.isNaN())
21367 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
21368 if (C.isNaN())
21369 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
21370 if (D.isNaN())
21371 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
21372 Recalc = true;
21373 }
21374 if (Recalc) {
21375 ResR = APFloat::getInf(Sem: A.getSemantics()) * (A * C - B * D);
21376 ResI = APFloat::getInf(Sem: A.getSemantics()) * (A * D + B * C);
21377 }
21378 }
21379}
21380
21381void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D,
21382 APFloat &ResR, APFloat &ResI) {
21383 // This is an implementation of complex division according to the
21384 // constraints laid out in C11 Annex G. The implementation uses the
21385 // following naming scheme:
21386 // (a + ib) / (c + id)
21387
21388 int DenomLogB = 0;
21389 APFloat MaxCD = maxnum(A: abs(X: C), B: abs(X: D));
21390 if (MaxCD.isFinite()) {
21391 DenomLogB = ilogb(Arg: MaxCD);
21392 C = scalbn(X: C, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21393 D = scalbn(X: D, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21394 }
21395 APFloat Denom = C * C + D * D;
21396 ResR =
21397 scalbn(X: (A * C + B * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21398 ResI =
21399 scalbn(X: (B * C - A * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21400 if (ResR.isNaN() && ResI.isNaN()) {
21401 if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) {
21402 ResR = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * A;
21403 ResI = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * B;
21404 } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() &&
21405 D.isFinite()) {
21406 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
21407 Sign: A);
21408 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
21409 Sign: B);
21410 ResR = APFloat::getInf(Sem: ResR.getSemantics()) * (A * C + B * D);
21411 ResI = APFloat::getInf(Sem: ResI.getSemantics()) * (B * C - A * D);
21412 } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) {
21413 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
21414 Sign: C);
21415 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
21416 Sign: D);
21417 ResR = APFloat::getZero(Sem: ResR.getSemantics()) * (A * C + B * D);
21418 ResI = APFloat::getZero(Sem: ResI.getSemantics()) * (B * C - A * D);
21419 }
21420 }
21421}
21422
21423APSInt NormalizeRotateAmount(const APSInt &Value, const APSInt &Amount) {
21424 // Normalize shift amount to [0, BitWidth) range to match runtime behavior
21425 APSInt NormAmt = Amount;
21426 unsigned BitWidth = Value.getBitWidth();
21427 unsigned AmtBitWidth = NormAmt.getBitWidth();
21428 if (BitWidth == 1) {
21429 // Rotating a 1-bit value is always a no-op
21430 NormAmt = APSInt(APInt(AmtBitWidth, 0), NormAmt.isUnsigned());
21431 } else if (BitWidth == 2) {
21432 // For 2-bit values: rotation amount is 0 or 1 based on
21433 // whether the amount is even or odd. We can't use srem here because
21434 // the divisor (2) would be misinterpreted as -2 in 2-bit signed arithmetic.
21435 NormAmt =
21436 APSInt(APInt(AmtBitWidth, NormAmt[0] ? 1 : 0), NormAmt.isUnsigned());
21437 } else {
21438 APInt Divisor;
21439 if (AmtBitWidth > BitWidth) {
21440 Divisor = llvm::APInt(AmtBitWidth, BitWidth);
21441 } else {
21442 Divisor = llvm::APInt(BitWidth, BitWidth);
21443 if (AmtBitWidth < BitWidth) {
21444 NormAmt = NormAmt.extend(width: BitWidth);
21445 }
21446 }
21447
21448 // Normalize to [0, BitWidth)
21449 if (NormAmt.isSigned()) {
21450 NormAmt = APSInt(NormAmt.srem(RHS: Divisor), /*isUnsigned=*/false);
21451 if (NormAmt.isNegative()) {
21452 APSInt SignedDivisor(Divisor, /*isUnsigned=*/false);
21453 NormAmt += SignedDivisor;
21454 }
21455 } else {
21456 NormAmt = APSInt(NormAmt.urem(RHS: Divisor), /*isUnsigned=*/true);
21457 }
21458 }
21459
21460 return NormAmt;
21461}
21462
21463bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
21464 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
21465 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
21466
21467 // Track whether the LHS or RHS is real at the type system level. When this is
21468 // the case we can simplify our evaluation strategy.
21469 bool LHSReal = false, RHSReal = false;
21470
21471 bool LHSOK;
21472 if (E->getLHS()->getType()->isRealFloatingType()) {
21473 LHSReal = true;
21474 APFloat &Real = Result.FloatReal;
21475 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: Real, Info);
21476 if (LHSOK) {
21477 Result.makeComplexFloat();
21478 Result.FloatImag = APFloat(Real.getSemantics());
21479 }
21480 } else {
21481 LHSOK = Visit(S: E->getLHS());
21482 }
21483 if (!LHSOK && !Info.noteFailure())
21484 return false;
21485
21486 ComplexValue RHS;
21487 if (E->getRHS()->getType()->isRealFloatingType()) {
21488 RHSReal = true;
21489 APFloat &Real = RHS.FloatReal;
21490 if (!EvaluateFloat(E: E->getRHS(), Result&: Real, Info) || !LHSOK)
21491 return false;
21492 RHS.makeComplexFloat();
21493 RHS.FloatImag = APFloat(Real.getSemantics());
21494 } else if (!EvaluateComplex(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
21495 return false;
21496
21497 assert(!(LHSReal && RHSReal) &&
21498 "Cannot have both operands of a complex operation be real.");
21499 switch (E->getOpcode()) {
21500 default: return Error(E);
21501 case BO_Add:
21502 if (Result.isComplexFloat()) {
21503 Result.getComplexFloatReal().add(RHS: RHS.getComplexFloatReal(),
21504 RM: APFloat::rmNearestTiesToEven);
21505 if (LHSReal)
21506 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
21507 else if (!RHSReal)
21508 Result.getComplexFloatImag().add(RHS: RHS.getComplexFloatImag(),
21509 RM: APFloat::rmNearestTiesToEven);
21510 } else {
21511 Result.getComplexIntReal() += RHS.getComplexIntReal();
21512 Result.getComplexIntImag() += RHS.getComplexIntImag();
21513 }
21514 break;
21515 case BO_Sub:
21516 if (Result.isComplexFloat()) {
21517 Result.getComplexFloatReal().subtract(RHS: RHS.getComplexFloatReal(),
21518 RM: APFloat::rmNearestTiesToEven);
21519 if (LHSReal) {
21520 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
21521 Result.getComplexFloatImag().changeSign();
21522 } else if (!RHSReal) {
21523 Result.getComplexFloatImag().subtract(RHS: RHS.getComplexFloatImag(),
21524 RM: APFloat::rmNearestTiesToEven);
21525 }
21526 } else {
21527 Result.getComplexIntReal() -= RHS.getComplexIntReal();
21528 Result.getComplexIntImag() -= RHS.getComplexIntImag();
21529 }
21530 break;
21531 case BO_Mul:
21532 if (Result.isComplexFloat()) {
21533 // This is an implementation of complex multiplication according to the
21534 // constraints laid out in C11 Annex G. The implementation uses the
21535 // following naming scheme:
21536 // (a + ib) * (c + id)
21537 ComplexValue LHS = Result;
21538 APFloat &A = LHS.getComplexFloatReal();
21539 APFloat &B = LHS.getComplexFloatImag();
21540 APFloat &C = RHS.getComplexFloatReal();
21541 APFloat &D = RHS.getComplexFloatImag();
21542 APFloat &ResR = Result.getComplexFloatReal();
21543 APFloat &ResI = Result.getComplexFloatImag();
21544 if (LHSReal) {
21545 assert(!RHSReal && "Cannot have two real operands for a complex op!");
21546 ResR = A;
21547 ResI = A;
21548 // ResR = A * C;
21549 // ResI = A * D;
21550 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: C) ||
21551 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: D))
21552 return false;
21553 } else if (RHSReal) {
21554 // ResR = C * A;
21555 // ResI = C * B;
21556 ResR = C;
21557 ResI = C;
21558 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: A) ||
21559 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: B))
21560 return false;
21561 } else {
21562 HandleComplexComplexMul(A, B, C, D, ResR, ResI);
21563 }
21564 } else {
21565 ComplexValue LHS = Result;
21566 Result.getComplexIntReal() =
21567 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
21568 LHS.getComplexIntImag() * RHS.getComplexIntImag());
21569 Result.getComplexIntImag() =
21570 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
21571 LHS.getComplexIntImag() * RHS.getComplexIntReal());
21572 }
21573 break;
21574 case BO_Div:
21575 if (Result.isComplexFloat()) {
21576 // This is an implementation of complex division according to the
21577 // constraints laid out in C11 Annex G. The implementation uses the
21578 // following naming scheme:
21579 // (a + ib) / (c + id)
21580 ComplexValue LHS = Result;
21581 APFloat &A = LHS.getComplexFloatReal();
21582 APFloat &B = LHS.getComplexFloatImag();
21583 APFloat &C = RHS.getComplexFloatReal();
21584 APFloat &D = RHS.getComplexFloatImag();
21585 APFloat &ResR = Result.getComplexFloatReal();
21586 APFloat &ResI = Result.getComplexFloatImag();
21587 if (RHSReal) {
21588 ResR = A;
21589 ResI = B;
21590 // ResR = A / C;
21591 // ResI = B / C;
21592 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Div, RHS: C) ||
21593 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Div, RHS: C))
21594 return false;
21595 } else {
21596 if (LHSReal) {
21597 // No real optimizations we can do here, stub out with zero.
21598 B = APFloat::getZero(Sem: A.getSemantics());
21599 }
21600 HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
21601 }
21602 } else {
21603 ComplexValue LHS = Result;
21604 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
21605 RHS.getComplexIntImag() * RHS.getComplexIntImag();
21606 if (Den.isZero())
21607 return Error(E, D: diag::note_expr_divide_by_zero);
21608
21609 Result.getComplexIntReal() =
21610 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
21611 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
21612 Result.getComplexIntImag() =
21613 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
21614 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
21615 }
21616 break;
21617 }
21618
21619 return true;
21620}
21621
21622bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
21623 // Get the operand value into 'Result'.
21624 if (!Visit(S: E->getSubExpr()))
21625 return false;
21626
21627 switch (E->getOpcode()) {
21628 default:
21629 return Error(E);
21630 case UO_Extension:
21631 return true;
21632 case UO_Plus:
21633 // The result is always just the subexpr.
21634 return true;
21635 case UO_Minus:
21636 if (Result.isComplexFloat()) {
21637 Result.getComplexFloatReal().changeSign();
21638 Result.getComplexFloatImag().changeSign();
21639 }
21640 else {
21641 Result.getComplexIntReal() = -Result.getComplexIntReal();
21642 Result.getComplexIntImag() = -Result.getComplexIntImag();
21643 }
21644 return true;
21645 case UO_Not:
21646 if (Result.isComplexFloat())
21647 Result.getComplexFloatImag().changeSign();
21648 else
21649 Result.getComplexIntImag() = -Result.getComplexIntImag();
21650 return true;
21651 }
21652}
21653
21654bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
21655 if (E->getNumInits() == 2) {
21656 if (E->getType()->isComplexType()) {
21657 Result.makeComplexFloat();
21658 if (!EvaluateFloat(E: E->getInit(Init: 0), Result&: Result.FloatReal, Info))
21659 return false;
21660 if (!EvaluateFloat(E: E->getInit(Init: 1), Result&: Result.FloatImag, Info))
21661 return false;
21662 } else {
21663 Result.makeComplexInt();
21664 if (!EvaluateInteger(E: E->getInit(Init: 0), Result&: Result.IntReal, Info))
21665 return false;
21666 if (!EvaluateInteger(E: E->getInit(Init: 1), Result&: Result.IntImag, Info))
21667 return false;
21668 }
21669 return true;
21670 }
21671 return ExprEvaluatorBaseTy::VisitInitListExpr(E);
21672}
21673
21674bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) {
21675 if (!IsConstantEvaluatedBuiltinCall(E))
21676 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21677
21678 switch (E->getBuiltinCallee()) {
21679 case Builtin::BI__builtin_complex:
21680 Result.makeComplexFloat();
21681 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: Result.FloatReal, Info))
21682 return false;
21683 if (!EvaluateFloat(E: E->getArg(Arg: 1), Result&: Result.FloatImag, Info))
21684 return false;
21685 return true;
21686
21687 default:
21688 return false;
21689 }
21690}
21691
21692//===----------------------------------------------------------------------===//
21693// Atomic expression evaluation, essentially just handling the NonAtomicToAtomic
21694// implicit conversion.
21695//===----------------------------------------------------------------------===//
21696
21697namespace {
21698class AtomicExprEvaluator :
21699 public ExprEvaluatorBase<AtomicExprEvaluator> {
21700 const LValue *This;
21701 APValue &Result;
21702public:
21703 AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result)
21704 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
21705
21706 bool Success(const APValue &V, const Expr *E) {
21707 Result = V;
21708 return true;
21709 }
21710
21711 bool ZeroInitialization(const Expr *E) {
21712 ImplicitValueInitExpr VIE(
21713 E->getType()->castAs<AtomicType>()->getValueType());
21714 // For atomic-qualified class (and array) types in C++, initialize the
21715 // _Atomic-wrapped subobject directly, in-place.
21716 return This ? EvaluateInPlace(Result, Info, This: *This, E: &VIE)
21717 : Evaluate(Result, Info, E: &VIE);
21718 }
21719
21720 bool VisitCastExpr(const CastExpr *E) {
21721 switch (E->getCastKind()) {
21722 default:
21723 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21724 case CK_NullToPointer:
21725 VisitIgnoredValue(E: E->getSubExpr());
21726 return ZeroInitialization(E);
21727 case CK_NonAtomicToAtomic:
21728 return This ? EvaluateInPlace(Result, Info, This: *This, E: E->getSubExpr())
21729 : Evaluate(Result, Info, E: E->getSubExpr());
21730 }
21731 }
21732};
21733} // end anonymous namespace
21734
21735static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
21736 EvalInfo &Info) {
21737 assert(!E->isValueDependent());
21738 assert(E->isPRValue() && E->getType()->isAtomicType());
21739 return AtomicExprEvaluator(Info, This, Result).Visit(S: E);
21740}
21741
21742//===----------------------------------------------------------------------===//
21743// Void expression evaluation, primarily for a cast to void on the LHS of a
21744// comma operator
21745//===----------------------------------------------------------------------===//
21746
21747namespace {
21748class VoidExprEvaluator
21749 : public ExprEvaluatorBase<VoidExprEvaluator> {
21750public:
21751 VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
21752
21753 bool Success(const APValue &V, const Expr *e) { return true; }
21754
21755 bool ZeroInitialization(const Expr *E) { return true; }
21756
21757 bool VisitCastExpr(const CastExpr *E) {
21758 switch (E->getCastKind()) {
21759 default:
21760 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21761 case CK_ToVoid:
21762 VisitIgnoredValue(E: E->getSubExpr());
21763 return true;
21764 }
21765 }
21766
21767 bool VisitCallExpr(const CallExpr *E) {
21768 if (!IsConstantEvaluatedBuiltinCall(E))
21769 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21770
21771 switch (E->getBuiltinCallee()) {
21772 case Builtin::BI__assume:
21773 case Builtin::BI__builtin_assume:
21774 // The argument is not evaluated!
21775 return true;
21776
21777 case Builtin::BI__builtin_operator_delete:
21778 return HandleOperatorDeleteCall(Info, E);
21779
21780 case Builtin::BIstdc_memreverse8:
21781 case Builtin::BI__builtin_stdc_memreverse8: {
21782 APSInt N;
21783 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: N, Info))
21784 return false;
21785 uint64_t NElems = N.getZExtValue();
21786
21787 LValue Ptr;
21788 if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Ptr, Info))
21789 return false;
21790
21791 if (!Ptr.checkNullPointerForFoldAccess(Info, E, AK: AK_Assign) ||
21792 Ptr.Designator.Invalid)
21793 return false;
21794
21795 QualType CharTy = Ptr.Designator.getType(Ctx&: Info.Ctx);
21796 uint64_t RemainingElems = Ptr.Designator.validIndexAdjustments().second;
21797 if (NElems > RemainingElems) {
21798 uint64_t ArrayIndex =
21799 Ptr.Designator.MostDerivedIsArrayElement
21800 ? Ptr.Designator.Entries.back().getAsArrayIndex()
21801 : (uint64_t)Ptr.Designator.IsOnePastTheEnd;
21802 APSInt Index =
21803 APSInt::getUnsigned(X: llvm::SaturatingAdd(X: ArrayIndex, Y: NElems - 1));
21804 Ptr.Designator.diagnosePointerArithmetic(Info, E, N: Index);
21805 return false;
21806 }
21807
21808 if (NElems <= 1)
21809 return true;
21810
21811 LValue Lo = Ptr;
21812 LValue Hi = Ptr;
21813 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Hi, EltTy: CharTy, Adjustment: NElems - 1))
21814 return false;
21815
21816 for (uint64_t I = 0, Half = NElems / 2; I < Half; ++I) {
21817 APValue LoVal, HiVal;
21818 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: Lo, RVal&: LoVal) ||
21819 !handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: Hi, RVal&: HiVal) ||
21820 !handleAssignment(Info, E, LVal: Lo, LValType: CharTy, Val&: HiVal) ||
21821 !handleAssignment(Info, E, LVal: Hi, LValType: CharTy, Val&: LoVal) ||
21822 !HandleLValueArrayAdjustment(Info, E, LVal&: Lo, EltTy: CharTy, Adjustment: 1) ||
21823 !HandleLValueArrayAdjustment(Info, E, LVal&: Hi, EltTy: CharTy, Adjustment: -1))
21824 return false;
21825 }
21826 return true;
21827 }
21828
21829 default:
21830 return false;
21831 }
21832 }
21833
21834 bool VisitCXXDeleteExpr(const CXXDeleteExpr *E);
21835};
21836} // end anonymous namespace
21837
21838bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
21839 // We cannot speculatively evaluate a delete expression.
21840 if (Info.SpeculativeEvaluationDepth)
21841 return false;
21842
21843 FunctionDecl *OperatorDelete = E->getOperatorDelete();
21844 if (!OperatorDelete
21845 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
21846 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
21847 << isa<CXXMethodDecl>(Val: OperatorDelete) << OperatorDelete;
21848 return false;
21849 }
21850
21851 const Expr *Arg = E->getArgument();
21852
21853 LValue Pointer;
21854 if (!EvaluatePointer(E: Arg, Result&: Pointer, Info))
21855 return false;
21856 if (Pointer.Designator.Invalid)
21857 return false;
21858
21859 // Deleting a null pointer has no effect.
21860 if (Pointer.isNullPointer()) {
21861 // This is the only case where we need to produce an extension warning:
21862 // the only other way we can succeed is if we find a dynamic allocation,
21863 // and we will have warned when we allocated it in that case.
21864 if (!Info.getLangOpts().CPlusPlus20)
21865 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
21866 return true;
21867 }
21868
21869 std::optional<DynAlloc *> Alloc = CheckDeleteKind(
21870 Info, E, Pointer,
21871 DeallocKind: E->isArrayForm() ? DynAllocKind::ArrayNew : DynAllocKind::New);
21872 if (!Alloc)
21873 return false;
21874 QualType AllocType = Pointer.Base.getDynamicAllocType();
21875
21876 // For the non-array case, the designator must be empty if the static type
21877 // does not have a virtual destructor.
21878 if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 &&
21879 !hasVirtualDestructor(T: Arg->getType()->getPointeeType())) {
21880 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_base_nonvirt_dtor)
21881 << Arg->getType()->getPointeeType() << AllocType;
21882 return false;
21883 }
21884
21885 // For a class type with a virtual destructor, the selected operator delete
21886 // is the one looked up when building the destructor.
21887 if (!E->isArrayForm() && !E->isGlobalDelete()) {
21888 const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(T: AllocType);
21889 if (VirtualDelete &&
21890 !VirtualDelete
21891 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
21892 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
21893 << isa<CXXMethodDecl>(Val: VirtualDelete) << VirtualDelete;
21894 return false;
21895 }
21896 }
21897
21898 if (!HandleDestruction(Info, Loc: E->getExprLoc(), LVBase: Pointer.getLValueBase(),
21899 Value&: (*Alloc)->Value, T: AllocType))
21900 return false;
21901
21902 if (!Info.HeapAllocs.erase(x: Pointer.Base.dyn_cast<DynamicAllocLValue>())) {
21903 // The element was already erased. This means the destructor call also
21904 // deleted the object.
21905 // FIXME: This probably results in undefined behavior before we get this
21906 // far, and should be diagnosed elsewhere first.
21907 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
21908 return false;
21909 }
21910
21911 return true;
21912}
21913
21914static bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
21915 assert(!E->isValueDependent());
21916 assert(E->isPRValue() && E->getType()->isVoidType());
21917 return VoidExprEvaluator(Info).Visit(S: E);
21918}
21919
21920//===----------------------------------------------------------------------===//
21921// Top level Expr::EvaluateAsRValue method.
21922//===----------------------------------------------------------------------===//
21923
21924static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {
21925 assert(!E->isValueDependent());
21926 // In C, function designators are not lvalues, but we evaluate them as if they
21927 // are.
21928 QualType T = E->getType();
21929 if (E->isGLValue() || T->isFunctionType()) {
21930 LValue LV;
21931 if (!EvaluateLValue(E, Result&: LV, Info))
21932 return false;
21933 LV.moveInto(V&: Result);
21934 } else if (T->isVectorType()) {
21935 if (!EvaluateVector(E, Result, Info))
21936 return false;
21937 } else if (T->isConstantMatrixType()) {
21938 if (!EvaluateMatrix(E, Result, Info))
21939 return false;
21940 } else if (T->isIntegralOrEnumerationType()) {
21941 if (!IntExprEvaluator(Info, Result).Visit(S: E))
21942 return false;
21943 } else if (T->isMetaInfoType()) {
21944 if (!EvaluateReflection(E, Result, Info))
21945 return false;
21946 } else if (T->hasPointerRepresentation()) {
21947 LValue LV;
21948 if (!EvaluatePointer(E, Result&: LV, Info))
21949 return false;
21950 LV.moveInto(V&: Result);
21951 } else if (T->isRealFloatingType()) {
21952 llvm::APFloat F(0.0);
21953 if (!EvaluateFloat(E, Result&: F, Info))
21954 return false;
21955 Result = APValue(F);
21956 } else if (T->isAnyComplexType()) {
21957 ComplexValue C;
21958 if (!EvaluateComplex(E, Result&: C, Info))
21959 return false;
21960 C.moveInto(v&: Result);
21961 } else if (T->isFixedPointType()) {
21962 if (!FixedPointExprEvaluator(Info, Result).Visit(S: E)) return false;
21963 } else if (T->isMemberPointerType()) {
21964 MemberPtr P;
21965 if (!EvaluateMemberPointer(E, Result&: P, Info))
21966 return false;
21967 P.moveInto(V&: Result);
21968 return true;
21969 } else if (T->isArrayType()) {
21970 LValue LV;
21971 APValue &Value =
21972 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
21973 if (!EvaluateArray(E, This: LV, Result&: Value, Info))
21974 return false;
21975 Result = Value;
21976 } else if (T->isRecordType()) {
21977 LValue LV;
21978 APValue &Value =
21979 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
21980 if (!EvaluateRecord(E, This: LV, Result&: Value, Info))
21981 return false;
21982 Result = Value;
21983 } else if (T->isVoidType()) {
21984 if (!Info.getLangOpts().CPlusPlus11)
21985 Info.CCEDiag(E, DiagId: diag::note_constexpr_nonliteral)
21986 << E->getType();
21987 if (!EvaluateVoid(E, Info))
21988 return false;
21989 } else if (T->isAtomicType()) {
21990 QualType Unqual = T.getAtomicUnqualifiedType();
21991 if (Unqual->isArrayType() || Unqual->isRecordType()) {
21992 LValue LV;
21993 APValue &Value = Info.CurrentCall->createTemporary(
21994 Key: E, T: Unqual, Scope: ScopeKind::FullExpression, LV);
21995 if (!EvaluateAtomic(E, This: &LV, Result&: Value, Info))
21996 return false;
21997 Result = Value;
21998 } else {
21999 if (!EvaluateAtomic(E, This: nullptr, Result, Info))
22000 return false;
22001 }
22002 } else if (Info.getLangOpts().CPlusPlus11) {
22003 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral) << E->getType();
22004 return false;
22005 } else {
22006 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
22007 return false;
22008 }
22009
22010 return true;
22011}
22012
22013/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some
22014/// cases, the in-place evaluation is essential, since later initializers for
22015/// an object can indirectly refer to subobjects which were initialized earlier.
22016static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,
22017 const Expr *E, bool AllowNonLiteralTypes) {
22018 assert(!E->isValueDependent());
22019
22020 // Normally expressions passed to EvaluateInPlace have a type, but not when
22021 // a VarDecl initializer is evaluated before the untyped ParenListExpr is
22022 // replaced with a CXXConstructExpr. This can happen in LLDB.
22023 if (E->getType().isNull())
22024 return false;
22025
22026 if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, This: &This))
22027 return false;
22028
22029 if (E->isPRValue()) {
22030 // Evaluate arrays and record types in-place, so that later initializers can
22031 // refer to earlier-initialized members of the object.
22032 QualType T = E->getType();
22033 if (T->isArrayType())
22034 return EvaluateArray(E, This, Result, Info);
22035 else if (T->isRecordType())
22036 return EvaluateRecord(E, This, Result, Info);
22037 else if (T->isAtomicType()) {
22038 QualType Unqual = T.getAtomicUnqualifiedType();
22039 if (Unqual->isArrayType() || Unqual->isRecordType())
22040 return EvaluateAtomic(E, This: &This, Result, Info);
22041 }
22042 }
22043
22044 // For any other type, in-place evaluation is unimportant.
22045 return Evaluate(Result, Info, E);
22046}
22047
22048/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
22049/// lvalue-to-rvalue cast if it is an lvalue.
22050static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
22051 assert(!E->isValueDependent());
22052 assert(!Info.Ctx.getLangOpts().EnableNewConstInterp);
22053
22054 if (E->getType().isNull())
22055 return false;
22056
22057 if (!CheckLiteralType(Info, E))
22058 return false;
22059
22060 if (!::Evaluate(Result, Info, E))
22061 return false;
22062
22063 // Implicit lvalue-to-rvalue cast.
22064 if (E->isGLValue()) {
22065 LValue LV;
22066 LV.setFrom(Ctx: Info.Ctx, V: Result);
22067 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
22068 return false;
22069 }
22070
22071 // Check this core constant expression is a constant expression.
22072 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
22073 Kind: ConstantExprKind::Normal) &&
22074 CheckMemoryLeaks(Info);
22075}
22076
22077static bool FastEvaluateAsRValue(const Expr *Exp, APValue &Result,
22078 const ASTContext &Ctx, bool &IsConst) {
22079 // Fast-path evaluations of integer literals, since we sometimes see files
22080 // containing vast quantities of these.
22081 if (const auto *L = dyn_cast<IntegerLiteral>(Val: Exp)) {
22082 Result =
22083 APValue(APSInt(L->getValue(), L->getType()->isUnsignedIntegerType()));
22084 IsConst = true;
22085 return true;
22086 }
22087
22088 if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Val: Exp)) {
22089 Result = APValue(APSInt(APInt(1, L->getValue())));
22090 IsConst = true;
22091 return true;
22092 }
22093
22094 if (const auto *FL = dyn_cast<FloatingLiteral>(Val: Exp)) {
22095 Result = APValue(FL->getValue());
22096 IsConst = true;
22097 return true;
22098 }
22099
22100 if (const auto *L = dyn_cast<CharacterLiteral>(Val: Exp)) {
22101 Result = APValue(Ctx.MakeIntValue(Value: L->getValue(), Type: L->getType()));
22102 IsConst = true;
22103 return true;
22104 }
22105
22106 if (const auto *CE = dyn_cast<ConstantExpr>(Val: Exp)) {
22107 if (CE->hasAPValueResult()) {
22108 APValue APV = CE->getAPValueResult();
22109 if (!APV.isLValue()) {
22110 Result = std::move(APV);
22111 IsConst = true;
22112 return true;
22113 }
22114 }
22115
22116 // The SubExpr is usually just an IntegerLiteral.
22117 return FastEvaluateAsRValue(Exp: CE->getSubExpr(), Result, Ctx, IsConst);
22118 }
22119
22120 // This case should be rare, but we need to check it before we check on
22121 // the type below.
22122 if (Exp->getType().isNull()) {
22123 IsConst = false;
22124 return true;
22125 }
22126
22127 return false;
22128}
22129
22130static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result,
22131 Expr::SideEffectsKind SEK) {
22132 return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) ||
22133 (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior);
22134}
22135
22136static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result,
22137 const ASTContext &Ctx, EvalInfo &Info) {
22138 assert(!E->isValueDependent());
22139 bool IsConst;
22140 if (FastEvaluateAsRValue(Exp: E, Result&: Result.Val, Ctx, IsConst))
22141 return IsConst;
22142
22143 return EvaluateAsRValue(Info, E, Result&: Result.Val);
22144}
22145
22146static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult,
22147 const ASTContext &Ctx,
22148 Expr::SideEffectsKind AllowSideEffects,
22149 EvalInfo &Info) {
22150 assert(!E->isValueDependent());
22151 if (!E->getType()->isIntegralOrEnumerationType())
22152 return false;
22153
22154 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info) ||
22155 !ExprResult.Val.isInt() ||
22156 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
22157 return false;
22158
22159 return true;
22160}
22161
22162static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult,
22163 const ASTContext &Ctx,
22164 Expr::SideEffectsKind AllowSideEffects,
22165 EvalInfo &Info) {
22166 assert(!E->isValueDependent());
22167 if (!E->getType()->isFixedPointType())
22168 return false;
22169
22170 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info))
22171 return false;
22172
22173 if (!ExprResult.Val.isFixedPoint() ||
22174 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
22175 return false;
22176
22177 return true;
22178}
22179
22180/// EvaluateAsRValue - Return true if this is a constant which we can fold using
22181/// any crazy technique (that has nothing to do with language standards) that
22182/// we want to. If this function returns true, it returns the folded constant
22183/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
22184/// will be applied to the result.
22185bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx,
22186 bool InConstantContext) const {
22187 assert(!isValueDependent() &&
22188 "Expression evaluator can't be called on a dependent expression.");
22189 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsRValue");
22190
22191 bool IsConst;
22192 if (FastEvaluateAsRValue(Exp: this, Result&: Result.Val, Ctx, IsConst))
22193 return IsConst;
22194
22195 if (Ctx.getLangOpts().EnableNewConstInterp) {
22196 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, Result);
22197 Settings.InConstantContext = InConstantContext;
22198 return Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: Result.Val);
22199 }
22200
22201 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
22202 Info.InConstantContext = InConstantContext;
22203 return ::EvaluateAsRValue(E: this, Result, Ctx, Info);
22204}
22205
22206bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx,
22207 bool InConstantContext) const {
22208 assert(!isValueDependent() &&
22209 "Expression evaluator can't be called on a dependent expression.");
22210 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsBooleanCondition");
22211 EvalResult Scratch;
22212 return EvaluateAsRValue(Result&: Scratch, Ctx, InConstantContext) &&
22213 HandleConversionToBool(Val: Scratch.Val, Result);
22214}
22215
22216bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx,
22217 SideEffectsKind AllowSideEffects,
22218 bool InConstantContext) const {
22219 assert(!isValueDependent() &&
22220 "Expression evaluator can't be called on a dependent expression.");
22221
22222 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsInt");
22223
22224 if (!getType()->isIntegralOrEnumerationType())
22225 return false;
22226
22227 bool IsConst;
22228 if (FastEvaluateAsRValue(Exp: this, Result&: Result.Val, Ctx, IsConst))
22229 return IsConst;
22230
22231 if (Ctx.getLangOpts().EnableNewConstInterp) {
22232 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, Result);
22233 Settings.InConstantContext = InConstantContext;
22234 if (!Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: Result.Val))
22235 return false;
22236
22237 if (!Result.Val.isInt() ||
22238 hasUnacceptableSideEffect(Result, SEK: AllowSideEffects))
22239 return false;
22240 return true;
22241 }
22242
22243 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
22244 Info.InConstantContext = InConstantContext;
22245 return ::EvaluateAsInt(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
22246}
22247
22248bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx,
22249 SideEffectsKind AllowSideEffects,
22250 bool InConstantContext) const {
22251 assert(!isValueDependent() &&
22252 "Expression evaluator can't be called on a dependent expression.");
22253 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFixedPoint");
22254
22255 if (!getType()->isFixedPointType())
22256 return false;
22257
22258 if (Ctx.getLangOpts().EnableNewConstInterp) {
22259 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, Result);
22260 Settings.InConstantContext = InConstantContext;
22261
22262 if (!Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: Result.Val))
22263 return false;
22264
22265 if (!Result.Val.isFixedPoint() ||
22266 hasUnacceptableSideEffect(Result, SEK: AllowSideEffects))
22267 return false;
22268
22269 return true;
22270 }
22271
22272 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
22273 Info.InConstantContext = InConstantContext;
22274 return ::EvaluateAsFixedPoint(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
22275}
22276
22277bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx,
22278 SideEffectsKind AllowSideEffects,
22279 bool InConstantContext) const {
22280 assert(!isValueDependent() &&
22281 "Expression evaluator can't be called on a dependent expression.");
22282
22283 if (!getType()->isRealFloatingType())
22284 return false;
22285
22286 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFloat");
22287 EvalResult ExprResult;
22288 if (!EvaluateAsRValue(Result&: ExprResult, Ctx, InConstantContext) ||
22289 !ExprResult.Val.isFloat() ||
22290 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
22291 return false;
22292
22293 Result = ExprResult.Val.getFloat();
22294 return true;
22295}
22296
22297bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx,
22298 bool InConstantContext) const {
22299 assert(!isValueDependent() &&
22300 "Expression evaluator can't be called on a dependent expression.");
22301
22302 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsLValue");
22303
22304 if (Ctx.getLangOpts().EnableNewConstInterp) {
22305 interp::EvalSettings Settings(EvaluationMode::ConstantFold, Result);
22306 Settings.InConstantContext = InConstantContext;
22307 return Ctx.getInterpContext().evaluate(Settings, E: this, Result&: Result.Val);
22308 }
22309
22310 EvalInfo Info(Ctx, Result, EvaluationMode::ConstantFold);
22311 Info.InConstantContext = InConstantContext;
22312 LValue LV;
22313 CheckedTemporaries CheckedTemps;
22314
22315 if (!EvaluateLValue(E: this, Result&: LV, Info) || !Info.discardCleanups() ||
22316 Result.HasSideEffects ||
22317 !CheckLValueConstantExpression(Info, Loc: getExprLoc(),
22318 Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV,
22319 Kind: ConstantExprKind::Normal, CheckedTemps))
22320 return false;
22321
22322 LV.moveInto(V&: Result.Val);
22323 return true;
22324}
22325
22326static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base,
22327 APValue DestroyedValue, QualType Type,
22328 SourceLocation Loc, Expr::EvalStatus &EStatus,
22329 bool IsConstantDestruction) {
22330 EvalInfo Info(Ctx, EStatus,
22331 IsConstantDestruction ? EvaluationMode::ConstantExpression
22332 : EvaluationMode::ConstantFold);
22333 Info.setEvaluatingDecl(Base, Value&: DestroyedValue,
22334 EDK: EvalInfo::EvaluatingDeclKind::Dtor);
22335 Info.InConstantContext = IsConstantDestruction;
22336
22337 LValue LVal;
22338 LVal.set(B: Base);
22339
22340 if (!HandleDestruction(Info, Loc, LVBase: Base, Value&: DestroyedValue, T: Type) ||
22341 EStatus.HasSideEffects)
22342 return false;
22343
22344 if (!Info.discardCleanups())
22345 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22346
22347 return true;
22348}
22349
22350bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
22351 ConstantExprKind Kind) const {
22352 assert(!isValueDependent() &&
22353 "Expression evaluator can't be called on a dependent expression.");
22354 bool IsConst;
22355 if (FastEvaluateAsRValue(Exp: this, Result&: Result.Val, Ctx, IsConst) &&
22356 Result.Val.hasValue())
22357 return true;
22358
22359 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr");
22360 EvaluationMode EM = Kind == ConstantExprKind::Initializer
22361 ? EvaluationMode::IgnoreSideEffects
22362 : EvaluationMode::ConstantExpression;
22363 if (Ctx.getLangOpts().EnableNewConstInterp) {
22364 interp::EvalSettings Settings(EM, Result, Kind);
22365 Settings.InConstantContext = true;
22366 return Ctx.getInterpContext().evaluate(Settings, E: this, Result&: Result.Val) &&
22367 !Result.HasSideEffects;
22368 }
22369
22370 EvalInfo Info(Ctx, Result, EM);
22371 Info.InConstantContext = true;
22372
22373 // The type of the object we're initializing is 'const T' for a class NTTP.
22374 QualType T = getType();
22375 if (Kind == ConstantExprKind::ClassTemplateArgument)
22376 T.addConst();
22377
22378 // If we're evaluating a prvalue, fake up a MaterializeTemporaryExpr to
22379 // represent the result of the evaluation. CheckConstantExpression ensures
22380 // this doesn't escape.
22381 MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true);
22382 APValue::LValueBase Base(&BaseMTE);
22383 Info.setEvaluatingDecl(Base, Value&: Result.Val);
22384
22385 LValue LVal;
22386 LVal.set(B: Base);
22387 // C++23 [intro.execution]/p5
22388 // A full-expression is [...] a constant-expression
22389 // So we need to make sure temporary objects are destroyed after having
22390 // evaluating the expression (per C++23 [class.temporary]/p4).
22391 FullExpressionRAII Scope(Info);
22392 if (!::EvaluateInPlace(Result&: Result.Val, Info, This: LVal, E: this) || !Scope.destroy())
22393 return false;
22394
22395 if (!Info.discardCleanups())
22396 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22397
22398 if (Result.HasSideEffects ||
22399 !CheckConstantExpression(Info, DiagLoc: getExprLoc(), Type: getStorageType(Ctx, E: this),
22400 Value: Result.Val, Kind))
22401 return false;
22402 if (!CheckMemoryLeaks(Info))
22403 return false;
22404
22405 // If this is a class template argument, it's required to have constant
22406 // destruction too.
22407 if (Kind == ConstantExprKind::ClassTemplateArgument &&
22408 (!EvaluateDestruction(Ctx, Base, DestroyedValue: Result.Val, Type: T, Loc: getBeginLoc(), EStatus&: Result,
22409 IsConstantDestruction: true) ||
22410 Result.HasSideEffects)) {
22411 // FIXME: Prefix a note to indicate that the problem is lack of constant
22412 // destruction.
22413 return false;
22414 }
22415 return true;
22416}
22417
22418bool Expr::EvaluateAsInitializer(const ASTContext &Ctx, const VarDecl *VD,
22419 Expr::EvalResult &EStatus,
22420 bool IsConstantInitialization) const {
22421 assert(!isValueDependent() &&
22422 "Expression evaluator can't be called on a dependent expression.");
22423 assert(VD && "Need a valid VarDecl");
22424
22425 llvm::TimeTraceScope TimeScope("EvaluateAsInitializer", [&] {
22426 std::string Name;
22427 llvm::raw_string_ostream OS(Name);
22428 VD->printQualifiedName(OS);
22429 return Name;
22430 });
22431
22432 EvaluationMode EvalMode =
22433 (IsConstantInitialization &&
22434 (Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23))
22435 ? EvaluationMode::ConstantExpression
22436 : EvaluationMode::ConstantFold;
22437
22438 if (Ctx.getLangOpts().EnableNewConstInterp) {
22439 interp::EvalSettings Settings(EvalMode, EStatus);
22440 Settings.InConstantContext = IsConstantInitialization;
22441 return Ctx.getInterpContext().evaluateAsInitializer(Settings, VD, Init: this,
22442 Result&: EStatus.Val);
22443 }
22444
22445 SourceLocation DeclLoc = VD->getLocation();
22446 QualType DeclTy = VD->getType();
22447
22448 EvalInfo Info(Ctx, EStatus, EvalMode);
22449 Info.setEvaluatingDecl(Base: VD, Value&: EStatus.Val);
22450 Info.InConstantContext = IsConstantInitialization;
22451
22452 LValue LVal;
22453 LVal.set(B: VD);
22454
22455 {
22456 // C++23 [intro.execution]/p5
22457 // A full-expression is ... an init-declarator ([dcl.decl]) or a
22458 // mem-initializer.
22459 // So we need to make sure temporary objects are destroyed after having
22460 // evaluated the expression (per C++23 [class.temporary]/p4).
22461 //
22462 // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the
22463 // serialization code calls ParmVarDecl::getDefaultArg() which strips the
22464 // outermost FullExpr, such as ExprWithCleanups.
22465 FullExpressionRAII Scope(Info);
22466 if (!EvaluateInPlace(Result&: EStatus.Val, Info, This: LVal, E: this,
22467 /*AllowNonLiteralTypes=*/true) ||
22468 EStatus.HasSideEffects)
22469 return false;
22470 }
22471
22472 // At this point, any lifetime-extended temporaries are completely
22473 // initialized.
22474 Info.performLifetimeExtension();
22475
22476 if (!Info.discardCleanups())
22477 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22478 return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value: EStatus.Val,
22479 Kind: ConstantExprKind::Normal) &&
22480 CheckMemoryLeaks(Info);
22481}
22482
22483bool VarDecl::evaluateDestruction(
22484 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
22485 // This function is only meaningful for records and arrays of records.
22486 QualType VarTy = getType();
22487 if (VarTy->isArrayType()) {
22488 QualType ElemTy = getASTContext().getBaseElementType(QT: VarTy);
22489 if (!ElemTy->isRecordType()) {
22490 ensureEvaluatedStmt()->HasConstantDestruction = true;
22491 return true;
22492 }
22493 } else if (!VarTy->isRecordType()) {
22494 ensureEvaluatedStmt()->HasConstantDestruction = true;
22495 return true;
22496 }
22497
22498 Expr::EvalStatus EStatus;
22499 EStatus.Diag = &Notes;
22500
22501 // Only treat the destruction as constant destruction if we formally have
22502 // constant initialization (or are usable in a constant expression).
22503 bool IsConstantDestruction = hasConstantInitialization();
22504 ASTContext &Ctx = getASTContext();
22505
22506 // Make a copy of the value for the destructor to mutate, if we know it.
22507 // Otherwise, treat the value as default-initialized; if the destructor works
22508 // anyway, then the destruction is constant (and must be essentially empty).
22509 APValue DestroyedValue;
22510 if (getEvaluatedValue())
22511 DestroyedValue = *getEvaluatedValue();
22512 else if (!handleDefaultInitValue(T: VarTy, Result&: DestroyedValue))
22513 return false;
22514
22515 if (Ctx.getLangOpts().EnableNewConstInterp) {
22516 interp::EvalSettings Settings(IsConstantDestruction
22517 ? EvaluationMode::ConstantExpression
22518 : EvaluationMode::ConstantFold,
22519 EStatus);
22520 Settings.InConstantContext = IsConstantDestruction;
22521 if (!Ctx.getInterpContext().evaluateDestruction(Settings, VD: this,
22522 Value: std::move(DestroyedValue)))
22523 return false;
22524 ensureEvaluatedStmt()->HasConstantDestruction = true;
22525 return true;
22526 }
22527
22528 if (!EvaluateDestruction(Ctx, Base: this, DestroyedValue: std::move(DestroyedValue), Type: VarTy,
22529 Loc: getLocation(), EStatus, IsConstantDestruction) ||
22530 EStatus.HasSideEffects)
22531 return false;
22532
22533 ensureEvaluatedStmt()->HasConstantDestruction = true;
22534 return true;
22535}
22536
22537/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
22538/// constant folded, but discard the result.
22539bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const {
22540 assert(!isValueDependent() &&
22541 "Expression evaluator can't be called on a dependent expression.");
22542
22543 EvalResult Result;
22544 return EvaluateAsRValue(Result, Ctx, /* in constant context */ InConstantContext: true) &&
22545 !hasUnacceptableSideEffect(Result, SEK);
22546}
22547
22548APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
22549 assert(!isValueDependent() &&
22550 "Expression evaluator can't be called on a dependent expression.");
22551
22552 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstInt");
22553 EvalResult EVResult;
22554
22555 if (Ctx.getLangOpts().EnableNewConstInterp) {
22556 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, EVResult);
22557 Settings.InConstantContext = true;
22558 [[maybe_unused]] bool Result =
22559 Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: EVResult.Val);
22560 assert(Result && "Could not evaluate expression");
22561 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22562
22563 return EVResult.Val.getInt();
22564 }
22565
22566 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22567 Info.InConstantContext = true;
22568
22569 bool Result = ::EvaluateAsRValue(E: this, Result&: EVResult, Ctx, Info);
22570 (void)Result;
22571 assert(Result && "Could not evaluate expression");
22572 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22573
22574 return EVResult.Val.getInt();
22575}
22576
22577APSInt Expr::EvaluateKnownConstIntCheckOverflow(
22578 const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const {
22579 assert(!isValueDependent() &&
22580 "Expression evaluator can't be called on a dependent expression.");
22581
22582 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstIntCheckOverflow");
22583 EvalResult EVResult;
22584 EVResult.Diag = Diag;
22585
22586 if (Ctx.getLangOpts().EnableNewConstInterp) {
22587 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, EVResult);
22588 Settings.InConstantContext = true;
22589 Settings.CheckingForUndefinedBehavior = true;
22590 [[maybe_unused]] bool Result =
22591 Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: EVResult.Val);
22592 assert(Result && "Could not evaluate expression");
22593 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22594
22595 return EVResult.Val.getInt();
22596 }
22597
22598 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22599 Info.InConstantContext = true;
22600 Info.CheckingForUndefinedBehavior = true;
22601
22602 bool Result = ::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
22603 (void)Result;
22604 assert(Result && "Could not evaluate expression");
22605 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22606
22607 return EVResult.Val.getInt();
22608}
22609
22610void Expr::EvaluateForOverflow(const ASTContext &Ctx) const {
22611 assert(!isValueDependent() &&
22612 "Expression evaluator can't be called on a dependent expression.");
22613
22614 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow");
22615 bool IsConst;
22616 EvalResult EVResult;
22617 if (FastEvaluateAsRValue(Exp: this, Result&: EVResult.Val, Ctx, IsConst))
22618 return;
22619
22620 if (Ctx.getLangOpts().EnableNewConstInterp) {
22621 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, EVResult);
22622 Settings.CheckingForUndefinedBehavior = true;
22623 (void)Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: EVResult.Val);
22624 return;
22625 }
22626
22627 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22628 Info.CheckingForUndefinedBehavior = true;
22629 (void)::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
22630}
22631
22632bool Expr::EvalResult::isGlobalLValue() const {
22633 assert(Val.isLValue());
22634 return IsGlobalLValue(B: Val.getLValueBase());
22635}
22636
22637/// isIntegerConstantExpr - this recursive routine will test if an expression is
22638/// an integer constant expression.
22639
22640/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
22641/// comma, etc
22642
22643// CheckICE - This function does the fundamental ICE checking: the returned
22644// ICEDiag contains an ICEKind indicating whether the expression is an ICE.
22645//
22646// Note that to reduce code duplication, this helper does no evaluation
22647// itself; the caller checks whether the expression is evaluatable, and
22648// in the rare cases where CheckICE actually cares about the evaluated
22649// value, it calls into Evaluate.
22650
22651namespace {
22652
22653enum ICEKind {
22654 /// This expression is an ICE.
22655 IK_ICE,
22656 /// This expression is not an ICE, but if it isn't evaluated, it's
22657 /// a legal subexpression for an ICE. This return value is used to handle
22658 /// the comma operator in C99 mode, and non-constant subexpressions.
22659 IK_ICEIfUnevaluated,
22660 /// This expression is not an ICE, and is not a legal subexpression for one.
22661 IK_NotICE
22662};
22663
22664struct ICEDiag {
22665 ICEKind Kind;
22666 SourceLocation Loc;
22667
22668 ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {}
22669};
22670
22671}
22672
22673static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); }
22674
22675static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; }
22676
22677static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) {
22678 Expr::EvalResult EVResult;
22679
22680 if (Ctx.getLangOpts().EnableNewConstInterp) {
22681 interp::EvalSettings Settings(EvaluationMode::ConstantExpression, EVResult);
22682 Settings.InConstantContext = true;
22683 if (!Ctx.getInterpContext().evaluateAsRValue(Settings, E, Result&: EVResult.Val) ||
22684 EVResult.HasSideEffects || !EVResult.Val.isInt())
22685 return ICEDiag(IK_NotICE, E->getBeginLoc());
22686 return NoDiag();
22687 }
22688
22689 Expr::EvalStatus Status;
22690 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
22691
22692 Info.InConstantContext = true;
22693 if (!::EvaluateAsRValue(E, Result&: EVResult, Ctx, Info) || EVResult.HasSideEffects ||
22694 !EVResult.Val.isInt())
22695 return ICEDiag(IK_NotICE, E->getBeginLoc());
22696
22697 return NoDiag();
22698}
22699
22700static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) {
22701 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
22702 if (!E->getType()->isIntegralOrEnumerationType())
22703 return ICEDiag(IK_NotICE, E->getBeginLoc());
22704
22705 switch (E->getStmtClass()) {
22706#define ABSTRACT_STMT(Node)
22707#define STMT(Node, Base) case Expr::Node##Class:
22708#define EXPR(Node, Base)
22709#include "clang/AST/StmtNodes.inc"
22710 case Expr::PredefinedExprClass:
22711 case Expr::FloatingLiteralClass:
22712 case Expr::ImaginaryLiteralClass:
22713 case Expr::StringLiteralClass:
22714 case Expr::ArraySubscriptExprClass:
22715 case Expr::MatrixSingleSubscriptExprClass:
22716 case Expr::MatrixSubscriptExprClass:
22717 case Expr::ArraySectionExprClass:
22718 case Expr::OMPArrayShapingExprClass:
22719 case Expr::OMPIteratorExprClass:
22720 case Expr::CompoundAssignOperatorClass:
22721 case Expr::CompoundLiteralExprClass:
22722 case Expr::ExtVectorElementExprClass:
22723 case Expr::MatrixElementExprClass:
22724 case Expr::DesignatedInitExprClass:
22725 case Expr::ArrayInitLoopExprClass:
22726 case Expr::ArrayInitIndexExprClass:
22727 case Expr::NoInitExprClass:
22728 case Expr::DesignatedInitUpdateExprClass:
22729 case Expr::ImplicitValueInitExprClass:
22730 case Expr::ParenListExprClass:
22731 case Expr::VAArgExprClass:
22732 case Expr::AddrLabelExprClass:
22733 case Expr::StmtExprClass:
22734 case Expr::CXXMemberCallExprClass:
22735 case Expr::CUDAKernelCallExprClass:
22736 case Expr::CXXAddrspaceCastExprClass:
22737 case Expr::CXXDynamicCastExprClass:
22738 case Expr::CXXTypeidExprClass:
22739 case Expr::CXXUuidofExprClass:
22740 case Expr::MSPropertyRefExprClass:
22741 case Expr::MSPropertySubscriptExprClass:
22742 case Expr::CXXNullPtrLiteralExprClass:
22743 case Expr::UserDefinedLiteralClass:
22744 case Expr::CXXThisExprClass:
22745 case Expr::CXXThrowExprClass:
22746 case Expr::CXXNewExprClass:
22747 case Expr::CXXDeleteExprClass:
22748 case Expr::CXXPseudoDestructorExprClass:
22749 case Expr::UnresolvedLookupExprClass:
22750 case Expr::RecoveryExprClass:
22751 case Expr::DependentScopeDeclRefExprClass:
22752 case Expr::DependentTemplateIdExprClass:
22753 case Expr::CXXConstructExprClass:
22754 case Expr::CXXInheritedCtorInitExprClass:
22755 case Expr::CXXStdInitializerListExprClass:
22756 case Expr::CXXBindTemporaryExprClass:
22757 case Expr::ExprWithCleanupsClass:
22758 case Expr::CXXTemporaryObjectExprClass:
22759 case Expr::CXXUnresolvedConstructExprClass:
22760 case Expr::CXXDependentScopeMemberExprClass:
22761 case Expr::UnresolvedMemberExprClass:
22762 case Expr::ObjCStringLiteralClass:
22763 case Expr::ObjCBoxedExprClass:
22764 case Expr::ObjCArrayLiteralClass:
22765 case Expr::ObjCDictionaryLiteralClass:
22766 case Expr::ObjCEncodeExprClass:
22767 case Expr::ObjCMessageExprClass:
22768 case Expr::ObjCSelectorExprClass:
22769 case Expr::ObjCProtocolExprClass:
22770 case Expr::ObjCIvarRefExprClass:
22771 case Expr::ObjCPropertyRefExprClass:
22772 case Expr::ObjCSubscriptRefExprClass:
22773 case Expr::ObjCIsaExprClass:
22774 case Expr::ObjCAvailabilityCheckExprClass:
22775 case Expr::ShuffleVectorExprClass:
22776 case Expr::ConvertVectorExprClass:
22777 case Expr::BlockExprClass:
22778 case Expr::NoStmtClass:
22779 case Expr::OpaqueValueExprClass:
22780 case Expr::PackExpansionExprClass:
22781 case Expr::SubstNonTypeTemplateParmPackExprClass:
22782 case Expr::FunctionParmPackExprClass:
22783 case Expr::AsTypeExprClass:
22784 case Expr::ObjCIndirectCopyRestoreExprClass:
22785 case Expr::MaterializeTemporaryExprClass:
22786 case Expr::PseudoObjectExprClass:
22787 case Expr::AtomicExprClass:
22788 case Expr::LambdaExprClass:
22789 case Expr::CXXFoldExprClass:
22790 case Expr::CoawaitExprClass:
22791 case Expr::DependentCoawaitExprClass:
22792 case Expr::CoyieldExprClass:
22793 case Expr::SYCLUniqueStableNameExprClass:
22794 case Expr::CXXParenListInitExprClass:
22795 case Expr::HLSLOutArgExprClass:
22796 case Expr::CXXExpansionSelectExprClass:
22797 return ICEDiag(IK_NotICE, E->getBeginLoc());
22798
22799 case Expr::MemberExprClass: {
22800 if (Ctx.getLangOpts().C23) {
22801 const Expr *ME = E->IgnoreParenImpCasts();
22802 while (const auto *M = dyn_cast<MemberExpr>(Val: ME)) {
22803 if (M->isArrow())
22804 return ICEDiag(IK_NotICE, E->getBeginLoc());
22805 ME = M->getBase()->IgnoreParenImpCasts();
22806 }
22807 const auto *DRE = dyn_cast<DeclRefExpr>(Val: ME);
22808 if (DRE) {
22809 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl());
22810 VD && VD->isConstexpr())
22811 return CheckEvalInICE(E, Ctx);
22812 }
22813 }
22814 return ICEDiag(IK_NotICE, E->getBeginLoc());
22815 }
22816
22817 case Expr::InitListExprClass: {
22818 // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the
22819 // form "T x = { a };" is equivalent to "T x = a;".
22820 // Unless we're initializing a reference, T is a scalar as it is known to be
22821 // of integral or enumeration type.
22822 if (E->isPRValue())
22823 if (cast<InitListExpr>(Val: E)->getNumInits() == 1)
22824 return CheckICE(E: cast<InitListExpr>(Val: E)->getInit(Init: 0), Ctx);
22825 return ICEDiag(IK_NotICE, E->getBeginLoc());
22826 }
22827
22828 case Expr::SizeOfPackExprClass:
22829 case Expr::GNUNullExprClass:
22830 case Expr::SourceLocExprClass:
22831 case Expr::EmbedExprClass:
22832 case Expr::OpenACCAsteriskSizeExprClass:
22833 return NoDiag();
22834
22835 case Expr::PackIndexingExprClass:
22836 return CheckICE(E: cast<PackIndexingExpr>(Val: E)->getSelectedExpr(), Ctx);
22837
22838 case Expr::SubstNonTypeTemplateParmExprClass:
22839 return
22840 CheckICE(E: cast<SubstNonTypeTemplateParmExpr>(Val: E)->getReplacement(), Ctx);
22841
22842 case Expr::ConstantExprClass:
22843 return CheckICE(E: cast<ConstantExpr>(Val: E)->getSubExpr(), Ctx);
22844
22845 case Expr::ParenExprClass:
22846 return CheckICE(E: cast<ParenExpr>(Val: E)->getSubExpr(), Ctx);
22847 case Expr::GenericSelectionExprClass:
22848 return CheckICE(E: cast<GenericSelectionExpr>(Val: E)->getResultExpr(), Ctx);
22849 case Expr::IntegerLiteralClass:
22850 case Expr::FixedPointLiteralClass:
22851 case Expr::CharacterLiteralClass:
22852 case Expr::ObjCBoolLiteralExprClass:
22853 case Expr::CXXBoolLiteralExprClass:
22854 case Expr::CXXScalarValueInitExprClass:
22855 case Expr::TypeTraitExprClass:
22856 case Expr::ConceptSpecializationExprClass:
22857 case Expr::RequiresExprClass:
22858 case Expr::ArrayTypeTraitExprClass:
22859 case Expr::ExpressionTraitExprClass:
22860 case Expr::CXXNoexceptExprClass:
22861 case Expr::CXXReflectExprClass:
22862 return NoDiag();
22863 case Expr::CallExprClass:
22864 case Expr::CXXOperatorCallExprClass: {
22865 // C99 6.6/3 allows function calls within unevaluated subexpressions of
22866 // constant expressions, but they can never be ICEs because an ICE cannot
22867 // contain an operand of (pointer to) function type.
22868 const CallExpr *CE = cast<CallExpr>(Val: E);
22869 if (CE->getBuiltinCallee())
22870 return CheckEvalInICE(E, Ctx);
22871 return ICEDiag(IK_NotICE, E->getBeginLoc());
22872 }
22873 case Expr::CXXRewrittenBinaryOperatorClass:
22874 return CheckICE(E: cast<CXXRewrittenBinaryOperator>(Val: E)->getSemanticForm(),
22875 Ctx);
22876 case Expr::DeclRefExprClass: {
22877 const NamedDecl *D = cast<DeclRefExpr>(Val: E)->getDecl();
22878 if (isa<EnumConstantDecl>(Val: D))
22879 return NoDiag();
22880
22881 // C++ and OpenCL (FIXME: spec reference?) allow reading const-qualified
22882 // integer variables in constant expressions:
22883 //
22884 // C++ 7.1.5.1p2
22885 // A variable of non-volatile const-qualified integral or enumeration
22886 // type initialized by an ICE can be used in ICEs.
22887 //
22888 // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In
22889 // that mode, use of reference variables should not be allowed.
22890 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
22891 if (VD && VD->isUsableInConstantExpressions(C: Ctx) &&
22892 !VD->getType()->isReferenceType())
22893 return NoDiag();
22894
22895 return ICEDiag(IK_NotICE, E->getBeginLoc());
22896 }
22897 case Expr::UnaryOperatorClass: {
22898 const UnaryOperator *Exp = cast<UnaryOperator>(Val: E);
22899 switch (Exp->getOpcode()) {
22900 case UO_PostInc:
22901 case UO_PostDec:
22902 case UO_PreInc:
22903 case UO_PreDec:
22904 case UO_AddrOf:
22905 case UO_Deref:
22906 case UO_Coawait:
22907 // C99 6.6/3 allows increment and decrement within unevaluated
22908 // subexpressions of constant expressions, but they can never be ICEs
22909 // because an ICE cannot contain an lvalue operand.
22910 return ICEDiag(IK_NotICE, E->getBeginLoc());
22911 case UO_Extension:
22912 case UO_LNot:
22913 case UO_Plus:
22914 case UO_Minus:
22915 case UO_Not:
22916 case UO_Real:
22917 case UO_Imag:
22918 return CheckICE(E: Exp->getSubExpr(), Ctx);
22919 }
22920 llvm_unreachable("invalid unary operator class");
22921 }
22922 case Expr::OffsetOfExprClass: {
22923 // Note that per C99, offsetof must be an ICE. And AFAIK, using
22924 // EvaluateAsRValue matches the proposed gcc behavior for cases like
22925 // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect
22926 // compliance: we should warn earlier for offsetof expressions with
22927 // array subscripts that aren't ICEs, and if the array subscripts
22928 // are ICEs, the value of the offsetof must be an integer constant.
22929 return CheckEvalInICE(E, Ctx);
22930 }
22931 case Expr::UnaryExprOrTypeTraitExprClass: {
22932 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(Val: E);
22933 if ((Exp->getKind() == UETT_SizeOf) &&
22934 Exp->getTypeOfArgument()->isVariableArrayType())
22935 return ICEDiag(IK_NotICE, E->getBeginLoc());
22936 if (Exp->getKind() == UETT_CountOf) {
22937 QualType ArgTy = Exp->getTypeOfArgument();
22938 if (ArgTy->isVariableArrayType()) {
22939 // We need to look whether the array is multidimensional. If it is,
22940 // then we want to check the size expression manually to see whether
22941 // it is an ICE or not.
22942 const auto *VAT = Ctx.getAsVariableArrayType(T: ArgTy);
22943 if (VAT->getElementType()->isArrayType())
22944 // Variable array size expression could be missing (e.g. int a[*][10])
22945 // In that case, it can't be a constant expression.
22946 return VAT->getSizeExpr() ? CheckICE(E: VAT->getSizeExpr(), Ctx)
22947 : ICEDiag(IK_NotICE, E->getBeginLoc());
22948
22949 // Otherwise, this is a regular VLA, which is definitely not an ICE.
22950 return ICEDiag(IK_NotICE, E->getBeginLoc());
22951 }
22952 }
22953 return NoDiag();
22954 }
22955 case Expr::BinaryOperatorClass: {
22956 const BinaryOperator *Exp = cast<BinaryOperator>(Val: E);
22957 switch (Exp->getOpcode()) {
22958 case BO_PtrMemD:
22959 case BO_PtrMemI:
22960 case BO_Assign:
22961 case BO_MulAssign:
22962 case BO_DivAssign:
22963 case BO_RemAssign:
22964 case BO_AddAssign:
22965 case BO_SubAssign:
22966 case BO_ShlAssign:
22967 case BO_ShrAssign:
22968 case BO_AndAssign:
22969 case BO_XorAssign:
22970 case BO_OrAssign:
22971 // C99 6.6/3 allows assignments within unevaluated subexpressions of
22972 // constant expressions, but they can never be ICEs because an ICE cannot
22973 // contain an lvalue operand.
22974 return ICEDiag(IK_NotICE, E->getBeginLoc());
22975
22976 case BO_Mul:
22977 case BO_Div:
22978 case BO_Rem:
22979 case BO_Add:
22980 case BO_Sub:
22981 case BO_Shl:
22982 case BO_Shr:
22983 case BO_LT:
22984 case BO_GT:
22985 case BO_LE:
22986 case BO_GE:
22987 case BO_EQ:
22988 case BO_NE:
22989 case BO_And:
22990 case BO_Xor:
22991 case BO_Or:
22992 case BO_Comma:
22993 case BO_Cmp: {
22994 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
22995 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
22996 if (Exp->getOpcode() == BO_Div ||
22997 Exp->getOpcode() == BO_Rem) {
22998 // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
22999 // we don't evaluate one.
23000 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) {
23001 llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
23002 if (REval == 0)
23003 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
23004 if (REval.isSigned() && REval.isAllOnes()) {
23005 llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
23006 if (LEval.isMinSignedValue())
23007 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
23008 }
23009 }
23010 }
23011 if (Exp->getOpcode() == BO_Comma) {
23012 if (Ctx.getLangOpts().C99) {
23013 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
23014 // if it isn't evaluated.
23015 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE)
23016 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
23017 } else {
23018 // In both C89 and C++, commas in ICEs are illegal.
23019 return ICEDiag(IK_NotICE, E->getBeginLoc());
23020 }
23021 }
23022 return Worst(A: LHSResult, B: RHSResult);
23023 }
23024 case BO_LAnd:
23025 case BO_LOr: {
23026 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
23027 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
23028 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) {
23029 // Rare case where the RHS has a comma "side-effect"; we need
23030 // to actually check the condition to see whether the side
23031 // with the comma is evaluated.
23032 if ((Exp->getOpcode() == BO_LAnd) !=
23033 (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
23034 return RHSResult;
23035 return NoDiag();
23036 }
23037
23038 return Worst(A: LHSResult, B: RHSResult);
23039 }
23040 }
23041 llvm_unreachable("invalid binary operator kind");
23042 }
23043 case Expr::ImplicitCastExprClass:
23044 case Expr::CStyleCastExprClass:
23045 case Expr::CXXFunctionalCastExprClass:
23046 case Expr::CXXStaticCastExprClass:
23047 case Expr::CXXReinterpretCastExprClass:
23048 case Expr::CXXConstCastExprClass:
23049 case Expr::ObjCBridgedCastExprClass: {
23050 const Expr *SubExpr = cast<CastExpr>(Val: E)->getSubExpr();
23051 if (isa<ExplicitCastExpr>(Val: E)) {
23052 if (const FloatingLiteral *FL
23053 = dyn_cast<FloatingLiteral>(Val: SubExpr->IgnoreParenImpCasts())) {
23054 unsigned DestWidth = Ctx.getIntWidth(T: E->getType());
23055 bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
23056 APSInt IgnoredVal(DestWidth, !DestSigned);
23057 bool Ignored;
23058 // If the value does not fit in the destination type, the behavior is
23059 // undefined, so we are not required to treat it as a constant
23060 // expression.
23061 if (FL->getValue().convertToInteger(Result&: IgnoredVal,
23062 RM: llvm::APFloat::rmTowardZero,
23063 IsExact: &Ignored) & APFloat::opInvalidOp)
23064 return ICEDiag(IK_NotICE, E->getBeginLoc());
23065 return NoDiag();
23066 }
23067 }
23068 switch (cast<CastExpr>(Val: E)->getCastKind()) {
23069 case CK_LValueToRValue:
23070 case CK_AtomicToNonAtomic:
23071 case CK_NonAtomicToAtomic:
23072 case CK_NoOp:
23073 case CK_IntegralToBoolean:
23074 case CK_IntegralCast:
23075 return CheckICE(E: SubExpr, Ctx);
23076 default:
23077 return ICEDiag(IK_NotICE, E->getBeginLoc());
23078 }
23079 }
23080 case Expr::BinaryConditionalOperatorClass: {
23081 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(Val: E);
23082 ICEDiag CommonResult = CheckICE(E: Exp->getCommon(), Ctx);
23083 if (CommonResult.Kind == IK_NotICE) return CommonResult;
23084 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
23085 if (FalseResult.Kind == IK_NotICE) return FalseResult;
23086 if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult;
23087 if (FalseResult.Kind == IK_ICEIfUnevaluated &&
23088 Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag();
23089 return FalseResult;
23090 }
23091 case Expr::ConditionalOperatorClass: {
23092 const ConditionalOperator *Exp = cast<ConditionalOperator>(Val: E);
23093 // If the condition (ignoring parens) is a __builtin_constant_p call,
23094 // then only the true side is actually considered in an integer constant
23095 // expression, and it is fully evaluated. This is an important GNU
23096 // extension. See GCC PR38377 for discussion.
23097 if (const CallExpr *CallCE
23098 = dyn_cast<CallExpr>(Val: Exp->getCond()->IgnoreParenCasts()))
23099 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
23100 return CheckEvalInICE(E, Ctx);
23101 ICEDiag CondResult = CheckICE(E: Exp->getCond(), Ctx);
23102 if (CondResult.Kind == IK_NotICE)
23103 return CondResult;
23104
23105 ICEDiag TrueResult = CheckICE(E: Exp->getTrueExpr(), Ctx);
23106 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
23107
23108 if (TrueResult.Kind == IK_NotICE)
23109 return TrueResult;
23110 if (FalseResult.Kind == IK_NotICE)
23111 return FalseResult;
23112 if (CondResult.Kind == IK_ICEIfUnevaluated)
23113 return CondResult;
23114 if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE)
23115 return NoDiag();
23116 // Rare case where the diagnostics depend on which side is evaluated
23117 // Note that if we get here, CondResult is 0, and at least one of
23118 // TrueResult and FalseResult is non-zero.
23119 if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0)
23120 return FalseResult;
23121 return TrueResult;
23122 }
23123 case Expr::CXXDefaultArgExprClass:
23124 return CheckICE(E: cast<CXXDefaultArgExpr>(Val: E)->getExpr(), Ctx);
23125 case Expr::CXXDefaultInitExprClass:
23126 return CheckICE(E: cast<CXXDefaultInitExpr>(Val: E)->getExpr(), Ctx);
23127 case Expr::ChooseExprClass: {
23128 return CheckICE(E: cast<ChooseExpr>(Val: E)->getChosenSubExpr(), Ctx);
23129 }
23130 case Expr::BuiltinBitCastExprClass: {
23131 if (!checkBitCastConstexprEligibility(Info: nullptr, Ctx, BCE: cast<CastExpr>(Val: E)))
23132 return ICEDiag(IK_NotICE, E->getBeginLoc());
23133 return CheckICE(E: cast<CastExpr>(Val: E)->getSubExpr(), Ctx);
23134 }
23135 }
23136
23137 llvm_unreachable("Invalid StmtClass!");
23138}
23139
23140/// Evaluate an expression as a C++11 integral constant expression.
23141static bool
23142EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, const Expr *E,
23143 llvm::APSInt *Value,
23144 bool AllowRelaxedEval = false) {
23145 if (!E->getType()->isIntegralOrUnscopedEnumerationType())
23146 return false;
23147
23148 APValue Result;
23149 if (!E->isCXX11ConstantExpr(Ctx, Result, AllowRelaxedEval))
23150 return false;
23151
23152 if (!Result.isInt())
23153 return false;
23154
23155 if (Value) *Value = Result.getInt();
23156 return true;
23157}
23158
23159bool Expr::isIntegerConstantExpr(const ASTContext &Ctx) const {
23160 assert(!isValueDependent() &&
23161 "Expression evaluator can't be called on a dependent expression.");
23162
23163 ExprTimeTraceScope TimeScope(this, Ctx, "isIntegerConstantExpr");
23164
23165 if (Ctx.getLangOpts().CPlusPlus11)
23166 return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: nullptr);
23167
23168 ICEDiag D = CheckICE(E: this, Ctx);
23169 if (D.Kind != IK_ICE)
23170 return false;
23171 return true;
23172}
23173
23174std::optional<llvm::APSInt>
23175Expr::getIntegerConstantExpr(const ASTContext &Ctx,
23176 bool AllowRelaxedEval) const {
23177 if (isValueDependent()) {
23178 // Expression evaluator can't succeed on a dependent expression.
23179 return std::nullopt;
23180 }
23181
23182 if (Ctx.getLangOpts().CPlusPlus11) {
23183 APSInt Value;
23184 if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: &Value,
23185 AllowRelaxedEval))
23186 return Value;
23187 return std::nullopt;
23188 }
23189
23190 if (!isIntegerConstantExpr(Ctx))
23191 return std::nullopt;
23192
23193 // The only possible side-effects here are due to UB discovered in the
23194 // evaluation (for instance, INT_MAX + 1). In such a case, we are still
23195 // required to treat the expression as an ICE, so we produce the folded
23196 // value.
23197 EvalResult ExprResult;
23198
23199 if (Ctx.getLangOpts().EnableNewConstInterp) {
23200 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects,
23201 ExprResult);
23202 Settings.InConstantContext = true;
23203 if (!Ctx.getInterpContext().evaluateAsRValue(Settings, E: this,
23204 Result&: ExprResult.Val))
23205 llvm_unreachable("ICE cannot be evaluated!");
23206 return ExprResult.Val.getInt();
23207 }
23208
23209 Expr::EvalStatus Status;
23210 EvalInfo Info(Ctx, Status, EvaluationMode::IgnoreSideEffects);
23211 Info.InConstantContext = true;
23212
23213 if (!::EvaluateAsInt(E: this, ExprResult, Ctx, AllowSideEffects: SE_AllowSideEffects, Info))
23214 llvm_unreachable("ICE cannot be evaluated!");
23215
23216 return ExprResult.Val.getInt();
23217}
23218
23219bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const {
23220 assert(!isValueDependent() &&
23221 "Expression evaluator can't be called on a dependent expression.");
23222
23223 return CheckICE(E: this, Ctx).Kind == IK_ICE;
23224}
23225
23226bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue &Result,
23227 bool AllowRelaxedEval) const {
23228 assert(!isValueDependent() &&
23229 "Expression evaluator can't be called on a dependent expression.");
23230
23231 // We support this checking in C++98 mode in order to diagnose compatibility
23232 // issues.
23233 assert(Ctx.getLangOpts().CPlusPlus);
23234
23235 bool IsConst;
23236 if (FastEvaluateAsRValue(Exp: this, Result, Ctx, IsConst) && Result.hasValue())
23237 return true;
23238
23239 bool IsConstExpr;
23240 Expr::EvalStatus Status;
23241 SmallVector<PartialDiagnosticAt> MSRelaxedDiag;
23242 Status.ExtendedDiag = AllowRelaxedEval ? &MSRelaxedDiag : nullptr;
23243
23244 if (Ctx.getLangOpts().EnableNewConstInterp) {
23245 interp::EvalSettings Settings(EvaluationMode::ConstantExpression, Status);
23246 IsConstExpr =
23247 Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result);
23248 } else {
23249 // Build evaluation settings.
23250 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
23251 IsConstExpr =
23252 ::EvaluateAsRValue(Info, E: this, Result) &&
23253 // NOTE: We don't produce a diagnostic for this, but the callers that
23254 // call us on arbitrary full-expressions should generally not care.
23255 Info.discardCleanups() && !Status.HasSideEffects;
23256 }
23257 return IsConstExpr && !Status.DiagEmitted;
23258}
23259
23260bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx,
23261 const FunctionDecl *Callee,
23262 ArrayRef<const Expr*> Args,
23263 const Expr *This) const {
23264 assert(!isValueDependent() &&
23265 "Expression evaluator can't be called on a dependent expression.");
23266
23267 llvm::TimeTraceScope TimeScope("EvaluateWithSubstitution", [&] {
23268 std::string Name;
23269 llvm::raw_string_ostream OS(Name);
23270 Callee->getNameForDiagnostic(OS, Policy: Ctx.getPrintingPolicy(),
23271 /*Qualified=*/true);
23272 return Name;
23273 });
23274
23275 Expr::EvalStatus Status;
23276
23277 if (Ctx.getLangOpts().EnableNewConstInterp) {
23278 interp::EvalSettings Settings(EvaluationMode::ConstantExpressionUnevaluated,
23279 Status);
23280 Settings.InConstantContext = true;
23281 if (std::optional<bool> BoolResult =
23282 Ctx.getInterpContext().evaluateWithSubstitution(Settings, Callee,
23283 Args, This, Condition: this)) {
23284 Value = APValue(APSInt(APInt(1, static_cast<uint64_t>(*BoolResult))));
23285 return true;
23286 }
23287 return false;
23288 }
23289
23290 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpressionUnevaluated);
23291 Info.InConstantContext = true;
23292
23293 LValue ThisVal;
23294 const LValue *ThisPtr = nullptr;
23295 if (This) {
23296#ifndef NDEBUG
23297 auto *MD = dyn_cast<CXXMethodDecl>(Callee);
23298 assert(MD && "Don't provide `this` for non-methods.");
23299 assert(MD->isImplicitObjectMemberFunction() &&
23300 "Don't provide `this` for methods without an implicit object.");
23301#endif
23302 if (!This->isValueDependent() &&
23303 EvaluateObjectArgument(Info, Object: This, This&: ThisVal) &&
23304 !Info.EvalStatus.HasSideEffects)
23305 ThisPtr = &ThisVal;
23306
23307 // Ignore any side-effects from a failed evaluation. This is safe because
23308 // they can't interfere with any other argument evaluation.
23309 Info.EvalStatus.HasSideEffects = false;
23310 }
23311
23312 CallRef Call = Info.CurrentCall->createCall(Callee);
23313 for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
23314 I != E; ++I) {
23315 unsigned Idx = I - Args.begin();
23316 if (Idx >= Callee->getNumParams())
23317 break;
23318 const ParmVarDecl *PVD = Callee->getParamDecl(i: Idx);
23319 if ((*I)->isValueDependent() ||
23320 !EvaluateCallArg(PVD, Arg: *I, Call, Info) ||
23321 Info.EvalStatus.HasSideEffects) {
23322 // If evaluation fails, throw away the argument entirely.
23323 if (APValue *Slot = Info.getParamSlot(Call, PVD))
23324 *Slot = APValue();
23325 }
23326
23327 // Ignore any side-effects from a failed evaluation. This is safe because
23328 // they can't interfere with any other argument evaluation.
23329 Info.EvalStatus.HasSideEffects = false;
23330 }
23331
23332 // Parameter cleanups happen in the caller and are not part of this
23333 // evaluation.
23334 Info.discardCleanups();
23335 Info.EvalStatus.HasSideEffects = false;
23336
23337 // Build fake call to Callee.
23338 CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, This,
23339 Call);
23340 // FIXME: Missing ExprWithCleanups in enable_if conditions?
23341 FullExpressionRAII Scope(Info);
23342 return Evaluate(Result&: Value, Info, E: this) && Scope.destroy() &&
23343 !Info.EvalStatus.HasSideEffects;
23344}
23345
23346bool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
23347 SmallVectorImpl<
23348 PartialDiagnosticAt> &Diags) {
23349 // FIXME: It would be useful to check constexpr function templates, but at the
23350 // moment the constant expression evaluator cannot cope with the non-rigorous
23351 // ASTs which we build for dependent expressions.
23352 if (FD->isDependentContext())
23353 return true;
23354
23355 llvm::TimeTraceScope TimeScope("isPotentialConstantExpr", [&] {
23356 std::string Name;
23357 llvm::raw_string_ostream OS(Name);
23358 FD->getNameForDiagnostic(OS, Policy: FD->getASTContext().getPrintingPolicy(),
23359 /*Qualified=*/true);
23360 return Name;
23361 });
23362
23363 const ASTContext &Ctx = FD->getASTContext();
23364 Expr::EvalStatus Status;
23365 Status.Diag = &Diags;
23366
23367 // The constexpr VM attempts to compile all methods to bytecode here.
23368 if (Ctx.getLangOpts().EnableNewConstInterp) {
23369 interp::EvalSettings Settings(EvaluationMode::ConstantExpression, Status);
23370 Settings.InConstantContext = true;
23371 Settings.CheckingPotentialConstantExpression = true;
23372 Ctx.getInterpContext().isPotentialConstantExpr(Settings, FD);
23373 return Diags.empty();
23374 }
23375
23376 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
23377 Info.InConstantContext = true;
23378 Info.CheckingPotentialConstantExpression = true;
23379
23380 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
23381 const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr;
23382
23383 // Fabricate an arbitrary expression on the stack and pretend that it
23384 // is a temporary being used as the 'this' pointer.
23385 LValue This;
23386 ImplicitValueInitExpr VIE(RD ? Info.Ctx.getCanonicalTagType(TD: RD)
23387 : Info.Ctx.IntTy);
23388 This.set(B: {&VIE, Info.CurrentCall->Index});
23389
23390 ArrayRef<const Expr*> Args;
23391
23392 APValue Scratch;
23393 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Val: FD)) {
23394 // Evaluate the call as a constant initializer, to allow the construction
23395 // of objects of non-literal types.
23396 Info.setEvaluatingDecl(Base: This.getLValueBase(), Value&: Scratch);
23397 HandleConstructorCall(E: &VIE, This, Args, Definition: CD, Info, Result&: Scratch);
23398 } else {
23399 SourceLocation Loc = FD->getLocation();
23400 HandleFunctionCall(
23401 CallLoc: Loc, Callee: FD, ObjectArg: (MD && MD->isImplicitObjectMemberFunction()) ? &This : nullptr,
23402 E: &VIE, Args, Call: CallRef(), Body: FD->getBody(), Info, Result&: Scratch,
23403 /*ResultSlot=*/nullptr);
23404 }
23405
23406 return Diags.empty();
23407}
23408
23409bool Expr::isPotentialConstantExprUnevaluated(Expr *E,
23410 const FunctionDecl *FD,
23411 SmallVectorImpl<
23412 PartialDiagnosticAt> &Diags) {
23413 assert(!E->isValueDependent() &&
23414 "Expression evaluator can't be called on a dependent expression.");
23415
23416 const ASTContext &Ctx = FD->getASTContext();
23417 Expr::EvalStatus Status;
23418 Status.Diag = &Diags;
23419
23420 if (Ctx.getLangOpts().EnableNewConstInterp) {
23421 interp::EvalSettings Settings(EvaluationMode::ConstantExpressionUnevaluated,
23422 Status);
23423 Settings.InConstantContext = true;
23424 Settings.CheckingPotentialConstantExpression = true;
23425 Ctx.getInterpContext().isPotentialConstantExprUnevaluated(Settings, E, FD);
23426 return Diags.empty();
23427 }
23428
23429 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpressionUnevaluated);
23430 Info.InConstantContext = true;
23431 Info.CheckingPotentialConstantExpression = true;
23432
23433 // Fabricate a call stack frame to give the arguments a plausible cover story.
23434 CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr,
23435 /*CallExpr=*/nullptr, CallRef());
23436
23437 APValue ResultScratch;
23438 Evaluate(Result&: ResultScratch, Info, E);
23439 return Diags.empty();
23440}
23441
23442std::optional<uint64_t> Expr::tryEvaluateObjectSize(const ASTContext &Ctx,
23443 unsigned Type) const {
23444 if (!getType()->isPointerType())
23445 return std::nullopt;
23446
23447 Expr::EvalStatus Status;
23448 if (Ctx.getLangOpts().EnableNewConstInterp) {
23449 interp::EvalSettings Settings(EvaluationMode::ConstantFold, Status);
23450 return Ctx.getInterpContext().tryEvaluateObjectSize(Settings, E: this, Kind: Type,
23451 /*IsDynamic=*/false);
23452 }
23453
23454 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23455 return tryEvaluateBuiltinObjectSize(E: this, Type, Info);
23456}
23457
23458static std::optional<uint64_t>
23459EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
23460 std::string *StringResult) {
23461 if (!E->getType()->hasPointerRepresentation() || !E->isPRValue())
23462 return std::nullopt;
23463
23464 LValue String;
23465
23466 if (!EvaluatePointer(E, Result&: String, Info))
23467 return std::nullopt;
23468
23469 // Fast path: if it's a string literal, search the string value.
23470 if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>(
23471 Val: String.getLValueBase().dyn_cast<const Expr *>())) {
23472 StringRef Str = S->getBytes();
23473 int64_t Off = String.Offset.getQuantity();
23474 if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size()) {
23475 UnsignedOrNone ZeroIndex = S->findZeroCodeUnit(StartIndex: Off);
23476 if (StringResult) {
23477 if (ZeroIndex)
23478 Str = Str.substr(Start: Off, N: *ZeroIndex);
23479 *StringResult = Str;
23480 }
23481
23482 return ZeroIndex.value_or(Def: Str.size());
23483 }
23484 // For an invalid index, fall through to the offset handling below.
23485 }
23486
23487 QualType CharTy = E->getType()->getPointeeType();
23488 // Slow path: scan the bytes of the string looking for the terminating 0.
23489 for (uint64_t Strlen = 0; /**/; ++Strlen) {
23490 APValue Char;
23491 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: String, RVal&: Char) ||
23492 !Char.isInt())
23493 return std::nullopt;
23494 if (!Char.getInt())
23495 return Strlen;
23496 else if (StringResult)
23497 StringResult->push_back(c: Char.getInt().getExtValue());
23498 if (!HandleLValueArrayAdjustment(Info, E, LVal&: String, EltTy: CharTy, Adjustment: 1))
23499 return std::nullopt;
23500 }
23501}
23502
23503std::optional<std::string> Expr::tryEvaluateString(ASTContext &Ctx) const {
23504 Expr::EvalStatus Status;
23505 std::string StringResult;
23506
23507 if (Ctx.getLangOpts().EnableNewConstInterp) {
23508 interp::EvalSettings Settings(EvaluationMode::ConstantFold, Status);
23509 if (!Ctx.getInterpContext().evaluateString(Settings, E: this, Result&: StringResult))
23510 return std::nullopt;
23511 return StringResult;
23512 }
23513
23514 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23515 if (EvaluateBuiltinStrLen(E: this, Info, StringResult: &StringResult))
23516 return StringResult;
23517 return std::nullopt;
23518}
23519
23520template <typename T>
23521static bool EvaluateCharRangeAsStringImpl(const Expr *, T &Result,
23522 const Expr *SizeExpression,
23523 const Expr *PtrExpression,
23524 ASTContext &Ctx,
23525 Expr::EvalResult &Status) {
23526 if (Ctx.getLangOpts().EnableNewConstInterp) {
23527 interp::EvalSettings Settings(EvaluationMode::ConstantExpression, Status);
23528 Settings.InConstantContext = true;
23529 return Ctx.getInterpContext().evaluateCharRange(Settings, SizeExpression,
23530 PtrExpression, Result);
23531 }
23532
23533 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
23534 Info.InConstantContext = true;
23535
23536 LValue String;
23537 FullExpressionRAII Scope(Info);
23538 APSInt SizeValue;
23539 if (!::EvaluateInteger(E: SizeExpression, Result&: SizeValue, Info))
23540 return false;
23541
23542 uint64_t Size = SizeValue.getZExtValue();
23543
23544 // FIXME: better protect against invalid or excessive sizes
23545 if constexpr (std::is_same_v<APValue, T>)
23546 Result = APValue(APValue::UninitArray{}, Size, Size);
23547 else {
23548 if (Size < Result.max_size())
23549 Result.reserve(Size);
23550 }
23551 if (!::EvaluatePointer(E: PtrExpression, Result&: String, Info))
23552 return false;
23553
23554 QualType CharTy = PtrExpression->getType()->getPointeeType();
23555 for (uint64_t I = 0; I < Size; ++I) {
23556 APValue Char;
23557 if (!handleLValueToRValueConversion(Info, Conv: PtrExpression, Type: CharTy, LVal: String,
23558 RVal&: Char))
23559 return false;
23560
23561 if constexpr (std::is_same_v<APValue, T>) {
23562 Result.getArrayInitializedElt(I) = std::move(Char);
23563 } else {
23564 APSInt C = Char.getInt();
23565
23566 assert(C.getBitWidth() <= 8 &&
23567 "string element not representable in char");
23568
23569 Result.push_back(static_cast<char>(C.getExtValue()));
23570 }
23571
23572 if (!HandleLValueArrayAdjustment(Info, E: PtrExpression, LVal&: String, EltTy: CharTy, Adjustment: 1))
23573 return false;
23574 }
23575
23576 return Scope.destroy() && CheckMemoryLeaks(Info);
23577}
23578
23579bool Expr::EvaluateCharRangeAsString(std::string &Result,
23580 const Expr *SizeExpression,
23581 const Expr *PtrExpression, ASTContext &Ctx,
23582 EvalResult &Status) const {
23583 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
23584 PtrExpression, Ctx, Status);
23585}
23586
23587bool Expr::EvaluateCharRangeAsString(APValue &Result,
23588 const Expr *SizeExpression,
23589 const Expr *PtrExpression, ASTContext &Ctx,
23590 EvalResult &Status) const {
23591 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
23592 PtrExpression, Ctx, Status);
23593}
23594
23595std::optional<uint64_t> Expr::tryEvaluateStrLen(const ASTContext &Ctx) const {
23596 Expr::EvalStatus Status;
23597
23598 if (Ctx.getLangOpts().EnableNewConstInterp) {
23599 interp::EvalSettings Settings(EvaluationMode::ConstantFold, Status);
23600 return Ctx.getInterpContext().evaluateStrlen(Settings, E: this);
23601 }
23602 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23603 return EvaluateBuiltinStrLen(E: this, Info);
23604}
23605
23606namespace {
23607struct IsWithinLifetimeHandler {
23608 EvalInfo &Info;
23609 static constexpr AccessKinds AccessKind = AccessKinds::AK_IsWithinLifetime;
23610 using result_type = std::optional<bool>;
23611 std::optional<bool> failed() { return std::nullopt; }
23612 template <typename T>
23613 std::optional<bool> found(T &Subobj, QualType SubobjType,
23614 APValue::LValueBase) {
23615 return true;
23616 }
23617 template <typename T>
23618 std::optional<bool> found(T &Subobj, QualType SubobjType) {
23619 return true;
23620 }
23621};
23622
23623std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &IEE,
23624 const CallExpr *E) {
23625 EvalInfo &Info = IEE.Info;
23626 // Sometimes this is called during some sorts of constant folding / early
23627 // evaluation. These are meant for non-constant expressions and are not
23628 // necessary since this consteval builtin will never be evaluated at runtime.
23629 // Just fail to evaluate when not in a constant context.
23630 if (!Info.InConstantContext)
23631 return std::nullopt;
23632 assert(E->getBuiltinCallee() == Builtin::BI__builtin_is_within_lifetime);
23633 const Expr *Arg = E->getArg(Arg: 0);
23634 if (Arg->isValueDependent())
23635 return std::nullopt;
23636 LValue Val;
23637 if (!EvaluatePointer(E: Arg, Result&: Val, Info))
23638 return std::nullopt;
23639
23640 if (Val.allowConstexprUnknown())
23641 return true;
23642
23643 auto Error = [&](int Diag) {
23644 bool CalledFromStd = false;
23645 const auto *Callee = Info.CurrentCall->getCallee();
23646 if (Callee && Callee->isInStdNamespace()) {
23647 const IdentifierInfo *Identifier = Callee->getIdentifier();
23648 CalledFromStd = Identifier && Identifier->isStr(Str: "is_within_lifetime");
23649 }
23650 Info.CCEDiag(Loc: CalledFromStd ? Info.CurrentCall->getCallRange().getBegin()
23651 : E->getExprLoc(),
23652 DiagId: diag::err_invalid_is_within_lifetime)
23653 << (CalledFromStd ? "std::is_within_lifetime"
23654 : "__builtin_is_within_lifetime")
23655 << Diag;
23656 return std::nullopt;
23657 };
23658 // C++2c [meta.const.eval]p4:
23659 // During the evaluation of an expression E as a core constant expression, a
23660 // call to this function is ill-formed unless p points to an object that is
23661 // usable in constant expressions or whose complete object's lifetime began
23662 // within E.
23663
23664 // Make sure it points to an object
23665 // nullptr does not point to an object
23666 if (Val.isNullPointer() || Val.getLValueBase().isNull())
23667 return Error(0);
23668 QualType T = Val.getLValueBase().getType();
23669 assert(!T->isFunctionType() &&
23670 "Pointers to functions should have been typed as function pointers "
23671 "which would have been rejected earlier");
23672 assert(T->isObjectType());
23673 // Hypothetical array element is not an object
23674 if (Val.getLValueDesignator().isOnePastTheEnd())
23675 return Error(1);
23676 assert(Val.getLValueDesignator().isValidSubobject() &&
23677 "Unchecked case for valid subobject");
23678 // All other ill-formed values should have failed EvaluatePointer, so the
23679 // object should be a pointer to an object that is usable in a constant
23680 // expression or whose complete lifetime began within the expression
23681 CompleteObject CO =
23682 findCompleteObject(Info, E, AK: AccessKinds::AK_IsWithinLifetime, LVal: Val, LValType: T);
23683 // The lifetime hasn't begun yet if we are still evaluating the
23684 // initializer ([basic.life]p(1.2))
23685 if (Info.EvaluatingDeclValue && CO.Value == Info.EvaluatingDeclValue)
23686 return Error(2);
23687
23688 if (!CO)
23689 return false;
23690 IsWithinLifetimeHandler handler{.Info: Info};
23691 return findSubobject(Info, E, Obj: CO, Sub: Val.getLValueDesignator(), handler);
23692}
23693} // namespace
23694