1//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Expr constant evaluator.
10//
11// Constant expression evaluation produces four main results:
12//
13// * A success/failure flag indicating whether constant folding was successful.
14// This is the 'bool' return value used by most of the code in this file. A
15// 'false' return value indicates that constant folding has failed, and any
16// appropriate diagnostic has already been produced.
17//
18// * An evaluated result, valid only if constant folding has not failed.
19//
20// * A flag indicating if evaluation encountered (unevaluated) side-effects.
21// These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1),
22// where it is possible to determine the evaluated result regardless.
23//
24// * A set of notes indicating why the evaluation was not a constant expression
25// (under the C++11 / C++1y rules only, at the moment), or, if folding failed
26// too, why the expression could not be folded.
27//
28// If we are checking for a potential constant expression, failure to constant
29// fold a potential constant sub-expression will be indicated by a 'false'
30// return value (the expression could not be folded) and no diagnostic (the
31// expression is not necessarily non-constant).
32//
33//===----------------------------------------------------------------------===//
34
35#include "ByteCode/Context.h"
36#include "ByteCode/Frame.h"
37#include "ByteCode/State.h"
38#include "ExprConstShared.h"
39#include "clang/AST/APValue.h"
40#include "clang/AST/ASTContext.h"
41#include "clang/AST/ASTLambda.h"
42#include "clang/AST/Attr.h"
43#include "clang/AST/CXXInheritance.h"
44#include "clang/AST/CharUnits.h"
45#include "clang/AST/ComparisonCategories.h"
46#include "clang/AST/CurrentSourceLocExprScope.h"
47#include "clang/AST/Expr.h"
48#include "clang/AST/InferAlloc.h"
49#include "clang/AST/OSLog.h"
50#include "clang/AST/OptionalDiagnostic.h"
51#include "clang/AST/RecordLayout.h"
52#include "clang/AST/StmtVisitor.h"
53#include "clang/AST/Type.h"
54#include "clang/AST/TypeLoc.h"
55#include "clang/Basic/Builtins.h"
56#include "clang/Basic/DiagnosticSema.h"
57#include "clang/Basic/TargetBuiltins.h"
58#include "clang/Basic/TargetInfo.h"
59#include "llvm/ADT/APFixedPoint.h"
60#include "llvm/ADT/Sequence.h"
61#include "llvm/ADT/SmallBitVector.h"
62#include "llvm/ADT/StringExtras.h"
63#include "llvm/Support/Casting.h"
64#include "llvm/Support/Debug.h"
65#include "llvm/Support/SaveAndRestore.h"
66#include "llvm/Support/SipHash.h"
67#include "llvm/Support/TimeProfiler.h"
68#include "llvm/Support/raw_ostream.h"
69#include <cstring>
70#include <functional>
71#include <limits>
72#include <optional>
73
74#define DEBUG_TYPE "exprconstant"
75
76using namespace clang;
77using llvm::APFixedPoint;
78using llvm::APInt;
79using llvm::APSInt;
80using llvm::APFloat;
81using llvm::FixedPointSemantics;
82
83namespace {
84 struct LValue;
85 class CallStackFrame;
86 class EvalInfo;
87
88 using SourceLocExprScopeGuard =
89 CurrentSourceLocExprScope::SourceLocExprScopeGuard;
90
91 static QualType getType(APValue::LValueBase B) {
92 return B.getType();
93 }
94
95 /// Get an LValue path entry, which is known to not be an array index, as a
96 /// field declaration.
97 static const FieldDecl *getAsField(APValue::LValuePathEntry E) {
98 return dyn_cast_or_null<FieldDecl>(Val: E.getAsBaseOrMember().getPointer());
99 }
100 /// Get an LValue path entry, which is known to not be an array index, as a
101 /// base class declaration.
102 static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) {
103 return dyn_cast_or_null<CXXRecordDecl>(Val: E.getAsBaseOrMember().getPointer());
104 }
105 /// Determine whether this LValue path entry for a base class names a virtual
106 /// base class.
107 static bool isVirtualBaseClass(APValue::LValuePathEntry E) {
108 return E.getAsBaseOrMember().getInt();
109 }
110
111 /// Given an expression, determine the type used to store the result of
112 /// evaluating that expression.
113 static QualType getStorageType(const ASTContext &Ctx, const Expr *E) {
114 if (E->isPRValue())
115 return E->getType();
116 return Ctx.getLValueReferenceType(T: E->getType());
117 }
118
119 static unsigned countNonVirtualBases(const CXXRecordDecl *RD) {
120 return llvm::count_if(Range: RD->bases(), P: [](auto &B) { return !B.isVirtual(); });
121 }
122
123 /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.
124 /// This will look through a single cast.
125 ///
126 /// Returns null if we couldn't unwrap a function with alloc_size.
127 static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) {
128 if (!E->getType()->isPointerType())
129 return nullptr;
130
131 E = E->IgnoreParens();
132 // If we're doing a variable assignment from e.g. malloc(N), there will
133 // probably be a cast of some kind. In exotic cases, we might also see a
134 // top-level ExprWithCleanups. Ignore them either way.
135 if (const auto *FE = dyn_cast<FullExpr>(Val: E))
136 E = FE->getSubExpr()->IgnoreParens();
137
138 if (const auto *Cast = dyn_cast<CastExpr>(Val: E))
139 E = Cast->getSubExpr()->IgnoreParens();
140
141 if (const auto *CE = dyn_cast<CallExpr>(Val: E))
142 return CE->getCalleeAllocSizeAttr() ? CE : nullptr;
143 return nullptr;
144 }
145
146 /// Determines whether or not the given Base contains a call to a function
147 /// with the alloc_size attribute.
148 static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) {
149 const auto *E = Base.dyn_cast<const Expr *>();
150 return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E);
151 }
152
153 /// Determines whether the given kind of constant expression is only ever
154 /// used for name mangling. If so, it's permitted to reference things that we
155 /// can't generate code for (in particular, dllimported functions).
156 static bool isForManglingOnly(ConstantExprKind Kind) {
157 switch (Kind) {
158 case ConstantExprKind::Normal:
159 case ConstantExprKind::ClassTemplateArgument:
160 case ConstantExprKind::ImmediateInvocation:
161 // Note that non-type template arguments of class type are emitted as
162 // template parameter objects.
163 return false;
164
165 case ConstantExprKind::NonClassTemplateArgument:
166 return true;
167 }
168 llvm_unreachable("unknown ConstantExprKind");
169 }
170
171 static bool isTemplateArgument(ConstantExprKind Kind) {
172 switch (Kind) {
173 case ConstantExprKind::Normal:
174 case ConstantExprKind::ImmediateInvocation:
175 return false;
176
177 case ConstantExprKind::ClassTemplateArgument:
178 case ConstantExprKind::NonClassTemplateArgument:
179 return true;
180 }
181 llvm_unreachable("unknown ConstantExprKind");
182 }
183
184 /// The bound to claim that an array of unknown bound has.
185 /// The value in MostDerivedArraySize is undefined in this case. So, set it
186 /// to an arbitrary value that's likely to loudly break things if it's used.
187 static const uint64_t AssumedSizeForUnsizedArray =
188 std::numeric_limits<uint64_t>::max() / 2;
189
190 /// Determines if an LValue with the given LValueBase will have an unsized
191 /// array in its designator.
192 /// Find the path length and type of the most-derived subobject in the given
193 /// path, and find the size of the containing array, if any.
194 static unsigned
195 findMostDerivedSubobject(const ASTContext &Ctx, APValue::LValueBase Base,
196 ArrayRef<APValue::LValuePathEntry> Path,
197 uint64_t &ArraySize, QualType &Type, bool &IsArray,
198 bool &FirstEntryIsUnsizedArray) {
199 // This only accepts LValueBases from APValues, and APValues don't support
200 // arrays that lack size info.
201 assert(!isBaseAnAllocSizeCall(Base) &&
202 "Unsized arrays shouldn't appear here");
203 unsigned MostDerivedLength = 0;
204 // The type of Base is a reference type if the base is a constexpr-unknown
205 // variable. In that case, look through the reference type.
206 Type = getType(B: Base).getNonReferenceType();
207
208 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
209 if (Type->isArrayType()) {
210 const ArrayType *AT = Ctx.getAsArrayType(T: Type);
211 Type = AT->getElementType();
212 MostDerivedLength = I + 1;
213 IsArray = true;
214
215 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) {
216 ArraySize = CAT->getZExtSize();
217 } else {
218 assert(I == 0 && "unexpected unsized array designator");
219 FirstEntryIsUnsizedArray = true;
220 ArraySize = AssumedSizeForUnsizedArray;
221 }
222 } else if (Type->isAnyComplexType()) {
223 const ComplexType *CT = Type->castAs<ComplexType>();
224 Type = CT->getElementType();
225 ArraySize = 2;
226 MostDerivedLength = I + 1;
227 IsArray = true;
228 } else if (const auto *VT = Type->getAs<VectorType>()) {
229 Type = VT->getElementType();
230 ArraySize = VT->getNumElements();
231 MostDerivedLength = I + 1;
232 IsArray = true;
233 } else if (const FieldDecl *FD = getAsField(E: Path[I])) {
234 Type = FD->getType();
235 ArraySize = 0;
236 MostDerivedLength = I + 1;
237 IsArray = false;
238 } else {
239 // Path[I] describes a base class.
240 ArraySize = 0;
241 IsArray = false;
242 }
243 }
244 return MostDerivedLength;
245 }
246
247 /// A path from a glvalue to a subobject of that glvalue.
248 struct SubobjectDesignator {
249 /// True if the subobject was named in a manner not supported by C++11. Such
250 /// lvalues can still be folded, but they are not core constant expressions
251 /// and we cannot perform lvalue-to-rvalue conversions on them.
252 LLVM_PREFERRED_TYPE(bool)
253 unsigned Invalid : 1;
254
255 /// Is this a pointer one past the end of an object?
256 LLVM_PREFERRED_TYPE(bool)
257 unsigned IsOnePastTheEnd : 1;
258
259 /// Indicator of whether the first entry is an unsized array.
260 LLVM_PREFERRED_TYPE(bool)
261 unsigned FirstEntryIsAnUnsizedArray : 1;
262
263 /// Indicator of whether the most-derived object is an array element.
264 LLVM_PREFERRED_TYPE(bool)
265 unsigned MostDerivedIsArrayElement : 1;
266
267 /// The length of the path to the most-derived object of which this is a
268 /// subobject.
269 unsigned MostDerivedPathLength : 28;
270
271 /// The size of the array of which the most-derived object is an element.
272 /// This will always be 0 if the most-derived object is not an array
273 /// element. 0 is not an indicator of whether or not the most-derived object
274 /// is an array, however, because 0-length arrays are allowed.
275 ///
276 /// If the current array is an unsized array, the value of this is
277 /// undefined.
278 uint64_t MostDerivedArraySize;
279 /// The type of the most derived object referred to by this address.
280 QualType MostDerivedType;
281
282 typedef APValue::LValuePathEntry PathEntry;
283
284 /// The entries on the path from the glvalue to the designated subobject.
285 SmallVector<PathEntry, 8> Entries;
286
287 SubobjectDesignator() : Invalid(true) {}
288
289 explicit SubobjectDesignator(QualType T)
290 : Invalid(false), IsOnePastTheEnd(false),
291 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
292 MostDerivedPathLength(0), MostDerivedArraySize(0),
293 MostDerivedType(T.isNull() ? QualType() : T.getNonReferenceType()) {}
294
295 SubobjectDesignator(const ASTContext &Ctx, const APValue &V)
296 : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false),
297 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
298 MostDerivedPathLength(0), MostDerivedArraySize(0) {
299 assert(V.isLValue() && "Non-LValue used to make an LValue designator?");
300 if (!Invalid) {
301 IsOnePastTheEnd = V.isLValueOnePastTheEnd();
302 llvm::append_range(C&: Entries, R: V.getLValuePath());
303 if (V.getLValueBase()) {
304 bool IsArray = false;
305 bool FirstIsUnsizedArray = false;
306 MostDerivedPathLength = findMostDerivedSubobject(
307 Ctx, Base: V.getLValueBase(), Path: V.getLValuePath(), ArraySize&: MostDerivedArraySize,
308 Type&: MostDerivedType, IsArray, FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
309 MostDerivedIsArrayElement = IsArray;
310 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
311 }
312 }
313 }
314
315 void truncate(ASTContext &Ctx, APValue::LValueBase Base,
316 unsigned NewLength) {
317 if (Invalid)
318 return;
319
320 assert(Base && "cannot truncate path for null pointer");
321 assert(NewLength <= Entries.size() && "not a truncation");
322
323 if (NewLength == Entries.size())
324 return;
325 Entries.resize(N: NewLength);
326
327 bool IsArray = false;
328 bool FirstIsUnsizedArray = false;
329 MostDerivedPathLength = findMostDerivedSubobject(
330 Ctx, Base, Path: Entries, ArraySize&: MostDerivedArraySize, Type&: MostDerivedType, IsArray,
331 FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
332 MostDerivedIsArrayElement = IsArray;
333 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
334 }
335
336 void setInvalid() {
337 Invalid = true;
338 Entries.clear();
339 }
340
341 /// Determine whether the most derived subobject is an array without a
342 /// known bound.
343 bool isMostDerivedAnUnsizedArray() const {
344 assert(!Invalid && "Calling this makes no sense on invalid designators");
345 return Entries.size() == 1 && FirstEntryIsAnUnsizedArray;
346 }
347
348 /// Determine what the most derived array's size is. Results in an assertion
349 /// failure if the most derived array lacks a size.
350 uint64_t getMostDerivedArraySize() const {
351 assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size");
352 return MostDerivedArraySize;
353 }
354
355 /// Determine whether this is a one-past-the-end pointer.
356 bool isOnePastTheEnd() const {
357 assert(!Invalid);
358 if (IsOnePastTheEnd)
359 return true;
360 if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement &&
361 Entries[MostDerivedPathLength - 1].getAsArrayIndex() ==
362 MostDerivedArraySize)
363 return true;
364 return false;
365 }
366
367 /// Get the range of valid index adjustments in the form
368 /// {maximum value that can be subtracted from this pointer,
369 /// maximum value that can be added to this pointer}
370 std::pair<uint64_t, uint64_t> validIndexAdjustments() {
371 if (Invalid || isMostDerivedAnUnsizedArray())
372 return {0, 0};
373
374 // [expr.add]p4: For the purposes of these operators, a pointer to a
375 // nonarray object behaves the same as a pointer to the first element of
376 // an array of length one with the type of the object as its element type.
377 bool IsArray = MostDerivedPathLength == Entries.size() &&
378 MostDerivedIsArrayElement;
379 uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex()
380 : (uint64_t)IsOnePastTheEnd;
381 uint64_t ArraySize =
382 IsArray ? getMostDerivedArraySize() : (uint64_t)1;
383 return {ArrayIndex, ArraySize - ArrayIndex};
384 }
385
386 /// Check that this refers to a valid subobject.
387 bool isValidSubobject() const {
388 if (Invalid)
389 return false;
390 return !isOnePastTheEnd();
391 }
392 /// Check that this refers to a valid subobject, and if not, produce a
393 /// relevant diagnostic and set the designator as invalid.
394 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK);
395
396 /// Get the type of the designated object.
397 QualType getType(ASTContext &Ctx) const {
398 assert(!Invalid && "invalid designator has no subobject type");
399 return MostDerivedPathLength == Entries.size()
400 ? MostDerivedType
401 : Ctx.getCanonicalTagType(TD: getAsBaseClass(E: Entries.back()));
402 }
403
404 /// Update this designator to refer to the first element within this array.
405 void addArrayUnchecked(const ConstantArrayType *CAT) {
406 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
407
408 // This is a most-derived object.
409 MostDerivedType = CAT->getElementType();
410 MostDerivedIsArrayElement = true;
411 MostDerivedArraySize = CAT->getZExtSize();
412 MostDerivedPathLength = Entries.size();
413 }
414 /// Update this designator to refer to the first element within the array of
415 /// elements of type T. This is an array of unknown size.
416 void addUnsizedArrayUnchecked(QualType ElemTy) {
417 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
418
419 MostDerivedType = ElemTy;
420 MostDerivedIsArrayElement = true;
421 // The value in MostDerivedArraySize is undefined in this case. So, set it
422 // to an arbitrary value that's likely to loudly break things if it's
423 // used.
424 MostDerivedArraySize = AssumedSizeForUnsizedArray;
425 MostDerivedPathLength = Entries.size();
426 }
427 /// Update this designator to refer to the given base or member of this
428 /// object.
429 void addDeclUnchecked(const Decl *D, bool Virtual = false) {
430 Entries.push_back(Elt: APValue::BaseOrMemberType(D, Virtual));
431
432 // If this isn't a base class, it's a new most-derived object.
433 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: D)) {
434 MostDerivedType = FD->getType();
435 MostDerivedIsArrayElement = false;
436 MostDerivedArraySize = 0;
437 MostDerivedPathLength = Entries.size();
438 }
439 }
440 /// Update this designator to refer to the given complex component.
441 void addComplexUnchecked(QualType EltTy, bool Imag) {
442 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Imag));
443
444 // This is technically a most-derived object, though in practice this
445 // is unlikely to matter.
446 MostDerivedType = EltTy;
447 MostDerivedIsArrayElement = true;
448 MostDerivedArraySize = 2;
449 MostDerivedPathLength = Entries.size();
450 }
451
452 void addVectorElementUnchecked(QualType EltTy, uint64_t Size,
453 uint64_t Idx) {
454 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Idx));
455 MostDerivedType = EltTy;
456 MostDerivedPathLength = Entries.size();
457 MostDerivedArraySize = 0;
458 MostDerivedIsArrayElement = false;
459 }
460
461 void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E);
462 void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E,
463 const APSInt &N);
464 /// Add N to the address of this subobject.
465 void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N, const LValue &LV);
466 };
467
468 /// A scope at the end of which an object can need to be destroyed.
469 enum class ScopeKind {
470 Block,
471 FullExpression,
472 Call
473 };
474
475 /// A reference to a particular call and its arguments.
476 struct CallRef {
477 CallRef() : OrigCallee(), CallIndex(0), Version() {}
478 CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version)
479 : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {}
480
481 explicit operator bool() const { return OrigCallee; }
482
483 /// Get the parameter that the caller initialized, corresponding to the
484 /// given parameter in the callee.
485 const ParmVarDecl *getOrigParam(const ParmVarDecl *PVD) const {
486 return OrigCallee ? OrigCallee->getParamDecl(i: PVD->getFunctionScopeIndex())
487 : PVD;
488 }
489
490 /// The callee at the point where the arguments were evaluated. This might
491 /// be different from the actual callee (a different redeclaration, or a
492 /// virtual override), but this function's parameters are the ones that
493 /// appear in the parameter map.
494 const FunctionDecl *OrigCallee;
495 /// The call index of the frame that holds the argument values.
496 unsigned CallIndex;
497 /// The version of the parameters corresponding to this call.
498 unsigned Version;
499 };
500
501 /// A stack frame in the constexpr call stack.
502 class CallStackFrame : public interp::Frame {
503 public:
504 EvalInfo &Info;
505
506 /// Parent - The caller of this stack frame.
507 CallStackFrame *Caller;
508
509 /// Callee - The function which was called.
510 const FunctionDecl *Callee;
511
512 /// This - The binding for the this pointer in this call, if any.
513 const LValue *This;
514
515 /// CallExpr - The syntactical structure of member function calls
516 const Expr *CallExpr;
517
518 /// Information on how to find the arguments to this call. Our arguments
519 /// are stored in our parent's CallStackFrame, using the ParmVarDecl* as a
520 /// key and this value as the version.
521 CallRef Arguments;
522
523 /// Source location information about the default argument or default
524 /// initializer expression we're evaluating, if any.
525 CurrentSourceLocExprScope CurSourceLocExprScope;
526
527 // Note that we intentionally use std::map here so that references to
528 // values are stable.
529 typedef std::pair<const void *, unsigned> MapKeyTy;
530 typedef std::map<MapKeyTy, APValue> MapTy;
531 /// Temporaries - Temporary lvalues materialized within this stack frame.
532 MapTy Temporaries;
533
534 /// CallRange - The source range of the call expression for this call.
535 SourceRange CallRange;
536
537 /// Index - The call index of this call.
538 unsigned Index;
539
540 /// The stack of integers for tracking version numbers for temporaries.
541 SmallVector<unsigned, 2> TempVersionStack = {1};
542 unsigned CurTempVersion = TempVersionStack.back();
543
544 unsigned getTempVersion() const { return TempVersionStack.back(); }
545
546 void pushTempVersion() {
547 TempVersionStack.push_back(Elt: ++CurTempVersion);
548 }
549
550 void popTempVersion() {
551 TempVersionStack.pop_back();
552 }
553
554 CallRef createCall(const FunctionDecl *Callee) {
555 return {Callee, Index, ++CurTempVersion};
556 }
557
558 // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact
559 // on the overall stack usage of deeply-recursing constexpr evaluations.
560 // (We should cache this map rather than recomputing it repeatedly.)
561 // But let's try this and see how it goes; we can look into caching the map
562 // as a later change.
563
564 /// LambdaCaptureFields - Mapping from captured variables/this to
565 /// corresponding data members in the closure class.
566 llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;
567 FieldDecl *LambdaThisCaptureField = nullptr;
568
569 CallStackFrame(EvalInfo &Info, SourceRange CallRange,
570 const FunctionDecl *Callee, const LValue *This,
571 const Expr *CallExpr, CallRef Arguments);
572 ~CallStackFrame();
573
574 // Return the temporary for Key whose version number is Version.
575 APValue *getTemporary(const void *Key, unsigned Version) {
576 MapKeyTy KV(Key, Version);
577 auto LB = Temporaries.lower_bound(x: KV);
578 if (LB != Temporaries.end() && LB->first == KV)
579 return &LB->second;
580 return nullptr;
581 }
582
583 // Return the current temporary for Key in the map.
584 APValue *getCurrentTemporary(const void *Key) {
585 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
586 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
587 return &std::prev(x: UB)->second;
588 return nullptr;
589 }
590
591 // Return the version number of the current temporary for Key.
592 unsigned getCurrentTemporaryVersion(const void *Key) const {
593 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
594 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
595 return std::prev(x: UB)->first.second;
596 return 0;
597 }
598
599 /// Allocate storage for an object of type T in this stack frame.
600 /// Populates LV with a handle to the created object. Key identifies
601 /// the temporary within the stack frame, and must not be reused without
602 /// bumping the temporary version number.
603 template<typename KeyT>
604 APValue &createTemporary(const KeyT *Key, QualType T,
605 ScopeKind Scope, LValue &LV);
606
607 /// Allocate storage for a parameter of a function call made in this frame.
608 APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV);
609
610 void describe(llvm::raw_ostream &OS) const override;
611
612 Frame *getCaller() const override { return Caller; }
613 SourceRange getCallRange() const override { return CallRange; }
614 const FunctionDecl *getCallee() const override { return Callee; }
615
616 bool isStdFunction() const {
617 for (const DeclContext *DC = Callee; DC; DC = DC->getParent())
618 if (DC->isStdNamespace())
619 return true;
620 return false;
621 }
622
623 /// Whether we're in a context where [[msvc::constexpr]] evaluation is
624 /// permitted. See MSConstexprDocs for description of permitted contexts.
625 bool CanEvalMSConstexpr = false;
626
627 private:
628 APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T,
629 ScopeKind Scope);
630 };
631
632 /// Temporarily override 'this'.
633 class ThisOverrideRAII {
634 public:
635 ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable)
636 : Frame(Frame), OldThis(Frame.This) {
637 if (Enable)
638 Frame.This = NewThis;
639 }
640 ~ThisOverrideRAII() {
641 Frame.This = OldThis;
642 }
643 private:
644 CallStackFrame &Frame;
645 const LValue *OldThis;
646 };
647
648 // A shorthand time trace scope struct, prints source range, for example
649 // {"name":"EvaluateAsRValue","args":{"detail":"<test.cc:8:21, col:25>"}}}
650 class ExprTimeTraceScope {
651 public:
652 ExprTimeTraceScope(const Expr *E, const ASTContext &Ctx, StringRef Name)
653 : TimeScope(Name, [E, &Ctx] {
654 return E->getSourceRange().printToString(SM: Ctx.getSourceManager());
655 }) {}
656
657 private:
658 llvm::TimeTraceScope TimeScope;
659 };
660
661 /// RAII object used to change the current ability of
662 /// [[msvc::constexpr]] evaulation.
663 struct MSConstexprContextRAII {
664 CallStackFrame &Frame;
665 bool OldValue;
666 explicit MSConstexprContextRAII(CallStackFrame &Frame, bool Value)
667 : Frame(Frame), OldValue(Frame.CanEvalMSConstexpr) {
668 Frame.CanEvalMSConstexpr = Value;
669 }
670
671 ~MSConstexprContextRAII() { Frame.CanEvalMSConstexpr = OldValue; }
672 };
673}
674
675static bool HandleDestruction(EvalInfo &Info, const Expr *E,
676 const LValue &This, QualType ThisType);
677static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
678 APValue::LValueBase LVBase, APValue &Value,
679 QualType T);
680
681namespace {
682 /// A cleanup, and a flag indicating whether it is lifetime-extended.
683 class Cleanup {
684 llvm::PointerIntPair<APValue*, 2, ScopeKind> Value;
685 APValue::LValueBase Base;
686 QualType T;
687
688 public:
689 Cleanup(APValue *Val, APValue::LValueBase Base, QualType T,
690 ScopeKind Scope)
691 : Value(Val, Scope), Base(Base), T(T) {}
692
693 /// Determine whether this cleanup should be performed at the end of the
694 /// given kind of scope.
695 bool isDestroyedAtEndOf(ScopeKind K) const {
696 return (int)Value.getInt() >= (int)K;
697 }
698 bool endLifetime(EvalInfo &Info, bool RunDestructors) {
699 if (RunDestructors) {
700 SourceLocation Loc;
701 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
702 Loc = VD->getLocation();
703 else if (const Expr *E = Base.dyn_cast<const Expr*>())
704 Loc = E->getExprLoc();
705 return HandleDestruction(Info, Loc, LVBase: Base, Value&: *Value.getPointer(), T);
706 }
707 *Value.getPointer() = APValue();
708 return true;
709 }
710
711 bool hasSideEffect() {
712 return T.isDestructedType();
713 }
714 };
715
716 /// A reference to an object whose construction we are currently evaluating.
717 struct ObjectUnderConstruction {
718 APValue::LValueBase Base;
719 ArrayRef<APValue::LValuePathEntry> Path;
720 friend bool operator==(const ObjectUnderConstruction &LHS,
721 const ObjectUnderConstruction &RHS) {
722 return LHS.Base == RHS.Base && LHS.Path == RHS.Path;
723 }
724 friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) {
725 return llvm::hash_combine(args: Obj.Base, args: Obj.Path);
726 }
727 };
728 enum class ConstructionPhase {
729 None,
730 Bases,
731 AfterBases,
732 AfterFields,
733 Destroying,
734 DestroyingBases
735 };
736}
737
738namespace llvm {
739template<> struct DenseMapInfo<ObjectUnderConstruction> {
740 using Base = DenseMapInfo<APValue::LValueBase>;
741 static unsigned getHashValue(const ObjectUnderConstruction &Object) {
742 return hash_value(Obj: Object);
743 }
744 static bool isEqual(const ObjectUnderConstruction &LHS,
745 const ObjectUnderConstruction &RHS) {
746 return LHS == RHS;
747 }
748};
749}
750
751namespace {
752 /// A dynamically-allocated heap object.
753 struct DynAlloc {
754 /// The value of this heap-allocated object.
755 APValue Value;
756 /// The allocating expression; used for diagnostics. Either a CXXNewExpr
757 /// or a CallExpr (the latter is for direct calls to operator new inside
758 /// std::allocator<T>::allocate).
759 const Expr *AllocExpr = nullptr;
760
761 enum Kind {
762 New,
763 ArrayNew,
764 StdAllocator
765 };
766
767 /// Get the kind of the allocation. This must match between allocation
768 /// and deallocation.
769 Kind getKind() const {
770 if (auto *NE = dyn_cast<CXXNewExpr>(Val: AllocExpr))
771 return NE->isArray() ? ArrayNew : New;
772 assert(isa<CallExpr>(AllocExpr));
773 return StdAllocator;
774 }
775 };
776
777 struct DynAllocOrder {
778 bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const {
779 return L.getIndex() < R.getIndex();
780 }
781 };
782
783 /// EvalInfo - This is a private struct used by the evaluator to capture
784 /// information about a subexpression as it is folded. It retains information
785 /// about the AST context, but also maintains information about the folded
786 /// expression.
787 ///
788 /// If an expression could be evaluated, it is still possible it is not a C
789 /// "integer constant expression" or constant expression. If not, this struct
790 /// captures information about how and why not.
791 ///
792 /// One bit of information passed *into* the request for constant folding
793 /// indicates whether the subexpression is "evaluated" or not according to C
794 /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
795 /// evaluate the expression regardless of what the RHS is, but C only allows
796 /// certain things in certain situations.
797 class EvalInfo final : public interp::State {
798 public:
799 /// CurrentCall - The top of the constexpr call stack.
800 CallStackFrame *CurrentCall;
801
802 /// CallStackDepth - The number of calls in the call stack right now.
803 unsigned CallStackDepth;
804
805 /// NextCallIndex - The next call index to assign.
806 unsigned NextCallIndex;
807
808 /// StepsLeft - The remaining number of evaluation steps we're permitted
809 /// to perform. This is essentially a limit for the number of statements
810 /// we will evaluate.
811 unsigned StepsLeft;
812
813 /// Enable the experimental new constant interpreter. If an expression is
814 /// not supported by the interpreter, an error is triggered.
815 bool EnableNewConstInterp;
816
817 /// BottomFrame - The frame in which evaluation started. This must be
818 /// initialized after CurrentCall and CallStackDepth.
819 CallStackFrame BottomFrame;
820
821 /// A stack of values whose lifetimes end at the end of some surrounding
822 /// evaluation frame.
823 llvm::SmallVector<Cleanup, 16> CleanupStack;
824
825 /// EvaluatingDecl - This is the declaration whose initializer is being
826 /// evaluated, if any.
827 APValue::LValueBase EvaluatingDecl;
828
829 enum class EvaluatingDeclKind {
830 None,
831 /// We're evaluating the construction of EvaluatingDecl.
832 Ctor,
833 /// We're evaluating the destruction of EvaluatingDecl.
834 Dtor,
835 };
836 EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None;
837
838 /// EvaluatingDeclValue - This is the value being constructed for the
839 /// declaration whose initializer is being evaluated, if any.
840 APValue *EvaluatingDeclValue;
841
842 /// Stack of loops and 'switch' statements which we're currently
843 /// breaking/continuing; null entries are used to mark unlabeled
844 /// break/continue.
845 SmallVector<const Stmt *> BreakContinueStack;
846
847 /// Set of objects that are currently being constructed.
848 llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase>
849 ObjectsUnderConstruction;
850
851 /// Current heap allocations, along with the location where each was
852 /// allocated. We use std::map here because we need stable addresses
853 /// for the stored APValues.
854 std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs;
855
856 /// The number of heap allocations performed so far in this evaluation.
857 unsigned NumHeapAllocs = 0;
858
859 struct EvaluatingConstructorRAII {
860 EvalInfo &EI;
861 ObjectUnderConstruction Object;
862 bool DidInsert;
863 EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object,
864 bool HasBases)
865 : EI(EI), Object(Object) {
866 DidInsert =
867 EI.ObjectsUnderConstruction
868 .insert(KV: {Object, HasBases ? ConstructionPhase::Bases
869 : ConstructionPhase::AfterBases})
870 .second;
871 }
872 void finishedConstructingBases() {
873 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases;
874 }
875 void finishedConstructingFields() {
876 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterFields;
877 }
878 ~EvaluatingConstructorRAII() {
879 if (DidInsert) EI.ObjectsUnderConstruction.erase(Val: Object);
880 }
881 };
882
883 struct EvaluatingDestructorRAII {
884 EvalInfo &EI;
885 ObjectUnderConstruction Object;
886 bool DidInsert;
887 EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object)
888 : EI(EI), Object(Object) {
889 DidInsert = EI.ObjectsUnderConstruction
890 .insert(KV: {Object, ConstructionPhase::Destroying})
891 .second;
892 }
893 void startedDestroyingBases() {
894 EI.ObjectsUnderConstruction[Object] =
895 ConstructionPhase::DestroyingBases;
896 }
897 ~EvaluatingDestructorRAII() {
898 if (DidInsert)
899 EI.ObjectsUnderConstruction.erase(Val: Object);
900 }
901 };
902
903 ConstructionPhase
904 isEvaluatingCtorDtor(APValue::LValueBase Base,
905 ArrayRef<APValue::LValuePathEntry> Path) {
906 return ObjectsUnderConstruction.lookup(Val: {.Base: Base, .Path: Path});
907 }
908
909 /// If we're currently speculatively evaluating, the outermost call stack
910 /// depth at which we can mutate state, otherwise 0.
911 unsigned SpeculativeEvaluationDepth = 0;
912
913 /// The current array initialization index, if we're performing array
914 /// initialization.
915 uint64_t ArrayInitIndex = -1;
916
917 EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode)
918 : State(const_cast<ASTContext &>(C), S), CurrentCall(nullptr),
919 CallStackDepth(0), NextCallIndex(1),
920 StepsLeft(C.getLangOpts().ConstexprStepLimit),
921 EnableNewConstInterp(C.getLangOpts().EnableNewConstInterp),
922 BottomFrame(*this, SourceLocation(), /*Callee=*/nullptr,
923 /*This=*/nullptr,
924 /*CallExpr=*/nullptr, CallRef()),
925 EvaluatingDecl((const ValueDecl *)nullptr),
926 EvaluatingDeclValue(nullptr) {
927 EvalMode = Mode;
928 }
929
930 ~EvalInfo() {
931 discardCleanups();
932 }
933
934 void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value,
935 EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) {
936 EvaluatingDecl = Base;
937 IsEvaluatingDecl = EDK;
938 EvaluatingDeclValue = &Value;
939 }
940
941 bool CheckCallLimit(SourceLocation Loc) {
942 // Don't perform any constexpr calls (other than the call we're checking)
943 // when checking a potential constant expression.
944 if (checkingPotentialConstantExpression() && CallStackDepth > 1)
945 return false;
946 if (NextCallIndex == 0) {
947 // NextCallIndex has wrapped around.
948 FFDiag(Loc, DiagId: diag::note_constexpr_call_limit_exceeded);
949 return false;
950 }
951 if (CallStackDepth <= getLangOpts().ConstexprCallDepth)
952 return true;
953 FFDiag(Loc, DiagId: diag::note_constexpr_depth_limit_exceeded)
954 << getLangOpts().ConstexprCallDepth;
955 return false;
956 }
957
958 bool CheckArraySize(SourceLocation Loc, unsigned BitWidth,
959 uint64_t ElemCount, bool Diag) {
960 // FIXME: GH63562
961 // APValue stores array extents as unsigned,
962 // so anything that is greater that unsigned would overflow when
963 // constructing the array, we catch this here.
964 if (BitWidth > ConstantArrayType::getMaxSizeBits(Context: Ctx) ||
965 ElemCount > uint64_t(std::numeric_limits<unsigned>::max())) {
966 if (Diag)
967 FFDiag(Loc, DiagId: diag::note_constexpr_new_too_large) << ElemCount;
968 return false;
969 }
970
971 // FIXME: GH63562
972 // Arrays allocate an APValue per element.
973 // We use the number of constexpr steps as a proxy for the maximum size
974 // of arrays to avoid exhausting the system resources, as initialization
975 // of each element is likely to take some number of steps anyway.
976 uint64_t Limit = getLangOpts().ConstexprStepLimit;
977 if (Limit != 0 && ElemCount > Limit) {
978 if (Diag) {
979 FFDiag(Loc, DiagId: diag::note_constexpr_new_exceeds_limits, ExtraNotes: 1)
980 << ElemCount << Limit;
981 Note(Loc, DiagId: diag::note_constexpr_steps);
982 }
983 return false;
984 }
985 return true;
986 }
987
988 std::pair<CallStackFrame *, unsigned>
989 getCallFrameAndDepth(unsigned CallIndex) {
990 assert(CallIndex && "no call index in getCallFrameAndDepth");
991 // We will eventually hit BottomFrame, which has Index 1, so Frame can't
992 // be null in this loop.
993 unsigned Depth = CallStackDepth;
994 CallStackFrame *Frame = CurrentCall;
995 while (Frame->Index > CallIndex) {
996 Frame = Frame->Caller;
997 --Depth;
998 }
999 if (Frame->Index == CallIndex)
1000 return {Frame, Depth};
1001 return {nullptr, 0};
1002 }
1003
1004 bool nextStep(const Stmt *S) {
1005 if (getLangOpts().ConstexprStepLimit == 0)
1006 return true;
1007
1008 if (!StepsLeft) {
1009 FFDiag(Loc: S->getBeginLoc(), DiagId: diag::note_constexpr_step_limit_exceeded, ExtraNotes: 1)
1010 << getLangOpts().ConstexprStepLimit;
1011 Note(Loc: S->getBeginLoc(), DiagId: diag::note_constexpr_steps);
1012 return false;
1013 }
1014 --StepsLeft;
1015 return true;
1016 }
1017
1018 APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV);
1019
1020 std::optional<DynAlloc *> lookupDynamicAlloc(DynamicAllocLValue DA) {
1021 std::optional<DynAlloc *> Result;
1022 auto It = HeapAllocs.find(x: DA);
1023 if (It != HeapAllocs.end())
1024 Result = &It->second;
1025 return Result;
1026 }
1027
1028 /// Get the allocated storage for the given parameter of the given call.
1029 APValue *getParamSlot(CallRef Call, const ParmVarDecl *PVD) {
1030 CallStackFrame *Frame = getCallFrameAndDepth(CallIndex: Call.CallIndex).first;
1031 return Frame ? Frame->getTemporary(Key: Call.getOrigParam(PVD), Version: Call.Version)
1032 : nullptr;
1033 }
1034
1035 /// Information about a stack frame for std::allocator<T>::[de]allocate.
1036 struct StdAllocatorCaller {
1037 unsigned FrameIndex;
1038 QualType ElemType;
1039 const Expr *Call;
1040 explicit operator bool() const { return FrameIndex != 0; };
1041 };
1042
1043 StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const {
1044 for (const CallStackFrame *Call = CurrentCall; Call->Caller != nullptr;
1045 Call = Call->Caller) {
1046 const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Val: Call->Callee);
1047 if (!MD)
1048 continue;
1049 const IdentifierInfo *FnII = MD->getIdentifier();
1050 if (!FnII || !FnII->isStr(Str: FnName))
1051 continue;
1052
1053 const auto *CTSD =
1054 dyn_cast<ClassTemplateSpecializationDecl>(Val: MD->getParent());
1055 if (!CTSD)
1056 continue;
1057
1058 const IdentifierInfo *ClassII = CTSD->getIdentifier();
1059 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
1060 if (CTSD->isInStdNamespace() && ClassII &&
1061 ClassII->isStr(Str: "allocator") && TAL.size() >= 1 &&
1062 TAL[0].getKind() == TemplateArgument::Type)
1063 return {.FrameIndex: Call->Index, .ElemType: TAL[0].getAsType(), .Call: Call->CallExpr};
1064 }
1065
1066 return {};
1067 }
1068
1069 void performLifetimeExtension() {
1070 // Disable the cleanups for lifetime-extended temporaries.
1071 llvm::erase_if(C&: CleanupStack, P: [](Cleanup &C) {
1072 return !C.isDestroyedAtEndOf(K: ScopeKind::FullExpression);
1073 });
1074 }
1075
1076 /// Throw away any remaining cleanups at the end of evaluation. If any
1077 /// cleanups would have had a side-effect, note that as an unmodeled
1078 /// side-effect and return false. Otherwise, return true.
1079 bool discardCleanups() {
1080 for (Cleanup &C : CleanupStack) {
1081 if (C.hasSideEffect() && !noteSideEffect()) {
1082 CleanupStack.clear();
1083 return false;
1084 }
1085 }
1086 CleanupStack.clear();
1087 return true;
1088 }
1089
1090 private:
1091 const interp::Frame *getCurrentFrame() override { return CurrentCall; }
1092
1093 unsigned getCallStackDepth() override { return CallStackDepth; }
1094 bool stepsLeft() const override { return StepsLeft > 0; }
1095
1096 public:
1097 /// Notes that we failed to evaluate an expression that other expressions
1098 /// directly depend on, and determine if we should keep evaluating. This
1099 /// should only be called if we actually intend to keep evaluating.
1100 ///
1101 /// Call noteSideEffect() instead if we may be able to ignore the value that
1102 /// we failed to evaluate, e.g. if we failed to evaluate Foo() in:
1103 ///
1104 /// (Foo(), 1) // use noteSideEffect
1105 /// (Foo() || true) // use noteSideEffect
1106 /// Foo() + 1 // use noteFailure
1107 [[nodiscard]] bool noteFailure() {
1108 // Failure when evaluating some expression often means there is some
1109 // subexpression whose evaluation was skipped. Therefore, (because we
1110 // don't track whether we skipped an expression when unwinding after an
1111 // evaluation failure) every evaluation failure that bubbles up from a
1112 // subexpression implies that a side-effect has potentially happened. We
1113 // skip setting the HasSideEffects flag to true until we decide to
1114 // continue evaluating after that point, which happens here.
1115 bool KeepGoing = keepEvaluatingAfterFailure();
1116 EvalStatus.HasSideEffects |= KeepGoing;
1117 return KeepGoing;
1118 }
1119
1120 class ArrayInitLoopIndex {
1121 EvalInfo &Info;
1122 uint64_t OuterIndex;
1123
1124 public:
1125 ArrayInitLoopIndex(EvalInfo &Info)
1126 : Info(Info), OuterIndex(Info.ArrayInitIndex) {
1127 Info.ArrayInitIndex = 0;
1128 }
1129 ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; }
1130
1131 operator uint64_t&() { return Info.ArrayInitIndex; }
1132 };
1133 };
1134
1135 /// Object used to treat all foldable expressions as constant expressions.
1136 struct FoldConstant {
1137 EvalInfo &Info;
1138 bool Enabled;
1139 bool HadNoPriorDiags;
1140 EvaluationMode OldMode;
1141
1142 explicit FoldConstant(EvalInfo &Info, bool Enabled)
1143 : Info(Info),
1144 Enabled(Enabled),
1145 HadNoPriorDiags(Info.EvalStatus.Diag &&
1146 Info.EvalStatus.Diag->empty() &&
1147 !Info.EvalStatus.HasSideEffects),
1148 OldMode(Info.EvalMode) {
1149 if (Enabled)
1150 Info.EvalMode = EvaluationMode::ConstantFold;
1151 }
1152 void keepDiagnostics() { Enabled = false; }
1153 ~FoldConstant() {
1154 if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() &&
1155 !Info.EvalStatus.HasSideEffects) {
1156 Info.EvalStatus.Diag->clear();
1157 Info.EvalStatus.DiagEmitted = false;
1158 }
1159 Info.EvalMode = OldMode;
1160 }
1161 };
1162
1163 /// RAII object used to set the current evaluation mode to ignore
1164 /// side-effects.
1165 struct IgnoreSideEffectsRAII {
1166 EvalInfo &Info;
1167 EvaluationMode OldMode;
1168 explicit IgnoreSideEffectsRAII(EvalInfo &Info)
1169 : Info(Info), OldMode(Info.EvalMode) {
1170 Info.EvalMode = EvaluationMode::IgnoreSideEffects;
1171 }
1172
1173 ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; }
1174 };
1175
1176 /// RAII object used to optionally suppress diagnostics and side-effects from
1177 /// a speculative evaluation.
1178 class SpeculativeEvaluationRAII {
1179 EvalInfo *Info = nullptr;
1180 Expr::EvalStatus OldStatus;
1181 unsigned OldSpeculativeEvaluationDepth = 0;
1182
1183 void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) {
1184 Info = Other.Info;
1185 OldStatus = Other.OldStatus;
1186 OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth;
1187 Other.Info = nullptr;
1188 }
1189
1190 void maybeRestoreState() {
1191 if (!Info)
1192 return;
1193
1194 Info->EvalStatus = OldStatus;
1195 Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth;
1196 }
1197
1198 public:
1199 SpeculativeEvaluationRAII() = default;
1200
1201 SpeculativeEvaluationRAII(
1202 EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr)
1203 : Info(&Info), OldStatus(Info.EvalStatus),
1204 OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) {
1205 Info.EvalStatus.Diag = NewDiag;
1206 Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1;
1207 }
1208
1209 SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete;
1210 SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) {
1211 moveFromAndCancel(Other: std::move(Other));
1212 }
1213
1214 SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) {
1215 maybeRestoreState();
1216 moveFromAndCancel(Other: std::move(Other));
1217 return *this;
1218 }
1219
1220 ~SpeculativeEvaluationRAII() { maybeRestoreState(); }
1221 };
1222
1223 /// RAII object wrapping a full-expression or block scope, and handling
1224 /// the ending of the lifetime of temporaries created within it.
1225 template<ScopeKind Kind>
1226 class ScopeRAII {
1227 EvalInfo &Info;
1228 unsigned OldStackSize;
1229 public:
1230 ScopeRAII(EvalInfo &Info)
1231 : Info(Info), OldStackSize(Info.CleanupStack.size()) {
1232 // Push a new temporary version. This is needed to distinguish between
1233 // temporaries created in different iterations of a loop.
1234 Info.CurrentCall->pushTempVersion();
1235 }
1236 bool destroy(bool RunDestructors = true) {
1237 bool OK = cleanup(Info, RunDestructors, OldStackSize);
1238 OldStackSize = std::numeric_limits<unsigned>::max();
1239 return OK;
1240 }
1241 ~ScopeRAII() {
1242 if (OldStackSize != std::numeric_limits<unsigned>::max())
1243 destroy(RunDestructors: false);
1244 // Body moved to a static method to encourage the compiler to inline away
1245 // instances of this class.
1246 Info.CurrentCall->popTempVersion();
1247 }
1248 private:
1249 static bool cleanup(EvalInfo &Info, bool RunDestructors,
1250 unsigned OldStackSize) {
1251 assert(OldStackSize <= Info.CleanupStack.size() &&
1252 "running cleanups out of order?");
1253
1254 // Run all cleanups for a block scope, and non-lifetime-extended cleanups
1255 // for a full-expression scope.
1256 bool Success = true;
1257 for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) {
1258 if (Info.CleanupStack[I - 1].isDestroyedAtEndOf(K: Kind)) {
1259 if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) {
1260 Success = false;
1261 break;
1262 }
1263 }
1264 }
1265
1266 // Compact any retained cleanups.
1267 auto NewEnd = Info.CleanupStack.begin() + OldStackSize;
1268 if (Kind != ScopeKind::Block)
1269 NewEnd =
1270 std::remove_if(NewEnd, Info.CleanupStack.end(), [](Cleanup &C) {
1271 return C.isDestroyedAtEndOf(K: Kind);
1272 });
1273 Info.CleanupStack.erase(CS: NewEnd, CE: Info.CleanupStack.end());
1274 return Success;
1275 }
1276 };
1277 typedef ScopeRAII<ScopeKind::Block> BlockScopeRAII;
1278 typedef ScopeRAII<ScopeKind::FullExpression> FullExpressionRAII;
1279 typedef ScopeRAII<ScopeKind::Call> CallScopeRAII;
1280}
1281
1282bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,
1283 CheckSubobjectKind CSK) {
1284 if (Invalid)
1285 return false;
1286 if (isOnePastTheEnd()) {
1287 Info.CCEDiag(E, DiagId: diag::note_constexpr_past_end_subobject)
1288 << CSK;
1289 setInvalid();
1290 return false;
1291 }
1292 // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there
1293 // must actually be at least one array element; even a VLA cannot have a
1294 // bound of zero. And if our index is nonzero, we already had a CCEDiag.
1295 return true;
1296}
1297
1298void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info,
1299 const Expr *E) {
1300 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsized_array_indexed);
1301 // Do not set the designator as invalid: we can represent this situation,
1302 // and correct handling of __builtin_object_size requires us to do so.
1303}
1304
1305void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
1306 const Expr *E,
1307 const APSInt &N) {
1308 // If we're complaining, we must be able to statically determine the size of
1309 // the most derived array.
1310 if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement)
1311 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1312 << N << /*array*/ 0
1313 << static_cast<unsigned>(getMostDerivedArraySize());
1314 else
1315 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1316 << N << /*non-array*/ 1;
1317 setInvalid();
1318}
1319
1320CallStackFrame::CallStackFrame(EvalInfo &Info, SourceRange CallRange,
1321 const FunctionDecl *Callee, const LValue *This,
1322 const Expr *CallExpr, CallRef Call)
1323 : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This),
1324 CallExpr(CallExpr), Arguments(Call), CallRange(CallRange),
1325 Index(Info.NextCallIndex++) {
1326 Info.CurrentCall = this;
1327 ++Info.CallStackDepth;
1328}
1329
1330CallStackFrame::~CallStackFrame() {
1331 assert(Info.CurrentCall == this && "calls retired out of order");
1332 --Info.CallStackDepth;
1333 Info.CurrentCall = Caller;
1334}
1335
1336static bool isRead(AccessKinds AK) {
1337 return AK == AK_Read || AK == AK_ReadObjectRepresentation ||
1338 AK == AK_IsWithinLifetime || AK == AK_Dereference;
1339}
1340
1341static bool isModification(AccessKinds AK) {
1342 switch (AK) {
1343 case AK_Read:
1344 case AK_ReadObjectRepresentation:
1345 case AK_MemberCall:
1346 case AK_DynamicCast:
1347 case AK_TypeId:
1348 case AK_IsWithinLifetime:
1349 case AK_Dereference:
1350 return false;
1351 case AK_Assign:
1352 case AK_Increment:
1353 case AK_Decrement:
1354 case AK_Construct:
1355 case AK_Destroy:
1356 return true;
1357 }
1358 llvm_unreachable("unknown access kind");
1359}
1360
1361static bool isAnyAccess(AccessKinds AK) {
1362 return isRead(AK) || isModification(AK);
1363}
1364
1365/// Is this an access per the C++ definition?
1366static bool isFormalAccess(AccessKinds AK) {
1367 return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy &&
1368 AK != AK_IsWithinLifetime && AK != AK_Dereference;
1369}
1370
1371/// Is this kind of access valid on an indeterminate object value?
1372static bool isValidIndeterminateAccess(AccessKinds AK) {
1373 switch (AK) {
1374 case AK_Read:
1375 case AK_Increment:
1376 case AK_Decrement:
1377 case AK_Dereference:
1378 // These need the object's value.
1379 return false;
1380
1381 case AK_IsWithinLifetime:
1382 case AK_ReadObjectRepresentation:
1383 case AK_Assign:
1384 case AK_Construct:
1385 case AK_Destroy:
1386 // Construction and destruction don't need the value.
1387 return true;
1388
1389 case AK_MemberCall:
1390 case AK_DynamicCast:
1391 case AK_TypeId:
1392 // These aren't really meaningful on scalars.
1393 return true;
1394 }
1395 llvm_unreachable("unknown access kind");
1396}
1397
1398namespace {
1399 struct ComplexValue {
1400 private:
1401 bool IsInt;
1402
1403 public:
1404 APSInt IntReal, IntImag;
1405 APFloat FloatReal, FloatImag;
1406
1407 ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {}
1408
1409 void makeComplexFloat() { IsInt = false; }
1410 bool isComplexFloat() const { return !IsInt; }
1411 APFloat &getComplexFloatReal() { return FloatReal; }
1412 APFloat &getComplexFloatImag() { return FloatImag; }
1413
1414 void makeComplexInt() { IsInt = true; }
1415 bool isComplexInt() const { return IsInt; }
1416 APSInt &getComplexIntReal() { return IntReal; }
1417 APSInt &getComplexIntImag() { return IntImag; }
1418
1419 void moveInto(APValue &v) const {
1420 if (isComplexFloat())
1421 v = APValue(FloatReal, FloatImag);
1422 else
1423 v = APValue(IntReal, IntImag);
1424 }
1425 void setFrom(const APValue &v) {
1426 assert(v.isComplexFloat() || v.isComplexInt());
1427 if (v.isComplexFloat()) {
1428 makeComplexFloat();
1429 FloatReal = v.getComplexFloatReal();
1430 FloatImag = v.getComplexFloatImag();
1431 } else {
1432 makeComplexInt();
1433 IntReal = v.getComplexIntReal();
1434 IntImag = v.getComplexIntImag();
1435 }
1436 }
1437 };
1438
1439 struct LValue {
1440 APValue::LValueBase Base;
1441 CharUnits Offset;
1442 SubobjectDesignator Designator;
1443 bool IsNullPtr : 1;
1444 bool InvalidBase : 1;
1445 // P2280R4 track if we have an unknown reference or pointer.
1446 bool AllowConstexprUnknown = false;
1447
1448 const APValue::LValueBase getLValueBase() const { return Base; }
1449 bool allowConstexprUnknown() const { return AllowConstexprUnknown; }
1450 CharUnits &getLValueOffset() { return Offset; }
1451 const CharUnits &getLValueOffset() const { return Offset; }
1452 SubobjectDesignator &getLValueDesignator() { return Designator; }
1453 const SubobjectDesignator &getLValueDesignator() const { return Designator;}
1454 bool isNullPointer() const { return IsNullPtr;}
1455
1456 unsigned getLValueCallIndex() const { return Base.getCallIndex(); }
1457 unsigned getLValueVersion() const { return Base.getVersion(); }
1458
1459 bool pointsToCompleteClass(const CXXRecordDecl *D) const {
1460 if (Designator.Entries.empty())
1461 return true;
1462
1463 return Designator.MostDerivedType->getAsCXXRecordDecl() == D;
1464 }
1465
1466 void moveInto(APValue &V) const {
1467 if (Designator.Invalid)
1468 V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr);
1469 else {
1470 assert(!InvalidBase && "APValues can't handle invalid LValue bases");
1471 V = APValue(Base, Offset, Designator.Entries,
1472 Designator.IsOnePastTheEnd, IsNullPtr);
1473 }
1474 if (AllowConstexprUnknown)
1475 V.setConstexprUnknown();
1476 }
1477 void setFrom(const ASTContext &Ctx, const APValue &V) {
1478 assert(V.isLValue() && "Setting LValue from a non-LValue?");
1479 Base = V.getLValueBase();
1480 Offset = V.getLValueOffset();
1481 InvalidBase = false;
1482 Designator = SubobjectDesignator(Ctx, V);
1483 IsNullPtr = V.isNullPointer();
1484 AllowConstexprUnknown = V.allowConstexprUnknown();
1485 }
1486
1487 void set(APValue::LValueBase B, bool BInvalid = false) {
1488#ifndef NDEBUG
1489 // We only allow a few types of invalid bases. Enforce that here.
1490 if (BInvalid) {
1491 const auto *E = B.get<const Expr *>();
1492 assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) &&
1493 "Unexpected type of invalid base");
1494 }
1495#endif
1496
1497 Base = B;
1498 Offset = CharUnits::fromQuantity(Quantity: 0);
1499 InvalidBase = BInvalid;
1500 Designator = SubobjectDesignator(getType(B));
1501 IsNullPtr = false;
1502 AllowConstexprUnknown = false;
1503 }
1504
1505 void setNull(ASTContext &Ctx, QualType PointerTy) {
1506 Base = (const ValueDecl *)nullptr;
1507 Offset =
1508 CharUnits::fromQuantity(Quantity: Ctx.getTargetNullPointerValue(QT: PointerTy));
1509 InvalidBase = false;
1510 Designator = SubobjectDesignator(PointerTy->getPointeeType());
1511 IsNullPtr = true;
1512 AllowConstexprUnknown = false;
1513 }
1514
1515 void setInvalid(APValue::LValueBase B, unsigned I = 0) {
1516 set(B, BInvalid: true);
1517 }
1518
1519 std::string toString(ASTContext &Ctx, QualType T) const {
1520 APValue Printable;
1521 moveInto(V&: Printable);
1522 return Printable.getAsString(Ctx, Ty: T);
1523 }
1524
1525 private:
1526 // Check that this LValue is not based on a null pointer. If it is, produce
1527 // a diagnostic and mark the designator as invalid.
1528 template <typename GenDiagType>
1529 bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) {
1530 if (Designator.Invalid)
1531 return false;
1532 if (IsNullPtr) {
1533 GenDiag();
1534 Designator.setInvalid();
1535 return false;
1536 }
1537 return true;
1538 }
1539
1540 public:
1541 bool checkNullPointer(EvalInfo &Info, const Expr *E,
1542 CheckSubobjectKind CSK) {
1543 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, CSK] {
1544 Info.CCEDiag(E, DiagId: diag::note_constexpr_null_subobject) << CSK;
1545 });
1546 }
1547
1548 bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E,
1549 AccessKinds AK) {
1550 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, AK] {
1551 if (AK == AccessKinds::AK_Dereference)
1552 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
1553 else
1554 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
1555 });
1556 }
1557
1558 // Check this LValue refers to an object. If not, set the designator to be
1559 // invalid and emit a diagnostic.
1560 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {
1561 return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) &&
1562 Designator.checkSubobject(Info, E, CSK);
1563 }
1564
1565 void addDecl(EvalInfo &Info, const Expr *E,
1566 const Decl *D, bool Virtual = false) {
1567 if (checkSubobject(Info, E, CSK: isa<FieldDecl>(Val: D) ? CSK_Field : CSK_Base))
1568 Designator.addDeclUnchecked(D, Virtual);
1569 }
1570 void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) {
1571 if (!Designator.Entries.empty()) {
1572 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsupported_unsized_array);
1573 Designator.setInvalid();
1574 return;
1575 }
1576 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer)) {
1577 assert(!Base || getType(Base).getNonReferenceType()->isPointerType() ||
1578 getType(Base).getNonReferenceType()->isArrayType());
1579 Designator.FirstEntryIsAnUnsizedArray = true;
1580 Designator.addUnsizedArrayUnchecked(ElemTy);
1581 }
1582 }
1583 void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {
1584 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer))
1585 Designator.addArrayUnchecked(CAT);
1586 }
1587 void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) {
1588 if (checkSubobject(Info, E, CSK: Imag ? CSK_Imag : CSK_Real))
1589 Designator.addComplexUnchecked(EltTy, Imag);
1590 }
1591 void addVectorElement(EvalInfo &Info, const Expr *E, QualType EltTy,
1592 uint64_t Size, uint64_t Idx) {
1593 if (checkSubobject(Info, E, CSK: CSK_VectorElement))
1594 Designator.addVectorElementUnchecked(EltTy, Size, Idx);
1595 }
1596 void clearIsNullPointer() {
1597 IsNullPtr = false;
1598 }
1599 void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E,
1600 const APSInt &Index, CharUnits ElementSize) {
1601 // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB,
1602 // but we're not required to diagnose it and it's valid in C++.)
1603 if (!Index)
1604 return;
1605
1606 // Compute the new offset in the appropriate width, wrapping at 64 bits.
1607 // FIXME: When compiling for a 32-bit target, we should use 32-bit
1608 // offsets.
1609 uint64_t Offset64 = Offset.getQuantity();
1610 uint64_t ElemSize64 = ElementSize.getQuantity();
1611 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
1612 Offset = CharUnits::fromQuantity(Quantity: Offset64 + ElemSize64 * Index64);
1613
1614 if (checkNullPointer(Info, E, CSK: CSK_ArrayIndex))
1615 Designator.adjustIndex(Info, E, N: Index, LV: *this);
1616 clearIsNullPointer();
1617 }
1618 void adjustOffset(CharUnits N) {
1619 Offset += N;
1620 if (N.getQuantity())
1621 clearIsNullPointer();
1622 }
1623 };
1624
1625 struct MemberPtr {
1626 MemberPtr() {}
1627 explicit MemberPtr(const ValueDecl *Decl)
1628 : DeclAndIsDerivedMember(Decl, false) {}
1629
1630 /// The member or (direct or indirect) field referred to by this member
1631 /// pointer, or 0 if this is a null member pointer.
1632 const ValueDecl *getDecl() const {
1633 return DeclAndIsDerivedMember.getPointer();
1634 }
1635 /// Is this actually a member of some type derived from the relevant class?
1636 bool isDerivedMember() const {
1637 return DeclAndIsDerivedMember.getInt();
1638 }
1639 /// Get the class which the declaration actually lives in.
1640 const CXXRecordDecl *getContainingRecord() const {
1641 return cast<CXXRecordDecl>(
1642 Val: DeclAndIsDerivedMember.getPointer()->getDeclContext());
1643 }
1644
1645 void moveInto(APValue &V) const {
1646 V = APValue(getDecl(), isDerivedMember(), Path);
1647 }
1648 void setFrom(const APValue &V) {
1649 assert(V.isMemberPointer());
1650 DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());
1651 DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());
1652 Path.clear();
1653 llvm::append_range(C&: Path, R: V.getMemberPointerPath());
1654 }
1655
1656 /// DeclAndIsDerivedMember - The member declaration, and a flag indicating
1657 /// whether the member is a member of some class derived from the class type
1658 /// of the member pointer.
1659 llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;
1660 /// Path - The path of base/derived classes from the member declaration's
1661 /// class (exclusive) to the class type of the member pointer (inclusive).
1662 SmallVector<const CXXRecordDecl*, 4> Path;
1663
1664 /// Perform a cast towards the class of the Decl (either up or down the
1665 /// hierarchy).
1666 bool castBack(const CXXRecordDecl *Class) {
1667 assert(!Path.empty());
1668 const CXXRecordDecl *Expected;
1669 if (Path.size() >= 2)
1670 Expected = Path[Path.size() - 2];
1671 else
1672 Expected = getContainingRecord();
1673 if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {
1674 // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
1675 // if B does not contain the original member and is not a base or
1676 // derived class of the class containing the original member, the result
1677 // of the cast is undefined.
1678 // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
1679 // (D::*). We consider that to be a language defect.
1680 return false;
1681 }
1682 Path.pop_back();
1683 return true;
1684 }
1685 /// Perform a base-to-derived member pointer cast.
1686 bool castToDerived(const CXXRecordDecl *Derived) {
1687 if (!getDecl())
1688 return true;
1689 if (!isDerivedMember()) {
1690 Path.push_back(Elt: Derived);
1691 return true;
1692 }
1693 if (!castBack(Class: Derived))
1694 return false;
1695 if (Path.empty())
1696 DeclAndIsDerivedMember.setInt(false);
1697 return true;
1698 }
1699 /// Perform a derived-to-base member pointer cast.
1700 bool castToBase(const CXXRecordDecl *Base) {
1701 if (!getDecl())
1702 return true;
1703 if (Path.empty())
1704 DeclAndIsDerivedMember.setInt(true);
1705 if (isDerivedMember()) {
1706 Path.push_back(Elt: Base);
1707 return true;
1708 }
1709 return castBack(Class: Base);
1710 }
1711 };
1712
1713 /// Compare two member pointers, which are assumed to be of the same type.
1714 static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {
1715 if (!LHS.getDecl() || !RHS.getDecl())
1716 return !LHS.getDecl() && !RHS.getDecl();
1717 if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())
1718 return false;
1719 return LHS.Path == RHS.Path;
1720 }
1721}
1722
1723void SubobjectDesignator::adjustIndex(EvalInfo &Info, const Expr *E, APSInt N,
1724 const LValue &LV) {
1725 if (Invalid || !N)
1726 return;
1727 uint64_t TruncatedN = N.extOrTrunc(width: 64).getZExtValue();
1728 if (isMostDerivedAnUnsizedArray()) {
1729 diagnoseUnsizedArrayPointerArithmetic(Info, E);
1730 // Can't verify -- trust that the user is doing the right thing (or if
1731 // not, trust that the caller will catch the bad behavior).
1732 // FIXME: Should we reject if this overflows, at least?
1733 Entries.back() =
1734 PathEntry::ArrayIndex(Index: Entries.back().getAsArrayIndex() + TruncatedN);
1735 return;
1736 }
1737
1738 // [expr.add]p4: For the purposes of these operators, a pointer to a
1739 // nonarray object behaves the same as a pointer to the first element of
1740 // an array of length one with the type of the object as its element type.
1741 bool IsArray =
1742 MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement;
1743 uint64_t ArrayIndex =
1744 IsArray ? Entries.back().getAsArrayIndex() : (uint64_t)IsOnePastTheEnd;
1745 uint64_t ArraySize = IsArray ? getMostDerivedArraySize() : (uint64_t)1;
1746
1747 if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) {
1748 if (!Info.checkingPotentialConstantExpression() ||
1749 !LV.AllowConstexprUnknown) {
1750 // Calculate the actual index in a wide enough type, so we can include
1751 // it in the note.
1752 N = N.extend(width: std::max<unsigned>(a: N.getBitWidth() + 1, b: 65));
1753 (llvm::APInt &)N += ArrayIndex;
1754 assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index");
1755 diagnosePointerArithmetic(Info, E, N);
1756 }
1757 setInvalid();
1758 return;
1759 }
1760
1761 ArrayIndex += TruncatedN;
1762 assert(ArrayIndex <= ArraySize &&
1763 "bounds check succeeded for out-of-bounds index");
1764
1765 if (IsArray)
1766 Entries.back() = PathEntry::ArrayIndex(Index: ArrayIndex);
1767 else
1768 IsOnePastTheEnd = (ArrayIndex != 0);
1769}
1770
1771static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E);
1772static bool EvaluateInPlace(APValue &Result, EvalInfo &Info,
1773 const LValue &This, const Expr *E,
1774 bool AllowNonLiteralTypes = false);
1775static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
1776 bool InvalidBaseOK = false);
1777static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info,
1778 bool InvalidBaseOK = false);
1779static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
1780 EvalInfo &Info);
1781static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info);
1782static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
1783static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
1784 EvalInfo &Info);
1785static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
1786static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
1787static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info);
1788static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
1789 EvalInfo &Info);
1790static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result);
1791static std::optional<uint64_t>
1792EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
1793 std::string *StringResult = nullptr);
1794
1795/// Evaluate an integer or fixed point expression into an APResult.
1796static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
1797 EvalInfo &Info);
1798
1799/// Evaluate only a fixed point expression into an APResult.
1800static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
1801 EvalInfo &Info);
1802
1803//===----------------------------------------------------------------------===//
1804// Misc utilities
1805//===----------------------------------------------------------------------===//
1806
1807/// Negate an APSInt in place, converting it to a signed form if necessary, and
1808/// preserving its value (by extending by up to one bit as needed).
1809static void negateAsSigned(APSInt &Int) {
1810 if (Int.isUnsigned() || Int.isMinSignedValue()) {
1811 Int = Int.extend(width: Int.getBitWidth() + 1);
1812 Int.setIsSigned(true);
1813 }
1814 Int = -Int;
1815}
1816
1817template<typename KeyT>
1818APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T,
1819 ScopeKind Scope, LValue &LV) {
1820 unsigned Version = getTempVersion();
1821 APValue::LValueBase Base(Key, Index, Version);
1822 LV.set(B: Base);
1823 return createLocal(Base, Key, T, Scope);
1824}
1825
1826/// Allocate storage for a parameter of a function call made in this frame.
1827APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD,
1828 LValue &LV) {
1829 assert(Args.CallIndex == Index && "creating parameter in wrong frame");
1830 APValue::LValueBase Base(PVD, Index, Args.Version);
1831 LV.set(B: Base);
1832 // We always destroy parameters at the end of the call, even if we'd allow
1833 // them to live to the end of the full-expression at runtime, in order to
1834 // give portable results and match other compilers.
1835 return createLocal(Base, Key: PVD, T: PVD->getType(), Scope: ScopeKind::Call);
1836}
1837
1838APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key,
1839 QualType T, ScopeKind Scope) {
1840 assert(Base.getCallIndex() == Index && "lvalue for wrong frame");
1841 unsigned Version = Base.getVersion();
1842 APValue &Result = Temporaries[MapKeyTy(Key, Version)];
1843 assert(Result.isAbsent() && "local created multiple times");
1844
1845 // If we're creating a local immediately in the operand of a speculative
1846 // evaluation, don't register a cleanup to be run outside the speculative
1847 // evaluation context, since we won't actually be able to initialize this
1848 // object.
1849 if (Index <= Info.SpeculativeEvaluationDepth) {
1850 if (T.isDestructedType())
1851 Info.noteSideEffect();
1852 } else {
1853 Info.CleanupStack.push_back(Elt: Cleanup(&Result, Base, T, Scope));
1854 }
1855 return Result;
1856}
1857
1858APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) {
1859 if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) {
1860 FFDiag(E, DiagId: diag::note_constexpr_heap_alloc_limit_exceeded);
1861 return nullptr;
1862 }
1863
1864 DynamicAllocLValue DA(NumHeapAllocs++);
1865 LV.set(B: APValue::LValueBase::getDynamicAlloc(LV: DA, Type: T));
1866 auto Result = HeapAllocs.emplace(args: std::piecewise_construct,
1867 args: std::forward_as_tuple(args&: DA), args: std::tuple<>());
1868 assert(Result.second && "reused a heap alloc index?");
1869 Result.first->second.AllocExpr = E;
1870 return &Result.first->second.Value;
1871}
1872
1873/// Produce a string describing the given constexpr call.
1874void CallStackFrame::describe(raw_ostream &Out) const {
1875 bool IsMemberCall = false;
1876 bool ExplicitInstanceParam = false;
1877 clang::PrintingPolicy PrintingPolicy = Info.Ctx.getPrintingPolicy();
1878 PrintingPolicy.SuppressLambdaBody = true;
1879
1880 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: Callee)) {
1881 IsMemberCall = !isa<CXXConstructorDecl>(Val: MD) && !MD->isStatic();
1882 ExplicitInstanceParam = MD->isExplicitObjectMemberFunction();
1883 }
1884
1885 if (!IsMemberCall)
1886 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1887 /*Qualified=*/false);
1888
1889 if (This && IsMemberCall) {
1890 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(Val: CallExpr)) {
1891 const Expr *Object = MCE->getImplicitObjectArgument();
1892 Object->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1893 /*Indentation=*/0);
1894 if (Object->getType()->isPointerType())
1895 Out << "->";
1896 else
1897 Out << ".";
1898 } else if (const auto *OCE =
1899 dyn_cast_if_present<CXXOperatorCallExpr>(Val: CallExpr)) {
1900 OCE->getArg(Arg: 0)->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1901 /*Indentation=*/0);
1902 Out << ".";
1903 } else {
1904 APValue Val;
1905 This->moveInto(V&: Val);
1906 Val.printPretty(
1907 OS&: Out, Ctx: Info.Ctx,
1908 Ty: Info.Ctx.getLValueReferenceType(T: This->Designator.MostDerivedType));
1909 Out << ".";
1910 }
1911 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1912 /*Qualified=*/false);
1913 }
1914
1915 Out << '(';
1916
1917 llvm::ListSeparator Comma;
1918 for (const ParmVarDecl *Param :
1919 Callee->parameters().slice(N: ExplicitInstanceParam)) {
1920 Out << Comma;
1921 const APValue *V = Info.getParamSlot(Call: Arguments, PVD: Param);
1922 if (V)
1923 V->printPretty(OS&: Out, Ctx: Info.Ctx, Ty: Param->getType());
1924 else
1925 Out << "<...>";
1926 }
1927
1928 Out << ')';
1929}
1930
1931/// Evaluate an expression to see if it had side-effects, and discard its
1932/// result.
1933/// \return \c true if the caller should keep evaluating.
1934static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) {
1935 assert(!E->isValueDependent());
1936 APValue Scratch;
1937 if (!Evaluate(Result&: Scratch, Info, E))
1938 // We don't need the value, but we might have skipped a side effect here.
1939 return Info.noteSideEffect();
1940 return true;
1941}
1942
1943/// Should this call expression be treated as forming an opaque constant?
1944static bool IsOpaqueConstantCall(const CallExpr *E) {
1945 unsigned Builtin = E->getBuiltinCallee();
1946 return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
1947 Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||
1948 Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||
1949 Builtin == Builtin::BI__builtin_function_start);
1950}
1951
1952static bool IsOpaqueConstantCall(const LValue &LVal) {
1953 const auto *BaseExpr =
1954 llvm::dyn_cast_if_present<CallExpr>(Val: LVal.Base.dyn_cast<const Expr *>());
1955 return BaseExpr && IsOpaqueConstantCall(E: BaseExpr);
1956}
1957
1958static bool IsGlobalLValue(APValue::LValueBase B) {
1959 // C++11 [expr.const]p3 An address constant expression is a prvalue core
1960 // constant expression of pointer type that evaluates to...
1961
1962 // ... a null pointer value, or a prvalue core constant expression of type
1963 // std::nullptr_t.
1964 if (!B)
1965 return true;
1966
1967 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
1968 // ... the address of an object with static storage duration,
1969 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
1970 return VD->hasGlobalStorage();
1971 if (isa<TemplateParamObjectDecl>(Val: D))
1972 return true;
1973 // ... the address of a function,
1974 // ... the address of a GUID [MS extension],
1975 // ... the address of an unnamed global constant
1976 return isa<FunctionDecl, MSGuidDecl, UnnamedGlobalConstantDecl>(Val: D);
1977 }
1978
1979 if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>())
1980 return true;
1981
1982 const Expr *E = B.get<const Expr*>();
1983 switch (E->getStmtClass()) {
1984 default:
1985 return false;
1986 case Expr::CompoundLiteralExprClass: {
1987 const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(Val: E);
1988 return CLE->isFileScope() && CLE->isLValue();
1989 }
1990 case Expr::MaterializeTemporaryExprClass:
1991 // A materialized temporary might have been lifetime-extended to static
1992 // storage duration.
1993 return cast<MaterializeTemporaryExpr>(Val: E)->getStorageDuration() == SD_Static;
1994 // A string literal has static storage duration.
1995 case Expr::StringLiteralClass:
1996 case Expr::PredefinedExprClass:
1997 case Expr::ObjCStringLiteralClass:
1998 case Expr::ObjCEncodeExprClass:
1999 return true;
2000 case Expr::ObjCBoxedExprClass:
2001 case Expr::ObjCArrayLiteralClass:
2002 case Expr::ObjCDictionaryLiteralClass:
2003 return cast<ObjCObjectLiteral>(Val: E)->isExpressibleAsConstantInitializer();
2004 case Expr::CallExprClass:
2005 return IsOpaqueConstantCall(E: cast<CallExpr>(Val: E));
2006 // For GCC compatibility, &&label has static storage duration.
2007 case Expr::AddrLabelExprClass:
2008 return true;
2009 // A Block literal expression may be used as the initialization value for
2010 // Block variables at global or local static scope.
2011 case Expr::BlockExprClass:
2012 return !cast<BlockExpr>(Val: E)->getBlockDecl()->hasCaptures();
2013 // The APValue generated from a __builtin_source_location will be emitted as a
2014 // literal.
2015 case Expr::SourceLocExprClass:
2016 return true;
2017 case Expr::ImplicitValueInitExprClass:
2018 // FIXME:
2019 // We can never form an lvalue with an implicit value initialization as its
2020 // base through expression evaluation, so these only appear in one case: the
2021 // implicit variable declaration we invent when checking whether a constexpr
2022 // constructor can produce a constant expression. We must assume that such
2023 // an expression might be a global lvalue.
2024 return true;
2025 }
2026}
2027
2028static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
2029 return LVal.Base.dyn_cast<const ValueDecl*>();
2030}
2031
2032// Information about an LValueBase that is some kind of string.
2033struct LValueBaseString {
2034 std::string ObjCEncodeStorage;
2035 StringRef Bytes;
2036 int CharWidth;
2037};
2038
2039// Gets the lvalue base of LVal as a string.
2040static bool GetLValueBaseAsString(const EvalInfo &Info, const LValue &LVal,
2041 LValueBaseString &AsString) {
2042 const auto *BaseExpr = LVal.Base.dyn_cast<const Expr *>();
2043 if (!BaseExpr)
2044 return false;
2045
2046 // For ObjCEncodeExpr, we need to compute and store the string.
2047 if (const auto *EE = dyn_cast<ObjCEncodeExpr>(Val: BaseExpr)) {
2048 Info.Ctx.getObjCEncodingForType(T: EE->getEncodedType(),
2049 S&: AsString.ObjCEncodeStorage);
2050 AsString.Bytes = AsString.ObjCEncodeStorage;
2051 AsString.CharWidth = 1;
2052 return true;
2053 }
2054
2055 // Otherwise, we have a StringLiteral.
2056 const auto *Lit = dyn_cast<StringLiteral>(Val: BaseExpr);
2057 if (const auto *PE = dyn_cast<PredefinedExpr>(Val: BaseExpr))
2058 Lit = PE->getFunctionName();
2059
2060 if (!Lit)
2061 return false;
2062
2063 AsString.Bytes = Lit->getBytes();
2064 AsString.CharWidth = Lit->getCharByteWidth();
2065 return true;
2066}
2067
2068// Determine whether two string literals potentially overlap. This will be the
2069// case if they agree on the values of all the bytes on the overlapping region
2070// between them.
2071//
2072// The overlapping region is the portion of the two string literals that must
2073// overlap in memory if the pointers actually point to the same address at
2074// runtime. For example, if LHS is "abcdef" + 3 and RHS is "cdef\0gh" + 1 then
2075// the overlapping region is "cdef\0", which in this case does agree, so the
2076// strings are potentially overlapping. Conversely, for "foobar" + 3 versus
2077// "bazbar" + 3, the overlapping region contains all of both strings, so they
2078// are not potentially overlapping, even though they agree from the given
2079// addresses onwards.
2080//
2081// See open core issue CWG2765 which is discussing the desired rule here.
2082static bool ArePotentiallyOverlappingStringLiterals(const EvalInfo &Info,
2083 const LValue &LHS,
2084 const LValue &RHS) {
2085 LValueBaseString LHSString, RHSString;
2086 if (!GetLValueBaseAsString(Info, LVal: LHS, AsString&: LHSString) ||
2087 !GetLValueBaseAsString(Info, LVal: RHS, AsString&: RHSString))
2088 return false;
2089
2090 // This is the byte offset to the location of the first character of LHS
2091 // within RHS. We don't need to look at the characters of one string that
2092 // would appear before the start of the other string if they were merged.
2093 CharUnits Offset = RHS.Offset - LHS.Offset;
2094 if (Offset.isNegative()) {
2095 if (LHSString.Bytes.size() < (size_t)-Offset.getQuantity())
2096 return false;
2097 LHSString.Bytes = LHSString.Bytes.drop_front(N: -Offset.getQuantity());
2098 } else {
2099 if (RHSString.Bytes.size() < (size_t)Offset.getQuantity())
2100 return false;
2101 RHSString.Bytes = RHSString.Bytes.drop_front(N: Offset.getQuantity());
2102 }
2103
2104 bool LHSIsLonger = LHSString.Bytes.size() > RHSString.Bytes.size();
2105 StringRef Longer = LHSIsLonger ? LHSString.Bytes : RHSString.Bytes;
2106 StringRef Shorter = LHSIsLonger ? RHSString.Bytes : LHSString.Bytes;
2107 int ShorterCharWidth = (LHSIsLonger ? RHSString : LHSString).CharWidth;
2108
2109 // The null terminator isn't included in the string data, so check for it
2110 // manually. If the longer string doesn't have a null terminator where the
2111 // shorter string ends, they aren't potentially overlapping.
2112 for (int NullByte : llvm::seq(Size: ShorterCharWidth)) {
2113 if (Shorter.size() + NullByte >= Longer.size())
2114 break;
2115 if (Longer[Shorter.size() + NullByte])
2116 return false;
2117 }
2118
2119 // Otherwise, they're potentially overlapping if and only if the overlapping
2120 // region is the same.
2121 return Shorter == Longer.take_front(N: Shorter.size());
2122}
2123
2124static bool IsWeakLValue(const LValue &Value) {
2125 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2126 return Decl && Decl->isWeak();
2127}
2128
2129static bool isZeroSized(const LValue &Value) {
2130 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2131 if (isa_and_nonnull<VarDecl>(Val: Decl)) {
2132 QualType Ty = Decl->getType();
2133 if (Ty->isArrayType())
2134 return Ty->isIncompleteType() ||
2135 Decl->getASTContext().getTypeSize(T: Ty) == 0;
2136 }
2137 return false;
2138}
2139
2140static bool HasSameBase(const LValue &A, const LValue &B) {
2141 if (!A.getLValueBase())
2142 return !B.getLValueBase();
2143 if (!B.getLValueBase())
2144 return false;
2145
2146 if (A.getLValueBase().getOpaqueValue() !=
2147 B.getLValueBase().getOpaqueValue())
2148 return false;
2149
2150 return A.getLValueCallIndex() == B.getLValueCallIndex() &&
2151 A.getLValueVersion() == B.getLValueVersion();
2152}
2153
2154static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) {
2155 assert(Base && "no location for a null lvalue");
2156 const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
2157
2158 // For a parameter, find the corresponding call stack frame (if it still
2159 // exists), and point at the parameter of the function definition we actually
2160 // invoked.
2161 if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(Val: VD)) {
2162 unsigned Idx = PVD->getFunctionScopeIndex();
2163 for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) {
2164 if (F->Arguments.CallIndex == Base.getCallIndex() &&
2165 F->Arguments.Version == Base.getVersion() && F->Callee &&
2166 Idx < F->Callee->getNumParams()) {
2167 VD = F->Callee->getParamDecl(i: Idx);
2168 break;
2169 }
2170 }
2171 }
2172
2173 if (VD)
2174 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
2175 else if (const Expr *E = Base.dyn_cast<const Expr*>())
2176 Info.Note(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
2177 else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
2178 // FIXME: Produce a note for dangling pointers too.
2179 if (std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA))
2180 Info.Note(Loc: (*Alloc)->AllocExpr->getExprLoc(),
2181 DiagId: diag::note_constexpr_dynamic_alloc_here);
2182 }
2183
2184 // We have no information to show for a typeid(T) object.
2185}
2186
2187enum class CheckEvaluationResultKind {
2188 ConstantExpression,
2189 FullyInitialized,
2190};
2191
2192/// Materialized temporaries that we've already checked to determine if they're
2193/// initializsed by a constant expression.
2194using CheckedTemporaries =
2195 llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>;
2196
2197static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2198 EvalInfo &Info, SourceLocation DiagLoc,
2199 QualType Type, const APValue &Value,
2200 ConstantExprKind Kind,
2201 const FieldDecl *SubobjectDecl,
2202 CheckedTemporaries &CheckedTemps,
2203 bool IsCompleteClass = true);
2204
2205/// Check that this reference or pointer core constant expression is a valid
2206/// value for an address or reference constant expression. Return true if we
2207/// can fold this expression, whether or not it's a constant expression.
2208static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
2209 QualType Type, const LValue &LVal,
2210 ConstantExprKind Kind,
2211 CheckedTemporaries &CheckedTemps) {
2212 bool IsReferenceType = Type->isReferenceType();
2213
2214 APValue::LValueBase Base = LVal.getLValueBase();
2215 const SubobjectDesignator &Designator = LVal.getLValueDesignator();
2216
2217 const Expr *BaseE = Base.dyn_cast<const Expr *>();
2218 const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>();
2219
2220 // Additional restrictions apply in a template argument. We only enforce the
2221 // C++20 restrictions here; additional syntactic and semantic restrictions
2222 // are applied elsewhere.
2223 if (isTemplateArgument(Kind)) {
2224 int InvalidBaseKind = -1;
2225 StringRef Ident;
2226 if (Base.is<TypeInfoLValue>())
2227 InvalidBaseKind = 0;
2228 else if (isa_and_nonnull<StringLiteral>(Val: BaseE))
2229 InvalidBaseKind = 1;
2230 else if (isa_and_nonnull<MaterializeTemporaryExpr>(Val: BaseE) ||
2231 isa_and_nonnull<LifetimeExtendedTemporaryDecl>(Val: BaseVD))
2232 InvalidBaseKind = 2;
2233 else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(Val: BaseE)) {
2234 InvalidBaseKind = 3;
2235 Ident = PE->getIdentKindName();
2236 }
2237
2238 if (InvalidBaseKind != -1) {
2239 Info.FFDiag(Loc, DiagId: diag::note_constexpr_invalid_template_arg)
2240 << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind
2241 << Ident;
2242 return false;
2243 }
2244 }
2245
2246 if (auto *FD = dyn_cast_or_null<FunctionDecl>(Val: BaseVD);
2247 FD && FD->isImmediateFunction()) {
2248 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible)
2249 << !Type->isAnyPointerType();
2250 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2251 return false;
2252 }
2253
2254 // Check that the object is a global. Note that the fake 'this' object we
2255 // manufacture when checking potential constant expressions is conservatively
2256 // assumed to be global here.
2257 if (!IsGlobalLValue(B: Base)) {
2258 if (Info.getLangOpts().CPlusPlus11) {
2259 Info.FFDiag(Loc, DiagId: diag::note_constexpr_non_global, ExtraNotes: 1)
2260 << IsReferenceType << !Designator.Entries.empty() << !!BaseVD
2261 << BaseVD;
2262 auto *VarD = dyn_cast_or_null<VarDecl>(Val: BaseVD);
2263 if (VarD && VarD->isConstexpr()) {
2264 // Non-static local constexpr variables have unintuitive semantics:
2265 // constexpr int a = 1;
2266 // constexpr const int *p = &a;
2267 // ... is invalid because the address of 'a' is not constant. Suggest
2268 // adding a 'static' in this case.
2269 Info.Note(Loc: VarD->getLocation(), DiagId: diag::note_constexpr_not_static)
2270 << VarD
2271 << FixItHint::CreateInsertion(InsertionLoc: VarD->getBeginLoc(), Code: "static ");
2272 } else {
2273 NoteLValueLocation(Info, Base);
2274 }
2275 } else {
2276 Info.FFDiag(Loc);
2277 }
2278 // Don't allow references to temporaries to escape.
2279 return false;
2280 }
2281 assert((Info.checkingPotentialConstantExpression() ||
2282 LVal.getLValueCallIndex() == 0) &&
2283 "have call index for global lvalue");
2284
2285 if (LVal.allowConstexprUnknown()) {
2286 if (BaseVD) {
2287 Info.FFDiag(Loc, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << BaseVD;
2288 NoteLValueLocation(Info, Base);
2289 } else {
2290 Info.FFDiag(Loc);
2291 }
2292 return false;
2293 }
2294
2295 if (Base.is<DynamicAllocLValue>()) {
2296 Info.FFDiag(Loc, DiagId: diag::note_constexpr_dynamic_alloc)
2297 << IsReferenceType << !Designator.Entries.empty();
2298 NoteLValueLocation(Info, Base);
2299 return false;
2300 }
2301
2302 if (BaseVD) {
2303 if (const VarDecl *Var = dyn_cast<const VarDecl>(Val: BaseVD)) {
2304 // Check if this is a thread-local variable.
2305 if (Var->getTLSKind())
2306 // FIXME: Diagnostic!
2307 return false;
2308
2309 // A dllimport variable never acts like a constant, unless we're
2310 // evaluating a value for use only in name mangling, and unless it's a
2311 // static local. For the latter case, we'd still need to evaluate the
2312 // constant expression in case we're inside a (inlined) function.
2313 if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>() &&
2314 !Var->isStaticLocal())
2315 return false;
2316
2317 // Address of a managed variable is never a constant expression.
2318 if (Info.getLangOpts().CUDA && Var->hasAttr<HIPManagedAttr>())
2319 return false;
2320
2321 // In CUDA/HIP device compilation, only device side variables have
2322 // constant addresses.
2323 if (Info.getLangOpts().CUDA && Info.getLangOpts().CUDAIsDevice &&
2324 Info.Ctx.CUDAConstantEvalCtx.NoWrongSidedVars) {
2325 if ((!Var->hasAttr<CUDADeviceAttr>() &&
2326 !Var->hasAttr<CUDAConstantAttr>() &&
2327 !Var->getType()->isCUDADeviceBuiltinSurfaceType() &&
2328 !Var->getType()->isCUDADeviceBuiltinTextureType()))
2329 return false;
2330 }
2331 }
2332 if (const auto *FD = dyn_cast<const FunctionDecl>(Val: BaseVD)) {
2333 // __declspec(dllimport) must be handled very carefully:
2334 // We must never initialize an expression with the thunk in C++.
2335 // Doing otherwise would allow the same id-expression to yield
2336 // different addresses for the same function in different translation
2337 // units. However, this means that we must dynamically initialize the
2338 // expression with the contents of the import address table at runtime.
2339 //
2340 // The C language has no notion of ODR; furthermore, it has no notion of
2341 // dynamic initialization. This means that we are permitted to
2342 // perform initialization with the address of the thunk.
2343 if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) &&
2344 FD->hasAttr<DLLImportAttr>())
2345 // FIXME: Diagnostic!
2346 return false;
2347 }
2348 } else if (const auto *MTE =
2349 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: BaseE)) {
2350 if (CheckedTemps.insert(Ptr: MTE).second) {
2351 QualType TempType = getType(B: Base);
2352 if (TempType.isDestructedType()) {
2353 Info.FFDiag(Loc: MTE->getExprLoc(),
2354 DiagId: diag::note_constexpr_unsupported_temporary_nontrivial_dtor)
2355 << TempType;
2356 return false;
2357 }
2358
2359 APValue *V = MTE->getOrCreateValue(MayCreate: false);
2360 assert(V && "evasluation result refers to uninitialised temporary");
2361 if (!CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2362 Info, DiagLoc: MTE->getExprLoc(), Type: TempType, Value: *V, Kind,
2363 /*SubobjectDecl=*/nullptr, CheckedTemps))
2364 return false;
2365 }
2366 }
2367
2368 // Allow address constant expressions to be past-the-end pointers. This is
2369 // an extension: the standard requires them to point to an object.
2370 if (!IsReferenceType)
2371 return true;
2372
2373 // A reference constant expression must refer to an object.
2374 if (!Base) {
2375 // FIXME: diagnostic
2376 Info.CCEDiag(Loc);
2377 return true;
2378 }
2379
2380 // Does this refer one past the end of some object?
2381 if (!Designator.Invalid && Designator.isOnePastTheEnd()) {
2382 Info.FFDiag(Loc, DiagId: diag::note_constexpr_past_end, ExtraNotes: 1)
2383 << !Designator.Entries.empty() << !!BaseVD << BaseVD;
2384 NoteLValueLocation(Info, Base);
2385 }
2386
2387 return true;
2388}
2389
2390/// Member pointers are constant expressions unless they point to a
2391/// non-virtual dllimport member function.
2392static bool CheckMemberPointerConstantExpression(EvalInfo &Info,
2393 SourceLocation Loc,
2394 QualType Type,
2395 const APValue &Value,
2396 ConstantExprKind Kind) {
2397 const ValueDecl *Member = Value.getMemberPointerDecl();
2398 const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Val: Member);
2399 if (!FD)
2400 return true;
2401 if (FD->isImmediateFunction()) {
2402 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible) << /*pointer*/ 0;
2403 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2404 return false;
2405 }
2406 return isForManglingOnly(Kind) || FD->isVirtual() ||
2407 !FD->hasAttr<DLLImportAttr>();
2408}
2409
2410/// Check that this core constant expression is of literal type, and if not,
2411/// produce an appropriate diagnostic.
2412static bool CheckLiteralType(EvalInfo &Info, const Expr *E,
2413 const LValue *This = nullptr) {
2414 // The restriction to literal types does not exist in C++23 anymore.
2415 if (Info.getLangOpts().CPlusPlus23)
2416 return true;
2417
2418 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Info.Ctx))
2419 return true;
2420
2421 // C++1y: A constant initializer for an object o [...] may also invoke
2422 // constexpr constructors for o and its subobjects even if those objects
2423 // are of non-literal class types.
2424 //
2425 // C++11 missed this detail for aggregates, so classes like this:
2426 // struct foo_t { union { int i; volatile int j; } u; };
2427 // are not (obviously) initializable like so:
2428 // __attribute__((__require_constant_initialization__))
2429 // static const foo_t x = {{0}};
2430 // because "i" is a subobject with non-literal initialization (due to the
2431 // volatile member of the union). See:
2432 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677
2433 // Therefore, we use the C++1y behavior.
2434 if (This && Info.EvaluatingDecl == This->getLValueBase())
2435 return true;
2436
2437 // Prvalue constant expressions must be of literal types.
2438 if (Info.getLangOpts().CPlusPlus11)
2439 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral)
2440 << E->getType();
2441 else
2442 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
2443 return false;
2444}
2445
2446static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2447 EvalInfo &Info, SourceLocation DiagLoc,
2448 QualType Type, const APValue &Value,
2449 ConstantExprKind Kind,
2450 const FieldDecl *SubobjectDecl,
2451 CheckedTemporaries &CheckedTemps,
2452 bool IsCompleteClass) {
2453 if (!Value.hasValue()) {
2454 if (SubobjectDecl) {
2455 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2456 << /*(name)*/ 1 << SubobjectDecl;
2457 Info.Note(Loc: SubobjectDecl->getLocation(),
2458 DiagId: diag::note_constexpr_subobject_declared_here);
2459 } else {
2460 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2461 << /*of type*/ 0 << Type;
2462 }
2463 return false;
2464 }
2465
2466 // We allow _Atomic(T) to be initialized from anything that T can be
2467 // initialized from.
2468 if (const AtomicType *AT = Type->getAs<AtomicType>())
2469 Type = AT->getValueType();
2470
2471 // Core issue 1454: For a literal constant expression of array or class type,
2472 // each subobject of its value shall have been initialized by a constant
2473 // expression.
2474 if (Value.isArray()) {
2475 QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();
2476 for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {
2477 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2478 Value: Value.getArrayInitializedElt(I), Kind,
2479 SubobjectDecl, CheckedTemps))
2480 return false;
2481 }
2482 if (!Value.hasArrayFiller())
2483 return true;
2484 return CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2485 Value: Value.getArrayFiller(), Kind, SubobjectDecl,
2486 CheckedTemps);
2487 }
2488 if (Value.isUnion() && Value.getUnionField()) {
2489 return CheckEvaluationResult(
2490 CERK, Info, DiagLoc, Type: Value.getUnionField()->getType(),
2491 Value: Value.getUnionValue(), Kind, SubobjectDecl: Value.getUnionField(), CheckedTemps);
2492 }
2493 if (Value.isStruct()) {
2494 auto *RD = Type->castAsRecordDecl();
2495 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2496 unsigned BaseIndex = 0;
2497 for (const CXXBaseSpecifier &BS : CD->bases()) {
2498 if (BS.isVirtual())
2499 continue;
2500 const APValue &BaseValue = Value.getStructBase(i: BaseIndex);
2501 if (!BaseValue.hasValue()) {
2502 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2503 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2504 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2505 return false;
2506 }
2507 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(), Value: BaseValue,
2508 Kind, /*SubobjectDecl=*/nullptr,
2509 CheckedTemps, /*IsCompleteClass=*/false))
2510 return false;
2511 ++BaseIndex;
2512 }
2513 }
2514 for (const auto *I : RD->fields()) {
2515 if (I->isUnnamedBitField())
2516 continue;
2517
2518 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: I->getType(),
2519 Value: Value.getStructField(i: I->getFieldIndex()), Kind,
2520 SubobjectDecl: I, CheckedTemps))
2521 return false;
2522 }
2523
2524 if (IsCompleteClass) {
2525 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2526 unsigned BaseIndex = 0;
2527 for (const CXXBaseSpecifier &BS : CD->vbases()) {
2528 assert(BS.isVirtual());
2529 const APValue &BaseValue = Value.getStructVirtualBase(i: BaseIndex);
2530 if (!BaseValue.hasValue()) {
2531 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2532 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2533 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2534 return false;
2535 }
2536 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(),
2537 Value: BaseValue, Kind, /*SubobjectDecl=*/nullptr,
2538 CheckedTemps, /*IsCompleteClass=*/false))
2539 return false;
2540 ++BaseIndex;
2541 }
2542 }
2543 }
2544 }
2545
2546 if (Value.isLValue() &&
2547 CERK == CheckEvaluationResultKind::ConstantExpression) {
2548 LValue LVal;
2549 LVal.setFrom(Ctx: Info.Ctx, V: Value);
2550 return CheckLValueConstantExpression(Info, Loc: DiagLoc, Type, LVal, Kind,
2551 CheckedTemps);
2552 }
2553
2554 if (Value.isMemberPointer() &&
2555 CERK == CheckEvaluationResultKind::ConstantExpression)
2556 return CheckMemberPointerConstantExpression(Info, Loc: DiagLoc, Type, Value, Kind);
2557
2558 // Everything else is fine.
2559 return true;
2560}
2561
2562/// Check that this core constant expression value is a valid value for a
2563/// constant expression. If not, report an appropriate diagnostic. Does not
2564/// check that the expression is of literal type.
2565static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
2566 QualType Type, const APValue &Value,
2567 ConstantExprKind Kind) {
2568 // Nothing to check for a constant expression of type 'cv void'.
2569 if (Type->isVoidType())
2570 return true;
2571
2572 CheckedTemporaries CheckedTemps;
2573 return CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2574 Info, DiagLoc, Type, Value, Kind,
2575 /*SubobjectDecl=*/nullptr, CheckedTemps);
2576}
2577
2578/// Check that this evaluated value is fully-initialized and can be loaded by
2579/// an lvalue-to-rvalue conversion.
2580static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc,
2581 QualType Type, const APValue &Value) {
2582 CheckedTemporaries CheckedTemps;
2583 return CheckEvaluationResult(
2584 CERK: CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value,
2585 Kind: ConstantExprKind::Normal, /*SubobjectDecl=*/nullptr, CheckedTemps);
2586}
2587
2588/// Enforce C++2a [expr.const]/4.17, which disallows new-expressions unless
2589/// "the allocated storage is deallocated within the evaluation".
2590static bool CheckMemoryLeaks(EvalInfo &Info) {
2591 if (!Info.HeapAllocs.empty()) {
2592 // We can still fold to a constant despite a compile-time memory leak,
2593 // so long as the heap allocation isn't referenced in the result (we check
2594 // that in CheckConstantExpression).
2595 Info.CCEDiag(E: Info.HeapAllocs.begin()->second.AllocExpr,
2596 DiagId: diag::note_constexpr_memory_leak)
2597 << unsigned(Info.HeapAllocs.size() - 1);
2598 }
2599 return true;
2600}
2601
2602static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {
2603 // A null base expression indicates a null pointer. These are always
2604 // evaluatable, and they are false unless the offset is zero.
2605 if (!Value.getLValueBase()) {
2606 // TODO: Should a non-null pointer with an offset of zero evaluate to true?
2607 Result = !Value.getLValueOffset().isZero();
2608 return true;
2609 }
2610
2611 // We have a non-null base. These are generally known to be true, but if it's
2612 // a weak declaration it can be null at runtime.
2613 Result = true;
2614 const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
2615 return !Decl || !Decl->isWeak();
2616}
2617
2618static bool HandleConversionToBool(const APValue &Val, bool &Result) {
2619 // TODO: This function should produce notes if it fails.
2620 switch (Val.getKind()) {
2621 case APValue::None:
2622 case APValue::Indeterminate:
2623 return false;
2624 case APValue::Int:
2625 Result = Val.getInt().getBoolValue();
2626 return true;
2627 case APValue::FixedPoint:
2628 Result = Val.getFixedPoint().getBoolValue();
2629 return true;
2630 case APValue::Float:
2631 Result = !Val.getFloat().isZero();
2632 return true;
2633 case APValue::ComplexInt:
2634 Result = Val.getComplexIntReal().getBoolValue() ||
2635 Val.getComplexIntImag().getBoolValue();
2636 return true;
2637 case APValue::ComplexFloat:
2638 Result = !Val.getComplexFloatReal().isZero() ||
2639 !Val.getComplexFloatImag().isZero();
2640 return true;
2641 case APValue::LValue:
2642 return EvalPointerValueAsBool(Value: Val, Result);
2643 case APValue::MemberPointer:
2644 if (Val.getMemberPointerDecl() && Val.getMemberPointerDecl()->isWeak()) {
2645 return false;
2646 }
2647 Result = Val.getMemberPointerDecl();
2648 return true;
2649 case APValue::Vector:
2650 case APValue::Matrix:
2651 case APValue::Array:
2652 case APValue::Struct:
2653 case APValue::Union:
2654 case APValue::AddrLabelDiff:
2655 return false;
2656 }
2657
2658 llvm_unreachable("unknown APValue kind");
2659}
2660
2661static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
2662 EvalInfo &Info) {
2663 assert(!E->isValueDependent());
2664 assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition");
2665 APValue Val;
2666 if (!Evaluate(Result&: Val, Info, E))
2667 return false;
2668 return HandleConversionToBool(Val, Result);
2669}
2670
2671template<typename T>
2672static bool HandleOverflow(EvalInfo &Info, const Expr *E,
2673 const T &SrcValue, QualType DestType) {
2674 Info.CCEDiag(E, DiagId: diag::note_constexpr_overflow) << SrcValue << DestType;
2675 if (const auto *OBT = DestType->getAs<OverflowBehaviorType>();
2676 OBT && OBT->isTrapKind()) {
2677 return false;
2678 }
2679 return Info.noteUndefinedBehavior();
2680}
2681
2682static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
2683 QualType SrcType, const APFloat &Value,
2684 QualType DestType, APSInt &Result) {
2685 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2686 // Determine whether we are converting to unsigned or signed.
2687 bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
2688
2689 Result = APSInt(DestWidth, !DestSigned);
2690 bool ignored;
2691 if (Value.convertToInteger(Result, RM: llvm::APFloat::rmTowardZero, IsExact: &ignored)
2692 & APFloat::opInvalidOp)
2693 return HandleOverflow(Info, E, SrcValue: Value, DestType);
2694 return true;
2695}
2696
2697/// Get rounding mode to use in evaluation of the specified expression.
2698///
2699/// If rounding mode is unknown at compile time, still try to evaluate the
2700/// expression. If the result is exact, it does not depend on rounding mode.
2701/// So return "tonearest" mode instead of "dynamic".
2702static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E) {
2703 llvm::RoundingMode RM =
2704 E->getFPFeaturesInEffect(LO: Info.getLangOpts()).getRoundingMode();
2705 if (RM == llvm::RoundingMode::Dynamic)
2706 RM = llvm::RoundingMode::NearestTiesToEven;
2707 return RM;
2708}
2709
2710/// Check if the given floating-point evaluation result is allowed for
2711/// compile-time constant folding during translation (as opposed to mandatory
2712/// constant expression evaluation).
2713static bool checkFloatingPointResultForConstantFolding(EvalInfo &Info,
2714 const Expr *E,
2715 APFloat::opStatus St) {
2716 // In a constant context, assume that any dynamic rounding mode or FP
2717 // exception state matches the default floating-point environment.
2718 if (Info.InConstantContext)
2719 return true;
2720
2721 FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.getLangOpts());
2722 if ((St & APFloat::opInexact) &&
2723 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
2724 // Inexact result means that it depends on rounding mode. If the requested
2725 // mode is dynamic, the evaluation cannot be made in compile time.
2726 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
2727 return false;
2728 }
2729
2730 if ((St != APFloat::opOK) &&
2731 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||
2732 FPO.getExceptionMode() != LangOptions::FPE_Ignore ||
2733 FPO.getAllowFEnvAccess())) {
2734 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
2735 return false;
2736 }
2737
2738 if ((St & APFloat::opStatus::opInvalidOp) &&
2739 FPO.getExceptionMode() != LangOptions::FPE_Ignore) {
2740 // There is no usefully definable result.
2741 Info.FFDiag(E);
2742 return false;
2743 }
2744
2745 // FIXME: if:
2746 // - evaluation triggered other FP exception, and
2747 // - exception mode is not "ignore", and
2748 // - the expression being evaluated is not a part of global variable
2749 // initializer,
2750 // the evaluation probably need to be rejected.
2751 return true;
2752}
2753
2754static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
2755 QualType SrcType, QualType DestType,
2756 APFloat &Result) {
2757 assert((isa<CastExpr>(E) || isa<CompoundAssignOperator>(E) ||
2758 isa<ConvertVectorExpr>(E)) &&
2759 "HandleFloatToFloatCast has been checked with only CastExpr, "
2760 "CompoundAssignOperator and ConvertVectorExpr. Please either validate "
2761 "the new expression or address the root cause of this usage.");
2762 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2763 APFloat::opStatus St;
2764 APFloat Value = Result;
2765 bool ignored;
2766 St = Result.convert(ToSemantics: Info.Ctx.getFloatTypeSemantics(T: DestType), RM, losesInfo: &ignored);
2767 return checkFloatingPointResultForConstantFolding(Info, E, St);
2768}
2769
2770static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
2771 QualType DestType, QualType SrcType,
2772 const APSInt &Value) {
2773 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2774 // Figure out if this is a truncate, extend or noop cast.
2775 // If the input is signed, do a sign extend, noop, or truncate.
2776 APSInt Result = Value.extOrTrunc(width: DestWidth);
2777 Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
2778 if (DestType->isBooleanType())
2779 Result = Value.getBoolValue();
2780 return Result;
2781}
2782
2783static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
2784 const FPOptions FPO,
2785 QualType SrcType, const APSInt &Value,
2786 QualType DestType, APFloat &Result) {
2787 Result = APFloat(Info.Ctx.getFloatTypeSemantics(T: DestType), 1);
2788 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2789 APFloat::opStatus St = Result.convertFromAPInt(Input: Value, IsSigned: Value.isSigned(), RM);
2790 return checkFloatingPointResultForConstantFolding(Info, E, St);
2791}
2792
2793static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E,
2794 APValue &Value, const FieldDecl *FD) {
2795 assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield");
2796
2797 if (!Value.isInt()) {
2798 // Trying to store a pointer-cast-to-integer into a bitfield.
2799 // FIXME: In this case, we should provide the diagnostic for casting
2800 // a pointer to an integer.
2801 assert(Value.isLValue() && "integral value neither int nor lvalue?");
2802 Info.FFDiag(E);
2803 return false;
2804 }
2805
2806 APSInt &Int = Value.getInt();
2807 unsigned OldBitWidth = Int.getBitWidth();
2808 unsigned NewBitWidth = FD->getBitWidthValue();
2809 if (NewBitWidth < OldBitWidth)
2810 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
2811 return true;
2812}
2813
2814/// Perform the given integer operation, which is known to need at most BitWidth
2815/// bits, and check for overflow in the original type (if that type was not an
2816/// unsigned type).
2817template<typename Operation>
2818static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
2819 const APSInt &LHS, const APSInt &RHS,
2820 unsigned BitWidth, Operation Op,
2821 APSInt &Result) {
2822 if (LHS.isUnsigned()) {
2823 Result = Op(LHS, RHS);
2824 return true;
2825 }
2826
2827 APSInt Value(Op(LHS.extend(width: BitWidth), RHS.extend(width: BitWidth)), false);
2828 Result = Value.trunc(width: LHS.getBitWidth());
2829 if (Result.extend(width: BitWidth) != Value && !E->getType().isWrapType()) {
2830 if (Info.checkingForUndefinedBehavior())
2831 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
2832 DiagID: diag::warn_integer_constant_overflow)
2833 << toString(I: Result, Radix: 10, Signed: Result.isSigned(), /*formatAsCLiteral=*/false,
2834 /*UpperCase=*/true, /*InsertSeparators=*/true)
2835 << E->getType() << E->getSourceRange();
2836 return HandleOverflow(Info, E, SrcValue: Value, DestType: E->getType());
2837 }
2838 return true;
2839}
2840
2841/// Perform the given binary integer operation.
2842static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E,
2843 const APSInt &LHS, BinaryOperatorKind Opcode,
2844 APSInt RHS, APSInt &Result) {
2845 bool HandleOverflowResult = true;
2846 switch (Opcode) {
2847 default:
2848 Info.FFDiag(E);
2849 return false;
2850 case BO_Mul:
2851 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() * 2,
2852 Op: std::multiplies<APSInt>(), Result);
2853 case BO_Add:
2854 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2855 Op: std::plus<APSInt>(), Result);
2856 case BO_Sub:
2857 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2858 Op: std::minus<APSInt>(), Result);
2859 case BO_And: Result = LHS & RHS; return true;
2860 case BO_Xor: Result = LHS ^ RHS; return true;
2861 case BO_Or: Result = LHS | RHS; return true;
2862 case BO_Div:
2863 case BO_Rem:
2864 if (RHS == 0) {
2865 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero)
2866 << E->getRHS()->getSourceRange();
2867 return false;
2868 }
2869 // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports
2870 // this operation and gives the two's complement result.
2871 if (RHS.isNegative() && RHS.isAllOnes() && LHS.isSigned() &&
2872 LHS.isMinSignedValue())
2873 HandleOverflowResult = HandleOverflow(
2874 Info, E, SrcValue: -LHS.extend(width: LHS.getBitWidth() + 1), DestType: E->getType());
2875 Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS);
2876 return HandleOverflowResult;
2877 case BO_Shl: {
2878 if (Info.getLangOpts().OpenCL)
2879 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2880 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2881 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2882 RHS.isUnsigned());
2883 else if (RHS.isSigned() && RHS.isNegative()) {
2884 // During constant-folding, a negative shift is an opposite shift. Such
2885 // a shift is not a constant expression.
2886 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2887 if (!Info.noteUndefinedBehavior())
2888 return false;
2889 RHS = -RHS;
2890 goto shift_right;
2891 }
2892 shift_left:
2893 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
2894 // the shifted type.
2895 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2896 if (SA != RHS) {
2897 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
2898 << RHS << E->getType() << LHS.getBitWidth();
2899 if (!Info.noteUndefinedBehavior())
2900 return false;
2901 } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) {
2902 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
2903 // operand, and must not overflow the corresponding unsigned type.
2904 // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to
2905 // E1 x 2^E2 module 2^N.
2906 if (LHS.isNegative()) {
2907 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_of_negative) << LHS;
2908 if (!Info.noteUndefinedBehavior())
2909 return false;
2910 } else if (LHS.countl_zero() < SA) {
2911 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_discards);
2912 if (!Info.noteUndefinedBehavior())
2913 return false;
2914 }
2915 }
2916 Result = LHS << SA;
2917 return true;
2918 }
2919 case BO_Shr: {
2920 if (Info.getLangOpts().OpenCL)
2921 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2922 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2923 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2924 RHS.isUnsigned());
2925 else if (RHS.isSigned() && RHS.isNegative()) {
2926 // During constant-folding, a negative shift is an opposite shift. Such a
2927 // shift is not a constant expression.
2928 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2929 if (!Info.noteUndefinedBehavior())
2930 return false;
2931 RHS = -RHS;
2932 goto shift_left;
2933 }
2934 shift_right:
2935 // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
2936 // shifted type.
2937 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2938 if (SA != RHS) {
2939 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
2940 << RHS << E->getType() << LHS.getBitWidth();
2941 if (!Info.noteUndefinedBehavior())
2942 return false;
2943 }
2944
2945 Result = LHS >> SA;
2946 return true;
2947 }
2948
2949 case BO_LT: Result = LHS < RHS; return true;
2950 case BO_GT: Result = LHS > RHS; return true;
2951 case BO_LE: Result = LHS <= RHS; return true;
2952 case BO_GE: Result = LHS >= RHS; return true;
2953 case BO_EQ: Result = LHS == RHS; return true;
2954 case BO_NE: Result = LHS != RHS; return true;
2955 case BO_Cmp:
2956 llvm_unreachable("BO_Cmp should be handled elsewhere");
2957 }
2958}
2959
2960/// Perform the given binary floating-point operation, in-place, on LHS.
2961static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E,
2962 APFloat &LHS, BinaryOperatorKind Opcode,
2963 const APFloat &RHS) {
2964 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2965 APFloat::opStatus St;
2966 switch (Opcode) {
2967 default:
2968 Info.FFDiag(E);
2969 return false;
2970 case BO_Mul:
2971 St = LHS.multiply(RHS, RM);
2972 break;
2973 case BO_Add:
2974 St = LHS.add(RHS, RM);
2975 break;
2976 case BO_Sub:
2977 St = LHS.subtract(RHS, RM);
2978 break;
2979 case BO_Div:
2980 // [expr.mul]p4:
2981 // If the second operand of / or % is zero the behavior is undefined.
2982 if (RHS.isZero())
2983 Info.CCEDiag(E, DiagId: diag::note_expr_divide_by_zero);
2984 St = LHS.divide(RHS, RM);
2985 break;
2986 }
2987
2988 // FIXME: The standard quote below is deleted by P3899R3.
2989 // [expr.pre]p4:
2990 // If during the evaluation of an expression, the result is not
2991 // mathematically defined [...], the behavior is undefined.
2992 // FIXME: C++ rules require us to not conform to IEEE 754 here.
2993 // FIXME: The NaN check should not be applied outside of "constant contexts"
2994 // because it prevents NaN propagation and the "invalid" status is the
2995 // responsibility of checkFloatingPointResultForConstantFolding.
2996 if (LHS.isNaN()) {
2997 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << LHS.isNaN();
2998 return Info.noteUndefinedBehavior();
2999 }
3000
3001 return checkFloatingPointResultForConstantFolding(Info, E, St);
3002}
3003
3004static bool handleLogicalOpForVector(const APInt &LHSValue,
3005 BinaryOperatorKind Opcode,
3006 const APInt &RHSValue, APInt &Result) {
3007 bool LHS = (LHSValue != 0);
3008 bool RHS = (RHSValue != 0);
3009
3010 if (Opcode == BO_LAnd)
3011 Result = LHS && RHS;
3012 else
3013 Result = LHS || RHS;
3014 return true;
3015}
3016static bool handleLogicalOpForVector(const APFloat &LHSValue,
3017 BinaryOperatorKind Opcode,
3018 const APFloat &RHSValue, APInt &Result) {
3019 bool LHS = !LHSValue.isZero();
3020 bool RHS = !RHSValue.isZero();
3021
3022 if (Opcode == BO_LAnd)
3023 Result = LHS && RHS;
3024 else
3025 Result = LHS || RHS;
3026 return true;
3027}
3028
3029static bool handleLogicalOpForVector(const APValue &LHSValue,
3030 BinaryOperatorKind Opcode,
3031 const APValue &RHSValue, APInt &Result) {
3032 // The result is always an int type, however operands match the first.
3033 if (LHSValue.getKind() == APValue::Int)
3034 return handleLogicalOpForVector(LHSValue: LHSValue.getInt(), Opcode,
3035 RHSValue: RHSValue.getInt(), Result);
3036 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3037 return handleLogicalOpForVector(LHSValue: LHSValue.getFloat(), Opcode,
3038 RHSValue: RHSValue.getFloat(), Result);
3039}
3040
3041template <typename APTy>
3042static bool
3043handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode,
3044 const APTy &RHSValue, APInt &Result) {
3045 switch (Opcode) {
3046 default:
3047 llvm_unreachable("unsupported binary operator");
3048 case BO_EQ:
3049 Result = (LHSValue == RHSValue);
3050 break;
3051 case BO_NE:
3052 Result = (LHSValue != RHSValue);
3053 break;
3054 case BO_LT:
3055 Result = (LHSValue < RHSValue);
3056 break;
3057 case BO_GT:
3058 Result = (LHSValue > RHSValue);
3059 break;
3060 case BO_LE:
3061 Result = (LHSValue <= RHSValue);
3062 break;
3063 case BO_GE:
3064 Result = (LHSValue >= RHSValue);
3065 break;
3066 }
3067
3068 // The boolean operations on these vector types use an instruction that
3069 // results in a mask of '-1' for the 'truth' value. Ensure that we negate 1
3070 // to -1 to make sure that we produce the correct value.
3071 Result.negate();
3072
3073 return true;
3074}
3075
3076static bool handleCompareOpForVector(const APValue &LHSValue,
3077 BinaryOperatorKind Opcode,
3078 const APValue &RHSValue, APInt &Result) {
3079 // The result is always an int type, however operands match the first.
3080 if (LHSValue.getKind() == APValue::Int)
3081 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getInt(), Opcode,
3082 RHSValue: RHSValue.getInt(), Result);
3083 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3084 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getFloat(), Opcode,
3085 RHSValue: RHSValue.getFloat(), Result);
3086}
3087
3088// Perform binary operations for vector types, in place on the LHS.
3089static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E,
3090 BinaryOperatorKind Opcode,
3091 APValue &LHSValue,
3092 const APValue &RHSValue) {
3093 assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI &&
3094 "Operation not supported on vector types");
3095
3096 const auto *VT = E->getType()->castAs<VectorType>();
3097 unsigned NumElements = VT->getNumElements();
3098 QualType EltTy = VT->getElementType();
3099
3100 // In the cases (typically C as I've observed) where we aren't evaluating
3101 // constexpr but are checking for cases where the LHS isn't yet evaluatable,
3102 // just give up.
3103 if (!LHSValue.isVector()) {
3104 assert(LHSValue.isLValue() &&
3105 "A vector result that isn't a vector OR uncalculated LValue");
3106 Info.FFDiag(E);
3107 return false;
3108 }
3109
3110 assert(LHSValue.getVectorLength() == NumElements &&
3111 RHSValue.getVectorLength() == NumElements && "Different vector sizes");
3112
3113 SmallVector<APValue, 4> ResultElements;
3114
3115 for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) {
3116 APValue LHSElt = LHSValue.getVectorElt(I: EltNum);
3117 APValue RHSElt = RHSValue.getVectorElt(I: EltNum);
3118
3119 if (EltTy->isIntegerType()) {
3120 APSInt EltResult{Info.Ctx.getIntWidth(T: EltTy),
3121 EltTy->isUnsignedIntegerType()};
3122 bool Success = true;
3123
3124 if (BinaryOperator::isLogicalOp(Opc: Opcode))
3125 Success = handleLogicalOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3126 else if (BinaryOperator::isComparisonOp(Opc: Opcode))
3127 Success = handleCompareOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3128 else
3129 Success = handleIntIntBinOp(Info, E, LHS: LHSElt.getInt(), Opcode,
3130 RHS: RHSElt.getInt(), Result&: EltResult);
3131
3132 if (!Success) {
3133 Info.FFDiag(E);
3134 return false;
3135 }
3136 ResultElements.emplace_back(Args&: EltResult);
3137
3138 } else if (EltTy->isFloatingType()) {
3139 assert(LHSElt.getKind() == APValue::Float &&
3140 RHSElt.getKind() == APValue::Float &&
3141 "Mismatched LHS/RHS/Result Type");
3142 APFloat LHSFloat = LHSElt.getFloat();
3143
3144 if (!handleFloatFloatBinOp(Info, E, LHS&: LHSFloat, Opcode,
3145 RHS: RHSElt.getFloat())) {
3146 Info.FFDiag(E);
3147 return false;
3148 }
3149
3150 ResultElements.emplace_back(Args&: LHSFloat);
3151 }
3152 }
3153
3154 LHSValue = APValue(ResultElements.data(), ResultElements.size());
3155 return true;
3156}
3157
3158/// Cast an lvalue referring to a base subobject to a derived class, by
3159/// truncating the lvalue's path to the given length.
3160static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
3161 const RecordDecl *TruncatedType,
3162 unsigned TruncatedElements) {
3163 SubobjectDesignator &D = Result.Designator;
3164
3165 // Check we actually point to a derived class object.
3166 if (TruncatedElements == D.Entries.size())
3167 return true;
3168 assert(TruncatedElements >= D.MostDerivedPathLength &&
3169 "not casting to a derived class");
3170 if (!Result.checkSubobject(Info, E, CSK: CSK_Derived))
3171 return false;
3172
3173 // Truncate the path to the subobject, and remove any derived-to-base offsets.
3174 const RecordDecl *RD = TruncatedType;
3175 for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
3176 if (RD->isInvalidDecl()) return false;
3177 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
3178 const CXXRecordDecl *Base = getAsBaseClass(E: D.Entries[I]);
3179 if (isVirtualBaseClass(E: D.Entries[I]))
3180 Result.Offset -= Layout.getVBaseClassOffset(VBase: Base);
3181 else
3182 Result.Offset -= Layout.getBaseClassOffset(Base);
3183 RD = Base;
3184 }
3185 D.Entries.resize(N: TruncatedElements);
3186 return true;
3187}
3188
3189static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3190 const CXXRecordDecl *Derived,
3191 const CXXRecordDecl *Base,
3192 const ASTRecordLayout *RL = nullptr) {
3193 if (!RL) {
3194 if (Derived->isInvalidDecl()) return false;
3195 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3196 }
3197
3198 Obj.addDecl(Info, E, D: Base, /*Virtual=*/false);
3199 Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
3200 return true;
3201}
3202
3203static bool HandleLValueDirectVirtualBase(EvalInfo &Info, const Expr *E,
3204 LValue &Obj,
3205 const CXXRecordDecl *Derived,
3206 const CXXRecordDecl *Base,
3207 const ASTRecordLayout *RL = nullptr) {
3208 if (!RL) {
3209 if (Derived->isInvalidDecl())
3210 return false;
3211 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3212 }
3213
3214 Obj.addDecl(Info, E, D: Base, /*Virtual=*/true);
3215 Obj.getLValueOffset() += RL->getVBaseClassOffset(VBase: Base);
3216 return true;
3217}
3218
3219static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3220 const CXXRecordDecl *DerivedDecl,
3221 const CXXBaseSpecifier *Base) {
3222 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
3223
3224 if (!Base->isVirtual())
3225 return HandleLValueDirectBase(Info, E, Obj, Derived: DerivedDecl, Base: BaseDecl);
3226
3227 SubobjectDesignator &D = Obj.Designator;
3228 if (D.Invalid)
3229 return false;
3230
3231 // Extract most-derived object and corresponding type.
3232 // FIXME: After implementing P2280R4 it became possible to get references
3233 // here. We do MostDerivedType->getAsCXXRecordDecl() in several other
3234 // locations and if we see crashes in those locations in the future
3235 // it may make more sense to move this fix into Lvalue::set.
3236 DerivedDecl = D.MostDerivedType.getNonReferenceType()->getAsCXXRecordDecl();
3237 if (!CastToDerivedClass(Info, E, Result&: Obj, TruncatedType: DerivedDecl, TruncatedElements: D.MostDerivedPathLength))
3238 return false;
3239
3240 // Find the virtual base class.
3241 if (DerivedDecl->isInvalidDecl()) return false;
3242 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: DerivedDecl);
3243 Obj.addDecl(Info, E, D: BaseDecl, /*Virtual*/ true);
3244 Obj.getLValueOffset() += Layout.getVBaseClassOffset(VBase: BaseDecl);
3245 return true;
3246}
3247
3248static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E,
3249 QualType Type, LValue &Result) {
3250 for (CastExpr::path_const_iterator PathI = E->path_begin(),
3251 PathE = E->path_end();
3252 PathI != PathE; ++PathI) {
3253 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Type->getAsCXXRecordDecl(),
3254 Base: *PathI))
3255 return false;
3256 Type = (*PathI)->getType();
3257 }
3258 return true;
3259}
3260
3261/// Cast an lvalue referring to a derived class to a known base subobject.
3262static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result,
3263 const CXXRecordDecl *DerivedRD,
3264 const CXXRecordDecl *BaseRD) {
3265 CXXBasePaths Paths(/*FindAmbiguities=*/false,
3266 /*RecordPaths=*/true, /*DetectVirtual=*/false);
3267 if (!DerivedRD->isDerivedFrom(Base: BaseRD, Paths))
3268 llvm_unreachable("Class must be derived from the passed in base class!");
3269
3270 for (CXXBasePathElement &Elem : Paths.front())
3271 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Elem.Class, Base: Elem.Base))
3272 return false;
3273 return true;
3274}
3275
3276/// Update LVal to refer to the given field, which must be a member of the type
3277/// currently described by LVal.
3278static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
3279 const FieldDecl *FD,
3280 const ASTRecordLayout *RL = nullptr) {
3281 if (!RL) {
3282 const RecordDecl *RD = FD->getParent();
3283 if (RD->isInvalidDecl())
3284 return false;
3285 // There are some cases where the base is not yet complete but we haven't
3286 // disagnosed (such as in a template instantation of an attribute that
3287 // references the expression, ala enable_if). These aren't necessarily
3288 // constant expressions so we return 'false', but they might be, so we don't
3289 // diagnose.
3290 if (!RD->isCompleteDefinition())
3291 return false;
3292 RL = &Info.Ctx.getASTRecordLayout(D: RD);
3293 }
3294
3295 unsigned I = FD->getFieldIndex();
3296 LVal.addDecl(Info, E, D: FD);
3297 LVal.adjustOffset(N: Info.Ctx.toCharUnitsFromBits(BitSize: RL->getFieldOffset(FieldNo: I)));
3298 return true;
3299}
3300
3301/// Update LVal to refer to the given indirect field.
3302static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
3303 LValue &LVal,
3304 const IndirectFieldDecl *IFD) {
3305 for (const auto *C : IFD->chain())
3306 if (!HandleLValueMember(Info, E, LVal, FD: cast<FieldDecl>(Val: C)))
3307 return false;
3308 return true;
3309}
3310
3311enum class SizeOfType {
3312 SizeOf,
3313 DataSizeOf,
3314};
3315
3316/// Get the size of the given type in char units.
3317static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type,
3318 CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) {
3319 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
3320 // extension.
3321 if (Type->isVoidType() || Type->isFunctionType()) {
3322 Size = CharUnits::One();
3323 return true;
3324 }
3325
3326 if (Type->isDependentType()) {
3327 Info.FFDiag(Loc);
3328 return false;
3329 }
3330
3331 if (!Type->isConstantSizeType()) {
3332 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
3333 // FIXME: Better diagnostic.
3334 Info.FFDiag(Loc);
3335 return false;
3336 }
3337
3338 if (SOT == SizeOfType::SizeOf)
3339 Size = Info.Ctx.getTypeSizeInChars(T: Type);
3340 else
3341 Size = Info.Ctx.getTypeInfoDataSizeInChars(T: Type).Width;
3342 return true;
3343}
3344
3345/// Update a pointer value to model pointer arithmetic.
3346/// \param Info - Information about the ongoing evaluation.
3347/// \param E - The expression being evaluated, for diagnostic purposes.
3348/// \param LVal - The pointer value to be updated.
3349/// \param EltTy - The pointee type represented by LVal.
3350/// \param Adjustment - The adjustment, in objects of type EltTy, to add.
3351static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3352 LValue &LVal, QualType EltTy,
3353 APSInt Adjustment) {
3354 CharUnits SizeOfPointee;
3355 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfPointee))
3356 return false;
3357
3358 LVal.adjustOffsetAndIndex(Info, E, Index: Adjustment, ElementSize: SizeOfPointee);
3359 return true;
3360}
3361
3362static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3363 LValue &LVal, QualType EltTy,
3364 int64_t Adjustment) {
3365 return HandleLValueArrayAdjustment(Info, E, LVal, EltTy,
3366 Adjustment: APSInt::get(X: Adjustment));
3367}
3368
3369/// Update an lvalue to refer to a component of a complex number.
3370/// \param Info - Information about the ongoing evaluation.
3371/// \param LVal - The lvalue to be updated.
3372/// \param EltTy - The complex number's component type.
3373/// \param Imag - False for the real component, true for the imaginary.
3374static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E,
3375 LValue &LVal, QualType EltTy,
3376 bool Imag) {
3377 if (Imag) {
3378 CharUnits SizeOfComponent;
3379 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfComponent))
3380 return false;
3381 LVal.Offset += SizeOfComponent;
3382 }
3383 LVal.addComplex(Info, E, EltTy, Imag);
3384 return true;
3385}
3386
3387static bool HandleLValueVectorElement(EvalInfo &Info, const Expr *E,
3388 LValue &LVal, QualType EltTy,
3389 uint64_t Size, uint64_t Idx) {
3390 if (Idx) {
3391 CharUnits SizeOfElement;
3392 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfElement))
3393 return false;
3394 LVal.Offset += SizeOfElement * Idx;
3395 }
3396 LVal.addVectorElement(Info, E, EltTy, Size, Idx);
3397 return true;
3398}
3399
3400/// Try to evaluate the initializer for a variable declaration.
3401///
3402/// \param Info Information about the ongoing evaluation.
3403/// \param E An expression to be used when printing diagnostics.
3404/// \param VD The variable whose initializer should be obtained.
3405/// \param Version The version of the variable within the frame.
3406/// \param Frame The frame in which the variable was created. Must be null
3407/// if this variable is not local to the evaluation.
3408/// \param Result Filled in with a pointer to the value of the variable.
3409static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
3410 const VarDecl *VD, CallStackFrame *Frame,
3411 unsigned Version, APValue *&Result) {
3412 // C++23 [expr.const]p8 If we have a reference type allow unknown references
3413 // and pointers.
3414 bool AllowConstexprUnknown =
3415 Info.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType();
3416
3417 APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version);
3418
3419 auto CheckUninitReference = [&](bool IsLocalVariable) {
3420 if (!Result || (!Result->hasValue() && VD->getType()->isReferenceType())) {
3421 // C++23 [expr.const]p8
3422 // ... For such an object that is not usable in constant expressions, the
3423 // dynamic type of the object is constexpr-unknown. For such a reference
3424 // that is not usable in constant expressions, the reference is treated
3425 // as binding to an unspecified object of the referenced type whose
3426 // lifetime and that of all subobjects includes the entire constant
3427 // evaluation and whose dynamic type is constexpr-unknown.
3428 //
3429 // Variables that are part of the current evaluation are not
3430 // constexpr-unknown.
3431 if (!AllowConstexprUnknown || IsLocalVariable) {
3432 if (!Info.checkingPotentialConstantExpression())
3433 Info.FFDiag(E, DiagId: diag::note_constexpr_use_uninit_reference);
3434 return false;
3435 }
3436 Result = nullptr;
3437 }
3438 return true;
3439 };
3440
3441 // If this is a local variable, dig out its value.
3442 if (Frame) {
3443 Result = Frame->getTemporary(Key: VD, Version);
3444 if (Result)
3445 return CheckUninitReference(/*IsLocalVariable=*/true);
3446
3447 if (!isa<ParmVarDecl>(Val: VD)) {
3448 // Assume variables referenced within a lambda's call operator that were
3449 // not declared within the call operator are captures and during checking
3450 // of a potential constant expression, assume they are unknown constant
3451 // expressions.
3452 assert(isLambdaCallOperator(Frame->Callee) &&
3453 (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) &&
3454 "missing value for local variable");
3455 if (Info.checkingPotentialConstantExpression())
3456 return false;
3457
3458 llvm_unreachable(
3459 "A variable in a frame should either be a local or a parameter");
3460 }
3461 }
3462
3463 // If we're currently evaluating the initializer of this declaration, use that
3464 // in-flight value.
3465 if (Info.EvaluatingDecl == Base) {
3466 Result = Info.EvaluatingDeclValue;
3467 return CheckUninitReference(/*IsLocalVariable=*/false);
3468 }
3469
3470 // P2280R4 struck the restriction that variable of reference type lifetime
3471 // should begin within the evaluation of E
3472 // Used to be C++20 [expr.const]p5.12.2:
3473 // ... its lifetime began within the evaluation of E;
3474 if (isa<ParmVarDecl>(Val: VD)) {
3475 if (AllowConstexprUnknown) {
3476 Result = nullptr;
3477 return true;
3478 }
3479
3480 // Assume parameters of a potential constant expression are usable in
3481 // constant expressions.
3482 if (!Info.checkingPotentialConstantExpression() ||
3483 !Info.CurrentCall->Callee ||
3484 !Info.CurrentCall->Callee->Equals(DC: VD->getDeclContext())) {
3485 if (Info.getLangOpts().CPlusPlus11) {
3486 Info.FFDiag(E, DiagId: diag::note_constexpr_function_param_value_unknown)
3487 << VD;
3488 NoteLValueLocation(Info, Base);
3489 } else {
3490 Info.FFDiag(E);
3491 }
3492 }
3493 return false;
3494 }
3495
3496 if (E->isValueDependent())
3497 return false;
3498
3499 // Dig out the initializer, and use the declaration which it's attached to.
3500 // FIXME: We should eventually check whether the variable has a reachable
3501 // initializing declaration.
3502 const Expr *Init = VD->getAnyInitializer(D&: VD);
3503 // P2280R4 struck the restriction that variable of reference type should have
3504 // a preceding initialization.
3505 // Used to be C++20 [expr.const]p5.12:
3506 // ... reference has a preceding initialization and either ...
3507 if (!Init && !AllowConstexprUnknown) {
3508 // Don't diagnose during potential constant expression checking; an
3509 // initializer might be added later.
3510 if (!Info.checkingPotentialConstantExpression()) {
3511 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_unknown, ExtraNotes: 1)
3512 << VD;
3513 NoteLValueLocation(Info, Base);
3514 }
3515 return false;
3516 }
3517
3518 // P2280R4 struck the initialization requirement for variables of reference
3519 // type so we can no longer assume we have an Init.
3520 // Used to be C++20 [expr.const]p5.12:
3521 // ... reference has a preceding initialization and either ...
3522 if (Init && Init->isValueDependent()) {
3523 // The DeclRefExpr is not value-dependent, but the variable it refers to
3524 // has a value-dependent initializer. This should only happen in
3525 // constant-folding cases, where the variable is not actually of a suitable
3526 // type for use in a constant expression (otherwise the DeclRefExpr would
3527 // have been value-dependent too), so diagnose that.
3528 assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx));
3529 if (!Info.checkingPotentialConstantExpression()) {
3530 Info.FFDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
3531 ? diag::note_constexpr_ltor_non_constexpr
3532 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
3533 << VD << VD->getType();
3534 NoteLValueLocation(Info, Base);
3535 }
3536 return false;
3537 }
3538
3539 // Check that we can fold the initializer. In C++, we will have already done
3540 // this in the cases where it matters for conformance.
3541 // P2280R4 struck the initialization requirement for variables of reference
3542 // type so we can no longer assume we have an Init.
3543 // Used to be C++20 [expr.const]p5.12:
3544 // ... reference has a preceding initialization and either ...
3545 if (Init && !VD->evaluateValue() && !AllowConstexprUnknown) {
3546 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3547 NoteLValueLocation(Info, Base);
3548 return false;
3549 }
3550
3551 // Check that the variable is actually usable in constant expressions. For a
3552 // const integral variable or a reference, we might have a non-constant
3553 // initializer that we can nonetheless evaluate the initializer for. Such
3554 // variables are not usable in constant expressions. In C++98, the
3555 // initializer also syntactically needs to be an ICE.
3556 //
3557 // FIXME: We don't diagnose cases that aren't potentially usable in constant
3558 // expressions here; doing so would regress diagnostics for things like
3559 // reading from a volatile constexpr variable.
3560 if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() &&
3561 VD->mightBeUsableInConstantExpressions(C: Info.Ctx) &&
3562 !AllowConstexprUnknown) ||
3563 ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) &&
3564 !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Context: Info.Ctx))) {
3565 if (Init) {
3566 Info.CCEDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3567 NoteLValueLocation(Info, Base);
3568 } else {
3569 Info.CCEDiag(E);
3570 }
3571 }
3572
3573 // Never use the initializer of a weak variable, not even for constant
3574 // folding. We can't be sure that this is the definition that will be used.
3575 if (VD->isWeak()) {
3576 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_weak) << VD;
3577 NoteLValueLocation(Info, Base);
3578 return false;
3579 }
3580
3581 Result = const_cast<APValue *>(VD->getEvaluatedValue());
3582
3583 if (!Result && !AllowConstexprUnknown)
3584 return false;
3585
3586 return CheckUninitReference(/*IsLocalVariable=*/false);
3587}
3588
3589/// Get the base index of the given base class within an APValue representing
3590/// the given derived class.
3591static unsigned getBaseIndex(const CXXRecordDecl *Derived,
3592 const CXXRecordDecl *Base) {
3593 Base = Base->getCanonicalDecl();
3594 unsigned Index = 0;
3595 for (const CXXBaseSpecifier &B : Derived->bases()) {
3596 if (B.isVirtual())
3597 continue;
3598 if (B.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3599 return Index;
3600 ++Index;
3601 }
3602
3603 for (const CXXBaseSpecifier &B : Derived->vbases()) {
3604 if (B.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3605 return Index;
3606 ++Index;
3607 }
3608
3609 llvm_unreachable("base class missing from derived class's bases list");
3610}
3611
3612/// Extract the value of a character from a string literal.
3613static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
3614 uint64_t Index) {
3615 assert(!isa<SourceLocExpr>(Lit) &&
3616 "SourceLocExpr should have already been converted to a StringLiteral");
3617
3618 // FIXME: Support MakeStringConstant
3619 if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Val: Lit)) {
3620 std::string Str;
3621 Info.Ctx.getObjCEncodingForType(T: ObjCEnc->getEncodedType(), S&: Str);
3622 assert(Index <= Str.size() && "Index too large");
3623 return APSInt::getUnsigned(X: Str.c_str()[Index]);
3624 }
3625
3626 if (auto PE = dyn_cast<PredefinedExpr>(Val: Lit))
3627 Lit = PE->getFunctionName();
3628 const StringLiteral *S = cast<StringLiteral>(Val: Lit);
3629 const ConstantArrayType *CAT =
3630 Info.Ctx.getAsConstantArrayType(T: S->getType());
3631 assert(CAT && "string literal isn't an array");
3632 QualType CharType = CAT->getElementType();
3633 assert(CharType->isIntegerType() && "unexpected character type");
3634 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3635 CharType->isUnsignedIntegerType());
3636 if (Index < S->getLength())
3637 Value = S->getCodeUnit(I: Index);
3638 return Value;
3639}
3640
3641// Expand a string literal into an array of characters.
3642//
3643// FIXME: This is inefficient; we should probably introduce something similar
3644// to the LLVM ConstantDataArray to make this cheaper.
3645static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S,
3646 APValue &Result,
3647 QualType AllocType = QualType()) {
3648 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
3649 T: AllocType.isNull() ? S->getType() : AllocType);
3650 assert(CAT && "string literal isn't an array");
3651 QualType CharType = CAT->getElementType();
3652 assert(CharType->isIntegerType() && "unexpected character type");
3653
3654 unsigned Elts = CAT->getZExtSize();
3655 Result = APValue(APValue::UninitArray(),
3656 std::min(a: S->getLength(), b: Elts), Elts);
3657 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3658 CharType->isUnsignedIntegerType());
3659 if (Result.hasArrayFiller())
3660 Result.getArrayFiller() = APValue(Value);
3661 for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) {
3662 Value = S->getCodeUnit(I);
3663 Result.getArrayInitializedElt(I) = APValue(Value);
3664 }
3665}
3666
3667// Expand an array so that it has more than Index filled elements.
3668static void expandArray(APValue &Array, unsigned Index) {
3669 unsigned Size = Array.getArraySize();
3670 assert(Index < Size);
3671
3672 // Always at least double the number of elements for which we store a value.
3673 unsigned OldElts = Array.getArrayInitializedElts();
3674 unsigned NewElts = std::max(a: Index+1, b: OldElts * 2);
3675 NewElts = std::min(a: Size, b: std::max(a: NewElts, b: 8u));
3676
3677 // Copy the data across.
3678 APValue NewValue(APValue::UninitArray(), NewElts, Size);
3679 for (unsigned I = 0; I != OldElts; ++I)
3680 NewValue.getArrayInitializedElt(I).swap(RHS&: Array.getArrayInitializedElt(I));
3681 for (unsigned I = OldElts; I != NewElts; ++I)
3682 NewValue.getArrayInitializedElt(I) = Array.getArrayFiller();
3683 if (NewValue.hasArrayFiller())
3684 NewValue.getArrayFiller() = Array.getArrayFiller();
3685 Array.swap(RHS&: NewValue);
3686}
3687
3688// Expand an indeterminate vector to materialize all elements.
3689static void expandVector(APValue &Vec, unsigned NumElements) {
3690 assert(Vec.isIndeterminate());
3691 SmallVector<APValue, 4> Elts(NumElements, APValue::IndeterminateValue());
3692 Vec = APValue(Elts.data(), Elts.size());
3693}
3694
3695/// Determine whether a type would actually be read by an lvalue-to-rvalue
3696/// conversion. If it's of class type, we may assume that the copy operation
3697/// is trivial. Note that this is never true for a union type with fields
3698/// (because the copy always "reads" the active member) and always true for
3699/// a non-class type.
3700bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD);
3701bool isReadByLvalueToRvalueConversion(QualType T) {
3702 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3703 return !RD || isReadByLvalueToRvalueConversion(RD);
3704}
3705bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) {
3706 // FIXME: A trivial copy of a union copies the object representation, even if
3707 // the union is empty.
3708 if (RD->isUnion())
3709 return !RD->field_empty();
3710 if (RD->isEmpty())
3711 return false;
3712
3713 for (auto *Field : RD->fields())
3714 if (!Field->isUnnamedBitField() &&
3715 isReadByLvalueToRvalueConversion(T: Field->getType()))
3716 return true;
3717
3718 for (auto &BaseSpec : RD->bases())
3719 if (isReadByLvalueToRvalueConversion(T: BaseSpec.getType()))
3720 return true;
3721
3722 return false;
3723}
3724
3725/// Diagnose an attempt to read from any unreadable field within the specified
3726/// type, which might be a class type.
3727static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK,
3728 QualType T) {
3729 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3730 if (!RD)
3731 return false;
3732
3733 if (!RD->hasMutableFields())
3734 return false;
3735
3736 for (auto *Field : RD->fields()) {
3737 // If we're actually going to read this field in some way, then it can't
3738 // be mutable. If we're in a union, then assigning to a mutable field
3739 // (even an empty one) can change the active member, so that's not OK.
3740 // FIXME: Add core issue number for the union case.
3741 if (Field->isMutable() &&
3742 (RD->isUnion() || isReadByLvalueToRvalueConversion(T: Field->getType()))) {
3743 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1) << AK << Field;
3744 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
3745 return true;
3746 }
3747
3748 if (diagnoseMutableFields(Info, E, AK, T: Field->getType()))
3749 return true;
3750 }
3751
3752 for (auto &BaseSpec : RD->bases())
3753 if (diagnoseMutableFields(Info, E, AK, T: BaseSpec.getType()))
3754 return true;
3755
3756 // All mutable fields were empty, and thus not actually read.
3757 return false;
3758}
3759
3760static bool lifetimeStartedInEvaluation(EvalInfo &Info,
3761 APValue::LValueBase Base,
3762 bool MutableSubobject = false) {
3763 // A temporary or transient heap allocation we created.
3764 if (Base.getCallIndex() || Base.is<DynamicAllocLValue>())
3765 return true;
3766
3767 switch (Info.IsEvaluatingDecl) {
3768 case EvalInfo::EvaluatingDeclKind::None:
3769 return false;
3770
3771 case EvalInfo::EvaluatingDeclKind::Ctor:
3772 // The variable whose initializer we're evaluating.
3773 if (Info.EvaluatingDecl == Base)
3774 return true;
3775
3776 // A temporary lifetime-extended by the variable whose initializer we're
3777 // evaluating.
3778 if (auto *BaseE = Base.dyn_cast<const Expr *>())
3779 if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(Val: BaseE))
3780 return Info.EvaluatingDecl == BaseMTE->getExtendingDecl();
3781 return false;
3782
3783 case EvalInfo::EvaluatingDeclKind::Dtor:
3784 // C++2a [expr.const]p6:
3785 // [during constant destruction] the lifetime of a and its non-mutable
3786 // subobjects (but not its mutable subobjects) [are] considered to start
3787 // within e.
3788 if (MutableSubobject || Base != Info.EvaluatingDecl)
3789 return false;
3790 // FIXME: We can meaningfully extend this to cover non-const objects, but
3791 // we will need special handling: we should be able to access only
3792 // subobjects of such objects that are themselves declared const.
3793 QualType T = getType(B: Base);
3794 return T.isConstQualified() || T->isReferenceType();
3795 }
3796
3797 llvm_unreachable("unknown evaluating decl kind");
3798}
3799
3800static bool CheckArraySize(EvalInfo &Info, const ConstantArrayType *CAT,
3801 SourceLocation CallLoc = {}) {
3802 return Info.CheckArraySize(
3803 Loc: CAT->getSizeExpr() ? CAT->getSizeExpr()->getBeginLoc() : CallLoc,
3804 BitWidth: CAT->getNumAddressingBits(Context: Info.Ctx), ElemCount: CAT->getZExtSize(),
3805 /*Diag=*/true);
3806}
3807
3808static bool handleScalarCast(EvalInfo &Info, const FPOptions FPO, const Expr *E,
3809 QualType SourceTy, QualType DestTy,
3810 APValue const &Original, APValue &Result) {
3811 // boolean must be checked before integer
3812 // since IsIntegerType() is true for bool
3813 if (SourceTy->isBooleanType()) {
3814 if (DestTy->isBooleanType()) {
3815 Result = Original;
3816 return true;
3817 }
3818 if (DestTy->isIntegerType() || DestTy->isRealFloatingType()) {
3819 bool BoolResult;
3820 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3821 return false;
3822 uint64_t IntResult = BoolResult;
3823 QualType IntType = DestTy->isIntegerType()
3824 ? DestTy
3825 : Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false);
3826 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: IntType));
3827 }
3828 if (DestTy->isRealFloatingType()) {
3829 APValue Result2 = APValue(APFloat(0.0));
3830 if (!HandleIntToFloatCast(Info, E, FPO,
3831 SrcType: Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false),
3832 Value: Result.getInt(), DestType: DestTy, Result&: Result2.getFloat()))
3833 return false;
3834 Result = std::move(Result2);
3835 }
3836 return true;
3837 }
3838 if (SourceTy->isIntegerType()) {
3839 if (DestTy->isRealFloatingType()) {
3840 Result = APValue(APFloat(0.0));
3841 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
3842 DestType: DestTy, Result&: Result.getFloat());
3843 }
3844 if (DestTy->isBooleanType()) {
3845 bool BoolResult;
3846 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3847 return false;
3848 uint64_t IntResult = BoolResult;
3849 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3850 return true;
3851 }
3852 if (DestTy->isIntegerType()) {
3853 Result = APValue(
3854 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
3855 return true;
3856 }
3857 } else if (SourceTy->isRealFloatingType()) {
3858 if (DestTy->isRealFloatingType()) {
3859 Result = Original;
3860 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
3861 Result&: Result.getFloat());
3862 }
3863 if (DestTy->isBooleanType()) {
3864 bool BoolResult;
3865 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3866 return false;
3867 uint64_t IntResult = BoolResult;
3868 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3869 return true;
3870 }
3871 if (DestTy->isIntegerType()) {
3872 Result = APValue(APSInt());
3873 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
3874 DestType: DestTy, Result&: Result.getInt());
3875 }
3876 }
3877
3878 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
3879 return false;
3880}
3881
3882// do the heavy lifting for casting to aggregate types
3883// because we have to deal with bitfields specially
3884static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,
3885 const Expr *E, APValue &Result,
3886 QualType ResultType,
3887 SmallVectorImpl<APValue> &Elements,
3888 SmallVectorImpl<QualType> &ElTypes) {
3889
3890 SmallVector<std::tuple<APValue *, QualType, unsigned>> WorkList = {
3891 {&Result, ResultType, 0}};
3892
3893 unsigned ElI = 0;
3894 while (!WorkList.empty() && ElI < Elements.size()) {
3895 auto [Res, Type, BitWidth] = WorkList.pop_back_val();
3896
3897 if (Type->isRealFloatingType()) {
3898 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3899 Result&: *Res))
3900 return false;
3901 ElI++;
3902 continue;
3903 }
3904 if (Type->isIntegerType()) {
3905 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3906 Result&: *Res))
3907 return false;
3908 if (BitWidth > 0) {
3909 if (!Res->isInt())
3910 return false;
3911 APSInt &Int = Res->getInt();
3912 unsigned OldBitWidth = Int.getBitWidth();
3913 unsigned NewBitWidth = BitWidth;
3914 if (NewBitWidth < OldBitWidth)
3915 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
3916 }
3917 ElI++;
3918 continue;
3919 }
3920 if (Type->isVectorType()) {
3921 QualType ElTy = Type->castAs<VectorType>()->getElementType();
3922 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
3923 SmallVector<APValue> Vals(NumEl);
3924 for (unsigned I = 0; I < NumEl; ++I) {
3925 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: ElTy, Original: Elements[ElI],
3926 Result&: Vals[I]))
3927 return false;
3928 ElI++;
3929 }
3930 *Res = APValue(Vals.data(), NumEl);
3931 continue;
3932 }
3933 if (Type->isConstantArrayType()) {
3934 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
3935 ->getElementType();
3936 uint64_t Size =
3937 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
3938 *Res = APValue(APValue::UninitArray(), Size, Size);
3939 for (int64_t I = Size - 1; I > -1; --I)
3940 WorkList.emplace_back(Args: &Res->getArrayInitializedElt(I), Args&: ElTy, Args: 0u);
3941 continue;
3942 }
3943 if (Type->isRecordType()) {
3944 const RecordDecl *RD = Type->getAsRecordDecl();
3945
3946 unsigned NumBases = 0;
3947 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
3948 NumBases = CXXRD->getNumBases();
3949
3950 *Res = APValue(APValue::UninitStruct(), NumBases, RD->getNumFields());
3951
3952 SmallVector<std::tuple<APValue *, QualType, unsigned>> ReverseList;
3953 // we need to traverse backwards
3954 // Visit the base classes.
3955 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
3956 if (CXXRD->getNumBases() > 0) {
3957 assert(CXXRD->getNumBases() == 1);
3958 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
3959 ReverseList.emplace_back(Args: &Res->getStructBase(i: 0), Args: BS.getType(), Args: 0u);
3960 }
3961 }
3962
3963 // Visit the fields.
3964 for (FieldDecl *FD : RD->fields()) {
3965 unsigned FDBW = 0;
3966 if (FD->isUnnamedBitField())
3967 continue;
3968 if (FD->isBitField()) {
3969 FDBW = FD->getBitWidthValue();
3970 }
3971
3972 ReverseList.emplace_back(Args: &Res->getStructField(i: FD->getFieldIndex()),
3973 Args: FD->getType(), Args&: FDBW);
3974 }
3975
3976 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
3977 llvm::append_range(C&: WorkList, R&: ReverseList);
3978 continue;
3979 }
3980 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
3981 return false;
3982 }
3983 return true;
3984}
3985
3986static bool handleElementwiseCast(EvalInfo &Info, const Expr *E,
3987 const FPOptions FPO,
3988 SmallVectorImpl<APValue> &Elements,
3989 SmallVectorImpl<QualType> &SrcTypes,
3990 SmallVectorImpl<QualType> &DestTypes,
3991 SmallVectorImpl<APValue> &Results) {
3992
3993 assert((Elements.size() == SrcTypes.size()) &&
3994 (Elements.size() == DestTypes.size()));
3995
3996 for (unsigned I = 0, ESz = Elements.size(); I < ESz; ++I) {
3997 APValue Original = Elements[I];
3998 QualType SourceTy = SrcTypes[I];
3999 QualType DestTy = DestTypes[I];
4000
4001 if (!handleScalarCast(Info, FPO, E, SourceTy, DestTy, Original, Result&: Results[I]))
4002 return false;
4003 }
4004 return true;
4005}
4006
4007static unsigned elementwiseSize(EvalInfo &Info, QualType BaseTy) {
4008
4009 SmallVector<QualType> WorkList = {BaseTy};
4010
4011 unsigned Size = 0;
4012 while (!WorkList.empty()) {
4013 QualType Type = WorkList.pop_back_val();
4014 if (Type->isRealFloatingType() || Type->isIntegerType() ||
4015 Type->isBooleanType()) {
4016 ++Size;
4017 continue;
4018 }
4019 if (Type->isVectorType()) {
4020 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
4021 Size += NumEl;
4022 continue;
4023 }
4024 if (Type->isConstantMatrixType()) {
4025 unsigned NumEl =
4026 Type->castAs<ConstantMatrixType>()->getNumElementsFlattened();
4027 Size += NumEl;
4028 continue;
4029 }
4030 if (Type->isConstantArrayType()) {
4031 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4032 ->getElementType();
4033 uint64_t ArrSize =
4034 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
4035 for (uint64_t I = 0; I < ArrSize; ++I) {
4036 WorkList.push_back(Elt: ElTy);
4037 }
4038 continue;
4039 }
4040 if (Type->isRecordType()) {
4041 const RecordDecl *RD = Type->getAsRecordDecl();
4042
4043 // Visit the base classes.
4044 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4045 if (CXXRD->getNumBases() > 0) {
4046 assert(CXXRD->getNumBases() == 1);
4047 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4048 WorkList.push_back(Elt: BS.getType());
4049 }
4050 }
4051
4052 // visit the fields.
4053 for (FieldDecl *FD : RD->fields()) {
4054 if (FD->isUnnamedBitField())
4055 continue;
4056 WorkList.push_back(Elt: FD->getType());
4057 }
4058 continue;
4059 }
4060 }
4061 return Size;
4062}
4063
4064static bool hlslAggSplatHelper(EvalInfo &Info, const Expr *E, APValue &SrcVal,
4065 QualType &SrcTy) {
4066 SrcTy = E->getType();
4067
4068 if (!Evaluate(Result&: SrcVal, Info, E))
4069 return false;
4070
4071 assert((SrcVal.isFloat() || SrcVal.isInt() ||
4072 (SrcVal.isVector() && SrcVal.getVectorLength() == 1)) &&
4073 "Not a valid HLSLAggregateSplatCast.");
4074
4075 if (SrcVal.isVector()) {
4076 assert(SrcTy->isVectorType() && "Type mismatch.");
4077 SrcTy = SrcTy->castAs<VectorType>()->getElementType();
4078 SrcVal = SrcVal.getVectorElt(I: 0);
4079 }
4080 if (SrcVal.isMatrix()) {
4081 assert(SrcTy->isConstantMatrixType() && "Type mismatch.");
4082 SrcTy = SrcTy->castAs<ConstantMatrixType>()->getElementType();
4083 SrcVal = SrcVal.getMatrixElt(Row: 0, Col: 0);
4084 }
4085 return true;
4086}
4087
4088static bool flattenAPValue(EvalInfo &Info, const Expr *E, APValue Value,
4089 QualType BaseTy, SmallVectorImpl<APValue> &Elements,
4090 SmallVectorImpl<QualType> &Types, unsigned Size) {
4091
4092 SmallVector<std::pair<APValue, QualType>> WorkList = {{Value, BaseTy}};
4093 unsigned Populated = 0;
4094 while (!WorkList.empty() && Populated < Size) {
4095 auto [Work, Type] = WorkList.pop_back_val();
4096
4097 if (Work.isFloat() || Work.isInt()) {
4098 Elements.push_back(Elt: Work);
4099 Types.push_back(Elt: Type);
4100 Populated++;
4101 continue;
4102 }
4103 if (Work.isVector()) {
4104 assert(Type->isVectorType() && "Type mismatch.");
4105 QualType ElTy = Type->castAs<VectorType>()->getElementType();
4106 for (unsigned I = 0; I < Work.getVectorLength() && Populated < Size;
4107 I++) {
4108 Elements.push_back(Elt: Work.getVectorElt(I));
4109 Types.push_back(Elt: ElTy);
4110 Populated++;
4111 }
4112 continue;
4113 }
4114 if (Work.isMatrix()) {
4115 assert(Type->isConstantMatrixType() && "Type mismatch.");
4116 const auto *MT = Type->castAs<ConstantMatrixType>();
4117 QualType ElTy = MT->getElementType();
4118 // Matrix elements are flattened in row-major order.
4119 for (unsigned Row = 0; Row < Work.getMatrixNumRows() && Populated < Size;
4120 Row++) {
4121 for (unsigned Col = 0;
4122 Col < Work.getMatrixNumColumns() && Populated < Size; Col++) {
4123 Elements.push_back(Elt: Work.getMatrixElt(Row, Col));
4124 Types.push_back(Elt: ElTy);
4125 Populated++;
4126 }
4127 }
4128 continue;
4129 }
4130 if (Work.isArray()) {
4131 assert(Type->isConstantArrayType() && "Type mismatch.");
4132 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4133 ->getElementType();
4134 for (int64_t I = Work.getArraySize() - 1; I > -1; --I) {
4135 WorkList.emplace_back(Args&: Work.getArrayInitializedElt(I), Args&: ElTy);
4136 }
4137 continue;
4138 }
4139
4140 if (Work.isStruct()) {
4141 assert(Type->isRecordType() && "Type mismatch.");
4142
4143 const RecordDecl *RD = Type->getAsRecordDecl();
4144
4145 SmallVector<std::pair<APValue, QualType>> ReverseList;
4146 // Visit the fields.
4147 for (FieldDecl *FD : RD->fields()) {
4148 if (FD->isUnnamedBitField())
4149 continue;
4150 ReverseList.emplace_back(Args&: Work.getStructField(i: FD->getFieldIndex()),
4151 Args: FD->getType());
4152 }
4153
4154 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
4155 llvm::append_range(C&: WorkList, R&: ReverseList);
4156
4157 // Visit the base classes.
4158 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4159 if (CXXRD->getNumBases() > 0) {
4160 assert(CXXRD->getNumBases() == 1);
4161 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4162 const APValue &Base = Work.getStructBase(i: 0);
4163
4164 // Can happen in error cases.
4165 if (!Base.isStruct())
4166 return false;
4167
4168 WorkList.emplace_back(Args: Base, Args: BS.getType());
4169 }
4170 }
4171 continue;
4172 }
4173 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4174 return false;
4175 }
4176 return true;
4177}
4178
4179namespace {
4180/// A handle to a complete object (an object that is not a subobject of
4181/// another object).
4182struct CompleteObject {
4183 /// The identity of the object.
4184 APValue::LValueBase Base;
4185 /// The value of the complete object.
4186 APValue *Value;
4187 /// The type of the complete object.
4188 QualType Type;
4189
4190 CompleteObject() : Value(nullptr) {}
4191 CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type)
4192 : Base(Base), Value(Value), Type(Type) {}
4193
4194 bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const {
4195 // If this isn't a "real" access (eg, if it's just accessing the type
4196 // info), allow it. We assume the type doesn't change dynamically for
4197 // subobjects of constexpr objects (even though we'd hit UB here if it
4198 // did). FIXME: Is this right?
4199 if (!isAnyAccess(AK))
4200 return true;
4201
4202 // In C++14 onwards, it is permitted to read a mutable member whose
4203 // lifetime began within the evaluation.
4204 // FIXME: Should we also allow this in C++11?
4205 if (!Info.getLangOpts().CPlusPlus14 &&
4206 AK != AccessKinds::AK_IsWithinLifetime)
4207 return false;
4208 return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true);
4209 }
4210
4211 explicit operator bool() const { return !Type.isNull(); }
4212};
4213} // end anonymous namespace
4214
4215static QualType getSubobjectType(QualType ObjType, QualType SubobjType,
4216 bool IsMutable = false) {
4217 // C++ [basic.type.qualifier]p1:
4218 // - A const object is an object of type const T or a non-mutable subobject
4219 // of a const object.
4220 if (ObjType.isConstQualified() && !IsMutable)
4221 SubobjType.addConst();
4222 // - A volatile object is an object of type const T or a subobject of a
4223 // volatile object.
4224 if (ObjType.isVolatileQualified())
4225 SubobjType.addVolatile();
4226 return SubobjType;
4227}
4228
4229/// Find the designated sub-object of an rvalue.
4230template <typename SubobjectHandler>
4231static typename SubobjectHandler::result_type
4232findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
4233 const SubobjectDesignator &Sub, SubobjectHandler &handler) {
4234 if (Sub.Invalid)
4235 // A diagnostic will have already been produced.
4236 return handler.failed();
4237 if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) {
4238 if (Info.getLangOpts().CPlusPlus11)
4239 Info.FFDiag(E, DiagId: Sub.isOnePastTheEnd()
4240 ? diag::note_constexpr_access_past_end
4241 : diag::note_constexpr_access_unsized_array)
4242 << handler.AccessKind;
4243 else
4244 Info.FFDiag(E);
4245 return handler.failed();
4246 }
4247
4248 APValue *O = Obj.Value;
4249 QualType ObjType = Obj.Type;
4250 const FieldDecl *LastField = nullptr;
4251 const FieldDecl *VolatileField = nullptr;
4252
4253 // Walk the designator's path to find the subobject.
4254 for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) {
4255 // Reading an indeterminate value is undefined, but assigning over one is OK.
4256 if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) ||
4257 (O->isIndeterminate() &&
4258 !isValidIndeterminateAccess(handler.AccessKind))) {
4259 // Object has ended lifetime.
4260 // If I is non-zero, some subobject (member or array element) of a
4261 // complete object has ended its lifetime, so this is valid for
4262 // IsWithinLifetime, resulting in false.
4263 if (I != 0 && handler.AccessKind == AK_IsWithinLifetime)
4264 return false;
4265 if (!Info.checkingPotentialConstantExpression()) {
4266 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
4267 << handler.AccessKind << O->isIndeterminate()
4268 << E->getSourceRange();
4269 NoteLValueLocation(Info, Base: Obj.Base);
4270 }
4271 return handler.failed();
4272 }
4273
4274 // C++ [class.ctor]p5, C++ [class.dtor]p5:
4275 // const and volatile semantics are not applied on an object under
4276 // {con,de}struction.
4277 if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) &&
4278 ObjType->isRecordType() &&
4279 Info.isEvaluatingCtorDtor(
4280 Base: Obj.Base, Path: ArrayRef(Sub.Entries.begin(), Sub.Entries.begin() + I)) !=
4281 ConstructionPhase::None) {
4282 ObjType = Info.Ctx.getCanonicalType(T: ObjType);
4283 ObjType.removeLocalConst();
4284 ObjType.removeLocalVolatile();
4285 }
4286
4287 // If this is our last pass, check that the final object type is OK.
4288 if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) {
4289 // Accesses to volatile objects are prohibited.
4290 if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) {
4291 if (Info.getLangOpts().CPlusPlus) {
4292 int DiagKind;
4293 SourceLocation Loc;
4294 const NamedDecl *Decl = nullptr;
4295 if (VolatileField) {
4296 DiagKind = 2;
4297 Loc = VolatileField->getLocation();
4298 Decl = VolatileField;
4299 } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) {
4300 DiagKind = 1;
4301 Loc = VD->getLocation();
4302 Decl = VD;
4303 } else {
4304 DiagKind = 0;
4305 if (auto *E = Obj.Base.dyn_cast<const Expr *>())
4306 Loc = E->getExprLoc();
4307 }
4308 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_obj, ExtraNotes: 1)
4309 << handler.AccessKind << DiagKind << Decl;
4310 Info.Note(Loc, DiagId: diag::note_constexpr_volatile_here) << DiagKind;
4311 } else {
4312 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4313 }
4314 return handler.failed();
4315 }
4316
4317 // If we are reading an object of class type, there may still be more
4318 // things we need to check: if there are any mutable subobjects, we
4319 // cannot perform this read. (This only happens when performing a trivial
4320 // copy or assignment.)
4321 if (ObjType->isRecordType() &&
4322 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind) &&
4323 diagnoseMutableFields(Info, E, handler.AccessKind, ObjType))
4324 return handler.failed();
4325 }
4326
4327 if (I == N) {
4328 if (!handler.found(*O, ObjType, Obj.Base))
4329 return false;
4330
4331 // If we modified a bit-field, truncate it to the right width.
4332 if (isModification(handler.AccessKind) &&
4333 LastField && LastField->isBitField() &&
4334 !truncateBitfieldValue(Info, E, Value&: *O, FD: LastField))
4335 return false;
4336
4337 return true;
4338 }
4339
4340 LastField = nullptr;
4341
4342 // The value of an atomic object is represented like a value of the
4343 // underlying type, so look through the _Atomic wrapper.
4344 if (const AtomicType *AT = ObjType->getAs<AtomicType>())
4345 ObjType = Info.Ctx.getQualifiedType(T: AT->getValueType(),
4346 Qs: ObjType.getQualifiers());
4347
4348 if (ObjType->isArrayType()) {
4349 // Next subobject is an array element.
4350 const ArrayType *AT = Info.Ctx.getAsArrayType(T: ObjType);
4351 assert((isa<ConstantArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
4352 "vla in literal type?");
4353 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4354 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4355 CAT && CAT->getSize().ule(RHS: Index)) {
4356 // Note, it should not be possible to form a pointer with a valid
4357 // designator which points more than one past the end of the array.
4358 if (Info.getLangOpts().CPlusPlus11)
4359 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4360 << handler.AccessKind;
4361 else
4362 Info.FFDiag(E);
4363 return handler.failed();
4364 }
4365
4366 ObjType = AT->getElementType();
4367
4368 if (O->getArrayInitializedElts() > Index)
4369 O = &O->getArrayInitializedElt(I: Index);
4370 else if (!isRead(handler.AccessKind)) {
4371 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4372 CAT && !CheckArraySize(Info, CAT, CallLoc: E->getExprLoc()))
4373 return handler.failed();
4374
4375 expandArray(Array&: *O, Index);
4376 O = &O->getArrayInitializedElt(I: Index);
4377 } else
4378 O = &O->getArrayFiller();
4379 } else if (ObjType->isAnyComplexType()) {
4380 // Next subobject is a complex number.
4381 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4382 if (Index > 1) {
4383 if (Info.getLangOpts().CPlusPlus11)
4384 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4385 << handler.AccessKind;
4386 else
4387 Info.FFDiag(E);
4388 return handler.failed();
4389 }
4390
4391 ObjType = getSubobjectType(
4392 ObjType, SubobjType: ObjType->castAs<ComplexType>()->getElementType());
4393
4394 assert(I == N - 1 && "extracting subobject of scalar?");
4395 if (O->isComplexInt()) {
4396 return handler.found(Index ? O->getComplexIntImag()
4397 : O->getComplexIntReal(), ObjType);
4398 } else {
4399 assert(O->isComplexFloat());
4400 return handler.found(Index ? O->getComplexFloatImag()
4401 : O->getComplexFloatReal(), ObjType);
4402 }
4403 } else if (const auto *VT = ObjType->getAs<VectorType>()) {
4404 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4405 unsigned NumElements = VT->getNumElements();
4406 if (Index == NumElements) {
4407 if (Info.getLangOpts().CPlusPlus11)
4408 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4409 << handler.AccessKind;
4410 else
4411 Info.FFDiag(E);
4412 return handler.failed();
4413 }
4414
4415 if (Index > NumElements) {
4416 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
4417 << Index << /*array*/ 0 << NumElements;
4418 return handler.failed();
4419 }
4420
4421 ObjType = VT->getElementType();
4422 assert(I == N - 1 && "extracting subobject of scalar?");
4423
4424 if (O->isIndeterminate()) {
4425 if (isRead(handler.AccessKind)) {
4426 Info.FFDiag(E);
4427 return handler.failed();
4428 }
4429 expandVector(Vec&: *O, NumElements);
4430 }
4431 assert(O->isVector() && "unexpected object during vector element access");
4432 return handler.found(O->getVectorElt(I: Index), ObjType, Obj.Base);
4433 } else if (const FieldDecl *Field = getAsField(E: Sub.Entries[I])) {
4434 if (Field->isMutable() &&
4435 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind)) {
4436 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1)
4437 << handler.AccessKind << Field;
4438 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
4439 return handler.failed();
4440 }
4441
4442 // Next subobject is a class, struct or union field.
4443 RecordDecl *RD = ObjType->castAsCanonical<RecordType>()->getDecl();
4444 if (RD->isUnion()) {
4445 const FieldDecl *UnionField = O->getUnionField();
4446 if (!UnionField ||
4447 UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {
4448 if (I == N - 1 && handler.AccessKind == AK_Construct) {
4449 // Placement new onto an inactive union member makes it active.
4450 O->setUnion(Field, Value: APValue());
4451 } else {
4452 // Pointer to/into inactive union member: Not within lifetime
4453 if (handler.AccessKind == AK_IsWithinLifetime)
4454 return false;
4455 // FIXME: If O->getUnionValue() is absent, report that there's no
4456 // active union member rather than reporting the prior active union
4457 // member. We'll need to fix nullptr_t to not use APValue() as its
4458 // representation first.
4459 Info.FFDiag(E, DiagId: diag::note_constexpr_access_inactive_union_member)
4460 << handler.AccessKind << Field << !UnionField << UnionField;
4461 return handler.failed();
4462 }
4463 }
4464 O = &O->getUnionValue();
4465 } else
4466 O = &O->getStructField(i: Field->getFieldIndex());
4467
4468 ObjType = getSubobjectType(ObjType, SubobjType: Field->getType(), IsMutable: Field->isMutable());
4469 LastField = Field;
4470 if (Field->getType().isVolatileQualified())
4471 VolatileField = Field;
4472 } else {
4473 // Next subobject is a base class.
4474 const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();
4475 const CXXRecordDecl *Base = getAsBaseClass(E: Sub.Entries[I]);
4476
4477 unsigned BaseIndex = getBaseIndex(Derived, Base);
4478 unsigned NumNonVirtualBases = O->getStructNumBases();
4479 if (BaseIndex >= NumNonVirtualBases) {
4480 O = &O->getStructVirtualBase(i: BaseIndex - NumNonVirtualBases);
4481 } else
4482 O = &O->getStructBase(i: BaseIndex);
4483
4484 ObjType = getSubobjectType(ObjType, SubobjType: Info.Ctx.getCanonicalTagType(TD: Base));
4485 }
4486 }
4487}
4488
4489namespace {
4490struct ExtractSubobjectHandler {
4491 EvalInfo &Info;
4492 const Expr *E;
4493 APValue &Result;
4494 const AccessKinds AccessKind;
4495
4496 typedef bool result_type;
4497 bool failed() { return false; }
4498 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4499 Result = Subobj;
4500 if (AccessKind == AK_ReadObjectRepresentation)
4501 return true;
4502 return CheckFullyInitialized(Info, DiagLoc: E->getExprLoc(), Type: SubobjType, Value: Result);
4503 }
4504 bool found(APSInt &Value, QualType SubobjType) {
4505 Result = APValue(Value);
4506 return true;
4507 }
4508 bool found(APFloat &Value, QualType SubobjType) {
4509 Result = APValue(Value);
4510 return true;
4511 }
4512};
4513} // end anonymous namespace
4514
4515/// Extract the designated sub-object of an rvalue.
4516static bool extractSubobject(EvalInfo &Info, const Expr *E,
4517 const CompleteObject &Obj,
4518 const SubobjectDesignator &Sub, APValue &Result,
4519 AccessKinds AK = AK_Read) {
4520 assert(AK == AK_Read || AK == AK_ReadObjectRepresentation);
4521 ExtractSubobjectHandler Handler = {.Info: Info, .E: E, .Result: Result, .AccessKind: AK};
4522 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4523}
4524
4525namespace {
4526struct ModifySubobjectHandler {
4527 EvalInfo &Info;
4528 APValue &NewVal;
4529 const Expr *E;
4530
4531 typedef bool result_type;
4532 static const AccessKinds AccessKind = AK_Assign;
4533
4534 bool checkConst(QualType QT) {
4535 // Assigning to a const object has undefined behavior.
4536 if (QT.isConstQualified()) {
4537 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
4538 return false;
4539 }
4540 return true;
4541 }
4542
4543 bool failed() { return false; }
4544 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4545 if (!checkConst(QT: SubobjType))
4546 return false;
4547 // We've been given ownership of NewVal, so just swap it in.
4548 Subobj.swap(RHS&: NewVal);
4549 return true;
4550 }
4551 bool found(APSInt &Value, QualType SubobjType) {
4552 if (!checkConst(QT: SubobjType))
4553 return false;
4554 if (!NewVal.isInt()) {
4555 // Maybe trying to write a cast pointer value into a complex?
4556 Info.FFDiag(E);
4557 return false;
4558 }
4559 Value = NewVal.getInt();
4560 return true;
4561 }
4562 bool found(APFloat &Value, QualType SubobjType) {
4563 if (!checkConst(QT: SubobjType))
4564 return false;
4565 Value = NewVal.getFloat();
4566 return true;
4567 }
4568};
4569} // end anonymous namespace
4570
4571const AccessKinds ModifySubobjectHandler::AccessKind;
4572
4573/// Update the designated sub-object of an rvalue to the given value.
4574static bool modifySubobject(EvalInfo &Info, const Expr *E,
4575 const CompleteObject &Obj,
4576 const SubobjectDesignator &Sub,
4577 APValue &NewVal) {
4578 ModifySubobjectHandler Handler = { .Info: Info, .NewVal: NewVal, .E: E };
4579 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4580}
4581
4582/// Find the position where two subobject designators diverge, or equivalently
4583/// the length of the common initial subsequence.
4584static unsigned FindDesignatorMismatch(QualType ObjType,
4585 const SubobjectDesignator &A,
4586 const SubobjectDesignator &B,
4587 bool &WasArrayIndex) {
4588 unsigned I = 0, N = std::min(a: A.Entries.size(), b: B.Entries.size());
4589 for (/**/; I != N; ++I) {
4590 if (!ObjType.isNull() &&
4591 (ObjType->isArrayType() || ObjType->isAnyComplexType())) {
4592 // Next subobject is an array element.
4593 if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) {
4594 WasArrayIndex = true;
4595 return I;
4596 }
4597 if (ObjType->isAnyComplexType())
4598 ObjType = ObjType->castAs<ComplexType>()->getElementType();
4599 else
4600 ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();
4601 } else {
4602 if (A.Entries[I].getAsBaseOrMember() !=
4603 B.Entries[I].getAsBaseOrMember()) {
4604 WasArrayIndex = false;
4605 return I;
4606 }
4607 if (const FieldDecl *FD = getAsField(E: A.Entries[I]))
4608 // Next subobject is a field.
4609 ObjType = FD->getType();
4610 else
4611 // Next subobject is a base class.
4612 ObjType = QualType();
4613 }
4614 }
4615 WasArrayIndex = false;
4616 return I;
4617}
4618
4619/// Determine whether the given subobject designators refer to elements of the
4620/// same array object.
4621static bool AreElementsOfSameArray(QualType ObjType,
4622 const SubobjectDesignator &A,
4623 const SubobjectDesignator &B) {
4624 if (A.Entries.size() != B.Entries.size())
4625 return false;
4626
4627 bool IsArray = A.MostDerivedIsArrayElement;
4628 if (IsArray && A.MostDerivedPathLength != A.Entries.size())
4629 // A is a subobject of the array element.
4630 return false;
4631
4632 // If A (and B) designates an array element, the last entry will be the array
4633 // index. That doesn't have to match. Otherwise, we're in the 'implicit array
4634 // of length 1' case, and the entire path must match.
4635 bool WasArrayIndex;
4636 unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);
4637 return CommonLength >= A.Entries.size() - IsArray;
4638}
4639
4640/// Find the complete object to which an LValue refers.
4641static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
4642 AccessKinds AK, const LValue &LVal,
4643 QualType LValType) {
4644 if (LVal.InvalidBase) {
4645 Info.FFDiag(E);
4646 return CompleteObject();
4647 }
4648
4649 if (!LVal.Base) {
4650 if (AK == AccessKinds::AK_Dereference)
4651 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
4652 else
4653 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
4654 return CompleteObject();
4655 }
4656
4657 CallStackFrame *Frame = nullptr;
4658 unsigned Depth = 0;
4659 if (LVal.getLValueCallIndex()) {
4660 std::tie(args&: Frame, args&: Depth) =
4661 Info.getCallFrameAndDepth(CallIndex: LVal.getLValueCallIndex());
4662 if (!Frame) {
4663 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit, ExtraNotes: 1)
4664 << AK << /*Indeterminate=*/false << E->getSourceRange();
4665 NoteLValueLocation(Info, Base: LVal.Base);
4666 return CompleteObject();
4667 }
4668 }
4669
4670 bool IsAccess = isAnyAccess(AK);
4671
4672 // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type
4673 // is not a constant expression (even if the object is non-volatile). We also
4674 // apply this rule to C++98, in order to conform to the expected 'volatile'
4675 // semantics.
4676 if (isFormalAccess(AK) && LValType.isVolatileQualified()) {
4677 if (Info.getLangOpts().CPlusPlus)
4678 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_type)
4679 << AK << LValType;
4680 else
4681 Info.FFDiag(E);
4682 return CompleteObject();
4683 }
4684
4685 // Compute value storage location and type of base object.
4686 APValue *BaseVal = nullptr;
4687 QualType BaseType = getType(B: LVal.Base);
4688
4689 if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl &&
4690 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4691 // This is the object whose initializer we're evaluating, so its lifetime
4692 // started in the current evaluation.
4693 BaseVal = Info.EvaluatingDeclValue;
4694 } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) {
4695 // Allow reading from a GUID declaration.
4696 if (auto *GD = dyn_cast<MSGuidDecl>(Val: D)) {
4697 if (isModification(AK)) {
4698 // All the remaining cases do not permit modification of the object.
4699 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4700 return CompleteObject();
4701 }
4702 APValue &V = GD->getAsAPValue();
4703 if (V.isAbsent()) {
4704 Info.FFDiag(E, DiagId: diag::note_constexpr_unsupported_layout)
4705 << GD->getType();
4706 return CompleteObject();
4707 }
4708 return CompleteObject(LVal.Base, &V, GD->getType());
4709 }
4710
4711 // Allow reading the APValue from an UnnamedGlobalConstantDecl.
4712 if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(Val: D)) {
4713 if (isModification(AK)) {
4714 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4715 return CompleteObject();
4716 }
4717 return CompleteObject(LVal.Base, const_cast<APValue *>(&GCD->getValue()),
4718 GCD->getType());
4719 }
4720
4721 // Allow reading from template parameter objects.
4722 if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: D)) {
4723 if (isModification(AK)) {
4724 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4725 return CompleteObject();
4726 }
4727 return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()),
4728 TPO->getType());
4729 }
4730
4731 // In C++98, const, non-volatile integers initialized with ICEs are ICEs.
4732 // In C++11, constexpr, non-volatile variables initialized with constant
4733 // expressions are constant expressions too. Inside constexpr functions,
4734 // parameters are constant expressions even if they're non-const.
4735 // In C++1y, objects local to a constant expression (those with a Frame) are
4736 // both readable and writable inside constant expressions.
4737 // In C, such things can also be folded, although they are not ICEs.
4738 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
4739 if (VD) {
4740 if (const VarDecl *VDef = VD->getDefinition(C&: Info.Ctx))
4741 VD = VDef;
4742 }
4743 if (!VD || VD->isInvalidDecl()) {
4744 Info.FFDiag(E);
4745 return CompleteObject();
4746 }
4747
4748 bool IsConstant = BaseType.isConstant(Ctx: Info.Ctx);
4749 bool ConstexprVar = false;
4750 if (const auto *VD = dyn_cast_if_present<VarDecl>(
4751 Val: Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()))
4752 ConstexprVar = VD->isConstexpr();
4753
4754 // Unless we're looking at a local variable or argument in a constexpr call,
4755 // the variable we're reading must be const (unless we are binding to a
4756 // reference).
4757 if (AK != clang::AK_Dereference && !Frame) {
4758 if (IsAccess && isa<ParmVarDecl>(Val: VD)) {
4759 // Access of a parameter that's not associated with a frame isn't going
4760 // to work out, but we can leave it to evaluateVarDeclInit to provide a
4761 // suitable diagnostic.
4762 } else if (Info.getLangOpts().CPlusPlus14 &&
4763 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4764 // OK, we can read and modify an object if we're in the process of
4765 // evaluating its initializer, because its lifetime began in this
4766 // evaluation.
4767 } else if (isModification(AK)) {
4768 // All the remaining cases do not permit modification of the object.
4769 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4770 return CompleteObject();
4771 } else if (VD->isConstexpr()) {
4772 // OK, we can read this variable.
4773 } else if (Info.getLangOpts().C23 && ConstexprVar) {
4774 Info.FFDiag(E);
4775 return CompleteObject();
4776 } else if (VD->isCXXForRangeImplicitVar()) {
4777 if (!IsAccess)
4778 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4779 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_for_range_var) << VD;
4780 return CompleteObject();
4781 } else if (BaseType->isIntegralOrEnumerationType()) {
4782 if (!IsConstant) {
4783 if (!IsAccess)
4784 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4785 if (Info.getLangOpts().CPlusPlus) {
4786 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_non_const_int, ExtraNotes: 1) << VD;
4787 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4788 } else {
4789 Info.FFDiag(E);
4790 }
4791 return CompleteObject();
4792 }
4793 } else if (!IsAccess) {
4794 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4795 } else if ((IsConstant || BaseType->isReferenceType()) &&
4796 Info.checkingPotentialConstantExpression() &&
4797 BaseType->isLiteralType(Ctx: Info.Ctx) && !VD->hasDefinition()) {
4798 // This variable might end up being constexpr. Don't diagnose it yet.
4799 } else if (IsConstant) {
4800 // Keep evaluating to see what we can do. In particular, we support
4801 // folding of const floating-point types, in order to make static const
4802 // data members of such types (supported as an extension) more useful.
4803 if (Info.getLangOpts().CPlusPlus) {
4804 Info.CCEDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4805 ? diag::note_constexpr_ltor_non_constexpr
4806 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4807 << VD << BaseType;
4808 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4809 } else {
4810 Info.CCEDiag(E);
4811 }
4812 } else {
4813 // Never allow reading a non-const value.
4814 if (Info.getLangOpts().CPlusPlus) {
4815 Info.FFDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4816 ? diag::note_constexpr_ltor_non_constexpr
4817 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4818 << VD << BaseType;
4819 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4820 } else {
4821 Info.FFDiag(E);
4822 }
4823 return CompleteObject();
4824 }
4825 }
4826
4827 // When binding to a reference, the variable does not need to be constexpr
4828 // or have constant initalization.
4829 if (AK != clang::AK_Dereference &&
4830 !evaluateVarDeclInit(Info, E, VD, Frame, Version: LVal.getLValueVersion(),
4831 Result&: BaseVal))
4832 return CompleteObject();
4833 // If evaluateVarDeclInit sees a constexpr-unknown variable, it returns
4834 // a null BaseVal. Any constexpr-unknown variable seen here is an error:
4835 // we can't access a constexpr-unknown object.
4836 if (AK != clang::AK_Dereference && !BaseVal) {
4837 if (!Info.checkingPotentialConstantExpression()) {
4838 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unknown_variable, ExtraNotes: 1)
4839 << AK << VD;
4840 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4841 }
4842 return CompleteObject();
4843 }
4844 } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) {
4845 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
4846 if (!Alloc) {
4847 Info.FFDiag(E, DiagId: diag::note_constexpr_access_deleted_object) << AK;
4848 return CompleteObject();
4849 }
4850 return CompleteObject(LVal.Base, &(*Alloc)->Value,
4851 LVal.Base.getDynamicAllocType());
4852 }
4853 // When binding to a reference, the variable does not need to be
4854 // within its lifetime.
4855 else if (AK != clang::AK_Dereference) {
4856 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
4857
4858 if (!Frame) {
4859 if (const MaterializeTemporaryExpr *MTE =
4860 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: Base)) {
4861 assert(MTE->getStorageDuration() == SD_Static &&
4862 "should have a frame for a non-global materialized temporary");
4863
4864 // C++20 [expr.const]p4: [DR2126]
4865 // An object or reference is usable in constant expressions if it is
4866 // - a temporary object of non-volatile const-qualified literal type
4867 // whose lifetime is extended to that of a variable that is usable
4868 // in constant expressions
4869 //
4870 // C++20 [expr.const]p5:
4871 // an lvalue-to-rvalue conversion [is not allowed unless it applies to]
4872 // - a non-volatile glvalue that refers to an object that is usable
4873 // in constant expressions, or
4874 // - a non-volatile glvalue of literal type that refers to a
4875 // non-volatile object whose lifetime began within the evaluation
4876 // of E;
4877 //
4878 // C++11 misses the 'began within the evaluation of e' check and
4879 // instead allows all temporaries, including things like:
4880 // int &&r = 1;
4881 // int x = ++r;
4882 // constexpr int k = r;
4883 // Therefore we use the C++14-onwards rules in C++11 too.
4884 //
4885 // Note that temporaries whose lifetimes began while evaluating a
4886 // variable's constructor are not usable while evaluating the
4887 // corresponding destructor, not even if they're of const-qualified
4888 // types.
4889 if (!MTE->isUsableInConstantExpressions(Context: Info.Ctx) &&
4890 !lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4891 if (!IsAccess)
4892 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4893 Info.FFDiag(E, DiagId: diag::note_constexpr_access_static_temporary, ExtraNotes: 1) << AK;
4894 Info.Note(Loc: MTE->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
4895 return CompleteObject();
4896 }
4897
4898 BaseVal = MTE->getOrCreateValue(MayCreate: false);
4899 assert(BaseVal && "got reference to unevaluated temporary");
4900 } else if (const CompoundLiteralExpr *CLE =
4901 dyn_cast_or_null<CompoundLiteralExpr>(Val: Base)) {
4902 // According to GCC info page:
4903 //
4904 // 6.28 Compound Literals
4905 //
4906 // As an optimization, G++ sometimes gives array compound literals
4907 // longer lifetimes: when the array either appears outside a function or
4908 // has a const-qualified type. If foo and its initializer had elements
4909 // of type char *const rather than char *, or if foo were a global
4910 // variable, the array would have static storage duration. But it is
4911 // probably safest just to avoid the use of array compound literals in
4912 // C++ code.
4913 //
4914 // Obey that rule by checking constness for converted array types.
4915 if (QualType CLETy = CLE->getType(); CLETy->isArrayType() &&
4916 !LValType->isArrayType() &&
4917 !CLETy.isConstant(Ctx: Info.Ctx)) {
4918 Info.FFDiag(E);
4919 Info.Note(Loc: CLE->getExprLoc(), DiagId: diag::note_declared_at);
4920 return CompleteObject();
4921 }
4922
4923 BaseVal = &CLE->getStaticValue();
4924 } else {
4925 if (!IsAccess)
4926 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4927 APValue Val;
4928 LVal.moveInto(V&: Val);
4929 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unreadable_object)
4930 << AK
4931 << Val.getAsString(Ctx: Info.Ctx,
4932 Ty: Info.Ctx.getLValueReferenceType(T: LValType));
4933 NoteLValueLocation(Info, Base: LVal.Base);
4934 return CompleteObject();
4935 }
4936 } else if (AK != clang::AK_Dereference) {
4937 BaseVal = Frame->getTemporary(Key: Base, Version: LVal.Base.getVersion());
4938 assert(BaseVal && "missing value for temporary");
4939 }
4940 }
4941
4942 // In C++14, we can't safely access any mutable state when we might be
4943 // evaluating after an unmodeled side effect. Parameters are modeled as state
4944 // in the caller, but aren't visible once the call returns, so they can be
4945 // modified in a speculatively-evaluated call.
4946 //
4947 // FIXME: Not all local state is mutable. Allow local constant subobjects
4948 // to be read here (but take care with 'mutable' fields).
4949 unsigned VisibleDepth = Depth;
4950 if (llvm::isa_and_nonnull<ParmVarDecl>(
4951 Val: LVal.Base.dyn_cast<const ValueDecl *>()))
4952 ++VisibleDepth;
4953 if ((Frame && Info.getLangOpts().CPlusPlus14 &&
4954 Info.EvalStatus.HasSideEffects) ||
4955 (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth))
4956 return CompleteObject();
4957
4958 return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType);
4959}
4960
4961/// Perform an lvalue-to-rvalue conversion on the given glvalue. This
4962/// can also be used for 'lvalue-to-lvalue' conversions for looking up the
4963/// glvalue referred to by an entity of reference type.
4964///
4965/// \param Info - Information about the ongoing evaluation.
4966/// \param Conv - The expression for which we are performing the conversion.
4967/// Used for diagnostics.
4968/// \param Type - The type of the glvalue (before stripping cv-qualifiers in the
4969/// case of a non-class type).
4970/// \param LVal - The glvalue on which we are attempting to perform this action.
4971/// \param RVal - The produced value will be placed here.
4972/// \param WantObjectRepresentation - If true, we're looking for the object
4973/// representation rather than the value, and in particular,
4974/// there is no requirement that the result be fully initialized.
4975static bool
4976handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type,
4977 const LValue &LVal, APValue &RVal,
4978 bool WantObjectRepresentation = false) {
4979 if (LVal.Designator.Invalid)
4980 return false;
4981
4982 // Check for special cases where there is no existing APValue to look at.
4983 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
4984
4985 AccessKinds AK =
4986 WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read;
4987
4988 if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) {
4989 if (isa<StringLiteral>(Val: Base) || isa<PredefinedExpr>(Val: Base)) {
4990 // Special-case character extraction so we don't have to construct an
4991 // APValue for the whole string.
4992 assert(LVal.Designator.Entries.size() <= 1 &&
4993 "Can only read characters from string literals");
4994 if (LVal.Designator.Entries.empty()) {
4995 // Fail for now for LValue to RValue conversion of an array.
4996 // (This shouldn't show up in C/C++, but it could be triggered by a
4997 // weird EvaluateAsRValue call from a tool.)
4998 Info.FFDiag(E: Conv);
4999 return false;
5000 }
5001 if (LVal.Designator.isOnePastTheEnd()) {
5002 if (Info.getLangOpts().CPlusPlus11)
5003 Info.FFDiag(E: Conv, DiagId: diag::note_constexpr_access_past_end) << AK;
5004 else
5005 Info.FFDiag(E: Conv);
5006 return false;
5007 }
5008 uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex();
5009 RVal = APValue(extractStringLiteralCharacter(Info, Lit: Base, Index: CharIndex));
5010 return true;
5011 }
5012 }
5013
5014 CompleteObject Obj = findCompleteObject(Info, E: Conv, AK, LVal, LValType: Type);
5015 return Obj && extractSubobject(Info, E: Conv, Obj, Sub: LVal.Designator, Result&: RVal, AK);
5016}
5017
5018static bool hlslElementwiseCastHelper(EvalInfo &Info, const Expr *E,
5019 QualType DestTy,
5020 SmallVectorImpl<APValue> &SrcVals,
5021 SmallVectorImpl<QualType> &SrcTypes) {
5022 APValue Val;
5023 if (!Evaluate(Result&: Val, Info, E))
5024 return false;
5025
5026 // must be dealing with a record
5027 if (Val.isLValue()) {
5028 LValue LVal;
5029 LVal.setFrom(Ctx: Info.Ctx, V: Val);
5030 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal, RVal&: Val))
5031 return false;
5032 }
5033
5034 unsigned NEls = elementwiseSize(Info, BaseTy: DestTy);
5035 // flatten the source
5036 if (!flattenAPValue(Info, E, Value: Val, BaseTy: E->getType(), Elements&: SrcVals, Types&: SrcTypes, Size: NEls))
5037 return false;
5038
5039 return true;
5040}
5041
5042/// Perform an assignment of Val to LVal. Takes ownership of Val.
5043static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal,
5044 QualType LValType, APValue &Val) {
5045 if (LVal.Designator.Invalid)
5046 return false;
5047
5048 if (!Info.getLangOpts().CPlusPlus14) {
5049 Info.FFDiag(E);
5050 return false;
5051 }
5052
5053 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5054 return Obj && modifySubobject(Info, E, Obj, Sub: LVal.Designator, NewVal&: Val);
5055}
5056
5057namespace {
5058struct CompoundAssignSubobjectHandler {
5059 EvalInfo &Info;
5060 const CompoundAssignOperator *E;
5061 QualType PromotedLHSType;
5062 BinaryOperatorKind Opcode;
5063 const APValue &RHS;
5064
5065 static const AccessKinds AccessKind = AK_Assign;
5066
5067 typedef bool result_type;
5068
5069 bool checkConst(QualType QT) {
5070 // Assigning to a const object has undefined behavior.
5071 if (QT.isConstQualified()) {
5072 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5073 return false;
5074 }
5075 return true;
5076 }
5077
5078 bool failed() { return false; }
5079 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5080 switch (Subobj.getKind()) {
5081 case APValue::Int:
5082 return found(Value&: Subobj.getInt(), SubobjType);
5083 case APValue::Float:
5084 return found(Value&: Subobj.getFloat(), SubobjType);
5085 case APValue::ComplexInt:
5086 case APValue::ComplexFloat:
5087 // FIXME: Implement complex compound assignment.
5088 Info.FFDiag(E);
5089 return false;
5090 case APValue::LValue:
5091 return foundPointer(Subobj, SubobjType);
5092 case APValue::Vector:
5093 return foundVector(Value&: Subobj, SubobjType);
5094 case APValue::Indeterminate:
5095 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
5096 << /*read of=*/0 << /*uninitialized object=*/1
5097 << E->getLHS()->getSourceRange();
5098 NoteLValueLocation(Info, Base);
5099 return false;
5100 default:
5101 // FIXME: can this happen?
5102 Info.FFDiag(E);
5103 return false;
5104 }
5105 }
5106
5107 bool foundVector(APValue &Value, QualType SubobjType) {
5108 if (!checkConst(QT: SubobjType))
5109 return false;
5110
5111 if (!SubobjType->isVectorType()) {
5112 Info.FFDiag(E);
5113 return false;
5114 }
5115 return handleVectorVectorBinOp(Info, E, Opcode, LHSValue&: Value, RHSValue: RHS);
5116 }
5117
5118 bool found(APSInt &Value, QualType SubobjType) {
5119 if (!checkConst(QT: SubobjType))
5120 return false;
5121
5122 if (!SubobjType->isIntegerType()) {
5123 // We don't support compound assignment on integer-cast-to-pointer
5124 // values.
5125 Info.FFDiag(E);
5126 return false;
5127 }
5128
5129 if (RHS.isInt()) {
5130 APSInt LHS =
5131 HandleIntToIntCast(Info, E, DestType: PromotedLHSType, SrcType: SubobjType, Value);
5132 if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS: RHS.getInt(), Result&: LHS))
5133 return false;
5134 Value = HandleIntToIntCast(Info, E, DestType: SubobjType, SrcType: PromotedLHSType, Value: LHS);
5135 return true;
5136 } else if (RHS.isFloat()) {
5137 const FPOptions FPO = E->getFPFeaturesInEffect(
5138 LO: Info.Ctx.getLangOpts());
5139 APFloat FValue(0.0);
5140 return HandleIntToFloatCast(Info, E, FPO, SrcType: SubobjType, Value,
5141 DestType: PromotedLHSType, Result&: FValue) &&
5142 handleFloatFloatBinOp(Info, E, LHS&: FValue, Opcode, RHS: RHS.getFloat()) &&
5143 HandleFloatToIntCast(Info, E, SrcType: PromotedLHSType, Value: FValue, DestType: SubobjType,
5144 Result&: Value);
5145 }
5146
5147 Info.FFDiag(E);
5148 return false;
5149 }
5150 bool found(APFloat &Value, QualType SubobjType) {
5151 return checkConst(QT: SubobjType) &&
5152 HandleFloatToFloatCast(Info, E, SrcType: SubobjType, DestType: PromotedLHSType,
5153 Result&: Value) &&
5154 handleFloatFloatBinOp(Info, E, LHS&: Value, Opcode, RHS: RHS.getFloat()) &&
5155 HandleFloatToFloatCast(Info, E, SrcType: PromotedLHSType, DestType: SubobjType, Result&: Value);
5156 }
5157 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5158 if (!checkConst(QT: SubobjType))
5159 return false;
5160
5161 QualType PointeeType;
5162 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5163 PointeeType = PT->getPointeeType();
5164
5165 if (PointeeType.isNull() || !RHS.isInt() ||
5166 (Opcode != BO_Add && Opcode != BO_Sub)) {
5167 Info.FFDiag(E);
5168 return false;
5169 }
5170
5171 APSInt Offset = RHS.getInt();
5172 if (Opcode == BO_Sub)
5173 negateAsSigned(Int&: Offset);
5174
5175 LValue LVal;
5176 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5177 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType, Adjustment: Offset))
5178 return false;
5179 LVal.moveInto(V&: Subobj);
5180 return true;
5181 }
5182};
5183} // end anonymous namespace
5184
5185const AccessKinds CompoundAssignSubobjectHandler::AccessKind;
5186
5187/// Perform a compound assignment of LVal <op>= RVal.
5188static bool handleCompoundAssignment(EvalInfo &Info,
5189 const CompoundAssignOperator *E,
5190 const LValue &LVal, QualType LValType,
5191 QualType PromotedLValType,
5192 BinaryOperatorKind Opcode,
5193 const APValue &RVal) {
5194 if (LVal.Designator.Invalid)
5195 return false;
5196
5197 if (!Info.getLangOpts().CPlusPlus14) {
5198 Info.FFDiag(E);
5199 return false;
5200 }
5201
5202 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5203 CompoundAssignSubobjectHandler Handler = { .Info: Info, .E: E, .PromotedLHSType: PromotedLValType, .Opcode: Opcode,
5204 .RHS: RVal };
5205 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5206}
5207
5208namespace {
5209struct IncDecSubobjectHandler {
5210 EvalInfo &Info;
5211 const UnaryOperator *E;
5212 AccessKinds AccessKind;
5213 APValue *Old;
5214
5215 typedef bool result_type;
5216
5217 bool checkConst(QualType QT) {
5218 // Assigning to a const object has undefined behavior.
5219 if (QT.isConstQualified()) {
5220 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5221 return false;
5222 }
5223 return true;
5224 }
5225
5226 bool failed() { return false; }
5227 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5228 // Stash the old value. Also clear Old, so we don't clobber it later
5229 // if we're post-incrementing a complex.
5230 if (Old) {
5231 *Old = Subobj;
5232 Old = nullptr;
5233 }
5234
5235 switch (Subobj.getKind()) {
5236 case APValue::Int:
5237 return found(Value&: Subobj.getInt(), SubobjType);
5238 case APValue::Float:
5239 return found(Value&: Subobj.getFloat(), SubobjType);
5240 case APValue::ComplexInt:
5241 return found(Value&: Subobj.getComplexIntReal(),
5242 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5243 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5244 case APValue::ComplexFloat:
5245 return found(Value&: Subobj.getComplexFloatReal(),
5246 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5247 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5248 case APValue::LValue:
5249 return foundPointer(Subobj, SubobjType);
5250 default:
5251 // FIXME: can this happen?
5252 Info.FFDiag(E);
5253 return false;
5254 }
5255 }
5256 bool found(APSInt &Value, QualType SubobjType) {
5257 if (!checkConst(QT: SubobjType))
5258 return false;
5259
5260 if (!SubobjType->isIntegerType()) {
5261 // We don't support increment / decrement on integer-cast-to-pointer
5262 // values.
5263 Info.FFDiag(E);
5264 return false;
5265 }
5266
5267 if (Old) *Old = APValue(Value);
5268
5269 // bool arithmetic promotes to int, and the conversion back to bool
5270 // doesn't reduce mod 2^n, so special-case it.
5271 if (SubobjType->isBooleanType()) {
5272 if (AccessKind == AK_Increment)
5273 Value = 1;
5274 else
5275 Value = !Value;
5276 return true;
5277 }
5278
5279 bool WasNegative = Value.isNegative();
5280 if (AccessKind == AK_Increment) {
5281 ++Value;
5282
5283 if (!WasNegative && Value.isNegative() && E->canOverflow() &&
5284 !SubobjType.isWrapType()) {
5285 APSInt ActualValue(Value, /*IsUnsigned*/true);
5286 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5287 }
5288 } else {
5289 --Value;
5290
5291 if (WasNegative && !Value.isNegative() && E->canOverflow() &&
5292 !SubobjType.isWrapType()) {
5293 unsigned BitWidth = Value.getBitWidth();
5294 APSInt ActualValue(Value.sext(width: BitWidth + 1), /*IsUnsigned*/false);
5295 ActualValue.setBit(BitWidth);
5296 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5297 }
5298 }
5299 return true;
5300 }
5301 bool found(APFloat &Value, QualType SubobjType) {
5302 if (!checkConst(QT: SubobjType))
5303 return false;
5304
5305 if (Old) *Old = APValue(Value);
5306
5307 APFloat One(Value.getSemantics(), 1);
5308 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
5309 APFloat::opStatus St;
5310 if (AccessKind == AK_Increment)
5311 St = Value.add(RHS: One, RM);
5312 else
5313 St = Value.subtract(RHS: One, RM);
5314 return checkFloatingPointResultForConstantFolding(Info, E, St);
5315 }
5316 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5317 if (!checkConst(QT: SubobjType))
5318 return false;
5319
5320 QualType PointeeType;
5321 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5322 PointeeType = PT->getPointeeType();
5323 else {
5324 Info.FFDiag(E);
5325 return false;
5326 }
5327
5328 LValue LVal;
5329 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5330 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType,
5331 Adjustment: AccessKind == AK_Increment ? 1 : -1))
5332 return false;
5333 LVal.moveInto(V&: Subobj);
5334 return true;
5335 }
5336};
5337} // end anonymous namespace
5338
5339/// Perform an increment or decrement on LVal.
5340static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal,
5341 QualType LValType, bool IsIncrement, APValue *Old) {
5342 if (LVal.Designator.Invalid)
5343 return false;
5344
5345 if (!Info.getLangOpts().CPlusPlus14) {
5346 Info.FFDiag(E);
5347 return false;
5348 }
5349
5350 AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement;
5351 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType);
5352 IncDecSubobjectHandler Handler = {.Info: Info, .E: cast<UnaryOperator>(Val: E), .AccessKind: AK, .Old: Old};
5353 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5354}
5355
5356/// Build an lvalue for the object argument of a member function call.
5357static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,
5358 LValue &This) {
5359 if (Object->getType()->isPointerType() && Object->isPRValue())
5360 return EvaluatePointer(E: Object, Result&: This, Info);
5361
5362 if (Object->isGLValue())
5363 return EvaluateLValue(E: Object, Result&: This, Info);
5364
5365 if (Object->getType()->isLiteralType(Ctx: Info.Ctx))
5366 return EvaluateTemporary(E: Object, Result&: This, Info);
5367
5368 if (Object->getType()->isRecordType() && Object->isPRValue())
5369 return EvaluateTemporary(E: Object, Result&: This, Info);
5370
5371 Info.FFDiag(E: Object, DiagId: diag::note_constexpr_nonliteral) << Object->getType();
5372 return false;
5373}
5374
5375/// HandleMemberPointerAccess - Evaluate a member access operation and build an
5376/// lvalue referring to the result.
5377///
5378/// \param Info - Information about the ongoing evaluation.
5379/// \param LV - An lvalue referring to the base of the member pointer.
5380/// \param RHS - The member pointer expression.
5381/// \param IncludeMember - Specifies whether the member itself is included in
5382/// the resulting LValue subobject designator. This is not possible when
5383/// creating a bound member function.
5384/// \return The field or method declaration to which the member pointer refers,
5385/// or 0 if evaluation fails.
5386static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5387 QualType LVType,
5388 LValue &LV,
5389 const Expr *RHS,
5390 bool IncludeMember = true) {
5391 MemberPtr MemPtr;
5392 if (!EvaluateMemberPointer(E: RHS, Result&: MemPtr, Info))
5393 return nullptr;
5394
5395 // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to
5396 // member value, the behavior is undefined.
5397 if (!MemPtr.getDecl()) {
5398 // FIXME: Specific diagnostic.
5399 Info.FFDiag(E: RHS);
5400 return nullptr;
5401 }
5402
5403 if (MemPtr.isDerivedMember()) {
5404 // This is a member of some derived class. Truncate LV appropriately.
5405 // The end of the derived-to-base path for the base object must match the
5406 // derived-to-base path for the member pointer.
5407 // C++23 [expr.mptr.oper]p4:
5408 // If the result of E1 is an object [...] whose most derived object does
5409 // not contain the member to which E2 refers, the behavior is undefined.
5410 if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >
5411 LV.Designator.Entries.size()) {
5412 Info.FFDiag(E: RHS);
5413 return nullptr;
5414 }
5415 unsigned PathLengthToMember =
5416 LV.Designator.Entries.size() - MemPtr.Path.size();
5417 for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {
5418 const CXXRecordDecl *LVDecl = getAsBaseClass(
5419 E: LV.Designator.Entries[PathLengthToMember + I]);
5420 const CXXRecordDecl *MPDecl = MemPtr.Path[I];
5421 if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) {
5422 Info.FFDiag(E: RHS);
5423 return nullptr;
5424 }
5425 }
5426 // MemPtr.Path only contains the base classes of the class directly
5427 // containing the member E2. It is still necessary to check that the class
5428 // directly containing the member E2 lies on the derived-to-base path of E1
5429 // to avoid incorrectly permitting member pointer access into a sibling
5430 // class of the class containing the member E2. If this class would
5431 // correspond to the most-derived class of E1, it either isn't contained in
5432 // LV.Designator.Entries or the corresponding entry refers to an array
5433 // element instead. Therefore get the most derived class directly in this
5434 // case. Otherwise the previous entry should correpond to this class.
5435 const CXXRecordDecl *LastLVDecl =
5436 (PathLengthToMember > LV.Designator.MostDerivedPathLength)
5437 ? getAsBaseClass(E: LV.Designator.Entries[PathLengthToMember - 1])
5438 : LV.Designator.MostDerivedType->getAsCXXRecordDecl();
5439 const CXXRecordDecl *LastMPDecl = MemPtr.getContainingRecord();
5440 if (LastLVDecl->getCanonicalDecl() != LastMPDecl->getCanonicalDecl()) {
5441 Info.FFDiag(E: RHS);
5442 return nullptr;
5443 }
5444
5445 // Truncate the lvalue to the appropriate derived class.
5446 if (!CastToDerivedClass(Info, E: RHS, Result&: LV, TruncatedType: MemPtr.getContainingRecord(),
5447 TruncatedElements: PathLengthToMember))
5448 return nullptr;
5449 } else if (!MemPtr.Path.empty()) {
5450 // Extend the LValue path with the member pointer's path.
5451 LV.Designator.Entries.reserve(N: LV.Designator.Entries.size() +
5452 MemPtr.Path.size() + IncludeMember);
5453
5454 // Walk down to the appropriate base class.
5455 if (const PointerType *PT = LVType->getAs<PointerType>())
5456 LVType = PT->getPointeeType();
5457 const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();
5458 assert(RD && "member pointer access on non-class-type expression");
5459 // The first class in the path is that of the lvalue.
5460 for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {
5461 const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];
5462 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, Base))
5463 return nullptr;
5464 RD = Base;
5465 }
5466 // Finally cast to the class containing the member.
5467 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD,
5468 Base: MemPtr.getContainingRecord()))
5469 return nullptr;
5470 }
5471
5472 // Add the member. Note that we cannot build bound member functions here.
5473 if (IncludeMember) {
5474 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: MemPtr.getDecl())) {
5475 if (!HandleLValueMember(Info, E: RHS, LVal&: LV, FD))
5476 return nullptr;
5477 } else if (const IndirectFieldDecl *IFD =
5478 dyn_cast<IndirectFieldDecl>(Val: MemPtr.getDecl())) {
5479 if (!HandleLValueIndirectMember(Info, E: RHS, LVal&: LV, IFD))
5480 return nullptr;
5481 } else {
5482 llvm_unreachable("can't construct reference to bound member function");
5483 }
5484 }
5485
5486 return MemPtr.getDecl();
5487}
5488
5489static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5490 const BinaryOperator *BO,
5491 LValue &LV,
5492 bool IncludeMember = true) {
5493 assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);
5494
5495 if (!EvaluateObjectArgument(Info, Object: BO->getLHS(), This&: LV)) {
5496 if (Info.noteFailure()) {
5497 MemberPtr MemPtr;
5498 EvaluateMemberPointer(E: BO->getRHS(), Result&: MemPtr, Info);
5499 }
5500 return nullptr;
5501 }
5502
5503 return HandleMemberPointerAccess(Info, LVType: BO->getLHS()->getType(), LV,
5504 RHS: BO->getRHS(), IncludeMember);
5505}
5506
5507/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
5508/// the provided lvalue, which currently refers to the base object.
5509static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
5510 LValue &Result) {
5511 SubobjectDesignator &D = Result.Designator;
5512 if (D.Invalid || !Result.checkNullPointer(Info, E, CSK: CSK_Derived))
5513 return false;
5514
5515 QualType TargetQT = E->getType();
5516 if (const PointerType *PT = TargetQT->getAs<PointerType>())
5517 TargetQT = PT->getPointeeType();
5518
5519 auto InvalidCast = [&]() {
5520 if (!Info.checkingPotentialConstantExpression() ||
5521 !Result.AllowConstexprUnknown) {
5522 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_downcast)
5523 << D.MostDerivedType << TargetQT;
5524 }
5525 return false;
5526 };
5527
5528 // Check this cast lands within the final derived-to-base subobject path.
5529 if (D.MostDerivedPathLength + E->path_size() > D.Entries.size())
5530 return InvalidCast();
5531
5532 // Check the type of the final cast. We don't need to check the path,
5533 // since a cast can only be formed if the path is unique.
5534 unsigned NewEntriesSize = D.Entries.size() - E->path_size();
5535 const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
5536 const CXXRecordDecl *FinalType;
5537 if (NewEntriesSize == D.MostDerivedPathLength)
5538 FinalType = D.MostDerivedType->getAsCXXRecordDecl();
5539 else
5540 FinalType = getAsBaseClass(E: D.Entries[NewEntriesSize - 1]);
5541 if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl())
5542 return InvalidCast();
5543
5544 // Truncate the lvalue to the appropriate derived class.
5545 return CastToDerivedClass(Info, E, Result, TruncatedType: TargetType, TruncatedElements: NewEntriesSize);
5546}
5547
5548/// Get the value to use for a default-initialized object of type T.
5549/// Return false if it encounters something invalid.
5550static bool handleDefaultInitValue(QualType T, APValue &Result,
5551 bool IsCompleteClass = true) {
5552 bool Success = true;
5553
5554 // If there is already a value present don't overwrite it.
5555 if (!Result.isAbsent())
5556 return true;
5557
5558 if (auto *RD = T->getAsCXXRecordDecl()) {
5559 if (RD->isInvalidDecl()) {
5560 Result = APValue();
5561 return false;
5562 }
5563 if (RD->isUnion()) {
5564 Result = APValue((const FieldDecl *)nullptr);
5565 return true;
5566 }
5567
5568 // bases() includes directly specified virtual bases as well.
5569 unsigned NonVirtualBases = countNonVirtualBases(RD);
5570 Result =
5571 APValue(APValue::UninitStruct(), NonVirtualBases, RD->getNumFields(),
5572 IsCompleteClass ? RD->getNumVBases() : 0);
5573
5574 unsigned Index = 0;
5575 for (const CXXBaseSpecifier &B : RD->bases()) {
5576 if (B.isVirtual())
5577 continue;
5578 Success &= handleDefaultInitValue(
5579 T: B.getType(), Result&: Result.getStructBase(i: Index), /*IsCompleteClass=*/false);
5580 ++Index;
5581 }
5582
5583 for (const auto *I : RD->fields()) {
5584 if (I->isUnnamedBitField())
5585 continue;
5586 Success &= handleDefaultInitValue(
5587 T: I->getType(), Result&: Result.getStructField(i: I->getFieldIndex()));
5588 }
5589
5590 if (IsCompleteClass) {
5591 Index = 0;
5592
5593 for (const auto &B : RD->vbases()) {
5594 Success &= handleDefaultInitValue(T: B.getType(),
5595 Result&: Result.getStructVirtualBase(i: Index),
5596 /*IsCompleteClass=*/false);
5597 ++Index;
5598 }
5599 } else {
5600 // Virtual bases should only exist at the top level of an APValue.
5601 assert(Result.getStructNumVirtualBases() == 0);
5602 }
5603
5604 return Success;
5605 }
5606
5607 if (auto *AT =
5608 dyn_cast_or_null<ConstantArrayType>(Val: T->getAsArrayTypeUnsafe())) {
5609 Result = APValue(APValue::UninitArray(), 0, AT->getZExtSize());
5610 if (Result.hasArrayFiller())
5611 Success &=
5612 handleDefaultInitValue(T: AT->getElementType(), Result&: Result.getArrayFiller());
5613 return Success;
5614 }
5615
5616 Result = APValue::IndeterminateValue();
5617 return true;
5618}
5619
5620namespace {
5621enum EvalStmtResult {
5622 /// Evaluation failed.
5623 ESR_Failed,
5624 /// Hit a 'return' statement.
5625 ESR_Returned,
5626 /// Evaluation succeeded.
5627 ESR_Succeeded,
5628 /// Hit a 'continue' statement.
5629 ESR_Continue,
5630 /// Hit a 'break' statement.
5631 ESR_Break,
5632 /// Still scanning for 'case' or 'default' statement.
5633 ESR_CaseNotFound
5634};
5635}
5636/// Evaluates the initializer of a reference.
5637static bool EvaluateInitForDeclOfReferenceType(EvalInfo &Info,
5638 const ValueDecl *D,
5639 const Expr *Init, LValue &Result,
5640 APValue &Val) {
5641 assert(Init->isGLValue() && D->getType()->isReferenceType());
5642 // A reference is an lvalue.
5643 if (!EvaluateLValue(E: Init, Result, Info))
5644 return false;
5645 // [C++26][decl.ref]
5646 // The object designated by such a glvalue can be outside its lifetime
5647 // Because a null pointer value or a pointer past the end of an object
5648 // does not point to an object, a reference in a well-defined program cannot
5649 // refer to such things;
5650 if (!Result.Designator.Invalid && Result.Designator.isOnePastTheEnd()) {
5651 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_access_past_end) << AK_Dereference;
5652 return false;
5653 }
5654
5655 // Save the result.
5656 Result.moveInto(V&: Val);
5657 return true;
5658}
5659
5660static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) {
5661 if (VD->isInvalidDecl())
5662 return false;
5663 // We don't need to evaluate the initializer for a static local.
5664 if (!VD->hasLocalStorage())
5665 return true;
5666
5667 LValue Result;
5668 APValue &Val = Info.CurrentCall->createTemporary(Key: VD, T: VD->getType(),
5669 Scope: ScopeKind::Block, LV&: Result);
5670
5671 const Expr *InitE = VD->getInit();
5672 if (!InitE) {
5673 if (VD->getType()->isDependentType())
5674 return Info.noteSideEffect();
5675 return handleDefaultInitValue(T: VD->getType(), Result&: Val);
5676 }
5677 if (InitE->isValueDependent())
5678 return false;
5679
5680 // For references to objects, check they do not designate a one-past-the-end
5681 // object.
5682 if (VD->getType()->isReferenceType()) {
5683 return EvaluateInitForDeclOfReferenceType(Info, D: VD, Init: InitE, Result, Val);
5684 } else if (!EvaluateInPlace(Result&: Val, Info, This: Result, E: InitE)) {
5685 // Wipe out any partially-computed value, to allow tracking that this
5686 // evaluation failed.
5687 Val = APValue();
5688 return false;
5689 }
5690
5691 return true;
5692}
5693
5694static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5695 const DecompositionDecl *DD);
5696
5697static bool EvaluateDecl(EvalInfo &Info, const Decl *D,
5698 bool EvaluateConditionDecl = false) {
5699 bool OK = true;
5700 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
5701 OK &= EvaluateVarDecl(Info, VD);
5702
5703 if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(Val: D);
5704 EvaluateConditionDecl && DD)
5705 OK &= EvaluateDecompositionDeclInit(Info, DD);
5706
5707 return OK;
5708}
5709
5710static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5711 const DecompositionDecl *DD) {
5712 bool OK = true;
5713 for (auto *BD : DD->flat_bindings())
5714 if (auto *VD = BD->getHoldingVar())
5715 OK &= EvaluateDecl(Info, D: VD, /*EvaluateConditionDecl=*/true);
5716
5717 return OK;
5718}
5719
5720static bool MaybeEvaluateDeferredVarDeclInit(EvalInfo &Info,
5721 const VarDecl *VD) {
5722 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(Val: VD)) {
5723 if (!EvaluateDecompositionDeclInit(Info, DD))
5724 return false;
5725 }
5726 return true;
5727}
5728
5729static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) {
5730 assert(E->isValueDependent());
5731 if (Info.noteSideEffect())
5732 return true;
5733 assert(E->containsErrors() && "valid value-dependent expression should never "
5734 "reach invalid code path.");
5735 return false;
5736}
5737
5738/// Evaluate a condition (either a variable declaration or an expression).
5739static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl,
5740 const Expr *Cond, bool &Result) {
5741 if (Cond->isValueDependent())
5742 return false;
5743 FullExpressionRAII Scope(Info);
5744 if (CondDecl && !EvaluateDecl(Info, D: CondDecl))
5745 return false;
5746 if (!EvaluateAsBooleanCondition(E: Cond, Result, Info))
5747 return false;
5748 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: CondDecl))
5749 return false;
5750 return Scope.destroy();
5751}
5752
5753namespace {
5754/// A location where the result (returned value) of evaluating a
5755/// statement should be stored.
5756struct StmtResult {
5757 /// The APValue that should be filled in with the returned value.
5758 APValue &Value;
5759 /// The location containing the result, if any (used to support RVO).
5760 const LValue *Slot;
5761};
5762
5763struct TempVersionRAII {
5764 CallStackFrame &Frame;
5765
5766 TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) {
5767 Frame.pushTempVersion();
5768 }
5769
5770 ~TempVersionRAII() {
5771 Frame.popTempVersion();
5772 }
5773};
5774
5775}
5776
5777static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
5778 const Stmt *S,
5779 const SwitchCase *SC = nullptr);
5780
5781/// Helper to implement named break/continue. Returns 'true' if the evaluation
5782/// result should be propagated up. Otherwise, it sets the evaluation result
5783/// to either Continue to continue the current loop, or Succeeded to break it.
5784static bool ShouldPropagateBreakContinue(EvalInfo &Info,
5785 const Stmt *LoopOrSwitch,
5786 ArrayRef<BlockScopeRAII *> Scopes,
5787 EvalStmtResult &ESR) {
5788 bool IsSwitch = isa<SwitchStmt>(Val: LoopOrSwitch);
5789
5790 // For loops, map Succeeded to Continue so we don't have to check for both.
5791 if (!IsSwitch && ESR == ESR_Succeeded) {
5792 ESR = ESR_Continue;
5793 return false;
5794 }
5795
5796 if (ESR != ESR_Break && ESR != ESR_Continue)
5797 return false;
5798
5799 // Are we breaking out of or continuing this statement?
5800 bool CanBreakOrContinue = !IsSwitch || ESR == ESR_Break;
5801 const Stmt *StackTop = Info.BreakContinueStack.back();
5802 if (CanBreakOrContinue && (StackTop == nullptr || StackTop == LoopOrSwitch)) {
5803 Info.BreakContinueStack.pop_back();
5804 if (ESR == ESR_Break)
5805 ESR = ESR_Succeeded;
5806 return false;
5807 }
5808
5809 // We're not. Propagate the result up.
5810 for (BlockScopeRAII *S : Scopes) {
5811 if (!S->destroy()) {
5812 ESR = ESR_Failed;
5813 break;
5814 }
5815 }
5816 return true;
5817}
5818
5819/// Evaluate the body of a loop, and translate the result as appropriate.
5820static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info,
5821 const Stmt *Body,
5822 const SwitchCase *Case = nullptr) {
5823 BlockScopeRAII Scope(Info);
5824
5825 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Body, SC: Case);
5826 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5827 ESR = ESR_Failed;
5828
5829 return ESR;
5830}
5831
5832/// Evaluate a switch statement.
5833static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
5834 const SwitchStmt *SS) {
5835 BlockScopeRAII Scope(Info);
5836
5837 // Evaluate the switch condition.
5838 APSInt Value;
5839 {
5840 if (const Stmt *Init = SS->getInit()) {
5841 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
5842 if (ESR != ESR_Succeeded) {
5843 if (ESR != ESR_Failed && !Scope.destroy())
5844 ESR = ESR_Failed;
5845 return ESR;
5846 }
5847 }
5848
5849 FullExpressionRAII CondScope(Info);
5850 if (SS->getConditionVariable() &&
5851 !EvaluateDecl(Info, D: SS->getConditionVariable()))
5852 return ESR_Failed;
5853 if (SS->getCond()->isValueDependent()) {
5854 // We don't know what the value is, and which branch should jump to.
5855 EvaluateDependentExpr(E: SS->getCond(), Info);
5856 return ESR_Failed;
5857 }
5858 if (!EvaluateInteger(E: SS->getCond(), Result&: Value, Info))
5859 return ESR_Failed;
5860
5861 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: SS->getConditionVariable()))
5862 return ESR_Failed;
5863
5864 if (!CondScope.destroy())
5865 return ESR_Failed;
5866 }
5867
5868 // Find the switch case corresponding to the value of the condition.
5869 // FIXME: Cache this lookup.
5870 const SwitchCase *Found = nullptr;
5871 for (const SwitchCase *SC = SS->getSwitchCaseList(); SC;
5872 SC = SC->getNextSwitchCase()) {
5873 if (isa<DefaultStmt>(Val: SC)) {
5874 Found = SC;
5875 continue;
5876 }
5877
5878 const CaseStmt *CS = cast<CaseStmt>(Val: SC);
5879 const Expr *LHS = CS->getLHS();
5880 const Expr *RHS = CS->getRHS();
5881 if (LHS->isValueDependent() || (RHS && RHS->isValueDependent()))
5882 return ESR_Failed;
5883 APSInt LHSValue = LHS->EvaluateKnownConstInt(Ctx: Info.Ctx);
5884 APSInt RHSValue = RHS ? RHS->EvaluateKnownConstInt(Ctx: Info.Ctx) : LHSValue;
5885 if (LHSValue <= Value && Value <= RHSValue) {
5886 Found = SC;
5887 break;
5888 }
5889 }
5890
5891 if (!Found)
5892 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
5893
5894 // Search the switch body for the switch case and evaluate it from there.
5895 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SS->getBody(), SC: Found);
5896 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5897 return ESR_Failed;
5898 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: SS, /*Scopes=*/{}, ESR))
5899 return ESR;
5900
5901 switch (ESR) {
5902 case ESR_Break:
5903 llvm_unreachable("Should have been converted to Succeeded");
5904 case ESR_Succeeded:
5905 case ESR_Continue:
5906 case ESR_Failed:
5907 case ESR_Returned:
5908 return ESR;
5909 case ESR_CaseNotFound:
5910 // This can only happen if the switch case is nested within a statement
5911 // expression. We have no intention of supporting that.
5912 Info.FFDiag(Loc: Found->getBeginLoc(),
5913 DiagId: diag::note_constexpr_stmt_expr_unsupported);
5914 return ESR_Failed;
5915 }
5916 llvm_unreachable("Invalid EvalStmtResult!");
5917}
5918
5919static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) {
5920 // An expression E is a core constant expression unless the evaluation of E
5921 // would evaluate one of the following: [C++23] - a control flow that passes
5922 // through a declaration of a variable with static or thread storage duration
5923 // unless that variable is usable in constant expressions.
5924 if (VD->isLocalVarDecl() && VD->isStaticLocal() &&
5925 !VD->isUsableInConstantExpressions(C: Info.Ctx)) {
5926 Info.CCEDiag(Loc: VD->getLocation(), DiagId: diag::note_constexpr_static_local)
5927 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
5928 return false;
5929 }
5930 return true;
5931}
5932
5933// Evaluate a statement.
5934static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
5935 const Stmt *S, const SwitchCase *Case) {
5936 if (!Info.nextStep(S))
5937 return ESR_Failed;
5938
5939 // If we're hunting down a 'case' or 'default' label, recurse through
5940 // substatements until we hit the label.
5941 if (Case) {
5942 switch (S->getStmtClass()) {
5943 case Stmt::CompoundStmtClass:
5944 // FIXME: Precompute which substatement of a compound statement we
5945 // would jump to, and go straight there rather than performing a
5946 // linear scan each time.
5947 case Stmt::LabelStmtClass:
5948 case Stmt::AttributedStmtClass:
5949 case Stmt::DoStmtClass:
5950 break;
5951
5952 case Stmt::CaseStmtClass:
5953 case Stmt::DefaultStmtClass:
5954 if (Case == S)
5955 Case = nullptr;
5956 break;
5957
5958 case Stmt::IfStmtClass: {
5959 // FIXME: Precompute which side of an 'if' we would jump to, and go
5960 // straight there rather than scanning both sides.
5961 const IfStmt *IS = cast<IfStmt>(Val: S);
5962
5963 // Wrap the evaluation in a block scope, in case it's a DeclStmt
5964 // preceded by our switch label.
5965 BlockScopeRAII Scope(Info);
5966
5967 // Step into the init statement in case it brings an (uninitialized)
5968 // variable into scope.
5969 if (const Stmt *Init = IS->getInit()) {
5970 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
5971 if (ESR != ESR_CaseNotFound) {
5972 assert(ESR != ESR_Succeeded);
5973 return ESR;
5974 }
5975 }
5976
5977 // Condition variable must be initialized if it exists.
5978 // FIXME: We can skip evaluating the body if there's a condition
5979 // variable, as there can't be any case labels within it.
5980 // (The same is true for 'for' statements.)
5981
5982 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: IS->getThen(), Case);
5983 if (ESR == ESR_Failed)
5984 return ESR;
5985 if (ESR != ESR_CaseNotFound)
5986 return Scope.destroy() ? ESR : ESR_Failed;
5987 if (!IS->getElse())
5988 return ESR_CaseNotFound;
5989
5990 ESR = EvaluateStmt(Result, Info, S: IS->getElse(), Case);
5991 if (ESR == ESR_Failed)
5992 return ESR;
5993 if (ESR != ESR_CaseNotFound)
5994 return Scope.destroy() ? ESR : ESR_Failed;
5995 return ESR_CaseNotFound;
5996 }
5997
5998 case Stmt::WhileStmtClass: {
5999 EvalStmtResult ESR =
6000 EvaluateLoopBody(Result, Info, Body: cast<WhileStmt>(Val: S)->getBody(), Case);
6001 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: S, /*Scopes=*/{}, ESR))
6002 return ESR;
6003 if (ESR != ESR_Continue)
6004 return ESR;
6005 break;
6006 }
6007
6008 case Stmt::ForStmtClass: {
6009 const ForStmt *FS = cast<ForStmt>(Val: S);
6010 BlockScopeRAII Scope(Info);
6011
6012 // Step into the init statement in case it brings an (uninitialized)
6013 // variable into scope.
6014 if (const Stmt *Init = FS->getInit()) {
6015 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
6016 if (ESR != ESR_CaseNotFound) {
6017 assert(ESR != ESR_Succeeded);
6018 return ESR;
6019 }
6020 }
6021
6022 EvalStmtResult ESR =
6023 EvaluateLoopBody(Result, Info, Body: FS->getBody(), Case);
6024 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, /*Scopes=*/{}, ESR))
6025 return ESR;
6026 if (ESR != ESR_Continue)
6027 return ESR;
6028 if (const auto *Inc = FS->getInc()) {
6029 if (Inc->isValueDependent()) {
6030 if (!EvaluateDependentExpr(E: Inc, Info))
6031 return ESR_Failed;
6032 } else {
6033 FullExpressionRAII IncScope(Info);
6034 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6035 return ESR_Failed;
6036 }
6037 }
6038 break;
6039 }
6040
6041 case Stmt::DeclStmtClass: {
6042 // Start the lifetime of any uninitialized variables we encounter. They
6043 // might be used by the selected branch of the switch.
6044 const DeclStmt *DS = cast<DeclStmt>(Val: S);
6045 for (const auto *D : DS->decls()) {
6046 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
6047 if (!CheckLocalVariableDeclaration(Info, VD))
6048 return ESR_Failed;
6049 if (VD->hasLocalStorage() && !VD->getInit())
6050 if (!EvaluateVarDecl(Info, VD))
6051 return ESR_Failed;
6052 // FIXME: If the variable has initialization that can't be jumped
6053 // over, bail out of any immediately-surrounding compound-statement
6054 // too. There can't be any case labels here.
6055 }
6056 }
6057 return ESR_CaseNotFound;
6058 }
6059
6060 default:
6061 return ESR_CaseNotFound;
6062 }
6063 }
6064
6065 switch (S->getStmtClass()) {
6066 default:
6067 if (const Expr *E = dyn_cast<Expr>(Val: S)) {
6068 if (E->isValueDependent()) {
6069 if (!EvaluateDependentExpr(E, Info))
6070 return ESR_Failed;
6071 } else {
6072 // Don't bother evaluating beyond an expression-statement which couldn't
6073 // be evaluated.
6074 // FIXME: Do we need the FullExpressionRAII object here?
6075 // VisitExprWithCleanups should create one when necessary.
6076 FullExpressionRAII Scope(Info);
6077 if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy())
6078 return ESR_Failed;
6079 }
6080 return ESR_Succeeded;
6081 }
6082
6083 Info.FFDiag(Loc: S->getBeginLoc()) << S->getSourceRange();
6084 return ESR_Failed;
6085
6086 case Stmt::NullStmtClass:
6087 return ESR_Succeeded;
6088
6089 case Stmt::DeclStmtClass: {
6090 const DeclStmt *DS = cast<DeclStmt>(Val: S);
6091 for (const auto *D : DS->decls()) {
6092 const VarDecl *VD = dyn_cast_or_null<VarDecl>(Val: D);
6093 if (VD && !CheckLocalVariableDeclaration(Info, VD))
6094 return ESR_Failed;
6095
6096 if (const auto *ESD = dyn_cast<CXXExpansionStmtDecl>(Val: D)) {
6097 assert(ESD->getInstantiations() && "not expanded?");
6098 return EvaluateStmt(Result, Info, S: ESD->getInstantiations(), Case);
6099 }
6100
6101 // Each declaration initialization is its own full-expression.
6102 FullExpressionRAII Scope(Info);
6103 if (!EvaluateDecl(Info, D, /*EvaluateConditionDecl=*/true) &&
6104 !Info.noteFailure())
6105 return ESR_Failed;
6106 if (!Scope.destroy())
6107 return ESR_Failed;
6108 }
6109 return ESR_Succeeded;
6110 }
6111
6112 case Stmt::ReturnStmtClass: {
6113 const Expr *RetExpr = cast<ReturnStmt>(Val: S)->getRetValue();
6114 FullExpressionRAII Scope(Info);
6115 if (RetExpr && RetExpr->isValueDependent()) {
6116 EvaluateDependentExpr(E: RetExpr, Info);
6117 // We know we returned, but we don't know what the value is.
6118 return ESR_Failed;
6119 }
6120 if (RetExpr &&
6121 !(Result.Slot
6122 ? EvaluateInPlace(Result&: Result.Value, Info, This: *Result.Slot, E: RetExpr)
6123 : Evaluate(Result&: Result.Value, Info, E: RetExpr)))
6124 return ESR_Failed;
6125 return Scope.destroy() ? ESR_Returned : ESR_Failed;
6126 }
6127
6128 case Stmt::CompoundStmtClass: {
6129 BlockScopeRAII Scope(Info);
6130
6131 const CompoundStmt *CS = cast<CompoundStmt>(Val: S);
6132 for (const auto *BI : CS->body()) {
6133 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: BI, Case);
6134 if (ESR == ESR_Succeeded)
6135 Case = nullptr;
6136 else if (ESR != ESR_CaseNotFound) {
6137 if (ESR != ESR_Failed && !Scope.destroy())
6138 return ESR_Failed;
6139 return ESR;
6140 }
6141 }
6142 if (Case)
6143 return ESR_CaseNotFound;
6144 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6145 }
6146
6147 case Stmt::IfStmtClass: {
6148 const IfStmt *IS = cast<IfStmt>(Val: S);
6149
6150 // Evaluate the condition, as either a var decl or as an expression.
6151 BlockScopeRAII Scope(Info);
6152 if (const Stmt *Init = IS->getInit()) {
6153 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
6154 if (ESR != ESR_Succeeded) {
6155 if (ESR != ESR_Failed && !Scope.destroy())
6156 return ESR_Failed;
6157 return ESR;
6158 }
6159 }
6160 bool Cond;
6161 if (IS->isConsteval()) {
6162 Cond = IS->isNonNegatedConsteval();
6163 // If we are not in a constant context, if consteval should not evaluate
6164 // to true.
6165 if (!Info.InConstantContext)
6166 Cond = !Cond;
6167 } else if (!EvaluateCond(Info, CondDecl: IS->getConditionVariable(), Cond: IS->getCond(),
6168 Result&: Cond))
6169 return ESR_Failed;
6170
6171 if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) {
6172 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SubStmt);
6173 if (ESR != ESR_Succeeded) {
6174 if (ESR != ESR_Failed && !Scope.destroy())
6175 return ESR_Failed;
6176 return ESR;
6177 }
6178 }
6179 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6180 }
6181
6182 case Stmt::WhileStmtClass: {
6183 const WhileStmt *WS = cast<WhileStmt>(Val: S);
6184 while (true) {
6185 BlockScopeRAII Scope(Info);
6186 bool Continue;
6187 if (!EvaluateCond(Info, CondDecl: WS->getConditionVariable(), Cond: WS->getCond(),
6188 Result&: Continue))
6189 return ESR_Failed;
6190 if (!Continue)
6191 break;
6192
6193 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: WS->getBody());
6194 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: WS, Scopes: &Scope, ESR))
6195 return ESR;
6196
6197 if (ESR != ESR_Continue) {
6198 if (ESR != ESR_Failed && !Scope.destroy())
6199 return ESR_Failed;
6200 return ESR;
6201 }
6202 if (!Scope.destroy())
6203 return ESR_Failed;
6204 }
6205 return ESR_Succeeded;
6206 }
6207
6208 case Stmt::DoStmtClass: {
6209 const DoStmt *DS = cast<DoStmt>(Val: S);
6210 bool Continue;
6211 do {
6212 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: DS->getBody(), Case);
6213 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: DS, /*Scopes=*/{}, ESR))
6214 return ESR;
6215 if (ESR != ESR_Continue)
6216 return ESR;
6217 Case = nullptr;
6218
6219 if (DS->getCond()->isValueDependent()) {
6220 EvaluateDependentExpr(E: DS->getCond(), Info);
6221 // Bailout as we don't know whether to keep going or terminate the loop.
6222 return ESR_Failed;
6223 }
6224 FullExpressionRAII CondScope(Info);
6225 if (!EvaluateAsBooleanCondition(E: DS->getCond(), Result&: Continue, Info) ||
6226 !CondScope.destroy())
6227 return ESR_Failed;
6228 } while (Continue);
6229 return ESR_Succeeded;
6230 }
6231
6232 case Stmt::ForStmtClass: {
6233 const ForStmt *FS = cast<ForStmt>(Val: S);
6234 BlockScopeRAII ForScope(Info);
6235 if (FS->getInit()) {
6236 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6237 if (ESR != ESR_Succeeded) {
6238 if (ESR != ESR_Failed && !ForScope.destroy())
6239 return ESR_Failed;
6240 return ESR;
6241 }
6242 }
6243 while (true) {
6244 BlockScopeRAII IterScope(Info);
6245 bool Continue = true;
6246 if (FS->getCond() && !EvaluateCond(Info, CondDecl: FS->getConditionVariable(),
6247 Cond: FS->getCond(), Result&: Continue))
6248 return ESR_Failed;
6249
6250 if (!Continue) {
6251 if (!IterScope.destroy())
6252 return ESR_Failed;
6253 break;
6254 }
6255
6256 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6257 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&IterScope, &ForScope}, ESR))
6258 return ESR;
6259 if (ESR != ESR_Continue) {
6260 if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy()))
6261 return ESR_Failed;
6262 return ESR;
6263 }
6264
6265 if (const auto *Inc = FS->getInc()) {
6266 if (Inc->isValueDependent()) {
6267 if (!EvaluateDependentExpr(E: Inc, Info))
6268 return ESR_Failed;
6269 } else {
6270 FullExpressionRAII IncScope(Info);
6271 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6272 return ESR_Failed;
6273 }
6274 }
6275
6276 if (!IterScope.destroy())
6277 return ESR_Failed;
6278 }
6279 return ForScope.destroy() ? ESR_Succeeded : ESR_Failed;
6280 }
6281
6282 case Stmt::CXXForRangeStmtClass: {
6283 const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(Val: S);
6284 BlockScopeRAII Scope(Info);
6285
6286 // Evaluate the init-statement if present.
6287 if (FS->getInit()) {
6288 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6289 if (ESR != ESR_Succeeded) {
6290 if (ESR != ESR_Failed && !Scope.destroy())
6291 return ESR_Failed;
6292 return ESR;
6293 }
6294 }
6295
6296 // Initialize the __range variable.
6297 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getRangeStmt());
6298 if (ESR != ESR_Succeeded) {
6299 if (ESR != ESR_Failed && !Scope.destroy())
6300 return ESR_Failed;
6301 return ESR;
6302 }
6303
6304 // In error-recovery cases it's possible to get here even if we failed to
6305 // synthesize the __begin and __end variables.
6306 if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond())
6307 return ESR_Failed;
6308
6309 // Create the __begin and __end iterators.
6310 ESR = EvaluateStmt(Result, Info, S: FS->getBeginStmt());
6311 if (ESR != ESR_Succeeded) {
6312 if (ESR != ESR_Failed && !Scope.destroy())
6313 return ESR_Failed;
6314 return ESR;
6315 }
6316 ESR = EvaluateStmt(Result, Info, S: FS->getEndStmt());
6317 if (ESR != ESR_Succeeded) {
6318 if (ESR != ESR_Failed && !Scope.destroy())
6319 return ESR_Failed;
6320 return ESR;
6321 }
6322
6323 while (true) {
6324 // Condition: __begin != __end.
6325 {
6326 if (FS->getCond()->isValueDependent()) {
6327 EvaluateDependentExpr(E: FS->getCond(), Info);
6328 // We don't know whether to keep going or terminate the loop.
6329 return ESR_Failed;
6330 }
6331 bool Continue = true;
6332 FullExpressionRAII CondExpr(Info);
6333 if (!EvaluateAsBooleanCondition(E: FS->getCond(), Result&: Continue, Info))
6334 return ESR_Failed;
6335 if (!Continue)
6336 break;
6337 }
6338
6339 // User's variable declaration, initialized by *__begin.
6340 BlockScopeRAII InnerScope(Info);
6341 ESR = EvaluateStmt(Result, Info, S: FS->getLoopVarStmt());
6342 if (ESR != ESR_Succeeded) {
6343 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6344 return ESR_Failed;
6345 return ESR;
6346 }
6347
6348 // Loop body.
6349 ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6350 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&InnerScope, &Scope}, ESR))
6351 return ESR;
6352 if (ESR != ESR_Continue) {
6353 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6354 return ESR_Failed;
6355 return ESR;
6356 }
6357 if (FS->getInc()->isValueDependent()) {
6358 if (!EvaluateDependentExpr(E: FS->getInc(), Info))
6359 return ESR_Failed;
6360 } else {
6361 // Increment: ++__begin
6362 if (!EvaluateIgnoredValue(Info, E: FS->getInc()))
6363 return ESR_Failed;
6364 }
6365
6366 if (!InnerScope.destroy())
6367 return ESR_Failed;
6368 }
6369
6370 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6371 }
6372
6373 case Stmt::CXXExpansionStmtInstantiationClass: {
6374 BlockScopeRAII Scope(Info);
6375 const auto *Expansion = cast<CXXExpansionStmtInstantiation>(Val: S);
6376 for (const Stmt *PreambleStmt : Expansion->getPreambleStmts()) {
6377 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: PreambleStmt);
6378 if (ESR != ESR_Succeeded) {
6379 if (ESR != ESR_Failed && !Scope.destroy())
6380 return ESR_Failed;
6381 return ESR;
6382 }
6383 }
6384
6385 // No need to push an extra scope for these since they're already
6386 // CompoundStmts.
6387 EvalStmtResult ESR = ESR_Succeeded;
6388 for (const Stmt *Instantiation : Expansion->getInstantiations()) {
6389 ESR = EvaluateStmt(Result, Info, S: Instantiation);
6390 if (ESR == ESR_Failed ||
6391 ShouldPropagateBreakContinue(Info, LoopOrSwitch: Expansion, Scopes: &Scope, ESR))
6392 return ESR;
6393 if (ESR != ESR_Continue) {
6394 // Succeeded here actually means we encountered a 'break'.
6395 assert(ESR == ESR_Succeeded || ESR == ESR_Returned);
6396 break;
6397 }
6398 }
6399
6400 // Map Continue back to Succeeded if we fell off the end of the loop.
6401 if (ESR == ESR_Continue)
6402 ESR = ESR_Succeeded;
6403
6404 return Scope.destroy() ? ESR : ESR_Failed;
6405 }
6406
6407 case Stmt::SwitchStmtClass:
6408 return EvaluateSwitch(Result, Info, SS: cast<SwitchStmt>(Val: S));
6409
6410 case Stmt::ContinueStmtClass:
6411 case Stmt::BreakStmtClass: {
6412 auto *B = cast<LoopControlStmt>(Val: S);
6413 Info.BreakContinueStack.push_back(Elt: B->getNamedLoopOrSwitch());
6414 return isa<ContinueStmt>(Val: S) ? ESR_Continue : ESR_Break;
6415 }
6416
6417 case Stmt::LabelStmtClass:
6418 return EvaluateStmt(Result, Info, S: cast<LabelStmt>(Val: S)->getSubStmt(), Case);
6419
6420 case Stmt::AttributedStmtClass: {
6421 const auto *AS = cast<AttributedStmt>(Val: S);
6422 const auto *SS = AS->getSubStmt();
6423 MSConstexprContextRAII ConstexprContext(
6424 *Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(container: AS->getAttrs()) &&
6425 isa<ReturnStmt>(Val: SS));
6426
6427 auto LO = Info.Ctx.getLangOpts();
6428 if (LO.CXXAssumptions && !LO.MSVCCompat) {
6429 for (auto *Attr : AS->getAttrs()) {
6430 auto *AA = dyn_cast<CXXAssumeAttr>(Val: Attr);
6431 if (!AA)
6432 continue;
6433
6434 auto *Assumption = AA->getAssumption();
6435 if (Assumption->isValueDependent())
6436 return ESR_Failed;
6437
6438 if (Assumption->HasSideEffects(Ctx: Info.Ctx))
6439 continue;
6440
6441 bool Value;
6442 if (!EvaluateAsBooleanCondition(E: Assumption, Result&: Value, Info))
6443 return ESR_Failed;
6444 if (!Value) {
6445 Info.CCEDiag(Loc: Assumption->getExprLoc(),
6446 DiagId: diag::note_constexpr_assumption_failed);
6447 return ESR_Failed;
6448 }
6449 }
6450 }
6451
6452 return EvaluateStmt(Result, Info, S: SS, Case);
6453 }
6454
6455 case Stmt::CaseStmtClass:
6456 case Stmt::DefaultStmtClass:
6457 return EvaluateStmt(Result, Info, S: cast<SwitchCase>(Val: S)->getSubStmt(), Case);
6458 case Stmt::CXXTryStmtClass:
6459 // Evaluate try blocks by evaluating all sub statements.
6460 return EvaluateStmt(Result, Info, S: cast<CXXTryStmt>(Val: S)->getTryBlock(), Case);
6461 }
6462}
6463
6464/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
6465/// default constructor. If so, we'll fold it whether or not it's marked as
6466/// constexpr. If it is marked as constexpr, we will never implicitly define it,
6467/// so we need special handling.
6468static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
6469 const CXXConstructorDecl *CD,
6470 bool IsValueInitialization) {
6471 if (!CD->isTrivial() || !CD->isDefaultConstructor())
6472 return false;
6473
6474 // Value-initialization does not call a trivial default constructor, so such a
6475 // call is a core constant expression whether or not the constructor is
6476 // constexpr.
6477 if (!CD->isConstexpr() && !IsValueInitialization) {
6478 if (Info.getLangOpts().CPlusPlus11) {
6479 // FIXME: If DiagDecl is an implicitly-declared special member function,
6480 // we should be much more explicit about why it's not constexpr.
6481 Info.CCEDiag(Loc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6482 << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
6483 Info.Note(Loc: CD->getLocation(), DiagId: diag::note_declared_at);
6484 } else {
6485 Info.CCEDiag(Loc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6486 }
6487 }
6488 return true;
6489}
6490
6491/// CheckConstexprFunction - Check that a function can be called in a constant
6492/// expression.
6493static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
6494 const FunctionDecl *Declaration,
6495 const FunctionDecl *Definition,
6496 const Stmt *Body) {
6497 // Potential constant expressions can contain calls to declared, but not yet
6498 // defined, constexpr functions.
6499 if (Info.checkingPotentialConstantExpression() && !Definition &&
6500 Declaration->isConstexpr())
6501 return false;
6502
6503 // Bail out if the function declaration itself is invalid. We will
6504 // have produced a relevant diagnostic while parsing it, so just
6505 // note the problematic sub-expression.
6506 if (Declaration->isInvalidDecl()) {
6507 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6508 return false;
6509 }
6510
6511 // DR1872: An instantiated virtual constexpr function can't be called in a
6512 // constant expression (prior to C++20). We can still constant-fold such a
6513 // call.
6514 if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Val: Declaration) &&
6515 cast<CXXMethodDecl>(Val: Declaration)->isVirtual())
6516 Info.CCEDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_call);
6517
6518 if (Definition && Definition->isInvalidDecl()) {
6519 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6520 return false;
6521 }
6522
6523 // Can we evaluate this function call?
6524 if (Definition && Body &&
6525 (Definition->isConstexpr() || (Info.CurrentCall->CanEvalMSConstexpr &&
6526 Definition->hasAttr<MSConstexprAttr>())))
6527 return true;
6528
6529 const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
6530 // Special note for the assert() macro, as the normal error message falsely
6531 // implies we cannot use an assertion during constant evaluation.
6532 if (CallLoc.isMacroID() && DiagDecl->getIdentifier()) {
6533 // FIXME: Instead of checking for an implementation-defined function,
6534 // check and evaluate the assert() macro.
6535 StringRef Name = DiagDecl->getName();
6536 bool AssertFailed =
6537 Name == "__assert_rtn" || Name == "__assert_fail" || Name == "_wassert";
6538 if (AssertFailed) {
6539 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_assert_failed);
6540 return false;
6541 }
6542 }
6543
6544 if (Info.getLangOpts().CPlusPlus11) {
6545 // If this function is not constexpr because it is an inherited
6546 // non-constexpr constructor, diagnose that directly.
6547 auto *CD = dyn_cast<CXXConstructorDecl>(Val: DiagDecl);
6548 if (CD && CD->isInheritingConstructor()) {
6549 auto *Inherited = CD->getInheritedConstructor().getConstructor();
6550 if (!Inherited->isConstexpr())
6551 DiagDecl = CD = Inherited;
6552 }
6553
6554 // FIXME: If DiagDecl is an implicitly-declared special member function
6555 // or an inheriting constructor, we should be much more explicit about why
6556 // it's not constexpr.
6557 if (CD && CD->isInheritingConstructor())
6558 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_inhctor, ExtraNotes: 1)
6559 << CD->getInheritedConstructor().getConstructor()->getParent();
6560 else
6561 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6562 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
6563 Info.Note(Loc: DiagDecl->getLocation(), DiagId: diag::note_declared_at);
6564 } else {
6565 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6566 }
6567 return false;
6568}
6569
6570namespace {
6571struct CheckDynamicTypeHandler {
6572 AccessKinds AccessKind;
6573 typedef bool result_type;
6574 bool failed() { return false; }
6575 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
6576 return true;
6577 }
6578 bool found(APSInt &Value, QualType SubobjType) { return true; }
6579 bool found(APFloat &Value, QualType SubobjType) { return true; }
6580};
6581} // end anonymous namespace
6582
6583/// Check that we can access the notional vptr of an object / determine its
6584/// dynamic type.
6585static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This,
6586 AccessKinds AK, bool Polymorphic) {
6587 if (This.Designator.Invalid)
6588 return false;
6589
6590 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: This, LValType: QualType());
6591
6592 if (!Obj)
6593 return false;
6594
6595 if (!Obj.Value) {
6596 // The object is not usable in constant expressions, so we can't inspect
6597 // its value to see if it's in-lifetime or what the active union members
6598 // are. We can still check for a one-past-the-end lvalue.
6599 if (This.Designator.isOnePastTheEnd() ||
6600 This.Designator.isMostDerivedAnUnsizedArray()) {
6601 Info.FFDiag(E, DiagId: This.Designator.isOnePastTheEnd()
6602 ? diag::note_constexpr_access_past_end
6603 : diag::note_constexpr_access_unsized_array)
6604 << AK;
6605 return false;
6606 } else if (Polymorphic) {
6607 // Conservatively refuse to perform a polymorphic operation if we would
6608 // not be able to read a notional 'vptr' value.
6609 if (!Info.checkingPotentialConstantExpression() ||
6610 !This.AllowConstexprUnknown) {
6611 APValue Val;
6612 This.moveInto(V&: Val);
6613 QualType StarThisType =
6614 Info.Ctx.getLValueReferenceType(T: This.Designator.getType(Ctx&: Info.Ctx));
6615 Info.FFDiag(E, DiagId: diag::note_constexpr_polymorphic_unknown_dynamic_type)
6616 << AK << Val.getAsString(Ctx: Info.Ctx, Ty: StarThisType);
6617 }
6618 return false;
6619 }
6620 return true;
6621 }
6622
6623 CheckDynamicTypeHandler Handler{.AccessKind: AK};
6624 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
6625}
6626
6627/// Check that the pointee of the 'this' pointer in a member function call is
6628/// either within its lifetime or in its period of construction or destruction.
6629static bool
6630checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E,
6631 const LValue &This,
6632 const CXXMethodDecl *NamedMember) {
6633 return checkDynamicType(
6634 Info, E, This,
6635 AK: isa<CXXDestructorDecl>(Val: NamedMember) ? AK_Destroy : AK_MemberCall, Polymorphic: false);
6636}
6637
6638struct DynamicType {
6639 /// The dynamic class type of the object.
6640 const CXXRecordDecl *Type;
6641 /// The corresponding path length in the lvalue.
6642 unsigned PathLength;
6643};
6644
6645static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator,
6646 unsigned PathLength) {
6647 assert(PathLength >= Designator.MostDerivedPathLength && PathLength <=
6648 Designator.Entries.size() && "invalid path length");
6649 return (PathLength == Designator.MostDerivedPathLength)
6650 ? Designator.MostDerivedType->getAsCXXRecordDecl()
6651 : getAsBaseClass(E: Designator.Entries[PathLength - 1]);
6652}
6653
6654/// Determine the dynamic type of an object.
6655static std::optional<DynamicType> ComputeDynamicType(EvalInfo &Info,
6656 const Expr *E,
6657 LValue &This,
6658 AccessKinds AK) {
6659 // If we don't have an lvalue denoting an object of class type, there is no
6660 // meaningful dynamic type. (We consider objects of non-class type to have no
6661 // dynamic type.)
6662 if (!checkDynamicType(Info, E, This, AK,
6663 Polymorphic: AK != AK_TypeId || This.AllowConstexprUnknown))
6664 return std::nullopt;
6665
6666 if (This.Designator.Invalid)
6667 return std::nullopt;
6668
6669 // Refuse to compute a dynamic type in the presence of virtual bases
6670 // before C++26. This shouldn't happen other than in constant-folding
6671 // situations, since literal types can't have virtual bases.
6672 const CXXRecordDecl *Class =
6673 This.Designator.MostDerivedType->getAsCXXRecordDecl();
6674 if (!Class || (!Info.getLangOpts().CPlusPlus26 && Class->getNumVBases())) {
6675 Info.FFDiag(E);
6676 return std::nullopt;
6677 }
6678
6679 // FIXME: For very deep class hierarchies, it might be beneficial to use a
6680 // binary search here instead. But the overwhelmingly common case is that
6681 // we're not in the middle of a constructor, so it probably doesn't matter
6682 // in practice.
6683 ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries;
6684 for (unsigned PathLength = This.Designator.MostDerivedPathLength;
6685 PathLength <= Path.size(); ++PathLength) {
6686 switch (Info.isEvaluatingCtorDtor(Base: This.getLValueBase(),
6687 Path: Path.slice(N: 0, M: PathLength))) {
6688 case ConstructionPhase::Bases:
6689 case ConstructionPhase::DestroyingBases:
6690 // We're constructing or destroying a base class. This is not the dynamic
6691 // type.
6692 break;
6693
6694 case ConstructionPhase::None:
6695 case ConstructionPhase::AfterBases:
6696 case ConstructionPhase::AfterFields:
6697 case ConstructionPhase::Destroying:
6698 // We've finished constructing the base classes and not yet started
6699 // destroying them again, so this is the dynamic type.
6700 return DynamicType{.Type: getBaseClassType(Designator&: This.Designator, PathLength),
6701 .PathLength: PathLength};
6702 }
6703 }
6704
6705 // CWG issue 1517: we're constructing a base class of the object described by
6706 // 'This', so that object has not yet begun its period of construction and
6707 // any polymorphic operation on it results in undefined behavior.
6708 Info.FFDiag(E);
6709 return std::nullopt;
6710}
6711
6712/// Perform virtual dispatch.
6713static const CXXMethodDecl *HandleVirtualDispatch(
6714 EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found,
6715 llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) {
6716 std::optional<DynamicType> DynType = ComputeDynamicType(
6717 Info, E, This,
6718 AK: isa<CXXDestructorDecl>(Val: Found) ? AK_Destroy : AK_MemberCall);
6719 if (!DynType)
6720 return nullptr;
6721
6722 // Find the final overrider. It must be declared in one of the classes on the
6723 // path from the dynamic type to the static type.
6724 // FIXME: If we ever allow literal types to have virtual base classes, that
6725 // won't be true.
6726 const CXXMethodDecl *Callee = Found;
6727 unsigned PathLength = DynType->PathLength;
6728 for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) {
6729 const CXXRecordDecl *Class = getBaseClassType(Designator&: This.Designator, PathLength);
6730 const CXXMethodDecl *Overrider =
6731 Found->getCorrespondingMethodDeclaredInClass(RD: Class, MayBeBase: false);
6732 if (Overrider) {
6733 Callee = Overrider;
6734 break;
6735 }
6736 }
6737
6738 // C++2a [class.abstract]p6:
6739 // the effect of making a virtual call to a pure virtual function [...] is
6740 // undefined
6741 if (Callee->isPureVirtual()) {
6742 Info.FFDiag(E, DiagId: diag::note_constexpr_pure_virtual_call, ExtraNotes: 1) << Callee;
6743 Info.Note(Loc: Callee->getLocation(), DiagId: diag::note_declared_at);
6744 return nullptr;
6745 }
6746
6747 // If necessary, walk the rest of the path to determine the sequence of
6748 // covariant adjustment steps to apply.
6749 if (!Info.Ctx.hasSameUnqualifiedType(T1: Callee->getReturnType(),
6750 T2: Found->getReturnType())) {
6751 CovariantAdjustmentPath.push_back(Elt: Callee->getReturnType());
6752 for (unsigned CovariantPathLength = PathLength + 1;
6753 CovariantPathLength != This.Designator.Entries.size();
6754 ++CovariantPathLength) {
6755 const CXXRecordDecl *NextClass =
6756 getBaseClassType(Designator&: This.Designator, PathLength: CovariantPathLength);
6757 const CXXMethodDecl *Next =
6758 Found->getCorrespondingMethodDeclaredInClass(RD: NextClass, MayBeBase: false);
6759 if (Next && !Info.Ctx.hasSameUnqualifiedType(
6760 T1: Next->getReturnType(), T2: CovariantAdjustmentPath.back()))
6761 CovariantAdjustmentPath.push_back(Elt: Next->getReturnType());
6762 }
6763 if (!Info.Ctx.hasSameUnqualifiedType(T1: Found->getReturnType(),
6764 T2: CovariantAdjustmentPath.back()))
6765 CovariantAdjustmentPath.push_back(Elt: Found->getReturnType());
6766 }
6767
6768 // Perform 'this' adjustment.
6769 if (!CastToDerivedClass(Info, E, Result&: This, TruncatedType: Callee->getParent(), TruncatedElements: PathLength))
6770 return nullptr;
6771
6772 return Callee;
6773}
6774
6775/// Perform the adjustment from a value returned by a virtual function to
6776/// a value of the statically expected type, which may be a pointer or
6777/// reference to a base class of the returned type.
6778static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E,
6779 APValue &Result,
6780 ArrayRef<QualType> Path) {
6781 assert(Result.isLValue() &&
6782 "unexpected kind of APValue for covariant return");
6783 if (Result.isNullPointer())
6784 return true;
6785
6786 LValue LVal;
6787 LVal.setFrom(Ctx: Info.Ctx, V: Result);
6788
6789 const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl();
6790 for (unsigned I = 1; I != Path.size(); ++I) {
6791 const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl();
6792 assert(OldClass && NewClass && "unexpected kind of covariant return");
6793 if (OldClass != NewClass &&
6794 !CastToBaseClass(Info, E, Result&: LVal, DerivedRD: OldClass, BaseRD: NewClass))
6795 return false;
6796 OldClass = NewClass;
6797 }
6798
6799 LVal.moveInto(V&: Result);
6800 return true;
6801}
6802
6803/// Determine whether \p Base, which is known to be a direct base class of
6804/// \p Derived, is a public base class.
6805static bool isBaseClassPublic(const CXXRecordDecl *Derived,
6806 const CXXRecordDecl *Base) {
6807 for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) {
6808 if (BaseSpec.isVirtual())
6809 continue;
6810 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6811 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6812 return BaseSpec.getAccessSpecifier() == AS_public;
6813 }
6814 for (const CXXBaseSpecifier &BaseSpec : Derived->vbases()) {
6815 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6816 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6817 return BaseSpec.getAccessSpecifier() == AS_public;
6818 }
6819
6820 llvm_unreachable("Base is not a direct base of Derived");
6821}
6822
6823/// Apply the given dynamic cast operation on the provided lvalue.
6824///
6825/// This implements the hard case of dynamic_cast, requiring a "runtime check"
6826/// to find a suitable target subobject.
6827static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E,
6828 LValue &Ptr) {
6829 // We can't do anything with a non-symbolic pointer value.
6830 SubobjectDesignator &D = Ptr.Designator;
6831 if (D.Invalid)
6832 return false;
6833
6834 // C++ [expr.dynamic.cast]p6:
6835 // If v is a null pointer value, the result is a null pointer value.
6836 if (Ptr.isNullPointer() && !E->isGLValue())
6837 return true;
6838
6839 // For all the other cases, we need the pointer to point to an object within
6840 // its lifetime / period of construction / destruction, and we need to know
6841 // its dynamic type.
6842 std::optional<DynamicType> DynType =
6843 ComputeDynamicType(Info, E, This&: Ptr, AK: AK_DynamicCast);
6844 if (!DynType)
6845 return false;
6846
6847 // C++ [expr.dynamic.cast]p7:
6848 // If T is "pointer to cv void", then the result is a pointer to the most
6849 // derived object
6850 if (E->getType()->isVoidPointerType())
6851 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength);
6852
6853 const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl();
6854 assert(C && "dynamic_cast target is not void pointer nor class");
6855 CanQualType CQT = Info.Ctx.getCanonicalTagType(TD: C);
6856
6857 auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) {
6858 // C++ [expr.dynamic.cast]p9:
6859 if (!E->isGLValue()) {
6860 // The value of a failed cast to pointer type is the null pointer value
6861 // of the required result type.
6862 Ptr.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
6863 return true;
6864 }
6865
6866 // A failed cast to reference type throws [...] std::bad_cast.
6867 unsigned DiagKind;
6868 if (!Paths && (declaresSameEntity(D1: DynType->Type, D2: C) ||
6869 DynType->Type->isDerivedFrom(Base: C)))
6870 DiagKind = 0;
6871 else if (!Paths || Paths->begin() == Paths->end())
6872 DiagKind = 1;
6873 else if (Paths->isAmbiguous(BaseType: CQT))
6874 DiagKind = 2;
6875 else {
6876 assert(Paths->front().Access != AS_public && "why did the cast fail?");
6877 DiagKind = 3;
6878 }
6879 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_cast_to_reference_failed)
6880 << DiagKind << Ptr.Designator.getType(Ctx&: Info.Ctx)
6881 << Info.Ctx.getCanonicalTagType(TD: DynType->Type)
6882 << E->getType().getUnqualifiedType();
6883 return false;
6884 };
6885
6886 // Runtime check, phase 1:
6887 // Walk from the base subobject towards the derived object looking for the
6888 // target type.
6889 for (int PathLength = Ptr.Designator.Entries.size();
6890 PathLength >= (int)DynType->PathLength; --PathLength) {
6891 const CXXRecordDecl *Class = getBaseClassType(Designator&: Ptr.Designator, PathLength);
6892 if (declaresSameEntity(D1: Class, D2: C))
6893 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: Class, TruncatedElements: PathLength);
6894 // We can only walk across public inheritance edges.
6895 if (PathLength > (int)DynType->PathLength &&
6896 !isBaseClassPublic(Derived: getBaseClassType(Designator&: Ptr.Designator, PathLength: PathLength - 1),
6897 Base: Class))
6898 return RuntimeCheckFailed(nullptr);
6899 }
6900
6901 // Runtime check, phase 2:
6902 // Search the dynamic type for an unambiguous public base of type C.
6903 CXXBasePaths Paths(/*FindAmbiguities=*/true,
6904 /*RecordPaths=*/true, /*DetectVirtual=*/false);
6905 if (DynType->Type->isDerivedFrom(Base: C, Paths) && !Paths.isAmbiguous(BaseType: CQT) &&
6906 Paths.front().Access == AS_public) {
6907 // Downcast to the dynamic type...
6908 if (!CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength))
6909 return false;
6910 // ... then upcast to the chosen base class subobject.
6911 for (CXXBasePathElement &Elem : Paths.front())
6912 if (!HandleLValueBase(Info, E, Obj&: Ptr, DerivedDecl: Elem.Class, Base: Elem.Base))
6913 return false;
6914 return true;
6915 }
6916
6917 // Otherwise, the runtime check fails.
6918 return RuntimeCheckFailed(&Paths);
6919}
6920
6921namespace {
6922struct StartLifetimeOfUnionMemberHandler {
6923 EvalInfo &Info;
6924 const Expr *LHSExpr;
6925 const FieldDecl *Field;
6926 bool DuringInit;
6927 bool Failed = false;
6928 static const AccessKinds AccessKind = AK_Assign;
6929
6930 typedef bool result_type;
6931 bool failed() { return Failed; }
6932 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
6933 // We are supposed to perform no initialization but begin the lifetime of
6934 // the object. We interpret that as meaning to do what default
6935 // initialization of the object would do if all constructors involved were
6936 // trivial:
6937 // * All base, non-variant member, and array element subobjects' lifetimes
6938 // begin
6939 // * No variant members' lifetimes begin
6940 // * All scalar subobjects whose lifetimes begin have indeterminate values
6941 assert(SubobjType->isUnionType());
6942 if (declaresSameEntity(D1: Subobj.getUnionField(), D2: Field)) {
6943 // This union member is already active. If it's also in-lifetime, there's
6944 // nothing to do.
6945 if (Subobj.getUnionValue().hasValue())
6946 return true;
6947 } else if (DuringInit) {
6948 // We're currently in the process of initializing a different union
6949 // member. If we carried on, that initialization would attempt to
6950 // store to an inactive union member, resulting in undefined behavior.
6951 Info.FFDiag(E: LHSExpr,
6952 DiagId: diag::note_constexpr_union_member_change_during_init);
6953 return false;
6954 }
6955 APValue Result;
6956 Failed = !handleDefaultInitValue(T: Field->getType(), Result);
6957 Subobj.setUnion(Field, Value: Result);
6958 return true;
6959 }
6960 bool found(APSInt &Value, QualType SubobjType) {
6961 llvm_unreachable("wrong value kind for union object");
6962 }
6963 bool found(APFloat &Value, QualType SubobjType) {
6964 llvm_unreachable("wrong value kind for union object");
6965 }
6966};
6967} // end anonymous namespace
6968
6969const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind;
6970
6971/// Handle a builtin simple-assignment or a call to a trivial assignment
6972/// operator whose left-hand side might involve a union member access. If it
6973/// does, implicitly start the lifetime of any accessed union elements per
6974/// C++20 [class.union]5.
6975static bool MaybeHandleUnionActiveMemberChange(EvalInfo &Info,
6976 const Expr *LHSExpr,
6977 const LValue &LHS) {
6978 if (LHS.InvalidBase || LHS.Designator.Invalid)
6979 return false;
6980
6981 llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths;
6982 // C++ [class.union]p5:
6983 // define the set S(E) of subexpressions of E as follows:
6984 unsigned PathLength = LHS.Designator.Entries.size();
6985 for (const Expr *E = LHSExpr; E != nullptr;) {
6986 // -- If E is of the form A.B, S(E) contains the elements of S(A)...
6987 if (auto *ME = dyn_cast<MemberExpr>(Val: E)) {
6988 auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl());
6989 // Note that we can't implicitly start the lifetime of a reference,
6990 // so we don't need to proceed any further if we reach one.
6991 if (!FD || FD->getType()->isReferenceType())
6992 break;
6993
6994 // ... and also contains A.B if B names a union member ...
6995 if (FD->getParent()->isUnion()) {
6996 // ... of a non-class, non-array type, or of a class type with a
6997 // trivial default constructor that is not deleted, or an array of
6998 // such types.
6999 auto *RD =
7000 FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
7001 if (!RD || RD->hasTrivialDefaultConstructor())
7002 UnionPathLengths.push_back(Elt: {PathLength - 1, FD});
7003 }
7004
7005 E = ME->getBase();
7006 --PathLength;
7007 assert(declaresSameEntity(FD,
7008 LHS.Designator.Entries[PathLength]
7009 .getAsBaseOrMember().getPointer()));
7010
7011 // -- If E is of the form A[B] and is interpreted as a built-in array
7012 // subscripting operator, S(E) is [S(the array operand, if any)].
7013 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) {
7014 // Step over an ArrayToPointerDecay implicit cast.
7015 auto *Base = ASE->getBase()->IgnoreImplicit();
7016 if (!Base->getType()->isArrayType())
7017 break;
7018
7019 E = Base;
7020 --PathLength;
7021
7022 } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E)) {
7023 // Step over a derived-to-base conversion.
7024 E = ICE->getSubExpr();
7025 if (ICE->getCastKind() == CK_NoOp)
7026 continue;
7027 if (ICE->getCastKind() != CK_DerivedToBase &&
7028 ICE->getCastKind() != CK_UncheckedDerivedToBase)
7029 break;
7030 // Walk path backwards as we walk up from the base to the derived class.
7031 for (const CXXBaseSpecifier *Elt : llvm::reverse(C: ICE->path())) {
7032 if (Elt->isVirtual()) {
7033 // A class with virtual base classes never has a trivial default
7034 // constructor, so S(E) is empty in this case.
7035 E = nullptr;
7036 break;
7037 }
7038
7039 --PathLength;
7040 assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(),
7041 LHS.Designator.Entries[PathLength]
7042 .getAsBaseOrMember().getPointer()));
7043 }
7044
7045 // -- Otherwise, S(E) is empty.
7046 } else {
7047 break;
7048 }
7049 }
7050
7051 // Common case: no unions' lifetimes are started.
7052 if (UnionPathLengths.empty())
7053 return true;
7054
7055 // if modification of X [would access an inactive union member], an object
7056 // of the type of X is implicitly created
7057 CompleteObject Obj =
7058 findCompleteObject(Info, E: LHSExpr, AK: AK_Assign, LVal: LHS, LValType: LHSExpr->getType());
7059 if (!Obj)
7060 return false;
7061 for (std::pair<unsigned, const FieldDecl *> LengthAndField :
7062 llvm::reverse(C&: UnionPathLengths)) {
7063 // Form a designator for the union object.
7064 SubobjectDesignator D = LHS.Designator;
7065 D.truncate(Ctx&: Info.Ctx, Base: LHS.Base, NewLength: LengthAndField.first);
7066
7067 bool DuringInit = Info.isEvaluatingCtorDtor(Base: LHS.Base, Path: D.Entries) ==
7068 ConstructionPhase::AfterBases;
7069 StartLifetimeOfUnionMemberHandler StartLifetime{
7070 .Info: Info, .LHSExpr: LHSExpr, .Field: LengthAndField.second, .DuringInit: DuringInit};
7071 if (!findSubobject(Info, E: LHSExpr, Obj, Sub: D, handler&: StartLifetime))
7072 return false;
7073 }
7074
7075 return true;
7076}
7077
7078static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg,
7079 CallRef Call, EvalInfo &Info, bool NonNull = false,
7080 APValue **EvaluatedArg = nullptr) {
7081 LValue LV;
7082 // Create the parameter slot and register its destruction. For a vararg
7083 // argument, create a temporary.
7084 // FIXME: For calling conventions that destroy parameters in the callee,
7085 // should we consider performing destruction when the function returns
7086 // instead?
7087 APValue &V = PVD ? Info.CurrentCall->createParam(Args: Call, PVD, LV)
7088 : Info.CurrentCall->createTemporary(Key: Arg, T: Arg->getType(),
7089 Scope: ScopeKind::Call, LV);
7090 if (!EvaluateInPlace(Result&: V, Info, This: LV, E: Arg))
7091 return false;
7092
7093 // Passing a null pointer to an __attribute__((nonnull)) parameter results in
7094 // undefined behavior, so is non-constant.
7095 if (NonNull && V.isLValue() && V.isNullPointer()) {
7096 Info.CCEDiag(E: Arg, DiagId: diag::note_non_null_attribute_failed);
7097 return false;
7098 }
7099
7100 if (EvaluatedArg)
7101 *EvaluatedArg = &V;
7102
7103 return true;
7104}
7105
7106/// Evaluate the arguments to a function call.
7107static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call,
7108 EvalInfo &Info, const FunctionDecl *Callee,
7109 bool RightToLeft = false,
7110 LValue *ObjectArg = nullptr) {
7111 bool Success = true;
7112 llvm::SmallBitVector ForbiddenNullArgs;
7113 if (Callee->hasAttr<NonNullAttr>()) {
7114 ForbiddenNullArgs.resize(N: Args.size());
7115 for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) {
7116 if (!Attr->args_size()) {
7117 ForbiddenNullArgs.set();
7118 break;
7119 } else
7120 for (auto Idx : Attr->args()) {
7121 unsigned ASTIdx = Idx.getASTIndex();
7122 if (ASTIdx >= Args.size())
7123 continue;
7124 ForbiddenNullArgs[ASTIdx] = true;
7125 }
7126 }
7127 }
7128 for (unsigned I = 0; I < Args.size(); I++) {
7129 unsigned Idx = RightToLeft ? Args.size() - I - 1 : I;
7130 const ParmVarDecl *PVD =
7131 Idx < Callee->getNumParams() ? Callee->getParamDecl(i: Idx) : nullptr;
7132 bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx];
7133 APValue *That = nullptr;
7134 if (!EvaluateCallArg(PVD, Arg: Args[Idx], Call, Info, NonNull, EvaluatedArg: &That)) {
7135 // If we're checking for a potential constant expression, evaluate all
7136 // initializers even if some of them fail.
7137 if (!Info.noteFailure())
7138 return false;
7139 Success = false;
7140 }
7141 if (PVD && PVD->isExplicitObjectParameter() && That && That->isLValue())
7142 ObjectArg->setFrom(Ctx: Info.Ctx, V: *That);
7143 }
7144 return Success;
7145}
7146
7147/// Perform a trivial copy from Param, which is the parameter of a copy or move
7148/// constructor or assignment operator.
7149static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param,
7150 const Expr *E, APValue &Result,
7151 bool CopyObjectRepresentation) {
7152 // Find the reference argument.
7153 CallStackFrame *Frame = Info.CurrentCall;
7154 APValue *RefValue = Info.getParamSlot(Call: Frame->Arguments, PVD: Param);
7155 if (!RefValue) {
7156 Info.FFDiag(E);
7157 return false;
7158 }
7159
7160 // Copy out the contents of the RHS object.
7161 LValue RefLValue;
7162 RefLValue.setFrom(Ctx: Info.Ctx, V: *RefValue);
7163 return handleLValueToRValueConversion(
7164 Info, Conv: E, Type: Param->getType().getNonReferenceType(), LVal: RefLValue, RVal&: Result,
7165 WantObjectRepresentation: CopyObjectRepresentation);
7166}
7167
7168/// Evaluate a function call.
7169static bool HandleFunctionCall(SourceLocation CallLoc,
7170 const FunctionDecl *Callee,
7171 const LValue *ObjectArg, const Expr *E,
7172 ArrayRef<const Expr *> Args, CallRef Call,
7173 const Stmt *Body, EvalInfo &Info,
7174 APValue &Result, const LValue *ResultSlot) {
7175 if (!Info.CheckCallLimit(Loc: CallLoc))
7176 return false;
7177
7178 CallStackFrame Frame(Info, E->getSourceRange(), Callee, ObjectArg, E, Call);
7179
7180 // For a trivial copy or move assignment, perform an APValue copy. This is
7181 // essential for unions, where the operations performed by the assignment
7182 // operator cannot be represented as statements.
7183 //
7184 // Skip this for non-union classes with no fields; in that case, the defaulted
7185 // copy/move does not actually read the object.
7186 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: Callee);
7187
7188 auto IsTrivialMemoryOperation = [&](const CXXMethodDecl *MD) {
7189 if (!MD || !MD->isDefaulted())
7190 return false;
7191 if (!MD->isCopyAssignmentOperator() && !MD->isMoveAssignmentOperator())
7192 return false;
7193 return MD->getParent()->isUnion() ||
7194 (MD->isTrivial() &&
7195 isReadByLvalueToRvalueConversion(RD: MD->getParent()));
7196 };
7197
7198 if (IsTrivialMemoryOperation(MD)) {
7199 unsigned ExplicitOffset = MD->isExplicitObjectMemberFunction() ? 1 : 0;
7200 assert(ObjectArg);
7201 APValue RHSValue;
7202 if (!handleTrivialCopy(Info, Param: MD->getParamDecl(i: 0), E: Args[0], Result&: RHSValue,
7203 CopyObjectRepresentation: MD->getParent()->isUnion()))
7204 return false;
7205
7206 LValue Obj;
7207 if (!handleAssignment(Info, E: Args[ExplicitOffset], LVal: *ObjectArg,
7208 LValType: MD->getFunctionObjectParameterReferenceType(),
7209 Val&: RHSValue))
7210 return false;
7211 ObjectArg->moveInto(V&: Result);
7212 return true;
7213 } else if (MD && isLambdaCallOperator(MD)) {
7214 // We're in a lambda; determine the lambda capture field maps unless we're
7215 // just constexpr checking a lambda's call operator. constexpr checking is
7216 // done before the captures have been added to the closure object (unless
7217 // we're inferring constexpr-ness), so we don't have access to them in this
7218 // case. But since we don't need the captures to constexpr check, we can
7219 // just ignore them.
7220 if (!Info.checkingPotentialConstantExpression())
7221 MD->getParent()->getCaptureFields(Captures&: Frame.LambdaCaptureFields,
7222 ThisCapture&: Frame.LambdaThisCaptureField);
7223 }
7224
7225 StmtResult Ret = {.Value: Result, .Slot: ResultSlot};
7226 EvalStmtResult ESR = EvaluateStmt(Result&: Ret, Info, S: Body);
7227 if (ESR == ESR_Succeeded) {
7228 if (Callee->getReturnType()->isVoidType())
7229 return true;
7230 Info.FFDiag(Loc: Callee->getEndLoc(), DiagId: diag::note_constexpr_no_return);
7231 }
7232 return ESR == ESR_Returned;
7233}
7234
7235static bool HandleConstructorCall(const Expr *E, const LValue &This,
7236 CallRef Call,
7237 const CXXConstructorDecl *Definition,
7238 EvalInfo &Info, APValue &Result,
7239 bool IsCompleteClass = true);
7240
7241static bool HandleConstructorCall(const Expr *E, const LValue &This,
7242 ArrayRef<const Expr *> Args,
7243 const CXXConstructorDecl *Definition,
7244 EvalInfo &Info, APValue &Result,
7245 bool IsCompleteClass = true) {
7246 CallScopeRAII CallScope(Info);
7247 CallRef Call = Info.CurrentCall->createCall(Callee: Definition);
7248 if (!EvaluateArgs(Args, Call, Info, Callee: Definition))
7249 return false;
7250
7251 return HandleConstructorCall(E, This, Call, Definition, Info, Result,
7252 IsCompleteClass) &&
7253 CallScope.destroy();
7254}
7255
7256/// Evaluate a constructor call.
7257static bool HandleConstructorCall(const Expr *E, const LValue &This,
7258 CallRef Call,
7259 const CXXConstructorDecl *Definition,
7260 EvalInfo &Info, APValue &Result,
7261 bool IsCompleteClass) {
7262
7263 SourceLocation CallLoc = E->getExprLoc();
7264 if (!Info.CheckCallLimit(Loc: CallLoc))
7265 return false;
7266
7267 const CXXRecordDecl *RD = Definition->getParent();
7268 if (!Info.getLangOpts().CPlusPlus26 && RD->getNumVBases()) {
7269 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_base) << RD;
7270 return false;
7271 }
7272
7273 EvalInfo::EvaluatingConstructorRAII EvalObj(
7274 Info,
7275 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
7276 RD->getNumBases());
7277 CallStackFrame Frame(Info, E->getSourceRange(), Definition, &This, E, Call);
7278
7279 // FIXME: Creating an APValue just to hold a nonexistent return value is
7280 // wasteful.
7281 APValue RetVal;
7282 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7283
7284 // If it's a delegating constructor, delegate.
7285 if (Definition->isDelegatingConstructor()) {
7286 CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
7287 if ((*I)->getInit()->isValueDependent()) {
7288 if (!EvaluateDependentExpr(E: (*I)->getInit(), Info))
7289 return false;
7290 } else {
7291 FullExpressionRAII InitScope(Info);
7292 if (!EvaluateInPlace(Result, Info, This, E: (*I)->getInit()) ||
7293 !InitScope.destroy())
7294 return false;
7295 }
7296 return EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed;
7297 }
7298
7299 // For a trivial copy or move constructor, perform an APValue copy. This is
7300 // essential for unions (or classes with anonymous union members), where the
7301 // operations performed by the constructor cannot be represented by
7302 // ctor-initializers.
7303 //
7304 // Skip this for empty non-union classes; we should not perform an
7305 // lvalue-to-rvalue conversion on them because their copy constructor does not
7306 // actually read them.
7307 if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() &&
7308 (Definition->getParent()->isUnion() ||
7309 (Definition->isTrivial() &&
7310 isReadByLvalueToRvalueConversion(RD: Definition->getParent())))) {
7311 return handleTrivialCopy(Info, Param: Definition->getParamDecl(i: 0), E, Result,
7312 CopyObjectRepresentation: Definition->getParent()->isUnion());
7313 }
7314
7315 // Reserve space for the struct members.
7316 if (!Result.hasValue()) {
7317 if (!RD->isUnion()) {
7318 unsigned NonVirtualBases = countNonVirtualBases(RD);
7319 Result = APValue(APValue::UninitStruct(), NonVirtualBases,
7320 RD->getNumFields(), RD->getNumVBases());
7321 } else
7322 // A union starts with no active member.
7323 Result = APValue((const FieldDecl*)nullptr);
7324 }
7325
7326 if (RD->isInvalidDecl()) return false;
7327 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7328
7329 // A scope for temporaries lifetime-extended by reference members.
7330 BlockScopeRAII LifetimeExtendedScope(Info);
7331
7332 bool Success = true;
7333 unsigned BasesSeen = 0;
7334 unsigned VirtualBasesSeen = 0;
7335 unsigned NonVirtualBases = countNonVirtualBases(RD);
7336
7337 CXXRecordDecl::field_iterator FieldIt = RD->field_begin();
7338 auto SkipToField = [&](FieldDecl *FD, bool Indirect) {
7339 // We might be initializing the same field again if this is an indirect
7340 // field initialization.
7341 if (FieldIt == RD->field_end() ||
7342 FieldIt->getFieldIndex() > FD->getFieldIndex()) {
7343 assert(Indirect && "fields out of order?");
7344 return;
7345 }
7346
7347 // Default-initialize any fields with no explicit initializer.
7348 for (; !declaresSameEntity(D1: *FieldIt, D2: FD); ++FieldIt) {
7349 assert(FieldIt != RD->field_end() && "missing field?");
7350 if (!FieldIt->isUnnamedBitField())
7351 Success &= handleDefaultInitValue(
7352 T: FieldIt->getType(),
7353 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7354 }
7355 ++FieldIt;
7356 };
7357 for (const auto *I : Definition->inits()) {
7358 LValue Subobject = This;
7359 LValue SubobjectParent = This;
7360 APValue *Value = &Result;
7361
7362 // Determine the subobject to initialize.
7363 FieldDecl *FD = nullptr;
7364 if (I->isBaseInitializer()) {
7365 QualType BaseType(I->getBaseClass(), 0);
7366 if (I->isBaseVirtual()) {
7367 if (This.pointsToCompleteClass(D: RD)) {
7368 if (!HandleLValueDirectVirtualBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7369 Base: BaseType->getAsCXXRecordDecl(),
7370 RL: &Layout))
7371 return false;
7372 Value = &Result.getStructVirtualBase(i: VirtualBasesSeen++);
7373 } else {
7374 continue;
7375 }
7376
7377 } else {
7378 if (!HandleLValueDirectBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7379 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7380 return false;
7381 Value = &Result.getStructBase(i: BasesSeen++);
7382 }
7383 } else if ((FD = I->getMember())) {
7384 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD, RL: &Layout))
7385 return false;
7386 if (RD->isUnion()) {
7387 Result = APValue(FD);
7388 Value = &Result.getUnionValue();
7389 } else {
7390 SkipToField(FD, false);
7391 Value = &Result.getStructField(i: FD->getFieldIndex());
7392 }
7393 } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) {
7394 // Walk the indirect field decl's chain to find the object to initialize,
7395 // and make sure we've initialized every step along it.
7396 auto IndirectFieldChain = IFD->chain();
7397 for (auto *C : IndirectFieldChain) {
7398 FD = cast<FieldDecl>(Val: C);
7399 CXXRecordDecl *CD = cast<CXXRecordDecl>(Val: FD->getParent());
7400 // Switch the union field if it differs. This happens if we had
7401 // preceding zero-initialization, and we're now initializing a union
7402 // subobject other than the first.
7403 // FIXME: In this case, the values of the other subobjects are
7404 // specified, since zero-initialization sets all padding bits to zero.
7405 if (!Value->hasValue() ||
7406 (Value->isUnion() &&
7407 !declaresSameEntity(D1: Value->getUnionField(), D2: FD))) {
7408 if (CD->isUnion())
7409 *Value = APValue(FD);
7410 else
7411 // FIXME: This immediately starts the lifetime of all members of
7412 // an anonymous struct. It would be preferable to strictly start
7413 // member lifetime in initialization order.
7414 Success &= handleDefaultInitValue(T: Info.Ctx.getCanonicalTagType(TD: CD),
7415 Result&: *Value);
7416 }
7417 // Store Subobject as its parent before updating it for the last element
7418 // in the chain.
7419 if (C == IndirectFieldChain.back())
7420 SubobjectParent = Subobject;
7421 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD))
7422 return false;
7423 if (CD->isUnion())
7424 Value = &Value->getUnionValue();
7425 else {
7426 if (C == IndirectFieldChain.front() && !RD->isUnion())
7427 SkipToField(FD, true);
7428 Value = &Value->getStructField(i: FD->getFieldIndex());
7429 }
7430 }
7431 } else {
7432 llvm_unreachable("unknown base initializer kind");
7433 }
7434
7435 // Need to override This for implicit field initializers as in this case
7436 // This refers to innermost anonymous struct/union containing initializer,
7437 // not to currently constructed class.
7438 const Expr *Init = I->getInit();
7439 if (Init->isValueDependent()) {
7440 if (!EvaluateDependentExpr(E: Init, Info))
7441 return false;
7442 } else {
7443 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent,
7444 isa<CXXDefaultInitExpr>(Val: Init));
7445 FullExpressionRAII InitScope(Info);
7446 if (FD && FD->getType()->isReferenceType() &&
7447 !FD->getType()->isFunctionReferenceType()) {
7448 LValue Result;
7449 if (!EvaluateInitForDeclOfReferenceType(Info, D: FD, Init, Result,
7450 Val&: *Value)) {
7451 if (!Info.noteFailure())
7452 return false;
7453 Success = false;
7454 }
7455 } else if (!EvaluateInPlace(Result&: *Value, Info, This: Subobject, E: Init) ||
7456 (FD && FD->isBitField() &&
7457 !truncateBitfieldValue(Info, E: Init, Value&: *Value, FD))) {
7458 // If we're checking for a potential constant expression, evaluate all
7459 // initializers even if some of them fail.
7460 if (!Info.noteFailure())
7461 return false;
7462 Success = false;
7463 }
7464 }
7465
7466 // This is the point at which the dynamic type of the object becomes this
7467 // class type.
7468 if (I->isBaseInitializer() && BasesSeen == NonVirtualBases)
7469 EvalObj.finishedConstructingBases();
7470 }
7471
7472 // Default-initialize any remaining fields.
7473 if (!RD->isUnion()) {
7474 for (; FieldIt != RD->field_end(); ++FieldIt) {
7475 if (!FieldIt->isUnnamedBitField())
7476 Success &= handleDefaultInitValue(
7477 T: FieldIt->getType(),
7478 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7479 }
7480 }
7481
7482 EvalObj.finishedConstructingFields();
7483
7484 return Success &&
7485 EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed &&
7486 LifetimeExtendedScope.destroy();
7487}
7488
7489static bool HandleDestructionImpl(EvalInfo &Info, SourceRange CallRange,
7490 const LValue &This, APValue &Value,
7491 QualType T, bool IsCompleteClass = true) {
7492 // Objects can only be destroyed while they're within their lifetimes.
7493 // FIXME: We have no representation for whether an object of type nullptr_t
7494 // is in its lifetime; it usually doesn't matter. Perhaps we should model it
7495 // as indeterminate instead?
7496 if (Value.isAbsent() && !T->isNullPtrType()) {
7497 APValue Printable;
7498 This.moveInto(V&: Printable);
7499 Info.FFDiag(Loc: CallRange.getBegin(),
7500 DiagId: diag::note_constexpr_destroy_out_of_lifetime)
7501 << Printable.getAsString(Ctx: Info.Ctx, Ty: Info.Ctx.getLValueReferenceType(T));
7502 return false;
7503 }
7504
7505 // Invent an expression for location purposes.
7506 // FIXME: We shouldn't need to do this.
7507 OpaqueValueExpr LocE(CallRange.getBegin(), Info.Ctx.IntTy, VK_PRValue);
7508
7509 // For arrays, destroy elements right-to-left.
7510 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) {
7511 uint64_t Size = CAT->getZExtSize();
7512 QualType ElemT = CAT->getElementType();
7513
7514 if (!CheckArraySize(Info, CAT, CallLoc: CallRange.getBegin()))
7515 return false;
7516
7517 LValue ElemLV = This;
7518 ElemLV.addArray(Info, E: &LocE, CAT);
7519 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: Size))
7520 return false;
7521
7522 // Ensure that we have actual array elements available to destroy; the
7523 // destructors might mutate the value, so we can't run them on the array
7524 // filler.
7525 if (Size && Size > Value.getArrayInitializedElts())
7526 expandArray(Array&: Value, Index: Value.getArraySize() - 1);
7527
7528 // The size of the array might have been reduced by
7529 // a placement new.
7530 for (Size = Value.getArraySize(); Size != 0; --Size) {
7531 APValue &Elem = Value.getArrayInitializedElt(I: Size - 1);
7532 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: -1) ||
7533 !HandleDestructionImpl(Info, CallRange, This: ElemLV, Value&: Elem, T: ElemT))
7534 return false;
7535 }
7536
7537 // End the lifetime of this array now.
7538 Value = APValue();
7539 return true;
7540 }
7541
7542 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
7543 if (!RD) {
7544 if (T.isDestructedType()) {
7545 Info.FFDiag(Loc: CallRange.getBegin(),
7546 DiagId: diag::note_constexpr_unsupported_destruction)
7547 << T;
7548 return false;
7549 }
7550
7551 Value = APValue();
7552 return true;
7553 }
7554
7555 if (!Info.getLangOpts().CPlusPlus26 && RD->getNumVBases()) {
7556 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_virtual_base) << RD;
7557 return false;
7558 }
7559
7560 // If an anonymous union would be destroyed, some enclosing destructor must
7561 // have been explicitly defined, and the anonymous union destruction should
7562 // have no effect.
7563 if (RD->isAnonymousStructOrUnion() && RD->isUnion()) {
7564 Value = APValue();
7565 return true;
7566 }
7567
7568 const CXXDestructorDecl *DD = RD->getDestructor();
7569 if (!DD && !RD->hasTrivialDestructor()) {
7570 Info.FFDiag(Loc: CallRange.getBegin());
7571 return false;
7572 }
7573
7574 if (!DD || DD->isTrivial()) {
7575 // A trivial destructor just ends the lifetime of the object. Check for
7576 // this case before checking for a body, because we might not bother
7577 // building a body for a trivial destructor. Note that it doesn't matter
7578 // whether the destructor is constexpr in this case; all trivial
7579 // destructors are constexpr.
7580 Value = APValue();
7581 return true;
7582 }
7583
7584 if (!Info.CheckCallLimit(Loc: CallRange.getBegin()))
7585 return false;
7586
7587 const FunctionDecl *Definition = nullptr;
7588 const Stmt *Body = DD->getBody(Definition);
7589
7590 if (!CheckConstexprFunction(Info, CallLoc: CallRange.getBegin(), Declaration: DD, Definition, Body))
7591 return false;
7592
7593 CallStackFrame Frame(Info, CallRange, Definition, &This, /*CallExpr=*/nullptr,
7594 CallRef());
7595
7596 // We're now in the period of destruction of this object.
7597 EvalInfo::EvaluatingDestructorRAII EvalObj(
7598 Info,
7599 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries});
7600 unsigned NonVirtualBases = countNonVirtualBases(RD);
7601 unsigned NumVirtualBases = RD->getNumVBases();
7602 unsigned BasesLeft = NonVirtualBases;
7603 if (!EvalObj.DidInsert) {
7604 // C++2a [class.dtor]p19:
7605 // the behavior is undefined if the destructor is invoked for an object
7606 // whose lifetime has ended
7607 // (Note that formally the lifetime ends when the period of destruction
7608 // begins, even though certain uses of the object remain valid until the
7609 // period of destruction ends.)
7610 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_double_destroy);
7611 return false;
7612 }
7613
7614 // FIXME: Creating an APValue just to hold a nonexistent return value is
7615 // wasteful.
7616 APValue RetVal;
7617 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7618 if (EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) == ESR_Failed)
7619 return false;
7620
7621 // A union destructor does not implicitly destroy its members.
7622 if (RD->isUnion())
7623 return true;
7624
7625 if (!ASTContext::hasLayout(D: RD))
7626 return false;
7627 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7628
7629 // We don't have a good way to iterate fields in reverse, so collect all the
7630 // fields first and then walk them backwards.
7631 SmallVector<FieldDecl*, 16> Fields(RD->fields());
7632 for (const FieldDecl *FD : llvm::reverse(C&: Fields)) {
7633 if (FD->isUnnamedBitField())
7634 continue;
7635
7636 LValue Subobject = This;
7637 if (!HandleLValueMember(Info, E: &LocE, LVal&: Subobject, FD, RL: &Layout))
7638 return false;
7639
7640 APValue *SubobjectValue = &Value.getStructField(i: FD->getFieldIndex());
7641 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7642 T: FD->getType()))
7643 return false;
7644 }
7645
7646 if (BasesLeft != 0 || NumVirtualBases != 0)
7647 EvalObj.startedDestroyingBases();
7648
7649 // Destroy base classes in reverse order.
7650 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->bases())) {
7651 if (Base.isVirtual())
7652 continue;
7653 --BasesLeft;
7654
7655 QualType BaseType = Base.getType();
7656 LValue Subobject = This;
7657 if (!HandleLValueDirectBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7658 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7659 return false;
7660
7661 APValue *SubobjectValue = &Value.getStructBase(i: BasesLeft);
7662 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7663 T: BaseType, /*IsCompleteClass=*/false))
7664 return false;
7665 }
7666 assert(BasesLeft == 0 && "NumBases was wrong?");
7667
7668 // Virtual bases.
7669 if (IsCompleteClass) {
7670 unsigned VirtualBasesLeft = NumVirtualBases;
7671 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->vbases())) {
7672 --VirtualBasesLeft;
7673
7674 QualType BaseType = Base.getType();
7675 LValue Subobject = This;
7676 if (!HandleLValueDirectVirtualBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7677 Base: BaseType->getAsCXXRecordDecl(),
7678 RL: &Layout))
7679 return false;
7680
7681 APValue *SubobjectValue = &Value.getStructVirtualBase(i: VirtualBasesLeft);
7682 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7683 T: BaseType, /*IsCompleteClass=*/false))
7684 return false;
7685 }
7686 assert(VirtualBasesLeft == 0 && "NumVirtualBases was wrong?");
7687 }
7688
7689 // The period of destruction ends now. The object is gone.
7690 Value = APValue();
7691 return true;
7692}
7693
7694namespace {
7695struct DestroyObjectHandler {
7696 EvalInfo &Info;
7697 const Expr *E;
7698 const LValue &This;
7699 const AccessKinds AccessKind;
7700
7701 typedef bool result_type;
7702 bool failed() { return false; }
7703 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
7704 return HandleDestructionImpl(Info, CallRange: E->getSourceRange(), This, Value&: Subobj,
7705 T: SubobjType);
7706 }
7707 bool found(APSInt &Value, QualType SubobjType) {
7708 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7709 return false;
7710 }
7711 bool found(APFloat &Value, QualType SubobjType) {
7712 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7713 return false;
7714 }
7715};
7716}
7717
7718/// Perform a destructor or pseudo-destructor call on the given object, which
7719/// might in general not be a complete object.
7720static bool HandleDestruction(EvalInfo &Info, const Expr *E,
7721 const LValue &This, QualType ThisType) {
7722 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Destroy, LVal: This, LValType: ThisType);
7723 DestroyObjectHandler Handler = {.Info: Info, .E: E, .This: This, .AccessKind: AK_Destroy};
7724 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
7725}
7726
7727/// Destroy and end the lifetime of the given complete object.
7728static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
7729 APValue::LValueBase LVBase, APValue &Value,
7730 QualType T) {
7731 // If we've had an unmodeled side-effect, we can't rely on mutable state
7732 // (such as the object we're about to destroy) being correct.
7733 if (Info.EvalStatus.HasSideEffects)
7734 return false;
7735
7736 LValue LV;
7737 LV.set(B: {LVBase});
7738 return HandleDestructionImpl(Info, CallRange: Loc, This: LV, Value, T);
7739}
7740
7741/// Perform a call to 'operator new' or to `__builtin_operator_new'.
7742static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E,
7743 LValue &Result) {
7744 if (Info.checkingPotentialConstantExpression() ||
7745 Info.SpeculativeEvaluationDepth)
7746 return false;
7747
7748 // This is permitted only within a call to std::allocator<T>::allocate.
7749 auto Caller = Info.getStdAllocatorCaller(FnName: "allocate");
7750 if (!Caller) {
7751 Info.FFDiag(Loc: E->getExprLoc(), DiagId: Info.getLangOpts().CPlusPlus20
7752 ? diag::note_constexpr_new_untyped
7753 : diag::note_constexpr_new);
7754 return false;
7755 }
7756
7757 QualType ElemType = Caller.ElemType;
7758 if (ElemType->isIncompleteType() || ElemType->isFunctionType()) {
7759 Info.FFDiag(Loc: E->getExprLoc(),
7760 DiagId: diag::note_constexpr_new_not_complete_object_type)
7761 << (ElemType->isIncompleteType() ? 0 : 1) << ElemType;
7762 return false;
7763 }
7764
7765 APSInt ByteSize;
7766 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: ByteSize, Info))
7767 return false;
7768 bool IsNothrow = false;
7769 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) {
7770 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7771 IsNothrow |= E->getType()->isNothrowT();
7772 }
7773
7774 CharUnits ElemSize;
7775 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElemType, Size&: ElemSize))
7776 return false;
7777 APInt Size, Remainder;
7778 APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity());
7779 APInt::udivrem(LHS: ByteSize, RHS: ElemSizeAP, Quotient&: Size, Remainder);
7780 if (Remainder != 0) {
7781 // This likely indicates a bug in the implementation of 'std::allocator'.
7782 Info.FFDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_operator_new_bad_size)
7783 << ByteSize << APSInt(ElemSizeAP, true) << ElemType;
7784 return false;
7785 }
7786
7787 if (!Info.CheckArraySize(Loc: E->getBeginLoc(), BitWidth: ByteSize.getActiveBits(),
7788 ElemCount: Size.getZExtValue(), /*Diag=*/!IsNothrow)) {
7789 if (IsNothrow) {
7790 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
7791 return true;
7792 }
7793 return false;
7794 }
7795
7796 QualType AllocType = Info.Ctx.getConstantArrayType(
7797 EltTy: ElemType, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
7798 APValue *Val = Info.createHeapAlloc(E: Caller.Call, T: AllocType, LV&: Result);
7799 *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue());
7800 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: AllocType));
7801 return true;
7802}
7803
7804static bool hasVirtualDestructor(QualType T) {
7805 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7806 if (CXXDestructorDecl *DD = RD->getDestructor())
7807 return DD->isVirtual();
7808 return false;
7809}
7810
7811static const FunctionDecl *getVirtualOperatorDelete(QualType T) {
7812 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7813 if (CXXDestructorDecl *DD = RD->getDestructor())
7814 return DD->isVirtual() ? DD->getOperatorDelete() : nullptr;
7815 return nullptr;
7816}
7817
7818/// Check that the given object is a suitable pointer to a heap allocation that
7819/// still exists and is of the right kind for the purpose of a deletion.
7820///
7821/// On success, returns the heap allocation to deallocate. On failure, produces
7822/// a diagnostic and returns std::nullopt.
7823static std::optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E,
7824 const LValue &Pointer,
7825 DynAlloc::Kind DeallocKind) {
7826 auto PointerAsString = [&] {
7827 return Pointer.toString(Ctx&: Info.Ctx, T: Info.Ctx.VoidPtrTy);
7828 };
7829
7830 DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>();
7831 if (!DA) {
7832 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_not_heap_alloc)
7833 << PointerAsString();
7834 if (Pointer.Base)
7835 NoteLValueLocation(Info, Base: Pointer.Base);
7836 return std::nullopt;
7837 }
7838
7839 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
7840 if (!Alloc) {
7841 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
7842 return std::nullopt;
7843 }
7844
7845 if (DeallocKind != (*Alloc)->getKind()) {
7846 QualType AllocType = Pointer.Base.getDynamicAllocType();
7847 Info.FFDiag(E, DiagId: diag::note_constexpr_new_delete_mismatch)
7848 << DeallocKind << (*Alloc)->getKind() << AllocType;
7849 NoteLValueLocation(Info, Base: Pointer.Base);
7850 return std::nullopt;
7851 }
7852
7853 bool Subobject = false;
7854 if (DeallocKind == DynAlloc::New) {
7855 Subobject = Pointer.Designator.MostDerivedPathLength != 0 ||
7856 Pointer.Designator.isOnePastTheEnd();
7857 } else {
7858 Subobject = Pointer.Designator.Entries.size() != 1 ||
7859 Pointer.Designator.Entries[0].getAsArrayIndex() != 0;
7860 }
7861 if (Subobject) {
7862 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_subobject)
7863 << PointerAsString() << Pointer.Designator.isOnePastTheEnd();
7864 return std::nullopt;
7865 }
7866
7867 return Alloc;
7868}
7869
7870// Perform a call to 'operator delete' or '__builtin_operator_delete'.
7871static bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) {
7872 if (Info.checkingPotentialConstantExpression() ||
7873 Info.SpeculativeEvaluationDepth)
7874 return false;
7875
7876 // This is permitted only within a call to std::allocator<T>::deallocate.
7877 if (!Info.getStdAllocatorCaller(FnName: "deallocate")) {
7878 Info.FFDiag(Loc: E->getExprLoc());
7879 return true;
7880 }
7881
7882 LValue Pointer;
7883 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Pointer, Info))
7884 return false;
7885 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I)
7886 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7887
7888 if (Pointer.Designator.Invalid)
7889 return false;
7890
7891 // Deleting a null pointer would have no effect, but it's not permitted by
7892 // std::allocator<T>::deallocate's contract.
7893 if (Pointer.isNullPointer()) {
7894 Info.CCEDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_deallocate_null);
7895 return true;
7896 }
7897
7898 if (!CheckDeleteKind(Info, E, Pointer, DeallocKind: DynAlloc::StdAllocator))
7899 return false;
7900
7901 Info.HeapAllocs.erase(x: Pointer.Base.get<DynamicAllocLValue>());
7902 return true;
7903}
7904
7905//===----------------------------------------------------------------------===//
7906// Generic Evaluation
7907//===----------------------------------------------------------------------===//
7908namespace {
7909
7910class BitCastBuffer {
7911 // FIXME: We're going to need bit-level granularity when we support
7912 // bit-fields.
7913 // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but
7914 // we don't support a host or target where that is the case. Still, we should
7915 // use a more generic type in case we ever do.
7916 SmallVector<std::optional<unsigned char>, 32> Bytes;
7917
7918 static_assert(std::numeric_limits<unsigned char>::digits >= 8,
7919 "Need at least 8 bit unsigned char");
7920
7921 bool TargetIsLittleEndian;
7922
7923public:
7924 BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian)
7925 : Bytes(Width.getQuantity()),
7926 TargetIsLittleEndian(TargetIsLittleEndian) {}
7927
7928 [[nodiscard]] bool readObject(CharUnits Offset, CharUnits Width,
7929 SmallVectorImpl<unsigned char> &Output) const {
7930 for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) {
7931 // If a byte of an integer is uninitialized, then the whole integer is
7932 // uninitialized.
7933 if (!Bytes[I.getQuantity()])
7934 return false;
7935 Output.push_back(Elt: *Bytes[I.getQuantity()]);
7936 }
7937 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
7938 std::reverse(first: Output.begin(), last: Output.end());
7939 return true;
7940 }
7941
7942 void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) {
7943 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
7944 std::reverse(first: Input.begin(), last: Input.end());
7945
7946 size_t Index = 0;
7947 for (unsigned char Byte : Input) {
7948 assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?");
7949 Bytes[Offset.getQuantity() + Index] = Byte;
7950 ++Index;
7951 }
7952 }
7953
7954 size_t size() { return Bytes.size(); }
7955};
7956
7957/// Traverse an APValue to produce an BitCastBuffer, emulating how the current
7958/// target would represent the value at runtime.
7959class APValueToBufferConverter {
7960 EvalInfo &Info;
7961 BitCastBuffer Buffer;
7962 const CastExpr *BCE;
7963
7964 APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth,
7965 const CastExpr *BCE)
7966 : Info(Info),
7967 Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()),
7968 BCE(BCE) {}
7969
7970 bool visit(const APValue &Val, QualType Ty) {
7971 return visit(Val, Ty, Offset: CharUnits::fromQuantity(Quantity: 0));
7972 }
7973
7974 // Write out Val with type Ty into Buffer starting at Offset.
7975 bool visit(const APValue &Val, QualType Ty, CharUnits Offset) {
7976 assert((size_t)Offset.getQuantity() <= Buffer.size());
7977
7978 // As a special case, nullptr_t has an indeterminate value.
7979 if (Ty->isNullPtrType())
7980 return true;
7981
7982 // Dig through Src to find the byte at SrcOffset.
7983 switch (Val.getKind()) {
7984 case APValue::Indeterminate:
7985 case APValue::None:
7986 return true;
7987
7988 case APValue::Int:
7989 return visitInt(Val: Val.getInt(), Ty, Offset);
7990 case APValue::Float:
7991 return visitFloat(Val: Val.getFloat(), Ty, Offset);
7992 case APValue::Array:
7993 return visitArray(Val, Ty, Offset);
7994 case APValue::Struct:
7995 return visitRecord(Val, Ty, Offset);
7996 case APValue::Vector:
7997 return visitVector(Val, Ty, Offset);
7998
7999 case APValue::ComplexInt:
8000 case APValue::ComplexFloat:
8001 return visitComplex(Val, Ty, Offset);
8002 case APValue::FixedPoint:
8003 // FIXME: We should support these.
8004
8005 case APValue::LValue:
8006 case APValue::Matrix:
8007 case APValue::Union:
8008 case APValue::MemberPointer:
8009 case APValue::AddrLabelDiff: {
8010 Info.FFDiag(Loc: BCE->getBeginLoc(),
8011 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8012 << Ty;
8013 return false;
8014 }
8015 }
8016 llvm_unreachable("Unhandled APValue::ValueKind");
8017 }
8018
8019 bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) {
8020 const RecordDecl *RD = Ty->getAsRecordDecl();
8021 if (!ASTContext::hasLayout(D: RD))
8022 return false;
8023 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8024
8025 // Visit the base classes.
8026 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8027 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8028 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8029 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8030 const APValue &Base = Val.getStructBase(i: I);
8031
8032 // Can happen in error cases.
8033 if (!Base.isStruct())
8034 return false;
8035
8036 if (!visitRecord(Val: Base, Ty: BS.getType(),
8037 Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset))
8038 return false;
8039 }
8040 }
8041
8042 // Visit the fields.
8043 unsigned FieldIdx = 0;
8044 for (FieldDecl *FD : RD->fields()) {
8045 if (FD->isBitField()) {
8046 Info.FFDiag(Loc: BCE->getBeginLoc(),
8047 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8048 return false;
8049 }
8050
8051 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8052
8053 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 &&
8054 "only bit-fields can have sub-char alignment");
8055 CharUnits FieldOffset =
8056 Info.Ctx.toCharUnitsFromBits(BitSize: FieldOffsetBits) + Offset;
8057 QualType FieldTy = FD->getType();
8058 if (!visit(Val: Val.getStructField(i: FieldIdx), Ty: FieldTy, Offset: FieldOffset))
8059 return false;
8060 ++FieldIdx;
8061 }
8062
8063 return true;
8064 }
8065
8066 bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) {
8067 const auto *CAT =
8068 dyn_cast_or_null<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe());
8069 if (!CAT)
8070 return false;
8071
8072 CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(T: CAT->getElementType());
8073 unsigned NumInitializedElts = Val.getArrayInitializedElts();
8074 unsigned ArraySize = Val.getArraySize();
8075 // First, initialize the initialized elements.
8076 for (unsigned I = 0; I != NumInitializedElts; ++I) {
8077 const APValue &SubObj = Val.getArrayInitializedElt(I);
8078 if (!visit(Val: SubObj, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
8079 return false;
8080 }
8081
8082 // Next, initialize the rest of the array using the filler.
8083 if (Val.hasArrayFiller()) {
8084 const APValue &Filler = Val.getArrayFiller();
8085 for (unsigned I = NumInitializedElts; I != ArraySize; ++I) {
8086 if (!visit(Val: Filler, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
8087 return false;
8088 }
8089 }
8090
8091 return true;
8092 }
8093
8094 bool visitComplex(const APValue &Val, QualType Ty, CharUnits Offset) {
8095 const ComplexType *ComplexTy = Ty->castAs<ComplexType>();
8096 QualType EltTy = ComplexTy->getElementType();
8097 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8098 bool IsInt = Val.isComplexInt();
8099
8100 if (IsInt) {
8101 if (!visitInt(Val: Val.getComplexIntReal(), Ty: EltTy,
8102 Offset: Offset + (0 * EltSizeChars)))
8103 return false;
8104 if (!visitInt(Val: Val.getComplexIntImag(), Ty: EltTy,
8105 Offset: Offset + (1 * EltSizeChars)))
8106 return false;
8107 } else {
8108 if (!visitFloat(Val: Val.getComplexFloatReal(), Ty: EltTy,
8109 Offset: Offset + (0 * EltSizeChars)))
8110 return false;
8111 if (!visitFloat(Val: Val.getComplexFloatImag(), Ty: EltTy,
8112 Offset: Offset + (1 * EltSizeChars)))
8113 return false;
8114 }
8115
8116 return true;
8117 }
8118
8119 bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) {
8120 const VectorType *VTy = Ty->castAs<VectorType>();
8121 QualType EltTy = VTy->getElementType();
8122 unsigned NElts = VTy->getNumElements();
8123
8124 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8125 // Special handling for OpenCL bool vectors:
8126 // Since these vectors are stored as packed bits, but we can't write
8127 // individual bits to the BitCastBuffer, we'll buffer all of the elements
8128 // together into an appropriately sized APInt and write them all out at
8129 // once. Because we don't accept vectors where NElts * EltSize isn't a
8130 // multiple of the char size, there will be no padding space, so we don't
8131 // have to worry about writing data which should have been left
8132 // uninitialized.
8133 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8134
8135 llvm::APInt Res = llvm::APInt::getZero(numBits: NElts);
8136 for (unsigned I = 0; I < NElts; ++I) {
8137 const llvm::APSInt &EltAsInt = Val.getVectorElt(I).getInt();
8138 assert(EltAsInt.isUnsigned() && EltAsInt.getBitWidth() == 1 &&
8139 "bool vector element must be 1-bit unsigned integer!");
8140
8141 Res.insertBits(SubBits: EltAsInt, bitPosition: BigEndian ? (NElts - I - 1) : I);
8142 }
8143
8144 SmallVector<uint8_t, 8> Bytes(NElts / 8);
8145 llvm::StoreIntToMemory(IntVal: Res, Dst: &*Bytes.begin(), StoreBytes: NElts / 8);
8146 Buffer.writeObject(Offset, Input&: Bytes);
8147 } else {
8148 // Iterate over each of the elements and write them out to the buffer at
8149 // the appropriate offset.
8150 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8151 for (unsigned I = 0; I < NElts; ++I) {
8152 if (!visit(Val: Val.getVectorElt(I), Ty: EltTy, Offset: Offset + I * EltSizeChars))
8153 return false;
8154 }
8155 }
8156
8157 return true;
8158 }
8159
8160 bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) {
8161 APSInt AdjustedVal = Val;
8162 unsigned Width = AdjustedVal.getBitWidth();
8163 if (Ty->isBooleanType()) {
8164 Width = Info.Ctx.getTypeSize(T: Ty);
8165 AdjustedVal = AdjustedVal.extend(width: Width);
8166 }
8167
8168 SmallVector<uint8_t, 8> Bytes(Width / 8);
8169 llvm::StoreIntToMemory(IntVal: AdjustedVal, Dst: &*Bytes.begin(), StoreBytes: Width / 8);
8170 Buffer.writeObject(Offset, Input&: Bytes);
8171 return true;
8172 }
8173
8174 bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) {
8175 APSInt AsInt(Val.bitcastToAPInt());
8176 return visitInt(Val: AsInt, Ty, Offset);
8177 }
8178
8179public:
8180 static std::optional<BitCastBuffer>
8181 convert(EvalInfo &Info, const APValue &Src, const CastExpr *BCE) {
8182 CharUnits DstSize = Info.Ctx.getTypeSizeInChars(T: BCE->getType());
8183 APValueToBufferConverter Converter(Info, DstSize, BCE);
8184 if (!Converter.visit(Val: Src, Ty: BCE->getSubExpr()->getType()))
8185 return std::nullopt;
8186 return Converter.Buffer;
8187 }
8188};
8189
8190/// Write an BitCastBuffer into an APValue.
8191class BufferToAPValueConverter {
8192 EvalInfo &Info;
8193 const BitCastBuffer &Buffer;
8194 const CastExpr *BCE;
8195
8196 BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer,
8197 const CastExpr *BCE)
8198 : Info(Info), Buffer(Buffer), BCE(BCE) {}
8199
8200 // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast
8201 // with an invalid type, so anything left is a deficiency on our part (FIXME).
8202 // Ideally this will be unreachable.
8203 std::nullopt_t unsupportedType(QualType Ty) {
8204 Info.FFDiag(Loc: BCE->getBeginLoc(),
8205 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8206 << Ty;
8207 return std::nullopt;
8208 }
8209
8210 std::nullopt_t unrepresentableValue(QualType Ty, const APSInt &Val) {
8211 Info.FFDiag(Loc: BCE->getBeginLoc(),
8212 DiagId: diag::note_constexpr_bit_cast_unrepresentable_value)
8213 << Ty << toString(I: Val, /*Radix=*/10);
8214 return std::nullopt;
8215 }
8216
8217 std::optional<APValue> visit(const BuiltinType *T, CharUnits Offset,
8218 const EnumType *EnumSugar = nullptr) {
8219 if (T->isNullPtrType()) {
8220 uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QT: QualType(T, 0));
8221 return APValue((Expr *)nullptr,
8222 /*Offset=*/CharUnits::fromQuantity(Quantity: NullValue),
8223 APValue::NoLValuePath{}, /*IsNullPtr=*/true);
8224 }
8225
8226 CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T);
8227
8228 // Work around floating point types that contain unused padding bytes. This
8229 // is really just `long double` on x86, which is the only fundamental type
8230 // with padding bytes.
8231 if (T->isRealFloatingType()) {
8232 const llvm::fltSemantics &Semantics =
8233 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8234 unsigned NumBits = llvm::APFloatBase::getSizeInBits(Sem: Semantics);
8235 assert(NumBits % 8 == 0);
8236 CharUnits NumBytes = CharUnits::fromQuantity(Quantity: NumBits / 8);
8237 if (NumBytes != SizeOf)
8238 SizeOf = NumBytes;
8239 }
8240
8241 SmallVector<uint8_t, 8> Bytes;
8242 if (!Buffer.readObject(Offset, Width: SizeOf, Output&: Bytes)) {
8243 // If this is std::byte or unsigned char, then its okay to store an
8244 // indeterminate value.
8245 bool IsStdByte = EnumSugar && EnumSugar->isStdByteType();
8246 bool IsUChar =
8247 !EnumSugar && (T->isSpecificBuiltinType(K: BuiltinType::UChar) ||
8248 T->isSpecificBuiltinType(K: BuiltinType::Char_U));
8249 if (!IsStdByte && !IsUChar) {
8250 QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0);
8251 Info.FFDiag(Loc: BCE->getExprLoc(),
8252 DiagId: diag::note_constexpr_bit_cast_indet_dest)
8253 << DisplayType << Info.Ctx.getLangOpts().CharIsSigned;
8254 return std::nullopt;
8255 }
8256
8257 return APValue::IndeterminateValue();
8258 }
8259
8260 APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true);
8261 llvm::LoadIntFromMemory(IntVal&: Val, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8262
8263 if (T->isIntegralOrEnumerationType()) {
8264 Val.setIsSigned(T->isSignedIntegerOrEnumerationType());
8265
8266 unsigned IntWidth = Info.Ctx.getIntWidth(T: QualType(T, 0));
8267 if (IntWidth != Val.getBitWidth()) {
8268 APSInt Truncated = Val.trunc(width: IntWidth);
8269 if (Truncated.extend(width: Val.getBitWidth()) != Val)
8270 return unrepresentableValue(Ty: QualType(T, 0), Val);
8271 Val = Truncated;
8272 }
8273
8274 return APValue(Val);
8275 }
8276
8277 if (T->isRealFloatingType()) {
8278 const llvm::fltSemantics &Semantics =
8279 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8280 return APValue(APFloat(Semantics, Val));
8281 }
8282
8283 return unsupportedType(Ty: QualType(T, 0));
8284 }
8285
8286 std::optional<APValue> visit(const RecordType *RTy, CharUnits Offset) {
8287 const RecordDecl *RD = RTy->getAsRecordDecl();
8288 if (RD->isInvalidDecl())
8289 return std::nullopt;
8290 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8291
8292 unsigned NumBases = 0;
8293 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
8294 NumBases = CXXRD->getNumBases();
8295
8296 APValue ResultVal(APValue::UninitStruct(), NumBases, RD->getNumFields());
8297
8298 // Visit the base classes.
8299 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8300 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8301 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8302 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8303
8304 std::optional<APValue> SubObj = visitType(
8305 Ty: BS.getType(), Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset);
8306 if (!SubObj)
8307 return std::nullopt;
8308 ResultVal.getStructBase(i: I) = *SubObj;
8309 }
8310 }
8311
8312 // Visit the fields.
8313 unsigned FieldIdx = 0;
8314 for (FieldDecl *FD : RD->fields()) {
8315 // FIXME: We don't currently support bit-fields. A lot of the logic for
8316 // this is in CodeGen, so we need to factor it around.
8317 if (FD->isBitField()) {
8318 Info.FFDiag(Loc: BCE->getBeginLoc(),
8319 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8320 return std::nullopt;
8321 }
8322
8323 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8324 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0);
8325
8326 CharUnits FieldOffset =
8327 CharUnits::fromQuantity(Quantity: FieldOffsetBits / Info.Ctx.getCharWidth()) +
8328 Offset;
8329 QualType FieldTy = FD->getType();
8330 std::optional<APValue> SubObj = visitType(Ty: FieldTy, Offset: FieldOffset);
8331 if (!SubObj)
8332 return std::nullopt;
8333 ResultVal.getStructField(i: FieldIdx) = *SubObj;
8334 ++FieldIdx;
8335 }
8336
8337 return ResultVal;
8338 }
8339
8340 std::optional<APValue> visit(const EnumType *Ty, CharUnits Offset) {
8341 QualType RepresentationType =
8342 Ty->getDecl()->getDefinitionOrSelf()->getIntegerType();
8343 assert(!RepresentationType.isNull() &&
8344 "enum forward decl should be caught by Sema");
8345 const auto *AsBuiltin =
8346 RepresentationType.getCanonicalType()->castAs<BuiltinType>();
8347 // Recurse into the underlying type. Treat std::byte transparently as
8348 // unsigned char.
8349 return visit(T: AsBuiltin, Offset, /*EnumTy=*/EnumSugar: Ty);
8350 }
8351
8352 std::optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) {
8353 size_t Size = Ty->getLimitedSize();
8354 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: Ty->getElementType());
8355
8356 APValue ArrayValue(APValue::UninitArray(), Size, Size);
8357 for (size_t I = 0; I != Size; ++I) {
8358 std::optional<APValue> ElementValue =
8359 visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8360 if (!ElementValue)
8361 return std::nullopt;
8362 ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue);
8363 }
8364
8365 return ArrayValue;
8366 }
8367
8368 std::optional<APValue> visit(const ComplexType *Ty, CharUnits Offset) {
8369 QualType ElementType = Ty->getElementType();
8370 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: ElementType);
8371 bool IsInt = ElementType->isIntegerType();
8372
8373 std::optional<APValue> Values[2];
8374 for (unsigned I = 0; I != 2; ++I) {
8375 Values[I] = visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8376 if (!Values[I])
8377 return std::nullopt;
8378 }
8379
8380 if (IsInt)
8381 return APValue(Values[0]->getInt(), Values[1]->getInt());
8382 return APValue(Values[0]->getFloat(), Values[1]->getFloat());
8383 }
8384
8385 std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) {
8386 QualType EltTy = VTy->getElementType();
8387 unsigned NElts = VTy->getNumElements();
8388 unsigned EltSize =
8389 VTy->isPackedVectorBoolType(ctx: Info.Ctx) ? 1 : Info.Ctx.getTypeSize(T: EltTy);
8390
8391 SmallVector<APValue, 4> Elts;
8392 Elts.reserve(N: NElts);
8393 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8394 // Special handling for OpenCL bool vectors:
8395 // Since these vectors are stored as packed bits, but we can't read
8396 // individual bits from the BitCastBuffer, we'll buffer all of the
8397 // elements together into an appropriately sized APInt and write them all
8398 // out at once. Because we don't accept vectors where NElts * EltSize
8399 // isn't a multiple of the char size, there will be no padding space, so
8400 // we don't have to worry about reading any padding data which didn't
8401 // actually need to be accessed.
8402 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8403
8404 SmallVector<uint8_t, 8> Bytes;
8405 Bytes.reserve(N: NElts / 8);
8406 if (!Buffer.readObject(Offset, Width: CharUnits::fromQuantity(Quantity: NElts / 8), Output&: Bytes))
8407 return std::nullopt;
8408
8409 APSInt SValInt(NElts, true);
8410 llvm::LoadIntFromMemory(IntVal&: SValInt, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8411
8412 for (unsigned I = 0; I < NElts; ++I) {
8413 llvm::APInt Elt =
8414 SValInt.extractBits(numBits: 1, bitPosition: (BigEndian ? NElts - I - 1 : I) * EltSize);
8415 Elts.emplace_back(
8416 Args: APSInt(std::move(Elt), !EltTy->isSignedIntegerType()));
8417 }
8418 } else {
8419 // Iterate over each of the elements and read them from the buffer at
8420 // the appropriate offset.
8421 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8422 for (unsigned I = 0; I < NElts; ++I) {
8423 std::optional<APValue> EltValue =
8424 visitType(Ty: EltTy, Offset: Offset + I * EltSizeChars);
8425 if (!EltValue)
8426 return std::nullopt;
8427 Elts.push_back(Elt: std::move(*EltValue));
8428 }
8429 }
8430
8431 return APValue(Elts.data(), Elts.size());
8432 }
8433
8434 std::optional<APValue> visit(const Type *Ty, CharUnits Offset) {
8435 return unsupportedType(Ty: QualType(Ty, 0));
8436 }
8437
8438 std::optional<APValue> visitType(QualType Ty, CharUnits Offset) {
8439 QualType Can = Ty.getCanonicalType();
8440
8441 switch (Can->getTypeClass()) {
8442#define TYPE(Class, Base) \
8443 case Type::Class: \
8444 return visit(cast<Class##Type>(Can.getTypePtr()), Offset);
8445#define ABSTRACT_TYPE(Class, Base)
8446#define NON_CANONICAL_TYPE(Class, Base) \
8447 case Type::Class: \
8448 llvm_unreachable("non-canonical type should be impossible!");
8449#define DEPENDENT_TYPE(Class, Base) \
8450 case Type::Class: \
8451 llvm_unreachable( \
8452 "dependent types aren't supported in the constant evaluator!");
8453#define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \
8454 case Type::Class: \
8455 llvm_unreachable("either dependent or not canonical!");
8456#include "clang/AST/TypeNodes.inc"
8457 }
8458 llvm_unreachable("Unhandled Type::TypeClass");
8459 }
8460
8461public:
8462 // Pull out a full value of type DstType.
8463 static std::optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer,
8464 const CastExpr *BCE) {
8465 BufferToAPValueConverter Converter(Info, Buffer, BCE);
8466 return Converter.visitType(Ty: BCE->getType(), Offset: CharUnits::fromQuantity(Quantity: 0));
8467 }
8468};
8469
8470static bool checkBitCastConstexprEligibilityType(SourceLocation Loc,
8471 QualType Ty, EvalInfo *Info,
8472 const ASTContext &Ctx,
8473 bool CheckingDest) {
8474 Ty = Ty.getCanonicalType();
8475
8476 auto diag = [&](int Reason) {
8477 if (Info)
8478 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_type)
8479 << CheckingDest << (Reason == 4) << Reason;
8480 return false;
8481 };
8482 auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) {
8483 if (Info)
8484 Info->Note(Loc: NoteLoc, DiagId: diag::note_constexpr_bit_cast_invalid_subtype)
8485 << NoteTy << Construct << Ty;
8486 return false;
8487 };
8488
8489 if (Ty->isUnionType())
8490 return diag(0);
8491 if (Ty->isPointerType())
8492 return diag(1);
8493 if (Ty->isMemberPointerType())
8494 return diag(2);
8495 if (Ty.isVolatileQualified())
8496 return diag(3);
8497
8498 if (RecordDecl *Record = Ty->getAsRecordDecl()) {
8499 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: Record)) {
8500 for (CXXBaseSpecifier &BS : CXXRD->bases())
8501 if (!checkBitCastConstexprEligibilityType(Loc, Ty: BS.getType(), Info, Ctx,
8502 CheckingDest))
8503 return note(1, BS.getType(), BS.getBeginLoc());
8504 }
8505 for (FieldDecl *FD : Record->fields()) {
8506 if (FD->getType()->isReferenceType())
8507 return diag(4);
8508 if (!checkBitCastConstexprEligibilityType(Loc, Ty: FD->getType(), Info, Ctx,
8509 CheckingDest))
8510 return note(0, FD->getType(), FD->getBeginLoc());
8511 }
8512 }
8513
8514 if (Ty->isArrayType() &&
8515 !checkBitCastConstexprEligibilityType(Loc, Ty: Ctx.getBaseElementType(QT: Ty),
8516 Info, Ctx, CheckingDest))
8517 return false;
8518
8519 if (const auto *VTy = Ty->getAs<VectorType>()) {
8520 QualType EltTy = VTy->getElementType();
8521 unsigned NElts = VTy->getNumElements();
8522 unsigned EltSize =
8523 VTy->isPackedVectorBoolType(ctx: Ctx) ? 1 : Ctx.getTypeSize(T: EltTy);
8524
8525 if ((NElts * EltSize) % Ctx.getCharWidth() != 0) {
8526 // The vector's size in bits is not a multiple of the target's byte size,
8527 // so its layout is unspecified. For now, we'll simply treat these cases
8528 // as unsupported (this should only be possible with OpenCL bool vectors
8529 // whose element count isn't a multiple of the byte size).
8530 if (Info)
8531 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_vector)
8532 << QualType(VTy, 0) << EltSize << NElts << Ctx.getCharWidth();
8533 return false;
8534 }
8535
8536 if (EltTy->isRealFloatingType() &&
8537 &Ctx.getFloatTypeSemantics(T: EltTy) == &APFloat::x87DoubleExtended()) {
8538 // The layout for x86_fp80 vectors seems to be handled very inconsistently
8539 // by both clang and LLVM, so for now we won't allow bit_casts involving
8540 // it in a constexpr context.
8541 if (Info)
8542 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8543 << EltTy;
8544 return false;
8545 }
8546 }
8547
8548 return true;
8549}
8550
8551static bool checkBitCastConstexprEligibility(EvalInfo *Info,
8552 const ASTContext &Ctx,
8553 const CastExpr *BCE) {
8554 bool DestOK = checkBitCastConstexprEligibilityType(
8555 Loc: BCE->getBeginLoc(), Ty: BCE->getType(), Info, Ctx, CheckingDest: true);
8556 bool SourceOK = DestOK && checkBitCastConstexprEligibilityType(
8557 Loc: BCE->getBeginLoc(),
8558 Ty: BCE->getSubExpr()->getType(), Info, Ctx, CheckingDest: false);
8559 return SourceOK;
8560}
8561
8562static bool handleRValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8563 const APValue &SourceRValue,
8564 const CastExpr *BCE) {
8565 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8566 "no host or target supports non 8-bit chars");
8567
8568 if (!checkBitCastConstexprEligibility(Info: &Info, Ctx: Info.Ctx, BCE))
8569 return false;
8570
8571 // Read out SourceValue into a char buffer.
8572 std::optional<BitCastBuffer> Buffer =
8573 APValueToBufferConverter::convert(Info, Src: SourceRValue, BCE);
8574 if (!Buffer)
8575 return false;
8576
8577 // Write out the buffer into a new APValue.
8578 std::optional<APValue> MaybeDestValue =
8579 BufferToAPValueConverter::convert(Info, Buffer&: *Buffer, BCE);
8580 if (!MaybeDestValue)
8581 return false;
8582
8583 DestValue = std::move(*MaybeDestValue);
8584 return true;
8585}
8586
8587static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8588 APValue &SourceValue,
8589 const CastExpr *BCE) {
8590 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8591 "no host or target supports non 8-bit chars");
8592 assert(SourceValue.isLValue() &&
8593 "LValueToRValueBitcast requires an lvalue operand!");
8594
8595 LValue SourceLValue;
8596 APValue SourceRValue;
8597 SourceLValue.setFrom(Ctx: Info.Ctx, V: SourceValue);
8598 if (!handleLValueToRValueConversion(
8599 Info, Conv: BCE, Type: BCE->getSubExpr()->getType().withConst(), LVal: SourceLValue,
8600 RVal&: SourceRValue, /*WantObjectRepresentation=*/true))
8601 return false;
8602
8603 return handleRValueToRValueBitCast(Info, DestValue, SourceRValue, BCE);
8604}
8605
8606template <class Derived>
8607class ExprEvaluatorBase
8608 : public ConstStmtVisitor<Derived, bool> {
8609private:
8610 Derived &getDerived() { return static_cast<Derived&>(*this); }
8611 bool DerivedSuccess(const APValue &V, const Expr *E) {
8612 return getDerived().Success(V, E);
8613 }
8614 bool DerivedZeroInitialization(const Expr *E) {
8615 return getDerived().ZeroInitialization(E);
8616 }
8617
8618 // Check whether a conditional operator with a non-constant condition is a
8619 // potential constant expression. If neither arm is a potential constant
8620 // expression, then the conditional operator is not either.
8621 template<typename ConditionalOperator>
8622 void CheckPotentialConstantConditional(const ConditionalOperator *E) {
8623 assert(Info.checkingPotentialConstantExpression());
8624
8625 // Speculatively evaluate both arms.
8626 SmallVector<PartialDiagnosticAt, 8> Diag;
8627 {
8628 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8629 StmtVisitorTy::Visit(E->getFalseExpr());
8630 if (Diag.empty())
8631 return;
8632 }
8633
8634 {
8635 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8636 Diag.clear();
8637 Info.EvalStatus.DiagEmitted = false;
8638 StmtVisitorTy::Visit(E->getTrueExpr());
8639 if (Diag.empty())
8640 return;
8641 }
8642
8643 Error(E, diag::note_constexpr_conditional_never_const);
8644 }
8645
8646
8647 template<typename ConditionalOperator>
8648 bool HandleConditionalOperator(const ConditionalOperator *E) {
8649 bool BoolResult;
8650 if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
8651 if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) {
8652 CheckPotentialConstantConditional(E);
8653 return false;
8654 }
8655 if (Info.noteFailure()) {
8656 StmtVisitorTy::Visit(E->getTrueExpr());
8657 StmtVisitorTy::Visit(E->getFalseExpr());
8658 }
8659 return false;
8660 }
8661
8662 Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
8663 return StmtVisitorTy::Visit(EvalExpr);
8664 }
8665
8666protected:
8667 EvalInfo &Info;
8668 typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy;
8669 typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
8670
8671 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
8672 return Info.CCEDiag(E, DiagId: D);
8673 }
8674
8675 bool ZeroInitialization(const Expr *E) { return Error(E); }
8676
8677 bool IsConstantEvaluatedBuiltinCall(const CallExpr *E) {
8678 unsigned BuiltinOp = E->getBuiltinCallee();
8679 return BuiltinOp != 0 &&
8680 Info.Ctx.BuiltinInfo.isConstantEvaluated(ID: BuiltinOp);
8681 }
8682
8683public:
8684 ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
8685
8686 EvalInfo &getEvalInfo() { return Info; }
8687
8688 /// Report an evaluation error. This should only be called when an error is
8689 /// first discovered. When propagating an error, just return false.
8690 bool Error(const Expr *E, diag::kind D) {
8691 Info.FFDiag(E, DiagId: D) << E->getSourceRange();
8692 return false;
8693 }
8694 bool Error(const Expr *E) {
8695 return Error(E, diag::note_invalid_subexpr_in_const_expr);
8696 }
8697
8698 bool VisitStmt(const Stmt *) {
8699 llvm_unreachable("Expression evaluator should not be called on stmts");
8700 }
8701 bool VisitExpr(const Expr *E) {
8702 return Error(E);
8703 }
8704
8705 bool VisitEmbedExpr(const EmbedExpr *E) {
8706 const auto It = E->begin();
8707 return StmtVisitorTy::Visit(*It);
8708 }
8709
8710 bool VisitPredefinedExpr(const PredefinedExpr *E) {
8711 return StmtVisitorTy::Visit(E->getFunctionName());
8712 }
8713 bool VisitConstantExpr(const ConstantExpr *E) {
8714 if (E->hasAPValueResult())
8715 return DerivedSuccess(V: E->getAPValueResult(), E);
8716
8717 return StmtVisitorTy::Visit(E->getSubExpr());
8718 }
8719
8720 bool VisitParenExpr(const ParenExpr *E)
8721 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8722 bool VisitUnaryExtension(const UnaryOperator *E)
8723 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8724 bool VisitUnaryPlus(const UnaryOperator *E)
8725 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8726 bool VisitChooseExpr(const ChooseExpr *E)
8727 { return StmtVisitorTy::Visit(E->getChosenSubExpr()); }
8728 bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
8729 { return StmtVisitorTy::Visit(E->getResultExpr()); }
8730 bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
8731 { return StmtVisitorTy::Visit(E->getReplacement()); }
8732 bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
8733 TempVersionRAII RAII(*Info.CurrentCall);
8734 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8735 return StmtVisitorTy::Visit(E->getExpr());
8736 }
8737 bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
8738 TempVersionRAII RAII(*Info.CurrentCall);
8739 // The initializer may not have been parsed yet, or might be erroneous.
8740 if (!E->getExpr())
8741 return Error(E);
8742 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8743 return StmtVisitorTy::Visit(E->getExpr());
8744 }
8745
8746 bool VisitExprWithCleanups(const ExprWithCleanups *E) {
8747 FullExpressionRAII Scope(Info);
8748 return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy();
8749 }
8750
8751 // Temporaries are registered when created, so we don't care about
8752 // CXXBindTemporaryExpr.
8753 bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) {
8754 return StmtVisitorTy::Visit(E->getSubExpr());
8755 }
8756
8757 bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
8758 if (E->getCastKind() != CK_PointerToIntegral)
8759 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8760 << diag::ConstexprInvalidCastKind::Reinterpret;
8761 return static_cast<Derived*>(this)->VisitCastExpr(E);
8762 }
8763 bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
8764 if (!Info.Ctx.getLangOpts().CPlusPlus20)
8765 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8766 << diag::ConstexprInvalidCastKind::Dynamic;
8767 return static_cast<Derived*>(this)->VisitCastExpr(E);
8768 }
8769 bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
8770 return static_cast<Derived*>(this)->VisitCastExpr(E);
8771 }
8772
8773 bool VisitBinaryOperator(const BinaryOperator *E) {
8774 switch (E->getOpcode()) {
8775 default:
8776 return Error(E);
8777
8778 case BO_Comma:
8779 VisitIgnoredValue(E: E->getLHS());
8780 return StmtVisitorTy::Visit(E->getRHS());
8781
8782 case BO_PtrMemD:
8783 case BO_PtrMemI: {
8784 LValue Obj;
8785 if (!HandleMemberPointerAccess(Info, BO: E, LV&: Obj))
8786 return false;
8787 APValue Result;
8788 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: Obj, RVal&: Result))
8789 return false;
8790 return DerivedSuccess(V: Result, E);
8791 }
8792 }
8793 }
8794
8795 bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) {
8796 return StmtVisitorTy::Visit(E->getSemanticForm());
8797 }
8798
8799 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
8800 // Evaluate and cache the common expression. We treat it as a temporary,
8801 // even though it's not quite the same thing.
8802 LValue CommonLV;
8803 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8804 Key: E->getOpaqueValue(),
8805 T: getStorageType(Ctx: Info.Ctx, E: E->getOpaqueValue()),
8806 Scope: ScopeKind::FullExpression, LV&: CommonLV),
8807 Info, E: E->getCommon()))
8808 return false;
8809
8810 return HandleConditionalOperator(E);
8811 }
8812
8813 bool VisitConditionalOperator(const ConditionalOperator *E) {
8814 bool IsBcpCall = false;
8815 // If the condition (ignoring parens) is a __builtin_constant_p call,
8816 // the result is a constant expression if it can be folded without
8817 // side-effects. This is an important GNU extension. See GCC PR38377
8818 // for discussion.
8819 if (const CallExpr *CallCE =
8820 dyn_cast<CallExpr>(Val: E->getCond()->IgnoreParenCasts()))
8821 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
8822 IsBcpCall = true;
8823
8824 // Always assume __builtin_constant_p(...) ? ... : ... is a potential
8825 // constant expression; we can't check whether it's potentially foldable.
8826 // FIXME: We should instead treat __builtin_constant_p as non-constant if
8827 // it would return 'false' in this mode.
8828 if (Info.checkingPotentialConstantExpression() && IsBcpCall)
8829 return false;
8830
8831 FoldConstant Fold(Info, IsBcpCall);
8832 if (!HandleConditionalOperator(E)) {
8833 Fold.keepDiagnostics();
8834 return false;
8835 }
8836
8837 return true;
8838 }
8839
8840 bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
8841 if (APValue *Value = Info.CurrentCall->getCurrentTemporary(Key: E);
8842 Value && !Value->isAbsent())
8843 return DerivedSuccess(V: *Value, E);
8844
8845 const Expr *Source = E->getSourceExpr();
8846 if (!Source)
8847 return Error(E);
8848 if (Source == E) {
8849 assert(0 && "OpaqueValueExpr recursively refers to itself");
8850 return Error(E);
8851 }
8852 return StmtVisitorTy::Visit(Source);
8853 }
8854
8855 bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
8856 for (const Expr *SemE : E->semantics()) {
8857 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: SemE)) {
8858 // FIXME: We can't handle the case where an OpaqueValueExpr is also the
8859 // result expression: there could be two different LValues that would
8860 // refer to the same object in that case, and we can't model that.
8861 if (SemE == E->getResultExpr())
8862 return Error(E);
8863
8864 // Unique OVEs get evaluated if and when we encounter them when
8865 // emitting the rest of the semantic form, rather than eagerly.
8866 if (OVE->isUnique())
8867 continue;
8868
8869 LValue LV;
8870 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8871 Key: OVE, T: getStorageType(Ctx: Info.Ctx, E: OVE),
8872 Scope: ScopeKind::FullExpression, LV),
8873 Info, E: OVE->getSourceExpr()))
8874 return false;
8875 } else if (SemE == E->getResultExpr()) {
8876 if (!StmtVisitorTy::Visit(SemE))
8877 return false;
8878 } else {
8879 if (!EvaluateIgnoredValue(Info, E: SemE))
8880 return false;
8881 }
8882 }
8883 return true;
8884 }
8885
8886 bool VisitCallExpr(const CallExpr *E) {
8887 APValue Result;
8888 if (!handleCallExpr(E, Result, ResultSlot: nullptr))
8889 return false;
8890 return DerivedSuccess(V: Result, E);
8891 }
8892
8893 bool handleCallExpr(const CallExpr *E, APValue &Result,
8894 const LValue *ResultSlot) {
8895 CallScopeRAII CallScope(Info);
8896
8897 const Expr *Callee = E->getCallee()->IgnoreParens();
8898 QualType CalleeType = Callee->getType();
8899
8900 const FunctionDecl *FD = nullptr;
8901 LValue *This = nullptr, ObjectArg;
8902 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
8903 bool HasQualifier = false;
8904
8905 CallRef Call;
8906
8907 // Extract function decl and 'this' pointer from the callee.
8908 if (CalleeType->isSpecificBuiltinType(K: BuiltinType::BoundMember)) {
8909 const CXXMethodDecl *Member = nullptr;
8910 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: Callee)) {
8911 // Explicit bound member calls, such as x.f() or p->g();
8912 if (!EvaluateObjectArgument(Info, Object: ME->getBase(), This&: ObjectArg))
8913 return false;
8914 Member = dyn_cast<CXXMethodDecl>(Val: ME->getMemberDecl());
8915 if (!Member)
8916 return Error(Callee);
8917 This = &ObjectArg;
8918 HasQualifier = ME->hasQualifier();
8919 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Val: Callee)) {
8920 // Indirect bound member calls ('.*' or '->*').
8921 const ValueDecl *D =
8922 HandleMemberPointerAccess(Info, BO: BE, LV&: ObjectArg, IncludeMember: false);
8923 if (!D)
8924 return false;
8925 Member = dyn_cast<CXXMethodDecl>(Val: D);
8926 if (!Member)
8927 return Error(Callee);
8928 This = &ObjectArg;
8929 } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Val: Callee)) {
8930 if (!Info.getLangOpts().CPlusPlus20)
8931 Info.CCEDiag(E: PDE, DiagId: diag::note_constexpr_pseudo_destructor);
8932 return EvaluateObjectArgument(Info, Object: PDE->getBase(), This&: ObjectArg) &&
8933 HandleDestruction(Info, E: PDE, This: ObjectArg, ThisType: PDE->getDestroyedType());
8934 } else
8935 return Error(Callee);
8936 FD = Member;
8937 } else if (CalleeType->isFunctionPointerType()) {
8938 LValue CalleeLV;
8939 if (!EvaluatePointer(E: Callee, Result&: CalleeLV, Info))
8940 return false;
8941
8942 if (!CalleeLV.getLValueOffset().isZero())
8943 return Error(Callee);
8944 if (CalleeLV.isNullPointer()) {
8945 Info.FFDiag(E: Callee, DiagId: diag::note_constexpr_null_callee)
8946 << const_cast<Expr *>(Callee);
8947 return false;
8948 }
8949 FD = dyn_cast_or_null<FunctionDecl>(
8950 Val: CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>());
8951 if (!FD)
8952 return Error(Callee);
8953 // Don't call function pointers which have been cast to some other type.
8954 // Per DR (no number yet), the caller and callee can differ in noexcept.
8955 if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec(
8956 T: CalleeType->getPointeeType(), U: FD->getType())) {
8957 return Error(E);
8958 }
8959
8960 // For an (overloaded) assignment expression, evaluate the RHS before the
8961 // LHS.
8962 auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E);
8963 if (OCE && OCE->isAssignmentOp()) {
8964 assert(Args.size() == 2 && "wrong number of arguments in assignment");
8965 Call = Info.CurrentCall->createCall(Callee: FD);
8966 bool HasThis = false;
8967 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FD))
8968 HasThis = MD->isImplicitObjectMemberFunction();
8969 if (!EvaluateArgs(Args: HasThis ? Args.slice(N: 1) : Args, Call, Info, Callee: FD,
8970 /*RightToLeft=*/true, ObjectArg: &ObjectArg))
8971 return false;
8972 }
8973
8974 // Overloaded operator calls to member functions are represented as normal
8975 // calls with '*this' as the first argument.
8976 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
8977 if (MD &&
8978 (MD->isImplicitObjectMemberFunction() || (OCE && MD->isStatic()))) {
8979 // FIXME: When selecting an implicit conversion for an overloaded
8980 // operator delete, we sometimes try to evaluate calls to conversion
8981 // operators without a 'this' parameter!
8982 if (Args.empty())
8983 return Error(E);
8984
8985 if (!EvaluateObjectArgument(Info, Object: Args[0], This&: ObjectArg))
8986 return false;
8987
8988 // If we are calling a static operator, the 'this' argument needs to be
8989 // ignored after being evaluated.
8990 if (MD->isInstance())
8991 This = &ObjectArg;
8992
8993 // If this is syntactically a simple assignment using a trivial
8994 // assignment operator, start the lifetimes of union members as needed,
8995 // per C++20 [class.union]5.
8996 if (Info.getLangOpts().CPlusPlus20 && OCE &&
8997 OCE->getOperator() == OO_Equal && MD->isTrivial() &&
8998 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: Args[0], LHS: ObjectArg))
8999 return false;
9000
9001 Args = Args.slice(N: 1);
9002 } else if (MD && MD->isLambdaStaticInvoker()) {
9003 // Map the static invoker for the lambda back to the call operator.
9004 // Conveniently, we don't have to slice out the 'this' argument (as is
9005 // being done for the non-static case), since a static member function
9006 // doesn't have an implicit argument passed in.
9007 const CXXRecordDecl *ClosureClass = MD->getParent();
9008 assert(
9009 ClosureClass->captures().empty() &&
9010 "Number of captures must be zero for conversion to function-ptr");
9011
9012 const CXXMethodDecl *LambdaCallOp =
9013 ClosureClass->getLambdaCallOperator();
9014
9015 // Set 'FD', the function that will be called below, to the call
9016 // operator. If the closure object represents a generic lambda, find
9017 // the corresponding specialization of the call operator.
9018
9019 if (ClosureClass->isGenericLambda()) {
9020 assert(MD->isFunctionTemplateSpecialization() &&
9021 "A generic lambda's static-invoker function must be a "
9022 "template specialization");
9023 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
9024 FunctionTemplateDecl *CallOpTemplate =
9025 LambdaCallOp->getDescribedFunctionTemplate();
9026 llvm::FoldingSetInsertToken InsertToken;
9027 FunctionDecl *CorrespondingCallOpSpecialization =
9028 CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertToken);
9029 assert(CorrespondingCallOpSpecialization &&
9030 "We must always have a function call operator specialization "
9031 "that corresponds to our static invoker specialization");
9032 assert(isa<CXXMethodDecl>(CorrespondingCallOpSpecialization));
9033 FD = CorrespondingCallOpSpecialization;
9034 } else
9035 FD = LambdaCallOp;
9036 } else if (FD->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
9037 if (FD->getDeclName().isAnyOperatorNew()) {
9038 LValue Ptr;
9039 if (!HandleOperatorNewCall(Info, E, Result&: Ptr))
9040 return false;
9041 Ptr.moveInto(V&: Result);
9042 return CallScope.destroy();
9043 } else {
9044 return HandleOperatorDeleteCall(Info, E) && CallScope.destroy();
9045 }
9046 }
9047 } else
9048 return Error(E);
9049
9050 // Evaluate the arguments now if we've not already done so.
9051 if (!Call) {
9052 Call = Info.CurrentCall->createCall(Callee: FD);
9053 if (!EvaluateArgs(Args, Call, Info, Callee: FD, /*RightToLeft*/ false,
9054 ObjectArg: &ObjectArg))
9055 return false;
9056 }
9057
9058 SmallVector<QualType, 4> CovariantAdjustmentPath;
9059 if (This) {
9060 auto *NamedMember = dyn_cast<CXXMethodDecl>(Val: FD);
9061 if (NamedMember && NamedMember->isVirtual() && !HasQualifier) {
9062 // Perform virtual dispatch, if necessary.
9063 FD = HandleVirtualDispatch(Info, E, This&: *This, Found: NamedMember,
9064 CovariantAdjustmentPath);
9065 if (!FD)
9066 return false;
9067 } else if (NamedMember && NamedMember->isImplicitObjectMemberFunction()) {
9068 // Check that the 'this' pointer points to an object of the right type.
9069 // FIXME: If this is an assignment operator call, we may need to change
9070 // the active union member before we check this.
9071 if (!checkNonVirtualMemberCallThisPointer(Info, E, This: *This, NamedMember))
9072 return false;
9073 }
9074 }
9075
9076 // Destructor calls are different enough that they have their own codepath.
9077 if (auto *DD = dyn_cast<CXXDestructorDecl>(Val: FD)) {
9078 assert(This && "no 'this' pointer for destructor call");
9079 return HandleDestruction(Info, E, This: *This,
9080 ThisType: Info.Ctx.getCanonicalTagType(TD: DD->getParent())) &&
9081 CallScope.destroy();
9082 }
9083
9084 const FunctionDecl *Definition = nullptr;
9085 Stmt *Body = FD->getBody(Definition);
9086 SourceLocation Loc = E->getExprLoc();
9087
9088 // Treat the object argument as `this` when evaluating defaulted
9089 // special menmber functions
9090 if (FD->hasCXXExplicitFunctionObjectParameter())
9091 This = &ObjectArg;
9092
9093 if (!CheckConstexprFunction(Info, CallLoc: Loc, Declaration: FD, Definition, Body) ||
9094 !HandleFunctionCall(CallLoc: Loc, Callee: Definition, ObjectArg: This, E, Args, Call, Body, Info,
9095 Result, ResultSlot))
9096 return false;
9097
9098 if (!CovariantAdjustmentPath.empty() &&
9099 !HandleCovariantReturnAdjustment(Info, E, Result,
9100 Path: CovariantAdjustmentPath))
9101 return false;
9102
9103 return CallScope.destroy();
9104 }
9105
9106 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9107 return StmtVisitorTy::Visit(E->getInitializer());
9108 }
9109 bool VisitInitListExpr(const InitListExpr *E) {
9110 if (E->getNumInits() == 0)
9111 return DerivedZeroInitialization(E);
9112 if (E->getNumInits() == 1)
9113 return StmtVisitorTy::Visit(E->getInit(Init: 0));
9114 return Error(E);
9115 }
9116 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
9117 return DerivedZeroInitialization(E);
9118 }
9119 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
9120 return DerivedZeroInitialization(E);
9121 }
9122 bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
9123 return DerivedZeroInitialization(E);
9124 }
9125
9126 /// A member expression where the object is a prvalue is itself a prvalue.
9127 bool VisitMemberExpr(const MemberExpr *E) {
9128 assert(!Info.Ctx.getLangOpts().CPlusPlus11 &&
9129 "missing temporary materialization conversion");
9130 assert(!E->isArrow() && "missing call to bound member function?");
9131
9132 APValue Val;
9133 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
9134 return false;
9135
9136 QualType BaseTy = E->getBase()->getType();
9137
9138 const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl());
9139 if (!FD) return Error(E);
9140 assert(!FD->getType()->isReferenceType() && "prvalue reference?");
9141 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9142 FD->getParent()->getCanonicalDecl() &&
9143 "record / field mismatch");
9144
9145 // Note: there is no lvalue base here. But this case should only ever
9146 // happen in C or in C++98, where we cannot be evaluating a constexpr
9147 // constructor, which is the only case the base matters.
9148 CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy);
9149 SubobjectDesignator Designator(BaseTy);
9150 Designator.addDeclUnchecked(D: FD);
9151
9152 APValue Result;
9153 return extractSubobject(Info, E, Obj, Sub: Designator, Result) &&
9154 DerivedSuccess(V: Result, E);
9155 }
9156
9157 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) {
9158 APValue Val;
9159 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
9160 return false;
9161
9162 if (Val.isVector()) {
9163 SmallVector<uint32_t, 4> Indices;
9164 E->getEncodedElementAccess(Elts&: Indices);
9165 if (Indices.size() == 1) {
9166 // Return scalar.
9167 return DerivedSuccess(V: Val.getVectorElt(I: Indices[0]), E);
9168 } else {
9169 // Construct new APValue vector.
9170 SmallVector<APValue, 4> Elts;
9171 for (unsigned I = 0; I < Indices.size(); ++I) {
9172 Elts.push_back(Elt: Val.getVectorElt(I: Indices[I]));
9173 }
9174 APValue VecResult(Elts.data(), Indices.size());
9175 return DerivedSuccess(V: VecResult, E);
9176 }
9177 }
9178
9179 return false;
9180 }
9181
9182 bool VisitCastExpr(const CastExpr *E) {
9183 switch (E->getCastKind()) {
9184 default:
9185 break;
9186
9187 case CK_AtomicToNonAtomic: {
9188 APValue AtomicVal;
9189 // This does not need to be done in place even for class/array types:
9190 // atomic-to-non-atomic conversion implies copying the object
9191 // representation.
9192 if (!Evaluate(Result&: AtomicVal, Info, E: E->getSubExpr()))
9193 return false;
9194 return DerivedSuccess(V: AtomicVal, E);
9195 }
9196
9197 case CK_NoOp:
9198 case CK_UserDefinedConversion:
9199 return StmtVisitorTy::Visit(E->getSubExpr());
9200
9201 case CK_HLSLArrayRValue: {
9202 const Expr *SubExpr = E->getSubExpr();
9203 if (!SubExpr->isGLValue()) {
9204 APValue Val;
9205 if (!Evaluate(Result&: Val, Info, E: SubExpr))
9206 return false;
9207 return DerivedSuccess(V: Val, E);
9208 }
9209
9210 LValue LVal;
9211 if (!EvaluateLValue(E: SubExpr, Result&: LVal, Info))
9212 return false;
9213 APValue RVal;
9214 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9215 if (!handleLValueToRValueConversion(Info, Conv: E, Type: SubExpr->getType(), LVal,
9216 RVal))
9217 return false;
9218 return DerivedSuccess(V: RVal, E);
9219 }
9220 case CK_LValueToRValue: {
9221 LValue LVal;
9222 if (!EvaluateLValue(E: E->getSubExpr(), Result&: LVal, Info))
9223 return false;
9224 APValue RVal;
9225 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9226 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
9227 LVal, RVal))
9228 return false;
9229 return DerivedSuccess(V: RVal, E);
9230 }
9231 case CK_LValueToRValueBitCast: {
9232 APValue DestValue, SourceValue;
9233 if (!Evaluate(Result&: SourceValue, Info, E: E->getSubExpr()))
9234 return false;
9235 if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, BCE: E))
9236 return false;
9237 return DerivedSuccess(V: DestValue, E);
9238 }
9239
9240 case CK_AddressSpaceConversion: {
9241 APValue Value;
9242 if (!Evaluate(Result&: Value, Info, E: E->getSubExpr()))
9243 return false;
9244 return DerivedSuccess(V: Value, E);
9245 }
9246 }
9247
9248 return Error(E);
9249 }
9250
9251 bool VisitUnaryPostInc(const UnaryOperator *UO) {
9252 return VisitUnaryPostIncDec(UO);
9253 }
9254 bool VisitUnaryPostDec(const UnaryOperator *UO) {
9255 return VisitUnaryPostIncDec(UO);
9256 }
9257 bool VisitUnaryPostIncDec(const UnaryOperator *UO) {
9258 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9259 return Error(UO);
9260
9261 LValue LVal;
9262 if (!EvaluateLValue(E: UO->getSubExpr(), Result&: LVal, Info))
9263 return false;
9264 APValue RVal;
9265 if (!handleIncDec(Info&: this->Info, E: UO, LVal, LValType: UO->getSubExpr()->getType(),
9266 IsIncrement: UO->isIncrementOp(), Old: &RVal))
9267 return false;
9268 return DerivedSuccess(V: RVal, E: UO);
9269 }
9270
9271 bool VisitStmtExpr(const StmtExpr *E) {
9272 // We will have checked the full-expressions inside the statement expression
9273 // when they were completed, and don't need to check them again now.
9274 llvm::SaveAndRestore NotCheckingForUB(Info.CheckingForUndefinedBehavior,
9275 false);
9276
9277 const CompoundStmt *CS = E->getSubStmt();
9278 if (CS->body_empty())
9279 return true;
9280
9281 BlockScopeRAII Scope(Info);
9282 for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
9283 BE = CS->body_end();
9284 /**/; ++BI) {
9285 if (BI + 1 == BE) {
9286 const Expr *FinalExpr = dyn_cast<Expr>(Val: *BI);
9287 if (!FinalExpr) {
9288 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9289 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9290 return false;
9291 }
9292 return this->Visit(FinalExpr) && Scope.destroy();
9293 }
9294
9295 APValue ReturnValue;
9296 StmtResult Result = { .Value: ReturnValue, .Slot: nullptr };
9297 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: *BI);
9298 if (ESR != ESR_Succeeded) {
9299 // FIXME: If the statement-expression terminated due to 'return',
9300 // 'break', or 'continue', it would be nice to propagate that to
9301 // the outer statement evaluation rather than bailing out.
9302 if (ESR != ESR_Failed)
9303 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9304 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9305 return false;
9306 }
9307 }
9308
9309 llvm_unreachable("Return from function from the loop above.");
9310 }
9311
9312 bool VisitPackIndexingExpr(const PackIndexingExpr *E) {
9313 return StmtVisitorTy::Visit(E->getSelectedExpr());
9314 }
9315
9316 /// Visit a value which is evaluated, but whose value is ignored.
9317 void VisitIgnoredValue(const Expr *E) {
9318 EvaluateIgnoredValue(Info, E);
9319 }
9320
9321 /// Potentially visit a MemberExpr's base expression.
9322 void VisitIgnoredBaseExpression(const Expr *E) {
9323 // While MSVC doesn't evaluate the base expression, it does diagnose the
9324 // presence of side-effecting behavior.
9325 if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Ctx: Info.Ctx))
9326 return;
9327 VisitIgnoredValue(E);
9328 }
9329};
9330
9331} // namespace
9332
9333//===----------------------------------------------------------------------===//
9334// Common base class for lvalue and temporary evaluation.
9335//===----------------------------------------------------------------------===//
9336namespace {
9337template<class Derived>
9338class LValueExprEvaluatorBase
9339 : public ExprEvaluatorBase<Derived> {
9340protected:
9341 LValue &Result;
9342 bool InvalidBaseOK;
9343 typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
9344 typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy;
9345
9346 bool Success(APValue::LValueBase B) {
9347 Result.set(B);
9348 return true;
9349 }
9350
9351 bool evaluatePointer(const Expr *E, LValue &Result) {
9352 return EvaluatePointer(E, Result, this->Info, InvalidBaseOK);
9353 }
9354
9355public:
9356 LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK)
9357 : ExprEvaluatorBaseTy(Info), Result(Result),
9358 InvalidBaseOK(InvalidBaseOK) {}
9359
9360 bool Success(const APValue &V, const Expr *E) {
9361 Result.setFrom(Ctx: this->Info.Ctx, V);
9362 return true;
9363 }
9364
9365 bool VisitMemberExpr(const MemberExpr *E) {
9366 // Handle non-static data members.
9367 QualType BaseTy;
9368 bool EvalOK;
9369 if (E->isArrow()) {
9370 EvalOK = evaluatePointer(E: E->getBase(), Result);
9371 BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType();
9372 } else if (E->getBase()->isPRValue()) {
9373 assert(E->getBase()->getType()->isRecordType());
9374 EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info);
9375 BaseTy = E->getBase()->getType();
9376 } else {
9377 EvalOK = this->Visit(E->getBase());
9378 BaseTy = E->getBase()->getType();
9379 }
9380 if (!EvalOK) {
9381 if (!InvalidBaseOK)
9382 return false;
9383 Result.setInvalid(B: E);
9384 return true;
9385 }
9386
9387 const ValueDecl *MD = E->getMemberDecl();
9388 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl())) {
9389 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9390 FD->getParent()->getCanonicalDecl() &&
9391 "record / field mismatch");
9392 (void)BaseTy;
9393 if (!HandleLValueMember(this->Info, E, Result, FD))
9394 return false;
9395 } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Val: MD)) {
9396 if (!HandleLValueIndirectMember(this->Info, E, Result, IFD))
9397 return false;
9398 } else
9399 return this->Error(E);
9400
9401 if (MD->getType()->isReferenceType()) {
9402 APValue RefValue;
9403 if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
9404 RefValue))
9405 return false;
9406 return Success(RefValue, E);
9407 }
9408 return true;
9409 }
9410
9411 bool VisitBinaryOperator(const BinaryOperator *E) {
9412 switch (E->getOpcode()) {
9413 default:
9414 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
9415
9416 case BO_PtrMemD:
9417 case BO_PtrMemI:
9418 return HandleMemberPointerAccess(this->Info, E, Result);
9419 }
9420 }
9421
9422 bool VisitCastExpr(const CastExpr *E) {
9423 switch (E->getCastKind()) {
9424 default:
9425 return ExprEvaluatorBaseTy::VisitCastExpr(E);
9426
9427 case CK_DerivedToBase:
9428 case CK_UncheckedDerivedToBase:
9429 if (!this->Visit(E->getSubExpr()))
9430 return false;
9431
9432 // Now figure out the necessary offset to add to the base LV to get from
9433 // the derived class to the base class.
9434 return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(),
9435 Result);
9436 }
9437 }
9438};
9439}
9440
9441//===----------------------------------------------------------------------===//
9442// LValue Evaluation
9443//
9444// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
9445// function designators (in C), decl references to void objects (in C), and
9446// temporaries (if building with -Wno-address-of-temporary).
9447//
9448// LValue evaluation produces values comprising a base expression of one of the
9449// following types:
9450// - Declarations
9451// * VarDecl
9452// * FunctionDecl
9453// - Literals
9454// * CompoundLiteralExpr in C (and in global scope in C++)
9455// * StringLiteral
9456// * PredefinedExpr
9457// * ObjCStringLiteralExpr
9458// * ObjCEncodeExpr
9459// * AddrLabelExpr
9460// * BlockExpr
9461// * CallExpr for a MakeStringConstant builtin
9462// - typeid(T) expressions, as TypeInfoLValues
9463// - Locals and temporaries
9464// * MaterializeTemporaryExpr
9465// * Any Expr, with a CallIndex indicating the function in which the temporary
9466// was evaluated, for cases where the MaterializeTemporaryExpr is missing
9467// from the AST (FIXME).
9468// * A MaterializeTemporaryExpr that has static storage duration, with no
9469// CallIndex, for a lifetime-extended temporary.
9470// * The ConstantExpr that is currently being evaluated during evaluation of an
9471// immediate invocation.
9472// plus an offset in bytes.
9473//===----------------------------------------------------------------------===//
9474namespace {
9475class LValueExprEvaluator
9476 : public LValueExprEvaluatorBase<LValueExprEvaluator> {
9477public:
9478 LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) :
9479 LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {}
9480
9481 bool VisitVarDecl(const Expr *E, const VarDecl *VD);
9482 bool VisitUnaryPreIncDec(const UnaryOperator *UO);
9483
9484 bool VisitCallExpr(const CallExpr *E);
9485 bool VisitDeclRefExpr(const DeclRefExpr *E);
9486 bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(B: E); }
9487 bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
9488 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
9489 bool VisitMemberExpr(const MemberExpr *E);
9490 bool VisitStringLiteral(const StringLiteral *E) {
9491 return Success(
9492 B: APValue::LValueBase(E, 0, Info.Ctx.getNextStringLiteralVersion()));
9493 }
9494 bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(B: E); }
9495 bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
9496 bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
9497 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
9498 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E);
9499 bool VisitUnaryDeref(const UnaryOperator *E);
9500 bool VisitUnaryReal(const UnaryOperator *E);
9501 bool VisitUnaryImag(const UnaryOperator *E);
9502 bool VisitUnaryPreInc(const UnaryOperator *UO) {
9503 return VisitUnaryPreIncDec(UO);
9504 }
9505 bool VisitUnaryPreDec(const UnaryOperator *UO) {
9506 return VisitUnaryPreIncDec(UO);
9507 }
9508 bool VisitBinAssign(const BinaryOperator *BO);
9509 bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO);
9510
9511 bool VisitCastExpr(const CastExpr *E) {
9512 switch (E->getCastKind()) {
9513 default:
9514 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
9515
9516 case CK_LValueBitCast:
9517 this->CCEDiag(E, D: diag::note_constexpr_invalid_cast)
9518 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
9519 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
9520 if (!Visit(S: E->getSubExpr()))
9521 return false;
9522 Result.Designator.setInvalid();
9523 return true;
9524
9525 case CK_BaseToDerived:
9526 if (!Visit(S: E->getSubExpr()))
9527 return false;
9528 return HandleBaseToDerivedCast(Info, E, Result);
9529
9530 case CK_Dynamic:
9531 if (!Visit(S: E->getSubExpr()))
9532 return false;
9533 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
9534 }
9535 }
9536};
9537} // end anonymous namespace
9538
9539/// Get an lvalue to a field of a lambda's closure type.
9540static bool HandleLambdaCapture(EvalInfo &Info, const Expr *E, LValue &Result,
9541 const CXXMethodDecl *MD, const FieldDecl *FD,
9542 bool LValueToRValueConversion) {
9543 // Static lambda function call operators can't have captures. We already
9544 // diagnosed this, so bail out here.
9545 if (MD->isStatic()) {
9546 assert(Info.CurrentCall->This == nullptr &&
9547 "This should not be set for a static call operator");
9548 return false;
9549 }
9550
9551 // Start with 'Result' referring to the complete closure object...
9552 if (MD->isExplicitObjectMemberFunction()) {
9553 // Self may be passed by reference or by value.
9554 const ParmVarDecl *Self = MD->getParamDecl(i: 0);
9555 if (Self->getType()->isReferenceType()) {
9556 APValue *RefValue = Info.getParamSlot(Call: Info.CurrentCall->Arguments, PVD: Self);
9557 if (!RefValue->allowConstexprUnknown() || RefValue->hasValue())
9558 Result.setFrom(Ctx: Info.Ctx, V: *RefValue);
9559 } else {
9560 const ParmVarDecl *VD = Info.CurrentCall->Arguments.getOrigParam(PVD: Self);
9561 CallStackFrame *Frame =
9562 Info.getCallFrameAndDepth(CallIndex: Info.CurrentCall->Arguments.CallIndex)
9563 .first;
9564 unsigned Version = Info.CurrentCall->Arguments.Version;
9565 Result.set(B: {VD, Frame->Index, Version});
9566 }
9567 } else
9568 Result = *Info.CurrentCall->This;
9569
9570 // ... then update it to refer to the field of the closure object
9571 // that represents the capture.
9572 if (!HandleLValueMember(Info, E, LVal&: Result, FD))
9573 return false;
9574
9575 // And if the field is of reference type (or if we captured '*this' by
9576 // reference), update 'Result' to refer to what
9577 // the field refers to.
9578 if (LValueToRValueConversion) {
9579 APValue RVal;
9580 if (!handleLValueToRValueConversion(Info, Conv: E, Type: FD->getType(), LVal: Result, RVal))
9581 return false;
9582 Result.setFrom(Ctx: Info.Ctx, V: RVal);
9583 }
9584 return true;
9585}
9586
9587/// Evaluate an expression as an lvalue. This can be legitimately called on
9588/// expressions which are not glvalues, in three cases:
9589/// * function designators in C, and
9590/// * "extern void" objects
9591/// * @selector() expressions in Objective-C
9592static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
9593 bool InvalidBaseOK) {
9594 assert(!E->isValueDependent());
9595 assert(E->isGLValue() || E->getType()->isFunctionType() ||
9596 E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E->IgnoreParens()));
9597 return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
9598}
9599
9600bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
9601 const ValueDecl *D = E->getDecl();
9602
9603 // If we are within a lambda's call operator, check whether the 'VD' referred
9604 // to within 'E' actually represents a lambda-capture that maps to a
9605 // data-member/field within the closure object, and if so, evaluate to the
9606 // field or what the field refers to.
9607 if (Info.CurrentCall && isLambdaCallOperator(DC: Info.CurrentCall->Callee) &&
9608 E->refersToEnclosingVariableOrCapture()) {
9609 // We don't always have a complete capture-map when checking or inferring if
9610 // the function call operator meets the requirements of a constexpr function
9611 // - but we don't need to evaluate the captures to determine constexprness
9612 // (dcl.constexpr C++17).
9613 if (Info.checkingPotentialConstantExpression())
9614 return false;
9615
9616 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(Val: D)) {
9617 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
9618 return HandleLambdaCapture(Info, E, Result, MD, FD,
9619 LValueToRValueConversion: FD->getType()->isReferenceType());
9620 }
9621 }
9622
9623 if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl,
9624 UnnamedGlobalConstantDecl>(Val: D))
9625 return Success(B: cast<ValueDecl>(Val: D));
9626 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
9627 return VisitVarDecl(E, VD);
9628 if (const BindingDecl *BD = dyn_cast<BindingDecl>(Val: D))
9629 return Visit(S: BD->getBinding());
9630 return Error(E);
9631}
9632
9633bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
9634 CallStackFrame *Frame = nullptr;
9635 unsigned Version = 0;
9636 if (VD->hasLocalStorage()) {
9637 // Only if a local variable was declared in the function currently being
9638 // evaluated, do we expect to be able to find its value in the current
9639 // frame. (Otherwise it was likely declared in an enclosing context and
9640 // could either have a valid evaluatable value (for e.g. a constexpr
9641 // variable) or be ill-formed (and trigger an appropriate evaluation
9642 // diagnostic)).
9643 CallStackFrame *CurrFrame = Info.CurrentCall;
9644 if (CurrFrame->Callee && CurrFrame->Callee->Equals(DC: VD->getDeclContext())) {
9645 // Function parameters are stored in some caller's frame. (Usually the
9646 // immediate caller, but for an inherited constructor they may be more
9647 // distant.)
9648 if (auto *PVD = dyn_cast<ParmVarDecl>(Val: VD)) {
9649 if (CurrFrame->Arguments) {
9650 VD = CurrFrame->Arguments.getOrigParam(PVD);
9651 Frame =
9652 Info.getCallFrameAndDepth(CallIndex: CurrFrame->Arguments.CallIndex).first;
9653 Version = CurrFrame->Arguments.Version;
9654 }
9655 } else {
9656 Frame = CurrFrame;
9657 Version = CurrFrame->getCurrentTemporaryVersion(Key: VD);
9658 }
9659 }
9660 }
9661
9662 if (!VD->getType()->isReferenceType()) {
9663 if (Frame) {
9664 Result.set(B: {VD, Frame->Index, Version});
9665 return true;
9666 }
9667 return Success(B: VD);
9668 }
9669
9670 if (!Info.getLangOpts().CPlusPlus11) {
9671 Info.CCEDiag(E, DiagId: diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
9672 << VD << VD->getType();
9673 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
9674 }
9675
9676 APValue *V;
9677 if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, Result&: V))
9678 return false;
9679
9680 if (!V) {
9681 Result.set(B: VD);
9682 Result.AllowConstexprUnknown = true;
9683 return true;
9684 }
9685
9686 return Success(V: *V, E);
9687}
9688
9689bool LValueExprEvaluator::VisitCallExpr(const CallExpr *E) {
9690 if (!IsConstantEvaluatedBuiltinCall(E))
9691 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9692
9693 switch (E->getBuiltinCallee()) {
9694 default:
9695 return false;
9696 case Builtin::BIas_const:
9697 case Builtin::BIforward:
9698 case Builtin::BIforward_like:
9699 case Builtin::BImove:
9700 case Builtin::BImove_if_noexcept:
9701 if (cast<FunctionDecl>(Val: E->getCalleeDecl())->isConstexpr())
9702 return Visit(S: E->getArg(Arg: 0));
9703 break;
9704 }
9705
9706 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9707}
9708
9709bool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
9710 const MaterializeTemporaryExpr *E) {
9711 // Walk through the expression to find the materialized temporary itself.
9712 SmallVector<const Expr *, 2> CommaLHSs;
9713 SmallVector<SubobjectAdjustment, 2> Adjustments;
9714 const Expr *Inner =
9715 E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments);
9716
9717 // If we passed any comma operators, evaluate their LHSs.
9718 for (const Expr *E : CommaLHSs)
9719 if (!EvaluateIgnoredValue(Info, E))
9720 return false;
9721
9722 // A materialized temporary with static storage duration can appear within the
9723 // result of a constant expression evaluation, so we need to preserve its
9724 // value for use outside this evaluation.
9725 APValue *Value;
9726 if (E->getStorageDuration() == SD_Static) {
9727 if (Info.EvalMode == EvaluationMode::ConstantFold)
9728 return false;
9729 // FIXME: What about SD_Thread?
9730 Value = E->getOrCreateValue(MayCreate: true);
9731 *Value = APValue();
9732 Result.set(B: E);
9733 } else {
9734 Value = &Info.CurrentCall->createTemporary(
9735 Key: E, T: Inner->getType(),
9736 Scope: E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression
9737 : ScopeKind::Block,
9738 LV&: Result);
9739 }
9740
9741 QualType Type = Inner->getType();
9742
9743 // Materialize the temporary itself.
9744 if (!EvaluateInPlace(Result&: *Value, Info, This: Result, E: Inner)) {
9745 *Value = APValue();
9746 return false;
9747 }
9748
9749 // Adjust our lvalue to refer to the desired subobject.
9750 for (unsigned I = Adjustments.size(); I != 0; /**/) {
9751 --I;
9752 switch (Adjustments[I].Kind) {
9753 case SubobjectAdjustment::DerivedToBaseAdjustment:
9754 if (!HandleLValueBasePath(Info, E: Adjustments[I].DerivedToBase.BasePath,
9755 Type, Result))
9756 return false;
9757 Type = Adjustments[I].DerivedToBase.BasePath->getType();
9758 break;
9759
9760 case SubobjectAdjustment::FieldAdjustment:
9761 if (!HandleLValueMember(Info, E, LVal&: Result, FD: Adjustments[I].Field))
9762 return false;
9763 Type = Adjustments[I].Field->getType();
9764 break;
9765
9766 case SubobjectAdjustment::MemberPointerAdjustment:
9767 if (!HandleMemberPointerAccess(Info&: this->Info, LVType: Type, LV&: Result,
9768 RHS: Adjustments[I].Ptr.RHS))
9769 return false;
9770 Type = Adjustments[I].Ptr.MPT->getPointeeType();
9771 break;
9772 }
9773 }
9774
9775 return true;
9776}
9777
9778bool
9779LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9780 assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) &&
9781 "lvalue compound literal in c++?");
9782 APValue *Lit;
9783 // If CompountLiteral has static storage, its value can be used outside
9784 // this expression. So evaluate it once and store it in ASTContext.
9785 if (E->hasStaticStorage()) {
9786 Lit = &E->getOrCreateStaticValue(Ctx&: Info.Ctx);
9787 Result.set(B: E);
9788 // Reset any previously evaluated state, otherwise evaluation below might
9789 // fail.
9790 // FIXME: Should we just re-use the previously evaluated value instead?
9791 *Lit = APValue();
9792 } else {
9793 assert(!Info.getLangOpts().CPlusPlus);
9794 Lit = &Info.CurrentCall->createTemporary(Key: E, T: E->getInitializer()->getType(),
9795 Scope: ScopeKind::Block, LV&: Result);
9796 }
9797 // FIXME: Evaluating in place isn't always right. We should figure out how to
9798 // use appropriate evaluation context here, see
9799 // clang/test/AST/static-compound-literals-reeval.cpp for a failure.
9800 if (!EvaluateInPlace(Result&: *Lit, Info, This: Result, E: E->getInitializer())) {
9801 *Lit = APValue();
9802 return false;
9803 }
9804 return true;
9805}
9806
9807bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
9808 TypeInfoLValue TypeInfo;
9809
9810 if (!E->isPotentiallyEvaluated()) {
9811 if (E->isTypeOperand())
9812 TypeInfo = TypeInfoLValue(E->getTypeOperand(Context: Info.Ctx).getTypePtr());
9813 else
9814 TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr());
9815 } else {
9816 if (!Info.Ctx.getLangOpts().CPlusPlus20) {
9817 Info.CCEDiag(E, DiagId: diag::note_constexpr_typeid_polymorphic)
9818 << E->getExprOperand()->getType()
9819 << E->getExprOperand()->getSourceRange();
9820 }
9821
9822 if (!Visit(S: E->getExprOperand()))
9823 return false;
9824
9825 std::optional<DynamicType> DynType =
9826 ComputeDynamicType(Info, E, This&: Result, AK: AK_TypeId);
9827 if (!DynType)
9828 return false;
9829
9830 TypeInfo = TypeInfoLValue(
9831 Info.Ctx.getCanonicalTagType(TD: DynType->Type).getTypePtr());
9832 }
9833
9834 return Success(B: APValue::LValueBase::getTypeInfo(LV: TypeInfo, TypeInfo: E->getType()));
9835}
9836
9837bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
9838 return Success(B: E->getGuidDecl());
9839}
9840
9841bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
9842 // Handle static data members.
9843 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: E->getMemberDecl())) {
9844 VisitIgnoredBaseExpression(E: E->getBase());
9845 return VisitVarDecl(E, VD);
9846 }
9847
9848 // Handle static member functions.
9849 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: E->getMemberDecl())) {
9850 if (MD->isStatic()) {
9851 VisitIgnoredBaseExpression(E: E->getBase());
9852 return Success(B: MD);
9853 }
9854 }
9855
9856 // Handle non-static data members.
9857 return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
9858}
9859
9860bool LValueExprEvaluator::VisitExtVectorElementExpr(
9861 const ExtVectorElementExpr *E) {
9862 bool Success = true;
9863
9864 APValue Val;
9865 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
9866 if (!Info.noteFailure())
9867 return false;
9868 Success = false;
9869 }
9870
9871 SmallVector<uint32_t, 4> Indices;
9872 E->getEncodedElementAccess(Elts&: Indices);
9873 // FIXME: support accessing more than one element
9874 if (Indices.size() > 1)
9875 return false;
9876
9877 if (Success) {
9878 Result.setFrom(Ctx: Info.Ctx, V: Val);
9879 QualType BaseType = E->getBase()->getType();
9880 if (E->isArrow())
9881 BaseType = BaseType->getPointeeType();
9882 const auto *VT = BaseType->castAs<VectorType>();
9883 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
9884 Size: VT->getNumElements(), Idx: Indices[0]);
9885 }
9886
9887 return Success;
9888}
9889
9890bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
9891 if (E->getBase()->getType()->isSveVLSBuiltinType())
9892 return Error(E);
9893
9894 APSInt Index;
9895 bool Success = true;
9896
9897 if (const auto *VT = E->getBase()->getType()->getAs<VectorType>()) {
9898 APValue Val;
9899 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
9900 if (!Info.noteFailure())
9901 return false;
9902 Success = false;
9903 }
9904
9905 if (!EvaluateInteger(E: E->getIdx(), Result&: Index, Info)) {
9906 if (!Info.noteFailure())
9907 return false;
9908 Success = false;
9909 }
9910
9911 if (Success) {
9912 Result.setFrom(Ctx: Info.Ctx, V: Val);
9913 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
9914 Size: VT->getNumElements(), Idx: Index.getZExtValue());
9915 }
9916
9917 return Success;
9918 }
9919
9920 // C++17's rules require us to evaluate the LHS first, regardless of which
9921 // side is the base.
9922 for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) {
9923 if (SubExpr == E->getBase() ? !evaluatePointer(E: SubExpr, Result)
9924 : !EvaluateInteger(E: SubExpr, Result&: Index, Info)) {
9925 if (!Info.noteFailure())
9926 return false;
9927 Success = false;
9928 }
9929 }
9930
9931 return Success &&
9932 HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: E->getType(), Adjustment: Index);
9933}
9934
9935bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
9936 bool Success = evaluatePointer(E: E->getSubExpr(), Result);
9937 // [C++26][expr.unary.op]
9938 // If the operand points to an object or function, the result
9939 // denotes that object or function; otherwise, the behavior is undefined.
9940 // Because &(*(type*)0) is a common pattern, we do not fail the evaluation
9941 // immediately.
9942 if (!Success || !E->getType().getNonReferenceType()->isObjectType())
9943 return Success;
9944 return bool(findCompleteObject(Info, E, AK: AK_Dereference, LVal: Result,
9945 LValType: E->getType())) ||
9946 Info.noteUndefinedBehavior();
9947}
9948
9949bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
9950 if (!Visit(S: E->getSubExpr()))
9951 return false;
9952 // __real is a no-op on scalar lvalues.
9953 if (E->getSubExpr()->getType()->isAnyComplexType())
9954 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: false);
9955 return true;
9956}
9957
9958bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
9959 assert(E->getSubExpr()->getType()->isAnyComplexType() &&
9960 "lvalue __imag__ on scalar?");
9961 if (!Visit(S: E->getSubExpr()))
9962 return false;
9963 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: true);
9964 return true;
9965}
9966
9967bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) {
9968 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9969 return Error(E: UO);
9970
9971 if (!this->Visit(S: UO->getSubExpr()))
9972 return false;
9973
9974 return handleIncDec(
9975 Info&: this->Info, E: UO, LVal: Result, LValType: UO->getSubExpr()->getType(),
9976 IsIncrement: UO->isIncrementOp(), Old: nullptr);
9977}
9978
9979bool LValueExprEvaluator::VisitCompoundAssignOperator(
9980 const CompoundAssignOperator *CAO) {
9981 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9982 return Error(E: CAO);
9983
9984 bool Success = true;
9985
9986 // C++17 onwards require that we evaluate the RHS first.
9987 APValue RHS;
9988 if (!Evaluate(Result&: RHS, Info&: this->Info, E: CAO->getRHS())) {
9989 if (!Info.noteFailure())
9990 return false;
9991 Success = false;
9992 }
9993
9994 // The overall lvalue result is the result of evaluating the LHS.
9995 if (!this->Visit(S: CAO->getLHS()) || !Success)
9996 return false;
9997
9998 return handleCompoundAssignment(
9999 Info&: this->Info, E: CAO,
10000 LVal: Result, LValType: CAO->getLHS()->getType(), PromotedLValType: CAO->getComputationLHSType(),
10001 Opcode: CAO->getOpForCompoundAssignment(Opc: CAO->getOpcode()), RVal: RHS);
10002}
10003
10004bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
10005 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
10006 return Error(E);
10007
10008 bool Success = true;
10009
10010 // C++17 onwards require that we evaluate the RHS first.
10011 APValue NewVal;
10012 if (!Evaluate(Result&: NewVal, Info&: this->Info, E: E->getRHS())) {
10013 if (!Info.noteFailure())
10014 return false;
10015 Success = false;
10016 }
10017
10018 if (!this->Visit(S: E->getLHS()) || !Success)
10019 return false;
10020
10021 if (Info.getLangOpts().CPlusPlus20 &&
10022 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: E->getLHS(), LHS: Result))
10023 return false;
10024
10025 return handleAssignment(Info&: this->Info, E, LVal: Result, LValType: E->getLHS()->getType(),
10026 Val&: NewVal);
10027}
10028
10029//===----------------------------------------------------------------------===//
10030// Pointer Evaluation
10031//===----------------------------------------------------------------------===//
10032
10033/// Convenience function. LVal's base must be a call to an alloc_size
10034/// function.
10035static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
10036 const LValue &LVal,
10037 llvm::APInt &Result) {
10038 assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) &&
10039 "Can't get the size of a non alloc_size function");
10040 const auto *Base = LVal.getLValueBase().get<const Expr *>();
10041 const CallExpr *CE = tryUnwrapAllocSizeCall(E: Base);
10042 std::optional<llvm::APInt> Size =
10043 CE->evaluateBytesReturnedByAllocSizeCall(Ctx);
10044 if (!Size)
10045 return false;
10046
10047 Result = std::move(*Size);
10048 return true;
10049}
10050
10051/// Attempts to evaluate the given LValueBase as the result of a call to
10052/// a function with the alloc_size attribute. If it was possible to do so, this
10053/// function will return true, make Result's Base point to said function call,
10054/// and mark Result's Base as invalid.
10055static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base,
10056 LValue &Result) {
10057 if (Base.isNull())
10058 return false;
10059
10060 // Because we do no form of static analysis, we only support const variables.
10061 //
10062 // Additionally, we can't support parameters, nor can we support static
10063 // variables (in the latter case, use-before-assign isn't UB; in the former,
10064 // we have no clue what they'll be assigned to).
10065 const auto *VD =
10066 dyn_cast_or_null<VarDecl>(Val: Base.dyn_cast<const ValueDecl *>());
10067 if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified())
10068 return false;
10069
10070 const Expr *Init = VD->getAnyInitializer();
10071 if (!Init || Init->getType().isNull())
10072 return false;
10073
10074 const Expr *E = Init->IgnoreParens();
10075 if (!tryUnwrapAllocSizeCall(E))
10076 return false;
10077
10078 // Store E instead of E unwrapped so that the type of the LValue's base is
10079 // what the user wanted.
10080 Result.setInvalid(B: E);
10081
10082 QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType();
10083 Result.addUnsizedArray(Info, E, ElemTy: Pointee);
10084 return true;
10085}
10086
10087namespace {
10088class PointerExprEvaluator
10089 : public ExprEvaluatorBase<PointerExprEvaluator> {
10090 LValue &Result;
10091 bool InvalidBaseOK;
10092
10093 bool Success(const Expr *E) {
10094 Result.set(B: E);
10095 return true;
10096 }
10097
10098 bool evaluateLValue(const Expr *E, LValue &Result) {
10099 return EvaluateLValue(E, Result, Info, InvalidBaseOK);
10100 }
10101
10102 bool evaluatePointer(const Expr *E, LValue &Result) {
10103 return EvaluatePointer(E, Result, Info, InvalidBaseOK);
10104 }
10105
10106 bool visitNonBuiltinCallExpr(const CallExpr *E);
10107public:
10108
10109 PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK)
10110 : ExprEvaluatorBaseTy(info), Result(Result),
10111 InvalidBaseOK(InvalidBaseOK) {}
10112
10113 bool Success(const APValue &V, const Expr *E) {
10114 Result.setFrom(Ctx: Info.Ctx, V);
10115 return true;
10116 }
10117 bool ZeroInitialization(const Expr *E) {
10118 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10119 return true;
10120 }
10121
10122 bool VisitBinaryOperator(const BinaryOperator *E);
10123 bool VisitCastExpr(const CastExpr* E);
10124 bool VisitUnaryAddrOf(const UnaryOperator *E);
10125 bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
10126 { return Success(E); }
10127 bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
10128 if (E->isExpressibleAsConstantInitializer())
10129 return Success(E);
10130 if (Info.noteFailure())
10131 EvaluateIgnoredValue(Info, E: E->getSubExpr());
10132 return Error(E);
10133 }
10134 bool VisitObjCArrayLiteral(const ObjCArrayLiteral *E) {
10135 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
10136 }
10137 bool VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) {
10138 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
10139 }
10140 bool VisitAddrLabelExpr(const AddrLabelExpr *E)
10141 { return Success(E); }
10142 bool VisitCallExpr(const CallExpr *E);
10143 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
10144 bool VisitBlockExpr(const BlockExpr *E) {
10145 if (!E->getBlockDecl()->hasCaptures())
10146 return Success(E);
10147 return Error(E);
10148 }
10149 bool VisitCXXThisExpr(const CXXThisExpr *E) {
10150 auto DiagnoseInvalidUseOfThis = [&] {
10151 if (Info.getLangOpts().CPlusPlus11)
10152 Info.FFDiag(E, DiagId: diag::note_constexpr_this) << E->isImplicit();
10153 else
10154 Info.FFDiag(E);
10155 };
10156
10157 // Can't look at 'this' when checking a potential constant expression.
10158 if (Info.checkingPotentialConstantExpression())
10159 return false;
10160
10161 bool IsExplicitLambda =
10162 isLambdaCallWithExplicitObjectParameter(DC: Info.CurrentCall->Callee);
10163 if (!IsExplicitLambda) {
10164 if (!Info.CurrentCall->This) {
10165 DiagnoseInvalidUseOfThis();
10166 return false;
10167 }
10168
10169 Result = *Info.CurrentCall->This;
10170 }
10171
10172 if (isLambdaCallOperator(DC: Info.CurrentCall->Callee)) {
10173 // Ensure we actually have captured 'this'. If something was wrong with
10174 // 'this' capture, the error would have been previously reported.
10175 // Otherwise we can be inside of a default initialization of an object
10176 // declared by lambda's body, so no need to return false.
10177 if (!Info.CurrentCall->LambdaThisCaptureField) {
10178 if (IsExplicitLambda && !Info.CurrentCall->This) {
10179 DiagnoseInvalidUseOfThis();
10180 return false;
10181 }
10182
10183 return true;
10184 }
10185
10186 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
10187 return HandleLambdaCapture(
10188 Info, E, Result, MD, FD: Info.CurrentCall->LambdaThisCaptureField,
10189 LValueToRValueConversion: Info.CurrentCall->LambdaThisCaptureField->getType()->isPointerType());
10190 }
10191 return true;
10192 }
10193
10194 bool VisitCXXNewExpr(const CXXNewExpr *E);
10195
10196 bool VisitSourceLocExpr(const SourceLocExpr *E) {
10197 assert(!E->isIntType() && "SourceLocExpr isn't a pointer type?");
10198 APValue LValResult = E->EvaluateInContext(
10199 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
10200 Result.setFrom(Ctx: Info.Ctx, V: LValResult);
10201 return true;
10202 }
10203
10204 bool VisitEmbedExpr(const EmbedExpr *E) {
10205 llvm::report_fatal_error(reason: "Not yet implemented for ExprConstant.cpp");
10206 return true;
10207 }
10208
10209 bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) {
10210 std::string ResultStr = E->ComputeName(Context&: Info.Ctx);
10211
10212 QualType CharTy = Info.Ctx.CharTy.withConst();
10213 APInt Size(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType()),
10214 ResultStr.size() + 1);
10215 QualType ArrayTy = Info.Ctx.getConstantArrayType(
10216 EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
10217
10218 StringLiteral *SL =
10219 StringLiteral::Create(Ctx: Info.Ctx, Str: ResultStr, Kind: StringLiteralKind::Ordinary,
10220 /*Pascal*/ false, Ty: ArrayTy, Locs: E->getLocation());
10221
10222 evaluateLValue(E: SL, Result);
10223 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: ArrayTy));
10224 return true;
10225 }
10226
10227 // FIXME: Missing: @protocol, @selector
10228};
10229} // end anonymous namespace
10230
10231static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info,
10232 bool InvalidBaseOK) {
10233 assert(!E->isValueDependent());
10234 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
10235 return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
10236}
10237
10238bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
10239 if (E->getOpcode() != BO_Add &&
10240 E->getOpcode() != BO_Sub)
10241 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
10242
10243 const Expr *PExp = E->getLHS();
10244 const Expr *IExp = E->getRHS();
10245 if (IExp->getType()->isPointerType())
10246 std::swap(a&: PExp, b&: IExp);
10247
10248 bool EvalPtrOK = evaluatePointer(E: PExp, Result);
10249 if (!EvalPtrOK && !Info.noteFailure())
10250 return false;
10251
10252 llvm::APSInt Offset;
10253 if (!EvaluateInteger(E: IExp, Result&: Offset, Info) || !EvalPtrOK)
10254 return false;
10255
10256 if (E->getOpcode() == BO_Sub)
10257 negateAsSigned(Int&: Offset);
10258
10259 QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType();
10260 return HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: Pointee, Adjustment: Offset);
10261}
10262
10263bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
10264 // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*'
10265 // operator, neither operator is evaluated and the result is as if both were
10266 // omitted (except that the operators' constraints, already enforced by Sema,
10267 // still apply, and the result is not an lvalue). So '&*p' is just the pointer
10268 // value 'p' with no dereference, and forming it is therefore not undefined
10269 // behavior even when 'p' is null, e.g. '&*(int *)0'. Evaluate the pointer
10270 // operand directly so we don't spuriously diagnose a null dereference.
10271 if (!Info.getLangOpts().CPlusPlus) {
10272 const Expr *Sub = E->getSubExpr()->IgnoreParens();
10273 if (const auto *Deref = dyn_cast<UnaryOperator>(Val: Sub);
10274 Deref && Deref->getOpcode() == UO_Deref)
10275 return evaluatePointer(E: Deref->getSubExpr(), Result);
10276 }
10277 return evaluateLValue(E: E->getSubExpr(), Result);
10278}
10279
10280// Is the provided decl 'std::source_location::current'?
10281static bool IsDeclSourceLocationCurrent(const FunctionDecl *FD) {
10282 if (!FD)
10283 return false;
10284 const IdentifierInfo *FnII = FD->getIdentifier();
10285 if (!FnII || !FnII->isStr(Str: "current"))
10286 return false;
10287
10288 const auto *RD = dyn_cast<RecordDecl>(Val: FD->getParent());
10289 if (!RD)
10290 return false;
10291
10292 const IdentifierInfo *ClassII = RD->getIdentifier();
10293 return RD->isInStdNamespace() && ClassII && ClassII->isStr(Str: "source_location");
10294}
10295
10296bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
10297 const Expr *SubExpr = E->getSubExpr();
10298
10299 switch (E->getCastKind()) {
10300 default:
10301 break;
10302 case CK_BitCast:
10303 case CK_CPointerToObjCPointerCast:
10304 case CK_BlockPointerToObjCPointerCast:
10305 case CK_AnyPointerToBlockPointerCast:
10306 case CK_AddressSpaceConversion:
10307 if (!Visit(S: SubExpr))
10308 return false;
10309 if (E->getType()->isFunctionPointerType() ||
10310 SubExpr->getType()->isFunctionPointerType()) {
10311 // Casting between two function pointer types, or between a function
10312 // pointer and an object pointer, is always a reinterpret_cast.
10313 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10314 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10315 << Info.Ctx.getLangOpts().CPlusPlus;
10316 Result.Designator.setInvalid();
10317 } else if (!E->getType()->isVoidPointerType()) {
10318 // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
10319 // permitted in constant expressions in C++11. Bitcasts from cv void* are
10320 // also static_casts, but we disallow them as a resolution to DR1312.
10321 //
10322 // In some circumstances, we permit casting from void* to cv1 T*, when the
10323 // actual pointee object is actually a cv2 T.
10324 bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&
10325 !Result.IsNullPtr;
10326 bool VoidPtrCastMaybeOK =
10327 Result.IsNullPtr ||
10328 (HasValidResult &&
10329 Info.Ctx.hasSimilarType(T1: Result.Designator.getType(Ctx&: Info.Ctx),
10330 T2: E->getType()->getPointeeType()));
10331 // 1. We'll allow it in std::allocator::allocate, and anything which that
10332 // calls.
10333 // 2. HACK 2022-03-28: Work around an issue with libstdc++'s
10334 // <source_location> header. Fixed in GCC 12 and later (2022-04-??).
10335 // We'll allow it in the body of std::source_location::current. GCC's
10336 // implementation had a parameter of type `void*`, and casts from
10337 // that back to `const __impl*` in its body.
10338 if (VoidPtrCastMaybeOK &&
10339 (Info.getStdAllocatorCaller(FnName: "allocate") ||
10340 IsDeclSourceLocationCurrent(FD: Info.CurrentCall->Callee) ||
10341 Info.getLangOpts().CPlusPlus26)) {
10342 // Permitted.
10343 } else {
10344 if (SubExpr->getType()->isVoidPointerType() &&
10345 Info.getLangOpts().CPlusPlus) {
10346 if (HasValidResult)
10347 CCEDiag(E, D: diag::note_constexpr_invalid_void_star_cast)
10348 << SubExpr->getType() << Info.getLangOpts().CPlusPlus26
10349 << Result.Designator.getType(Ctx&: Info.Ctx).getCanonicalType()
10350 << E->getType()->getPointeeType();
10351 else
10352 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10353 << diag::ConstexprInvalidCastKind::CastFrom
10354 << SubExpr->getType();
10355 } else
10356 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10357 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10358 << Info.Ctx.getLangOpts().CPlusPlus;
10359 Result.Designator.setInvalid();
10360 }
10361 }
10362 if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr)
10363 ZeroInitialization(E);
10364 return true;
10365
10366 case CK_DerivedToBase:
10367 case CK_UncheckedDerivedToBase:
10368 if (!evaluatePointer(E: E->getSubExpr(), Result))
10369 return false;
10370 if (!Result.Base && Result.Offset.isZero())
10371 return true;
10372
10373 // Now figure out the necessary offset to add to the base LV to get from
10374 // the derived class to the base class.
10375 return HandleLValueBasePath(Info, E, Type: E->getSubExpr()->getType()->
10376 castAs<PointerType>()->getPointeeType(),
10377 Result);
10378
10379 case CK_BaseToDerived:
10380 if (!Visit(S: E->getSubExpr()))
10381 return false;
10382 if (!Result.Base && Result.Offset.isZero())
10383 return true;
10384 return HandleBaseToDerivedCast(Info, E, Result);
10385
10386 case CK_Dynamic:
10387 if (!Visit(S: E->getSubExpr()))
10388 return false;
10389 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
10390
10391 case CK_NullToPointer:
10392 VisitIgnoredValue(E: E->getSubExpr());
10393 return ZeroInitialization(E);
10394
10395 case CK_IntegralToPointer: {
10396 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10397 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10398 << Info.Ctx.getLangOpts().CPlusPlus;
10399
10400 APValue Value;
10401 if (!EvaluateIntegerOrLValue(E: SubExpr, Result&: Value, Info))
10402 break;
10403
10404 if (Value.isInt()) {
10405 unsigned Size = Info.Ctx.getTypeSize(T: E->getType());
10406 uint64_t N = Value.getInt().extOrTrunc(width: Size).getZExtValue();
10407 if (N == Info.Ctx.getTargetNullPointerValue(QT: E->getType())) {
10408 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10409 } else {
10410 Result.Base = (Expr *)nullptr;
10411 Result.InvalidBase = false;
10412 Result.Offset = CharUnits::fromQuantity(Quantity: N);
10413 Result.Designator.setInvalid();
10414 Result.IsNullPtr = false;
10415 }
10416 return true;
10417 } else {
10418 // In rare instances, the value isn't an lvalue.
10419 // For example, when the value is the difference between the addresses of
10420 // two labels. We reject that as a constant expression because we can't
10421 // compute a valid offset to convert into a pointer.
10422 if (!Value.isLValue())
10423 return false;
10424
10425 // Cast is of an lvalue, no need to change value.
10426 Result.setFrom(Ctx: Info.Ctx, V: Value);
10427 return true;
10428 }
10429 }
10430
10431 case CK_ArrayToPointerDecay: {
10432 if (SubExpr->isGLValue()) {
10433 if (!evaluateLValue(E: SubExpr, Result))
10434 return false;
10435 } else {
10436 APValue &Value = Info.CurrentCall->createTemporary(
10437 Key: SubExpr, T: SubExpr->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
10438 if (!EvaluateInPlace(Result&: Value, Info, This: Result, E: SubExpr))
10439 return false;
10440 }
10441 // The result is a pointer to the first element of the array.
10442 auto *AT = Info.Ctx.getAsArrayType(T: SubExpr->getType());
10443 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT))
10444 Result.addArray(Info, E, CAT);
10445 else
10446 Result.addUnsizedArray(Info, E, ElemTy: AT->getElementType());
10447 return true;
10448 }
10449
10450 case CK_FunctionToPointerDecay:
10451 return evaluateLValue(E: SubExpr, Result);
10452
10453 case CK_LValueToRValue: {
10454 LValue LVal;
10455 if (!evaluateLValue(E: E->getSubExpr(), Result&: LVal))
10456 return false;
10457
10458 APValue RVal;
10459 // Note, we use the subexpression's type in order to retain cv-qualifiers.
10460 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
10461 LVal, RVal))
10462 return InvalidBaseOK &&
10463 evaluateLValueAsAllocSize(Info, Base: LVal.Base, Result);
10464 return Success(V: RVal, E);
10465 }
10466 }
10467
10468 return ExprEvaluatorBaseTy::VisitCastExpr(E);
10469}
10470
10471static CharUnits GetAlignOfType(const ASTContext &Ctx, QualType T,
10472 UnaryExprOrTypeTrait ExprKind) {
10473 // C++ [expr.alignof]p3:
10474 // When alignof is applied to a reference type, the result is the
10475 // alignment of the referenced type.
10476 T = T.getNonReferenceType();
10477
10478 if (T.getQualifiers().hasUnaligned())
10479 return CharUnits::One();
10480
10481 const bool AlignOfReturnsPreferred =
10482 Ctx.getLangOpts().isCompatibleWith(Version: LangOptions::ClangABI::Ver7);
10483
10484 // __alignof is defined to return the preferred alignment.
10485 // Before 8, clang returned the preferred alignment for alignof and _Alignof
10486 // as well.
10487 if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
10488 return Ctx.toCharUnitsFromBits(BitSize: Ctx.getPreferredTypeAlign(T: T.getTypePtr()));
10489 // alignof and _Alignof are defined to return the ABI alignment.
10490 else if (ExprKind == UETT_AlignOf)
10491 return Ctx.getTypeAlignInChars(T: T.getTypePtr());
10492 else
10493 llvm_unreachable("GetAlignOfType on a non-alignment ExprKind");
10494}
10495
10496// Convert a builtin ID to the canonical x86 builtin ID the constant evaluators
10497// dispatch on in their x86 target-specific cases, or 0 if \p BuiltinOp is a
10498// target builtin those cases should not handle.
10499//
10500// Target-independent builtins are returned unchanged. Target builtin IDs of
10501// different targets overlap (each target numbers its builtins from
10502// Builtin::FirstTSBuiltin), so a target builtin ID is only meaningful for the
10503// target that owns it. Determine the owning target (translating an auxiliary ID
10504// back to its canonical value) and only return the ID when x86 owns it;
10505// otherwise an overlapping ID could be misinterpreted as an unrelated x86
10506// builtin.
10507unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
10508 unsigned BuiltinOp) {
10509 // Target-independent builtins have the same ID regardless of the target, so
10510 // they can be dispatched as-is. This is the common case and is intentionally
10511 // kept to a single comparison so callers can use this on hot paths (e.g. the
10512 // bytecode interpreter's builtin dispatch) without re-deriving the ID from
10513 // the call expression.
10514 if (BuiltinOp < Builtin::FirstTSBuiltin)
10515 return BuiltinOp;
10516
10517 // Determine the target that owns this builtin, translating an auxiliary ID
10518 // back to its canonical value.
10519 const TargetInfo *OwningTarget;
10520 if (Ctx.BuiltinInfo.isAuxBuiltinID(ID: BuiltinOp)) {
10521 OwningTarget = Ctx.getAuxTargetInfo();
10522 BuiltinOp = Ctx.BuiltinInfo.getAuxBuiltinID(ID: BuiltinOp);
10523 } else {
10524 OwningTarget = &Ctx.getTargetInfo();
10525 }
10526
10527 if (!OwningTarget)
10528 return 0;
10529
10530 // x86 and x86_64 share a single builtin set and are the only architectures
10531 // whose target-specific builtins the constant evaluators currently fold.
10532 switch (OwningTarget->getTriple().getArch()) {
10533 case llvm::Triple::x86:
10534 case llvm::Triple::x86_64:
10535 return BuiltinOp;
10536 default:
10537 return 0;
10538 }
10539}
10540
10541unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
10542 const CallExpr *E) {
10543 return ConvertBuiltinIDToX86BuiltinID(Ctx, BuiltinOp: E->getBuiltinCallee());
10544}
10545
10546CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E,
10547 UnaryExprOrTypeTrait ExprKind) {
10548 E = E->IgnoreParens();
10549
10550 // The kinds of expressions that we have special-case logic here for
10551 // should be kept up to date with the special checks for those
10552 // expressions in Sema.
10553
10554 // alignof decl is always accepted, even if it doesn't make sense: we default
10555 // to 1 in those cases.
10556 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E))
10557 return Ctx.getDeclAlign(D: DRE->getDecl(),
10558 /*RefAsPointee*/ ForAlignof: true);
10559
10560 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: E))
10561 return Ctx.getDeclAlign(D: ME->getMemberDecl(),
10562 /*RefAsPointee*/ ForAlignof: true);
10563
10564 return GetAlignOfType(Ctx, T: E->getType(), ExprKind);
10565}
10566
10567static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) {
10568 if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>())
10569 return Info.Ctx.getDeclAlign(D: VD);
10570 if (const auto *E = Value.Base.dyn_cast<const Expr *>())
10571 return GetAlignOfExpr(Ctx: Info.Ctx, E, ExprKind: UETT_AlignOf);
10572 return GetAlignOfType(Ctx: Info.Ctx, T: Value.Base.getTypeInfoType(), ExprKind: UETT_AlignOf);
10573}
10574
10575/// Evaluate the value of the alignment argument to __builtin_align_{up,down},
10576/// __builtin_is_aligned and __builtin_assume_aligned.
10577static bool getAlignmentArgument(const Expr *E, QualType ForType,
10578 EvalInfo &Info, APSInt &Alignment) {
10579 if (!EvaluateInteger(E, Result&: Alignment, Info))
10580 return false;
10581 if (Alignment < 0 || !Alignment.isPowerOf2()) {
10582 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_alignment) << Alignment;
10583 return false;
10584 }
10585 unsigned SrcWidth = Info.Ctx.getIntWidth(T: ForType);
10586 APSInt MaxValue(APInt::getOneBitSet(numBits: SrcWidth, BitNo: SrcWidth - 1));
10587 if (APSInt::compareValues(I1: Alignment, I2: MaxValue) > 0) {
10588 Info.FFDiag(E, DiagId: diag::note_constexpr_alignment_too_big)
10589 << MaxValue << ForType << Alignment;
10590 return false;
10591 }
10592 // Ensure both alignment and source value have the same bit width so that we
10593 // don't assert when computing the resulting value.
10594 APSInt ExtAlignment =
10595 APSInt(Alignment.zextOrTrunc(width: SrcWidth), /*isUnsigned=*/true);
10596 assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 &&
10597 "Alignment should not be changed by ext/trunc");
10598 Alignment = ExtAlignment;
10599 assert(Alignment.getBitWidth() == SrcWidth);
10600 return true;
10601}
10602
10603// To be clear: this happily visits unsupported builtins. Better name welcomed.
10604bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {
10605 if (ExprEvaluatorBaseTy::VisitCallExpr(E))
10606 return true;
10607
10608 if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr()))
10609 return false;
10610
10611 Result.setInvalid(B: E);
10612 QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType();
10613 Result.addUnsizedArray(Info, E, ElemTy: PointeeTy);
10614 return true;
10615}
10616
10617bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
10618 if (!IsConstantEvaluatedBuiltinCall(E))
10619 return visitNonBuiltinCallExpr(E);
10620 return VisitBuiltinCallExpr(E, BuiltinOp: ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E));
10621}
10622
10623// Determine if T is a character type for which we guarantee that
10624// sizeof(T) == 1.
10625static bool isOneByteCharacterType(QualType T) {
10626 return T->isCharType() || T->isChar8Type();
10627}
10628
10629bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
10630 unsigned BuiltinOp) {
10631 if (IsOpaqueConstantCall(E))
10632 return Success(E);
10633
10634 switch (BuiltinOp) {
10635 case Builtin::BIaddressof:
10636 case Builtin::BI__addressof:
10637 case Builtin::BI__builtin_addressof:
10638 return evaluateLValue(E: E->getArg(Arg: 0), Result);
10639 case Builtin::BI__builtin_assume_aligned: {
10640 // We need to be very careful here because: if the pointer does not have the
10641 // asserted alignment, then the behavior is undefined, and undefined
10642 // behavior is non-constant.
10643 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10644 return false;
10645
10646 LValue OffsetResult(Result);
10647 APSInt Alignment;
10648 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10649 Alignment))
10650 return false;
10651 CharUnits Align = CharUnits::fromQuantity(Quantity: Alignment.getZExtValue());
10652
10653 if (E->getNumArgs() > 2) {
10654 APSInt Offset;
10655 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Offset, Info))
10656 return false;
10657
10658 int64_t AdditionalOffset = -Offset.getZExtValue();
10659 OffsetResult.Offset += CharUnits::fromQuantity(Quantity: AdditionalOffset);
10660 }
10661
10662 // If there is a base object, then it must have the correct alignment.
10663 if (OffsetResult.Base) {
10664 CharUnits BaseAlignment = getBaseAlignment(Info, Value: OffsetResult);
10665
10666 if (BaseAlignment < Align) {
10667 Result.Designator.setInvalid();
10668 CCEDiag(E: E->getArg(Arg: 0), D: diag::note_constexpr_baa_insufficient_alignment)
10669 << 0 << BaseAlignment.getQuantity() << Align.getQuantity();
10670 return false;
10671 }
10672 }
10673
10674 // The offset must also have the correct alignment.
10675 if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) {
10676 Result.Designator.setInvalid();
10677
10678 (OffsetResult.Base
10679 ? CCEDiag(E: E->getArg(Arg: 0),
10680 D: diag::note_constexpr_baa_insufficient_alignment)
10681 << 1
10682 : CCEDiag(E: E->getArg(Arg: 0),
10683 D: diag::note_constexpr_baa_value_insufficient_alignment))
10684 << OffsetResult.Offset.getQuantity() << Align.getQuantity();
10685 return false;
10686 }
10687
10688 return true;
10689 }
10690 case Builtin::BI__builtin_align_up:
10691 case Builtin::BI__builtin_align_down: {
10692 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10693 return false;
10694 APSInt Alignment;
10695 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10696 Alignment))
10697 return false;
10698 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Result);
10699 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Result.Offset);
10700 // For align_up/align_down, we can return the same value if the alignment
10701 // is known to be greater or equal to the requested value.
10702 if (PtrAlign.getQuantity() >= Alignment)
10703 return true;
10704
10705 // The alignment could be greater than the minimum at run-time, so we cannot
10706 // infer much about the resulting pointer value. One case is possible:
10707 // For `_Alignas(32) char buf[N]; __builtin_align_down(&buf[idx], 32)` we
10708 // can infer the correct index if the requested alignment is smaller than
10709 // the base alignment so we can perform the computation on the offset.
10710 if (BaseAlignment.getQuantity() >= Alignment) {
10711 assert(Alignment.getBitWidth() <= 64 &&
10712 "Cannot handle > 64-bit address-space");
10713 uint64_t Alignment64 = Alignment.getZExtValue();
10714 CharUnits NewOffset = CharUnits::fromQuantity(
10715 Quantity: BuiltinOp == Builtin::BI__builtin_align_down
10716 ? llvm::alignDown(Value: Result.Offset.getQuantity(), Align: Alignment64)
10717 : llvm::alignTo(Value: Result.Offset.getQuantity(), Align: Alignment64));
10718 Result.adjustOffset(N: NewOffset - Result.Offset);
10719 // TODO: diagnose out-of-bounds values/only allow for arrays?
10720 return true;
10721 }
10722 // Otherwise, we cannot constant-evaluate the result.
10723 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_adjust)
10724 << Alignment;
10725 return false;
10726 }
10727 case Builtin::BI__builtin_operator_new:
10728 return HandleOperatorNewCall(Info, E, Result);
10729 case Builtin::BI__builtin_launder:
10730 return evaluatePointer(E: E->getArg(Arg: 0), Result);
10731 case Builtin::BIstrchr:
10732 case Builtin::BIwcschr:
10733 case Builtin::BImemchr:
10734 case Builtin::BIwmemchr:
10735 if (Info.getLangOpts().CPlusPlus11)
10736 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10737 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10738 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10739 else
10740 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10741 [[fallthrough]];
10742 case Builtin::BI__builtin_strchr:
10743 case Builtin::BI__builtin_wcschr:
10744 case Builtin::BI__builtin_memchr:
10745 case Builtin::BI__builtin_char_memchr:
10746 case Builtin::BI__builtin_wmemchr: {
10747 if (!Visit(S: E->getArg(Arg: 0)))
10748 return false;
10749 APSInt Desired;
10750 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Desired, Info))
10751 return false;
10752 uint64_t MaxLength = uint64_t(-1);
10753 if (BuiltinOp != Builtin::BIstrchr &&
10754 BuiltinOp != Builtin::BIwcschr &&
10755 BuiltinOp != Builtin::BI__builtin_strchr &&
10756 BuiltinOp != Builtin::BI__builtin_wcschr) {
10757 APSInt N;
10758 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10759 return false;
10760 MaxLength = N.getZExtValue();
10761 }
10762 // We cannot find the value if there are no candidates to match against.
10763 if (MaxLength == 0u)
10764 return ZeroInitialization(E);
10765 if (!Result.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
10766 Result.Designator.Invalid)
10767 return false;
10768 QualType CharTy = Result.Designator.getType(Ctx&: Info.Ctx);
10769 bool IsRawByte = BuiltinOp == Builtin::BImemchr ||
10770 BuiltinOp == Builtin::BI__builtin_memchr;
10771 assert(IsRawByte ||
10772 Info.Ctx.hasSameUnqualifiedType(
10773 CharTy, E->getArg(0)->getType()->getPointeeType()));
10774 // Pointers to const void may point to objects of incomplete type.
10775 if (IsRawByte && CharTy->isIncompleteType()) {
10776 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_incomplete_type) << CharTy;
10777 return false;
10778 }
10779 // Give up on byte-oriented matching against multibyte elements.
10780 // FIXME: We can compare the bytes in the correct order.
10781 if (IsRawByte && !isOneByteCharacterType(T: CharTy)) {
10782 Info.FFDiag(E, DiagId: diag::note_constexpr_memchr_unsupported)
10783 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy;
10784 return false;
10785 }
10786 // Figure out what value we're actually looking for (after converting to
10787 // the corresponding unsigned type if necessary).
10788 uint64_t DesiredVal;
10789 bool StopAtNull = false;
10790 switch (BuiltinOp) {
10791 case Builtin::BIstrchr:
10792 case Builtin::BI__builtin_strchr:
10793 // strchr compares directly to the passed integer, and therefore
10794 // always fails if given an int that is not a char.
10795 if (!APSInt::isSameValue(I1: HandleIntToIntCast(Info, E, DestType: CharTy,
10796 SrcType: E->getArg(Arg: 1)->getType(),
10797 Value: Desired),
10798 I2: Desired))
10799 return ZeroInitialization(E);
10800 StopAtNull = true;
10801 [[fallthrough]];
10802 case Builtin::BImemchr:
10803 case Builtin::BI__builtin_memchr:
10804 case Builtin::BI__builtin_char_memchr:
10805 // memchr compares by converting both sides to unsigned char. That's also
10806 // correct for strchr if we get this far (to cope with plain char being
10807 // unsigned in the strchr case).
10808 DesiredVal = Desired.trunc(width: Info.Ctx.getCharWidth()).getZExtValue();
10809 break;
10810
10811 case Builtin::BIwcschr:
10812 case Builtin::BI__builtin_wcschr:
10813 StopAtNull = true;
10814 [[fallthrough]];
10815 case Builtin::BIwmemchr:
10816 case Builtin::BI__builtin_wmemchr:
10817 // wcschr and wmemchr are given a wchar_t to look for. Just use it.
10818 DesiredVal = Desired.getZExtValue();
10819 break;
10820 }
10821
10822 for (; MaxLength; --MaxLength) {
10823 APValue Char;
10824 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: Result, RVal&: Char) ||
10825 !Char.isInt())
10826 return false;
10827 if (Char.getInt().getZExtValue() == DesiredVal)
10828 return true;
10829 if (StopAtNull && !Char.getInt())
10830 break;
10831 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: CharTy, Adjustment: 1))
10832 return false;
10833 }
10834 // Not found: return nullptr.
10835 return ZeroInitialization(E);
10836 }
10837
10838 case Builtin::BImemcpy:
10839 case Builtin::BImemmove:
10840 case Builtin::BIwmemcpy:
10841 case Builtin::BIwmemmove:
10842 if (Info.getLangOpts().CPlusPlus11)
10843 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10844 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10845 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10846 else
10847 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10848 [[fallthrough]];
10849 case Builtin::BI__builtin_memcpy:
10850 case Builtin::BI__builtin_memmove:
10851 case Builtin::BI__builtin_wmemcpy:
10852 case Builtin::BI__builtin_wmemmove: {
10853 bool WChar = BuiltinOp == Builtin::BIwmemcpy ||
10854 BuiltinOp == Builtin::BIwmemmove ||
10855 BuiltinOp == Builtin::BI__builtin_wmemcpy ||
10856 BuiltinOp == Builtin::BI__builtin_wmemmove;
10857 bool Move = BuiltinOp == Builtin::BImemmove ||
10858 BuiltinOp == Builtin::BIwmemmove ||
10859 BuiltinOp == Builtin::BI__builtin_memmove ||
10860 BuiltinOp == Builtin::BI__builtin_wmemmove;
10861
10862 // The result of mem* is the first argument.
10863 if (!Visit(S: E->getArg(Arg: 0)))
10864 return false;
10865 LValue Dest = Result;
10866
10867 LValue Src;
10868 if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Src, Info))
10869 return false;
10870
10871 APSInt N;
10872 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10873 return false;
10874 assert(!N.isSigned() && "memcpy and friends take an unsigned size");
10875
10876 // If the size is zero, we treat this as always being a valid no-op.
10877 // (Even if one of the src and dest pointers is null.)
10878 if (!N)
10879 return true;
10880
10881 // Otherwise, if either of the operands is null, we can't proceed. Don't
10882 // try to determine the type of the copied objects, because there aren't
10883 // any.
10884 if (!Src.Base || !Dest.Base) {
10885 APValue Val;
10886 (!Src.Base ? Src : Dest).moveInto(V&: Val);
10887 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_null)
10888 << Move << WChar << !!Src.Base
10889 << Val.getAsString(Ctx: Info.Ctx, Ty: E->getArg(Arg: 0)->getType());
10890 return false;
10891 }
10892 if (Src.Designator.Invalid || Dest.Designator.Invalid)
10893 return false;
10894
10895 // We require that Src and Dest are both pointers to arrays of
10896 // trivially-copyable type. (For the wide version, the designator will be
10897 // invalid if the designated object is not a wchar_t.)
10898 QualType T = Dest.Designator.getType(Ctx&: Info.Ctx);
10899 QualType SrcT = Src.Designator.getType(Ctx&: Info.Ctx);
10900 if (!Info.Ctx.hasSameUnqualifiedType(T1: T, T2: SrcT)) {
10901 // FIXME: Consider using our bit_cast implementation to support this.
10902 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T;
10903 return false;
10904 }
10905 if (T->isIncompleteType()) {
10906 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_incomplete_type) << Move << T;
10907 return false;
10908 }
10909 if (!T.isTriviallyCopyableType(Context: Info.Ctx)) {
10910 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_nontrivial) << Move << T;
10911 return false;
10912 }
10913
10914 // Figure out how many T's we're copying.
10915 uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity();
10916 if (TSize == 0)
10917 return false;
10918 if (!WChar) {
10919 uint64_t Remainder;
10920 llvm::APInt OrigN = N;
10921 llvm::APInt::udivrem(LHS: OrigN, RHS: TSize, Quotient&: N, Remainder);
10922 if (Remainder) {
10923 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
10924 << Move << WChar << 0 << T << toString(I: OrigN, Radix: 10, /*Signed*/false)
10925 << (unsigned)TSize;
10926 return false;
10927 }
10928 }
10929
10930 // Check that the copying will remain within the arrays, just so that we
10931 // can give a more meaningful diagnostic. This implicitly also checks that
10932 // N fits into 64 bits.
10933 uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second;
10934 uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second;
10935 if (N.ugt(RHS: RemainingSrcSize) || N.ugt(RHS: RemainingDestSize)) {
10936 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
10937 << Move << WChar << (N.ugt(RHS: RemainingSrcSize) ? 1 : 2) << T
10938 << toString(I: N, Radix: 10, /*Signed*/false);
10939 return false;
10940 }
10941 uint64_t NElems = N.getZExtValue();
10942 uint64_t NBytes = NElems * TSize;
10943
10944 // Check for overlap.
10945 int Direction = 1;
10946 if (HasSameBase(A: Src, B: Dest)) {
10947 uint64_t SrcOffset = Src.getLValueOffset().getQuantity();
10948 uint64_t DestOffset = Dest.getLValueOffset().getQuantity();
10949 if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) {
10950 // Dest is inside the source region.
10951 if (!Move) {
10952 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
10953 return false;
10954 }
10955 // For memmove and friends, copy backwards.
10956 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: NElems - 1) ||
10957 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: NElems - 1))
10958 return false;
10959 Direction = -1;
10960 } else if (!Move && SrcOffset >= DestOffset &&
10961 SrcOffset - DestOffset < NBytes) {
10962 // Src is inside the destination region for memcpy: invalid.
10963 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
10964 return false;
10965 }
10966 }
10967
10968 while (true) {
10969 APValue Val;
10970 // FIXME: Set WantObjectRepresentation to true if we're copying a
10971 // char-like type?
10972 if (!handleLValueToRValueConversion(Info, Conv: E, Type: T, LVal: Src, RVal&: Val) ||
10973 !handleAssignment(Info, E, LVal: Dest, LValType: T, Val))
10974 return false;
10975 // Do not iterate past the last element; if we're copying backwards, that
10976 // might take us off the start of the array.
10977 if (--NElems == 0)
10978 return true;
10979 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: Direction) ||
10980 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: Direction))
10981 return false;
10982 }
10983 }
10984
10985 default:
10986 return false;
10987 }
10988}
10989
10990static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
10991 APValue &Result, const InitListExpr *ILE,
10992 QualType AllocType);
10993static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
10994 APValue &Result,
10995 const CXXConstructExpr *CCE,
10996 QualType AllocType);
10997
10998bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {
10999 if (!Info.getLangOpts().CPlusPlus20)
11000 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
11001
11002 // We cannot speculatively evaluate a delete expression.
11003 if (Info.SpeculativeEvaluationDepth)
11004 return false;
11005
11006 FunctionDecl *OperatorNew = E->getOperatorNew();
11007 QualType AllocType = E->getAllocatedType();
11008 QualType TargetType = AllocType;
11009
11010 bool IsNothrow = false;
11011 bool IsPlacement = false;
11012
11013 if (E->getNumPlacementArgs() == 1 &&
11014 E->getPlacementArg(I: 0)->getType()->isNothrowT()) {
11015 // The only new-placement list we support is of the form (std::nothrow).
11016 //
11017 // FIXME: There is no restriction on this, but it's not clear that any
11018 // other form makes any sense. We get here for cases such as:
11019 //
11020 // new (std::align_val_t{N}) X(int)
11021 //
11022 // (which should presumably be valid only if N is a multiple of
11023 // alignof(int), and in any case can't be deallocated unless N is
11024 // alignof(X) and X has new-extended alignment).
11025 LValue Nothrow;
11026 if (!EvaluateLValue(E: E->getPlacementArg(I: 0), Result&: Nothrow, Info))
11027 return false;
11028 IsNothrow = true;
11029 } else if (OperatorNew->isReservedGlobalPlacementOperator()) {
11030 if (Info.CurrentCall->isStdFunction() || Info.getLangOpts().CPlusPlus26 ||
11031 (Info.CurrentCall->CanEvalMSConstexpr &&
11032 OperatorNew->hasAttr<MSConstexprAttr>())) {
11033 if (!EvaluatePointer(E: E->getPlacementArg(I: 0), Result, Info))
11034 return false;
11035 if (Result.Designator.Invalid)
11036 return false;
11037 TargetType = E->getPlacementArg(I: 0)->getType();
11038 IsPlacement = true;
11039 } else {
11040 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
11041 << /*C++26 feature*/ 1 << E->getSourceRange();
11042 return false;
11043 }
11044 } else if (E->getNumPlacementArgs()) {
11045 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
11046 << /*Unsupported*/ 0 << E->getSourceRange();
11047 return false;
11048 } else if (!OperatorNew
11049 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
11050 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
11051 << isa<CXXMethodDecl>(Val: OperatorNew) << OperatorNew;
11052 return false;
11053 }
11054
11055 const Expr *Init = E->getInitializer();
11056 const InitListExpr *ResizedArrayILE = nullptr;
11057 const CXXConstructExpr *ResizedArrayCCE = nullptr;
11058 bool ValueInit = false;
11059
11060 if (std::optional<const Expr *> ArraySize = E->getArraySize()) {
11061 const Expr *Stripped = *ArraySize;
11062 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped);
11063 Stripped = ICE->getSubExpr())
11064 if (ICE->getCastKind() != CK_NoOp &&
11065 ICE->getCastKind() != CK_IntegralCast)
11066 break;
11067
11068 llvm::APSInt ArrayBound;
11069 if (!EvaluateInteger(E: Stripped, Result&: ArrayBound, Info))
11070 return false;
11071
11072 // C++ [expr.new]p9:
11073 // The expression is erroneous if:
11074 // -- [...] its value before converting to size_t [or] applying the
11075 // second standard conversion sequence is less than zero
11076 if (ArrayBound.isSigned() && ArrayBound.isNegative()) {
11077 if (IsNothrow)
11078 return ZeroInitialization(E);
11079
11080 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_negative)
11081 << ArrayBound << (*ArraySize)->getSourceRange();
11082 return false;
11083 }
11084
11085 // -- its value is such that the size of the allocated object would
11086 // exceed the implementation-defined limit
11087 if (!Info.CheckArraySize(Loc: ArraySize.value()->getExprLoc(),
11088 BitWidth: ConstantArrayType::getNumAddressingBits(
11089 Context: Info.Ctx, ElementType: AllocType, NumElements: ArrayBound),
11090 ElemCount: ArrayBound.getZExtValue(), /*Diag=*/!IsNothrow)) {
11091 if (IsNothrow)
11092 return ZeroInitialization(E);
11093 return false;
11094 }
11095
11096 // -- the new-initializer is a braced-init-list and the number of
11097 // array elements for which initializers are provided [...]
11098 // exceeds the number of elements to initialize
11099 if (!Init) {
11100 // No initialization is performed.
11101 } else if (isa<CXXScalarValueInitExpr>(Val: Init) ||
11102 isa<ImplicitValueInitExpr>(Val: Init)) {
11103 ValueInit = true;
11104 } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Val: Init)) {
11105 ResizedArrayCCE = CCE;
11106 } else {
11107 auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType());
11108 assert(CAT && "unexpected type for array initializer");
11109
11110 unsigned Bits =
11111 std::max(a: CAT->getSizeBitWidth(), b: ArrayBound.getBitWidth());
11112 llvm::APInt InitBound = CAT->getSize().zext(width: Bits);
11113 llvm::APInt AllocBound = ArrayBound.zext(width: Bits);
11114 if (InitBound.ugt(RHS: AllocBound)) {
11115 if (IsNothrow)
11116 return ZeroInitialization(E);
11117
11118 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_too_small)
11119 << toString(I: AllocBound, Radix: 10, /*Signed=*/false)
11120 << toString(I: InitBound, Radix: 10, /*Signed=*/false)
11121 << (*ArraySize)->getSourceRange();
11122 return false;
11123 }
11124
11125 // If the sizes differ, we must have an initializer list, and we need
11126 // special handling for this case when we initialize.
11127 if (InitBound != AllocBound)
11128 ResizedArrayILE = cast<InitListExpr>(Val: Init);
11129 }
11130
11131 AllocType = Info.Ctx.getConstantArrayType(EltTy: AllocType, ArySize: ArrayBound, SizeExpr: nullptr,
11132 ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
11133 } else if (E->isArray()) {
11134 // We have an array new-expression whose array size could not be
11135 // determined, e.g. 'new int[]()', where the bound is neither given nor
11136 // deducible from the initializer. This is ill-formed and already
11137 // diagnosed, so bail out rather than mis-evaluating a scalar allocation
11138 // as an array (which would later crash the evaluator).
11139 return false;
11140 } else {
11141 assert(!AllocType->isArrayType() &&
11142 "array allocation with non-array new");
11143 }
11144
11145 APValue *Val;
11146 if (IsPlacement) {
11147 AccessKinds AK = AK_Construct;
11148 struct FindObjectHandler {
11149 EvalInfo &Info;
11150 const Expr *E;
11151 QualType AllocType;
11152 const AccessKinds AccessKind;
11153 APValue *Value;
11154
11155 typedef bool result_type;
11156 bool failed() { return false; }
11157 bool checkConst(QualType QT) {
11158 if (QT.isConstQualified()) {
11159 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
11160 return false;
11161 }
11162 return true;
11163 }
11164 bool found(APValue &Subobj, QualType SubobjType,
11165 APValue::LValueBase Base) {
11166 if (!checkConst(QT: SubobjType))
11167 return false;
11168 // FIXME: Reject the cases where [basic.life]p8 would not permit the
11169 // old name of the object to be used to name the new object.
11170 if (!Info.Ctx.hasSimilarType(T1: SubobjType, T2: AllocType)) {
11171 Info.FFDiag(E, DiagId: diag::note_constexpr_placement_new_wrong_type)
11172 << SubobjType << AllocType;
11173 return false;
11174 }
11175 Value = &Subobj;
11176 return true;
11177 }
11178 bool found(APSInt &Value, QualType SubobjType) {
11179 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
11180 return false;
11181 }
11182 bool found(APFloat &Value, QualType SubobjType) {
11183 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
11184 return false;
11185 }
11186 } Handler = {.Info: Info, .E: E, .AllocType: AllocType, .AccessKind: AK, .Value: nullptr};
11187
11188 if (AllocType->isArrayType() &&
11189 Result.Designator.MostDerivedIsArrayElement &&
11190 Result.Designator.Entries.back().getAsArrayIndex() == 0) {
11191 // The destination of placement new is pointing to the first element
11192 // of an array. There's a special case in [expr.const]: "[...] if T is an
11193 // array type, to the first element of such an object [...]". Handle
11194 // that case here by dropping the last entry in the designator list.
11195 QualType AllocElementType =
11196 Info.Ctx.getAsArrayType(T: AllocType)->getElementType();
11197 if (Info.Ctx.hasSimilarType(T1: AllocElementType,
11198 T2: Result.Designator.MostDerivedType)) {
11199 Result.Designator.truncate(Ctx&: Info.Ctx, Base: Result.Base,
11200 NewLength: Result.Designator.MostDerivedPathLength - 1);
11201 }
11202 }
11203
11204 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: Result, LValType: AllocType);
11205 if (!Obj || !findSubobject(Info, E, Obj, Sub: Result.Designator, handler&: Handler))
11206 return false;
11207
11208 Val = Handler.Value;
11209
11210 // [basic.life]p1:
11211 // The lifetime of an object o of type T ends when [...] the storage
11212 // which the object occupies is [...] reused by an object that is not
11213 // nested within o (6.6.2).
11214 *Val = APValue();
11215 } else {
11216 // Perform the allocation and obtain a pointer to the resulting object.
11217 Val = Info.createHeapAlloc(E, T: AllocType, LV&: Result);
11218 if (!Val)
11219 return false;
11220 }
11221
11222 if (ValueInit) {
11223 ImplicitValueInitExpr VIE(AllocType);
11224 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: &VIE))
11225 return false;
11226 } else if (ResizedArrayILE) {
11227 if (!EvaluateArrayNewInitList(Info, This&: Result, Result&: *Val, ILE: ResizedArrayILE,
11228 AllocType))
11229 return false;
11230 } else if (ResizedArrayCCE) {
11231 if (!EvaluateArrayNewConstructExpr(Info, This&: Result, Result&: *Val, CCE: ResizedArrayCCE,
11232 AllocType))
11233 return false;
11234 } else if (Init) {
11235 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: Init))
11236 return false;
11237 } else if (!handleDefaultInitValue(T: AllocType, Result&: *Val)) {
11238 return false;
11239 }
11240
11241 // Array new returns a pointer to the first element, not a pointer to the
11242 // array.
11243 if (auto *AT = AllocType->getAsArrayTypeUnsafe())
11244 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val: AT));
11245
11246 return true;
11247}
11248//===----------------------------------------------------------------------===//
11249// Member Pointer Evaluation
11250//===----------------------------------------------------------------------===//
11251
11252namespace {
11253class MemberPointerExprEvaluator
11254 : public ExprEvaluatorBase<MemberPointerExprEvaluator> {
11255 MemberPtr &Result;
11256
11257 bool Success(const ValueDecl *D) {
11258 Result = MemberPtr(D);
11259 return true;
11260 }
11261public:
11262
11263 MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
11264 : ExprEvaluatorBaseTy(Info), Result(Result) {}
11265
11266 bool Success(const APValue &V, const Expr *E) {
11267 Result.setFrom(V);
11268 return true;
11269 }
11270 bool ZeroInitialization(const Expr *E) {
11271 return Success(D: (const ValueDecl*)nullptr);
11272 }
11273
11274 bool VisitCastExpr(const CastExpr *E);
11275 bool VisitUnaryAddrOf(const UnaryOperator *E);
11276};
11277} // end anonymous namespace
11278
11279static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
11280 EvalInfo &Info) {
11281 assert(!E->isValueDependent());
11282 assert(E->isPRValue() && E->getType()->isMemberPointerType());
11283 return MemberPointerExprEvaluator(Info, Result).Visit(S: E);
11284}
11285
11286bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
11287 switch (E->getCastKind()) {
11288 default:
11289 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11290
11291 case CK_NullToMemberPointer:
11292 VisitIgnoredValue(E: E->getSubExpr());
11293 return ZeroInitialization(E);
11294
11295 case CK_BaseToDerivedMemberPointer: {
11296 if (!Visit(S: E->getSubExpr()))
11297 return false;
11298 if (E->path_empty())
11299 return true;
11300 // Base-to-derived member pointer casts store the path in derived-to-base
11301 // order, so iterate backwards. The CXXBaseSpecifier also provides us with
11302 // the wrong end of the derived->base arc, so stagger the path by one class.
11303 typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
11304 for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
11305 PathI != PathE; ++PathI) {
11306 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11307 const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
11308 if (!Result.castToDerived(Derived))
11309 return Error(E);
11310 }
11311 if (!Result.castToDerived(Derived: E->getType()
11312 ->castAs<MemberPointerType>()
11313 ->getMostRecentCXXRecordDecl()))
11314 return Error(E);
11315 return true;
11316 }
11317
11318 case CK_DerivedToBaseMemberPointer:
11319 if (!Visit(S: E->getSubExpr()))
11320 return false;
11321 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11322 PathE = E->path_end(); PathI != PathE; ++PathI) {
11323 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11324 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11325 if (!Result.castToBase(Base))
11326 return Error(E);
11327 }
11328 return true;
11329 }
11330}
11331
11332bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
11333 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
11334 // member can be formed.
11335 return Success(D: cast<DeclRefExpr>(Val: E->getSubExpr())->getDecl());
11336}
11337
11338//===----------------------------------------------------------------------===//
11339// Record Evaluation
11340//===----------------------------------------------------------------------===//
11341
11342namespace {
11343 class RecordExprEvaluator
11344 : public ExprEvaluatorBase<RecordExprEvaluator> {
11345 const LValue &This;
11346 APValue &Result;
11347 public:
11348
11349 RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
11350 : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
11351
11352 bool Success(const APValue &V, const Expr *E) {
11353 Result = V;
11354 return true;
11355 }
11356 bool ZeroInitialization(const Expr *E) {
11357 return ZeroInitialization(E, T: E->getType());
11358 }
11359 bool ZeroInitialization(const Expr *E, QualType T);
11360
11361 bool VisitCallExpr(const CallExpr *E) {
11362 return handleCallExpr(E, Result, ResultSlot: &This);
11363 }
11364 bool VisitCastExpr(const CastExpr *E);
11365 bool VisitInitListExpr(const InitListExpr *E);
11366 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
11367 return VisitCXXConstructExpr(E, T: E->getType());
11368 }
11369 bool VisitLambdaExpr(const LambdaExpr *E);
11370 bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E);
11371 bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T);
11372 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E);
11373 bool VisitBinCmp(const BinaryOperator *E);
11374 bool VisitTypeTraitExpr(const TypeTraitExpr *E);
11375 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
11376 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
11377 ArrayRef<Expr *> Args);
11378 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
11379 };
11380}
11381
11382/// Perform zero-initialization on an object of non-union class type.
11383/// C++11 [dcl.init]p5:
11384/// To zero-initialize an object or reference of type T means:
11385/// [...]
11386/// -- if T is a (possibly cv-qualified) non-union class type,
11387/// each non-static data member and each base-class subobject is
11388/// zero-initialized
11389static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
11390 const RecordDecl *RD,
11391 const LValue &This, APValue &Result,
11392 bool IsCompleteClass = true) {
11393 assert(!RD->isUnion() && "Expected non-union class type");
11394 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD);
11395
11396 if (CD) {
11397 unsigned NonVirtualBases = countNonVirtualBases(RD: CD);
11398 Result =
11399 APValue(APValue::UninitStruct(), NonVirtualBases, RD->getNumFields(),
11400 IsCompleteClass ? CD->getNumVBases() : 0);
11401 } else {
11402 Result = APValue(APValue::UninitStruct(), 0, RD->getNumFields());
11403 }
11404
11405 if (RD->isInvalidDecl()) return false;
11406 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11407
11408 if (CD) {
11409 unsigned Index = 0;
11410
11411 for (const auto &B : CD->bases()) {
11412 if (B.isVirtual())
11413 continue;
11414 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
11415 LValue Subobject = This;
11416 if (!HandleLValueDirectBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11417 return false;
11418 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11419 Result&: Result.getStructBase(i: Index),
11420 /*IsCompleteClass=*/false))
11421 return false;
11422 ++Index;
11423 }
11424 }
11425
11426 for (const auto *I : RD->fields()) {
11427 // -- if T is a reference type, no initialization is performed.
11428 if (I->isUnnamedBitField() || I->getType()->isReferenceType())
11429 continue;
11430
11431 LValue Subobject = This;
11432 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: I, RL: &Layout))
11433 return false;
11434
11435 ImplicitValueInitExpr VIE(I->getType());
11436 if (!EvaluateInPlace(
11437 Result&: Result.getStructField(i: I->getFieldIndex()), Info, This: Subobject, E: &VIE))
11438 return false;
11439 }
11440
11441 if (CD && This.pointsToCompleteClass(D: CD)) {
11442 unsigned Index = 0;
11443 for (const auto &B : CD->vbases()) {
11444 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
11445 LValue Subobject = This;
11446 if (!HandleLValueDirectVirtualBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11447 return false;
11448 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11449 Result&: Result.getStructVirtualBase(i: Index),
11450 /*IsCompleteClass=*/false))
11451 return false;
11452 ++Index;
11453 }
11454 }
11455
11456 return true;
11457}
11458
11459bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) {
11460 const auto *RD = T->castAsRecordDecl();
11461 if (RD->isInvalidDecl()) return false;
11462 if (RD->isUnion()) {
11463 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
11464 // object's first non-static named data member is zero-initialized
11465 RecordDecl::field_iterator I = RD->field_begin();
11466 while (I != RD->field_end() && (*I)->isUnnamedBitField())
11467 ++I;
11468 if (I == RD->field_end()) {
11469 Result = APValue((const FieldDecl*)nullptr);
11470 return true;
11471 }
11472
11473 LValue Subobject = This;
11474 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: *I))
11475 return false;
11476 Result = APValue(*I);
11477 ImplicitValueInitExpr VIE(I->getType());
11478 return EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: &VIE);
11479 }
11480
11481 if (!Info.getLangOpts().CPlusPlus26) {
11482 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11483 CXXRD && CXXRD->getNumVBases()) {
11484 Info.FFDiag(E, DiagId: diag::note_constexpr_virtual_base) << RD;
11485 return false;
11486 }
11487 }
11488
11489 return HandleClassZeroInitialization(Info, E, RD, This, Result);
11490}
11491
11492bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
11493 switch (E->getCastKind()) {
11494 default:
11495 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11496
11497 case CK_ConstructorConversion:
11498 return Visit(S: E->getSubExpr());
11499
11500 case CK_DerivedToBase:
11501 case CK_UncheckedDerivedToBase: {
11502 APValue DerivedObject;
11503 if (!Evaluate(Result&: DerivedObject, Info, E: E->getSubExpr()))
11504 return false;
11505 if (!DerivedObject.isStruct())
11506 return Error(E: E->getSubExpr());
11507
11508 // Derived-to-base rvalue conversion: just slice off the derived part.
11509 APValue *Value = &DerivedObject;
11510 const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
11511 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11512 PathE = E->path_end(); PathI != PathE; ++PathI) {
11513 assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
11514 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11515 Value = &Value->getStructBase(i: getBaseIndex(Derived: RD, Base));
11516 RD = Base;
11517 }
11518 Result = *Value;
11519 return true;
11520 }
11521 case CK_HLSLAggregateSplatCast: {
11522 APValue Val;
11523 QualType ValTy;
11524
11525 if (!hlslAggSplatHelper(Info, E: E->getSubExpr(), SrcVal&: Val, SrcTy&: ValTy))
11526 return false;
11527
11528 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
11529 // splat our Val
11530 SmallVector<APValue> SplatEls(NEls, Val);
11531 SmallVector<QualType> SplatType(NEls, ValTy);
11532
11533 // cast the elements and construct our struct result
11534 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11535 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
11536 ElTypes&: SplatType))
11537 return false;
11538
11539 return true;
11540 }
11541 case CK_HLSLElementwiseCast: {
11542 SmallVector<APValue> SrcEls;
11543 SmallVector<QualType> SrcTypes;
11544
11545 if (!hlslElementwiseCastHelper(Info, E: E->getSubExpr(), DestTy: E->getType(), SrcVals&: SrcEls,
11546 SrcTypes))
11547 return false;
11548
11549 // cast the elements and construct our struct result
11550 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11551 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
11552 ElTypes&: SrcTypes))
11553 return false;
11554
11555 return true;
11556 }
11557 case CK_ToUnion: {
11558 const FieldDecl *Field = E->getTargetUnionField();
11559 LValue Subobject = This;
11560 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field))
11561 return false;
11562 Result = APValue(Field);
11563 if (!EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject,
11564 E: E->getSubExpr()))
11565 return false;
11566 if (Field->isBitField()) {
11567 if (!truncateBitfieldValue(Info, E: E->getSubExpr(), Value&: Result.getUnionValue(),
11568 FD: Field))
11569 return false;
11570 }
11571 return true;
11572 }
11573 }
11574}
11575
11576bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
11577 if (E->isTransparent())
11578 return Visit(S: E->getInit(Init: 0));
11579 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits());
11580}
11581
11582bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr(
11583 const Expr *ExprToVisit, ArrayRef<Expr *> Args) {
11584 const auto *RD = ExprToVisit->getType()->castAsRecordDecl();
11585 if (RD->isInvalidDecl()) return false;
11586 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11587 auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11588
11589 EvalInfo::EvaluatingConstructorRAII EvalObj(
11590 Info,
11591 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
11592 CXXRD && CXXRD->getNumBases());
11593
11594 if (RD->isUnion()) {
11595 const FieldDecl *Field;
11596 if (auto *ILE = dyn_cast<InitListExpr>(Val: ExprToVisit)) {
11597 Field = ILE->getInitializedFieldInUnion();
11598 } else if (auto *PLIE = dyn_cast<CXXParenListInitExpr>(Val: ExprToVisit)) {
11599 Field = PLIE->getInitializedFieldInUnion();
11600 } else {
11601 llvm_unreachable(
11602 "Expression is neither an init list nor a C++ paren list");
11603 }
11604
11605 Result = APValue(Field);
11606 if (!Field)
11607 return true;
11608
11609 // If the initializer list for a union does not contain any elements, the
11610 // first element of the union is value-initialized.
11611 // FIXME: The element should be initialized from an initializer list.
11612 // Is this difference ever observable for initializer lists which
11613 // we don't build?
11614 ImplicitValueInitExpr VIE(Field->getType());
11615 const Expr *InitExpr = Args.empty() ? &VIE : Args[0];
11616
11617 LValue Subobject = This;
11618 if (!HandleLValueMember(Info, E: InitExpr, LVal&: Subobject, FD: Field, RL: &Layout))
11619 return false;
11620
11621 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11622 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11623 isa<CXXDefaultInitExpr>(Val: InitExpr));
11624
11625 if (EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: InitExpr)) {
11626 if (Field->isBitField())
11627 return truncateBitfieldValue(Info, E: InitExpr, Value&: Result.getUnionValue(),
11628 FD: Field);
11629 return true;
11630 }
11631
11632 return false;
11633 }
11634
11635 if (!Result.hasValue())
11636 Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0,
11637 RD->getNumFields());
11638 unsigned ElementNo = 0;
11639 bool Success = true;
11640
11641 // Initialize base classes.
11642 if (CXXRD && CXXRD->getNumBases()) {
11643 for (const auto &Base : CXXRD->bases()) {
11644 assert(ElementNo < Args.size() && "missing init for base class");
11645 const Expr *Init = Args[ElementNo];
11646
11647 LValue Subobject = This;
11648 if (!HandleLValueBase(Info, E: Init, Obj&: Subobject, DerivedDecl: CXXRD, Base: &Base))
11649 return false;
11650
11651 APValue &FieldVal = Result.getStructBase(i: ElementNo);
11652 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init)) {
11653 if (!Info.noteFailure())
11654 return false;
11655 Success = false;
11656 }
11657 ++ElementNo;
11658 }
11659
11660 EvalObj.finishedConstructingBases();
11661 }
11662
11663 // Initialize members.
11664 for (const auto *Field : RD->fields()) {
11665 // Anonymous bit-fields are not considered members of the class for
11666 // purposes of aggregate initialization.
11667 if (Field->isUnnamedBitField())
11668 continue;
11669
11670 LValue Subobject = This;
11671
11672 bool HaveInit = ElementNo < Args.size();
11673
11674 // FIXME: Diagnostics here should point to the end of the initializer
11675 // list, not the start.
11676 if (!HandleLValueMember(Info, E: HaveInit ? Args[ElementNo] : ExprToVisit,
11677 LVal&: Subobject, FD: Field, RL: &Layout))
11678 return false;
11679
11680 // Perform an implicit value-initialization for members beyond the end of
11681 // the initializer list.
11682 ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
11683 const Expr *Init = HaveInit ? Args[ElementNo++] : &VIE;
11684
11685 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
11686 // aren't supposed to be modified.
11687 if (isa<NoInitExpr>(Val: Init))
11688 continue;
11689
11690 if (Field->getType()->isIncompleteArrayType()) {
11691 if (auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType())) {
11692 if (!CAT->isZeroSize()) {
11693 // Bail out for now. This might sort of "work", but the rest of the
11694 // code isn't really prepared to handle it.
11695 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_unsupported_flexible_array);
11696 return false;
11697 }
11698 }
11699 }
11700
11701 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11702 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11703 isa<CXXDefaultInitExpr>(Val: Init));
11704
11705 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
11706 if (Field->getType()->isReferenceType()) {
11707 LValue Result;
11708 if (!EvaluateInitForDeclOfReferenceType(Info, D: Field, Init, Result,
11709 Val&: FieldVal)) {
11710 if (!Info.noteFailure())
11711 return false;
11712 Success = false;
11713 }
11714 } else if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init) ||
11715 (Field->isBitField() &&
11716 !truncateBitfieldValue(Info, E: Init, Value&: FieldVal, FD: Field))) {
11717 if (!Info.noteFailure())
11718 return false;
11719 Success = false;
11720 }
11721 }
11722
11723 EvalObj.finishedConstructingFields();
11724
11725 return Success;
11726}
11727
11728bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
11729 QualType T) {
11730 // Note that E's type is not necessarily the type of our class here; we might
11731 // be initializing an array element instead.
11732 const CXXConstructorDecl *FD = E->getConstructor();
11733 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false;
11734
11735 bool ZeroInit = E->requiresZeroInitialization();
11736 if (CheckTrivialDefaultConstructor(Info, Loc: E->getExprLoc(), CD: FD, IsValueInitialization: ZeroInit)) {
11737 if (ZeroInit)
11738 return ZeroInitialization(E, T);
11739
11740 return handleDefaultInitValue(T, Result);
11741 }
11742
11743 const FunctionDecl *Definition = nullptr;
11744 auto Body = FD->getBody(Definition);
11745
11746 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11747 return false;
11748
11749 // Avoid materializing a temporary for an elidable copy/move constructor.
11750 if (E->isElidable() && !ZeroInit) {
11751 // FIXME: This only handles the simplest case, where the source object
11752 // is passed directly as the first argument to the constructor.
11753 // This should also handle stepping though implicit casts and
11754 // and conversion sequences which involve two steps, with a
11755 // conversion operator followed by a converting constructor.
11756 const Expr *SrcObj = E->getArg(Arg: 0);
11757 assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent()));
11758 assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType()));
11759 if (const MaterializeTemporaryExpr *ME =
11760 dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj))
11761 return Visit(S: ME->getSubExpr());
11762 }
11763
11764 if (ZeroInit && !ZeroInitialization(E, T))
11765 return false;
11766
11767 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
11768 return HandleConstructorCall(E, This, Args,
11769 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
11770 Result);
11771}
11772
11773bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr(
11774 const CXXInheritedCtorInitExpr *E) {
11775 if (!Info.CurrentCall) {
11776 assert(Info.checkingPotentialConstantExpression());
11777 return false;
11778 }
11779
11780 const CXXConstructorDecl *FD = E->getConstructor();
11781 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl())
11782 return false;
11783
11784 const FunctionDecl *Definition = nullptr;
11785 auto Body = FD->getBody(Definition);
11786
11787 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11788 return false;
11789
11790 return HandleConstructorCall(E, This, Call: Info.CurrentCall->Arguments,
11791 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
11792 Result);
11793}
11794
11795bool RecordExprEvaluator::VisitCXXStdInitializerListExpr(
11796 const CXXStdInitializerListExpr *E) {
11797 const ConstantArrayType *ArrayType =
11798 Info.Ctx.getAsConstantArrayType(T: E->getSubExpr()->getType());
11799
11800 LValue Array;
11801 if (!EvaluateLValue(E: E->getSubExpr(), Result&: Array, Info))
11802 return false;
11803
11804 assert(ArrayType && "unexpected type for array initializer");
11805
11806 // Get a pointer to the first element of the array.
11807 Array.addArray(Info, E, CAT: ArrayType);
11808
11809 // FIXME: What if the initializer_list type has base classes, etc?
11810 Result = APValue(APValue::UninitStruct(), 0, 2);
11811 Array.moveInto(V&: Result.getStructField(i: 0));
11812
11813 auto *Record = E->getType()->castAsRecordDecl();
11814 RecordDecl::field_iterator Field = Record->field_begin();
11815 assert(Field != Record->field_end() &&
11816 Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
11817 ArrayType->getElementType()) &&
11818 "Expected std::initializer_list first field to be const E *");
11819 ++Field;
11820 assert(Field != Record->field_end() &&
11821 "Expected std::initializer_list to have two fields");
11822
11823 if (Info.Ctx.hasSameType(T1: Field->getType(), T2: Info.Ctx.getSizeType())) {
11824 // Length.
11825 Result.getStructField(i: 1) = APValue(APSInt(ArrayType->getSize()));
11826 } else {
11827 // End pointer.
11828 assert(Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
11829 ArrayType->getElementType()) &&
11830 "Expected std::initializer_list second field to be const E *");
11831 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Array,
11832 EltTy: ArrayType->getElementType(),
11833 Adjustment: ArrayType->getZExtSize()))
11834 return false;
11835 Array.moveInto(V&: Result.getStructField(i: 1));
11836 }
11837
11838 assert(++Field == Record->field_end() &&
11839 "Expected std::initializer_list to only have two fields");
11840
11841 return true;
11842}
11843
11844bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {
11845 const CXXRecordDecl *ClosureClass = E->getLambdaClass();
11846 if (ClosureClass->isInvalidDecl())
11847 return false;
11848
11849 const size_t NumFields = ClosureClass->getNumFields();
11850
11851 assert(NumFields == (size_t)std::distance(E->capture_init_begin(),
11852 E->capture_init_end()) &&
11853 "The number of lambda capture initializers should equal the number of "
11854 "fields within the closure type");
11855
11856 Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields);
11857 // Iterate through all the lambda's closure object's fields and initialize
11858 // them.
11859 auto *CaptureInitIt = E->capture_init_begin();
11860 bool Success = true;
11861 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: ClosureClass);
11862 for (const auto *Field : ClosureClass->fields()) {
11863 assert(CaptureInitIt != E->capture_init_end());
11864 // Get the initializer for this field
11865 Expr *const CurFieldInit = *CaptureInitIt++;
11866
11867 // If there is no initializer, either this is a VLA or an error has
11868 // occurred.
11869 if (!CurFieldInit || CurFieldInit->containsErrors())
11870 return Error(E);
11871
11872 LValue Subobject = This;
11873
11874 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field, RL: &Layout))
11875 return false;
11876
11877 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
11878 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: CurFieldInit)) {
11879 if (!Info.keepEvaluatingAfterFailure())
11880 return false;
11881 Success = false;
11882 }
11883 }
11884 return Success;
11885}
11886
11887bool RecordExprEvaluator::VisitDesignatedInitUpdateExpr(
11888 const DesignatedInitUpdateExpr *E) {
11889 if (!Visit(S: E->getBase()))
11890 return false;
11891 return Visit(S: E->getUpdater());
11892}
11893
11894static bool EvaluateRecord(const Expr *E, const LValue &This,
11895 APValue &Result, EvalInfo &Info) {
11896 assert(!E->isValueDependent());
11897 assert(E->isPRValue() && E->getType()->isRecordType() &&
11898 "can't evaluate expression as a record rvalue");
11899 return RecordExprEvaluator(Info, This, Result).Visit(S: E);
11900}
11901
11902//===----------------------------------------------------------------------===//
11903// Temporary Evaluation
11904//
11905// Temporaries are represented in the AST as rvalues, but generally behave like
11906// lvalues. The full-object of which the temporary is a subobject is implicitly
11907// materialized so that a reference can bind to it.
11908//===----------------------------------------------------------------------===//
11909namespace {
11910class TemporaryExprEvaluator
11911 : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
11912public:
11913 TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
11914 LValueExprEvaluatorBaseTy(Info, Result, false) {}
11915
11916 /// Visit an expression which constructs the value of this temporary.
11917 bool VisitConstructExpr(const Expr *E) {
11918 APValue &Value = Info.CurrentCall->createTemporary(
11919 Key: E, T: E->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
11920 return EvaluateInPlace(Result&: Value, Info, This: Result, E);
11921 }
11922
11923 bool VisitCastExpr(const CastExpr *E) {
11924 switch (E->getCastKind()) {
11925 default:
11926 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
11927
11928 case CK_ConstructorConversion:
11929 return VisitConstructExpr(E: E->getSubExpr());
11930 }
11931 }
11932 bool VisitInitListExpr(const InitListExpr *E) {
11933 return VisitConstructExpr(E);
11934 }
11935 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
11936 return VisitConstructExpr(E);
11937 }
11938 bool VisitCallExpr(const CallExpr *E) {
11939 return VisitConstructExpr(E);
11940 }
11941 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) {
11942 return VisitConstructExpr(E);
11943 }
11944 bool VisitLambdaExpr(const LambdaExpr *E) {
11945 return VisitConstructExpr(E);
11946 }
11947};
11948} // end anonymous namespace
11949
11950/// Evaluate an expression of record type as a temporary.
11951static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
11952 assert(!E->isValueDependent());
11953 assert(E->isPRValue() && E->getType()->isRecordType());
11954 return TemporaryExprEvaluator(Info, Result).Visit(S: E);
11955}
11956
11957//===----------------------------------------------------------------------===//
11958// Vector Evaluation
11959//===----------------------------------------------------------------------===//
11960
11961namespace {
11962 class VectorExprEvaluator
11963 : public ExprEvaluatorBase<VectorExprEvaluator> {
11964 APValue &Result;
11965 public:
11966
11967 VectorExprEvaluator(EvalInfo &info, APValue &Result)
11968 : ExprEvaluatorBaseTy(info), Result(Result) {}
11969
11970 bool Success(ArrayRef<APValue> V, const Expr *E) {
11971 assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
11972 // FIXME: remove this APValue copy.
11973 Result = APValue(V.data(), V.size());
11974 return true;
11975 }
11976 bool Success(const APValue &V, const Expr *E) {
11977 assert(V.isVector());
11978 Result = V;
11979 return true;
11980 }
11981 bool ZeroInitialization(const Expr *E);
11982
11983 bool VisitUnaryReal(const UnaryOperator *E)
11984 { return Visit(S: E->getSubExpr()); }
11985 bool VisitCastExpr(const CastExpr* E);
11986 bool VisitInitListExpr(const InitListExpr *E);
11987 bool VisitUnaryImag(const UnaryOperator *E);
11988 bool VisitBinaryOperator(const BinaryOperator *E);
11989 bool VisitUnaryOperator(const UnaryOperator *E);
11990 bool VisitCallExpr(const CallExpr *E);
11991 bool VisitConvertVectorExpr(const ConvertVectorExpr *E);
11992 bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E);
11993
11994 // FIXME: Missing: conditional operator (for GNU
11995 // conditional select), ExtVectorElementExpr
11996 };
11997} // end anonymous namespace
11998
11999static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
12000 assert(E->isPRValue() && E->getType()->isVectorType() &&
12001 "not a vector prvalue");
12002 return VectorExprEvaluator(Info, Result).Visit(S: E);
12003}
12004
12005static llvm::APInt ConvertBoolVectorToInt(const APValue &Val) {
12006 assert(Val.isVector() && "expected vector APValue");
12007 unsigned NumElts = Val.getVectorLength();
12008
12009 // Each element is one bit, so create an integer with NumElts bits.
12010 llvm::APInt Result(NumElts, 0);
12011
12012 for (unsigned I = 0; I < NumElts; ++I) {
12013 const APValue &Elt = Val.getVectorElt(I);
12014 assert(Elt.isInt() && "expected integer element in bool vector");
12015
12016 if (Elt.getInt().getBoolValue())
12017 Result.setBit(I);
12018 }
12019
12020 return Result;
12021}
12022
12023bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) {
12024 const VectorType *VTy = E->getType()->castAs<VectorType>();
12025 unsigned NElts = VTy->getNumElements();
12026
12027 const Expr *SE = E->getSubExpr();
12028 QualType SETy = SE->getType();
12029
12030 switch (E->getCastKind()) {
12031 case CK_VectorSplat: {
12032 APValue Val = APValue();
12033 if (SETy->isIntegerType()) {
12034 APSInt IntResult;
12035 if (!EvaluateInteger(E: SE, Result&: IntResult, Info))
12036 return false;
12037 Val = APValue(std::move(IntResult));
12038 } else if (SETy->isRealFloatingType()) {
12039 APFloat FloatResult(0.0);
12040 if (!EvaluateFloat(E: SE, Result&: FloatResult, Info))
12041 return false;
12042 Val = APValue(std::move(FloatResult));
12043 } else {
12044 return Error(E);
12045 }
12046
12047 // Splat and create vector APValue.
12048 SmallVector<APValue, 4> Elts(NElts, Val);
12049 return Success(V: Elts, E);
12050 }
12051 case CK_BitCast: {
12052 APValue SVal;
12053 if (!Evaluate(Result&: SVal, Info, E: SE))
12054 return false;
12055
12056 if (!SVal.isInt() && !SVal.isFloat() && !SVal.isVector()) {
12057 // Give up if the input isn't an int, float, or vector. For example, we
12058 // reject "(v4i16)(intptr_t)&a".
12059 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_cast)
12060 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
12061 << Info.Ctx.getLangOpts().CPlusPlus;
12062 return false;
12063 }
12064
12065 if (!handleRValueToRValueBitCast(Info, DestValue&: Result, SourceRValue: SVal, BCE: E))
12066 return false;
12067
12068 return true;
12069 }
12070 case CK_HLSLVectorTruncation: {
12071 APValue Val;
12072 SmallVector<APValue, 4> Elements;
12073 if (!EvaluateVector(E: SE, Result&: Val, Info))
12074 return Error(E);
12075 for (unsigned I = 0; I < NElts; I++)
12076 Elements.push_back(Elt: Val.getVectorElt(I));
12077 return Success(V: Elements, E);
12078 }
12079 case CK_HLSLMatrixTruncation: {
12080 // Matrix truncation occurs in row-major order.
12081 APValue Val;
12082 if (!EvaluateMatrix(E: SE, Result&: Val, Info))
12083 return Error(E);
12084 SmallVector<APValue, 16> Elements;
12085 for (unsigned Row = 0;
12086 Row < Val.getMatrixNumRows() && Elements.size() < NElts; Row++)
12087 for (unsigned Col = 0;
12088 Col < Val.getMatrixNumColumns() && Elements.size() < NElts; Col++)
12089 Elements.push_back(Elt: Val.getMatrixElt(Row, Col));
12090 return Success(V: Elements, E);
12091 }
12092 case CK_HLSLAggregateSplatCast: {
12093 APValue Val;
12094 QualType ValTy;
12095
12096 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
12097 return false;
12098
12099 // cast our Val once.
12100 APValue Result;
12101 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12102 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: VTy->getElementType(), Original: Val,
12103 Result))
12104 return false;
12105
12106 SmallVector<APValue, 4> SplatEls(NElts, Result);
12107 return Success(V: SplatEls, E);
12108 }
12109 case CK_HLSLElementwiseCast: {
12110 SmallVector<APValue> SrcVals;
12111 SmallVector<QualType> SrcTypes;
12112
12113 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
12114 return false;
12115
12116 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12117 SmallVector<QualType, 4> DestTypes(NElts, VTy->getElementType());
12118 SmallVector<APValue, 4> ResultEls(NElts);
12119 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
12120 Results&: ResultEls))
12121 return false;
12122 return Success(V: ResultEls, E);
12123 }
12124 case CK_IntegralToFloating:
12125 case CK_FloatingToIntegral:
12126 case CK_IntegralCast:
12127 case CK_FloatingCast:
12128 case CK_FloatingToBoolean:
12129 case CK_IntegralToBoolean: {
12130 // These casts apply element-wise when the source is a vector type.
12131 assert(SETy->isVectorType() && "expected vector source type");
12132 APValue SrcVal;
12133 if (!EvaluateVector(E: SE, Result&: SrcVal, Info))
12134 return Error(E);
12135
12136 assert(SrcVal.getVectorLength() == NElts);
12137 QualType SrcEltTy = SETy->castAs<VectorType>()->getElementType();
12138 QualType DstEltTy = VTy->getElementType();
12139 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12140
12141 SmallVector<APValue, 4> ResultEls(NElts);
12142 for (unsigned I = 0; I < NElts; ++I) {
12143 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcEltTy, DestTy: DstEltTy,
12144 Original: SrcVal.getVectorElt(I), Result&: ResultEls[I]))
12145 return Error(E);
12146 }
12147 return Success(V: ResultEls, E);
12148 }
12149 default:
12150 return ExprEvaluatorBaseTy::VisitCastExpr(E);
12151 }
12152}
12153
12154bool
12155VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
12156 const VectorType *VT = E->getType()->castAs<VectorType>();
12157 unsigned NumInits = E->getNumInits();
12158 unsigned NumElements = VT->getNumElements();
12159
12160 QualType EltTy = VT->getElementType();
12161 SmallVector<APValue, 4> Elements;
12162
12163 // MFloat8 type doesn't have constants and thus constant folding
12164 // is impossible.
12165 if (EltTy->isMFloat8Type())
12166 return false;
12167
12168 // The number of initializers can be less than the number of
12169 // vector elements. For OpenCL, this can be due to nested vector
12170 // initialization. For GCC compatibility, missing trailing elements
12171 // should be initialized with zeroes.
12172 unsigned CountInits = 0, CountElts = 0;
12173 while (CountElts < NumElements) {
12174 // Handle nested vector initialization.
12175 if (CountInits < NumInits
12176 && E->getInit(Init: CountInits)->getType()->isVectorType()) {
12177 APValue v;
12178 if (!EvaluateVector(E: E->getInit(Init: CountInits), Result&: v, Info))
12179 return Error(E);
12180 unsigned vlen = v.getVectorLength();
12181 for (unsigned j = 0; j < vlen; j++)
12182 Elements.push_back(Elt: v.getVectorElt(I: j));
12183 CountElts += vlen;
12184 } else if (EltTy->isIntegerType()) {
12185 llvm::APSInt sInt(32);
12186 if (CountInits < NumInits) {
12187 if (!EvaluateInteger(E: E->getInit(Init: CountInits), Result&: sInt, Info))
12188 return false;
12189 } else // trailing integer zero.
12190 sInt = Info.Ctx.MakeIntValue(Value: 0, Type: EltTy);
12191 Elements.push_back(Elt: APValue(sInt));
12192 CountElts++;
12193 } else {
12194 llvm::APFloat f(0.0);
12195 if (CountInits < NumInits) {
12196 if (!EvaluateFloat(E: E->getInit(Init: CountInits), Result&: f, Info))
12197 return false;
12198 } else // trailing float zero.
12199 f = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy));
12200 Elements.push_back(Elt: APValue(f));
12201 CountElts++;
12202 }
12203 CountInits++;
12204 }
12205 return Success(V: Elements, E);
12206}
12207
12208bool
12209VectorExprEvaluator::ZeroInitialization(const Expr *E) {
12210 const auto *VT = E->getType()->castAs<VectorType>();
12211 QualType EltTy = VT->getElementType();
12212 APValue ZeroElement;
12213 if (EltTy->isIntegerType())
12214 ZeroElement = APValue(Info.Ctx.MakeIntValue(Value: 0, Type: EltTy));
12215 else
12216 ZeroElement =
12217 APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy)));
12218
12219 SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
12220 return Success(V: Elements, E);
12221}
12222
12223bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
12224 VisitIgnoredValue(E: E->getSubExpr());
12225 return ZeroInitialization(E);
12226}
12227
12228bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
12229 BinaryOperatorKind Op = E->getOpcode();
12230 assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp &&
12231 "Operation not supported on vector types");
12232
12233 if (Op == BO_Comma)
12234 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
12235
12236 Expr *LHS = E->getLHS();
12237 Expr *RHS = E->getRHS();
12238
12239 assert(LHS->getType()->isVectorType() && RHS->getType()->isVectorType() &&
12240 "Must both be vector types");
12241 // Checking JUST the types are the same would be fine, except shifts don't
12242 // need to have their types be the same (since you always shift by an int).
12243 assert(LHS->getType()->castAs<VectorType>()->getNumElements() ==
12244 E->getType()->castAs<VectorType>()->getNumElements() &&
12245 RHS->getType()->castAs<VectorType>()->getNumElements() ==
12246 E->getType()->castAs<VectorType>()->getNumElements() &&
12247 "All operands must be the same size.");
12248
12249 APValue LHSValue;
12250 APValue RHSValue;
12251 bool LHSOK = Evaluate(Result&: LHSValue, Info, E: LHS);
12252 if (!LHSOK && !Info.noteFailure())
12253 return false;
12254 if (!Evaluate(Result&: RHSValue, Info, E: RHS) || !LHSOK)
12255 return false;
12256
12257 if (!handleVectorVectorBinOp(Info, E, Opcode: Op, LHSValue, RHSValue))
12258 return false;
12259
12260 return Success(V: LHSValue, E);
12261}
12262
12263static std::optional<APValue> handleVectorUnaryOperator(ASTContext &Ctx,
12264 QualType ResultTy,
12265 UnaryOperatorKind Op,
12266 APValue Elt) {
12267 switch (Op) {
12268 case UO_Plus:
12269 // Nothing to do here.
12270 return Elt;
12271 case UO_Minus:
12272 if (Elt.getKind() == APValue::Int) {
12273 Elt.getInt().negate();
12274 } else {
12275 assert(Elt.getKind() == APValue::Float &&
12276 "Vector can only be int or float type");
12277 Elt.getFloat().changeSign();
12278 }
12279 return Elt;
12280 case UO_Not:
12281 // This is only valid for integral types anyway, so we don't have to handle
12282 // float here.
12283 assert(Elt.getKind() == APValue::Int &&
12284 "Vector operator ~ can only be int");
12285 Elt.getInt().flipAllBits();
12286 return Elt;
12287 case UO_LNot: {
12288 if (Elt.getKind() == APValue::Int) {
12289 Elt.getInt() = !Elt.getInt();
12290 // operator ! on vectors returns -1 for 'truth', so negate it.
12291 Elt.getInt().negate();
12292 return Elt;
12293 }
12294 assert(Elt.getKind() == APValue::Float &&
12295 "Vector can only be int or float type");
12296 // Float types result in an int of the same size, but -1 for true, or 0 for
12297 // false.
12298 APSInt EltResult{Ctx.getIntWidth(T: ResultTy),
12299 ResultTy->isUnsignedIntegerType()};
12300 if (Elt.getFloat().isZero())
12301 EltResult.setAllBits();
12302 else
12303 EltResult.clearAllBits();
12304
12305 return APValue{EltResult};
12306 }
12307 default:
12308 // FIXME: Implement the rest of the unary operators.
12309 return std::nullopt;
12310 }
12311}
12312
12313bool VectorExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
12314 Expr *SubExpr = E->getSubExpr();
12315 const auto *VD = SubExpr->getType()->castAs<VectorType>();
12316 // This result element type differs in the case of negating a floating point
12317 // vector, since the result type is the a vector of the equivilant sized
12318 // integer.
12319 const QualType ResultEltTy = VD->getElementType();
12320 UnaryOperatorKind Op = E->getOpcode();
12321
12322 APValue SubExprValue;
12323 if (!Evaluate(Result&: SubExprValue, Info, E: SubExpr))
12324 return false;
12325
12326 // FIXME: This vector evaluator someday needs to be changed to be LValue
12327 // aware/keep LValue information around, rather than dealing with just vector
12328 // types directly. Until then, we cannot handle cases where the operand to
12329 // these unary operators is an LValue. The only case I've been able to see
12330 // cause this is operator++ assigning to a member expression (only valid in
12331 // altivec compilations) in C mode, so this shouldn't limit us too much.
12332 if (SubExprValue.isLValue())
12333 return false;
12334
12335 assert(SubExprValue.getVectorLength() == VD->getNumElements() &&
12336 "Vector length doesn't match type?");
12337
12338 SmallVector<APValue, 4> ResultElements;
12339 for (unsigned EltNum = 0; EltNum < VD->getNumElements(); ++EltNum) {
12340 std::optional<APValue> Elt = handleVectorUnaryOperator(
12341 Ctx&: Info.Ctx, ResultTy: ResultEltTy, Op, Elt: SubExprValue.getVectorElt(I: EltNum));
12342 if (!Elt)
12343 return false;
12344 ResultElements.push_back(Elt: *Elt);
12345 }
12346 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12347}
12348
12349static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO,
12350 const Expr *E, QualType SourceTy,
12351 QualType DestTy, APValue const &Original,
12352 APValue &Result) {
12353 if (SourceTy->isIntegerType()) {
12354 if (DestTy->isRealFloatingType()) {
12355 Result = APValue(APFloat(0.0));
12356 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
12357 DestType: DestTy, Result&: Result.getFloat());
12358 }
12359 if (DestTy->isIntegerType()) {
12360 Result = APValue(
12361 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
12362 return true;
12363 }
12364 } else if (SourceTy->isRealFloatingType()) {
12365 if (DestTy->isRealFloatingType()) {
12366 Result = Original;
12367 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
12368 Result&: Result.getFloat());
12369 }
12370 if (DestTy->isIntegerType()) {
12371 Result = APValue(APSInt());
12372 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
12373 DestType: DestTy, Result&: Result.getInt());
12374 }
12375 }
12376
12377 Info.FFDiag(E, DiagId: diag::err_convertvector_constexpr_unsupported_vector_cast)
12378 << SourceTy << DestTy;
12379 return false;
12380}
12381
12382static bool evalPackBuiltin(const CallExpr *E, EvalInfo &Info, APValue &Result,
12383 llvm::function_ref<APInt(const APSInt &)> PackFn) {
12384 APValue LHS, RHS;
12385 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: LHS) ||
12386 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: RHS))
12387 return false;
12388
12389 unsigned LHSVecLen = LHS.getVectorLength();
12390 unsigned RHSVecLen = RHS.getVectorLength();
12391
12392 assert(LHSVecLen != 0 && LHSVecLen == RHSVecLen &&
12393 "pack builtin LHSVecLen must equal to RHSVecLen");
12394
12395 const VectorType *VT0 = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
12396 const unsigned SrcBits = Info.Ctx.getIntWidth(T: VT0->getElementType());
12397
12398 const VectorType *DstVT = E->getType()->castAs<VectorType>();
12399 QualType DstElemTy = DstVT->getElementType();
12400 const bool DstIsUnsigned = DstElemTy->isUnsignedIntegerType();
12401
12402 const unsigned SrcPerLane = 128 / SrcBits;
12403 const unsigned Lanes = LHSVecLen * SrcBits / 128;
12404
12405 SmallVector<APValue, 64> Out;
12406 Out.reserve(N: LHSVecLen + RHSVecLen);
12407
12408 for (unsigned Lane = 0; Lane != Lanes; ++Lane) {
12409 unsigned base = Lane * SrcPerLane;
12410 for (unsigned I = 0; I != SrcPerLane; ++I)
12411 Out.emplace_back(Args: APValue(
12412 APSInt(PackFn(LHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12413 for (unsigned I = 0; I != SrcPerLane; ++I)
12414 Out.emplace_back(Args: APValue(
12415 APSInt(PackFn(RHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12416 }
12417
12418 Result = APValue(Out.data(), Out.size());
12419 return true;
12420}
12421
12422static bool evalShuffleGeneric(
12423 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12424 llvm::function_ref<std::pair<unsigned, int>(unsigned, unsigned)>
12425 GetSourceIndex) {
12426
12427 const auto *VT = Call->getType()->getAs<VectorType>();
12428 if (!VT)
12429 return false;
12430
12431 unsigned ShuffleMask = 0;
12432 APValue A, MaskVector, B;
12433 bool IsVectorMask = false;
12434 bool IsSingleOperand = (Call->getNumArgs() == 2);
12435
12436 if (IsSingleOperand) {
12437 QualType MaskType = Call->getArg(Arg: 1)->getType();
12438 if (MaskType->isVectorType()) {
12439 IsVectorMask = true;
12440 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12441 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector))
12442 return false;
12443 B = A;
12444 } else if (MaskType->isIntegerType()) {
12445 APSInt MaskImm;
12446 if (!EvaluateInteger(E: Call->getArg(Arg: 1), Result&: MaskImm, Info))
12447 return false;
12448 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12449 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A))
12450 return false;
12451 B = A;
12452 } else {
12453 return false;
12454 }
12455 } else {
12456 QualType Arg2Type = Call->getArg(Arg: 2)->getType();
12457 if (Arg2Type->isVectorType()) {
12458 IsVectorMask = true;
12459 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12460 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector) ||
12461 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 2), Result&: B))
12462 return false;
12463 } else if (Arg2Type->isIntegerType()) {
12464 APSInt MaskImm;
12465 if (!EvaluateInteger(E: Call->getArg(Arg: 2), Result&: MaskImm, Info))
12466 return false;
12467 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12468 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12469 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: B))
12470 return false;
12471 } else {
12472 return false;
12473 }
12474 }
12475
12476 unsigned NumElts = VT->getNumElements();
12477 SmallVector<APValue, 64> ResultElements;
12478 ResultElements.reserve(N: NumElts);
12479
12480 for (unsigned DstIdx = 0; DstIdx != NumElts; ++DstIdx) {
12481 if (IsVectorMask) {
12482 ShuffleMask = static_cast<unsigned>(
12483 MaskVector.getVectorElt(I: DstIdx).getInt().getZExtValue());
12484 }
12485 auto [SrcVecIdx, SrcIdx] = GetSourceIndex(DstIdx, ShuffleMask);
12486
12487 if (SrcIdx < 0) {
12488 // Zero out this element
12489 QualType ElemTy = VT->getElementType();
12490 if (ElemTy->isRealFloatingType()) {
12491 ResultElements.push_back(
12492 Elt: APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy))));
12493 } else if (ElemTy->isIntegerType()) {
12494 APValue Zero(Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy));
12495 ResultElements.push_back(Elt: APValue(Zero));
12496 } else {
12497 // Other types of fallback logic
12498 ResultElements.push_back(Elt: APValue());
12499 }
12500 } else {
12501 const APValue &Src = (SrcVecIdx == 0) ? A : B;
12502 ResultElements.push_back(Elt: Src.getVectorElt(I: SrcIdx));
12503 }
12504 }
12505
12506 Out = APValue(ResultElements.data(), ResultElements.size());
12507 return true;
12508}
12509static bool ConvertDoubleToFloatStrict(EvalInfo &Info, const Expr *E,
12510 APFloat OrigVal, APValue &Result) {
12511
12512 if (OrigVal.isInfinity()) {
12513 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 0;
12514 return false;
12515 }
12516 if (OrigVal.isNaN()) {
12517 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 1;
12518 return false;
12519 }
12520
12521 APFloat Val = OrigVal;
12522 bool LosesInfo = false;
12523 APFloat::opStatus Status = Val.convert(
12524 ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven, losesInfo: &LosesInfo);
12525
12526 if (LosesInfo || Val.isDenormal()) {
12527 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
12528 return false;
12529 }
12530
12531 if (Status != APFloat::opOK) {
12532 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
12533 return false;
12534 }
12535
12536 Result = APValue(Val);
12537 return true;
12538}
12539static bool evalShiftWithCount(
12540 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12541 llvm::function_ref<APInt(const APInt &, uint64_t)> ShiftOp,
12542 llvm::function_ref<APInt(const APInt &, unsigned)> OverflowOp) {
12543
12544 APValue Source, Count;
12545 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: Source) ||
12546 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: Count))
12547 return false;
12548
12549 assert(Call->getNumArgs() == 2);
12550
12551 QualType SourceTy = Call->getArg(Arg: 0)->getType();
12552 assert(SourceTy->isVectorType() &&
12553 Call->getArg(1)->getType()->isVectorType());
12554
12555 QualType DestEltTy = SourceTy->castAs<VectorType>()->getElementType();
12556 unsigned DestEltWidth = Source.getVectorElt(I: 0).getInt().getBitWidth();
12557 unsigned DestLen = Source.getVectorLength();
12558 bool IsDestUnsigned = DestEltTy->isUnsignedIntegerType();
12559 unsigned CountEltWidth = Count.getVectorElt(I: 0).getInt().getBitWidth();
12560 unsigned NumBitsInQWord = 64;
12561 unsigned NumCountElts = NumBitsInQWord / CountEltWidth;
12562 SmallVector<APValue, 64> Result;
12563 Result.reserve(N: DestLen);
12564
12565 uint64_t CountLQWord = 0;
12566 for (unsigned EltIdx = 0; EltIdx != NumCountElts; ++EltIdx) {
12567 uint64_t Elt = Count.getVectorElt(I: EltIdx).getInt().getZExtValue();
12568 CountLQWord |= (Elt << (EltIdx * CountEltWidth));
12569 }
12570
12571 for (unsigned EltIdx = 0; EltIdx != DestLen; ++EltIdx) {
12572 APInt Elt = Source.getVectorElt(I: EltIdx).getInt();
12573 if (CountLQWord < DestEltWidth) {
12574 Result.push_back(
12575 Elt: APValue(APSInt(ShiftOp(Elt, CountLQWord), IsDestUnsigned)));
12576 } else {
12577 Result.push_back(
12578 Elt: APValue(APSInt(OverflowOp(Elt, DestEltWidth), IsDestUnsigned)));
12579 }
12580 }
12581 Out = APValue(Result.data(), Result.size());
12582 return true;
12583}
12584
12585std::optional<APFloat> EvalScalarMinMaxFp(const APFloat &A, const APFloat &B,
12586 std::optional<APSInt> RoundingMode,
12587 bool IsMin) {
12588 APSInt DefaultMode(APInt(32, 4), /*isUnsigned=*/true);
12589 if (RoundingMode.value_or(u&: DefaultMode) != 4)
12590 return std::nullopt;
12591 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
12592 B.isInfinity() || B.isDenormal())
12593 return std::nullopt;
12594 if (A.isZero() && B.isZero())
12595 return B;
12596 return IsMin ? llvm::minimum(A, B) : llvm::maximum(A, B);
12597}
12598
12599bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
12600 if (!IsConstantEvaluatedBuiltinCall(E))
12601 return ExprEvaluatorBaseTy::VisitCallExpr(E);
12602
12603 unsigned BuiltinOp = ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E);
12604
12605 auto EvaluateBinOpExpr =
12606 [&](llvm::function_ref<APInt(const APSInt &, const APSInt &)> Fn) {
12607 APValue SourceLHS, SourceRHS;
12608 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12609 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12610 return false;
12611
12612 auto *DestTy = E->getType()->castAs<VectorType>();
12613 QualType DestEltTy = DestTy->getElementType();
12614 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12615 unsigned SourceLen = SourceLHS.getVectorLength();
12616 SmallVector<APValue, 4> ResultElements;
12617 ResultElements.reserve(N: SourceLen);
12618
12619 if (SourceRHS.isInt()) {
12620 const APSInt &RHS = SourceRHS.getInt();
12621 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12622 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12623 ResultElements.push_back(
12624 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12625 }
12626 } else {
12627 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12628 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12629 const APSInt &RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
12630 ResultElements.push_back(
12631 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12632 }
12633 }
12634 return Success(V: APValue(ResultElements.data(), SourceLen), E);
12635 };
12636
12637 auto EvaluateFpBinOpExpr =
12638 [&](llvm::function_ref<std::optional<APFloat>(
12639 const APFloat &, const APFloat &, std::optional<APSInt>)>
12640 Fn,
12641 bool IsScalar = false) {
12642 assert(E->getNumArgs() == 2 || E->getNumArgs() == 3);
12643 APValue A, B;
12644 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
12645 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
12646 return false;
12647
12648 assert(A.isVector() && B.isVector());
12649 assert(A.getVectorLength() == B.getVectorLength());
12650
12651 std::optional<APSInt> RoundingMode;
12652 if (E->getNumArgs() == 3) {
12653 APSInt Imm;
12654 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
12655 return false;
12656 RoundingMode = Imm;
12657 }
12658
12659 unsigned NumElems = A.getVectorLength();
12660 SmallVector<APValue, 4> ResultElements;
12661 ResultElements.reserve(N: NumElems);
12662
12663 for (unsigned EltNum = 0; EltNum < NumElems; ++EltNum) {
12664 if (IsScalar && EltNum > 0) {
12665 ResultElements.push_back(Elt: A.getVectorElt(I: EltNum));
12666 continue;
12667 }
12668 const APFloat &EltA = A.getVectorElt(I: EltNum).getFloat();
12669 const APFloat &EltB = B.getVectorElt(I: EltNum).getFloat();
12670 std::optional<APFloat> Result = Fn(EltA, EltB, RoundingMode);
12671 if (!Result)
12672 return false;
12673 ResultElements.push_back(Elt: APValue(*Result));
12674 }
12675 return Success(V: APValue(ResultElements.data(), NumElems), E);
12676 };
12677
12678 auto EvaluateScalarFpRoundMaskBinOp =
12679 [&](llvm::function_ref<std::optional<APFloat>(
12680 const APFloat &, const APFloat &, std::optional<APSInt>)>
12681 Fn) {
12682 assert(E->getNumArgs() == 5);
12683 APValue VecA, VecB, VecSrc;
12684 APSInt MaskVal, Rounding;
12685
12686 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
12687 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
12688 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
12689 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskVal, Info) ||
12690 !EvaluateInteger(E: E->getArg(Arg: 4), Result&: Rounding, Info))
12691 return false;
12692
12693 unsigned NumElems = VecA.getVectorLength();
12694 SmallVector<APValue, 8> ResultElements;
12695 ResultElements.reserve(N: NumElems);
12696
12697 if (MaskVal.getZExtValue() & 1) {
12698 const APFloat &EltA = VecA.getVectorElt(I: 0).getFloat();
12699 const APFloat &EltB = VecB.getVectorElt(I: 0).getFloat();
12700 std::optional<APFloat> Result = Fn(EltA, EltB, Rounding);
12701 if (!Result)
12702 return false;
12703 ResultElements.push_back(Elt: APValue(*Result));
12704 } else {
12705 ResultElements.push_back(Elt: VecSrc.getVectorElt(I: 0));
12706 }
12707
12708 for (unsigned I = 1; I < NumElems; ++I)
12709 ResultElements.push_back(Elt: VecA.getVectorElt(I));
12710
12711 return Success(V: APValue(ResultElements.data(), NumElems), E);
12712 };
12713
12714 auto EvalSelectScalar = [&](unsigned Len) -> bool {
12715 APSInt Mask;
12716 APValue AVal, WVal;
12717 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info) ||
12718 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: AVal) ||
12719 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: WVal))
12720 return false;
12721
12722 bool TakeA0 = (Mask.getZExtValue() & 1u) != 0;
12723 SmallVector<APValue, 4> Res;
12724 Res.reserve(N: Len);
12725 Res.push_back(Elt: TakeA0 ? AVal.getVectorElt(I: 0) : WVal.getVectorElt(I: 0));
12726 for (unsigned I = 1; I < Len; ++I)
12727 Res.push_back(Elt: WVal.getVectorElt(I));
12728 APValue V(Res.data(), Res.size());
12729 return Success(V, E);
12730 };
12731
12732 auto EvalVectorDotProduct = [&](bool IsSaturating) -> bool {
12733 APValue Source, OperandA, OperandB;
12734 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
12735 !EvaluateVector(E: E->getArg(Arg: 1), Result&: OperandA, Info) ||
12736 !EvaluateVector(E: E->getArg(Arg: 2), Result&: OperandB, Info)) {
12737 return false;
12738 }
12739
12740 unsigned NumSrcElems = Source.getVectorLength();
12741 unsigned NumOperandElems = OperandA.getVectorLength();
12742 unsigned ElemsPerLane = NumOperandElems / NumSrcElems;
12743
12744 assert(OperandA.getVectorLength() == OperandB.getVectorLength());
12745
12746 SmallVector<APValue, 16> Result;
12747 Result.reserve(N: NumSrcElems);
12748 for (unsigned I = 0; I != NumSrcElems; ++I) {
12749 APSInt DotProduct = Source.getVectorElt(I).getInt();
12750 DotProduct = DotProduct.extend(width: 64);
12751 for (unsigned J = 0; J != ElemsPerLane; ++J) {
12752 APSInt OpA = APSInt(
12753 OperandA.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
12754 false);
12755 APSInt OpB = APSInt(
12756 OperandB.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
12757 false);
12758 DotProduct += OpA * OpB;
12759 }
12760 if (IsSaturating) {
12761 DotProduct = APSInt(DotProduct.truncSSat(width: 32), false);
12762 } else {
12763 DotProduct = APSInt(DotProduct.trunc(width: 32), false);
12764 }
12765 Result.push_back(Elt: APValue(DotProduct));
12766 }
12767
12768 return Success(V: APValue(Result.data(), Result.size()), E);
12769 };
12770
12771 switch (BuiltinOp) {
12772 default:
12773 return false;
12774 case Builtin::BI__builtin_elementwise_popcount:
12775 case Builtin::BI__builtin_elementwise_bitreverse: {
12776 APValue Source;
12777 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
12778 return false;
12779
12780 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12781 unsigned SourceLen = Source.getVectorLength();
12782 SmallVector<APValue, 4> ResultElements;
12783 ResultElements.reserve(N: SourceLen);
12784
12785 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12786 APSInt Elt = Source.getVectorElt(I: EltNum).getInt();
12787 switch (BuiltinOp) {
12788 case Builtin::BI__builtin_elementwise_popcount:
12789 ResultElements.push_back(Elt: APValue(
12790 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), Elt.popcount()),
12791 DestEltTy->isUnsignedIntegerOrEnumerationType())));
12792 break;
12793 case Builtin::BI__builtin_elementwise_bitreverse:
12794 ResultElements.push_back(
12795 Elt: APValue(APSInt(Elt.reverseBits(),
12796 DestEltTy->isUnsignedIntegerOrEnumerationType())));
12797 break;
12798 }
12799 }
12800
12801 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12802 }
12803 case Builtin::BI__builtin_elementwise_abs: {
12804 APValue Source;
12805 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
12806 return false;
12807
12808 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12809 unsigned SourceLen = Source.getVectorLength();
12810 SmallVector<APValue, 4> ResultElements;
12811 ResultElements.reserve(N: SourceLen);
12812
12813 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12814 APValue CurrentEle = Source.getVectorElt(I: EltNum);
12815 APValue Val = DestEltTy->isFloatingType()
12816 ? APValue(llvm::abs(X: CurrentEle.getFloat()))
12817 : APValue(APSInt(
12818 CurrentEle.getInt().abs(),
12819 DestEltTy->isUnsignedIntegerOrEnumerationType()));
12820 ResultElements.push_back(Elt: Val);
12821 }
12822
12823 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12824 }
12825
12826 case Builtin::BI__builtin_elementwise_add_sat:
12827 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12828 return LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
12829 });
12830
12831 case Builtin::BI__builtin_elementwise_sub_sat:
12832 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12833 return LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
12834 });
12835
12836 case X86::BI__builtin_ia32_extract128i256:
12837 case X86::BI__builtin_ia32_vextractf128_pd256:
12838 case X86::BI__builtin_ia32_vextractf128_ps256:
12839 case X86::BI__builtin_ia32_vextractf128_si256: {
12840 APValue SourceVec, SourceImm;
12841 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
12842 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceImm))
12843 return false;
12844
12845 if (!SourceVec.isVector())
12846 return false;
12847
12848 const auto *RetVT = E->getType()->castAs<VectorType>();
12849 unsigned RetLen = RetVT->getNumElements();
12850 unsigned Idx = SourceImm.getInt().getZExtValue() & 1;
12851
12852 SmallVector<APValue, 32> ResultElements;
12853 ResultElements.reserve(N: RetLen);
12854
12855 for (unsigned I = 0; I < RetLen; I++)
12856 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Idx * RetLen + I));
12857
12858 return Success(V: APValue(ResultElements.data(), RetLen), E);
12859 }
12860
12861 case clang::X86::BI__builtin_ia32_cvtmask2b128:
12862 case clang::X86::BI__builtin_ia32_cvtmask2b256:
12863 case clang::X86::BI__builtin_ia32_cvtmask2b512:
12864 case clang::X86::BI__builtin_ia32_cvtmask2w128:
12865 case clang::X86::BI__builtin_ia32_cvtmask2w256:
12866 case clang::X86::BI__builtin_ia32_cvtmask2w512:
12867 case clang::X86::BI__builtin_ia32_cvtmask2d128:
12868 case clang::X86::BI__builtin_ia32_cvtmask2d256:
12869 case clang::X86::BI__builtin_ia32_cvtmask2d512:
12870 case clang::X86::BI__builtin_ia32_cvtmask2q128:
12871 case clang::X86::BI__builtin_ia32_cvtmask2q256:
12872 case clang::X86::BI__builtin_ia32_cvtmask2q512: {
12873 assert(E->getNumArgs() == 1);
12874 APSInt Mask;
12875 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info))
12876 return false;
12877
12878 QualType VecTy = E->getType();
12879 const VectorType *VT = VecTy->castAs<VectorType>();
12880 unsigned VectorLen = VT->getNumElements();
12881 QualType ElemTy = VT->getElementType();
12882 unsigned ElemWidth = Info.Ctx.getTypeSize(T: ElemTy);
12883
12884 SmallVector<APValue, 16> Elems;
12885 for (unsigned I = 0; I != VectorLen; ++I) {
12886 bool BitSet = Mask[I];
12887 APSInt ElemVal(ElemWidth, /*isUnsigned=*/false);
12888 if (BitSet) {
12889 ElemVal.setAllBits();
12890 }
12891 Elems.push_back(Elt: APValue(ElemVal));
12892 }
12893 return Success(V: APValue(Elems.data(), VectorLen), E);
12894 }
12895
12896 case X86::BI__builtin_ia32_extracti32x4_256_mask:
12897 case X86::BI__builtin_ia32_extractf32x4_256_mask:
12898 case X86::BI__builtin_ia32_extracti32x4_mask:
12899 case X86::BI__builtin_ia32_extractf32x4_mask:
12900 case X86::BI__builtin_ia32_extracti32x8_mask:
12901 case X86::BI__builtin_ia32_extractf32x8_mask:
12902 case X86::BI__builtin_ia32_extracti64x2_256_mask:
12903 case X86::BI__builtin_ia32_extractf64x2_256_mask:
12904 case X86::BI__builtin_ia32_extracti64x2_512_mask:
12905 case X86::BI__builtin_ia32_extractf64x2_512_mask:
12906 case X86::BI__builtin_ia32_extracti64x4_mask:
12907 case X86::BI__builtin_ia32_extractf64x4_mask: {
12908 APValue SourceVec, MergeVec;
12909 APSInt Imm, MaskImm;
12910
12911 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
12912 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info) ||
12913 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MergeVec) ||
12914 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskImm, Info))
12915 return false;
12916
12917 const auto *RetVT = E->getType()->castAs<VectorType>();
12918 unsigned RetLen = RetVT->getNumElements();
12919
12920 if (!SourceVec.isVector() || !MergeVec.isVector())
12921 return false;
12922 unsigned SrcLen = SourceVec.getVectorLength();
12923 unsigned Lanes = SrcLen / RetLen;
12924 unsigned Lane = static_cast<unsigned>(Imm.getZExtValue() % Lanes);
12925 unsigned Base = Lane * RetLen;
12926
12927 SmallVector<APValue, 32> ResultElements;
12928 ResultElements.reserve(N: RetLen);
12929 for (unsigned I = 0; I < RetLen; ++I) {
12930 if (MaskImm[I])
12931 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Base + I));
12932 else
12933 ResultElements.push_back(Elt: MergeVec.getVectorElt(I));
12934 }
12935 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12936 }
12937
12938 case clang::X86::BI__builtin_ia32_pavgb128:
12939 case clang::X86::BI__builtin_ia32_pavgw128:
12940 case clang::X86::BI__builtin_ia32_pavgb256:
12941 case clang::X86::BI__builtin_ia32_pavgw256:
12942 case clang::X86::BI__builtin_ia32_pavgb512:
12943 case clang::X86::BI__builtin_ia32_pavgw512:
12944 return EvaluateBinOpExpr(llvm::APIntOps::avgCeilU);
12945
12946 case clang::X86::BI__builtin_ia32_pmulhrsw128:
12947 case clang::X86::BI__builtin_ia32_pmulhrsw256:
12948 case clang::X86::BI__builtin_ia32_pmulhrsw512:
12949 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12950 return (llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS).ashr(ShiftAmt: 14) + 1)
12951 .extractBits(numBits: 16, bitPosition: 1);
12952 });
12953
12954 case clang::X86::BI__builtin_ia32_psadbw128:
12955 case clang::X86::BI__builtin_ia32_psadbw256:
12956 case clang::X86::BI__builtin_ia32_psadbw512: {
12957 APValue SourceLHS, SourceRHS;
12958 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12959 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12960 return false;
12961
12962 assert(SourceLHS.isVector() && SourceRHS.isVector());
12963 unsigned SourceLen = SourceLHS.getVectorLength();
12964 assert(SourceLen == SourceRHS.getVectorLength());
12965 assert((SourceLen % 8) == 0);
12966
12967 auto *DestTy = E->getType()->castAs<VectorType>();
12968 QualType DestEltTy = DestTy->getElementType();
12969 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12970 SmallVector<APValue, 8> ResultElements;
12971 ResultElements.reserve(N: SourceLen / 8);
12972
12973 for (unsigned Lane = 0; Lane != SourceLen; Lane += 8) {
12974 APInt Sum(64, 0);
12975 for (unsigned I = 0; I != 8; ++I) {
12976 APInt LHS = SourceLHS.getVectorElt(I: Lane + I).getInt().extOrTrunc(width: 8);
12977 APInt RHS = SourceRHS.getVectorElt(I: Lane + I).getInt().extOrTrunc(width: 8);
12978 Sum += llvm::APIntOps::abdu(A: LHS, B: RHS).zext(width: 64);
12979 }
12980 ResultElements.push_back(Elt: APValue(APSInt(Sum, DestUnsigned)));
12981 }
12982
12983 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12984 }
12985
12986 case clang::X86::BI__builtin_ia32_pmaddubsw128:
12987 case clang::X86::BI__builtin_ia32_pmaddubsw256:
12988 case clang::X86::BI__builtin_ia32_pmaddubsw512:
12989 case clang::X86::BI__builtin_ia32_pmaddwd128:
12990 case clang::X86::BI__builtin_ia32_pmaddwd256:
12991 case clang::X86::BI__builtin_ia32_pmaddwd512: {
12992 APValue SourceLHS, SourceRHS;
12993 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12994 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12995 return false;
12996
12997 auto *DestTy = E->getType()->castAs<VectorType>();
12998 QualType DestEltTy = DestTy->getElementType();
12999 unsigned SourceLen = SourceLHS.getVectorLength();
13000 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13001 SmallVector<APValue, 4> ResultElements;
13002 ResultElements.reserve(N: SourceLen / 2);
13003
13004 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
13005 const APSInt &LoLHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13006 const APSInt &HiLHS = SourceLHS.getVectorElt(I: EltNum + 1).getInt();
13007 const APSInt &LoRHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13008 const APSInt &HiRHS = SourceRHS.getVectorElt(I: EltNum + 1).getInt();
13009 unsigned BitWidth = 2 * LoLHS.getBitWidth();
13010
13011 switch (BuiltinOp) {
13012 case clang::X86::BI__builtin_ia32_pmaddubsw128:
13013 case clang::X86::BI__builtin_ia32_pmaddubsw256:
13014 case clang::X86::BI__builtin_ia32_pmaddubsw512:
13015 ResultElements.push_back(Elt: APValue(
13016 APSInt((LoLHS.zext(width: BitWidth) * LoRHS.sext(width: BitWidth))
13017 .sadd_sat(RHS: (HiLHS.zext(width: BitWidth) * HiRHS.sext(width: BitWidth))),
13018 DestUnsigned)));
13019 break;
13020 case clang::X86::BI__builtin_ia32_pmaddwd128:
13021 case clang::X86::BI__builtin_ia32_pmaddwd256:
13022 case clang::X86::BI__builtin_ia32_pmaddwd512:
13023 ResultElements.push_back(
13024 Elt: APValue(APSInt((LoLHS.sext(width: BitWidth) * LoRHS.sext(width: BitWidth)) +
13025 (HiLHS.sext(width: BitWidth) * HiRHS.sext(width: BitWidth)),
13026 DestUnsigned)));
13027 break;
13028 }
13029 }
13030
13031 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13032 }
13033
13034 case clang::X86::BI__builtin_ia32_bmacor16x16x16_v16hi:
13035 case clang::X86::BI__builtin_ia32_bmacor16x16x16_v32hi:
13036 case clang::X86::BI__builtin_ia32_bmacxor16x16x16_v16hi:
13037 case clang::X86::BI__builtin_ia32_bmacxor16x16x16_v32hi: {
13038 // Bit Matrix Multiply and Accumulate (AVX512BMM). Each 256-bit lane holds
13039 // a 16x16 bit matrix as 16 x i16 elements; element i is row i and bit j of
13040 // that element is entry [i][j]. The accumulator (third argument, src1 in
13041 // the AMD ISA) provides the initial value of each result bit, into which
13042 // the bit-matrix product of the first two arguments (src2 * src3) is
13043 // reduced with OR (vbmacor) or XOR (vbmacxor):
13044 // for i in 0..15, j in 0..15:
13045 // bit = C[16*i+j]
13046 // for k in 0..15: bit OP= A[16*i+k] & B[16*k+j]
13047 // dest[16*i+j] = bit
13048 APValue SourceA, SourceB, SourceC;
13049 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
13050 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
13051 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13052 return false;
13053
13054 bool IsXor = E->getBuiltinCallee() ==
13055 clang::X86::BI__builtin_ia32_bmacxor16x16x16_v16hi ||
13056 E->getBuiltinCallee() ==
13057 clang::X86::BI__builtin_ia32_bmacxor16x16x16_v32hi;
13058
13059 unsigned SourceLen = SourceA.getVectorLength();
13060 assert(SourceLen % 16 == 0 && "BMM operates on 256-bit lanes of 16 x i16");
13061 auto *DestTy = E->getType()->castAs<VectorType>();
13062 QualType DestEltTy = DestTy->getElementType();
13063 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13064
13065 SmallVector<APValue, 32> ResultElements(SourceLen);
13066 for (unsigned Lane = 0; Lane != SourceLen; Lane += 16) {
13067 for (unsigned I = 0; I != 16; ++I) {
13068 uint16_t A =
13069 (uint16_t)SourceA.getVectorElt(I: Lane + I).getInt().getZExtValue();
13070 uint16_t Dst =
13071 (uint16_t)SourceC.getVectorElt(I: Lane + I).getInt().getZExtValue();
13072 for (unsigned J = 0; J != 16; ++J) {
13073 // Seed the reduction with the accumulator bit, then fold in each
13074 // product term with the same operator (OR for vbmacor, XOR for
13075 // vbmacxor).
13076 unsigned Bit = (Dst >> J) & 1u;
13077 for (unsigned K = 0; K != 16; ++K) {
13078 uint16_t B = (uint16_t)SourceB.getVectorElt(I: Lane + K)
13079 .getInt()
13080 .getZExtValue();
13081 unsigned Product = ((A >> K) & 1u) & ((B >> J) & 1u);
13082 Bit = IsXor ? (Bit ^ Product) : (Bit | Product);
13083 }
13084 Dst = (Dst & ~(uint16_t(1) << J)) | (uint16_t(Bit) << J);
13085 }
13086 ResultElements[Lane + I] =
13087 APValue(APSInt(APInt(16, Dst), DestUnsigned));
13088 }
13089 }
13090 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13091 }
13092
13093 case clang::X86::BI__builtin_ia32_dbpsadbw128:
13094 case clang::X86::BI__builtin_ia32_dbpsadbw256:
13095 case clang::X86::BI__builtin_ia32_dbpsadbw512: {
13096 APValue SourceA, SourceB, SourceImm;
13097 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
13098 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
13099 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceImm))
13100 return false;
13101
13102 unsigned SourceLen = SourceA.getVectorLength();
13103 constexpr unsigned LaneSize = 16; // 128-bit lane = 16 bytes
13104 unsigned Imm = SourceImm.getInt().getZExtValue();
13105
13106 auto *DestTy = E->getType()->castAs<VectorType>();
13107 QualType DestEltTy = DestTy->getElementType();
13108 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13109 SmallVector<APValue, 32> ResultElements;
13110 ResultElements.reserve(N: SourceLen / 2);
13111
13112 // Phase 1: Shuffle SourceB using all four 2-bit fields of imm8.
13113 // Within each 128-bit lane, for group j (0..3), select a 4-byte block
13114 // from SourceB based on bits [2*j+1:2*j] of imm8.
13115 SmallVector<uint8_t, 64> Shuffled(SourceLen);
13116 for (unsigned I = 0; I < SourceLen; I += LaneSize) {
13117 for (unsigned J = 0; J < 4; ++J) {
13118 unsigned Part = (Imm >> (2 * J)) & 3;
13119 for (unsigned K = 0; K < 4; ++K) {
13120 Shuffled[I + 4 * J + K] = static_cast<uint8_t>(
13121 SourceB.getVectorElt(I: I + 4 * Part + K).getInt().getZExtValue());
13122 }
13123 }
13124 }
13125
13126 // Phase 2: Sliding SAD computation.
13127 // For every group of 4 output u16 values, compute absolute differences
13128 // using overlapping windows into SourceA and the shuffled array.
13129 unsigned Size = SourceLen / 2; // number of output u16 elements
13130 for (unsigned I = 0; I < Size; I += 4) {
13131 unsigned Sad[4] = {0, 0, 0, 0};
13132 for (unsigned J = 0; J < 4; ++J) {
13133 uint8_t A1 = static_cast<uint8_t>(
13134 SourceA.getVectorElt(I: 2 * I + J).getInt().getZExtValue());
13135 uint8_t A2 = static_cast<uint8_t>(
13136 SourceA.getVectorElt(I: 2 * I + J + 4).getInt().getZExtValue());
13137 uint8_t B0 = Shuffled[2 * I + J];
13138 uint8_t B1 = Shuffled[2 * I + J + 1];
13139 uint8_t B2 = Shuffled[2 * I + J + 2];
13140 uint8_t B3 = Shuffled[2 * I + J + 3];
13141 Sad[0] += (A1 > B0) ? (A1 - B0) : (B0 - A1);
13142 Sad[1] += (A1 > B1) ? (A1 - B1) : (B1 - A1);
13143 Sad[2] += (A2 > B2) ? (A2 - B2) : (B2 - A2);
13144 Sad[3] += (A2 > B3) ? (A2 - B3) : (B3 - A2);
13145 }
13146 for (unsigned R = 0; R < 4; ++R)
13147 ResultElements.push_back(
13148 Elt: APValue(APSInt(APInt(16, Sad[R]), DestUnsigned)));
13149 }
13150
13151 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13152 }
13153
13154 case clang::X86::BI__builtin_ia32_mpsadbw128:
13155 case clang::X86::BI__builtin_ia32_mpsadbw256: {
13156 APValue SourceA, SourceB;
13157 APSInt SourceImm;
13158 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: SourceA, Info) ||
13159 !EvaluateVector(E: E->getArg(Arg: 1), Result&: SourceB, Info) ||
13160 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: SourceImm, Info))
13161 return false;
13162 unsigned SourceLen = SourceA.getVectorLength();
13163 constexpr unsigned LaneSize = 16;
13164 assert((SourceLen == LaneSize || SourceLen == 2 * LaneSize) &&
13165 "MPSADBW operates on 128-bit or 256-bit vectors");
13166 unsigned NumLanes = SourceLen / LaneSize;
13167 unsigned Imm = SourceImm.getZExtValue();
13168
13169 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13170 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13171 SmallVector<APValue, 16> ResultElements;
13172 ResultElements.reserve(N: SourceLen / 2);
13173
13174 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
13175 unsigned Ctrl = (Imm >> (3 * Lane)) & 0x7;
13176 unsigned AOff = ((Ctrl >> 2) & 1) * 4;
13177 unsigned BOff = (Ctrl & 3) * 4;
13178 for (unsigned J = 0; J != 8; ++J) {
13179 uint16_t Sad = 0;
13180 for (unsigned K = 0; K != 4; ++K) {
13181 uint8_t A = static_cast<uint8_t>(
13182 SourceA.getVectorElt(I: Lane * LaneSize + AOff + J + K)
13183 .getInt()
13184 .getZExtValue());
13185 uint8_t B = static_cast<uint8_t>(
13186 SourceB.getVectorElt(I: Lane * LaneSize + BOff + K)
13187 .getInt()
13188 .getZExtValue());
13189 Sad += (A > B) ? (A - B) : (B - A);
13190 }
13191 ResultElements.push_back(Elt: APValue(APSInt(APInt(16, Sad), DestUnsigned)));
13192 }
13193 }
13194 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13195 }
13196
13197 case clang::X86::BI__builtin_ia32_pmulhuw128:
13198 case clang::X86::BI__builtin_ia32_pmulhuw256:
13199 case clang::X86::BI__builtin_ia32_pmulhuw512:
13200 return EvaluateBinOpExpr(llvm::APIntOps::mulhu);
13201
13202 case clang::X86::BI__builtin_ia32_pmulhw128:
13203 case clang::X86::BI__builtin_ia32_pmulhw256:
13204 case clang::X86::BI__builtin_ia32_pmulhw512:
13205 return EvaluateBinOpExpr(llvm::APIntOps::mulhs);
13206
13207 case clang::X86::BI__builtin_ia32_psllv2di:
13208 case clang::X86::BI__builtin_ia32_psllv4di:
13209 case clang::X86::BI__builtin_ia32_psllv4si:
13210 case clang::X86::BI__builtin_ia32_psllv8di:
13211 case clang::X86::BI__builtin_ia32_psllv8hi:
13212 case clang::X86::BI__builtin_ia32_psllv8si:
13213 case clang::X86::BI__builtin_ia32_psllv16hi:
13214 case clang::X86::BI__builtin_ia32_psllv16si:
13215 case clang::X86::BI__builtin_ia32_psllv32hi:
13216 case clang::X86::BI__builtin_ia32_psllwi128:
13217 case clang::X86::BI__builtin_ia32_pslldi128:
13218 case clang::X86::BI__builtin_ia32_psllqi128:
13219 case clang::X86::BI__builtin_ia32_psllwi256:
13220 case clang::X86::BI__builtin_ia32_pslldi256:
13221 case clang::X86::BI__builtin_ia32_psllqi256:
13222 case clang::X86::BI__builtin_ia32_psllwi512:
13223 case clang::X86::BI__builtin_ia32_pslldi512:
13224 case clang::X86::BI__builtin_ia32_psllqi512:
13225 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13226 if (RHS.uge(RHS: LHS.getBitWidth())) {
13227 return APInt::getZero(numBits: LHS.getBitWidth());
13228 }
13229 return LHS.shl(shiftAmt: RHS.getZExtValue());
13230 });
13231
13232 case clang::X86::BI__builtin_ia32_psrav4si:
13233 case clang::X86::BI__builtin_ia32_psrav8di:
13234 case clang::X86::BI__builtin_ia32_psrav8hi:
13235 case clang::X86::BI__builtin_ia32_psrav8si:
13236 case clang::X86::BI__builtin_ia32_psrav16hi:
13237 case clang::X86::BI__builtin_ia32_psrav16si:
13238 case clang::X86::BI__builtin_ia32_psrav32hi:
13239 case clang::X86::BI__builtin_ia32_psravq128:
13240 case clang::X86::BI__builtin_ia32_psravq256:
13241 case clang::X86::BI__builtin_ia32_psrawi128:
13242 case clang::X86::BI__builtin_ia32_psradi128:
13243 case clang::X86::BI__builtin_ia32_psraqi128:
13244 case clang::X86::BI__builtin_ia32_psrawi256:
13245 case clang::X86::BI__builtin_ia32_psradi256:
13246 case clang::X86::BI__builtin_ia32_psraqi256:
13247 case clang::X86::BI__builtin_ia32_psrawi512:
13248 case clang::X86::BI__builtin_ia32_psradi512:
13249 case clang::X86::BI__builtin_ia32_psraqi512:
13250 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13251 if (RHS.uge(RHS: LHS.getBitWidth())) {
13252 return LHS.ashr(ShiftAmt: LHS.getBitWidth() - 1);
13253 }
13254 return LHS.ashr(ShiftAmt: RHS.getZExtValue());
13255 });
13256
13257 case clang::X86::BI__builtin_ia32_psrlv2di:
13258 case clang::X86::BI__builtin_ia32_psrlv4di:
13259 case clang::X86::BI__builtin_ia32_psrlv4si:
13260 case clang::X86::BI__builtin_ia32_psrlv8di:
13261 case clang::X86::BI__builtin_ia32_psrlv8hi:
13262 case clang::X86::BI__builtin_ia32_psrlv8si:
13263 case clang::X86::BI__builtin_ia32_psrlv16hi:
13264 case clang::X86::BI__builtin_ia32_psrlv16si:
13265 case clang::X86::BI__builtin_ia32_psrlv32hi:
13266 case clang::X86::BI__builtin_ia32_psrlwi128:
13267 case clang::X86::BI__builtin_ia32_psrldi128:
13268 case clang::X86::BI__builtin_ia32_psrlqi128:
13269 case clang::X86::BI__builtin_ia32_psrlwi256:
13270 case clang::X86::BI__builtin_ia32_psrldi256:
13271 case clang::X86::BI__builtin_ia32_psrlqi256:
13272 case clang::X86::BI__builtin_ia32_psrlwi512:
13273 case clang::X86::BI__builtin_ia32_psrldi512:
13274 case clang::X86::BI__builtin_ia32_psrlqi512:
13275 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13276 if (RHS.uge(RHS: LHS.getBitWidth())) {
13277 return APInt::getZero(numBits: LHS.getBitWidth());
13278 }
13279 return LHS.lshr(shiftAmt: RHS.getZExtValue());
13280 });
13281 case X86::BI__builtin_ia32_packsswb128:
13282 case X86::BI__builtin_ia32_packsswb256:
13283 case X86::BI__builtin_ia32_packsswb512:
13284 case X86::BI__builtin_ia32_packssdw128:
13285 case X86::BI__builtin_ia32_packssdw256:
13286 case X86::BI__builtin_ia32_packssdw512:
13287 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
13288 return APSInt(Src).truncSSat(width: Src.getBitWidth() / 2);
13289 });
13290 case X86::BI__builtin_ia32_packusdw128:
13291 case X86::BI__builtin_ia32_packusdw256:
13292 case X86::BI__builtin_ia32_packusdw512:
13293 case X86::BI__builtin_ia32_packuswb128:
13294 case X86::BI__builtin_ia32_packuswb256:
13295 case X86::BI__builtin_ia32_packuswb512:
13296 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
13297 return APSInt(Src).truncSSatU(width: Src.getBitWidth() / 2);
13298 });
13299 case clang::X86::BI__builtin_ia32_selectss_128:
13300 return EvalSelectScalar(4);
13301 case clang::X86::BI__builtin_ia32_selectsd_128:
13302 return EvalSelectScalar(2);
13303 case clang::X86::BI__builtin_ia32_selectsh_128:
13304 case clang::X86::BI__builtin_ia32_selectsbf_128:
13305 return EvalSelectScalar(8);
13306 case clang::X86::BI__builtin_ia32_pmuldq128:
13307 case clang::X86::BI__builtin_ia32_pmuldq256:
13308 case clang::X86::BI__builtin_ia32_pmuldq512:
13309 case clang::X86::BI__builtin_ia32_pmuludq128:
13310 case clang::X86::BI__builtin_ia32_pmuludq256:
13311 case clang::X86::BI__builtin_ia32_pmuludq512: {
13312 APValue SourceLHS, SourceRHS;
13313 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13314 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13315 return false;
13316
13317 unsigned SourceLen = SourceLHS.getVectorLength();
13318 SmallVector<APValue, 4> ResultElements;
13319 ResultElements.reserve(N: SourceLen / 2);
13320
13321 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
13322 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13323 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13324
13325 switch (BuiltinOp) {
13326 case clang::X86::BI__builtin_ia32_pmuludq128:
13327 case clang::X86::BI__builtin_ia32_pmuludq256:
13328 case clang::X86::BI__builtin_ia32_pmuludq512:
13329 ResultElements.push_back(
13330 Elt: APValue(APSInt(llvm::APIntOps::muluExtended(C1: LHS, C2: RHS), true)));
13331 break;
13332 case clang::X86::BI__builtin_ia32_pmuldq128:
13333 case clang::X86::BI__builtin_ia32_pmuldq256:
13334 case clang::X86::BI__builtin_ia32_pmuldq512:
13335 ResultElements.push_back(
13336 Elt: APValue(APSInt(llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS), false)));
13337 break;
13338 }
13339 }
13340
13341 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13342 }
13343
13344 case X86::BI__builtin_ia32_vpmadd52luq128:
13345 case X86::BI__builtin_ia32_vpmadd52luq256:
13346 case X86::BI__builtin_ia32_vpmadd52luq512: {
13347 APValue A, B, C;
13348 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
13349 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
13350 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
13351 return false;
13352
13353 unsigned ALen = A.getVectorLength();
13354 SmallVector<APValue, 4> ResultElements;
13355 ResultElements.reserve(N: ALen);
13356
13357 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
13358 APInt AElt = A.getVectorElt(I: EltNum).getInt();
13359 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13360 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13361 APSInt ResElt(AElt + (BElt * CElt).zext(width: 64), false);
13362 ResultElements.push_back(Elt: APValue(ResElt));
13363 }
13364
13365 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13366 }
13367 case X86::BI__builtin_ia32_vpmadd52huq128:
13368 case X86::BI__builtin_ia32_vpmadd52huq256:
13369 case X86::BI__builtin_ia32_vpmadd52huq512: {
13370 APValue A, B, C;
13371 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
13372 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
13373 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
13374 return false;
13375
13376 unsigned ALen = A.getVectorLength();
13377 SmallVector<APValue, 4> ResultElements;
13378 ResultElements.reserve(N: ALen);
13379
13380 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
13381 APInt AElt = A.getVectorElt(I: EltNum).getInt();
13382 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13383 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13384 APSInt ResElt(AElt + llvm::APIntOps::mulhu(C1: BElt, C2: CElt).zext(width: 64), false);
13385 ResultElements.push_back(Elt: APValue(ResElt));
13386 }
13387
13388 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13389 }
13390
13391 case clang::X86::BI__builtin_ia32_vprotbi:
13392 case clang::X86::BI__builtin_ia32_vprotdi:
13393 case clang::X86::BI__builtin_ia32_vprotqi:
13394 case clang::X86::BI__builtin_ia32_vprotwi:
13395 case clang::X86::BI__builtin_ia32_prold128:
13396 case clang::X86::BI__builtin_ia32_prold256:
13397 case clang::X86::BI__builtin_ia32_prold512:
13398 case clang::X86::BI__builtin_ia32_prolq128:
13399 case clang::X86::BI__builtin_ia32_prolq256:
13400 case clang::X86::BI__builtin_ia32_prolq512:
13401 return EvaluateBinOpExpr(
13402 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotl(rotateAmt: RHS); });
13403
13404 case clang::X86::BI__builtin_ia32_prord128:
13405 case clang::X86::BI__builtin_ia32_prord256:
13406 case clang::X86::BI__builtin_ia32_prord512:
13407 case clang::X86::BI__builtin_ia32_prorq128:
13408 case clang::X86::BI__builtin_ia32_prorq256:
13409 case clang::X86::BI__builtin_ia32_prorq512:
13410 return EvaluateBinOpExpr(
13411 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotr(rotateAmt: RHS); });
13412
13413 case Builtin::BI__builtin_elementwise_max:
13414 case Builtin::BI__builtin_elementwise_min: {
13415 APValue SourceLHS, SourceRHS;
13416 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13417 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13418 return false;
13419
13420 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13421
13422 if (!DestEltTy->isIntegerType())
13423 return false;
13424
13425 unsigned SourceLen = SourceLHS.getVectorLength();
13426 SmallVector<APValue, 4> ResultElements;
13427 ResultElements.reserve(N: SourceLen);
13428
13429 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13430 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13431 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13432 switch (BuiltinOp) {
13433 case Builtin::BI__builtin_elementwise_max:
13434 ResultElements.push_back(
13435 Elt: APValue(APSInt(std::max(a: LHS, b: RHS),
13436 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13437 break;
13438 case Builtin::BI__builtin_elementwise_min:
13439 ResultElements.push_back(
13440 Elt: APValue(APSInt(std::min(a: LHS, b: RHS),
13441 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13442 break;
13443 }
13444 }
13445
13446 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13447 }
13448 case X86::BI__builtin_ia32_vpshldd128:
13449 case X86::BI__builtin_ia32_vpshldd256:
13450 case X86::BI__builtin_ia32_vpshldd512:
13451 case X86::BI__builtin_ia32_vpshldq128:
13452 case X86::BI__builtin_ia32_vpshldq256:
13453 case X86::BI__builtin_ia32_vpshldq512:
13454 case X86::BI__builtin_ia32_vpshldw128:
13455 case X86::BI__builtin_ia32_vpshldw256:
13456 case X86::BI__builtin_ia32_vpshldw512: {
13457 APValue SourceHi, SourceLo, SourceAmt;
13458 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
13459 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
13460 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13461 return false;
13462
13463 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13464 unsigned SourceLen = SourceHi.getVectorLength();
13465 SmallVector<APValue, 32> ResultElements;
13466 ResultElements.reserve(N: SourceLen);
13467
13468 APInt Amt = SourceAmt.getInt();
13469 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13470 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13471 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13472 APInt R = llvm::APIntOps::fshl(Hi, Lo, Shift: Amt);
13473 ResultElements.push_back(
13474 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13475 }
13476
13477 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13478 }
13479 case X86::BI__builtin_ia32_vpshrdd128:
13480 case X86::BI__builtin_ia32_vpshrdd256:
13481 case X86::BI__builtin_ia32_vpshrdd512:
13482 case X86::BI__builtin_ia32_vpshrdq128:
13483 case X86::BI__builtin_ia32_vpshrdq256:
13484 case X86::BI__builtin_ia32_vpshrdq512:
13485 case X86::BI__builtin_ia32_vpshrdw128:
13486 case X86::BI__builtin_ia32_vpshrdw256:
13487 case X86::BI__builtin_ia32_vpshrdw512: {
13488 // NOTE: Reversed Hi/Lo operands.
13489 APValue SourceHi, SourceLo, SourceAmt;
13490 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLo) ||
13491 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceHi) ||
13492 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13493 return false;
13494
13495 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13496 unsigned SourceLen = SourceHi.getVectorLength();
13497 SmallVector<APValue, 32> ResultElements;
13498 ResultElements.reserve(N: SourceLen);
13499
13500 APInt Amt = SourceAmt.getInt();
13501 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13502 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13503 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13504 APInt R = llvm::APIntOps::fshr(Hi, Lo, Shift: Amt);
13505 ResultElements.push_back(
13506 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13507 }
13508
13509 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13510 }
13511 case X86::BI__builtin_ia32_compressdf128_mask:
13512 case X86::BI__builtin_ia32_compressdf256_mask:
13513 case X86::BI__builtin_ia32_compressdf512_mask:
13514 case X86::BI__builtin_ia32_compressdi128_mask:
13515 case X86::BI__builtin_ia32_compressdi256_mask:
13516 case X86::BI__builtin_ia32_compressdi512_mask:
13517 case X86::BI__builtin_ia32_compresshi128_mask:
13518 case X86::BI__builtin_ia32_compresshi256_mask:
13519 case X86::BI__builtin_ia32_compresshi512_mask:
13520 case X86::BI__builtin_ia32_compressqi128_mask:
13521 case X86::BI__builtin_ia32_compressqi256_mask:
13522 case X86::BI__builtin_ia32_compressqi512_mask:
13523 case X86::BI__builtin_ia32_compresssf128_mask:
13524 case X86::BI__builtin_ia32_compresssf256_mask:
13525 case X86::BI__builtin_ia32_compresssf512_mask:
13526 case X86::BI__builtin_ia32_compresssi128_mask:
13527 case X86::BI__builtin_ia32_compresssi256_mask:
13528 case X86::BI__builtin_ia32_compresssi512_mask: {
13529 APValue Source, Passthru;
13530 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13531 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13532 return false;
13533 APSInt Mask;
13534 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13535 return false;
13536
13537 unsigned NumElts = Source.getVectorLength();
13538 SmallVector<APValue, 64> ResultElements;
13539 ResultElements.reserve(N: NumElts);
13540
13541 for (unsigned I = 0; I != NumElts; ++I) {
13542 if (Mask[I])
13543 ResultElements.push_back(Elt: Source.getVectorElt(I));
13544 }
13545 for (unsigned I = ResultElements.size(); I != NumElts; ++I) {
13546 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13547 }
13548
13549 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13550 }
13551 case X86::BI__builtin_ia32_expanddf128_mask:
13552 case X86::BI__builtin_ia32_expanddf256_mask:
13553 case X86::BI__builtin_ia32_expanddf512_mask:
13554 case X86::BI__builtin_ia32_expanddi128_mask:
13555 case X86::BI__builtin_ia32_expanddi256_mask:
13556 case X86::BI__builtin_ia32_expanddi512_mask:
13557 case X86::BI__builtin_ia32_expandhi128_mask:
13558 case X86::BI__builtin_ia32_expandhi256_mask:
13559 case X86::BI__builtin_ia32_expandhi512_mask:
13560 case X86::BI__builtin_ia32_expandqi128_mask:
13561 case X86::BI__builtin_ia32_expandqi256_mask:
13562 case X86::BI__builtin_ia32_expandqi512_mask:
13563 case X86::BI__builtin_ia32_expandsf128_mask:
13564 case X86::BI__builtin_ia32_expandsf256_mask:
13565 case X86::BI__builtin_ia32_expandsf512_mask:
13566 case X86::BI__builtin_ia32_expandsi128_mask:
13567 case X86::BI__builtin_ia32_expandsi256_mask:
13568 case X86::BI__builtin_ia32_expandsi512_mask: {
13569 APValue Source, Passthru;
13570 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13571 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13572 return false;
13573 APSInt Mask;
13574 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13575 return false;
13576
13577 unsigned NumElts = Source.getVectorLength();
13578 SmallVector<APValue, 64> ResultElements;
13579 ResultElements.reserve(N: NumElts);
13580
13581 unsigned SourceIdx = 0;
13582 for (unsigned I = 0; I != NumElts; ++I) {
13583 if (Mask[I])
13584 ResultElements.push_back(Elt: Source.getVectorElt(I: SourceIdx++));
13585 else
13586 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13587 }
13588 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13589 }
13590 case X86::BI__builtin_ia32_vpconflictsi_128:
13591 case X86::BI__builtin_ia32_vpconflictsi_256:
13592 case X86::BI__builtin_ia32_vpconflictsi_512:
13593 case X86::BI__builtin_ia32_vpconflictdi_128:
13594 case X86::BI__builtin_ia32_vpconflictdi_256:
13595 case X86::BI__builtin_ia32_vpconflictdi_512: {
13596 APValue Source;
13597
13598 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
13599 return false;
13600
13601 unsigned SourceLen = Source.getVectorLength();
13602 SmallVector<APValue, 32> ResultElements;
13603 ResultElements.reserve(N: SourceLen);
13604
13605 const auto *VecT = E->getType()->castAs<VectorType>();
13606 bool DestUnsigned =
13607 VecT->getElementType()->isUnsignedIntegerOrEnumerationType();
13608
13609 for (unsigned I = 0; I != SourceLen; ++I) {
13610 const APValue &EltI = Source.getVectorElt(I);
13611
13612 APInt ConflictMask(EltI.getInt().getBitWidth(), 0);
13613 for (unsigned J = 0; J != I; ++J) {
13614 const APValue &EltJ = Source.getVectorElt(I: J);
13615 ConflictMask.setBitVal(BitPosition: J, BitValue: EltI.getInt() == EltJ.getInt());
13616 }
13617 ResultElements.push_back(Elt: APValue(APSInt(ConflictMask, DestUnsigned)));
13618 }
13619 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13620 }
13621 case X86::BI__builtin_ia32_blendpd:
13622 case X86::BI__builtin_ia32_blendpd256:
13623 case X86::BI__builtin_ia32_blendps:
13624 case X86::BI__builtin_ia32_blendps256:
13625 case X86::BI__builtin_ia32_pblendw128:
13626 case X86::BI__builtin_ia32_pblendw256:
13627 case X86::BI__builtin_ia32_pblendd128:
13628 case X86::BI__builtin_ia32_pblendd256: {
13629 APValue SourceF, SourceT, SourceC;
13630 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13631 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13632 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13633 return false;
13634
13635 const APInt &C = SourceC.getInt();
13636 unsigned SourceLen = SourceF.getVectorLength();
13637 SmallVector<APValue, 32> ResultElements;
13638 ResultElements.reserve(N: SourceLen);
13639 for (unsigned EltNum = 0; EltNum != SourceLen; ++EltNum) {
13640 const APValue &F = SourceF.getVectorElt(I: EltNum);
13641 const APValue &T = SourceT.getVectorElt(I: EltNum);
13642 ResultElements.push_back(Elt: C[EltNum % 8] ? T : F);
13643 }
13644
13645 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13646 }
13647
13648 case X86::BI__builtin_ia32_psignb128:
13649 case X86::BI__builtin_ia32_psignb256:
13650 case X86::BI__builtin_ia32_psignw128:
13651 case X86::BI__builtin_ia32_psignw256:
13652 case X86::BI__builtin_ia32_psignd128:
13653 case X86::BI__builtin_ia32_psignd256:
13654 return EvaluateBinOpExpr([](const APInt &AElem, const APInt &BElem) {
13655 if (BElem.isZero())
13656 return APInt::getZero(numBits: AElem.getBitWidth());
13657 if (BElem.isNegative())
13658 return -AElem;
13659 return AElem;
13660 });
13661
13662 case X86::BI__builtin_ia32_blendvpd:
13663 case X86::BI__builtin_ia32_blendvpd256:
13664 case X86::BI__builtin_ia32_blendvps:
13665 case X86::BI__builtin_ia32_blendvps256:
13666 case X86::BI__builtin_ia32_pblendvb128:
13667 case X86::BI__builtin_ia32_pblendvb256: {
13668 // SSE blendv by mask signbit: "Result = C[] < 0 ? T[] : F[]".
13669 APValue SourceF, SourceT, SourceC;
13670 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13671 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13672 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13673 return false;
13674
13675 unsigned SourceLen = SourceF.getVectorLength();
13676 SmallVector<APValue, 32> ResultElements;
13677 ResultElements.reserve(N: SourceLen);
13678
13679 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13680 const APValue &F = SourceF.getVectorElt(I: EltNum);
13681 const APValue &T = SourceT.getVectorElt(I: EltNum);
13682 const APValue &C = SourceC.getVectorElt(I: EltNum);
13683 APInt M = C.isInt() ? (APInt)C.getInt() : C.getFloat().bitcastToAPInt();
13684 ResultElements.push_back(Elt: M.isNegative() ? T : F);
13685 }
13686
13687 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13688 }
13689 case X86::BI__builtin_ia32_selectb_128:
13690 case X86::BI__builtin_ia32_selectb_256:
13691 case X86::BI__builtin_ia32_selectb_512:
13692 case X86::BI__builtin_ia32_selectw_128:
13693 case X86::BI__builtin_ia32_selectw_256:
13694 case X86::BI__builtin_ia32_selectw_512:
13695 case X86::BI__builtin_ia32_selectd_128:
13696 case X86::BI__builtin_ia32_selectd_256:
13697 case X86::BI__builtin_ia32_selectd_512:
13698 case X86::BI__builtin_ia32_selectq_128:
13699 case X86::BI__builtin_ia32_selectq_256:
13700 case X86::BI__builtin_ia32_selectq_512:
13701 case X86::BI__builtin_ia32_selectph_128:
13702 case X86::BI__builtin_ia32_selectph_256:
13703 case X86::BI__builtin_ia32_selectph_512:
13704 case X86::BI__builtin_ia32_selectpbf_128:
13705 case X86::BI__builtin_ia32_selectpbf_256:
13706 case X86::BI__builtin_ia32_selectpbf_512:
13707 case X86::BI__builtin_ia32_selectps_128:
13708 case X86::BI__builtin_ia32_selectps_256:
13709 case X86::BI__builtin_ia32_selectps_512:
13710 case X86::BI__builtin_ia32_selectpd_128:
13711 case X86::BI__builtin_ia32_selectpd_256:
13712 case X86::BI__builtin_ia32_selectpd_512: {
13713 // AVX512 predicated move: "Result = Mask[] ? LHS[] : RHS[]".
13714 APValue SourceMask, SourceLHS, SourceRHS;
13715 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceMask) ||
13716 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLHS) ||
13717 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceRHS))
13718 return false;
13719
13720 APSInt Mask = SourceMask.getInt();
13721 unsigned SourceLen = SourceLHS.getVectorLength();
13722 SmallVector<APValue, 4> ResultElements;
13723 ResultElements.reserve(N: SourceLen);
13724
13725 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13726 const APValue &LHS = SourceLHS.getVectorElt(I: EltNum);
13727 const APValue &RHS = SourceRHS.getVectorElt(I: EltNum);
13728 ResultElements.push_back(Elt: Mask[EltNum] ? LHS : RHS);
13729 }
13730
13731 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13732 }
13733
13734 case X86::BI__builtin_ia32_cvtsd2ss: {
13735 APValue VecA, VecB;
13736 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13737 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB))
13738 return false;
13739
13740 SmallVector<APValue, 4> Elements;
13741
13742 APValue ResultVal;
13743 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13744 Result&: ResultVal))
13745 return false;
13746
13747 Elements.push_back(Elt: ResultVal);
13748
13749 unsigned NumEltsA = VecA.getVectorLength();
13750 for (unsigned I = 1; I < NumEltsA; ++I) {
13751 Elements.push_back(Elt: VecA.getVectorElt(I));
13752 }
13753
13754 return Success(V: Elements, E);
13755 }
13756 case X86::BI__builtin_ia32_cvtsd2ss_round_mask: {
13757 APValue VecA, VecB, VecSrc, MaskValue;
13758
13759 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13760 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
13761 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
13762 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: MaskValue))
13763 return false;
13764
13765 unsigned Mask = MaskValue.getInt().getZExtValue();
13766 SmallVector<APValue, 4> Elements;
13767
13768 if (Mask & 1) {
13769 APValue ResultVal;
13770 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13771 Result&: ResultVal))
13772 return false;
13773 Elements.push_back(Elt: ResultVal);
13774 } else {
13775 Elements.push_back(Elt: VecSrc.getVectorElt(I: 0));
13776 }
13777
13778 unsigned NumEltsA = VecA.getVectorLength();
13779 for (unsigned I = 1; I < NumEltsA; ++I) {
13780 Elements.push_back(Elt: VecA.getVectorElt(I));
13781 }
13782
13783 return Success(V: Elements, E);
13784 }
13785 case X86::BI__builtin_ia32_cvtpd2ps:
13786 case X86::BI__builtin_ia32_cvtpd2ps256:
13787 case X86::BI__builtin_ia32_cvtpd2ps_mask:
13788 case X86::BI__builtin_ia32_cvtpd2ps512_mask: {
13789
13790 const auto BuiltinID = BuiltinOp;
13791 bool IsMasked = (BuiltinID == X86::BI__builtin_ia32_cvtpd2ps_mask ||
13792 BuiltinID == X86::BI__builtin_ia32_cvtpd2ps512_mask);
13793
13794 APValue InputValue;
13795 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: InputValue))
13796 return false;
13797
13798 APValue MergeValue;
13799 unsigned Mask = 0xFFFFFFFF;
13800 bool NeedsMerge = false;
13801 if (IsMasked) {
13802 APValue MaskValue;
13803 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MaskValue))
13804 return false;
13805 Mask = MaskValue.getInt().getZExtValue();
13806 auto NumEltsResult = E->getType()->getAs<VectorType>()->getNumElements();
13807 for (unsigned I = 0; I < NumEltsResult; ++I) {
13808 if (!((Mask >> I) & 1)) {
13809 NeedsMerge = true;
13810 break;
13811 }
13812 }
13813 if (NeedsMerge) {
13814 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: MergeValue))
13815 return false;
13816 }
13817 }
13818
13819 unsigned NumEltsResult =
13820 E->getType()->getAs<VectorType>()->getNumElements();
13821 unsigned NumEltsInput = InputValue.getVectorLength();
13822 SmallVector<APValue, 8> Elements;
13823 for (unsigned I = 0; I < NumEltsResult; ++I) {
13824 if (IsMasked && !((Mask >> I) & 1)) {
13825 if (!NeedsMerge) {
13826 return false;
13827 }
13828 Elements.push_back(Elt: MergeValue.getVectorElt(I));
13829 continue;
13830 }
13831
13832 if (I >= NumEltsInput) {
13833 Elements.push_back(Elt: APValue(APFloat::getZero(Sem: APFloat::IEEEsingle())));
13834 continue;
13835 }
13836
13837 APValue ResultVal;
13838 if (!ConvertDoubleToFloatStrict(
13839 Info, E, OrigVal: InputValue.getVectorElt(I).getFloat(), Result&: ResultVal))
13840 return false;
13841
13842 Elements.push_back(Elt: ResultVal);
13843 }
13844 return Success(V: Elements, E);
13845 }
13846
13847 case X86::BI__builtin_ia32_shufps:
13848 case X86::BI__builtin_ia32_shufps256:
13849 case X86::BI__builtin_ia32_shufps512: {
13850 APValue R;
13851 if (!evalShuffleGeneric(
13852 Info, Call: E, Out&: R,
13853 GetSourceIndex: [](unsigned DstIdx,
13854 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13855 constexpr unsigned LaneBits = 128u;
13856 unsigned NumElemPerLane = LaneBits / 32;
13857 unsigned NumSelectableElems = NumElemPerLane / 2;
13858 unsigned BitsPerElem = 2;
13859 unsigned IndexMask = (1u << BitsPerElem) - 1;
13860 unsigned MaskBits = 8;
13861 unsigned Lane = DstIdx / NumElemPerLane;
13862 unsigned ElemInLane = DstIdx % NumElemPerLane;
13863 unsigned LaneOffset = Lane * NumElemPerLane;
13864 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
13865 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
13866 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
13867 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
13868 }))
13869 return false;
13870 return Success(V: R, E);
13871 }
13872 case X86::BI__builtin_ia32_shufpd:
13873 case X86::BI__builtin_ia32_shufpd256:
13874 case X86::BI__builtin_ia32_shufpd512: {
13875 APValue R;
13876 if (!evalShuffleGeneric(
13877 Info, Call: E, Out&: R,
13878 GetSourceIndex: [](unsigned DstIdx,
13879 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13880 constexpr unsigned LaneBits = 128u;
13881 unsigned NumElemPerLane = LaneBits / 64;
13882 unsigned NumSelectableElems = NumElemPerLane / 2;
13883 unsigned BitsPerElem = 1;
13884 unsigned IndexMask = (1u << BitsPerElem) - 1;
13885 unsigned MaskBits = 8;
13886 unsigned Lane = DstIdx / NumElemPerLane;
13887 unsigned ElemInLane = DstIdx % NumElemPerLane;
13888 unsigned LaneOffset = Lane * NumElemPerLane;
13889 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
13890 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
13891 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
13892 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
13893 }))
13894 return false;
13895 return Success(V: R, E);
13896 }
13897 case X86::BI__builtin_ia32_insertps128: {
13898 APValue R;
13899 if (!evalShuffleGeneric(
13900 Info, Call: E, Out&: R,
13901 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13902 // Bits [3:0]: zero mask - if bit is set, zero this element
13903 if ((Mask & (1 << DstIdx)) != 0) {
13904 return {0, -1};
13905 }
13906 // Bits [7:6]: select element from source vector Y (0-3)
13907 // Bits [5:4]: select destination position (0-3)
13908 unsigned SrcElem = (Mask >> 6) & 0x3;
13909 unsigned DstElem = (Mask >> 4) & 0x3;
13910 if (DstIdx == DstElem) {
13911 // Insert element from source vector (B) at this position
13912 return {1, static_cast<int>(SrcElem)};
13913 } else {
13914 // Copy from destination vector (A)
13915 return {0, static_cast<int>(DstIdx)};
13916 }
13917 }))
13918 return false;
13919 return Success(V: R, E);
13920 }
13921 case X86::BI__builtin_ia32_pshufb128:
13922 case X86::BI__builtin_ia32_pshufb256:
13923 case X86::BI__builtin_ia32_pshufb512: {
13924 APValue R;
13925 if (!evalShuffleGeneric(
13926 Info, Call: E, Out&: R,
13927 GetSourceIndex: [](unsigned DstIdx,
13928 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13929 uint8_t Ctlb = static_cast<uint8_t>(ShuffleMask);
13930 if (Ctlb & 0x80)
13931 return std::make_pair(x: 0, y: -1);
13932
13933 unsigned LaneBase = (DstIdx / 16) * 16;
13934 unsigned SrcOffset = Ctlb & 0x0F;
13935 unsigned SrcIdx = LaneBase + SrcOffset;
13936 return std::make_pair(x: 0, y: static_cast<int>(SrcIdx));
13937 }))
13938 return false;
13939 return Success(V: R, E);
13940 }
13941
13942 case X86::BI__builtin_ia32_pshuflw:
13943 case X86::BI__builtin_ia32_pshuflw256:
13944 case X86::BI__builtin_ia32_pshuflw512: {
13945 APValue R;
13946 if (!evalShuffleGeneric(
13947 Info, Call: E, Out&: R,
13948 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13949 constexpr unsigned LaneBits = 128u;
13950 constexpr unsigned ElemBits = 16u;
13951 constexpr unsigned LaneElts = LaneBits / ElemBits;
13952 constexpr unsigned HalfSize = 4;
13953 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13954 unsigned LaneIdx = DstIdx % LaneElts;
13955 if (LaneIdx < HalfSize) {
13956 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
13957 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
13958 }
13959 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
13960 }))
13961 return false;
13962 return Success(V: R, E);
13963 }
13964
13965 case X86::BI__builtin_ia32_pshufhw:
13966 case X86::BI__builtin_ia32_pshufhw256:
13967 case X86::BI__builtin_ia32_pshufhw512: {
13968 APValue R;
13969 if (!evalShuffleGeneric(
13970 Info, Call: E, Out&: R,
13971 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13972 constexpr unsigned LaneBits = 128u;
13973 constexpr unsigned ElemBits = 16u;
13974 constexpr unsigned LaneElts = LaneBits / ElemBits;
13975 constexpr unsigned HalfSize = 4;
13976 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13977 unsigned LaneIdx = DstIdx % LaneElts;
13978 if (LaneIdx >= HalfSize) {
13979 unsigned Rel = LaneIdx - HalfSize;
13980 unsigned Sel = (Mask >> (2 * Rel)) & 0x3;
13981 return std::make_pair(
13982 x: 0, y: static_cast<int>(LaneBase + HalfSize + Sel));
13983 }
13984 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
13985 }))
13986 return false;
13987 return Success(V: R, E);
13988 }
13989
13990 case X86::BI__builtin_ia32_pshufd:
13991 case X86::BI__builtin_ia32_pshufd256:
13992 case X86::BI__builtin_ia32_pshufd512:
13993 case X86::BI__builtin_ia32_vpermilps:
13994 case X86::BI__builtin_ia32_vpermilps256:
13995 case X86::BI__builtin_ia32_vpermilps512: {
13996 APValue R;
13997 if (!evalShuffleGeneric(
13998 Info, Call: E, Out&: R,
13999 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14000 constexpr unsigned LaneBits = 128u;
14001 constexpr unsigned ElemBits = 32u;
14002 constexpr unsigned LaneElts = LaneBits / ElemBits;
14003 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
14004 unsigned LaneIdx = DstIdx % LaneElts;
14005 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
14006 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
14007 }))
14008 return false;
14009 return Success(V: R, E);
14010 }
14011
14012 case X86::BI__builtin_ia32_vpermilvarpd:
14013 case X86::BI__builtin_ia32_vpermilvarpd256:
14014 case X86::BI__builtin_ia32_vpermilvarpd512: {
14015 APValue R;
14016 if (!evalShuffleGeneric(
14017 Info, Call: E, Out&: R,
14018 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14019 unsigned NumElemPerLane = 2;
14020 unsigned Lane = DstIdx / NumElemPerLane;
14021 unsigned Offset = Mask & 0b10 ? 1 : 0;
14022 return std::make_pair(
14023 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
14024 }))
14025 return false;
14026 return Success(V: R, E);
14027 }
14028
14029 case X86::BI__builtin_ia32_vpermilpd:
14030 case X86::BI__builtin_ia32_vpermilpd256:
14031 case X86::BI__builtin_ia32_vpermilpd512: {
14032 APValue R;
14033 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
14034 unsigned NumElemPerLane = 2;
14035 unsigned BitsPerElem = 1;
14036 unsigned MaskBits = 8;
14037 unsigned IndexMask = 0x1;
14038 unsigned Lane = DstIdx / NumElemPerLane;
14039 unsigned LaneOffset = Lane * NumElemPerLane;
14040 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
14041 unsigned Index = (Control >> BitIndex) & IndexMask;
14042 return std::make_pair(x: 0, y: static_cast<int>(LaneOffset + Index));
14043 }))
14044 return false;
14045 return Success(V: R, E);
14046 }
14047
14048 case X86::BI__builtin_ia32_permdf256:
14049 case X86::BI__builtin_ia32_permdi256: {
14050 APValue R;
14051 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
14052 // permute4x64 operates on 4 64-bit elements
14053 // For element i (0-3), extract bits [2*i+1:2*i] from Control
14054 unsigned Index = (Control >> (2 * DstIdx)) & 0x3;
14055 return std::make_pair(x: 0, y: static_cast<int>(Index));
14056 }))
14057 return false;
14058 return Success(V: R, E);
14059 }
14060
14061 case X86::BI__builtin_ia32_vpermilvarps:
14062 case X86::BI__builtin_ia32_vpermilvarps256:
14063 case X86::BI__builtin_ia32_vpermilvarps512: {
14064 APValue R;
14065 if (!evalShuffleGeneric(
14066 Info, Call: E, Out&: R,
14067 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14068 unsigned NumElemPerLane = 4;
14069 unsigned Lane = DstIdx / NumElemPerLane;
14070 unsigned Offset = Mask & 0b11;
14071 return std::make_pair(
14072 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
14073 }))
14074 return false;
14075 return Success(V: R, E);
14076 }
14077
14078 case X86::BI__builtin_ia32_vpmultishiftqb128:
14079 case X86::BI__builtin_ia32_vpmultishiftqb256:
14080 case X86::BI__builtin_ia32_vpmultishiftqb512: {
14081 assert(E->getNumArgs() == 2);
14082
14083 APValue A, B;
14084 if (!Evaluate(Result&: A, Info, E: E->getArg(Arg: 0)) || !Evaluate(Result&: B, Info, E: E->getArg(Arg: 1)))
14085 return false;
14086
14087 assert(A.getVectorLength() == B.getVectorLength());
14088 unsigned NumBytesInQWord = 8;
14089 unsigned NumBitsInByte = 8;
14090 unsigned NumBytes = A.getVectorLength();
14091 unsigned NumQWords = NumBytes / NumBytesInQWord;
14092 SmallVector<APValue, 64> Result;
14093 Result.reserve(N: NumBytes);
14094
14095 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
14096 APInt BQWord(64, 0);
14097 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14098 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
14099 uint64_t Byte = B.getVectorElt(I: Idx).getInt().getZExtValue();
14100 BQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
14101 }
14102
14103 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14104 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
14105 uint64_t Ctrl = A.getVectorElt(I: Idx).getInt().getZExtValue() & 0x3F;
14106
14107 APInt Byte(8, 0);
14108 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
14109 Byte.setBitVal(BitPosition: BitIdx, BitValue: BQWord[(Ctrl + BitIdx) & 0x3F]);
14110 }
14111 Result.push_back(Elt: APValue(APSInt(Byte, /*isUnsigned*/ true)));
14112 }
14113 }
14114 return Success(V: APValue(Result.data(), Result.size()), E);
14115 }
14116
14117 case X86::BI__builtin_ia32_phminposuw128: {
14118 APValue Source;
14119 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
14120 return false;
14121 unsigned SourceLen = Source.getVectorLength();
14122 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14123 QualType ElemQT = VT->getElementType();
14124 unsigned ElemBitWidth = Info.Ctx.getTypeSize(T: ElemQT);
14125
14126 APInt MinIndex(ElemBitWidth, 0);
14127 APInt MinVal = Source.getVectorElt(I: 0).getInt();
14128 for (unsigned I = 1; I != SourceLen; ++I) {
14129 APInt Val = Source.getVectorElt(I).getInt();
14130 if (MinVal.ugt(RHS: Val)) {
14131 MinVal = Val;
14132 MinIndex = I;
14133 }
14134 }
14135
14136 bool ResultUnsigned = E->getCallReturnType(Ctx: Info.Ctx)
14137 ->castAs<VectorType>()
14138 ->getElementType()
14139 ->isUnsignedIntegerOrEnumerationType();
14140
14141 SmallVector<APValue, 8> Result;
14142 Result.reserve(N: SourceLen);
14143 Result.emplace_back(Args: APSInt(MinVal, ResultUnsigned));
14144 Result.emplace_back(Args: APSInt(MinIndex, ResultUnsigned));
14145 for (unsigned I = 0; I != SourceLen - 2; ++I) {
14146 Result.emplace_back(Args: APSInt(APInt(ElemBitWidth, 0), ResultUnsigned));
14147 }
14148 return Success(V: APValue(Result.data(), Result.size()), E);
14149 }
14150
14151 case X86::BI__builtin_ia32_psraq128:
14152 case X86::BI__builtin_ia32_psraq256:
14153 case X86::BI__builtin_ia32_psraq512:
14154 case X86::BI__builtin_ia32_psrad128:
14155 case X86::BI__builtin_ia32_psrad256:
14156 case X86::BI__builtin_ia32_psrad512:
14157 case X86::BI__builtin_ia32_psraw128:
14158 case X86::BI__builtin_ia32_psraw256:
14159 case X86::BI__builtin_ia32_psraw512: {
14160 APValue R;
14161 if (!evalShiftWithCount(
14162 Info, Call: E, Out&: R,
14163 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.ashr(ShiftAmt: Count); },
14164 OverflowOp: [](const APInt &Elt, unsigned Width) {
14165 return Elt.ashr(ShiftAmt: Width - 1);
14166 }))
14167 return false;
14168 return Success(V: R, E);
14169 }
14170
14171 case X86::BI__builtin_ia32_psllq128:
14172 case X86::BI__builtin_ia32_psllq256:
14173 case X86::BI__builtin_ia32_psllq512:
14174 case X86::BI__builtin_ia32_pslld128:
14175 case X86::BI__builtin_ia32_pslld256:
14176 case X86::BI__builtin_ia32_pslld512:
14177 case X86::BI__builtin_ia32_psllw128:
14178 case X86::BI__builtin_ia32_psllw256:
14179 case X86::BI__builtin_ia32_psllw512: {
14180 APValue R;
14181 if (!evalShiftWithCount(
14182 Info, Call: E, Out&: R,
14183 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.shl(shiftAmt: Count); },
14184 OverflowOp: [](const APInt &Elt, unsigned Width) {
14185 return APInt::getZero(numBits: Width);
14186 }))
14187 return false;
14188 return Success(V: R, E);
14189 }
14190
14191 case X86::BI__builtin_ia32_psrlq128:
14192 case X86::BI__builtin_ia32_psrlq256:
14193 case X86::BI__builtin_ia32_psrlq512:
14194 case X86::BI__builtin_ia32_psrld128:
14195 case X86::BI__builtin_ia32_psrld256:
14196 case X86::BI__builtin_ia32_psrld512:
14197 case X86::BI__builtin_ia32_psrlw128:
14198 case X86::BI__builtin_ia32_psrlw256:
14199 case X86::BI__builtin_ia32_psrlw512: {
14200 APValue R;
14201 if (!evalShiftWithCount(
14202 Info, Call: E, Out&: R,
14203 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.lshr(shiftAmt: Count); },
14204 OverflowOp: [](const APInt &Elt, unsigned Width) {
14205 return APInt::getZero(numBits: Width);
14206 }))
14207 return false;
14208 return Success(V: R, E);
14209 }
14210
14211 case X86::BI__builtin_ia32_pternlogd128_mask:
14212 case X86::BI__builtin_ia32_pternlogd256_mask:
14213 case X86::BI__builtin_ia32_pternlogd512_mask:
14214 case X86::BI__builtin_ia32_pternlogq128_mask:
14215 case X86::BI__builtin_ia32_pternlogq256_mask:
14216 case X86::BI__builtin_ia32_pternlogq512_mask: {
14217 APValue AValue, BValue, CValue, ImmValue, UValue;
14218 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
14219 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
14220 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
14221 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
14222 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
14223 return false;
14224
14225 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14226 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14227 APInt Imm = ImmValue.getInt();
14228 APInt U = UValue.getInt();
14229 unsigned ResultLen = AValue.getVectorLength();
14230 SmallVector<APValue, 16> ResultElements;
14231 ResultElements.reserve(N: ResultLen);
14232
14233 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
14234 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
14235 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
14236 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
14237
14238 if (U[EltNum]) {
14239 unsigned BitWidth = ALane.getBitWidth();
14240 APInt ResLane(BitWidth, 0);
14241
14242 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
14243 unsigned ABit = ALane[Bit];
14244 unsigned BBit = BLane[Bit];
14245 unsigned CBit = CLane[Bit];
14246
14247 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
14248 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
14249 }
14250 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
14251 } else {
14252 ResultElements.push_back(Elt: APValue(APSInt(ALane, DestUnsigned)));
14253 }
14254 }
14255 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14256 }
14257 case X86::BI__builtin_ia32_pternlogd128_maskz:
14258 case X86::BI__builtin_ia32_pternlogd256_maskz:
14259 case X86::BI__builtin_ia32_pternlogd512_maskz:
14260 case X86::BI__builtin_ia32_pternlogq128_maskz:
14261 case X86::BI__builtin_ia32_pternlogq256_maskz:
14262 case X86::BI__builtin_ia32_pternlogq512_maskz: {
14263 APValue AValue, BValue, CValue, ImmValue, UValue;
14264 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
14265 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
14266 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
14267 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
14268 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
14269 return false;
14270
14271 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14272 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14273 APInt Imm = ImmValue.getInt();
14274 APInt U = UValue.getInt();
14275 unsigned ResultLen = AValue.getVectorLength();
14276 SmallVector<APValue, 16> ResultElements;
14277 ResultElements.reserve(N: ResultLen);
14278
14279 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
14280 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
14281 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
14282 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
14283
14284 unsigned BitWidth = ALane.getBitWidth();
14285 APInt ResLane(BitWidth, 0);
14286
14287 if (U[EltNum]) {
14288 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
14289 unsigned ABit = ALane[Bit];
14290 unsigned BBit = BLane[Bit];
14291 unsigned CBit = CLane[Bit];
14292
14293 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
14294 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
14295 }
14296 }
14297 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
14298 }
14299 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14300 }
14301
14302 case Builtin::BI__builtin_elementwise_clzg:
14303 case Builtin::BI__builtin_elementwise_ctzg: {
14304 APValue SourceLHS;
14305 std::optional<APValue> Fallback;
14306 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS))
14307 return false;
14308 if (E->getNumArgs() > 1) {
14309 APValue FallbackTmp;
14310 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: FallbackTmp))
14311 return false;
14312 Fallback = FallbackTmp;
14313 }
14314
14315 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14316 unsigned SourceLen = SourceLHS.getVectorLength();
14317 SmallVector<APValue, 4> ResultElements;
14318 ResultElements.reserve(N: SourceLen);
14319
14320 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14321 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
14322 if (!LHS) {
14323 // Without a fallback, a zero element is undefined
14324 if (!Fallback) {
14325 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
14326 << /*IsTrailing=*/(BuiltinOp ==
14327 Builtin::BI__builtin_elementwise_ctzg);
14328 return false;
14329 }
14330 ResultElements.push_back(Elt: Fallback->getVectorElt(I: EltNum));
14331 continue;
14332 }
14333 switch (BuiltinOp) {
14334 case Builtin::BI__builtin_elementwise_clzg:
14335 ResultElements.push_back(Elt: APValue(
14336 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countl_zero()),
14337 DestEltTy->isUnsignedIntegerOrEnumerationType())));
14338 break;
14339 case Builtin::BI__builtin_elementwise_ctzg:
14340 ResultElements.push_back(Elt: APValue(
14341 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countr_zero()),
14342 DestEltTy->isUnsignedIntegerOrEnumerationType())));
14343 break;
14344 }
14345 }
14346
14347 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14348 }
14349
14350 case Builtin::BI__builtin_elementwise_fma: {
14351 APValue SourceX, SourceY, SourceZ;
14352 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceX) ||
14353 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceY) ||
14354 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceZ))
14355 return false;
14356
14357 unsigned SourceLen = SourceX.getVectorLength();
14358 SmallVector<APValue> ResultElements;
14359 ResultElements.reserve(N: SourceLen);
14360 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14361 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14362 const APFloat &X = SourceX.getVectorElt(I: EltNum).getFloat();
14363 const APFloat &Y = SourceY.getVectorElt(I: EltNum).getFloat();
14364 const APFloat &Z = SourceZ.getVectorElt(I: EltNum).getFloat();
14365 APFloat Result(X);
14366 (void)Result.fusedMultiplyAdd(Multiplicand: Y, Addend: Z, RM);
14367 ResultElements.push_back(Elt: APValue(Result));
14368 }
14369 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14370 }
14371
14372 case clang::X86::BI__builtin_ia32_phaddw128:
14373 case clang::X86::BI__builtin_ia32_phaddw256:
14374 case clang::X86::BI__builtin_ia32_phaddd128:
14375 case clang::X86::BI__builtin_ia32_phaddd256:
14376 case clang::X86::BI__builtin_ia32_phaddsw128:
14377 case clang::X86::BI__builtin_ia32_phaddsw256:
14378
14379 case clang::X86::BI__builtin_ia32_phsubw128:
14380 case clang::X86::BI__builtin_ia32_phsubw256:
14381 case clang::X86::BI__builtin_ia32_phsubd128:
14382 case clang::X86::BI__builtin_ia32_phsubd256:
14383 case clang::X86::BI__builtin_ia32_phsubsw128:
14384 case clang::X86::BI__builtin_ia32_phsubsw256: {
14385 APValue SourceLHS, SourceRHS;
14386 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14387 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14388 return false;
14389 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14390 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14391
14392 unsigned NumElts = SourceLHS.getVectorLength();
14393 unsigned EltBits = Info.Ctx.getIntWidth(T: DestEltTy);
14394 unsigned EltsPerLane = 128 / EltBits;
14395 SmallVector<APValue, 4> ResultElements;
14396 ResultElements.reserve(N: NumElts);
14397
14398 for (unsigned LaneStart = 0; LaneStart != NumElts;
14399 LaneStart += EltsPerLane) {
14400 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14401 APSInt LHSA = SourceLHS.getVectorElt(I: LaneStart + I).getInt();
14402 APSInt LHSB = SourceLHS.getVectorElt(I: LaneStart + I + 1).getInt();
14403 switch (BuiltinOp) {
14404 case clang::X86::BI__builtin_ia32_phaddw128:
14405 case clang::X86::BI__builtin_ia32_phaddw256:
14406 case clang::X86::BI__builtin_ia32_phaddd128:
14407 case clang::X86::BI__builtin_ia32_phaddd256: {
14408 APSInt Res(LHSA + LHSB, DestUnsigned);
14409 ResultElements.push_back(Elt: APValue(Res));
14410 break;
14411 }
14412 case clang::X86::BI__builtin_ia32_phaddsw128:
14413 case clang::X86::BI__builtin_ia32_phaddsw256: {
14414 APSInt Res(LHSA.sadd_sat(RHS: LHSB));
14415 ResultElements.push_back(Elt: APValue(Res));
14416 break;
14417 }
14418 case clang::X86::BI__builtin_ia32_phsubw128:
14419 case clang::X86::BI__builtin_ia32_phsubw256:
14420 case clang::X86::BI__builtin_ia32_phsubd128:
14421 case clang::X86::BI__builtin_ia32_phsubd256: {
14422 APSInt Res(LHSA - LHSB, DestUnsigned);
14423 ResultElements.push_back(Elt: APValue(Res));
14424 break;
14425 }
14426 case clang::X86::BI__builtin_ia32_phsubsw128:
14427 case clang::X86::BI__builtin_ia32_phsubsw256: {
14428 APSInt Res(LHSA.ssub_sat(RHS: LHSB));
14429 ResultElements.push_back(Elt: APValue(Res));
14430 break;
14431 }
14432 }
14433 }
14434 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14435 APSInt RHSA = SourceRHS.getVectorElt(I: LaneStart + I).getInt();
14436 APSInt RHSB = SourceRHS.getVectorElt(I: LaneStart + I + 1).getInt();
14437 switch (BuiltinOp) {
14438 case clang::X86::BI__builtin_ia32_phaddw128:
14439 case clang::X86::BI__builtin_ia32_phaddw256:
14440 case clang::X86::BI__builtin_ia32_phaddd128:
14441 case clang::X86::BI__builtin_ia32_phaddd256: {
14442 APSInt Res(RHSA + RHSB, DestUnsigned);
14443 ResultElements.push_back(Elt: APValue(Res));
14444 break;
14445 }
14446 case clang::X86::BI__builtin_ia32_phaddsw128:
14447 case clang::X86::BI__builtin_ia32_phaddsw256: {
14448 APSInt Res(RHSA.sadd_sat(RHS: RHSB));
14449 ResultElements.push_back(Elt: APValue(Res));
14450 break;
14451 }
14452 case clang::X86::BI__builtin_ia32_phsubw128:
14453 case clang::X86::BI__builtin_ia32_phsubw256:
14454 case clang::X86::BI__builtin_ia32_phsubd128:
14455 case clang::X86::BI__builtin_ia32_phsubd256: {
14456 APSInt Res(RHSA - RHSB, DestUnsigned);
14457 ResultElements.push_back(Elt: APValue(Res));
14458 break;
14459 }
14460 case clang::X86::BI__builtin_ia32_phsubsw128:
14461 case clang::X86::BI__builtin_ia32_phsubsw256: {
14462 APSInt Res(RHSA.ssub_sat(RHS: RHSB));
14463 ResultElements.push_back(Elt: APValue(Res));
14464 break;
14465 }
14466 }
14467 }
14468 }
14469 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14470 }
14471 case clang::X86::BI__builtin_ia32_haddpd:
14472 case clang::X86::BI__builtin_ia32_haddps:
14473 case clang::X86::BI__builtin_ia32_haddps256:
14474 case clang::X86::BI__builtin_ia32_haddpd256:
14475 case clang::X86::BI__builtin_ia32_hsubpd:
14476 case clang::X86::BI__builtin_ia32_hsubps:
14477 case clang::X86::BI__builtin_ia32_hsubps256:
14478 case clang::X86::BI__builtin_ia32_hsubpd256: {
14479 APValue SourceLHS, SourceRHS;
14480 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14481 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14482 return false;
14483 unsigned NumElts = SourceLHS.getVectorLength();
14484 SmallVector<APValue, 4> ResultElements;
14485 ResultElements.reserve(N: NumElts);
14486 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14487 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14488 unsigned EltBits = Info.Ctx.getTypeSize(T: DestEltTy);
14489 unsigned NumLanes = NumElts * EltBits / 128;
14490 unsigned NumElemsPerLane = NumElts / NumLanes;
14491 unsigned HalfElemsPerLane = NumElemsPerLane / 2;
14492
14493 for (unsigned L = 0; L != NumElts; L += NumElemsPerLane) {
14494 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14495 APFloat LHSA = SourceLHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14496 APFloat LHSB = SourceLHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14497 switch (BuiltinOp) {
14498 case clang::X86::BI__builtin_ia32_haddpd:
14499 case clang::X86::BI__builtin_ia32_haddps:
14500 case clang::X86::BI__builtin_ia32_haddps256:
14501 case clang::X86::BI__builtin_ia32_haddpd256:
14502 LHSA.add(RHS: LHSB, RM);
14503 break;
14504 case clang::X86::BI__builtin_ia32_hsubpd:
14505 case clang::X86::BI__builtin_ia32_hsubps:
14506 case clang::X86::BI__builtin_ia32_hsubps256:
14507 case clang::X86::BI__builtin_ia32_hsubpd256:
14508 LHSA.subtract(RHS: LHSB, RM);
14509 break;
14510 }
14511 ResultElements.push_back(Elt: APValue(LHSA));
14512 }
14513 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14514 APFloat RHSA = SourceRHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14515 APFloat RHSB = SourceRHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14516 switch (BuiltinOp) {
14517 case clang::X86::BI__builtin_ia32_haddpd:
14518 case clang::X86::BI__builtin_ia32_haddps:
14519 case clang::X86::BI__builtin_ia32_haddps256:
14520 case clang::X86::BI__builtin_ia32_haddpd256:
14521 RHSA.add(RHS: RHSB, RM);
14522 break;
14523 case clang::X86::BI__builtin_ia32_hsubpd:
14524 case clang::X86::BI__builtin_ia32_hsubps:
14525 case clang::X86::BI__builtin_ia32_hsubps256:
14526 case clang::X86::BI__builtin_ia32_hsubpd256:
14527 RHSA.subtract(RHS: RHSB, RM);
14528 break;
14529 }
14530 ResultElements.push_back(Elt: APValue(RHSA));
14531 }
14532 }
14533 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14534 }
14535 case clang::X86::BI__builtin_ia32_addsubpd:
14536 case clang::X86::BI__builtin_ia32_addsubps:
14537 case clang::X86::BI__builtin_ia32_addsubpd256:
14538 case clang::X86::BI__builtin_ia32_addsubps256: {
14539 // Addsub: alternates between subtraction and addition
14540 // Result[i] = (i % 2 == 0) ? (a[i] - b[i]) : (a[i] + b[i])
14541 APValue SourceLHS, SourceRHS;
14542 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14543 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14544 return false;
14545 unsigned NumElems = SourceLHS.getVectorLength();
14546 SmallVector<APValue, 8> ResultElements;
14547 ResultElements.reserve(N: NumElems);
14548 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14549
14550 for (unsigned I = 0; I != NumElems; ++I) {
14551 APFloat LHS = SourceLHS.getVectorElt(I).getFloat();
14552 APFloat RHS = SourceRHS.getVectorElt(I).getFloat();
14553 if (I % 2 == 0) {
14554 // Even indices: subtract
14555 LHS.subtract(RHS, RM);
14556 } else {
14557 // Odd indices: add
14558 LHS.add(RHS, RM);
14559 }
14560 ResultElements.push_back(Elt: APValue(LHS));
14561 }
14562 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14563 }
14564 case clang::X86::BI__builtin_ia32_pclmulqdq128:
14565 case clang::X86::BI__builtin_ia32_pclmulqdq256:
14566 case clang::X86::BI__builtin_ia32_pclmulqdq512: {
14567 // PCLMULQDQ: carry-less multiplication of selected 64-bit halves
14568 // imm8 bit 0: selects lower (0) or upper (1) 64 bits of first operand
14569 // imm8 bit 4: selects lower (0) or upper (1) 64 bits of second operand
14570 APValue SourceLHS, SourceRHS;
14571 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14572 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14573 return false;
14574
14575 APSInt Imm8;
14576 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm8, Info))
14577 return false;
14578
14579 // Extract bits 0 and 4 from imm8
14580 bool SelectUpperA = (Imm8 & 0x01) != 0;
14581 bool SelectUpperB = (Imm8 & 0x10) != 0;
14582
14583 unsigned NumElems = SourceLHS.getVectorLength();
14584 SmallVector<APValue, 8> ResultElements;
14585 ResultElements.reserve(N: NumElems);
14586 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14587 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14588
14589 // Process each 128-bit lane
14590 for (unsigned Lane = 0; Lane < NumElems; Lane += 2) {
14591 // Get the two 64-bit halves of the first operand
14592 APSInt A0 = SourceLHS.getVectorElt(I: Lane + 0).getInt();
14593 APSInt A1 = SourceLHS.getVectorElt(I: Lane + 1).getInt();
14594 // Get the two 64-bit halves of the second operand
14595 APSInt B0 = SourceRHS.getVectorElt(I: Lane + 0).getInt();
14596 APSInt B1 = SourceRHS.getVectorElt(I: Lane + 1).getInt();
14597
14598 // Select the appropriate 64-bit values based on imm8
14599 APInt A = SelectUpperA ? A1 : A0;
14600 APInt B = SelectUpperB ? B1 : B0;
14601
14602 // Extend both operands to 128 bits for carry-less multiplication
14603 APInt A128 = A.zext(width: 128);
14604 APInt B128 = B.zext(width: 128);
14605
14606 // Use APIntOps::clmul for carry-less multiplication
14607 APInt Result = llvm::APIntOps::clmul(LHS: A128, RHS: B128);
14608
14609 // Split the 128-bit result into two 64-bit halves
14610 APSInt ResultLow(Result.extractBits(numBits: 64, bitPosition: 0), DestUnsigned);
14611 APSInt ResultHigh(Result.extractBits(numBits: 64, bitPosition: 64), DestUnsigned);
14612
14613 ResultElements.push_back(Elt: APValue(ResultLow));
14614 ResultElements.push_back(Elt: APValue(ResultHigh));
14615 }
14616
14617 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14618 }
14619 case Builtin::BI__builtin_elementwise_clmul:
14620 return EvaluateBinOpExpr(llvm::APIntOps::clmul);
14621 case Builtin::BI__builtin_elementwise_pext:
14622 return EvaluateBinOpExpr(llvm::APIntOps::pext);
14623 case Builtin::BI__builtin_elementwise_pdep:
14624 return EvaluateBinOpExpr(llvm::APIntOps::pdep);
14625 case Builtin::BI__builtin_elementwise_fshl:
14626 case Builtin::BI__builtin_elementwise_fshr: {
14627 APValue SourceHi, SourceLo, SourceShift;
14628 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
14629 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
14630 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceShift))
14631 return false;
14632
14633 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14634 if (!DestEltTy->isIntegerType())
14635 return false;
14636
14637 unsigned SourceLen = SourceHi.getVectorLength();
14638 SmallVector<APValue> ResultElements;
14639 ResultElements.reserve(N: SourceLen);
14640 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14641 const APSInt &Hi = SourceHi.getVectorElt(I: EltNum).getInt();
14642 const APSInt &Lo = SourceLo.getVectorElt(I: EltNum).getInt();
14643 const APSInt &Shift = SourceShift.getVectorElt(I: EltNum).getInt();
14644 switch (BuiltinOp) {
14645 case Builtin::BI__builtin_elementwise_fshl:
14646 ResultElements.push_back(Elt: APValue(
14647 APSInt(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned())));
14648 break;
14649 case Builtin::BI__builtin_elementwise_fshr:
14650 ResultElements.push_back(Elt: APValue(
14651 APSInt(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned())));
14652 break;
14653 }
14654 }
14655
14656 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14657 }
14658
14659 case X86::BI__builtin_ia32_shuf_f32x4_256:
14660 case X86::BI__builtin_ia32_shuf_i32x4_256:
14661 case X86::BI__builtin_ia32_shuf_f64x2_256:
14662 case X86::BI__builtin_ia32_shuf_i64x2_256:
14663 case X86::BI__builtin_ia32_shuf_f32x4:
14664 case X86::BI__builtin_ia32_shuf_i32x4:
14665 case X86::BI__builtin_ia32_shuf_f64x2:
14666 case X86::BI__builtin_ia32_shuf_i64x2: {
14667 APValue SourceA, SourceB;
14668 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
14669 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB))
14670 return false;
14671
14672 APSInt Imm;
14673 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14674 return false;
14675
14676 // Destination and sources A, B all have the same type.
14677 unsigned NumElems = SourceA.getVectorLength();
14678 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14679 QualType ElemQT = VT->getElementType();
14680 unsigned ElemBits = Info.Ctx.getTypeSize(T: ElemQT);
14681 unsigned LaneBits = 128u;
14682 unsigned NumLanes = (NumElems * ElemBits) / LaneBits;
14683 unsigned NumElemsPerLane = LaneBits / ElemBits;
14684
14685 unsigned DstLen = SourceA.getVectorLength();
14686 SmallVector<APValue, 16> ResultElements;
14687 ResultElements.reserve(N: DstLen);
14688
14689 APValue R;
14690 if (!evalShuffleGeneric(
14691 Info, Call: E, Out&: R,
14692 GetSourceIndex: [NumLanes, NumElemsPerLane](unsigned DstIdx, unsigned ShuffleMask)
14693 -> std::pair<unsigned, int> {
14694 // DstIdx determines source. ShuffleMask selects lane in source.
14695 unsigned BitsPerElem = NumLanes / 2;
14696 unsigned IndexMask = (1u << BitsPerElem) - 1;
14697 unsigned Lane = DstIdx / NumElemsPerLane;
14698 unsigned SrcIdx = (Lane < NumLanes / 2) ? 0 : 1;
14699 unsigned BitIdx = BitsPerElem * Lane;
14700 unsigned SrcLaneIdx = (ShuffleMask >> BitIdx) & IndexMask;
14701 unsigned ElemInLane = DstIdx % NumElemsPerLane;
14702 unsigned IdxToPick = SrcLaneIdx * NumElemsPerLane + ElemInLane;
14703 return {SrcIdx, IdxToPick};
14704 }))
14705 return false;
14706 return Success(V: R, E);
14707 }
14708
14709 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14710 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14711 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi:
14712 case X86::BI__builtin_ia32_vgf2p8affineqb_v16qi:
14713 case X86::BI__builtin_ia32_vgf2p8affineqb_v32qi:
14714 case X86::BI__builtin_ia32_vgf2p8affineqb_v64qi: {
14715
14716 APValue X, A;
14717 APSInt Imm;
14718 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: X) ||
14719 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: A) ||
14720 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14721 return false;
14722
14723 assert(X.isVector() && A.isVector());
14724 assert(X.getVectorLength() == A.getVectorLength());
14725
14726 bool IsInverse = false;
14727 switch (BuiltinOp) {
14728 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14729 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14730 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi: {
14731 IsInverse = true;
14732 }
14733 }
14734
14735 unsigned NumBitsInByte = 8;
14736 unsigned NumBytesInQWord = 8;
14737 unsigned NumBitsInQWord = 64;
14738 unsigned NumBytes = A.getVectorLength();
14739 unsigned NumQWords = NumBytes / NumBytesInQWord;
14740 SmallVector<APValue, 64> Result;
14741 Result.reserve(N: NumBytes);
14742
14743 // computing A*X + Imm
14744 for (unsigned QWordIdx = 0; QWordIdx != NumQWords; ++QWordIdx) {
14745 // Extract the QWords from X, A
14746 APInt XQWord(NumBitsInQWord, 0);
14747 APInt AQWord(NumBitsInQWord, 0);
14748 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14749 unsigned Idx = QWordIdx * NumBytesInQWord + ByteIdx;
14750 APInt XByte = X.getVectorElt(I: Idx).getInt();
14751 APInt AByte = A.getVectorElt(I: Idx).getInt();
14752 XQWord.insertBits(SubBits: XByte, bitPosition: ByteIdx * NumBitsInByte);
14753 AQWord.insertBits(SubBits: AByte, bitPosition: ByteIdx * NumBitsInByte);
14754 }
14755
14756 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14757 uint8_t XByte =
14758 XQWord.lshr(shiftAmt: ByteIdx * NumBitsInByte).getLoBits(numBits: 8).getZExtValue();
14759 Result.push_back(Elt: APValue(APSInt(
14760 APInt(8, GFNIAffine(XByte, AQword: AQWord, Imm, Inverse: IsInverse)), false)));
14761 }
14762 }
14763
14764 return Success(V: APValue(Result.data(), Result.size()), E);
14765 }
14766
14767 case X86::BI__builtin_ia32_vgf2p8mulb_v16qi:
14768 case X86::BI__builtin_ia32_vgf2p8mulb_v32qi:
14769 case X86::BI__builtin_ia32_vgf2p8mulb_v64qi: {
14770 APValue A, B;
14771 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
14772 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
14773 return false;
14774
14775 assert(A.isVector() && B.isVector());
14776 assert(A.getVectorLength() == B.getVectorLength());
14777
14778 unsigned NumBytes = A.getVectorLength();
14779 SmallVector<APValue, 64> Result;
14780 Result.reserve(N: NumBytes);
14781
14782 for (unsigned ByteIdx = 0; ByteIdx != NumBytes; ++ByteIdx) {
14783 uint8_t AByte = A.getVectorElt(I: ByteIdx).getInt().getZExtValue();
14784 uint8_t BByte = B.getVectorElt(I: ByteIdx).getInt().getZExtValue();
14785 Result.push_back(Elt: APValue(
14786 APSInt(APInt(8, GFNIMul(AByte, BByte)), /*IsUnsigned=*/false)));
14787 }
14788
14789 return Success(V: APValue(Result.data(), Result.size()), E);
14790 }
14791
14792 case X86::BI__builtin_ia32_insertf32x4_256:
14793 case X86::BI__builtin_ia32_inserti32x4_256:
14794 case X86::BI__builtin_ia32_insertf64x2_256:
14795 case X86::BI__builtin_ia32_inserti64x2_256:
14796 case X86::BI__builtin_ia32_insertf32x4:
14797 case X86::BI__builtin_ia32_inserti32x4:
14798 case X86::BI__builtin_ia32_insertf64x2_512:
14799 case X86::BI__builtin_ia32_inserti64x2_512:
14800 case X86::BI__builtin_ia32_insertf32x8:
14801 case X86::BI__builtin_ia32_inserti32x8:
14802 case X86::BI__builtin_ia32_insertf64x4:
14803 case X86::BI__builtin_ia32_inserti64x4:
14804 case X86::BI__builtin_ia32_vinsertf128_ps256:
14805 case X86::BI__builtin_ia32_vinsertf128_pd256:
14806 case X86::BI__builtin_ia32_vinsertf128_si256:
14807 case X86::BI__builtin_ia32_insert128i256: {
14808 APValue SourceDst, SourceSub;
14809 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceDst) ||
14810 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceSub))
14811 return false;
14812
14813 APSInt Imm;
14814 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14815 return false;
14816
14817 assert(SourceDst.isVector() && SourceSub.isVector());
14818 unsigned DstLen = SourceDst.getVectorLength();
14819 unsigned SubLen = SourceSub.getVectorLength();
14820 assert(SubLen != 0 && DstLen != 0 && (DstLen % SubLen) == 0);
14821 unsigned NumLanes = DstLen / SubLen;
14822 unsigned LaneIdx = (Imm.getZExtValue() % NumLanes) * SubLen;
14823
14824 SmallVector<APValue, 16> ResultElements;
14825 ResultElements.reserve(N: DstLen);
14826
14827 for (unsigned EltNum = 0; EltNum < DstLen; ++EltNum) {
14828 if (EltNum >= LaneIdx && EltNum < LaneIdx + SubLen)
14829 ResultElements.push_back(Elt: SourceSub.getVectorElt(I: EltNum - LaneIdx));
14830 else
14831 ResultElements.push_back(Elt: SourceDst.getVectorElt(I: EltNum));
14832 }
14833
14834 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14835 }
14836
14837 case clang::X86::BI__builtin_ia32_vec_set_v4hi:
14838 case clang::X86::BI__builtin_ia32_vec_set_v16qi:
14839 case clang::X86::BI__builtin_ia32_vec_set_v8hi:
14840 case clang::X86::BI__builtin_ia32_vec_set_v4si:
14841 case clang::X86::BI__builtin_ia32_vec_set_v2di:
14842 case clang::X86::BI__builtin_ia32_vec_set_v32qi:
14843 case clang::X86::BI__builtin_ia32_vec_set_v16hi:
14844 case clang::X86::BI__builtin_ia32_vec_set_v8si:
14845 case clang::X86::BI__builtin_ia32_vec_set_v4di: {
14846 APValue VecVal;
14847 APSInt Scalar, IndexAPS;
14848 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: VecVal, Info) ||
14849 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Scalar, Info) ||
14850 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: IndexAPS, Info))
14851 return false;
14852
14853 QualType ElemTy = E->getType()->castAs<VectorType>()->getElementType();
14854 unsigned ElemWidth = Info.Ctx.getIntWidth(T: ElemTy);
14855 bool ElemUnsigned = ElemTy->isUnsignedIntegerOrEnumerationType();
14856 Scalar.setIsUnsigned(ElemUnsigned);
14857 APSInt ElemAPS = Scalar.extOrTrunc(width: ElemWidth);
14858 APValue ElemAV(ElemAPS);
14859
14860 unsigned NumElems = VecVal.getVectorLength();
14861 unsigned Index =
14862 static_cast<unsigned>(IndexAPS.getZExtValue() & (NumElems - 1));
14863
14864 SmallVector<APValue, 4> Elems;
14865 Elems.reserve(N: NumElems);
14866 for (unsigned ElemNum = 0; ElemNum != NumElems; ++ElemNum)
14867 Elems.push_back(Elt: ElemNum == Index ? ElemAV : VecVal.getVectorElt(I: ElemNum));
14868
14869 return Success(V: APValue(Elems.data(), NumElems), E);
14870 }
14871
14872 case X86::BI__builtin_ia32_pslldqi128_byteshift:
14873 case X86::BI__builtin_ia32_pslldqi256_byteshift:
14874 case X86::BI__builtin_ia32_pslldqi512_byteshift: {
14875 APValue R;
14876 if (!evalShuffleGeneric(
14877 Info, Call: E, Out&: R,
14878 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
14879 unsigned LaneBase = (DstIdx / 16) * 16;
14880 unsigned LaneIdx = DstIdx % 16;
14881 if (LaneIdx < Shift)
14882 return std::make_pair(x: 0, y: -1);
14883
14884 return std::make_pair(
14885 x: 0, y: static_cast<int>(LaneBase + LaneIdx - Shift));
14886 }))
14887 return false;
14888 return Success(V: R, E);
14889 }
14890
14891 case X86::BI__builtin_ia32_psrldqi128_byteshift:
14892 case X86::BI__builtin_ia32_psrldqi256_byteshift:
14893 case X86::BI__builtin_ia32_psrldqi512_byteshift: {
14894 APValue R;
14895 if (!evalShuffleGeneric(
14896 Info, Call: E, Out&: R,
14897 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
14898 unsigned LaneBase = (DstIdx / 16) * 16;
14899 unsigned LaneIdx = DstIdx % 16;
14900 if (LaneIdx + Shift < 16)
14901 return std::make_pair(
14902 x: 0, y: static_cast<int>(LaneBase + LaneIdx + Shift));
14903
14904 return std::make_pair(x: 0, y: -1);
14905 }))
14906 return false;
14907 return Success(V: R, E);
14908 }
14909
14910 case X86::BI__builtin_ia32_palignr128:
14911 case X86::BI__builtin_ia32_palignr256:
14912 case X86::BI__builtin_ia32_palignr512: {
14913 APValue R;
14914 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Shift) {
14915 // Default to -1 → zero-fill this destination element
14916 unsigned VecIdx = 1;
14917 int ElemIdx = -1;
14918
14919 int Lane = DstIdx / 16;
14920 int Offset = DstIdx % 16;
14921
14922 // Elements come from VecB first, then VecA after the shift boundary
14923 unsigned ShiftedIdx = Offset + (Shift & 0xFF);
14924 if (ShiftedIdx < 16) { // from VecB
14925 ElemIdx = ShiftedIdx + (Lane * 16);
14926 } else if (ShiftedIdx < 32) { // from VecA
14927 VecIdx = 0;
14928 ElemIdx = (ShiftedIdx - 16) + (Lane * 16);
14929 }
14930
14931 return std::pair<unsigned, int>{VecIdx, ElemIdx};
14932 }))
14933 return false;
14934 return Success(V: R, E);
14935 }
14936 case X86::BI__builtin_ia32_alignd128:
14937 case X86::BI__builtin_ia32_alignd256:
14938 case X86::BI__builtin_ia32_alignd512:
14939 case X86::BI__builtin_ia32_alignq128:
14940 case X86::BI__builtin_ia32_alignq256:
14941 case X86::BI__builtin_ia32_alignq512: {
14942 APValue R;
14943 unsigned NumElems = E->getType()->castAs<VectorType>()->getNumElements();
14944 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14945 GetSourceIndex: [NumElems](unsigned DstIdx, unsigned Shift) {
14946 unsigned Imm = Shift & 0xFF;
14947 unsigned EffectiveShift = Imm & (NumElems - 1);
14948 unsigned SourcePos = DstIdx + EffectiveShift;
14949 unsigned VecIdx = SourcePos < NumElems ? 1 : 0;
14950 unsigned ElemIdx = SourcePos & (NumElems - 1);
14951
14952 return std::pair<unsigned, int>{
14953 VecIdx, static_cast<int>(ElemIdx)};
14954 }))
14955 return false;
14956 return Success(V: R, E);
14957 }
14958 case X86::BI__builtin_ia32_permvarsi256:
14959 case X86::BI__builtin_ia32_permvarsf256:
14960 case X86::BI__builtin_ia32_permvardf512:
14961 case X86::BI__builtin_ia32_permvardi512:
14962 case X86::BI__builtin_ia32_permvarhi128: {
14963 APValue R;
14964 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14965 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14966 int Offset = ShuffleMask & 0x7;
14967 return std::pair<unsigned, int>{0, Offset};
14968 }))
14969 return false;
14970 return Success(V: R, E);
14971 }
14972 case X86::BI__builtin_ia32_permvarqi128:
14973 case X86::BI__builtin_ia32_permvarhi256:
14974 case X86::BI__builtin_ia32_permvarsi512:
14975 case X86::BI__builtin_ia32_permvarsf512: {
14976 APValue R;
14977 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14978 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14979 int Offset = ShuffleMask & 0xF;
14980 return std::pair<unsigned, int>{0, Offset};
14981 }))
14982 return false;
14983 return Success(V: R, E);
14984 }
14985 case X86::BI__builtin_ia32_permvardi256:
14986 case X86::BI__builtin_ia32_permvardf256: {
14987 APValue R;
14988 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14989 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14990 int Offset = ShuffleMask & 0x3;
14991 return std::pair<unsigned, int>{0, Offset};
14992 }))
14993 return false;
14994 return Success(V: R, E);
14995 }
14996 case X86::BI__builtin_ia32_permvarqi256:
14997 case X86::BI__builtin_ia32_permvarhi512: {
14998 APValue R;
14999 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15000 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15001 int Offset = ShuffleMask & 0x1F;
15002 return std::pair<unsigned, int>{0, Offset};
15003 }))
15004 return false;
15005 return Success(V: R, E);
15006 }
15007 case X86::BI__builtin_ia32_permvarqi512: {
15008 APValue R;
15009 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15010 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15011 int Offset = ShuffleMask & 0x3F;
15012 return std::pair<unsigned, int>{0, Offset};
15013 }))
15014 return false;
15015 return Success(V: R, E);
15016 }
15017 case X86::BI__builtin_ia32_vpermi2varq128:
15018 case X86::BI__builtin_ia32_vpermi2varpd128: {
15019 APValue R;
15020 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15021 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15022 int Offset = ShuffleMask & 0x1;
15023 unsigned SrcIdx = (ShuffleMask >> 1) & 0x1;
15024 return std::pair<unsigned, int>{SrcIdx, Offset};
15025 }))
15026 return false;
15027 return Success(V: R, E);
15028 }
15029 case X86::BI__builtin_ia32_vpermi2vard128:
15030 case X86::BI__builtin_ia32_vpermi2varps128:
15031 case X86::BI__builtin_ia32_vpermi2varq256:
15032 case X86::BI__builtin_ia32_vpermi2varpd256: {
15033 APValue R;
15034 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15035 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15036 int Offset = ShuffleMask & 0x3;
15037 unsigned SrcIdx = (ShuffleMask >> 2) & 0x1;
15038 return std::pair<unsigned, int>{SrcIdx, Offset};
15039 }))
15040 return false;
15041 return Success(V: R, E);
15042 }
15043 case X86::BI__builtin_ia32_vpermi2varhi128:
15044 case X86::BI__builtin_ia32_vpermi2vard256:
15045 case X86::BI__builtin_ia32_vpermi2varps256:
15046 case X86::BI__builtin_ia32_vpermi2varq512:
15047 case X86::BI__builtin_ia32_vpermi2varpd512: {
15048 APValue R;
15049 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15050 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15051 int Offset = ShuffleMask & 0x7;
15052 unsigned SrcIdx = (ShuffleMask >> 3) & 0x1;
15053 return std::pair<unsigned, int>{SrcIdx, Offset};
15054 }))
15055 return false;
15056 return Success(V: R, E);
15057 }
15058 case X86::BI__builtin_ia32_vpermi2varqi128:
15059 case X86::BI__builtin_ia32_vpermi2varhi256:
15060 case X86::BI__builtin_ia32_vpermi2vard512:
15061 case X86::BI__builtin_ia32_vpermi2varps512: {
15062 APValue R;
15063 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15064 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15065 int Offset = ShuffleMask & 0xF;
15066 unsigned SrcIdx = (ShuffleMask >> 4) & 0x1;
15067 return std::pair<unsigned, int>{SrcIdx, Offset};
15068 }))
15069 return false;
15070 return Success(V: R, E);
15071 }
15072 case X86::BI__builtin_ia32_vpermi2varqi256:
15073 case X86::BI__builtin_ia32_vpermi2varhi512: {
15074 APValue R;
15075 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15076 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15077 int Offset = ShuffleMask & 0x1F;
15078 unsigned SrcIdx = (ShuffleMask >> 5) & 0x1;
15079 return std::pair<unsigned, int>{SrcIdx, Offset};
15080 }))
15081 return false;
15082 return Success(V: R, E);
15083 }
15084 case X86::BI__builtin_ia32_vpermi2varqi512: {
15085 APValue R;
15086 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15087 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15088 int Offset = ShuffleMask & 0x3F;
15089 unsigned SrcIdx = (ShuffleMask >> 6) & 0x1;
15090 return std::pair<unsigned, int>{SrcIdx, Offset};
15091 }))
15092 return false;
15093 return Success(V: R, E);
15094 }
15095
15096 case clang::X86::BI__builtin_ia32_minps:
15097 case clang::X86::BI__builtin_ia32_minpd:
15098 case clang::X86::BI__builtin_ia32_minps256:
15099 case clang::X86::BI__builtin_ia32_minpd256:
15100 case clang::X86::BI__builtin_ia32_minps512:
15101 case clang::X86::BI__builtin_ia32_minpd512:
15102 case clang::X86::BI__builtin_ia32_minph128:
15103 case clang::X86::BI__builtin_ia32_minph256:
15104 case clang::X86::BI__builtin_ia32_minph512:
15105 return EvaluateFpBinOpExpr(
15106 [](const APFloat &A, const APFloat &B,
15107 std::optional<APSInt>) -> std::optional<APFloat> {
15108 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
15109 B.isInfinity() || B.isDenormal())
15110 return std::nullopt;
15111 if (A.isZero() && B.isZero())
15112 return B;
15113 return llvm::minimum(A, B);
15114 });
15115
15116 case clang::X86::BI__builtin_ia32_minss:
15117 case clang::X86::BI__builtin_ia32_minsd:
15118 return EvaluateFpBinOpExpr(
15119 [](const APFloat &A, const APFloat &B,
15120 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15121 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/true);
15122 },
15123 /*IsScalar=*/true);
15124
15125 case clang::X86::BI__builtin_ia32_minsd_round_mask:
15126 case clang::X86::BI__builtin_ia32_minss_round_mask:
15127 case clang::X86::BI__builtin_ia32_minsh_round_mask:
15128 case clang::X86::BI__builtin_ia32_maxsd_round_mask:
15129 case clang::X86::BI__builtin_ia32_maxss_round_mask:
15130 case clang::X86::BI__builtin_ia32_maxsh_round_mask: {
15131 bool IsMin = BuiltinOp == clang::X86::BI__builtin_ia32_minsd_round_mask ||
15132 BuiltinOp == clang::X86::BI__builtin_ia32_minss_round_mask ||
15133 BuiltinOp == clang::X86::BI__builtin_ia32_minsh_round_mask;
15134 return EvaluateScalarFpRoundMaskBinOp(
15135 [IsMin](const APFloat &A, const APFloat &B,
15136 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15137 return EvalScalarMinMaxFp(A, B, RoundingMode, IsMin);
15138 });
15139 }
15140
15141 case clang::X86::BI__builtin_ia32_maxps:
15142 case clang::X86::BI__builtin_ia32_maxpd:
15143 case clang::X86::BI__builtin_ia32_maxps256:
15144 case clang::X86::BI__builtin_ia32_maxpd256:
15145 case clang::X86::BI__builtin_ia32_maxps512:
15146 case clang::X86::BI__builtin_ia32_maxpd512:
15147 case clang::X86::BI__builtin_ia32_maxph128:
15148 case clang::X86::BI__builtin_ia32_maxph256:
15149 case clang::X86::BI__builtin_ia32_maxph512:
15150 return EvaluateFpBinOpExpr(
15151 [](const APFloat &A, const APFloat &B,
15152 std::optional<APSInt>) -> std::optional<APFloat> {
15153 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
15154 B.isInfinity() || B.isDenormal())
15155 return std::nullopt;
15156 if (A.isZero() && B.isZero())
15157 return B;
15158 return llvm::maximum(A, B);
15159 });
15160
15161 case clang::X86::BI__builtin_ia32_maxss:
15162 case clang::X86::BI__builtin_ia32_maxsd:
15163 return EvaluateFpBinOpExpr(
15164 [](const APFloat &A, const APFloat &B,
15165 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15166 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/false);
15167 },
15168 /*IsScalar=*/true);
15169
15170 case clang::X86::BI__builtin_ia32_vcvtps2ph:
15171 case clang::X86::BI__builtin_ia32_vcvtps2ph256: {
15172 APValue SrcVec;
15173 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SrcVec))
15174 return false;
15175
15176 APSInt Imm;
15177 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info))
15178 return false;
15179
15180 const auto *SrcVTy = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
15181 unsigned SrcNumElems = SrcVTy->getNumElements();
15182 const auto *DstVTy = E->getType()->castAs<VectorType>();
15183 unsigned DstNumElems = DstVTy->getNumElements();
15184 QualType DstElemTy = DstVTy->getElementType();
15185
15186 const llvm::fltSemantics &HalfSem =
15187 Info.Ctx.getFloatTypeSemantics(T: Info.Ctx.HalfTy);
15188
15189 int ImmVal = Imm.getZExtValue();
15190 bool UseMXCSR = (ImmVal & 4) != 0;
15191 bool IsFPConstrained =
15192 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained();
15193
15194 llvm::RoundingMode RM;
15195 if (!UseMXCSR) {
15196 switch (ImmVal & 3) {
15197 case 0:
15198 RM = llvm::RoundingMode::NearestTiesToEven;
15199 break;
15200 case 1:
15201 RM = llvm::RoundingMode::TowardNegative;
15202 break;
15203 case 2:
15204 RM = llvm::RoundingMode::TowardPositive;
15205 break;
15206 case 3:
15207 RM = llvm::RoundingMode::TowardZero;
15208 break;
15209 default:
15210 llvm_unreachable("Invalid immediate rounding mode");
15211 }
15212 } else {
15213 RM = llvm::RoundingMode::NearestTiesToEven;
15214 }
15215
15216 SmallVector<APValue, 8> ResultElements;
15217 ResultElements.reserve(N: DstNumElems);
15218
15219 for (unsigned I = 0; I < SrcNumElems; ++I) {
15220 APFloat SrcVal = SrcVec.getVectorElt(I).getFloat();
15221
15222 bool LostInfo;
15223 APFloat::opStatus St = SrcVal.convert(ToSemantics: HalfSem, RM, losesInfo: &LostInfo);
15224
15225 if (UseMXCSR && IsFPConstrained && St != APFloat::opOK) {
15226 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
15227 return false;
15228 }
15229
15230 APSInt DstInt(SrcVal.bitcastToAPInt(),
15231 DstElemTy->isUnsignedIntegerOrEnumerationType());
15232 ResultElements.push_back(Elt: APValue(DstInt));
15233 }
15234
15235 if (DstNumElems > SrcNumElems) {
15236 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: DstElemTy);
15237 for (unsigned I = SrcNumElems; I < DstNumElems; ++I) {
15238 ResultElements.push_back(Elt: APValue(Zero));
15239 }
15240 }
15241
15242 return Success(V: ResultElements, E);
15243 }
15244 case X86::BI__builtin_ia32_vperm2f128_pd256:
15245 case X86::BI__builtin_ia32_vperm2f128_ps256:
15246 case X86::BI__builtin_ia32_vperm2f128_si256:
15247 case X86::BI__builtin_ia32_permti256: {
15248 unsigned NumElements =
15249 E->getArg(Arg: 0)->getType()->getAs<VectorType>()->getNumElements();
15250 unsigned PreservedBitsCnt = NumElements >> 2;
15251 APValue R;
15252 if (!evalShuffleGeneric(
15253 Info, Call: E, Out&: R,
15254 GetSourceIndex: [PreservedBitsCnt](unsigned DstIdx, unsigned ShuffleMask) {
15255 unsigned ControlBitsCnt = DstIdx >> PreservedBitsCnt << 2;
15256 unsigned ControlBits = ShuffleMask >> ControlBitsCnt;
15257
15258 if (ControlBits & 0b1000)
15259 return std::make_pair(x: 0u, y: -1);
15260
15261 unsigned SrcVecIdx = (ControlBits & 0b10) >> 1;
15262 unsigned PreservedBitsMask = (1 << PreservedBitsCnt) - 1;
15263 int SrcIdx = ((ControlBits & 0b1) << PreservedBitsCnt) |
15264 (DstIdx & PreservedBitsMask);
15265 return std::make_pair(x&: SrcVecIdx, y&: SrcIdx);
15266 }))
15267 return false;
15268 return Success(V: R, E);
15269 }
15270 case X86::BI__builtin_ia32_vpdpwssd128:
15271 case X86::BI__builtin_ia32_vpdpwssd256:
15272 case X86::BI__builtin_ia32_vpdpwssd512:
15273 case X86::BI__builtin_ia32_vpdpbusd128:
15274 case X86::BI__builtin_ia32_vpdpbusd256:
15275 case X86::BI__builtin_ia32_vpdpbusd512:
15276 return EvalVectorDotProduct(false);
15277 case X86::BI__builtin_ia32_vpdpwssds128:
15278 case X86::BI__builtin_ia32_vpdpwssds256:
15279 case X86::BI__builtin_ia32_vpdpwssds512:
15280 case X86::BI__builtin_ia32_vpdpbusds128:
15281 case X86::BI__builtin_ia32_vpdpbusds256:
15282 case X86::BI__builtin_ia32_vpdpbusds512:
15283 return EvalVectorDotProduct(true);
15284 case X86::BI__builtin_ia32_cvtpd2dq:
15285 case X86::BI__builtin_ia32_cvtps2dq:
15286 case X86::BI__builtin_ia32_cvttpd2dq:
15287 case X86::BI__builtin_ia32_cvttps2dq:
15288 case X86::BI__builtin_ia32_cvtpd2dq256:
15289 case X86::BI__builtin_ia32_cvtps2dq256:
15290 case X86::BI__builtin_ia32_cvttpd2dq256:
15291 case X86::BI__builtin_ia32_cvttps2dq256: {
15292 APValue SrcVec;
15293 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SrcVec) || !SrcVec.isVector())
15294 return false;
15295
15296 const auto *VT = E->getType()->castAs<VectorType>();
15297 QualType EltTy = VT->getElementType();
15298 bool isUnsigned = EltTy->isUnsignedIntegerType();
15299 unsigned BitWidth = Info.Ctx.getIntWidth(T: EltTy);
15300
15301 unsigned NumSrcElems = SrcVec.getVectorLength();
15302 unsigned NumDstElems = VT->getNumElements();
15303
15304 SmallVector<APValue, 8> ResultElts;
15305 for (unsigned i = 0; i != NumDstElems; ++i) {
15306 if (i < NumSrcElems) {
15307 llvm::APFloat FloatElem = SrcVec.getVectorElt(I: i).getFloat();
15308 llvm::APSInt IntResult(BitWidth, isUnsigned);
15309 bool IsExact = false;
15310 // We only allow exact conversions so rounding mode does not matter for
15311 // cvt* and cvtt* builtins
15312 FloatElem.convertToInteger(Result&: IntResult, RM: llvm::APFloat::rmTowardZero,
15313 IsExact: &IsExact);
15314 if (!IsExact)
15315 return false;
15316 ResultElts.push_back(Elt: APValue(IntResult));
15317 } else
15318 // Pad remaining lanes with zero
15319 ResultElts.push_back(Elt: APValue(llvm::APSInt(BitWidth, isUnsigned)));
15320 }
15321 return Success(V: ResultElts, E);
15322 }
15323 }
15324}
15325
15326bool VectorExprEvaluator::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
15327 APValue Source;
15328 QualType SourceVecType = E->getSrcExpr()->getType();
15329 if (!EvaluateAsRValue(Info, E: E->getSrcExpr(), Result&: Source))
15330 return false;
15331
15332 QualType DestTy = E->getType()->castAs<VectorType>()->getElementType();
15333 QualType SourceTy = SourceVecType->castAs<VectorType>()->getElementType();
15334
15335 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15336
15337 auto SourceLen = Source.getVectorLength();
15338 SmallVector<APValue, 4> ResultElements;
15339 ResultElements.reserve(N: SourceLen);
15340 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
15341 APValue Elt;
15342 if (!handleVectorElementCast(Info, FPO, E, SourceTy, DestTy,
15343 Original: Source.getVectorElt(I: EltNum), Result&: Elt))
15344 return false;
15345 ResultElements.push_back(Elt: std::move(Elt));
15346 }
15347
15348 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15349}
15350
15351static bool handleVectorShuffle(EvalInfo &Info, const ShuffleVectorExpr *E,
15352 QualType ElemType, APValue const &VecVal1,
15353 APValue const &VecVal2, unsigned EltNum,
15354 APValue &Result) {
15355 unsigned const TotalElementsInInputVector1 = VecVal1.getVectorLength();
15356 unsigned const TotalElementsInInputVector2 = VecVal2.getVectorLength();
15357
15358 APSInt IndexVal = E->getShuffleMaskIdx(N: EltNum);
15359 int64_t index = IndexVal.getExtValue();
15360 // The spec says that -1 should be treated as undef for optimizations,
15361 // but in constexpr we'd have to produce an APValue::Indeterminate,
15362 // which is prohibited from being a top-level constant value. Emit a
15363 // diagnostic instead.
15364 if (index == -1) {
15365 Info.FFDiag(
15366 E, DiagId: diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
15367 << EltNum;
15368 return false;
15369 }
15370
15371 if (index < 0 ||
15372 index >= TotalElementsInInputVector1 + TotalElementsInInputVector2)
15373 llvm_unreachable("Out of bounds shuffle index");
15374
15375 if (index >= TotalElementsInInputVector1)
15376 Result = VecVal2.getVectorElt(I: index - TotalElementsInInputVector1);
15377 else
15378 Result = VecVal1.getVectorElt(I: index);
15379 return true;
15380}
15381
15382bool VectorExprEvaluator::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
15383 // FIXME: Unary shuffle with mask not currently supported.
15384 if (E->getNumSubExprs() == 2)
15385 return Error(E);
15386 APValue VecVal1;
15387 const Expr *Vec1 = E->getExpr(Index: 0);
15388 if (!EvaluateAsRValue(Info, E: Vec1, Result&: VecVal1))
15389 return false;
15390 APValue VecVal2;
15391 const Expr *Vec2 = E->getExpr(Index: 1);
15392 if (!EvaluateAsRValue(Info, E: Vec2, Result&: VecVal2))
15393 return false;
15394
15395 VectorType const *DestVecTy = E->getType()->castAs<VectorType>();
15396 QualType DestElTy = DestVecTy->getElementType();
15397
15398 auto TotalElementsInOutputVector = DestVecTy->getNumElements();
15399
15400 SmallVector<APValue, 4> ResultElements;
15401 ResultElements.reserve(N: TotalElementsInOutputVector);
15402 for (unsigned EltNum = 0; EltNum < TotalElementsInOutputVector; ++EltNum) {
15403 APValue Elt;
15404 if (!handleVectorShuffle(Info, E, ElemType: DestElTy, VecVal1, VecVal2, EltNum, Result&: Elt))
15405 return false;
15406 ResultElements.push_back(Elt: std::move(Elt));
15407 }
15408
15409 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15410}
15411
15412//===----------------------------------------------------------------------===//
15413// Matrix Evaluation
15414//===----------------------------------------------------------------------===//
15415
15416namespace {
15417class MatrixExprEvaluator : public ExprEvaluatorBase<MatrixExprEvaluator> {
15418 APValue &Result;
15419
15420public:
15421 MatrixExprEvaluator(EvalInfo &Info, APValue &Result)
15422 : ExprEvaluatorBaseTy(Info), Result(Result) {}
15423
15424 bool Success(ArrayRef<APValue> M, const Expr *E) {
15425 auto *CMTy = E->getType()->castAs<ConstantMatrixType>();
15426 assert(M.size() == CMTy->getNumElementsFlattened());
15427 // FIXME: remove this APValue copy.
15428 Result = APValue(M.data(), CMTy->getNumRows(), CMTy->getNumColumns());
15429 return true;
15430 }
15431 bool Success(const APValue &M, const Expr *E) {
15432 assert(M.isMatrix() && "expected matrix");
15433 Result = M;
15434 return true;
15435 }
15436
15437 bool VisitCastExpr(const CastExpr *E);
15438 bool VisitInitListExpr(const InitListExpr *E);
15439};
15440} // end anonymous namespace
15441
15442static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info) {
15443 assert(E->isPRValue() && E->getType()->isConstantMatrixType() &&
15444 "not a matrix prvalue");
15445 return MatrixExprEvaluator(Info, Result).Visit(S: E);
15446}
15447
15448bool MatrixExprEvaluator::VisitCastExpr(const CastExpr *E) {
15449 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15450 unsigned NumRows = MT->getNumRows();
15451 unsigned NumCols = MT->getNumColumns();
15452 unsigned NElts = NumRows * NumCols;
15453 QualType EltTy = MT->getElementType();
15454 const Expr *SE = E->getSubExpr();
15455
15456 switch (E->getCastKind()) {
15457 case CK_HLSLAggregateSplatCast: {
15458 APValue Val;
15459 QualType ValTy;
15460
15461 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15462 return false;
15463
15464 APValue CastedVal;
15465 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15466 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: EltTy, Original: Val, Result&: CastedVal))
15467 return false;
15468
15469 SmallVector<APValue, 16> SplatEls(NElts, CastedVal);
15470 return Success(M: SplatEls, E);
15471 }
15472 case CK_HLSLElementwiseCast: {
15473 SmallVector<APValue> SrcVals;
15474 SmallVector<QualType> SrcTypes;
15475
15476 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
15477 return false;
15478
15479 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15480 SmallVector<QualType, 16> DestTypes(NElts, EltTy);
15481 SmallVector<APValue, 16> ResultEls(NElts);
15482 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
15483 Results&: ResultEls))
15484 return false;
15485 return Success(M: ResultEls, E);
15486 }
15487 default:
15488 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15489 }
15490}
15491
15492bool MatrixExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
15493 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15494 QualType EltTy = MT->getElementType();
15495
15496 assert(E->getNumInits() == MT->getNumElementsFlattened() &&
15497 "Expected number of elements in initializer list to match the number "
15498 "of matrix elements");
15499
15500 SmallVector<APValue, 16> Elements;
15501 Elements.reserve(N: MT->getNumElementsFlattened());
15502
15503 // The following loop assumes the elements of the matrix InitListExpr are in
15504 // row-major order, which matches the row-major ordering assumption of the
15505 // matrix APValue.
15506 for (unsigned I = 0, N = MT->getNumElementsFlattened(); I < N; ++I) {
15507 if (EltTy->isIntegerType()) {
15508 llvm::APSInt IntVal;
15509 if (!EvaluateInteger(E: E->getInit(Init: I), Result&: IntVal, Info))
15510 return false;
15511 Elements.push_back(Elt: APValue(IntVal));
15512 } else {
15513 llvm::APFloat FloatVal(0.0);
15514 if (!EvaluateFloat(E: E->getInit(Init: I), Result&: FloatVal, Info))
15515 return false;
15516 Elements.push_back(Elt: APValue(FloatVal));
15517 }
15518 }
15519
15520 return Success(M: Elements, E);
15521}
15522
15523//===----------------------------------------------------------------------===//
15524// Array Evaluation
15525//===----------------------------------------------------------------------===//
15526
15527namespace {
15528 class ArrayExprEvaluator
15529 : public ExprEvaluatorBase<ArrayExprEvaluator> {
15530 const LValue &This;
15531 APValue &Result;
15532 public:
15533
15534 ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
15535 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
15536
15537 bool Success(const APValue &V, const Expr *E) {
15538 assert(V.isArray() && "expected array");
15539 Result = V;
15540 return true;
15541 }
15542
15543 bool ZeroInitialization(const Expr *E) {
15544 const ConstantArrayType *CAT =
15545 Info.Ctx.getAsConstantArrayType(T: E->getType());
15546 if (!CAT) {
15547 if (E->getType()->isIncompleteArrayType()) {
15548 // We can be asked to zero-initialize a flexible array member; this
15549 // is represented as an ImplicitValueInitExpr of incomplete array
15550 // type. In this case, the array has zero elements.
15551 Result = APValue(APValue::UninitArray(), 0, 0);
15552 return true;
15553 }
15554 // FIXME: We could handle VLAs here.
15555 return Error(E);
15556 }
15557
15558 Result = APValue(APValue::UninitArray(), 0, CAT->getZExtSize());
15559 if (!Result.hasArrayFiller())
15560 return true;
15561
15562 // Zero-initialize all elements.
15563 LValue Subobject = This;
15564 Subobject.addArray(Info, E, CAT);
15565 ImplicitValueInitExpr VIE(CAT->getElementType());
15566 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject, E: &VIE);
15567 }
15568
15569 bool VisitCallExpr(const CallExpr *E) {
15570 return handleCallExpr(E, Result, ResultSlot: &This);
15571 }
15572 bool VisitCastExpr(const CastExpr *E);
15573 bool VisitInitListExpr(const InitListExpr *E,
15574 QualType AllocType = QualType());
15575 bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E);
15576 bool VisitCXXConstructExpr(const CXXConstructExpr *E);
15577 bool VisitCXXConstructExpr(const CXXConstructExpr *E,
15578 const LValue &Subobject,
15579 APValue *Value, QualType Type);
15580 bool VisitStringLiteral(const StringLiteral *E,
15581 QualType AllocType = QualType()) {
15582 expandStringLiteral(Info, S: E, Result, AllocType);
15583 return true;
15584 }
15585 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
15586 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
15587 ArrayRef<Expr *> Args,
15588 const Expr *ArrayFiller,
15589 QualType AllocType = QualType());
15590 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
15591 };
15592} // end anonymous namespace
15593
15594static bool EvaluateArray(const Expr *E, const LValue &This,
15595 APValue &Result, EvalInfo &Info) {
15596 assert(!E->isValueDependent());
15597 assert(E->isPRValue() && E->getType()->isArrayType() &&
15598 "not an array prvalue");
15599 return ArrayExprEvaluator(Info, This, Result).Visit(S: E);
15600}
15601
15602static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
15603 APValue &Result, const InitListExpr *ILE,
15604 QualType AllocType) {
15605 assert(!ILE->isValueDependent());
15606 assert(ILE->isPRValue() && ILE->getType()->isArrayType() &&
15607 "not an array prvalue");
15608 return ArrayExprEvaluator(Info, This, Result)
15609 .VisitInitListExpr(E: ILE, AllocType);
15610}
15611
15612static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
15613 APValue &Result,
15614 const CXXConstructExpr *CCE,
15615 QualType AllocType) {
15616 assert(!CCE->isValueDependent());
15617 assert(CCE->isPRValue() && CCE->getType()->isArrayType() &&
15618 "not an array prvalue");
15619 return ArrayExprEvaluator(Info, This, Result)
15620 .VisitCXXConstructExpr(E: CCE, Subobject: This, Value: &Result, Type: AllocType);
15621}
15622
15623// Return true iff the given array filler may depend on the element index.
15624static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) {
15625 // For now, just allow non-class value-initialization and initialization
15626 // lists comprised of them.
15627 if (isa<ImplicitValueInitExpr>(Val: FillerExpr))
15628 return false;
15629 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Val: FillerExpr)) {
15630 for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) {
15631 if (MaybeElementDependentArrayFiller(FillerExpr: ILE->getInit(Init: I)))
15632 return true;
15633 }
15634
15635 if (ILE->hasArrayFiller() &&
15636 MaybeElementDependentArrayFiller(FillerExpr: ILE->getArrayFiller()))
15637 return true;
15638
15639 return false;
15640 }
15641 return true;
15642}
15643
15644bool ArrayExprEvaluator::VisitCastExpr(const CastExpr *E) {
15645 const Expr *SE = E->getSubExpr();
15646
15647 switch (E->getCastKind()) {
15648 default:
15649 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15650 case CK_HLSLAggregateSplatCast: {
15651 APValue Val;
15652 QualType ValTy;
15653
15654 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15655 return false;
15656
15657 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
15658
15659 SmallVector<APValue> SplatEls(NEls, Val);
15660 SmallVector<QualType> SplatType(NEls, ValTy);
15661
15662 // cast the elements
15663 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15664 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
15665 ElTypes&: SplatType))
15666 return false;
15667
15668 return true;
15669 }
15670 case CK_HLSLElementwiseCast: {
15671 SmallVector<APValue> SrcEls;
15672 SmallVector<QualType> SrcTypes;
15673
15674 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals&: SrcEls, SrcTypes))
15675 return false;
15676
15677 // cast the elements
15678 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15679 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
15680 ElTypes&: SrcTypes))
15681 return false;
15682 return true;
15683 }
15684 }
15685}
15686
15687bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E,
15688 QualType AllocType) {
15689 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15690 T: AllocType.isNull() ? E->getType() : AllocType);
15691 if (!CAT)
15692 return Error(E);
15693
15694 // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
15695 // an appropriately-typed string literal enclosed in braces.
15696 if (E->isStringLiteralInit()) {
15697 auto *SL = dyn_cast<StringLiteral>(Val: E->getInit(Init: 0)->IgnoreParenImpCasts());
15698 // FIXME: Support ObjCEncodeExpr here once we support it in
15699 // ArrayExprEvaluator generally.
15700 if (!SL)
15701 return Error(E);
15702 return VisitStringLiteral(E: SL, AllocType);
15703 }
15704 // Any other transparent list init will need proper handling of the
15705 // AllocType; we can't just recurse to the inner initializer.
15706 assert(!E->isTransparent() &&
15707 "transparent array list initialization is not string literal init?");
15708
15709 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits(), ArrayFiller: E->getArrayFiller(),
15710 AllocType);
15711}
15712
15713bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
15714 const Expr *ExprToVisit, ArrayRef<Expr *> Args, const Expr *ArrayFiller,
15715 QualType AllocType) {
15716 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15717 T: AllocType.isNull() ? ExprToVisit->getType() : AllocType);
15718
15719 bool Success = true;
15720
15721 unsigned NumEltsToInit = Args.size();
15722 unsigned NumElts = CAT->getZExtSize();
15723
15724 // If the initializer might depend on the array index, run it for each
15725 // array element.
15726 if (NumEltsToInit != NumElts &&
15727 MaybeElementDependentArrayFiller(FillerExpr: ArrayFiller)) {
15728 NumEltsToInit = NumElts;
15729 } else {
15730 // Add additional elements represented by EmbedExpr.
15731 for (auto *Init : Args) {
15732 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts()))
15733 NumEltsToInit += EmbedS->getDataElementCount() - 1;
15734 }
15735 // If we have extra elements in the list, they will be discarded.
15736 if (NumEltsToInit > NumElts)
15737 NumEltsToInit = NumElts;
15738 // If we're overwriting memory which already has an object, make sure we
15739 // don't reduce the number of non-filler elements. (It's possible to
15740 // optimize this in some cases, but the logic gets really complicated.)
15741 if (Result.hasValue() && NumEltsToInit < Result.getArrayInitializedElts())
15742 NumEltsToInit = Result.getArrayInitializedElts();
15743 }
15744
15745 LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
15746 << NumEltsToInit << ".\n");
15747
15748 if (!Result.hasValue()) {
15749 Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15750 } else if (Result.getArrayInitializedElts() != NumEltsToInit) {
15751 // Number of inititalized elts changed. Recreate the APValue, and copy over
15752 // the relevant elements. (This is essentially just fixing the internal
15753 // representation of the value, because it's tied to the number of
15754 // non-filler elements.)
15755 //
15756 // This should be hit rarely, but there are some edge cases:
15757 //
15758 // - The array could be zero-initialized.
15759 // - There could be a DesignatedInitListExpr.
15760 // - operator new[] can be used to start the lifetime early.
15761 APValue NewResult = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15762 // First copy existing elements.
15763 unsigned NumOldElts = Result.getArrayInitializedElts();
15764 for (unsigned I = 0; I < NumOldElts; ++I) {
15765 NewResult.getArrayInitializedElt(I) =
15766 std::move(Result.getArrayInitializedElt(I));
15767 }
15768 // Then copy the array filler over the remaining elements.
15769 for (unsigned I = Result.getArrayInitializedElts(); I < NumEltsToInit; ++I)
15770 NewResult.getArrayInitializedElt(I) = Result.getArrayFiller();
15771 if (NewResult.hasArrayFiller() && Result.hasArrayFiller())
15772 NewResult.getArrayFiller() = Result.getArrayFiller();
15773 Result = std::move(NewResult);
15774 }
15775
15776 LValue Subobject = This;
15777 Subobject.addArray(Info, E: ExprToVisit, CAT);
15778 auto Eval = [&](const Expr *Init, unsigned ArrayIndex) {
15779 if (Init->isValueDependent())
15780 return EvaluateDependentExpr(E: Init, Info);
15781
15782 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
15783 // aren't supposed to be modified.
15784 if (isa<NoInitExpr>(Val: Init))
15785 return true;
15786
15787 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: ArrayIndex), Info,
15788 This: Subobject, E: Init) ||
15789 !HandleLValueArrayAdjustment(Info, E: Init, LVal&: Subobject,
15790 EltTy: CAT->getElementType(), Adjustment: 1)) {
15791 if (!Info.noteFailure())
15792 return false;
15793 Success = false;
15794 }
15795 return true;
15796 };
15797 unsigned ArrayIndex = 0;
15798 QualType DestTy = CAT->getElementType();
15799 APSInt Value(Info.Ctx.getTypeSize(T: DestTy), DestTy->isUnsignedIntegerType());
15800 for (unsigned Index = 0; Index != NumEltsToInit; ++Index) {
15801 const Expr *Init = Index < Args.size() ? Args[Index] : ArrayFiller;
15802 if (ArrayIndex >= NumEltsToInit)
15803 break;
15804 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) {
15805 StringLiteral *SL = EmbedS->getDataStringLiteral();
15806 for (unsigned I = EmbedS->getStartingElementPos(),
15807 N = EmbedS->getDataElementCount();
15808 I != EmbedS->getStartingElementPos() + N; ++I) {
15809 Value = SL->getCodeUnit(I);
15810 if (DestTy->isIntegerType()) {
15811 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(Value);
15812 } else {
15813 assert(DestTy->isFloatingType() && "unexpected type");
15814 const FPOptions FPO =
15815 Init->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15816 APFloat FValue(0.0);
15817 if (!HandleIntToFloatCast(Info, E: Init, FPO, SrcType: EmbedS->getType(), Value,
15818 DestType: DestTy, Result&: FValue))
15819 return false;
15820 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(FValue);
15821 }
15822 ArrayIndex++;
15823 }
15824 } else {
15825 if (!Eval(Init, ArrayIndex))
15826 return false;
15827 ++ArrayIndex;
15828 }
15829 }
15830
15831 if (!Result.hasArrayFiller())
15832 return Success;
15833
15834 // If we get here, we have a trivial filler, which we can just evaluate
15835 // once and splat over the rest of the array elements.
15836 assert(ArrayFiller && "no array filler for incomplete init list");
15837 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject,
15838 E: ArrayFiller) &&
15839 Success;
15840}
15841
15842bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
15843 LValue CommonLV;
15844 if (E->getCommonExpr() &&
15845 !Evaluate(Result&: Info.CurrentCall->createTemporary(
15846 Key: E->getCommonExpr(),
15847 T: getStorageType(Ctx: Info.Ctx, E: E->getCommonExpr()),
15848 Scope: ScopeKind::FullExpression, LV&: CommonLV),
15849 Info, E: E->getCommonExpr()->getSourceExpr()))
15850 return false;
15851
15852 auto *CAT = cast<ConstantArrayType>(Val: E->getType()->castAsArrayTypeUnsafe());
15853
15854 uint64_t Elements = CAT->getZExtSize();
15855 Result = APValue(APValue::UninitArray(), Elements, Elements);
15856
15857 LValue Subobject = This;
15858 Subobject.addArray(Info, E, CAT);
15859
15860 bool Success = true;
15861 for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) {
15862 // C++ [class.temporary]/5
15863 // There are four contexts in which temporaries are destroyed at a different
15864 // point than the end of the full-expression. [...] The second context is
15865 // when a copy constructor is called to copy an element of an array while
15866 // the entire array is copied [...]. In either case, if the constructor has
15867 // one or more default arguments, the destruction of every temporary created
15868 // in a default argument is sequenced before the construction of the next
15869 // array element, if any.
15870 FullExpressionRAII Scope(Info);
15871
15872 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index),
15873 Info, This: Subobject, E: E->getSubExpr()) ||
15874 !HandleLValueArrayAdjustment(Info, E, LVal&: Subobject,
15875 EltTy: CAT->getElementType(), Adjustment: 1)) {
15876 if (!Info.noteFailure())
15877 return false;
15878 Success = false;
15879 }
15880
15881 // Make sure we run the destructors too.
15882 Scope.destroy();
15883 }
15884
15885 return Success;
15886}
15887
15888bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
15889 return VisitCXXConstructExpr(E, Subobject: This, Value: &Result, Type: E->getType());
15890}
15891
15892bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
15893 const LValue &Subobject,
15894 APValue *Value,
15895 QualType Type) {
15896 bool HadZeroInit = Value->hasValue();
15897
15898 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: Type)) {
15899 unsigned FinalSize = CAT->getZExtSize();
15900
15901 // Preserve the array filler if we had prior zero-initialization.
15902 APValue Filler =
15903 HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller()
15904 : APValue();
15905
15906 *Value = APValue(APValue::UninitArray(), 0, FinalSize);
15907 if (FinalSize == 0)
15908 return true;
15909
15910 bool HasTrivialConstructor = CheckTrivialDefaultConstructor(
15911 Info, Loc: E->getExprLoc(), CD: E->getConstructor(),
15912 IsValueInitialization: E->requiresZeroInitialization());
15913 LValue ArrayElt = Subobject;
15914 ArrayElt.addArray(Info, E, CAT);
15915 // We do the whole initialization in two passes, first for just one element,
15916 // then for the whole array. It's possible we may find out we can't do const
15917 // init in the first pass, in which case we avoid allocating a potentially
15918 // large array. We don't do more passes because expanding array requires
15919 // copying the data, which is wasteful.
15920 for (const unsigned N : {1u, FinalSize}) {
15921 unsigned OldElts = Value->getArrayInitializedElts();
15922 if (OldElts == N)
15923 break;
15924
15925 // Expand the array to appropriate size.
15926 APValue NewValue(APValue::UninitArray(), N, FinalSize);
15927 for (unsigned I = 0; I < OldElts; ++I)
15928 NewValue.getArrayInitializedElt(I).swap(
15929 RHS&: Value->getArrayInitializedElt(I));
15930 Value->swap(RHS&: NewValue);
15931
15932 if (HadZeroInit)
15933 for (unsigned I = OldElts; I < N; ++I)
15934 Value->getArrayInitializedElt(I) = Filler;
15935
15936 if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) {
15937 // If we have a trivial constructor, only evaluate it once and copy
15938 // the result into all the array elements.
15939 APValue &FirstResult = Value->getArrayInitializedElt(I: 0);
15940 for (unsigned I = OldElts; I < FinalSize; ++I)
15941 Value->getArrayInitializedElt(I) = FirstResult;
15942 } else {
15943 for (unsigned I = OldElts; I < N; ++I) {
15944 if (!VisitCXXConstructExpr(E, Subobject: ArrayElt,
15945 Value: &Value->getArrayInitializedElt(I),
15946 Type: CAT->getElementType()) ||
15947 !HandleLValueArrayAdjustment(Info, E, LVal&: ArrayElt,
15948 EltTy: CAT->getElementType(), Adjustment: 1))
15949 return false;
15950 // When checking for const initilization any diagnostic is considered
15951 // an error.
15952 if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&
15953 !Info.keepEvaluatingAfterFailure())
15954 return false;
15955 }
15956 }
15957 }
15958
15959 return true;
15960 }
15961
15962 if (!Type->isRecordType())
15963 return Error(E);
15964
15965 return RecordExprEvaluator(Info, Subobject, *Value)
15966 .VisitCXXConstructExpr(E, T: Type);
15967}
15968
15969bool ArrayExprEvaluator::VisitCXXParenListInitExpr(
15970 const CXXParenListInitExpr *E) {
15971 assert(E->getType()->isConstantArrayType() &&
15972 "Expression result is not a constant array type");
15973
15974 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs(),
15975 ArrayFiller: E->getArrayFiller());
15976}
15977
15978bool ArrayExprEvaluator::VisitDesignatedInitUpdateExpr(
15979 const DesignatedInitUpdateExpr *E) {
15980 if (!Visit(S: E->getBase()))
15981 return false;
15982 return Visit(S: E->getUpdater());
15983}
15984
15985//===----------------------------------------------------------------------===//
15986// Integer Evaluation
15987//
15988// As a GNU extension, we support casting pointers to sufficiently-wide integer
15989// types and back in constant folding. Integer values are thus represented
15990// either as an integer-valued APValue, or as an lvalue-valued APValue.
15991//===----------------------------------------------------------------------===//
15992
15993namespace {
15994class IntExprEvaluator
15995 : public ExprEvaluatorBase<IntExprEvaluator> {
15996 APValue &Result;
15997public:
15998 IntExprEvaluator(EvalInfo &info, APValue &result)
15999 : ExprEvaluatorBaseTy(info), Result(result) {}
16000
16001 bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) {
16002 assert(E->getType()->isIntegralOrEnumerationType() &&
16003 "Invalid evaluation result.");
16004 assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
16005 "Invalid evaluation result.");
16006 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16007 "Invalid evaluation result.");
16008 Result = APValue(SI);
16009 return true;
16010 }
16011 bool Success(const llvm::APSInt &SI, const Expr *E) {
16012 return Success(SI, E, Result);
16013 }
16014
16015 bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) {
16016 assert(E->getType()->isIntegralOrEnumerationType() &&
16017 "Invalid evaluation result.");
16018 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16019 "Invalid evaluation result.");
16020 Result = APValue(APSInt(I));
16021 Result.getInt().setIsUnsigned(
16022 E->getType()->isUnsignedIntegerOrEnumerationType());
16023 return true;
16024 }
16025 bool Success(const llvm::APInt &I, const Expr *E) {
16026 return Success(I, E, Result);
16027 }
16028
16029 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
16030 assert(E->getType()->isIntegralOrEnumerationType() &&
16031 "Invalid evaluation result.");
16032 Result = APValue(Info.Ctx.MakeIntValue(Value, Type: E->getType()));
16033 return true;
16034 }
16035 bool Success(uint64_t Value, const Expr *E) {
16036 return Success(Value, E, Result);
16037 }
16038
16039 bool Success(CharUnits Size, const Expr *E) {
16040 return Success(Value: Size.getQuantity(), E);
16041 }
16042
16043 bool Success(const APValue &V, const Expr *E) {
16044 // C++23 [expr.const]p8 If we have a variable that is unknown reference or
16045 // pointer allow further evaluation of the value.
16046 if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate() ||
16047 V.allowConstexprUnknown()) {
16048 Result = V;
16049 return true;
16050 }
16051 return Success(SI: V.getInt(), E);
16052 }
16053
16054 bool ZeroInitialization(const Expr *E) { return Success(Value: 0, E); }
16055
16056 friend std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &,
16057 const CallExpr *);
16058
16059 //===--------------------------------------------------------------------===//
16060 // Visitor Methods
16061 //===--------------------------------------------------------------------===//
16062
16063 bool VisitIntegerLiteral(const IntegerLiteral *E) {
16064 return Success(I: E->getValue(), E);
16065 }
16066 bool VisitCharacterLiteral(const CharacterLiteral *E) {
16067 return Success(Value: E->getValue(), E);
16068 }
16069
16070 bool CheckReferencedDecl(const Expr *E, const Decl *D);
16071 bool VisitDeclRefExpr(const DeclRefExpr *E) {
16072 if (CheckReferencedDecl(E, D: E->getDecl()))
16073 return true;
16074
16075 return ExprEvaluatorBaseTy::VisitDeclRefExpr(S: E);
16076 }
16077 bool VisitMemberExpr(const MemberExpr *E) {
16078 if (CheckReferencedDecl(E, D: E->getMemberDecl())) {
16079 VisitIgnoredBaseExpression(E: E->getBase());
16080 return true;
16081 }
16082
16083 return ExprEvaluatorBaseTy::VisitMemberExpr(E);
16084 }
16085
16086 bool VisitCallExpr(const CallExpr *E);
16087 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
16088 bool VisitBinaryOperator(const BinaryOperator *E);
16089 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
16090 bool VisitUnaryOperator(const UnaryOperator *E);
16091
16092 bool VisitCastExpr(const CastExpr* E);
16093 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
16094
16095 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
16096 return Success(Value: E->getValue(), E);
16097 }
16098
16099 bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
16100 return Success(Value: E->getValue(), E);
16101 }
16102
16103 bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
16104 if (Info.ArrayInitIndex == uint64_t(-1)) {
16105 // We were asked to evaluate this subexpression independent of the
16106 // enclosing ArrayInitLoopExpr. We can't do that.
16107 Info.FFDiag(E);
16108 return false;
16109 }
16110 return Success(Value: Info.ArrayInitIndex, E);
16111 }
16112
16113 // Note, GNU defines __null as an integer, not a pointer.
16114 bool VisitGNUNullExpr(const GNUNullExpr *E) {
16115 return ZeroInitialization(E);
16116 }
16117
16118 bool VisitTypeTraitExpr(const TypeTraitExpr *E) {
16119 if (E->isStoredAsBoolean())
16120 return Success(Value: E->getBoolValue(), E);
16121 if (E->getAPValue().isAbsent())
16122 return false;
16123 assert(E->getAPValue().isInt() && "APValue type not supported");
16124 return Success(SI: E->getAPValue().getInt(), E);
16125 }
16126
16127 bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
16128 return Success(Value: E->getValue(), E);
16129 }
16130
16131 bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
16132 return Success(Value: E->getValue(), E);
16133 }
16134
16135 bool VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *E) {
16136 // This should not be evaluated during constant expr evaluation, as it
16137 // should always be in an unevaluated context (the args list of a 'gang' or
16138 // 'tile' clause).
16139 return Error(E);
16140 }
16141
16142 bool VisitUnaryReal(const UnaryOperator *E);
16143 bool VisitUnaryImag(const UnaryOperator *E);
16144
16145 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
16146 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
16147 bool VisitSourceLocExpr(const SourceLocExpr *E);
16148 bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);
16149 bool VisitRequiresExpr(const RequiresExpr *E);
16150 // FIXME: Missing: array subscript of vector, member of vector
16151};
16152
16153class FixedPointExprEvaluator
16154 : public ExprEvaluatorBase<FixedPointExprEvaluator> {
16155 APValue &Result;
16156
16157 public:
16158 FixedPointExprEvaluator(EvalInfo &info, APValue &result)
16159 : ExprEvaluatorBaseTy(info), Result(result) {}
16160
16161 bool Success(const llvm::APInt &I, const Expr *E) {
16162 return Success(
16163 V: APFixedPoint(I, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
16164 }
16165
16166 bool Success(uint64_t Value, const Expr *E) {
16167 return Success(
16168 V: APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
16169 }
16170
16171 bool Success(const APValue &V, const Expr *E) {
16172 return Success(V: V.getFixedPoint(), E);
16173 }
16174
16175 bool Success(const APFixedPoint &V, const Expr *E) {
16176 assert(E->getType()->isFixedPointType() && "Invalid evaluation result.");
16177 assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16178 "Invalid evaluation result.");
16179 Result = APValue(V);
16180 return true;
16181 }
16182
16183 bool ZeroInitialization(const Expr *E) {
16184 return Success(Value: 0, E);
16185 }
16186
16187 //===--------------------------------------------------------------------===//
16188 // Visitor Methods
16189 //===--------------------------------------------------------------------===//
16190
16191 bool VisitFixedPointLiteral(const FixedPointLiteral *E) {
16192 return Success(I: E->getValue(), E);
16193 }
16194
16195 bool VisitCastExpr(const CastExpr *E);
16196 bool VisitUnaryOperator(const UnaryOperator *E);
16197 bool VisitBinaryOperator(const BinaryOperator *E);
16198};
16199} // end anonymous namespace
16200
16201/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
16202/// produce either the integer value or a pointer.
16203///
16204/// GCC has a heinous extension which folds casts between pointer types and
16205/// pointer-sized integral types. We support this by allowing the evaluation of
16206/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
16207/// Some simple arithmetic on such values is supported (they are treated much
16208/// like char*).
16209static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
16210 EvalInfo &Info) {
16211 assert(!E->isValueDependent());
16212 assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType());
16213 return IntExprEvaluator(Info, Result).Visit(S: E);
16214}
16215
16216static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
16217 assert(!E->isValueDependent());
16218 APValue Val;
16219 if (!EvaluateIntegerOrLValue(E, Result&: Val, Info))
16220 return false;
16221 if (!Val.isInt()) {
16222 // FIXME: It would be better to produce the diagnostic for casting
16223 // a pointer to an integer.
16224 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
16225 return false;
16226 }
16227 Result = Val.getInt();
16228 return true;
16229}
16230
16231bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) {
16232 APValue Evaluated = E->EvaluateInContext(
16233 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
16234 return Success(V: Evaluated, E);
16235}
16236
16237static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
16238 EvalInfo &Info) {
16239 assert(!E->isValueDependent());
16240 if (E->getType()->isFixedPointType()) {
16241 APValue Val;
16242 if (!FixedPointExprEvaluator(Info, Val).Visit(S: E))
16243 return false;
16244 if (!Val.isFixedPoint())
16245 return false;
16246
16247 Result = Val.getFixedPoint();
16248 return true;
16249 }
16250 return false;
16251}
16252
16253static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
16254 EvalInfo &Info) {
16255 assert(!E->isValueDependent());
16256 if (E->getType()->isIntegerType()) {
16257 auto FXSema = Info.Ctx.getFixedPointSemantics(Ty: E->getType());
16258 APSInt Val;
16259 if (!EvaluateInteger(E, Result&: Val, Info))
16260 return false;
16261 Result = APFixedPoint(Val, FXSema);
16262 return true;
16263 } else if (E->getType()->isFixedPointType()) {
16264 return EvaluateFixedPoint(E, Result, Info);
16265 }
16266 return false;
16267}
16268
16269/// Check whether the given declaration can be directly converted to an integral
16270/// rvalue. If not, no diagnostic is produced; there are other things we can
16271/// try.
16272bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
16273 // Enums are integer constant exprs.
16274 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(Val: D)) {
16275 // Check for signedness/width mismatches between E type and ECD value.
16276 bool SameSign = (ECD->getInitVal().isSigned()
16277 == E->getType()->isSignedIntegerOrEnumerationType());
16278 bool SameWidth = (ECD->getInitVal().getBitWidth()
16279 == Info.Ctx.getIntWidth(T: E->getType()));
16280 if (SameSign && SameWidth)
16281 return Success(SI: ECD->getInitVal(), E);
16282 else {
16283 // Get rid of mismatch (otherwise Success assertions will fail)
16284 // by computing a new value matching the type of E.
16285 llvm::APSInt Val = ECD->getInitVal();
16286 if (!SameSign)
16287 Val.setIsSigned(!ECD->getInitVal().isSigned());
16288 if (!SameWidth)
16289 Val = Val.extOrTrunc(width: Info.Ctx.getIntWidth(T: E->getType()));
16290 return Success(SI: Val, E);
16291 }
16292 }
16293 return false;
16294}
16295
16296/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16297/// as GCC.
16298GCCTypeClass EvaluateBuiltinClassifyType(QualType T,
16299 const LangOptions &LangOpts) {
16300 assert(!T->isDependentType() && "unexpected dependent type");
16301
16302 QualType CanTy = T.getCanonicalType();
16303
16304 switch (CanTy->getTypeClass()) {
16305#define TYPE(ID, BASE)
16306#define DEPENDENT_TYPE(ID, BASE) case Type::ID:
16307#define NON_CANONICAL_TYPE(ID, BASE) case Type::ID:
16308#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID:
16309#include "clang/AST/TypeNodes.inc"
16310 case Type::Auto:
16311 case Type::DeducedTemplateSpecialization:
16312 llvm_unreachable("unexpected non-canonical or dependent type");
16313
16314 case Type::Builtin:
16315 switch (cast<BuiltinType>(Val&: CanTy)->getKind()) {
16316#define BUILTIN_TYPE(ID, SINGLETON_ID)
16317#define SIGNED_TYPE(ID, SINGLETON_ID) \
16318 case BuiltinType::ID: return GCCTypeClass::Integer;
16319#define FLOATING_TYPE(ID, SINGLETON_ID) \
16320 case BuiltinType::ID: return GCCTypeClass::RealFloat;
16321#define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \
16322 case BuiltinType::ID: break;
16323#include "clang/AST/BuiltinTypes.def"
16324 case BuiltinType::Void:
16325 return GCCTypeClass::Void;
16326
16327 case BuiltinType::Bool:
16328 return GCCTypeClass::Bool;
16329
16330 case BuiltinType::Char_U:
16331 case BuiltinType::UChar:
16332 case BuiltinType::WChar_U:
16333 case BuiltinType::Char8:
16334 case BuiltinType::Char16:
16335 case BuiltinType::Char32:
16336 case BuiltinType::UShort:
16337 case BuiltinType::UInt:
16338 case BuiltinType::ULong:
16339 case BuiltinType::ULongLong:
16340 case BuiltinType::UInt128:
16341 return GCCTypeClass::Integer;
16342
16343 case BuiltinType::UShortAccum:
16344 case BuiltinType::UAccum:
16345 case BuiltinType::ULongAccum:
16346 case BuiltinType::UShortFract:
16347 case BuiltinType::UFract:
16348 case BuiltinType::ULongFract:
16349 case BuiltinType::SatUShortAccum:
16350 case BuiltinType::SatUAccum:
16351 case BuiltinType::SatULongAccum:
16352 case BuiltinType::SatUShortFract:
16353 case BuiltinType::SatUFract:
16354 case BuiltinType::SatULongFract:
16355 return GCCTypeClass::None;
16356
16357 case BuiltinType::NullPtr:
16358
16359 case BuiltinType::ObjCId:
16360 case BuiltinType::ObjCClass:
16361 case BuiltinType::ObjCSel:
16362#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
16363 case BuiltinType::Id:
16364#include "clang/Basic/OpenCLImageTypes.def"
16365#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
16366 case BuiltinType::Id:
16367#include "clang/Basic/OpenCLExtensionTypes.def"
16368 case BuiltinType::OCLSampler:
16369 case BuiltinType::OCLEvent:
16370 case BuiltinType::OCLClkEvent:
16371 case BuiltinType::OCLQueue:
16372 case BuiltinType::OCLReserveID:
16373#define SVE_TYPE(Name, Id, SingletonId) \
16374 case BuiltinType::Id:
16375#include "clang/Basic/AArch64ACLETypes.def"
16376#define PPC_VECTOR_TYPE(Name, Id, Size) \
16377 case BuiltinType::Id:
16378#include "clang/Basic/PPCTypes.def"
16379#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16380#include "clang/Basic/RISCVVTypes.def"
16381#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16382#include "clang/Basic/WebAssemblyReferenceTypes.def"
16383#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
16384#include "clang/Basic/AMDGPUTypes.def"
16385#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16386#include "clang/Basic/HLSLIntangibleTypes.def"
16387#define SPIRV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16388#include "clang/Basic/SPIRVTypes.def"
16389 return GCCTypeClass::None;
16390
16391 case BuiltinType::Dependent:
16392 llvm_unreachable("unexpected dependent type");
16393 };
16394 llvm_unreachable("unexpected placeholder type");
16395
16396 case Type::Enum:
16397 return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer;
16398
16399 case Type::Pointer:
16400 case Type::ConstantArray:
16401 case Type::VariableArray:
16402 case Type::IncompleteArray:
16403 case Type::FunctionNoProto:
16404 case Type::FunctionProto:
16405 case Type::ArrayParameter:
16406 return GCCTypeClass::Pointer;
16407
16408 case Type::MemberPointer:
16409 return CanTy->isMemberDataPointerType()
16410 ? GCCTypeClass::PointerToDataMember
16411 : GCCTypeClass::PointerToMemberFunction;
16412
16413 case Type::Complex:
16414 return GCCTypeClass::Complex;
16415
16416 case Type::Record:
16417 return CanTy->isUnionType() ? GCCTypeClass::Union
16418 : GCCTypeClass::ClassOrStruct;
16419
16420 case Type::Atomic:
16421 // GCC classifies _Atomic T the same as T.
16422 return EvaluateBuiltinClassifyType(
16423 T: CanTy->castAs<AtomicType>()->getValueType(), LangOpts);
16424
16425 case Type::Vector:
16426 case Type::ExtVector:
16427 return GCCTypeClass::Vector;
16428
16429 case Type::BlockPointer:
16430 case Type::ConstantMatrix:
16431 case Type::ObjCObject:
16432 case Type::ObjCInterface:
16433 case Type::ObjCObjectPointer:
16434 case Type::Pipe:
16435 case Type::HLSLAttributedResource:
16436 case Type::HLSLInlineSpirv:
16437 case Type::OverflowBehavior:
16438 // Classify all other types that don't fit into the regular
16439 // classification the same way.
16440 return GCCTypeClass::None;
16441
16442 case Type::BitInt:
16443 return GCCTypeClass::BitInt;
16444
16445 case Type::LValueReference:
16446 case Type::RValueReference:
16447 llvm_unreachable("invalid type for expression");
16448 }
16449
16450 llvm_unreachable("unexpected type class");
16451}
16452
16453/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16454/// as GCC.
16455static GCCTypeClass
16456EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) {
16457 // If no argument was supplied, default to None. This isn't
16458 // ideal, however it is what gcc does.
16459 if (E->getNumArgs() == 0)
16460 return GCCTypeClass::None;
16461
16462 // FIXME: Bizarrely, GCC treats a call with more than one argument as not
16463 // being an ICE, but still folds it to a constant using the type of the first
16464 // argument.
16465 return EvaluateBuiltinClassifyType(T: E->getArg(Arg: 0)->getType(), LangOpts);
16466}
16467
16468/// EvaluateBuiltinConstantPForLValue - Determine the result of
16469/// __builtin_constant_p when applied to the given pointer.
16470///
16471/// A pointer is only "constant" if it is null (or a pointer cast to integer)
16472/// or it points to the first character of a string literal.
16473static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) {
16474 APValue::LValueBase Base = LV.getLValueBase();
16475 if (Base.isNull()) {
16476 // A null base is acceptable.
16477 return true;
16478 } else if (const Expr *E = Base.dyn_cast<const Expr *>()) {
16479 if (!isa<StringLiteral>(Val: E))
16480 return false;
16481 return LV.getLValueOffset().isZero();
16482 } else if (Base.is<TypeInfoLValue>()) {
16483 // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to
16484 // evaluate to true.
16485 return true;
16486 } else {
16487 // Any other base is not constant enough for GCC.
16488 return false;
16489 }
16490}
16491
16492/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
16493/// GCC as we can manage.
16494static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) {
16495 // This evaluation is not permitted to have side-effects, so evaluate it in
16496 // a speculative evaluation context.
16497 SpeculativeEvaluationRAII SpeculativeEval(Info);
16498
16499 // Constant-folding is always enabled for the operand of __builtin_constant_p
16500 // (even when the enclosing evaluation context otherwise requires a strict
16501 // language-specific constant expression).
16502 FoldConstant Fold(Info, true);
16503
16504 QualType ArgType = Arg->getType();
16505
16506 // __builtin_constant_p always has one operand. The rules which gcc follows
16507 // are not precisely documented, but are as follows:
16508 //
16509 // - If the operand is of integral, floating, complex or enumeration type,
16510 // and can be folded to a known value of that type, it returns 1.
16511 // - If the operand can be folded to a pointer to the first character
16512 // of a string literal (or such a pointer cast to an integral type)
16513 // or to a null pointer or an integer cast to a pointer, it returns 1.
16514 //
16515 // Otherwise, it returns 0.
16516 //
16517 // FIXME: GCC also intends to return 1 for literals of aggregate types, but
16518 // its support for this did not work prior to GCC 9 and is not yet well
16519 // understood.
16520 if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() ||
16521 ArgType->isAnyComplexType() || ArgType->isPointerType() ||
16522 ArgType->isNullPtrType()) {
16523 APValue V;
16524 if (!::EvaluateAsRValue(Info, E: Arg, Result&: V) || Info.EvalStatus.HasSideEffects) {
16525 Fold.keepDiagnostics();
16526 return false;
16527 }
16528
16529 // For a pointer (possibly cast to integer), there are special rules.
16530 if (V.getKind() == APValue::LValue)
16531 return EvaluateBuiltinConstantPForLValue(LV: V);
16532
16533 // Otherwise, any constant value is good enough.
16534 return V.hasValue();
16535 }
16536
16537 // Anything else isn't considered to be sufficiently constant.
16538 return false;
16539}
16540
16541/// Retrieves the "underlying object type" of the given expression,
16542/// as used by __builtin_object_size.
16543static QualType getObjectType(APValue::LValueBase B) {
16544 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
16545 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
16546 return VD->getType();
16547 } else if (const Expr *E = B.dyn_cast<const Expr*>()) {
16548 if (isa<CompoundLiteralExpr>(Val: E))
16549 return E->getType();
16550 } else if (B.is<TypeInfoLValue>()) {
16551 return B.getTypeInfoType();
16552 } else if (B.is<DynamicAllocLValue>()) {
16553 return B.getDynamicAllocType();
16554 }
16555
16556 return QualType();
16557}
16558
16559/// A more selective version of E->IgnoreParenCasts for
16560/// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only
16561/// to change the type of E.
16562/// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo`
16563///
16564/// Always returns an RValue with a pointer representation.
16565const Expr *ignorePointerCastsAndParens(const Expr *E) {
16566 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
16567
16568 const Expr *NoParens = E->IgnoreParens();
16569 const auto *Cast = dyn_cast<CastExpr>(Val: NoParens);
16570 if (Cast == nullptr)
16571 return NoParens;
16572
16573 // We only conservatively allow a few kinds of casts, because this code is
16574 // inherently a simple solution that seeks to support the common case.
16575 auto CastKind = Cast->getCastKind();
16576 if (CastKind != CK_NoOp && CastKind != CK_BitCast &&
16577 CastKind != CK_AddressSpaceConversion)
16578 return NoParens;
16579
16580 const auto *SubExpr = Cast->getSubExpr();
16581 if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue())
16582 return NoParens;
16583 return ignorePointerCastsAndParens(E: SubExpr);
16584}
16585
16586/// Checks to see if the given LValue's Designator is at the end of the LValue's
16587/// record layout. e.g.
16588/// struct { struct { int a, b; } fst, snd; } obj;
16589/// obj.fst // no
16590/// obj.snd // yes
16591/// obj.fst.a // no
16592/// obj.fst.b // no
16593/// obj.snd.a // no
16594/// obj.snd.b // yes
16595///
16596/// Please note: this function is specialized for how __builtin_object_size
16597/// views "objects".
16598///
16599/// If this encounters an invalid RecordDecl or otherwise cannot determine the
16600/// correct result, it will always return true.
16601static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) {
16602 assert(!LVal.Designator.Invalid);
16603
16604 auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD) {
16605 const RecordDecl *Parent = FD->getParent();
16606 if (Parent->isInvalidDecl() || Parent->isUnion())
16607 return true;
16608 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(D: Parent);
16609 return FD->getFieldIndex() + 1 == Layout.getFieldCount();
16610 };
16611
16612 auto &Base = LVal.getLValueBase();
16613 if (auto *ME = dyn_cast_or_null<MemberExpr>(Val: Base.dyn_cast<const Expr *>())) {
16614 if (auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl())) {
16615 if (!IsLastOrInvalidFieldDecl(FD))
16616 return false;
16617 } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(Val: ME->getMemberDecl())) {
16618 for (auto *FD : IFD->chain()) {
16619 if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(Val: FD)))
16620 return false;
16621 }
16622 }
16623 }
16624
16625 unsigned I = 0;
16626 QualType BaseType = getType(B: Base);
16627 if (LVal.Designator.FirstEntryIsAnUnsizedArray) {
16628 // If we don't know the array bound, conservatively assume we're looking at
16629 // the final array element.
16630 ++I;
16631 if (BaseType->isIncompleteArrayType())
16632 BaseType = Ctx.getAsArrayType(T: BaseType)->getElementType();
16633 else
16634 BaseType = BaseType->castAs<PointerType>()->getPointeeType();
16635 }
16636
16637 for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) {
16638 const auto &Entry = LVal.Designator.Entries[I];
16639 if (BaseType->isArrayType()) {
16640 // Because __builtin_object_size treats arrays as objects, we can ignore
16641 // the index iff this is the last array in the Designator.
16642 if (I + 1 == E)
16643 return true;
16644 const auto *CAT = cast<ConstantArrayType>(Val: Ctx.getAsArrayType(T: BaseType));
16645 uint64_t Index = Entry.getAsArrayIndex();
16646 if (Index + 1 != CAT->getZExtSize())
16647 return false;
16648 BaseType = CAT->getElementType();
16649 } else if (BaseType->isAnyComplexType()) {
16650 const auto *CT = BaseType->castAs<ComplexType>();
16651 uint64_t Index = Entry.getAsArrayIndex();
16652 if (Index != 1)
16653 return false;
16654 BaseType = CT->getElementType();
16655 } else if (auto *FD = getAsField(E: Entry)) {
16656 if (!IsLastOrInvalidFieldDecl(FD))
16657 return false;
16658 BaseType = FD->getType();
16659 } else {
16660 assert(getAsBaseClass(Entry) && "Expecting cast to a base class");
16661 return false;
16662 }
16663 }
16664 return true;
16665}
16666
16667/// Tests to see if the LValue has a user-specified designator (that isn't
16668/// necessarily valid). Note that this always returns 'true' if the LValue has
16669/// an unsized array as its first designator entry, because there's currently no
16670/// way to tell if the user typed *foo or foo[0].
16671static bool refersToCompleteObject(const LValue &LVal) {
16672 if (LVal.Designator.Invalid)
16673 return false;
16674
16675 if (!LVal.Designator.Entries.empty())
16676 return LVal.Designator.isMostDerivedAnUnsizedArray();
16677
16678 if (!LVal.InvalidBase)
16679 return true;
16680
16681 // If `E` is a MemberExpr, then the first part of the designator is hiding in
16682 // the LValueBase.
16683 const auto *E = LVal.Base.dyn_cast<const Expr *>();
16684 return !E || !isa<MemberExpr>(Val: E);
16685}
16686
16687/// Attempts to detect a user writing into a piece of memory that's impossible
16688/// to figure out the size of by just using types.
16689static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) {
16690 const SubobjectDesignator &Designator = LVal.Designator;
16691 // Notes:
16692 // - Users can only write off of the end when we have an invalid base. Invalid
16693 // bases imply we don't know where the memory came from.
16694 // - We used to be a bit more aggressive here; we'd only be conservative if
16695 // the array at the end was flexible, or if it had 0 or 1 elements. This
16696 // broke some common standard library extensions (PR30346), but was
16697 // otherwise seemingly fine. It may be useful to reintroduce this behavior
16698 // with some sort of list. OTOH, it seems that GCC is always
16699 // conservative with the last element in structs (if it's an array), so our
16700 // current behavior is more compatible than an explicit list approach would
16701 // be.
16702 auto isFlexibleArrayMember = [&] {
16703 using FAMKind = LangOptions::StrictFlexArraysLevelKind;
16704 FAMKind StrictFlexArraysLevel =
16705 Ctx.getLangOpts().getStrictFlexArraysLevel();
16706
16707 if (Designator.isMostDerivedAnUnsizedArray())
16708 return true;
16709
16710 if (StrictFlexArraysLevel == FAMKind::Default)
16711 return true;
16712
16713 if (Designator.getMostDerivedArraySize() == 0 &&
16714 StrictFlexArraysLevel != FAMKind::IncompleteOnly)
16715 return true;
16716
16717 if (Designator.getMostDerivedArraySize() == 1 &&
16718 StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete)
16719 return true;
16720
16721 return false;
16722 };
16723
16724 return LVal.InvalidBase &&
16725 Designator.Entries.size() == Designator.MostDerivedPathLength &&
16726 Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() &&
16727 isDesignatorAtObjectEnd(Ctx, LVal);
16728}
16729
16730/// Converts the given APInt to CharUnits, assuming the APInt is unsigned.
16731/// Fails if the conversion would cause loss of precision.
16732static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int,
16733 CharUnits &Result) {
16734 auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max();
16735 if (Int.ugt(RHS: CharUnitsMax))
16736 return false;
16737 Result = CharUnits::fromQuantity(Quantity: Int.getZExtValue());
16738 return true;
16739}
16740
16741/// If we're evaluating the object size of an instance of a struct that
16742/// contains a flexible array member, add the size of the initializer.
16743static void addFlexibleArrayMemberInitSize(EvalInfo &Info, const QualType &T,
16744 const LValue &LV, CharUnits &Size) {
16745 if (!T.isNull() && T->isStructureType() &&
16746 T->castAsRecordDecl()->hasFlexibleArrayMember())
16747 if (const auto *V = LV.getLValueBase().dyn_cast<const ValueDecl *>())
16748 if (const auto *VD = dyn_cast<VarDecl>(Val: V))
16749 if (VD->hasInit())
16750 Size += VD->getFlexibleArrayInitChars(Ctx: Info.Ctx);
16751}
16752
16753/// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will
16754/// determine how many bytes exist from the beginning of the object to either
16755/// the end of the current subobject, or the end of the object itself, depending
16756/// on what the LValue looks like + the value of Type.
16757///
16758/// If this returns false, the value of Result is undefined.
16759static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc,
16760 unsigned Type, const LValue &LVal,
16761 CharUnits &EndOffset) {
16762 bool DetermineForCompleteObject = refersToCompleteObject(LVal);
16763
16764 auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) {
16765 if (Ty.isNull())
16766 return false;
16767
16768 Ty = Ty.getNonReferenceType();
16769
16770 if (Ty->isIncompleteType() || Ty->isFunctionType())
16771 return false;
16772
16773 return HandleSizeof(Info, Loc: ExprLoc, Type: Ty, Size&: Result);
16774 };
16775
16776 // We want to evaluate the size of the entire object. This is a valid fallback
16777 // for when Type=1 and the designator is invalid, because we're asked for an
16778 // upper-bound.
16779 if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) {
16780 // Type=3 wants a lower bound, so we can't fall back to this.
16781 if (Type == 3 && !DetermineForCompleteObject)
16782 return false;
16783
16784 llvm::APInt APEndOffset;
16785 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
16786 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
16787 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
16788
16789 if (LVal.InvalidBase)
16790 return false;
16791
16792 QualType BaseTy = getObjectType(B: LVal.getLValueBase());
16793 const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset);
16794 addFlexibleArrayMemberInitSize(Info, T: BaseTy, LV: LVal, Size&: EndOffset);
16795 return Ret;
16796 }
16797
16798 // We want to evaluate the size of a subobject.
16799 const SubobjectDesignator &Designator = LVal.Designator;
16800
16801 // The following is a moderately common idiom in C:
16802 //
16803 // struct Foo { int a; char c[1]; };
16804 // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar));
16805 // strcpy(&F->c[0], Bar);
16806 //
16807 // In order to not break too much legacy code, we need to support it.
16808 if (isUserWritingOffTheEnd(Ctx: Info.Ctx, LVal)) {
16809 // If we can resolve this to an alloc_size call, we can hand that back,
16810 // because we know for certain how many bytes there are to write to.
16811 llvm::APInt APEndOffset;
16812 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
16813 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
16814 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
16815
16816 // If we cannot determine the size of the initial allocation, then we can't
16817 // given an accurate upper-bound. However, we are still able to give
16818 // conservative lower-bounds for Type=3.
16819 if (Type == 1)
16820 return false;
16821 }
16822
16823 CharUnits BytesPerElem;
16824 if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem))
16825 return false;
16826
16827 // According to the GCC documentation, we want the size of the subobject
16828 // denoted by the pointer. But that's not quite right -- what we actually
16829 // want is the size of the immediately-enclosing array, if there is one.
16830 int64_t ElemsRemaining;
16831 if (Designator.MostDerivedIsArrayElement &&
16832 Designator.Entries.size() == Designator.MostDerivedPathLength) {
16833 uint64_t ArraySize = Designator.getMostDerivedArraySize();
16834 uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex();
16835 ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex;
16836 } else {
16837 ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1;
16838 }
16839
16840 EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining;
16841 return true;
16842}
16843
16844/// Tries to evaluate the __builtin_object_size for @p E.
16845///
16846/// If @p IsDynamic is true (i.e. we're evaluating
16847/// __builtin_dynamic_object_size) and the operand designates a flexible array
16848/// member annotated with 'counted_by', we refuse to fold so that IR generation
16849/// can emit the count-based runtime size computation.
16850static std::optional<uint64_t>
16851tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, EvalInfo &Info,
16852 bool IsDynamic = false) {
16853
16854 // Determine the denoted object.
16855 LValue LVal;
16856 {
16857 // The operand of __builtin_object_size is never evaluated for side-effects.
16858 // If there are any, but we can determine the pointed-to object anyway, then
16859 // ignore the side-effects.
16860 SpeculativeEvaluationRAII SpeculativeEval(Info);
16861 IgnoreSideEffectsRAII Fold(Info);
16862
16863 if (E->isGLValue()) {
16864 // It's possible for us to be given GLValues if we're called via
16865 // Expr::tryEvaluateObjectSize.
16866 APValue RVal;
16867 if (!EvaluateAsRValue(Info, E, Result&: RVal))
16868 return std::nullopt;
16869 LVal.setFrom(Ctx: Info.Ctx, V: RVal);
16870 } else if (!EvaluatePointer(E: ignorePointerCastsAndParens(E), Result&: LVal, Info,
16871 /*InvalidBaseOK=*/true))
16872 return std::nullopt;
16873 }
16874
16875 // If we point to before the start of the object, there are no accessible
16876 // bytes.
16877 if (LVal.getLValueOffset().isNegative())
16878 return 0;
16879
16880 // For __builtin_dynamic_object_size on a counted_by-annotated flexible
16881 // array member, defer to IR generation (emitCountedBySize in CGBuiltin):
16882 // its runtime computation uses the live 'count' field and is more accurate
16883 // than the layout/initializer-derived size we'd produce here. Use the same
16884 // findStructFieldAccess form-recognition CGBuiltin does, so we refuse to
16885 // fold on exactly the shapes that path handles (and, importantly, *not*
16886 // on '&af.fam' which designates the array-as-a-whole and stays on the
16887 // layout-derived path to match GCC). Checked after the negative-offset
16888 // early return above so that obviously out-of-bounds operands still fold
16889 // to 0, preserving existing behavior.
16890 if (IsDynamic) {
16891 const auto *ME = dyn_cast_or_null<MemberExpr>(Val: findStructFieldAccess(E));
16892 const auto *FD = ME ? dyn_cast<FieldDecl>(Val: ME->getMemberDecl()) : nullptr;
16893 if (FD && FD->getType()->isCountAttributedType())
16894 return std::nullopt;
16895 }
16896
16897 CharUnits EndOffset;
16898 if (!determineEndOffset(Info, ExprLoc: E->getExprLoc(), Type, LVal, EndOffset))
16899 return std::nullopt;
16900
16901 // If we've fallen outside of the end offset, just pretend there's nothing to
16902 // write to/read from.
16903 if (EndOffset <= LVal.getLValueOffset())
16904 return 0;
16905 return (EndOffset - LVal.getLValueOffset()).getQuantity();
16906}
16907
16908bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
16909 if (!IsConstantEvaluatedBuiltinCall(E))
16910 return ExprEvaluatorBaseTy::VisitCallExpr(E);
16911 return VisitBuiltinCallExpr(E, BuiltinOp: ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E));
16912}
16913
16914static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info,
16915 APValue &Val, APSInt &Alignment) {
16916 QualType SrcTy = E->getArg(Arg: 0)->getType();
16917 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: SrcTy, Info, Alignment))
16918 return false;
16919 // Even though we are evaluating integer expressions we could get a pointer
16920 // argument for the __builtin_is_aligned() case.
16921 if (SrcTy->isPointerType()) {
16922 LValue Ptr;
16923 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Ptr, Info))
16924 return false;
16925 Ptr.moveInto(V&: Val);
16926 } else if (!SrcTy->isIntegralOrEnumerationType()) {
16927 Info.FFDiag(E: E->getArg(Arg: 0));
16928 return false;
16929 } else {
16930 APSInt SrcInt;
16931 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SrcInt, Info))
16932 return false;
16933 assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() &&
16934 "Bit widths must be the same");
16935 Val = APValue(SrcInt);
16936 }
16937 assert(Val.hasValue());
16938 return true;
16939}
16940
16941bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
16942 unsigned BuiltinOp) {
16943 auto EvalTestOp = [&](llvm::function_ref<bool(const APInt &, const APInt &)>
16944 Fn) {
16945 APValue SourceLHS, SourceRHS;
16946 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
16947 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
16948 return false;
16949
16950 unsigned SourceLen = SourceLHS.getVectorLength();
16951 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
16952 QualType ElemQT = VT->getElementType();
16953 unsigned LaneWidth = Info.Ctx.getTypeSize(T: ElemQT);
16954
16955 APInt AWide(LaneWidth * SourceLen, 0);
16956 APInt BWide(LaneWidth * SourceLen, 0);
16957
16958 for (unsigned I = 0; I != SourceLen; ++I) {
16959 APInt ALane;
16960 APInt BLane;
16961 if (ElemQT->isIntegerType()) { // Get value.
16962 ALane = SourceLHS.getVectorElt(I).getInt();
16963 BLane = SourceRHS.getVectorElt(I).getInt();
16964 } else if (ElemQT->isFloatingType()) { // Get only sign bit.
16965 ALane =
16966 SourceLHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
16967 BLane =
16968 SourceRHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
16969 } else { // Must be integer or floating type.
16970 return false;
16971 }
16972 AWide.insertBits(SubBits: ALane, bitPosition: I * LaneWidth);
16973 BWide.insertBits(SubBits: BLane, bitPosition: I * LaneWidth);
16974 }
16975 return Success(Value: Fn(AWide, BWide), E);
16976 };
16977
16978 auto HandleMaskBinOp =
16979 [&](llvm::function_ref<APSInt(const APSInt &, const APSInt &)> Fn)
16980 -> bool {
16981 APValue LHS, RHS;
16982 if (!Evaluate(Result&: LHS, Info, E: E->getArg(Arg: 0)) ||
16983 !Evaluate(Result&: RHS, Info, E: E->getArg(Arg: 1)))
16984 return false;
16985
16986 APSInt ResultInt = Fn(LHS.getInt(), RHS.getInt());
16987
16988 return Success(V: APValue(ResultInt), E);
16989 };
16990
16991 auto HandleCRC32 = [&](unsigned DataBytes) -> bool {
16992 APSInt CRC, Data;
16993 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CRC, Info) ||
16994 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Data, Info))
16995 return false;
16996
16997 uint64_t CRCVal = CRC.getZExtValue();
16998 uint64_t DataVal = Data.getZExtValue();
16999
17000 // CRC32C polynomial (iSCSI polynomial, bit-reversed)
17001 static const uint32_t CRC32C_POLY = 0x82F63B78;
17002
17003 // Process each byte
17004 uint32_t Result = static_cast<uint32_t>(CRCVal);
17005 for (unsigned I = 0; I != DataBytes; ++I) {
17006 uint8_t Byte = static_cast<uint8_t>((DataVal >> (I * 8)) & 0xFF);
17007 Result ^= Byte;
17008 for (int J = 0; J != 8; ++J) {
17009 Result = (Result >> 1) ^ ((Result & 1) ? CRC32C_POLY : 0);
17010 }
17011 }
17012
17013 return Success(Value: Result, E);
17014 };
17015
17016 switch (BuiltinOp) {
17017 default:
17018 return false;
17019
17020 case X86::BI__builtin_ia32_crc32qi:
17021 return HandleCRC32(1);
17022 case X86::BI__builtin_ia32_crc32hi:
17023 return HandleCRC32(2);
17024 case X86::BI__builtin_ia32_crc32si:
17025 return HandleCRC32(4);
17026 case X86::BI__builtin_ia32_crc32di:
17027 return HandleCRC32(8);
17028
17029 case Builtin::BI__builtin_dynamic_object_size:
17030 case Builtin::BI__builtin_object_size: {
17031 // The type was checked when we built the expression.
17032 unsigned Type =
17033 E->getArg(Arg: 1)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
17034 assert(Type <= 3 && "unexpected type");
17035
17036 bool IsDynamic = BuiltinOp == Builtin::BI__builtin_dynamic_object_size;
17037 if (std::optional<uint64_t> Size =
17038 tryEvaluateBuiltinObjectSize(E: E->getArg(Arg: 0), Type, Info, IsDynamic))
17039 return Success(Value: *Size, E);
17040
17041 if (E->getArg(Arg: 0)->HasSideEffects(Ctx: Info.Ctx))
17042 return Success(Value: (Type & 2) ? 0 : -1, E);
17043
17044 // Expression had no side effects, but we couldn't statically determine the
17045 // size of the referenced object.
17046 switch (Info.EvalMode) {
17047 case EvaluationMode::ConstantExpression:
17048 case EvaluationMode::ConstantFold:
17049 case EvaluationMode::IgnoreSideEffects:
17050 // Leave it to IR generation.
17051 return Error(E);
17052 case EvaluationMode::ConstantExpressionUnevaluated:
17053 // Reduce it to a constant now.
17054 return Success(Value: (Type & 2) ? 0 : -1, E);
17055 }
17056
17057 llvm_unreachable("unexpected EvalMode");
17058 }
17059
17060 case Builtin::BI__builtin_os_log_format_buffer_size: {
17061 analyze_os_log::OSLogBufferLayout Layout;
17062 analyze_os_log::computeOSLogBufferLayout(Ctx&: Info.Ctx, E, layout&: Layout);
17063 return Success(Value: Layout.size().getQuantity(), E);
17064 }
17065
17066 case Builtin::BI__builtin_is_aligned: {
17067 APValue Src;
17068 APSInt Alignment;
17069 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17070 return false;
17071 if (Src.isLValue()) {
17072 // If we evaluated a pointer, check the minimum known alignment.
17073 LValue Ptr;
17074 Ptr.setFrom(Ctx: Info.Ctx, V: Src);
17075 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Ptr);
17076 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Ptr.Offset);
17077 // We can return true if the known alignment at the computed offset is
17078 // greater than the requested alignment.
17079 assert(PtrAlign.isPowerOfTwo());
17080 assert(Alignment.isPowerOf2());
17081 if (PtrAlign.getQuantity() >= Alignment)
17082 return Success(Value: 1, E);
17083 // If the alignment is not known to be sufficient, some cases could still
17084 // be aligned at run time. However, if the requested alignment is less or
17085 // equal to the base alignment and the offset is not aligned, we know that
17086 // the run-time value can never be aligned.
17087 if (BaseAlignment.getQuantity() >= Alignment &&
17088 PtrAlign.getQuantity() < Alignment)
17089 return Success(Value: 0, E);
17090 // Otherwise we can't infer whether the value is sufficiently aligned.
17091 // TODO: __builtin_is_aligned(__builtin_align_{down,up{(expr, N), N)
17092 // in cases where we can't fully evaluate the pointer.
17093 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_compute)
17094 << Alignment;
17095 return false;
17096 }
17097 assert(Src.isInt());
17098 return Success(Value: (Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E);
17099 }
17100 case Builtin::BI__builtin_align_up: {
17101 APValue Src;
17102 APSInt Alignment;
17103 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17104 return false;
17105 if (!Src.isInt())
17106 return Error(E);
17107 APSInt AlignedVal =
17108 APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1),
17109 Src.getInt().isUnsigned());
17110 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
17111 return Success(SI: AlignedVal, E);
17112 }
17113 case Builtin::BI__builtin_align_down: {
17114 APValue Src;
17115 APSInt Alignment;
17116 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17117 return false;
17118 if (!Src.isInt())
17119 return Error(E);
17120 APSInt AlignedVal =
17121 APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned());
17122 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
17123 return Success(SI: AlignedVal, E);
17124 }
17125
17126 case Builtin::BI__builtin_bitreverseg:
17127 case Builtin::BI__builtin_bitreverse8:
17128 case Builtin::BI__builtin_bitreverse16:
17129 case Builtin::BI__builtin_bitreverse32:
17130 case Builtin::BI__builtin_bitreverse64:
17131 case Builtin::BI__builtin_elementwise_bitreverse: {
17132 APSInt Val;
17133 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17134 return false;
17135
17136 return Success(I: Val.reverseBits(), E);
17137 }
17138 case Builtin::BI__builtin_bswapg:
17139 case Builtin::BI__builtin_bswap16:
17140 case Builtin::BI__builtin_bswap32:
17141 case Builtin::BI__builtin_bswap64:
17142 case Builtin::BIstdc_memreverse8u8:
17143 case Builtin::BIstdc_memreverse8u16:
17144 case Builtin::BIstdc_memreverse8u32:
17145 case Builtin::BIstdc_memreverse8u64: {
17146 APSInt Val;
17147 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17148 return false;
17149 if (Val.getBitWidth() == 8 || Val.getBitWidth() == 1)
17150 return Success(SI: Val, E);
17151
17152 return Success(I: Val.byteSwap(), E);
17153 }
17154
17155 case Builtin::BI__builtin_classify_type:
17156 return Success(Value: (int)EvaluateBuiltinClassifyType(E, LangOpts: Info.getLangOpts()), E);
17157
17158 case Builtin::BI__builtin_clrsb:
17159 case Builtin::BI__builtin_clrsbl:
17160 case Builtin::BI__builtin_clrsbll: {
17161 APSInt Val;
17162 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17163 return false;
17164
17165 return Success(Value: Val.getBitWidth() - Val.getSignificantBits(), E);
17166 }
17167
17168 case Builtin::BI__builtin_clz:
17169 case Builtin::BI__builtin_clzl:
17170 case Builtin::BI__builtin_clzll:
17171 case Builtin::BI__builtin_clzs:
17172 case Builtin::BI__builtin_clzg:
17173 case Builtin::BI__builtin_elementwise_clzg:
17174 case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes
17175 case Builtin::BI__lzcnt:
17176 case Builtin::BI__lzcnt64: {
17177 APSInt Val;
17178 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17179 APValue Vec;
17180 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17181 return false;
17182 Val = ConvertBoolVectorToInt(Val: Vec);
17183 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17184 return false;
17185 }
17186
17187 std::optional<APSInt> Fallback;
17188 if ((BuiltinOp == Builtin::BI__builtin_clzg ||
17189 BuiltinOp == Builtin::BI__builtin_elementwise_clzg) &&
17190 E->getNumArgs() > 1) {
17191 APSInt FallbackTemp;
17192 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
17193 return false;
17194 Fallback = FallbackTemp;
17195 }
17196
17197 if (!Val) {
17198 if (Fallback)
17199 return Success(SI: *Fallback, E);
17200
17201 // When the argument is 0, the result of GCC builtins is undefined,
17202 // whereas for Microsoft intrinsics, the result is the bit-width of the
17203 // argument.
17204 bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 &&
17205 BuiltinOp != Builtin::BI__lzcnt &&
17206 BuiltinOp != Builtin::BI__lzcnt64;
17207
17208 if (BuiltinOp == Builtin::BI__builtin_elementwise_clzg) {
17209 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
17210 << /*IsTrailing=*/false;
17211 }
17212
17213 if (ZeroIsUndefined)
17214 return Error(E);
17215 }
17216
17217 return Success(Value: Val.countl_zero(), E);
17218 }
17219
17220 case Builtin::BI__builtin_constant_p: {
17221 const Expr *Arg = E->getArg(Arg: 0);
17222 if (EvaluateBuiltinConstantP(Info, Arg))
17223 return Success(Value: true, E);
17224 if (Info.InConstantContext || Arg->HasSideEffects(Ctx: Info.Ctx)) {
17225 // Outside a constant context, eagerly evaluate to false in the presence
17226 // of side-effects in order to avoid -Wunsequenced false-positives in
17227 // a branch on __builtin_constant_p(expr).
17228 return Success(Value: false, E);
17229 }
17230 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17231 return false;
17232 }
17233
17234 case Builtin::BI__noop:
17235 // __noop always evaluates successfully and returns 0.
17236 return Success(Value: 0, E);
17237
17238 case Builtin::BI__builtin_is_constant_evaluated: {
17239 const auto *Callee = Info.CurrentCall->getCallee();
17240 if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression &&
17241 (Info.CallStackDepth == 1 ||
17242 (Info.CallStackDepth == 2 && Callee->isInStdNamespace() &&
17243 Callee->getIdentifier() &&
17244 Callee->getIdentifier()->isStr(Str: "is_constant_evaluated")))) {
17245 // FIXME: Find a better way to avoid duplicated diagnostics.
17246 if (Info.EvalStatus.Diag)
17247 Info.report(Loc: (Info.CallStackDepth == 1)
17248 ? E->getExprLoc()
17249 : Info.CurrentCall->getCallRange().getBegin(),
17250 DiagId: diag::warn_is_constant_evaluated_always_true_constexpr)
17251 << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated"
17252 : "std::is_constant_evaluated");
17253 }
17254
17255 return Success(Value: Info.InConstantContext, E);
17256 }
17257
17258 case Builtin::BI__builtin_is_within_lifetime:
17259 if (auto result = EvaluateBuiltinIsWithinLifetime(*this, E))
17260 return Success(Value: *result, E);
17261 return false;
17262
17263 case Builtin::BI__builtin_ctz:
17264 case Builtin::BI__builtin_ctzl:
17265 case Builtin::BI__builtin_ctzll:
17266 case Builtin::BI__builtin_ctzs:
17267 case Builtin::BI__builtin_ctzg:
17268 case Builtin::BI__builtin_elementwise_ctzg: {
17269 APSInt Val;
17270 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17271 APValue Vec;
17272 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17273 return false;
17274 Val = ConvertBoolVectorToInt(Val: Vec);
17275 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17276 return false;
17277 }
17278
17279 std::optional<APSInt> Fallback;
17280 if ((BuiltinOp == Builtin::BI__builtin_ctzg ||
17281 BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) &&
17282 E->getNumArgs() > 1) {
17283 APSInt FallbackTemp;
17284 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
17285 return false;
17286 Fallback = FallbackTemp;
17287 }
17288
17289 if (!Val) {
17290 if (Fallback)
17291 return Success(SI: *Fallback, E);
17292
17293 if (BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) {
17294 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
17295 << /*IsTrailing=*/true;
17296 }
17297 return Error(E);
17298 }
17299
17300 return Success(Value: Val.countr_zero(), E);
17301 }
17302
17303 case Builtin::BI__builtin_eh_return_data_regno: {
17304 int Operand = E->getArg(Arg: 0)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
17305 Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(RegNo: Operand);
17306 return Success(Value: Operand, E);
17307 }
17308
17309 case Builtin::BI__builtin_elementwise_abs: {
17310 APSInt Val;
17311 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17312 return false;
17313
17314 return Success(I: Val.abs(), E);
17315 }
17316
17317 case Builtin::BI__builtin_expect:
17318 case Builtin::BI__builtin_expect_with_probability:
17319 return Visit(S: E->getArg(Arg: 0));
17320
17321 case Builtin::BI__builtin_ptrauth_string_discriminator: {
17322 const auto *Literal =
17323 cast<StringLiteral>(Val: E->getArg(Arg: 0)->IgnoreParenImpCasts());
17324 uint64_t Result = getPointerAuthStableSipHash(S: Literal->getString());
17325 return Success(Value: Result, E);
17326 }
17327
17328 case Builtin::BI__builtin_infer_alloc_token: {
17329 // If we fail to infer a type, this fails to be a constant expression; this
17330 // can be checked with __builtin_constant_p(...).
17331 QualType AllocType = infer_alloc::inferPossibleType(E, Ctx: Info.Ctx, CastE: nullptr);
17332 if (AllocType.isNull())
17333 return Error(
17334 E, D: diag::note_constexpr_infer_alloc_token_type_inference_failed);
17335 auto ATMD = infer_alloc::getAllocTokenMetadata(T: AllocType, Ctx: Info.Ctx);
17336 if (!ATMD)
17337 return Error(E, D: diag::note_constexpr_infer_alloc_token_no_metadata);
17338 auto Mode =
17339 Info.getLangOpts().AllocTokenMode.value_or(u: llvm::DefaultAllocTokenMode);
17340 uint64_t BitWidth = Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType());
17341 auto MaxTokensOpt = Info.getLangOpts().AllocTokenMax;
17342 uint64_t MaxTokens =
17343 MaxTokensOpt.value_or(u: 0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
17344 auto MaybeToken = llvm::getAllocToken(Mode, Metadata: *ATMD, MaxTokens);
17345 if (!MaybeToken)
17346 return Error(E, D: diag::note_constexpr_infer_alloc_token_stateful_mode);
17347 return Success(I: llvm::APInt(BitWidth, *MaybeToken), E);
17348 }
17349
17350 case Builtin::BI__builtin_ffs:
17351 case Builtin::BI__builtin_ffsl:
17352 case Builtin::BI__builtin_ffsll: {
17353 APSInt Val;
17354 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17355 return false;
17356
17357 unsigned N = Val.countr_zero();
17358 return Success(Value: N == Val.getBitWidth() ? 0 : N + 1, E);
17359 }
17360
17361 case Builtin::BI__builtin_fpclassify: {
17362 APFloat Val(0.0);
17363 if (!EvaluateFloat(E: E->getArg(Arg: 5), Result&: Val, Info))
17364 return false;
17365 unsigned Arg;
17366 switch (Val.getCategory()) {
17367 case APFloat::fcNaN: Arg = 0; break;
17368 case APFloat::fcInfinity: Arg = 1; break;
17369 case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break;
17370 case APFloat::fcZero: Arg = 4; break;
17371 }
17372 return Visit(S: E->getArg(Arg));
17373 }
17374
17375 case Builtin::BI__builtin_isinf_sign: {
17376 APFloat Val(0.0);
17377 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17378 Success(Value: Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E);
17379 }
17380
17381 case Builtin::BI__builtin_isinf: {
17382 APFloat Val(0.0);
17383 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17384 Success(Value: Val.isInfinity() ? 1 : 0, E);
17385 }
17386
17387 case Builtin::BI__builtin_isfinite: {
17388 APFloat Val(0.0);
17389 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17390 Success(Value: Val.isFinite() ? 1 : 0, E);
17391 }
17392
17393 case Builtin::BI__builtin_isnan: {
17394 APFloat Val(0.0);
17395 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17396 Success(Value: Val.isNaN() ? 1 : 0, E);
17397 }
17398
17399 case Builtin::BI__builtin_isnormal: {
17400 APFloat Val(0.0);
17401 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17402 Success(Value: Val.isNormal() ? 1 : 0, E);
17403 }
17404
17405 case Builtin::BI__builtin_issubnormal: {
17406 APFloat Val(0.0);
17407 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17408 Success(Value: Val.isDenormal() ? 1 : 0, E);
17409 }
17410
17411 case Builtin::BI__builtin_iszero: {
17412 APFloat Val(0.0);
17413 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17414 Success(Value: Val.isZero() ? 1 : 0, E);
17415 }
17416
17417 case Builtin::BI__builtin_signbit:
17418 case Builtin::BI__builtin_signbitf:
17419 case Builtin::BI__builtin_signbitl: {
17420 APFloat Val(0.0);
17421 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17422 Success(Value: Val.isNegative() ? 1 : 0, E);
17423 }
17424
17425 case Builtin::BI__builtin_isgreater:
17426 case Builtin::BI__builtin_isgreaterequal:
17427 case Builtin::BI__builtin_isless:
17428 case Builtin::BI__builtin_islessequal:
17429 case Builtin::BI__builtin_islessgreater:
17430 case Builtin::BI__builtin_isunordered: {
17431 APFloat LHS(0.0);
17432 APFloat RHS(0.0);
17433 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17434 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
17435 return false;
17436
17437 return Success(
17438 Value: [&] {
17439 switch (BuiltinOp) {
17440 case Builtin::BI__builtin_isgreater:
17441 return LHS > RHS;
17442 case Builtin::BI__builtin_isgreaterequal:
17443 return LHS >= RHS;
17444 case Builtin::BI__builtin_isless:
17445 return LHS < RHS;
17446 case Builtin::BI__builtin_islessequal:
17447 return LHS <= RHS;
17448 case Builtin::BI__builtin_islessgreater: {
17449 APFloat::cmpResult cmp = LHS.compare(RHS);
17450 return cmp == APFloat::cmpResult::cmpLessThan ||
17451 cmp == APFloat::cmpResult::cmpGreaterThan;
17452 }
17453 case Builtin::BI__builtin_isunordered:
17454 return LHS.compare(RHS) == APFloat::cmpResult::cmpUnordered;
17455 default:
17456 llvm_unreachable("Unexpected builtin ID: Should be a floating "
17457 "point comparison function");
17458 }
17459 }()
17460 ? 1
17461 : 0,
17462 E);
17463 }
17464
17465 case Builtin::BI__builtin_issignaling: {
17466 APFloat Val(0.0);
17467 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17468 Success(Value: Val.isSignaling() ? 1 : 0, E);
17469 }
17470
17471 case Builtin::BI__builtin_isfpclass: {
17472 APSInt MaskVal;
17473 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: MaskVal, Info))
17474 return false;
17475 unsigned Test = static_cast<llvm::FPClassTest>(MaskVal.getZExtValue());
17476 APFloat Val(0.0);
17477 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17478 Success(Value: (Val.classify() & Test) ? 1 : 0, E);
17479 }
17480
17481 case Builtin::BI__builtin_parity:
17482 case Builtin::BI__builtin_parityl:
17483 case Builtin::BI__builtin_parityll: {
17484 APSInt Val;
17485 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17486 return false;
17487
17488 return Success(Value: Val.popcount() % 2, E);
17489 }
17490
17491 case Builtin::BI__builtin_abs:
17492 case Builtin::BI__builtin_labs:
17493 case Builtin::BI__builtin_llabs: {
17494 APSInt Val;
17495 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17496 return false;
17497 if (Val == APSInt(APInt::getSignedMinValue(numBits: Val.getBitWidth()),
17498 /*IsUnsigned=*/false))
17499 return false;
17500 if (Val.isNegative())
17501 Val.negate();
17502 return Success(SI: Val, E);
17503 }
17504
17505 case Builtin::BI__builtin_popcount:
17506 case Builtin::BI__builtin_popcountl:
17507 case Builtin::BI__builtin_popcountll:
17508 case Builtin::BI__builtin_popcountg:
17509 case Builtin::BI__builtin_elementwise_popcount:
17510 case Builtin::BI__popcnt16: // Microsoft variants of popcount
17511 case Builtin::BI__popcnt:
17512 case Builtin::BI__popcnt64: {
17513 APSInt Val;
17514 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17515 APValue Vec;
17516 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17517 return false;
17518 Val = ConvertBoolVectorToInt(Val: Vec);
17519 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17520 return false;
17521 }
17522
17523 return Success(Value: Val.popcount(), E);
17524 }
17525
17526 case Builtin::BI__builtin_rotateleft8:
17527 case Builtin::BI__builtin_rotateleft16:
17528 case Builtin::BI__builtin_rotateleft32:
17529 case Builtin::BI__builtin_rotateleft64:
17530 case Builtin::BI__builtin_rotateright8:
17531 case Builtin::BI__builtin_rotateright16:
17532 case Builtin::BI__builtin_rotateright32:
17533 case Builtin::BI__builtin_rotateright64:
17534 case Builtin::BI__builtin_stdc_rotate_left:
17535 case Builtin::BI__builtin_stdc_rotate_right:
17536 case Builtin::BIstdc_rotate_left_uc:
17537 case Builtin::BIstdc_rotate_left_us:
17538 case Builtin::BIstdc_rotate_left_ui:
17539 case Builtin::BIstdc_rotate_left_ul:
17540 case Builtin::BIstdc_rotate_left_ull:
17541 case Builtin::BIstdc_rotate_right_uc:
17542 case Builtin::BIstdc_rotate_right_us:
17543 case Builtin::BIstdc_rotate_right_ui:
17544 case Builtin::BIstdc_rotate_right_ul:
17545 case Builtin::BIstdc_rotate_right_ull:
17546 case Builtin::BI_rotl8: // Microsoft variants of rotate left
17547 case Builtin::BI_rotl16:
17548 case Builtin::BI_rotl:
17549 case Builtin::BI_lrotl:
17550 case Builtin::BI_rotl64:
17551 case Builtin::BI_rotr8: // Microsoft variants of rotate right
17552 case Builtin::BI_rotr16:
17553 case Builtin::BI_rotr:
17554 case Builtin::BI_lrotr:
17555 case Builtin::BI_rotr64: {
17556 APSInt Value, Amount;
17557 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Value, Info) ||
17558 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amount, Info))
17559 return false;
17560
17561 Amount = NormalizeRotateAmount(Value, Amount);
17562
17563 switch (BuiltinOp) {
17564 case Builtin::BI__builtin_rotateright8:
17565 case Builtin::BI__builtin_rotateright16:
17566 case Builtin::BI__builtin_rotateright32:
17567 case Builtin::BI__builtin_rotateright64:
17568 case Builtin::BI__builtin_stdc_rotate_right:
17569 case Builtin::BIstdc_rotate_right_uc:
17570 case Builtin::BIstdc_rotate_right_us:
17571 case Builtin::BIstdc_rotate_right_ui:
17572 case Builtin::BIstdc_rotate_right_ul:
17573 case Builtin::BIstdc_rotate_right_ull:
17574 case Builtin::BI_rotr8:
17575 case Builtin::BI_rotr16:
17576 case Builtin::BI_rotr:
17577 case Builtin::BI_lrotr:
17578 case Builtin::BI_rotr64:
17579 return Success(
17580 SI: APSInt(Value.rotr(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17581 default:
17582 return Success(
17583 SI: APSInt(Value.rotl(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17584 }
17585 }
17586
17587 case Builtin::BIstdc_leading_zeros_uc:
17588 case Builtin::BIstdc_leading_zeros_us:
17589 case Builtin::BIstdc_leading_zeros_ui:
17590 case Builtin::BIstdc_leading_zeros_ul:
17591 case Builtin::BIstdc_leading_zeros_ull:
17592 case Builtin::BIstdc_leading_ones_uc:
17593 case Builtin::BIstdc_leading_ones_us:
17594 case Builtin::BIstdc_leading_ones_ui:
17595 case Builtin::BIstdc_leading_ones_ul:
17596 case Builtin::BIstdc_leading_ones_ull:
17597 case Builtin::BIstdc_trailing_zeros_uc:
17598 case Builtin::BIstdc_trailing_zeros_us:
17599 case Builtin::BIstdc_trailing_zeros_ui:
17600 case Builtin::BIstdc_trailing_zeros_ul:
17601 case Builtin::BIstdc_trailing_zeros_ull:
17602 case Builtin::BIstdc_trailing_ones_uc:
17603 case Builtin::BIstdc_trailing_ones_us:
17604 case Builtin::BIstdc_trailing_ones_ui:
17605 case Builtin::BIstdc_trailing_ones_ul:
17606 case Builtin::BIstdc_trailing_ones_ull:
17607 case Builtin::BIstdc_first_leading_zero_uc:
17608 case Builtin::BIstdc_first_leading_zero_us:
17609 case Builtin::BIstdc_first_leading_zero_ui:
17610 case Builtin::BIstdc_first_leading_zero_ul:
17611 case Builtin::BIstdc_first_leading_zero_ull:
17612 case Builtin::BIstdc_first_leading_one_uc:
17613 case Builtin::BIstdc_first_leading_one_us:
17614 case Builtin::BIstdc_first_leading_one_ui:
17615 case Builtin::BIstdc_first_leading_one_ul:
17616 case Builtin::BIstdc_first_leading_one_ull:
17617 case Builtin::BIstdc_first_trailing_zero_uc:
17618 case Builtin::BIstdc_first_trailing_zero_us:
17619 case Builtin::BIstdc_first_trailing_zero_ui:
17620 case Builtin::BIstdc_first_trailing_zero_ul:
17621 case Builtin::BIstdc_first_trailing_zero_ull:
17622 case Builtin::BIstdc_first_trailing_one_uc:
17623 case Builtin::BIstdc_first_trailing_one_us:
17624 case Builtin::BIstdc_first_trailing_one_ui:
17625 case Builtin::BIstdc_first_trailing_one_ul:
17626 case Builtin::BIstdc_first_trailing_one_ull:
17627 case Builtin::BIstdc_count_zeros_uc:
17628 case Builtin::BIstdc_count_zeros_us:
17629 case Builtin::BIstdc_count_zeros_ui:
17630 case Builtin::BIstdc_count_zeros_ul:
17631 case Builtin::BIstdc_count_zeros_ull:
17632 case Builtin::BIstdc_count_ones_uc:
17633 case Builtin::BIstdc_count_ones_us:
17634 case Builtin::BIstdc_count_ones_ui:
17635 case Builtin::BIstdc_count_ones_ul:
17636 case Builtin::BIstdc_count_ones_ull:
17637 case Builtin::BIstdc_has_single_bit_uc:
17638 case Builtin::BIstdc_has_single_bit_us:
17639 case Builtin::BIstdc_has_single_bit_ui:
17640 case Builtin::BIstdc_has_single_bit_ul:
17641 case Builtin::BIstdc_has_single_bit_ull:
17642 case Builtin::BIstdc_bit_width_uc:
17643 case Builtin::BIstdc_bit_width_us:
17644 case Builtin::BIstdc_bit_width_ui:
17645 case Builtin::BIstdc_bit_width_ul:
17646 case Builtin::BIstdc_bit_width_ull:
17647 case Builtin::BIstdc_bit_floor_uc:
17648 case Builtin::BIstdc_bit_floor_us:
17649 case Builtin::BIstdc_bit_floor_ui:
17650 case Builtin::BIstdc_bit_floor_ul:
17651 case Builtin::BIstdc_bit_floor_ull:
17652 case Builtin::BIstdc_bit_ceil_uc:
17653 case Builtin::BIstdc_bit_ceil_us:
17654 case Builtin::BIstdc_bit_ceil_ui:
17655 case Builtin::BIstdc_bit_ceil_ul:
17656 case Builtin::BIstdc_bit_ceil_ull:
17657 case Builtin::BI__builtin_stdc_leading_zeros:
17658 case Builtin::BI__builtin_stdc_leading_ones:
17659 case Builtin::BI__builtin_stdc_trailing_zeros:
17660 case Builtin::BI__builtin_stdc_trailing_ones:
17661 case Builtin::BI__builtin_stdc_first_leading_zero:
17662 case Builtin::BI__builtin_stdc_first_leading_one:
17663 case Builtin::BI__builtin_stdc_first_trailing_zero:
17664 case Builtin::BI__builtin_stdc_first_trailing_one:
17665 case Builtin::BI__builtin_stdc_count_zeros:
17666 case Builtin::BI__builtin_stdc_count_ones:
17667 case Builtin::BI__builtin_stdc_has_single_bit:
17668 case Builtin::BI__builtin_stdc_bit_width:
17669 case Builtin::BI__builtin_stdc_bit_floor:
17670 case Builtin::BI__builtin_stdc_bit_ceil: {
17671 APSInt Val;
17672 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17673 return false;
17674
17675 unsigned BitWidth = Val.getBitWidth();
17676 const unsigned ResBitWidth = Info.Ctx.getIntWidth(T: E->getType());
17677
17678 switch (BuiltinOp) {
17679 case Builtin::BIstdc_leading_zeros_uc:
17680 case Builtin::BIstdc_leading_zeros_us:
17681 case Builtin::BIstdc_leading_zeros_ui:
17682 case Builtin::BIstdc_leading_zeros_ul:
17683 case Builtin::BIstdc_leading_zeros_ull:
17684 case Builtin::BI__builtin_stdc_leading_zeros:
17685 return Success(I: APInt(ResBitWidth, Val.countl_zero()), E);
17686 case Builtin::BIstdc_leading_ones_uc:
17687 case Builtin::BIstdc_leading_ones_us:
17688 case Builtin::BIstdc_leading_ones_ui:
17689 case Builtin::BIstdc_leading_ones_ul:
17690 case Builtin::BIstdc_leading_ones_ull:
17691 case Builtin::BI__builtin_stdc_leading_ones:
17692 return Success(I: APInt(ResBitWidth, Val.countl_one()), E);
17693 case Builtin::BIstdc_trailing_zeros_uc:
17694 case Builtin::BIstdc_trailing_zeros_us:
17695 case Builtin::BIstdc_trailing_zeros_ui:
17696 case Builtin::BIstdc_trailing_zeros_ul:
17697 case Builtin::BIstdc_trailing_zeros_ull:
17698 case Builtin::BI__builtin_stdc_trailing_zeros:
17699 return Success(I: APInt(ResBitWidth, Val.countr_zero()), E);
17700 case Builtin::BIstdc_trailing_ones_uc:
17701 case Builtin::BIstdc_trailing_ones_us:
17702 case Builtin::BIstdc_trailing_ones_ui:
17703 case Builtin::BIstdc_trailing_ones_ul:
17704 case Builtin::BIstdc_trailing_ones_ull:
17705 case Builtin::BI__builtin_stdc_trailing_ones:
17706 return Success(I: APInt(ResBitWidth, Val.countr_one()), E);
17707 case Builtin::BIstdc_first_leading_zero_uc:
17708 case Builtin::BIstdc_first_leading_zero_us:
17709 case Builtin::BIstdc_first_leading_zero_ui:
17710 case Builtin::BIstdc_first_leading_zero_ul:
17711 case Builtin::BIstdc_first_leading_zero_ull:
17712 case Builtin::BI__builtin_stdc_first_leading_zero:
17713 return Success(
17714 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countl_one() + 1), E);
17715 case Builtin::BIstdc_first_leading_one_uc:
17716 case Builtin::BIstdc_first_leading_one_us:
17717 case Builtin::BIstdc_first_leading_one_ui:
17718 case Builtin::BIstdc_first_leading_one_ul:
17719 case Builtin::BIstdc_first_leading_one_ull:
17720 case Builtin::BI__builtin_stdc_first_leading_one:
17721 return Success(
17722 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countl_zero() + 1), E);
17723 case Builtin::BIstdc_first_trailing_zero_uc:
17724 case Builtin::BIstdc_first_trailing_zero_us:
17725 case Builtin::BIstdc_first_trailing_zero_ui:
17726 case Builtin::BIstdc_first_trailing_zero_ul:
17727 case Builtin::BIstdc_first_trailing_zero_ull:
17728 case Builtin::BI__builtin_stdc_first_trailing_zero:
17729 return Success(
17730 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countr_one() + 1), E);
17731 case Builtin::BIstdc_first_trailing_one_uc:
17732 case Builtin::BIstdc_first_trailing_one_us:
17733 case Builtin::BIstdc_first_trailing_one_ui:
17734 case Builtin::BIstdc_first_trailing_one_ul:
17735 case Builtin::BIstdc_first_trailing_one_ull:
17736 case Builtin::BI__builtin_stdc_first_trailing_one:
17737 return Success(
17738 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countr_zero() + 1), E);
17739 case Builtin::BIstdc_count_zeros_uc:
17740 case Builtin::BIstdc_count_zeros_us:
17741 case Builtin::BIstdc_count_zeros_ui:
17742 case Builtin::BIstdc_count_zeros_ul:
17743 case Builtin::BIstdc_count_zeros_ull:
17744 case Builtin::BI__builtin_stdc_count_zeros: {
17745 APInt Cnt(ResBitWidth, BitWidth - Val.popcount());
17746 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
17747 }
17748 case Builtin::BIstdc_count_ones_uc:
17749 case Builtin::BIstdc_count_ones_us:
17750 case Builtin::BIstdc_count_ones_ui:
17751 case Builtin::BIstdc_count_ones_ul:
17752 case Builtin::BIstdc_count_ones_ull:
17753 case Builtin::BI__builtin_stdc_count_ones: {
17754 APInt Cnt(ResBitWidth, Val.popcount());
17755 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
17756 }
17757 case Builtin::BIstdc_has_single_bit_uc:
17758 case Builtin::BIstdc_has_single_bit_us:
17759 case Builtin::BIstdc_has_single_bit_ui:
17760 case Builtin::BIstdc_has_single_bit_ul:
17761 case Builtin::BIstdc_has_single_bit_ull:
17762 case Builtin::BI__builtin_stdc_has_single_bit: {
17763 APInt Res(ResBitWidth, Val.popcount() == 1 ? 1 : 0);
17764 return Success(SI: APSInt(Res, /*IsUnsigned*/ true), E);
17765 }
17766 case Builtin::BIstdc_bit_width_uc:
17767 case Builtin::BIstdc_bit_width_us:
17768 case Builtin::BIstdc_bit_width_ui:
17769 case Builtin::BIstdc_bit_width_ul:
17770 case Builtin::BIstdc_bit_width_ull:
17771 case Builtin::BI__builtin_stdc_bit_width:
17772 return Success(I: APInt(ResBitWidth, BitWidth - Val.countl_zero()), E);
17773 case Builtin::BIstdc_bit_floor_uc:
17774 case Builtin::BIstdc_bit_floor_us:
17775 case Builtin::BIstdc_bit_floor_ui:
17776 case Builtin::BIstdc_bit_floor_ul:
17777 case Builtin::BIstdc_bit_floor_ull:
17778 case Builtin::BI__builtin_stdc_bit_floor: {
17779 if (Val.isZero())
17780 return Success(I: APInt(BitWidth, 0), E);
17781 unsigned Exp = BitWidth - Val.countl_zero() - 1;
17782 return Success(
17783 SI: APSInt(APInt::getOneBitSet(numBits: BitWidth, BitNo: Exp), /*IsUnsigned*/ true), E);
17784 }
17785 case Builtin::BIstdc_bit_ceil_uc:
17786 case Builtin::BIstdc_bit_ceil_us:
17787 case Builtin::BIstdc_bit_ceil_ui:
17788 case Builtin::BIstdc_bit_ceil_ul:
17789 case Builtin::BIstdc_bit_ceil_ull:
17790 case Builtin::BI__builtin_stdc_bit_ceil: {
17791 if (Val.ule(RHS: 1))
17792 return Success(SI: APSInt(APInt(BitWidth, 1), /*IsUnsigned*/ true), E);
17793 APInt ValMinusOne = Val - 1;
17794 unsigned LZ = ValMinusOne.countl_zero();
17795 if (LZ == 0)
17796 return Success(SI: APSInt(APInt(BitWidth, 0), /*IsUnsigned*/ true),
17797 E); // overflows; wrap to 0
17798 APInt Result = APInt::getOneBitSet(numBits: BitWidth, BitNo: BitWidth - LZ);
17799 return Success(SI: APSInt(Result, /*IsUnsigned*/ true), E);
17800 }
17801 default:
17802 llvm_unreachable("Unknown stdc builtin");
17803 }
17804 }
17805
17806 case Builtin::BI__builtin_elementwise_add_sat: {
17807 APSInt LHS, RHS;
17808 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17809 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17810 return false;
17811
17812 APInt Result = LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
17813 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17814 }
17815 case Builtin::BI__builtin_elementwise_sub_sat: {
17816 APSInt LHS, RHS;
17817 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17818 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17819 return false;
17820
17821 APInt Result = LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
17822 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17823 }
17824 case Builtin::BI__builtin_elementwise_max: {
17825 APSInt LHS, RHS;
17826 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17827 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17828 return false;
17829
17830 APInt Result = std::max(a: LHS, b: RHS);
17831 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17832 }
17833 case Builtin::BI__builtin_elementwise_min: {
17834 APSInt LHS, RHS;
17835 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17836 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17837 return false;
17838
17839 APInt Result = std::min(a: LHS, b: RHS);
17840 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17841 }
17842 case Builtin::BI__builtin_elementwise_clmul: {
17843 APSInt LHS, RHS;
17844 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17845 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17846 return false;
17847
17848 APInt Result = llvm::APIntOps::clmul(LHS, RHS);
17849 return Success(SI: APSInt(Result, LHS.isUnsigned()), E);
17850 }
17851 case Builtin::BI__builtin_elementwise_fshl:
17852 case Builtin::BI__builtin_elementwise_fshr: {
17853 APSInt Hi, Lo, Shift;
17854 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Hi, Info) ||
17855 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Lo, Info) ||
17856 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Shift, Info))
17857 return false;
17858
17859 switch (BuiltinOp) {
17860 case Builtin::BI__builtin_elementwise_fshl: {
17861 APSInt Result(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned());
17862 return Success(SI: Result, E);
17863 }
17864 case Builtin::BI__builtin_elementwise_fshr: {
17865 APSInt Result(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned());
17866 return Success(SI: Result, E);
17867 }
17868 }
17869 llvm_unreachable("Fully covered switch above");
17870 }
17871 case Builtin::BIstrlen:
17872 case Builtin::BIwcslen:
17873 // A call to strlen is not a constant expression.
17874 if (Info.getLangOpts().CPlusPlus11)
17875 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
17876 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
17877 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
17878 else
17879 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17880 [[fallthrough]];
17881 case Builtin::BI__builtin_strlen:
17882 case Builtin::BI__builtin_wcslen: {
17883 // As an extension, we support __builtin_strlen() as a constant expression,
17884 // and support folding strlen() to a constant.
17885 if (std::optional<uint64_t> StrLen =
17886 EvaluateBuiltinStrLen(E: E->getArg(Arg: 0), Info))
17887 return Success(Value: *StrLen, E);
17888 return false;
17889 }
17890
17891 case Builtin::BIstrcmp:
17892 case Builtin::BIwcscmp:
17893 case Builtin::BIstrncmp:
17894 case Builtin::BIwcsncmp:
17895 case Builtin::BImemcmp:
17896 case Builtin::BIbcmp:
17897 case Builtin::BIwmemcmp:
17898 // A call to strlen is not a constant expression.
17899 if (Info.getLangOpts().CPlusPlus11)
17900 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
17901 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
17902 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
17903 else
17904 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17905 [[fallthrough]];
17906 case Builtin::BI__builtin_strcmp:
17907 case Builtin::BI__builtin_wcscmp:
17908 case Builtin::BI__builtin_strncmp:
17909 case Builtin::BI__builtin_wcsncmp:
17910 case Builtin::BI__builtin_memcmp:
17911 case Builtin::BI__builtin_bcmp:
17912 case Builtin::BI__builtin_wmemcmp: {
17913 LValue String1, String2;
17914 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: String1, Info) ||
17915 !EvaluatePointer(E: E->getArg(Arg: 1), Result&: String2, Info))
17916 return false;
17917
17918 uint64_t MaxLength = uint64_t(-1);
17919 if (BuiltinOp != Builtin::BIstrcmp &&
17920 BuiltinOp != Builtin::BIwcscmp &&
17921 BuiltinOp != Builtin::BI__builtin_strcmp &&
17922 BuiltinOp != Builtin::BI__builtin_wcscmp) {
17923 APSInt N;
17924 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
17925 return false;
17926 MaxLength = N.getZExtValue();
17927 }
17928
17929 // Empty substrings compare equal by definition.
17930 if (MaxLength == 0u)
17931 return Success(Value: 0, E);
17932
17933 if (!String1.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
17934 !String2.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
17935 String1.Designator.Invalid || String2.Designator.Invalid)
17936 return false;
17937
17938 QualType CharTy1 = String1.Designator.getType(Ctx&: Info.Ctx);
17939 QualType CharTy2 = String2.Designator.getType(Ctx&: Info.Ctx);
17940
17941 bool IsRawByte = BuiltinOp == Builtin::BImemcmp ||
17942 BuiltinOp == Builtin::BIbcmp ||
17943 BuiltinOp == Builtin::BI__builtin_memcmp ||
17944 BuiltinOp == Builtin::BI__builtin_bcmp;
17945
17946 assert(IsRawByte ||
17947 (Info.Ctx.hasSameUnqualifiedType(
17948 CharTy1, E->getArg(0)->getType()->getPointeeType()) &&
17949 Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2)));
17950
17951 // For memcmp, allow comparing any arrays of '[[un]signed] char' or
17952 // 'char8_t', but no other types.
17953 if (IsRawByte &&
17954 !(isOneByteCharacterType(T: CharTy1) && isOneByteCharacterType(T: CharTy2))) {
17955 // FIXME: Consider using our bit_cast implementation to support this.
17956 Info.FFDiag(E, DiagId: diag::note_constexpr_memcmp_unsupported)
17957 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy1
17958 << CharTy2;
17959 return false;
17960 }
17961
17962 const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) {
17963 return handleLValueToRValueConversion(Info, Conv: E, Type: CharTy1, LVal: String1, RVal&: Char1) &&
17964 handleLValueToRValueConversion(Info, Conv: E, Type: CharTy2, LVal: String2, RVal&: Char2) &&
17965 Char1.isInt() && Char2.isInt();
17966 };
17967 const auto &AdvanceElems = [&] {
17968 return HandleLValueArrayAdjustment(Info, E, LVal&: String1, EltTy: CharTy1, Adjustment: 1) &&
17969 HandleLValueArrayAdjustment(Info, E, LVal&: String2, EltTy: CharTy2, Adjustment: 1);
17970 };
17971
17972 bool StopAtNull =
17973 (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp &&
17974 BuiltinOp != Builtin::BIwmemcmp &&
17975 BuiltinOp != Builtin::BI__builtin_memcmp &&
17976 BuiltinOp != Builtin::BI__builtin_bcmp &&
17977 BuiltinOp != Builtin::BI__builtin_wmemcmp);
17978 bool IsWide = BuiltinOp == Builtin::BIwcscmp ||
17979 BuiltinOp == Builtin::BIwcsncmp ||
17980 BuiltinOp == Builtin::BIwmemcmp ||
17981 BuiltinOp == Builtin::BI__builtin_wcscmp ||
17982 BuiltinOp == Builtin::BI__builtin_wcsncmp ||
17983 BuiltinOp == Builtin::BI__builtin_wmemcmp;
17984
17985 for (; MaxLength; --MaxLength) {
17986 APValue Char1, Char2;
17987 if (!ReadCurElems(Char1, Char2))
17988 return false;
17989 if (Char1.getInt().ne(RHS: Char2.getInt())) {
17990 if (IsWide) // wmemcmp compares with wchar_t signedness.
17991 return Success(Value: Char1.getInt() < Char2.getInt() ? -1 : 1, E);
17992 // memcmp always compares unsigned chars.
17993 return Success(Value: Char1.getInt().ult(RHS: Char2.getInt()) ? -1 : 1, E);
17994 }
17995 if (StopAtNull && !Char1.getInt())
17996 return Success(Value: 0, E);
17997 assert(!(StopAtNull && !Char2.getInt()));
17998 if (!AdvanceElems())
17999 return false;
18000 }
18001 // We hit the strncmp / memcmp limit.
18002 return Success(Value: 0, E);
18003 }
18004
18005 case Builtin::BI__atomic_always_lock_free:
18006 case Builtin::BI__atomic_is_lock_free:
18007 case Builtin::BI__c11_atomic_is_lock_free: {
18008 APSInt SizeVal;
18009 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SizeVal, Info))
18010 return false;
18011
18012 // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
18013 // of two less than or equal to the maximum inline atomic width, we know it
18014 // is lock-free. If the size isn't a power of two, or greater than the
18015 // maximum alignment where we promote atomics, we know it is not lock-free
18016 // (at least not in the sense of atomic_is_lock_free). Otherwise,
18017 // the answer can only be determined at runtime; for example, 16-byte
18018 // atomics have lock-free implementations on some, but not all,
18019 // x86-64 processors.
18020
18021 // Check power-of-two.
18022 CharUnits Size = CharUnits::fromQuantity(Quantity: SizeVal.getZExtValue());
18023 if (Size.isPowerOfTwo()) {
18024 // Check against inlining width.
18025 unsigned InlineWidthBits =
18026 Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
18027 if (Size <= Info.Ctx.toCharUnitsFromBits(BitSize: InlineWidthBits)) {
18028 if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||
18029 Size == CharUnits::One())
18030 return Success(Value: 1, E);
18031
18032 // If the pointer argument can be evaluated to a compile-time constant
18033 // integer (or nullptr), check if that value is appropriately aligned.
18034 const Expr *PtrArg = E->getArg(Arg: 1);
18035 Expr::EvalResult ExprResult;
18036 APSInt IntResult;
18037 if (PtrArg->EvaluateAsRValue(Result&: ExprResult, Ctx: Info.Ctx) &&
18038 ExprResult.Val.toIntegralConstant(Result&: IntResult, SrcTy: PtrArg->getType(),
18039 Ctx: Info.Ctx) &&
18040 IntResult.isAligned(A: Size.getAsAlign()))
18041 return Success(Value: 1, E);
18042
18043 // Otherwise, check if the type's alignment against Size.
18044 if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: PtrArg)) {
18045 // Drop the potential implicit-cast to 'const volatile void*', getting
18046 // the underlying type.
18047 if (ICE->getCastKind() == CK_BitCast)
18048 PtrArg = ICE->getSubExpr();
18049 }
18050
18051 if (auto PtrTy = PtrArg->getType()->getAs<PointerType>()) {
18052 QualType PointeeType = PtrTy->getPointeeType();
18053 if (!PointeeType->isIncompleteType() &&
18054 Info.Ctx.getTypeAlignInChars(T: PointeeType) >= Size) {
18055 // OK, we will inline operations on this object.
18056 return Success(Value: 1, E);
18057 }
18058 }
18059 }
18060 }
18061
18062 return BuiltinOp == Builtin::BI__atomic_always_lock_free ?
18063 Success(Value: 0, E) : Error(E);
18064 }
18065 case Builtin::BI__builtin_addcb:
18066 case Builtin::BI__builtin_addcs:
18067 case Builtin::BI__builtin_addc:
18068 case Builtin::BI__builtin_addcl:
18069 case Builtin::BI__builtin_addcll:
18070 case Builtin::BI__builtin_subcb:
18071 case Builtin::BI__builtin_subcs:
18072 case Builtin::BI__builtin_subc:
18073 case Builtin::BI__builtin_subcl:
18074 case Builtin::BI__builtin_subcll: {
18075 LValue CarryOutLValue;
18076 APSInt LHS, RHS, CarryIn, CarryOut, Result;
18077 QualType ResultType = E->getArg(Arg: 0)->getType();
18078 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18079 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18080 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: CarryIn, Info) ||
18081 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: CarryOutLValue, Info))
18082 return false;
18083 // Copy the number of bits and sign.
18084 Result = LHS;
18085 CarryOut = LHS;
18086
18087 bool FirstOverflowed = false;
18088 bool SecondOverflowed = false;
18089 switch (BuiltinOp) {
18090 default:
18091 llvm_unreachable("Invalid value for BuiltinOp");
18092 case Builtin::BI__builtin_addcb:
18093 case Builtin::BI__builtin_addcs:
18094 case Builtin::BI__builtin_addc:
18095 case Builtin::BI__builtin_addcl:
18096 case Builtin::BI__builtin_addcll:
18097 Result =
18098 LHS.uadd_ov(RHS, Overflow&: FirstOverflowed).uadd_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
18099 break;
18100 case Builtin::BI__builtin_subcb:
18101 case Builtin::BI__builtin_subcs:
18102 case Builtin::BI__builtin_subc:
18103 case Builtin::BI__builtin_subcl:
18104 case Builtin::BI__builtin_subcll:
18105 Result =
18106 LHS.usub_ov(RHS, Overflow&: FirstOverflowed).usub_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
18107 break;
18108 }
18109
18110 // It is possible for both overflows to happen but CGBuiltin uses an OR so
18111 // this is consistent.
18112 CarryOut = (uint64_t)(FirstOverflowed | SecondOverflowed);
18113 APValue APV{CarryOut};
18114 if (!handleAssignment(Info, E, LVal: CarryOutLValue, LValType: ResultType, Val&: APV))
18115 return false;
18116 return Success(SI: Result, E);
18117 }
18118 case Builtin::BI__builtin_add_overflow:
18119 case Builtin::BI__builtin_sub_overflow:
18120 case Builtin::BI__builtin_mul_overflow:
18121 case Builtin::BI__builtin_sadd_overflow:
18122 case Builtin::BI__builtin_uadd_overflow:
18123 case Builtin::BI__builtin_uaddl_overflow:
18124 case Builtin::BI__builtin_uaddll_overflow:
18125 case Builtin::BI__builtin_usub_overflow:
18126 case Builtin::BI__builtin_usubl_overflow:
18127 case Builtin::BI__builtin_usubll_overflow:
18128 case Builtin::BI__builtin_umul_overflow:
18129 case Builtin::BI__builtin_umull_overflow:
18130 case Builtin::BI__builtin_umulll_overflow:
18131 case Builtin::BI__builtin_saddl_overflow:
18132 case Builtin::BI__builtin_saddll_overflow:
18133 case Builtin::BI__builtin_ssub_overflow:
18134 case Builtin::BI__builtin_ssubl_overflow:
18135 case Builtin::BI__builtin_ssubll_overflow:
18136 case Builtin::BI__builtin_smul_overflow:
18137 case Builtin::BI__builtin_smull_overflow:
18138 case Builtin::BI__builtin_smulll_overflow: {
18139 LValue ResultLValue;
18140 APSInt LHS, RHS;
18141
18142 QualType ResultType = E->getArg(Arg: 2)->getType()->getPointeeType();
18143 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18144 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18145 !EvaluatePointer(E: E->getArg(Arg: 2), Result&: ResultLValue, Info))
18146 return false;
18147
18148 APSInt Result;
18149 bool DidOverflow = false;
18150
18151 // If the types don't have to match, enlarge all 3 to the largest of them.
18152 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
18153 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
18154 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
18155 bool IsSigned = LHS.isSigned() || RHS.isSigned() ||
18156 ResultType->isSignedIntegerOrEnumerationType();
18157 bool AllSigned = LHS.isSigned() && RHS.isSigned() &&
18158 ResultType->isSignedIntegerOrEnumerationType();
18159 uint64_t LHSSize = LHS.getBitWidth();
18160 uint64_t RHSSize = RHS.getBitWidth();
18161 uint64_t ResultSize = Info.Ctx.getIntWidth(T: ResultType);
18162 uint64_t MaxBits = std::max(a: std::max(a: LHSSize, b: RHSSize), b: ResultSize);
18163
18164 // Add an additional bit if the signedness isn't uniformly agreed to. We
18165 // could do this ONLY if there is a signed and an unsigned that both have
18166 // MaxBits, but the code to check that is pretty nasty. The issue will be
18167 // caught in the shrink-to-result later anyway.
18168 if (IsSigned && !AllSigned)
18169 ++MaxBits;
18170
18171 LHS = APSInt(LHS.extOrTrunc(width: MaxBits), !IsSigned);
18172 RHS = APSInt(RHS.extOrTrunc(width: MaxBits), !IsSigned);
18173 Result = APSInt(MaxBits, !IsSigned);
18174 }
18175
18176 // Find largest int.
18177 switch (BuiltinOp) {
18178 default:
18179 llvm_unreachable("Invalid value for BuiltinOp");
18180 case Builtin::BI__builtin_add_overflow:
18181 case Builtin::BI__builtin_sadd_overflow:
18182 case Builtin::BI__builtin_saddl_overflow:
18183 case Builtin::BI__builtin_saddll_overflow:
18184 case Builtin::BI__builtin_uadd_overflow:
18185 case Builtin::BI__builtin_uaddl_overflow:
18186 case Builtin::BI__builtin_uaddll_overflow:
18187 Result = LHS.isSigned() ? LHS.sadd_ov(RHS, Overflow&: DidOverflow)
18188 : LHS.uadd_ov(RHS, Overflow&: DidOverflow);
18189 break;
18190 case Builtin::BI__builtin_sub_overflow:
18191 case Builtin::BI__builtin_ssub_overflow:
18192 case Builtin::BI__builtin_ssubl_overflow:
18193 case Builtin::BI__builtin_ssubll_overflow:
18194 case Builtin::BI__builtin_usub_overflow:
18195 case Builtin::BI__builtin_usubl_overflow:
18196 case Builtin::BI__builtin_usubll_overflow:
18197 Result = LHS.isSigned() ? LHS.ssub_ov(RHS, Overflow&: DidOverflow)
18198 : LHS.usub_ov(RHS, Overflow&: DidOverflow);
18199 break;
18200 case Builtin::BI__builtin_mul_overflow:
18201 case Builtin::BI__builtin_smul_overflow:
18202 case Builtin::BI__builtin_smull_overflow:
18203 case Builtin::BI__builtin_smulll_overflow:
18204 case Builtin::BI__builtin_umul_overflow:
18205 case Builtin::BI__builtin_umull_overflow:
18206 case Builtin::BI__builtin_umulll_overflow:
18207 Result = LHS.isSigned() ? LHS.smul_ov(RHS, Overflow&: DidOverflow)
18208 : LHS.umul_ov(RHS, Overflow&: DidOverflow);
18209 break;
18210 }
18211
18212 // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead,
18213 // since it will give us the behavior of a TruncOrSelf in the case where
18214 // its parameter <= its size. We previously set Result to be at least the
18215 // integer width of the result, so getIntWidth(ResultType) <=
18216 // Result.BitWidth will work exactly like TruncOrSelf.
18217 APSInt Temp = Result.extOrTrunc(width: Info.Ctx.getIntWidth(T: ResultType));
18218 Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType());
18219
18220 // In the case where multiple sizes are allowed, truncate and see if
18221 // the values are the same.
18222 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
18223 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
18224 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
18225 if (!APSInt::isSameValue(I1: Temp, I2: Result))
18226 DidOverflow = true;
18227 }
18228 Result = Temp;
18229
18230 APValue APV{Result};
18231 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
18232 return false;
18233 return Success(Value: DidOverflow, E);
18234 }
18235
18236 case Builtin::BI__builtin_reduce_add:
18237 case Builtin::BI__builtin_reduce_mul:
18238 case Builtin::BI__builtin_reduce_and:
18239 case Builtin::BI__builtin_reduce_or:
18240 case Builtin::BI__builtin_reduce_xor:
18241 case Builtin::BI__builtin_reduce_min:
18242 case Builtin::BI__builtin_reduce_max: {
18243 APValue Source;
18244 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
18245 return false;
18246
18247 unsigned SourceLen = Source.getVectorLength();
18248 APSInt Reduced = Source.getVectorElt(I: 0).getInt();
18249 for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) {
18250 switch (BuiltinOp) {
18251 default:
18252 return false;
18253 case Builtin::BI__builtin_reduce_add: {
18254 if (!CheckedIntArithmetic(
18255 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
18256 BitWidth: Reduced.getBitWidth() + 1, Op: std::plus<APSInt>(), Result&: Reduced))
18257 return false;
18258 break;
18259 }
18260 case Builtin::BI__builtin_reduce_mul: {
18261 if (!CheckedIntArithmetic(
18262 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
18263 BitWidth: Reduced.getBitWidth() * 2, Op: std::multiplies<APSInt>(), Result&: Reduced))
18264 return false;
18265 break;
18266 }
18267 case Builtin::BI__builtin_reduce_and: {
18268 Reduced &= Source.getVectorElt(I: EltNum).getInt();
18269 break;
18270 }
18271 case Builtin::BI__builtin_reduce_or: {
18272 Reduced |= Source.getVectorElt(I: EltNum).getInt();
18273 break;
18274 }
18275 case Builtin::BI__builtin_reduce_xor: {
18276 Reduced ^= Source.getVectorElt(I: EltNum).getInt();
18277 break;
18278 }
18279 case Builtin::BI__builtin_reduce_min: {
18280 Reduced = std::min(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
18281 break;
18282 }
18283 case Builtin::BI__builtin_reduce_max: {
18284 Reduced = std::max(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
18285 break;
18286 }
18287 }
18288 }
18289
18290 return Success(SI: Reduced, E);
18291 }
18292
18293 case clang::X86::BI__builtin_ia32_addcarryx_u32:
18294 case clang::X86::BI__builtin_ia32_addcarryx_u64:
18295 case clang::X86::BI__builtin_ia32_subborrow_u32:
18296 case clang::X86::BI__builtin_ia32_subborrow_u64: {
18297 LValue ResultLValue;
18298 APSInt CarryIn, LHS, RHS;
18299 QualType ResultType = E->getArg(Arg: 3)->getType()->getPointeeType();
18300 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CarryIn, Info) ||
18301 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: LHS, Info) ||
18302 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: RHS, Info) ||
18303 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: ResultLValue, Info))
18304 return false;
18305
18306 bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||
18307 BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;
18308
18309 unsigned BitWidth = LHS.getBitWidth();
18310 unsigned CarryInBit = CarryIn.ugt(RHS: 0) ? 1 : 0;
18311 APInt ExResult =
18312 IsAdd
18313 ? (LHS.zext(width: BitWidth + 1) + (RHS.zext(width: BitWidth + 1) + CarryInBit))
18314 : (LHS.zext(width: BitWidth + 1) - (RHS.zext(width: BitWidth + 1) + CarryInBit));
18315
18316 APInt Result = ExResult.extractBits(numBits: BitWidth, bitPosition: 0);
18317 uint64_t CarryOut = ExResult.extractBitsAsZExtValue(numBits: 1, bitPosition: BitWidth);
18318
18319 APValue APV{APSInt(Result, /*isUnsigned=*/true)};
18320 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
18321 return false;
18322 return Success(Value: CarryOut, E);
18323 }
18324
18325 case clang::X86::BI__builtin_ia32_movmskps:
18326 case clang::X86::BI__builtin_ia32_movmskpd:
18327 case clang::X86::BI__builtin_ia32_pmovmskb128:
18328 case clang::X86::BI__builtin_ia32_pmovmskb256:
18329 case clang::X86::BI__builtin_ia32_movmskps256:
18330 case clang::X86::BI__builtin_ia32_movmskpd256: {
18331 APValue Source;
18332 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
18333 return false;
18334 unsigned SourceLen = Source.getVectorLength();
18335 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
18336 QualType ElemQT = VT->getElementType();
18337 unsigned ResultLen = Info.Ctx.getTypeSize(
18338 T: E->getCallReturnType(Ctx: Info.Ctx)); // Always 32-bit integer.
18339 APInt Result(ResultLen, 0);
18340
18341 for (unsigned I = 0; I != SourceLen; ++I) {
18342 APInt Elem;
18343 if (ElemQT->isIntegerType()) {
18344 Elem = Source.getVectorElt(I).getInt();
18345 } else if (ElemQT->isRealFloatingType()) {
18346 Elem = Source.getVectorElt(I).getFloat().bitcastToAPInt();
18347 } else {
18348 return false;
18349 }
18350 Result.setBitVal(BitPosition: I, BitValue: Elem.isNegative());
18351 }
18352 return Success(I: Result, E);
18353 }
18354
18355 case clang::X86::BI__builtin_ia32_bextr_u32:
18356 case clang::X86::BI__builtin_ia32_bextr_u64:
18357 case clang::X86::BI__builtin_ia32_bextri_u32:
18358 case clang::X86::BI__builtin_ia32_bextri_u64: {
18359 APSInt Val, Idx;
18360 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18361 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
18362 return false;
18363
18364 unsigned BitWidth = Val.getBitWidth();
18365 uint64_t Shift = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
18366 uint64_t Length = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 8);
18367 Length = Length > BitWidth ? BitWidth : Length;
18368
18369 // Handle out of bounds cases.
18370 if (Length == 0 || Shift >= BitWidth)
18371 return Success(Value: 0, E);
18372
18373 uint64_t Result = Val.getZExtValue() >> Shift;
18374 Result &= llvm::maskTrailingOnes<uint64_t>(N: Length);
18375 return Success(Value: Result, E);
18376 }
18377
18378 case clang::X86::BI__builtin_ia32_bzhi_si:
18379 case clang::X86::BI__builtin_ia32_bzhi_di: {
18380 APSInt Val, Idx;
18381 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18382 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
18383 return false;
18384
18385 unsigned BitWidth = Val.getBitWidth();
18386 unsigned Index = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
18387 if (Index < BitWidth)
18388 Val.clearHighBits(hiBits: BitWidth - Index);
18389 return Success(SI: Val, E);
18390 }
18391
18392 case clang::X86::BI__builtin_ia32_ktestcqi:
18393 case clang::X86::BI__builtin_ia32_ktestchi:
18394 case clang::X86::BI__builtin_ia32_ktestcsi:
18395 case clang::X86::BI__builtin_ia32_ktestcdi: {
18396 APSInt A, B;
18397 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18398 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18399 return false;
18400
18401 return Success(Value: (~A & B) == 0, E);
18402 }
18403
18404 case clang::X86::BI__builtin_ia32_ktestzqi:
18405 case clang::X86::BI__builtin_ia32_ktestzhi:
18406 case clang::X86::BI__builtin_ia32_ktestzsi:
18407 case clang::X86::BI__builtin_ia32_ktestzdi: {
18408 APSInt A, B;
18409 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18410 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18411 return false;
18412
18413 return Success(Value: (A & B) == 0, E);
18414 }
18415
18416 case clang::X86::BI__builtin_ia32_kortestcqi:
18417 case clang::X86::BI__builtin_ia32_kortestchi:
18418 case clang::X86::BI__builtin_ia32_kortestcsi:
18419 case clang::X86::BI__builtin_ia32_kortestcdi: {
18420 APSInt A, B;
18421 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18422 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18423 return false;
18424
18425 return Success(Value: ~(A | B) == 0, E);
18426 }
18427
18428 case clang::X86::BI__builtin_ia32_kortestzqi:
18429 case clang::X86::BI__builtin_ia32_kortestzhi:
18430 case clang::X86::BI__builtin_ia32_kortestzsi:
18431 case clang::X86::BI__builtin_ia32_kortestzdi: {
18432 APSInt A, B;
18433 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18434 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18435 return false;
18436
18437 return Success(Value: (A | B) == 0, E);
18438 }
18439
18440 case clang::X86::BI__builtin_ia32_kunpckhi:
18441 case clang::X86::BI__builtin_ia32_kunpckdi:
18442 case clang::X86::BI__builtin_ia32_kunpcksi: {
18443 APSInt A, B;
18444 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18445 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18446 return false;
18447
18448 // Generic kunpack: extract lower half of each operand and concatenate
18449 // Result = A[HalfWidth-1:0] concat B[HalfWidth-1:0]
18450 unsigned BW = A.getBitWidth();
18451 APSInt Result(A.trunc(width: BW / 2).concat(NewLSB: B.trunc(width: BW / 2)), A.isUnsigned());
18452 return Success(SI: Result, E);
18453 }
18454
18455 case clang::X86::BI__builtin_ia32_lzcnt_u16:
18456 case clang::X86::BI__builtin_ia32_lzcnt_u32:
18457 case clang::X86::BI__builtin_ia32_lzcnt_u64: {
18458 APSInt Val;
18459 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18460 return false;
18461 return Success(Value: Val.countLeadingZeros(), E);
18462 }
18463
18464 case clang::X86::BI__builtin_ia32_tzcnt_u16:
18465 case clang::X86::BI__builtin_ia32_tzcnt_u32:
18466 case clang::X86::BI__builtin_ia32_tzcnt_u64: {
18467 APSInt Val;
18468 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18469 return false;
18470 return Success(Value: Val.countTrailingZeros(), E);
18471 }
18472
18473 case Builtin::BI__builtin_elementwise_pdep: {
18474 APSInt Val, Msk;
18475 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18476 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18477 return false;
18478 return Success(I: llvm::APIntOps::pdep(Val, Mask: Msk), E);
18479 }
18480
18481 case Builtin::BI__builtin_elementwise_pext: {
18482 APSInt Val, Msk;
18483 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18484 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18485 return false;
18486 return Success(I: llvm::APIntOps::pext(Val, Mask: Msk), E);
18487 }
18488
18489 case X86::BI__builtin_ia32_ptestz128:
18490 case X86::BI__builtin_ia32_ptestz256:
18491 case X86::BI__builtin_ia32_vtestzps:
18492 case X86::BI__builtin_ia32_vtestzps256:
18493 case X86::BI__builtin_ia32_vtestzpd:
18494 case X86::BI__builtin_ia32_vtestzpd256: {
18495 return EvalTestOp(
18496 [](const APInt &A, const APInt &B) { return (A & B) == 0; });
18497 }
18498 case X86::BI__builtin_ia32_ptestc128:
18499 case X86::BI__builtin_ia32_ptestc256:
18500 case X86::BI__builtin_ia32_vtestcps:
18501 case X86::BI__builtin_ia32_vtestcps256:
18502 case X86::BI__builtin_ia32_vtestcpd:
18503 case X86::BI__builtin_ia32_vtestcpd256: {
18504 return EvalTestOp(
18505 [](const APInt &A, const APInt &B) { return (~A & B) == 0; });
18506 }
18507 case X86::BI__builtin_ia32_ptestnzc128:
18508 case X86::BI__builtin_ia32_ptestnzc256:
18509 case X86::BI__builtin_ia32_vtestnzcps:
18510 case X86::BI__builtin_ia32_vtestnzcps256:
18511 case X86::BI__builtin_ia32_vtestnzcpd:
18512 case X86::BI__builtin_ia32_vtestnzcpd256: {
18513 return EvalTestOp([](const APInt &A, const APInt &B) {
18514 return ((A & B) != 0) && ((~A & B) != 0);
18515 });
18516 }
18517 case X86::BI__builtin_ia32_kandqi:
18518 case X86::BI__builtin_ia32_kandhi:
18519 case X86::BI__builtin_ia32_kandsi:
18520 case X86::BI__builtin_ia32_kanddi: {
18521 return HandleMaskBinOp(
18522 [](const APSInt &LHS, const APSInt &RHS) { return LHS & RHS; });
18523 }
18524
18525 case X86::BI__builtin_ia32_kandnqi:
18526 case X86::BI__builtin_ia32_kandnhi:
18527 case X86::BI__builtin_ia32_kandnsi:
18528 case X86::BI__builtin_ia32_kandndi: {
18529 return HandleMaskBinOp(
18530 [](const APSInt &LHS, const APSInt &RHS) { return ~LHS & RHS; });
18531 }
18532
18533 case X86::BI__builtin_ia32_korqi:
18534 case X86::BI__builtin_ia32_korhi:
18535 case X86::BI__builtin_ia32_korsi:
18536 case X86::BI__builtin_ia32_kordi: {
18537 return HandleMaskBinOp(
18538 [](const APSInt &LHS, const APSInt &RHS) { return LHS | RHS; });
18539 }
18540
18541 case X86::BI__builtin_ia32_kxnorqi:
18542 case X86::BI__builtin_ia32_kxnorhi:
18543 case X86::BI__builtin_ia32_kxnorsi:
18544 case X86::BI__builtin_ia32_kxnordi: {
18545 return HandleMaskBinOp(
18546 [](const APSInt &LHS, const APSInt &RHS) { return ~(LHS ^ RHS); });
18547 }
18548
18549 case X86::BI__builtin_ia32_kxorqi:
18550 case X86::BI__builtin_ia32_kxorhi:
18551 case X86::BI__builtin_ia32_kxorsi:
18552 case X86::BI__builtin_ia32_kxordi: {
18553 return HandleMaskBinOp(
18554 [](const APSInt &LHS, const APSInt &RHS) { return LHS ^ RHS; });
18555 }
18556
18557 case X86::BI__builtin_ia32_knotqi:
18558 case X86::BI__builtin_ia32_knothi:
18559 case X86::BI__builtin_ia32_knotsi:
18560 case X86::BI__builtin_ia32_knotdi: {
18561 APSInt Val;
18562 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18563 return false;
18564 APSInt Result = ~Val;
18565 return Success(V: APValue(Result), E);
18566 }
18567
18568 case X86::BI__builtin_ia32_kaddqi:
18569 case X86::BI__builtin_ia32_kaddhi:
18570 case X86::BI__builtin_ia32_kaddsi:
18571 case X86::BI__builtin_ia32_kadddi: {
18572 return HandleMaskBinOp(
18573 [](const APSInt &LHS, const APSInt &RHS) { return LHS + RHS; });
18574 }
18575
18576 case X86::BI__builtin_ia32_kmovb:
18577 case X86::BI__builtin_ia32_kmovw:
18578 case X86::BI__builtin_ia32_kmovd:
18579 case X86::BI__builtin_ia32_kmovq: {
18580 APSInt Val;
18581 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18582 return false;
18583 return Success(SI: Val, E);
18584 }
18585
18586 case X86::BI__builtin_ia32_kshiftliqi:
18587 case X86::BI__builtin_ia32_kshiftlihi:
18588 case X86::BI__builtin_ia32_kshiftlisi:
18589 case X86::BI__builtin_ia32_kshiftlidi: {
18590 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18591 unsigned Amt = RHS.getZExtValue() & 0xFF;
18592 if (Amt >= LHS.getBitWidth())
18593 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18594 return APSInt(LHS.shl(shiftAmt: Amt), LHS.isUnsigned());
18595 });
18596 }
18597
18598 case X86::BI__builtin_ia32_kshiftriqi:
18599 case X86::BI__builtin_ia32_kshiftrihi:
18600 case X86::BI__builtin_ia32_kshiftrisi:
18601 case X86::BI__builtin_ia32_kshiftridi: {
18602 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18603 unsigned Amt = RHS.getZExtValue() & 0xFF;
18604 if (Amt >= LHS.getBitWidth())
18605 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18606 return APSInt(LHS.lshr(shiftAmt: Amt), LHS.isUnsigned());
18607 });
18608 }
18609
18610 case clang::X86::BI__builtin_ia32_vec_ext_v4hi:
18611 case clang::X86::BI__builtin_ia32_vec_ext_v16qi:
18612 case clang::X86::BI__builtin_ia32_vec_ext_v8hi:
18613 case clang::X86::BI__builtin_ia32_vec_ext_v4si:
18614 case clang::X86::BI__builtin_ia32_vec_ext_v2di:
18615 case clang::X86::BI__builtin_ia32_vec_ext_v32qi:
18616 case clang::X86::BI__builtin_ia32_vec_ext_v16hi:
18617 case clang::X86::BI__builtin_ia32_vec_ext_v8si:
18618 case clang::X86::BI__builtin_ia32_vec_ext_v4di: {
18619 APValue Vec;
18620 APSInt IdxAPS;
18621 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
18622 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
18623 return false;
18624 unsigned N = Vec.getVectorLength();
18625 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
18626 return Success(SI: Vec.getVectorElt(I: Idx).getInt(), E);
18627 }
18628
18629 case clang::X86::BI__builtin_ia32_cvtb2mask128:
18630 case clang::X86::BI__builtin_ia32_cvtb2mask256:
18631 case clang::X86::BI__builtin_ia32_cvtb2mask512:
18632 case clang::X86::BI__builtin_ia32_cvtw2mask128:
18633 case clang::X86::BI__builtin_ia32_cvtw2mask256:
18634 case clang::X86::BI__builtin_ia32_cvtw2mask512:
18635 case clang::X86::BI__builtin_ia32_cvtd2mask128:
18636 case clang::X86::BI__builtin_ia32_cvtd2mask256:
18637 case clang::X86::BI__builtin_ia32_cvtd2mask512:
18638 case clang::X86::BI__builtin_ia32_cvtq2mask128:
18639 case clang::X86::BI__builtin_ia32_cvtq2mask256:
18640 case clang::X86::BI__builtin_ia32_cvtq2mask512: {
18641 assert(E->getNumArgs() == 1);
18642 APValue Vec;
18643 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
18644 return false;
18645
18646 unsigned VectorLen = Vec.getVectorLength();
18647 unsigned RetWidth = Info.Ctx.getIntWidth(T: E->getType());
18648 llvm::APInt Bits(RetWidth, 0);
18649
18650 for (unsigned ElemNum = 0; ElemNum != VectorLen; ++ElemNum) {
18651 const APSInt &A = Vec.getVectorElt(I: ElemNum).getInt();
18652 unsigned MSB = A[A.getBitWidth() - 1];
18653 Bits.setBitVal(BitPosition: ElemNum, BitValue: MSB);
18654 }
18655
18656 APSInt RetMask(Bits, /*isUnsigned=*/true);
18657 return Success(V: APValue(RetMask), E);
18658 }
18659
18660 case clang::X86::BI__builtin_ia32_cmpb128_mask:
18661 case clang::X86::BI__builtin_ia32_cmpw128_mask:
18662 case clang::X86::BI__builtin_ia32_cmpd128_mask:
18663 case clang::X86::BI__builtin_ia32_cmpq128_mask:
18664 case clang::X86::BI__builtin_ia32_cmpb256_mask:
18665 case clang::X86::BI__builtin_ia32_cmpw256_mask:
18666 case clang::X86::BI__builtin_ia32_cmpd256_mask:
18667 case clang::X86::BI__builtin_ia32_cmpq256_mask:
18668 case clang::X86::BI__builtin_ia32_cmpb512_mask:
18669 case clang::X86::BI__builtin_ia32_cmpw512_mask:
18670 case clang::X86::BI__builtin_ia32_cmpd512_mask:
18671 case clang::X86::BI__builtin_ia32_cmpq512_mask:
18672 case clang::X86::BI__builtin_ia32_ucmpb128_mask:
18673 case clang::X86::BI__builtin_ia32_ucmpw128_mask:
18674 case clang::X86::BI__builtin_ia32_ucmpd128_mask:
18675 case clang::X86::BI__builtin_ia32_ucmpq128_mask:
18676 case clang::X86::BI__builtin_ia32_ucmpb256_mask:
18677 case clang::X86::BI__builtin_ia32_ucmpw256_mask:
18678 case clang::X86::BI__builtin_ia32_ucmpd256_mask:
18679 case clang::X86::BI__builtin_ia32_ucmpq256_mask:
18680 case clang::X86::BI__builtin_ia32_ucmpb512_mask:
18681 case clang::X86::BI__builtin_ia32_ucmpw512_mask:
18682 case clang::X86::BI__builtin_ia32_ucmpd512_mask:
18683 case clang::X86::BI__builtin_ia32_ucmpq512_mask: {
18684 assert(E->getNumArgs() == 4);
18685
18686 bool IsUnsigned =
18687 (BuiltinOp >= clang::X86::BI__builtin_ia32_ucmpb128_mask &&
18688 BuiltinOp <= clang::X86::BI__builtin_ia32_ucmpw512_mask);
18689
18690 APValue LHS, RHS;
18691 APSInt Mask, Opcode;
18692 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18693 !EvaluateVector(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18694 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Opcode, Info) ||
18695 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: Mask, Info))
18696 return false;
18697
18698 assert(LHS.getVectorLength() == RHS.getVectorLength());
18699
18700 unsigned VectorLen = LHS.getVectorLength();
18701 unsigned RetWidth = Mask.getBitWidth();
18702
18703 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
18704
18705 for (unsigned ElemNum = 0; ElemNum < VectorLen; ++ElemNum) {
18706 const APSInt &A = LHS.getVectorElt(I: ElemNum).getInt();
18707 const APSInt &B = RHS.getVectorElt(I: ElemNum).getInt();
18708 bool Result = false;
18709
18710 switch (Opcode.getExtValue() & 0x7) {
18711 case 0: // _MM_CMPINT_EQ
18712 Result = (A == B);
18713 break;
18714 case 1: // _MM_CMPINT_LT
18715 Result = IsUnsigned ? A.ult(RHS: B) : A.slt(RHS: B);
18716 break;
18717 case 2: // _MM_CMPINT_LE
18718 Result = IsUnsigned ? A.ule(RHS: B) : A.sle(RHS: B);
18719 break;
18720 case 3: // _MM_CMPINT_FALSE
18721 Result = false;
18722 break;
18723 case 4: // _MM_CMPINT_NE
18724 Result = (A != B);
18725 break;
18726 case 5: // _MM_CMPINT_NLT (>=)
18727 Result = IsUnsigned ? A.uge(RHS: B) : A.sge(RHS: B);
18728 break;
18729 case 6: // _MM_CMPINT_NLE (>)
18730 Result = IsUnsigned ? A.ugt(RHS: B) : A.sgt(RHS: B);
18731 break;
18732 case 7: // _MM_CMPINT_TRUE
18733 Result = true;
18734 break;
18735 }
18736
18737 RetMask.setBitVal(BitPosition: ElemNum, BitValue: Mask[ElemNum] && Result);
18738 }
18739
18740 return Success(V: APValue(RetMask), E);
18741 }
18742 case X86::BI__builtin_ia32_cvtss2si:
18743 case X86::BI__builtin_ia32_cvtsd2si:
18744 case X86::BI__builtin_ia32_cvttss2si:
18745 case X86::BI__builtin_ia32_cvttsd2si:
18746 case X86::BI__builtin_ia32_cvtss2si64:
18747 case X86::BI__builtin_ia32_cvtsd2si64:
18748 case X86::BI__builtin_ia32_cvttss2si64:
18749 case X86::BI__builtin_ia32_cvttsd2si64: {
18750 APValue ArgVal;
18751 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: ArgVal))
18752 return false;
18753
18754 assert(ArgVal.isVector() && "Expected a vector argument");
18755 llvm::APFloat FloatElem = ArgVal.getVectorElt(I: 0).getFloat();
18756 unsigned BitWidth = Info.Ctx.getIntWidth(T: E->getType());
18757 bool isUnsigned = E->getType()->isUnsignedIntegerType();
18758
18759 llvm::APSInt IntResult(BitWidth, isUnsigned);
18760 bool IsExact = false;
18761 // We only allow exact conversions so rounding mode does not matter for cvt*
18762 // and cvtt* builtins
18763 FloatElem.convertToInteger(Result&: IntResult, RM: llvm::APFloat::rmTowardZero,
18764 IsExact: &IsExact);
18765 if (!IsExact)
18766 return false;
18767
18768 return Success(SI: IntResult, E);
18769 }
18770 case X86::BI__builtin_ia32_vpshufbitqmb128_mask:
18771 case X86::BI__builtin_ia32_vpshufbitqmb256_mask:
18772 case X86::BI__builtin_ia32_vpshufbitqmb512_mask: {
18773 assert(E->getNumArgs() == 3);
18774
18775 APValue Source, ShuffleMask;
18776 APSInt ZeroMask;
18777 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
18778 !EvaluateVector(E: E->getArg(Arg: 1), Result&: ShuffleMask, Info) ||
18779 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: ZeroMask, Info))
18780 return false;
18781
18782 assert(Source.getVectorLength() == ShuffleMask.getVectorLength());
18783 assert(ZeroMask.getBitWidth() == Source.getVectorLength());
18784
18785 unsigned NumBytesInQWord = 8;
18786 unsigned NumBitsInByte = 8;
18787 unsigned NumBytes = Source.getVectorLength();
18788 unsigned NumQWords = NumBytes / NumBytesInQWord;
18789 unsigned RetWidth = ZeroMask.getBitWidth();
18790 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
18791
18792 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
18793 APInt SourceQWord(64, 0);
18794 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
18795 uint64_t Byte = Source.getVectorElt(I: QWordId * NumBytesInQWord + ByteIdx)
18796 .getInt()
18797 .getZExtValue();
18798 SourceQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
18799 }
18800
18801 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
18802 unsigned SelIdx = QWordId * NumBytesInQWord + ByteIdx;
18803 unsigned M =
18804 ShuffleMask.getVectorElt(I: SelIdx).getInt().getZExtValue() & 0x3F;
18805 if (ZeroMask[SelIdx]) {
18806 RetMask.setBitVal(BitPosition: SelIdx, BitValue: SourceQWord[M]);
18807 }
18808 }
18809 }
18810 return Success(V: APValue(RetMask), E);
18811 }
18812 }
18813}
18814
18815/// Determine whether this is a pointer past the end of the complete
18816/// object referred to by the lvalue.
18817static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx,
18818 const LValue &LV) {
18819 // A null pointer can be viewed as being "past the end" but we don't
18820 // choose to look at it that way here.
18821 if (!LV.getLValueBase())
18822 return false;
18823
18824 // If the designator is valid and refers to a subobject, we're not pointing
18825 // past the end.
18826 if (!LV.getLValueDesignator().Invalid &&
18827 !LV.getLValueDesignator().isOnePastTheEnd())
18828 return false;
18829
18830 // A pointer to an incomplete type might be past-the-end if the type's size is
18831 // zero. We cannot tell because the type is incomplete.
18832 QualType Ty = getType(B: LV.getLValueBase());
18833 if (Ty->isIncompleteType())
18834 return true;
18835
18836 // Can't be past the end of an invalid object.
18837 if (LV.getLValueDesignator().Invalid)
18838 return false;
18839
18840 // We're a past-the-end pointer if we point to the byte after the object,
18841 // no matter what our type or path is.
18842 auto Size = Ctx.getTypeSizeInChars(T: Ty);
18843 return LV.getLValueOffset() == Size;
18844}
18845
18846namespace {
18847
18848/// Data recursive integer evaluator of certain binary operators.
18849///
18850/// We use a data recursive algorithm for binary operators so that we are able
18851/// to handle extreme cases of chained binary operators without causing stack
18852/// overflow.
18853class DataRecursiveIntBinOpEvaluator {
18854 struct EvalResult {
18855 APValue Val;
18856 bool Failed = false;
18857
18858 EvalResult() = default;
18859
18860 void swap(EvalResult &RHS) {
18861 Val.swap(RHS&: RHS.Val);
18862 Failed = RHS.Failed;
18863 RHS.Failed = false;
18864 }
18865 };
18866
18867 struct Job {
18868 const Expr *E;
18869 EvalResult LHSResult; // meaningful only for binary operator expression.
18870 enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind;
18871
18872 Job() = default;
18873 Job(Job &&) = default;
18874
18875 void startSpeculativeEval(EvalInfo &Info) {
18876 SpecEvalRAII = SpeculativeEvaluationRAII(Info);
18877 }
18878
18879 private:
18880 SpeculativeEvaluationRAII SpecEvalRAII;
18881 };
18882
18883 SmallVector<Job, 16> Queue;
18884
18885 IntExprEvaluator &IntEval;
18886 EvalInfo &Info;
18887 APValue &FinalResult;
18888
18889public:
18890 DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result)
18891 : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { }
18892
18893 /// True if \param E is a binary operator that we are going to handle
18894 /// data recursively.
18895 /// We handle binary operators that are comma, logical, or that have operands
18896 /// with integral or enumeration type.
18897 static bool shouldEnqueue(const BinaryOperator *E) {
18898 return E->getOpcode() == BO_Comma || E->isLogicalOp() ||
18899 (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() &&
18900 E->getLHS()->getType()->isIntegralOrEnumerationType() &&
18901 E->getRHS()->getType()->isIntegralOrEnumerationType());
18902 }
18903
18904 bool Traverse(const BinaryOperator *E) {
18905 enqueue(E);
18906 EvalResult PrevResult;
18907 while (!Queue.empty())
18908 process(Result&: PrevResult);
18909
18910 if (PrevResult.Failed) return false;
18911
18912 FinalResult.swap(RHS&: PrevResult.Val);
18913 return true;
18914 }
18915
18916private:
18917 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
18918 return IntEval.Success(Value, E, Result);
18919 }
18920 bool Success(const APSInt &Value, const Expr *E, APValue &Result) {
18921 return IntEval.Success(SI: Value, E, Result);
18922 }
18923 bool Error(const Expr *E) {
18924 return IntEval.Error(E);
18925 }
18926 bool Error(const Expr *E, diag::kind D) {
18927 return IntEval.Error(E, D);
18928 }
18929
18930 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
18931 return Info.CCEDiag(E, DiagId: D);
18932 }
18933
18934 // Returns true if visiting the RHS is necessary, false otherwise.
18935 bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
18936 bool &SuppressRHSDiags);
18937
18938 bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
18939 const BinaryOperator *E, APValue &Result);
18940
18941 void EvaluateExpr(const Expr *E, EvalResult &Result) {
18942 Result.Failed = !Evaluate(Result&: Result.Val, Info, E);
18943 if (Result.Failed)
18944 Result.Val = APValue();
18945 }
18946
18947 void process(EvalResult &Result);
18948
18949 void enqueue(const Expr *E) {
18950 E = E->IgnoreParens();
18951 Queue.resize(N: Queue.size()+1);
18952 Queue.back().E = E;
18953 Queue.back().Kind = Job::AnyExprKind;
18954 }
18955};
18956
18957}
18958
18959bool DataRecursiveIntBinOpEvaluator::
18960 VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
18961 bool &SuppressRHSDiags) {
18962 if (E->getOpcode() == BO_Comma) {
18963 // Ignore LHS but note if we could not evaluate it.
18964 if (LHSResult.Failed)
18965 return Info.noteSideEffect();
18966 return true;
18967 }
18968
18969 if (E->isLogicalOp()) {
18970 bool LHSAsBool;
18971 if (!LHSResult.Failed && HandleConversionToBool(Val: LHSResult.Val, Result&: LHSAsBool)) {
18972 // We were able to evaluate the LHS, see if we can get away with not
18973 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
18974 if (LHSAsBool == (E->getOpcode() == BO_LOr)) {
18975 Success(Value: LHSAsBool, E, Result&: LHSResult.Val);
18976 return false; // Ignore RHS
18977 }
18978 } else {
18979 LHSResult.Failed = true;
18980
18981 // Since we weren't able to evaluate the left hand side, it
18982 // might have had side effects.
18983 if (!Info.noteSideEffect())
18984 return false;
18985
18986 // We can't evaluate the LHS; however, sometimes the result
18987 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
18988 // Don't ignore RHS and suppress diagnostics from this arm.
18989 SuppressRHSDiags = true;
18990 }
18991
18992 return true;
18993 }
18994
18995 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
18996 E->getRHS()->getType()->isIntegralOrEnumerationType());
18997
18998 if (LHSResult.Failed && !Info.noteFailure())
18999 return false; // Ignore RHS;
19000
19001 return true;
19002}
19003
19004static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index,
19005 bool IsSub) {
19006 // Compute the new offset in the appropriate width, wrapping at 64 bits.
19007 // FIXME: When compiling for a 32-bit target, we should use 32-bit
19008 // offsets.
19009 assert(!LVal.hasLValuePath() && "have designator for integer lvalue");
19010 CharUnits &Offset = LVal.getLValueOffset();
19011 uint64_t Offset64 = Offset.getQuantity();
19012 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
19013 Offset = CharUnits::fromQuantity(Quantity: IsSub ? Offset64 - Index64
19014 : Offset64 + Index64);
19015}
19016
19017bool DataRecursiveIntBinOpEvaluator::
19018 VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
19019 const BinaryOperator *E, APValue &Result) {
19020 if (E->getOpcode() == BO_Comma) {
19021 if (RHSResult.Failed)
19022 return false;
19023 Result = RHSResult.Val;
19024 return true;
19025 }
19026
19027 if (E->isLogicalOp()) {
19028 bool lhsResult, rhsResult;
19029 bool LHSIsOK = HandleConversionToBool(Val: LHSResult.Val, Result&: lhsResult);
19030 bool RHSIsOK = HandleConversionToBool(Val: RHSResult.Val, Result&: rhsResult);
19031
19032 if (LHSIsOK) {
19033 if (RHSIsOK) {
19034 if (E->getOpcode() == BO_LOr)
19035 return Success(Value: lhsResult || rhsResult, E, Result);
19036 else
19037 return Success(Value: lhsResult && rhsResult, E, Result);
19038 }
19039 } else {
19040 if (RHSIsOK) {
19041 // We can't evaluate the LHS; however, sometimes the result
19042 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
19043 if (rhsResult == (E->getOpcode() == BO_LOr))
19044 return Success(Value: rhsResult, E, Result);
19045 }
19046 }
19047
19048 return false;
19049 }
19050
19051 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
19052 E->getRHS()->getType()->isIntegralOrEnumerationType());
19053
19054 if (LHSResult.Failed || RHSResult.Failed)
19055 return false;
19056
19057 const APValue &LHSVal = LHSResult.Val;
19058 const APValue &RHSVal = RHSResult.Val;
19059
19060 // Handle cases like (unsigned long)&a + 4.
19061 if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
19062 Result = LHSVal;
19063 addOrSubLValueAsInteger(LVal&: Result, Index: RHSVal.getInt(), IsSub: E->getOpcode() == BO_Sub);
19064 return true;
19065 }
19066
19067 // Handle cases like 4 + (unsigned long)&a
19068 if (E->getOpcode() == BO_Add &&
19069 RHSVal.isLValue() && LHSVal.isInt()) {
19070 Result = RHSVal;
19071 addOrSubLValueAsInteger(LVal&: Result, Index: LHSVal.getInt(), /*IsSub*/false);
19072 return true;
19073 }
19074
19075 if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
19076 // Handle (intptr_t)&&A - (intptr_t)&&B.
19077 if (!LHSVal.getLValueOffset().isZero() ||
19078 !RHSVal.getLValueOffset().isZero())
19079 return false;
19080 const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
19081 const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
19082 if (!LHSExpr || !RHSExpr)
19083 return false;
19084 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
19085 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
19086 if (!LHSAddrExpr || !RHSAddrExpr)
19087 return false;
19088 // Make sure both labels come from the same function.
19089 if (LHSAddrExpr->getLabel()->getDeclContext() !=
19090 RHSAddrExpr->getLabel()->getDeclContext())
19091 return false;
19092 Result = APValue(LHSAddrExpr, RHSAddrExpr);
19093 return true;
19094 }
19095
19096 // All the remaining cases expect both operands to be an integer
19097 if (!LHSVal.isInt() || !RHSVal.isInt())
19098 return Error(E);
19099
19100 // Set up the width and signedness manually, in case it can't be deduced
19101 // from the operation we're performing.
19102 // FIXME: Don't do this in the cases where we can deduce it.
19103 APSInt Value(Info.Ctx.getIntWidth(T: E->getType()),
19104 E->getType()->isUnsignedIntegerOrEnumerationType());
19105 if (!handleIntIntBinOp(Info, E, LHS: LHSVal.getInt(), Opcode: E->getOpcode(),
19106 RHS: RHSVal.getInt(), Result&: Value))
19107 return false;
19108 return Success(Value, E, Result);
19109}
19110
19111void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) {
19112 Job &job = Queue.back();
19113
19114 switch (job.Kind) {
19115 case Job::AnyExprKind: {
19116 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(Val: job.E)) {
19117 if (shouldEnqueue(E: Bop)) {
19118 job.Kind = Job::BinOpKind;
19119 enqueue(E: Bop->getLHS());
19120 return;
19121 }
19122 }
19123
19124 EvaluateExpr(E: job.E, Result);
19125 Queue.pop_back();
19126 return;
19127 }
19128
19129 case Job::BinOpKind: {
19130 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
19131 bool SuppressRHSDiags = false;
19132 if (!VisitBinOpLHSOnly(LHSResult&: Result, E: Bop, SuppressRHSDiags)) {
19133 Queue.pop_back();
19134 return;
19135 }
19136 if (SuppressRHSDiags)
19137 job.startSpeculativeEval(Info);
19138 job.LHSResult.swap(RHS&: Result);
19139 job.Kind = Job::BinOpVisitedLHSKind;
19140 enqueue(E: Bop->getRHS());
19141 return;
19142 }
19143
19144 case Job::BinOpVisitedLHSKind: {
19145 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
19146 EvalResult RHS;
19147 RHS.swap(RHS&: Result);
19148 Result.Failed = !VisitBinOp(LHSResult: job.LHSResult, RHSResult: RHS, E: Bop, Result&: Result.Val);
19149 Queue.pop_back();
19150 return;
19151 }
19152 }
19153
19154 llvm_unreachable("Invalid Job::Kind!");
19155}
19156
19157namespace {
19158enum class CmpResult {
19159 Unequal,
19160 Less,
19161 Equal,
19162 Greater,
19163 Unordered,
19164};
19165}
19166
19167template <class SuccessCB, class AfterCB>
19168static bool
19169EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
19170 SuccessCB &&Success, AfterCB &&DoAfter) {
19171 assert(!E->isValueDependent());
19172 assert(E->isComparisonOp() && "expected comparison operator");
19173 assert((E->getOpcode() == BO_Cmp ||
19174 E->getType()->isIntegralOrEnumerationType()) &&
19175 "unsupported binary expression evaluation");
19176 auto Error = [&](const Expr *E) {
19177 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
19178 return false;
19179 };
19180
19181 bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp;
19182 bool IsEquality = E->isEqualityOp();
19183
19184 QualType LHSTy = E->getLHS()->getType();
19185 QualType RHSTy = E->getRHS()->getType();
19186
19187 if (LHSTy->isIntegralOrEnumerationType() &&
19188 RHSTy->isIntegralOrEnumerationType()) {
19189 APSInt LHS, RHS;
19190 bool LHSOK = EvaluateInteger(E: E->getLHS(), Result&: LHS, Info);
19191 if (!LHSOK && !Info.noteFailure())
19192 return false;
19193 if (!EvaluateInteger(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
19194 return false;
19195 if (LHS < RHS)
19196 return Success(CmpResult::Less, E);
19197 if (LHS > RHS)
19198 return Success(CmpResult::Greater, E);
19199 return Success(CmpResult::Equal, E);
19200 }
19201
19202 if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) {
19203 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHSTy));
19204 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHSTy));
19205
19206 bool LHSOK = EvaluateFixedPointOrInteger(E: E->getLHS(), Result&: LHSFX, Info);
19207 if (!LHSOK && !Info.noteFailure())
19208 return false;
19209 if (!EvaluateFixedPointOrInteger(E: E->getRHS(), Result&: RHSFX, Info) || !LHSOK)
19210 return false;
19211 if (LHSFX < RHSFX)
19212 return Success(CmpResult::Less, E);
19213 if (LHSFX > RHSFX)
19214 return Success(CmpResult::Greater, E);
19215 return Success(CmpResult::Equal, E);
19216 }
19217
19218 if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) {
19219 ComplexValue LHS, RHS;
19220 bool LHSOK;
19221 if (E->isAssignmentOp()) {
19222 LValue LV;
19223 EvaluateLValue(E: E->getLHS(), Result&: LV, Info);
19224 LHSOK = false;
19225 } else if (LHSTy->isRealFloatingType()) {
19226 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: LHS.FloatReal, Info);
19227 if (LHSOK) {
19228 LHS.makeComplexFloat();
19229 LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics());
19230 }
19231 } else {
19232 LHSOK = EvaluateComplex(E: E->getLHS(), Res&: LHS, Info);
19233 }
19234 if (!LHSOK && !Info.noteFailure())
19235 return false;
19236
19237 if (E->getRHS()->getType()->isRealFloatingType()) {
19238 if (!EvaluateFloat(E: E->getRHS(), Result&: RHS.FloatReal, Info) || !LHSOK)
19239 return false;
19240 RHS.makeComplexFloat();
19241 RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics());
19242 } else if (!EvaluateComplex(E: E->getRHS(), Res&: RHS, Info) || !LHSOK)
19243 return false;
19244
19245 if (LHS.isComplexFloat()) {
19246 APFloat::cmpResult CR_r =
19247 LHS.getComplexFloatReal().compare(RHS: RHS.getComplexFloatReal());
19248 APFloat::cmpResult CR_i =
19249 LHS.getComplexFloatImag().compare(RHS: RHS.getComplexFloatImag());
19250 bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual;
19251 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
19252 } else {
19253 assert(IsEquality && "invalid complex comparison");
19254 bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
19255 LHS.getComplexIntImag() == RHS.getComplexIntImag();
19256 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
19257 }
19258 }
19259
19260 if (LHSTy->isRealFloatingType() &&
19261 RHSTy->isRealFloatingType()) {
19262 APFloat RHS(0.0), LHS(0.0);
19263
19264 bool LHSOK = EvaluateFloat(E: E->getRHS(), Result&: RHS, Info);
19265 if (!LHSOK && !Info.noteFailure())
19266 return false;
19267
19268 if (!EvaluateFloat(E: E->getLHS(), Result&: LHS, Info) || !LHSOK)
19269 return false;
19270
19271 assert(E->isComparisonOp() && "Invalid binary operator!");
19272 llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS);
19273 if (!Info.InConstantContext &&
19274 APFloatCmpResult == APFloat::cmpUnordered &&
19275 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained()) {
19276 // Note: Compares may raise invalid in some cases involving NaN or sNaN.
19277 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
19278 return false;
19279 }
19280 auto GetCmpRes = [&]() {
19281 switch (APFloatCmpResult) {
19282 case APFloat::cmpEqual:
19283 return CmpResult::Equal;
19284 case APFloat::cmpLessThan:
19285 return CmpResult::Less;
19286 case APFloat::cmpGreaterThan:
19287 return CmpResult::Greater;
19288 case APFloat::cmpUnordered:
19289 return CmpResult::Unordered;
19290 }
19291 llvm_unreachable("Unrecognised APFloat::cmpResult enum");
19292 };
19293 return Success(GetCmpRes(), E);
19294 }
19295
19296 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
19297 LValue LHSValue, RHSValue;
19298
19299 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
19300 if (!LHSOK && !Info.noteFailure())
19301 return false;
19302
19303 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19304 return false;
19305
19306 // Reject differing bases from the normal codepath; we special-case
19307 // comparisons to null.
19308 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
19309 // Bail out early if we're checking potential constant expression.
19310 // Otherwise, prefer to diagnose other issues.
19311 if (Info.checkingPotentialConstantExpression() &&
19312 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
19313 return false;
19314 auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) {
19315 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
19316 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
19317 Info.FFDiag(E, DiagId: DiagID)
19318 << (Reversed ? RHS : LHS) << (Reversed ? LHS : RHS);
19319 return false;
19320 };
19321 // Inequalities and subtractions between unrelated pointers have
19322 // unspecified or undefined behavior.
19323 if (!IsEquality)
19324 return DiagComparison(
19325 diag::note_constexpr_pointer_comparison_unspecified);
19326 // A constant address may compare equal to the address of a symbol.
19327 // The one exception is that address of an object cannot compare equal
19328 // to a null pointer constant.
19329 // TODO: Should we restrict this to actual null pointers, and exclude the
19330 // case of zero cast to pointer type?
19331 if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
19332 (!RHSValue.Base && !RHSValue.Offset.isZero()))
19333 return DiagComparison(diag::note_constexpr_pointer_constant_comparison,
19334 !RHSValue.Base);
19335 // C++2c [intro.object]/10:
19336 // Two objects [...] may have the same address if [...] they are both
19337 // potentially non-unique objects.
19338 // C++2c [intro.object]/9:
19339 // An object is potentially non-unique if it is a string literal object,
19340 // the backing array of an initializer list, or a subobject thereof.
19341 //
19342 // This makes the comparison result unspecified, so it's not a constant
19343 // expression.
19344 //
19345 // TODO: Do we need to handle the initializer list case here?
19346 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
19347 return DiagComparison(diag::note_constexpr_literal_comparison);
19348 if (IsOpaqueConstantCall(LVal: LHSValue) || IsOpaqueConstantCall(LVal: RHSValue))
19349 return DiagComparison(diag::note_constexpr_opaque_call_comparison,
19350 !IsOpaqueConstantCall(LVal: LHSValue));
19351 // We can't tell whether weak symbols will end up pointing to the same
19352 // object.
19353 if (IsWeakLValue(Value: LHSValue) || IsWeakLValue(Value: RHSValue))
19354 return DiagComparison(diag::note_constexpr_pointer_weak_comparison,
19355 !IsWeakLValue(Value: LHSValue));
19356 // We can't compare the address of the start of one object with the
19357 // past-the-end address of another object, per C++ DR1652.
19358 if (LHSValue.Base && LHSValue.Offset.isZero() &&
19359 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: RHSValue))
19360 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
19361 true);
19362 if (RHSValue.Base && RHSValue.Offset.isZero() &&
19363 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: LHSValue))
19364 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
19365 false);
19366 // We can't tell whether an object is at the same address as another
19367 // zero sized object.
19368 if ((RHSValue.Base && isZeroSized(Value: LHSValue)) ||
19369 (LHSValue.Base && isZeroSized(Value: RHSValue)))
19370 return DiagComparison(
19371 diag::note_constexpr_pointer_comparison_zero_sized);
19372 if (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)
19373 return DiagComparison(
19374 diag::note_constexpr_pointer_comparison_unspecified);
19375 // FIXME: Verify both variables are live.
19376 return Success(CmpResult::Unequal, E);
19377 }
19378
19379 const CharUnits &LHSOffset = LHSValue.getLValueOffset();
19380 const CharUnits &RHSOffset = RHSValue.getLValueOffset();
19381
19382 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
19383 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
19384
19385 // C++11 [expr.rel]p2:
19386 // - If two pointers point to non-static data members of the same object,
19387 // or to subobjects or array elements fo such members, recursively, the
19388 // pointer to the later declared member compares greater provided the
19389 // two members have the same access control and provided their class is
19390 // not a union.
19391 // [...]
19392 // - Otherwise pointer comparisons are unspecified.
19393 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {
19394 bool WasArrayIndex;
19395 unsigned Mismatch = FindDesignatorMismatch(
19396 ObjType: LHSValue.Base.isNull() ? QualType()
19397 : getType(B: LHSValue.Base).getNonReferenceType(),
19398 A: LHSDesignator, B: RHSDesignator, WasArrayIndex);
19399 // At the point where the designators diverge, the comparison has a
19400 // specified value if:
19401 // - we are comparing array indices
19402 // - we are comparing fields of a union, or fields with the same access
19403 // Otherwise, the result is unspecified and thus the comparison is not a
19404 // constant expression.
19405 if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
19406 Mismatch < RHSDesignator.Entries.size()) {
19407 const FieldDecl *LF = getAsField(E: LHSDesignator.Entries[Mismatch]);
19408 const FieldDecl *RF = getAsField(E: RHSDesignator.Entries[Mismatch]);
19409 if (!LF && !RF)
19410 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_classes);
19411 else if (!LF)
19412 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
19413 << getAsBaseClass(E: LHSDesignator.Entries[Mismatch])
19414 << RF->getParent() << RF;
19415 else if (!RF)
19416 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
19417 << getAsBaseClass(E: RHSDesignator.Entries[Mismatch])
19418 << LF->getParent() << LF;
19419 else if (!LF->getParent()->isUnion() &&
19420 LF->getAccess() != RF->getAccess())
19421 Info.CCEDiag(E,
19422 DiagId: diag::note_constexpr_pointer_comparison_differing_access)
19423 << LF << LF->getAccess() << RF << RF->getAccess()
19424 << LF->getParent();
19425 }
19426 }
19427
19428 // The comparison here must be unsigned, and performed with the same
19429 // width as the pointer.
19430 unsigned PtrSize = Info.Ctx.getTypeSize(T: LHSTy);
19431 uint64_t CompareLHS = LHSOffset.getQuantity();
19432 uint64_t CompareRHS = RHSOffset.getQuantity();
19433 assert(PtrSize <= 64 && "Unexpected pointer width");
19434 uint64_t Mask = ~0ULL >> (64 - PtrSize);
19435 CompareLHS &= Mask;
19436 CompareRHS &= Mask;
19437
19438 // If there is a base and this is a relational operator, we can only
19439 // compare pointers within the object in question; otherwise, the result
19440 // depends on where the object is located in memory.
19441 if (!LHSValue.Base.isNull() && IsRelational) {
19442 QualType BaseTy = getType(B: LHSValue.Base).getNonReferenceType();
19443 if (BaseTy->isIncompleteType())
19444 return Error(E);
19445 CharUnits Size = Info.Ctx.getTypeSizeInChars(T: BaseTy);
19446 uint64_t OffsetLimit = Size.getQuantity();
19447 if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)
19448 return Error(E);
19449 }
19450
19451 if (CompareLHS < CompareRHS)
19452 return Success(CmpResult::Less, E);
19453 if (CompareLHS > CompareRHS)
19454 return Success(CmpResult::Greater, E);
19455 return Success(CmpResult::Equal, E);
19456 }
19457
19458 if (LHSTy->isMemberPointerType()) {
19459 assert(IsEquality && "unexpected member pointer operation");
19460 assert(RHSTy->isMemberPointerType() && "invalid comparison");
19461
19462 MemberPtr LHSValue, RHSValue;
19463
19464 bool LHSOK = EvaluateMemberPointer(E: E->getLHS(), Result&: LHSValue, Info);
19465 if (!LHSOK && !Info.noteFailure())
19466 return false;
19467
19468 if (!EvaluateMemberPointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19469 return false;
19470
19471 // If either operand is a pointer to a weak function, the comparison is not
19472 // constant.
19473 if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) {
19474 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19475 << LHSValue.getDecl();
19476 return false;
19477 }
19478 if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) {
19479 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19480 << RHSValue.getDecl();
19481 return false;
19482 }
19483
19484 // C++11 [expr.eq]p2:
19485 // If both operands are null, they compare equal. Otherwise if only one is
19486 // null, they compare unequal.
19487 if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
19488 bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
19489 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19490 }
19491
19492 // Otherwise if either is a pointer to a virtual member function, the
19493 // result is unspecified.
19494 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: LHSValue.getDecl()))
19495 if (MD->isVirtual())
19496 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19497 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: RHSValue.getDecl()))
19498 if (MD->isVirtual())
19499 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19500
19501 // Otherwise they compare equal if and only if they would refer to the
19502 // same member of the same most derived object or the same subobject if
19503 // they were dereferenced with a hypothetical object of the associated
19504 // class type.
19505 bool Equal = LHSValue == RHSValue;
19506 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19507 }
19508
19509 if (LHSTy->isNullPtrType()) {
19510 assert(E->isComparisonOp() && "unexpected nullptr operation");
19511 assert(RHSTy->isNullPtrType() && "missing pointer conversion");
19512 // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
19513 // are compared, the result is true of the operator is <=, >= or ==, and
19514 // false otherwise.
19515 LValue Res;
19516 if (!EvaluatePointer(E: E->getLHS(), Result&: Res, Info) ||
19517 !EvaluatePointer(E: E->getRHS(), Result&: Res, Info))
19518 return false;
19519 return Success(CmpResult::Equal, E);
19520 }
19521
19522 return DoAfter();
19523}
19524
19525static bool EvaluateComparisonResult(EvalInfo &Info, const Expr *E,
19526 ComparisonCategoryResult CCR,
19527 APValue &Result) {
19528 const ComparisonCategoryInfo &CmpInfo =
19529 Info.Ctx.CompCategories.getInfoForType(Ty: E->getType());
19530 const VarDecl *VD = CmpInfo.getValueInfo(ValueKind: CmpInfo.makeWeakResult(Res: CCR))->VD;
19531
19532 // Check and evaluate the result as a constant expression.
19533 LValue LV;
19534 LV.set(B: VD);
19535 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
19536 return false;
19537 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
19538 Kind: ConstantExprKind::Normal);
19539}
19540
19541bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) {
19542 if (!CheckLiteralType(Info, E))
19543 return false;
19544
19545 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19546 ComparisonCategoryResult CCR;
19547 switch (CR) {
19548 case CmpResult::Unequal:
19549 llvm_unreachable("should never produce Unequal for three-way comparison");
19550 case CmpResult::Less:
19551 CCR = ComparisonCategoryResult::Less;
19552 break;
19553 case CmpResult::Equal:
19554 CCR = ComparisonCategoryResult::Equal;
19555 break;
19556 case CmpResult::Greater:
19557 CCR = ComparisonCategoryResult::Greater;
19558 break;
19559 case CmpResult::Unordered:
19560 CCR = ComparisonCategoryResult::Unordered;
19561 break;
19562 }
19563 return EvaluateComparisonResult(Info, E, CCR, Result);
19564 };
19565 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19566 return ExprEvaluatorBaseTy::VisitBinCmp(S: E);
19567 });
19568}
19569
19570bool RecordExprEvaluator::VisitTypeTraitExpr(const TypeTraitExpr *E) {
19571 if (!CheckLiteralType(Info, E))
19572 return false;
19573
19574 assert(E->isStoredAsComparisonResult() &&
19575 "expected a strong_ordering type trait with a stored value");
19576
19577 ComparisonCategoryResult CCR = static_cast<ComparisonCategoryResult>(
19578 E->getAPValue().getInt().getZExtValue());
19579 return EvaluateComparisonResult(Info, E, CCR, Result);
19580}
19581
19582bool RecordExprEvaluator::VisitCXXParenListInitExpr(
19583 const CXXParenListInitExpr *E) {
19584 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs());
19585}
19586
19587bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
19588 // We don't support assignment in C. C++ assignments don't get here because
19589 // assignment is an lvalue in C++.
19590 if (E->isAssignmentOp()) {
19591 Error(E);
19592 if (!Info.noteFailure())
19593 return false;
19594 }
19595
19596 if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E))
19597 return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E);
19598
19599 assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() ||
19600 !E->getRHS()->getType()->isIntegralOrEnumerationType()) &&
19601 "DataRecursiveIntBinOpEvaluator should have handled integral types");
19602
19603 if (E->isComparisonOp()) {
19604 // Evaluate builtin binary comparisons by evaluating them as three-way
19605 // comparisons and then translating the result.
19606 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19607 assert((CR != CmpResult::Unequal || E->isEqualityOp()) &&
19608 "should only produce Unequal for equality comparisons");
19609 bool IsEqual = CR == CmpResult::Equal,
19610 IsLess = CR == CmpResult::Less,
19611 IsGreater = CR == CmpResult::Greater;
19612 auto Op = E->getOpcode();
19613 switch (Op) {
19614 default:
19615 llvm_unreachable("unsupported binary operator");
19616 case BO_EQ:
19617 case BO_NE:
19618 return Success(Value: IsEqual == (Op == BO_EQ), E);
19619 case BO_LT:
19620 return Success(Value: IsLess, E);
19621 case BO_GT:
19622 return Success(Value: IsGreater, E);
19623 case BO_LE:
19624 return Success(Value: IsEqual || IsLess, E);
19625 case BO_GE:
19626 return Success(Value: IsEqual || IsGreater, E);
19627 }
19628 };
19629 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19630 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19631 });
19632 }
19633
19634 QualType LHSTy = E->getLHS()->getType();
19635 QualType RHSTy = E->getRHS()->getType();
19636
19637 if (LHSTy->isPointerType() && RHSTy->isPointerType() &&
19638 E->getOpcode() == BO_Sub) {
19639 LValue LHSValue, RHSValue;
19640
19641 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
19642 if (!LHSOK && !Info.noteFailure())
19643 return false;
19644
19645 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19646 return false;
19647
19648 // Reject differing bases from the normal codepath; we special-case
19649 // comparisons to null.
19650 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
19651 if (Info.checkingPotentialConstantExpression() &&
19652 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
19653 return false;
19654
19655 const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>();
19656 const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>();
19657
19658 auto DiagArith = [&](unsigned DiagID) {
19659 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
19660 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
19661 Info.FFDiag(E, DiagId: DiagID) << LHS << RHS;
19662 if (LHSExpr && LHSExpr == RHSExpr)
19663 Info.Note(Loc: LHSExpr->getExprLoc(),
19664 DiagId: diag::note_constexpr_repeated_literal_eval)
19665 << LHSExpr->getSourceRange();
19666 return false;
19667 };
19668
19669 if (!LHSExpr || !RHSExpr)
19670 return DiagArith(diag::note_constexpr_pointer_arith_unspecified);
19671
19672 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
19673 return DiagArith(diag::note_constexpr_literal_arith);
19674
19675 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
19676 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
19677 if (!LHSAddrExpr || !RHSAddrExpr)
19678 return Error(E);
19679 // Make sure both labels come from the same function.
19680 if (LHSAddrExpr->getLabel()->getDeclContext() !=
19681 RHSAddrExpr->getLabel()->getDeclContext())
19682 return Error(E);
19683 return Success(V: APValue(LHSAddrExpr, RHSAddrExpr), E);
19684 }
19685 const CharUnits &LHSOffset = LHSValue.getLValueOffset();
19686 const CharUnits &RHSOffset = RHSValue.getLValueOffset();
19687
19688 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
19689 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
19690
19691 // C++11 [expr.add]p6:
19692 // Unless both pointers point to elements of the same array object, or
19693 // one past the last element of the array object, the behavior is
19694 // undefined.
19695 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
19696 !AreElementsOfSameArray(ObjType: getType(B: LHSValue.Base), A: LHSDesignator,
19697 B: RHSDesignator))
19698 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_not_same_array);
19699
19700 QualType Type = E->getLHS()->getType();
19701 QualType ElementType = Type->castAs<PointerType>()->getPointeeType();
19702
19703 CharUnits ElementSize;
19704 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElementType, Size&: ElementSize))
19705 return false;
19706
19707 // As an extension, a type may have zero size (empty struct or union in
19708 // C, array of zero length). Pointer subtraction in such cases has
19709 // undefined behavior, so is not constant.
19710 if (ElementSize.isZero()) {
19711 Info.FFDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_zero_size)
19712 << ElementType;
19713 return false;
19714 }
19715
19716 // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
19717 // and produce incorrect results when it overflows. Such behavior
19718 // appears to be non-conforming, but is common, so perhaps we should
19719 // assume the standard intended for such cases to be undefined behavior
19720 // and check for them.
19721
19722 // Compute (LHSOffset - RHSOffset) / Size carefully, checking for
19723 // overflow in the final conversion to ptrdiff_t.
19724 APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
19725 APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
19726 APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true),
19727 false);
19728 APSInt TrueResult = (LHS - RHS) / ElemSize;
19729 APSInt Result = TrueResult.trunc(width: Info.Ctx.getIntWidth(T: E->getType()));
19730
19731 if (Result.extend(width: 65) != TrueResult &&
19732 !HandleOverflow(Info, E, SrcValue: TrueResult, DestType: E->getType()))
19733 return false;
19734 return Success(SI: Result, E);
19735 }
19736
19737 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19738}
19739
19740/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
19741/// a result as the expression's type.
19742bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
19743 const UnaryExprOrTypeTraitExpr *E) {
19744 switch(E->getKind()) {
19745 case UETT_PreferredAlignOf:
19746 case UETT_AlignOf: {
19747 if (E->isArgumentType())
19748 return Success(
19749 Size: GetAlignOfType(Ctx: Info.Ctx, T: E->getArgumentType(), ExprKind: E->getKind()), E);
19750 else
19751 return Success(
19752 Size: GetAlignOfExpr(Ctx: Info.Ctx, E: E->getArgumentExpr(), ExprKind: E->getKind()), E);
19753 }
19754
19755 case UETT_PtrAuthTypeDiscriminator: {
19756 if (E->getArgumentType()->isDependentType())
19757 return false;
19758 return Success(
19759 Value: Info.Ctx.getPointerAuthTypeDiscriminator(T: E->getArgumentType()), E);
19760 }
19761 case UETT_VecStep: {
19762 QualType Ty = E->getTypeOfArgument();
19763
19764 if (Ty->isVectorType()) {
19765 unsigned n = Ty->castAs<VectorType>()->getNumElements();
19766
19767 // The vec_step built-in functions that take a 3-component
19768 // vector return 4. (OpenCL 1.1 spec 6.11.12)
19769 if (n == 3)
19770 n = 4;
19771
19772 return Success(Value: n, E);
19773 } else
19774 return Success(Value: 1, E);
19775 }
19776
19777 case UETT_DataSizeOf:
19778 case UETT_SizeOf: {
19779 QualType SrcTy = E->getTypeOfArgument();
19780 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
19781 // the result is the size of the referenced type."
19782 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
19783 SrcTy = Ref->getPointeeType();
19784
19785 CharUnits Sizeof;
19786 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: SrcTy, Size&: Sizeof,
19787 SOT: E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf
19788 : SizeOfType::SizeOf)) {
19789 return false;
19790 }
19791 return Success(Size: Sizeof, E);
19792 }
19793 case UETT_OpenMPRequiredSimdAlign:
19794 assert(E->isArgumentType());
19795 return Success(
19796 Value: Info.Ctx.toCharUnitsFromBits(
19797 BitSize: Info.Ctx.getOpenMPDefaultSimdAlign(T: E->getArgumentType()))
19798 .getQuantity(),
19799 E);
19800 case UETT_VectorElements: {
19801 QualType Ty = E->getTypeOfArgument();
19802 // If the vector has a fixed size, we can determine the number of elements
19803 // at compile time.
19804 if (const auto *VT = Ty->getAs<VectorType>())
19805 return Success(Value: VT->getNumElements(), E);
19806
19807 assert(Ty->isSizelessVectorType());
19808 if (Info.InConstantContext)
19809 Info.CCEDiag(E, DiagId: diag::note_constexpr_non_const_vectorelements)
19810 << E->getSourceRange();
19811
19812 return false;
19813 }
19814 case UETT_CountOf: {
19815 QualType Ty = E->getTypeOfArgument();
19816 assert(Ty->isArrayType());
19817
19818 // We don't need to worry about array element qualifiers, so getting the
19819 // unsafe array type is fine.
19820 if (const auto *CAT =
19821 dyn_cast<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe())) {
19822 return Success(I: CAT->getSize(), E);
19823 }
19824
19825 assert(!Ty->isConstantSizeType());
19826
19827 // If it's a variable-length array type, we need to check whether it is a
19828 // multidimensional array. If so, we need to check the size expression of
19829 // the VLA to see if it's a constant size. If so, we can return that value.
19830 const auto *VAT = Info.Ctx.getAsVariableArrayType(T: Ty);
19831 assert(VAT);
19832 if (VAT->getElementType()->isArrayType()) {
19833 // Variable array size expression could be missing (e.g. int a[*][10]) In
19834 // that case, it can't be a constant expression.
19835 if (!VAT->getSizeExpr()) {
19836 Info.FFDiag(Loc: E->getBeginLoc());
19837 return false;
19838 }
19839
19840 std::optional<APSInt> Res =
19841 VAT->getSizeExpr()->getIntegerConstantExpr(Ctx: Info.Ctx);
19842 if (Res) {
19843 // The resulting value always has type size_t, so we need to make the
19844 // returned APInt have the correct sign and bit-width.
19845 APInt Val{
19846 static_cast<unsigned>(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType())),
19847 Res->getZExtValue()};
19848 return Success(I: Val, E);
19849 }
19850 }
19851
19852 // Definitely a variable-length type, which is not an ICE.
19853 // FIXME: Better diagnostic.
19854 Info.FFDiag(Loc: E->getBeginLoc());
19855 return false;
19856 }
19857 }
19858
19859 llvm_unreachable("unknown expr/type trait");
19860}
19861
19862bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
19863 Info.Ctx.recordOffsetOfEvaluation(E: OOE);
19864 CharUnits Result;
19865 unsigned n = OOE->getNumComponents();
19866 if (n == 0)
19867 return Error(E: OOE);
19868 QualType CurrentType = OOE->getTypeSourceInfo()->getType();
19869 for (unsigned i = 0; i != n; ++i) {
19870 OffsetOfNode ON = OOE->getComponent(Idx: i);
19871 switch (ON.getKind()) {
19872 case OffsetOfNode::Array: {
19873 const Expr *Idx = OOE->getIndexExpr(Idx: ON.getArrayExprIndex());
19874 APSInt IdxResult;
19875 if (!EvaluateInteger(E: Idx, Result&: IdxResult, Info))
19876 return false;
19877 const ArrayType *AT = Info.Ctx.getAsArrayType(T: CurrentType);
19878 if (!AT)
19879 return Error(E: OOE);
19880 CurrentType = AT->getElementType();
19881 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(T: CurrentType);
19882 // Reject negative indices, indices too large to fit in int64_t,
19883 // and overflow in the offset computation.
19884 if (IdxResult.isNegative() || IdxResult.getActiveBits() > 63)
19885 return Error(E: OOE);
19886 int64_t IdxVal = IdxResult.getExtValue();
19887 int64_t ElemSize = ElementSize.getQuantity();
19888 if (IdxVal != 0 &&
19889 ElemSize > std::numeric_limits<int64_t>::max() / IdxVal)
19890 return Error(E: OOE, D: diag::note_constexpr_offsetof_overflow);
19891 int64_t Offset = IdxVal * ElemSize;
19892 if (Result.getQuantity() > std::numeric_limits<int64_t>::max() - Offset)
19893 return Error(E: OOE, D: diag::note_constexpr_offsetof_overflow);
19894 Result += CharUnits::fromQuantity(Quantity: Offset);
19895 break;
19896 }
19897
19898 case OffsetOfNode::Field: {
19899 FieldDecl *MemberDecl = ON.getField();
19900 const auto *RD = CurrentType->getAsRecordDecl();
19901 if (!RD)
19902 return Error(E: OOE);
19903 if (RD->isInvalidDecl()) return false;
19904 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
19905 unsigned i = MemberDecl->getFieldIndex();
19906 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
19907 Result += Info.Ctx.toCharUnitsFromBits(BitSize: RL.getFieldOffset(FieldNo: i));
19908 CurrentType = MemberDecl->getType().getNonReferenceType();
19909 break;
19910 }
19911
19912 case OffsetOfNode::Identifier:
19913 llvm_unreachable("dependent __builtin_offsetof");
19914
19915 case OffsetOfNode::Base: {
19916 CXXBaseSpecifier *BaseSpec = ON.getBase();
19917 if (BaseSpec->isVirtual())
19918 return Error(E: OOE);
19919
19920 // Find the layout of the class whose base we are looking into.
19921 const auto *RD = CurrentType->getAsCXXRecordDecl();
19922 if (!RD)
19923 return Error(E: OOE);
19924 if (RD->isInvalidDecl()) return false;
19925 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
19926
19927 // Find the base class itself.
19928 CurrentType = BaseSpec->getType();
19929 const auto *BaseRD = CurrentType->getAsCXXRecordDecl();
19930 if (!BaseRD)
19931 return Error(E: OOE);
19932
19933 // Add the offset to the base.
19934 Result += RL.getBaseClassOffset(Base: BaseRD);
19935 break;
19936 }
19937 }
19938 }
19939 return Success(Size: Result, E: OOE);
19940}
19941
19942bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
19943 switch (E->getOpcode()) {
19944 default:
19945 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
19946 // See C99 6.6p3.
19947 return Error(E);
19948 case UO_Extension:
19949 // FIXME: Should extension allow i-c-e extension expressions in its scope?
19950 // If so, we could clear the diagnostic ID.
19951 return Visit(S: E->getSubExpr());
19952 case UO_Plus:
19953 // The result is just the value.
19954 return Visit(S: E->getSubExpr());
19955 case UO_Minus: {
19956 if (!Visit(S: E->getSubExpr()))
19957 return false;
19958 if (!Result.isInt()) return Error(E);
19959 const APSInt &Value = Result.getInt();
19960 if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() &&
19961 !E->getType().isWrapType()) {
19962 if (Info.checkingForUndefinedBehavior())
19963 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
19964 DiagID: diag::warn_integer_constant_overflow)
19965 << toString(I: Value, Radix: 10, Signed: Value.isSigned(), /*formatAsCLiteral=*/false,
19966 /*UpperCase=*/true, /*InsertSeparators=*/true)
19967 << E->getType() << E->getSourceRange();
19968
19969 if (!HandleOverflow(Info, E, SrcValue: -Value.extend(width: Value.getBitWidth() + 1),
19970 DestType: E->getType()))
19971 return false;
19972 }
19973 return Success(SI: -Value, E);
19974 }
19975 case UO_Not: {
19976 if (!Visit(S: E->getSubExpr()))
19977 return false;
19978 if (!Result.isInt()) return Error(E);
19979 return Success(SI: ~Result.getInt(), E);
19980 }
19981 case UO_LNot: {
19982 bool bres;
19983 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
19984 return false;
19985 return Success(Value: !bres, E);
19986 }
19987 }
19988}
19989
19990/// HandleCast - This is used to evaluate implicit or explicit casts where the
19991/// result type is integer.
19992bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
19993 const Expr *SubExpr = E->getSubExpr();
19994 QualType DestType = E->getType();
19995 QualType SrcType = SubExpr->getType();
19996
19997 switch (E->getCastKind()) {
19998 case CK_BaseToDerived:
19999 case CK_DerivedToBase:
20000 case CK_UncheckedDerivedToBase:
20001 case CK_Dynamic:
20002 case CK_ToUnion:
20003 case CK_ArrayToPointerDecay:
20004 case CK_FunctionToPointerDecay:
20005 case CK_NullToPointer:
20006 case CK_NullToMemberPointer:
20007 case CK_BaseToDerivedMemberPointer:
20008 case CK_DerivedToBaseMemberPointer:
20009 case CK_ReinterpretMemberPointer:
20010 case CK_ConstructorConversion:
20011 case CK_IntegralToPointer:
20012 case CK_ToVoid:
20013 case CK_VectorSplat:
20014 case CK_IntegralToFloating:
20015 case CK_FloatingCast:
20016 case CK_CPointerToObjCPointerCast:
20017 case CK_BlockPointerToObjCPointerCast:
20018 case CK_AnyPointerToBlockPointerCast:
20019 case CK_ObjCObjectLValueCast:
20020 case CK_FloatingRealToComplex:
20021 case CK_FloatingComplexToReal:
20022 case CK_FloatingComplexCast:
20023 case CK_FloatingComplexToIntegralComplex:
20024 case CK_IntegralRealToComplex:
20025 case CK_IntegralComplexCast:
20026 case CK_IntegralComplexToFloatingComplex:
20027 case CK_BuiltinFnToFnPtr:
20028 case CK_ZeroToOCLOpaqueType:
20029 case CK_NonAtomicToAtomic:
20030 case CK_AddressSpaceConversion:
20031 case CK_IntToOCLSampler:
20032 case CK_FloatingToFixedPoint:
20033 case CK_FixedPointToFloating:
20034 case CK_FixedPointCast:
20035 case CK_IntegralToFixedPoint:
20036 case CK_MatrixCast:
20037 case CK_HLSLAggregateSplatCast:
20038 llvm_unreachable("invalid cast kind for integral value");
20039
20040 case CK_BitCast:
20041 case CK_Dependent:
20042 case CK_LValueBitCast:
20043 case CK_ARCProduceObject:
20044 case CK_ARCConsumeObject:
20045 case CK_ARCReclaimReturnedObject:
20046 case CK_ARCExtendBlockObject:
20047 case CK_CopyAndAutoreleaseBlockObject:
20048 return Error(E);
20049
20050 case CK_UserDefinedConversion:
20051 case CK_LValueToRValue:
20052 case CK_AtomicToNonAtomic:
20053 case CK_NoOp:
20054 case CK_LValueToRValueBitCast:
20055 case CK_HLSLArrayRValue:
20056 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20057
20058 case CK_MemberPointerToBoolean:
20059 case CK_PointerToBoolean:
20060 case CK_IntegralToBoolean:
20061 case CK_FloatingToBoolean:
20062 case CK_BooleanToSignedIntegral:
20063 case CK_FloatingComplexToBoolean:
20064 case CK_IntegralComplexToBoolean: {
20065 bool BoolResult;
20066 if (!EvaluateAsBooleanCondition(E: SubExpr, Result&: BoolResult, Info))
20067 return false;
20068 uint64_t IntResult = BoolResult;
20069 if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral)
20070 IntResult = (uint64_t)-1;
20071 return Success(Value: IntResult, E);
20072 }
20073
20074 case CK_FixedPointToIntegral: {
20075 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SrcType));
20076 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
20077 return false;
20078 bool Overflowed;
20079 llvm::APSInt Result = Src.convertToInt(
20080 DstWidth: Info.Ctx.getIntWidth(T: DestType),
20081 DstSign: DestType->isSignedIntegerOrEnumerationType(), Overflow: &Overflowed);
20082 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Result, DestType))
20083 return false;
20084 return Success(SI: Result, E);
20085 }
20086
20087 case CK_FixedPointToBoolean: {
20088 // Unsigned padding does not affect this.
20089 APValue Val;
20090 if (!Evaluate(Result&: Val, Info, E: SubExpr))
20091 return false;
20092 return Success(Value: Val.getFixedPoint().getBoolValue(), E);
20093 }
20094
20095 case CK_IntegralCast: {
20096 if (!Visit(S: SubExpr))
20097 return false;
20098
20099 if (!Result.isInt()) {
20100 // Allow casts of address-of-label differences if they are no-ops
20101 // or narrowing, if the result is at least 32 bits wide.
20102 // (The narrowing case isn't actually guaranteed to
20103 // be constant-evaluatable except in some narrow cases which are hard
20104 // to detect here. We let it through on the assumption the user knows
20105 // what they are doing.)
20106 if (Result.isAddrLabelDiff()) {
20107 unsigned DestBits = Info.Ctx.getTypeSize(T: DestType);
20108 return DestBits >= 32 && DestBits <= Info.Ctx.getTypeSize(T: SrcType);
20109 }
20110 // Only allow casts of lvalues if they are lossless.
20111 return Info.Ctx.getTypeSize(T: DestType) == Info.Ctx.getTypeSize(T: SrcType);
20112 }
20113
20114 if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) {
20115 const auto *ED = DestType->getAsEnumDecl();
20116 // Check that the value is within the range of the enumeration values.
20117 //
20118 // This corressponds to [expr.static.cast]p10 which says:
20119 // A value of integral or enumeration type can be explicitly converted
20120 // to a complete enumeration type ... If the enumeration type does not
20121 // have a fixed underlying type, the value is unchanged if the original
20122 // value is within the range of the enumeration values ([dcl.enum]), and
20123 // otherwise, the behavior is undefined.
20124 //
20125 // This was resolved as part of DR2338 which has CD5 status.
20126 if (!ED->isFixed()) {
20127 llvm::APInt Min;
20128 llvm::APInt Max;
20129
20130 ED->getValueRange(Max, Min);
20131 --Max;
20132
20133 if (ED->getNumNegativeBits() &&
20134 (Max.slt(RHS: Result.getInt().getSExtValue()) ||
20135 Min.sgt(RHS: Result.getInt().getSExtValue())))
20136 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
20137 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getSExtValue()
20138 << Max.getSExtValue() << ED;
20139 else if (!ED->getNumNegativeBits() &&
20140 Max.ult(RHS: Result.getInt().getZExtValue()))
20141 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
20142 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getZExtValue()
20143 << Max.getZExtValue() << ED;
20144 }
20145 }
20146
20147 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType,
20148 Value: Result.getInt()), E);
20149 }
20150
20151 case CK_PointerToIntegral: {
20152 CCEDiag(E, D: diag::note_constexpr_invalid_cast_ptrtoint)
20153 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
20154 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
20155
20156 LValue LV;
20157 if (!EvaluatePointer(E: SubExpr, Result&: LV, Info))
20158 return false;
20159
20160 if (LV.getLValueBase()) {
20161 CCEDiag(E, D: diag::note_constexpr_has_lvalue) << E->getSourceRange();
20162 // Only allow based lvalue casts if they are lossless.
20163 // FIXME: Allow a larger integer size than the pointer size, and allow
20164 // narrowing back down to pointer width in subsequent integral casts.
20165 // FIXME: Check integer type's active bits, not its type size.
20166 if (Info.Ctx.getTypeSize(T: DestType) != Info.Ctx.getTypeSize(T: SrcType))
20167 return Error(E);
20168
20169 LV.Designator.setInvalid();
20170 LV.moveInto(V&: Result);
20171 return true;
20172 }
20173
20174 APSInt AsInt;
20175 APValue V;
20176 LV.moveInto(V);
20177 if (!V.toIntegralConstant(Result&: AsInt, SrcTy: SrcType, Ctx: Info.Ctx))
20178 llvm_unreachable("Can't cast this!");
20179
20180 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType, Value: AsInt), E);
20181 }
20182
20183 case CK_IntegralComplexToReal: {
20184 ComplexValue C;
20185 if (!EvaluateComplex(E: SubExpr, Res&: C, Info))
20186 return false;
20187 return Success(SI: C.getComplexIntReal(), E);
20188 }
20189
20190 case CK_FloatingToIntegral: {
20191 APFloat F(0.0);
20192 if (!EvaluateFloat(E: SubExpr, Result&: F, Info))
20193 return false;
20194
20195 APSInt Value;
20196 if (!HandleFloatToIntCast(Info, E, SrcType, Value: F, DestType, Result&: Value))
20197 return false;
20198 return Success(SI: Value, E);
20199 }
20200 case CK_HLSLVectorTruncation: {
20201 APValue Val;
20202 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
20203 return Error(E);
20204 return Success(V: Val.getVectorElt(I: 0), E);
20205 }
20206 case CK_HLSLMatrixTruncation: {
20207 APValue Val;
20208 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
20209 return Error(E);
20210 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), E);
20211 }
20212 case CK_HLSLElementwiseCast: {
20213 SmallVector<APValue> SrcVals;
20214 SmallVector<QualType> SrcTypes;
20215
20216 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: DestType, SrcVals, SrcTypes))
20217 return false;
20218
20219 // cast our single element
20220 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
20221 APValue ResultVal;
20222 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: DestType, Original: SrcVals[0],
20223 Result&: ResultVal))
20224 return false;
20225 return Success(V: ResultVal, E);
20226 }
20227 }
20228
20229 llvm_unreachable("unknown cast resulting in integral value");
20230}
20231
20232bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
20233 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20234 ComplexValue LV;
20235 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
20236 return false;
20237 if (!LV.isComplexInt())
20238 return Error(E);
20239 return Success(SI: LV.getComplexIntReal(), E);
20240 }
20241
20242 return Visit(S: E->getSubExpr());
20243}
20244
20245bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
20246 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
20247 ComplexValue LV;
20248 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
20249 return false;
20250 if (!LV.isComplexInt())
20251 return Error(E);
20252 return Success(SI: LV.getComplexIntImag(), E);
20253 }
20254
20255 VisitIgnoredValue(E: E->getSubExpr());
20256 return Success(Value: 0, E);
20257}
20258
20259bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
20260 return Success(Value: E->getPackLength(), E);
20261}
20262
20263bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
20264 return Success(Value: E->getValue(), E);
20265}
20266
20267bool IntExprEvaluator::VisitConceptSpecializationExpr(
20268 const ConceptSpecializationExpr *E) {
20269 return Success(Value: E->isSatisfied(), E);
20270}
20271
20272bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) {
20273 return Success(Value: E->isSatisfied(), E);
20274}
20275
20276bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20277 switch (E->getOpcode()) {
20278 default:
20279 // Invalid unary operators
20280 return Error(E);
20281 case UO_Plus:
20282 // The result is just the value.
20283 return Visit(S: E->getSubExpr());
20284 case UO_Minus: {
20285 if (!Visit(S: E->getSubExpr())) return false;
20286 if (!Result.isFixedPoint())
20287 return Error(E);
20288 bool Overflowed;
20289 APFixedPoint Negated = Result.getFixedPoint().negate(Overflow: &Overflowed);
20290 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Negated, DestType: E->getType()))
20291 return false;
20292 return Success(V: Negated, E);
20293 }
20294 case UO_LNot: {
20295 bool bres;
20296 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
20297 return false;
20298 return Success(Value: !bres, E);
20299 }
20300 }
20301}
20302
20303bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) {
20304 const Expr *SubExpr = E->getSubExpr();
20305 QualType DestType = E->getType();
20306 assert(DestType->isFixedPointType() &&
20307 "Expected destination type to be a fixed point type");
20308 auto DestFXSema = Info.Ctx.getFixedPointSemantics(Ty: DestType);
20309
20310 switch (E->getCastKind()) {
20311 case CK_FixedPointCast: {
20312 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
20313 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
20314 return false;
20315 bool Overflowed;
20316 APFixedPoint Result = Src.convert(DstSema: DestFXSema, Overflow: &Overflowed);
20317 if (Overflowed) {
20318 if (Info.checkingForUndefinedBehavior())
20319 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20320 DiagID: diag::warn_fixedpoint_constant_overflow)
20321 << Result.toString() << E->getType();
20322 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20323 return false;
20324 }
20325 return Success(V: Result, E);
20326 }
20327 case CK_IntegralToFixedPoint: {
20328 APSInt Src;
20329 if (!EvaluateInteger(E: SubExpr, Result&: Src, Info))
20330 return false;
20331
20332 bool Overflowed;
20333 APFixedPoint IntResult = APFixedPoint::getFromIntValue(
20334 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
20335
20336 if (Overflowed) {
20337 if (Info.checkingForUndefinedBehavior())
20338 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20339 DiagID: diag::warn_fixedpoint_constant_overflow)
20340 << IntResult.toString() << E->getType();
20341 if (!HandleOverflow(Info, E, SrcValue: IntResult, DestType: E->getType()))
20342 return false;
20343 }
20344
20345 return Success(V: IntResult, E);
20346 }
20347 case CK_FloatingToFixedPoint: {
20348 APFloat Src(0.0);
20349 if (!EvaluateFloat(E: SubExpr, Result&: Src, Info))
20350 return false;
20351
20352 bool Overflowed;
20353 APFixedPoint Result = APFixedPoint::getFromFloatValue(
20354 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
20355
20356 if (Overflowed) {
20357 if (Info.checkingForUndefinedBehavior())
20358 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20359 DiagID: diag::warn_fixedpoint_constant_overflow)
20360 << Result.toString() << E->getType();
20361 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20362 return false;
20363 }
20364
20365 return Success(V: Result, E);
20366 }
20367 case CK_NoOp:
20368 case CK_LValueToRValue:
20369 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20370 default:
20371 return Error(E);
20372 }
20373}
20374
20375bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20376 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20377 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20378
20379 const Expr *LHS = E->getLHS();
20380 const Expr *RHS = E->getRHS();
20381 FixedPointSemantics ResultFXSema =
20382 Info.Ctx.getFixedPointSemantics(Ty: E->getType());
20383
20384 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHS->getType()));
20385 if (!EvaluateFixedPointOrInteger(E: LHS, Result&: LHSFX, Info))
20386 return false;
20387 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHS->getType()));
20388 if (!EvaluateFixedPointOrInteger(E: RHS, Result&: RHSFX, Info))
20389 return false;
20390
20391 bool OpOverflow = false, ConversionOverflow = false;
20392 APFixedPoint Result(LHSFX.getSemantics());
20393 switch (E->getOpcode()) {
20394 case BO_Add: {
20395 Result = LHSFX.add(Other: RHSFX, Overflow: &OpOverflow)
20396 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20397 break;
20398 }
20399 case BO_Sub: {
20400 Result = LHSFX.sub(Other: RHSFX, Overflow: &OpOverflow)
20401 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20402 break;
20403 }
20404 case BO_Mul: {
20405 Result = LHSFX.mul(Other: RHSFX, Overflow: &OpOverflow)
20406 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20407 break;
20408 }
20409 case BO_Div: {
20410 if (RHSFX.getValue() == 0) {
20411 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero);
20412 return false;
20413 }
20414 Result = LHSFX.div(Other: RHSFX, Overflow: &OpOverflow)
20415 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20416 break;
20417 }
20418 case BO_Shl:
20419 case BO_Shr: {
20420 FixedPointSemantics LHSSema = LHSFX.getSemantics();
20421 llvm::APSInt RHSVal = RHSFX.getValue();
20422
20423 unsigned ShiftBW =
20424 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding();
20425 unsigned Amt = RHSVal.getLimitedValue(Limit: ShiftBW - 1);
20426 // Embedded-C 4.1.6.2.2:
20427 // The right operand must be nonnegative and less than the total number
20428 // of (nonpadding) bits of the fixed-point operand ...
20429 if (RHSVal.isNegative())
20430 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHSVal;
20431 else if (Amt != RHSVal)
20432 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
20433 << RHSVal << E->getType() << ShiftBW;
20434
20435 if (E->getOpcode() == BO_Shl)
20436 Result = LHSFX.shl(Amt, Overflow: &OpOverflow);
20437 else
20438 Result = LHSFX.shr(Amt, Overflow: &OpOverflow);
20439 break;
20440 }
20441 default:
20442 return false;
20443 }
20444 if (OpOverflow || ConversionOverflow) {
20445 if (Info.checkingForUndefinedBehavior())
20446 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20447 DiagID: diag::warn_fixedpoint_constant_overflow)
20448 << Result.toString() << E->getType();
20449 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20450 return false;
20451 }
20452 return Success(V: Result, E);
20453}
20454
20455//===----------------------------------------------------------------------===//
20456// Float Evaluation
20457//===----------------------------------------------------------------------===//
20458
20459namespace {
20460class FloatExprEvaluator
20461 : public ExprEvaluatorBase<FloatExprEvaluator> {
20462 APFloat &Result;
20463public:
20464 FloatExprEvaluator(EvalInfo &info, APFloat &result)
20465 : ExprEvaluatorBaseTy(info), Result(result) {}
20466
20467 bool Success(const APValue &V, const Expr *e) {
20468 Result = V.getFloat();
20469 return true;
20470 }
20471
20472 bool ZeroInitialization(const Expr *E) {
20473 Result = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
20474 return true;
20475 }
20476
20477 bool VisitCallExpr(const CallExpr *E);
20478
20479 bool VisitUnaryOperator(const UnaryOperator *E);
20480 bool VisitBinaryOperator(const BinaryOperator *E);
20481 bool VisitFloatingLiteral(const FloatingLiteral *E);
20482 bool VisitCastExpr(const CastExpr *E);
20483
20484 bool VisitUnaryReal(const UnaryOperator *E);
20485 bool VisitUnaryImag(const UnaryOperator *E);
20486
20487 // FIXME: Missing: array subscript of vector, member of vector
20488};
20489} // end anonymous namespace
20490
20491static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
20492 assert(!E->isValueDependent());
20493 assert(E->isPRValue() && E->getType()->isRealFloatingType());
20494 return FloatExprEvaluator(Info, Result).Visit(S: E);
20495}
20496
20497static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
20498 QualType ResultTy,
20499 const Expr *Arg,
20500 bool SNaN,
20501 llvm::APFloat &Result) {
20502 const StringLiteral *S = dyn_cast<StringLiteral>(Val: Arg->IgnoreParenCasts());
20503 if (!S || !S->isOrdinary())
20504 return false;
20505
20506 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(T: ResultTy);
20507
20508 llvm::APInt fill;
20509
20510 // Treat empty strings as if they were zero.
20511 if (S->getString().empty())
20512 fill = llvm::APInt(32, 0);
20513 else if (S->getString().getAsInteger(Radix: 0, Result&: fill))
20514 return false;
20515
20516 if (Context.getTargetInfo().isNan2008()) {
20517 if (SNaN)
20518 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20519 else
20520 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20521 } else {
20522 // Prior to IEEE 754-2008, architectures were allowed to choose whether
20523 // the first bit of their significand was set for qNaN or sNaN. MIPS chose
20524 // a different encoding to what became a standard in 2008, and for pre-
20525 // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as
20526 // sNaN. This is now known as "legacy NaN" encoding.
20527 if (SNaN)
20528 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20529 else
20530 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20531 }
20532
20533 return true;
20534}
20535
20536bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
20537 if (!IsConstantEvaluatedBuiltinCall(E))
20538 return ExprEvaluatorBaseTy::VisitCallExpr(E);
20539
20540 unsigned BuiltinOp = ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E);
20541
20542 switch (BuiltinOp) {
20543 default:
20544 return false;
20545
20546 case Builtin::BI__builtin_huge_val:
20547 case Builtin::BI__builtin_huge_valf:
20548 case Builtin::BI__builtin_huge_vall:
20549 case Builtin::BI__builtin_huge_valf16:
20550 case Builtin::BI__builtin_huge_valf128:
20551 case Builtin::BI__builtin_inf:
20552 case Builtin::BI__builtin_inff:
20553 case Builtin::BI__builtin_infl:
20554 case Builtin::BI__builtin_inff16:
20555 case Builtin::BI__builtin_inff128: {
20556 const llvm::fltSemantics &Sem =
20557 Info.Ctx.getFloatTypeSemantics(T: E->getType());
20558 Result = llvm::APFloat::getInf(Sem);
20559 return true;
20560 }
20561
20562 case Builtin::BI__builtin_nans:
20563 case Builtin::BI__builtin_nansf:
20564 case Builtin::BI__builtin_nansl:
20565 case Builtin::BI__builtin_nansf16:
20566 case Builtin::BI__builtin_nansf128:
20567 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20568 SNaN: true, Result))
20569 return Error(E);
20570 return true;
20571
20572 case Builtin::BI__builtin_nan:
20573 case Builtin::BI__builtin_nanf:
20574 case Builtin::BI__builtin_nanl:
20575 case Builtin::BI__builtin_nanf16:
20576 case Builtin::BI__builtin_nanf128:
20577 // If this is __builtin_nan() turn this into a nan, otherwise we
20578 // can't constant fold it.
20579 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20580 SNaN: false, Result))
20581 return Error(E);
20582 return true;
20583
20584 case Builtin::BI__builtin_elementwise_abs:
20585 case Builtin::BI__builtin_fabs:
20586 case Builtin::BI__builtin_fabsf:
20587 case Builtin::BI__builtin_fabsl:
20588 case Builtin::BI__builtin_fabsf128:
20589 // The C standard says "fabs raises no floating-point exceptions,
20590 // even if x is a signaling NaN. The returned value is independent of
20591 // the current rounding direction mode." Therefore constant folding can
20592 // proceed without regard to the floating point settings.
20593 // Reference, WG14 N2478 F.10.4.3
20594 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info))
20595 return false;
20596
20597 if (Result.isNegative())
20598 Result.changeSign();
20599 return true;
20600
20601 case Builtin::BI__arithmetic_fence:
20602 return EvaluateFloat(E: E->getArg(Arg: 0), Result, Info);
20603
20604 // FIXME: Builtin::BI__builtin_powi
20605 // FIXME: Builtin::BI__builtin_powif
20606 // FIXME: Builtin::BI__builtin_powil
20607
20608 case Builtin::BI__builtin_copysign:
20609 case Builtin::BI__builtin_copysignf:
20610 case Builtin::BI__builtin_copysignl:
20611 case Builtin::BI__builtin_copysignf128: {
20612 APFloat RHS(0.);
20613 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20614 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20615 return false;
20616 Result.copySign(RHS);
20617 return true;
20618 }
20619
20620 case Builtin::BI__builtin_fmax:
20621 case Builtin::BI__builtin_fmaxf:
20622 case Builtin::BI__builtin_fmaxl:
20623 case Builtin::BI__builtin_fmaxf16:
20624 case Builtin::BI__builtin_fmaxf128: {
20625 APFloat RHS(0.);
20626 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20627 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20628 return false;
20629 Result = maxnum(A: Result, B: RHS);
20630 return true;
20631 }
20632
20633 case Builtin::BI__builtin_fmin:
20634 case Builtin::BI__builtin_fminf:
20635 case Builtin::BI__builtin_fminl:
20636 case Builtin::BI__builtin_fminf16:
20637 case Builtin::BI__builtin_fminf128: {
20638 APFloat RHS(0.);
20639 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20640 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20641 return false;
20642 Result = minnum(A: Result, B: RHS);
20643 return true;
20644 }
20645
20646 case Builtin::BI__builtin_fmaximum_num:
20647 case Builtin::BI__builtin_fmaximum_numf:
20648 case Builtin::BI__builtin_fmaximum_numl:
20649 case Builtin::BI__builtin_fmaximum_numf16:
20650 case Builtin::BI__builtin_fmaximum_numf128: {
20651 APFloat RHS(0.);
20652 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20653 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20654 return false;
20655 Result = maximumnum(A: Result, B: RHS);
20656 return true;
20657 }
20658
20659 case Builtin::BI__builtin_fminimum_num:
20660 case Builtin::BI__builtin_fminimum_numf:
20661 case Builtin::BI__builtin_fminimum_numl:
20662 case Builtin::BI__builtin_fminimum_numf16:
20663 case Builtin::BI__builtin_fminimum_numf128: {
20664 APFloat RHS(0.);
20665 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20666 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20667 return false;
20668 Result = minimumnum(A: Result, B: RHS);
20669 return true;
20670 }
20671
20672 case Builtin::BI__builtin_elementwise_fma: {
20673 if (!E->getArg(Arg: 0)->isPRValue() || !E->getArg(Arg: 1)->isPRValue() ||
20674 !E->getArg(Arg: 2)->isPRValue()) {
20675 return false;
20676 }
20677 APFloat SourceY(0.), SourceZ(0.);
20678 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20679 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: SourceY, Info) ||
20680 !EvaluateFloat(E: E->getArg(Arg: 2), Result&: SourceZ, Info))
20681 return false;
20682 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
20683 (void)Result.fusedMultiplyAdd(Multiplicand: SourceY, Addend: SourceZ, RM);
20684 return true;
20685 }
20686
20687 case clang::X86::BI__builtin_ia32_vec_ext_v4sf: {
20688 APValue Vec;
20689 APSInt IdxAPS;
20690 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
20691 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
20692 return false;
20693 unsigned N = Vec.getVectorLength();
20694 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
20695 return Success(V: Vec.getVectorElt(I: Idx), e: E);
20696 }
20697 }
20698}
20699
20700bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
20701 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20702 ComplexValue CV;
20703 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
20704 return false;
20705 Result = CV.FloatReal;
20706 return true;
20707 }
20708
20709 return Visit(S: E->getSubExpr());
20710}
20711
20712bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
20713 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20714 ComplexValue CV;
20715 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
20716 return false;
20717 Result = CV.FloatImag;
20718 return true;
20719 }
20720
20721 VisitIgnoredValue(E: E->getSubExpr());
20722 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(T: E->getType());
20723 Result = llvm::APFloat::getZero(Sem);
20724 return true;
20725}
20726
20727bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20728 switch (E->getOpcode()) {
20729 default: return Error(E);
20730 case UO_Plus:
20731 return EvaluateFloat(E: E->getSubExpr(), Result, Info);
20732 case UO_Minus:
20733 // In C standard, WG14 N2478 F.3 p4
20734 // "the unary - raises no floating point exceptions,
20735 // even if the operand is signalling."
20736 if (!EvaluateFloat(E: E->getSubExpr(), Result, Info))
20737 return false;
20738 Result.changeSign();
20739 return true;
20740 }
20741}
20742
20743bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20744 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20745 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20746
20747 APFloat RHS(0.0);
20748 bool LHSOK = EvaluateFloat(E: E->getLHS(), Result, Info);
20749 if (!LHSOK && !Info.noteFailure())
20750 return false;
20751 return EvaluateFloat(E: E->getRHS(), Result&: RHS, Info) && LHSOK &&
20752 handleFloatFloatBinOp(Info, E, LHS&: Result, Opcode: E->getOpcode(), RHS);
20753}
20754
20755bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
20756 Result = E->getValue();
20757 return true;
20758}
20759
20760bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
20761 const Expr* SubExpr = E->getSubExpr();
20762
20763 switch (E->getCastKind()) {
20764 default:
20765 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20766
20767 case CK_HLSLAggregateSplatCast:
20768 llvm_unreachable("invalid cast kind for floating value");
20769
20770 case CK_IntegralToFloating: {
20771 APSInt IntResult;
20772 const FPOptions FPO = E->getFPFeaturesInEffect(
20773 LO: Info.Ctx.getLangOpts());
20774 return EvaluateInteger(E: SubExpr, Result&: IntResult, Info) &&
20775 HandleIntToFloatCast(Info, E, FPO, SrcType: SubExpr->getType(),
20776 Value: IntResult, DestType: E->getType(), Result);
20777 }
20778
20779 case CK_FixedPointToFloating: {
20780 APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
20781 if (!EvaluateFixedPoint(E: SubExpr, Result&: FixResult, Info))
20782 return false;
20783 Result =
20784 FixResult.convertToFloat(FloatSema: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
20785 return true;
20786 }
20787
20788 case CK_FloatingCast: {
20789 if (!Visit(S: SubExpr))
20790 return false;
20791 return HandleFloatToFloatCast(Info, E, SrcType: SubExpr->getType(), DestType: E->getType(),
20792 Result);
20793 }
20794
20795 case CK_FloatingComplexToReal: {
20796 ComplexValue V;
20797 if (!EvaluateComplex(E: SubExpr, Res&: V, Info))
20798 return false;
20799 Result = V.getComplexFloatReal();
20800 return true;
20801 }
20802 case CK_HLSLVectorTruncation: {
20803 APValue Val;
20804 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
20805 return Error(E);
20806 return Success(V: Val.getVectorElt(I: 0), e: E);
20807 }
20808 case CK_HLSLMatrixTruncation: {
20809 APValue Val;
20810 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
20811 return Error(E);
20812 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), e: E);
20813 }
20814 case CK_HLSLElementwiseCast: {
20815 SmallVector<APValue> SrcVals;
20816 SmallVector<QualType> SrcTypes;
20817
20818 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: E->getType(), SrcVals,
20819 SrcTypes))
20820 return false;
20821 APValue Val;
20822
20823 // cast our single element
20824 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
20825 APValue ResultVal;
20826 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: E->getType(), Original: SrcVals[0],
20827 Result&: ResultVal))
20828 return false;
20829 return Success(V: ResultVal, e: E);
20830 }
20831 }
20832}
20833
20834//===----------------------------------------------------------------------===//
20835// Complex Evaluation (for float and integer)
20836//===----------------------------------------------------------------------===//
20837
20838namespace {
20839class ComplexExprEvaluator
20840 : public ExprEvaluatorBase<ComplexExprEvaluator> {
20841 ComplexValue &Result;
20842
20843public:
20844 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
20845 : ExprEvaluatorBaseTy(info), Result(Result) {}
20846
20847 bool Success(const APValue &V, const Expr *e) {
20848 Result.setFrom(V);
20849 return true;
20850 }
20851
20852 bool ZeroInitialization(const Expr *E);
20853
20854 //===--------------------------------------------------------------------===//
20855 // Visitor Methods
20856 //===--------------------------------------------------------------------===//
20857
20858 bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
20859 bool VisitCastExpr(const CastExpr *E);
20860 bool VisitBinaryOperator(const BinaryOperator *E);
20861 bool VisitUnaryOperator(const UnaryOperator *E);
20862 bool VisitInitListExpr(const InitListExpr *E);
20863 bool VisitCallExpr(const CallExpr *E);
20864};
20865} // end anonymous namespace
20866
20867static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
20868 EvalInfo &Info) {
20869 assert(!E->isValueDependent());
20870 assert(E->isPRValue() && E->getType()->isAnyComplexType());
20871 return ComplexExprEvaluator(Info, Result).Visit(S: E);
20872}
20873
20874bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
20875 QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();
20876 if (ElemTy->isRealFloatingType()) {
20877 Result.makeComplexFloat();
20878 APFloat Zero = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy));
20879 Result.FloatReal = Zero;
20880 Result.FloatImag = Zero;
20881 } else {
20882 Result.makeComplexInt();
20883 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy);
20884 Result.IntReal = Zero;
20885 Result.IntImag = Zero;
20886 }
20887 return true;
20888}
20889
20890bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
20891 const Expr* SubExpr = E->getSubExpr();
20892
20893 if (SubExpr->getType()->isRealFloatingType()) {
20894 Result.makeComplexFloat();
20895 APFloat &Imag = Result.FloatImag;
20896 if (!EvaluateFloat(E: SubExpr, Result&: Imag, Info))
20897 return false;
20898
20899 Result.FloatReal = APFloat(Imag.getSemantics());
20900 return true;
20901 } else {
20902 assert(SubExpr->getType()->isIntegerType() &&
20903 "Unexpected imaginary literal.");
20904
20905 Result.makeComplexInt();
20906 APSInt &Imag = Result.IntImag;
20907 if (!EvaluateInteger(E: SubExpr, Result&: Imag, Info))
20908 return false;
20909
20910 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
20911 return true;
20912 }
20913}
20914
20915bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
20916
20917 switch (E->getCastKind()) {
20918 case CK_BitCast:
20919 case CK_BaseToDerived:
20920 case CK_DerivedToBase:
20921 case CK_UncheckedDerivedToBase:
20922 case CK_Dynamic:
20923 case CK_ToUnion:
20924 case CK_ArrayToPointerDecay:
20925 case CK_FunctionToPointerDecay:
20926 case CK_NullToPointer:
20927 case CK_NullToMemberPointer:
20928 case CK_BaseToDerivedMemberPointer:
20929 case CK_DerivedToBaseMemberPointer:
20930 case CK_MemberPointerToBoolean:
20931 case CK_ReinterpretMemberPointer:
20932 case CK_ConstructorConversion:
20933 case CK_IntegralToPointer:
20934 case CK_PointerToIntegral:
20935 case CK_PointerToBoolean:
20936 case CK_ToVoid:
20937 case CK_VectorSplat:
20938 case CK_IntegralCast:
20939 case CK_BooleanToSignedIntegral:
20940 case CK_IntegralToBoolean:
20941 case CK_IntegralToFloating:
20942 case CK_FloatingToIntegral:
20943 case CK_FloatingToBoolean:
20944 case CK_FloatingCast:
20945 case CK_CPointerToObjCPointerCast:
20946 case CK_BlockPointerToObjCPointerCast:
20947 case CK_AnyPointerToBlockPointerCast:
20948 case CK_ObjCObjectLValueCast:
20949 case CK_FloatingComplexToReal:
20950 case CK_FloatingComplexToBoolean:
20951 case CK_IntegralComplexToReal:
20952 case CK_IntegralComplexToBoolean:
20953 case CK_ARCProduceObject:
20954 case CK_ARCConsumeObject:
20955 case CK_ARCReclaimReturnedObject:
20956 case CK_ARCExtendBlockObject:
20957 case CK_CopyAndAutoreleaseBlockObject:
20958 case CK_BuiltinFnToFnPtr:
20959 case CK_ZeroToOCLOpaqueType:
20960 case CK_NonAtomicToAtomic:
20961 case CK_AddressSpaceConversion:
20962 case CK_IntToOCLSampler:
20963 case CK_FloatingToFixedPoint:
20964 case CK_FixedPointToFloating:
20965 case CK_FixedPointCast:
20966 case CK_FixedPointToBoolean:
20967 case CK_FixedPointToIntegral:
20968 case CK_IntegralToFixedPoint:
20969 case CK_MatrixCast:
20970 case CK_HLSLVectorTruncation:
20971 case CK_HLSLMatrixTruncation:
20972 case CK_HLSLElementwiseCast:
20973 case CK_HLSLAggregateSplatCast:
20974 llvm_unreachable("invalid cast kind for complex value");
20975
20976 case CK_LValueToRValue:
20977 case CK_AtomicToNonAtomic:
20978 case CK_NoOp:
20979 case CK_LValueToRValueBitCast:
20980 case CK_HLSLArrayRValue:
20981 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20982
20983 case CK_Dependent:
20984 case CK_LValueBitCast:
20985 case CK_UserDefinedConversion:
20986 return Error(E);
20987
20988 case CK_FloatingRealToComplex: {
20989 APFloat &Real = Result.FloatReal;
20990 if (!EvaluateFloat(E: E->getSubExpr(), Result&: Real, Info))
20991 return false;
20992
20993 Result.makeComplexFloat();
20994 Result.FloatImag = APFloat(Real.getSemantics());
20995 return true;
20996 }
20997
20998 case CK_FloatingComplexCast: {
20999 if (!Visit(S: E->getSubExpr()))
21000 return false;
21001
21002 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21003 QualType From
21004 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21005
21006 return HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatReal) &&
21007 HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatImag);
21008 }
21009
21010 case CK_FloatingComplexToIntegralComplex: {
21011 if (!Visit(S: E->getSubExpr()))
21012 return false;
21013
21014 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21015 QualType From
21016 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21017 Result.makeComplexInt();
21018 return HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatReal,
21019 DestType: To, Result&: Result.IntReal) &&
21020 HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatImag,
21021 DestType: To, Result&: Result.IntImag);
21022 }
21023
21024 case CK_IntegralRealToComplex: {
21025 APSInt &Real = Result.IntReal;
21026 if (!EvaluateInteger(E: E->getSubExpr(), Result&: Real, Info))
21027 return false;
21028
21029 Result.makeComplexInt();
21030 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
21031 return true;
21032 }
21033
21034 case CK_IntegralComplexCast: {
21035 if (!Visit(S: E->getSubExpr()))
21036 return false;
21037
21038 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21039 QualType From
21040 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21041
21042 Result.IntReal = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntReal);
21043 Result.IntImag = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntImag);
21044 return true;
21045 }
21046
21047 case CK_IntegralComplexToFloatingComplex: {
21048 if (!Visit(S: E->getSubExpr()))
21049 return false;
21050
21051 const FPOptions FPO = E->getFPFeaturesInEffect(
21052 LO: Info.Ctx.getLangOpts());
21053 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21054 QualType From
21055 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21056 Result.makeComplexFloat();
21057 return HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntReal,
21058 DestType: To, Result&: Result.FloatReal) &&
21059 HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntImag,
21060 DestType: To, Result&: Result.FloatImag);
21061 }
21062 }
21063
21064 llvm_unreachable("unknown cast resulting in complex value");
21065}
21066
21067uint8_t GFNIMultiplicativeInverse(uint8_t Byte) {
21068 // Lookup Table for Multiplicative Inverse in GF(2^8)
21069 const uint8_t GFInv[256] = {
21070 0x00, 0x01, 0x8d, 0xf6, 0xcb, 0x52, 0x7b, 0xd1, 0xe8, 0x4f, 0x29, 0xc0,
21071 0xb0, 0xe1, 0xe5, 0xc7, 0x74, 0xb4, 0xaa, 0x4b, 0x99, 0x2b, 0x60, 0x5f,
21072 0x58, 0x3f, 0xfd, 0xcc, 0xff, 0x40, 0xee, 0xb2, 0x3a, 0x6e, 0x5a, 0xf1,
21073 0x55, 0x4d, 0xa8, 0xc9, 0xc1, 0x0a, 0x98, 0x15, 0x30, 0x44, 0xa2, 0xc2,
21074 0x2c, 0x45, 0x92, 0x6c, 0xf3, 0x39, 0x66, 0x42, 0xf2, 0x35, 0x20, 0x6f,
21075 0x77, 0xbb, 0x59, 0x19, 0x1d, 0xfe, 0x37, 0x67, 0x2d, 0x31, 0xf5, 0x69,
21076 0xa7, 0x64, 0xab, 0x13, 0x54, 0x25, 0xe9, 0x09, 0xed, 0x5c, 0x05, 0xca,
21077 0x4c, 0x24, 0x87, 0xbf, 0x18, 0x3e, 0x22, 0xf0, 0x51, 0xec, 0x61, 0x17,
21078 0x16, 0x5e, 0xaf, 0xd3, 0x49, 0xa6, 0x36, 0x43, 0xf4, 0x47, 0x91, 0xdf,
21079 0x33, 0x93, 0x21, 0x3b, 0x79, 0xb7, 0x97, 0x85, 0x10, 0xb5, 0xba, 0x3c,
21080 0xb6, 0x70, 0xd0, 0x06, 0xa1, 0xfa, 0x81, 0x82, 0x83, 0x7e, 0x7f, 0x80,
21081 0x96, 0x73, 0xbe, 0x56, 0x9b, 0x9e, 0x95, 0xd9, 0xf7, 0x02, 0xb9, 0xa4,
21082 0xde, 0x6a, 0x32, 0x6d, 0xd8, 0x8a, 0x84, 0x72, 0x2a, 0x14, 0x9f, 0x88,
21083 0xf9, 0xdc, 0x89, 0x9a, 0xfb, 0x7c, 0x2e, 0xc3, 0x8f, 0xb8, 0x65, 0x48,
21084 0x26, 0xc8, 0x12, 0x4a, 0xce, 0xe7, 0xd2, 0x62, 0x0c, 0xe0, 0x1f, 0xef,
21085 0x11, 0x75, 0x78, 0x71, 0xa5, 0x8e, 0x76, 0x3d, 0xbd, 0xbc, 0x86, 0x57,
21086 0x0b, 0x28, 0x2f, 0xa3, 0xda, 0xd4, 0xe4, 0x0f, 0xa9, 0x27, 0x53, 0x04,
21087 0x1b, 0xfc, 0xac, 0xe6, 0x7a, 0x07, 0xae, 0x63, 0xc5, 0xdb, 0xe2, 0xea,
21088 0x94, 0x8b, 0xc4, 0xd5, 0x9d, 0xf8, 0x90, 0x6b, 0xb1, 0x0d, 0xd6, 0xeb,
21089 0xc6, 0x0e, 0xcf, 0xad, 0x08, 0x4e, 0xd7, 0xe3, 0x5d, 0x50, 0x1e, 0xb3,
21090 0x5b, 0x23, 0x38, 0x34, 0x68, 0x46, 0x03, 0x8c, 0xdd, 0x9c, 0x7d, 0xa0,
21091 0xcd, 0x1a, 0x41, 0x1c};
21092
21093 return GFInv[Byte];
21094}
21095
21096uint8_t GFNIAffine(uint8_t XByte, const APInt &AQword, const APSInt &Imm,
21097 bool Inverse) {
21098 unsigned NumBitsInByte = 8;
21099 // Computing the affine transformation
21100 uint8_t RetByte = 0;
21101 for (uint32_t BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
21102 uint8_t AByte =
21103 AQword.lshr(shiftAmt: (7 - static_cast<int32_t>(BitIdx)) * NumBitsInByte)
21104 .getLoBits(numBits: 8)
21105 .getZExtValue();
21106 uint8_t Product;
21107 if (Inverse) {
21108 Product = AByte & GFNIMultiplicativeInverse(Byte: XByte);
21109 } else {
21110 Product = AByte & XByte;
21111 }
21112 uint8_t Parity = 0;
21113
21114 // Dot product in GF(2) uses XOR instead of addition
21115 for (unsigned PBitIdx = 0; PBitIdx != NumBitsInByte; ++PBitIdx) {
21116 Parity = Parity ^ ((Product >> PBitIdx) & 0x1);
21117 }
21118
21119 uint8_t Temp = Imm[BitIdx] ? 1 : 0;
21120 RetByte |= (Temp ^ Parity) << BitIdx;
21121 }
21122 return RetByte;
21123}
21124
21125uint8_t GFNIMul(uint8_t AByte, uint8_t BByte) {
21126 // Multiplying two polynomials of degree 7
21127 // Polynomial of degree 7
21128 // x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
21129 uint16_t TWord = 0;
21130 unsigned NumBitsInByte = 8;
21131 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
21132 if ((BByte >> BitIdx) & 0x1) {
21133 TWord = TWord ^ (AByte << BitIdx);
21134 }
21135 }
21136
21137 // When multiplying two polynomials of degree 7
21138 // results in a polynomial of degree 14
21139 // so the result has to be reduced to 7
21140 // Reduction polynomial is x^8 + x^4 + x^3 + x + 1 i.e. 0x11B
21141 for (int32_t BitIdx = 14; BitIdx > 7; --BitIdx) {
21142 if ((TWord >> BitIdx) & 0x1) {
21143 TWord = TWord ^ (0x11B << (BitIdx - 8));
21144 }
21145 }
21146 return (TWord & 0xFF);
21147}
21148
21149void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D,
21150 APFloat &ResR, APFloat &ResI) {
21151 // This is an implementation of complex multiplication according to the
21152 // constraints laid out in C11 Annex G. The implementation uses the
21153 // following naming scheme:
21154 // (a + ib) * (c + id)
21155
21156 APFloat AC = A * C;
21157 APFloat BD = B * D;
21158 APFloat AD = A * D;
21159 APFloat BC = B * C;
21160 ResR = AC - BD;
21161 ResI = AD + BC;
21162 if (ResR.isNaN() && ResI.isNaN()) {
21163 bool Recalc = false;
21164 if (A.isInfinity() || B.isInfinity()) {
21165 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
21166 Sign: A);
21167 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
21168 Sign: B);
21169 if (C.isNaN())
21170 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
21171 if (D.isNaN())
21172 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
21173 Recalc = true;
21174 }
21175 if (C.isInfinity() || D.isInfinity()) {
21176 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
21177 Sign: C);
21178 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
21179 Sign: D);
21180 if (A.isNaN())
21181 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
21182 if (B.isNaN())
21183 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
21184 Recalc = true;
21185 }
21186 if (!Recalc && (AC.isInfinity() || BD.isInfinity() || AD.isInfinity() ||
21187 BC.isInfinity())) {
21188 if (A.isNaN())
21189 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
21190 if (B.isNaN())
21191 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
21192 if (C.isNaN())
21193 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
21194 if (D.isNaN())
21195 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
21196 Recalc = true;
21197 }
21198 if (Recalc) {
21199 ResR = APFloat::getInf(Sem: A.getSemantics()) * (A * C - B * D);
21200 ResI = APFloat::getInf(Sem: A.getSemantics()) * (A * D + B * C);
21201 }
21202 }
21203}
21204
21205void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D,
21206 APFloat &ResR, APFloat &ResI) {
21207 // This is an implementation of complex division according to the
21208 // constraints laid out in C11 Annex G. The implementation uses the
21209 // following naming scheme:
21210 // (a + ib) / (c + id)
21211
21212 int DenomLogB = 0;
21213 APFloat MaxCD = maxnum(A: abs(X: C), B: abs(X: D));
21214 if (MaxCD.isFinite()) {
21215 DenomLogB = ilogb(Arg: MaxCD);
21216 C = scalbn(X: C, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21217 D = scalbn(X: D, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21218 }
21219 APFloat Denom = C * C + D * D;
21220 ResR =
21221 scalbn(X: (A * C + B * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21222 ResI =
21223 scalbn(X: (B * C - A * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21224 if (ResR.isNaN() && ResI.isNaN()) {
21225 if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) {
21226 ResR = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * A;
21227 ResI = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * B;
21228 } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() &&
21229 D.isFinite()) {
21230 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
21231 Sign: A);
21232 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
21233 Sign: B);
21234 ResR = APFloat::getInf(Sem: ResR.getSemantics()) * (A * C + B * D);
21235 ResI = APFloat::getInf(Sem: ResI.getSemantics()) * (B * C - A * D);
21236 } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) {
21237 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
21238 Sign: C);
21239 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
21240 Sign: D);
21241 ResR = APFloat::getZero(Sem: ResR.getSemantics()) * (A * C + B * D);
21242 ResI = APFloat::getZero(Sem: ResI.getSemantics()) * (B * C - A * D);
21243 }
21244 }
21245}
21246
21247APSInt NormalizeRotateAmount(const APSInt &Value, const APSInt &Amount) {
21248 // Normalize shift amount to [0, BitWidth) range to match runtime behavior
21249 APSInt NormAmt = Amount;
21250 unsigned BitWidth = Value.getBitWidth();
21251 unsigned AmtBitWidth = NormAmt.getBitWidth();
21252 if (BitWidth == 1) {
21253 // Rotating a 1-bit value is always a no-op
21254 NormAmt = APSInt(APInt(AmtBitWidth, 0), NormAmt.isUnsigned());
21255 } else if (BitWidth == 2) {
21256 // For 2-bit values: rotation amount is 0 or 1 based on
21257 // whether the amount is even or odd. We can't use srem here because
21258 // the divisor (2) would be misinterpreted as -2 in 2-bit signed arithmetic.
21259 NormAmt =
21260 APSInt(APInt(AmtBitWidth, NormAmt[0] ? 1 : 0), NormAmt.isUnsigned());
21261 } else {
21262 APInt Divisor;
21263 if (AmtBitWidth > BitWidth) {
21264 Divisor = llvm::APInt(AmtBitWidth, BitWidth);
21265 } else {
21266 Divisor = llvm::APInt(BitWidth, BitWidth);
21267 if (AmtBitWidth < BitWidth) {
21268 NormAmt = NormAmt.extend(width: BitWidth);
21269 }
21270 }
21271
21272 // Normalize to [0, BitWidth)
21273 if (NormAmt.isSigned()) {
21274 NormAmt = APSInt(NormAmt.srem(RHS: Divisor), /*isUnsigned=*/false);
21275 if (NormAmt.isNegative()) {
21276 APSInt SignedDivisor(Divisor, /*isUnsigned=*/false);
21277 NormAmt += SignedDivisor;
21278 }
21279 } else {
21280 NormAmt = APSInt(NormAmt.urem(RHS: Divisor), /*isUnsigned=*/true);
21281 }
21282 }
21283
21284 return NormAmt;
21285}
21286
21287bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
21288 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
21289 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
21290
21291 // Track whether the LHS or RHS is real at the type system level. When this is
21292 // the case we can simplify our evaluation strategy.
21293 bool LHSReal = false, RHSReal = false;
21294
21295 bool LHSOK;
21296 if (E->getLHS()->getType()->isRealFloatingType()) {
21297 LHSReal = true;
21298 APFloat &Real = Result.FloatReal;
21299 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: Real, Info);
21300 if (LHSOK) {
21301 Result.makeComplexFloat();
21302 Result.FloatImag = APFloat(Real.getSemantics());
21303 }
21304 } else {
21305 LHSOK = Visit(S: E->getLHS());
21306 }
21307 if (!LHSOK && !Info.noteFailure())
21308 return false;
21309
21310 ComplexValue RHS;
21311 if (E->getRHS()->getType()->isRealFloatingType()) {
21312 RHSReal = true;
21313 APFloat &Real = RHS.FloatReal;
21314 if (!EvaluateFloat(E: E->getRHS(), Result&: Real, Info) || !LHSOK)
21315 return false;
21316 RHS.makeComplexFloat();
21317 RHS.FloatImag = APFloat(Real.getSemantics());
21318 } else if (!EvaluateComplex(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
21319 return false;
21320
21321 assert(!(LHSReal && RHSReal) &&
21322 "Cannot have both operands of a complex operation be real.");
21323 switch (E->getOpcode()) {
21324 default: return Error(E);
21325 case BO_Add:
21326 if (Result.isComplexFloat()) {
21327 Result.getComplexFloatReal().add(RHS: RHS.getComplexFloatReal(),
21328 RM: APFloat::rmNearestTiesToEven);
21329 if (LHSReal)
21330 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
21331 else if (!RHSReal)
21332 Result.getComplexFloatImag().add(RHS: RHS.getComplexFloatImag(),
21333 RM: APFloat::rmNearestTiesToEven);
21334 } else {
21335 Result.getComplexIntReal() += RHS.getComplexIntReal();
21336 Result.getComplexIntImag() += RHS.getComplexIntImag();
21337 }
21338 break;
21339 case BO_Sub:
21340 if (Result.isComplexFloat()) {
21341 Result.getComplexFloatReal().subtract(RHS: RHS.getComplexFloatReal(),
21342 RM: APFloat::rmNearestTiesToEven);
21343 if (LHSReal) {
21344 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
21345 Result.getComplexFloatImag().changeSign();
21346 } else if (!RHSReal) {
21347 Result.getComplexFloatImag().subtract(RHS: RHS.getComplexFloatImag(),
21348 RM: APFloat::rmNearestTiesToEven);
21349 }
21350 } else {
21351 Result.getComplexIntReal() -= RHS.getComplexIntReal();
21352 Result.getComplexIntImag() -= RHS.getComplexIntImag();
21353 }
21354 break;
21355 case BO_Mul:
21356 if (Result.isComplexFloat()) {
21357 // This is an implementation of complex multiplication according to the
21358 // constraints laid out in C11 Annex G. The implementation uses the
21359 // following naming scheme:
21360 // (a + ib) * (c + id)
21361 ComplexValue LHS = Result;
21362 APFloat &A = LHS.getComplexFloatReal();
21363 APFloat &B = LHS.getComplexFloatImag();
21364 APFloat &C = RHS.getComplexFloatReal();
21365 APFloat &D = RHS.getComplexFloatImag();
21366 APFloat &ResR = Result.getComplexFloatReal();
21367 APFloat &ResI = Result.getComplexFloatImag();
21368 if (LHSReal) {
21369 assert(!RHSReal && "Cannot have two real operands for a complex op!");
21370 ResR = A;
21371 ResI = A;
21372 // ResR = A * C;
21373 // ResI = A * D;
21374 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: C) ||
21375 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: D))
21376 return false;
21377 } else if (RHSReal) {
21378 // ResR = C * A;
21379 // ResI = C * B;
21380 ResR = C;
21381 ResI = C;
21382 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: A) ||
21383 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: B))
21384 return false;
21385 } else {
21386 HandleComplexComplexMul(A, B, C, D, ResR, ResI);
21387 }
21388 } else {
21389 ComplexValue LHS = Result;
21390 Result.getComplexIntReal() =
21391 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
21392 LHS.getComplexIntImag() * RHS.getComplexIntImag());
21393 Result.getComplexIntImag() =
21394 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
21395 LHS.getComplexIntImag() * RHS.getComplexIntReal());
21396 }
21397 break;
21398 case BO_Div:
21399 if (Result.isComplexFloat()) {
21400 // This is an implementation of complex division according to the
21401 // constraints laid out in C11 Annex G. The implementation uses the
21402 // following naming scheme:
21403 // (a + ib) / (c + id)
21404 ComplexValue LHS = Result;
21405 APFloat &A = LHS.getComplexFloatReal();
21406 APFloat &B = LHS.getComplexFloatImag();
21407 APFloat &C = RHS.getComplexFloatReal();
21408 APFloat &D = RHS.getComplexFloatImag();
21409 APFloat &ResR = Result.getComplexFloatReal();
21410 APFloat &ResI = Result.getComplexFloatImag();
21411 if (RHSReal) {
21412 ResR = A;
21413 ResI = B;
21414 // ResR = A / C;
21415 // ResI = B / C;
21416 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Div, RHS: C) ||
21417 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Div, RHS: C))
21418 return false;
21419 } else {
21420 if (LHSReal) {
21421 // No real optimizations we can do here, stub out with zero.
21422 B = APFloat::getZero(Sem: A.getSemantics());
21423 }
21424 HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
21425 }
21426 } else {
21427 ComplexValue LHS = Result;
21428 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
21429 RHS.getComplexIntImag() * RHS.getComplexIntImag();
21430 if (Den.isZero())
21431 return Error(E, D: diag::note_expr_divide_by_zero);
21432
21433 Result.getComplexIntReal() =
21434 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
21435 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
21436 Result.getComplexIntImag() =
21437 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
21438 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
21439 }
21440 break;
21441 }
21442
21443 return true;
21444}
21445
21446bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
21447 // Get the operand value into 'Result'.
21448 if (!Visit(S: E->getSubExpr()))
21449 return false;
21450
21451 switch (E->getOpcode()) {
21452 default:
21453 return Error(E);
21454 case UO_Extension:
21455 return true;
21456 case UO_Plus:
21457 // The result is always just the subexpr.
21458 return true;
21459 case UO_Minus:
21460 if (Result.isComplexFloat()) {
21461 Result.getComplexFloatReal().changeSign();
21462 Result.getComplexFloatImag().changeSign();
21463 }
21464 else {
21465 Result.getComplexIntReal() = -Result.getComplexIntReal();
21466 Result.getComplexIntImag() = -Result.getComplexIntImag();
21467 }
21468 return true;
21469 case UO_Not:
21470 if (Result.isComplexFloat())
21471 Result.getComplexFloatImag().changeSign();
21472 else
21473 Result.getComplexIntImag() = -Result.getComplexIntImag();
21474 return true;
21475 }
21476}
21477
21478bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
21479 if (E->getNumInits() == 2) {
21480 if (E->getType()->isComplexType()) {
21481 Result.makeComplexFloat();
21482 if (!EvaluateFloat(E: E->getInit(Init: 0), Result&: Result.FloatReal, Info))
21483 return false;
21484 if (!EvaluateFloat(E: E->getInit(Init: 1), Result&: Result.FloatImag, Info))
21485 return false;
21486 } else {
21487 Result.makeComplexInt();
21488 if (!EvaluateInteger(E: E->getInit(Init: 0), Result&: Result.IntReal, Info))
21489 return false;
21490 if (!EvaluateInteger(E: E->getInit(Init: 1), Result&: Result.IntImag, Info))
21491 return false;
21492 }
21493 return true;
21494 }
21495 return ExprEvaluatorBaseTy::VisitInitListExpr(E);
21496}
21497
21498bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) {
21499 if (!IsConstantEvaluatedBuiltinCall(E))
21500 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21501
21502 switch (E->getBuiltinCallee()) {
21503 case Builtin::BI__builtin_complex:
21504 Result.makeComplexFloat();
21505 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: Result.FloatReal, Info))
21506 return false;
21507 if (!EvaluateFloat(E: E->getArg(Arg: 1), Result&: Result.FloatImag, Info))
21508 return false;
21509 return true;
21510
21511 default:
21512 return false;
21513 }
21514}
21515
21516//===----------------------------------------------------------------------===//
21517// Atomic expression evaluation, essentially just handling the NonAtomicToAtomic
21518// implicit conversion.
21519//===----------------------------------------------------------------------===//
21520
21521namespace {
21522class AtomicExprEvaluator :
21523 public ExprEvaluatorBase<AtomicExprEvaluator> {
21524 const LValue *This;
21525 APValue &Result;
21526public:
21527 AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result)
21528 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
21529
21530 bool Success(const APValue &V, const Expr *E) {
21531 Result = V;
21532 return true;
21533 }
21534
21535 bool ZeroInitialization(const Expr *E) {
21536 ImplicitValueInitExpr VIE(
21537 E->getType()->castAs<AtomicType>()->getValueType());
21538 // For atomic-qualified class (and array) types in C++, initialize the
21539 // _Atomic-wrapped subobject directly, in-place.
21540 return This ? EvaluateInPlace(Result, Info, This: *This, E: &VIE)
21541 : Evaluate(Result, Info, E: &VIE);
21542 }
21543
21544 bool VisitCastExpr(const CastExpr *E) {
21545 switch (E->getCastKind()) {
21546 default:
21547 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21548 case CK_NullToPointer:
21549 VisitIgnoredValue(E: E->getSubExpr());
21550 return ZeroInitialization(E);
21551 case CK_NonAtomicToAtomic:
21552 return This ? EvaluateInPlace(Result, Info, This: *This, E: E->getSubExpr())
21553 : Evaluate(Result, Info, E: E->getSubExpr());
21554 }
21555 }
21556};
21557} // end anonymous namespace
21558
21559static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
21560 EvalInfo &Info) {
21561 assert(!E->isValueDependent());
21562 assert(E->isPRValue() && E->getType()->isAtomicType());
21563 return AtomicExprEvaluator(Info, This, Result).Visit(S: E);
21564}
21565
21566//===----------------------------------------------------------------------===//
21567// Void expression evaluation, primarily for a cast to void on the LHS of a
21568// comma operator
21569//===----------------------------------------------------------------------===//
21570
21571namespace {
21572class VoidExprEvaluator
21573 : public ExprEvaluatorBase<VoidExprEvaluator> {
21574public:
21575 VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
21576
21577 bool Success(const APValue &V, const Expr *e) { return true; }
21578
21579 bool ZeroInitialization(const Expr *E) { return true; }
21580
21581 bool VisitCastExpr(const CastExpr *E) {
21582 switch (E->getCastKind()) {
21583 default:
21584 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21585 case CK_ToVoid:
21586 VisitIgnoredValue(E: E->getSubExpr());
21587 return true;
21588 }
21589 }
21590
21591 bool VisitCallExpr(const CallExpr *E) {
21592 if (!IsConstantEvaluatedBuiltinCall(E))
21593 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21594
21595 switch (E->getBuiltinCallee()) {
21596 case Builtin::BI__assume:
21597 case Builtin::BI__builtin_assume:
21598 // The argument is not evaluated!
21599 return true;
21600
21601 case Builtin::BI__builtin_operator_delete:
21602 return HandleOperatorDeleteCall(Info, E);
21603
21604 default:
21605 return false;
21606 }
21607 }
21608
21609 bool VisitCXXDeleteExpr(const CXXDeleteExpr *E);
21610};
21611} // end anonymous namespace
21612
21613bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
21614 // We cannot speculatively evaluate a delete expression.
21615 if (Info.SpeculativeEvaluationDepth)
21616 return false;
21617
21618 FunctionDecl *OperatorDelete = E->getOperatorDelete();
21619 if (!OperatorDelete
21620 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
21621 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
21622 << isa<CXXMethodDecl>(Val: OperatorDelete) << OperatorDelete;
21623 return false;
21624 }
21625
21626 const Expr *Arg = E->getArgument();
21627
21628 LValue Pointer;
21629 if (!EvaluatePointer(E: Arg, Result&: Pointer, Info))
21630 return false;
21631 if (Pointer.Designator.Invalid)
21632 return false;
21633
21634 // Deleting a null pointer has no effect.
21635 if (Pointer.isNullPointer()) {
21636 // This is the only case where we need to produce an extension warning:
21637 // the only other way we can succeed is if we find a dynamic allocation,
21638 // and we will have warned when we allocated it in that case.
21639 if (!Info.getLangOpts().CPlusPlus20)
21640 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
21641 return true;
21642 }
21643
21644 std::optional<DynAlloc *> Alloc = CheckDeleteKind(
21645 Info, E, Pointer, DeallocKind: E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New);
21646 if (!Alloc)
21647 return false;
21648 QualType AllocType = Pointer.Base.getDynamicAllocType();
21649
21650 // For the non-array case, the designator must be empty if the static type
21651 // does not have a virtual destructor.
21652 if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 &&
21653 !hasVirtualDestructor(T: Arg->getType()->getPointeeType())) {
21654 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_base_nonvirt_dtor)
21655 << Arg->getType()->getPointeeType() << AllocType;
21656 return false;
21657 }
21658
21659 // For a class type with a virtual destructor, the selected operator delete
21660 // is the one looked up when building the destructor.
21661 if (!E->isArrayForm() && !E->isGlobalDelete()) {
21662 const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(T: AllocType);
21663 if (VirtualDelete &&
21664 !VirtualDelete
21665 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
21666 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
21667 << isa<CXXMethodDecl>(Val: VirtualDelete) << VirtualDelete;
21668 return false;
21669 }
21670 }
21671
21672 if (!HandleDestruction(Info, Loc: E->getExprLoc(), LVBase: Pointer.getLValueBase(),
21673 Value&: (*Alloc)->Value, T: AllocType))
21674 return false;
21675
21676 if (!Info.HeapAllocs.erase(x: Pointer.Base.dyn_cast<DynamicAllocLValue>())) {
21677 // The element was already erased. This means the destructor call also
21678 // deleted the object.
21679 // FIXME: This probably results in undefined behavior before we get this
21680 // far, and should be diagnosed elsewhere first.
21681 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
21682 return false;
21683 }
21684
21685 return true;
21686}
21687
21688static bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
21689 assert(!E->isValueDependent());
21690 assert(E->isPRValue() && E->getType()->isVoidType());
21691 return VoidExprEvaluator(Info).Visit(S: E);
21692}
21693
21694//===----------------------------------------------------------------------===//
21695// Top level Expr::EvaluateAsRValue method.
21696//===----------------------------------------------------------------------===//
21697
21698static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {
21699 assert(!E->isValueDependent());
21700 // In C, function designators are not lvalues, but we evaluate them as if they
21701 // are.
21702 QualType T = E->getType();
21703 if (E->isGLValue() || T->isFunctionType()) {
21704 LValue LV;
21705 if (!EvaluateLValue(E, Result&: LV, Info))
21706 return false;
21707 LV.moveInto(V&: Result);
21708 } else if (T->isVectorType()) {
21709 if (!EvaluateVector(E, Result, Info))
21710 return false;
21711 } else if (T->isConstantMatrixType()) {
21712 if (!EvaluateMatrix(E, Result, Info))
21713 return false;
21714 } else if (T->isIntegralOrEnumerationType()) {
21715 if (!IntExprEvaluator(Info, Result).Visit(S: E))
21716 return false;
21717 } else if (T->hasPointerRepresentation()) {
21718 LValue LV;
21719 if (!EvaluatePointer(E, Result&: LV, Info))
21720 return false;
21721 LV.moveInto(V&: Result);
21722 } else if (T->isRealFloatingType()) {
21723 llvm::APFloat F(0.0);
21724 if (!EvaluateFloat(E, Result&: F, Info))
21725 return false;
21726 Result = APValue(F);
21727 } else if (T->isAnyComplexType()) {
21728 ComplexValue C;
21729 if (!EvaluateComplex(E, Result&: C, Info))
21730 return false;
21731 C.moveInto(v&: Result);
21732 } else if (T->isFixedPointType()) {
21733 if (!FixedPointExprEvaluator(Info, Result).Visit(S: E)) return false;
21734 } else if (T->isMemberPointerType()) {
21735 MemberPtr P;
21736 if (!EvaluateMemberPointer(E, Result&: P, Info))
21737 return false;
21738 P.moveInto(V&: Result);
21739 return true;
21740 } else if (T->isArrayType()) {
21741 LValue LV;
21742 APValue &Value =
21743 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
21744 if (!EvaluateArray(E, This: LV, Result&: Value, Info))
21745 return false;
21746 Result = Value;
21747 } else if (T->isRecordType()) {
21748 LValue LV;
21749 APValue &Value =
21750 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
21751 if (!EvaluateRecord(E, This: LV, Result&: Value, Info))
21752 return false;
21753 Result = Value;
21754 } else if (T->isVoidType()) {
21755 if (!Info.getLangOpts().CPlusPlus11)
21756 Info.CCEDiag(E, DiagId: diag::note_constexpr_nonliteral)
21757 << E->getType();
21758 if (!EvaluateVoid(E, Info))
21759 return false;
21760 } else if (T->isAtomicType()) {
21761 QualType Unqual = T.getAtomicUnqualifiedType();
21762 if (Unqual->isArrayType() || Unqual->isRecordType()) {
21763 LValue LV;
21764 APValue &Value = Info.CurrentCall->createTemporary(
21765 Key: E, T: Unqual, Scope: ScopeKind::FullExpression, LV);
21766 if (!EvaluateAtomic(E, This: &LV, Result&: Value, Info))
21767 return false;
21768 Result = Value;
21769 } else {
21770 if (!EvaluateAtomic(E, This: nullptr, Result, Info))
21771 return false;
21772 }
21773 } else if (Info.getLangOpts().CPlusPlus11) {
21774 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral) << E->getType();
21775 return false;
21776 } else {
21777 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
21778 return false;
21779 }
21780
21781 return true;
21782}
21783
21784/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some
21785/// cases, the in-place evaluation is essential, since later initializers for
21786/// an object can indirectly refer to subobjects which were initialized earlier.
21787static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,
21788 const Expr *E, bool AllowNonLiteralTypes) {
21789 assert(!E->isValueDependent());
21790
21791 // Normally expressions passed to EvaluateInPlace have a type, but not when
21792 // a VarDecl initializer is evaluated before the untyped ParenListExpr is
21793 // replaced with a CXXConstructExpr. This can happen in LLDB.
21794 if (E->getType().isNull())
21795 return false;
21796
21797 if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, This: &This))
21798 return false;
21799
21800 if (E->isPRValue()) {
21801 // Evaluate arrays and record types in-place, so that later initializers can
21802 // refer to earlier-initialized members of the object.
21803 QualType T = E->getType();
21804 if (T->isArrayType())
21805 return EvaluateArray(E, This, Result, Info);
21806 else if (T->isRecordType())
21807 return EvaluateRecord(E, This, Result, Info);
21808 else if (T->isAtomicType()) {
21809 QualType Unqual = T.getAtomicUnqualifiedType();
21810 if (Unqual->isArrayType() || Unqual->isRecordType())
21811 return EvaluateAtomic(E, This: &This, Result, Info);
21812 }
21813 }
21814
21815 // For any other type, in-place evaluation is unimportant.
21816 return Evaluate(Result, Info, E);
21817}
21818
21819/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
21820/// lvalue-to-rvalue cast if it is an lvalue.
21821static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
21822 assert(!E->isValueDependent());
21823
21824 if (E->getType().isNull())
21825 return false;
21826
21827 if (!CheckLiteralType(Info, E))
21828 return false;
21829
21830 if (Info.EnableNewConstInterp) {
21831 if (!Info.Ctx.getInterpContext().evaluateAsRValue(Parent&: Info, E, Result))
21832 return false;
21833 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
21834 Kind: ConstantExprKind::Normal);
21835 }
21836
21837 if (!::Evaluate(Result, Info, E))
21838 return false;
21839
21840 // Implicit lvalue-to-rvalue cast.
21841 if (E->isGLValue()) {
21842 LValue LV;
21843 LV.setFrom(Ctx: Info.Ctx, V: Result);
21844 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
21845 return false;
21846 }
21847
21848 // Check this core constant expression is a constant expression.
21849 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
21850 Kind: ConstantExprKind::Normal) &&
21851 CheckMemoryLeaks(Info);
21852}
21853
21854static bool FastEvaluateAsRValue(const Expr *Exp, APValue &Result,
21855 const ASTContext &Ctx, bool &IsConst) {
21856 // Fast-path evaluations of integer literals, since we sometimes see files
21857 // containing vast quantities of these.
21858 if (const auto *L = dyn_cast<IntegerLiteral>(Val: Exp)) {
21859 Result =
21860 APValue(APSInt(L->getValue(), L->getType()->isUnsignedIntegerType()));
21861 IsConst = true;
21862 return true;
21863 }
21864
21865 if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Val: Exp)) {
21866 Result = APValue(APSInt(APInt(1, L->getValue())));
21867 IsConst = true;
21868 return true;
21869 }
21870
21871 if (const auto *FL = dyn_cast<FloatingLiteral>(Val: Exp)) {
21872 Result = APValue(FL->getValue());
21873 IsConst = true;
21874 return true;
21875 }
21876
21877 if (const auto *L = dyn_cast<CharacterLiteral>(Val: Exp)) {
21878 Result = APValue(Ctx.MakeIntValue(Value: L->getValue(), Type: L->getType()));
21879 IsConst = true;
21880 return true;
21881 }
21882
21883 if (const auto *CE = dyn_cast<ConstantExpr>(Val: Exp)) {
21884 if (CE->hasAPValueResult()) {
21885 APValue APV = CE->getAPValueResult();
21886 if (!APV.isLValue()) {
21887 Result = std::move(APV);
21888 IsConst = true;
21889 return true;
21890 }
21891 }
21892
21893 // The SubExpr is usually just an IntegerLiteral.
21894 return FastEvaluateAsRValue(Exp: CE->getSubExpr(), Result, Ctx, IsConst);
21895 }
21896
21897 // This case should be rare, but we need to check it before we check on
21898 // the type below.
21899 if (Exp->getType().isNull()) {
21900 IsConst = false;
21901 return true;
21902 }
21903
21904 return false;
21905}
21906
21907static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result,
21908 Expr::SideEffectsKind SEK) {
21909 return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) ||
21910 (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior);
21911}
21912
21913static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result,
21914 const ASTContext &Ctx, EvalInfo &Info) {
21915 assert(!E->isValueDependent());
21916 bool IsConst;
21917 if (FastEvaluateAsRValue(Exp: E, Result&: Result.Val, Ctx, IsConst))
21918 return IsConst;
21919
21920 return EvaluateAsRValue(Info, E, Result&: Result.Val);
21921}
21922
21923static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult,
21924 const ASTContext &Ctx,
21925 Expr::SideEffectsKind AllowSideEffects,
21926 EvalInfo &Info) {
21927 assert(!E->isValueDependent());
21928 if (!E->getType()->isIntegralOrEnumerationType())
21929 return false;
21930
21931 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info) ||
21932 !ExprResult.Val.isInt() ||
21933 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
21934 return false;
21935
21936 return true;
21937}
21938
21939static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult,
21940 const ASTContext &Ctx,
21941 Expr::SideEffectsKind AllowSideEffects,
21942 EvalInfo &Info) {
21943 assert(!E->isValueDependent());
21944 if (!E->getType()->isFixedPointType())
21945 return false;
21946
21947 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info))
21948 return false;
21949
21950 if (!ExprResult.Val.isFixedPoint() ||
21951 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
21952 return false;
21953
21954 return true;
21955}
21956
21957/// EvaluateAsRValue - Return true if this is a constant which we can fold using
21958/// any crazy technique (that has nothing to do with language standards) that
21959/// we want to. If this function returns true, it returns the folded constant
21960/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
21961/// will be applied to the result.
21962bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx,
21963 bool InConstantContext) const {
21964 assert(!isValueDependent() &&
21965 "Expression evaluator can't be called on a dependent expression.");
21966 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsRValue");
21967 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21968 Info.InConstantContext = InConstantContext;
21969 return ::EvaluateAsRValue(E: this, Result, Ctx, Info);
21970}
21971
21972bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx,
21973 bool InConstantContext) const {
21974 assert(!isValueDependent() &&
21975 "Expression evaluator can't be called on a dependent expression.");
21976 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsBooleanCondition");
21977 EvalResult Scratch;
21978 return EvaluateAsRValue(Result&: Scratch, Ctx, InConstantContext) &&
21979 HandleConversionToBool(Val: Scratch.Val, Result);
21980}
21981
21982bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx,
21983 SideEffectsKind AllowSideEffects,
21984 bool InConstantContext) const {
21985 assert(!isValueDependent() &&
21986 "Expression evaluator can't be called on a dependent expression.");
21987 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsInt");
21988 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21989 Info.InConstantContext = InConstantContext;
21990 return ::EvaluateAsInt(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
21991}
21992
21993bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx,
21994 SideEffectsKind AllowSideEffects,
21995 bool InConstantContext) const {
21996 assert(!isValueDependent() &&
21997 "Expression evaluator can't be called on a dependent expression.");
21998 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFixedPoint");
21999 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
22000 Info.InConstantContext = InConstantContext;
22001 return ::EvaluateAsFixedPoint(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
22002}
22003
22004bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx,
22005 SideEffectsKind AllowSideEffects,
22006 bool InConstantContext) const {
22007 assert(!isValueDependent() &&
22008 "Expression evaluator can't be called on a dependent expression.");
22009
22010 if (!getType()->isRealFloatingType())
22011 return false;
22012
22013 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFloat");
22014 EvalResult ExprResult;
22015 if (!EvaluateAsRValue(Result&: ExprResult, Ctx, InConstantContext) ||
22016 !ExprResult.Val.isFloat() ||
22017 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
22018 return false;
22019
22020 Result = ExprResult.Val.getFloat();
22021 return true;
22022}
22023
22024bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx,
22025 bool InConstantContext) const {
22026 assert(!isValueDependent() &&
22027 "Expression evaluator can't be called on a dependent expression.");
22028
22029 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsLValue");
22030 EvalInfo Info(Ctx, Result, EvaluationMode::ConstantFold);
22031 Info.InConstantContext = InConstantContext;
22032 LValue LV;
22033 CheckedTemporaries CheckedTemps;
22034
22035 if (Info.EnableNewConstInterp) {
22036 if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val,
22037 Kind: ConstantExprKind::Normal))
22038 return false;
22039
22040 LV.setFrom(Ctx, V: Result.Val);
22041 return CheckLValueConstantExpression(
22042 Info, Loc: getExprLoc(), Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV,
22043 Kind: ConstantExprKind::Normal, CheckedTemps);
22044 }
22045
22046 if (!EvaluateLValue(E: this, Result&: LV, Info) || !Info.discardCleanups() ||
22047 Result.HasSideEffects ||
22048 !CheckLValueConstantExpression(Info, Loc: getExprLoc(),
22049 Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV,
22050 Kind: ConstantExprKind::Normal, CheckedTemps))
22051 return false;
22052
22053 LV.moveInto(V&: Result.Val);
22054 return true;
22055}
22056
22057static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base,
22058 APValue DestroyedValue, QualType Type,
22059 SourceLocation Loc, Expr::EvalStatus &EStatus,
22060 bool IsConstantDestruction) {
22061 EvalInfo Info(Ctx, EStatus,
22062 IsConstantDestruction ? EvaluationMode::ConstantExpression
22063 : EvaluationMode::ConstantFold);
22064 Info.setEvaluatingDecl(Base, Value&: DestroyedValue,
22065 EDK: EvalInfo::EvaluatingDeclKind::Dtor);
22066 Info.InConstantContext = IsConstantDestruction;
22067
22068 LValue LVal;
22069 LVal.set(B: Base);
22070
22071 if (!HandleDestruction(Info, Loc, LVBase: Base, Value&: DestroyedValue, T: Type) ||
22072 EStatus.HasSideEffects)
22073 return false;
22074
22075 if (!Info.discardCleanups())
22076 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22077
22078 return true;
22079}
22080
22081bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
22082 ConstantExprKind Kind) const {
22083 assert(!isValueDependent() &&
22084 "Expression evaluator can't be called on a dependent expression.");
22085 bool IsConst;
22086 if (FastEvaluateAsRValue(Exp: this, Result&: Result.Val, Ctx, IsConst) &&
22087 Result.Val.hasValue())
22088 return true;
22089
22090 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr");
22091 EvaluationMode EM = EvaluationMode::ConstantExpression;
22092 EvalInfo Info(Ctx, Result, EM);
22093 Info.InConstantContext = true;
22094
22095 if (Info.EnableNewConstInterp) {
22096 if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val, Kind))
22097 return false;
22098 return CheckConstantExpression(Info, DiagLoc: getExprLoc(),
22099 Type: getStorageType(Ctx, E: this), Value: Result.Val, Kind);
22100 }
22101
22102 // The type of the object we're initializing is 'const T' for a class NTTP.
22103 QualType T = getType();
22104 if (Kind == ConstantExprKind::ClassTemplateArgument)
22105 T.addConst();
22106
22107 // If we're evaluating a prvalue, fake up a MaterializeTemporaryExpr to
22108 // represent the result of the evaluation. CheckConstantExpression ensures
22109 // this doesn't escape.
22110 MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true);
22111 APValue::LValueBase Base(&BaseMTE);
22112 Info.setEvaluatingDecl(Base, Value&: Result.Val);
22113
22114 LValue LVal;
22115 LVal.set(B: Base);
22116 // C++23 [intro.execution]/p5
22117 // A full-expression is [...] a constant-expression
22118 // So we need to make sure temporary objects are destroyed after having
22119 // evaluating the expression (per C++23 [class.temporary]/p4).
22120 FullExpressionRAII Scope(Info);
22121 if (!::EvaluateInPlace(Result&: Result.Val, Info, This: LVal, E: this) ||
22122 Result.HasSideEffects || !Scope.destroy())
22123 return false;
22124
22125 if (!Info.discardCleanups())
22126 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22127
22128 if (!CheckConstantExpression(Info, DiagLoc: getExprLoc(), Type: getStorageType(Ctx, E: this),
22129 Value: Result.Val, Kind))
22130 return false;
22131 if (!CheckMemoryLeaks(Info))
22132 return false;
22133
22134 // If this is a class template argument, it's required to have constant
22135 // destruction too.
22136 if (Kind == ConstantExprKind::ClassTemplateArgument &&
22137 (!EvaluateDestruction(Ctx, Base, DestroyedValue: Result.Val, Type: T, Loc: getBeginLoc(), EStatus&: Result,
22138 IsConstantDestruction: true) ||
22139 Result.HasSideEffects)) {
22140 // FIXME: Prefix a note to indicate that the problem is lack of constant
22141 // destruction.
22142 return false;
22143 }
22144 return true;
22145}
22146
22147bool Expr::EvaluateAsInitializer(const ASTContext &Ctx, const VarDecl *VD,
22148 Expr::EvalResult &EStatus,
22149 bool IsConstantInitialization) const {
22150 assert(!isValueDependent() &&
22151 "Expression evaluator can't be called on a dependent expression.");
22152 assert(VD && "Need a valid VarDecl");
22153
22154 llvm::TimeTraceScope TimeScope("EvaluateAsInitializer", [&] {
22155 std::string Name;
22156 llvm::raw_string_ostream OS(Name);
22157 VD->printQualifiedName(OS);
22158 return Name;
22159 });
22160
22161 EvalInfo Info(Ctx, EStatus,
22162 (IsConstantInitialization &&
22163 (Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23))
22164 ? EvaluationMode::ConstantExpression
22165 : EvaluationMode::ConstantFold);
22166 Info.setEvaluatingDecl(Base: VD, Value&: EStatus.Val);
22167 Info.InConstantContext = IsConstantInitialization;
22168
22169 SourceLocation DeclLoc = VD->getLocation();
22170 QualType DeclTy = VD->getType();
22171
22172 if (Info.EnableNewConstInterp) {
22173 auto &InterpCtx = Ctx.getInterpContext();
22174 if (!InterpCtx.evaluateAsInitializer(Parent&: Info, VD, Init: this, Result&: EStatus.Val))
22175 return false;
22176
22177 return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value: EStatus.Val,
22178 Kind: ConstantExprKind::Normal);
22179 } else {
22180 LValue LVal;
22181 LVal.set(B: VD);
22182
22183 {
22184 // C++23 [intro.execution]/p5
22185 // A full-expression is ... an init-declarator ([dcl.decl]) or a
22186 // mem-initializer.
22187 // So we need to make sure temporary objects are destroyed after having
22188 // evaluated the expression (per C++23 [class.temporary]/p4).
22189 //
22190 // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the
22191 // serialization code calls ParmVarDecl::getDefaultArg() which strips the
22192 // outermost FullExpr, such as ExprWithCleanups.
22193 FullExpressionRAII Scope(Info);
22194 if (!EvaluateInPlace(Result&: EStatus.Val, Info, This: LVal, E: this,
22195 /*AllowNonLiteralTypes=*/true) ||
22196 EStatus.HasSideEffects)
22197 return false;
22198 }
22199
22200 // At this point, any lifetime-extended temporaries are completely
22201 // initialized.
22202 Info.performLifetimeExtension();
22203
22204 if (!Info.discardCleanups())
22205 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22206 }
22207
22208 return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value: EStatus.Val,
22209 Kind: ConstantExprKind::Normal) &&
22210 CheckMemoryLeaks(Info);
22211}
22212
22213bool VarDecl::evaluateDestruction(
22214 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
22215 // This function is only meaningful for records and arrays of records.
22216 QualType VarTy = getType();
22217 if (VarTy->isArrayType()) {
22218 QualType ElemTy = getASTContext().getBaseElementType(QT: VarTy);
22219 if (!ElemTy->isRecordType()) {
22220 ensureEvaluatedStmt()->HasConstantDestruction = true;
22221 return true;
22222 }
22223 } else if (!VarTy->isRecordType()) {
22224 ensureEvaluatedStmt()->HasConstantDestruction = true;
22225 return true;
22226 }
22227
22228 Expr::EvalStatus EStatus;
22229 EStatus.Diag = &Notes;
22230
22231 // Only treat the destruction as constant destruction if we formally have
22232 // constant initialization (or are usable in a constant expression).
22233 bool IsConstantDestruction = hasConstantInitialization();
22234 ASTContext &Ctx = getASTContext();
22235
22236 // Make a copy of the value for the destructor to mutate, if we know it.
22237 // Otherwise, treat the value as default-initialized; if the destructor works
22238 // anyway, then the destruction is constant (and must be essentially empty).
22239 APValue DestroyedValue;
22240 if (getEvaluatedValue())
22241 DestroyedValue = *getEvaluatedValue();
22242 else if (!handleDefaultInitValue(T: VarTy, Result&: DestroyedValue))
22243 return false;
22244
22245 if (Ctx.getLangOpts().EnableNewConstInterp) {
22246 EvalInfo Info(Ctx, EStatus,
22247 IsConstantDestruction ? EvaluationMode::ConstantExpression
22248 : EvaluationMode::ConstantFold);
22249 Info.InConstantContext = IsConstantDestruction;
22250 if (!Ctx.getInterpContext().evaluateDestruction(Parent&: Info, VD: this,
22251 Value: std::move(DestroyedValue)))
22252 return false;
22253 ensureEvaluatedStmt()->HasConstantDestruction = true;
22254 return true;
22255 }
22256
22257 if (!EvaluateDestruction(Ctx, Base: this, DestroyedValue: std::move(DestroyedValue), Type: VarTy,
22258 Loc: getLocation(), EStatus, IsConstantDestruction) ||
22259 EStatus.HasSideEffects)
22260 return false;
22261
22262 ensureEvaluatedStmt()->HasConstantDestruction = true;
22263 return true;
22264}
22265
22266/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
22267/// constant folded, but discard the result.
22268bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const {
22269 assert(!isValueDependent() &&
22270 "Expression evaluator can't be called on a dependent expression.");
22271
22272 EvalResult Result;
22273 return EvaluateAsRValue(Result, Ctx, /* in constant context */ InConstantContext: true) &&
22274 !hasUnacceptableSideEffect(Result, SEK);
22275}
22276
22277APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
22278 assert(!isValueDependent() &&
22279 "Expression evaluator can't be called on a dependent expression.");
22280
22281 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstInt");
22282 EvalResult EVResult;
22283 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22284 Info.InConstantContext = true;
22285
22286 bool Result = ::EvaluateAsRValue(E: this, Result&: EVResult, Ctx, Info);
22287 (void)Result;
22288 assert(Result && "Could not evaluate expression");
22289 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22290
22291 return EVResult.Val.getInt();
22292}
22293
22294APSInt Expr::EvaluateKnownConstIntCheckOverflow(
22295 const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const {
22296 assert(!isValueDependent() &&
22297 "Expression evaluator can't be called on a dependent expression.");
22298
22299 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstIntCheckOverflow");
22300 EvalResult EVResult;
22301 EVResult.Diag = Diag;
22302 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22303 Info.InConstantContext = true;
22304 Info.CheckingForUndefinedBehavior = true;
22305
22306 bool Result = ::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
22307 (void)Result;
22308 assert(Result && "Could not evaluate expression");
22309 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22310
22311 return EVResult.Val.getInt();
22312}
22313
22314void Expr::EvaluateForOverflow(const ASTContext &Ctx) const {
22315 assert(!isValueDependent() &&
22316 "Expression evaluator can't be called on a dependent expression.");
22317
22318 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow");
22319 bool IsConst;
22320 EvalResult EVResult;
22321 if (!FastEvaluateAsRValue(Exp: this, Result&: EVResult.Val, Ctx, IsConst)) {
22322 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22323 Info.CheckingForUndefinedBehavior = true;
22324 (void)::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
22325 }
22326}
22327
22328bool Expr::EvalResult::isGlobalLValue() const {
22329 assert(Val.isLValue());
22330 return IsGlobalLValue(B: Val.getLValueBase());
22331}
22332
22333/// isIntegerConstantExpr - this recursive routine will test if an expression is
22334/// an integer constant expression.
22335
22336/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
22337/// comma, etc
22338
22339// CheckICE - This function does the fundamental ICE checking: the returned
22340// ICEDiag contains an ICEKind indicating whether the expression is an ICE.
22341//
22342// Note that to reduce code duplication, this helper does no evaluation
22343// itself; the caller checks whether the expression is evaluatable, and
22344// in the rare cases where CheckICE actually cares about the evaluated
22345// value, it calls into Evaluate.
22346
22347namespace {
22348
22349enum ICEKind {
22350 /// This expression is an ICE.
22351 IK_ICE,
22352 /// This expression is not an ICE, but if it isn't evaluated, it's
22353 /// a legal subexpression for an ICE. This return value is used to handle
22354 /// the comma operator in C99 mode, and non-constant subexpressions.
22355 IK_ICEIfUnevaluated,
22356 /// This expression is not an ICE, and is not a legal subexpression for one.
22357 IK_NotICE
22358};
22359
22360struct ICEDiag {
22361 ICEKind Kind;
22362 SourceLocation Loc;
22363
22364 ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {}
22365};
22366
22367}
22368
22369static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); }
22370
22371static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; }
22372
22373static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) {
22374 Expr::EvalResult EVResult;
22375 Expr::EvalStatus Status;
22376 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
22377
22378 Info.InConstantContext = true;
22379 if (!::EvaluateAsRValue(E, Result&: EVResult, Ctx, Info) || EVResult.HasSideEffects ||
22380 !EVResult.Val.isInt())
22381 return ICEDiag(IK_NotICE, E->getBeginLoc());
22382
22383 return NoDiag();
22384}
22385
22386static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) {
22387 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
22388 if (!E->getType()->isIntegralOrEnumerationType())
22389 return ICEDiag(IK_NotICE, E->getBeginLoc());
22390
22391 switch (E->getStmtClass()) {
22392#define ABSTRACT_STMT(Node)
22393#define STMT(Node, Base) case Expr::Node##Class:
22394#define EXPR(Node, Base)
22395#include "clang/AST/StmtNodes.inc"
22396 case Expr::PredefinedExprClass:
22397 case Expr::FloatingLiteralClass:
22398 case Expr::ImaginaryLiteralClass:
22399 case Expr::StringLiteralClass:
22400 case Expr::ArraySubscriptExprClass:
22401 case Expr::MatrixSingleSubscriptExprClass:
22402 case Expr::MatrixSubscriptExprClass:
22403 case Expr::ArraySectionExprClass:
22404 case Expr::OMPArrayShapingExprClass:
22405 case Expr::OMPIteratorExprClass:
22406 case Expr::CompoundAssignOperatorClass:
22407 case Expr::CompoundLiteralExprClass:
22408 case Expr::ExtVectorElementExprClass:
22409 case Expr::MatrixElementExprClass:
22410 case Expr::DesignatedInitExprClass:
22411 case Expr::ArrayInitLoopExprClass:
22412 case Expr::ArrayInitIndexExprClass:
22413 case Expr::NoInitExprClass:
22414 case Expr::DesignatedInitUpdateExprClass:
22415 case Expr::ImplicitValueInitExprClass:
22416 case Expr::ParenListExprClass:
22417 case Expr::VAArgExprClass:
22418 case Expr::AddrLabelExprClass:
22419 case Expr::StmtExprClass:
22420 case Expr::CXXMemberCallExprClass:
22421 case Expr::CUDAKernelCallExprClass:
22422 case Expr::CXXAddrspaceCastExprClass:
22423 case Expr::CXXDynamicCastExprClass:
22424 case Expr::CXXTypeidExprClass:
22425 case Expr::CXXUuidofExprClass:
22426 case Expr::MSPropertyRefExprClass:
22427 case Expr::MSPropertySubscriptExprClass:
22428 case Expr::CXXNullPtrLiteralExprClass:
22429 case Expr::UserDefinedLiteralClass:
22430 case Expr::CXXThisExprClass:
22431 case Expr::CXXThrowExprClass:
22432 case Expr::CXXNewExprClass:
22433 case Expr::CXXDeleteExprClass:
22434 case Expr::CXXPseudoDestructorExprClass:
22435 case Expr::UnresolvedLookupExprClass:
22436 case Expr::RecoveryExprClass:
22437 case Expr::DependentScopeDeclRefExprClass:
22438 case Expr::DependentTemplateIdExprClass:
22439 case Expr::CXXConstructExprClass:
22440 case Expr::CXXInheritedCtorInitExprClass:
22441 case Expr::CXXStdInitializerListExprClass:
22442 case Expr::CXXBindTemporaryExprClass:
22443 case Expr::ExprWithCleanupsClass:
22444 case Expr::CXXTemporaryObjectExprClass:
22445 case Expr::CXXUnresolvedConstructExprClass:
22446 case Expr::CXXDependentScopeMemberExprClass:
22447 case Expr::UnresolvedMemberExprClass:
22448 case Expr::ObjCStringLiteralClass:
22449 case Expr::ObjCBoxedExprClass:
22450 case Expr::ObjCArrayLiteralClass:
22451 case Expr::ObjCDictionaryLiteralClass:
22452 case Expr::ObjCEncodeExprClass:
22453 case Expr::ObjCMessageExprClass:
22454 case Expr::ObjCSelectorExprClass:
22455 case Expr::ObjCProtocolExprClass:
22456 case Expr::ObjCIvarRefExprClass:
22457 case Expr::ObjCPropertyRefExprClass:
22458 case Expr::ObjCSubscriptRefExprClass:
22459 case Expr::ObjCIsaExprClass:
22460 case Expr::ObjCAvailabilityCheckExprClass:
22461 case Expr::ShuffleVectorExprClass:
22462 case Expr::ConvertVectorExprClass:
22463 case Expr::BlockExprClass:
22464 case Expr::NoStmtClass:
22465 case Expr::OpaqueValueExprClass:
22466 case Expr::PackExpansionExprClass:
22467 case Expr::SubstNonTypeTemplateParmPackExprClass:
22468 case Expr::FunctionParmPackExprClass:
22469 case Expr::AsTypeExprClass:
22470 case Expr::ObjCIndirectCopyRestoreExprClass:
22471 case Expr::MaterializeTemporaryExprClass:
22472 case Expr::PseudoObjectExprClass:
22473 case Expr::AtomicExprClass:
22474 case Expr::LambdaExprClass:
22475 case Expr::CXXFoldExprClass:
22476 case Expr::CoawaitExprClass:
22477 case Expr::DependentCoawaitExprClass:
22478 case Expr::CoyieldExprClass:
22479 case Expr::SYCLUniqueStableNameExprClass:
22480 case Expr::CXXParenListInitExprClass:
22481 case Expr::HLSLOutArgExprClass:
22482 case Expr::CXXExpansionSelectExprClass:
22483 return ICEDiag(IK_NotICE, E->getBeginLoc());
22484
22485 case Expr::MemberExprClass: {
22486 if (Ctx.getLangOpts().C23) {
22487 const Expr *ME = E->IgnoreParenImpCasts();
22488 while (const auto *M = dyn_cast<MemberExpr>(Val: ME)) {
22489 if (M->isArrow())
22490 return ICEDiag(IK_NotICE, E->getBeginLoc());
22491 ME = M->getBase()->IgnoreParenImpCasts();
22492 }
22493 const auto *DRE = dyn_cast<DeclRefExpr>(Val: ME);
22494 if (DRE) {
22495 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl());
22496 VD && VD->isConstexpr())
22497 return CheckEvalInICE(E, Ctx);
22498 }
22499 }
22500 return ICEDiag(IK_NotICE, E->getBeginLoc());
22501 }
22502
22503 case Expr::InitListExprClass: {
22504 // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the
22505 // form "T x = { a };" is equivalent to "T x = a;".
22506 // Unless we're initializing a reference, T is a scalar as it is known to be
22507 // of integral or enumeration type.
22508 if (E->isPRValue())
22509 if (cast<InitListExpr>(Val: E)->getNumInits() == 1)
22510 return CheckICE(E: cast<InitListExpr>(Val: E)->getInit(Init: 0), Ctx);
22511 return ICEDiag(IK_NotICE, E->getBeginLoc());
22512 }
22513
22514 case Expr::SizeOfPackExprClass:
22515 case Expr::GNUNullExprClass:
22516 case Expr::SourceLocExprClass:
22517 case Expr::EmbedExprClass:
22518 case Expr::OpenACCAsteriskSizeExprClass:
22519 return NoDiag();
22520
22521 case Expr::PackIndexingExprClass:
22522 return CheckICE(E: cast<PackIndexingExpr>(Val: E)->getSelectedExpr(), Ctx);
22523
22524 case Expr::SubstNonTypeTemplateParmExprClass:
22525 return
22526 CheckICE(E: cast<SubstNonTypeTemplateParmExpr>(Val: E)->getReplacement(), Ctx);
22527
22528 case Expr::ConstantExprClass:
22529 return CheckICE(E: cast<ConstantExpr>(Val: E)->getSubExpr(), Ctx);
22530
22531 case Expr::ParenExprClass:
22532 return CheckICE(E: cast<ParenExpr>(Val: E)->getSubExpr(), Ctx);
22533 case Expr::GenericSelectionExprClass:
22534 return CheckICE(E: cast<GenericSelectionExpr>(Val: E)->getResultExpr(), Ctx);
22535 case Expr::IntegerLiteralClass:
22536 case Expr::FixedPointLiteralClass:
22537 case Expr::CharacterLiteralClass:
22538 case Expr::ObjCBoolLiteralExprClass:
22539 case Expr::CXXBoolLiteralExprClass:
22540 case Expr::CXXScalarValueInitExprClass:
22541 case Expr::TypeTraitExprClass:
22542 case Expr::ConceptSpecializationExprClass:
22543 case Expr::RequiresExprClass:
22544 case Expr::ArrayTypeTraitExprClass:
22545 case Expr::ExpressionTraitExprClass:
22546 case Expr::CXXNoexceptExprClass:
22547 case Expr::CXXReflectExprClass:
22548 return NoDiag();
22549 case Expr::CallExprClass:
22550 case Expr::CXXOperatorCallExprClass: {
22551 // C99 6.6/3 allows function calls within unevaluated subexpressions of
22552 // constant expressions, but they can never be ICEs because an ICE cannot
22553 // contain an operand of (pointer to) function type.
22554 const CallExpr *CE = cast<CallExpr>(Val: E);
22555 if (CE->getBuiltinCallee())
22556 return CheckEvalInICE(E, Ctx);
22557 return ICEDiag(IK_NotICE, E->getBeginLoc());
22558 }
22559 case Expr::CXXRewrittenBinaryOperatorClass:
22560 return CheckICE(E: cast<CXXRewrittenBinaryOperator>(Val: E)->getSemanticForm(),
22561 Ctx);
22562 case Expr::DeclRefExprClass: {
22563 const NamedDecl *D = cast<DeclRefExpr>(Val: E)->getDecl();
22564 if (isa<EnumConstantDecl>(Val: D))
22565 return NoDiag();
22566
22567 // C++ and OpenCL (FIXME: spec reference?) allow reading const-qualified
22568 // integer variables in constant expressions:
22569 //
22570 // C++ 7.1.5.1p2
22571 // A variable of non-volatile const-qualified integral or enumeration
22572 // type initialized by an ICE can be used in ICEs.
22573 //
22574 // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In
22575 // that mode, use of reference variables should not be allowed.
22576 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
22577 if (VD && VD->isUsableInConstantExpressions(C: Ctx) &&
22578 !VD->getType()->isReferenceType())
22579 return NoDiag();
22580
22581 return ICEDiag(IK_NotICE, E->getBeginLoc());
22582 }
22583 case Expr::UnaryOperatorClass: {
22584 const UnaryOperator *Exp = cast<UnaryOperator>(Val: E);
22585 switch (Exp->getOpcode()) {
22586 case UO_PostInc:
22587 case UO_PostDec:
22588 case UO_PreInc:
22589 case UO_PreDec:
22590 case UO_AddrOf:
22591 case UO_Deref:
22592 case UO_Coawait:
22593 // C99 6.6/3 allows increment and decrement within unevaluated
22594 // subexpressions of constant expressions, but they can never be ICEs
22595 // because an ICE cannot contain an lvalue operand.
22596 return ICEDiag(IK_NotICE, E->getBeginLoc());
22597 case UO_Extension:
22598 case UO_LNot:
22599 case UO_Plus:
22600 case UO_Minus:
22601 case UO_Not:
22602 case UO_Real:
22603 case UO_Imag:
22604 return CheckICE(E: Exp->getSubExpr(), Ctx);
22605 }
22606 llvm_unreachable("invalid unary operator class");
22607 }
22608 case Expr::OffsetOfExprClass: {
22609 // Note that per C99, offsetof must be an ICE. And AFAIK, using
22610 // EvaluateAsRValue matches the proposed gcc behavior for cases like
22611 // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect
22612 // compliance: we should warn earlier for offsetof expressions with
22613 // array subscripts that aren't ICEs, and if the array subscripts
22614 // are ICEs, the value of the offsetof must be an integer constant.
22615 return CheckEvalInICE(E, Ctx);
22616 }
22617 case Expr::UnaryExprOrTypeTraitExprClass: {
22618 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(Val: E);
22619 if ((Exp->getKind() == UETT_SizeOf) &&
22620 Exp->getTypeOfArgument()->isVariableArrayType())
22621 return ICEDiag(IK_NotICE, E->getBeginLoc());
22622 if (Exp->getKind() == UETT_CountOf) {
22623 QualType ArgTy = Exp->getTypeOfArgument();
22624 if (ArgTy->isVariableArrayType()) {
22625 // We need to look whether the array is multidimensional. If it is,
22626 // then we want to check the size expression manually to see whether
22627 // it is an ICE or not.
22628 const auto *VAT = Ctx.getAsVariableArrayType(T: ArgTy);
22629 if (VAT->getElementType()->isArrayType())
22630 // Variable array size expression could be missing (e.g. int a[*][10])
22631 // In that case, it can't be a constant expression.
22632 return VAT->getSizeExpr() ? CheckICE(E: VAT->getSizeExpr(), Ctx)
22633 : ICEDiag(IK_NotICE, E->getBeginLoc());
22634
22635 // Otherwise, this is a regular VLA, which is definitely not an ICE.
22636 return ICEDiag(IK_NotICE, E->getBeginLoc());
22637 }
22638 }
22639 return NoDiag();
22640 }
22641 case Expr::BinaryOperatorClass: {
22642 const BinaryOperator *Exp = cast<BinaryOperator>(Val: E);
22643 switch (Exp->getOpcode()) {
22644 case BO_PtrMemD:
22645 case BO_PtrMemI:
22646 case BO_Assign:
22647 case BO_MulAssign:
22648 case BO_DivAssign:
22649 case BO_RemAssign:
22650 case BO_AddAssign:
22651 case BO_SubAssign:
22652 case BO_ShlAssign:
22653 case BO_ShrAssign:
22654 case BO_AndAssign:
22655 case BO_XorAssign:
22656 case BO_OrAssign:
22657 // C99 6.6/3 allows assignments within unevaluated subexpressions of
22658 // constant expressions, but they can never be ICEs because an ICE cannot
22659 // contain an lvalue operand.
22660 return ICEDiag(IK_NotICE, E->getBeginLoc());
22661
22662 case BO_Mul:
22663 case BO_Div:
22664 case BO_Rem:
22665 case BO_Add:
22666 case BO_Sub:
22667 case BO_Shl:
22668 case BO_Shr:
22669 case BO_LT:
22670 case BO_GT:
22671 case BO_LE:
22672 case BO_GE:
22673 case BO_EQ:
22674 case BO_NE:
22675 case BO_And:
22676 case BO_Xor:
22677 case BO_Or:
22678 case BO_Comma:
22679 case BO_Cmp: {
22680 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
22681 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
22682 if (Exp->getOpcode() == BO_Div ||
22683 Exp->getOpcode() == BO_Rem) {
22684 // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
22685 // we don't evaluate one.
22686 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) {
22687 llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
22688 if (REval == 0)
22689 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22690 if (REval.isSigned() && REval.isAllOnes()) {
22691 llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
22692 if (LEval.isMinSignedValue())
22693 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22694 }
22695 }
22696 }
22697 if (Exp->getOpcode() == BO_Comma) {
22698 if (Ctx.getLangOpts().C99) {
22699 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
22700 // if it isn't evaluated.
22701 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE)
22702 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22703 } else {
22704 // In both C89 and C++, commas in ICEs are illegal.
22705 return ICEDiag(IK_NotICE, E->getBeginLoc());
22706 }
22707 }
22708 return Worst(A: LHSResult, B: RHSResult);
22709 }
22710 case BO_LAnd:
22711 case BO_LOr: {
22712 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
22713 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
22714 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) {
22715 // Rare case where the RHS has a comma "side-effect"; we need
22716 // to actually check the condition to see whether the side
22717 // with the comma is evaluated.
22718 if ((Exp->getOpcode() == BO_LAnd) !=
22719 (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
22720 return RHSResult;
22721 return NoDiag();
22722 }
22723
22724 return Worst(A: LHSResult, B: RHSResult);
22725 }
22726 }
22727 llvm_unreachable("invalid binary operator kind");
22728 }
22729 case Expr::ImplicitCastExprClass:
22730 case Expr::CStyleCastExprClass:
22731 case Expr::CXXFunctionalCastExprClass:
22732 case Expr::CXXStaticCastExprClass:
22733 case Expr::CXXReinterpretCastExprClass:
22734 case Expr::CXXConstCastExprClass:
22735 case Expr::ObjCBridgedCastExprClass: {
22736 const Expr *SubExpr = cast<CastExpr>(Val: E)->getSubExpr();
22737 if (isa<ExplicitCastExpr>(Val: E)) {
22738 if (const FloatingLiteral *FL
22739 = dyn_cast<FloatingLiteral>(Val: SubExpr->IgnoreParenImpCasts())) {
22740 unsigned DestWidth = Ctx.getIntWidth(T: E->getType());
22741 bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
22742 APSInt IgnoredVal(DestWidth, !DestSigned);
22743 bool Ignored;
22744 // If the value does not fit in the destination type, the behavior is
22745 // undefined, so we are not required to treat it as a constant
22746 // expression.
22747 if (FL->getValue().convertToInteger(Result&: IgnoredVal,
22748 RM: llvm::APFloat::rmTowardZero,
22749 IsExact: &Ignored) & APFloat::opInvalidOp)
22750 return ICEDiag(IK_NotICE, E->getBeginLoc());
22751 return NoDiag();
22752 }
22753 }
22754 switch (cast<CastExpr>(Val: E)->getCastKind()) {
22755 case CK_LValueToRValue:
22756 case CK_AtomicToNonAtomic:
22757 case CK_NonAtomicToAtomic:
22758 case CK_NoOp:
22759 case CK_IntegralToBoolean:
22760 case CK_IntegralCast:
22761 return CheckICE(E: SubExpr, Ctx);
22762 default:
22763 return ICEDiag(IK_NotICE, E->getBeginLoc());
22764 }
22765 }
22766 case Expr::BinaryConditionalOperatorClass: {
22767 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(Val: E);
22768 ICEDiag CommonResult = CheckICE(E: Exp->getCommon(), Ctx);
22769 if (CommonResult.Kind == IK_NotICE) return CommonResult;
22770 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
22771 if (FalseResult.Kind == IK_NotICE) return FalseResult;
22772 if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult;
22773 if (FalseResult.Kind == IK_ICEIfUnevaluated &&
22774 Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag();
22775 return FalseResult;
22776 }
22777 case Expr::ConditionalOperatorClass: {
22778 const ConditionalOperator *Exp = cast<ConditionalOperator>(Val: E);
22779 // If the condition (ignoring parens) is a __builtin_constant_p call,
22780 // then only the true side is actually considered in an integer constant
22781 // expression, and it is fully evaluated. This is an important GNU
22782 // extension. See GCC PR38377 for discussion.
22783 if (const CallExpr *CallCE
22784 = dyn_cast<CallExpr>(Val: Exp->getCond()->IgnoreParenCasts()))
22785 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
22786 return CheckEvalInICE(E, Ctx);
22787 ICEDiag CondResult = CheckICE(E: Exp->getCond(), Ctx);
22788 if (CondResult.Kind == IK_NotICE)
22789 return CondResult;
22790
22791 ICEDiag TrueResult = CheckICE(E: Exp->getTrueExpr(), Ctx);
22792 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
22793
22794 if (TrueResult.Kind == IK_NotICE)
22795 return TrueResult;
22796 if (FalseResult.Kind == IK_NotICE)
22797 return FalseResult;
22798 if (CondResult.Kind == IK_ICEIfUnevaluated)
22799 return CondResult;
22800 if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE)
22801 return NoDiag();
22802 // Rare case where the diagnostics depend on which side is evaluated
22803 // Note that if we get here, CondResult is 0, and at least one of
22804 // TrueResult and FalseResult is non-zero.
22805 if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0)
22806 return FalseResult;
22807 return TrueResult;
22808 }
22809 case Expr::CXXDefaultArgExprClass:
22810 return CheckICE(E: cast<CXXDefaultArgExpr>(Val: E)->getExpr(), Ctx);
22811 case Expr::CXXDefaultInitExprClass:
22812 return CheckICE(E: cast<CXXDefaultInitExpr>(Val: E)->getExpr(), Ctx);
22813 case Expr::ChooseExprClass: {
22814 return CheckICE(E: cast<ChooseExpr>(Val: E)->getChosenSubExpr(), Ctx);
22815 }
22816 case Expr::BuiltinBitCastExprClass: {
22817 if (!checkBitCastConstexprEligibility(Info: nullptr, Ctx, BCE: cast<CastExpr>(Val: E)))
22818 return ICEDiag(IK_NotICE, E->getBeginLoc());
22819 return CheckICE(E: cast<CastExpr>(Val: E)->getSubExpr(), Ctx);
22820 }
22821 }
22822
22823 llvm_unreachable("Invalid StmtClass!");
22824}
22825
22826/// Evaluate an expression as a C++11 integral constant expression.
22827static bool
22828EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, const Expr *E,
22829 llvm::APSInt *Value,
22830 bool AllowRelaxedEval = false) {
22831 if (!E->getType()->isIntegralOrUnscopedEnumerationType())
22832 return false;
22833
22834 APValue Result;
22835 if (!E->isCXX11ConstantExpr(Ctx, Result: &Result, AllowRelaxedEval))
22836 return false;
22837
22838 if (!Result.isInt())
22839 return false;
22840
22841 if (Value) *Value = Result.getInt();
22842 return true;
22843}
22844
22845bool Expr::isIntegerConstantExpr(const ASTContext &Ctx) const {
22846 assert(!isValueDependent() &&
22847 "Expression evaluator can't be called on a dependent expression.");
22848
22849 ExprTimeTraceScope TimeScope(this, Ctx, "isIntegerConstantExpr");
22850
22851 if (Ctx.getLangOpts().CPlusPlus11)
22852 return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: nullptr);
22853
22854 ICEDiag D = CheckICE(E: this, Ctx);
22855 if (D.Kind != IK_ICE)
22856 return false;
22857 return true;
22858}
22859
22860std::optional<llvm::APSInt>
22861Expr::getIntegerConstantExpr(const ASTContext &Ctx,
22862 bool AllowRelaxedEval) const {
22863 if (isValueDependent()) {
22864 // Expression evaluator can't succeed on a dependent expression.
22865 return std::nullopt;
22866 }
22867
22868 if (Ctx.getLangOpts().CPlusPlus11) {
22869 APSInt Value;
22870 if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: &Value,
22871 AllowRelaxedEval))
22872 return Value;
22873 return std::nullopt;
22874 }
22875
22876 if (!isIntegerConstantExpr(Ctx))
22877 return std::nullopt;
22878
22879 // The only possible side-effects here are due to UB discovered in the
22880 // evaluation (for instance, INT_MAX + 1). In such a case, we are still
22881 // required to treat the expression as an ICE, so we produce the folded
22882 // value.
22883 EvalResult ExprResult;
22884 Expr::EvalStatus Status;
22885 EvalInfo Info(Ctx, Status, EvaluationMode::IgnoreSideEffects);
22886 Info.InConstantContext = true;
22887
22888 if (!::EvaluateAsInt(E: this, ExprResult, Ctx, AllowSideEffects: SE_AllowSideEffects, Info))
22889 llvm_unreachable("ICE cannot be evaluated!");
22890
22891 return ExprResult.Val.getInt();
22892}
22893
22894bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const {
22895 assert(!isValueDependent() &&
22896 "Expression evaluator can't be called on a dependent expression.");
22897
22898 return CheckICE(E: this, Ctx).Kind == IK_ICE;
22899}
22900
22901bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result,
22902 bool AllowRelaxedEval) const {
22903 assert(!isValueDependent() &&
22904 "Expression evaluator can't be called on a dependent expression.");
22905
22906 // We support this checking in C++98 mode in order to diagnose compatibility
22907 // issues.
22908 assert(Ctx.getLangOpts().CPlusPlus);
22909
22910 bool IsConst;
22911 APValue Scratch;
22912 if (FastEvaluateAsRValue(Exp: this, Result&: Scratch, Ctx, IsConst) && Scratch.hasValue()) {
22913 if (Result)
22914 *Result = std::move(Scratch);
22915 return true;
22916 }
22917
22918 // Build evaluation settings.
22919 Expr::EvalStatus Status;
22920 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
22921 SmallVector<PartialDiagnosticAt> MSRelaxedDiag;
22922 Status.ExtendedDiag = AllowRelaxedEval ? &MSRelaxedDiag : nullptr;
22923
22924 bool IsConstExpr =
22925 ::EvaluateAsRValue(Info, E: this, Result&: Result ? *Result : Scratch) &&
22926 // NOTE: We don't produce a diagnostic for this, but the callers that
22927 // call us on arbitrary full-expressions should generally not care.
22928 Info.discardCleanups() && !Status.HasSideEffects;
22929
22930 return IsConstExpr && !Status.DiagEmitted;
22931}
22932
22933bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx,
22934 const FunctionDecl *Callee,
22935 ArrayRef<const Expr*> Args,
22936 const Expr *This) const {
22937 assert(!isValueDependent() &&
22938 "Expression evaluator can't be called on a dependent expression.");
22939
22940 llvm::TimeTraceScope TimeScope("EvaluateWithSubstitution", [&] {
22941 std::string Name;
22942 llvm::raw_string_ostream OS(Name);
22943 Callee->getNameForDiagnostic(OS, Policy: Ctx.getPrintingPolicy(),
22944 /*Qualified=*/true);
22945 return Name;
22946 });
22947
22948 Expr::EvalStatus Status;
22949 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpressionUnevaluated);
22950 Info.InConstantContext = true;
22951
22952 if (Info.EnableNewConstInterp) {
22953 if (std::optional<bool> BoolResult =
22954 Info.Ctx.getInterpContext().evaluateWithSubstitution(
22955 Parent&: Info, Callee, Args, This, Condition: this)) {
22956 Value = APValue(APSInt(APInt(1, static_cast<uint64_t>(*BoolResult))));
22957 return true;
22958 }
22959 return false;
22960 }
22961
22962 LValue ThisVal;
22963 const LValue *ThisPtr = nullptr;
22964 if (This) {
22965#ifndef NDEBUG
22966 auto *MD = dyn_cast<CXXMethodDecl>(Callee);
22967 assert(MD && "Don't provide `this` for non-methods.");
22968 assert(MD->isImplicitObjectMemberFunction() &&
22969 "Don't provide `this` for methods without an implicit object.");
22970#endif
22971 if (!This->isValueDependent() &&
22972 EvaluateObjectArgument(Info, Object: This, This&: ThisVal) &&
22973 !Info.EvalStatus.HasSideEffects)
22974 ThisPtr = &ThisVal;
22975
22976 // Ignore any side-effects from a failed evaluation. This is safe because
22977 // they can't interfere with any other argument evaluation.
22978 Info.EvalStatus.HasSideEffects = false;
22979 }
22980
22981 CallRef Call = Info.CurrentCall->createCall(Callee);
22982 for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
22983 I != E; ++I) {
22984 unsigned Idx = I - Args.begin();
22985 if (Idx >= Callee->getNumParams())
22986 break;
22987 const ParmVarDecl *PVD = Callee->getParamDecl(i: Idx);
22988 if ((*I)->isValueDependent() ||
22989 !EvaluateCallArg(PVD, Arg: *I, Call, Info) ||
22990 Info.EvalStatus.HasSideEffects) {
22991 // If evaluation fails, throw away the argument entirely.
22992 if (APValue *Slot = Info.getParamSlot(Call, PVD))
22993 *Slot = APValue();
22994 }
22995
22996 // Ignore any side-effects from a failed evaluation. This is safe because
22997 // they can't interfere with any other argument evaluation.
22998 Info.EvalStatus.HasSideEffects = false;
22999 }
23000
23001 // Parameter cleanups happen in the caller and are not part of this
23002 // evaluation.
23003 Info.discardCleanups();
23004 Info.EvalStatus.HasSideEffects = false;
23005
23006 // Build fake call to Callee.
23007 CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, This,
23008 Call);
23009 // FIXME: Missing ExprWithCleanups in enable_if conditions?
23010 FullExpressionRAII Scope(Info);
23011 return Evaluate(Result&: Value, Info, E: this) && Scope.destroy() &&
23012 !Info.EvalStatus.HasSideEffects;
23013}
23014
23015bool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
23016 SmallVectorImpl<
23017 PartialDiagnosticAt> &Diags) {
23018 // FIXME: It would be useful to check constexpr function templates, but at the
23019 // moment the constant expression evaluator cannot cope with the non-rigorous
23020 // ASTs which we build for dependent expressions.
23021 if (FD->isDependentContext())
23022 return true;
23023
23024 llvm::TimeTraceScope TimeScope("isPotentialConstantExpr", [&] {
23025 std::string Name;
23026 llvm::raw_string_ostream OS(Name);
23027 FD->getNameForDiagnostic(OS, Policy: FD->getASTContext().getPrintingPolicy(),
23028 /*Qualified=*/true);
23029 return Name;
23030 });
23031
23032 Expr::EvalStatus Status;
23033 Status.Diag = &Diags;
23034
23035 EvalInfo Info(FD->getASTContext(), Status,
23036 EvaluationMode::ConstantExpression);
23037 Info.InConstantContext = true;
23038 Info.CheckingPotentialConstantExpression = true;
23039
23040 // The constexpr VM attempts to compile all methods to bytecode here.
23041 if (Info.EnableNewConstInterp) {
23042 Info.Ctx.getInterpContext().isPotentialConstantExpr(Parent&: Info, FD);
23043 return Diags.empty();
23044 }
23045
23046 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
23047 const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr;
23048
23049 // Fabricate an arbitrary expression on the stack and pretend that it
23050 // is a temporary being used as the 'this' pointer.
23051 LValue This;
23052 ImplicitValueInitExpr VIE(RD ? Info.Ctx.getCanonicalTagType(TD: RD)
23053 : Info.Ctx.IntTy);
23054 This.set(B: {&VIE, Info.CurrentCall->Index});
23055
23056 ArrayRef<const Expr*> Args;
23057
23058 APValue Scratch;
23059 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Val: FD)) {
23060 // Evaluate the call as a constant initializer, to allow the construction
23061 // of objects of non-literal types.
23062 Info.setEvaluatingDecl(Base: This.getLValueBase(), Value&: Scratch);
23063 HandleConstructorCall(E: &VIE, This, Args, Definition: CD, Info, Result&: Scratch);
23064 } else {
23065 SourceLocation Loc = FD->getLocation();
23066 HandleFunctionCall(
23067 CallLoc: Loc, Callee: FD, ObjectArg: (MD && MD->isImplicitObjectMemberFunction()) ? &This : nullptr,
23068 E: &VIE, Args, Call: CallRef(), Body: FD->getBody(), Info, Result&: Scratch,
23069 /*ResultSlot=*/nullptr);
23070 }
23071
23072 return Diags.empty();
23073}
23074
23075bool Expr::isPotentialConstantExprUnevaluated(Expr *E,
23076 const FunctionDecl *FD,
23077 SmallVectorImpl<
23078 PartialDiagnosticAt> &Diags) {
23079 assert(!E->isValueDependent() &&
23080 "Expression evaluator can't be called on a dependent expression.");
23081
23082 Expr::EvalStatus Status;
23083 Status.Diag = &Diags;
23084
23085 EvalInfo Info(FD->getASTContext(), Status,
23086 EvaluationMode::ConstantExpressionUnevaluated);
23087 Info.InConstantContext = true;
23088 Info.CheckingPotentialConstantExpression = true;
23089
23090 if (Info.EnableNewConstInterp) {
23091 Info.Ctx.getInterpContext().isPotentialConstantExprUnevaluated(Parent&: Info, E, FD);
23092 return Diags.empty();
23093 }
23094
23095 // Fabricate a call stack frame to give the arguments a plausible cover story.
23096 CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr,
23097 /*CallExpr=*/nullptr, CallRef());
23098
23099 APValue ResultScratch;
23100 Evaluate(Result&: ResultScratch, Info, E);
23101 return Diags.empty();
23102}
23103
23104std::optional<uint64_t> Expr::tryEvaluateObjectSize(const ASTContext &Ctx,
23105 unsigned Type) const {
23106 if (!getType()->isPointerType())
23107 return std::nullopt;
23108
23109 Expr::EvalStatus Status;
23110 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23111 if (Info.EnableNewConstInterp)
23112 return Info.Ctx.getInterpContext().tryEvaluateObjectSize(
23113 Parent&: Info, E: this, Kind: Type,
23114 /*IsDynamic=*/false);
23115
23116 return tryEvaluateBuiltinObjectSize(E: this, Type, Info);
23117}
23118
23119static std::optional<uint64_t>
23120EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
23121 std::string *StringResult) {
23122 if (!E->getType()->hasPointerRepresentation() || !E->isPRValue())
23123 return std::nullopt;
23124
23125 LValue String;
23126
23127 if (!EvaluatePointer(E, Result&: String, Info))
23128 return std::nullopt;
23129
23130 // Fast path: if it's a string literal, search the string value.
23131 if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>(
23132 Val: String.getLValueBase().dyn_cast<const Expr *>())) {
23133 StringRef Str = S->getBytes();
23134 int64_t Off = String.Offset.getQuantity();
23135 if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size()) {
23136 UnsignedOrNone ZeroIndex = S->findZeroCodeUnit(StartIndex: Off);
23137 if (StringResult) {
23138 if (ZeroIndex)
23139 Str = Str.substr(Start: Off, N: *ZeroIndex);
23140 *StringResult = Str;
23141 }
23142
23143 return ZeroIndex.value_or(Def: Str.size());
23144 }
23145 // For an invalid index, fall through to the offset handling below.
23146 }
23147
23148 QualType CharTy = E->getType()->getPointeeType();
23149 // Slow path: scan the bytes of the string looking for the terminating 0.
23150 for (uint64_t Strlen = 0; /**/; ++Strlen) {
23151 APValue Char;
23152 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: String, RVal&: Char) ||
23153 !Char.isInt())
23154 return std::nullopt;
23155 if (!Char.getInt())
23156 return Strlen;
23157 else if (StringResult)
23158 StringResult->push_back(c: Char.getInt().getExtValue());
23159 if (!HandleLValueArrayAdjustment(Info, E, LVal&: String, EltTy: CharTy, Adjustment: 1))
23160 return std::nullopt;
23161 }
23162}
23163
23164std::optional<std::string> Expr::tryEvaluateString(ASTContext &Ctx) const {
23165 Expr::EvalStatus Status;
23166 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23167 std::string StringResult;
23168
23169 if (Info.EnableNewConstInterp) {
23170 if (!Info.Ctx.getInterpContext().evaluateString(Parent&: Info, E: this, Result&: StringResult))
23171 return std::nullopt;
23172 return StringResult;
23173 }
23174
23175 if (EvaluateBuiltinStrLen(E: this, Info, StringResult: &StringResult))
23176 return StringResult;
23177 return std::nullopt;
23178}
23179
23180template <typename T>
23181static bool EvaluateCharRangeAsStringImpl(const Expr *, T &Result,
23182 const Expr *SizeExpression,
23183 const Expr *PtrExpression,
23184 ASTContext &Ctx,
23185 Expr::EvalResult &Status) {
23186 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
23187 Info.InConstantContext = true;
23188
23189 if (Info.EnableNewConstInterp)
23190 return Info.Ctx.getInterpContext().evaluateCharRange(Info, SizeExpression,
23191 PtrExpression, Result);
23192
23193 LValue String;
23194 FullExpressionRAII Scope(Info);
23195 APSInt SizeValue;
23196 if (!::EvaluateInteger(E: SizeExpression, Result&: SizeValue, Info))
23197 return false;
23198
23199 uint64_t Size = SizeValue.getZExtValue();
23200
23201 // FIXME: better protect against invalid or excessive sizes
23202 if constexpr (std::is_same_v<APValue, T>)
23203 Result = APValue(APValue::UninitArray{}, Size, Size);
23204 else {
23205 if (Size < Result.max_size())
23206 Result.reserve(Size);
23207 }
23208 if (!::EvaluatePointer(E: PtrExpression, Result&: String, Info))
23209 return false;
23210
23211 QualType CharTy = PtrExpression->getType()->getPointeeType();
23212 for (uint64_t I = 0; I < Size; ++I) {
23213 APValue Char;
23214 if (!handleLValueToRValueConversion(Info, Conv: PtrExpression, Type: CharTy, LVal: String,
23215 RVal&: Char))
23216 return false;
23217
23218 if constexpr (std::is_same_v<APValue, T>) {
23219 Result.getArrayInitializedElt(I) = std::move(Char);
23220 } else {
23221 APSInt C = Char.getInt();
23222
23223 assert(C.getBitWidth() <= 8 &&
23224 "string element not representable in char");
23225
23226 Result.push_back(static_cast<char>(C.getExtValue()));
23227 }
23228
23229 if (!HandleLValueArrayAdjustment(Info, E: PtrExpression, LVal&: String, EltTy: CharTy, Adjustment: 1))
23230 return false;
23231 }
23232
23233 return Scope.destroy() && CheckMemoryLeaks(Info);
23234}
23235
23236bool Expr::EvaluateCharRangeAsString(std::string &Result,
23237 const Expr *SizeExpression,
23238 const Expr *PtrExpression, ASTContext &Ctx,
23239 EvalResult &Status) const {
23240 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
23241 PtrExpression, Ctx, Status);
23242}
23243
23244bool Expr::EvaluateCharRangeAsString(APValue &Result,
23245 const Expr *SizeExpression,
23246 const Expr *PtrExpression, ASTContext &Ctx,
23247 EvalResult &Status) const {
23248 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
23249 PtrExpression, Ctx, Status);
23250}
23251
23252std::optional<uint64_t> Expr::tryEvaluateStrLen(const ASTContext &Ctx) const {
23253 Expr::EvalStatus Status;
23254 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23255
23256 if (Info.EnableNewConstInterp)
23257 return Info.Ctx.getInterpContext().evaluateStrlen(Parent&: Info, E: this);
23258 return EvaluateBuiltinStrLen(E: this, Info);
23259}
23260
23261namespace {
23262struct IsWithinLifetimeHandler {
23263 EvalInfo &Info;
23264 static constexpr AccessKinds AccessKind = AccessKinds::AK_IsWithinLifetime;
23265 using result_type = std::optional<bool>;
23266 std::optional<bool> failed() { return std::nullopt; }
23267 template <typename T>
23268 std::optional<bool> found(T &Subobj, QualType SubobjType,
23269 APValue::LValueBase) {
23270 return true;
23271 }
23272 template <typename T>
23273 std::optional<bool> found(T &Subobj, QualType SubobjType) {
23274 return true;
23275 }
23276};
23277
23278std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &IEE,
23279 const CallExpr *E) {
23280 EvalInfo &Info = IEE.Info;
23281 // Sometimes this is called during some sorts of constant folding / early
23282 // evaluation. These are meant for non-constant expressions and are not
23283 // necessary since this consteval builtin will never be evaluated at runtime.
23284 // Just fail to evaluate when not in a constant context.
23285 if (!Info.InConstantContext)
23286 return std::nullopt;
23287 assert(E->getBuiltinCallee() == Builtin::BI__builtin_is_within_lifetime);
23288 const Expr *Arg = E->getArg(Arg: 0);
23289 if (Arg->isValueDependent())
23290 return std::nullopt;
23291 LValue Val;
23292 if (!EvaluatePointer(E: Arg, Result&: Val, Info))
23293 return std::nullopt;
23294
23295 if (Val.allowConstexprUnknown())
23296 return true;
23297
23298 auto Error = [&](int Diag) {
23299 bool CalledFromStd = false;
23300 const auto *Callee = Info.CurrentCall->getCallee();
23301 if (Callee && Callee->isInStdNamespace()) {
23302 const IdentifierInfo *Identifier = Callee->getIdentifier();
23303 CalledFromStd = Identifier && Identifier->isStr(Str: "is_within_lifetime");
23304 }
23305 Info.CCEDiag(Loc: CalledFromStd ? Info.CurrentCall->getCallRange().getBegin()
23306 : E->getExprLoc(),
23307 DiagId: diag::err_invalid_is_within_lifetime)
23308 << (CalledFromStd ? "std::is_within_lifetime"
23309 : "__builtin_is_within_lifetime")
23310 << Diag;
23311 return std::nullopt;
23312 };
23313 // C++2c [meta.const.eval]p4:
23314 // During the evaluation of an expression E as a core constant expression, a
23315 // call to this function is ill-formed unless p points to an object that is
23316 // usable in constant expressions or whose complete object's lifetime began
23317 // within E.
23318
23319 // Make sure it points to an object
23320 // nullptr does not point to an object
23321 if (Val.isNullPointer() || Val.getLValueBase().isNull())
23322 return Error(0);
23323 QualType T = Val.getLValueBase().getType();
23324 assert(!T->isFunctionType() &&
23325 "Pointers to functions should have been typed as function pointers "
23326 "which would have been rejected earlier");
23327 assert(T->isObjectType());
23328 // Hypothetical array element is not an object
23329 if (Val.getLValueDesignator().isOnePastTheEnd())
23330 return Error(1);
23331 assert(Val.getLValueDesignator().isValidSubobject() &&
23332 "Unchecked case for valid subobject");
23333 // All other ill-formed values should have failed EvaluatePointer, so the
23334 // object should be a pointer to an object that is usable in a constant
23335 // expression or whose complete lifetime began within the expression
23336 CompleteObject CO =
23337 findCompleteObject(Info, E, AK: AccessKinds::AK_IsWithinLifetime, LVal: Val, LValType: T);
23338 // The lifetime hasn't begun yet if we are still evaluating the
23339 // initializer ([basic.life]p(1.2))
23340 if (Info.EvaluatingDeclValue && CO.Value == Info.EvaluatingDeclValue)
23341 return Error(2);
23342
23343 if (!CO)
23344 return false;
23345 IsWithinLifetimeHandler handler{.Info: Info};
23346 return findSubobject(Info, E, Obj: CO, Sub: Val.getLValueDesignator(), handler);
23347}
23348} // namespace
23349