1//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Expr constant evaluator.
10//
11// Constant expression evaluation produces four main results:
12//
13// * A success/failure flag indicating whether constant folding was successful.
14// This is the 'bool' return value used by most of the code in this file. A
15// 'false' return value indicates that constant folding has failed, and any
16// appropriate diagnostic has already been produced.
17//
18// * An evaluated result, valid only if constant folding has not failed.
19//
20// * A flag indicating if evaluation encountered (unevaluated) side-effects.
21// These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1),
22// where it is possible to determine the evaluated result regardless.
23//
24// * A set of notes indicating why the evaluation was not a constant expression
25// (under the C++11 / C++1y rules only, at the moment), or, if folding failed
26// too, why the expression could not be folded.
27//
28// If we are checking for a potential constant expression, failure to constant
29// fold a potential constant sub-expression will be indicated by a 'false'
30// return value (the expression could not be folded) and no diagnostic (the
31// expression is not necessarily non-constant).
32//
33//===----------------------------------------------------------------------===//
34
35#include "ByteCode/Context.h"
36#include "ByteCode/Frame.h"
37#include "ByteCode/State.h"
38#include "ExprConstShared.h"
39#include "clang/AST/APValue.h"
40#include "clang/AST/ASTContext.h"
41#include "clang/AST/ASTLambda.h"
42#include "clang/AST/Attr.h"
43#include "clang/AST/CXXInheritance.h"
44#include "clang/AST/CharUnits.h"
45#include "clang/AST/CurrentSourceLocExprScope.h"
46#include "clang/AST/Expr.h"
47#include "clang/AST/InferAlloc.h"
48#include "clang/AST/OSLog.h"
49#include "clang/AST/OptionalDiagnostic.h"
50#include "clang/AST/RecordLayout.h"
51#include "clang/AST/StmtVisitor.h"
52#include "clang/AST/Type.h"
53#include "clang/AST/TypeLoc.h"
54#include "clang/Basic/Builtins.h"
55#include "clang/Basic/DiagnosticSema.h"
56#include "clang/Basic/TargetBuiltins.h"
57#include "clang/Basic/TargetInfo.h"
58#include "llvm/ADT/APFixedPoint.h"
59#include "llvm/ADT/Sequence.h"
60#include "llvm/ADT/SmallBitVector.h"
61#include "llvm/ADT/StringExtras.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/Debug.h"
64#include "llvm/Support/SaveAndRestore.h"
65#include "llvm/Support/SipHash.h"
66#include "llvm/Support/TimeProfiler.h"
67#include "llvm/Support/raw_ostream.h"
68#include <cstring>
69#include <functional>
70#include <limits>
71#include <optional>
72
73#define DEBUG_TYPE "exprconstant"
74
75using namespace clang;
76using llvm::APFixedPoint;
77using llvm::APInt;
78using llvm::APSInt;
79using llvm::APFloat;
80using llvm::FixedPointSemantics;
81
82namespace {
83 struct LValue;
84 class CallStackFrame;
85 class EvalInfo;
86
87 using SourceLocExprScopeGuard =
88 CurrentSourceLocExprScope::SourceLocExprScopeGuard;
89
90 static QualType getType(APValue::LValueBase B) {
91 return B.getType();
92 }
93
94 /// Get an LValue path entry, which is known to not be an array index, as a
95 /// field declaration.
96 static const FieldDecl *getAsField(APValue::LValuePathEntry E) {
97 return dyn_cast_or_null<FieldDecl>(Val: E.getAsBaseOrMember().getPointer());
98 }
99 /// Get an LValue path entry, which is known to not be an array index, as a
100 /// base class declaration.
101 static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) {
102 return dyn_cast_or_null<CXXRecordDecl>(Val: E.getAsBaseOrMember().getPointer());
103 }
104 /// Determine whether this LValue path entry for a base class names a virtual
105 /// base class.
106 static bool isVirtualBaseClass(APValue::LValuePathEntry E) {
107 return E.getAsBaseOrMember().getInt();
108 }
109
110 /// Given an expression, determine the type used to store the result of
111 /// evaluating that expression.
112 static QualType getStorageType(const ASTContext &Ctx, const Expr *E) {
113 if (E->isPRValue())
114 return E->getType();
115 return Ctx.getLValueReferenceType(T: E->getType());
116 }
117
118 static unsigned countNonVirtualBases(const CXXRecordDecl *RD) {
119 return llvm::count_if(Range: RD->bases(), P: [](auto &B) { return !B.isVirtual(); });
120 }
121
122 /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.
123 /// This will look through a single cast.
124 ///
125 /// Returns null if we couldn't unwrap a function with alloc_size.
126 static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) {
127 if (!E->getType()->isPointerType())
128 return nullptr;
129
130 E = E->IgnoreParens();
131 // If we're doing a variable assignment from e.g. malloc(N), there will
132 // probably be a cast of some kind. In exotic cases, we might also see a
133 // top-level ExprWithCleanups. Ignore them either way.
134 if (const auto *FE = dyn_cast<FullExpr>(Val: E))
135 E = FE->getSubExpr()->IgnoreParens();
136
137 if (const auto *Cast = dyn_cast<CastExpr>(Val: E))
138 E = Cast->getSubExpr()->IgnoreParens();
139
140 if (const auto *CE = dyn_cast<CallExpr>(Val: E))
141 return CE->getCalleeAllocSizeAttr() ? CE : nullptr;
142 return nullptr;
143 }
144
145 /// Determines whether or not the given Base contains a call to a function
146 /// with the alloc_size attribute.
147 static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) {
148 const auto *E = Base.dyn_cast<const Expr *>();
149 return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E);
150 }
151
152 /// Determines whether the given kind of constant expression is only ever
153 /// used for name mangling. If so, it's permitted to reference things that we
154 /// can't generate code for (in particular, dllimported functions).
155 static bool isForManglingOnly(ConstantExprKind Kind) {
156 switch (Kind) {
157 case ConstantExprKind::Normal:
158 case ConstantExprKind::ClassTemplateArgument:
159 case ConstantExprKind::ImmediateInvocation:
160 // Note that non-type template arguments of class type are emitted as
161 // template parameter objects.
162 return false;
163
164 case ConstantExprKind::NonClassTemplateArgument:
165 return true;
166 }
167 llvm_unreachable("unknown ConstantExprKind");
168 }
169
170 static bool isTemplateArgument(ConstantExprKind Kind) {
171 switch (Kind) {
172 case ConstantExprKind::Normal:
173 case ConstantExprKind::ImmediateInvocation:
174 return false;
175
176 case ConstantExprKind::ClassTemplateArgument:
177 case ConstantExprKind::NonClassTemplateArgument:
178 return true;
179 }
180 llvm_unreachable("unknown ConstantExprKind");
181 }
182
183 /// The bound to claim that an array of unknown bound has.
184 /// The value in MostDerivedArraySize is undefined in this case. So, set it
185 /// to an arbitrary value that's likely to loudly break things if it's used.
186 static const uint64_t AssumedSizeForUnsizedArray =
187 std::numeric_limits<uint64_t>::max() / 2;
188
189 /// Determines if an LValue with the given LValueBase will have an unsized
190 /// array in its designator.
191 /// Find the path length and type of the most-derived subobject in the given
192 /// path, and find the size of the containing array, if any.
193 static unsigned
194 findMostDerivedSubobject(const ASTContext &Ctx, APValue::LValueBase Base,
195 ArrayRef<APValue::LValuePathEntry> Path,
196 uint64_t &ArraySize, QualType &Type, bool &IsArray,
197 bool &FirstEntryIsUnsizedArray) {
198 // This only accepts LValueBases from APValues, and APValues don't support
199 // arrays that lack size info.
200 assert(!isBaseAnAllocSizeCall(Base) &&
201 "Unsized arrays shouldn't appear here");
202 unsigned MostDerivedLength = 0;
203 // The type of Base is a reference type if the base is a constexpr-unknown
204 // variable. In that case, look through the reference type.
205 Type = getType(B: Base).getNonReferenceType();
206
207 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
208 if (Type->isArrayType()) {
209 const ArrayType *AT = Ctx.getAsArrayType(T: Type);
210 Type = AT->getElementType();
211 MostDerivedLength = I + 1;
212 IsArray = true;
213
214 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) {
215 ArraySize = CAT->getZExtSize();
216 } else {
217 assert(I == 0 && "unexpected unsized array designator");
218 FirstEntryIsUnsizedArray = true;
219 ArraySize = AssumedSizeForUnsizedArray;
220 }
221 } else if (Type->isAnyComplexType()) {
222 const ComplexType *CT = Type->castAs<ComplexType>();
223 Type = CT->getElementType();
224 ArraySize = 2;
225 MostDerivedLength = I + 1;
226 IsArray = true;
227 } else if (const auto *VT = Type->getAs<VectorType>()) {
228 Type = VT->getElementType();
229 ArraySize = VT->getNumElements();
230 MostDerivedLength = I + 1;
231 IsArray = true;
232 } else if (const FieldDecl *FD = getAsField(E: Path[I])) {
233 Type = FD->getType();
234 ArraySize = 0;
235 MostDerivedLength = I + 1;
236 IsArray = false;
237 } else {
238 // Path[I] describes a base class.
239 ArraySize = 0;
240 IsArray = false;
241 }
242 }
243 return MostDerivedLength;
244 }
245
246 /// A path from a glvalue to a subobject of that glvalue.
247 struct SubobjectDesignator {
248 /// True if the subobject was named in a manner not supported by C++11. Such
249 /// lvalues can still be folded, but they are not core constant expressions
250 /// and we cannot perform lvalue-to-rvalue conversions on them.
251 LLVM_PREFERRED_TYPE(bool)
252 unsigned Invalid : 1;
253
254 /// Is this a pointer one past the end of an object?
255 LLVM_PREFERRED_TYPE(bool)
256 unsigned IsOnePastTheEnd : 1;
257
258 /// Indicator of whether the first entry is an unsized array.
259 LLVM_PREFERRED_TYPE(bool)
260 unsigned FirstEntryIsAnUnsizedArray : 1;
261
262 /// Indicator of whether the most-derived object is an array element.
263 LLVM_PREFERRED_TYPE(bool)
264 unsigned MostDerivedIsArrayElement : 1;
265
266 /// The length of the path to the most-derived object of which this is a
267 /// subobject.
268 unsigned MostDerivedPathLength : 28;
269
270 /// The size of the array of which the most-derived object is an element.
271 /// This will always be 0 if the most-derived object is not an array
272 /// element. 0 is not an indicator of whether or not the most-derived object
273 /// is an array, however, because 0-length arrays are allowed.
274 ///
275 /// If the current array is an unsized array, the value of this is
276 /// undefined.
277 uint64_t MostDerivedArraySize;
278 /// The type of the most derived object referred to by this address.
279 QualType MostDerivedType;
280
281 typedef APValue::LValuePathEntry PathEntry;
282
283 /// The entries on the path from the glvalue to the designated subobject.
284 SmallVector<PathEntry, 8> Entries;
285
286 SubobjectDesignator() : Invalid(true) {}
287
288 explicit SubobjectDesignator(QualType T)
289 : Invalid(false), IsOnePastTheEnd(false),
290 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
291 MostDerivedPathLength(0), MostDerivedArraySize(0),
292 MostDerivedType(T.isNull() ? QualType() : T.getNonReferenceType()) {}
293
294 SubobjectDesignator(const ASTContext &Ctx, const APValue &V)
295 : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false),
296 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
297 MostDerivedPathLength(0), MostDerivedArraySize(0) {
298 assert(V.isLValue() && "Non-LValue used to make an LValue designator?");
299 if (!Invalid) {
300 IsOnePastTheEnd = V.isLValueOnePastTheEnd();
301 llvm::append_range(C&: Entries, R: V.getLValuePath());
302 if (V.getLValueBase()) {
303 bool IsArray = false;
304 bool FirstIsUnsizedArray = false;
305 MostDerivedPathLength = findMostDerivedSubobject(
306 Ctx, Base: V.getLValueBase(), Path: V.getLValuePath(), ArraySize&: MostDerivedArraySize,
307 Type&: MostDerivedType, IsArray, FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
308 MostDerivedIsArrayElement = IsArray;
309 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
310 }
311 }
312 }
313
314 void truncate(ASTContext &Ctx, APValue::LValueBase Base,
315 unsigned NewLength) {
316 if (Invalid)
317 return;
318
319 assert(Base && "cannot truncate path for null pointer");
320 assert(NewLength <= Entries.size() && "not a truncation");
321
322 if (NewLength == Entries.size())
323 return;
324 Entries.resize(N: NewLength);
325
326 bool IsArray = false;
327 bool FirstIsUnsizedArray = false;
328 MostDerivedPathLength = findMostDerivedSubobject(
329 Ctx, Base, Path: Entries, ArraySize&: MostDerivedArraySize, Type&: MostDerivedType, IsArray,
330 FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
331 MostDerivedIsArrayElement = IsArray;
332 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
333 }
334
335 void setInvalid() {
336 Invalid = true;
337 Entries.clear();
338 }
339
340 /// Determine whether the most derived subobject is an array without a
341 /// known bound.
342 bool isMostDerivedAnUnsizedArray() const {
343 assert(!Invalid && "Calling this makes no sense on invalid designators");
344 return Entries.size() == 1 && FirstEntryIsAnUnsizedArray;
345 }
346
347 /// Determine what the most derived array's size is. Results in an assertion
348 /// failure if the most derived array lacks a size.
349 uint64_t getMostDerivedArraySize() const {
350 assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size");
351 return MostDerivedArraySize;
352 }
353
354 /// Determine whether this is a one-past-the-end pointer.
355 bool isOnePastTheEnd() const {
356 assert(!Invalid);
357 if (IsOnePastTheEnd)
358 return true;
359 if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement &&
360 Entries[MostDerivedPathLength - 1].getAsArrayIndex() ==
361 MostDerivedArraySize)
362 return true;
363 return false;
364 }
365
366 /// Get the range of valid index adjustments in the form
367 /// {maximum value that can be subtracted from this pointer,
368 /// maximum value that can be added to this pointer}
369 std::pair<uint64_t, uint64_t> validIndexAdjustments() {
370 if (Invalid || isMostDerivedAnUnsizedArray())
371 return {0, 0};
372
373 // [expr.add]p4: For the purposes of these operators, a pointer to a
374 // nonarray object behaves the same as a pointer to the first element of
375 // an array of length one with the type of the object as its element type.
376 bool IsArray = MostDerivedPathLength == Entries.size() &&
377 MostDerivedIsArrayElement;
378 uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex()
379 : (uint64_t)IsOnePastTheEnd;
380 uint64_t ArraySize =
381 IsArray ? getMostDerivedArraySize() : (uint64_t)1;
382 return {ArrayIndex, ArraySize - ArrayIndex};
383 }
384
385 /// Check that this refers to a valid subobject.
386 bool isValidSubobject() const {
387 if (Invalid)
388 return false;
389 return !isOnePastTheEnd();
390 }
391 /// Check that this refers to a valid subobject, and if not, produce a
392 /// relevant diagnostic and set the designator as invalid.
393 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK);
394
395 /// Get the type of the designated object.
396 QualType getType(ASTContext &Ctx) const {
397 assert(!Invalid && "invalid designator has no subobject type");
398 return MostDerivedPathLength == Entries.size()
399 ? MostDerivedType
400 : Ctx.getCanonicalTagType(TD: getAsBaseClass(E: Entries.back()));
401 }
402
403 /// Update this designator to refer to the first element within this array.
404 void addArrayUnchecked(const ConstantArrayType *CAT) {
405 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
406
407 // This is a most-derived object.
408 MostDerivedType = CAT->getElementType();
409 MostDerivedIsArrayElement = true;
410 MostDerivedArraySize = CAT->getZExtSize();
411 MostDerivedPathLength = Entries.size();
412 }
413 /// Update this designator to refer to the first element within the array of
414 /// elements of type T. This is an array of unknown size.
415 void addUnsizedArrayUnchecked(QualType ElemTy) {
416 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
417
418 MostDerivedType = ElemTy;
419 MostDerivedIsArrayElement = true;
420 // The value in MostDerivedArraySize is undefined in this case. So, set it
421 // to an arbitrary value that's likely to loudly break things if it's
422 // used.
423 MostDerivedArraySize = AssumedSizeForUnsizedArray;
424 MostDerivedPathLength = Entries.size();
425 }
426 /// Update this designator to refer to the given base or member of this
427 /// object.
428 void addDeclUnchecked(const Decl *D, bool Virtual = false) {
429 Entries.push_back(Elt: APValue::BaseOrMemberType(D, Virtual));
430
431 // If this isn't a base class, it's a new most-derived object.
432 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: D)) {
433 MostDerivedType = FD->getType();
434 MostDerivedIsArrayElement = false;
435 MostDerivedArraySize = 0;
436 MostDerivedPathLength = Entries.size();
437 }
438 }
439 /// Update this designator to refer to the given complex component.
440 void addComplexUnchecked(QualType EltTy, bool Imag) {
441 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Imag));
442
443 // This is technically a most-derived object, though in practice this
444 // is unlikely to matter.
445 MostDerivedType = EltTy;
446 MostDerivedIsArrayElement = true;
447 MostDerivedArraySize = 2;
448 MostDerivedPathLength = Entries.size();
449 }
450
451 void addVectorElementUnchecked(QualType EltTy, uint64_t Size,
452 uint64_t Idx) {
453 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Idx));
454 MostDerivedType = EltTy;
455 MostDerivedPathLength = Entries.size();
456 MostDerivedArraySize = 0;
457 MostDerivedIsArrayElement = false;
458 }
459
460 void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E);
461 void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E,
462 const APSInt &N);
463 /// Add N to the address of this subobject.
464 void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N, const LValue &LV);
465 };
466
467 /// A scope at the end of which an object can need to be destroyed.
468 enum class ScopeKind {
469 Block,
470 FullExpression,
471 Call
472 };
473
474 /// A reference to a particular call and its arguments.
475 struct CallRef {
476 CallRef() : OrigCallee(), CallIndex(0), Version() {}
477 CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version)
478 : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {}
479
480 explicit operator bool() const { return OrigCallee; }
481
482 /// Get the parameter that the caller initialized, corresponding to the
483 /// given parameter in the callee.
484 const ParmVarDecl *getOrigParam(const ParmVarDecl *PVD) const {
485 return OrigCallee ? OrigCallee->getParamDecl(i: PVD->getFunctionScopeIndex())
486 : PVD;
487 }
488
489 /// The callee at the point where the arguments were evaluated. This might
490 /// be different from the actual callee (a different redeclaration, or a
491 /// virtual override), but this function's parameters are the ones that
492 /// appear in the parameter map.
493 const FunctionDecl *OrigCallee;
494 /// The call index of the frame that holds the argument values.
495 unsigned CallIndex;
496 /// The version of the parameters corresponding to this call.
497 unsigned Version;
498 };
499
500 /// A stack frame in the constexpr call stack.
501 class CallStackFrame : public interp::Frame {
502 public:
503 EvalInfo &Info;
504
505 /// Parent - The caller of this stack frame.
506 CallStackFrame *Caller;
507
508 /// Callee - The function which was called.
509 const FunctionDecl *Callee;
510
511 /// This - The binding for the this pointer in this call, if any.
512 const LValue *This;
513
514 /// CallExpr - The syntactical structure of member function calls
515 const Expr *CallExpr;
516
517 /// Information on how to find the arguments to this call. Our arguments
518 /// are stored in our parent's CallStackFrame, using the ParmVarDecl* as a
519 /// key and this value as the version.
520 CallRef Arguments;
521
522 /// Source location information about the default argument or default
523 /// initializer expression we're evaluating, if any.
524 CurrentSourceLocExprScope CurSourceLocExprScope;
525
526 // Note that we intentionally use std::map here so that references to
527 // values are stable.
528 typedef std::pair<const void *, unsigned> MapKeyTy;
529 typedef std::map<MapKeyTy, APValue> MapTy;
530 /// Temporaries - Temporary lvalues materialized within this stack frame.
531 MapTy Temporaries;
532
533 /// CallRange - The source range of the call expression for this call.
534 SourceRange CallRange;
535
536 /// Index - The call index of this call.
537 unsigned Index;
538
539 /// The stack of integers for tracking version numbers for temporaries.
540 SmallVector<unsigned, 2> TempVersionStack = {1};
541 unsigned CurTempVersion = TempVersionStack.back();
542
543 unsigned getTempVersion() const { return TempVersionStack.back(); }
544
545 void pushTempVersion() {
546 TempVersionStack.push_back(Elt: ++CurTempVersion);
547 }
548
549 void popTempVersion() {
550 TempVersionStack.pop_back();
551 }
552
553 CallRef createCall(const FunctionDecl *Callee) {
554 return {Callee, Index, ++CurTempVersion};
555 }
556
557 // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact
558 // on the overall stack usage of deeply-recursing constexpr evaluations.
559 // (We should cache this map rather than recomputing it repeatedly.)
560 // But let's try this and see how it goes; we can look into caching the map
561 // as a later change.
562
563 /// LambdaCaptureFields - Mapping from captured variables/this to
564 /// corresponding data members in the closure class.
565 llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;
566 FieldDecl *LambdaThisCaptureField = nullptr;
567
568 CallStackFrame(EvalInfo &Info, SourceRange CallRange,
569 const FunctionDecl *Callee, const LValue *This,
570 const Expr *CallExpr, CallRef Arguments);
571 ~CallStackFrame();
572
573 // Return the temporary for Key whose version number is Version.
574 APValue *getTemporary(const void *Key, unsigned Version) {
575 MapKeyTy KV(Key, Version);
576 auto LB = Temporaries.lower_bound(x: KV);
577 if (LB != Temporaries.end() && LB->first == KV)
578 return &LB->second;
579 return nullptr;
580 }
581
582 // Return the current temporary for Key in the map.
583 APValue *getCurrentTemporary(const void *Key) {
584 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
585 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
586 return &std::prev(x: UB)->second;
587 return nullptr;
588 }
589
590 // Return the version number of the current temporary for Key.
591 unsigned getCurrentTemporaryVersion(const void *Key) const {
592 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
593 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
594 return std::prev(x: UB)->first.second;
595 return 0;
596 }
597
598 /// Allocate storage for an object of type T in this stack frame.
599 /// Populates LV with a handle to the created object. Key identifies
600 /// the temporary within the stack frame, and must not be reused without
601 /// bumping the temporary version number.
602 template<typename KeyT>
603 APValue &createTemporary(const KeyT *Key, QualType T,
604 ScopeKind Scope, LValue &LV);
605
606 /// Allocate storage for a parameter of a function call made in this frame.
607 APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV);
608
609 void describe(llvm::raw_ostream &OS) const override;
610
611 Frame *getCaller() const override { return Caller; }
612 SourceRange getCallRange() const override { return CallRange; }
613 const FunctionDecl *getCallee() const override { return Callee; }
614
615 bool isStdFunction() const {
616 for (const DeclContext *DC = Callee; DC; DC = DC->getParent())
617 if (DC->isStdNamespace())
618 return true;
619 return false;
620 }
621
622 /// Whether we're in a context where [[msvc::constexpr]] evaluation is
623 /// permitted. See MSConstexprDocs for description of permitted contexts.
624 bool CanEvalMSConstexpr = false;
625
626 private:
627 APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T,
628 ScopeKind Scope);
629 };
630
631 /// Temporarily override 'this'.
632 class ThisOverrideRAII {
633 public:
634 ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable)
635 : Frame(Frame), OldThis(Frame.This) {
636 if (Enable)
637 Frame.This = NewThis;
638 }
639 ~ThisOverrideRAII() {
640 Frame.This = OldThis;
641 }
642 private:
643 CallStackFrame &Frame;
644 const LValue *OldThis;
645 };
646
647 // A shorthand time trace scope struct, prints source range, for example
648 // {"name":"EvaluateAsRValue","args":{"detail":"<test.cc:8:21, col:25>"}}}
649 class ExprTimeTraceScope {
650 public:
651 ExprTimeTraceScope(const Expr *E, const ASTContext &Ctx, StringRef Name)
652 : TimeScope(Name, [E, &Ctx] {
653 return E->getSourceRange().printToString(SM: Ctx.getSourceManager());
654 }) {}
655
656 private:
657 llvm::TimeTraceScope TimeScope;
658 };
659
660 /// RAII object used to change the current ability of
661 /// [[msvc::constexpr]] evaulation.
662 struct MSConstexprContextRAII {
663 CallStackFrame &Frame;
664 bool OldValue;
665 explicit MSConstexprContextRAII(CallStackFrame &Frame, bool Value)
666 : Frame(Frame), OldValue(Frame.CanEvalMSConstexpr) {
667 Frame.CanEvalMSConstexpr = Value;
668 }
669
670 ~MSConstexprContextRAII() { Frame.CanEvalMSConstexpr = OldValue; }
671 };
672}
673
674static bool HandleDestruction(EvalInfo &Info, const Expr *E,
675 const LValue &This, QualType ThisType);
676static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
677 APValue::LValueBase LVBase, APValue &Value,
678 QualType T);
679
680namespace {
681 /// A cleanup, and a flag indicating whether it is lifetime-extended.
682 class Cleanup {
683 llvm::PointerIntPair<APValue*, 2, ScopeKind> Value;
684 APValue::LValueBase Base;
685 QualType T;
686
687 public:
688 Cleanup(APValue *Val, APValue::LValueBase Base, QualType T,
689 ScopeKind Scope)
690 : Value(Val, Scope), Base(Base), T(T) {}
691
692 /// Determine whether this cleanup should be performed at the end of the
693 /// given kind of scope.
694 bool isDestroyedAtEndOf(ScopeKind K) const {
695 return (int)Value.getInt() >= (int)K;
696 }
697 bool endLifetime(EvalInfo &Info, bool RunDestructors) {
698 if (RunDestructors) {
699 SourceLocation Loc;
700 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
701 Loc = VD->getLocation();
702 else if (const Expr *E = Base.dyn_cast<const Expr*>())
703 Loc = E->getExprLoc();
704 return HandleDestruction(Info, Loc, LVBase: Base, Value&: *Value.getPointer(), T);
705 }
706 *Value.getPointer() = APValue();
707 return true;
708 }
709
710 bool hasSideEffect() {
711 return T.isDestructedType();
712 }
713 };
714
715 /// A reference to an object whose construction we are currently evaluating.
716 struct ObjectUnderConstruction {
717 APValue::LValueBase Base;
718 ArrayRef<APValue::LValuePathEntry> Path;
719 friend bool operator==(const ObjectUnderConstruction &LHS,
720 const ObjectUnderConstruction &RHS) {
721 return LHS.Base == RHS.Base && LHS.Path == RHS.Path;
722 }
723 friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) {
724 return llvm::hash_combine(args: Obj.Base, args: Obj.Path);
725 }
726 };
727 enum class ConstructionPhase {
728 None,
729 Bases,
730 AfterBases,
731 AfterFields,
732 Destroying,
733 DestroyingBases
734 };
735}
736
737namespace llvm {
738template<> struct DenseMapInfo<ObjectUnderConstruction> {
739 using Base = DenseMapInfo<APValue::LValueBase>;
740 static unsigned getHashValue(const ObjectUnderConstruction &Object) {
741 return hash_value(Obj: Object);
742 }
743 static bool isEqual(const ObjectUnderConstruction &LHS,
744 const ObjectUnderConstruction &RHS) {
745 return LHS == RHS;
746 }
747};
748}
749
750namespace {
751 /// A dynamically-allocated heap object.
752 struct DynAlloc {
753 /// The value of this heap-allocated object.
754 APValue Value;
755 /// The allocating expression; used for diagnostics. Either a CXXNewExpr
756 /// or a CallExpr (the latter is for direct calls to operator new inside
757 /// std::allocator<T>::allocate).
758 const Expr *AllocExpr = nullptr;
759
760 enum Kind {
761 New,
762 ArrayNew,
763 StdAllocator
764 };
765
766 /// Get the kind of the allocation. This must match between allocation
767 /// and deallocation.
768 Kind getKind() const {
769 if (auto *NE = dyn_cast<CXXNewExpr>(Val: AllocExpr))
770 return NE->isArray() ? ArrayNew : New;
771 assert(isa<CallExpr>(AllocExpr));
772 return StdAllocator;
773 }
774 };
775
776 struct DynAllocOrder {
777 bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const {
778 return L.getIndex() < R.getIndex();
779 }
780 };
781
782 /// EvalInfo - This is a private struct used by the evaluator to capture
783 /// information about a subexpression as it is folded. It retains information
784 /// about the AST context, but also maintains information about the folded
785 /// expression.
786 ///
787 /// If an expression could be evaluated, it is still possible it is not a C
788 /// "integer constant expression" or constant expression. If not, this struct
789 /// captures information about how and why not.
790 ///
791 /// One bit of information passed *into* the request for constant folding
792 /// indicates whether the subexpression is "evaluated" or not according to C
793 /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
794 /// evaluate the expression regardless of what the RHS is, but C only allows
795 /// certain things in certain situations.
796 class EvalInfo final : public interp::State {
797 public:
798 /// CurrentCall - The top of the constexpr call stack.
799 CallStackFrame *CurrentCall;
800
801 /// CallStackDepth - The number of calls in the call stack right now.
802 unsigned CallStackDepth;
803
804 /// NextCallIndex - The next call index to assign.
805 unsigned NextCallIndex;
806
807 /// StepsLeft - The remaining number of evaluation steps we're permitted
808 /// to perform. This is essentially a limit for the number of statements
809 /// we will evaluate.
810 unsigned StepsLeft;
811
812 /// Enable the experimental new constant interpreter. If an expression is
813 /// not supported by the interpreter, an error is triggered.
814 bool EnableNewConstInterp;
815
816 /// BottomFrame - The frame in which evaluation started. This must be
817 /// initialized after CurrentCall and CallStackDepth.
818 CallStackFrame BottomFrame;
819
820 /// A stack of values whose lifetimes end at the end of some surrounding
821 /// evaluation frame.
822 llvm::SmallVector<Cleanup, 16> CleanupStack;
823
824 /// EvaluatingDecl - This is the declaration whose initializer is being
825 /// evaluated, if any.
826 APValue::LValueBase EvaluatingDecl;
827
828 enum class EvaluatingDeclKind {
829 None,
830 /// We're evaluating the construction of EvaluatingDecl.
831 Ctor,
832 /// We're evaluating the destruction of EvaluatingDecl.
833 Dtor,
834 };
835 EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None;
836
837 /// EvaluatingDeclValue - This is the value being constructed for the
838 /// declaration whose initializer is being evaluated, if any.
839 APValue *EvaluatingDeclValue;
840
841 /// Stack of loops and 'switch' statements which we're currently
842 /// breaking/continuing; null entries are used to mark unlabeled
843 /// break/continue.
844 SmallVector<const Stmt *> BreakContinueStack;
845
846 /// Set of objects that are currently being constructed.
847 llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase>
848 ObjectsUnderConstruction;
849
850 /// Current heap allocations, along with the location where each was
851 /// allocated. We use std::map here because we need stable addresses
852 /// for the stored APValues.
853 std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs;
854
855 /// The number of heap allocations performed so far in this evaluation.
856 unsigned NumHeapAllocs = 0;
857
858 struct EvaluatingConstructorRAII {
859 EvalInfo &EI;
860 ObjectUnderConstruction Object;
861 bool DidInsert;
862 EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object,
863 bool HasBases)
864 : EI(EI), Object(Object) {
865 DidInsert =
866 EI.ObjectsUnderConstruction
867 .insert(KV: {Object, HasBases ? ConstructionPhase::Bases
868 : ConstructionPhase::AfterBases})
869 .second;
870 }
871 void finishedConstructingBases() {
872 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases;
873 }
874 void finishedConstructingFields() {
875 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterFields;
876 }
877 ~EvaluatingConstructorRAII() {
878 if (DidInsert) EI.ObjectsUnderConstruction.erase(Val: Object);
879 }
880 };
881
882 struct EvaluatingDestructorRAII {
883 EvalInfo &EI;
884 ObjectUnderConstruction Object;
885 bool DidInsert;
886 EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object)
887 : EI(EI), Object(Object) {
888 DidInsert = EI.ObjectsUnderConstruction
889 .insert(KV: {Object, ConstructionPhase::Destroying})
890 .second;
891 }
892 void startedDestroyingBases() {
893 EI.ObjectsUnderConstruction[Object] =
894 ConstructionPhase::DestroyingBases;
895 }
896 ~EvaluatingDestructorRAII() {
897 if (DidInsert)
898 EI.ObjectsUnderConstruction.erase(Val: Object);
899 }
900 };
901
902 ConstructionPhase
903 isEvaluatingCtorDtor(APValue::LValueBase Base,
904 ArrayRef<APValue::LValuePathEntry> Path) {
905 return ObjectsUnderConstruction.lookup(Val: {.Base: Base, .Path: Path});
906 }
907
908 /// If we're currently speculatively evaluating, the outermost call stack
909 /// depth at which we can mutate state, otherwise 0.
910 unsigned SpeculativeEvaluationDepth = 0;
911
912 /// The current array initialization index, if we're performing array
913 /// initialization.
914 uint64_t ArrayInitIndex = -1;
915
916 EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode)
917 : State(const_cast<ASTContext &>(C), S), CurrentCall(nullptr),
918 CallStackDepth(0), NextCallIndex(1),
919 StepsLeft(C.getLangOpts().ConstexprStepLimit),
920 EnableNewConstInterp(C.getLangOpts().EnableNewConstInterp),
921 BottomFrame(*this, SourceLocation(), /*Callee=*/nullptr,
922 /*This=*/nullptr,
923 /*CallExpr=*/nullptr, CallRef()),
924 EvaluatingDecl((const ValueDecl *)nullptr),
925 EvaluatingDeclValue(nullptr) {
926 EvalMode = Mode;
927 }
928
929 ~EvalInfo() {
930 discardCleanups();
931 }
932
933 void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value,
934 EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) {
935 EvaluatingDecl = Base;
936 IsEvaluatingDecl = EDK;
937 EvaluatingDeclValue = &Value;
938 }
939
940 bool CheckCallLimit(SourceLocation Loc) {
941 // Don't perform any constexpr calls (other than the call we're checking)
942 // when checking a potential constant expression.
943 if (checkingPotentialConstantExpression() && CallStackDepth > 1)
944 return false;
945 if (NextCallIndex == 0) {
946 // NextCallIndex has wrapped around.
947 FFDiag(Loc, DiagId: diag::note_constexpr_call_limit_exceeded);
948 return false;
949 }
950 if (CallStackDepth <= getLangOpts().ConstexprCallDepth)
951 return true;
952 FFDiag(Loc, DiagId: diag::note_constexpr_depth_limit_exceeded)
953 << getLangOpts().ConstexprCallDepth;
954 return false;
955 }
956
957 bool CheckArraySize(SourceLocation Loc, unsigned BitWidth,
958 uint64_t ElemCount, bool Diag) {
959 // FIXME: GH63562
960 // APValue stores array extents as unsigned,
961 // so anything that is greater that unsigned would overflow when
962 // constructing the array, we catch this here.
963 if (BitWidth > ConstantArrayType::getMaxSizeBits(Context: Ctx) ||
964 ElemCount > uint64_t(std::numeric_limits<unsigned>::max())) {
965 if (Diag)
966 FFDiag(Loc, DiagId: diag::note_constexpr_new_too_large) << ElemCount;
967 return false;
968 }
969
970 // FIXME: GH63562
971 // Arrays allocate an APValue per element.
972 // We use the number of constexpr steps as a proxy for the maximum size
973 // of arrays to avoid exhausting the system resources, as initialization
974 // of each element is likely to take some number of steps anyway.
975 uint64_t Limit = getLangOpts().ConstexprStepLimit;
976 if (Limit != 0 && ElemCount > Limit) {
977 if (Diag)
978 FFDiag(Loc, DiagId: diag::note_constexpr_new_exceeds_limits)
979 << ElemCount << Limit;
980 return false;
981 }
982 return true;
983 }
984
985 std::pair<CallStackFrame *, unsigned>
986 getCallFrameAndDepth(unsigned CallIndex) {
987 assert(CallIndex && "no call index in getCallFrameAndDepth");
988 // We will eventually hit BottomFrame, which has Index 1, so Frame can't
989 // be null in this loop.
990 unsigned Depth = CallStackDepth;
991 CallStackFrame *Frame = CurrentCall;
992 while (Frame->Index > CallIndex) {
993 Frame = Frame->Caller;
994 --Depth;
995 }
996 if (Frame->Index == CallIndex)
997 return {Frame, Depth};
998 return {nullptr, 0};
999 }
1000
1001 bool nextStep(const Stmt *S) {
1002 if (getLangOpts().ConstexprStepLimit == 0)
1003 return true;
1004
1005 if (!StepsLeft) {
1006 FFDiag(Loc: S->getBeginLoc(), DiagId: diag::note_constexpr_step_limit_exceeded);
1007 return false;
1008 }
1009 --StepsLeft;
1010 return true;
1011 }
1012
1013 APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV);
1014
1015 std::optional<DynAlloc *> lookupDynamicAlloc(DynamicAllocLValue DA) {
1016 std::optional<DynAlloc *> Result;
1017 auto It = HeapAllocs.find(x: DA);
1018 if (It != HeapAllocs.end())
1019 Result = &It->second;
1020 return Result;
1021 }
1022
1023 /// Get the allocated storage for the given parameter of the given call.
1024 APValue *getParamSlot(CallRef Call, const ParmVarDecl *PVD) {
1025 CallStackFrame *Frame = getCallFrameAndDepth(CallIndex: Call.CallIndex).first;
1026 return Frame ? Frame->getTemporary(Key: Call.getOrigParam(PVD), Version: Call.Version)
1027 : nullptr;
1028 }
1029
1030 /// Information about a stack frame for std::allocator<T>::[de]allocate.
1031 struct StdAllocatorCaller {
1032 unsigned FrameIndex;
1033 QualType ElemType;
1034 const Expr *Call;
1035 explicit operator bool() const { return FrameIndex != 0; };
1036 };
1037
1038 StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const {
1039 for (const CallStackFrame *Call = CurrentCall; Call->Caller != nullptr;
1040 Call = Call->Caller) {
1041 const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Val: Call->Callee);
1042 if (!MD)
1043 continue;
1044 const IdentifierInfo *FnII = MD->getIdentifier();
1045 if (!FnII || !FnII->isStr(Str: FnName))
1046 continue;
1047
1048 const auto *CTSD =
1049 dyn_cast<ClassTemplateSpecializationDecl>(Val: MD->getParent());
1050 if (!CTSD)
1051 continue;
1052
1053 const IdentifierInfo *ClassII = CTSD->getIdentifier();
1054 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
1055 if (CTSD->isInStdNamespace() && ClassII &&
1056 ClassII->isStr(Str: "allocator") && TAL.size() >= 1 &&
1057 TAL[0].getKind() == TemplateArgument::Type)
1058 return {.FrameIndex: Call->Index, .ElemType: TAL[0].getAsType(), .Call: Call->CallExpr};
1059 }
1060
1061 return {};
1062 }
1063
1064 void performLifetimeExtension() {
1065 // Disable the cleanups for lifetime-extended temporaries.
1066 llvm::erase_if(C&: CleanupStack, P: [](Cleanup &C) {
1067 return !C.isDestroyedAtEndOf(K: ScopeKind::FullExpression);
1068 });
1069 }
1070
1071 /// Throw away any remaining cleanups at the end of evaluation. If any
1072 /// cleanups would have had a side-effect, note that as an unmodeled
1073 /// side-effect and return false. Otherwise, return true.
1074 bool discardCleanups() {
1075 for (Cleanup &C : CleanupStack) {
1076 if (C.hasSideEffect() && !noteSideEffect()) {
1077 CleanupStack.clear();
1078 return false;
1079 }
1080 }
1081 CleanupStack.clear();
1082 return true;
1083 }
1084
1085 private:
1086 const interp::Frame *getCurrentFrame() override { return CurrentCall; }
1087
1088 unsigned getCallStackDepth() override { return CallStackDepth; }
1089 bool stepsLeft() const override { return StepsLeft > 0; }
1090
1091 public:
1092 /// Notes that we failed to evaluate an expression that other expressions
1093 /// directly depend on, and determine if we should keep evaluating. This
1094 /// should only be called if we actually intend to keep evaluating.
1095 ///
1096 /// Call noteSideEffect() instead if we may be able to ignore the value that
1097 /// we failed to evaluate, e.g. if we failed to evaluate Foo() in:
1098 ///
1099 /// (Foo(), 1) // use noteSideEffect
1100 /// (Foo() || true) // use noteSideEffect
1101 /// Foo() + 1 // use noteFailure
1102 [[nodiscard]] bool noteFailure() {
1103 // Failure when evaluating some expression often means there is some
1104 // subexpression whose evaluation was skipped. Therefore, (because we
1105 // don't track whether we skipped an expression when unwinding after an
1106 // evaluation failure) every evaluation failure that bubbles up from a
1107 // subexpression implies that a side-effect has potentially happened. We
1108 // skip setting the HasSideEffects flag to true until we decide to
1109 // continue evaluating after that point, which happens here.
1110 bool KeepGoing = keepEvaluatingAfterFailure();
1111 EvalStatus.HasSideEffects |= KeepGoing;
1112 return KeepGoing;
1113 }
1114
1115 class ArrayInitLoopIndex {
1116 EvalInfo &Info;
1117 uint64_t OuterIndex;
1118
1119 public:
1120 ArrayInitLoopIndex(EvalInfo &Info)
1121 : Info(Info), OuterIndex(Info.ArrayInitIndex) {
1122 Info.ArrayInitIndex = 0;
1123 }
1124 ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; }
1125
1126 operator uint64_t&() { return Info.ArrayInitIndex; }
1127 };
1128 };
1129
1130 /// Object used to treat all foldable expressions as constant expressions.
1131 struct FoldConstant {
1132 EvalInfo &Info;
1133 bool Enabled;
1134 bool HadNoPriorDiags;
1135 EvaluationMode OldMode;
1136
1137 explicit FoldConstant(EvalInfo &Info, bool Enabled)
1138 : Info(Info),
1139 Enabled(Enabled),
1140 HadNoPriorDiags(Info.EvalStatus.Diag &&
1141 Info.EvalStatus.Diag->empty() &&
1142 !Info.EvalStatus.HasSideEffects),
1143 OldMode(Info.EvalMode) {
1144 if (Enabled)
1145 Info.EvalMode = EvaluationMode::ConstantFold;
1146 }
1147 void keepDiagnostics() { Enabled = false; }
1148 ~FoldConstant() {
1149 if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() &&
1150 !Info.EvalStatus.HasSideEffects) {
1151 Info.EvalStatus.Diag->clear();
1152 Info.EvalStatus.DiagEmitted = false;
1153 }
1154 Info.EvalMode = OldMode;
1155 }
1156 };
1157
1158 /// RAII object used to set the current evaluation mode to ignore
1159 /// side-effects.
1160 struct IgnoreSideEffectsRAII {
1161 EvalInfo &Info;
1162 EvaluationMode OldMode;
1163 explicit IgnoreSideEffectsRAII(EvalInfo &Info)
1164 : Info(Info), OldMode(Info.EvalMode) {
1165 Info.EvalMode = EvaluationMode::IgnoreSideEffects;
1166 }
1167
1168 ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; }
1169 };
1170
1171 /// RAII object used to optionally suppress diagnostics and side-effects from
1172 /// a speculative evaluation.
1173 class SpeculativeEvaluationRAII {
1174 EvalInfo *Info = nullptr;
1175 Expr::EvalStatus OldStatus;
1176 unsigned OldSpeculativeEvaluationDepth = 0;
1177
1178 void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) {
1179 Info = Other.Info;
1180 OldStatus = Other.OldStatus;
1181 OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth;
1182 Other.Info = nullptr;
1183 }
1184
1185 void maybeRestoreState() {
1186 if (!Info)
1187 return;
1188
1189 Info->EvalStatus = OldStatus;
1190 Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth;
1191 }
1192
1193 public:
1194 SpeculativeEvaluationRAII() = default;
1195
1196 SpeculativeEvaluationRAII(
1197 EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr)
1198 : Info(&Info), OldStatus(Info.EvalStatus),
1199 OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) {
1200 Info.EvalStatus.Diag = NewDiag;
1201 Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1;
1202 }
1203
1204 SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete;
1205 SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) {
1206 moveFromAndCancel(Other: std::move(Other));
1207 }
1208
1209 SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) {
1210 maybeRestoreState();
1211 moveFromAndCancel(Other: std::move(Other));
1212 return *this;
1213 }
1214
1215 ~SpeculativeEvaluationRAII() { maybeRestoreState(); }
1216 };
1217
1218 /// RAII object wrapping a full-expression or block scope, and handling
1219 /// the ending of the lifetime of temporaries created within it.
1220 template<ScopeKind Kind>
1221 class ScopeRAII {
1222 EvalInfo &Info;
1223 unsigned OldStackSize;
1224 public:
1225 ScopeRAII(EvalInfo &Info)
1226 : Info(Info), OldStackSize(Info.CleanupStack.size()) {
1227 // Push a new temporary version. This is needed to distinguish between
1228 // temporaries created in different iterations of a loop.
1229 Info.CurrentCall->pushTempVersion();
1230 }
1231 bool destroy(bool RunDestructors = true) {
1232 bool OK = cleanup(Info, RunDestructors, OldStackSize);
1233 OldStackSize = std::numeric_limits<unsigned>::max();
1234 return OK;
1235 }
1236 ~ScopeRAII() {
1237 if (OldStackSize != std::numeric_limits<unsigned>::max())
1238 destroy(RunDestructors: false);
1239 // Body moved to a static method to encourage the compiler to inline away
1240 // instances of this class.
1241 Info.CurrentCall->popTempVersion();
1242 }
1243 private:
1244 static bool cleanup(EvalInfo &Info, bool RunDestructors,
1245 unsigned OldStackSize) {
1246 assert(OldStackSize <= Info.CleanupStack.size() &&
1247 "running cleanups out of order?");
1248
1249 // Run all cleanups for a block scope, and non-lifetime-extended cleanups
1250 // for a full-expression scope.
1251 bool Success = true;
1252 for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) {
1253 if (Info.CleanupStack[I - 1].isDestroyedAtEndOf(K: Kind)) {
1254 if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) {
1255 Success = false;
1256 break;
1257 }
1258 }
1259 }
1260
1261 // Compact any retained cleanups.
1262 auto NewEnd = Info.CleanupStack.begin() + OldStackSize;
1263 if (Kind != ScopeKind::Block)
1264 NewEnd =
1265 std::remove_if(NewEnd, Info.CleanupStack.end(), [](Cleanup &C) {
1266 return C.isDestroyedAtEndOf(K: Kind);
1267 });
1268 Info.CleanupStack.erase(CS: NewEnd, CE: Info.CleanupStack.end());
1269 return Success;
1270 }
1271 };
1272 typedef ScopeRAII<ScopeKind::Block> BlockScopeRAII;
1273 typedef ScopeRAII<ScopeKind::FullExpression> FullExpressionRAII;
1274 typedef ScopeRAII<ScopeKind::Call> CallScopeRAII;
1275}
1276
1277bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,
1278 CheckSubobjectKind CSK) {
1279 if (Invalid)
1280 return false;
1281 if (isOnePastTheEnd()) {
1282 Info.CCEDiag(E, DiagId: diag::note_constexpr_past_end_subobject)
1283 << CSK;
1284 setInvalid();
1285 return false;
1286 }
1287 // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there
1288 // must actually be at least one array element; even a VLA cannot have a
1289 // bound of zero. And if our index is nonzero, we already had a CCEDiag.
1290 return true;
1291}
1292
1293void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info,
1294 const Expr *E) {
1295 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsized_array_indexed);
1296 // Do not set the designator as invalid: we can represent this situation,
1297 // and correct handling of __builtin_object_size requires us to do so.
1298}
1299
1300void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
1301 const Expr *E,
1302 const APSInt &N) {
1303 // If we're complaining, we must be able to statically determine the size of
1304 // the most derived array.
1305 if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement)
1306 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1307 << N << /*array*/ 0
1308 << static_cast<unsigned>(getMostDerivedArraySize());
1309 else
1310 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1311 << N << /*non-array*/ 1;
1312 setInvalid();
1313}
1314
1315CallStackFrame::CallStackFrame(EvalInfo &Info, SourceRange CallRange,
1316 const FunctionDecl *Callee, const LValue *This,
1317 const Expr *CallExpr, CallRef Call)
1318 : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This),
1319 CallExpr(CallExpr), Arguments(Call), CallRange(CallRange),
1320 Index(Info.NextCallIndex++) {
1321 Info.CurrentCall = this;
1322 ++Info.CallStackDepth;
1323}
1324
1325CallStackFrame::~CallStackFrame() {
1326 assert(Info.CurrentCall == this && "calls retired out of order");
1327 --Info.CallStackDepth;
1328 Info.CurrentCall = Caller;
1329}
1330
1331static bool isRead(AccessKinds AK) {
1332 return AK == AK_Read || AK == AK_ReadObjectRepresentation ||
1333 AK == AK_IsWithinLifetime || AK == AK_Dereference;
1334}
1335
1336static bool isModification(AccessKinds AK) {
1337 switch (AK) {
1338 case AK_Read:
1339 case AK_ReadObjectRepresentation:
1340 case AK_MemberCall:
1341 case AK_DynamicCast:
1342 case AK_TypeId:
1343 case AK_IsWithinLifetime:
1344 case AK_Dereference:
1345 return false;
1346 case AK_Assign:
1347 case AK_Increment:
1348 case AK_Decrement:
1349 case AK_Construct:
1350 case AK_Destroy:
1351 return true;
1352 }
1353 llvm_unreachable("unknown access kind");
1354}
1355
1356static bool isAnyAccess(AccessKinds AK) {
1357 return isRead(AK) || isModification(AK);
1358}
1359
1360/// Is this an access per the C++ definition?
1361static bool isFormalAccess(AccessKinds AK) {
1362 return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy &&
1363 AK != AK_IsWithinLifetime && AK != AK_Dereference;
1364}
1365
1366/// Is this kind of access valid on an indeterminate object value?
1367static bool isValidIndeterminateAccess(AccessKinds AK) {
1368 switch (AK) {
1369 case AK_Read:
1370 case AK_Increment:
1371 case AK_Decrement:
1372 case AK_Dereference:
1373 // These need the object's value.
1374 return false;
1375
1376 case AK_IsWithinLifetime:
1377 case AK_ReadObjectRepresentation:
1378 case AK_Assign:
1379 case AK_Construct:
1380 case AK_Destroy:
1381 // Construction and destruction don't need the value.
1382 return true;
1383
1384 case AK_MemberCall:
1385 case AK_DynamicCast:
1386 case AK_TypeId:
1387 // These aren't really meaningful on scalars.
1388 return true;
1389 }
1390 llvm_unreachable("unknown access kind");
1391}
1392
1393namespace {
1394 struct ComplexValue {
1395 private:
1396 bool IsInt;
1397
1398 public:
1399 APSInt IntReal, IntImag;
1400 APFloat FloatReal, FloatImag;
1401
1402 ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {}
1403
1404 void makeComplexFloat() { IsInt = false; }
1405 bool isComplexFloat() const { return !IsInt; }
1406 APFloat &getComplexFloatReal() { return FloatReal; }
1407 APFloat &getComplexFloatImag() { return FloatImag; }
1408
1409 void makeComplexInt() { IsInt = true; }
1410 bool isComplexInt() const { return IsInt; }
1411 APSInt &getComplexIntReal() { return IntReal; }
1412 APSInt &getComplexIntImag() { return IntImag; }
1413
1414 void moveInto(APValue &v) const {
1415 if (isComplexFloat())
1416 v = APValue(FloatReal, FloatImag);
1417 else
1418 v = APValue(IntReal, IntImag);
1419 }
1420 void setFrom(const APValue &v) {
1421 assert(v.isComplexFloat() || v.isComplexInt());
1422 if (v.isComplexFloat()) {
1423 makeComplexFloat();
1424 FloatReal = v.getComplexFloatReal();
1425 FloatImag = v.getComplexFloatImag();
1426 } else {
1427 makeComplexInt();
1428 IntReal = v.getComplexIntReal();
1429 IntImag = v.getComplexIntImag();
1430 }
1431 }
1432 };
1433
1434 struct LValue {
1435 APValue::LValueBase Base;
1436 CharUnits Offset;
1437 SubobjectDesignator Designator;
1438 bool IsNullPtr : 1;
1439 bool InvalidBase : 1;
1440 // P2280R4 track if we have an unknown reference or pointer.
1441 bool AllowConstexprUnknown = false;
1442
1443 const APValue::LValueBase getLValueBase() const { return Base; }
1444 bool allowConstexprUnknown() const { return AllowConstexprUnknown; }
1445 CharUnits &getLValueOffset() { return Offset; }
1446 const CharUnits &getLValueOffset() const { return Offset; }
1447 SubobjectDesignator &getLValueDesignator() { return Designator; }
1448 const SubobjectDesignator &getLValueDesignator() const { return Designator;}
1449 bool isNullPointer() const { return IsNullPtr;}
1450
1451 unsigned getLValueCallIndex() const { return Base.getCallIndex(); }
1452 unsigned getLValueVersion() const { return Base.getVersion(); }
1453
1454 bool pointsToCompleteClass(const CXXRecordDecl *D) const {
1455 if (Designator.Entries.empty())
1456 return true;
1457
1458 return Designator.MostDerivedType->getAsCXXRecordDecl() == D;
1459 }
1460
1461 void moveInto(APValue &V) const {
1462 if (Designator.Invalid)
1463 V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr);
1464 else {
1465 assert(!InvalidBase && "APValues can't handle invalid LValue bases");
1466 V = APValue(Base, Offset, Designator.Entries,
1467 Designator.IsOnePastTheEnd, IsNullPtr);
1468 }
1469 if (AllowConstexprUnknown)
1470 V.setConstexprUnknown();
1471 }
1472 void setFrom(const ASTContext &Ctx, const APValue &V) {
1473 assert(V.isLValue() && "Setting LValue from a non-LValue?");
1474 Base = V.getLValueBase();
1475 Offset = V.getLValueOffset();
1476 InvalidBase = false;
1477 Designator = SubobjectDesignator(Ctx, V);
1478 IsNullPtr = V.isNullPointer();
1479 AllowConstexprUnknown = V.allowConstexprUnknown();
1480 }
1481
1482 void set(APValue::LValueBase B, bool BInvalid = false) {
1483#ifndef NDEBUG
1484 // We only allow a few types of invalid bases. Enforce that here.
1485 if (BInvalid) {
1486 const auto *E = B.get<const Expr *>();
1487 assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) &&
1488 "Unexpected type of invalid base");
1489 }
1490#endif
1491
1492 Base = B;
1493 Offset = CharUnits::fromQuantity(Quantity: 0);
1494 InvalidBase = BInvalid;
1495 Designator = SubobjectDesignator(getType(B));
1496 IsNullPtr = false;
1497 AllowConstexprUnknown = false;
1498 }
1499
1500 void setNull(ASTContext &Ctx, QualType PointerTy) {
1501 Base = (const ValueDecl *)nullptr;
1502 Offset =
1503 CharUnits::fromQuantity(Quantity: Ctx.getTargetNullPointerValue(QT: PointerTy));
1504 InvalidBase = false;
1505 Designator = SubobjectDesignator(PointerTy->getPointeeType());
1506 IsNullPtr = true;
1507 AllowConstexprUnknown = false;
1508 }
1509
1510 void setInvalid(APValue::LValueBase B, unsigned I = 0) {
1511 set(B, BInvalid: true);
1512 }
1513
1514 std::string toString(ASTContext &Ctx, QualType T) const {
1515 APValue Printable;
1516 moveInto(V&: Printable);
1517 return Printable.getAsString(Ctx, Ty: T);
1518 }
1519
1520 private:
1521 // Check that this LValue is not based on a null pointer. If it is, produce
1522 // a diagnostic and mark the designator as invalid.
1523 template <typename GenDiagType>
1524 bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) {
1525 if (Designator.Invalid)
1526 return false;
1527 if (IsNullPtr) {
1528 GenDiag();
1529 Designator.setInvalid();
1530 return false;
1531 }
1532 return true;
1533 }
1534
1535 public:
1536 bool checkNullPointer(EvalInfo &Info, const Expr *E,
1537 CheckSubobjectKind CSK) {
1538 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, CSK] {
1539 Info.CCEDiag(E, DiagId: diag::note_constexpr_null_subobject) << CSK;
1540 });
1541 }
1542
1543 bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E,
1544 AccessKinds AK) {
1545 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, AK] {
1546 if (AK == AccessKinds::AK_Dereference)
1547 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
1548 else
1549 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
1550 });
1551 }
1552
1553 // Check this LValue refers to an object. If not, set the designator to be
1554 // invalid and emit a diagnostic.
1555 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {
1556 return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) &&
1557 Designator.checkSubobject(Info, E, CSK);
1558 }
1559
1560 void addDecl(EvalInfo &Info, const Expr *E,
1561 const Decl *D, bool Virtual = false) {
1562 if (checkSubobject(Info, E, CSK: isa<FieldDecl>(Val: D) ? CSK_Field : CSK_Base))
1563 Designator.addDeclUnchecked(D, Virtual);
1564 }
1565 void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) {
1566 if (!Designator.Entries.empty()) {
1567 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsupported_unsized_array);
1568 Designator.setInvalid();
1569 return;
1570 }
1571 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer)) {
1572 assert(!Base || getType(Base).getNonReferenceType()->isPointerType() ||
1573 getType(Base).getNonReferenceType()->isArrayType());
1574 Designator.FirstEntryIsAnUnsizedArray = true;
1575 Designator.addUnsizedArrayUnchecked(ElemTy);
1576 }
1577 }
1578 void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {
1579 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer))
1580 Designator.addArrayUnchecked(CAT);
1581 }
1582 void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) {
1583 if (checkSubobject(Info, E, CSK: Imag ? CSK_Imag : CSK_Real))
1584 Designator.addComplexUnchecked(EltTy, Imag);
1585 }
1586 void addVectorElement(EvalInfo &Info, const Expr *E, QualType EltTy,
1587 uint64_t Size, uint64_t Idx) {
1588 if (checkSubobject(Info, E, CSK: CSK_VectorElement))
1589 Designator.addVectorElementUnchecked(EltTy, Size, Idx);
1590 }
1591 void clearIsNullPointer() {
1592 IsNullPtr = false;
1593 }
1594 void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E,
1595 const APSInt &Index, CharUnits ElementSize) {
1596 // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB,
1597 // but we're not required to diagnose it and it's valid in C++.)
1598 if (!Index)
1599 return;
1600
1601 // Compute the new offset in the appropriate width, wrapping at 64 bits.
1602 // FIXME: When compiling for a 32-bit target, we should use 32-bit
1603 // offsets.
1604 uint64_t Offset64 = Offset.getQuantity();
1605 uint64_t ElemSize64 = ElementSize.getQuantity();
1606 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
1607 Offset = CharUnits::fromQuantity(Quantity: Offset64 + ElemSize64 * Index64);
1608
1609 if (checkNullPointer(Info, E, CSK: CSK_ArrayIndex))
1610 Designator.adjustIndex(Info, E, N: Index, LV: *this);
1611 clearIsNullPointer();
1612 }
1613 void adjustOffset(CharUnits N) {
1614 Offset += N;
1615 if (N.getQuantity())
1616 clearIsNullPointer();
1617 }
1618 };
1619
1620 struct MemberPtr {
1621 MemberPtr() {}
1622 explicit MemberPtr(const ValueDecl *Decl)
1623 : DeclAndIsDerivedMember(Decl, false) {}
1624
1625 /// The member or (direct or indirect) field referred to by this member
1626 /// pointer, or 0 if this is a null member pointer.
1627 const ValueDecl *getDecl() const {
1628 return DeclAndIsDerivedMember.getPointer();
1629 }
1630 /// Is this actually a member of some type derived from the relevant class?
1631 bool isDerivedMember() const {
1632 return DeclAndIsDerivedMember.getInt();
1633 }
1634 /// Get the class which the declaration actually lives in.
1635 const CXXRecordDecl *getContainingRecord() const {
1636 return cast<CXXRecordDecl>(
1637 Val: DeclAndIsDerivedMember.getPointer()->getDeclContext());
1638 }
1639
1640 void moveInto(APValue &V) const {
1641 V = APValue(getDecl(), isDerivedMember(), Path);
1642 }
1643 void setFrom(const APValue &V) {
1644 assert(V.isMemberPointer());
1645 DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());
1646 DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());
1647 Path.clear();
1648 llvm::append_range(C&: Path, R: V.getMemberPointerPath());
1649 }
1650
1651 /// DeclAndIsDerivedMember - The member declaration, and a flag indicating
1652 /// whether the member is a member of some class derived from the class type
1653 /// of the member pointer.
1654 llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;
1655 /// Path - The path of base/derived classes from the member declaration's
1656 /// class (exclusive) to the class type of the member pointer (inclusive).
1657 SmallVector<const CXXRecordDecl*, 4> Path;
1658
1659 /// Perform a cast towards the class of the Decl (either up or down the
1660 /// hierarchy).
1661 bool castBack(const CXXRecordDecl *Class) {
1662 assert(!Path.empty());
1663 const CXXRecordDecl *Expected;
1664 if (Path.size() >= 2)
1665 Expected = Path[Path.size() - 2];
1666 else
1667 Expected = getContainingRecord();
1668 if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {
1669 // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
1670 // if B does not contain the original member and is not a base or
1671 // derived class of the class containing the original member, the result
1672 // of the cast is undefined.
1673 // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
1674 // (D::*). We consider that to be a language defect.
1675 return false;
1676 }
1677 Path.pop_back();
1678 return true;
1679 }
1680 /// Perform a base-to-derived member pointer cast.
1681 bool castToDerived(const CXXRecordDecl *Derived) {
1682 if (!getDecl())
1683 return true;
1684 if (!isDerivedMember()) {
1685 Path.push_back(Elt: Derived);
1686 return true;
1687 }
1688 if (!castBack(Class: Derived))
1689 return false;
1690 if (Path.empty())
1691 DeclAndIsDerivedMember.setInt(false);
1692 return true;
1693 }
1694 /// Perform a derived-to-base member pointer cast.
1695 bool castToBase(const CXXRecordDecl *Base) {
1696 if (!getDecl())
1697 return true;
1698 if (Path.empty())
1699 DeclAndIsDerivedMember.setInt(true);
1700 if (isDerivedMember()) {
1701 Path.push_back(Elt: Base);
1702 return true;
1703 }
1704 return castBack(Class: Base);
1705 }
1706 };
1707
1708 /// Compare two member pointers, which are assumed to be of the same type.
1709 static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {
1710 if (!LHS.getDecl() || !RHS.getDecl())
1711 return !LHS.getDecl() && !RHS.getDecl();
1712 if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())
1713 return false;
1714 return LHS.Path == RHS.Path;
1715 }
1716}
1717
1718void SubobjectDesignator::adjustIndex(EvalInfo &Info, const Expr *E, APSInt N,
1719 const LValue &LV) {
1720 if (Invalid || !N)
1721 return;
1722 uint64_t TruncatedN = N.extOrTrunc(width: 64).getZExtValue();
1723 if (isMostDerivedAnUnsizedArray()) {
1724 diagnoseUnsizedArrayPointerArithmetic(Info, E);
1725 // Can't verify -- trust that the user is doing the right thing (or if
1726 // not, trust that the caller will catch the bad behavior).
1727 // FIXME: Should we reject if this overflows, at least?
1728 Entries.back() =
1729 PathEntry::ArrayIndex(Index: Entries.back().getAsArrayIndex() + TruncatedN);
1730 return;
1731 }
1732
1733 // [expr.add]p4: For the purposes of these operators, a pointer to a
1734 // nonarray object behaves the same as a pointer to the first element of
1735 // an array of length one with the type of the object as its element type.
1736 bool IsArray =
1737 MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement;
1738 uint64_t ArrayIndex =
1739 IsArray ? Entries.back().getAsArrayIndex() : (uint64_t)IsOnePastTheEnd;
1740 uint64_t ArraySize = IsArray ? getMostDerivedArraySize() : (uint64_t)1;
1741
1742 if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) {
1743 if (!Info.checkingPotentialConstantExpression() ||
1744 !LV.AllowConstexprUnknown) {
1745 // Calculate the actual index in a wide enough type, so we can include
1746 // it in the note.
1747 N = N.extend(width: std::max<unsigned>(a: N.getBitWidth() + 1, b: 65));
1748 (llvm::APInt &)N += ArrayIndex;
1749 assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index");
1750 diagnosePointerArithmetic(Info, E, N);
1751 }
1752 setInvalid();
1753 return;
1754 }
1755
1756 ArrayIndex += TruncatedN;
1757 assert(ArrayIndex <= ArraySize &&
1758 "bounds check succeeded for out-of-bounds index");
1759
1760 if (IsArray)
1761 Entries.back() = PathEntry::ArrayIndex(Index: ArrayIndex);
1762 else
1763 IsOnePastTheEnd = (ArrayIndex != 0);
1764}
1765
1766static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E);
1767static bool EvaluateInPlace(APValue &Result, EvalInfo &Info,
1768 const LValue &This, const Expr *E,
1769 bool AllowNonLiteralTypes = false);
1770static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
1771 bool InvalidBaseOK = false);
1772static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info,
1773 bool InvalidBaseOK = false);
1774static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
1775 EvalInfo &Info);
1776static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info);
1777static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
1778static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
1779 EvalInfo &Info);
1780static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
1781static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
1782static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info);
1783static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
1784 EvalInfo &Info);
1785static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result);
1786static std::optional<uint64_t>
1787EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
1788 std::string *StringResult = nullptr);
1789
1790/// Evaluate an integer or fixed point expression into an APResult.
1791static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
1792 EvalInfo &Info);
1793
1794/// Evaluate only a fixed point expression into an APResult.
1795static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
1796 EvalInfo &Info);
1797
1798//===----------------------------------------------------------------------===//
1799// Misc utilities
1800//===----------------------------------------------------------------------===//
1801
1802/// Negate an APSInt in place, converting it to a signed form if necessary, and
1803/// preserving its value (by extending by up to one bit as needed).
1804static void negateAsSigned(APSInt &Int) {
1805 if (Int.isUnsigned() || Int.isMinSignedValue()) {
1806 Int = Int.extend(width: Int.getBitWidth() + 1);
1807 Int.setIsSigned(true);
1808 }
1809 Int = -Int;
1810}
1811
1812template<typename KeyT>
1813APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T,
1814 ScopeKind Scope, LValue &LV) {
1815 unsigned Version = getTempVersion();
1816 APValue::LValueBase Base(Key, Index, Version);
1817 LV.set(B: Base);
1818 return createLocal(Base, Key, T, Scope);
1819}
1820
1821/// Allocate storage for a parameter of a function call made in this frame.
1822APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD,
1823 LValue &LV) {
1824 assert(Args.CallIndex == Index && "creating parameter in wrong frame");
1825 APValue::LValueBase Base(PVD, Index, Args.Version);
1826 LV.set(B: Base);
1827 // We always destroy parameters at the end of the call, even if we'd allow
1828 // them to live to the end of the full-expression at runtime, in order to
1829 // give portable results and match other compilers.
1830 return createLocal(Base, Key: PVD, T: PVD->getType(), Scope: ScopeKind::Call);
1831}
1832
1833APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key,
1834 QualType T, ScopeKind Scope) {
1835 assert(Base.getCallIndex() == Index && "lvalue for wrong frame");
1836 unsigned Version = Base.getVersion();
1837 APValue &Result = Temporaries[MapKeyTy(Key, Version)];
1838 assert(Result.isAbsent() && "local created multiple times");
1839
1840 // If we're creating a local immediately in the operand of a speculative
1841 // evaluation, don't register a cleanup to be run outside the speculative
1842 // evaluation context, since we won't actually be able to initialize this
1843 // object.
1844 if (Index <= Info.SpeculativeEvaluationDepth) {
1845 if (T.isDestructedType())
1846 Info.noteSideEffect();
1847 } else {
1848 Info.CleanupStack.push_back(Elt: Cleanup(&Result, Base, T, Scope));
1849 }
1850 return Result;
1851}
1852
1853APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) {
1854 if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) {
1855 FFDiag(E, DiagId: diag::note_constexpr_heap_alloc_limit_exceeded);
1856 return nullptr;
1857 }
1858
1859 DynamicAllocLValue DA(NumHeapAllocs++);
1860 LV.set(B: APValue::LValueBase::getDynamicAlloc(LV: DA, Type: T));
1861 auto Result = HeapAllocs.emplace(args: std::piecewise_construct,
1862 args: std::forward_as_tuple(args&: DA), args: std::tuple<>());
1863 assert(Result.second && "reused a heap alloc index?");
1864 Result.first->second.AllocExpr = E;
1865 return &Result.first->second.Value;
1866}
1867
1868/// Produce a string describing the given constexpr call.
1869void CallStackFrame::describe(raw_ostream &Out) const {
1870 bool IsMemberCall = false;
1871 bool ExplicitInstanceParam = false;
1872 clang::PrintingPolicy PrintingPolicy = Info.Ctx.getPrintingPolicy();
1873 PrintingPolicy.SuppressLambdaBody = true;
1874
1875 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: Callee)) {
1876 IsMemberCall = !isa<CXXConstructorDecl>(Val: MD) && !MD->isStatic();
1877 ExplicitInstanceParam = MD->isExplicitObjectMemberFunction();
1878 }
1879
1880 if (!IsMemberCall)
1881 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1882 /*Qualified=*/false);
1883
1884 if (This && IsMemberCall) {
1885 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(Val: CallExpr)) {
1886 const Expr *Object = MCE->getImplicitObjectArgument();
1887 Object->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1888 /*Indentation=*/0);
1889 if (Object->getType()->isPointerType())
1890 Out << "->";
1891 else
1892 Out << ".";
1893 } else if (const auto *OCE =
1894 dyn_cast_if_present<CXXOperatorCallExpr>(Val: CallExpr)) {
1895 OCE->getArg(Arg: 0)->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1896 /*Indentation=*/0);
1897 Out << ".";
1898 } else {
1899 APValue Val;
1900 This->moveInto(V&: Val);
1901 Val.printPretty(
1902 OS&: Out, Ctx: Info.Ctx,
1903 Ty: Info.Ctx.getLValueReferenceType(T: This->Designator.MostDerivedType));
1904 Out << ".";
1905 }
1906 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1907 /*Qualified=*/false);
1908 }
1909
1910 Out << '(';
1911
1912 llvm::ListSeparator Comma;
1913 for (const ParmVarDecl *Param :
1914 Callee->parameters().slice(N: ExplicitInstanceParam)) {
1915 Out << Comma;
1916 const APValue *V = Info.getParamSlot(Call: Arguments, PVD: Param);
1917 if (V)
1918 V->printPretty(OS&: Out, Ctx: Info.Ctx, Ty: Param->getType());
1919 else
1920 Out << "<...>";
1921 }
1922
1923 Out << ')';
1924}
1925
1926/// Evaluate an expression to see if it had side-effects, and discard its
1927/// result.
1928/// \return \c true if the caller should keep evaluating.
1929static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) {
1930 assert(!E->isValueDependent());
1931 APValue Scratch;
1932 if (!Evaluate(Result&: Scratch, Info, E))
1933 // We don't need the value, but we might have skipped a side effect here.
1934 return Info.noteSideEffect();
1935 return true;
1936}
1937
1938/// Should this call expression be treated as forming an opaque constant?
1939static bool IsOpaqueConstantCall(const CallExpr *E) {
1940 unsigned Builtin = E->getBuiltinCallee();
1941 return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
1942 Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||
1943 Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||
1944 Builtin == Builtin::BI__builtin_function_start);
1945}
1946
1947static bool IsOpaqueConstantCall(const LValue &LVal) {
1948 const auto *BaseExpr =
1949 llvm::dyn_cast_if_present<CallExpr>(Val: LVal.Base.dyn_cast<const Expr *>());
1950 return BaseExpr && IsOpaqueConstantCall(E: BaseExpr);
1951}
1952
1953static bool IsGlobalLValue(APValue::LValueBase B) {
1954 // C++11 [expr.const]p3 An address constant expression is a prvalue core
1955 // constant expression of pointer type that evaluates to...
1956
1957 // ... a null pointer value, or a prvalue core constant expression of type
1958 // std::nullptr_t.
1959 if (!B)
1960 return true;
1961
1962 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
1963 // ... the address of an object with static storage duration,
1964 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
1965 return VD->hasGlobalStorage();
1966 if (isa<TemplateParamObjectDecl>(Val: D))
1967 return true;
1968 // ... the address of a function,
1969 // ... the address of a GUID [MS extension],
1970 // ... the address of an unnamed global constant
1971 return isa<FunctionDecl, MSGuidDecl, UnnamedGlobalConstantDecl>(Val: D);
1972 }
1973
1974 if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>())
1975 return true;
1976
1977 const Expr *E = B.get<const Expr*>();
1978 switch (E->getStmtClass()) {
1979 default:
1980 return false;
1981 case Expr::CompoundLiteralExprClass: {
1982 const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(Val: E);
1983 return CLE->isFileScope() && CLE->isLValue();
1984 }
1985 case Expr::MaterializeTemporaryExprClass:
1986 // A materialized temporary might have been lifetime-extended to static
1987 // storage duration.
1988 return cast<MaterializeTemporaryExpr>(Val: E)->getStorageDuration() == SD_Static;
1989 // A string literal has static storage duration.
1990 case Expr::StringLiteralClass:
1991 case Expr::PredefinedExprClass:
1992 case Expr::ObjCStringLiteralClass:
1993 case Expr::ObjCEncodeExprClass:
1994 return true;
1995 case Expr::ObjCBoxedExprClass:
1996 case Expr::ObjCArrayLiteralClass:
1997 case Expr::ObjCDictionaryLiteralClass:
1998 return cast<ObjCObjectLiteral>(Val: E)->isExpressibleAsConstantInitializer();
1999 case Expr::CallExprClass:
2000 return IsOpaqueConstantCall(E: cast<CallExpr>(Val: E));
2001 // For GCC compatibility, &&label has static storage duration.
2002 case Expr::AddrLabelExprClass:
2003 return true;
2004 // A Block literal expression may be used as the initialization value for
2005 // Block variables at global or local static scope.
2006 case Expr::BlockExprClass:
2007 return !cast<BlockExpr>(Val: E)->getBlockDecl()->hasCaptures();
2008 // The APValue generated from a __builtin_source_location will be emitted as a
2009 // literal.
2010 case Expr::SourceLocExprClass:
2011 return true;
2012 case Expr::ImplicitValueInitExprClass:
2013 // FIXME:
2014 // We can never form an lvalue with an implicit value initialization as its
2015 // base through expression evaluation, so these only appear in one case: the
2016 // implicit variable declaration we invent when checking whether a constexpr
2017 // constructor can produce a constant expression. We must assume that such
2018 // an expression might be a global lvalue.
2019 return true;
2020 }
2021}
2022
2023static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
2024 return LVal.Base.dyn_cast<const ValueDecl*>();
2025}
2026
2027// Information about an LValueBase that is some kind of string.
2028struct LValueBaseString {
2029 std::string ObjCEncodeStorage;
2030 StringRef Bytes;
2031 int CharWidth;
2032};
2033
2034// Gets the lvalue base of LVal as a string.
2035static bool GetLValueBaseAsString(const EvalInfo &Info, const LValue &LVal,
2036 LValueBaseString &AsString) {
2037 const auto *BaseExpr = LVal.Base.dyn_cast<const Expr *>();
2038 if (!BaseExpr)
2039 return false;
2040
2041 // For ObjCEncodeExpr, we need to compute and store the string.
2042 if (const auto *EE = dyn_cast<ObjCEncodeExpr>(Val: BaseExpr)) {
2043 Info.Ctx.getObjCEncodingForType(T: EE->getEncodedType(),
2044 S&: AsString.ObjCEncodeStorage);
2045 AsString.Bytes = AsString.ObjCEncodeStorage;
2046 AsString.CharWidth = 1;
2047 return true;
2048 }
2049
2050 // Otherwise, we have a StringLiteral.
2051 const auto *Lit = dyn_cast<StringLiteral>(Val: BaseExpr);
2052 if (const auto *PE = dyn_cast<PredefinedExpr>(Val: BaseExpr))
2053 Lit = PE->getFunctionName();
2054
2055 if (!Lit)
2056 return false;
2057
2058 AsString.Bytes = Lit->getBytes();
2059 AsString.CharWidth = Lit->getCharByteWidth();
2060 return true;
2061}
2062
2063// Determine whether two string literals potentially overlap. This will be the
2064// case if they agree on the values of all the bytes on the overlapping region
2065// between them.
2066//
2067// The overlapping region is the portion of the two string literals that must
2068// overlap in memory if the pointers actually point to the same address at
2069// runtime. For example, if LHS is "abcdef" + 3 and RHS is "cdef\0gh" + 1 then
2070// the overlapping region is "cdef\0", which in this case does agree, so the
2071// strings are potentially overlapping. Conversely, for "foobar" + 3 versus
2072// "bazbar" + 3, the overlapping region contains all of both strings, so they
2073// are not potentially overlapping, even though they agree from the given
2074// addresses onwards.
2075//
2076// See open core issue CWG2765 which is discussing the desired rule here.
2077static bool ArePotentiallyOverlappingStringLiterals(const EvalInfo &Info,
2078 const LValue &LHS,
2079 const LValue &RHS) {
2080 LValueBaseString LHSString, RHSString;
2081 if (!GetLValueBaseAsString(Info, LVal: LHS, AsString&: LHSString) ||
2082 !GetLValueBaseAsString(Info, LVal: RHS, AsString&: RHSString))
2083 return false;
2084
2085 // This is the byte offset to the location of the first character of LHS
2086 // within RHS. We don't need to look at the characters of one string that
2087 // would appear before the start of the other string if they were merged.
2088 CharUnits Offset = RHS.Offset - LHS.Offset;
2089 if (Offset.isNegative()) {
2090 if (LHSString.Bytes.size() < (size_t)-Offset.getQuantity())
2091 return false;
2092 LHSString.Bytes = LHSString.Bytes.drop_front(N: -Offset.getQuantity());
2093 } else {
2094 if (RHSString.Bytes.size() < (size_t)Offset.getQuantity())
2095 return false;
2096 RHSString.Bytes = RHSString.Bytes.drop_front(N: Offset.getQuantity());
2097 }
2098
2099 bool LHSIsLonger = LHSString.Bytes.size() > RHSString.Bytes.size();
2100 StringRef Longer = LHSIsLonger ? LHSString.Bytes : RHSString.Bytes;
2101 StringRef Shorter = LHSIsLonger ? RHSString.Bytes : LHSString.Bytes;
2102 int ShorterCharWidth = (LHSIsLonger ? RHSString : LHSString).CharWidth;
2103
2104 // The null terminator isn't included in the string data, so check for it
2105 // manually. If the longer string doesn't have a null terminator where the
2106 // shorter string ends, they aren't potentially overlapping.
2107 for (int NullByte : llvm::seq(Size: ShorterCharWidth)) {
2108 if (Shorter.size() + NullByte >= Longer.size())
2109 break;
2110 if (Longer[Shorter.size() + NullByte])
2111 return false;
2112 }
2113
2114 // Otherwise, they're potentially overlapping if and only if the overlapping
2115 // region is the same.
2116 return Shorter == Longer.take_front(N: Shorter.size());
2117}
2118
2119static bool IsWeakLValue(const LValue &Value) {
2120 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2121 return Decl && Decl->isWeak();
2122}
2123
2124static bool isZeroSized(const LValue &Value) {
2125 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2126 if (isa_and_nonnull<VarDecl>(Val: Decl)) {
2127 QualType Ty = Decl->getType();
2128 if (Ty->isArrayType())
2129 return Ty->isIncompleteType() ||
2130 Decl->getASTContext().getTypeSize(T: Ty) == 0;
2131 }
2132 return false;
2133}
2134
2135static bool HasSameBase(const LValue &A, const LValue &B) {
2136 if (!A.getLValueBase())
2137 return !B.getLValueBase();
2138 if (!B.getLValueBase())
2139 return false;
2140
2141 if (A.getLValueBase().getOpaqueValue() !=
2142 B.getLValueBase().getOpaqueValue())
2143 return false;
2144
2145 return A.getLValueCallIndex() == B.getLValueCallIndex() &&
2146 A.getLValueVersion() == B.getLValueVersion();
2147}
2148
2149static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) {
2150 assert(Base && "no location for a null lvalue");
2151 const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
2152
2153 // For a parameter, find the corresponding call stack frame (if it still
2154 // exists), and point at the parameter of the function definition we actually
2155 // invoked.
2156 if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(Val: VD)) {
2157 unsigned Idx = PVD->getFunctionScopeIndex();
2158 for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) {
2159 if (F->Arguments.CallIndex == Base.getCallIndex() &&
2160 F->Arguments.Version == Base.getVersion() && F->Callee &&
2161 Idx < F->Callee->getNumParams()) {
2162 VD = F->Callee->getParamDecl(i: Idx);
2163 break;
2164 }
2165 }
2166 }
2167
2168 if (VD)
2169 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
2170 else if (const Expr *E = Base.dyn_cast<const Expr*>())
2171 Info.Note(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
2172 else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
2173 // FIXME: Produce a note for dangling pointers too.
2174 if (std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA))
2175 Info.Note(Loc: (*Alloc)->AllocExpr->getExprLoc(),
2176 DiagId: diag::note_constexpr_dynamic_alloc_here);
2177 }
2178
2179 // We have no information to show for a typeid(T) object.
2180}
2181
2182enum class CheckEvaluationResultKind {
2183 ConstantExpression,
2184 FullyInitialized,
2185};
2186
2187/// Materialized temporaries that we've already checked to determine if they're
2188/// initializsed by a constant expression.
2189using CheckedTemporaries =
2190 llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>;
2191
2192static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2193 EvalInfo &Info, SourceLocation DiagLoc,
2194 QualType Type, const APValue &Value,
2195 ConstantExprKind Kind,
2196 const FieldDecl *SubobjectDecl,
2197 CheckedTemporaries &CheckedTemps,
2198 bool IsCompleteClass = true);
2199
2200/// Check that this reference or pointer core constant expression is a valid
2201/// value for an address or reference constant expression. Return true if we
2202/// can fold this expression, whether or not it's a constant expression.
2203static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
2204 QualType Type, const LValue &LVal,
2205 ConstantExprKind Kind,
2206 CheckedTemporaries &CheckedTemps) {
2207 bool IsReferenceType = Type->isReferenceType();
2208
2209 APValue::LValueBase Base = LVal.getLValueBase();
2210 const SubobjectDesignator &Designator = LVal.getLValueDesignator();
2211
2212 const Expr *BaseE = Base.dyn_cast<const Expr *>();
2213 const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>();
2214
2215 // Additional restrictions apply in a template argument. We only enforce the
2216 // C++20 restrictions here; additional syntactic and semantic restrictions
2217 // are applied elsewhere.
2218 if (isTemplateArgument(Kind)) {
2219 int InvalidBaseKind = -1;
2220 StringRef Ident;
2221 if (Base.is<TypeInfoLValue>())
2222 InvalidBaseKind = 0;
2223 else if (isa_and_nonnull<StringLiteral>(Val: BaseE))
2224 InvalidBaseKind = 1;
2225 else if (isa_and_nonnull<MaterializeTemporaryExpr>(Val: BaseE) ||
2226 isa_and_nonnull<LifetimeExtendedTemporaryDecl>(Val: BaseVD))
2227 InvalidBaseKind = 2;
2228 else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(Val: BaseE)) {
2229 InvalidBaseKind = 3;
2230 Ident = PE->getIdentKindName();
2231 }
2232
2233 if (InvalidBaseKind != -1) {
2234 Info.FFDiag(Loc, DiagId: diag::note_constexpr_invalid_template_arg)
2235 << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind
2236 << Ident;
2237 return false;
2238 }
2239 }
2240
2241 if (auto *FD = dyn_cast_or_null<FunctionDecl>(Val: BaseVD);
2242 FD && FD->isImmediateFunction()) {
2243 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible)
2244 << !Type->isAnyPointerType();
2245 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2246 return false;
2247 }
2248
2249 // Check that the object is a global. Note that the fake 'this' object we
2250 // manufacture when checking potential constant expressions is conservatively
2251 // assumed to be global here.
2252 if (!IsGlobalLValue(B: Base)) {
2253 if (Info.getLangOpts().CPlusPlus11) {
2254 Info.FFDiag(Loc, DiagId: diag::note_constexpr_non_global, ExtraNotes: 1)
2255 << IsReferenceType << !Designator.Entries.empty() << !!BaseVD
2256 << BaseVD;
2257 auto *VarD = dyn_cast_or_null<VarDecl>(Val: BaseVD);
2258 if (VarD && VarD->isConstexpr()) {
2259 // Non-static local constexpr variables have unintuitive semantics:
2260 // constexpr int a = 1;
2261 // constexpr const int *p = &a;
2262 // ... is invalid because the address of 'a' is not constant. Suggest
2263 // adding a 'static' in this case.
2264 Info.Note(Loc: VarD->getLocation(), DiagId: diag::note_constexpr_not_static)
2265 << VarD
2266 << FixItHint::CreateInsertion(InsertionLoc: VarD->getBeginLoc(), Code: "static ");
2267 } else {
2268 NoteLValueLocation(Info, Base);
2269 }
2270 } else {
2271 Info.FFDiag(Loc);
2272 }
2273 // Don't allow references to temporaries to escape.
2274 return false;
2275 }
2276 assert((Info.checkingPotentialConstantExpression() ||
2277 LVal.getLValueCallIndex() == 0) &&
2278 "have call index for global lvalue");
2279
2280 if (LVal.allowConstexprUnknown()) {
2281 if (BaseVD) {
2282 Info.FFDiag(Loc, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << BaseVD;
2283 NoteLValueLocation(Info, Base);
2284 } else {
2285 Info.FFDiag(Loc);
2286 }
2287 return false;
2288 }
2289
2290 if (Base.is<DynamicAllocLValue>()) {
2291 Info.FFDiag(Loc, DiagId: diag::note_constexpr_dynamic_alloc)
2292 << IsReferenceType << !Designator.Entries.empty();
2293 NoteLValueLocation(Info, Base);
2294 return false;
2295 }
2296
2297 if (BaseVD) {
2298 if (const VarDecl *Var = dyn_cast<const VarDecl>(Val: BaseVD)) {
2299 // Check if this is a thread-local variable.
2300 if (Var->getTLSKind())
2301 // FIXME: Diagnostic!
2302 return false;
2303
2304 // A dllimport variable never acts like a constant, unless we're
2305 // evaluating a value for use only in name mangling, and unless it's a
2306 // static local. For the latter case, we'd still need to evaluate the
2307 // constant expression in case we're inside a (inlined) function.
2308 if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>() &&
2309 !Var->isStaticLocal())
2310 return false;
2311
2312 // In CUDA/HIP device compilation, only device side variables have
2313 // constant addresses.
2314 if (Info.getLangOpts().CUDA && Info.getLangOpts().CUDAIsDevice &&
2315 Info.Ctx.CUDAConstantEvalCtx.NoWrongSidedVars) {
2316 if ((!Var->hasAttr<CUDADeviceAttr>() &&
2317 !Var->hasAttr<CUDAConstantAttr>() &&
2318 !Var->getType()->isCUDADeviceBuiltinSurfaceType() &&
2319 !Var->getType()->isCUDADeviceBuiltinTextureType()) ||
2320 Var->hasAttr<HIPManagedAttr>())
2321 return false;
2322 }
2323 }
2324 if (const auto *FD = dyn_cast<const FunctionDecl>(Val: BaseVD)) {
2325 // __declspec(dllimport) must be handled very carefully:
2326 // We must never initialize an expression with the thunk in C++.
2327 // Doing otherwise would allow the same id-expression to yield
2328 // different addresses for the same function in different translation
2329 // units. However, this means that we must dynamically initialize the
2330 // expression with the contents of the import address table at runtime.
2331 //
2332 // The C language has no notion of ODR; furthermore, it has no notion of
2333 // dynamic initialization. This means that we are permitted to
2334 // perform initialization with the address of the thunk.
2335 if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) &&
2336 FD->hasAttr<DLLImportAttr>())
2337 // FIXME: Diagnostic!
2338 return false;
2339 }
2340 } else if (const auto *MTE =
2341 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: BaseE)) {
2342 if (CheckedTemps.insert(Ptr: MTE).second) {
2343 QualType TempType = getType(B: Base);
2344 if (TempType.isDestructedType()) {
2345 Info.FFDiag(Loc: MTE->getExprLoc(),
2346 DiagId: diag::note_constexpr_unsupported_temporary_nontrivial_dtor)
2347 << TempType;
2348 return false;
2349 }
2350
2351 APValue *V = MTE->getOrCreateValue(MayCreate: false);
2352 assert(V && "evasluation result refers to uninitialised temporary");
2353 if (!CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2354 Info, DiagLoc: MTE->getExprLoc(), Type: TempType, Value: *V, Kind,
2355 /*SubobjectDecl=*/nullptr, CheckedTemps))
2356 return false;
2357 }
2358 }
2359
2360 // Allow address constant expressions to be past-the-end pointers. This is
2361 // an extension: the standard requires them to point to an object.
2362 if (!IsReferenceType)
2363 return true;
2364
2365 // A reference constant expression must refer to an object.
2366 if (!Base) {
2367 // FIXME: diagnostic
2368 Info.CCEDiag(Loc);
2369 return true;
2370 }
2371
2372 // Does this refer one past the end of some object?
2373 if (!Designator.Invalid && Designator.isOnePastTheEnd()) {
2374 Info.FFDiag(Loc, DiagId: diag::note_constexpr_past_end, ExtraNotes: 1)
2375 << !Designator.Entries.empty() << !!BaseVD << BaseVD;
2376 NoteLValueLocation(Info, Base);
2377 }
2378
2379 return true;
2380}
2381
2382/// Member pointers are constant expressions unless they point to a
2383/// non-virtual dllimport member function.
2384static bool CheckMemberPointerConstantExpression(EvalInfo &Info,
2385 SourceLocation Loc,
2386 QualType Type,
2387 const APValue &Value,
2388 ConstantExprKind Kind) {
2389 const ValueDecl *Member = Value.getMemberPointerDecl();
2390 const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Val: Member);
2391 if (!FD)
2392 return true;
2393 if (FD->isImmediateFunction()) {
2394 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible) << /*pointer*/ 0;
2395 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2396 return false;
2397 }
2398 return isForManglingOnly(Kind) || FD->isVirtual() ||
2399 !FD->hasAttr<DLLImportAttr>();
2400}
2401
2402/// Check that this core constant expression is of literal type, and if not,
2403/// produce an appropriate diagnostic.
2404static bool CheckLiteralType(EvalInfo &Info, const Expr *E,
2405 const LValue *This = nullptr) {
2406 // The restriction to literal types does not exist in C++23 anymore.
2407 if (Info.getLangOpts().CPlusPlus23)
2408 return true;
2409
2410 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Info.Ctx))
2411 return true;
2412
2413 // C++1y: A constant initializer for an object o [...] may also invoke
2414 // constexpr constructors for o and its subobjects even if those objects
2415 // are of non-literal class types.
2416 //
2417 // C++11 missed this detail for aggregates, so classes like this:
2418 // struct foo_t { union { int i; volatile int j; } u; };
2419 // are not (obviously) initializable like so:
2420 // __attribute__((__require_constant_initialization__))
2421 // static const foo_t x = {{0}};
2422 // because "i" is a subobject with non-literal initialization (due to the
2423 // volatile member of the union). See:
2424 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677
2425 // Therefore, we use the C++1y behavior.
2426 if (This && Info.EvaluatingDecl == This->getLValueBase())
2427 return true;
2428
2429 // Prvalue constant expressions must be of literal types.
2430 if (Info.getLangOpts().CPlusPlus11)
2431 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral)
2432 << E->getType();
2433 else
2434 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
2435 return false;
2436}
2437
2438static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2439 EvalInfo &Info, SourceLocation DiagLoc,
2440 QualType Type, const APValue &Value,
2441 ConstantExprKind Kind,
2442 const FieldDecl *SubobjectDecl,
2443 CheckedTemporaries &CheckedTemps,
2444 bool IsCompleteClass) {
2445 if (!Value.hasValue()) {
2446 if (SubobjectDecl) {
2447 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2448 << /*(name)*/ 1 << SubobjectDecl;
2449 Info.Note(Loc: SubobjectDecl->getLocation(),
2450 DiagId: diag::note_constexpr_subobject_declared_here);
2451 } else {
2452 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2453 << /*of type*/ 0 << Type;
2454 }
2455 return false;
2456 }
2457
2458 // We allow _Atomic(T) to be initialized from anything that T can be
2459 // initialized from.
2460 if (const AtomicType *AT = Type->getAs<AtomicType>())
2461 Type = AT->getValueType();
2462
2463 // Core issue 1454: For a literal constant expression of array or class type,
2464 // each subobject of its value shall have been initialized by a constant
2465 // expression.
2466 if (Value.isArray()) {
2467 QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();
2468 for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {
2469 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2470 Value: Value.getArrayInitializedElt(I), Kind,
2471 SubobjectDecl, CheckedTemps))
2472 return false;
2473 }
2474 if (!Value.hasArrayFiller())
2475 return true;
2476 return CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2477 Value: Value.getArrayFiller(), Kind, SubobjectDecl,
2478 CheckedTemps);
2479 }
2480 if (Value.isUnion() && Value.getUnionField()) {
2481 return CheckEvaluationResult(
2482 CERK, Info, DiagLoc, Type: Value.getUnionField()->getType(),
2483 Value: Value.getUnionValue(), Kind, SubobjectDecl: Value.getUnionField(), CheckedTemps);
2484 }
2485 if (Value.isStruct()) {
2486 auto *RD = Type->castAsRecordDecl();
2487 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2488 unsigned BaseIndex = 0;
2489 for (const CXXBaseSpecifier &BS : CD->bases()) {
2490 if (BS.isVirtual())
2491 continue;
2492 const APValue &BaseValue = Value.getStructBase(i: BaseIndex);
2493 if (!BaseValue.hasValue()) {
2494 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2495 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2496 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2497 return false;
2498 }
2499 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(), Value: BaseValue,
2500 Kind, /*SubobjectDecl=*/nullptr,
2501 CheckedTemps, /*IsCompleteClass=*/false))
2502 return false;
2503 ++BaseIndex;
2504 }
2505 }
2506 for (const auto *I : RD->fields()) {
2507 if (I->isUnnamedBitField())
2508 continue;
2509
2510 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: I->getType(),
2511 Value: Value.getStructField(i: I->getFieldIndex()), Kind,
2512 SubobjectDecl: I, CheckedTemps))
2513 return false;
2514 }
2515
2516 if (IsCompleteClass) {
2517 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2518 unsigned BaseIndex = 0;
2519 for (const CXXBaseSpecifier &BS : CD->vbases()) {
2520 assert(BS.isVirtual());
2521 const APValue &BaseValue = Value.getStructVirtualBase(i: BaseIndex);
2522 if (!BaseValue.hasValue()) {
2523 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2524 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2525 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2526 return false;
2527 }
2528 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(),
2529 Value: BaseValue, Kind, /*SubobjectDecl=*/nullptr,
2530 CheckedTemps, /*IsCompleteClass=*/false))
2531 return false;
2532 ++BaseIndex;
2533 }
2534 }
2535 }
2536 }
2537
2538 if (Value.isLValue() &&
2539 CERK == CheckEvaluationResultKind::ConstantExpression) {
2540 LValue LVal;
2541 LVal.setFrom(Ctx: Info.Ctx, V: Value);
2542 return CheckLValueConstantExpression(Info, Loc: DiagLoc, Type, LVal, Kind,
2543 CheckedTemps);
2544 }
2545
2546 if (Value.isMemberPointer() &&
2547 CERK == CheckEvaluationResultKind::ConstantExpression)
2548 return CheckMemberPointerConstantExpression(Info, Loc: DiagLoc, Type, Value, Kind);
2549
2550 // Everything else is fine.
2551 return true;
2552}
2553
2554/// Check that this core constant expression value is a valid value for a
2555/// constant expression. If not, report an appropriate diagnostic. Does not
2556/// check that the expression is of literal type.
2557static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
2558 QualType Type, const APValue &Value,
2559 ConstantExprKind Kind) {
2560 // Nothing to check for a constant expression of type 'cv void'.
2561 if (Type->isVoidType())
2562 return true;
2563
2564 CheckedTemporaries CheckedTemps;
2565 return CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2566 Info, DiagLoc, Type, Value, Kind,
2567 /*SubobjectDecl=*/nullptr, CheckedTemps);
2568}
2569
2570/// Check that this evaluated value is fully-initialized and can be loaded by
2571/// an lvalue-to-rvalue conversion.
2572static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc,
2573 QualType Type, const APValue &Value) {
2574 CheckedTemporaries CheckedTemps;
2575 return CheckEvaluationResult(
2576 CERK: CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value,
2577 Kind: ConstantExprKind::Normal, /*SubobjectDecl=*/nullptr, CheckedTemps);
2578}
2579
2580/// Enforce C++2a [expr.const]/4.17, which disallows new-expressions unless
2581/// "the allocated storage is deallocated within the evaluation".
2582static bool CheckMemoryLeaks(EvalInfo &Info) {
2583 if (!Info.HeapAllocs.empty()) {
2584 // We can still fold to a constant despite a compile-time memory leak,
2585 // so long as the heap allocation isn't referenced in the result (we check
2586 // that in CheckConstantExpression).
2587 Info.CCEDiag(E: Info.HeapAllocs.begin()->second.AllocExpr,
2588 DiagId: diag::note_constexpr_memory_leak)
2589 << unsigned(Info.HeapAllocs.size() - 1);
2590 }
2591 return true;
2592}
2593
2594static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {
2595 // A null base expression indicates a null pointer. These are always
2596 // evaluatable, and they are false unless the offset is zero.
2597 if (!Value.getLValueBase()) {
2598 // TODO: Should a non-null pointer with an offset of zero evaluate to true?
2599 Result = !Value.getLValueOffset().isZero();
2600 return true;
2601 }
2602
2603 // We have a non-null base. These are generally known to be true, but if it's
2604 // a weak declaration it can be null at runtime.
2605 Result = true;
2606 const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
2607 return !Decl || !Decl->isWeak();
2608}
2609
2610static bool HandleConversionToBool(const APValue &Val, bool &Result) {
2611 // TODO: This function should produce notes if it fails.
2612 switch (Val.getKind()) {
2613 case APValue::None:
2614 case APValue::Indeterminate:
2615 return false;
2616 case APValue::Int:
2617 Result = Val.getInt().getBoolValue();
2618 return true;
2619 case APValue::FixedPoint:
2620 Result = Val.getFixedPoint().getBoolValue();
2621 return true;
2622 case APValue::Float:
2623 Result = !Val.getFloat().isZero();
2624 return true;
2625 case APValue::ComplexInt:
2626 Result = Val.getComplexIntReal().getBoolValue() ||
2627 Val.getComplexIntImag().getBoolValue();
2628 return true;
2629 case APValue::ComplexFloat:
2630 Result = !Val.getComplexFloatReal().isZero() ||
2631 !Val.getComplexFloatImag().isZero();
2632 return true;
2633 case APValue::LValue:
2634 return EvalPointerValueAsBool(Value: Val, Result);
2635 case APValue::MemberPointer:
2636 if (Val.getMemberPointerDecl() && Val.getMemberPointerDecl()->isWeak()) {
2637 return false;
2638 }
2639 Result = Val.getMemberPointerDecl();
2640 return true;
2641 case APValue::Vector:
2642 case APValue::Matrix:
2643 case APValue::Array:
2644 case APValue::Struct:
2645 case APValue::Union:
2646 case APValue::AddrLabelDiff:
2647 return false;
2648 }
2649
2650 llvm_unreachable("unknown APValue kind");
2651}
2652
2653static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
2654 EvalInfo &Info) {
2655 assert(!E->isValueDependent());
2656 assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition");
2657 APValue Val;
2658 if (!Evaluate(Result&: Val, Info, E))
2659 return false;
2660 return HandleConversionToBool(Val, Result);
2661}
2662
2663template<typename T>
2664static bool HandleOverflow(EvalInfo &Info, const Expr *E,
2665 const T &SrcValue, QualType DestType) {
2666 Info.CCEDiag(E, DiagId: diag::note_constexpr_overflow) << SrcValue << DestType;
2667 if (const auto *OBT = DestType->getAs<OverflowBehaviorType>();
2668 OBT && OBT->isTrapKind()) {
2669 return false;
2670 }
2671 return Info.noteUndefinedBehavior();
2672}
2673
2674static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
2675 QualType SrcType, const APFloat &Value,
2676 QualType DestType, APSInt &Result) {
2677 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2678 // Determine whether we are converting to unsigned or signed.
2679 bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
2680
2681 Result = APSInt(DestWidth, !DestSigned);
2682 bool ignored;
2683 if (Value.convertToInteger(Result, RM: llvm::APFloat::rmTowardZero, IsExact: &ignored)
2684 & APFloat::opInvalidOp)
2685 return HandleOverflow(Info, E, SrcValue: Value, DestType);
2686 return true;
2687}
2688
2689/// Get rounding mode to use in evaluation of the specified expression.
2690///
2691/// If rounding mode is unknown at compile time, still try to evaluate the
2692/// expression. If the result is exact, it does not depend on rounding mode.
2693/// So return "tonearest" mode instead of "dynamic".
2694static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E) {
2695 llvm::RoundingMode RM =
2696 E->getFPFeaturesInEffect(LO: Info.getLangOpts()).getRoundingMode();
2697 if (RM == llvm::RoundingMode::Dynamic)
2698 RM = llvm::RoundingMode::NearestTiesToEven;
2699 return RM;
2700}
2701
2702/// Check if the given evaluation result is allowed for constant evaluation.
2703static bool checkFloatingPointResult(EvalInfo &Info, const Expr *E,
2704 APFloat::opStatus St) {
2705 // In a constant context, assume that any dynamic rounding mode or FP
2706 // exception state matches the default floating-point environment.
2707 if (Info.InConstantContext)
2708 return true;
2709
2710 FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.getLangOpts());
2711 if ((St & APFloat::opInexact) &&
2712 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
2713 // Inexact result means that it depends on rounding mode. If the requested
2714 // mode is dynamic, the evaluation cannot be made in compile time.
2715 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
2716 return false;
2717 }
2718
2719 if ((St != APFloat::opOK) &&
2720 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||
2721 FPO.getExceptionMode() != LangOptions::FPE_Ignore ||
2722 FPO.getAllowFEnvAccess())) {
2723 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
2724 return false;
2725 }
2726
2727 if ((St & APFloat::opStatus::opInvalidOp) &&
2728 FPO.getExceptionMode() != LangOptions::FPE_Ignore) {
2729 // There is no usefully definable result.
2730 Info.FFDiag(E);
2731 return false;
2732 }
2733
2734 // FIXME: if:
2735 // - evaluation triggered other FP exception, and
2736 // - exception mode is not "ignore", and
2737 // - the expression being evaluated is not a part of global variable
2738 // initializer,
2739 // the evaluation probably need to be rejected.
2740 return true;
2741}
2742
2743static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
2744 QualType SrcType, QualType DestType,
2745 APFloat &Result) {
2746 assert((isa<CastExpr>(E) || isa<CompoundAssignOperator>(E) ||
2747 isa<ConvertVectorExpr>(E)) &&
2748 "HandleFloatToFloatCast has been checked with only CastExpr, "
2749 "CompoundAssignOperator and ConvertVectorExpr. Please either validate "
2750 "the new expression or address the root cause of this usage.");
2751 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2752 APFloat::opStatus St;
2753 APFloat Value = Result;
2754 bool ignored;
2755 St = Result.convert(ToSemantics: Info.Ctx.getFloatTypeSemantics(T: DestType), RM, losesInfo: &ignored);
2756 return checkFloatingPointResult(Info, E, St);
2757}
2758
2759static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
2760 QualType DestType, QualType SrcType,
2761 const APSInt &Value) {
2762 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2763 // Figure out if this is a truncate, extend or noop cast.
2764 // If the input is signed, do a sign extend, noop, or truncate.
2765 APSInt Result = Value.extOrTrunc(width: DestWidth);
2766 Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
2767 if (DestType->isBooleanType())
2768 Result = Value.getBoolValue();
2769 return Result;
2770}
2771
2772static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
2773 const FPOptions FPO,
2774 QualType SrcType, const APSInt &Value,
2775 QualType DestType, APFloat &Result) {
2776 Result = APFloat(Info.Ctx.getFloatTypeSemantics(T: DestType), 1);
2777 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2778 APFloat::opStatus St = Result.convertFromAPInt(Input: Value, IsSigned: Value.isSigned(), RM);
2779 return checkFloatingPointResult(Info, E, St);
2780}
2781
2782static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E,
2783 APValue &Value, const FieldDecl *FD) {
2784 assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield");
2785
2786 if (!Value.isInt()) {
2787 // Trying to store a pointer-cast-to-integer into a bitfield.
2788 // FIXME: In this case, we should provide the diagnostic for casting
2789 // a pointer to an integer.
2790 assert(Value.isLValue() && "integral value neither int nor lvalue?");
2791 Info.FFDiag(E);
2792 return false;
2793 }
2794
2795 APSInt &Int = Value.getInt();
2796 unsigned OldBitWidth = Int.getBitWidth();
2797 unsigned NewBitWidth = FD->getBitWidthValue();
2798 if (NewBitWidth < OldBitWidth)
2799 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
2800 return true;
2801}
2802
2803/// Perform the given integer operation, which is known to need at most BitWidth
2804/// bits, and check for overflow in the original type (if that type was not an
2805/// unsigned type).
2806template<typename Operation>
2807static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
2808 const APSInt &LHS, const APSInt &RHS,
2809 unsigned BitWidth, Operation Op,
2810 APSInt &Result) {
2811 if (LHS.isUnsigned()) {
2812 Result = Op(LHS, RHS);
2813 return true;
2814 }
2815
2816 APSInt Value(Op(LHS.extend(width: BitWidth), RHS.extend(width: BitWidth)), false);
2817 Result = Value.trunc(width: LHS.getBitWidth());
2818 if (Result.extend(width: BitWidth) != Value && !E->getType().isWrapType()) {
2819 if (Info.checkingForUndefinedBehavior())
2820 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
2821 DiagID: diag::warn_integer_constant_overflow)
2822 << toString(I: Result, Radix: 10, Signed: Result.isSigned(), /*formatAsCLiteral=*/false,
2823 /*UpperCase=*/true, /*InsertSeparators=*/true)
2824 << E->getType() << E->getSourceRange();
2825 return HandleOverflow(Info, E, SrcValue: Value, DestType: E->getType());
2826 }
2827 return true;
2828}
2829
2830/// Perform the given binary integer operation.
2831static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E,
2832 const APSInt &LHS, BinaryOperatorKind Opcode,
2833 APSInt RHS, APSInt &Result) {
2834 bool HandleOverflowResult = true;
2835 switch (Opcode) {
2836 default:
2837 Info.FFDiag(E);
2838 return false;
2839 case BO_Mul:
2840 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() * 2,
2841 Op: std::multiplies<APSInt>(), Result);
2842 case BO_Add:
2843 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2844 Op: std::plus<APSInt>(), Result);
2845 case BO_Sub:
2846 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2847 Op: std::minus<APSInt>(), Result);
2848 case BO_And: Result = LHS & RHS; return true;
2849 case BO_Xor: Result = LHS ^ RHS; return true;
2850 case BO_Or: Result = LHS | RHS; return true;
2851 case BO_Div:
2852 case BO_Rem:
2853 if (RHS == 0) {
2854 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero)
2855 << E->getRHS()->getSourceRange();
2856 return false;
2857 }
2858 // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports
2859 // this operation and gives the two's complement result.
2860 if (RHS.isNegative() && RHS.isAllOnes() && LHS.isSigned() &&
2861 LHS.isMinSignedValue())
2862 HandleOverflowResult = HandleOverflow(
2863 Info, E, SrcValue: -LHS.extend(width: LHS.getBitWidth() + 1), DestType: E->getType());
2864 Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS);
2865 return HandleOverflowResult;
2866 case BO_Shl: {
2867 if (Info.getLangOpts().OpenCL)
2868 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2869 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2870 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2871 RHS.isUnsigned());
2872 else if (RHS.isSigned() && RHS.isNegative()) {
2873 // During constant-folding, a negative shift is an opposite shift. Such
2874 // a shift is not a constant expression.
2875 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2876 if (!Info.noteUndefinedBehavior())
2877 return false;
2878 RHS = -RHS;
2879 goto shift_right;
2880 }
2881 shift_left:
2882 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
2883 // the shifted type.
2884 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2885 if (SA != RHS) {
2886 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
2887 << RHS << E->getType() << LHS.getBitWidth();
2888 if (!Info.noteUndefinedBehavior())
2889 return false;
2890 } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) {
2891 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
2892 // operand, and must not overflow the corresponding unsigned type.
2893 // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to
2894 // E1 x 2^E2 module 2^N.
2895 if (LHS.isNegative()) {
2896 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_of_negative) << LHS;
2897 if (!Info.noteUndefinedBehavior())
2898 return false;
2899 } else if (LHS.countl_zero() < SA) {
2900 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_discards);
2901 if (!Info.noteUndefinedBehavior())
2902 return false;
2903 }
2904 }
2905 Result = LHS << SA;
2906 return true;
2907 }
2908 case BO_Shr: {
2909 if (Info.getLangOpts().OpenCL)
2910 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2911 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2912 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2913 RHS.isUnsigned());
2914 else if (RHS.isSigned() && RHS.isNegative()) {
2915 // During constant-folding, a negative shift is an opposite shift. Such a
2916 // shift is not a constant expression.
2917 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2918 if (!Info.noteUndefinedBehavior())
2919 return false;
2920 RHS = -RHS;
2921 goto shift_left;
2922 }
2923 shift_right:
2924 // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
2925 // shifted type.
2926 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2927 if (SA != RHS) {
2928 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
2929 << RHS << E->getType() << LHS.getBitWidth();
2930 if (!Info.noteUndefinedBehavior())
2931 return false;
2932 }
2933
2934 Result = LHS >> SA;
2935 return true;
2936 }
2937
2938 case BO_LT: Result = LHS < RHS; return true;
2939 case BO_GT: Result = LHS > RHS; return true;
2940 case BO_LE: Result = LHS <= RHS; return true;
2941 case BO_GE: Result = LHS >= RHS; return true;
2942 case BO_EQ: Result = LHS == RHS; return true;
2943 case BO_NE: Result = LHS != RHS; return true;
2944 case BO_Cmp:
2945 llvm_unreachable("BO_Cmp should be handled elsewhere");
2946 }
2947}
2948
2949/// Perform the given binary floating-point operation, in-place, on LHS.
2950static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E,
2951 APFloat &LHS, BinaryOperatorKind Opcode,
2952 const APFloat &RHS) {
2953 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2954 APFloat::opStatus St;
2955 switch (Opcode) {
2956 default:
2957 Info.FFDiag(E);
2958 return false;
2959 case BO_Mul:
2960 St = LHS.multiply(RHS, RM);
2961 break;
2962 case BO_Add:
2963 St = LHS.add(RHS, RM);
2964 break;
2965 case BO_Sub:
2966 St = LHS.subtract(RHS, RM);
2967 break;
2968 case BO_Div:
2969 // [expr.mul]p4:
2970 // If the second operand of / or % is zero the behavior is undefined.
2971 if (RHS.isZero())
2972 Info.CCEDiag(E, DiagId: diag::note_expr_divide_by_zero);
2973 St = LHS.divide(RHS, RM);
2974 break;
2975 }
2976
2977 // [expr.pre]p4:
2978 // If during the evaluation of an expression, the result is not
2979 // mathematically defined [...], the behavior is undefined.
2980 // FIXME: C++ rules require us to not conform to IEEE 754 here.
2981 if (LHS.isNaN()) {
2982 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << LHS.isNaN();
2983 return Info.noteUndefinedBehavior();
2984 }
2985
2986 return checkFloatingPointResult(Info, E, St);
2987}
2988
2989static bool handleLogicalOpForVector(const APInt &LHSValue,
2990 BinaryOperatorKind Opcode,
2991 const APInt &RHSValue, APInt &Result) {
2992 bool LHS = (LHSValue != 0);
2993 bool RHS = (RHSValue != 0);
2994
2995 if (Opcode == BO_LAnd)
2996 Result = LHS && RHS;
2997 else
2998 Result = LHS || RHS;
2999 return true;
3000}
3001static bool handleLogicalOpForVector(const APFloat &LHSValue,
3002 BinaryOperatorKind Opcode,
3003 const APFloat &RHSValue, APInt &Result) {
3004 bool LHS = !LHSValue.isZero();
3005 bool RHS = !RHSValue.isZero();
3006
3007 if (Opcode == BO_LAnd)
3008 Result = LHS && RHS;
3009 else
3010 Result = LHS || RHS;
3011 return true;
3012}
3013
3014static bool handleLogicalOpForVector(const APValue &LHSValue,
3015 BinaryOperatorKind Opcode,
3016 const APValue &RHSValue, APInt &Result) {
3017 // The result is always an int type, however operands match the first.
3018 if (LHSValue.getKind() == APValue::Int)
3019 return handleLogicalOpForVector(LHSValue: LHSValue.getInt(), Opcode,
3020 RHSValue: RHSValue.getInt(), Result);
3021 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3022 return handleLogicalOpForVector(LHSValue: LHSValue.getFloat(), Opcode,
3023 RHSValue: RHSValue.getFloat(), Result);
3024}
3025
3026template <typename APTy>
3027static bool
3028handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode,
3029 const APTy &RHSValue, APInt &Result) {
3030 switch (Opcode) {
3031 default:
3032 llvm_unreachable("unsupported binary operator");
3033 case BO_EQ:
3034 Result = (LHSValue == RHSValue);
3035 break;
3036 case BO_NE:
3037 Result = (LHSValue != RHSValue);
3038 break;
3039 case BO_LT:
3040 Result = (LHSValue < RHSValue);
3041 break;
3042 case BO_GT:
3043 Result = (LHSValue > RHSValue);
3044 break;
3045 case BO_LE:
3046 Result = (LHSValue <= RHSValue);
3047 break;
3048 case BO_GE:
3049 Result = (LHSValue >= RHSValue);
3050 break;
3051 }
3052
3053 // The boolean operations on these vector types use an instruction that
3054 // results in a mask of '-1' for the 'truth' value. Ensure that we negate 1
3055 // to -1 to make sure that we produce the correct value.
3056 Result.negate();
3057
3058 return true;
3059}
3060
3061static bool handleCompareOpForVector(const APValue &LHSValue,
3062 BinaryOperatorKind Opcode,
3063 const APValue &RHSValue, APInt &Result) {
3064 // The result is always an int type, however operands match the first.
3065 if (LHSValue.getKind() == APValue::Int)
3066 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getInt(), Opcode,
3067 RHSValue: RHSValue.getInt(), Result);
3068 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3069 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getFloat(), Opcode,
3070 RHSValue: RHSValue.getFloat(), Result);
3071}
3072
3073// Perform binary operations for vector types, in place on the LHS.
3074static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E,
3075 BinaryOperatorKind Opcode,
3076 APValue &LHSValue,
3077 const APValue &RHSValue) {
3078 assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI &&
3079 "Operation not supported on vector types");
3080
3081 const auto *VT = E->getType()->castAs<VectorType>();
3082 unsigned NumElements = VT->getNumElements();
3083 QualType EltTy = VT->getElementType();
3084
3085 // In the cases (typically C as I've observed) where we aren't evaluating
3086 // constexpr but are checking for cases where the LHS isn't yet evaluatable,
3087 // just give up.
3088 if (!LHSValue.isVector()) {
3089 assert(LHSValue.isLValue() &&
3090 "A vector result that isn't a vector OR uncalculated LValue");
3091 Info.FFDiag(E);
3092 return false;
3093 }
3094
3095 assert(LHSValue.getVectorLength() == NumElements &&
3096 RHSValue.getVectorLength() == NumElements && "Different vector sizes");
3097
3098 SmallVector<APValue, 4> ResultElements;
3099
3100 for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) {
3101 APValue LHSElt = LHSValue.getVectorElt(I: EltNum);
3102 APValue RHSElt = RHSValue.getVectorElt(I: EltNum);
3103
3104 if (EltTy->isIntegerType()) {
3105 APSInt EltResult{Info.Ctx.getIntWidth(T: EltTy),
3106 EltTy->isUnsignedIntegerType()};
3107 bool Success = true;
3108
3109 if (BinaryOperator::isLogicalOp(Opc: Opcode))
3110 Success = handleLogicalOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3111 else if (BinaryOperator::isComparisonOp(Opc: Opcode))
3112 Success = handleCompareOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3113 else
3114 Success = handleIntIntBinOp(Info, E, LHS: LHSElt.getInt(), Opcode,
3115 RHS: RHSElt.getInt(), Result&: EltResult);
3116
3117 if (!Success) {
3118 Info.FFDiag(E);
3119 return false;
3120 }
3121 ResultElements.emplace_back(Args&: EltResult);
3122
3123 } else if (EltTy->isFloatingType()) {
3124 assert(LHSElt.getKind() == APValue::Float &&
3125 RHSElt.getKind() == APValue::Float &&
3126 "Mismatched LHS/RHS/Result Type");
3127 APFloat LHSFloat = LHSElt.getFloat();
3128
3129 if (!handleFloatFloatBinOp(Info, E, LHS&: LHSFloat, Opcode,
3130 RHS: RHSElt.getFloat())) {
3131 Info.FFDiag(E);
3132 return false;
3133 }
3134
3135 ResultElements.emplace_back(Args&: LHSFloat);
3136 }
3137 }
3138
3139 LHSValue = APValue(ResultElements.data(), ResultElements.size());
3140 return true;
3141}
3142
3143/// Cast an lvalue referring to a base subobject to a derived class, by
3144/// truncating the lvalue's path to the given length.
3145static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
3146 const RecordDecl *TruncatedType,
3147 unsigned TruncatedElements) {
3148 SubobjectDesignator &D = Result.Designator;
3149
3150 // Check we actually point to a derived class object.
3151 if (TruncatedElements == D.Entries.size())
3152 return true;
3153 assert(TruncatedElements >= D.MostDerivedPathLength &&
3154 "not casting to a derived class");
3155 if (!Result.checkSubobject(Info, E, CSK: CSK_Derived))
3156 return false;
3157
3158 // Truncate the path to the subobject, and remove any derived-to-base offsets.
3159 const RecordDecl *RD = TruncatedType;
3160 for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
3161 if (RD->isInvalidDecl()) return false;
3162 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
3163 const CXXRecordDecl *Base = getAsBaseClass(E: D.Entries[I]);
3164 if (isVirtualBaseClass(E: D.Entries[I]))
3165 Result.Offset -= Layout.getVBaseClassOffset(VBase: Base);
3166 else
3167 Result.Offset -= Layout.getBaseClassOffset(Base);
3168 RD = Base;
3169 }
3170 D.Entries.resize(N: TruncatedElements);
3171 return true;
3172}
3173
3174static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3175 const CXXRecordDecl *Derived,
3176 const CXXRecordDecl *Base,
3177 const ASTRecordLayout *RL = nullptr) {
3178 if (!RL) {
3179 if (Derived->isInvalidDecl()) return false;
3180 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3181 }
3182
3183 Obj.addDecl(Info, E, D: Base, /*Virtual=*/false);
3184 Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
3185 return true;
3186}
3187
3188static bool HandleLValueDirectVirtualBase(EvalInfo &Info, const Expr *E,
3189 LValue &Obj,
3190 const CXXRecordDecl *Derived,
3191 const CXXRecordDecl *Base,
3192 const ASTRecordLayout *RL = nullptr) {
3193 if (!RL) {
3194 if (Derived->isInvalidDecl())
3195 return false;
3196 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3197 }
3198
3199 Obj.addDecl(Info, E, D: Base, /*Virtual=*/true);
3200 Obj.getLValueOffset() += RL->getVBaseClassOffset(VBase: Base);
3201 return true;
3202}
3203
3204static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3205 const CXXRecordDecl *DerivedDecl,
3206 const CXXBaseSpecifier *Base) {
3207 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
3208
3209 if (!Base->isVirtual())
3210 return HandleLValueDirectBase(Info, E, Obj, Derived: DerivedDecl, Base: BaseDecl);
3211
3212 SubobjectDesignator &D = Obj.Designator;
3213 if (D.Invalid)
3214 return false;
3215
3216 // Extract most-derived object and corresponding type.
3217 // FIXME: After implementing P2280R4 it became possible to get references
3218 // here. We do MostDerivedType->getAsCXXRecordDecl() in several other
3219 // locations and if we see crashes in those locations in the future
3220 // it may make more sense to move this fix into Lvalue::set.
3221 DerivedDecl = D.MostDerivedType.getNonReferenceType()->getAsCXXRecordDecl();
3222 if (!CastToDerivedClass(Info, E, Result&: Obj, TruncatedType: DerivedDecl, TruncatedElements: D.MostDerivedPathLength))
3223 return false;
3224
3225 // Find the virtual base class.
3226 if (DerivedDecl->isInvalidDecl()) return false;
3227 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: DerivedDecl);
3228 Obj.addDecl(Info, E, D: BaseDecl, /*Virtual*/ true);
3229 Obj.getLValueOffset() += Layout.getVBaseClassOffset(VBase: BaseDecl);
3230 return true;
3231}
3232
3233static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E,
3234 QualType Type, LValue &Result) {
3235 for (CastExpr::path_const_iterator PathI = E->path_begin(),
3236 PathE = E->path_end();
3237 PathI != PathE; ++PathI) {
3238 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Type->getAsCXXRecordDecl(),
3239 Base: *PathI))
3240 return false;
3241 Type = (*PathI)->getType();
3242 }
3243 return true;
3244}
3245
3246/// Cast an lvalue referring to a derived class to a known base subobject.
3247static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result,
3248 const CXXRecordDecl *DerivedRD,
3249 const CXXRecordDecl *BaseRD) {
3250 CXXBasePaths Paths(/*FindAmbiguities=*/false,
3251 /*RecordPaths=*/true, /*DetectVirtual=*/false);
3252 if (!DerivedRD->isDerivedFrom(Base: BaseRD, Paths))
3253 llvm_unreachable("Class must be derived from the passed in base class!");
3254
3255 for (CXXBasePathElement &Elem : Paths.front())
3256 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Elem.Class, Base: Elem.Base))
3257 return false;
3258 return true;
3259}
3260
3261/// Update LVal to refer to the given field, which must be a member of the type
3262/// currently described by LVal.
3263static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
3264 const FieldDecl *FD,
3265 const ASTRecordLayout *RL = nullptr) {
3266 if (!RL) {
3267 const RecordDecl *RD = FD->getParent();
3268 if (RD->isInvalidDecl())
3269 return false;
3270 // There are some cases where the base is not yet complete but we haven't
3271 // disagnosed (such as in a template instantation of an attribute that
3272 // references the expression, ala enable_if). These aren't necessarily
3273 // constant expressions so we return 'false', but they might be, so we don't
3274 // diagnose.
3275 if (!RD->isCompleteDefinition())
3276 return false;
3277 RL = &Info.Ctx.getASTRecordLayout(D: RD);
3278 }
3279
3280 unsigned I = FD->getFieldIndex();
3281 LVal.addDecl(Info, E, D: FD);
3282 LVal.adjustOffset(N: Info.Ctx.toCharUnitsFromBits(BitSize: RL->getFieldOffset(FieldNo: I)));
3283 return true;
3284}
3285
3286/// Update LVal to refer to the given indirect field.
3287static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
3288 LValue &LVal,
3289 const IndirectFieldDecl *IFD) {
3290 for (const auto *C : IFD->chain())
3291 if (!HandleLValueMember(Info, E, LVal, FD: cast<FieldDecl>(Val: C)))
3292 return false;
3293 return true;
3294}
3295
3296enum class SizeOfType {
3297 SizeOf,
3298 DataSizeOf,
3299};
3300
3301/// Get the size of the given type in char units.
3302static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type,
3303 CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) {
3304 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
3305 // extension.
3306 if (Type->isVoidType() || Type->isFunctionType()) {
3307 Size = CharUnits::One();
3308 return true;
3309 }
3310
3311 if (Type->isDependentType()) {
3312 Info.FFDiag(Loc);
3313 return false;
3314 }
3315
3316 if (!Type->isConstantSizeType()) {
3317 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
3318 // FIXME: Better diagnostic.
3319 Info.FFDiag(Loc);
3320 return false;
3321 }
3322
3323 if (SOT == SizeOfType::SizeOf)
3324 Size = Info.Ctx.getTypeSizeInChars(T: Type);
3325 else
3326 Size = Info.Ctx.getTypeInfoDataSizeInChars(T: Type).Width;
3327 return true;
3328}
3329
3330/// Update a pointer value to model pointer arithmetic.
3331/// \param Info - Information about the ongoing evaluation.
3332/// \param E - The expression being evaluated, for diagnostic purposes.
3333/// \param LVal - The pointer value to be updated.
3334/// \param EltTy - The pointee type represented by LVal.
3335/// \param Adjustment - The adjustment, in objects of type EltTy, to add.
3336static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3337 LValue &LVal, QualType EltTy,
3338 APSInt Adjustment) {
3339 CharUnits SizeOfPointee;
3340 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfPointee))
3341 return false;
3342
3343 LVal.adjustOffsetAndIndex(Info, E, Index: Adjustment, ElementSize: SizeOfPointee);
3344 return true;
3345}
3346
3347static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3348 LValue &LVal, QualType EltTy,
3349 int64_t Adjustment) {
3350 return HandleLValueArrayAdjustment(Info, E, LVal, EltTy,
3351 Adjustment: APSInt::get(X: Adjustment));
3352}
3353
3354/// Update an lvalue to refer to a component of a complex number.
3355/// \param Info - Information about the ongoing evaluation.
3356/// \param LVal - The lvalue to be updated.
3357/// \param EltTy - The complex number's component type.
3358/// \param Imag - False for the real component, true for the imaginary.
3359static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E,
3360 LValue &LVal, QualType EltTy,
3361 bool Imag) {
3362 if (Imag) {
3363 CharUnits SizeOfComponent;
3364 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfComponent))
3365 return false;
3366 LVal.Offset += SizeOfComponent;
3367 }
3368 LVal.addComplex(Info, E, EltTy, Imag);
3369 return true;
3370}
3371
3372static bool HandleLValueVectorElement(EvalInfo &Info, const Expr *E,
3373 LValue &LVal, QualType EltTy,
3374 uint64_t Size, uint64_t Idx) {
3375 if (Idx) {
3376 CharUnits SizeOfElement;
3377 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfElement))
3378 return false;
3379 LVal.Offset += SizeOfElement * Idx;
3380 }
3381 LVal.addVectorElement(Info, E, EltTy, Size, Idx);
3382 return true;
3383}
3384
3385/// Try to evaluate the initializer for a variable declaration.
3386///
3387/// \param Info Information about the ongoing evaluation.
3388/// \param E An expression to be used when printing diagnostics.
3389/// \param VD The variable whose initializer should be obtained.
3390/// \param Version The version of the variable within the frame.
3391/// \param Frame The frame in which the variable was created. Must be null
3392/// if this variable is not local to the evaluation.
3393/// \param Result Filled in with a pointer to the value of the variable.
3394static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
3395 const VarDecl *VD, CallStackFrame *Frame,
3396 unsigned Version, APValue *&Result) {
3397 // C++23 [expr.const]p8 If we have a reference type allow unknown references
3398 // and pointers.
3399 bool AllowConstexprUnknown =
3400 Info.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType();
3401
3402 APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version);
3403
3404 auto CheckUninitReference = [&](bool IsLocalVariable) {
3405 if (!Result || (!Result->hasValue() && VD->getType()->isReferenceType())) {
3406 // C++23 [expr.const]p8
3407 // ... For such an object that is not usable in constant expressions, the
3408 // dynamic type of the object is constexpr-unknown. For such a reference
3409 // that is not usable in constant expressions, the reference is treated
3410 // as binding to an unspecified object of the referenced type whose
3411 // lifetime and that of all subobjects includes the entire constant
3412 // evaluation and whose dynamic type is constexpr-unknown.
3413 //
3414 // Variables that are part of the current evaluation are not
3415 // constexpr-unknown.
3416 if (!AllowConstexprUnknown || IsLocalVariable) {
3417 if (!Info.checkingPotentialConstantExpression())
3418 Info.FFDiag(E, DiagId: diag::note_constexpr_use_uninit_reference);
3419 return false;
3420 }
3421 Result = nullptr;
3422 }
3423 return true;
3424 };
3425
3426 // If this is a local variable, dig out its value.
3427 if (Frame) {
3428 Result = Frame->getTemporary(Key: VD, Version);
3429 if (Result)
3430 return CheckUninitReference(/*IsLocalVariable=*/true);
3431
3432 if (!isa<ParmVarDecl>(Val: VD)) {
3433 // Assume variables referenced within a lambda's call operator that were
3434 // not declared within the call operator are captures and during checking
3435 // of a potential constant expression, assume they are unknown constant
3436 // expressions.
3437 assert(isLambdaCallOperator(Frame->Callee) &&
3438 (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) &&
3439 "missing value for local variable");
3440 if (Info.checkingPotentialConstantExpression())
3441 return false;
3442
3443 llvm_unreachable(
3444 "A variable in a frame should either be a local or a parameter");
3445 }
3446 }
3447
3448 // If we're currently evaluating the initializer of this declaration, use that
3449 // in-flight value.
3450 if (Info.EvaluatingDecl == Base) {
3451 Result = Info.EvaluatingDeclValue;
3452 return CheckUninitReference(/*IsLocalVariable=*/false);
3453 }
3454
3455 // P2280R4 struck the restriction that variable of reference type lifetime
3456 // should begin within the evaluation of E
3457 // Used to be C++20 [expr.const]p5.12.2:
3458 // ... its lifetime began within the evaluation of E;
3459 if (isa<ParmVarDecl>(Val: VD)) {
3460 if (AllowConstexprUnknown) {
3461 Result = nullptr;
3462 return true;
3463 }
3464
3465 // Assume parameters of a potential constant expression are usable in
3466 // constant expressions.
3467 if (!Info.checkingPotentialConstantExpression() ||
3468 !Info.CurrentCall->Callee ||
3469 !Info.CurrentCall->Callee->Equals(DC: VD->getDeclContext())) {
3470 if (Info.getLangOpts().CPlusPlus11) {
3471 Info.FFDiag(E, DiagId: diag::note_constexpr_function_param_value_unknown)
3472 << VD;
3473 NoteLValueLocation(Info, Base);
3474 } else {
3475 Info.FFDiag(E);
3476 }
3477 }
3478 return false;
3479 }
3480
3481 if (E->isValueDependent())
3482 return false;
3483
3484 // Dig out the initializer, and use the declaration which it's attached to.
3485 // FIXME: We should eventually check whether the variable has a reachable
3486 // initializing declaration.
3487 const Expr *Init = VD->getAnyInitializer(D&: VD);
3488 // P2280R4 struck the restriction that variable of reference type should have
3489 // a preceding initialization.
3490 // Used to be C++20 [expr.const]p5.12:
3491 // ... reference has a preceding initialization and either ...
3492 if (!Init && !AllowConstexprUnknown) {
3493 // Don't diagnose during potential constant expression checking; an
3494 // initializer might be added later.
3495 if (!Info.checkingPotentialConstantExpression()) {
3496 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_unknown, ExtraNotes: 1)
3497 << VD;
3498 NoteLValueLocation(Info, Base);
3499 }
3500 return false;
3501 }
3502
3503 // P2280R4 struck the initialization requirement for variables of reference
3504 // type so we can no longer assume we have an Init.
3505 // Used to be C++20 [expr.const]p5.12:
3506 // ... reference has a preceding initialization and either ...
3507 if (Init && Init->isValueDependent()) {
3508 // The DeclRefExpr is not value-dependent, but the variable it refers to
3509 // has a value-dependent initializer. This should only happen in
3510 // constant-folding cases, where the variable is not actually of a suitable
3511 // type for use in a constant expression (otherwise the DeclRefExpr would
3512 // have been value-dependent too), so diagnose that.
3513 assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx));
3514 if (!Info.checkingPotentialConstantExpression()) {
3515 Info.FFDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
3516 ? diag::note_constexpr_ltor_non_constexpr
3517 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
3518 << VD << VD->getType();
3519 NoteLValueLocation(Info, Base);
3520 }
3521 return false;
3522 }
3523
3524 // Check that we can fold the initializer. In C++, we will have already done
3525 // this in the cases where it matters for conformance.
3526 // P2280R4 struck the initialization requirement for variables of reference
3527 // type so we can no longer assume we have an Init.
3528 // Used to be C++20 [expr.const]p5.12:
3529 // ... reference has a preceding initialization and either ...
3530 if (Init && !VD->evaluateValue() && !AllowConstexprUnknown) {
3531 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3532 NoteLValueLocation(Info, Base);
3533 return false;
3534 }
3535
3536 // Check that the variable is actually usable in constant expressions. For a
3537 // const integral variable or a reference, we might have a non-constant
3538 // initializer that we can nonetheless evaluate the initializer for. Such
3539 // variables are not usable in constant expressions. In C++98, the
3540 // initializer also syntactically needs to be an ICE.
3541 //
3542 // FIXME: We don't diagnose cases that aren't potentially usable in constant
3543 // expressions here; doing so would regress diagnostics for things like
3544 // reading from a volatile constexpr variable.
3545 if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() &&
3546 VD->mightBeUsableInConstantExpressions(C: Info.Ctx) &&
3547 !AllowConstexprUnknown) ||
3548 ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) &&
3549 !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Context: Info.Ctx))) {
3550 if (Init) {
3551 Info.CCEDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3552 NoteLValueLocation(Info, Base);
3553 } else {
3554 Info.CCEDiag(E);
3555 }
3556 }
3557
3558 // Never use the initializer of a weak variable, not even for constant
3559 // folding. We can't be sure that this is the definition that will be used.
3560 if (VD->isWeak()) {
3561 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_weak) << VD;
3562 NoteLValueLocation(Info, Base);
3563 return false;
3564 }
3565
3566 Result = const_cast<APValue *>(VD->getEvaluatedValue());
3567
3568 if (!Result && !AllowConstexprUnknown)
3569 return false;
3570
3571 return CheckUninitReference(/*IsLocalVariable=*/false);
3572}
3573
3574/// Get the base index of the given base class within an APValue representing
3575/// the given derived class.
3576static unsigned getBaseIndex(const CXXRecordDecl *Derived,
3577 const CXXRecordDecl *Base) {
3578 Base = Base->getCanonicalDecl();
3579 unsigned Index = 0;
3580 for (const CXXBaseSpecifier &B : Derived->bases()) {
3581 if (B.isVirtual())
3582 continue;
3583 if (B.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3584 return Index;
3585 ++Index;
3586 }
3587
3588 for (const CXXBaseSpecifier &B : Derived->vbases()) {
3589 if (B.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3590 return Index;
3591 ++Index;
3592 }
3593
3594 llvm_unreachable("base class missing from derived class's bases list");
3595}
3596
3597/// Extract the value of a character from a string literal.
3598static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
3599 uint64_t Index) {
3600 assert(!isa<SourceLocExpr>(Lit) &&
3601 "SourceLocExpr should have already been converted to a StringLiteral");
3602
3603 // FIXME: Support MakeStringConstant
3604 if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Val: Lit)) {
3605 std::string Str;
3606 Info.Ctx.getObjCEncodingForType(T: ObjCEnc->getEncodedType(), S&: Str);
3607 assert(Index <= Str.size() && "Index too large");
3608 return APSInt::getUnsigned(X: Str.c_str()[Index]);
3609 }
3610
3611 if (auto PE = dyn_cast<PredefinedExpr>(Val: Lit))
3612 Lit = PE->getFunctionName();
3613 const StringLiteral *S = cast<StringLiteral>(Val: Lit);
3614 const ConstantArrayType *CAT =
3615 Info.Ctx.getAsConstantArrayType(T: S->getType());
3616 assert(CAT && "string literal isn't an array");
3617 QualType CharType = CAT->getElementType();
3618 assert(CharType->isIntegerType() && "unexpected character type");
3619 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3620 CharType->isUnsignedIntegerType());
3621 if (Index < S->getLength())
3622 Value = S->getCodeUnit(i: Index);
3623 return Value;
3624}
3625
3626// Expand a string literal into an array of characters.
3627//
3628// FIXME: This is inefficient; we should probably introduce something similar
3629// to the LLVM ConstantDataArray to make this cheaper.
3630static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S,
3631 APValue &Result,
3632 QualType AllocType = QualType()) {
3633 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
3634 T: AllocType.isNull() ? S->getType() : AllocType);
3635 assert(CAT && "string literal isn't an array");
3636 QualType CharType = CAT->getElementType();
3637 assert(CharType->isIntegerType() && "unexpected character type");
3638
3639 unsigned Elts = CAT->getZExtSize();
3640 Result = APValue(APValue::UninitArray(),
3641 std::min(a: S->getLength(), b: Elts), Elts);
3642 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3643 CharType->isUnsignedIntegerType());
3644 if (Result.hasArrayFiller())
3645 Result.getArrayFiller() = APValue(Value);
3646 for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) {
3647 Value = S->getCodeUnit(i: I);
3648 Result.getArrayInitializedElt(I) = APValue(Value);
3649 }
3650}
3651
3652// Expand an array so that it has more than Index filled elements.
3653static void expandArray(APValue &Array, unsigned Index) {
3654 unsigned Size = Array.getArraySize();
3655 assert(Index < Size);
3656
3657 // Always at least double the number of elements for which we store a value.
3658 unsigned OldElts = Array.getArrayInitializedElts();
3659 unsigned NewElts = std::max(a: Index+1, b: OldElts * 2);
3660 NewElts = std::min(a: Size, b: std::max(a: NewElts, b: 8u));
3661
3662 // Copy the data across.
3663 APValue NewValue(APValue::UninitArray(), NewElts, Size);
3664 for (unsigned I = 0; I != OldElts; ++I)
3665 NewValue.getArrayInitializedElt(I).swap(RHS&: Array.getArrayInitializedElt(I));
3666 for (unsigned I = OldElts; I != NewElts; ++I)
3667 NewValue.getArrayInitializedElt(I) = Array.getArrayFiller();
3668 if (NewValue.hasArrayFiller())
3669 NewValue.getArrayFiller() = Array.getArrayFiller();
3670 Array.swap(RHS&: NewValue);
3671}
3672
3673// Expand an indeterminate vector to materialize all elements.
3674static void expandVector(APValue &Vec, unsigned NumElements) {
3675 assert(Vec.isIndeterminate());
3676 SmallVector<APValue, 4> Elts(NumElements, APValue::IndeterminateValue());
3677 Vec = APValue(Elts.data(), Elts.size());
3678}
3679
3680/// Determine whether a type would actually be read by an lvalue-to-rvalue
3681/// conversion. If it's of class type, we may assume that the copy operation
3682/// is trivial. Note that this is never true for a union type with fields
3683/// (because the copy always "reads" the active member) and always true for
3684/// a non-class type.
3685static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD);
3686static bool isReadByLvalueToRvalueConversion(QualType T) {
3687 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3688 return !RD || isReadByLvalueToRvalueConversion(RD);
3689}
3690static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) {
3691 // FIXME: A trivial copy of a union copies the object representation, even if
3692 // the union is empty.
3693 if (RD->isUnion())
3694 return !RD->field_empty();
3695 if (RD->isEmpty())
3696 return false;
3697
3698 for (auto *Field : RD->fields())
3699 if (!Field->isUnnamedBitField() &&
3700 isReadByLvalueToRvalueConversion(T: Field->getType()))
3701 return true;
3702
3703 for (auto &BaseSpec : RD->bases())
3704 if (isReadByLvalueToRvalueConversion(T: BaseSpec.getType()))
3705 return true;
3706
3707 return false;
3708}
3709
3710/// Diagnose an attempt to read from any unreadable field within the specified
3711/// type, which might be a class type.
3712static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK,
3713 QualType T) {
3714 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3715 if (!RD)
3716 return false;
3717
3718 if (!RD->hasMutableFields())
3719 return false;
3720
3721 for (auto *Field : RD->fields()) {
3722 // If we're actually going to read this field in some way, then it can't
3723 // be mutable. If we're in a union, then assigning to a mutable field
3724 // (even an empty one) can change the active member, so that's not OK.
3725 // FIXME: Add core issue number for the union case.
3726 if (Field->isMutable() &&
3727 (RD->isUnion() || isReadByLvalueToRvalueConversion(T: Field->getType()))) {
3728 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1) << AK << Field;
3729 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
3730 return true;
3731 }
3732
3733 if (diagnoseMutableFields(Info, E, AK, T: Field->getType()))
3734 return true;
3735 }
3736
3737 for (auto &BaseSpec : RD->bases())
3738 if (diagnoseMutableFields(Info, E, AK, T: BaseSpec.getType()))
3739 return true;
3740
3741 // All mutable fields were empty, and thus not actually read.
3742 return false;
3743}
3744
3745static bool lifetimeStartedInEvaluation(EvalInfo &Info,
3746 APValue::LValueBase Base,
3747 bool MutableSubobject = false) {
3748 // A temporary or transient heap allocation we created.
3749 if (Base.getCallIndex() || Base.is<DynamicAllocLValue>())
3750 return true;
3751
3752 switch (Info.IsEvaluatingDecl) {
3753 case EvalInfo::EvaluatingDeclKind::None:
3754 return false;
3755
3756 case EvalInfo::EvaluatingDeclKind::Ctor:
3757 // The variable whose initializer we're evaluating.
3758 if (Info.EvaluatingDecl == Base)
3759 return true;
3760
3761 // A temporary lifetime-extended by the variable whose initializer we're
3762 // evaluating.
3763 if (auto *BaseE = Base.dyn_cast<const Expr *>())
3764 if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(Val: BaseE))
3765 return Info.EvaluatingDecl == BaseMTE->getExtendingDecl();
3766 return false;
3767
3768 case EvalInfo::EvaluatingDeclKind::Dtor:
3769 // C++2a [expr.const]p6:
3770 // [during constant destruction] the lifetime of a and its non-mutable
3771 // subobjects (but not its mutable subobjects) [are] considered to start
3772 // within e.
3773 if (MutableSubobject || Base != Info.EvaluatingDecl)
3774 return false;
3775 // FIXME: We can meaningfully extend this to cover non-const objects, but
3776 // we will need special handling: we should be able to access only
3777 // subobjects of such objects that are themselves declared const.
3778 QualType T = getType(B: Base);
3779 return T.isConstQualified() || T->isReferenceType();
3780 }
3781
3782 llvm_unreachable("unknown evaluating decl kind");
3783}
3784
3785static bool CheckArraySize(EvalInfo &Info, const ConstantArrayType *CAT,
3786 SourceLocation CallLoc = {}) {
3787 return Info.CheckArraySize(
3788 Loc: CAT->getSizeExpr() ? CAT->getSizeExpr()->getBeginLoc() : CallLoc,
3789 BitWidth: CAT->getNumAddressingBits(Context: Info.Ctx), ElemCount: CAT->getZExtSize(),
3790 /*Diag=*/true);
3791}
3792
3793static bool handleScalarCast(EvalInfo &Info, const FPOptions FPO, const Expr *E,
3794 QualType SourceTy, QualType DestTy,
3795 APValue const &Original, APValue &Result) {
3796 // boolean must be checked before integer
3797 // since IsIntegerType() is true for bool
3798 if (SourceTy->isBooleanType()) {
3799 if (DestTy->isBooleanType()) {
3800 Result = Original;
3801 return true;
3802 }
3803 if (DestTy->isIntegerType() || DestTy->isRealFloatingType()) {
3804 bool BoolResult;
3805 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3806 return false;
3807 uint64_t IntResult = BoolResult;
3808 QualType IntType = DestTy->isIntegerType()
3809 ? DestTy
3810 : Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false);
3811 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: IntType));
3812 }
3813 if (DestTy->isRealFloatingType()) {
3814 APValue Result2 = APValue(APFloat(0.0));
3815 if (!HandleIntToFloatCast(Info, E, FPO,
3816 SrcType: Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false),
3817 Value: Result.getInt(), DestType: DestTy, Result&: Result2.getFloat()))
3818 return false;
3819 Result = std::move(Result2);
3820 }
3821 return true;
3822 }
3823 if (SourceTy->isIntegerType()) {
3824 if (DestTy->isRealFloatingType()) {
3825 Result = APValue(APFloat(0.0));
3826 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
3827 DestType: DestTy, Result&: Result.getFloat());
3828 }
3829 if (DestTy->isBooleanType()) {
3830 bool BoolResult;
3831 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3832 return false;
3833 uint64_t IntResult = BoolResult;
3834 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3835 return true;
3836 }
3837 if (DestTy->isIntegerType()) {
3838 Result = APValue(
3839 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
3840 return true;
3841 }
3842 } else if (SourceTy->isRealFloatingType()) {
3843 if (DestTy->isRealFloatingType()) {
3844 Result = Original;
3845 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
3846 Result&: Result.getFloat());
3847 }
3848 if (DestTy->isBooleanType()) {
3849 bool BoolResult;
3850 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3851 return false;
3852 uint64_t IntResult = BoolResult;
3853 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3854 return true;
3855 }
3856 if (DestTy->isIntegerType()) {
3857 Result = APValue(APSInt());
3858 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
3859 DestType: DestTy, Result&: Result.getInt());
3860 }
3861 }
3862
3863 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
3864 return false;
3865}
3866
3867// do the heavy lifting for casting to aggregate types
3868// because we have to deal with bitfields specially
3869static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,
3870 const Expr *E, APValue &Result,
3871 QualType ResultType,
3872 SmallVectorImpl<APValue> &Elements,
3873 SmallVectorImpl<QualType> &ElTypes) {
3874
3875 SmallVector<std::tuple<APValue *, QualType, unsigned>> WorkList = {
3876 {&Result, ResultType, 0}};
3877
3878 unsigned ElI = 0;
3879 while (!WorkList.empty() && ElI < Elements.size()) {
3880 auto [Res, Type, BitWidth] = WorkList.pop_back_val();
3881
3882 if (Type->isRealFloatingType()) {
3883 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3884 Result&: *Res))
3885 return false;
3886 ElI++;
3887 continue;
3888 }
3889 if (Type->isIntegerType()) {
3890 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3891 Result&: *Res))
3892 return false;
3893 if (BitWidth > 0) {
3894 if (!Res->isInt())
3895 return false;
3896 APSInt &Int = Res->getInt();
3897 unsigned OldBitWidth = Int.getBitWidth();
3898 unsigned NewBitWidth = BitWidth;
3899 if (NewBitWidth < OldBitWidth)
3900 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
3901 }
3902 ElI++;
3903 continue;
3904 }
3905 if (Type->isVectorType()) {
3906 QualType ElTy = Type->castAs<VectorType>()->getElementType();
3907 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
3908 SmallVector<APValue> Vals(NumEl);
3909 for (unsigned I = 0; I < NumEl; ++I) {
3910 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: ElTy, Original: Elements[ElI],
3911 Result&: Vals[I]))
3912 return false;
3913 ElI++;
3914 }
3915 *Res = APValue(Vals.data(), NumEl);
3916 continue;
3917 }
3918 if (Type->isConstantArrayType()) {
3919 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
3920 ->getElementType();
3921 uint64_t Size =
3922 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
3923 *Res = APValue(APValue::UninitArray(), Size, Size);
3924 for (int64_t I = Size - 1; I > -1; --I)
3925 WorkList.emplace_back(Args: &Res->getArrayInitializedElt(I), Args&: ElTy, Args: 0u);
3926 continue;
3927 }
3928 if (Type->isRecordType()) {
3929 const RecordDecl *RD = Type->getAsRecordDecl();
3930
3931 unsigned NumBases = 0;
3932 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
3933 NumBases = CXXRD->getNumBases();
3934
3935 *Res = APValue(APValue::UninitStruct(), NumBases, RD->getNumFields());
3936
3937 SmallVector<std::tuple<APValue *, QualType, unsigned>> ReverseList;
3938 // we need to traverse backwards
3939 // Visit the base classes.
3940 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
3941 if (CXXRD->getNumBases() > 0) {
3942 assert(CXXRD->getNumBases() == 1);
3943 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
3944 ReverseList.emplace_back(Args: &Res->getStructBase(i: 0), Args: BS.getType(), Args: 0u);
3945 }
3946 }
3947
3948 // Visit the fields.
3949 for (FieldDecl *FD : RD->fields()) {
3950 unsigned FDBW = 0;
3951 if (FD->isUnnamedBitField())
3952 continue;
3953 if (FD->isBitField()) {
3954 FDBW = FD->getBitWidthValue();
3955 }
3956
3957 ReverseList.emplace_back(Args: &Res->getStructField(i: FD->getFieldIndex()),
3958 Args: FD->getType(), Args&: FDBW);
3959 }
3960
3961 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
3962 llvm::append_range(C&: WorkList, R&: ReverseList);
3963 continue;
3964 }
3965 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
3966 return false;
3967 }
3968 return true;
3969}
3970
3971static bool handleElementwiseCast(EvalInfo &Info, const Expr *E,
3972 const FPOptions FPO,
3973 SmallVectorImpl<APValue> &Elements,
3974 SmallVectorImpl<QualType> &SrcTypes,
3975 SmallVectorImpl<QualType> &DestTypes,
3976 SmallVectorImpl<APValue> &Results) {
3977
3978 assert((Elements.size() == SrcTypes.size()) &&
3979 (Elements.size() == DestTypes.size()));
3980
3981 for (unsigned I = 0, ESz = Elements.size(); I < ESz; ++I) {
3982 APValue Original = Elements[I];
3983 QualType SourceTy = SrcTypes[I];
3984 QualType DestTy = DestTypes[I];
3985
3986 if (!handleScalarCast(Info, FPO, E, SourceTy, DestTy, Original, Result&: Results[I]))
3987 return false;
3988 }
3989 return true;
3990}
3991
3992static unsigned elementwiseSize(EvalInfo &Info, QualType BaseTy) {
3993
3994 SmallVector<QualType> WorkList = {BaseTy};
3995
3996 unsigned Size = 0;
3997 while (!WorkList.empty()) {
3998 QualType Type = WorkList.pop_back_val();
3999 if (Type->isRealFloatingType() || Type->isIntegerType() ||
4000 Type->isBooleanType()) {
4001 ++Size;
4002 continue;
4003 }
4004 if (Type->isVectorType()) {
4005 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
4006 Size += NumEl;
4007 continue;
4008 }
4009 if (Type->isConstantMatrixType()) {
4010 unsigned NumEl =
4011 Type->castAs<ConstantMatrixType>()->getNumElementsFlattened();
4012 Size += NumEl;
4013 continue;
4014 }
4015 if (Type->isConstantArrayType()) {
4016 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4017 ->getElementType();
4018 uint64_t ArrSize =
4019 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
4020 for (uint64_t I = 0; I < ArrSize; ++I) {
4021 WorkList.push_back(Elt: ElTy);
4022 }
4023 continue;
4024 }
4025 if (Type->isRecordType()) {
4026 const RecordDecl *RD = Type->getAsRecordDecl();
4027
4028 // Visit the base classes.
4029 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4030 if (CXXRD->getNumBases() > 0) {
4031 assert(CXXRD->getNumBases() == 1);
4032 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4033 WorkList.push_back(Elt: BS.getType());
4034 }
4035 }
4036
4037 // visit the fields.
4038 for (FieldDecl *FD : RD->fields()) {
4039 if (FD->isUnnamedBitField())
4040 continue;
4041 WorkList.push_back(Elt: FD->getType());
4042 }
4043 continue;
4044 }
4045 }
4046 return Size;
4047}
4048
4049static bool hlslAggSplatHelper(EvalInfo &Info, const Expr *E, APValue &SrcVal,
4050 QualType &SrcTy) {
4051 SrcTy = E->getType();
4052
4053 if (!Evaluate(Result&: SrcVal, Info, E))
4054 return false;
4055
4056 assert((SrcVal.isFloat() || SrcVal.isInt() ||
4057 (SrcVal.isVector() && SrcVal.getVectorLength() == 1)) &&
4058 "Not a valid HLSLAggregateSplatCast.");
4059
4060 if (SrcVal.isVector()) {
4061 assert(SrcTy->isVectorType() && "Type mismatch.");
4062 SrcTy = SrcTy->castAs<VectorType>()->getElementType();
4063 SrcVal = SrcVal.getVectorElt(I: 0);
4064 }
4065 if (SrcVal.isMatrix()) {
4066 assert(SrcTy->isConstantMatrixType() && "Type mismatch.");
4067 SrcTy = SrcTy->castAs<ConstantMatrixType>()->getElementType();
4068 SrcVal = SrcVal.getMatrixElt(Row: 0, Col: 0);
4069 }
4070 return true;
4071}
4072
4073static bool flattenAPValue(EvalInfo &Info, const Expr *E, APValue Value,
4074 QualType BaseTy, SmallVectorImpl<APValue> &Elements,
4075 SmallVectorImpl<QualType> &Types, unsigned Size) {
4076
4077 SmallVector<std::pair<APValue, QualType>> WorkList = {{Value, BaseTy}};
4078 unsigned Populated = 0;
4079 while (!WorkList.empty() && Populated < Size) {
4080 auto [Work, Type] = WorkList.pop_back_val();
4081
4082 if (Work.isFloat() || Work.isInt()) {
4083 Elements.push_back(Elt: Work);
4084 Types.push_back(Elt: Type);
4085 Populated++;
4086 continue;
4087 }
4088 if (Work.isVector()) {
4089 assert(Type->isVectorType() && "Type mismatch.");
4090 QualType ElTy = Type->castAs<VectorType>()->getElementType();
4091 for (unsigned I = 0; I < Work.getVectorLength() && Populated < Size;
4092 I++) {
4093 Elements.push_back(Elt: Work.getVectorElt(I));
4094 Types.push_back(Elt: ElTy);
4095 Populated++;
4096 }
4097 continue;
4098 }
4099 if (Work.isMatrix()) {
4100 assert(Type->isConstantMatrixType() && "Type mismatch.");
4101 const auto *MT = Type->castAs<ConstantMatrixType>();
4102 QualType ElTy = MT->getElementType();
4103 // Matrix elements are flattened in row-major order.
4104 for (unsigned Row = 0; Row < Work.getMatrixNumRows() && Populated < Size;
4105 Row++) {
4106 for (unsigned Col = 0;
4107 Col < Work.getMatrixNumColumns() && Populated < Size; Col++) {
4108 Elements.push_back(Elt: Work.getMatrixElt(Row, Col));
4109 Types.push_back(Elt: ElTy);
4110 Populated++;
4111 }
4112 }
4113 continue;
4114 }
4115 if (Work.isArray()) {
4116 assert(Type->isConstantArrayType() && "Type mismatch.");
4117 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4118 ->getElementType();
4119 for (int64_t I = Work.getArraySize() - 1; I > -1; --I) {
4120 WorkList.emplace_back(Args&: Work.getArrayInitializedElt(I), Args&: ElTy);
4121 }
4122 continue;
4123 }
4124
4125 if (Work.isStruct()) {
4126 assert(Type->isRecordType() && "Type mismatch.");
4127
4128 const RecordDecl *RD = Type->getAsRecordDecl();
4129
4130 SmallVector<std::pair<APValue, QualType>> ReverseList;
4131 // Visit the fields.
4132 for (FieldDecl *FD : RD->fields()) {
4133 if (FD->isUnnamedBitField())
4134 continue;
4135 ReverseList.emplace_back(Args&: Work.getStructField(i: FD->getFieldIndex()),
4136 Args: FD->getType());
4137 }
4138
4139 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
4140 llvm::append_range(C&: WorkList, R&: ReverseList);
4141
4142 // Visit the base classes.
4143 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4144 if (CXXRD->getNumBases() > 0) {
4145 assert(CXXRD->getNumBases() == 1);
4146 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4147 const APValue &Base = Work.getStructBase(i: 0);
4148
4149 // Can happen in error cases.
4150 if (!Base.isStruct())
4151 return false;
4152
4153 WorkList.emplace_back(Args: Base, Args: BS.getType());
4154 }
4155 }
4156 continue;
4157 }
4158 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4159 return false;
4160 }
4161 return true;
4162}
4163
4164namespace {
4165/// A handle to a complete object (an object that is not a subobject of
4166/// another object).
4167struct CompleteObject {
4168 /// The identity of the object.
4169 APValue::LValueBase Base;
4170 /// The value of the complete object.
4171 APValue *Value;
4172 /// The type of the complete object.
4173 QualType Type;
4174
4175 CompleteObject() : Value(nullptr) {}
4176 CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type)
4177 : Base(Base), Value(Value), Type(Type) {}
4178
4179 bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const {
4180 // If this isn't a "real" access (eg, if it's just accessing the type
4181 // info), allow it. We assume the type doesn't change dynamically for
4182 // subobjects of constexpr objects (even though we'd hit UB here if it
4183 // did). FIXME: Is this right?
4184 if (!isAnyAccess(AK))
4185 return true;
4186
4187 // In C++14 onwards, it is permitted to read a mutable member whose
4188 // lifetime began within the evaluation.
4189 // FIXME: Should we also allow this in C++11?
4190 if (!Info.getLangOpts().CPlusPlus14 &&
4191 AK != AccessKinds::AK_IsWithinLifetime)
4192 return false;
4193 return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true);
4194 }
4195
4196 explicit operator bool() const { return !Type.isNull(); }
4197};
4198} // end anonymous namespace
4199
4200static QualType getSubobjectType(QualType ObjType, QualType SubobjType,
4201 bool IsMutable = false) {
4202 // C++ [basic.type.qualifier]p1:
4203 // - A const object is an object of type const T or a non-mutable subobject
4204 // of a const object.
4205 if (ObjType.isConstQualified() && !IsMutable)
4206 SubobjType.addConst();
4207 // - A volatile object is an object of type const T or a subobject of a
4208 // volatile object.
4209 if (ObjType.isVolatileQualified())
4210 SubobjType.addVolatile();
4211 return SubobjType;
4212}
4213
4214/// Find the designated sub-object of an rvalue.
4215template <typename SubobjectHandler>
4216static typename SubobjectHandler::result_type
4217findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
4218 const SubobjectDesignator &Sub, SubobjectHandler &handler) {
4219 if (Sub.Invalid)
4220 // A diagnostic will have already been produced.
4221 return handler.failed();
4222 if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) {
4223 if (Info.getLangOpts().CPlusPlus11)
4224 Info.FFDiag(E, DiagId: Sub.isOnePastTheEnd()
4225 ? diag::note_constexpr_access_past_end
4226 : diag::note_constexpr_access_unsized_array)
4227 << handler.AccessKind;
4228 else
4229 Info.FFDiag(E);
4230 return handler.failed();
4231 }
4232
4233 APValue *O = Obj.Value;
4234 QualType ObjType = Obj.Type;
4235 const FieldDecl *LastField = nullptr;
4236 const FieldDecl *VolatileField = nullptr;
4237
4238 // Walk the designator's path to find the subobject.
4239 for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) {
4240 // Reading an indeterminate value is undefined, but assigning over one is OK.
4241 if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) ||
4242 (O->isIndeterminate() &&
4243 !isValidIndeterminateAccess(handler.AccessKind))) {
4244 // Object has ended lifetime.
4245 // If I is non-zero, some subobject (member or array element) of a
4246 // complete object has ended its lifetime, so this is valid for
4247 // IsWithinLifetime, resulting in false.
4248 if (I != 0 && handler.AccessKind == AK_IsWithinLifetime)
4249 return false;
4250 if (!Info.checkingPotentialConstantExpression()) {
4251 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
4252 << handler.AccessKind << O->isIndeterminate()
4253 << E->getSourceRange();
4254 NoteLValueLocation(Info, Base: Obj.Base);
4255 }
4256 return handler.failed();
4257 }
4258
4259 // C++ [class.ctor]p5, C++ [class.dtor]p5:
4260 // const and volatile semantics are not applied on an object under
4261 // {con,de}struction.
4262 if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) &&
4263 ObjType->isRecordType() &&
4264 Info.isEvaluatingCtorDtor(
4265 Base: Obj.Base, Path: ArrayRef(Sub.Entries.begin(), Sub.Entries.begin() + I)) !=
4266 ConstructionPhase::None) {
4267 ObjType = Info.Ctx.getCanonicalType(T: ObjType);
4268 ObjType.removeLocalConst();
4269 ObjType.removeLocalVolatile();
4270 }
4271
4272 // If this is our last pass, check that the final object type is OK.
4273 if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) {
4274 // Accesses to volatile objects are prohibited.
4275 if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) {
4276 if (Info.getLangOpts().CPlusPlus) {
4277 int DiagKind;
4278 SourceLocation Loc;
4279 const NamedDecl *Decl = nullptr;
4280 if (VolatileField) {
4281 DiagKind = 2;
4282 Loc = VolatileField->getLocation();
4283 Decl = VolatileField;
4284 } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) {
4285 DiagKind = 1;
4286 Loc = VD->getLocation();
4287 Decl = VD;
4288 } else {
4289 DiagKind = 0;
4290 if (auto *E = Obj.Base.dyn_cast<const Expr *>())
4291 Loc = E->getExprLoc();
4292 }
4293 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_obj, ExtraNotes: 1)
4294 << handler.AccessKind << DiagKind << Decl;
4295 Info.Note(Loc, DiagId: diag::note_constexpr_volatile_here) << DiagKind;
4296 } else {
4297 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4298 }
4299 return handler.failed();
4300 }
4301
4302 // If we are reading an object of class type, there may still be more
4303 // things we need to check: if there are any mutable subobjects, we
4304 // cannot perform this read. (This only happens when performing a trivial
4305 // copy or assignment.)
4306 if (ObjType->isRecordType() &&
4307 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind) &&
4308 diagnoseMutableFields(Info, E, handler.AccessKind, ObjType))
4309 return handler.failed();
4310 }
4311
4312 if (I == N) {
4313 if (!handler.found(*O, ObjType, Obj.Base))
4314 return false;
4315
4316 // If we modified a bit-field, truncate it to the right width.
4317 if (isModification(handler.AccessKind) &&
4318 LastField && LastField->isBitField() &&
4319 !truncateBitfieldValue(Info, E, Value&: *O, FD: LastField))
4320 return false;
4321
4322 return true;
4323 }
4324
4325 LastField = nullptr;
4326
4327 // The value of an atomic object is represented like a value of the
4328 // underlying type, so look through the _Atomic wrapper.
4329 if (const AtomicType *AT = ObjType->getAs<AtomicType>())
4330 ObjType = Info.Ctx.getQualifiedType(T: AT->getValueType(),
4331 Qs: ObjType.getQualifiers());
4332
4333 if (ObjType->isArrayType()) {
4334 // Next subobject is an array element.
4335 const ArrayType *AT = Info.Ctx.getAsArrayType(T: ObjType);
4336 assert((isa<ConstantArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
4337 "vla in literal type?");
4338 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4339 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4340 CAT && CAT->getSize().ule(RHS: Index)) {
4341 // Note, it should not be possible to form a pointer with a valid
4342 // designator which points more than one past the end of the array.
4343 if (Info.getLangOpts().CPlusPlus11)
4344 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4345 << handler.AccessKind;
4346 else
4347 Info.FFDiag(E);
4348 return handler.failed();
4349 }
4350
4351 ObjType = AT->getElementType();
4352
4353 if (O->getArrayInitializedElts() > Index)
4354 O = &O->getArrayInitializedElt(I: Index);
4355 else if (!isRead(handler.AccessKind)) {
4356 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4357 CAT && !CheckArraySize(Info, CAT, CallLoc: E->getExprLoc()))
4358 return handler.failed();
4359
4360 expandArray(Array&: *O, Index);
4361 O = &O->getArrayInitializedElt(I: Index);
4362 } else
4363 O = &O->getArrayFiller();
4364 } else if (ObjType->isAnyComplexType()) {
4365 // Next subobject is a complex number.
4366 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4367 if (Index > 1) {
4368 if (Info.getLangOpts().CPlusPlus11)
4369 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4370 << handler.AccessKind;
4371 else
4372 Info.FFDiag(E);
4373 return handler.failed();
4374 }
4375
4376 ObjType = getSubobjectType(
4377 ObjType, SubobjType: ObjType->castAs<ComplexType>()->getElementType());
4378
4379 assert(I == N - 1 && "extracting subobject of scalar?");
4380 if (O->isComplexInt()) {
4381 return handler.found(Index ? O->getComplexIntImag()
4382 : O->getComplexIntReal(), ObjType);
4383 } else {
4384 assert(O->isComplexFloat());
4385 return handler.found(Index ? O->getComplexFloatImag()
4386 : O->getComplexFloatReal(), ObjType);
4387 }
4388 } else if (const auto *VT = ObjType->getAs<VectorType>()) {
4389 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4390 unsigned NumElements = VT->getNumElements();
4391 if (Index == NumElements) {
4392 if (Info.getLangOpts().CPlusPlus11)
4393 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4394 << handler.AccessKind;
4395 else
4396 Info.FFDiag(E);
4397 return handler.failed();
4398 }
4399
4400 if (Index > NumElements) {
4401 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
4402 << Index << /*array*/ 0 << NumElements;
4403 return handler.failed();
4404 }
4405
4406 ObjType = VT->getElementType();
4407 assert(I == N - 1 && "extracting subobject of scalar?");
4408
4409 if (O->isIndeterminate()) {
4410 if (isRead(handler.AccessKind)) {
4411 Info.FFDiag(E);
4412 return handler.failed();
4413 }
4414 expandVector(Vec&: *O, NumElements);
4415 }
4416 assert(O->isVector() && "unexpected object during vector element access");
4417 return handler.found(O->getVectorElt(I: Index), ObjType, Obj.Base);
4418 } else if (const FieldDecl *Field = getAsField(E: Sub.Entries[I])) {
4419 if (Field->isMutable() &&
4420 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind)) {
4421 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1)
4422 << handler.AccessKind << Field;
4423 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
4424 return handler.failed();
4425 }
4426
4427 // Next subobject is a class, struct or union field.
4428 RecordDecl *RD = ObjType->castAsCanonical<RecordType>()->getDecl();
4429 if (RD->isUnion()) {
4430 const FieldDecl *UnionField = O->getUnionField();
4431 if (!UnionField ||
4432 UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {
4433 if (I == N - 1 && handler.AccessKind == AK_Construct) {
4434 // Placement new onto an inactive union member makes it active.
4435 O->setUnion(Field, Value: APValue());
4436 } else {
4437 // Pointer to/into inactive union member: Not within lifetime
4438 if (handler.AccessKind == AK_IsWithinLifetime)
4439 return false;
4440 // FIXME: If O->getUnionValue() is absent, report that there's no
4441 // active union member rather than reporting the prior active union
4442 // member. We'll need to fix nullptr_t to not use APValue() as its
4443 // representation first.
4444 Info.FFDiag(E, DiagId: diag::note_constexpr_access_inactive_union_member)
4445 << handler.AccessKind << Field << !UnionField << UnionField;
4446 return handler.failed();
4447 }
4448 }
4449 O = &O->getUnionValue();
4450 } else
4451 O = &O->getStructField(i: Field->getFieldIndex());
4452
4453 ObjType = getSubobjectType(ObjType, SubobjType: Field->getType(), IsMutable: Field->isMutable());
4454 LastField = Field;
4455 if (Field->getType().isVolatileQualified())
4456 VolatileField = Field;
4457 } else {
4458 // Next subobject is a base class.
4459 const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();
4460 const CXXRecordDecl *Base = getAsBaseClass(E: Sub.Entries[I]);
4461
4462 unsigned BaseIndex = getBaseIndex(Derived, Base);
4463 unsigned NumNonVirtualBases = O->getStructNumBases();
4464 if (BaseIndex >= NumNonVirtualBases) {
4465 O = &O->getStructVirtualBase(i: BaseIndex - NumNonVirtualBases);
4466 } else
4467 O = &O->getStructBase(i: BaseIndex);
4468
4469 ObjType = getSubobjectType(ObjType, SubobjType: Info.Ctx.getCanonicalTagType(TD: Base));
4470 }
4471 }
4472}
4473
4474namespace {
4475struct ExtractSubobjectHandler {
4476 EvalInfo &Info;
4477 const Expr *E;
4478 APValue &Result;
4479 const AccessKinds AccessKind;
4480
4481 typedef bool result_type;
4482 bool failed() { return false; }
4483 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4484 Result = Subobj;
4485 if (AccessKind == AK_ReadObjectRepresentation)
4486 return true;
4487 return CheckFullyInitialized(Info, DiagLoc: E->getExprLoc(), Type: SubobjType, Value: Result);
4488 }
4489 bool found(APSInt &Value, QualType SubobjType) {
4490 Result = APValue(Value);
4491 return true;
4492 }
4493 bool found(APFloat &Value, QualType SubobjType) {
4494 Result = APValue(Value);
4495 return true;
4496 }
4497};
4498} // end anonymous namespace
4499
4500/// Extract the designated sub-object of an rvalue.
4501static bool extractSubobject(EvalInfo &Info, const Expr *E,
4502 const CompleteObject &Obj,
4503 const SubobjectDesignator &Sub, APValue &Result,
4504 AccessKinds AK = AK_Read) {
4505 assert(AK == AK_Read || AK == AK_ReadObjectRepresentation);
4506 ExtractSubobjectHandler Handler = {.Info: Info, .E: E, .Result: Result, .AccessKind: AK};
4507 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4508}
4509
4510namespace {
4511struct ModifySubobjectHandler {
4512 EvalInfo &Info;
4513 APValue &NewVal;
4514 const Expr *E;
4515
4516 typedef bool result_type;
4517 static const AccessKinds AccessKind = AK_Assign;
4518
4519 bool checkConst(QualType QT) {
4520 // Assigning to a const object has undefined behavior.
4521 if (QT.isConstQualified()) {
4522 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
4523 return false;
4524 }
4525 return true;
4526 }
4527
4528 bool failed() { return false; }
4529 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4530 if (!checkConst(QT: SubobjType))
4531 return false;
4532 // We've been given ownership of NewVal, so just swap it in.
4533 Subobj.swap(RHS&: NewVal);
4534 return true;
4535 }
4536 bool found(APSInt &Value, QualType SubobjType) {
4537 if (!checkConst(QT: SubobjType))
4538 return false;
4539 if (!NewVal.isInt()) {
4540 // Maybe trying to write a cast pointer value into a complex?
4541 Info.FFDiag(E);
4542 return false;
4543 }
4544 Value = NewVal.getInt();
4545 return true;
4546 }
4547 bool found(APFloat &Value, QualType SubobjType) {
4548 if (!checkConst(QT: SubobjType))
4549 return false;
4550 Value = NewVal.getFloat();
4551 return true;
4552 }
4553};
4554} // end anonymous namespace
4555
4556const AccessKinds ModifySubobjectHandler::AccessKind;
4557
4558/// Update the designated sub-object of an rvalue to the given value.
4559static bool modifySubobject(EvalInfo &Info, const Expr *E,
4560 const CompleteObject &Obj,
4561 const SubobjectDesignator &Sub,
4562 APValue &NewVal) {
4563 ModifySubobjectHandler Handler = { .Info: Info, .NewVal: NewVal, .E: E };
4564 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4565}
4566
4567/// Find the position where two subobject designators diverge, or equivalently
4568/// the length of the common initial subsequence.
4569static unsigned FindDesignatorMismatch(QualType ObjType,
4570 const SubobjectDesignator &A,
4571 const SubobjectDesignator &B,
4572 bool &WasArrayIndex) {
4573 unsigned I = 0, N = std::min(a: A.Entries.size(), b: B.Entries.size());
4574 for (/**/; I != N; ++I) {
4575 if (!ObjType.isNull() &&
4576 (ObjType->isArrayType() || ObjType->isAnyComplexType())) {
4577 // Next subobject is an array element.
4578 if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) {
4579 WasArrayIndex = true;
4580 return I;
4581 }
4582 if (ObjType->isAnyComplexType())
4583 ObjType = ObjType->castAs<ComplexType>()->getElementType();
4584 else
4585 ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();
4586 } else {
4587 if (A.Entries[I].getAsBaseOrMember() !=
4588 B.Entries[I].getAsBaseOrMember()) {
4589 WasArrayIndex = false;
4590 return I;
4591 }
4592 if (const FieldDecl *FD = getAsField(E: A.Entries[I]))
4593 // Next subobject is a field.
4594 ObjType = FD->getType();
4595 else
4596 // Next subobject is a base class.
4597 ObjType = QualType();
4598 }
4599 }
4600 WasArrayIndex = false;
4601 return I;
4602}
4603
4604/// Determine whether the given subobject designators refer to elements of the
4605/// same array object.
4606static bool AreElementsOfSameArray(QualType ObjType,
4607 const SubobjectDesignator &A,
4608 const SubobjectDesignator &B) {
4609 if (A.Entries.size() != B.Entries.size())
4610 return false;
4611
4612 bool IsArray = A.MostDerivedIsArrayElement;
4613 if (IsArray && A.MostDerivedPathLength != A.Entries.size())
4614 // A is a subobject of the array element.
4615 return false;
4616
4617 // If A (and B) designates an array element, the last entry will be the array
4618 // index. That doesn't have to match. Otherwise, we're in the 'implicit array
4619 // of length 1' case, and the entire path must match.
4620 bool WasArrayIndex;
4621 unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);
4622 return CommonLength >= A.Entries.size() - IsArray;
4623}
4624
4625/// Find the complete object to which an LValue refers.
4626static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
4627 AccessKinds AK, const LValue &LVal,
4628 QualType LValType) {
4629 if (LVal.InvalidBase) {
4630 Info.FFDiag(E);
4631 return CompleteObject();
4632 }
4633
4634 if (!LVal.Base) {
4635 if (AK == AccessKinds::AK_Dereference)
4636 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
4637 else
4638 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
4639 return CompleteObject();
4640 }
4641
4642 CallStackFrame *Frame = nullptr;
4643 unsigned Depth = 0;
4644 if (LVal.getLValueCallIndex()) {
4645 std::tie(args&: Frame, args&: Depth) =
4646 Info.getCallFrameAndDepth(CallIndex: LVal.getLValueCallIndex());
4647 if (!Frame) {
4648 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit, ExtraNotes: 1)
4649 << AK << /*Indeterminate=*/false << E->getSourceRange();
4650 NoteLValueLocation(Info, Base: LVal.Base);
4651 return CompleteObject();
4652 }
4653 }
4654
4655 bool IsAccess = isAnyAccess(AK);
4656
4657 // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type
4658 // is not a constant expression (even if the object is non-volatile). We also
4659 // apply this rule to C++98, in order to conform to the expected 'volatile'
4660 // semantics.
4661 if (isFormalAccess(AK) && LValType.isVolatileQualified()) {
4662 if (Info.getLangOpts().CPlusPlus)
4663 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_type)
4664 << AK << LValType;
4665 else
4666 Info.FFDiag(E);
4667 return CompleteObject();
4668 }
4669
4670 // Compute value storage location and type of base object.
4671 APValue *BaseVal = nullptr;
4672 QualType BaseType = getType(B: LVal.Base);
4673
4674 if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl &&
4675 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4676 // This is the object whose initializer we're evaluating, so its lifetime
4677 // started in the current evaluation.
4678 BaseVal = Info.EvaluatingDeclValue;
4679 } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) {
4680 // Allow reading from a GUID declaration.
4681 if (auto *GD = dyn_cast<MSGuidDecl>(Val: D)) {
4682 if (isModification(AK)) {
4683 // All the remaining cases do not permit modification of the object.
4684 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4685 return CompleteObject();
4686 }
4687 APValue &V = GD->getAsAPValue();
4688 if (V.isAbsent()) {
4689 Info.FFDiag(E, DiagId: diag::note_constexpr_unsupported_layout)
4690 << GD->getType();
4691 return CompleteObject();
4692 }
4693 return CompleteObject(LVal.Base, &V, GD->getType());
4694 }
4695
4696 // Allow reading the APValue from an UnnamedGlobalConstantDecl.
4697 if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(Val: D)) {
4698 if (isModification(AK)) {
4699 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4700 return CompleteObject();
4701 }
4702 return CompleteObject(LVal.Base, const_cast<APValue *>(&GCD->getValue()),
4703 GCD->getType());
4704 }
4705
4706 // Allow reading from template parameter objects.
4707 if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: D)) {
4708 if (isModification(AK)) {
4709 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4710 return CompleteObject();
4711 }
4712 return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()),
4713 TPO->getType());
4714 }
4715
4716 // In C++98, const, non-volatile integers initialized with ICEs are ICEs.
4717 // In C++11, constexpr, non-volatile variables initialized with constant
4718 // expressions are constant expressions too. Inside constexpr functions,
4719 // parameters are constant expressions even if they're non-const.
4720 // In C++1y, objects local to a constant expression (those with a Frame) are
4721 // both readable and writable inside constant expressions.
4722 // In C, such things can also be folded, although they are not ICEs.
4723 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
4724 if (VD) {
4725 if (const VarDecl *VDef = VD->getDefinition(C&: Info.Ctx))
4726 VD = VDef;
4727 }
4728 if (!VD || VD->isInvalidDecl()) {
4729 Info.FFDiag(E);
4730 return CompleteObject();
4731 }
4732
4733 bool IsConstant = BaseType.isConstant(Ctx: Info.Ctx);
4734 bool ConstexprVar = false;
4735 if (const auto *VD = dyn_cast_if_present<VarDecl>(
4736 Val: Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()))
4737 ConstexprVar = VD->isConstexpr();
4738
4739 // Unless we're looking at a local variable or argument in a constexpr call,
4740 // the variable we're reading must be const (unless we are binding to a
4741 // reference).
4742 if (AK != clang::AK_Dereference && !Frame) {
4743 if (IsAccess && isa<ParmVarDecl>(Val: VD)) {
4744 // Access of a parameter that's not associated with a frame isn't going
4745 // to work out, but we can leave it to evaluateVarDeclInit to provide a
4746 // suitable diagnostic.
4747 } else if (Info.getLangOpts().CPlusPlus14 &&
4748 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4749 // OK, we can read and modify an object if we're in the process of
4750 // evaluating its initializer, because its lifetime began in this
4751 // evaluation.
4752 } else if (isModification(AK)) {
4753 // All the remaining cases do not permit modification of the object.
4754 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4755 return CompleteObject();
4756 } else if (VD->isConstexpr()) {
4757 // OK, we can read this variable.
4758 } else if (Info.getLangOpts().C23 && ConstexprVar) {
4759 Info.FFDiag(E);
4760 return CompleteObject();
4761 } else if (BaseType->isIntegralOrEnumerationType()) {
4762 if (!IsConstant) {
4763 if (!IsAccess)
4764 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4765 if (Info.getLangOpts().CPlusPlus) {
4766 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_non_const_int, ExtraNotes: 1) << VD;
4767 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4768 } else {
4769 Info.FFDiag(E);
4770 }
4771 return CompleteObject();
4772 }
4773 } else if (!IsAccess) {
4774 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4775 } else if ((IsConstant || BaseType->isReferenceType()) &&
4776 Info.checkingPotentialConstantExpression() &&
4777 BaseType->isLiteralType(Ctx: Info.Ctx) && !VD->hasDefinition()) {
4778 // This variable might end up being constexpr. Don't diagnose it yet.
4779 } else if (IsConstant) {
4780 // Keep evaluating to see what we can do. In particular, we support
4781 // folding of const floating-point types, in order to make static const
4782 // data members of such types (supported as an extension) more useful.
4783 if (Info.getLangOpts().CPlusPlus) {
4784 Info.CCEDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4785 ? diag::note_constexpr_ltor_non_constexpr
4786 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4787 << VD << BaseType;
4788 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4789 } else {
4790 Info.CCEDiag(E);
4791 }
4792 } else {
4793 // Never allow reading a non-const value.
4794 if (Info.getLangOpts().CPlusPlus) {
4795 Info.FFDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4796 ? diag::note_constexpr_ltor_non_constexpr
4797 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4798 << VD << BaseType;
4799 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4800 } else {
4801 Info.FFDiag(E);
4802 }
4803 return CompleteObject();
4804 }
4805 }
4806
4807 // When binding to a reference, the variable does not need to be constexpr
4808 // or have constant initalization.
4809 if (AK != clang::AK_Dereference &&
4810 !evaluateVarDeclInit(Info, E, VD, Frame, Version: LVal.getLValueVersion(),
4811 Result&: BaseVal))
4812 return CompleteObject();
4813 // If evaluateVarDeclInit sees a constexpr-unknown variable, it returns
4814 // a null BaseVal. Any constexpr-unknown variable seen here is an error:
4815 // we can't access a constexpr-unknown object.
4816 if (AK != clang::AK_Dereference && !BaseVal) {
4817 if (!Info.checkingPotentialConstantExpression()) {
4818 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unknown_variable, ExtraNotes: 1)
4819 << AK << VD;
4820 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4821 }
4822 return CompleteObject();
4823 }
4824 } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) {
4825 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
4826 if (!Alloc) {
4827 Info.FFDiag(E, DiagId: diag::note_constexpr_access_deleted_object) << AK;
4828 return CompleteObject();
4829 }
4830 return CompleteObject(LVal.Base, &(*Alloc)->Value,
4831 LVal.Base.getDynamicAllocType());
4832 }
4833 // When binding to a reference, the variable does not need to be
4834 // within its lifetime.
4835 else if (AK != clang::AK_Dereference) {
4836 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
4837
4838 if (!Frame) {
4839 if (const MaterializeTemporaryExpr *MTE =
4840 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: Base)) {
4841 assert(MTE->getStorageDuration() == SD_Static &&
4842 "should have a frame for a non-global materialized temporary");
4843
4844 // C++20 [expr.const]p4: [DR2126]
4845 // An object or reference is usable in constant expressions if it is
4846 // - a temporary object of non-volatile const-qualified literal type
4847 // whose lifetime is extended to that of a variable that is usable
4848 // in constant expressions
4849 //
4850 // C++20 [expr.const]p5:
4851 // an lvalue-to-rvalue conversion [is not allowed unless it applies to]
4852 // - a non-volatile glvalue that refers to an object that is usable
4853 // in constant expressions, or
4854 // - a non-volatile glvalue of literal type that refers to a
4855 // non-volatile object whose lifetime began within the evaluation
4856 // of E;
4857 //
4858 // C++11 misses the 'began within the evaluation of e' check and
4859 // instead allows all temporaries, including things like:
4860 // int &&r = 1;
4861 // int x = ++r;
4862 // constexpr int k = r;
4863 // Therefore we use the C++14-onwards rules in C++11 too.
4864 //
4865 // Note that temporaries whose lifetimes began while evaluating a
4866 // variable's constructor are not usable while evaluating the
4867 // corresponding destructor, not even if they're of const-qualified
4868 // types.
4869 if (!MTE->isUsableInConstantExpressions(Context: Info.Ctx) &&
4870 !lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4871 if (!IsAccess)
4872 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4873 Info.FFDiag(E, DiagId: diag::note_constexpr_access_static_temporary, ExtraNotes: 1) << AK;
4874 Info.Note(Loc: MTE->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
4875 return CompleteObject();
4876 }
4877
4878 BaseVal = MTE->getOrCreateValue(MayCreate: false);
4879 assert(BaseVal && "got reference to unevaluated temporary");
4880 } else if (const CompoundLiteralExpr *CLE =
4881 dyn_cast_or_null<CompoundLiteralExpr>(Val: Base)) {
4882 // According to GCC info page:
4883 //
4884 // 6.28 Compound Literals
4885 //
4886 // As an optimization, G++ sometimes gives array compound literals
4887 // longer lifetimes: when the array either appears outside a function or
4888 // has a const-qualified type. If foo and its initializer had elements
4889 // of type char *const rather than char *, or if foo were a global
4890 // variable, the array would have static storage duration. But it is
4891 // probably safest just to avoid the use of array compound literals in
4892 // C++ code.
4893 //
4894 // Obey that rule by checking constness for converted array types.
4895 if (QualType CLETy = CLE->getType(); CLETy->isArrayType() &&
4896 !LValType->isArrayType() &&
4897 !CLETy.isConstant(Ctx: Info.Ctx)) {
4898 Info.FFDiag(E);
4899 Info.Note(Loc: CLE->getExprLoc(), DiagId: diag::note_declared_at);
4900 return CompleteObject();
4901 }
4902
4903 BaseVal = &CLE->getStaticValue();
4904 } else {
4905 if (!IsAccess)
4906 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4907 APValue Val;
4908 LVal.moveInto(V&: Val);
4909 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unreadable_object)
4910 << AK
4911 << Val.getAsString(Ctx: Info.Ctx,
4912 Ty: Info.Ctx.getLValueReferenceType(T: LValType));
4913 NoteLValueLocation(Info, Base: LVal.Base);
4914 return CompleteObject();
4915 }
4916 } else if (AK != clang::AK_Dereference) {
4917 BaseVal = Frame->getTemporary(Key: Base, Version: LVal.Base.getVersion());
4918 assert(BaseVal && "missing value for temporary");
4919 }
4920 }
4921
4922 // In C++14, we can't safely access any mutable state when we might be
4923 // evaluating after an unmodeled side effect. Parameters are modeled as state
4924 // in the caller, but aren't visible once the call returns, so they can be
4925 // modified in a speculatively-evaluated call.
4926 //
4927 // FIXME: Not all local state is mutable. Allow local constant subobjects
4928 // to be read here (but take care with 'mutable' fields).
4929 unsigned VisibleDepth = Depth;
4930 if (llvm::isa_and_nonnull<ParmVarDecl>(
4931 Val: LVal.Base.dyn_cast<const ValueDecl *>()))
4932 ++VisibleDepth;
4933 if ((Frame && Info.getLangOpts().CPlusPlus14 &&
4934 Info.EvalStatus.HasSideEffects) ||
4935 (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth))
4936 return CompleteObject();
4937
4938 return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType);
4939}
4940
4941/// Perform an lvalue-to-rvalue conversion on the given glvalue. This
4942/// can also be used for 'lvalue-to-lvalue' conversions for looking up the
4943/// glvalue referred to by an entity of reference type.
4944///
4945/// \param Info - Information about the ongoing evaluation.
4946/// \param Conv - The expression for which we are performing the conversion.
4947/// Used for diagnostics.
4948/// \param Type - The type of the glvalue (before stripping cv-qualifiers in the
4949/// case of a non-class type).
4950/// \param LVal - The glvalue on which we are attempting to perform this action.
4951/// \param RVal - The produced value will be placed here.
4952/// \param WantObjectRepresentation - If true, we're looking for the object
4953/// representation rather than the value, and in particular,
4954/// there is no requirement that the result be fully initialized.
4955static bool
4956handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type,
4957 const LValue &LVal, APValue &RVal,
4958 bool WantObjectRepresentation = false) {
4959 if (LVal.Designator.Invalid)
4960 return false;
4961
4962 // Check for special cases where there is no existing APValue to look at.
4963 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
4964
4965 AccessKinds AK =
4966 WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read;
4967
4968 if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) {
4969 if (isa<StringLiteral>(Val: Base) || isa<PredefinedExpr>(Val: Base)) {
4970 // Special-case character extraction so we don't have to construct an
4971 // APValue for the whole string.
4972 assert(LVal.Designator.Entries.size() <= 1 &&
4973 "Can only read characters from string literals");
4974 if (LVal.Designator.Entries.empty()) {
4975 // Fail for now for LValue to RValue conversion of an array.
4976 // (This shouldn't show up in C/C++, but it could be triggered by a
4977 // weird EvaluateAsRValue call from a tool.)
4978 Info.FFDiag(E: Conv);
4979 return false;
4980 }
4981 if (LVal.Designator.isOnePastTheEnd()) {
4982 if (Info.getLangOpts().CPlusPlus11)
4983 Info.FFDiag(E: Conv, DiagId: diag::note_constexpr_access_past_end) << AK;
4984 else
4985 Info.FFDiag(E: Conv);
4986 return false;
4987 }
4988 uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex();
4989 RVal = APValue(extractStringLiteralCharacter(Info, Lit: Base, Index: CharIndex));
4990 return true;
4991 }
4992 }
4993
4994 CompleteObject Obj = findCompleteObject(Info, E: Conv, AK, LVal, LValType: Type);
4995 return Obj && extractSubobject(Info, E: Conv, Obj, Sub: LVal.Designator, Result&: RVal, AK);
4996}
4997
4998static bool hlslElementwiseCastHelper(EvalInfo &Info, const Expr *E,
4999 QualType DestTy,
5000 SmallVectorImpl<APValue> &SrcVals,
5001 SmallVectorImpl<QualType> &SrcTypes) {
5002 APValue Val;
5003 if (!Evaluate(Result&: Val, Info, E))
5004 return false;
5005
5006 // must be dealing with a record
5007 if (Val.isLValue()) {
5008 LValue LVal;
5009 LVal.setFrom(Ctx: Info.Ctx, V: Val);
5010 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal, RVal&: Val))
5011 return false;
5012 }
5013
5014 unsigned NEls = elementwiseSize(Info, BaseTy: DestTy);
5015 // flatten the source
5016 if (!flattenAPValue(Info, E, Value: Val, BaseTy: E->getType(), Elements&: SrcVals, Types&: SrcTypes, Size: NEls))
5017 return false;
5018
5019 return true;
5020}
5021
5022/// Perform an assignment of Val to LVal. Takes ownership of Val.
5023static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal,
5024 QualType LValType, APValue &Val) {
5025 if (LVal.Designator.Invalid)
5026 return false;
5027
5028 if (!Info.getLangOpts().CPlusPlus14) {
5029 Info.FFDiag(E);
5030 return false;
5031 }
5032
5033 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5034 return Obj && modifySubobject(Info, E, Obj, Sub: LVal.Designator, NewVal&: Val);
5035}
5036
5037namespace {
5038struct CompoundAssignSubobjectHandler {
5039 EvalInfo &Info;
5040 const CompoundAssignOperator *E;
5041 QualType PromotedLHSType;
5042 BinaryOperatorKind Opcode;
5043 const APValue &RHS;
5044
5045 static const AccessKinds AccessKind = AK_Assign;
5046
5047 typedef bool result_type;
5048
5049 bool checkConst(QualType QT) {
5050 // Assigning to a const object has undefined behavior.
5051 if (QT.isConstQualified()) {
5052 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5053 return false;
5054 }
5055 return true;
5056 }
5057
5058 bool failed() { return false; }
5059 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5060 switch (Subobj.getKind()) {
5061 case APValue::Int:
5062 return found(Value&: Subobj.getInt(), SubobjType);
5063 case APValue::Float:
5064 return found(Value&: Subobj.getFloat(), SubobjType);
5065 case APValue::ComplexInt:
5066 case APValue::ComplexFloat:
5067 // FIXME: Implement complex compound assignment.
5068 Info.FFDiag(E);
5069 return false;
5070 case APValue::LValue:
5071 return foundPointer(Subobj, SubobjType);
5072 case APValue::Vector:
5073 return foundVector(Value&: Subobj, SubobjType);
5074 case APValue::Indeterminate:
5075 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
5076 << /*read of=*/0 << /*uninitialized object=*/1
5077 << E->getLHS()->getSourceRange();
5078 NoteLValueLocation(Info, Base);
5079 return false;
5080 default:
5081 // FIXME: can this happen?
5082 Info.FFDiag(E);
5083 return false;
5084 }
5085 }
5086
5087 bool foundVector(APValue &Value, QualType SubobjType) {
5088 if (!checkConst(QT: SubobjType))
5089 return false;
5090
5091 if (!SubobjType->isVectorType()) {
5092 Info.FFDiag(E);
5093 return false;
5094 }
5095 return handleVectorVectorBinOp(Info, E, Opcode, LHSValue&: Value, RHSValue: RHS);
5096 }
5097
5098 bool found(APSInt &Value, QualType SubobjType) {
5099 if (!checkConst(QT: SubobjType))
5100 return false;
5101
5102 if (!SubobjType->isIntegerType()) {
5103 // We don't support compound assignment on integer-cast-to-pointer
5104 // values.
5105 Info.FFDiag(E);
5106 return false;
5107 }
5108
5109 if (RHS.isInt()) {
5110 APSInt LHS =
5111 HandleIntToIntCast(Info, E, DestType: PromotedLHSType, SrcType: SubobjType, Value);
5112 if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS: RHS.getInt(), Result&: LHS))
5113 return false;
5114 Value = HandleIntToIntCast(Info, E, DestType: SubobjType, SrcType: PromotedLHSType, Value: LHS);
5115 return true;
5116 } else if (RHS.isFloat()) {
5117 const FPOptions FPO = E->getFPFeaturesInEffect(
5118 LO: Info.Ctx.getLangOpts());
5119 APFloat FValue(0.0);
5120 return HandleIntToFloatCast(Info, E, FPO, SrcType: SubobjType, Value,
5121 DestType: PromotedLHSType, Result&: FValue) &&
5122 handleFloatFloatBinOp(Info, E, LHS&: FValue, Opcode, RHS: RHS.getFloat()) &&
5123 HandleFloatToIntCast(Info, E, SrcType: PromotedLHSType, Value: FValue, DestType: SubobjType,
5124 Result&: Value);
5125 }
5126
5127 Info.FFDiag(E);
5128 return false;
5129 }
5130 bool found(APFloat &Value, QualType SubobjType) {
5131 return checkConst(QT: SubobjType) &&
5132 HandleFloatToFloatCast(Info, E, SrcType: SubobjType, DestType: PromotedLHSType,
5133 Result&: Value) &&
5134 handleFloatFloatBinOp(Info, E, LHS&: Value, Opcode, RHS: RHS.getFloat()) &&
5135 HandleFloatToFloatCast(Info, E, SrcType: PromotedLHSType, DestType: SubobjType, Result&: Value);
5136 }
5137 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5138 if (!checkConst(QT: SubobjType))
5139 return false;
5140
5141 QualType PointeeType;
5142 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5143 PointeeType = PT->getPointeeType();
5144
5145 if (PointeeType.isNull() || !RHS.isInt() ||
5146 (Opcode != BO_Add && Opcode != BO_Sub)) {
5147 Info.FFDiag(E);
5148 return false;
5149 }
5150
5151 APSInt Offset = RHS.getInt();
5152 if (Opcode == BO_Sub)
5153 negateAsSigned(Int&: Offset);
5154
5155 LValue LVal;
5156 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5157 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType, Adjustment: Offset))
5158 return false;
5159 LVal.moveInto(V&: Subobj);
5160 return true;
5161 }
5162};
5163} // end anonymous namespace
5164
5165const AccessKinds CompoundAssignSubobjectHandler::AccessKind;
5166
5167/// Perform a compound assignment of LVal <op>= RVal.
5168static bool handleCompoundAssignment(EvalInfo &Info,
5169 const CompoundAssignOperator *E,
5170 const LValue &LVal, QualType LValType,
5171 QualType PromotedLValType,
5172 BinaryOperatorKind Opcode,
5173 const APValue &RVal) {
5174 if (LVal.Designator.Invalid)
5175 return false;
5176
5177 if (!Info.getLangOpts().CPlusPlus14) {
5178 Info.FFDiag(E);
5179 return false;
5180 }
5181
5182 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5183 CompoundAssignSubobjectHandler Handler = { .Info: Info, .E: E, .PromotedLHSType: PromotedLValType, .Opcode: Opcode,
5184 .RHS: RVal };
5185 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5186}
5187
5188namespace {
5189struct IncDecSubobjectHandler {
5190 EvalInfo &Info;
5191 const UnaryOperator *E;
5192 AccessKinds AccessKind;
5193 APValue *Old;
5194
5195 typedef bool result_type;
5196
5197 bool checkConst(QualType QT) {
5198 // Assigning to a const object has undefined behavior.
5199 if (QT.isConstQualified()) {
5200 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5201 return false;
5202 }
5203 return true;
5204 }
5205
5206 bool failed() { return false; }
5207 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5208 // Stash the old value. Also clear Old, so we don't clobber it later
5209 // if we're post-incrementing a complex.
5210 if (Old) {
5211 *Old = Subobj;
5212 Old = nullptr;
5213 }
5214
5215 switch (Subobj.getKind()) {
5216 case APValue::Int:
5217 return found(Value&: Subobj.getInt(), SubobjType);
5218 case APValue::Float:
5219 return found(Value&: Subobj.getFloat(), SubobjType);
5220 case APValue::ComplexInt:
5221 return found(Value&: Subobj.getComplexIntReal(),
5222 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5223 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5224 case APValue::ComplexFloat:
5225 return found(Value&: Subobj.getComplexFloatReal(),
5226 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5227 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5228 case APValue::LValue:
5229 return foundPointer(Subobj, SubobjType);
5230 default:
5231 // FIXME: can this happen?
5232 Info.FFDiag(E);
5233 return false;
5234 }
5235 }
5236 bool found(APSInt &Value, QualType SubobjType) {
5237 if (!checkConst(QT: SubobjType))
5238 return false;
5239
5240 if (!SubobjType->isIntegerType()) {
5241 // We don't support increment / decrement on integer-cast-to-pointer
5242 // values.
5243 Info.FFDiag(E);
5244 return false;
5245 }
5246
5247 if (Old) *Old = APValue(Value);
5248
5249 // bool arithmetic promotes to int, and the conversion back to bool
5250 // doesn't reduce mod 2^n, so special-case it.
5251 if (SubobjType->isBooleanType()) {
5252 if (AccessKind == AK_Increment)
5253 Value = 1;
5254 else
5255 Value = !Value;
5256 return true;
5257 }
5258
5259 bool WasNegative = Value.isNegative();
5260 if (AccessKind == AK_Increment) {
5261 ++Value;
5262
5263 if (!WasNegative && Value.isNegative() && E->canOverflow() &&
5264 !SubobjType.isWrapType()) {
5265 APSInt ActualValue(Value, /*IsUnsigned*/true);
5266 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5267 }
5268 } else {
5269 --Value;
5270
5271 if (WasNegative && !Value.isNegative() && E->canOverflow() &&
5272 !SubobjType.isWrapType()) {
5273 unsigned BitWidth = Value.getBitWidth();
5274 APSInt ActualValue(Value.sext(width: BitWidth + 1), /*IsUnsigned*/false);
5275 ActualValue.setBit(BitWidth);
5276 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5277 }
5278 }
5279 return true;
5280 }
5281 bool found(APFloat &Value, QualType SubobjType) {
5282 if (!checkConst(QT: SubobjType))
5283 return false;
5284
5285 if (Old) *Old = APValue(Value);
5286
5287 APFloat One(Value.getSemantics(), 1);
5288 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
5289 APFloat::opStatus St;
5290 if (AccessKind == AK_Increment)
5291 St = Value.add(RHS: One, RM);
5292 else
5293 St = Value.subtract(RHS: One, RM);
5294 return checkFloatingPointResult(Info, E, St);
5295 }
5296 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5297 if (!checkConst(QT: SubobjType))
5298 return false;
5299
5300 QualType PointeeType;
5301 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5302 PointeeType = PT->getPointeeType();
5303 else {
5304 Info.FFDiag(E);
5305 return false;
5306 }
5307
5308 LValue LVal;
5309 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5310 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType,
5311 Adjustment: AccessKind == AK_Increment ? 1 : -1))
5312 return false;
5313 LVal.moveInto(V&: Subobj);
5314 return true;
5315 }
5316};
5317} // end anonymous namespace
5318
5319/// Perform an increment or decrement on LVal.
5320static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal,
5321 QualType LValType, bool IsIncrement, APValue *Old) {
5322 if (LVal.Designator.Invalid)
5323 return false;
5324
5325 if (!Info.getLangOpts().CPlusPlus14) {
5326 Info.FFDiag(E);
5327 return false;
5328 }
5329
5330 AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement;
5331 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType);
5332 IncDecSubobjectHandler Handler = {.Info: Info, .E: cast<UnaryOperator>(Val: E), .AccessKind: AK, .Old: Old};
5333 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5334}
5335
5336/// Build an lvalue for the object argument of a member function call.
5337static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,
5338 LValue &This) {
5339 if (Object->getType()->isPointerType() && Object->isPRValue())
5340 return EvaluatePointer(E: Object, Result&: This, Info);
5341
5342 if (Object->isGLValue())
5343 return EvaluateLValue(E: Object, Result&: This, Info);
5344
5345 if (Object->getType()->isLiteralType(Ctx: Info.Ctx))
5346 return EvaluateTemporary(E: Object, Result&: This, Info);
5347
5348 if (Object->getType()->isRecordType() && Object->isPRValue())
5349 return EvaluateTemporary(E: Object, Result&: This, Info);
5350
5351 Info.FFDiag(E: Object, DiagId: diag::note_constexpr_nonliteral) << Object->getType();
5352 return false;
5353}
5354
5355/// HandleMemberPointerAccess - Evaluate a member access operation and build an
5356/// lvalue referring to the result.
5357///
5358/// \param Info - Information about the ongoing evaluation.
5359/// \param LV - An lvalue referring to the base of the member pointer.
5360/// \param RHS - The member pointer expression.
5361/// \param IncludeMember - Specifies whether the member itself is included in
5362/// the resulting LValue subobject designator. This is not possible when
5363/// creating a bound member function.
5364/// \return The field or method declaration to which the member pointer refers,
5365/// or 0 if evaluation fails.
5366static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5367 QualType LVType,
5368 LValue &LV,
5369 const Expr *RHS,
5370 bool IncludeMember = true) {
5371 MemberPtr MemPtr;
5372 if (!EvaluateMemberPointer(E: RHS, Result&: MemPtr, Info))
5373 return nullptr;
5374
5375 // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to
5376 // member value, the behavior is undefined.
5377 if (!MemPtr.getDecl()) {
5378 // FIXME: Specific diagnostic.
5379 Info.FFDiag(E: RHS);
5380 return nullptr;
5381 }
5382
5383 if (MemPtr.isDerivedMember()) {
5384 // This is a member of some derived class. Truncate LV appropriately.
5385 // The end of the derived-to-base path for the base object must match the
5386 // derived-to-base path for the member pointer.
5387 // C++23 [expr.mptr.oper]p4:
5388 // If the result of E1 is an object [...] whose most derived object does
5389 // not contain the member to which E2 refers, the behavior is undefined.
5390 if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >
5391 LV.Designator.Entries.size()) {
5392 Info.FFDiag(E: RHS);
5393 return nullptr;
5394 }
5395 unsigned PathLengthToMember =
5396 LV.Designator.Entries.size() - MemPtr.Path.size();
5397 for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {
5398 const CXXRecordDecl *LVDecl = getAsBaseClass(
5399 E: LV.Designator.Entries[PathLengthToMember + I]);
5400 const CXXRecordDecl *MPDecl = MemPtr.Path[I];
5401 if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) {
5402 Info.FFDiag(E: RHS);
5403 return nullptr;
5404 }
5405 }
5406 // MemPtr.Path only contains the base classes of the class directly
5407 // containing the member E2. It is still necessary to check that the class
5408 // directly containing the member E2 lies on the derived-to-base path of E1
5409 // to avoid incorrectly permitting member pointer access into a sibling
5410 // class of the class containing the member E2. If this class would
5411 // correspond to the most-derived class of E1, it either isn't contained in
5412 // LV.Designator.Entries or the corresponding entry refers to an array
5413 // element instead. Therefore get the most derived class directly in this
5414 // case. Otherwise the previous entry should correpond to this class.
5415 const CXXRecordDecl *LastLVDecl =
5416 (PathLengthToMember > LV.Designator.MostDerivedPathLength)
5417 ? getAsBaseClass(E: LV.Designator.Entries[PathLengthToMember - 1])
5418 : LV.Designator.MostDerivedType->getAsCXXRecordDecl();
5419 const CXXRecordDecl *LastMPDecl = MemPtr.getContainingRecord();
5420 if (LastLVDecl->getCanonicalDecl() != LastMPDecl->getCanonicalDecl()) {
5421 Info.FFDiag(E: RHS);
5422 return nullptr;
5423 }
5424
5425 // Truncate the lvalue to the appropriate derived class.
5426 if (!CastToDerivedClass(Info, E: RHS, Result&: LV, TruncatedType: MemPtr.getContainingRecord(),
5427 TruncatedElements: PathLengthToMember))
5428 return nullptr;
5429 } else if (!MemPtr.Path.empty()) {
5430 // Extend the LValue path with the member pointer's path.
5431 LV.Designator.Entries.reserve(N: LV.Designator.Entries.size() +
5432 MemPtr.Path.size() + IncludeMember);
5433
5434 // Walk down to the appropriate base class.
5435 if (const PointerType *PT = LVType->getAs<PointerType>())
5436 LVType = PT->getPointeeType();
5437 const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();
5438 assert(RD && "member pointer access on non-class-type expression");
5439 // The first class in the path is that of the lvalue.
5440 for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {
5441 const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];
5442 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, Base))
5443 return nullptr;
5444 RD = Base;
5445 }
5446 // Finally cast to the class containing the member.
5447 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD,
5448 Base: MemPtr.getContainingRecord()))
5449 return nullptr;
5450 }
5451
5452 // Add the member. Note that we cannot build bound member functions here.
5453 if (IncludeMember) {
5454 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: MemPtr.getDecl())) {
5455 if (!HandleLValueMember(Info, E: RHS, LVal&: LV, FD))
5456 return nullptr;
5457 } else if (const IndirectFieldDecl *IFD =
5458 dyn_cast<IndirectFieldDecl>(Val: MemPtr.getDecl())) {
5459 if (!HandleLValueIndirectMember(Info, E: RHS, LVal&: LV, IFD))
5460 return nullptr;
5461 } else {
5462 llvm_unreachable("can't construct reference to bound member function");
5463 }
5464 }
5465
5466 return MemPtr.getDecl();
5467}
5468
5469static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5470 const BinaryOperator *BO,
5471 LValue &LV,
5472 bool IncludeMember = true) {
5473 assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);
5474
5475 if (!EvaluateObjectArgument(Info, Object: BO->getLHS(), This&: LV)) {
5476 if (Info.noteFailure()) {
5477 MemberPtr MemPtr;
5478 EvaluateMemberPointer(E: BO->getRHS(), Result&: MemPtr, Info);
5479 }
5480 return nullptr;
5481 }
5482
5483 return HandleMemberPointerAccess(Info, LVType: BO->getLHS()->getType(), LV,
5484 RHS: BO->getRHS(), IncludeMember);
5485}
5486
5487/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
5488/// the provided lvalue, which currently refers to the base object.
5489static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
5490 LValue &Result) {
5491 SubobjectDesignator &D = Result.Designator;
5492 if (D.Invalid || !Result.checkNullPointer(Info, E, CSK: CSK_Derived))
5493 return false;
5494
5495 QualType TargetQT = E->getType();
5496 if (const PointerType *PT = TargetQT->getAs<PointerType>())
5497 TargetQT = PT->getPointeeType();
5498
5499 auto InvalidCast = [&]() {
5500 if (!Info.checkingPotentialConstantExpression() ||
5501 !Result.AllowConstexprUnknown) {
5502 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_downcast)
5503 << D.MostDerivedType << TargetQT;
5504 }
5505 return false;
5506 };
5507
5508 // Check this cast lands within the final derived-to-base subobject path.
5509 if (D.MostDerivedPathLength + E->path_size() > D.Entries.size())
5510 return InvalidCast();
5511
5512 // Check the type of the final cast. We don't need to check the path,
5513 // since a cast can only be formed if the path is unique.
5514 unsigned NewEntriesSize = D.Entries.size() - E->path_size();
5515 const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
5516 const CXXRecordDecl *FinalType;
5517 if (NewEntriesSize == D.MostDerivedPathLength)
5518 FinalType = D.MostDerivedType->getAsCXXRecordDecl();
5519 else
5520 FinalType = getAsBaseClass(E: D.Entries[NewEntriesSize - 1]);
5521 if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl())
5522 return InvalidCast();
5523
5524 // Truncate the lvalue to the appropriate derived class.
5525 return CastToDerivedClass(Info, E, Result, TruncatedType: TargetType, TruncatedElements: NewEntriesSize);
5526}
5527
5528/// Get the value to use for a default-initialized object of type T.
5529/// Return false if it encounters something invalid.
5530static bool handleDefaultInitValue(QualType T, APValue &Result,
5531 bool IsCompleteClass = true) {
5532 bool Success = true;
5533
5534 // If there is already a value present don't overwrite it.
5535 if (!Result.isAbsent())
5536 return true;
5537
5538 if (auto *RD = T->getAsCXXRecordDecl()) {
5539 if (RD->isInvalidDecl()) {
5540 Result = APValue();
5541 return false;
5542 }
5543 if (RD->isUnion()) {
5544 Result = APValue((const FieldDecl *)nullptr);
5545 return true;
5546 }
5547
5548 // bases() includes directly specified virtual bases as well.
5549 unsigned NonVirtualBases = countNonVirtualBases(RD);
5550 Result =
5551 APValue(APValue::UninitStruct(), NonVirtualBases, RD->getNumFields(),
5552 IsCompleteClass ? RD->getNumVBases() : 0);
5553
5554 unsigned Index = 0;
5555 for (const CXXBaseSpecifier &B : RD->bases()) {
5556 if (B.isVirtual())
5557 continue;
5558 Success &= handleDefaultInitValue(
5559 T: B.getType(), Result&: Result.getStructBase(i: Index), /*IsCompleteClass=*/false);
5560 ++Index;
5561 }
5562
5563 for (const auto *I : RD->fields()) {
5564 if (I->isUnnamedBitField())
5565 continue;
5566 Success &= handleDefaultInitValue(
5567 T: I->getType(), Result&: Result.getStructField(i: I->getFieldIndex()));
5568 }
5569
5570 if (IsCompleteClass) {
5571 Index = 0;
5572
5573 for (const auto &B : RD->vbases()) {
5574 Success &= handleDefaultInitValue(T: B.getType(),
5575 Result&: Result.getStructVirtualBase(i: Index),
5576 /*IsCompleteClass=*/false);
5577 ++Index;
5578 }
5579 } else {
5580 // Virtual bases should only exist at the top level of an APValue.
5581 assert(Result.getStructNumVirtualBases() == 0);
5582 }
5583
5584 return Success;
5585 }
5586
5587 if (auto *AT =
5588 dyn_cast_or_null<ConstantArrayType>(Val: T->getAsArrayTypeUnsafe())) {
5589 Result = APValue(APValue::UninitArray(), 0, AT->getZExtSize());
5590 if (Result.hasArrayFiller())
5591 Success &=
5592 handleDefaultInitValue(T: AT->getElementType(), Result&: Result.getArrayFiller());
5593 return Success;
5594 }
5595
5596 Result = APValue::IndeterminateValue();
5597 return true;
5598}
5599
5600namespace {
5601enum EvalStmtResult {
5602 /// Evaluation failed.
5603 ESR_Failed,
5604 /// Hit a 'return' statement.
5605 ESR_Returned,
5606 /// Evaluation succeeded.
5607 ESR_Succeeded,
5608 /// Hit a 'continue' statement.
5609 ESR_Continue,
5610 /// Hit a 'break' statement.
5611 ESR_Break,
5612 /// Still scanning for 'case' or 'default' statement.
5613 ESR_CaseNotFound
5614};
5615}
5616/// Evaluates the initializer of a reference.
5617static bool EvaluateInitForDeclOfReferenceType(EvalInfo &Info,
5618 const ValueDecl *D,
5619 const Expr *Init, LValue &Result,
5620 APValue &Val) {
5621 assert(Init->isGLValue() && D->getType()->isReferenceType());
5622 // A reference is an lvalue.
5623 if (!EvaluateLValue(E: Init, Result, Info))
5624 return false;
5625 // [C++26][decl.ref]
5626 // The object designated by such a glvalue can be outside its lifetime
5627 // Because a null pointer value or a pointer past the end of an object
5628 // does not point to an object, a reference in a well-defined program cannot
5629 // refer to such things;
5630 if (!Result.Designator.Invalid && Result.Designator.isOnePastTheEnd()) {
5631 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_access_past_end) << AK_Dereference;
5632 return false;
5633 }
5634
5635 // Save the result.
5636 Result.moveInto(V&: Val);
5637 return true;
5638}
5639
5640static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) {
5641 if (VD->isInvalidDecl())
5642 return false;
5643 // We don't need to evaluate the initializer for a static local.
5644 if (!VD->hasLocalStorage())
5645 return true;
5646
5647 LValue Result;
5648 APValue &Val = Info.CurrentCall->createTemporary(Key: VD, T: VD->getType(),
5649 Scope: ScopeKind::Block, LV&: Result);
5650
5651 const Expr *InitE = VD->getInit();
5652 if (!InitE) {
5653 if (VD->getType()->isDependentType())
5654 return Info.noteSideEffect();
5655 return handleDefaultInitValue(T: VD->getType(), Result&: Val);
5656 }
5657 if (InitE->isValueDependent())
5658 return false;
5659
5660 // For references to objects, check they do not designate a one-past-the-end
5661 // object.
5662 if (VD->getType()->isReferenceType()) {
5663 return EvaluateInitForDeclOfReferenceType(Info, D: VD, Init: InitE, Result, Val);
5664 } else if (!EvaluateInPlace(Result&: Val, Info, This: Result, E: InitE)) {
5665 // Wipe out any partially-computed value, to allow tracking that this
5666 // evaluation failed.
5667 Val = APValue();
5668 return false;
5669 }
5670
5671 return true;
5672}
5673
5674static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5675 const DecompositionDecl *DD);
5676
5677static bool EvaluateDecl(EvalInfo &Info, const Decl *D,
5678 bool EvaluateConditionDecl = false) {
5679 bool OK = true;
5680 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
5681 OK &= EvaluateVarDecl(Info, VD);
5682
5683 if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(Val: D);
5684 EvaluateConditionDecl && DD)
5685 OK &= EvaluateDecompositionDeclInit(Info, DD);
5686
5687 return OK;
5688}
5689
5690static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5691 const DecompositionDecl *DD) {
5692 bool OK = true;
5693 for (auto *BD : DD->flat_bindings())
5694 if (auto *VD = BD->getHoldingVar())
5695 OK &= EvaluateDecl(Info, D: VD, /*EvaluateConditionDecl=*/true);
5696
5697 return OK;
5698}
5699
5700static bool MaybeEvaluateDeferredVarDeclInit(EvalInfo &Info,
5701 const VarDecl *VD) {
5702 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(Val: VD)) {
5703 if (!EvaluateDecompositionDeclInit(Info, DD))
5704 return false;
5705 }
5706 return true;
5707}
5708
5709static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) {
5710 assert(E->isValueDependent());
5711 if (Info.noteSideEffect())
5712 return true;
5713 assert(E->containsErrors() && "valid value-dependent expression should never "
5714 "reach invalid code path.");
5715 return false;
5716}
5717
5718/// Evaluate a condition (either a variable declaration or an expression).
5719static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl,
5720 const Expr *Cond, bool &Result) {
5721 if (Cond->isValueDependent())
5722 return false;
5723 FullExpressionRAII Scope(Info);
5724 if (CondDecl && !EvaluateDecl(Info, D: CondDecl))
5725 return false;
5726 if (!EvaluateAsBooleanCondition(E: Cond, Result, Info))
5727 return false;
5728 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: CondDecl))
5729 return false;
5730 return Scope.destroy();
5731}
5732
5733namespace {
5734/// A location where the result (returned value) of evaluating a
5735/// statement should be stored.
5736struct StmtResult {
5737 /// The APValue that should be filled in with the returned value.
5738 APValue &Value;
5739 /// The location containing the result, if any (used to support RVO).
5740 const LValue *Slot;
5741};
5742
5743struct TempVersionRAII {
5744 CallStackFrame &Frame;
5745
5746 TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) {
5747 Frame.pushTempVersion();
5748 }
5749
5750 ~TempVersionRAII() {
5751 Frame.popTempVersion();
5752 }
5753};
5754
5755}
5756
5757static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
5758 const Stmt *S,
5759 const SwitchCase *SC = nullptr);
5760
5761/// Helper to implement named break/continue. Returns 'true' if the evaluation
5762/// result should be propagated up. Otherwise, it sets the evaluation result
5763/// to either Continue to continue the current loop, or Succeeded to break it.
5764static bool ShouldPropagateBreakContinue(EvalInfo &Info,
5765 const Stmt *LoopOrSwitch,
5766 ArrayRef<BlockScopeRAII *> Scopes,
5767 EvalStmtResult &ESR) {
5768 bool IsSwitch = isa<SwitchStmt>(Val: LoopOrSwitch);
5769
5770 // For loops, map Succeeded to Continue so we don't have to check for both.
5771 if (!IsSwitch && ESR == ESR_Succeeded) {
5772 ESR = ESR_Continue;
5773 return false;
5774 }
5775
5776 if (ESR != ESR_Break && ESR != ESR_Continue)
5777 return false;
5778
5779 // Are we breaking out of or continuing this statement?
5780 bool CanBreakOrContinue = !IsSwitch || ESR == ESR_Break;
5781 const Stmt *StackTop = Info.BreakContinueStack.back();
5782 if (CanBreakOrContinue && (StackTop == nullptr || StackTop == LoopOrSwitch)) {
5783 Info.BreakContinueStack.pop_back();
5784 if (ESR == ESR_Break)
5785 ESR = ESR_Succeeded;
5786 return false;
5787 }
5788
5789 // We're not. Propagate the result up.
5790 for (BlockScopeRAII *S : Scopes) {
5791 if (!S->destroy()) {
5792 ESR = ESR_Failed;
5793 break;
5794 }
5795 }
5796 return true;
5797}
5798
5799/// Evaluate the body of a loop, and translate the result as appropriate.
5800static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info,
5801 const Stmt *Body,
5802 const SwitchCase *Case = nullptr) {
5803 BlockScopeRAII Scope(Info);
5804
5805 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Body, SC: Case);
5806 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5807 ESR = ESR_Failed;
5808
5809 return ESR;
5810}
5811
5812/// Evaluate a switch statement.
5813static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
5814 const SwitchStmt *SS) {
5815 BlockScopeRAII Scope(Info);
5816
5817 // Evaluate the switch condition.
5818 APSInt Value;
5819 {
5820 if (const Stmt *Init = SS->getInit()) {
5821 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
5822 if (ESR != ESR_Succeeded) {
5823 if (ESR != ESR_Failed && !Scope.destroy())
5824 ESR = ESR_Failed;
5825 return ESR;
5826 }
5827 }
5828
5829 FullExpressionRAII CondScope(Info);
5830 if (SS->getConditionVariable() &&
5831 !EvaluateDecl(Info, D: SS->getConditionVariable()))
5832 return ESR_Failed;
5833 if (SS->getCond()->isValueDependent()) {
5834 // We don't know what the value is, and which branch should jump to.
5835 EvaluateDependentExpr(E: SS->getCond(), Info);
5836 return ESR_Failed;
5837 }
5838 if (!EvaluateInteger(E: SS->getCond(), Result&: Value, Info))
5839 return ESR_Failed;
5840
5841 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: SS->getConditionVariable()))
5842 return ESR_Failed;
5843
5844 if (!CondScope.destroy())
5845 return ESR_Failed;
5846 }
5847
5848 // Find the switch case corresponding to the value of the condition.
5849 // FIXME: Cache this lookup.
5850 const SwitchCase *Found = nullptr;
5851 for (const SwitchCase *SC = SS->getSwitchCaseList(); SC;
5852 SC = SC->getNextSwitchCase()) {
5853 if (isa<DefaultStmt>(Val: SC)) {
5854 Found = SC;
5855 continue;
5856 }
5857
5858 const CaseStmt *CS = cast<CaseStmt>(Val: SC);
5859 const Expr *LHS = CS->getLHS();
5860 const Expr *RHS = CS->getRHS();
5861 if (LHS->isValueDependent() || (RHS && RHS->isValueDependent()))
5862 return ESR_Failed;
5863 APSInt LHSValue = LHS->EvaluateKnownConstInt(Ctx: Info.Ctx);
5864 APSInt RHSValue = RHS ? RHS->EvaluateKnownConstInt(Ctx: Info.Ctx) : LHSValue;
5865 if (LHSValue <= Value && Value <= RHSValue) {
5866 Found = SC;
5867 break;
5868 }
5869 }
5870
5871 if (!Found)
5872 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
5873
5874 // Search the switch body for the switch case and evaluate it from there.
5875 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SS->getBody(), SC: Found);
5876 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5877 return ESR_Failed;
5878 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: SS, /*Scopes=*/{}, ESR))
5879 return ESR;
5880
5881 switch (ESR) {
5882 case ESR_Break:
5883 llvm_unreachable("Should have been converted to Succeeded");
5884 case ESR_Succeeded:
5885 case ESR_Continue:
5886 case ESR_Failed:
5887 case ESR_Returned:
5888 return ESR;
5889 case ESR_CaseNotFound:
5890 // This can only happen if the switch case is nested within a statement
5891 // expression. We have no intention of supporting that.
5892 Info.FFDiag(Loc: Found->getBeginLoc(),
5893 DiagId: diag::note_constexpr_stmt_expr_unsupported);
5894 return ESR_Failed;
5895 }
5896 llvm_unreachable("Invalid EvalStmtResult!");
5897}
5898
5899static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) {
5900 // An expression E is a core constant expression unless the evaluation of E
5901 // would evaluate one of the following: [C++23] - a control flow that passes
5902 // through a declaration of a variable with static or thread storage duration
5903 // unless that variable is usable in constant expressions.
5904 if (VD->isLocalVarDecl() && VD->isStaticLocal() &&
5905 !VD->isUsableInConstantExpressions(C: Info.Ctx)) {
5906 Info.CCEDiag(Loc: VD->getLocation(), DiagId: diag::note_constexpr_static_local)
5907 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
5908 return false;
5909 }
5910 return true;
5911}
5912
5913// Evaluate a statement.
5914static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
5915 const Stmt *S, const SwitchCase *Case) {
5916 if (!Info.nextStep(S))
5917 return ESR_Failed;
5918
5919 // If we're hunting down a 'case' or 'default' label, recurse through
5920 // substatements until we hit the label.
5921 if (Case) {
5922 switch (S->getStmtClass()) {
5923 case Stmt::CompoundStmtClass:
5924 // FIXME: Precompute which substatement of a compound statement we
5925 // would jump to, and go straight there rather than performing a
5926 // linear scan each time.
5927 case Stmt::LabelStmtClass:
5928 case Stmt::AttributedStmtClass:
5929 case Stmt::DoStmtClass:
5930 break;
5931
5932 case Stmt::CaseStmtClass:
5933 case Stmt::DefaultStmtClass:
5934 if (Case == S)
5935 Case = nullptr;
5936 break;
5937
5938 case Stmt::IfStmtClass: {
5939 // FIXME: Precompute which side of an 'if' we would jump to, and go
5940 // straight there rather than scanning both sides.
5941 const IfStmt *IS = cast<IfStmt>(Val: S);
5942
5943 // Wrap the evaluation in a block scope, in case it's a DeclStmt
5944 // preceded by our switch label.
5945 BlockScopeRAII Scope(Info);
5946
5947 // Step into the init statement in case it brings an (uninitialized)
5948 // variable into scope.
5949 if (const Stmt *Init = IS->getInit()) {
5950 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
5951 if (ESR != ESR_CaseNotFound) {
5952 assert(ESR != ESR_Succeeded);
5953 return ESR;
5954 }
5955 }
5956
5957 // Condition variable must be initialized if it exists.
5958 // FIXME: We can skip evaluating the body if there's a condition
5959 // variable, as there can't be any case labels within it.
5960 // (The same is true for 'for' statements.)
5961
5962 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: IS->getThen(), Case);
5963 if (ESR == ESR_Failed)
5964 return ESR;
5965 if (ESR != ESR_CaseNotFound)
5966 return Scope.destroy() ? ESR : ESR_Failed;
5967 if (!IS->getElse())
5968 return ESR_CaseNotFound;
5969
5970 ESR = EvaluateStmt(Result, Info, S: IS->getElse(), Case);
5971 if (ESR == ESR_Failed)
5972 return ESR;
5973 if (ESR != ESR_CaseNotFound)
5974 return Scope.destroy() ? ESR : ESR_Failed;
5975 return ESR_CaseNotFound;
5976 }
5977
5978 case Stmt::WhileStmtClass: {
5979 EvalStmtResult ESR =
5980 EvaluateLoopBody(Result, Info, Body: cast<WhileStmt>(Val: S)->getBody(), Case);
5981 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: S, /*Scopes=*/{}, ESR))
5982 return ESR;
5983 if (ESR != ESR_Continue)
5984 return ESR;
5985 break;
5986 }
5987
5988 case Stmt::ForStmtClass: {
5989 const ForStmt *FS = cast<ForStmt>(Val: S);
5990 BlockScopeRAII Scope(Info);
5991
5992 // Step into the init statement in case it brings an (uninitialized)
5993 // variable into scope.
5994 if (const Stmt *Init = FS->getInit()) {
5995 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
5996 if (ESR != ESR_CaseNotFound) {
5997 assert(ESR != ESR_Succeeded);
5998 return ESR;
5999 }
6000 }
6001
6002 EvalStmtResult ESR =
6003 EvaluateLoopBody(Result, Info, Body: FS->getBody(), Case);
6004 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, /*Scopes=*/{}, ESR))
6005 return ESR;
6006 if (ESR != ESR_Continue)
6007 return ESR;
6008 if (const auto *Inc = FS->getInc()) {
6009 if (Inc->isValueDependent()) {
6010 if (!EvaluateDependentExpr(E: Inc, Info))
6011 return ESR_Failed;
6012 } else {
6013 FullExpressionRAII IncScope(Info);
6014 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6015 return ESR_Failed;
6016 }
6017 }
6018 break;
6019 }
6020
6021 case Stmt::DeclStmtClass: {
6022 // Start the lifetime of any uninitialized variables we encounter. They
6023 // might be used by the selected branch of the switch.
6024 const DeclStmt *DS = cast<DeclStmt>(Val: S);
6025 for (const auto *D : DS->decls()) {
6026 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
6027 if (!CheckLocalVariableDeclaration(Info, VD))
6028 return ESR_Failed;
6029 if (VD->hasLocalStorage() && !VD->getInit())
6030 if (!EvaluateVarDecl(Info, VD))
6031 return ESR_Failed;
6032 // FIXME: If the variable has initialization that can't be jumped
6033 // over, bail out of any immediately-surrounding compound-statement
6034 // too. There can't be any case labels here.
6035 }
6036 }
6037 return ESR_CaseNotFound;
6038 }
6039
6040 default:
6041 return ESR_CaseNotFound;
6042 }
6043 }
6044
6045 switch (S->getStmtClass()) {
6046 default:
6047 if (const Expr *E = dyn_cast<Expr>(Val: S)) {
6048 if (E->isValueDependent()) {
6049 if (!EvaluateDependentExpr(E, Info))
6050 return ESR_Failed;
6051 } else {
6052 // Don't bother evaluating beyond an expression-statement which couldn't
6053 // be evaluated.
6054 // FIXME: Do we need the FullExpressionRAII object here?
6055 // VisitExprWithCleanups should create one when necessary.
6056 FullExpressionRAII Scope(Info);
6057 if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy())
6058 return ESR_Failed;
6059 }
6060 return ESR_Succeeded;
6061 }
6062
6063 Info.FFDiag(Loc: S->getBeginLoc()) << S->getSourceRange();
6064 return ESR_Failed;
6065
6066 case Stmt::NullStmtClass:
6067 return ESR_Succeeded;
6068
6069 case Stmt::DeclStmtClass: {
6070 const DeclStmt *DS = cast<DeclStmt>(Val: S);
6071 for (const auto *D : DS->decls()) {
6072 const VarDecl *VD = dyn_cast_or_null<VarDecl>(Val: D);
6073 if (VD && !CheckLocalVariableDeclaration(Info, VD))
6074 return ESR_Failed;
6075
6076 if (const auto *ESD = dyn_cast<CXXExpansionStmtDecl>(Val: D)) {
6077 assert(ESD->getInstantiations() && "not expanded?");
6078 return EvaluateStmt(Result, Info, S: ESD->getInstantiations(), Case);
6079 }
6080
6081 // Each declaration initialization is its own full-expression.
6082 FullExpressionRAII Scope(Info);
6083 if (!EvaluateDecl(Info, D, /*EvaluateConditionDecl=*/true) &&
6084 !Info.noteFailure())
6085 return ESR_Failed;
6086 if (!Scope.destroy())
6087 return ESR_Failed;
6088 }
6089 return ESR_Succeeded;
6090 }
6091
6092 case Stmt::ReturnStmtClass: {
6093 const Expr *RetExpr = cast<ReturnStmt>(Val: S)->getRetValue();
6094 FullExpressionRAII Scope(Info);
6095 if (RetExpr && RetExpr->isValueDependent()) {
6096 EvaluateDependentExpr(E: RetExpr, Info);
6097 // We know we returned, but we don't know what the value is.
6098 return ESR_Failed;
6099 }
6100 if (RetExpr &&
6101 !(Result.Slot
6102 ? EvaluateInPlace(Result&: Result.Value, Info, This: *Result.Slot, E: RetExpr)
6103 : Evaluate(Result&: Result.Value, Info, E: RetExpr)))
6104 return ESR_Failed;
6105 return Scope.destroy() ? ESR_Returned : ESR_Failed;
6106 }
6107
6108 case Stmt::CompoundStmtClass: {
6109 BlockScopeRAII Scope(Info);
6110
6111 const CompoundStmt *CS = cast<CompoundStmt>(Val: S);
6112 for (const auto *BI : CS->body()) {
6113 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: BI, Case);
6114 if (ESR == ESR_Succeeded)
6115 Case = nullptr;
6116 else if (ESR != ESR_CaseNotFound) {
6117 if (ESR != ESR_Failed && !Scope.destroy())
6118 return ESR_Failed;
6119 return ESR;
6120 }
6121 }
6122 if (Case)
6123 return ESR_CaseNotFound;
6124 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6125 }
6126
6127 case Stmt::IfStmtClass: {
6128 const IfStmt *IS = cast<IfStmt>(Val: S);
6129
6130 // Evaluate the condition, as either a var decl or as an expression.
6131 BlockScopeRAII Scope(Info);
6132 if (const Stmt *Init = IS->getInit()) {
6133 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
6134 if (ESR != ESR_Succeeded) {
6135 if (ESR != ESR_Failed && !Scope.destroy())
6136 return ESR_Failed;
6137 return ESR;
6138 }
6139 }
6140 bool Cond;
6141 if (IS->isConsteval()) {
6142 Cond = IS->isNonNegatedConsteval();
6143 // If we are not in a constant context, if consteval should not evaluate
6144 // to true.
6145 if (!Info.InConstantContext)
6146 Cond = !Cond;
6147 } else if (!EvaluateCond(Info, CondDecl: IS->getConditionVariable(), Cond: IS->getCond(),
6148 Result&: Cond))
6149 return ESR_Failed;
6150
6151 if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) {
6152 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SubStmt);
6153 if (ESR != ESR_Succeeded) {
6154 if (ESR != ESR_Failed && !Scope.destroy())
6155 return ESR_Failed;
6156 return ESR;
6157 }
6158 }
6159 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6160 }
6161
6162 case Stmt::WhileStmtClass: {
6163 const WhileStmt *WS = cast<WhileStmt>(Val: S);
6164 while (true) {
6165 BlockScopeRAII Scope(Info);
6166 bool Continue;
6167 if (!EvaluateCond(Info, CondDecl: WS->getConditionVariable(), Cond: WS->getCond(),
6168 Result&: Continue))
6169 return ESR_Failed;
6170 if (!Continue)
6171 break;
6172
6173 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: WS->getBody());
6174 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: WS, Scopes: &Scope, ESR))
6175 return ESR;
6176
6177 if (ESR != ESR_Continue) {
6178 if (ESR != ESR_Failed && !Scope.destroy())
6179 return ESR_Failed;
6180 return ESR;
6181 }
6182 if (!Scope.destroy())
6183 return ESR_Failed;
6184 }
6185 return ESR_Succeeded;
6186 }
6187
6188 case Stmt::DoStmtClass: {
6189 const DoStmt *DS = cast<DoStmt>(Val: S);
6190 bool Continue;
6191 do {
6192 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: DS->getBody(), Case);
6193 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: DS, /*Scopes=*/{}, ESR))
6194 return ESR;
6195 if (ESR != ESR_Continue)
6196 return ESR;
6197 Case = nullptr;
6198
6199 if (DS->getCond()->isValueDependent()) {
6200 EvaluateDependentExpr(E: DS->getCond(), Info);
6201 // Bailout as we don't know whether to keep going or terminate the loop.
6202 return ESR_Failed;
6203 }
6204 FullExpressionRAII CondScope(Info);
6205 if (!EvaluateAsBooleanCondition(E: DS->getCond(), Result&: Continue, Info) ||
6206 !CondScope.destroy())
6207 return ESR_Failed;
6208 } while (Continue);
6209 return ESR_Succeeded;
6210 }
6211
6212 case Stmt::ForStmtClass: {
6213 const ForStmt *FS = cast<ForStmt>(Val: S);
6214 BlockScopeRAII ForScope(Info);
6215 if (FS->getInit()) {
6216 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6217 if (ESR != ESR_Succeeded) {
6218 if (ESR != ESR_Failed && !ForScope.destroy())
6219 return ESR_Failed;
6220 return ESR;
6221 }
6222 }
6223 while (true) {
6224 BlockScopeRAII IterScope(Info);
6225 bool Continue = true;
6226 if (FS->getCond() && !EvaluateCond(Info, CondDecl: FS->getConditionVariable(),
6227 Cond: FS->getCond(), Result&: Continue))
6228 return ESR_Failed;
6229
6230 if (!Continue) {
6231 if (!IterScope.destroy())
6232 return ESR_Failed;
6233 break;
6234 }
6235
6236 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6237 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&IterScope, &ForScope}, ESR))
6238 return ESR;
6239 if (ESR != ESR_Continue) {
6240 if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy()))
6241 return ESR_Failed;
6242 return ESR;
6243 }
6244
6245 if (const auto *Inc = FS->getInc()) {
6246 if (Inc->isValueDependent()) {
6247 if (!EvaluateDependentExpr(E: Inc, Info))
6248 return ESR_Failed;
6249 } else {
6250 FullExpressionRAII IncScope(Info);
6251 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6252 return ESR_Failed;
6253 }
6254 }
6255
6256 if (!IterScope.destroy())
6257 return ESR_Failed;
6258 }
6259 return ForScope.destroy() ? ESR_Succeeded : ESR_Failed;
6260 }
6261
6262 case Stmt::CXXForRangeStmtClass: {
6263 const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(Val: S);
6264 BlockScopeRAII Scope(Info);
6265
6266 // Evaluate the init-statement if present.
6267 if (FS->getInit()) {
6268 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6269 if (ESR != ESR_Succeeded) {
6270 if (ESR != ESR_Failed && !Scope.destroy())
6271 return ESR_Failed;
6272 return ESR;
6273 }
6274 }
6275
6276 // Initialize the __range variable.
6277 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getRangeStmt());
6278 if (ESR != ESR_Succeeded) {
6279 if (ESR != ESR_Failed && !Scope.destroy())
6280 return ESR_Failed;
6281 return ESR;
6282 }
6283
6284 // In error-recovery cases it's possible to get here even if we failed to
6285 // synthesize the __begin and __end variables.
6286 if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond())
6287 return ESR_Failed;
6288
6289 // Create the __begin and __end iterators.
6290 ESR = EvaluateStmt(Result, Info, S: FS->getBeginStmt());
6291 if (ESR != ESR_Succeeded) {
6292 if (ESR != ESR_Failed && !Scope.destroy())
6293 return ESR_Failed;
6294 return ESR;
6295 }
6296 ESR = EvaluateStmt(Result, Info, S: FS->getEndStmt());
6297 if (ESR != ESR_Succeeded) {
6298 if (ESR != ESR_Failed && !Scope.destroy())
6299 return ESR_Failed;
6300 return ESR;
6301 }
6302
6303 while (true) {
6304 // Condition: __begin != __end.
6305 {
6306 if (FS->getCond()->isValueDependent()) {
6307 EvaluateDependentExpr(E: FS->getCond(), Info);
6308 // We don't know whether to keep going or terminate the loop.
6309 return ESR_Failed;
6310 }
6311 bool Continue = true;
6312 FullExpressionRAII CondExpr(Info);
6313 if (!EvaluateAsBooleanCondition(E: FS->getCond(), Result&: Continue, Info))
6314 return ESR_Failed;
6315 if (!Continue)
6316 break;
6317 }
6318
6319 // User's variable declaration, initialized by *__begin.
6320 BlockScopeRAII InnerScope(Info);
6321 ESR = EvaluateStmt(Result, Info, S: FS->getLoopVarStmt());
6322 if (ESR != ESR_Succeeded) {
6323 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6324 return ESR_Failed;
6325 return ESR;
6326 }
6327
6328 // Loop body.
6329 ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6330 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&InnerScope, &Scope}, ESR))
6331 return ESR;
6332 if (ESR != ESR_Continue) {
6333 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6334 return ESR_Failed;
6335 return ESR;
6336 }
6337 if (FS->getInc()->isValueDependent()) {
6338 if (!EvaluateDependentExpr(E: FS->getInc(), Info))
6339 return ESR_Failed;
6340 } else {
6341 // Increment: ++__begin
6342 if (!EvaluateIgnoredValue(Info, E: FS->getInc()))
6343 return ESR_Failed;
6344 }
6345
6346 if (!InnerScope.destroy())
6347 return ESR_Failed;
6348 }
6349
6350 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6351 }
6352
6353 case Stmt::CXXExpansionStmtInstantiationClass: {
6354 BlockScopeRAII Scope(Info);
6355 const auto *Expansion = cast<CXXExpansionStmtInstantiation>(Val: S);
6356 for (const Stmt *PreambleStmt : Expansion->getPreambleStmts()) {
6357 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: PreambleStmt);
6358 if (ESR != ESR_Succeeded) {
6359 if (ESR != ESR_Failed && !Scope.destroy())
6360 return ESR_Failed;
6361 return ESR;
6362 }
6363 }
6364
6365 // No need to push an extra scope for these since they're already
6366 // CompoundStmts.
6367 EvalStmtResult ESR = ESR_Succeeded;
6368 for (const Stmt *Instantiation : Expansion->getInstantiations()) {
6369 ESR = EvaluateStmt(Result, Info, S: Instantiation);
6370 if (ESR == ESR_Failed ||
6371 ShouldPropagateBreakContinue(Info, LoopOrSwitch: Expansion, Scopes: &Scope, ESR))
6372 return ESR;
6373 if (ESR != ESR_Continue) {
6374 // Succeeded here actually means we encountered a 'break'.
6375 assert(ESR == ESR_Succeeded || ESR == ESR_Returned);
6376 break;
6377 }
6378 }
6379
6380 // Map Continue back to Succeeded if we fell off the end of the loop.
6381 if (ESR == ESR_Continue)
6382 ESR = ESR_Succeeded;
6383
6384 return Scope.destroy() ? ESR : ESR_Failed;
6385 }
6386
6387 case Stmt::SwitchStmtClass:
6388 return EvaluateSwitch(Result, Info, SS: cast<SwitchStmt>(Val: S));
6389
6390 case Stmt::ContinueStmtClass:
6391 case Stmt::BreakStmtClass: {
6392 auto *B = cast<LoopControlStmt>(Val: S);
6393 Info.BreakContinueStack.push_back(Elt: B->getNamedLoopOrSwitch());
6394 return isa<ContinueStmt>(Val: S) ? ESR_Continue : ESR_Break;
6395 }
6396
6397 case Stmt::LabelStmtClass:
6398 return EvaluateStmt(Result, Info, S: cast<LabelStmt>(Val: S)->getSubStmt(), Case);
6399
6400 case Stmt::AttributedStmtClass: {
6401 const auto *AS = cast<AttributedStmt>(Val: S);
6402 const auto *SS = AS->getSubStmt();
6403 MSConstexprContextRAII ConstexprContext(
6404 *Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(container: AS->getAttrs()) &&
6405 isa<ReturnStmt>(Val: SS));
6406
6407 auto LO = Info.Ctx.getLangOpts();
6408 if (LO.CXXAssumptions && !LO.MSVCCompat) {
6409 for (auto *Attr : AS->getAttrs()) {
6410 auto *AA = dyn_cast<CXXAssumeAttr>(Val: Attr);
6411 if (!AA)
6412 continue;
6413
6414 auto *Assumption = AA->getAssumption();
6415 if (Assumption->isValueDependent())
6416 return ESR_Failed;
6417
6418 if (Assumption->HasSideEffects(Ctx: Info.Ctx))
6419 continue;
6420
6421 bool Value;
6422 if (!EvaluateAsBooleanCondition(E: Assumption, Result&: Value, Info))
6423 return ESR_Failed;
6424 if (!Value) {
6425 Info.CCEDiag(Loc: Assumption->getExprLoc(),
6426 DiagId: diag::note_constexpr_assumption_failed);
6427 return ESR_Failed;
6428 }
6429 }
6430 }
6431
6432 return EvaluateStmt(Result, Info, S: SS, Case);
6433 }
6434
6435 case Stmt::CaseStmtClass:
6436 case Stmt::DefaultStmtClass:
6437 return EvaluateStmt(Result, Info, S: cast<SwitchCase>(Val: S)->getSubStmt(), Case);
6438 case Stmt::CXXTryStmtClass:
6439 // Evaluate try blocks by evaluating all sub statements.
6440 return EvaluateStmt(Result, Info, S: cast<CXXTryStmt>(Val: S)->getTryBlock(), Case);
6441 }
6442}
6443
6444/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
6445/// default constructor. If so, we'll fold it whether or not it's marked as
6446/// constexpr. If it is marked as constexpr, we will never implicitly define it,
6447/// so we need special handling.
6448static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
6449 const CXXConstructorDecl *CD,
6450 bool IsValueInitialization) {
6451 if (!CD->isTrivial() || !CD->isDefaultConstructor())
6452 return false;
6453
6454 // Value-initialization does not call a trivial default constructor, so such a
6455 // call is a core constant expression whether or not the constructor is
6456 // constexpr.
6457 if (!CD->isConstexpr() && !IsValueInitialization) {
6458 if (Info.getLangOpts().CPlusPlus11) {
6459 // FIXME: If DiagDecl is an implicitly-declared special member function,
6460 // we should be much more explicit about why it's not constexpr.
6461 Info.CCEDiag(Loc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6462 << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
6463 Info.Note(Loc: CD->getLocation(), DiagId: diag::note_declared_at);
6464 } else {
6465 Info.CCEDiag(Loc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6466 }
6467 }
6468 return true;
6469}
6470
6471/// CheckConstexprFunction - Check that a function can be called in a constant
6472/// expression.
6473static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
6474 const FunctionDecl *Declaration,
6475 const FunctionDecl *Definition,
6476 const Stmt *Body) {
6477 // Potential constant expressions can contain calls to declared, but not yet
6478 // defined, constexpr functions.
6479 if (Info.checkingPotentialConstantExpression() && !Definition &&
6480 Declaration->isConstexpr())
6481 return false;
6482
6483 // Bail out if the function declaration itself is invalid. We will
6484 // have produced a relevant diagnostic while parsing it, so just
6485 // note the problematic sub-expression.
6486 if (Declaration->isInvalidDecl()) {
6487 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6488 return false;
6489 }
6490
6491 // DR1872: An instantiated virtual constexpr function can't be called in a
6492 // constant expression (prior to C++20). We can still constant-fold such a
6493 // call.
6494 if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Val: Declaration) &&
6495 cast<CXXMethodDecl>(Val: Declaration)->isVirtual())
6496 Info.CCEDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_call);
6497
6498 if (Definition && Definition->isInvalidDecl()) {
6499 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6500 return false;
6501 }
6502
6503 // Can we evaluate this function call?
6504 if (Definition && Body &&
6505 (Definition->isConstexpr() || (Info.CurrentCall->CanEvalMSConstexpr &&
6506 Definition->hasAttr<MSConstexprAttr>())))
6507 return true;
6508
6509 const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
6510 // Special note for the assert() macro, as the normal error message falsely
6511 // implies we cannot use an assertion during constant evaluation.
6512 if (CallLoc.isMacroID() && DiagDecl->getIdentifier()) {
6513 // FIXME: Instead of checking for an implementation-defined function,
6514 // check and evaluate the assert() macro.
6515 StringRef Name = DiagDecl->getName();
6516 bool AssertFailed =
6517 Name == "__assert_rtn" || Name == "__assert_fail" || Name == "_wassert";
6518 if (AssertFailed) {
6519 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_assert_failed);
6520 return false;
6521 }
6522 }
6523
6524 if (Info.getLangOpts().CPlusPlus11) {
6525 // If this function is not constexpr because it is an inherited
6526 // non-constexpr constructor, diagnose that directly.
6527 auto *CD = dyn_cast<CXXConstructorDecl>(Val: DiagDecl);
6528 if (CD && CD->isInheritingConstructor()) {
6529 auto *Inherited = CD->getInheritedConstructor().getConstructor();
6530 if (!Inherited->isConstexpr())
6531 DiagDecl = CD = Inherited;
6532 }
6533
6534 // FIXME: If DiagDecl is an implicitly-declared special member function
6535 // or an inheriting constructor, we should be much more explicit about why
6536 // it's not constexpr.
6537 if (CD && CD->isInheritingConstructor())
6538 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_inhctor, ExtraNotes: 1)
6539 << CD->getInheritedConstructor().getConstructor()->getParent();
6540 else
6541 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6542 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
6543 Info.Note(Loc: DiagDecl->getLocation(), DiagId: diag::note_declared_at);
6544 } else {
6545 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6546 }
6547 return false;
6548}
6549
6550namespace {
6551struct CheckDynamicTypeHandler {
6552 AccessKinds AccessKind;
6553 typedef bool result_type;
6554 bool failed() { return false; }
6555 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
6556 return true;
6557 }
6558 bool found(APSInt &Value, QualType SubobjType) { return true; }
6559 bool found(APFloat &Value, QualType SubobjType) { return true; }
6560};
6561} // end anonymous namespace
6562
6563/// Check that we can access the notional vptr of an object / determine its
6564/// dynamic type.
6565static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This,
6566 AccessKinds AK, bool Polymorphic) {
6567 if (This.Designator.Invalid)
6568 return false;
6569
6570 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: This, LValType: QualType());
6571
6572 if (!Obj)
6573 return false;
6574
6575 if (!Obj.Value) {
6576 // The object is not usable in constant expressions, so we can't inspect
6577 // its value to see if it's in-lifetime or what the active union members
6578 // are. We can still check for a one-past-the-end lvalue.
6579 if (This.Designator.isOnePastTheEnd() ||
6580 This.Designator.isMostDerivedAnUnsizedArray()) {
6581 Info.FFDiag(E, DiagId: This.Designator.isOnePastTheEnd()
6582 ? diag::note_constexpr_access_past_end
6583 : diag::note_constexpr_access_unsized_array)
6584 << AK;
6585 return false;
6586 } else if (Polymorphic) {
6587 // Conservatively refuse to perform a polymorphic operation if we would
6588 // not be able to read a notional 'vptr' value.
6589 if (!Info.checkingPotentialConstantExpression() ||
6590 !This.AllowConstexprUnknown) {
6591 APValue Val;
6592 This.moveInto(V&: Val);
6593 QualType StarThisType =
6594 Info.Ctx.getLValueReferenceType(T: This.Designator.getType(Ctx&: Info.Ctx));
6595 Info.FFDiag(E, DiagId: diag::note_constexpr_polymorphic_unknown_dynamic_type)
6596 << AK << Val.getAsString(Ctx: Info.Ctx, Ty: StarThisType);
6597 }
6598 return false;
6599 }
6600 return true;
6601 }
6602
6603 CheckDynamicTypeHandler Handler{.AccessKind: AK};
6604 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
6605}
6606
6607/// Check that the pointee of the 'this' pointer in a member function call is
6608/// either within its lifetime or in its period of construction or destruction.
6609static bool
6610checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E,
6611 const LValue &This,
6612 const CXXMethodDecl *NamedMember) {
6613 return checkDynamicType(
6614 Info, E, This,
6615 AK: isa<CXXDestructorDecl>(Val: NamedMember) ? AK_Destroy : AK_MemberCall, Polymorphic: false);
6616}
6617
6618struct DynamicType {
6619 /// The dynamic class type of the object.
6620 const CXXRecordDecl *Type;
6621 /// The corresponding path length in the lvalue.
6622 unsigned PathLength;
6623};
6624
6625static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator,
6626 unsigned PathLength) {
6627 assert(PathLength >= Designator.MostDerivedPathLength && PathLength <=
6628 Designator.Entries.size() && "invalid path length");
6629 return (PathLength == Designator.MostDerivedPathLength)
6630 ? Designator.MostDerivedType->getAsCXXRecordDecl()
6631 : getAsBaseClass(E: Designator.Entries[PathLength - 1]);
6632}
6633
6634/// Determine the dynamic type of an object.
6635static std::optional<DynamicType> ComputeDynamicType(EvalInfo &Info,
6636 const Expr *E,
6637 LValue &This,
6638 AccessKinds AK) {
6639 // If we don't have an lvalue denoting an object of class type, there is no
6640 // meaningful dynamic type. (We consider objects of non-class type to have no
6641 // dynamic type.)
6642 if (!checkDynamicType(Info, E, This, AK,
6643 Polymorphic: AK != AK_TypeId || This.AllowConstexprUnknown))
6644 return std::nullopt;
6645
6646 if (This.Designator.Invalid)
6647 return std::nullopt;
6648
6649 // Refuse to compute a dynamic type in the presence of virtual bases
6650 // before C++26. This shouldn't happen other than in constant-folding
6651 // situations, since literal types can't have virtual bases.
6652 const CXXRecordDecl *Class =
6653 This.Designator.MostDerivedType->getAsCXXRecordDecl();
6654 if (!Class || (!Info.getLangOpts().CPlusPlus26 && Class->getNumVBases())) {
6655 Info.FFDiag(E);
6656 return std::nullopt;
6657 }
6658
6659 // FIXME: For very deep class hierarchies, it might be beneficial to use a
6660 // binary search here instead. But the overwhelmingly common case is that
6661 // we're not in the middle of a constructor, so it probably doesn't matter
6662 // in practice.
6663 ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries;
6664 for (unsigned PathLength = This.Designator.MostDerivedPathLength;
6665 PathLength <= Path.size(); ++PathLength) {
6666 switch (Info.isEvaluatingCtorDtor(Base: This.getLValueBase(),
6667 Path: Path.slice(N: 0, M: PathLength))) {
6668 case ConstructionPhase::Bases:
6669 case ConstructionPhase::DestroyingBases:
6670 // We're constructing or destroying a base class. This is not the dynamic
6671 // type.
6672 break;
6673
6674 case ConstructionPhase::None:
6675 case ConstructionPhase::AfterBases:
6676 case ConstructionPhase::AfterFields:
6677 case ConstructionPhase::Destroying:
6678 // We've finished constructing the base classes and not yet started
6679 // destroying them again, so this is the dynamic type.
6680 return DynamicType{.Type: getBaseClassType(Designator&: This.Designator, PathLength),
6681 .PathLength: PathLength};
6682 }
6683 }
6684
6685 // CWG issue 1517: we're constructing a base class of the object described by
6686 // 'This', so that object has not yet begun its period of construction and
6687 // any polymorphic operation on it results in undefined behavior.
6688 Info.FFDiag(E);
6689 return std::nullopt;
6690}
6691
6692/// Perform virtual dispatch.
6693static const CXXMethodDecl *HandleVirtualDispatch(
6694 EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found,
6695 llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) {
6696 std::optional<DynamicType> DynType = ComputeDynamicType(
6697 Info, E, This,
6698 AK: isa<CXXDestructorDecl>(Val: Found) ? AK_Destroy : AK_MemberCall);
6699 if (!DynType)
6700 return nullptr;
6701
6702 // Find the final overrider. It must be declared in one of the classes on the
6703 // path from the dynamic type to the static type.
6704 // FIXME: If we ever allow literal types to have virtual base classes, that
6705 // won't be true.
6706 const CXXMethodDecl *Callee = Found;
6707 unsigned PathLength = DynType->PathLength;
6708 for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) {
6709 const CXXRecordDecl *Class = getBaseClassType(Designator&: This.Designator, PathLength);
6710 const CXXMethodDecl *Overrider =
6711 Found->getCorrespondingMethodDeclaredInClass(RD: Class, MayBeBase: false);
6712 if (Overrider) {
6713 Callee = Overrider;
6714 break;
6715 }
6716 }
6717
6718 // C++2a [class.abstract]p6:
6719 // the effect of making a virtual call to a pure virtual function [...] is
6720 // undefined
6721 if (Callee->isPureVirtual()) {
6722 Info.FFDiag(E, DiagId: diag::note_constexpr_pure_virtual_call, ExtraNotes: 1) << Callee;
6723 Info.Note(Loc: Callee->getLocation(), DiagId: diag::note_declared_at);
6724 return nullptr;
6725 }
6726
6727 // If necessary, walk the rest of the path to determine the sequence of
6728 // covariant adjustment steps to apply.
6729 if (!Info.Ctx.hasSameUnqualifiedType(T1: Callee->getReturnType(),
6730 T2: Found->getReturnType())) {
6731 CovariantAdjustmentPath.push_back(Elt: Callee->getReturnType());
6732 for (unsigned CovariantPathLength = PathLength + 1;
6733 CovariantPathLength != This.Designator.Entries.size();
6734 ++CovariantPathLength) {
6735 const CXXRecordDecl *NextClass =
6736 getBaseClassType(Designator&: This.Designator, PathLength: CovariantPathLength);
6737 const CXXMethodDecl *Next =
6738 Found->getCorrespondingMethodDeclaredInClass(RD: NextClass, MayBeBase: false);
6739 if (Next && !Info.Ctx.hasSameUnqualifiedType(
6740 T1: Next->getReturnType(), T2: CovariantAdjustmentPath.back()))
6741 CovariantAdjustmentPath.push_back(Elt: Next->getReturnType());
6742 }
6743 if (!Info.Ctx.hasSameUnqualifiedType(T1: Found->getReturnType(),
6744 T2: CovariantAdjustmentPath.back()))
6745 CovariantAdjustmentPath.push_back(Elt: Found->getReturnType());
6746 }
6747
6748 // Perform 'this' adjustment.
6749 if (!CastToDerivedClass(Info, E, Result&: This, TruncatedType: Callee->getParent(), TruncatedElements: PathLength))
6750 return nullptr;
6751
6752 return Callee;
6753}
6754
6755/// Perform the adjustment from a value returned by a virtual function to
6756/// a value of the statically expected type, which may be a pointer or
6757/// reference to a base class of the returned type.
6758static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E,
6759 APValue &Result,
6760 ArrayRef<QualType> Path) {
6761 assert(Result.isLValue() &&
6762 "unexpected kind of APValue for covariant return");
6763 if (Result.isNullPointer())
6764 return true;
6765
6766 LValue LVal;
6767 LVal.setFrom(Ctx: Info.Ctx, V: Result);
6768
6769 const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl();
6770 for (unsigned I = 1; I != Path.size(); ++I) {
6771 const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl();
6772 assert(OldClass && NewClass && "unexpected kind of covariant return");
6773 if (OldClass != NewClass &&
6774 !CastToBaseClass(Info, E, Result&: LVal, DerivedRD: OldClass, BaseRD: NewClass))
6775 return false;
6776 OldClass = NewClass;
6777 }
6778
6779 LVal.moveInto(V&: Result);
6780 return true;
6781}
6782
6783/// Determine whether \p Base, which is known to be a direct base class of
6784/// \p Derived, is a public base class.
6785static bool isBaseClassPublic(const CXXRecordDecl *Derived,
6786 const CXXRecordDecl *Base) {
6787 for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) {
6788 if (BaseSpec.isVirtual())
6789 continue;
6790 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6791 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6792 return BaseSpec.getAccessSpecifier() == AS_public;
6793 }
6794 for (const CXXBaseSpecifier &BaseSpec : Derived->vbases()) {
6795 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6796 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6797 return BaseSpec.getAccessSpecifier() == AS_public;
6798 }
6799
6800 llvm_unreachable("Base is not a direct base of Derived");
6801}
6802
6803/// Apply the given dynamic cast operation on the provided lvalue.
6804///
6805/// This implements the hard case of dynamic_cast, requiring a "runtime check"
6806/// to find a suitable target subobject.
6807static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E,
6808 LValue &Ptr) {
6809 // We can't do anything with a non-symbolic pointer value.
6810 SubobjectDesignator &D = Ptr.Designator;
6811 if (D.Invalid)
6812 return false;
6813
6814 // C++ [expr.dynamic.cast]p6:
6815 // If v is a null pointer value, the result is a null pointer value.
6816 if (Ptr.isNullPointer() && !E->isGLValue())
6817 return true;
6818
6819 // For all the other cases, we need the pointer to point to an object within
6820 // its lifetime / period of construction / destruction, and we need to know
6821 // its dynamic type.
6822 std::optional<DynamicType> DynType =
6823 ComputeDynamicType(Info, E, This&: Ptr, AK: AK_DynamicCast);
6824 if (!DynType)
6825 return false;
6826
6827 // C++ [expr.dynamic.cast]p7:
6828 // If T is "pointer to cv void", then the result is a pointer to the most
6829 // derived object
6830 if (E->getType()->isVoidPointerType())
6831 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength);
6832
6833 const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl();
6834 assert(C && "dynamic_cast target is not void pointer nor class");
6835 CanQualType CQT = Info.Ctx.getCanonicalTagType(TD: C);
6836
6837 auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) {
6838 // C++ [expr.dynamic.cast]p9:
6839 if (!E->isGLValue()) {
6840 // The value of a failed cast to pointer type is the null pointer value
6841 // of the required result type.
6842 Ptr.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
6843 return true;
6844 }
6845
6846 // A failed cast to reference type throws [...] std::bad_cast.
6847 unsigned DiagKind;
6848 if (!Paths && (declaresSameEntity(D1: DynType->Type, D2: C) ||
6849 DynType->Type->isDerivedFrom(Base: C)))
6850 DiagKind = 0;
6851 else if (!Paths || Paths->begin() == Paths->end())
6852 DiagKind = 1;
6853 else if (Paths->isAmbiguous(BaseType: CQT))
6854 DiagKind = 2;
6855 else {
6856 assert(Paths->front().Access != AS_public && "why did the cast fail?");
6857 DiagKind = 3;
6858 }
6859 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_cast_to_reference_failed)
6860 << DiagKind << Ptr.Designator.getType(Ctx&: Info.Ctx)
6861 << Info.Ctx.getCanonicalTagType(TD: DynType->Type)
6862 << E->getType().getUnqualifiedType();
6863 return false;
6864 };
6865
6866 // Runtime check, phase 1:
6867 // Walk from the base subobject towards the derived object looking for the
6868 // target type.
6869 for (int PathLength = Ptr.Designator.Entries.size();
6870 PathLength >= (int)DynType->PathLength; --PathLength) {
6871 const CXXRecordDecl *Class = getBaseClassType(Designator&: Ptr.Designator, PathLength);
6872 if (declaresSameEntity(D1: Class, D2: C))
6873 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: Class, TruncatedElements: PathLength);
6874 // We can only walk across public inheritance edges.
6875 if (PathLength > (int)DynType->PathLength &&
6876 !isBaseClassPublic(Derived: getBaseClassType(Designator&: Ptr.Designator, PathLength: PathLength - 1),
6877 Base: Class))
6878 return RuntimeCheckFailed(nullptr);
6879 }
6880
6881 // Runtime check, phase 2:
6882 // Search the dynamic type for an unambiguous public base of type C.
6883 CXXBasePaths Paths(/*FindAmbiguities=*/true,
6884 /*RecordPaths=*/true, /*DetectVirtual=*/false);
6885 if (DynType->Type->isDerivedFrom(Base: C, Paths) && !Paths.isAmbiguous(BaseType: CQT) &&
6886 Paths.front().Access == AS_public) {
6887 // Downcast to the dynamic type...
6888 if (!CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength))
6889 return false;
6890 // ... then upcast to the chosen base class subobject.
6891 for (CXXBasePathElement &Elem : Paths.front())
6892 if (!HandleLValueBase(Info, E, Obj&: Ptr, DerivedDecl: Elem.Class, Base: Elem.Base))
6893 return false;
6894 return true;
6895 }
6896
6897 // Otherwise, the runtime check fails.
6898 return RuntimeCheckFailed(&Paths);
6899}
6900
6901namespace {
6902struct StartLifetimeOfUnionMemberHandler {
6903 EvalInfo &Info;
6904 const Expr *LHSExpr;
6905 const FieldDecl *Field;
6906 bool DuringInit;
6907 bool Failed = false;
6908 static const AccessKinds AccessKind = AK_Assign;
6909
6910 typedef bool result_type;
6911 bool failed() { return Failed; }
6912 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
6913 // We are supposed to perform no initialization but begin the lifetime of
6914 // the object. We interpret that as meaning to do what default
6915 // initialization of the object would do if all constructors involved were
6916 // trivial:
6917 // * All base, non-variant member, and array element subobjects' lifetimes
6918 // begin
6919 // * No variant members' lifetimes begin
6920 // * All scalar subobjects whose lifetimes begin have indeterminate values
6921 assert(SubobjType->isUnionType());
6922 if (declaresSameEntity(D1: Subobj.getUnionField(), D2: Field)) {
6923 // This union member is already active. If it's also in-lifetime, there's
6924 // nothing to do.
6925 if (Subobj.getUnionValue().hasValue())
6926 return true;
6927 } else if (DuringInit) {
6928 // We're currently in the process of initializing a different union
6929 // member. If we carried on, that initialization would attempt to
6930 // store to an inactive union member, resulting in undefined behavior.
6931 Info.FFDiag(E: LHSExpr,
6932 DiagId: diag::note_constexpr_union_member_change_during_init);
6933 return false;
6934 }
6935 APValue Result;
6936 Failed = !handleDefaultInitValue(T: Field->getType(), Result);
6937 Subobj.setUnion(Field, Value: Result);
6938 return true;
6939 }
6940 bool found(APSInt &Value, QualType SubobjType) {
6941 llvm_unreachable("wrong value kind for union object");
6942 }
6943 bool found(APFloat &Value, QualType SubobjType) {
6944 llvm_unreachable("wrong value kind for union object");
6945 }
6946};
6947} // end anonymous namespace
6948
6949const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind;
6950
6951/// Handle a builtin simple-assignment or a call to a trivial assignment
6952/// operator whose left-hand side might involve a union member access. If it
6953/// does, implicitly start the lifetime of any accessed union elements per
6954/// C++20 [class.union]5.
6955static bool MaybeHandleUnionActiveMemberChange(EvalInfo &Info,
6956 const Expr *LHSExpr,
6957 const LValue &LHS) {
6958 if (LHS.InvalidBase || LHS.Designator.Invalid)
6959 return false;
6960
6961 llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths;
6962 // C++ [class.union]p5:
6963 // define the set S(E) of subexpressions of E as follows:
6964 unsigned PathLength = LHS.Designator.Entries.size();
6965 for (const Expr *E = LHSExpr; E != nullptr;) {
6966 // -- If E is of the form A.B, S(E) contains the elements of S(A)...
6967 if (auto *ME = dyn_cast<MemberExpr>(Val: E)) {
6968 auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl());
6969 // Note that we can't implicitly start the lifetime of a reference,
6970 // so we don't need to proceed any further if we reach one.
6971 if (!FD || FD->getType()->isReferenceType())
6972 break;
6973
6974 // ... and also contains A.B if B names a union member ...
6975 if (FD->getParent()->isUnion()) {
6976 // ... of a non-class, non-array type, or of a class type with a
6977 // trivial default constructor that is not deleted, or an array of
6978 // such types.
6979 auto *RD =
6980 FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
6981 if (!RD || RD->hasTrivialDefaultConstructor())
6982 UnionPathLengths.push_back(Elt: {PathLength - 1, FD});
6983 }
6984
6985 E = ME->getBase();
6986 --PathLength;
6987 assert(declaresSameEntity(FD,
6988 LHS.Designator.Entries[PathLength]
6989 .getAsBaseOrMember().getPointer()));
6990
6991 // -- If E is of the form A[B] and is interpreted as a built-in array
6992 // subscripting operator, S(E) is [S(the array operand, if any)].
6993 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) {
6994 // Step over an ArrayToPointerDecay implicit cast.
6995 auto *Base = ASE->getBase()->IgnoreImplicit();
6996 if (!Base->getType()->isArrayType())
6997 break;
6998
6999 E = Base;
7000 --PathLength;
7001
7002 } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E)) {
7003 // Step over a derived-to-base conversion.
7004 E = ICE->getSubExpr();
7005 if (ICE->getCastKind() == CK_NoOp)
7006 continue;
7007 if (ICE->getCastKind() != CK_DerivedToBase &&
7008 ICE->getCastKind() != CK_UncheckedDerivedToBase)
7009 break;
7010 // Walk path backwards as we walk up from the base to the derived class.
7011 for (const CXXBaseSpecifier *Elt : llvm::reverse(C: ICE->path())) {
7012 if (Elt->isVirtual()) {
7013 // A class with virtual base classes never has a trivial default
7014 // constructor, so S(E) is empty in this case.
7015 E = nullptr;
7016 break;
7017 }
7018
7019 --PathLength;
7020 assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(),
7021 LHS.Designator.Entries[PathLength]
7022 .getAsBaseOrMember().getPointer()));
7023 }
7024
7025 // -- Otherwise, S(E) is empty.
7026 } else {
7027 break;
7028 }
7029 }
7030
7031 // Common case: no unions' lifetimes are started.
7032 if (UnionPathLengths.empty())
7033 return true;
7034
7035 // if modification of X [would access an inactive union member], an object
7036 // of the type of X is implicitly created
7037 CompleteObject Obj =
7038 findCompleteObject(Info, E: LHSExpr, AK: AK_Assign, LVal: LHS, LValType: LHSExpr->getType());
7039 if (!Obj)
7040 return false;
7041 for (std::pair<unsigned, const FieldDecl *> LengthAndField :
7042 llvm::reverse(C&: UnionPathLengths)) {
7043 // Form a designator for the union object.
7044 SubobjectDesignator D = LHS.Designator;
7045 D.truncate(Ctx&: Info.Ctx, Base: LHS.Base, NewLength: LengthAndField.first);
7046
7047 bool DuringInit = Info.isEvaluatingCtorDtor(Base: LHS.Base, Path: D.Entries) ==
7048 ConstructionPhase::AfterBases;
7049 StartLifetimeOfUnionMemberHandler StartLifetime{
7050 .Info: Info, .LHSExpr: LHSExpr, .Field: LengthAndField.second, .DuringInit: DuringInit};
7051 if (!findSubobject(Info, E: LHSExpr, Obj, Sub: D, handler&: StartLifetime))
7052 return false;
7053 }
7054
7055 return true;
7056}
7057
7058static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg,
7059 CallRef Call, EvalInfo &Info, bool NonNull = false,
7060 APValue **EvaluatedArg = nullptr) {
7061 LValue LV;
7062 // Create the parameter slot and register its destruction. For a vararg
7063 // argument, create a temporary.
7064 // FIXME: For calling conventions that destroy parameters in the callee,
7065 // should we consider performing destruction when the function returns
7066 // instead?
7067 APValue &V = PVD ? Info.CurrentCall->createParam(Args: Call, PVD, LV)
7068 : Info.CurrentCall->createTemporary(Key: Arg, T: Arg->getType(),
7069 Scope: ScopeKind::Call, LV);
7070 if (!EvaluateInPlace(Result&: V, Info, This: LV, E: Arg))
7071 return false;
7072
7073 // Passing a null pointer to an __attribute__((nonnull)) parameter results in
7074 // undefined behavior, so is non-constant.
7075 if (NonNull && V.isLValue() && V.isNullPointer()) {
7076 Info.CCEDiag(E: Arg, DiagId: diag::note_non_null_attribute_failed);
7077 return false;
7078 }
7079
7080 if (EvaluatedArg)
7081 *EvaluatedArg = &V;
7082
7083 return true;
7084}
7085
7086/// Evaluate the arguments to a function call.
7087static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call,
7088 EvalInfo &Info, const FunctionDecl *Callee,
7089 bool RightToLeft = false,
7090 LValue *ObjectArg = nullptr) {
7091 bool Success = true;
7092 llvm::SmallBitVector ForbiddenNullArgs;
7093 if (Callee->hasAttr<NonNullAttr>()) {
7094 ForbiddenNullArgs.resize(N: Args.size());
7095 for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) {
7096 if (!Attr->args_size()) {
7097 ForbiddenNullArgs.set();
7098 break;
7099 } else
7100 for (auto Idx : Attr->args()) {
7101 unsigned ASTIdx = Idx.getASTIndex();
7102 if (ASTIdx >= Args.size())
7103 continue;
7104 ForbiddenNullArgs[ASTIdx] = true;
7105 }
7106 }
7107 }
7108 for (unsigned I = 0; I < Args.size(); I++) {
7109 unsigned Idx = RightToLeft ? Args.size() - I - 1 : I;
7110 const ParmVarDecl *PVD =
7111 Idx < Callee->getNumParams() ? Callee->getParamDecl(i: Idx) : nullptr;
7112 bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx];
7113 APValue *That = nullptr;
7114 if (!EvaluateCallArg(PVD, Arg: Args[Idx], Call, Info, NonNull, EvaluatedArg: &That)) {
7115 // If we're checking for a potential constant expression, evaluate all
7116 // initializers even if some of them fail.
7117 if (!Info.noteFailure())
7118 return false;
7119 Success = false;
7120 }
7121 if (PVD && PVD->isExplicitObjectParameter() && That && That->isLValue())
7122 ObjectArg->setFrom(Ctx: Info.Ctx, V: *That);
7123 }
7124 return Success;
7125}
7126
7127/// Perform a trivial copy from Param, which is the parameter of a copy or move
7128/// constructor or assignment operator.
7129static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param,
7130 const Expr *E, APValue &Result,
7131 bool CopyObjectRepresentation) {
7132 // Find the reference argument.
7133 CallStackFrame *Frame = Info.CurrentCall;
7134 APValue *RefValue = Info.getParamSlot(Call: Frame->Arguments, PVD: Param);
7135 if (!RefValue) {
7136 Info.FFDiag(E);
7137 return false;
7138 }
7139
7140 // Copy out the contents of the RHS object.
7141 LValue RefLValue;
7142 RefLValue.setFrom(Ctx: Info.Ctx, V: *RefValue);
7143 return handleLValueToRValueConversion(
7144 Info, Conv: E, Type: Param->getType().getNonReferenceType(), LVal: RefLValue, RVal&: Result,
7145 WantObjectRepresentation: CopyObjectRepresentation);
7146}
7147
7148/// Evaluate a function call.
7149static bool HandleFunctionCall(SourceLocation CallLoc,
7150 const FunctionDecl *Callee,
7151 const LValue *ObjectArg, const Expr *E,
7152 ArrayRef<const Expr *> Args, CallRef Call,
7153 const Stmt *Body, EvalInfo &Info,
7154 APValue &Result, const LValue *ResultSlot) {
7155 if (!Info.CheckCallLimit(Loc: CallLoc))
7156 return false;
7157
7158 CallStackFrame Frame(Info, E->getSourceRange(), Callee, ObjectArg, E, Call);
7159
7160 // For a trivial copy or move assignment, perform an APValue copy. This is
7161 // essential for unions, where the operations performed by the assignment
7162 // operator cannot be represented as statements.
7163 //
7164 // Skip this for non-union classes with no fields; in that case, the defaulted
7165 // copy/move does not actually read the object.
7166 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: Callee);
7167
7168 auto IsTrivialMemoryOperation = [&](const CXXMethodDecl *MD) {
7169 if (!MD || !MD->isDefaulted())
7170 return false;
7171 if (!MD->isCopyAssignmentOperator() && !MD->isMoveAssignmentOperator())
7172 return false;
7173 return MD->getParent()->isUnion() ||
7174 (MD->isTrivial() &&
7175 isReadByLvalueToRvalueConversion(RD: MD->getParent()));
7176 };
7177
7178 if (IsTrivialMemoryOperation(MD)) {
7179 unsigned ExplicitOffset = MD->isExplicitObjectMemberFunction() ? 1 : 0;
7180 assert(ObjectArg);
7181 APValue RHSValue;
7182 if (!handleTrivialCopy(Info, Param: MD->getParamDecl(i: 0), E: Args[0], Result&: RHSValue,
7183 CopyObjectRepresentation: MD->getParent()->isUnion()))
7184 return false;
7185
7186 LValue Obj;
7187 if (!handleAssignment(Info, E: Args[ExplicitOffset], LVal: *ObjectArg,
7188 LValType: MD->getFunctionObjectParameterReferenceType(),
7189 Val&: RHSValue))
7190 return false;
7191 ObjectArg->moveInto(V&: Result);
7192 return true;
7193 } else if (MD && isLambdaCallOperator(MD)) {
7194 // We're in a lambda; determine the lambda capture field maps unless we're
7195 // just constexpr checking a lambda's call operator. constexpr checking is
7196 // done before the captures have been added to the closure object (unless
7197 // we're inferring constexpr-ness), so we don't have access to them in this
7198 // case. But since we don't need the captures to constexpr check, we can
7199 // just ignore them.
7200 if (!Info.checkingPotentialConstantExpression())
7201 MD->getParent()->getCaptureFields(Captures&: Frame.LambdaCaptureFields,
7202 ThisCapture&: Frame.LambdaThisCaptureField);
7203 }
7204
7205 StmtResult Ret = {.Value: Result, .Slot: ResultSlot};
7206 EvalStmtResult ESR = EvaluateStmt(Result&: Ret, Info, S: Body);
7207 if (ESR == ESR_Succeeded) {
7208 if (Callee->getReturnType()->isVoidType())
7209 return true;
7210 Info.FFDiag(Loc: Callee->getEndLoc(), DiagId: diag::note_constexpr_no_return);
7211 }
7212 return ESR == ESR_Returned;
7213}
7214
7215static bool HandleConstructorCall(const Expr *E, const LValue &This,
7216 CallRef Call,
7217 const CXXConstructorDecl *Definition,
7218 EvalInfo &Info, APValue &Result,
7219 bool IsCompleteClass = true);
7220
7221static bool HandleConstructorCall(const Expr *E, const LValue &This,
7222 ArrayRef<const Expr *> Args,
7223 const CXXConstructorDecl *Definition,
7224 EvalInfo &Info, APValue &Result,
7225 bool IsCompleteClass = true) {
7226 CallScopeRAII CallScope(Info);
7227 CallRef Call = Info.CurrentCall->createCall(Callee: Definition);
7228 if (!EvaluateArgs(Args, Call, Info, Callee: Definition))
7229 return false;
7230
7231 return HandleConstructorCall(E, This, Call, Definition, Info, Result,
7232 IsCompleteClass) &&
7233 CallScope.destroy();
7234}
7235
7236/// Evaluate a constructor call.
7237static bool HandleConstructorCall(const Expr *E, const LValue &This,
7238 CallRef Call,
7239 const CXXConstructorDecl *Definition,
7240 EvalInfo &Info, APValue &Result,
7241 bool IsCompleteClass) {
7242
7243 SourceLocation CallLoc = E->getExprLoc();
7244 if (!Info.CheckCallLimit(Loc: CallLoc))
7245 return false;
7246
7247 const CXXRecordDecl *RD = Definition->getParent();
7248 if (!Info.getLangOpts().CPlusPlus26 && RD->getNumVBases()) {
7249 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_base) << RD;
7250 return false;
7251 }
7252
7253 EvalInfo::EvaluatingConstructorRAII EvalObj(
7254 Info,
7255 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
7256 RD->getNumBases());
7257 CallStackFrame Frame(Info, E->getSourceRange(), Definition, &This, E, Call);
7258
7259 // FIXME: Creating an APValue just to hold a nonexistent return value is
7260 // wasteful.
7261 APValue RetVal;
7262 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7263
7264 // If it's a delegating constructor, delegate.
7265 if (Definition->isDelegatingConstructor()) {
7266 CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
7267 if ((*I)->getInit()->isValueDependent()) {
7268 if (!EvaluateDependentExpr(E: (*I)->getInit(), Info))
7269 return false;
7270 } else {
7271 FullExpressionRAII InitScope(Info);
7272 if (!EvaluateInPlace(Result, Info, This, E: (*I)->getInit()) ||
7273 !InitScope.destroy())
7274 return false;
7275 }
7276 return EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed;
7277 }
7278
7279 // For a trivial copy or move constructor, perform an APValue copy. This is
7280 // essential for unions (or classes with anonymous union members), where the
7281 // operations performed by the constructor cannot be represented by
7282 // ctor-initializers.
7283 //
7284 // Skip this for empty non-union classes; we should not perform an
7285 // lvalue-to-rvalue conversion on them because their copy constructor does not
7286 // actually read them.
7287 if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() &&
7288 (Definition->getParent()->isUnion() ||
7289 (Definition->isTrivial() &&
7290 isReadByLvalueToRvalueConversion(RD: Definition->getParent())))) {
7291 return handleTrivialCopy(Info, Param: Definition->getParamDecl(i: 0), E, Result,
7292 CopyObjectRepresentation: Definition->getParent()->isUnion());
7293 }
7294
7295 // Reserve space for the struct members.
7296 if (!Result.hasValue()) {
7297 if (!RD->isUnion()) {
7298 unsigned NonVirtualBases = countNonVirtualBases(RD);
7299 Result = APValue(APValue::UninitStruct(), NonVirtualBases,
7300 RD->getNumFields(), RD->getNumVBases());
7301 } else
7302 // A union starts with no active member.
7303 Result = APValue((const FieldDecl*)nullptr);
7304 }
7305
7306 if (RD->isInvalidDecl()) return false;
7307 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7308
7309 // A scope for temporaries lifetime-extended by reference members.
7310 BlockScopeRAII LifetimeExtendedScope(Info);
7311
7312 bool Success = true;
7313 unsigned BasesSeen = 0;
7314 unsigned VirtualBasesSeen = 0;
7315 unsigned NonVirtualBases = countNonVirtualBases(RD);
7316
7317 CXXRecordDecl::field_iterator FieldIt = RD->field_begin();
7318 auto SkipToField = [&](FieldDecl *FD, bool Indirect) {
7319 // We might be initializing the same field again if this is an indirect
7320 // field initialization.
7321 if (FieldIt == RD->field_end() ||
7322 FieldIt->getFieldIndex() > FD->getFieldIndex()) {
7323 assert(Indirect && "fields out of order?");
7324 return;
7325 }
7326
7327 // Default-initialize any fields with no explicit initializer.
7328 for (; !declaresSameEntity(D1: *FieldIt, D2: FD); ++FieldIt) {
7329 assert(FieldIt != RD->field_end() && "missing field?");
7330 if (!FieldIt->isUnnamedBitField())
7331 Success &= handleDefaultInitValue(
7332 T: FieldIt->getType(),
7333 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7334 }
7335 ++FieldIt;
7336 };
7337 for (const auto *I : Definition->inits()) {
7338 LValue Subobject = This;
7339 LValue SubobjectParent = This;
7340 APValue *Value = &Result;
7341
7342 // Determine the subobject to initialize.
7343 FieldDecl *FD = nullptr;
7344 if (I->isBaseInitializer()) {
7345 QualType BaseType(I->getBaseClass(), 0);
7346 if (I->isBaseVirtual()) {
7347 if (This.pointsToCompleteClass(D: RD)) {
7348 if (!HandleLValueDirectVirtualBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7349 Base: BaseType->getAsCXXRecordDecl(),
7350 RL: &Layout))
7351 return false;
7352 Value = &Result.getStructVirtualBase(i: VirtualBasesSeen++);
7353 } else {
7354 continue;
7355 }
7356
7357 } else {
7358 if (!HandleLValueDirectBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7359 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7360 return false;
7361 Value = &Result.getStructBase(i: BasesSeen++);
7362 }
7363 } else if ((FD = I->getMember())) {
7364 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD, RL: &Layout))
7365 return false;
7366 if (RD->isUnion()) {
7367 Result = APValue(FD);
7368 Value = &Result.getUnionValue();
7369 } else {
7370 SkipToField(FD, false);
7371 Value = &Result.getStructField(i: FD->getFieldIndex());
7372 }
7373 } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) {
7374 // Walk the indirect field decl's chain to find the object to initialize,
7375 // and make sure we've initialized every step along it.
7376 auto IndirectFieldChain = IFD->chain();
7377 for (auto *C : IndirectFieldChain) {
7378 FD = cast<FieldDecl>(Val: C);
7379 CXXRecordDecl *CD = cast<CXXRecordDecl>(Val: FD->getParent());
7380 // Switch the union field if it differs. This happens if we had
7381 // preceding zero-initialization, and we're now initializing a union
7382 // subobject other than the first.
7383 // FIXME: In this case, the values of the other subobjects are
7384 // specified, since zero-initialization sets all padding bits to zero.
7385 if (!Value->hasValue() ||
7386 (Value->isUnion() &&
7387 !declaresSameEntity(D1: Value->getUnionField(), D2: FD))) {
7388 if (CD->isUnion())
7389 *Value = APValue(FD);
7390 else
7391 // FIXME: This immediately starts the lifetime of all members of
7392 // an anonymous struct. It would be preferable to strictly start
7393 // member lifetime in initialization order.
7394 Success &= handleDefaultInitValue(T: Info.Ctx.getCanonicalTagType(TD: CD),
7395 Result&: *Value);
7396 }
7397 // Store Subobject as its parent before updating it for the last element
7398 // in the chain.
7399 if (C == IndirectFieldChain.back())
7400 SubobjectParent = Subobject;
7401 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD))
7402 return false;
7403 if (CD->isUnion())
7404 Value = &Value->getUnionValue();
7405 else {
7406 if (C == IndirectFieldChain.front() && !RD->isUnion())
7407 SkipToField(FD, true);
7408 Value = &Value->getStructField(i: FD->getFieldIndex());
7409 }
7410 }
7411 } else {
7412 llvm_unreachable("unknown base initializer kind");
7413 }
7414
7415 // Need to override This for implicit field initializers as in this case
7416 // This refers to innermost anonymous struct/union containing initializer,
7417 // not to currently constructed class.
7418 const Expr *Init = I->getInit();
7419 if (Init->isValueDependent()) {
7420 if (!EvaluateDependentExpr(E: Init, Info))
7421 return false;
7422 } else {
7423 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent,
7424 isa<CXXDefaultInitExpr>(Val: Init));
7425 FullExpressionRAII InitScope(Info);
7426 if (FD && FD->getType()->isReferenceType() &&
7427 !FD->getType()->isFunctionReferenceType()) {
7428 LValue Result;
7429 if (!EvaluateInitForDeclOfReferenceType(Info, D: FD, Init, Result,
7430 Val&: *Value)) {
7431 if (!Info.noteFailure())
7432 return false;
7433 Success = false;
7434 }
7435 } else if (!EvaluateInPlace(Result&: *Value, Info, This: Subobject, E: Init) ||
7436 (FD && FD->isBitField() &&
7437 !truncateBitfieldValue(Info, E: Init, Value&: *Value, FD))) {
7438 // If we're checking for a potential constant expression, evaluate all
7439 // initializers even if some of them fail.
7440 if (!Info.noteFailure())
7441 return false;
7442 Success = false;
7443 }
7444 }
7445
7446 // This is the point at which the dynamic type of the object becomes this
7447 // class type.
7448 if (I->isBaseInitializer() && BasesSeen == NonVirtualBases)
7449 EvalObj.finishedConstructingBases();
7450 }
7451
7452 // Default-initialize any remaining fields.
7453 if (!RD->isUnion()) {
7454 for (; FieldIt != RD->field_end(); ++FieldIt) {
7455 if (!FieldIt->isUnnamedBitField())
7456 Success &= handleDefaultInitValue(
7457 T: FieldIt->getType(),
7458 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7459 }
7460 }
7461
7462 EvalObj.finishedConstructingFields();
7463
7464 return Success &&
7465 EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed &&
7466 LifetimeExtendedScope.destroy();
7467}
7468
7469static bool HandleDestructionImpl(EvalInfo &Info, SourceRange CallRange,
7470 const LValue &This, APValue &Value,
7471 QualType T, bool IsCompleteClass = true) {
7472 // Objects can only be destroyed while they're within their lifetimes.
7473 // FIXME: We have no representation for whether an object of type nullptr_t
7474 // is in its lifetime; it usually doesn't matter. Perhaps we should model it
7475 // as indeterminate instead?
7476 if (Value.isAbsent() && !T->isNullPtrType()) {
7477 APValue Printable;
7478 This.moveInto(V&: Printable);
7479 Info.FFDiag(Loc: CallRange.getBegin(),
7480 DiagId: diag::note_constexpr_destroy_out_of_lifetime)
7481 << Printable.getAsString(Ctx: Info.Ctx, Ty: Info.Ctx.getLValueReferenceType(T));
7482 return false;
7483 }
7484
7485 // Invent an expression for location purposes.
7486 // FIXME: We shouldn't need to do this.
7487 OpaqueValueExpr LocE(CallRange.getBegin(), Info.Ctx.IntTy, VK_PRValue);
7488
7489 // For arrays, destroy elements right-to-left.
7490 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) {
7491 uint64_t Size = CAT->getZExtSize();
7492 QualType ElemT = CAT->getElementType();
7493
7494 if (!CheckArraySize(Info, CAT, CallLoc: CallRange.getBegin()))
7495 return false;
7496
7497 LValue ElemLV = This;
7498 ElemLV.addArray(Info, E: &LocE, CAT);
7499 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: Size))
7500 return false;
7501
7502 // Ensure that we have actual array elements available to destroy; the
7503 // destructors might mutate the value, so we can't run them on the array
7504 // filler.
7505 if (Size && Size > Value.getArrayInitializedElts())
7506 expandArray(Array&: Value, Index: Value.getArraySize() - 1);
7507
7508 // The size of the array might have been reduced by
7509 // a placement new.
7510 for (Size = Value.getArraySize(); Size != 0; --Size) {
7511 APValue &Elem = Value.getArrayInitializedElt(I: Size - 1);
7512 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: -1) ||
7513 !HandleDestructionImpl(Info, CallRange, This: ElemLV, Value&: Elem, T: ElemT))
7514 return false;
7515 }
7516
7517 // End the lifetime of this array now.
7518 Value = APValue();
7519 return true;
7520 }
7521
7522 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
7523 if (!RD) {
7524 if (T.isDestructedType()) {
7525 Info.FFDiag(Loc: CallRange.getBegin(),
7526 DiagId: diag::note_constexpr_unsupported_destruction)
7527 << T;
7528 return false;
7529 }
7530
7531 Value = APValue();
7532 return true;
7533 }
7534
7535 if (!Info.getLangOpts().CPlusPlus26 && RD->getNumVBases()) {
7536 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_virtual_base) << RD;
7537 return false;
7538 }
7539
7540 // If an anonymous union would be destroyed, some enclosing destructor must
7541 // have been explicitly defined, and the anonymous union destruction should
7542 // have no effect.
7543 if (RD->isAnonymousStructOrUnion() && RD->isUnion()) {
7544 Value = APValue();
7545 return true;
7546 }
7547
7548 const CXXDestructorDecl *DD = RD->getDestructor();
7549 if (!DD && !RD->hasTrivialDestructor()) {
7550 Info.FFDiag(Loc: CallRange.getBegin());
7551 return false;
7552 }
7553
7554 if (!DD || DD->isTrivial()) {
7555 // A trivial destructor just ends the lifetime of the object. Check for
7556 // this case before checking for a body, because we might not bother
7557 // building a body for a trivial destructor. Note that it doesn't matter
7558 // whether the destructor is constexpr in this case; all trivial
7559 // destructors are constexpr.
7560 Value = APValue();
7561 return true;
7562 }
7563
7564 if (!Info.CheckCallLimit(Loc: CallRange.getBegin()))
7565 return false;
7566
7567 const FunctionDecl *Definition = nullptr;
7568 const Stmt *Body = DD->getBody(Definition);
7569
7570 if (!CheckConstexprFunction(Info, CallLoc: CallRange.getBegin(), Declaration: DD, Definition, Body))
7571 return false;
7572
7573 CallStackFrame Frame(Info, CallRange, Definition, &This, /*CallExpr=*/nullptr,
7574 CallRef());
7575
7576 // We're now in the period of destruction of this object.
7577 EvalInfo::EvaluatingDestructorRAII EvalObj(
7578 Info,
7579 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries});
7580 unsigned NonVirtualBases = countNonVirtualBases(RD);
7581 unsigned NumVirtualBases = RD->getNumVBases();
7582 unsigned BasesLeft = NonVirtualBases;
7583 if (!EvalObj.DidInsert) {
7584 // C++2a [class.dtor]p19:
7585 // the behavior is undefined if the destructor is invoked for an object
7586 // whose lifetime has ended
7587 // (Note that formally the lifetime ends when the period of destruction
7588 // begins, even though certain uses of the object remain valid until the
7589 // period of destruction ends.)
7590 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_double_destroy);
7591 return false;
7592 }
7593
7594 // FIXME: Creating an APValue just to hold a nonexistent return value is
7595 // wasteful.
7596 APValue RetVal;
7597 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7598 if (EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) == ESR_Failed)
7599 return false;
7600
7601 // A union destructor does not implicitly destroy its members.
7602 if (RD->isUnion())
7603 return true;
7604
7605 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7606
7607 // We don't have a good way to iterate fields in reverse, so collect all the
7608 // fields first and then walk them backwards.
7609 SmallVector<FieldDecl*, 16> Fields(RD->fields());
7610 for (const FieldDecl *FD : llvm::reverse(C&: Fields)) {
7611 if (FD->isUnnamedBitField())
7612 continue;
7613
7614 LValue Subobject = This;
7615 if (!HandleLValueMember(Info, E: &LocE, LVal&: Subobject, FD, RL: &Layout))
7616 return false;
7617
7618 APValue *SubobjectValue = &Value.getStructField(i: FD->getFieldIndex());
7619 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7620 T: FD->getType()))
7621 return false;
7622 }
7623
7624 if (BasesLeft != 0 || NumVirtualBases != 0)
7625 EvalObj.startedDestroyingBases();
7626
7627 // Destroy base classes in reverse order.
7628 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->bases())) {
7629 if (Base.isVirtual())
7630 continue;
7631 --BasesLeft;
7632
7633 QualType BaseType = Base.getType();
7634 LValue Subobject = This;
7635 if (!HandleLValueDirectBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7636 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7637 return false;
7638
7639 APValue *SubobjectValue = &Value.getStructBase(i: BasesLeft);
7640 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7641 T: BaseType, /*IsCompleteClass=*/false))
7642 return false;
7643 }
7644 assert(BasesLeft == 0 && "NumBases was wrong?");
7645
7646 // Virtual bases.
7647 if (IsCompleteClass) {
7648 unsigned VirtualBasesLeft = NumVirtualBases;
7649 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->vbases())) {
7650 --VirtualBasesLeft;
7651
7652 QualType BaseType = Base.getType();
7653 LValue Subobject = This;
7654 if (!HandleLValueDirectVirtualBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7655 Base: BaseType->getAsCXXRecordDecl(),
7656 RL: &Layout))
7657 return false;
7658
7659 APValue *SubobjectValue = &Value.getStructVirtualBase(i: VirtualBasesLeft);
7660 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7661 T: BaseType, /*IsCompleteClass=*/false))
7662 return false;
7663 }
7664 assert(VirtualBasesLeft == 0 && "NumVirtualBases was wrong?");
7665 }
7666
7667 // The period of destruction ends now. The object is gone.
7668 Value = APValue();
7669 return true;
7670}
7671
7672namespace {
7673struct DestroyObjectHandler {
7674 EvalInfo &Info;
7675 const Expr *E;
7676 const LValue &This;
7677 const AccessKinds AccessKind;
7678
7679 typedef bool result_type;
7680 bool failed() { return false; }
7681 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
7682 return HandleDestructionImpl(Info, CallRange: E->getSourceRange(), This, Value&: Subobj,
7683 T: SubobjType);
7684 }
7685 bool found(APSInt &Value, QualType SubobjType) {
7686 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7687 return false;
7688 }
7689 bool found(APFloat &Value, QualType SubobjType) {
7690 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7691 return false;
7692 }
7693};
7694}
7695
7696/// Perform a destructor or pseudo-destructor call on the given object, which
7697/// might in general not be a complete object.
7698static bool HandleDestruction(EvalInfo &Info, const Expr *E,
7699 const LValue &This, QualType ThisType) {
7700 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Destroy, LVal: This, LValType: ThisType);
7701 DestroyObjectHandler Handler = {.Info: Info, .E: E, .This: This, .AccessKind: AK_Destroy};
7702 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
7703}
7704
7705/// Destroy and end the lifetime of the given complete object.
7706static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
7707 APValue::LValueBase LVBase, APValue &Value,
7708 QualType T) {
7709 // If we've had an unmodeled side-effect, we can't rely on mutable state
7710 // (such as the object we're about to destroy) being correct.
7711 if (Info.EvalStatus.HasSideEffects)
7712 return false;
7713
7714 LValue LV;
7715 LV.set(B: {LVBase});
7716 return HandleDestructionImpl(Info, CallRange: Loc, This: LV, Value, T);
7717}
7718
7719/// Perform a call to 'operator new' or to `__builtin_operator_new'.
7720static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E,
7721 LValue &Result) {
7722 if (Info.checkingPotentialConstantExpression() ||
7723 Info.SpeculativeEvaluationDepth)
7724 return false;
7725
7726 // This is permitted only within a call to std::allocator<T>::allocate.
7727 auto Caller = Info.getStdAllocatorCaller(FnName: "allocate");
7728 if (!Caller) {
7729 Info.FFDiag(Loc: E->getExprLoc(), DiagId: Info.getLangOpts().CPlusPlus20
7730 ? diag::note_constexpr_new_untyped
7731 : diag::note_constexpr_new);
7732 return false;
7733 }
7734
7735 QualType ElemType = Caller.ElemType;
7736 if (ElemType->isIncompleteType() || ElemType->isFunctionType()) {
7737 Info.FFDiag(Loc: E->getExprLoc(),
7738 DiagId: diag::note_constexpr_new_not_complete_object_type)
7739 << (ElemType->isIncompleteType() ? 0 : 1) << ElemType;
7740 return false;
7741 }
7742
7743 APSInt ByteSize;
7744 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: ByteSize, Info))
7745 return false;
7746 bool IsNothrow = false;
7747 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) {
7748 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7749 IsNothrow |= E->getType()->isNothrowT();
7750 }
7751
7752 CharUnits ElemSize;
7753 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElemType, Size&: ElemSize))
7754 return false;
7755 APInt Size, Remainder;
7756 APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity());
7757 APInt::udivrem(LHS: ByteSize, RHS: ElemSizeAP, Quotient&: Size, Remainder);
7758 if (Remainder != 0) {
7759 // This likely indicates a bug in the implementation of 'std::allocator'.
7760 Info.FFDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_operator_new_bad_size)
7761 << ByteSize << APSInt(ElemSizeAP, true) << ElemType;
7762 return false;
7763 }
7764
7765 if (!Info.CheckArraySize(Loc: E->getBeginLoc(), BitWidth: ByteSize.getActiveBits(),
7766 ElemCount: Size.getZExtValue(), /*Diag=*/!IsNothrow)) {
7767 if (IsNothrow) {
7768 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
7769 return true;
7770 }
7771 return false;
7772 }
7773
7774 QualType AllocType = Info.Ctx.getConstantArrayType(
7775 EltTy: ElemType, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
7776 APValue *Val = Info.createHeapAlloc(E: Caller.Call, T: AllocType, LV&: Result);
7777 *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue());
7778 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: AllocType));
7779 return true;
7780}
7781
7782static bool hasVirtualDestructor(QualType T) {
7783 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7784 if (CXXDestructorDecl *DD = RD->getDestructor())
7785 return DD->isVirtual();
7786 return false;
7787}
7788
7789static const FunctionDecl *getVirtualOperatorDelete(QualType T) {
7790 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7791 if (CXXDestructorDecl *DD = RD->getDestructor())
7792 return DD->isVirtual() ? DD->getOperatorDelete() : nullptr;
7793 return nullptr;
7794}
7795
7796/// Check that the given object is a suitable pointer to a heap allocation that
7797/// still exists and is of the right kind for the purpose of a deletion.
7798///
7799/// On success, returns the heap allocation to deallocate. On failure, produces
7800/// a diagnostic and returns std::nullopt.
7801static std::optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E,
7802 const LValue &Pointer,
7803 DynAlloc::Kind DeallocKind) {
7804 auto PointerAsString = [&] {
7805 return Pointer.toString(Ctx&: Info.Ctx, T: Info.Ctx.VoidPtrTy);
7806 };
7807
7808 DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>();
7809 if (!DA) {
7810 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_not_heap_alloc)
7811 << PointerAsString();
7812 if (Pointer.Base)
7813 NoteLValueLocation(Info, Base: Pointer.Base);
7814 return std::nullopt;
7815 }
7816
7817 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
7818 if (!Alloc) {
7819 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
7820 return std::nullopt;
7821 }
7822
7823 if (DeallocKind != (*Alloc)->getKind()) {
7824 QualType AllocType = Pointer.Base.getDynamicAllocType();
7825 Info.FFDiag(E, DiagId: diag::note_constexpr_new_delete_mismatch)
7826 << DeallocKind << (*Alloc)->getKind() << AllocType;
7827 NoteLValueLocation(Info, Base: Pointer.Base);
7828 return std::nullopt;
7829 }
7830
7831 bool Subobject = false;
7832 if (DeallocKind == DynAlloc::New) {
7833 Subobject = Pointer.Designator.MostDerivedPathLength != 0 ||
7834 Pointer.Designator.isOnePastTheEnd();
7835 } else {
7836 Subobject = Pointer.Designator.Entries.size() != 1 ||
7837 Pointer.Designator.Entries[0].getAsArrayIndex() != 0;
7838 }
7839 if (Subobject) {
7840 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_subobject)
7841 << PointerAsString() << Pointer.Designator.isOnePastTheEnd();
7842 return std::nullopt;
7843 }
7844
7845 return Alloc;
7846}
7847
7848// Perform a call to 'operator delete' or '__builtin_operator_delete'.
7849static bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) {
7850 if (Info.checkingPotentialConstantExpression() ||
7851 Info.SpeculativeEvaluationDepth)
7852 return false;
7853
7854 // This is permitted only within a call to std::allocator<T>::deallocate.
7855 if (!Info.getStdAllocatorCaller(FnName: "deallocate")) {
7856 Info.FFDiag(Loc: E->getExprLoc());
7857 return true;
7858 }
7859
7860 LValue Pointer;
7861 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Pointer, Info))
7862 return false;
7863 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I)
7864 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7865
7866 if (Pointer.Designator.Invalid)
7867 return false;
7868
7869 // Deleting a null pointer would have no effect, but it's not permitted by
7870 // std::allocator<T>::deallocate's contract.
7871 if (Pointer.isNullPointer()) {
7872 Info.CCEDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_deallocate_null);
7873 return true;
7874 }
7875
7876 if (!CheckDeleteKind(Info, E, Pointer, DeallocKind: DynAlloc::StdAllocator))
7877 return false;
7878
7879 Info.HeapAllocs.erase(x: Pointer.Base.get<DynamicAllocLValue>());
7880 return true;
7881}
7882
7883//===----------------------------------------------------------------------===//
7884// Generic Evaluation
7885//===----------------------------------------------------------------------===//
7886namespace {
7887
7888class BitCastBuffer {
7889 // FIXME: We're going to need bit-level granularity when we support
7890 // bit-fields.
7891 // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but
7892 // we don't support a host or target where that is the case. Still, we should
7893 // use a more generic type in case we ever do.
7894 SmallVector<std::optional<unsigned char>, 32> Bytes;
7895
7896 static_assert(std::numeric_limits<unsigned char>::digits >= 8,
7897 "Need at least 8 bit unsigned char");
7898
7899 bool TargetIsLittleEndian;
7900
7901public:
7902 BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian)
7903 : Bytes(Width.getQuantity()),
7904 TargetIsLittleEndian(TargetIsLittleEndian) {}
7905
7906 [[nodiscard]] bool readObject(CharUnits Offset, CharUnits Width,
7907 SmallVectorImpl<unsigned char> &Output) const {
7908 for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) {
7909 // If a byte of an integer is uninitialized, then the whole integer is
7910 // uninitialized.
7911 if (!Bytes[I.getQuantity()])
7912 return false;
7913 Output.push_back(Elt: *Bytes[I.getQuantity()]);
7914 }
7915 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
7916 std::reverse(first: Output.begin(), last: Output.end());
7917 return true;
7918 }
7919
7920 void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) {
7921 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
7922 std::reverse(first: Input.begin(), last: Input.end());
7923
7924 size_t Index = 0;
7925 for (unsigned char Byte : Input) {
7926 assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?");
7927 Bytes[Offset.getQuantity() + Index] = Byte;
7928 ++Index;
7929 }
7930 }
7931
7932 size_t size() { return Bytes.size(); }
7933};
7934
7935/// Traverse an APValue to produce an BitCastBuffer, emulating how the current
7936/// target would represent the value at runtime.
7937class APValueToBufferConverter {
7938 EvalInfo &Info;
7939 BitCastBuffer Buffer;
7940 const CastExpr *BCE;
7941
7942 APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth,
7943 const CastExpr *BCE)
7944 : Info(Info),
7945 Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()),
7946 BCE(BCE) {}
7947
7948 bool visit(const APValue &Val, QualType Ty) {
7949 return visit(Val, Ty, Offset: CharUnits::fromQuantity(Quantity: 0));
7950 }
7951
7952 // Write out Val with type Ty into Buffer starting at Offset.
7953 bool visit(const APValue &Val, QualType Ty, CharUnits Offset) {
7954 assert((size_t)Offset.getQuantity() <= Buffer.size());
7955
7956 // As a special case, nullptr_t has an indeterminate value.
7957 if (Ty->isNullPtrType())
7958 return true;
7959
7960 // Dig through Src to find the byte at SrcOffset.
7961 switch (Val.getKind()) {
7962 case APValue::Indeterminate:
7963 case APValue::None:
7964 return true;
7965
7966 case APValue::Int:
7967 return visitInt(Val: Val.getInt(), Ty, Offset);
7968 case APValue::Float:
7969 return visitFloat(Val: Val.getFloat(), Ty, Offset);
7970 case APValue::Array:
7971 return visitArray(Val, Ty, Offset);
7972 case APValue::Struct:
7973 return visitRecord(Val, Ty, Offset);
7974 case APValue::Vector:
7975 return visitVector(Val, Ty, Offset);
7976
7977 case APValue::ComplexInt:
7978 case APValue::ComplexFloat:
7979 return visitComplex(Val, Ty, Offset);
7980 case APValue::FixedPoint:
7981 // FIXME: We should support these.
7982
7983 case APValue::LValue:
7984 case APValue::Matrix:
7985 case APValue::Union:
7986 case APValue::MemberPointer:
7987 case APValue::AddrLabelDiff: {
7988 Info.FFDiag(Loc: BCE->getBeginLoc(),
7989 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
7990 << Ty;
7991 return false;
7992 }
7993 }
7994 llvm_unreachable("Unhandled APValue::ValueKind");
7995 }
7996
7997 bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) {
7998 const RecordDecl *RD = Ty->getAsRecordDecl();
7999 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8000
8001 // Visit the base classes.
8002 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8003 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8004 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8005 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8006 const APValue &Base = Val.getStructBase(i: I);
8007
8008 // Can happen in error cases.
8009 if (!Base.isStruct())
8010 return false;
8011
8012 if (!visitRecord(Val: Base, Ty: BS.getType(),
8013 Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset))
8014 return false;
8015 }
8016 }
8017
8018 // Visit the fields.
8019 unsigned FieldIdx = 0;
8020 for (FieldDecl *FD : RD->fields()) {
8021 if (FD->isBitField()) {
8022 Info.FFDiag(Loc: BCE->getBeginLoc(),
8023 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8024 return false;
8025 }
8026
8027 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8028
8029 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 &&
8030 "only bit-fields can have sub-char alignment");
8031 CharUnits FieldOffset =
8032 Info.Ctx.toCharUnitsFromBits(BitSize: FieldOffsetBits) + Offset;
8033 QualType FieldTy = FD->getType();
8034 if (!visit(Val: Val.getStructField(i: FieldIdx), Ty: FieldTy, Offset: FieldOffset))
8035 return false;
8036 ++FieldIdx;
8037 }
8038
8039 return true;
8040 }
8041
8042 bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) {
8043 const auto *CAT =
8044 dyn_cast_or_null<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe());
8045 if (!CAT)
8046 return false;
8047
8048 CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(T: CAT->getElementType());
8049 unsigned NumInitializedElts = Val.getArrayInitializedElts();
8050 unsigned ArraySize = Val.getArraySize();
8051 // First, initialize the initialized elements.
8052 for (unsigned I = 0; I != NumInitializedElts; ++I) {
8053 const APValue &SubObj = Val.getArrayInitializedElt(I);
8054 if (!visit(Val: SubObj, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
8055 return false;
8056 }
8057
8058 // Next, initialize the rest of the array using the filler.
8059 if (Val.hasArrayFiller()) {
8060 const APValue &Filler = Val.getArrayFiller();
8061 for (unsigned I = NumInitializedElts; I != ArraySize; ++I) {
8062 if (!visit(Val: Filler, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
8063 return false;
8064 }
8065 }
8066
8067 return true;
8068 }
8069
8070 bool visitComplex(const APValue &Val, QualType Ty, CharUnits Offset) {
8071 const ComplexType *ComplexTy = Ty->castAs<ComplexType>();
8072 QualType EltTy = ComplexTy->getElementType();
8073 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8074 bool IsInt = Val.isComplexInt();
8075
8076 if (IsInt) {
8077 if (!visitInt(Val: Val.getComplexIntReal(), Ty: EltTy,
8078 Offset: Offset + (0 * EltSizeChars)))
8079 return false;
8080 if (!visitInt(Val: Val.getComplexIntImag(), Ty: EltTy,
8081 Offset: Offset + (1 * EltSizeChars)))
8082 return false;
8083 } else {
8084 if (!visitFloat(Val: Val.getComplexFloatReal(), Ty: EltTy,
8085 Offset: Offset + (0 * EltSizeChars)))
8086 return false;
8087 if (!visitFloat(Val: Val.getComplexFloatImag(), Ty: EltTy,
8088 Offset: Offset + (1 * EltSizeChars)))
8089 return false;
8090 }
8091
8092 return true;
8093 }
8094
8095 bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) {
8096 const VectorType *VTy = Ty->castAs<VectorType>();
8097 QualType EltTy = VTy->getElementType();
8098 unsigned NElts = VTy->getNumElements();
8099
8100 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8101 // Special handling for OpenCL bool vectors:
8102 // Since these vectors are stored as packed bits, but we can't write
8103 // individual bits to the BitCastBuffer, we'll buffer all of the elements
8104 // together into an appropriately sized APInt and write them all out at
8105 // once. Because we don't accept vectors where NElts * EltSize isn't a
8106 // multiple of the char size, there will be no padding space, so we don't
8107 // have to worry about writing data which should have been left
8108 // uninitialized.
8109 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8110
8111 llvm::APInt Res = llvm::APInt::getZero(numBits: NElts);
8112 for (unsigned I = 0; I < NElts; ++I) {
8113 const llvm::APSInt &EltAsInt = Val.getVectorElt(I).getInt();
8114 assert(EltAsInt.isUnsigned() && EltAsInt.getBitWidth() == 1 &&
8115 "bool vector element must be 1-bit unsigned integer!");
8116
8117 Res.insertBits(SubBits: EltAsInt, bitPosition: BigEndian ? (NElts - I - 1) : I);
8118 }
8119
8120 SmallVector<uint8_t, 8> Bytes(NElts / 8);
8121 llvm::StoreIntToMemory(IntVal: Res, Dst: &*Bytes.begin(), StoreBytes: NElts / 8);
8122 Buffer.writeObject(Offset, Input&: Bytes);
8123 } else {
8124 // Iterate over each of the elements and write them out to the buffer at
8125 // the appropriate offset.
8126 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8127 for (unsigned I = 0; I < NElts; ++I) {
8128 if (!visit(Val: Val.getVectorElt(I), Ty: EltTy, Offset: Offset + I * EltSizeChars))
8129 return false;
8130 }
8131 }
8132
8133 return true;
8134 }
8135
8136 bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) {
8137 APSInt AdjustedVal = Val;
8138 unsigned Width = AdjustedVal.getBitWidth();
8139 if (Ty->isBooleanType()) {
8140 Width = Info.Ctx.getTypeSize(T: Ty);
8141 AdjustedVal = AdjustedVal.extend(width: Width);
8142 }
8143
8144 SmallVector<uint8_t, 8> Bytes(Width / 8);
8145 llvm::StoreIntToMemory(IntVal: AdjustedVal, Dst: &*Bytes.begin(), StoreBytes: Width / 8);
8146 Buffer.writeObject(Offset, Input&: Bytes);
8147 return true;
8148 }
8149
8150 bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) {
8151 APSInt AsInt(Val.bitcastToAPInt());
8152 return visitInt(Val: AsInt, Ty, Offset);
8153 }
8154
8155public:
8156 static std::optional<BitCastBuffer>
8157 convert(EvalInfo &Info, const APValue &Src, const CastExpr *BCE) {
8158 CharUnits DstSize = Info.Ctx.getTypeSizeInChars(T: BCE->getType());
8159 APValueToBufferConverter Converter(Info, DstSize, BCE);
8160 if (!Converter.visit(Val: Src, Ty: BCE->getSubExpr()->getType()))
8161 return std::nullopt;
8162 return Converter.Buffer;
8163 }
8164};
8165
8166/// Write an BitCastBuffer into an APValue.
8167class BufferToAPValueConverter {
8168 EvalInfo &Info;
8169 const BitCastBuffer &Buffer;
8170 const CastExpr *BCE;
8171
8172 BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer,
8173 const CastExpr *BCE)
8174 : Info(Info), Buffer(Buffer), BCE(BCE) {}
8175
8176 // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast
8177 // with an invalid type, so anything left is a deficiency on our part (FIXME).
8178 // Ideally this will be unreachable.
8179 std::nullopt_t unsupportedType(QualType Ty) {
8180 Info.FFDiag(Loc: BCE->getBeginLoc(),
8181 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8182 << Ty;
8183 return std::nullopt;
8184 }
8185
8186 std::nullopt_t unrepresentableValue(QualType Ty, const APSInt &Val) {
8187 Info.FFDiag(Loc: BCE->getBeginLoc(),
8188 DiagId: diag::note_constexpr_bit_cast_unrepresentable_value)
8189 << Ty << toString(I: Val, /*Radix=*/10);
8190 return std::nullopt;
8191 }
8192
8193 std::optional<APValue> visit(const BuiltinType *T, CharUnits Offset,
8194 const EnumType *EnumSugar = nullptr) {
8195 if (T->isNullPtrType()) {
8196 uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QT: QualType(T, 0));
8197 return APValue((Expr *)nullptr,
8198 /*Offset=*/CharUnits::fromQuantity(Quantity: NullValue),
8199 APValue::NoLValuePath{}, /*IsNullPtr=*/true);
8200 }
8201
8202 CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T);
8203
8204 // Work around floating point types that contain unused padding bytes. This
8205 // is really just `long double` on x86, which is the only fundamental type
8206 // with padding bytes.
8207 if (T->isRealFloatingType()) {
8208 const llvm::fltSemantics &Semantics =
8209 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8210 unsigned NumBits = llvm::APFloatBase::getSizeInBits(Sem: Semantics);
8211 assert(NumBits % 8 == 0);
8212 CharUnits NumBytes = CharUnits::fromQuantity(Quantity: NumBits / 8);
8213 if (NumBytes != SizeOf)
8214 SizeOf = NumBytes;
8215 }
8216
8217 SmallVector<uint8_t, 8> Bytes;
8218 if (!Buffer.readObject(Offset, Width: SizeOf, Output&: Bytes)) {
8219 // If this is std::byte or unsigned char, then its okay to store an
8220 // indeterminate value.
8221 bool IsStdByte = EnumSugar && EnumSugar->isStdByteType();
8222 bool IsUChar =
8223 !EnumSugar && (T->isSpecificBuiltinType(K: BuiltinType::UChar) ||
8224 T->isSpecificBuiltinType(K: BuiltinType::Char_U));
8225 if (!IsStdByte && !IsUChar) {
8226 QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0);
8227 Info.FFDiag(Loc: BCE->getExprLoc(),
8228 DiagId: diag::note_constexpr_bit_cast_indet_dest)
8229 << DisplayType << Info.Ctx.getLangOpts().CharIsSigned;
8230 return std::nullopt;
8231 }
8232
8233 return APValue::IndeterminateValue();
8234 }
8235
8236 APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true);
8237 llvm::LoadIntFromMemory(IntVal&: Val, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8238
8239 if (T->isIntegralOrEnumerationType()) {
8240 Val.setIsSigned(T->isSignedIntegerOrEnumerationType());
8241
8242 unsigned IntWidth = Info.Ctx.getIntWidth(T: QualType(T, 0));
8243 if (IntWidth != Val.getBitWidth()) {
8244 APSInt Truncated = Val.trunc(width: IntWidth);
8245 if (Truncated.extend(width: Val.getBitWidth()) != Val)
8246 return unrepresentableValue(Ty: QualType(T, 0), Val);
8247 Val = Truncated;
8248 }
8249
8250 return APValue(Val);
8251 }
8252
8253 if (T->isRealFloatingType()) {
8254 const llvm::fltSemantics &Semantics =
8255 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8256 return APValue(APFloat(Semantics, Val));
8257 }
8258
8259 return unsupportedType(Ty: QualType(T, 0));
8260 }
8261
8262 std::optional<APValue> visit(const RecordType *RTy, CharUnits Offset) {
8263 const RecordDecl *RD = RTy->getAsRecordDecl();
8264 if (RD->isInvalidDecl())
8265 return std::nullopt;
8266 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8267
8268 unsigned NumBases = 0;
8269 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
8270 NumBases = CXXRD->getNumBases();
8271
8272 APValue ResultVal(APValue::UninitStruct(), NumBases, RD->getNumFields());
8273
8274 // Visit the base classes.
8275 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8276 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8277 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8278 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8279
8280 std::optional<APValue> SubObj = visitType(
8281 Ty: BS.getType(), Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset);
8282 if (!SubObj)
8283 return std::nullopt;
8284 ResultVal.getStructBase(i: I) = *SubObj;
8285 }
8286 }
8287
8288 // Visit the fields.
8289 unsigned FieldIdx = 0;
8290 for (FieldDecl *FD : RD->fields()) {
8291 // FIXME: We don't currently support bit-fields. A lot of the logic for
8292 // this is in CodeGen, so we need to factor it around.
8293 if (FD->isBitField()) {
8294 Info.FFDiag(Loc: BCE->getBeginLoc(),
8295 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8296 return std::nullopt;
8297 }
8298
8299 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8300 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0);
8301
8302 CharUnits FieldOffset =
8303 CharUnits::fromQuantity(Quantity: FieldOffsetBits / Info.Ctx.getCharWidth()) +
8304 Offset;
8305 QualType FieldTy = FD->getType();
8306 std::optional<APValue> SubObj = visitType(Ty: FieldTy, Offset: FieldOffset);
8307 if (!SubObj)
8308 return std::nullopt;
8309 ResultVal.getStructField(i: FieldIdx) = *SubObj;
8310 ++FieldIdx;
8311 }
8312
8313 return ResultVal;
8314 }
8315
8316 std::optional<APValue> visit(const EnumType *Ty, CharUnits Offset) {
8317 QualType RepresentationType =
8318 Ty->getDecl()->getDefinitionOrSelf()->getIntegerType();
8319 assert(!RepresentationType.isNull() &&
8320 "enum forward decl should be caught by Sema");
8321 const auto *AsBuiltin =
8322 RepresentationType.getCanonicalType()->castAs<BuiltinType>();
8323 // Recurse into the underlying type. Treat std::byte transparently as
8324 // unsigned char.
8325 return visit(T: AsBuiltin, Offset, /*EnumTy=*/EnumSugar: Ty);
8326 }
8327
8328 std::optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) {
8329 size_t Size = Ty->getLimitedSize();
8330 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: Ty->getElementType());
8331
8332 APValue ArrayValue(APValue::UninitArray(), Size, Size);
8333 for (size_t I = 0; I != Size; ++I) {
8334 std::optional<APValue> ElementValue =
8335 visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8336 if (!ElementValue)
8337 return std::nullopt;
8338 ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue);
8339 }
8340
8341 return ArrayValue;
8342 }
8343
8344 std::optional<APValue> visit(const ComplexType *Ty, CharUnits Offset) {
8345 QualType ElementType = Ty->getElementType();
8346 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: ElementType);
8347 bool IsInt = ElementType->isIntegerType();
8348
8349 std::optional<APValue> Values[2];
8350 for (unsigned I = 0; I != 2; ++I) {
8351 Values[I] = visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8352 if (!Values[I])
8353 return std::nullopt;
8354 }
8355
8356 if (IsInt)
8357 return APValue(Values[0]->getInt(), Values[1]->getInt());
8358 return APValue(Values[0]->getFloat(), Values[1]->getFloat());
8359 }
8360
8361 std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) {
8362 QualType EltTy = VTy->getElementType();
8363 unsigned NElts = VTy->getNumElements();
8364 unsigned EltSize =
8365 VTy->isPackedVectorBoolType(ctx: Info.Ctx) ? 1 : Info.Ctx.getTypeSize(T: EltTy);
8366
8367 SmallVector<APValue, 4> Elts;
8368 Elts.reserve(N: NElts);
8369 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8370 // Special handling for OpenCL bool vectors:
8371 // Since these vectors are stored as packed bits, but we can't read
8372 // individual bits from the BitCastBuffer, we'll buffer all of the
8373 // elements together into an appropriately sized APInt and write them all
8374 // out at once. Because we don't accept vectors where NElts * EltSize
8375 // isn't a multiple of the char size, there will be no padding space, so
8376 // we don't have to worry about reading any padding data which didn't
8377 // actually need to be accessed.
8378 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8379
8380 SmallVector<uint8_t, 8> Bytes;
8381 Bytes.reserve(N: NElts / 8);
8382 if (!Buffer.readObject(Offset, Width: CharUnits::fromQuantity(Quantity: NElts / 8), Output&: Bytes))
8383 return std::nullopt;
8384
8385 APSInt SValInt(NElts, true);
8386 llvm::LoadIntFromMemory(IntVal&: SValInt, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8387
8388 for (unsigned I = 0; I < NElts; ++I) {
8389 llvm::APInt Elt =
8390 SValInt.extractBits(numBits: 1, bitPosition: (BigEndian ? NElts - I - 1 : I) * EltSize);
8391 Elts.emplace_back(
8392 Args: APSInt(std::move(Elt), !EltTy->isSignedIntegerType()));
8393 }
8394 } else {
8395 // Iterate over each of the elements and read them from the buffer at
8396 // the appropriate offset.
8397 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8398 for (unsigned I = 0; I < NElts; ++I) {
8399 std::optional<APValue> EltValue =
8400 visitType(Ty: EltTy, Offset: Offset + I * EltSizeChars);
8401 if (!EltValue)
8402 return std::nullopt;
8403 Elts.push_back(Elt: std::move(*EltValue));
8404 }
8405 }
8406
8407 return APValue(Elts.data(), Elts.size());
8408 }
8409
8410 std::optional<APValue> visit(const Type *Ty, CharUnits Offset) {
8411 return unsupportedType(Ty: QualType(Ty, 0));
8412 }
8413
8414 std::optional<APValue> visitType(QualType Ty, CharUnits Offset) {
8415 QualType Can = Ty.getCanonicalType();
8416
8417 switch (Can->getTypeClass()) {
8418#define TYPE(Class, Base) \
8419 case Type::Class: \
8420 return visit(cast<Class##Type>(Can.getTypePtr()), Offset);
8421#define ABSTRACT_TYPE(Class, Base)
8422#define NON_CANONICAL_TYPE(Class, Base) \
8423 case Type::Class: \
8424 llvm_unreachable("non-canonical type should be impossible!");
8425#define DEPENDENT_TYPE(Class, Base) \
8426 case Type::Class: \
8427 llvm_unreachable( \
8428 "dependent types aren't supported in the constant evaluator!");
8429#define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \
8430 case Type::Class: \
8431 llvm_unreachable("either dependent or not canonical!");
8432#include "clang/AST/TypeNodes.inc"
8433 }
8434 llvm_unreachable("Unhandled Type::TypeClass");
8435 }
8436
8437public:
8438 // Pull out a full value of type DstType.
8439 static std::optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer,
8440 const CastExpr *BCE) {
8441 BufferToAPValueConverter Converter(Info, Buffer, BCE);
8442 return Converter.visitType(Ty: BCE->getType(), Offset: CharUnits::fromQuantity(Quantity: 0));
8443 }
8444};
8445
8446static bool checkBitCastConstexprEligibilityType(SourceLocation Loc,
8447 QualType Ty, EvalInfo *Info,
8448 const ASTContext &Ctx,
8449 bool CheckingDest) {
8450 Ty = Ty.getCanonicalType();
8451
8452 auto diag = [&](int Reason) {
8453 if (Info)
8454 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_type)
8455 << CheckingDest << (Reason == 4) << Reason;
8456 return false;
8457 };
8458 auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) {
8459 if (Info)
8460 Info->Note(Loc: NoteLoc, DiagId: diag::note_constexpr_bit_cast_invalid_subtype)
8461 << NoteTy << Construct << Ty;
8462 return false;
8463 };
8464
8465 if (Ty->isUnionType())
8466 return diag(0);
8467 if (Ty->isPointerType())
8468 return diag(1);
8469 if (Ty->isMemberPointerType())
8470 return diag(2);
8471 if (Ty.isVolatileQualified())
8472 return diag(3);
8473
8474 if (RecordDecl *Record = Ty->getAsRecordDecl()) {
8475 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: Record)) {
8476 for (CXXBaseSpecifier &BS : CXXRD->bases())
8477 if (!checkBitCastConstexprEligibilityType(Loc, Ty: BS.getType(), Info, Ctx,
8478 CheckingDest))
8479 return note(1, BS.getType(), BS.getBeginLoc());
8480 }
8481 for (FieldDecl *FD : Record->fields()) {
8482 if (FD->getType()->isReferenceType())
8483 return diag(4);
8484 if (!checkBitCastConstexprEligibilityType(Loc, Ty: FD->getType(), Info, Ctx,
8485 CheckingDest))
8486 return note(0, FD->getType(), FD->getBeginLoc());
8487 }
8488 }
8489
8490 if (Ty->isArrayType() &&
8491 !checkBitCastConstexprEligibilityType(Loc, Ty: Ctx.getBaseElementType(QT: Ty),
8492 Info, Ctx, CheckingDest))
8493 return false;
8494
8495 if (const auto *VTy = Ty->getAs<VectorType>()) {
8496 QualType EltTy = VTy->getElementType();
8497 unsigned NElts = VTy->getNumElements();
8498 unsigned EltSize =
8499 VTy->isPackedVectorBoolType(ctx: Ctx) ? 1 : Ctx.getTypeSize(T: EltTy);
8500
8501 if ((NElts * EltSize) % Ctx.getCharWidth() != 0) {
8502 // The vector's size in bits is not a multiple of the target's byte size,
8503 // so its layout is unspecified. For now, we'll simply treat these cases
8504 // as unsupported (this should only be possible with OpenCL bool vectors
8505 // whose element count isn't a multiple of the byte size).
8506 if (Info)
8507 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_vector)
8508 << QualType(VTy, 0) << EltSize << NElts << Ctx.getCharWidth();
8509 return false;
8510 }
8511
8512 if (EltTy->isRealFloatingType() &&
8513 &Ctx.getFloatTypeSemantics(T: EltTy) == &APFloat::x87DoubleExtended()) {
8514 // The layout for x86_fp80 vectors seems to be handled very inconsistently
8515 // by both clang and LLVM, so for now we won't allow bit_casts involving
8516 // it in a constexpr context.
8517 if (Info)
8518 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8519 << EltTy;
8520 return false;
8521 }
8522 }
8523
8524 return true;
8525}
8526
8527static bool checkBitCastConstexprEligibility(EvalInfo *Info,
8528 const ASTContext &Ctx,
8529 const CastExpr *BCE) {
8530 bool DestOK = checkBitCastConstexprEligibilityType(
8531 Loc: BCE->getBeginLoc(), Ty: BCE->getType(), Info, Ctx, CheckingDest: true);
8532 bool SourceOK = DestOK && checkBitCastConstexprEligibilityType(
8533 Loc: BCE->getBeginLoc(),
8534 Ty: BCE->getSubExpr()->getType(), Info, Ctx, CheckingDest: false);
8535 return SourceOK;
8536}
8537
8538static bool handleRValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8539 const APValue &SourceRValue,
8540 const CastExpr *BCE) {
8541 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8542 "no host or target supports non 8-bit chars");
8543
8544 if (!checkBitCastConstexprEligibility(Info: &Info, Ctx: Info.Ctx, BCE))
8545 return false;
8546
8547 // Read out SourceValue into a char buffer.
8548 std::optional<BitCastBuffer> Buffer =
8549 APValueToBufferConverter::convert(Info, Src: SourceRValue, BCE);
8550 if (!Buffer)
8551 return false;
8552
8553 // Write out the buffer into a new APValue.
8554 std::optional<APValue> MaybeDestValue =
8555 BufferToAPValueConverter::convert(Info, Buffer&: *Buffer, BCE);
8556 if (!MaybeDestValue)
8557 return false;
8558
8559 DestValue = std::move(*MaybeDestValue);
8560 return true;
8561}
8562
8563static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8564 APValue &SourceValue,
8565 const CastExpr *BCE) {
8566 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8567 "no host or target supports non 8-bit chars");
8568 assert(SourceValue.isLValue() &&
8569 "LValueToRValueBitcast requires an lvalue operand!");
8570
8571 LValue SourceLValue;
8572 APValue SourceRValue;
8573 SourceLValue.setFrom(Ctx: Info.Ctx, V: SourceValue);
8574 if (!handleLValueToRValueConversion(
8575 Info, Conv: BCE, Type: BCE->getSubExpr()->getType().withConst(), LVal: SourceLValue,
8576 RVal&: SourceRValue, /*WantObjectRepresentation=*/true))
8577 return false;
8578
8579 return handleRValueToRValueBitCast(Info, DestValue, SourceRValue, BCE);
8580}
8581
8582template <class Derived>
8583class ExprEvaluatorBase
8584 : public ConstStmtVisitor<Derived, bool> {
8585private:
8586 Derived &getDerived() { return static_cast<Derived&>(*this); }
8587 bool DerivedSuccess(const APValue &V, const Expr *E) {
8588 return getDerived().Success(V, E);
8589 }
8590 bool DerivedZeroInitialization(const Expr *E) {
8591 return getDerived().ZeroInitialization(E);
8592 }
8593
8594 // Check whether a conditional operator with a non-constant condition is a
8595 // potential constant expression. If neither arm is a potential constant
8596 // expression, then the conditional operator is not either.
8597 template<typename ConditionalOperator>
8598 void CheckPotentialConstantConditional(const ConditionalOperator *E) {
8599 assert(Info.checkingPotentialConstantExpression());
8600
8601 // Speculatively evaluate both arms.
8602 SmallVector<PartialDiagnosticAt, 8> Diag;
8603 {
8604 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8605 StmtVisitorTy::Visit(E->getFalseExpr());
8606 if (Diag.empty())
8607 return;
8608 }
8609
8610 {
8611 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8612 Diag.clear();
8613 Info.EvalStatus.DiagEmitted = false;
8614 StmtVisitorTy::Visit(E->getTrueExpr());
8615 if (Diag.empty())
8616 return;
8617 }
8618
8619 Error(E, diag::note_constexpr_conditional_never_const);
8620 }
8621
8622
8623 template<typename ConditionalOperator>
8624 bool HandleConditionalOperator(const ConditionalOperator *E) {
8625 bool BoolResult;
8626 if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
8627 if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) {
8628 CheckPotentialConstantConditional(E);
8629 return false;
8630 }
8631 if (Info.noteFailure()) {
8632 StmtVisitorTy::Visit(E->getTrueExpr());
8633 StmtVisitorTy::Visit(E->getFalseExpr());
8634 }
8635 return false;
8636 }
8637
8638 Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
8639 return StmtVisitorTy::Visit(EvalExpr);
8640 }
8641
8642protected:
8643 EvalInfo &Info;
8644 typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy;
8645 typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
8646
8647 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
8648 return Info.CCEDiag(E, DiagId: D);
8649 }
8650
8651 bool ZeroInitialization(const Expr *E) { return Error(E); }
8652
8653 bool IsConstantEvaluatedBuiltinCall(const CallExpr *E) {
8654 unsigned BuiltinOp = E->getBuiltinCallee();
8655 return BuiltinOp != 0 &&
8656 Info.Ctx.BuiltinInfo.isConstantEvaluated(ID: BuiltinOp);
8657 }
8658
8659public:
8660 ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
8661
8662 EvalInfo &getEvalInfo() { return Info; }
8663
8664 /// Report an evaluation error. This should only be called when an error is
8665 /// first discovered. When propagating an error, just return false.
8666 bool Error(const Expr *E, diag::kind D) {
8667 Info.FFDiag(E, DiagId: D) << E->getSourceRange();
8668 return false;
8669 }
8670 bool Error(const Expr *E) {
8671 return Error(E, diag::note_invalid_subexpr_in_const_expr);
8672 }
8673
8674 bool VisitStmt(const Stmt *) {
8675 llvm_unreachable("Expression evaluator should not be called on stmts");
8676 }
8677 bool VisitExpr(const Expr *E) {
8678 return Error(E);
8679 }
8680
8681 bool VisitEmbedExpr(const EmbedExpr *E) {
8682 const auto It = E->begin();
8683 return StmtVisitorTy::Visit(*It);
8684 }
8685
8686 bool VisitPredefinedExpr(const PredefinedExpr *E) {
8687 return StmtVisitorTy::Visit(E->getFunctionName());
8688 }
8689 bool VisitConstantExpr(const ConstantExpr *E) {
8690 if (E->hasAPValueResult())
8691 return DerivedSuccess(V: E->getAPValueResult(), E);
8692
8693 return StmtVisitorTy::Visit(E->getSubExpr());
8694 }
8695
8696 bool VisitParenExpr(const ParenExpr *E)
8697 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8698 bool VisitUnaryExtension(const UnaryOperator *E)
8699 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8700 bool VisitUnaryPlus(const UnaryOperator *E)
8701 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8702 bool VisitChooseExpr(const ChooseExpr *E)
8703 { return StmtVisitorTy::Visit(E->getChosenSubExpr()); }
8704 bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
8705 { return StmtVisitorTy::Visit(E->getResultExpr()); }
8706 bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
8707 { return StmtVisitorTy::Visit(E->getReplacement()); }
8708 bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
8709 TempVersionRAII RAII(*Info.CurrentCall);
8710 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8711 return StmtVisitorTy::Visit(E->getExpr());
8712 }
8713 bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
8714 TempVersionRAII RAII(*Info.CurrentCall);
8715 // The initializer may not have been parsed yet, or might be erroneous.
8716 if (!E->getExpr())
8717 return Error(E);
8718 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8719 return StmtVisitorTy::Visit(E->getExpr());
8720 }
8721
8722 bool VisitExprWithCleanups(const ExprWithCleanups *E) {
8723 FullExpressionRAII Scope(Info);
8724 return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy();
8725 }
8726
8727 // Temporaries are registered when created, so we don't care about
8728 // CXXBindTemporaryExpr.
8729 bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) {
8730 return StmtVisitorTy::Visit(E->getSubExpr());
8731 }
8732
8733 bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
8734 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8735 << diag::ConstexprInvalidCastKind::Reinterpret;
8736 return static_cast<Derived*>(this)->VisitCastExpr(E);
8737 }
8738 bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
8739 if (!Info.Ctx.getLangOpts().CPlusPlus20)
8740 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8741 << diag::ConstexprInvalidCastKind::Dynamic;
8742 return static_cast<Derived*>(this)->VisitCastExpr(E);
8743 }
8744 bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
8745 return static_cast<Derived*>(this)->VisitCastExpr(E);
8746 }
8747
8748 bool VisitBinaryOperator(const BinaryOperator *E) {
8749 switch (E->getOpcode()) {
8750 default:
8751 return Error(E);
8752
8753 case BO_Comma:
8754 VisitIgnoredValue(E: E->getLHS());
8755 return StmtVisitorTy::Visit(E->getRHS());
8756
8757 case BO_PtrMemD:
8758 case BO_PtrMemI: {
8759 LValue Obj;
8760 if (!HandleMemberPointerAccess(Info, BO: E, LV&: Obj))
8761 return false;
8762 APValue Result;
8763 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: Obj, RVal&: Result))
8764 return false;
8765 return DerivedSuccess(V: Result, E);
8766 }
8767 }
8768 }
8769
8770 bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) {
8771 return StmtVisitorTy::Visit(E->getSemanticForm());
8772 }
8773
8774 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
8775 // Evaluate and cache the common expression. We treat it as a temporary,
8776 // even though it's not quite the same thing.
8777 LValue CommonLV;
8778 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8779 Key: E->getOpaqueValue(),
8780 T: getStorageType(Ctx: Info.Ctx, E: E->getOpaqueValue()),
8781 Scope: ScopeKind::FullExpression, LV&: CommonLV),
8782 Info, E: E->getCommon()))
8783 return false;
8784
8785 return HandleConditionalOperator(E);
8786 }
8787
8788 bool VisitConditionalOperator(const ConditionalOperator *E) {
8789 bool IsBcpCall = false;
8790 // If the condition (ignoring parens) is a __builtin_constant_p call,
8791 // the result is a constant expression if it can be folded without
8792 // side-effects. This is an important GNU extension. See GCC PR38377
8793 // for discussion.
8794 if (const CallExpr *CallCE =
8795 dyn_cast<CallExpr>(Val: E->getCond()->IgnoreParenCasts()))
8796 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
8797 IsBcpCall = true;
8798
8799 // Always assume __builtin_constant_p(...) ? ... : ... is a potential
8800 // constant expression; we can't check whether it's potentially foldable.
8801 // FIXME: We should instead treat __builtin_constant_p as non-constant if
8802 // it would return 'false' in this mode.
8803 if (Info.checkingPotentialConstantExpression() && IsBcpCall)
8804 return false;
8805
8806 FoldConstant Fold(Info, IsBcpCall);
8807 if (!HandleConditionalOperator(E)) {
8808 Fold.keepDiagnostics();
8809 return false;
8810 }
8811
8812 return true;
8813 }
8814
8815 bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
8816 if (APValue *Value = Info.CurrentCall->getCurrentTemporary(Key: E);
8817 Value && !Value->isAbsent())
8818 return DerivedSuccess(V: *Value, E);
8819
8820 const Expr *Source = E->getSourceExpr();
8821 if (!Source)
8822 return Error(E);
8823 if (Source == E) {
8824 assert(0 && "OpaqueValueExpr recursively refers to itself");
8825 return Error(E);
8826 }
8827 return StmtVisitorTy::Visit(Source);
8828 }
8829
8830 bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
8831 for (const Expr *SemE : E->semantics()) {
8832 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: SemE)) {
8833 // FIXME: We can't handle the case where an OpaqueValueExpr is also the
8834 // result expression: there could be two different LValues that would
8835 // refer to the same object in that case, and we can't model that.
8836 if (SemE == E->getResultExpr())
8837 return Error(E);
8838
8839 // Unique OVEs get evaluated if and when we encounter them when
8840 // emitting the rest of the semantic form, rather than eagerly.
8841 if (OVE->isUnique())
8842 continue;
8843
8844 LValue LV;
8845 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8846 Key: OVE, T: getStorageType(Ctx: Info.Ctx, E: OVE),
8847 Scope: ScopeKind::FullExpression, LV),
8848 Info, E: OVE->getSourceExpr()))
8849 return false;
8850 } else if (SemE == E->getResultExpr()) {
8851 if (!StmtVisitorTy::Visit(SemE))
8852 return false;
8853 } else {
8854 if (!EvaluateIgnoredValue(Info, E: SemE))
8855 return false;
8856 }
8857 }
8858 return true;
8859 }
8860
8861 bool VisitCallExpr(const CallExpr *E) {
8862 APValue Result;
8863 if (!handleCallExpr(E, Result, ResultSlot: nullptr))
8864 return false;
8865 return DerivedSuccess(V: Result, E);
8866 }
8867
8868 bool handleCallExpr(const CallExpr *E, APValue &Result,
8869 const LValue *ResultSlot) {
8870 CallScopeRAII CallScope(Info);
8871
8872 const Expr *Callee = E->getCallee()->IgnoreParens();
8873 QualType CalleeType = Callee->getType();
8874
8875 const FunctionDecl *FD = nullptr;
8876 LValue *This = nullptr, ObjectArg;
8877 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
8878 bool HasQualifier = false;
8879
8880 CallRef Call;
8881
8882 // Extract function decl and 'this' pointer from the callee.
8883 if (CalleeType->isSpecificBuiltinType(K: BuiltinType::BoundMember)) {
8884 const CXXMethodDecl *Member = nullptr;
8885 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: Callee)) {
8886 // Explicit bound member calls, such as x.f() or p->g();
8887 if (!EvaluateObjectArgument(Info, Object: ME->getBase(), This&: ObjectArg))
8888 return false;
8889 Member = dyn_cast<CXXMethodDecl>(Val: ME->getMemberDecl());
8890 if (!Member)
8891 return Error(Callee);
8892 This = &ObjectArg;
8893 HasQualifier = ME->hasQualifier();
8894 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Val: Callee)) {
8895 // Indirect bound member calls ('.*' or '->*').
8896 const ValueDecl *D =
8897 HandleMemberPointerAccess(Info, BO: BE, LV&: ObjectArg, IncludeMember: false);
8898 if (!D)
8899 return false;
8900 Member = dyn_cast<CXXMethodDecl>(Val: D);
8901 if (!Member)
8902 return Error(Callee);
8903 This = &ObjectArg;
8904 } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Val: Callee)) {
8905 if (!Info.getLangOpts().CPlusPlus20)
8906 Info.CCEDiag(E: PDE, DiagId: diag::note_constexpr_pseudo_destructor);
8907 return EvaluateObjectArgument(Info, Object: PDE->getBase(), This&: ObjectArg) &&
8908 HandleDestruction(Info, E: PDE, This: ObjectArg, ThisType: PDE->getDestroyedType());
8909 } else
8910 return Error(Callee);
8911 FD = Member;
8912 } else if (CalleeType->isFunctionPointerType()) {
8913 LValue CalleeLV;
8914 if (!EvaluatePointer(E: Callee, Result&: CalleeLV, Info))
8915 return false;
8916
8917 if (!CalleeLV.getLValueOffset().isZero())
8918 return Error(Callee);
8919 if (CalleeLV.isNullPointer()) {
8920 Info.FFDiag(E: Callee, DiagId: diag::note_constexpr_null_callee)
8921 << const_cast<Expr *>(Callee);
8922 return false;
8923 }
8924 FD = dyn_cast_or_null<FunctionDecl>(
8925 Val: CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>());
8926 if (!FD)
8927 return Error(Callee);
8928 // Don't call function pointers which have been cast to some other type.
8929 // Per DR (no number yet), the caller and callee can differ in noexcept.
8930 if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec(
8931 T: CalleeType->getPointeeType(), U: FD->getType())) {
8932 return Error(E);
8933 }
8934
8935 // For an (overloaded) assignment expression, evaluate the RHS before the
8936 // LHS.
8937 auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E);
8938 if (OCE && OCE->isAssignmentOp()) {
8939 assert(Args.size() == 2 && "wrong number of arguments in assignment");
8940 Call = Info.CurrentCall->createCall(Callee: FD);
8941 bool HasThis = false;
8942 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FD))
8943 HasThis = MD->isImplicitObjectMemberFunction();
8944 if (!EvaluateArgs(Args: HasThis ? Args.slice(N: 1) : Args, Call, Info, Callee: FD,
8945 /*RightToLeft=*/true, ObjectArg: &ObjectArg))
8946 return false;
8947 }
8948
8949 // Overloaded operator calls to member functions are represented as normal
8950 // calls with '*this' as the first argument.
8951 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
8952 if (MD &&
8953 (MD->isImplicitObjectMemberFunction() || (OCE && MD->isStatic()))) {
8954 // FIXME: When selecting an implicit conversion for an overloaded
8955 // operator delete, we sometimes try to evaluate calls to conversion
8956 // operators without a 'this' parameter!
8957 if (Args.empty())
8958 return Error(E);
8959
8960 if (!EvaluateObjectArgument(Info, Object: Args[0], This&: ObjectArg))
8961 return false;
8962
8963 // If we are calling a static operator, the 'this' argument needs to be
8964 // ignored after being evaluated.
8965 if (MD->isInstance())
8966 This = &ObjectArg;
8967
8968 // If this is syntactically a simple assignment using a trivial
8969 // assignment operator, start the lifetimes of union members as needed,
8970 // per C++20 [class.union]5.
8971 if (Info.getLangOpts().CPlusPlus20 && OCE &&
8972 OCE->getOperator() == OO_Equal && MD->isTrivial() &&
8973 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: Args[0], LHS: ObjectArg))
8974 return false;
8975
8976 Args = Args.slice(N: 1);
8977 } else if (MD && MD->isLambdaStaticInvoker()) {
8978 // Map the static invoker for the lambda back to the call operator.
8979 // Conveniently, we don't have to slice out the 'this' argument (as is
8980 // being done for the non-static case), since a static member function
8981 // doesn't have an implicit argument passed in.
8982 const CXXRecordDecl *ClosureClass = MD->getParent();
8983 assert(
8984 ClosureClass->captures().empty() &&
8985 "Number of captures must be zero for conversion to function-ptr");
8986
8987 const CXXMethodDecl *LambdaCallOp =
8988 ClosureClass->getLambdaCallOperator();
8989
8990 // Set 'FD', the function that will be called below, to the call
8991 // operator. If the closure object represents a generic lambda, find
8992 // the corresponding specialization of the call operator.
8993
8994 if (ClosureClass->isGenericLambda()) {
8995 assert(MD->isFunctionTemplateSpecialization() &&
8996 "A generic lambda's static-invoker function must be a "
8997 "template specialization");
8998 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
8999 FunctionTemplateDecl *CallOpTemplate =
9000 LambdaCallOp->getDescribedFunctionTemplate();
9001 void *InsertPos = nullptr;
9002 FunctionDecl *CorrespondingCallOpSpecialization =
9003 CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertPos);
9004 assert(CorrespondingCallOpSpecialization &&
9005 "We must always have a function call operator specialization "
9006 "that corresponds to our static invoker specialization");
9007 assert(isa<CXXMethodDecl>(CorrespondingCallOpSpecialization));
9008 FD = CorrespondingCallOpSpecialization;
9009 } else
9010 FD = LambdaCallOp;
9011 } else if (FD->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
9012 if (FD->getDeclName().isAnyOperatorNew()) {
9013 LValue Ptr;
9014 if (!HandleOperatorNewCall(Info, E, Result&: Ptr))
9015 return false;
9016 Ptr.moveInto(V&: Result);
9017 return CallScope.destroy();
9018 } else {
9019 return HandleOperatorDeleteCall(Info, E) && CallScope.destroy();
9020 }
9021 }
9022 } else
9023 return Error(E);
9024
9025 // Evaluate the arguments now if we've not already done so.
9026 if (!Call) {
9027 Call = Info.CurrentCall->createCall(Callee: FD);
9028 if (!EvaluateArgs(Args, Call, Info, Callee: FD, /*RightToLeft*/ false,
9029 ObjectArg: &ObjectArg))
9030 return false;
9031 }
9032
9033 SmallVector<QualType, 4> CovariantAdjustmentPath;
9034 if (This) {
9035 auto *NamedMember = dyn_cast<CXXMethodDecl>(Val: FD);
9036 if (NamedMember && NamedMember->isVirtual() && !HasQualifier) {
9037 // Perform virtual dispatch, if necessary.
9038 FD = HandleVirtualDispatch(Info, E, This&: *This, Found: NamedMember,
9039 CovariantAdjustmentPath);
9040 if (!FD)
9041 return false;
9042 } else if (NamedMember && NamedMember->isImplicitObjectMemberFunction()) {
9043 // Check that the 'this' pointer points to an object of the right type.
9044 // FIXME: If this is an assignment operator call, we may need to change
9045 // the active union member before we check this.
9046 if (!checkNonVirtualMemberCallThisPointer(Info, E, This: *This, NamedMember))
9047 return false;
9048 }
9049 }
9050
9051 // Destructor calls are different enough that they have their own codepath.
9052 if (auto *DD = dyn_cast<CXXDestructorDecl>(Val: FD)) {
9053 assert(This && "no 'this' pointer for destructor call");
9054 return HandleDestruction(Info, E, This: *This,
9055 ThisType: Info.Ctx.getCanonicalTagType(TD: DD->getParent())) &&
9056 CallScope.destroy();
9057 }
9058
9059 const FunctionDecl *Definition = nullptr;
9060 Stmt *Body = FD->getBody(Definition);
9061 SourceLocation Loc = E->getExprLoc();
9062
9063 // Treat the object argument as `this` when evaluating defaulted
9064 // special menmber functions
9065 if (FD->hasCXXExplicitFunctionObjectParameter())
9066 This = &ObjectArg;
9067
9068 if (!CheckConstexprFunction(Info, CallLoc: Loc, Declaration: FD, Definition, Body) ||
9069 !HandleFunctionCall(CallLoc: Loc, Callee: Definition, ObjectArg: This, E, Args, Call, Body, Info,
9070 Result, ResultSlot))
9071 return false;
9072
9073 if (!CovariantAdjustmentPath.empty() &&
9074 !HandleCovariantReturnAdjustment(Info, E, Result,
9075 Path: CovariantAdjustmentPath))
9076 return false;
9077
9078 return CallScope.destroy();
9079 }
9080
9081 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9082 return StmtVisitorTy::Visit(E->getInitializer());
9083 }
9084 bool VisitInitListExpr(const InitListExpr *E) {
9085 if (E->getNumInits() == 0)
9086 return DerivedZeroInitialization(E);
9087 if (E->getNumInits() == 1)
9088 return StmtVisitorTy::Visit(E->getInit(Init: 0));
9089 return Error(E);
9090 }
9091 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
9092 return DerivedZeroInitialization(E);
9093 }
9094 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
9095 return DerivedZeroInitialization(E);
9096 }
9097 bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
9098 return DerivedZeroInitialization(E);
9099 }
9100
9101 /// A member expression where the object is a prvalue is itself a prvalue.
9102 bool VisitMemberExpr(const MemberExpr *E) {
9103 assert(!Info.Ctx.getLangOpts().CPlusPlus11 &&
9104 "missing temporary materialization conversion");
9105 assert(!E->isArrow() && "missing call to bound member function?");
9106
9107 APValue Val;
9108 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
9109 return false;
9110
9111 QualType BaseTy = E->getBase()->getType();
9112
9113 const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl());
9114 if (!FD) return Error(E);
9115 assert(!FD->getType()->isReferenceType() && "prvalue reference?");
9116 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9117 FD->getParent()->getCanonicalDecl() &&
9118 "record / field mismatch");
9119
9120 // Note: there is no lvalue base here. But this case should only ever
9121 // happen in C or in C++98, where we cannot be evaluating a constexpr
9122 // constructor, which is the only case the base matters.
9123 CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy);
9124 SubobjectDesignator Designator(BaseTy);
9125 Designator.addDeclUnchecked(D: FD);
9126
9127 APValue Result;
9128 return extractSubobject(Info, E, Obj, Sub: Designator, Result) &&
9129 DerivedSuccess(V: Result, E);
9130 }
9131
9132 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) {
9133 APValue Val;
9134 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
9135 return false;
9136
9137 if (Val.isVector()) {
9138 SmallVector<uint32_t, 4> Indices;
9139 E->getEncodedElementAccess(Elts&: Indices);
9140 if (Indices.size() == 1) {
9141 // Return scalar.
9142 return DerivedSuccess(V: Val.getVectorElt(I: Indices[0]), E);
9143 } else {
9144 // Construct new APValue vector.
9145 SmallVector<APValue, 4> Elts;
9146 for (unsigned I = 0; I < Indices.size(); ++I) {
9147 Elts.push_back(Elt: Val.getVectorElt(I: Indices[I]));
9148 }
9149 APValue VecResult(Elts.data(), Indices.size());
9150 return DerivedSuccess(V: VecResult, E);
9151 }
9152 }
9153
9154 return false;
9155 }
9156
9157 bool VisitCastExpr(const CastExpr *E) {
9158 switch (E->getCastKind()) {
9159 default:
9160 break;
9161
9162 case CK_AtomicToNonAtomic: {
9163 APValue AtomicVal;
9164 // This does not need to be done in place even for class/array types:
9165 // atomic-to-non-atomic conversion implies copying the object
9166 // representation.
9167 if (!Evaluate(Result&: AtomicVal, Info, E: E->getSubExpr()))
9168 return false;
9169 return DerivedSuccess(V: AtomicVal, E);
9170 }
9171
9172 case CK_NoOp:
9173 case CK_UserDefinedConversion:
9174 return StmtVisitorTy::Visit(E->getSubExpr());
9175
9176 case CK_HLSLArrayRValue: {
9177 const Expr *SubExpr = E->getSubExpr();
9178 if (!SubExpr->isGLValue()) {
9179 APValue Val;
9180 if (!Evaluate(Result&: Val, Info, E: SubExpr))
9181 return false;
9182 return DerivedSuccess(V: Val, E);
9183 }
9184
9185 LValue LVal;
9186 if (!EvaluateLValue(E: SubExpr, Result&: LVal, Info))
9187 return false;
9188 APValue RVal;
9189 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9190 if (!handleLValueToRValueConversion(Info, Conv: E, Type: SubExpr->getType(), LVal,
9191 RVal))
9192 return false;
9193 return DerivedSuccess(V: RVal, E);
9194 }
9195 case CK_LValueToRValue: {
9196 LValue LVal;
9197 if (!EvaluateLValue(E: E->getSubExpr(), Result&: LVal, Info))
9198 return false;
9199 APValue RVal;
9200 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9201 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
9202 LVal, RVal))
9203 return false;
9204 return DerivedSuccess(V: RVal, E);
9205 }
9206 case CK_LValueToRValueBitCast: {
9207 APValue DestValue, SourceValue;
9208 if (!Evaluate(Result&: SourceValue, Info, E: E->getSubExpr()))
9209 return false;
9210 if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, BCE: E))
9211 return false;
9212 return DerivedSuccess(V: DestValue, E);
9213 }
9214
9215 case CK_AddressSpaceConversion: {
9216 APValue Value;
9217 if (!Evaluate(Result&: Value, Info, E: E->getSubExpr()))
9218 return false;
9219 return DerivedSuccess(V: Value, E);
9220 }
9221 }
9222
9223 return Error(E);
9224 }
9225
9226 bool VisitUnaryPostInc(const UnaryOperator *UO) {
9227 return VisitUnaryPostIncDec(UO);
9228 }
9229 bool VisitUnaryPostDec(const UnaryOperator *UO) {
9230 return VisitUnaryPostIncDec(UO);
9231 }
9232 bool VisitUnaryPostIncDec(const UnaryOperator *UO) {
9233 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9234 return Error(UO);
9235
9236 LValue LVal;
9237 if (!EvaluateLValue(E: UO->getSubExpr(), Result&: LVal, Info))
9238 return false;
9239 APValue RVal;
9240 if (!handleIncDec(Info&: this->Info, E: UO, LVal, LValType: UO->getSubExpr()->getType(),
9241 IsIncrement: UO->isIncrementOp(), Old: &RVal))
9242 return false;
9243 return DerivedSuccess(V: RVal, E: UO);
9244 }
9245
9246 bool VisitStmtExpr(const StmtExpr *E) {
9247 // We will have checked the full-expressions inside the statement expression
9248 // when they were completed, and don't need to check them again now.
9249 llvm::SaveAndRestore NotCheckingForUB(Info.CheckingForUndefinedBehavior,
9250 false);
9251
9252 const CompoundStmt *CS = E->getSubStmt();
9253 if (CS->body_empty())
9254 return true;
9255
9256 BlockScopeRAII Scope(Info);
9257 for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
9258 BE = CS->body_end();
9259 /**/; ++BI) {
9260 if (BI + 1 == BE) {
9261 const Expr *FinalExpr = dyn_cast<Expr>(Val: *BI);
9262 if (!FinalExpr) {
9263 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9264 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9265 return false;
9266 }
9267 return this->Visit(FinalExpr) && Scope.destroy();
9268 }
9269
9270 APValue ReturnValue;
9271 StmtResult Result = { .Value: ReturnValue, .Slot: nullptr };
9272 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: *BI);
9273 if (ESR != ESR_Succeeded) {
9274 // FIXME: If the statement-expression terminated due to 'return',
9275 // 'break', or 'continue', it would be nice to propagate that to
9276 // the outer statement evaluation rather than bailing out.
9277 if (ESR != ESR_Failed)
9278 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9279 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9280 return false;
9281 }
9282 }
9283
9284 llvm_unreachable("Return from function from the loop above.");
9285 }
9286
9287 bool VisitPackIndexingExpr(const PackIndexingExpr *E) {
9288 return StmtVisitorTy::Visit(E->getSelectedExpr());
9289 }
9290
9291 /// Visit a value which is evaluated, but whose value is ignored.
9292 void VisitIgnoredValue(const Expr *E) {
9293 EvaluateIgnoredValue(Info, E);
9294 }
9295
9296 /// Potentially visit a MemberExpr's base expression.
9297 void VisitIgnoredBaseExpression(const Expr *E) {
9298 // While MSVC doesn't evaluate the base expression, it does diagnose the
9299 // presence of side-effecting behavior.
9300 if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Ctx: Info.Ctx))
9301 return;
9302 VisitIgnoredValue(E);
9303 }
9304};
9305
9306} // namespace
9307
9308//===----------------------------------------------------------------------===//
9309// Common base class for lvalue and temporary evaluation.
9310//===----------------------------------------------------------------------===//
9311namespace {
9312template<class Derived>
9313class LValueExprEvaluatorBase
9314 : public ExprEvaluatorBase<Derived> {
9315protected:
9316 LValue &Result;
9317 bool InvalidBaseOK;
9318 typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
9319 typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy;
9320
9321 bool Success(APValue::LValueBase B) {
9322 Result.set(B);
9323 return true;
9324 }
9325
9326 bool evaluatePointer(const Expr *E, LValue &Result) {
9327 return EvaluatePointer(E, Result, this->Info, InvalidBaseOK);
9328 }
9329
9330public:
9331 LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK)
9332 : ExprEvaluatorBaseTy(Info), Result(Result),
9333 InvalidBaseOK(InvalidBaseOK) {}
9334
9335 bool Success(const APValue &V, const Expr *E) {
9336 Result.setFrom(Ctx: this->Info.Ctx, V);
9337 return true;
9338 }
9339
9340 bool VisitMemberExpr(const MemberExpr *E) {
9341 // Handle non-static data members.
9342 QualType BaseTy;
9343 bool EvalOK;
9344 if (E->isArrow()) {
9345 EvalOK = evaluatePointer(E: E->getBase(), Result);
9346 BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType();
9347 } else if (E->getBase()->isPRValue()) {
9348 assert(E->getBase()->getType()->isRecordType());
9349 EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info);
9350 BaseTy = E->getBase()->getType();
9351 } else {
9352 EvalOK = this->Visit(E->getBase());
9353 BaseTy = E->getBase()->getType();
9354 }
9355 if (!EvalOK) {
9356 if (!InvalidBaseOK)
9357 return false;
9358 Result.setInvalid(B: E);
9359 return true;
9360 }
9361
9362 const ValueDecl *MD = E->getMemberDecl();
9363 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl())) {
9364 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9365 FD->getParent()->getCanonicalDecl() &&
9366 "record / field mismatch");
9367 (void)BaseTy;
9368 if (!HandleLValueMember(this->Info, E, Result, FD))
9369 return false;
9370 } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Val: MD)) {
9371 if (!HandleLValueIndirectMember(this->Info, E, Result, IFD))
9372 return false;
9373 } else
9374 return this->Error(E);
9375
9376 if (MD->getType()->isReferenceType()) {
9377 APValue RefValue;
9378 if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
9379 RefValue))
9380 return false;
9381 return Success(RefValue, E);
9382 }
9383 return true;
9384 }
9385
9386 bool VisitBinaryOperator(const BinaryOperator *E) {
9387 switch (E->getOpcode()) {
9388 default:
9389 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
9390
9391 case BO_PtrMemD:
9392 case BO_PtrMemI:
9393 return HandleMemberPointerAccess(this->Info, E, Result);
9394 }
9395 }
9396
9397 bool VisitCastExpr(const CastExpr *E) {
9398 switch (E->getCastKind()) {
9399 default:
9400 return ExprEvaluatorBaseTy::VisitCastExpr(E);
9401
9402 case CK_DerivedToBase:
9403 case CK_UncheckedDerivedToBase:
9404 if (!this->Visit(E->getSubExpr()))
9405 return false;
9406
9407 // Now figure out the necessary offset to add to the base LV to get from
9408 // the derived class to the base class.
9409 return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(),
9410 Result);
9411 }
9412 }
9413};
9414}
9415
9416//===----------------------------------------------------------------------===//
9417// LValue Evaluation
9418//
9419// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
9420// function designators (in C), decl references to void objects (in C), and
9421// temporaries (if building with -Wno-address-of-temporary).
9422//
9423// LValue evaluation produces values comprising a base expression of one of the
9424// following types:
9425// - Declarations
9426// * VarDecl
9427// * FunctionDecl
9428// - Literals
9429// * CompoundLiteralExpr in C (and in global scope in C++)
9430// * StringLiteral
9431// * PredefinedExpr
9432// * ObjCStringLiteralExpr
9433// * ObjCEncodeExpr
9434// * AddrLabelExpr
9435// * BlockExpr
9436// * CallExpr for a MakeStringConstant builtin
9437// - typeid(T) expressions, as TypeInfoLValues
9438// - Locals and temporaries
9439// * MaterializeTemporaryExpr
9440// * Any Expr, with a CallIndex indicating the function in which the temporary
9441// was evaluated, for cases where the MaterializeTemporaryExpr is missing
9442// from the AST (FIXME).
9443// * A MaterializeTemporaryExpr that has static storage duration, with no
9444// CallIndex, for a lifetime-extended temporary.
9445// * The ConstantExpr that is currently being evaluated during evaluation of an
9446// immediate invocation.
9447// plus an offset in bytes.
9448//===----------------------------------------------------------------------===//
9449namespace {
9450class LValueExprEvaluator
9451 : public LValueExprEvaluatorBase<LValueExprEvaluator> {
9452public:
9453 LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) :
9454 LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {}
9455
9456 bool VisitVarDecl(const Expr *E, const VarDecl *VD);
9457 bool VisitUnaryPreIncDec(const UnaryOperator *UO);
9458
9459 bool VisitCallExpr(const CallExpr *E);
9460 bool VisitDeclRefExpr(const DeclRefExpr *E);
9461 bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(B: E); }
9462 bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
9463 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
9464 bool VisitMemberExpr(const MemberExpr *E);
9465 bool VisitStringLiteral(const StringLiteral *E) {
9466 return Success(
9467 B: APValue::LValueBase(E, 0, Info.Ctx.getNextStringLiteralVersion()));
9468 }
9469 bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(B: E); }
9470 bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
9471 bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
9472 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
9473 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E);
9474 bool VisitUnaryDeref(const UnaryOperator *E);
9475 bool VisitUnaryReal(const UnaryOperator *E);
9476 bool VisitUnaryImag(const UnaryOperator *E);
9477 bool VisitUnaryPreInc(const UnaryOperator *UO) {
9478 return VisitUnaryPreIncDec(UO);
9479 }
9480 bool VisitUnaryPreDec(const UnaryOperator *UO) {
9481 return VisitUnaryPreIncDec(UO);
9482 }
9483 bool VisitBinAssign(const BinaryOperator *BO);
9484 bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO);
9485
9486 bool VisitCastExpr(const CastExpr *E) {
9487 switch (E->getCastKind()) {
9488 default:
9489 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
9490
9491 case CK_LValueBitCast:
9492 this->CCEDiag(E, D: diag::note_constexpr_invalid_cast)
9493 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
9494 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
9495 if (!Visit(S: E->getSubExpr()))
9496 return false;
9497 Result.Designator.setInvalid();
9498 return true;
9499
9500 case CK_BaseToDerived:
9501 if (!Visit(S: E->getSubExpr()))
9502 return false;
9503 return HandleBaseToDerivedCast(Info, E, Result);
9504
9505 case CK_Dynamic:
9506 if (!Visit(S: E->getSubExpr()))
9507 return false;
9508 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
9509 }
9510 }
9511};
9512} // end anonymous namespace
9513
9514/// Get an lvalue to a field of a lambda's closure type.
9515static bool HandleLambdaCapture(EvalInfo &Info, const Expr *E, LValue &Result,
9516 const CXXMethodDecl *MD, const FieldDecl *FD,
9517 bool LValueToRValueConversion) {
9518 // Static lambda function call operators can't have captures. We already
9519 // diagnosed this, so bail out here.
9520 if (MD->isStatic()) {
9521 assert(Info.CurrentCall->This == nullptr &&
9522 "This should not be set for a static call operator");
9523 return false;
9524 }
9525
9526 // Start with 'Result' referring to the complete closure object...
9527 if (MD->isExplicitObjectMemberFunction()) {
9528 // Self may be passed by reference or by value.
9529 const ParmVarDecl *Self = MD->getParamDecl(i: 0);
9530 if (Self->getType()->isReferenceType()) {
9531 APValue *RefValue = Info.getParamSlot(Call: Info.CurrentCall->Arguments, PVD: Self);
9532 if (!RefValue->allowConstexprUnknown() || RefValue->hasValue())
9533 Result.setFrom(Ctx: Info.Ctx, V: *RefValue);
9534 } else {
9535 const ParmVarDecl *VD = Info.CurrentCall->Arguments.getOrigParam(PVD: Self);
9536 CallStackFrame *Frame =
9537 Info.getCallFrameAndDepth(CallIndex: Info.CurrentCall->Arguments.CallIndex)
9538 .first;
9539 unsigned Version = Info.CurrentCall->Arguments.Version;
9540 Result.set(B: {VD, Frame->Index, Version});
9541 }
9542 } else
9543 Result = *Info.CurrentCall->This;
9544
9545 // ... then update it to refer to the field of the closure object
9546 // that represents the capture.
9547 if (!HandleLValueMember(Info, E, LVal&: Result, FD))
9548 return false;
9549
9550 // And if the field is of reference type (or if we captured '*this' by
9551 // reference), update 'Result' to refer to what
9552 // the field refers to.
9553 if (LValueToRValueConversion) {
9554 APValue RVal;
9555 if (!handleLValueToRValueConversion(Info, Conv: E, Type: FD->getType(), LVal: Result, RVal))
9556 return false;
9557 Result.setFrom(Ctx: Info.Ctx, V: RVal);
9558 }
9559 return true;
9560}
9561
9562/// Evaluate an expression as an lvalue. This can be legitimately called on
9563/// expressions which are not glvalues, in three cases:
9564/// * function designators in C, and
9565/// * "extern void" objects
9566/// * @selector() expressions in Objective-C
9567static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
9568 bool InvalidBaseOK) {
9569 assert(!E->isValueDependent());
9570 assert(E->isGLValue() || E->getType()->isFunctionType() ||
9571 E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E->IgnoreParens()));
9572 return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
9573}
9574
9575bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
9576 const ValueDecl *D = E->getDecl();
9577
9578 // If we are within a lambda's call operator, check whether the 'VD' referred
9579 // to within 'E' actually represents a lambda-capture that maps to a
9580 // data-member/field within the closure object, and if so, evaluate to the
9581 // field or what the field refers to.
9582 if (Info.CurrentCall && isLambdaCallOperator(DC: Info.CurrentCall->Callee) &&
9583 E->refersToEnclosingVariableOrCapture()) {
9584 // We don't always have a complete capture-map when checking or inferring if
9585 // the function call operator meets the requirements of a constexpr function
9586 // - but we don't need to evaluate the captures to determine constexprness
9587 // (dcl.constexpr C++17).
9588 if (Info.checkingPotentialConstantExpression())
9589 return false;
9590
9591 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(Val: D)) {
9592 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
9593 return HandleLambdaCapture(Info, E, Result, MD, FD,
9594 LValueToRValueConversion: FD->getType()->isReferenceType());
9595 }
9596 }
9597
9598 if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl,
9599 UnnamedGlobalConstantDecl>(Val: D))
9600 return Success(B: cast<ValueDecl>(Val: D));
9601 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
9602 return VisitVarDecl(E, VD);
9603 if (const BindingDecl *BD = dyn_cast<BindingDecl>(Val: D))
9604 return Visit(S: BD->getBinding());
9605 return Error(E);
9606}
9607
9608bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
9609 CallStackFrame *Frame = nullptr;
9610 unsigned Version = 0;
9611 if (VD->hasLocalStorage()) {
9612 // Only if a local variable was declared in the function currently being
9613 // evaluated, do we expect to be able to find its value in the current
9614 // frame. (Otherwise it was likely declared in an enclosing context and
9615 // could either have a valid evaluatable value (for e.g. a constexpr
9616 // variable) or be ill-formed (and trigger an appropriate evaluation
9617 // diagnostic)).
9618 CallStackFrame *CurrFrame = Info.CurrentCall;
9619 if (CurrFrame->Callee && CurrFrame->Callee->Equals(DC: VD->getDeclContext())) {
9620 // Function parameters are stored in some caller's frame. (Usually the
9621 // immediate caller, but for an inherited constructor they may be more
9622 // distant.)
9623 if (auto *PVD = dyn_cast<ParmVarDecl>(Val: VD)) {
9624 if (CurrFrame->Arguments) {
9625 VD = CurrFrame->Arguments.getOrigParam(PVD);
9626 Frame =
9627 Info.getCallFrameAndDepth(CallIndex: CurrFrame->Arguments.CallIndex).first;
9628 Version = CurrFrame->Arguments.Version;
9629 }
9630 } else {
9631 Frame = CurrFrame;
9632 Version = CurrFrame->getCurrentTemporaryVersion(Key: VD);
9633 }
9634 }
9635 }
9636
9637 if (!VD->getType()->isReferenceType()) {
9638 if (Frame) {
9639 Result.set(B: {VD, Frame->Index, Version});
9640 return true;
9641 }
9642 return Success(B: VD);
9643 }
9644
9645 if (!Info.getLangOpts().CPlusPlus11) {
9646 Info.CCEDiag(E, DiagId: diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
9647 << VD << VD->getType();
9648 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
9649 }
9650
9651 APValue *V;
9652 if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, Result&: V))
9653 return false;
9654
9655 if (!V) {
9656 Result.set(B: VD);
9657 Result.AllowConstexprUnknown = true;
9658 return true;
9659 }
9660
9661 return Success(V: *V, E);
9662}
9663
9664bool LValueExprEvaluator::VisitCallExpr(const CallExpr *E) {
9665 if (!IsConstantEvaluatedBuiltinCall(E))
9666 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9667
9668 switch (E->getBuiltinCallee()) {
9669 default:
9670 return false;
9671 case Builtin::BIas_const:
9672 case Builtin::BIforward:
9673 case Builtin::BIforward_like:
9674 case Builtin::BImove:
9675 case Builtin::BImove_if_noexcept:
9676 if (cast<FunctionDecl>(Val: E->getCalleeDecl())->isConstexpr())
9677 return Visit(S: E->getArg(Arg: 0));
9678 break;
9679 }
9680
9681 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9682}
9683
9684bool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
9685 const MaterializeTemporaryExpr *E) {
9686 // Walk through the expression to find the materialized temporary itself.
9687 SmallVector<const Expr *, 2> CommaLHSs;
9688 SmallVector<SubobjectAdjustment, 2> Adjustments;
9689 const Expr *Inner =
9690 E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments);
9691
9692 // If we passed any comma operators, evaluate their LHSs.
9693 for (const Expr *E : CommaLHSs)
9694 if (!EvaluateIgnoredValue(Info, E))
9695 return false;
9696
9697 // A materialized temporary with static storage duration can appear within the
9698 // result of a constant expression evaluation, so we need to preserve its
9699 // value for use outside this evaluation.
9700 APValue *Value;
9701 if (E->getStorageDuration() == SD_Static) {
9702 if (Info.EvalMode == EvaluationMode::ConstantFold)
9703 return false;
9704 // FIXME: What about SD_Thread?
9705 Value = E->getOrCreateValue(MayCreate: true);
9706 *Value = APValue();
9707 Result.set(B: E);
9708 } else {
9709 Value = &Info.CurrentCall->createTemporary(
9710 Key: E, T: Inner->getType(),
9711 Scope: E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression
9712 : ScopeKind::Block,
9713 LV&: Result);
9714 }
9715
9716 QualType Type = Inner->getType();
9717
9718 // Materialize the temporary itself.
9719 if (!EvaluateInPlace(Result&: *Value, Info, This: Result, E: Inner)) {
9720 *Value = APValue();
9721 return false;
9722 }
9723
9724 // Adjust our lvalue to refer to the desired subobject.
9725 for (unsigned I = Adjustments.size(); I != 0; /**/) {
9726 --I;
9727 switch (Adjustments[I].Kind) {
9728 case SubobjectAdjustment::DerivedToBaseAdjustment:
9729 if (!HandleLValueBasePath(Info, E: Adjustments[I].DerivedToBase.BasePath,
9730 Type, Result))
9731 return false;
9732 Type = Adjustments[I].DerivedToBase.BasePath->getType();
9733 break;
9734
9735 case SubobjectAdjustment::FieldAdjustment:
9736 if (!HandleLValueMember(Info, E, LVal&: Result, FD: Adjustments[I].Field))
9737 return false;
9738 Type = Adjustments[I].Field->getType();
9739 break;
9740
9741 case SubobjectAdjustment::MemberPointerAdjustment:
9742 if (!HandleMemberPointerAccess(Info&: this->Info, LVType: Type, LV&: Result,
9743 RHS: Adjustments[I].Ptr.RHS))
9744 return false;
9745 Type = Adjustments[I].Ptr.MPT->getPointeeType();
9746 break;
9747 }
9748 }
9749
9750 return true;
9751}
9752
9753bool
9754LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9755 assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) &&
9756 "lvalue compound literal in c++?");
9757 APValue *Lit;
9758 // If CompountLiteral has static storage, its value can be used outside
9759 // this expression. So evaluate it once and store it in ASTContext.
9760 if (E->hasStaticStorage()) {
9761 Lit = &E->getOrCreateStaticValue(Ctx&: Info.Ctx);
9762 Result.set(B: E);
9763 // Reset any previously evaluated state, otherwise evaluation below might
9764 // fail.
9765 // FIXME: Should we just re-use the previously evaluated value instead?
9766 *Lit = APValue();
9767 } else {
9768 assert(!Info.getLangOpts().CPlusPlus);
9769 Lit = &Info.CurrentCall->createTemporary(Key: E, T: E->getInitializer()->getType(),
9770 Scope: ScopeKind::Block, LV&: Result);
9771 }
9772 // FIXME: Evaluating in place isn't always right. We should figure out how to
9773 // use appropriate evaluation context here, see
9774 // clang/test/AST/static-compound-literals-reeval.cpp for a failure.
9775 if (!EvaluateInPlace(Result&: *Lit, Info, This: Result, E: E->getInitializer())) {
9776 *Lit = APValue();
9777 return false;
9778 }
9779 return true;
9780}
9781
9782bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
9783 TypeInfoLValue TypeInfo;
9784
9785 if (!E->isPotentiallyEvaluated()) {
9786 if (E->isTypeOperand())
9787 TypeInfo = TypeInfoLValue(E->getTypeOperand(Context: Info.Ctx).getTypePtr());
9788 else
9789 TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr());
9790 } else {
9791 if (!Info.Ctx.getLangOpts().CPlusPlus20) {
9792 Info.CCEDiag(E, DiagId: diag::note_constexpr_typeid_polymorphic)
9793 << E->getExprOperand()->getType()
9794 << E->getExprOperand()->getSourceRange();
9795 }
9796
9797 if (!Visit(S: E->getExprOperand()))
9798 return false;
9799
9800 std::optional<DynamicType> DynType =
9801 ComputeDynamicType(Info, E, This&: Result, AK: AK_TypeId);
9802 if (!DynType)
9803 return false;
9804
9805 TypeInfo = TypeInfoLValue(
9806 Info.Ctx.getCanonicalTagType(TD: DynType->Type).getTypePtr());
9807 }
9808
9809 return Success(B: APValue::LValueBase::getTypeInfo(LV: TypeInfo, TypeInfo: E->getType()));
9810}
9811
9812bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
9813 return Success(B: E->getGuidDecl());
9814}
9815
9816bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
9817 // Handle static data members.
9818 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: E->getMemberDecl())) {
9819 VisitIgnoredBaseExpression(E: E->getBase());
9820 return VisitVarDecl(E, VD);
9821 }
9822
9823 // Handle static member functions.
9824 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: E->getMemberDecl())) {
9825 if (MD->isStatic()) {
9826 VisitIgnoredBaseExpression(E: E->getBase());
9827 return Success(B: MD);
9828 }
9829 }
9830
9831 // Handle non-static data members.
9832 return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
9833}
9834
9835bool LValueExprEvaluator::VisitExtVectorElementExpr(
9836 const ExtVectorElementExpr *E) {
9837 bool Success = true;
9838
9839 APValue Val;
9840 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
9841 if (!Info.noteFailure())
9842 return false;
9843 Success = false;
9844 }
9845
9846 SmallVector<uint32_t, 4> Indices;
9847 E->getEncodedElementAccess(Elts&: Indices);
9848 // FIXME: support accessing more than one element
9849 if (Indices.size() > 1)
9850 return false;
9851
9852 if (Success) {
9853 Result.setFrom(Ctx: Info.Ctx, V: Val);
9854 QualType BaseType = E->getBase()->getType();
9855 if (E->isArrow())
9856 BaseType = BaseType->getPointeeType();
9857 const auto *VT = BaseType->castAs<VectorType>();
9858 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
9859 Size: VT->getNumElements(), Idx: Indices[0]);
9860 }
9861
9862 return Success;
9863}
9864
9865bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
9866 if (E->getBase()->getType()->isSveVLSBuiltinType())
9867 return Error(E);
9868
9869 APSInt Index;
9870 bool Success = true;
9871
9872 if (const auto *VT = E->getBase()->getType()->getAs<VectorType>()) {
9873 APValue Val;
9874 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
9875 if (!Info.noteFailure())
9876 return false;
9877 Success = false;
9878 }
9879
9880 if (!EvaluateInteger(E: E->getIdx(), Result&: Index, Info)) {
9881 if (!Info.noteFailure())
9882 return false;
9883 Success = false;
9884 }
9885
9886 if (Success) {
9887 Result.setFrom(Ctx: Info.Ctx, V: Val);
9888 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
9889 Size: VT->getNumElements(), Idx: Index.getZExtValue());
9890 }
9891
9892 return Success;
9893 }
9894
9895 // C++17's rules require us to evaluate the LHS first, regardless of which
9896 // side is the base.
9897 for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) {
9898 if (SubExpr == E->getBase() ? !evaluatePointer(E: SubExpr, Result)
9899 : !EvaluateInteger(E: SubExpr, Result&: Index, Info)) {
9900 if (!Info.noteFailure())
9901 return false;
9902 Success = false;
9903 }
9904 }
9905
9906 return Success &&
9907 HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: E->getType(), Adjustment: Index);
9908}
9909
9910bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
9911 bool Success = evaluatePointer(E: E->getSubExpr(), Result);
9912 // [C++26][expr.unary.op]
9913 // If the operand points to an object or function, the result
9914 // denotes that object or function; otherwise, the behavior is undefined.
9915 // Because &(*(type*)0) is a common pattern, we do not fail the evaluation
9916 // immediately.
9917 if (!Success || !E->getType().getNonReferenceType()->isObjectType())
9918 return Success;
9919 return bool(findCompleteObject(Info, E, AK: AK_Dereference, LVal: Result,
9920 LValType: E->getType())) ||
9921 Info.noteUndefinedBehavior();
9922}
9923
9924bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
9925 if (!Visit(S: E->getSubExpr()))
9926 return false;
9927 // __real is a no-op on scalar lvalues.
9928 if (E->getSubExpr()->getType()->isAnyComplexType())
9929 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: false);
9930 return true;
9931}
9932
9933bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
9934 assert(E->getSubExpr()->getType()->isAnyComplexType() &&
9935 "lvalue __imag__ on scalar?");
9936 if (!Visit(S: E->getSubExpr()))
9937 return false;
9938 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: true);
9939 return true;
9940}
9941
9942bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) {
9943 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9944 return Error(E: UO);
9945
9946 if (!this->Visit(S: UO->getSubExpr()))
9947 return false;
9948
9949 return handleIncDec(
9950 Info&: this->Info, E: UO, LVal: Result, LValType: UO->getSubExpr()->getType(),
9951 IsIncrement: UO->isIncrementOp(), Old: nullptr);
9952}
9953
9954bool LValueExprEvaluator::VisitCompoundAssignOperator(
9955 const CompoundAssignOperator *CAO) {
9956 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9957 return Error(E: CAO);
9958
9959 bool Success = true;
9960
9961 // C++17 onwards require that we evaluate the RHS first.
9962 APValue RHS;
9963 if (!Evaluate(Result&: RHS, Info&: this->Info, E: CAO->getRHS())) {
9964 if (!Info.noteFailure())
9965 return false;
9966 Success = false;
9967 }
9968
9969 // The overall lvalue result is the result of evaluating the LHS.
9970 if (!this->Visit(S: CAO->getLHS()) || !Success)
9971 return false;
9972
9973 return handleCompoundAssignment(
9974 Info&: this->Info, E: CAO,
9975 LVal: Result, LValType: CAO->getLHS()->getType(), PromotedLValType: CAO->getComputationLHSType(),
9976 Opcode: CAO->getOpForCompoundAssignment(Opc: CAO->getOpcode()), RVal: RHS);
9977}
9978
9979bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
9980 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9981 return Error(E);
9982
9983 bool Success = true;
9984
9985 // C++17 onwards require that we evaluate the RHS first.
9986 APValue NewVal;
9987 if (!Evaluate(Result&: NewVal, Info&: this->Info, E: E->getRHS())) {
9988 if (!Info.noteFailure())
9989 return false;
9990 Success = false;
9991 }
9992
9993 if (!this->Visit(S: E->getLHS()) || !Success)
9994 return false;
9995
9996 if (Info.getLangOpts().CPlusPlus20 &&
9997 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: E->getLHS(), LHS: Result))
9998 return false;
9999
10000 return handleAssignment(Info&: this->Info, E, LVal: Result, LValType: E->getLHS()->getType(),
10001 Val&: NewVal);
10002}
10003
10004//===----------------------------------------------------------------------===//
10005// Pointer Evaluation
10006//===----------------------------------------------------------------------===//
10007
10008/// Convenience function. LVal's base must be a call to an alloc_size
10009/// function.
10010static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
10011 const LValue &LVal,
10012 llvm::APInt &Result) {
10013 assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) &&
10014 "Can't get the size of a non alloc_size function");
10015 const auto *Base = LVal.getLValueBase().get<const Expr *>();
10016 const CallExpr *CE = tryUnwrapAllocSizeCall(E: Base);
10017 std::optional<llvm::APInt> Size =
10018 CE->evaluateBytesReturnedByAllocSizeCall(Ctx);
10019 if (!Size)
10020 return false;
10021
10022 Result = std::move(*Size);
10023 return true;
10024}
10025
10026/// Attempts to evaluate the given LValueBase as the result of a call to
10027/// a function with the alloc_size attribute. If it was possible to do so, this
10028/// function will return true, make Result's Base point to said function call,
10029/// and mark Result's Base as invalid.
10030static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base,
10031 LValue &Result) {
10032 if (Base.isNull())
10033 return false;
10034
10035 // Because we do no form of static analysis, we only support const variables.
10036 //
10037 // Additionally, we can't support parameters, nor can we support static
10038 // variables (in the latter case, use-before-assign isn't UB; in the former,
10039 // we have no clue what they'll be assigned to).
10040 const auto *VD =
10041 dyn_cast_or_null<VarDecl>(Val: Base.dyn_cast<const ValueDecl *>());
10042 if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified())
10043 return false;
10044
10045 const Expr *Init = VD->getAnyInitializer();
10046 if (!Init || Init->getType().isNull())
10047 return false;
10048
10049 const Expr *E = Init->IgnoreParens();
10050 if (!tryUnwrapAllocSizeCall(E))
10051 return false;
10052
10053 // Store E instead of E unwrapped so that the type of the LValue's base is
10054 // what the user wanted.
10055 Result.setInvalid(B: E);
10056
10057 QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType();
10058 Result.addUnsizedArray(Info, E, ElemTy: Pointee);
10059 return true;
10060}
10061
10062namespace {
10063class PointerExprEvaluator
10064 : public ExprEvaluatorBase<PointerExprEvaluator> {
10065 LValue &Result;
10066 bool InvalidBaseOK;
10067
10068 bool Success(const Expr *E) {
10069 Result.set(B: E);
10070 return true;
10071 }
10072
10073 bool evaluateLValue(const Expr *E, LValue &Result) {
10074 return EvaluateLValue(E, Result, Info, InvalidBaseOK);
10075 }
10076
10077 bool evaluatePointer(const Expr *E, LValue &Result) {
10078 return EvaluatePointer(E, Result, Info, InvalidBaseOK);
10079 }
10080
10081 bool visitNonBuiltinCallExpr(const CallExpr *E);
10082public:
10083
10084 PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK)
10085 : ExprEvaluatorBaseTy(info), Result(Result),
10086 InvalidBaseOK(InvalidBaseOK) {}
10087
10088 bool Success(const APValue &V, const Expr *E) {
10089 Result.setFrom(Ctx: Info.Ctx, V);
10090 return true;
10091 }
10092 bool ZeroInitialization(const Expr *E) {
10093 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10094 return true;
10095 }
10096
10097 bool VisitBinaryOperator(const BinaryOperator *E);
10098 bool VisitCastExpr(const CastExpr* E);
10099 bool VisitUnaryAddrOf(const UnaryOperator *E);
10100 bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
10101 { return Success(E); }
10102 bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
10103 if (E->isExpressibleAsConstantInitializer())
10104 return Success(E);
10105 if (Info.noteFailure())
10106 EvaluateIgnoredValue(Info, E: E->getSubExpr());
10107 return Error(E);
10108 }
10109 bool VisitObjCArrayLiteral(const ObjCArrayLiteral *E) {
10110 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
10111 }
10112 bool VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) {
10113 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
10114 }
10115 bool VisitAddrLabelExpr(const AddrLabelExpr *E)
10116 { return Success(E); }
10117 bool VisitCallExpr(const CallExpr *E);
10118 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
10119 bool VisitBlockExpr(const BlockExpr *E) {
10120 if (!E->getBlockDecl()->hasCaptures())
10121 return Success(E);
10122 return Error(E);
10123 }
10124 bool VisitCXXThisExpr(const CXXThisExpr *E) {
10125 auto DiagnoseInvalidUseOfThis = [&] {
10126 if (Info.getLangOpts().CPlusPlus11)
10127 Info.FFDiag(E, DiagId: diag::note_constexpr_this) << E->isImplicit();
10128 else
10129 Info.FFDiag(E);
10130 };
10131
10132 // Can't look at 'this' when checking a potential constant expression.
10133 if (Info.checkingPotentialConstantExpression())
10134 return false;
10135
10136 bool IsExplicitLambda =
10137 isLambdaCallWithExplicitObjectParameter(DC: Info.CurrentCall->Callee);
10138 if (!IsExplicitLambda) {
10139 if (!Info.CurrentCall->This) {
10140 DiagnoseInvalidUseOfThis();
10141 return false;
10142 }
10143
10144 Result = *Info.CurrentCall->This;
10145 }
10146
10147 if (isLambdaCallOperator(DC: Info.CurrentCall->Callee)) {
10148 // Ensure we actually have captured 'this'. If something was wrong with
10149 // 'this' capture, the error would have been previously reported.
10150 // Otherwise we can be inside of a default initialization of an object
10151 // declared by lambda's body, so no need to return false.
10152 if (!Info.CurrentCall->LambdaThisCaptureField) {
10153 if (IsExplicitLambda && !Info.CurrentCall->This) {
10154 DiagnoseInvalidUseOfThis();
10155 return false;
10156 }
10157
10158 return true;
10159 }
10160
10161 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
10162 return HandleLambdaCapture(
10163 Info, E, Result, MD, FD: Info.CurrentCall->LambdaThisCaptureField,
10164 LValueToRValueConversion: Info.CurrentCall->LambdaThisCaptureField->getType()->isPointerType());
10165 }
10166 return true;
10167 }
10168
10169 bool VisitCXXNewExpr(const CXXNewExpr *E);
10170
10171 bool VisitSourceLocExpr(const SourceLocExpr *E) {
10172 assert(!E->isIntType() && "SourceLocExpr isn't a pointer type?");
10173 APValue LValResult = E->EvaluateInContext(
10174 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
10175 Result.setFrom(Ctx: Info.Ctx, V: LValResult);
10176 return true;
10177 }
10178
10179 bool VisitEmbedExpr(const EmbedExpr *E) {
10180 llvm::report_fatal_error(reason: "Not yet implemented for ExprConstant.cpp");
10181 return true;
10182 }
10183
10184 bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) {
10185 std::string ResultStr = E->ComputeName(Context&: Info.Ctx);
10186
10187 QualType CharTy = Info.Ctx.CharTy.withConst();
10188 APInt Size(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType()),
10189 ResultStr.size() + 1);
10190 QualType ArrayTy = Info.Ctx.getConstantArrayType(
10191 EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
10192
10193 StringLiteral *SL =
10194 StringLiteral::Create(Ctx: Info.Ctx, Str: ResultStr, Kind: StringLiteralKind::Ordinary,
10195 /*Pascal*/ false, Ty: ArrayTy, Locs: E->getLocation());
10196
10197 evaluateLValue(E: SL, Result);
10198 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: ArrayTy));
10199 return true;
10200 }
10201
10202 // FIXME: Missing: @protocol, @selector
10203};
10204} // end anonymous namespace
10205
10206static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info,
10207 bool InvalidBaseOK) {
10208 assert(!E->isValueDependent());
10209 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
10210 return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
10211}
10212
10213bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
10214 if (E->getOpcode() != BO_Add &&
10215 E->getOpcode() != BO_Sub)
10216 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
10217
10218 const Expr *PExp = E->getLHS();
10219 const Expr *IExp = E->getRHS();
10220 if (IExp->getType()->isPointerType())
10221 std::swap(a&: PExp, b&: IExp);
10222
10223 bool EvalPtrOK = evaluatePointer(E: PExp, Result);
10224 if (!EvalPtrOK && !Info.noteFailure())
10225 return false;
10226
10227 llvm::APSInt Offset;
10228 if (!EvaluateInteger(E: IExp, Result&: Offset, Info) || !EvalPtrOK)
10229 return false;
10230
10231 if (E->getOpcode() == BO_Sub)
10232 negateAsSigned(Int&: Offset);
10233
10234 QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType();
10235 return HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: Pointee, Adjustment: Offset);
10236}
10237
10238bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
10239 // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*'
10240 // operator, neither operator is evaluated and the result is as if both were
10241 // omitted (except that the operators' constraints, already enforced by Sema,
10242 // still apply, and the result is not an lvalue). So '&*p' is just the pointer
10243 // value 'p' with no dereference, and forming it is therefore not undefined
10244 // behavior even when 'p' is null, e.g. '&*(int *)0'. Evaluate the pointer
10245 // operand directly so we don't spuriously diagnose a null dereference.
10246 if (!Info.getLangOpts().CPlusPlus) {
10247 const Expr *Sub = E->getSubExpr()->IgnoreParens();
10248 if (const auto *Deref = dyn_cast<UnaryOperator>(Val: Sub);
10249 Deref && Deref->getOpcode() == UO_Deref)
10250 return evaluatePointer(E: Deref->getSubExpr(), Result);
10251 }
10252 return evaluateLValue(E: E->getSubExpr(), Result);
10253}
10254
10255// Is the provided decl 'std::source_location::current'?
10256static bool IsDeclSourceLocationCurrent(const FunctionDecl *FD) {
10257 if (!FD)
10258 return false;
10259 const IdentifierInfo *FnII = FD->getIdentifier();
10260 if (!FnII || !FnII->isStr(Str: "current"))
10261 return false;
10262
10263 const auto *RD = dyn_cast<RecordDecl>(Val: FD->getParent());
10264 if (!RD)
10265 return false;
10266
10267 const IdentifierInfo *ClassII = RD->getIdentifier();
10268 return RD->isInStdNamespace() && ClassII && ClassII->isStr(Str: "source_location");
10269}
10270
10271bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
10272 const Expr *SubExpr = E->getSubExpr();
10273
10274 switch (E->getCastKind()) {
10275 default:
10276 break;
10277 case CK_BitCast:
10278 case CK_CPointerToObjCPointerCast:
10279 case CK_BlockPointerToObjCPointerCast:
10280 case CK_AnyPointerToBlockPointerCast:
10281 case CK_AddressSpaceConversion:
10282 if (!Visit(S: SubExpr))
10283 return false;
10284 if (E->getType()->isFunctionPointerType() ||
10285 SubExpr->getType()->isFunctionPointerType()) {
10286 // Casting between two function pointer types, or between a function
10287 // pointer and an object pointer, is always a reinterpret_cast.
10288 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10289 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10290 << Info.Ctx.getLangOpts().CPlusPlus;
10291 Result.Designator.setInvalid();
10292 } else if (!E->getType()->isVoidPointerType()) {
10293 // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
10294 // permitted in constant expressions in C++11. Bitcasts from cv void* are
10295 // also static_casts, but we disallow them as a resolution to DR1312.
10296 //
10297 // In some circumstances, we permit casting from void* to cv1 T*, when the
10298 // actual pointee object is actually a cv2 T.
10299 bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&
10300 !Result.IsNullPtr;
10301 bool VoidPtrCastMaybeOK =
10302 Result.IsNullPtr ||
10303 (HasValidResult &&
10304 Info.Ctx.hasSimilarType(T1: Result.Designator.getType(Ctx&: Info.Ctx),
10305 T2: E->getType()->getPointeeType()));
10306 // 1. We'll allow it in std::allocator::allocate, and anything which that
10307 // calls.
10308 // 2. HACK 2022-03-28: Work around an issue with libstdc++'s
10309 // <source_location> header. Fixed in GCC 12 and later (2022-04-??).
10310 // We'll allow it in the body of std::source_location::current. GCC's
10311 // implementation had a parameter of type `void*`, and casts from
10312 // that back to `const __impl*` in its body.
10313 if (VoidPtrCastMaybeOK &&
10314 (Info.getStdAllocatorCaller(FnName: "allocate") ||
10315 IsDeclSourceLocationCurrent(FD: Info.CurrentCall->Callee) ||
10316 Info.getLangOpts().CPlusPlus26)) {
10317 // Permitted.
10318 } else {
10319 if (SubExpr->getType()->isVoidPointerType() &&
10320 Info.getLangOpts().CPlusPlus) {
10321 if (HasValidResult)
10322 CCEDiag(E, D: diag::note_constexpr_invalid_void_star_cast)
10323 << SubExpr->getType() << Info.getLangOpts().CPlusPlus26
10324 << Result.Designator.getType(Ctx&: Info.Ctx).getCanonicalType()
10325 << E->getType()->getPointeeType();
10326 else
10327 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10328 << diag::ConstexprInvalidCastKind::CastFrom
10329 << SubExpr->getType();
10330 } else
10331 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10332 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10333 << Info.Ctx.getLangOpts().CPlusPlus;
10334 Result.Designator.setInvalid();
10335 }
10336 }
10337 if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr)
10338 ZeroInitialization(E);
10339 return true;
10340
10341 case CK_DerivedToBase:
10342 case CK_UncheckedDerivedToBase:
10343 if (!evaluatePointer(E: E->getSubExpr(), Result))
10344 return false;
10345 if (!Result.Base && Result.Offset.isZero())
10346 return true;
10347
10348 // Now figure out the necessary offset to add to the base LV to get from
10349 // the derived class to the base class.
10350 return HandleLValueBasePath(Info, E, Type: E->getSubExpr()->getType()->
10351 castAs<PointerType>()->getPointeeType(),
10352 Result);
10353
10354 case CK_BaseToDerived:
10355 if (!Visit(S: E->getSubExpr()))
10356 return false;
10357 if (!Result.Base && Result.Offset.isZero())
10358 return true;
10359 return HandleBaseToDerivedCast(Info, E, Result);
10360
10361 case CK_Dynamic:
10362 if (!Visit(S: E->getSubExpr()))
10363 return false;
10364 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
10365
10366 case CK_NullToPointer:
10367 VisitIgnoredValue(E: E->getSubExpr());
10368 return ZeroInitialization(E);
10369
10370 case CK_IntegralToPointer: {
10371 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10372 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10373 << Info.Ctx.getLangOpts().CPlusPlus;
10374
10375 APValue Value;
10376 if (!EvaluateIntegerOrLValue(E: SubExpr, Result&: Value, Info))
10377 break;
10378
10379 if (Value.isInt()) {
10380 unsigned Size = Info.Ctx.getTypeSize(T: E->getType());
10381 uint64_t N = Value.getInt().extOrTrunc(width: Size).getZExtValue();
10382 if (N == Info.Ctx.getTargetNullPointerValue(QT: E->getType())) {
10383 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10384 } else {
10385 Result.Base = (Expr *)nullptr;
10386 Result.InvalidBase = false;
10387 Result.Offset = CharUnits::fromQuantity(Quantity: N);
10388 Result.Designator.setInvalid();
10389 Result.IsNullPtr = false;
10390 }
10391 return true;
10392 } else {
10393 // In rare instances, the value isn't an lvalue.
10394 // For example, when the value is the difference between the addresses of
10395 // two labels. We reject that as a constant expression because we can't
10396 // compute a valid offset to convert into a pointer.
10397 if (!Value.isLValue())
10398 return false;
10399
10400 // Cast is of an lvalue, no need to change value.
10401 Result.setFrom(Ctx: Info.Ctx, V: Value);
10402 return true;
10403 }
10404 }
10405
10406 case CK_ArrayToPointerDecay: {
10407 if (SubExpr->isGLValue()) {
10408 if (!evaluateLValue(E: SubExpr, Result))
10409 return false;
10410 } else {
10411 APValue &Value = Info.CurrentCall->createTemporary(
10412 Key: SubExpr, T: SubExpr->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
10413 if (!EvaluateInPlace(Result&: Value, Info, This: Result, E: SubExpr))
10414 return false;
10415 }
10416 // The result is a pointer to the first element of the array.
10417 auto *AT = Info.Ctx.getAsArrayType(T: SubExpr->getType());
10418 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT))
10419 Result.addArray(Info, E, CAT);
10420 else
10421 Result.addUnsizedArray(Info, E, ElemTy: AT->getElementType());
10422 return true;
10423 }
10424
10425 case CK_FunctionToPointerDecay:
10426 return evaluateLValue(E: SubExpr, Result);
10427
10428 case CK_LValueToRValue: {
10429 LValue LVal;
10430 if (!evaluateLValue(E: E->getSubExpr(), Result&: LVal))
10431 return false;
10432
10433 APValue RVal;
10434 // Note, we use the subexpression's type in order to retain cv-qualifiers.
10435 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
10436 LVal, RVal))
10437 return InvalidBaseOK &&
10438 evaluateLValueAsAllocSize(Info, Base: LVal.Base, Result);
10439 return Success(V: RVal, E);
10440 }
10441 }
10442
10443 return ExprEvaluatorBaseTy::VisitCastExpr(E);
10444}
10445
10446static CharUnits GetAlignOfType(const ASTContext &Ctx, QualType T,
10447 UnaryExprOrTypeTrait ExprKind) {
10448 // C++ [expr.alignof]p3:
10449 // When alignof is applied to a reference type, the result is the
10450 // alignment of the referenced type.
10451 T = T.getNonReferenceType();
10452
10453 if (T.getQualifiers().hasUnaligned())
10454 return CharUnits::One();
10455
10456 const bool AlignOfReturnsPreferred =
10457 Ctx.getLangOpts().isCompatibleWith(Version: LangOptions::ClangABI::Ver7);
10458
10459 // __alignof is defined to return the preferred alignment.
10460 // Before 8, clang returned the preferred alignment for alignof and _Alignof
10461 // as well.
10462 if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
10463 return Ctx.toCharUnitsFromBits(BitSize: Ctx.getPreferredTypeAlign(T: T.getTypePtr()));
10464 // alignof and _Alignof are defined to return the ABI alignment.
10465 else if (ExprKind == UETT_AlignOf)
10466 return Ctx.getTypeAlignInChars(T: T.getTypePtr());
10467 else
10468 llvm_unreachable("GetAlignOfType on a non-alignment ExprKind");
10469}
10470
10471// Convert a builtin ID to the canonical x86 builtin ID the constant evaluators
10472// dispatch on in their x86 target-specific cases, or 0 if \p BuiltinOp is a
10473// target builtin those cases should not handle.
10474//
10475// Target-independent builtins are returned unchanged. Target builtin IDs of
10476// different targets overlap (each target numbers its builtins from
10477// Builtin::FirstTSBuiltin), so a target builtin ID is only meaningful for the
10478// target that owns it. Determine the owning target (translating an auxiliary ID
10479// back to its canonical value) and only return the ID when x86 owns it;
10480// otherwise an overlapping ID could be misinterpreted as an unrelated x86
10481// builtin.
10482unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
10483 unsigned BuiltinOp) {
10484 // Target-independent builtins have the same ID regardless of the target, so
10485 // they can be dispatched as-is. This is the common case and is intentionally
10486 // kept to a single comparison so callers can use this on hot paths (e.g. the
10487 // bytecode interpreter's builtin dispatch) without re-deriving the ID from
10488 // the call expression.
10489 if (BuiltinOp < Builtin::FirstTSBuiltin)
10490 return BuiltinOp;
10491
10492 // Determine the target that owns this builtin, translating an auxiliary ID
10493 // back to its canonical value.
10494 const TargetInfo *OwningTarget;
10495 if (Ctx.BuiltinInfo.isAuxBuiltinID(ID: BuiltinOp)) {
10496 OwningTarget = Ctx.getAuxTargetInfo();
10497 BuiltinOp = Ctx.BuiltinInfo.getAuxBuiltinID(ID: BuiltinOp);
10498 } else {
10499 OwningTarget = &Ctx.getTargetInfo();
10500 }
10501
10502 if (!OwningTarget)
10503 return 0;
10504
10505 // x86 and x86_64 share a single builtin set and are the only architectures
10506 // whose target-specific builtins the constant evaluators currently fold.
10507 switch (OwningTarget->getTriple().getArch()) {
10508 case llvm::Triple::x86:
10509 case llvm::Triple::x86_64:
10510 return BuiltinOp;
10511 default:
10512 return 0;
10513 }
10514}
10515
10516unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
10517 const CallExpr *E) {
10518 return ConvertBuiltinIDToX86BuiltinID(Ctx, BuiltinOp: E->getBuiltinCallee());
10519}
10520
10521CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E,
10522 UnaryExprOrTypeTrait ExprKind) {
10523 E = E->IgnoreParens();
10524
10525 // The kinds of expressions that we have special-case logic here for
10526 // should be kept up to date with the special checks for those
10527 // expressions in Sema.
10528
10529 // alignof decl is always accepted, even if it doesn't make sense: we default
10530 // to 1 in those cases.
10531 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E))
10532 return Ctx.getDeclAlign(D: DRE->getDecl(),
10533 /*RefAsPointee*/ ForAlignof: true);
10534
10535 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: E))
10536 return Ctx.getDeclAlign(D: ME->getMemberDecl(),
10537 /*RefAsPointee*/ ForAlignof: true);
10538
10539 return GetAlignOfType(Ctx, T: E->getType(), ExprKind);
10540}
10541
10542static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) {
10543 if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>())
10544 return Info.Ctx.getDeclAlign(D: VD);
10545 if (const auto *E = Value.Base.dyn_cast<const Expr *>())
10546 return GetAlignOfExpr(Ctx: Info.Ctx, E, ExprKind: UETT_AlignOf);
10547 return GetAlignOfType(Ctx: Info.Ctx, T: Value.Base.getTypeInfoType(), ExprKind: UETT_AlignOf);
10548}
10549
10550/// Evaluate the value of the alignment argument to __builtin_align_{up,down},
10551/// __builtin_is_aligned and __builtin_assume_aligned.
10552static bool getAlignmentArgument(const Expr *E, QualType ForType,
10553 EvalInfo &Info, APSInt &Alignment) {
10554 if (!EvaluateInteger(E, Result&: Alignment, Info))
10555 return false;
10556 if (Alignment < 0 || !Alignment.isPowerOf2()) {
10557 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_alignment) << Alignment;
10558 return false;
10559 }
10560 unsigned SrcWidth = Info.Ctx.getIntWidth(T: ForType);
10561 APSInt MaxValue(APInt::getOneBitSet(numBits: SrcWidth, BitNo: SrcWidth - 1));
10562 if (APSInt::compareValues(I1: Alignment, I2: MaxValue) > 0) {
10563 Info.FFDiag(E, DiagId: diag::note_constexpr_alignment_too_big)
10564 << MaxValue << ForType << Alignment;
10565 return false;
10566 }
10567 // Ensure both alignment and source value have the same bit width so that we
10568 // don't assert when computing the resulting value.
10569 APSInt ExtAlignment =
10570 APSInt(Alignment.zextOrTrunc(width: SrcWidth), /*isUnsigned=*/true);
10571 assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 &&
10572 "Alignment should not be changed by ext/trunc");
10573 Alignment = ExtAlignment;
10574 assert(Alignment.getBitWidth() == SrcWidth);
10575 return true;
10576}
10577
10578// To be clear: this happily visits unsupported builtins. Better name welcomed.
10579bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {
10580 if (ExprEvaluatorBaseTy::VisitCallExpr(E))
10581 return true;
10582
10583 if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr()))
10584 return false;
10585
10586 Result.setInvalid(B: E);
10587 QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType();
10588 Result.addUnsizedArray(Info, E, ElemTy: PointeeTy);
10589 return true;
10590}
10591
10592bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
10593 if (!IsConstantEvaluatedBuiltinCall(E))
10594 return visitNonBuiltinCallExpr(E);
10595 return VisitBuiltinCallExpr(E, BuiltinOp: ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E));
10596}
10597
10598// Determine if T is a character type for which we guarantee that
10599// sizeof(T) == 1.
10600static bool isOneByteCharacterType(QualType T) {
10601 return T->isCharType() || T->isChar8Type();
10602}
10603
10604bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
10605 unsigned BuiltinOp) {
10606 if (IsOpaqueConstantCall(E))
10607 return Success(E);
10608
10609 switch (BuiltinOp) {
10610 case Builtin::BIaddressof:
10611 case Builtin::BI__addressof:
10612 case Builtin::BI__builtin_addressof:
10613 return evaluateLValue(E: E->getArg(Arg: 0), Result);
10614 case Builtin::BI__builtin_assume_aligned: {
10615 // We need to be very careful here because: if the pointer does not have the
10616 // asserted alignment, then the behavior is undefined, and undefined
10617 // behavior is non-constant.
10618 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10619 return false;
10620
10621 LValue OffsetResult(Result);
10622 APSInt Alignment;
10623 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10624 Alignment))
10625 return false;
10626 CharUnits Align = CharUnits::fromQuantity(Quantity: Alignment.getZExtValue());
10627
10628 if (E->getNumArgs() > 2) {
10629 APSInt Offset;
10630 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Offset, Info))
10631 return false;
10632
10633 int64_t AdditionalOffset = -Offset.getZExtValue();
10634 OffsetResult.Offset += CharUnits::fromQuantity(Quantity: AdditionalOffset);
10635 }
10636
10637 // If there is a base object, then it must have the correct alignment.
10638 if (OffsetResult.Base) {
10639 CharUnits BaseAlignment = getBaseAlignment(Info, Value: OffsetResult);
10640
10641 if (BaseAlignment < Align) {
10642 Result.Designator.setInvalid();
10643 CCEDiag(E: E->getArg(Arg: 0), D: diag::note_constexpr_baa_insufficient_alignment)
10644 << 0 << BaseAlignment.getQuantity() << Align.getQuantity();
10645 return false;
10646 }
10647 }
10648
10649 // The offset must also have the correct alignment.
10650 if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) {
10651 Result.Designator.setInvalid();
10652
10653 (OffsetResult.Base
10654 ? CCEDiag(E: E->getArg(Arg: 0),
10655 D: diag::note_constexpr_baa_insufficient_alignment)
10656 << 1
10657 : CCEDiag(E: E->getArg(Arg: 0),
10658 D: diag::note_constexpr_baa_value_insufficient_alignment))
10659 << OffsetResult.Offset.getQuantity() << Align.getQuantity();
10660 return false;
10661 }
10662
10663 return true;
10664 }
10665 case Builtin::BI__builtin_align_up:
10666 case Builtin::BI__builtin_align_down: {
10667 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10668 return false;
10669 APSInt Alignment;
10670 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10671 Alignment))
10672 return false;
10673 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Result);
10674 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Result.Offset);
10675 // For align_up/align_down, we can return the same value if the alignment
10676 // is known to be greater or equal to the requested value.
10677 if (PtrAlign.getQuantity() >= Alignment)
10678 return true;
10679
10680 // The alignment could be greater than the minimum at run-time, so we cannot
10681 // infer much about the resulting pointer value. One case is possible:
10682 // For `_Alignas(32) char buf[N]; __builtin_align_down(&buf[idx], 32)` we
10683 // can infer the correct index if the requested alignment is smaller than
10684 // the base alignment so we can perform the computation on the offset.
10685 if (BaseAlignment.getQuantity() >= Alignment) {
10686 assert(Alignment.getBitWidth() <= 64 &&
10687 "Cannot handle > 64-bit address-space");
10688 uint64_t Alignment64 = Alignment.getZExtValue();
10689 CharUnits NewOffset = CharUnits::fromQuantity(
10690 Quantity: BuiltinOp == Builtin::BI__builtin_align_down
10691 ? llvm::alignDown(Value: Result.Offset.getQuantity(), Align: Alignment64)
10692 : llvm::alignTo(Value: Result.Offset.getQuantity(), Align: Alignment64));
10693 Result.adjustOffset(N: NewOffset - Result.Offset);
10694 // TODO: diagnose out-of-bounds values/only allow for arrays?
10695 return true;
10696 }
10697 // Otherwise, we cannot constant-evaluate the result.
10698 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_adjust)
10699 << Alignment;
10700 return false;
10701 }
10702 case Builtin::BI__builtin_operator_new:
10703 return HandleOperatorNewCall(Info, E, Result);
10704 case Builtin::BI__builtin_launder:
10705 return evaluatePointer(E: E->getArg(Arg: 0), Result);
10706 case Builtin::BIstrchr:
10707 case Builtin::BIwcschr:
10708 case Builtin::BImemchr:
10709 case Builtin::BIwmemchr:
10710 if (Info.getLangOpts().CPlusPlus11)
10711 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10712 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10713 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10714 else
10715 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10716 [[fallthrough]];
10717 case Builtin::BI__builtin_strchr:
10718 case Builtin::BI__builtin_wcschr:
10719 case Builtin::BI__builtin_memchr:
10720 case Builtin::BI__builtin_char_memchr:
10721 case Builtin::BI__builtin_wmemchr: {
10722 if (!Visit(S: E->getArg(Arg: 0)))
10723 return false;
10724 APSInt Desired;
10725 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Desired, Info))
10726 return false;
10727 uint64_t MaxLength = uint64_t(-1);
10728 if (BuiltinOp != Builtin::BIstrchr &&
10729 BuiltinOp != Builtin::BIwcschr &&
10730 BuiltinOp != Builtin::BI__builtin_strchr &&
10731 BuiltinOp != Builtin::BI__builtin_wcschr) {
10732 APSInt N;
10733 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10734 return false;
10735 MaxLength = N.getZExtValue();
10736 }
10737 // We cannot find the value if there are no candidates to match against.
10738 if (MaxLength == 0u)
10739 return ZeroInitialization(E);
10740 if (!Result.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
10741 Result.Designator.Invalid)
10742 return false;
10743 QualType CharTy = Result.Designator.getType(Ctx&: Info.Ctx);
10744 bool IsRawByte = BuiltinOp == Builtin::BImemchr ||
10745 BuiltinOp == Builtin::BI__builtin_memchr;
10746 assert(IsRawByte ||
10747 Info.Ctx.hasSameUnqualifiedType(
10748 CharTy, E->getArg(0)->getType()->getPointeeType()));
10749 // Pointers to const void may point to objects of incomplete type.
10750 if (IsRawByte && CharTy->isIncompleteType()) {
10751 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_incomplete_type) << CharTy;
10752 return false;
10753 }
10754 // Give up on byte-oriented matching against multibyte elements.
10755 // FIXME: We can compare the bytes in the correct order.
10756 if (IsRawByte && !isOneByteCharacterType(T: CharTy)) {
10757 Info.FFDiag(E, DiagId: diag::note_constexpr_memchr_unsupported)
10758 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy;
10759 return false;
10760 }
10761 // Figure out what value we're actually looking for (after converting to
10762 // the corresponding unsigned type if necessary).
10763 uint64_t DesiredVal;
10764 bool StopAtNull = false;
10765 switch (BuiltinOp) {
10766 case Builtin::BIstrchr:
10767 case Builtin::BI__builtin_strchr:
10768 // strchr compares directly to the passed integer, and therefore
10769 // always fails if given an int that is not a char.
10770 if (!APSInt::isSameValue(I1: HandleIntToIntCast(Info, E, DestType: CharTy,
10771 SrcType: E->getArg(Arg: 1)->getType(),
10772 Value: Desired),
10773 I2: Desired))
10774 return ZeroInitialization(E);
10775 StopAtNull = true;
10776 [[fallthrough]];
10777 case Builtin::BImemchr:
10778 case Builtin::BI__builtin_memchr:
10779 case Builtin::BI__builtin_char_memchr:
10780 // memchr compares by converting both sides to unsigned char. That's also
10781 // correct for strchr if we get this far (to cope with plain char being
10782 // unsigned in the strchr case).
10783 DesiredVal = Desired.trunc(width: Info.Ctx.getCharWidth()).getZExtValue();
10784 break;
10785
10786 case Builtin::BIwcschr:
10787 case Builtin::BI__builtin_wcschr:
10788 StopAtNull = true;
10789 [[fallthrough]];
10790 case Builtin::BIwmemchr:
10791 case Builtin::BI__builtin_wmemchr:
10792 // wcschr and wmemchr are given a wchar_t to look for. Just use it.
10793 DesiredVal = Desired.getZExtValue();
10794 break;
10795 }
10796
10797 for (; MaxLength; --MaxLength) {
10798 APValue Char;
10799 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: Result, RVal&: Char) ||
10800 !Char.isInt())
10801 return false;
10802 if (Char.getInt().getZExtValue() == DesiredVal)
10803 return true;
10804 if (StopAtNull && !Char.getInt())
10805 break;
10806 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: CharTy, Adjustment: 1))
10807 return false;
10808 }
10809 // Not found: return nullptr.
10810 return ZeroInitialization(E);
10811 }
10812
10813 case Builtin::BImemcpy:
10814 case Builtin::BImemmove:
10815 case Builtin::BIwmemcpy:
10816 case Builtin::BIwmemmove:
10817 if (Info.getLangOpts().CPlusPlus11)
10818 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10819 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10820 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10821 else
10822 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10823 [[fallthrough]];
10824 case Builtin::BI__builtin_memcpy:
10825 case Builtin::BI__builtin_memmove:
10826 case Builtin::BI__builtin_wmemcpy:
10827 case Builtin::BI__builtin_wmemmove: {
10828 bool WChar = BuiltinOp == Builtin::BIwmemcpy ||
10829 BuiltinOp == Builtin::BIwmemmove ||
10830 BuiltinOp == Builtin::BI__builtin_wmemcpy ||
10831 BuiltinOp == Builtin::BI__builtin_wmemmove;
10832 bool Move = BuiltinOp == Builtin::BImemmove ||
10833 BuiltinOp == Builtin::BIwmemmove ||
10834 BuiltinOp == Builtin::BI__builtin_memmove ||
10835 BuiltinOp == Builtin::BI__builtin_wmemmove;
10836
10837 // The result of mem* is the first argument.
10838 if (!Visit(S: E->getArg(Arg: 0)))
10839 return false;
10840 LValue Dest = Result;
10841
10842 LValue Src;
10843 if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Src, Info))
10844 return false;
10845
10846 APSInt N;
10847 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10848 return false;
10849 assert(!N.isSigned() && "memcpy and friends take an unsigned size");
10850
10851 // If the size is zero, we treat this as always being a valid no-op.
10852 // (Even if one of the src and dest pointers is null.)
10853 if (!N)
10854 return true;
10855
10856 // Otherwise, if either of the operands is null, we can't proceed. Don't
10857 // try to determine the type of the copied objects, because there aren't
10858 // any.
10859 if (!Src.Base || !Dest.Base) {
10860 APValue Val;
10861 (!Src.Base ? Src : Dest).moveInto(V&: Val);
10862 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_null)
10863 << Move << WChar << !!Src.Base
10864 << Val.getAsString(Ctx: Info.Ctx, Ty: E->getArg(Arg: 0)->getType());
10865 return false;
10866 }
10867 if (Src.Designator.Invalid || Dest.Designator.Invalid)
10868 return false;
10869
10870 // We require that Src and Dest are both pointers to arrays of
10871 // trivially-copyable type. (For the wide version, the designator will be
10872 // invalid if the designated object is not a wchar_t.)
10873 QualType T = Dest.Designator.getType(Ctx&: Info.Ctx);
10874 QualType SrcT = Src.Designator.getType(Ctx&: Info.Ctx);
10875 if (!Info.Ctx.hasSameUnqualifiedType(T1: T, T2: SrcT)) {
10876 // FIXME: Consider using our bit_cast implementation to support this.
10877 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T;
10878 return false;
10879 }
10880 if (T->isIncompleteType()) {
10881 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_incomplete_type) << Move << T;
10882 return false;
10883 }
10884 if (!T.isTriviallyCopyableType(Context: Info.Ctx)) {
10885 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_nontrivial) << Move << T;
10886 return false;
10887 }
10888
10889 // Figure out how many T's we're copying.
10890 uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity();
10891 if (TSize == 0)
10892 return false;
10893 if (!WChar) {
10894 uint64_t Remainder;
10895 llvm::APInt OrigN = N;
10896 llvm::APInt::udivrem(LHS: OrigN, RHS: TSize, Quotient&: N, Remainder);
10897 if (Remainder) {
10898 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
10899 << Move << WChar << 0 << T << toString(I: OrigN, Radix: 10, /*Signed*/false)
10900 << (unsigned)TSize;
10901 return false;
10902 }
10903 }
10904
10905 // Check that the copying will remain within the arrays, just so that we
10906 // can give a more meaningful diagnostic. This implicitly also checks that
10907 // N fits into 64 bits.
10908 uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second;
10909 uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second;
10910 if (N.ugt(RHS: RemainingSrcSize) || N.ugt(RHS: RemainingDestSize)) {
10911 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
10912 << Move << WChar << (N.ugt(RHS: RemainingSrcSize) ? 1 : 2) << T
10913 << toString(I: N, Radix: 10, /*Signed*/false);
10914 return false;
10915 }
10916 uint64_t NElems = N.getZExtValue();
10917 uint64_t NBytes = NElems * TSize;
10918
10919 // Check for overlap.
10920 int Direction = 1;
10921 if (HasSameBase(A: Src, B: Dest)) {
10922 uint64_t SrcOffset = Src.getLValueOffset().getQuantity();
10923 uint64_t DestOffset = Dest.getLValueOffset().getQuantity();
10924 if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) {
10925 // Dest is inside the source region.
10926 if (!Move) {
10927 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
10928 return false;
10929 }
10930 // For memmove and friends, copy backwards.
10931 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: NElems - 1) ||
10932 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: NElems - 1))
10933 return false;
10934 Direction = -1;
10935 } else if (!Move && SrcOffset >= DestOffset &&
10936 SrcOffset - DestOffset < NBytes) {
10937 // Src is inside the destination region for memcpy: invalid.
10938 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
10939 return false;
10940 }
10941 }
10942
10943 while (true) {
10944 APValue Val;
10945 // FIXME: Set WantObjectRepresentation to true if we're copying a
10946 // char-like type?
10947 if (!handleLValueToRValueConversion(Info, Conv: E, Type: T, LVal: Src, RVal&: Val) ||
10948 !handleAssignment(Info, E, LVal: Dest, LValType: T, Val))
10949 return false;
10950 // Do not iterate past the last element; if we're copying backwards, that
10951 // might take us off the start of the array.
10952 if (--NElems == 0)
10953 return true;
10954 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: Direction) ||
10955 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: Direction))
10956 return false;
10957 }
10958 }
10959
10960 default:
10961 return false;
10962 }
10963}
10964
10965static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
10966 APValue &Result, const InitListExpr *ILE,
10967 QualType AllocType);
10968static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
10969 APValue &Result,
10970 const CXXConstructExpr *CCE,
10971 QualType AllocType);
10972
10973bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {
10974 if (!Info.getLangOpts().CPlusPlus20)
10975 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
10976
10977 // We cannot speculatively evaluate a delete expression.
10978 if (Info.SpeculativeEvaluationDepth)
10979 return false;
10980
10981 FunctionDecl *OperatorNew = E->getOperatorNew();
10982 QualType AllocType = E->getAllocatedType();
10983 QualType TargetType = AllocType;
10984
10985 bool IsNothrow = false;
10986 bool IsPlacement = false;
10987
10988 if (E->getNumPlacementArgs() == 1 &&
10989 E->getPlacementArg(I: 0)->getType()->isNothrowT()) {
10990 // The only new-placement list we support is of the form (std::nothrow).
10991 //
10992 // FIXME: There is no restriction on this, but it's not clear that any
10993 // other form makes any sense. We get here for cases such as:
10994 //
10995 // new (std::align_val_t{N}) X(int)
10996 //
10997 // (which should presumably be valid only if N is a multiple of
10998 // alignof(int), and in any case can't be deallocated unless N is
10999 // alignof(X) and X has new-extended alignment).
11000 LValue Nothrow;
11001 if (!EvaluateLValue(E: E->getPlacementArg(I: 0), Result&: Nothrow, Info))
11002 return false;
11003 IsNothrow = true;
11004 } else if (OperatorNew->isReservedGlobalPlacementOperator()) {
11005 if (Info.CurrentCall->isStdFunction() || Info.getLangOpts().CPlusPlus26 ||
11006 (Info.CurrentCall->CanEvalMSConstexpr &&
11007 OperatorNew->hasAttr<MSConstexprAttr>())) {
11008 if (!EvaluatePointer(E: E->getPlacementArg(I: 0), Result, Info))
11009 return false;
11010 if (Result.Designator.Invalid)
11011 return false;
11012 TargetType = E->getPlacementArg(I: 0)->getType();
11013 IsPlacement = true;
11014 } else {
11015 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
11016 << /*C++26 feature*/ 1 << E->getSourceRange();
11017 return false;
11018 }
11019 } else if (E->getNumPlacementArgs()) {
11020 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
11021 << /*Unsupported*/ 0 << E->getSourceRange();
11022 return false;
11023 } else if (!OperatorNew
11024 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
11025 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
11026 << isa<CXXMethodDecl>(Val: OperatorNew) << OperatorNew;
11027 return false;
11028 }
11029
11030 const Expr *Init = E->getInitializer();
11031 const InitListExpr *ResizedArrayILE = nullptr;
11032 const CXXConstructExpr *ResizedArrayCCE = nullptr;
11033 bool ValueInit = false;
11034
11035 if (std::optional<const Expr *> ArraySize = E->getArraySize()) {
11036 const Expr *Stripped = *ArraySize;
11037 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped);
11038 Stripped = ICE->getSubExpr())
11039 if (ICE->getCastKind() != CK_NoOp &&
11040 ICE->getCastKind() != CK_IntegralCast)
11041 break;
11042
11043 llvm::APSInt ArrayBound;
11044 if (!EvaluateInteger(E: Stripped, Result&: ArrayBound, Info))
11045 return false;
11046
11047 // C++ [expr.new]p9:
11048 // The expression is erroneous if:
11049 // -- [...] its value before converting to size_t [or] applying the
11050 // second standard conversion sequence is less than zero
11051 if (ArrayBound.isSigned() && ArrayBound.isNegative()) {
11052 if (IsNothrow)
11053 return ZeroInitialization(E);
11054
11055 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_negative)
11056 << ArrayBound << (*ArraySize)->getSourceRange();
11057 return false;
11058 }
11059
11060 // -- its value is such that the size of the allocated object would
11061 // exceed the implementation-defined limit
11062 if (!Info.CheckArraySize(Loc: ArraySize.value()->getExprLoc(),
11063 BitWidth: ConstantArrayType::getNumAddressingBits(
11064 Context: Info.Ctx, ElementType: AllocType, NumElements: ArrayBound),
11065 ElemCount: ArrayBound.getZExtValue(), /*Diag=*/!IsNothrow)) {
11066 if (IsNothrow)
11067 return ZeroInitialization(E);
11068 return false;
11069 }
11070
11071 // -- the new-initializer is a braced-init-list and the number of
11072 // array elements for which initializers are provided [...]
11073 // exceeds the number of elements to initialize
11074 if (!Init) {
11075 // No initialization is performed.
11076 } else if (isa<CXXScalarValueInitExpr>(Val: Init) ||
11077 isa<ImplicitValueInitExpr>(Val: Init)) {
11078 ValueInit = true;
11079 } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Val: Init)) {
11080 ResizedArrayCCE = CCE;
11081 } else {
11082 auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType());
11083 assert(CAT && "unexpected type for array initializer");
11084
11085 unsigned Bits =
11086 std::max(a: CAT->getSizeBitWidth(), b: ArrayBound.getBitWidth());
11087 llvm::APInt InitBound = CAT->getSize().zext(width: Bits);
11088 llvm::APInt AllocBound = ArrayBound.zext(width: Bits);
11089 if (InitBound.ugt(RHS: AllocBound)) {
11090 if (IsNothrow)
11091 return ZeroInitialization(E);
11092
11093 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_too_small)
11094 << toString(I: AllocBound, Radix: 10, /*Signed=*/false)
11095 << toString(I: InitBound, Radix: 10, /*Signed=*/false)
11096 << (*ArraySize)->getSourceRange();
11097 return false;
11098 }
11099
11100 // If the sizes differ, we must have an initializer list, and we need
11101 // special handling for this case when we initialize.
11102 if (InitBound != AllocBound)
11103 ResizedArrayILE = cast<InitListExpr>(Val: Init);
11104 }
11105
11106 AllocType = Info.Ctx.getConstantArrayType(EltTy: AllocType, ArySize: ArrayBound, SizeExpr: nullptr,
11107 ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
11108 } else if (E->isArray()) {
11109 // We have an array new-expression whose array size could not be
11110 // determined, e.g. 'new int[]()', where the bound is neither given nor
11111 // deducible from the initializer. This is ill-formed and already
11112 // diagnosed, so bail out rather than mis-evaluating a scalar allocation
11113 // as an array (which would later crash the evaluator).
11114 return false;
11115 } else {
11116 assert(!AllocType->isArrayType() &&
11117 "array allocation with non-array new");
11118 }
11119
11120 APValue *Val;
11121 if (IsPlacement) {
11122 AccessKinds AK = AK_Construct;
11123 struct FindObjectHandler {
11124 EvalInfo &Info;
11125 const Expr *E;
11126 QualType AllocType;
11127 const AccessKinds AccessKind;
11128 APValue *Value;
11129
11130 typedef bool result_type;
11131 bool failed() { return false; }
11132 bool checkConst(QualType QT) {
11133 if (QT.isConstQualified()) {
11134 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
11135 return false;
11136 }
11137 return true;
11138 }
11139 bool found(APValue &Subobj, QualType SubobjType,
11140 APValue::LValueBase Base) {
11141 if (!checkConst(QT: SubobjType))
11142 return false;
11143 // FIXME: Reject the cases where [basic.life]p8 would not permit the
11144 // old name of the object to be used to name the new object.
11145 if (!Info.Ctx.hasSimilarType(T1: SubobjType, T2: AllocType)) {
11146 Info.FFDiag(E, DiagId: diag::note_constexpr_placement_new_wrong_type)
11147 << SubobjType << AllocType;
11148 return false;
11149 }
11150 Value = &Subobj;
11151 return true;
11152 }
11153 bool found(APSInt &Value, QualType SubobjType) {
11154 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
11155 return false;
11156 }
11157 bool found(APFloat &Value, QualType SubobjType) {
11158 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
11159 return false;
11160 }
11161 } Handler = {.Info: Info, .E: E, .AllocType: AllocType, .AccessKind: AK, .Value: nullptr};
11162
11163 if (AllocType->isArrayType() &&
11164 Result.Designator.MostDerivedIsArrayElement &&
11165 Result.Designator.Entries.back().getAsArrayIndex() == 0) {
11166 // The destination of placement new is pointing to the first element
11167 // of an array. There's a special case in [expr.const]: "[...] if T is an
11168 // array type, to the first element of such an object [...]". Handle
11169 // that case here by dropping the last entry in the designator list.
11170 QualType AllocElementType =
11171 Info.Ctx.getAsArrayType(T: AllocType)->getElementType();
11172 if (Info.Ctx.hasSimilarType(T1: AllocElementType,
11173 T2: Result.Designator.MostDerivedType)) {
11174 Result.Designator.truncate(Ctx&: Info.Ctx, Base: Result.Base,
11175 NewLength: Result.Designator.MostDerivedPathLength - 1);
11176 }
11177 }
11178
11179 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: Result, LValType: AllocType);
11180 if (!Obj || !findSubobject(Info, E, Obj, Sub: Result.Designator, handler&: Handler))
11181 return false;
11182
11183 Val = Handler.Value;
11184
11185 // [basic.life]p1:
11186 // The lifetime of an object o of type T ends when [...] the storage
11187 // which the object occupies is [...] reused by an object that is not
11188 // nested within o (6.6.2).
11189 *Val = APValue();
11190 } else {
11191 // Perform the allocation and obtain a pointer to the resulting object.
11192 Val = Info.createHeapAlloc(E, T: AllocType, LV&: Result);
11193 if (!Val)
11194 return false;
11195 }
11196
11197 if (ValueInit) {
11198 ImplicitValueInitExpr VIE(AllocType);
11199 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: &VIE))
11200 return false;
11201 } else if (ResizedArrayILE) {
11202 if (!EvaluateArrayNewInitList(Info, This&: Result, Result&: *Val, ILE: ResizedArrayILE,
11203 AllocType))
11204 return false;
11205 } else if (ResizedArrayCCE) {
11206 if (!EvaluateArrayNewConstructExpr(Info, This&: Result, Result&: *Val, CCE: ResizedArrayCCE,
11207 AllocType))
11208 return false;
11209 } else if (Init) {
11210 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: Init))
11211 return false;
11212 } else if (!handleDefaultInitValue(T: AllocType, Result&: *Val)) {
11213 return false;
11214 }
11215
11216 // Array new returns a pointer to the first element, not a pointer to the
11217 // array.
11218 if (auto *AT = AllocType->getAsArrayTypeUnsafe())
11219 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val: AT));
11220
11221 return true;
11222}
11223//===----------------------------------------------------------------------===//
11224// Member Pointer Evaluation
11225//===----------------------------------------------------------------------===//
11226
11227namespace {
11228class MemberPointerExprEvaluator
11229 : public ExprEvaluatorBase<MemberPointerExprEvaluator> {
11230 MemberPtr &Result;
11231
11232 bool Success(const ValueDecl *D) {
11233 Result = MemberPtr(D);
11234 return true;
11235 }
11236public:
11237
11238 MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
11239 : ExprEvaluatorBaseTy(Info), Result(Result) {}
11240
11241 bool Success(const APValue &V, const Expr *E) {
11242 Result.setFrom(V);
11243 return true;
11244 }
11245 bool ZeroInitialization(const Expr *E) {
11246 return Success(D: (const ValueDecl*)nullptr);
11247 }
11248
11249 bool VisitCastExpr(const CastExpr *E);
11250 bool VisitUnaryAddrOf(const UnaryOperator *E);
11251};
11252} // end anonymous namespace
11253
11254static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
11255 EvalInfo &Info) {
11256 assert(!E->isValueDependent());
11257 assert(E->isPRValue() && E->getType()->isMemberPointerType());
11258 return MemberPointerExprEvaluator(Info, Result).Visit(S: E);
11259}
11260
11261bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
11262 switch (E->getCastKind()) {
11263 default:
11264 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11265
11266 case CK_NullToMemberPointer:
11267 VisitIgnoredValue(E: E->getSubExpr());
11268 return ZeroInitialization(E);
11269
11270 case CK_BaseToDerivedMemberPointer: {
11271 if (!Visit(S: E->getSubExpr()))
11272 return false;
11273 if (E->path_empty())
11274 return true;
11275 // Base-to-derived member pointer casts store the path in derived-to-base
11276 // order, so iterate backwards. The CXXBaseSpecifier also provides us with
11277 // the wrong end of the derived->base arc, so stagger the path by one class.
11278 typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
11279 for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
11280 PathI != PathE; ++PathI) {
11281 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11282 const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
11283 if (!Result.castToDerived(Derived))
11284 return Error(E);
11285 }
11286 if (!Result.castToDerived(Derived: E->getType()
11287 ->castAs<MemberPointerType>()
11288 ->getMostRecentCXXRecordDecl()))
11289 return Error(E);
11290 return true;
11291 }
11292
11293 case CK_DerivedToBaseMemberPointer:
11294 if (!Visit(S: E->getSubExpr()))
11295 return false;
11296 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11297 PathE = E->path_end(); PathI != PathE; ++PathI) {
11298 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11299 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11300 if (!Result.castToBase(Base))
11301 return Error(E);
11302 }
11303 return true;
11304 }
11305}
11306
11307bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
11308 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
11309 // member can be formed.
11310 return Success(D: cast<DeclRefExpr>(Val: E->getSubExpr())->getDecl());
11311}
11312
11313//===----------------------------------------------------------------------===//
11314// Record Evaluation
11315//===----------------------------------------------------------------------===//
11316
11317namespace {
11318 class RecordExprEvaluator
11319 : public ExprEvaluatorBase<RecordExprEvaluator> {
11320 const LValue &This;
11321 APValue &Result;
11322 public:
11323
11324 RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
11325 : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
11326
11327 bool Success(const APValue &V, const Expr *E) {
11328 Result = V;
11329 return true;
11330 }
11331 bool ZeroInitialization(const Expr *E) {
11332 return ZeroInitialization(E, T: E->getType());
11333 }
11334 bool ZeroInitialization(const Expr *E, QualType T);
11335
11336 bool VisitCallExpr(const CallExpr *E) {
11337 return handleCallExpr(E, Result, ResultSlot: &This);
11338 }
11339 bool VisitCastExpr(const CastExpr *E);
11340 bool VisitInitListExpr(const InitListExpr *E);
11341 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
11342 return VisitCXXConstructExpr(E, T: E->getType());
11343 }
11344 bool VisitLambdaExpr(const LambdaExpr *E);
11345 bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E);
11346 bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T);
11347 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E);
11348 bool VisitBinCmp(const BinaryOperator *E);
11349 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
11350 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
11351 ArrayRef<Expr *> Args);
11352 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
11353 };
11354}
11355
11356/// Perform zero-initialization on an object of non-union class type.
11357/// C++11 [dcl.init]p5:
11358/// To zero-initialize an object or reference of type T means:
11359/// [...]
11360/// -- if T is a (possibly cv-qualified) non-union class type,
11361/// each non-static data member and each base-class subobject is
11362/// zero-initialized
11363static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
11364 const RecordDecl *RD,
11365 const LValue &This, APValue &Result,
11366 bool IsCompleteClass = true) {
11367 assert(!RD->isUnion() && "Expected non-union class type");
11368 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD);
11369
11370 if (CD) {
11371 unsigned NonVirtualBases = countNonVirtualBases(RD: CD);
11372 Result =
11373 APValue(APValue::UninitStruct(), NonVirtualBases, RD->getNumFields(),
11374 IsCompleteClass ? CD->getNumVBases() : 0);
11375 } else {
11376 Result = APValue(APValue::UninitStruct(), 0, RD->getNumFields());
11377 }
11378
11379 if (RD->isInvalidDecl()) return false;
11380 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11381
11382 if (CD) {
11383 unsigned Index = 0;
11384
11385 for (const auto &B : CD->bases()) {
11386 if (B.isVirtual())
11387 continue;
11388 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
11389 LValue Subobject = This;
11390 if (!HandleLValueDirectBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11391 return false;
11392 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11393 Result&: Result.getStructBase(i: Index),
11394 /*IsCompleteClass=*/false))
11395 return false;
11396 ++Index;
11397 }
11398 }
11399
11400 for (const auto *I : RD->fields()) {
11401 // -- if T is a reference type, no initialization is performed.
11402 if (I->isUnnamedBitField() || I->getType()->isReferenceType())
11403 continue;
11404
11405 LValue Subobject = This;
11406 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: I, RL: &Layout))
11407 return false;
11408
11409 ImplicitValueInitExpr VIE(I->getType());
11410 if (!EvaluateInPlace(
11411 Result&: Result.getStructField(i: I->getFieldIndex()), Info, This: Subobject, E: &VIE))
11412 return false;
11413 }
11414
11415 if (CD && This.pointsToCompleteClass(D: CD)) {
11416 unsigned Index = 0;
11417 for (const auto &B : CD->vbases()) {
11418 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
11419 LValue Subobject = This;
11420 if (!HandleLValueDirectVirtualBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11421 return false;
11422 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11423 Result&: Result.getStructVirtualBase(i: Index),
11424 /*IsCompleteClass=*/false))
11425 return false;
11426 ++Index;
11427 }
11428 }
11429
11430 return true;
11431}
11432
11433bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) {
11434 const auto *RD = T->castAsRecordDecl();
11435 if (RD->isInvalidDecl()) return false;
11436 if (RD->isUnion()) {
11437 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
11438 // object's first non-static named data member is zero-initialized
11439 RecordDecl::field_iterator I = RD->field_begin();
11440 while (I != RD->field_end() && (*I)->isUnnamedBitField())
11441 ++I;
11442 if (I == RD->field_end()) {
11443 Result = APValue((const FieldDecl*)nullptr);
11444 return true;
11445 }
11446
11447 LValue Subobject = This;
11448 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: *I))
11449 return false;
11450 Result = APValue(*I);
11451 ImplicitValueInitExpr VIE(I->getType());
11452 return EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: &VIE);
11453 }
11454
11455 if (!Info.getLangOpts().CPlusPlus26) {
11456 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11457 CXXRD && CXXRD->getNumVBases()) {
11458 Info.FFDiag(E, DiagId: diag::note_constexpr_virtual_base) << RD;
11459 return false;
11460 }
11461 }
11462
11463 return HandleClassZeroInitialization(Info, E, RD, This, Result);
11464}
11465
11466bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
11467 switch (E->getCastKind()) {
11468 default:
11469 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11470
11471 case CK_ConstructorConversion:
11472 return Visit(S: E->getSubExpr());
11473
11474 case CK_DerivedToBase:
11475 case CK_UncheckedDerivedToBase: {
11476 APValue DerivedObject;
11477 if (!Evaluate(Result&: DerivedObject, Info, E: E->getSubExpr()))
11478 return false;
11479 if (!DerivedObject.isStruct())
11480 return Error(E: E->getSubExpr());
11481
11482 // Derived-to-base rvalue conversion: just slice off the derived part.
11483 APValue *Value = &DerivedObject;
11484 const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
11485 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11486 PathE = E->path_end(); PathI != PathE; ++PathI) {
11487 assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
11488 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11489 Value = &Value->getStructBase(i: getBaseIndex(Derived: RD, Base));
11490 RD = Base;
11491 }
11492 Result = *Value;
11493 return true;
11494 }
11495 case CK_HLSLAggregateSplatCast: {
11496 APValue Val;
11497 QualType ValTy;
11498
11499 if (!hlslAggSplatHelper(Info, E: E->getSubExpr(), SrcVal&: Val, SrcTy&: ValTy))
11500 return false;
11501
11502 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
11503 // splat our Val
11504 SmallVector<APValue> SplatEls(NEls, Val);
11505 SmallVector<QualType> SplatType(NEls, ValTy);
11506
11507 // cast the elements and construct our struct result
11508 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11509 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
11510 ElTypes&: SplatType))
11511 return false;
11512
11513 return true;
11514 }
11515 case CK_HLSLElementwiseCast: {
11516 SmallVector<APValue> SrcEls;
11517 SmallVector<QualType> SrcTypes;
11518
11519 if (!hlslElementwiseCastHelper(Info, E: E->getSubExpr(), DestTy: E->getType(), SrcVals&: SrcEls,
11520 SrcTypes))
11521 return false;
11522
11523 // cast the elements and construct our struct result
11524 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11525 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
11526 ElTypes&: SrcTypes))
11527 return false;
11528
11529 return true;
11530 }
11531 case CK_ToUnion: {
11532 const FieldDecl *Field = E->getTargetUnionField();
11533 LValue Subobject = This;
11534 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field))
11535 return false;
11536 Result = APValue(Field);
11537 if (!EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject,
11538 E: E->getSubExpr()))
11539 return false;
11540 if (Field->isBitField()) {
11541 if (!truncateBitfieldValue(Info, E: E->getSubExpr(), Value&: Result.getUnionValue(),
11542 FD: Field))
11543 return false;
11544 }
11545 return true;
11546 }
11547 }
11548}
11549
11550bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
11551 if (E->isTransparent())
11552 return Visit(S: E->getInit(Init: 0));
11553 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits());
11554}
11555
11556bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr(
11557 const Expr *ExprToVisit, ArrayRef<Expr *> Args) {
11558 const auto *RD = ExprToVisit->getType()->castAsRecordDecl();
11559 if (RD->isInvalidDecl()) return false;
11560 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11561 auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11562
11563 EvalInfo::EvaluatingConstructorRAII EvalObj(
11564 Info,
11565 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
11566 CXXRD && CXXRD->getNumBases());
11567
11568 if (RD->isUnion()) {
11569 const FieldDecl *Field;
11570 if (auto *ILE = dyn_cast<InitListExpr>(Val: ExprToVisit)) {
11571 Field = ILE->getInitializedFieldInUnion();
11572 } else if (auto *PLIE = dyn_cast<CXXParenListInitExpr>(Val: ExprToVisit)) {
11573 Field = PLIE->getInitializedFieldInUnion();
11574 } else {
11575 llvm_unreachable(
11576 "Expression is neither an init list nor a C++ paren list");
11577 }
11578
11579 Result = APValue(Field);
11580 if (!Field)
11581 return true;
11582
11583 // If the initializer list for a union does not contain any elements, the
11584 // first element of the union is value-initialized.
11585 // FIXME: The element should be initialized from an initializer list.
11586 // Is this difference ever observable for initializer lists which
11587 // we don't build?
11588 ImplicitValueInitExpr VIE(Field->getType());
11589 const Expr *InitExpr = Args.empty() ? &VIE : Args[0];
11590
11591 LValue Subobject = This;
11592 if (!HandleLValueMember(Info, E: InitExpr, LVal&: Subobject, FD: Field, RL: &Layout))
11593 return false;
11594
11595 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11596 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11597 isa<CXXDefaultInitExpr>(Val: InitExpr));
11598
11599 if (EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: InitExpr)) {
11600 if (Field->isBitField())
11601 return truncateBitfieldValue(Info, E: InitExpr, Value&: Result.getUnionValue(),
11602 FD: Field);
11603 return true;
11604 }
11605
11606 return false;
11607 }
11608
11609 if (!Result.hasValue())
11610 Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0,
11611 RD->getNumFields());
11612 unsigned ElementNo = 0;
11613 bool Success = true;
11614
11615 // Initialize base classes.
11616 if (CXXRD && CXXRD->getNumBases()) {
11617 for (const auto &Base : CXXRD->bases()) {
11618 assert(ElementNo < Args.size() && "missing init for base class");
11619 const Expr *Init = Args[ElementNo];
11620
11621 LValue Subobject = This;
11622 if (!HandleLValueBase(Info, E: Init, Obj&: Subobject, DerivedDecl: CXXRD, Base: &Base))
11623 return false;
11624
11625 APValue &FieldVal = Result.getStructBase(i: ElementNo);
11626 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init)) {
11627 if (!Info.noteFailure())
11628 return false;
11629 Success = false;
11630 }
11631 ++ElementNo;
11632 }
11633
11634 EvalObj.finishedConstructingBases();
11635 }
11636
11637 // Initialize members.
11638 for (const auto *Field : RD->fields()) {
11639 // Anonymous bit-fields are not considered members of the class for
11640 // purposes of aggregate initialization.
11641 if (Field->isUnnamedBitField())
11642 continue;
11643
11644 LValue Subobject = This;
11645
11646 bool HaveInit = ElementNo < Args.size();
11647
11648 // FIXME: Diagnostics here should point to the end of the initializer
11649 // list, not the start.
11650 if (!HandleLValueMember(Info, E: HaveInit ? Args[ElementNo] : ExprToVisit,
11651 LVal&: Subobject, FD: Field, RL: &Layout))
11652 return false;
11653
11654 // Perform an implicit value-initialization for members beyond the end of
11655 // the initializer list.
11656 ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
11657 const Expr *Init = HaveInit ? Args[ElementNo++] : &VIE;
11658
11659 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
11660 // aren't supposed to be modified.
11661 if (isa<NoInitExpr>(Val: Init))
11662 continue;
11663
11664 if (Field->getType()->isIncompleteArrayType()) {
11665 if (auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType())) {
11666 if (!CAT->isZeroSize()) {
11667 // Bail out for now. This might sort of "work", but the rest of the
11668 // code isn't really prepared to handle it.
11669 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_unsupported_flexible_array);
11670 return false;
11671 }
11672 }
11673 }
11674
11675 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11676 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11677 isa<CXXDefaultInitExpr>(Val: Init));
11678
11679 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
11680 if (Field->getType()->isReferenceType()) {
11681 LValue Result;
11682 if (!EvaluateInitForDeclOfReferenceType(Info, D: Field, Init, Result,
11683 Val&: FieldVal)) {
11684 if (!Info.noteFailure())
11685 return false;
11686 Success = false;
11687 }
11688 } else if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init) ||
11689 (Field->isBitField() &&
11690 !truncateBitfieldValue(Info, E: Init, Value&: FieldVal, FD: Field))) {
11691 if (!Info.noteFailure())
11692 return false;
11693 Success = false;
11694 }
11695 }
11696
11697 EvalObj.finishedConstructingFields();
11698
11699 return Success;
11700}
11701
11702bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
11703 QualType T) {
11704 // Note that E's type is not necessarily the type of our class here; we might
11705 // be initializing an array element instead.
11706 const CXXConstructorDecl *FD = E->getConstructor();
11707 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false;
11708
11709 bool ZeroInit = E->requiresZeroInitialization();
11710 if (CheckTrivialDefaultConstructor(Info, Loc: E->getExprLoc(), CD: FD, IsValueInitialization: ZeroInit)) {
11711 if (ZeroInit)
11712 return ZeroInitialization(E, T);
11713
11714 return handleDefaultInitValue(T, Result);
11715 }
11716
11717 const FunctionDecl *Definition = nullptr;
11718 auto Body = FD->getBody(Definition);
11719
11720 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11721 return false;
11722
11723 // Avoid materializing a temporary for an elidable copy/move constructor.
11724 if (E->isElidable() && !ZeroInit) {
11725 // FIXME: This only handles the simplest case, where the source object
11726 // is passed directly as the first argument to the constructor.
11727 // This should also handle stepping though implicit casts and
11728 // and conversion sequences which involve two steps, with a
11729 // conversion operator followed by a converting constructor.
11730 const Expr *SrcObj = E->getArg(Arg: 0);
11731 assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent()));
11732 assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType()));
11733 if (const MaterializeTemporaryExpr *ME =
11734 dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj))
11735 return Visit(S: ME->getSubExpr());
11736 }
11737
11738 if (ZeroInit && !ZeroInitialization(E, T))
11739 return false;
11740
11741 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
11742 return HandleConstructorCall(E, This, Args,
11743 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
11744 Result);
11745}
11746
11747bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr(
11748 const CXXInheritedCtorInitExpr *E) {
11749 if (!Info.CurrentCall) {
11750 assert(Info.checkingPotentialConstantExpression());
11751 return false;
11752 }
11753
11754 const CXXConstructorDecl *FD = E->getConstructor();
11755 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl())
11756 return false;
11757
11758 const FunctionDecl *Definition = nullptr;
11759 auto Body = FD->getBody(Definition);
11760
11761 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11762 return false;
11763
11764 return HandleConstructorCall(E, This, Call: Info.CurrentCall->Arguments,
11765 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
11766 Result);
11767}
11768
11769bool RecordExprEvaluator::VisitCXXStdInitializerListExpr(
11770 const CXXStdInitializerListExpr *E) {
11771 const ConstantArrayType *ArrayType =
11772 Info.Ctx.getAsConstantArrayType(T: E->getSubExpr()->getType());
11773
11774 LValue Array;
11775 if (!EvaluateLValue(E: E->getSubExpr(), Result&: Array, Info))
11776 return false;
11777
11778 assert(ArrayType && "unexpected type for array initializer");
11779
11780 // Get a pointer to the first element of the array.
11781 Array.addArray(Info, E, CAT: ArrayType);
11782
11783 // FIXME: What if the initializer_list type has base classes, etc?
11784 Result = APValue(APValue::UninitStruct(), 0, 2);
11785 Array.moveInto(V&: Result.getStructField(i: 0));
11786
11787 auto *Record = E->getType()->castAsRecordDecl();
11788 RecordDecl::field_iterator Field = Record->field_begin();
11789 assert(Field != Record->field_end() &&
11790 Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
11791 ArrayType->getElementType()) &&
11792 "Expected std::initializer_list first field to be const E *");
11793 ++Field;
11794 assert(Field != Record->field_end() &&
11795 "Expected std::initializer_list to have two fields");
11796
11797 if (Info.Ctx.hasSameType(T1: Field->getType(), T2: Info.Ctx.getSizeType())) {
11798 // Length.
11799 Result.getStructField(i: 1) = APValue(APSInt(ArrayType->getSize()));
11800 } else {
11801 // End pointer.
11802 assert(Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
11803 ArrayType->getElementType()) &&
11804 "Expected std::initializer_list second field to be const E *");
11805 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Array,
11806 EltTy: ArrayType->getElementType(),
11807 Adjustment: ArrayType->getZExtSize()))
11808 return false;
11809 Array.moveInto(V&: Result.getStructField(i: 1));
11810 }
11811
11812 assert(++Field == Record->field_end() &&
11813 "Expected std::initializer_list to only have two fields");
11814
11815 return true;
11816}
11817
11818bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {
11819 const CXXRecordDecl *ClosureClass = E->getLambdaClass();
11820 if (ClosureClass->isInvalidDecl())
11821 return false;
11822
11823 const size_t NumFields = ClosureClass->getNumFields();
11824
11825 assert(NumFields == (size_t)std::distance(E->capture_init_begin(),
11826 E->capture_init_end()) &&
11827 "The number of lambda capture initializers should equal the number of "
11828 "fields within the closure type");
11829
11830 Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields);
11831 // Iterate through all the lambda's closure object's fields and initialize
11832 // them.
11833 auto *CaptureInitIt = E->capture_init_begin();
11834 bool Success = true;
11835 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: ClosureClass);
11836 for (const auto *Field : ClosureClass->fields()) {
11837 assert(CaptureInitIt != E->capture_init_end());
11838 // Get the initializer for this field
11839 Expr *const CurFieldInit = *CaptureInitIt++;
11840
11841 // If there is no initializer, either this is a VLA or an error has
11842 // occurred.
11843 if (!CurFieldInit || CurFieldInit->containsErrors())
11844 return Error(E);
11845
11846 LValue Subobject = This;
11847
11848 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field, RL: &Layout))
11849 return false;
11850
11851 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
11852 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: CurFieldInit)) {
11853 if (!Info.keepEvaluatingAfterFailure())
11854 return false;
11855 Success = false;
11856 }
11857 }
11858 return Success;
11859}
11860
11861bool RecordExprEvaluator::VisitDesignatedInitUpdateExpr(
11862 const DesignatedInitUpdateExpr *E) {
11863 if (!Visit(S: E->getBase()))
11864 return false;
11865 return Visit(S: E->getUpdater());
11866}
11867
11868static bool EvaluateRecord(const Expr *E, const LValue &This,
11869 APValue &Result, EvalInfo &Info) {
11870 assert(!E->isValueDependent());
11871 assert(E->isPRValue() && E->getType()->isRecordType() &&
11872 "can't evaluate expression as a record rvalue");
11873 return RecordExprEvaluator(Info, This, Result).Visit(S: E);
11874}
11875
11876//===----------------------------------------------------------------------===//
11877// Temporary Evaluation
11878//
11879// Temporaries are represented in the AST as rvalues, but generally behave like
11880// lvalues. The full-object of which the temporary is a subobject is implicitly
11881// materialized so that a reference can bind to it.
11882//===----------------------------------------------------------------------===//
11883namespace {
11884class TemporaryExprEvaluator
11885 : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
11886public:
11887 TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
11888 LValueExprEvaluatorBaseTy(Info, Result, false) {}
11889
11890 /// Visit an expression which constructs the value of this temporary.
11891 bool VisitConstructExpr(const Expr *E) {
11892 APValue &Value = Info.CurrentCall->createTemporary(
11893 Key: E, T: E->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
11894 return EvaluateInPlace(Result&: Value, Info, This: Result, E);
11895 }
11896
11897 bool VisitCastExpr(const CastExpr *E) {
11898 switch (E->getCastKind()) {
11899 default:
11900 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
11901
11902 case CK_ConstructorConversion:
11903 return VisitConstructExpr(E: E->getSubExpr());
11904 }
11905 }
11906 bool VisitInitListExpr(const InitListExpr *E) {
11907 return VisitConstructExpr(E);
11908 }
11909 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
11910 return VisitConstructExpr(E);
11911 }
11912 bool VisitCallExpr(const CallExpr *E) {
11913 return VisitConstructExpr(E);
11914 }
11915 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) {
11916 return VisitConstructExpr(E);
11917 }
11918 bool VisitLambdaExpr(const LambdaExpr *E) {
11919 return VisitConstructExpr(E);
11920 }
11921};
11922} // end anonymous namespace
11923
11924/// Evaluate an expression of record type as a temporary.
11925static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
11926 assert(!E->isValueDependent());
11927 assert(E->isPRValue() && E->getType()->isRecordType());
11928 return TemporaryExprEvaluator(Info, Result).Visit(S: E);
11929}
11930
11931//===----------------------------------------------------------------------===//
11932// Vector Evaluation
11933//===----------------------------------------------------------------------===//
11934
11935namespace {
11936 class VectorExprEvaluator
11937 : public ExprEvaluatorBase<VectorExprEvaluator> {
11938 APValue &Result;
11939 public:
11940
11941 VectorExprEvaluator(EvalInfo &info, APValue &Result)
11942 : ExprEvaluatorBaseTy(info), Result(Result) {}
11943
11944 bool Success(ArrayRef<APValue> V, const Expr *E) {
11945 assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
11946 // FIXME: remove this APValue copy.
11947 Result = APValue(V.data(), V.size());
11948 return true;
11949 }
11950 bool Success(const APValue &V, const Expr *E) {
11951 assert(V.isVector());
11952 Result = V;
11953 return true;
11954 }
11955 bool ZeroInitialization(const Expr *E);
11956
11957 bool VisitUnaryReal(const UnaryOperator *E)
11958 { return Visit(S: E->getSubExpr()); }
11959 bool VisitCastExpr(const CastExpr* E);
11960 bool VisitInitListExpr(const InitListExpr *E);
11961 bool VisitUnaryImag(const UnaryOperator *E);
11962 bool VisitBinaryOperator(const BinaryOperator *E);
11963 bool VisitUnaryOperator(const UnaryOperator *E);
11964 bool VisitCallExpr(const CallExpr *E);
11965 bool VisitConvertVectorExpr(const ConvertVectorExpr *E);
11966 bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E);
11967
11968 // FIXME: Missing: conditional operator (for GNU
11969 // conditional select), ExtVectorElementExpr
11970 };
11971} // end anonymous namespace
11972
11973static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
11974 assert(E->isPRValue() && E->getType()->isVectorType() &&
11975 "not a vector prvalue");
11976 return VectorExprEvaluator(Info, Result).Visit(S: E);
11977}
11978
11979static llvm::APInt ConvertBoolVectorToInt(const APValue &Val) {
11980 assert(Val.isVector() && "expected vector APValue");
11981 unsigned NumElts = Val.getVectorLength();
11982
11983 // Each element is one bit, so create an integer with NumElts bits.
11984 llvm::APInt Result(NumElts, 0);
11985
11986 for (unsigned I = 0; I < NumElts; ++I) {
11987 const APValue &Elt = Val.getVectorElt(I);
11988 assert(Elt.isInt() && "expected integer element in bool vector");
11989
11990 if (Elt.getInt().getBoolValue())
11991 Result.setBit(I);
11992 }
11993
11994 return Result;
11995}
11996
11997bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) {
11998 const VectorType *VTy = E->getType()->castAs<VectorType>();
11999 unsigned NElts = VTy->getNumElements();
12000
12001 const Expr *SE = E->getSubExpr();
12002 QualType SETy = SE->getType();
12003
12004 switch (E->getCastKind()) {
12005 case CK_VectorSplat: {
12006 APValue Val = APValue();
12007 if (SETy->isIntegerType()) {
12008 APSInt IntResult;
12009 if (!EvaluateInteger(E: SE, Result&: IntResult, Info))
12010 return false;
12011 Val = APValue(std::move(IntResult));
12012 } else if (SETy->isRealFloatingType()) {
12013 APFloat FloatResult(0.0);
12014 if (!EvaluateFloat(E: SE, Result&: FloatResult, Info))
12015 return false;
12016 Val = APValue(std::move(FloatResult));
12017 } else {
12018 return Error(E);
12019 }
12020
12021 // Splat and create vector APValue.
12022 SmallVector<APValue, 4> Elts(NElts, Val);
12023 return Success(V: Elts, E);
12024 }
12025 case CK_BitCast: {
12026 APValue SVal;
12027 if (!Evaluate(Result&: SVal, Info, E: SE))
12028 return false;
12029
12030 if (!SVal.isInt() && !SVal.isFloat() && !SVal.isVector()) {
12031 // Give up if the input isn't an int, float, or vector. For example, we
12032 // reject "(v4i16)(intptr_t)&a".
12033 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_cast)
12034 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
12035 << Info.Ctx.getLangOpts().CPlusPlus;
12036 return false;
12037 }
12038
12039 if (!handleRValueToRValueBitCast(Info, DestValue&: Result, SourceRValue: SVal, BCE: E))
12040 return false;
12041
12042 return true;
12043 }
12044 case CK_HLSLVectorTruncation: {
12045 APValue Val;
12046 SmallVector<APValue, 4> Elements;
12047 if (!EvaluateVector(E: SE, Result&: Val, Info))
12048 return Error(E);
12049 for (unsigned I = 0; I < NElts; I++)
12050 Elements.push_back(Elt: Val.getVectorElt(I));
12051 return Success(V: Elements, E);
12052 }
12053 case CK_HLSLMatrixTruncation: {
12054 // Matrix truncation occurs in row-major order.
12055 APValue Val;
12056 if (!EvaluateMatrix(E: SE, Result&: Val, Info))
12057 return Error(E);
12058 SmallVector<APValue, 16> Elements;
12059 for (unsigned Row = 0;
12060 Row < Val.getMatrixNumRows() && Elements.size() < NElts; Row++)
12061 for (unsigned Col = 0;
12062 Col < Val.getMatrixNumColumns() && Elements.size() < NElts; Col++)
12063 Elements.push_back(Elt: Val.getMatrixElt(Row, Col));
12064 return Success(V: Elements, E);
12065 }
12066 case CK_HLSLAggregateSplatCast: {
12067 APValue Val;
12068 QualType ValTy;
12069
12070 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
12071 return false;
12072
12073 // cast our Val once.
12074 APValue Result;
12075 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12076 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: VTy->getElementType(), Original: Val,
12077 Result))
12078 return false;
12079
12080 SmallVector<APValue, 4> SplatEls(NElts, Result);
12081 return Success(V: SplatEls, E);
12082 }
12083 case CK_HLSLElementwiseCast: {
12084 SmallVector<APValue> SrcVals;
12085 SmallVector<QualType> SrcTypes;
12086
12087 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
12088 return false;
12089
12090 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12091 SmallVector<QualType, 4> DestTypes(NElts, VTy->getElementType());
12092 SmallVector<APValue, 4> ResultEls(NElts);
12093 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
12094 Results&: ResultEls))
12095 return false;
12096 return Success(V: ResultEls, E);
12097 }
12098 case CK_IntegralToFloating:
12099 case CK_FloatingToIntegral:
12100 case CK_IntegralCast:
12101 case CK_FloatingCast:
12102 case CK_FloatingToBoolean:
12103 case CK_IntegralToBoolean: {
12104 // These casts apply element-wise when the source is a vector type.
12105 assert(SETy->isVectorType() && "expected vector source type");
12106 APValue SrcVal;
12107 if (!EvaluateVector(E: SE, Result&: SrcVal, Info))
12108 return Error(E);
12109
12110 assert(SrcVal.getVectorLength() == NElts);
12111 QualType SrcEltTy = SETy->castAs<VectorType>()->getElementType();
12112 QualType DstEltTy = VTy->getElementType();
12113 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12114
12115 SmallVector<APValue, 4> ResultEls(NElts);
12116 for (unsigned I = 0; I < NElts; ++I) {
12117 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcEltTy, DestTy: DstEltTy,
12118 Original: SrcVal.getVectorElt(I), Result&: ResultEls[I]))
12119 return Error(E);
12120 }
12121 return Success(V: ResultEls, E);
12122 }
12123 default:
12124 return ExprEvaluatorBaseTy::VisitCastExpr(E);
12125 }
12126}
12127
12128bool
12129VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
12130 const VectorType *VT = E->getType()->castAs<VectorType>();
12131 unsigned NumInits = E->getNumInits();
12132 unsigned NumElements = VT->getNumElements();
12133
12134 QualType EltTy = VT->getElementType();
12135 SmallVector<APValue, 4> Elements;
12136
12137 // MFloat8 type doesn't have constants and thus constant folding
12138 // is impossible.
12139 if (EltTy->isMFloat8Type())
12140 return false;
12141
12142 // The number of initializers can be less than the number of
12143 // vector elements. For OpenCL, this can be due to nested vector
12144 // initialization. For GCC compatibility, missing trailing elements
12145 // should be initialized with zeroes.
12146 unsigned CountInits = 0, CountElts = 0;
12147 while (CountElts < NumElements) {
12148 // Handle nested vector initialization.
12149 if (CountInits < NumInits
12150 && E->getInit(Init: CountInits)->getType()->isVectorType()) {
12151 APValue v;
12152 if (!EvaluateVector(E: E->getInit(Init: CountInits), Result&: v, Info))
12153 return Error(E);
12154 unsigned vlen = v.getVectorLength();
12155 for (unsigned j = 0; j < vlen; j++)
12156 Elements.push_back(Elt: v.getVectorElt(I: j));
12157 CountElts += vlen;
12158 } else if (EltTy->isIntegerType()) {
12159 llvm::APSInt sInt(32);
12160 if (CountInits < NumInits) {
12161 if (!EvaluateInteger(E: E->getInit(Init: CountInits), Result&: sInt, Info))
12162 return false;
12163 } else // trailing integer zero.
12164 sInt = Info.Ctx.MakeIntValue(Value: 0, Type: EltTy);
12165 Elements.push_back(Elt: APValue(sInt));
12166 CountElts++;
12167 } else {
12168 llvm::APFloat f(0.0);
12169 if (CountInits < NumInits) {
12170 if (!EvaluateFloat(E: E->getInit(Init: CountInits), Result&: f, Info))
12171 return false;
12172 } else // trailing float zero.
12173 f = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy));
12174 Elements.push_back(Elt: APValue(f));
12175 CountElts++;
12176 }
12177 CountInits++;
12178 }
12179 return Success(V: Elements, E);
12180}
12181
12182bool
12183VectorExprEvaluator::ZeroInitialization(const Expr *E) {
12184 const auto *VT = E->getType()->castAs<VectorType>();
12185 QualType EltTy = VT->getElementType();
12186 APValue ZeroElement;
12187 if (EltTy->isIntegerType())
12188 ZeroElement = APValue(Info.Ctx.MakeIntValue(Value: 0, Type: EltTy));
12189 else
12190 ZeroElement =
12191 APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy)));
12192
12193 SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
12194 return Success(V: Elements, E);
12195}
12196
12197bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
12198 VisitIgnoredValue(E: E->getSubExpr());
12199 return ZeroInitialization(E);
12200}
12201
12202bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
12203 BinaryOperatorKind Op = E->getOpcode();
12204 assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp &&
12205 "Operation not supported on vector types");
12206
12207 if (Op == BO_Comma)
12208 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
12209
12210 Expr *LHS = E->getLHS();
12211 Expr *RHS = E->getRHS();
12212
12213 assert(LHS->getType()->isVectorType() && RHS->getType()->isVectorType() &&
12214 "Must both be vector types");
12215 // Checking JUST the types are the same would be fine, except shifts don't
12216 // need to have their types be the same (since you always shift by an int).
12217 assert(LHS->getType()->castAs<VectorType>()->getNumElements() ==
12218 E->getType()->castAs<VectorType>()->getNumElements() &&
12219 RHS->getType()->castAs<VectorType>()->getNumElements() ==
12220 E->getType()->castAs<VectorType>()->getNumElements() &&
12221 "All operands must be the same size.");
12222
12223 APValue LHSValue;
12224 APValue RHSValue;
12225 bool LHSOK = Evaluate(Result&: LHSValue, Info, E: LHS);
12226 if (!LHSOK && !Info.noteFailure())
12227 return false;
12228 if (!Evaluate(Result&: RHSValue, Info, E: RHS) || !LHSOK)
12229 return false;
12230
12231 if (!handleVectorVectorBinOp(Info, E, Opcode: Op, LHSValue, RHSValue))
12232 return false;
12233
12234 return Success(V: LHSValue, E);
12235}
12236
12237static std::optional<APValue> handleVectorUnaryOperator(ASTContext &Ctx,
12238 QualType ResultTy,
12239 UnaryOperatorKind Op,
12240 APValue Elt) {
12241 switch (Op) {
12242 case UO_Plus:
12243 // Nothing to do here.
12244 return Elt;
12245 case UO_Minus:
12246 if (Elt.getKind() == APValue::Int) {
12247 Elt.getInt().negate();
12248 } else {
12249 assert(Elt.getKind() == APValue::Float &&
12250 "Vector can only be int or float type");
12251 Elt.getFloat().changeSign();
12252 }
12253 return Elt;
12254 case UO_Not:
12255 // This is only valid for integral types anyway, so we don't have to handle
12256 // float here.
12257 assert(Elt.getKind() == APValue::Int &&
12258 "Vector operator ~ can only be int");
12259 Elt.getInt().flipAllBits();
12260 return Elt;
12261 case UO_LNot: {
12262 if (Elt.getKind() == APValue::Int) {
12263 Elt.getInt() = !Elt.getInt();
12264 // operator ! on vectors returns -1 for 'truth', so negate it.
12265 Elt.getInt().negate();
12266 return Elt;
12267 }
12268 assert(Elt.getKind() == APValue::Float &&
12269 "Vector can only be int or float type");
12270 // Float types result in an int of the same size, but -1 for true, or 0 for
12271 // false.
12272 APSInt EltResult{Ctx.getIntWidth(T: ResultTy),
12273 ResultTy->isUnsignedIntegerType()};
12274 if (Elt.getFloat().isZero())
12275 EltResult.setAllBits();
12276 else
12277 EltResult.clearAllBits();
12278
12279 return APValue{EltResult};
12280 }
12281 default:
12282 // FIXME: Implement the rest of the unary operators.
12283 return std::nullopt;
12284 }
12285}
12286
12287bool VectorExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
12288 Expr *SubExpr = E->getSubExpr();
12289 const auto *VD = SubExpr->getType()->castAs<VectorType>();
12290 // This result element type differs in the case of negating a floating point
12291 // vector, since the result type is the a vector of the equivilant sized
12292 // integer.
12293 const QualType ResultEltTy = VD->getElementType();
12294 UnaryOperatorKind Op = E->getOpcode();
12295
12296 APValue SubExprValue;
12297 if (!Evaluate(Result&: SubExprValue, Info, E: SubExpr))
12298 return false;
12299
12300 // FIXME: This vector evaluator someday needs to be changed to be LValue
12301 // aware/keep LValue information around, rather than dealing with just vector
12302 // types directly. Until then, we cannot handle cases where the operand to
12303 // these unary operators is an LValue. The only case I've been able to see
12304 // cause this is operator++ assigning to a member expression (only valid in
12305 // altivec compilations) in C mode, so this shouldn't limit us too much.
12306 if (SubExprValue.isLValue())
12307 return false;
12308
12309 assert(SubExprValue.getVectorLength() == VD->getNumElements() &&
12310 "Vector length doesn't match type?");
12311
12312 SmallVector<APValue, 4> ResultElements;
12313 for (unsigned EltNum = 0; EltNum < VD->getNumElements(); ++EltNum) {
12314 std::optional<APValue> Elt = handleVectorUnaryOperator(
12315 Ctx&: Info.Ctx, ResultTy: ResultEltTy, Op, Elt: SubExprValue.getVectorElt(I: EltNum));
12316 if (!Elt)
12317 return false;
12318 ResultElements.push_back(Elt: *Elt);
12319 }
12320 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12321}
12322
12323static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO,
12324 const Expr *E, QualType SourceTy,
12325 QualType DestTy, APValue const &Original,
12326 APValue &Result) {
12327 if (SourceTy->isIntegerType()) {
12328 if (DestTy->isRealFloatingType()) {
12329 Result = APValue(APFloat(0.0));
12330 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
12331 DestType: DestTy, Result&: Result.getFloat());
12332 }
12333 if (DestTy->isIntegerType()) {
12334 Result = APValue(
12335 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
12336 return true;
12337 }
12338 } else if (SourceTy->isRealFloatingType()) {
12339 if (DestTy->isRealFloatingType()) {
12340 Result = Original;
12341 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
12342 Result&: Result.getFloat());
12343 }
12344 if (DestTy->isIntegerType()) {
12345 Result = APValue(APSInt());
12346 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
12347 DestType: DestTy, Result&: Result.getInt());
12348 }
12349 }
12350
12351 Info.FFDiag(E, DiagId: diag::err_convertvector_constexpr_unsupported_vector_cast)
12352 << SourceTy << DestTy;
12353 return false;
12354}
12355
12356static bool evalPackBuiltin(const CallExpr *E, EvalInfo &Info, APValue &Result,
12357 llvm::function_ref<APInt(const APSInt &)> PackFn) {
12358 APValue LHS, RHS;
12359 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: LHS) ||
12360 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: RHS))
12361 return false;
12362
12363 unsigned LHSVecLen = LHS.getVectorLength();
12364 unsigned RHSVecLen = RHS.getVectorLength();
12365
12366 assert(LHSVecLen != 0 && LHSVecLen == RHSVecLen &&
12367 "pack builtin LHSVecLen must equal to RHSVecLen");
12368
12369 const VectorType *VT0 = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
12370 const unsigned SrcBits = Info.Ctx.getIntWidth(T: VT0->getElementType());
12371
12372 const VectorType *DstVT = E->getType()->castAs<VectorType>();
12373 QualType DstElemTy = DstVT->getElementType();
12374 const bool DstIsUnsigned = DstElemTy->isUnsignedIntegerType();
12375
12376 const unsigned SrcPerLane = 128 / SrcBits;
12377 const unsigned Lanes = LHSVecLen * SrcBits / 128;
12378
12379 SmallVector<APValue, 64> Out;
12380 Out.reserve(N: LHSVecLen + RHSVecLen);
12381
12382 for (unsigned Lane = 0; Lane != Lanes; ++Lane) {
12383 unsigned base = Lane * SrcPerLane;
12384 for (unsigned I = 0; I != SrcPerLane; ++I)
12385 Out.emplace_back(Args: APValue(
12386 APSInt(PackFn(LHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12387 for (unsigned I = 0; I != SrcPerLane; ++I)
12388 Out.emplace_back(Args: APValue(
12389 APSInt(PackFn(RHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12390 }
12391
12392 Result = APValue(Out.data(), Out.size());
12393 return true;
12394}
12395
12396static bool evalShuffleGeneric(
12397 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12398 llvm::function_ref<std::pair<unsigned, int>(unsigned, unsigned)>
12399 GetSourceIndex) {
12400
12401 const auto *VT = Call->getType()->getAs<VectorType>();
12402 if (!VT)
12403 return false;
12404
12405 unsigned ShuffleMask = 0;
12406 APValue A, MaskVector, B;
12407 bool IsVectorMask = false;
12408 bool IsSingleOperand = (Call->getNumArgs() == 2);
12409
12410 if (IsSingleOperand) {
12411 QualType MaskType = Call->getArg(Arg: 1)->getType();
12412 if (MaskType->isVectorType()) {
12413 IsVectorMask = true;
12414 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12415 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector))
12416 return false;
12417 B = A;
12418 } else if (MaskType->isIntegerType()) {
12419 APSInt MaskImm;
12420 if (!EvaluateInteger(E: Call->getArg(Arg: 1), Result&: MaskImm, Info))
12421 return false;
12422 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12423 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A))
12424 return false;
12425 B = A;
12426 } else {
12427 return false;
12428 }
12429 } else {
12430 QualType Arg2Type = Call->getArg(Arg: 2)->getType();
12431 if (Arg2Type->isVectorType()) {
12432 IsVectorMask = true;
12433 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12434 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector) ||
12435 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 2), Result&: B))
12436 return false;
12437 } else if (Arg2Type->isIntegerType()) {
12438 APSInt MaskImm;
12439 if (!EvaluateInteger(E: Call->getArg(Arg: 2), Result&: MaskImm, Info))
12440 return false;
12441 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12442 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12443 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: B))
12444 return false;
12445 } else {
12446 return false;
12447 }
12448 }
12449
12450 unsigned NumElts = VT->getNumElements();
12451 SmallVector<APValue, 64> ResultElements;
12452 ResultElements.reserve(N: NumElts);
12453
12454 for (unsigned DstIdx = 0; DstIdx != NumElts; ++DstIdx) {
12455 if (IsVectorMask) {
12456 ShuffleMask = static_cast<unsigned>(
12457 MaskVector.getVectorElt(I: DstIdx).getInt().getZExtValue());
12458 }
12459 auto [SrcVecIdx, SrcIdx] = GetSourceIndex(DstIdx, ShuffleMask);
12460
12461 if (SrcIdx < 0) {
12462 // Zero out this element
12463 QualType ElemTy = VT->getElementType();
12464 if (ElemTy->isRealFloatingType()) {
12465 ResultElements.push_back(
12466 Elt: APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy))));
12467 } else if (ElemTy->isIntegerType()) {
12468 APValue Zero(Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy));
12469 ResultElements.push_back(Elt: APValue(Zero));
12470 } else {
12471 // Other types of fallback logic
12472 ResultElements.push_back(Elt: APValue());
12473 }
12474 } else {
12475 const APValue &Src = (SrcVecIdx == 0) ? A : B;
12476 ResultElements.push_back(Elt: Src.getVectorElt(I: SrcIdx));
12477 }
12478 }
12479
12480 Out = APValue(ResultElements.data(), ResultElements.size());
12481 return true;
12482}
12483static bool ConvertDoubleToFloatStrict(EvalInfo &Info, const Expr *E,
12484 APFloat OrigVal, APValue &Result) {
12485
12486 if (OrigVal.isInfinity()) {
12487 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 0;
12488 return false;
12489 }
12490 if (OrigVal.isNaN()) {
12491 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 1;
12492 return false;
12493 }
12494
12495 APFloat Val = OrigVal;
12496 bool LosesInfo = false;
12497 APFloat::opStatus Status = Val.convert(
12498 ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven, losesInfo: &LosesInfo);
12499
12500 if (LosesInfo || Val.isDenormal()) {
12501 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
12502 return false;
12503 }
12504
12505 if (Status != APFloat::opOK) {
12506 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
12507 return false;
12508 }
12509
12510 Result = APValue(Val);
12511 return true;
12512}
12513static bool evalShiftWithCount(
12514 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12515 llvm::function_ref<APInt(const APInt &, uint64_t)> ShiftOp,
12516 llvm::function_ref<APInt(const APInt &, unsigned)> OverflowOp) {
12517
12518 APValue Source, Count;
12519 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: Source) ||
12520 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: Count))
12521 return false;
12522
12523 assert(Call->getNumArgs() == 2);
12524
12525 QualType SourceTy = Call->getArg(Arg: 0)->getType();
12526 assert(SourceTy->isVectorType() &&
12527 Call->getArg(1)->getType()->isVectorType());
12528
12529 QualType DestEltTy = SourceTy->castAs<VectorType>()->getElementType();
12530 unsigned DestEltWidth = Source.getVectorElt(I: 0).getInt().getBitWidth();
12531 unsigned DestLen = Source.getVectorLength();
12532 bool IsDestUnsigned = DestEltTy->isUnsignedIntegerType();
12533 unsigned CountEltWidth = Count.getVectorElt(I: 0).getInt().getBitWidth();
12534 unsigned NumBitsInQWord = 64;
12535 unsigned NumCountElts = NumBitsInQWord / CountEltWidth;
12536 SmallVector<APValue, 64> Result;
12537 Result.reserve(N: DestLen);
12538
12539 uint64_t CountLQWord = 0;
12540 for (unsigned EltIdx = 0; EltIdx != NumCountElts; ++EltIdx) {
12541 uint64_t Elt = Count.getVectorElt(I: EltIdx).getInt().getZExtValue();
12542 CountLQWord |= (Elt << (EltIdx * CountEltWidth));
12543 }
12544
12545 for (unsigned EltIdx = 0; EltIdx != DestLen; ++EltIdx) {
12546 APInt Elt = Source.getVectorElt(I: EltIdx).getInt();
12547 if (CountLQWord < DestEltWidth) {
12548 Result.push_back(
12549 Elt: APValue(APSInt(ShiftOp(Elt, CountLQWord), IsDestUnsigned)));
12550 } else {
12551 Result.push_back(
12552 Elt: APValue(APSInt(OverflowOp(Elt, DestEltWidth), IsDestUnsigned)));
12553 }
12554 }
12555 Out = APValue(Result.data(), Result.size());
12556 return true;
12557}
12558
12559std::optional<APFloat> EvalScalarMinMaxFp(const APFloat &A, const APFloat &B,
12560 std::optional<APSInt> RoundingMode,
12561 bool IsMin) {
12562 APSInt DefaultMode(APInt(32, 4), /*isUnsigned=*/true);
12563 if (RoundingMode.value_or(u&: DefaultMode) != 4)
12564 return std::nullopt;
12565 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
12566 B.isInfinity() || B.isDenormal())
12567 return std::nullopt;
12568 if (A.isZero() && B.isZero())
12569 return B;
12570 return IsMin ? llvm::minimum(A, B) : llvm::maximum(A, B);
12571}
12572
12573bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
12574 if (!IsConstantEvaluatedBuiltinCall(E))
12575 return ExprEvaluatorBaseTy::VisitCallExpr(E);
12576
12577 unsigned BuiltinOp = ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E);
12578
12579 auto EvaluateBinOpExpr =
12580 [&](llvm::function_ref<APInt(const APSInt &, const APSInt &)> Fn) {
12581 APValue SourceLHS, SourceRHS;
12582 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12583 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12584 return false;
12585
12586 auto *DestTy = E->getType()->castAs<VectorType>();
12587 QualType DestEltTy = DestTy->getElementType();
12588 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12589 unsigned SourceLen = SourceLHS.getVectorLength();
12590 SmallVector<APValue, 4> ResultElements;
12591 ResultElements.reserve(N: SourceLen);
12592
12593 if (SourceRHS.isInt()) {
12594 const APSInt &RHS = SourceRHS.getInt();
12595 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12596 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12597 ResultElements.push_back(
12598 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12599 }
12600 } else {
12601 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12602 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12603 const APSInt &RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
12604 ResultElements.push_back(
12605 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12606 }
12607 }
12608 return Success(V: APValue(ResultElements.data(), SourceLen), E);
12609 };
12610
12611 auto EvaluateFpBinOpExpr =
12612 [&](llvm::function_ref<std::optional<APFloat>(
12613 const APFloat &, const APFloat &, std::optional<APSInt>)>
12614 Fn,
12615 bool IsScalar = false) {
12616 assert(E->getNumArgs() == 2 || E->getNumArgs() == 3);
12617 APValue A, B;
12618 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
12619 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
12620 return false;
12621
12622 assert(A.isVector() && B.isVector());
12623 assert(A.getVectorLength() == B.getVectorLength());
12624
12625 std::optional<APSInt> RoundingMode;
12626 if (E->getNumArgs() == 3) {
12627 APSInt Imm;
12628 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
12629 return false;
12630 RoundingMode = Imm;
12631 }
12632
12633 unsigned NumElems = A.getVectorLength();
12634 SmallVector<APValue, 4> ResultElements;
12635 ResultElements.reserve(N: NumElems);
12636
12637 for (unsigned EltNum = 0; EltNum < NumElems; ++EltNum) {
12638 if (IsScalar && EltNum > 0) {
12639 ResultElements.push_back(Elt: A.getVectorElt(I: EltNum));
12640 continue;
12641 }
12642 const APFloat &EltA = A.getVectorElt(I: EltNum).getFloat();
12643 const APFloat &EltB = B.getVectorElt(I: EltNum).getFloat();
12644 std::optional<APFloat> Result = Fn(EltA, EltB, RoundingMode);
12645 if (!Result)
12646 return false;
12647 ResultElements.push_back(Elt: APValue(*Result));
12648 }
12649 return Success(V: APValue(ResultElements.data(), NumElems), E);
12650 };
12651
12652 auto EvaluateScalarFpRoundMaskBinOp =
12653 [&](llvm::function_ref<std::optional<APFloat>(
12654 const APFloat &, const APFloat &, std::optional<APSInt>)>
12655 Fn) {
12656 assert(E->getNumArgs() == 5);
12657 APValue VecA, VecB, VecSrc;
12658 APSInt MaskVal, Rounding;
12659
12660 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
12661 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
12662 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
12663 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskVal, Info) ||
12664 !EvaluateInteger(E: E->getArg(Arg: 4), Result&: Rounding, Info))
12665 return false;
12666
12667 unsigned NumElems = VecA.getVectorLength();
12668 SmallVector<APValue, 8> ResultElements;
12669 ResultElements.reserve(N: NumElems);
12670
12671 if (MaskVal.getZExtValue() & 1) {
12672 const APFloat &EltA = VecA.getVectorElt(I: 0).getFloat();
12673 const APFloat &EltB = VecB.getVectorElt(I: 0).getFloat();
12674 std::optional<APFloat> Result = Fn(EltA, EltB, Rounding);
12675 if (!Result)
12676 return false;
12677 ResultElements.push_back(Elt: APValue(*Result));
12678 } else {
12679 ResultElements.push_back(Elt: VecSrc.getVectorElt(I: 0));
12680 }
12681
12682 for (unsigned I = 1; I < NumElems; ++I)
12683 ResultElements.push_back(Elt: VecA.getVectorElt(I));
12684
12685 return Success(V: APValue(ResultElements.data(), NumElems), E);
12686 };
12687
12688 auto EvalSelectScalar = [&](unsigned Len) -> bool {
12689 APSInt Mask;
12690 APValue AVal, WVal;
12691 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info) ||
12692 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: AVal) ||
12693 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: WVal))
12694 return false;
12695
12696 bool TakeA0 = (Mask.getZExtValue() & 1u) != 0;
12697 SmallVector<APValue, 4> Res;
12698 Res.reserve(N: Len);
12699 Res.push_back(Elt: TakeA0 ? AVal.getVectorElt(I: 0) : WVal.getVectorElt(I: 0));
12700 for (unsigned I = 1; I < Len; ++I)
12701 Res.push_back(Elt: WVal.getVectorElt(I));
12702 APValue V(Res.data(), Res.size());
12703 return Success(V, E);
12704 };
12705
12706 auto EvalVectorDotProduct = [&](bool IsSaturating) -> bool {
12707 APValue Source, OperandA, OperandB;
12708 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
12709 !EvaluateVector(E: E->getArg(Arg: 1), Result&: OperandA, Info) ||
12710 !EvaluateVector(E: E->getArg(Arg: 2), Result&: OperandB, Info)) {
12711 return false;
12712 }
12713
12714 unsigned NumSrcElems = Source.getVectorLength();
12715 unsigned NumOperandElems = OperandA.getVectorLength();
12716 unsigned ElemsPerLane = NumOperandElems / NumSrcElems;
12717
12718 assert(OperandA.getVectorLength() == OperandB.getVectorLength());
12719
12720 SmallVector<APValue, 16> Result;
12721 Result.reserve(N: NumSrcElems);
12722 for (unsigned I = 0; I != NumSrcElems; ++I) {
12723 APSInt DotProduct = Source.getVectorElt(I).getInt();
12724 DotProduct = DotProduct.extend(width: 64);
12725 for (unsigned J = 0; J != ElemsPerLane; ++J) {
12726 APSInt OpA = APSInt(
12727 OperandA.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
12728 false);
12729 APSInt OpB = APSInt(
12730 OperandB.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
12731 false);
12732 DotProduct += OpA * OpB;
12733 }
12734 if (IsSaturating) {
12735 DotProduct = APSInt(DotProduct.truncSSat(width: 32), false);
12736 } else {
12737 DotProduct = APSInt(DotProduct.trunc(width: 32), false);
12738 }
12739 Result.push_back(Elt: APValue(DotProduct));
12740 }
12741
12742 return Success(V: APValue(Result.data(), Result.size()), E);
12743 };
12744
12745 switch (BuiltinOp) {
12746 default:
12747 return false;
12748 case Builtin::BI__builtin_elementwise_popcount:
12749 case Builtin::BI__builtin_elementwise_bitreverse: {
12750 APValue Source;
12751 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
12752 return false;
12753
12754 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12755 unsigned SourceLen = Source.getVectorLength();
12756 SmallVector<APValue, 4> ResultElements;
12757 ResultElements.reserve(N: SourceLen);
12758
12759 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12760 APSInt Elt = Source.getVectorElt(I: EltNum).getInt();
12761 switch (BuiltinOp) {
12762 case Builtin::BI__builtin_elementwise_popcount:
12763 ResultElements.push_back(Elt: APValue(
12764 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), Elt.popcount()),
12765 DestEltTy->isUnsignedIntegerOrEnumerationType())));
12766 break;
12767 case Builtin::BI__builtin_elementwise_bitreverse:
12768 ResultElements.push_back(
12769 Elt: APValue(APSInt(Elt.reverseBits(),
12770 DestEltTy->isUnsignedIntegerOrEnumerationType())));
12771 break;
12772 }
12773 }
12774
12775 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12776 }
12777 case Builtin::BI__builtin_elementwise_abs: {
12778 APValue Source;
12779 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
12780 return false;
12781
12782 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12783 unsigned SourceLen = Source.getVectorLength();
12784 SmallVector<APValue, 4> ResultElements;
12785 ResultElements.reserve(N: SourceLen);
12786
12787 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12788 APValue CurrentEle = Source.getVectorElt(I: EltNum);
12789 APValue Val = DestEltTy->isFloatingType()
12790 ? APValue(llvm::abs(X: CurrentEle.getFloat()))
12791 : APValue(APSInt(
12792 CurrentEle.getInt().abs(),
12793 DestEltTy->isUnsignedIntegerOrEnumerationType()));
12794 ResultElements.push_back(Elt: Val);
12795 }
12796
12797 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12798 }
12799
12800 case Builtin::BI__builtin_elementwise_add_sat:
12801 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12802 return LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
12803 });
12804
12805 case Builtin::BI__builtin_elementwise_sub_sat:
12806 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12807 return LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
12808 });
12809
12810 case X86::BI__builtin_ia32_extract128i256:
12811 case X86::BI__builtin_ia32_vextractf128_pd256:
12812 case X86::BI__builtin_ia32_vextractf128_ps256:
12813 case X86::BI__builtin_ia32_vextractf128_si256: {
12814 APValue SourceVec, SourceImm;
12815 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
12816 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceImm))
12817 return false;
12818
12819 if (!SourceVec.isVector())
12820 return false;
12821
12822 const auto *RetVT = E->getType()->castAs<VectorType>();
12823 unsigned RetLen = RetVT->getNumElements();
12824 unsigned Idx = SourceImm.getInt().getZExtValue() & 1;
12825
12826 SmallVector<APValue, 32> ResultElements;
12827 ResultElements.reserve(N: RetLen);
12828
12829 for (unsigned I = 0; I < RetLen; I++)
12830 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Idx * RetLen + I));
12831
12832 return Success(V: APValue(ResultElements.data(), RetLen), E);
12833 }
12834
12835 case clang::X86::BI__builtin_ia32_cvtmask2b128:
12836 case clang::X86::BI__builtin_ia32_cvtmask2b256:
12837 case clang::X86::BI__builtin_ia32_cvtmask2b512:
12838 case clang::X86::BI__builtin_ia32_cvtmask2w128:
12839 case clang::X86::BI__builtin_ia32_cvtmask2w256:
12840 case clang::X86::BI__builtin_ia32_cvtmask2w512:
12841 case clang::X86::BI__builtin_ia32_cvtmask2d128:
12842 case clang::X86::BI__builtin_ia32_cvtmask2d256:
12843 case clang::X86::BI__builtin_ia32_cvtmask2d512:
12844 case clang::X86::BI__builtin_ia32_cvtmask2q128:
12845 case clang::X86::BI__builtin_ia32_cvtmask2q256:
12846 case clang::X86::BI__builtin_ia32_cvtmask2q512: {
12847 assert(E->getNumArgs() == 1);
12848 APSInt Mask;
12849 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info))
12850 return false;
12851
12852 QualType VecTy = E->getType();
12853 const VectorType *VT = VecTy->castAs<VectorType>();
12854 unsigned VectorLen = VT->getNumElements();
12855 QualType ElemTy = VT->getElementType();
12856 unsigned ElemWidth = Info.Ctx.getTypeSize(T: ElemTy);
12857
12858 SmallVector<APValue, 16> Elems;
12859 for (unsigned I = 0; I != VectorLen; ++I) {
12860 bool BitSet = Mask[I];
12861 APSInt ElemVal(ElemWidth, /*isUnsigned=*/false);
12862 if (BitSet) {
12863 ElemVal.setAllBits();
12864 }
12865 Elems.push_back(Elt: APValue(ElemVal));
12866 }
12867 return Success(V: APValue(Elems.data(), VectorLen), E);
12868 }
12869
12870 case X86::BI__builtin_ia32_extracti32x4_256_mask:
12871 case X86::BI__builtin_ia32_extractf32x4_256_mask:
12872 case X86::BI__builtin_ia32_extracti32x4_mask:
12873 case X86::BI__builtin_ia32_extractf32x4_mask:
12874 case X86::BI__builtin_ia32_extracti32x8_mask:
12875 case X86::BI__builtin_ia32_extractf32x8_mask:
12876 case X86::BI__builtin_ia32_extracti64x2_256_mask:
12877 case X86::BI__builtin_ia32_extractf64x2_256_mask:
12878 case X86::BI__builtin_ia32_extracti64x2_512_mask:
12879 case X86::BI__builtin_ia32_extractf64x2_512_mask:
12880 case X86::BI__builtin_ia32_extracti64x4_mask:
12881 case X86::BI__builtin_ia32_extractf64x4_mask: {
12882 APValue SourceVec, MergeVec;
12883 APSInt Imm, MaskImm;
12884
12885 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
12886 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info) ||
12887 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MergeVec) ||
12888 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskImm, Info))
12889 return false;
12890
12891 const auto *RetVT = E->getType()->castAs<VectorType>();
12892 unsigned RetLen = RetVT->getNumElements();
12893
12894 if (!SourceVec.isVector() || !MergeVec.isVector())
12895 return false;
12896 unsigned SrcLen = SourceVec.getVectorLength();
12897 unsigned Lanes = SrcLen / RetLen;
12898 unsigned Lane = static_cast<unsigned>(Imm.getZExtValue() % Lanes);
12899 unsigned Base = Lane * RetLen;
12900
12901 SmallVector<APValue, 32> ResultElements;
12902 ResultElements.reserve(N: RetLen);
12903 for (unsigned I = 0; I < RetLen; ++I) {
12904 if (MaskImm[I])
12905 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Base + I));
12906 else
12907 ResultElements.push_back(Elt: MergeVec.getVectorElt(I));
12908 }
12909 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12910 }
12911
12912 case clang::X86::BI__builtin_ia32_pavgb128:
12913 case clang::X86::BI__builtin_ia32_pavgw128:
12914 case clang::X86::BI__builtin_ia32_pavgb256:
12915 case clang::X86::BI__builtin_ia32_pavgw256:
12916 case clang::X86::BI__builtin_ia32_pavgb512:
12917 case clang::X86::BI__builtin_ia32_pavgw512:
12918 return EvaluateBinOpExpr(llvm::APIntOps::avgCeilU);
12919
12920 case clang::X86::BI__builtin_ia32_pmulhrsw128:
12921 case clang::X86::BI__builtin_ia32_pmulhrsw256:
12922 case clang::X86::BI__builtin_ia32_pmulhrsw512:
12923 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12924 return (llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS).ashr(ShiftAmt: 14) + 1)
12925 .extractBits(numBits: 16, bitPosition: 1);
12926 });
12927
12928 case clang::X86::BI__builtin_ia32_psadbw128:
12929 case clang::X86::BI__builtin_ia32_psadbw256:
12930 case clang::X86::BI__builtin_ia32_psadbw512: {
12931 APValue SourceLHS, SourceRHS;
12932 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12933 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12934 return false;
12935
12936 assert(SourceLHS.isVector() && SourceRHS.isVector());
12937 unsigned SourceLen = SourceLHS.getVectorLength();
12938 assert(SourceLen == SourceRHS.getVectorLength());
12939 assert((SourceLen % 8) == 0);
12940
12941 auto *DestTy = E->getType()->castAs<VectorType>();
12942 QualType DestEltTy = DestTy->getElementType();
12943 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12944 SmallVector<APValue, 8> ResultElements;
12945 ResultElements.reserve(N: SourceLen / 8);
12946
12947 for (unsigned Lane = 0; Lane != SourceLen; Lane += 8) {
12948 APInt Sum(64, 0);
12949 for (unsigned I = 0; I != 8; ++I) {
12950 APInt LHS = SourceLHS.getVectorElt(I: Lane + I).getInt().extOrTrunc(width: 8);
12951 APInt RHS = SourceRHS.getVectorElt(I: Lane + I).getInt().extOrTrunc(width: 8);
12952 Sum += llvm::APIntOps::abdu(A: LHS, B: RHS).zext(width: 64);
12953 }
12954 ResultElements.push_back(Elt: APValue(APSInt(Sum, DestUnsigned)));
12955 }
12956
12957 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12958 }
12959
12960 case clang::X86::BI__builtin_ia32_pmaddubsw128:
12961 case clang::X86::BI__builtin_ia32_pmaddubsw256:
12962 case clang::X86::BI__builtin_ia32_pmaddubsw512:
12963 case clang::X86::BI__builtin_ia32_pmaddwd128:
12964 case clang::X86::BI__builtin_ia32_pmaddwd256:
12965 case clang::X86::BI__builtin_ia32_pmaddwd512: {
12966 APValue SourceLHS, SourceRHS;
12967 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12968 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12969 return false;
12970
12971 auto *DestTy = E->getType()->castAs<VectorType>();
12972 QualType DestEltTy = DestTy->getElementType();
12973 unsigned SourceLen = SourceLHS.getVectorLength();
12974 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12975 SmallVector<APValue, 4> ResultElements;
12976 ResultElements.reserve(N: SourceLen / 2);
12977
12978 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
12979 const APSInt &LoLHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12980 const APSInt &HiLHS = SourceLHS.getVectorElt(I: EltNum + 1).getInt();
12981 const APSInt &LoRHS = SourceRHS.getVectorElt(I: EltNum).getInt();
12982 const APSInt &HiRHS = SourceRHS.getVectorElt(I: EltNum + 1).getInt();
12983 unsigned BitWidth = 2 * LoLHS.getBitWidth();
12984
12985 switch (BuiltinOp) {
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 ResultElements.push_back(Elt: APValue(
12990 APSInt((LoLHS.zext(width: BitWidth) * LoRHS.sext(width: BitWidth))
12991 .sadd_sat(RHS: (HiLHS.zext(width: BitWidth) * HiRHS.sext(width: BitWidth))),
12992 DestUnsigned)));
12993 break;
12994 case clang::X86::BI__builtin_ia32_pmaddwd128:
12995 case clang::X86::BI__builtin_ia32_pmaddwd256:
12996 case clang::X86::BI__builtin_ia32_pmaddwd512:
12997 ResultElements.push_back(
12998 Elt: APValue(APSInt((LoLHS.sext(width: BitWidth) * LoRHS.sext(width: BitWidth)) +
12999 (HiLHS.sext(width: BitWidth) * HiRHS.sext(width: BitWidth)),
13000 DestUnsigned)));
13001 break;
13002 }
13003 }
13004
13005 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13006 }
13007
13008 case clang::X86::BI__builtin_ia32_bmacor16x16x16_v16hi:
13009 case clang::X86::BI__builtin_ia32_bmacor16x16x16_v32hi:
13010 case clang::X86::BI__builtin_ia32_bmacxor16x16x16_v16hi:
13011 case clang::X86::BI__builtin_ia32_bmacxor16x16x16_v32hi: {
13012 // Bit Matrix Multiply and Accumulate (AVX512BMM). Each 256-bit lane holds
13013 // a 16x16 bit matrix as 16 x i16 elements; element i is row i and bit j of
13014 // that element is entry [i][j]. The accumulator (third argument, src1 in
13015 // the AMD ISA) provides the initial value of each result bit, into which
13016 // the bit-matrix product of the first two arguments (src2 * src3) is
13017 // reduced with OR (vbmacor) or XOR (vbmacxor):
13018 // for i in 0..15, j in 0..15:
13019 // bit = C[16*i+j]
13020 // for k in 0..15: bit OP= A[16*i+k] & B[16*k+j]
13021 // dest[16*i+j] = bit
13022 APValue SourceA, SourceB, SourceC;
13023 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
13024 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
13025 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13026 return false;
13027
13028 bool IsXor = E->getBuiltinCallee() ==
13029 clang::X86::BI__builtin_ia32_bmacxor16x16x16_v16hi ||
13030 E->getBuiltinCallee() ==
13031 clang::X86::BI__builtin_ia32_bmacxor16x16x16_v32hi;
13032
13033 unsigned SourceLen = SourceA.getVectorLength();
13034 assert(SourceLen % 16 == 0 && "BMM operates on 256-bit lanes of 16 x i16");
13035 auto *DestTy = E->getType()->castAs<VectorType>();
13036 QualType DestEltTy = DestTy->getElementType();
13037 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13038
13039 SmallVector<APValue, 32> ResultElements(SourceLen);
13040 for (unsigned Lane = 0; Lane != SourceLen; Lane += 16) {
13041 for (unsigned I = 0; I != 16; ++I) {
13042 uint16_t A =
13043 (uint16_t)SourceA.getVectorElt(I: Lane + I).getInt().getZExtValue();
13044 uint16_t Dst =
13045 (uint16_t)SourceC.getVectorElt(I: Lane + I).getInt().getZExtValue();
13046 for (unsigned J = 0; J != 16; ++J) {
13047 // Seed the reduction with the accumulator bit, then fold in each
13048 // product term with the same operator (OR for vbmacor, XOR for
13049 // vbmacxor).
13050 unsigned Bit = (Dst >> J) & 1u;
13051 for (unsigned K = 0; K != 16; ++K) {
13052 uint16_t B = (uint16_t)SourceB.getVectorElt(I: Lane + K)
13053 .getInt()
13054 .getZExtValue();
13055 unsigned Product = ((A >> K) & 1u) & ((B >> J) & 1u);
13056 Bit = IsXor ? (Bit ^ Product) : (Bit | Product);
13057 }
13058 Dst = (Dst & ~(uint16_t(1) << J)) | (uint16_t(Bit) << J);
13059 }
13060 ResultElements[Lane + I] =
13061 APValue(APSInt(APInt(16, Dst), DestUnsigned));
13062 }
13063 }
13064 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13065 }
13066
13067 case clang::X86::BI__builtin_ia32_dbpsadbw128:
13068 case clang::X86::BI__builtin_ia32_dbpsadbw256:
13069 case clang::X86::BI__builtin_ia32_dbpsadbw512: {
13070 APValue SourceA, SourceB, SourceImm;
13071 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
13072 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
13073 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceImm))
13074 return false;
13075
13076 unsigned SourceLen = SourceA.getVectorLength();
13077 constexpr unsigned LaneSize = 16; // 128-bit lane = 16 bytes
13078 unsigned Imm = SourceImm.getInt().getZExtValue();
13079
13080 auto *DestTy = E->getType()->castAs<VectorType>();
13081 QualType DestEltTy = DestTy->getElementType();
13082 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13083 SmallVector<APValue, 32> ResultElements;
13084 ResultElements.reserve(N: SourceLen / 2);
13085
13086 // Phase 1: Shuffle SourceB using all four 2-bit fields of imm8.
13087 // Within each 128-bit lane, for group j (0..3), select a 4-byte block
13088 // from SourceB based on bits [2*j+1:2*j] of imm8.
13089 SmallVector<uint8_t, 64> Shuffled(SourceLen);
13090 for (unsigned I = 0; I < SourceLen; I += LaneSize) {
13091 for (unsigned J = 0; J < 4; ++J) {
13092 unsigned Part = (Imm >> (2 * J)) & 3;
13093 for (unsigned K = 0; K < 4; ++K) {
13094 Shuffled[I + 4 * J + K] = static_cast<uint8_t>(
13095 SourceB.getVectorElt(I: I + 4 * Part + K).getInt().getZExtValue());
13096 }
13097 }
13098 }
13099
13100 // Phase 2: Sliding SAD computation.
13101 // For every group of 4 output u16 values, compute absolute differences
13102 // using overlapping windows into SourceA and the shuffled array.
13103 unsigned Size = SourceLen / 2; // number of output u16 elements
13104 for (unsigned I = 0; I < Size; I += 4) {
13105 unsigned Sad[4] = {0, 0, 0, 0};
13106 for (unsigned J = 0; J < 4; ++J) {
13107 uint8_t A1 = static_cast<uint8_t>(
13108 SourceA.getVectorElt(I: 2 * I + J).getInt().getZExtValue());
13109 uint8_t A2 = static_cast<uint8_t>(
13110 SourceA.getVectorElt(I: 2 * I + J + 4).getInt().getZExtValue());
13111 uint8_t B0 = Shuffled[2 * I + J];
13112 uint8_t B1 = Shuffled[2 * I + J + 1];
13113 uint8_t B2 = Shuffled[2 * I + J + 2];
13114 uint8_t B3 = Shuffled[2 * I + J + 3];
13115 Sad[0] += (A1 > B0) ? (A1 - B0) : (B0 - A1);
13116 Sad[1] += (A1 > B1) ? (A1 - B1) : (B1 - A1);
13117 Sad[2] += (A2 > B2) ? (A2 - B2) : (B2 - A2);
13118 Sad[3] += (A2 > B3) ? (A2 - B3) : (B3 - A2);
13119 }
13120 for (unsigned R = 0; R < 4; ++R)
13121 ResultElements.push_back(
13122 Elt: APValue(APSInt(APInt(16, Sad[R]), DestUnsigned)));
13123 }
13124
13125 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13126 }
13127
13128 case clang::X86::BI__builtin_ia32_mpsadbw128:
13129 case clang::X86::BI__builtin_ia32_mpsadbw256: {
13130 APValue SourceA, SourceB;
13131 APSInt SourceImm;
13132 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: SourceA, Info) ||
13133 !EvaluateVector(E: E->getArg(Arg: 1), Result&: SourceB, Info) ||
13134 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: SourceImm, Info))
13135 return false;
13136 unsigned SourceLen = SourceA.getVectorLength();
13137 constexpr unsigned LaneSize = 16;
13138 assert((SourceLen == LaneSize || SourceLen == 2 * LaneSize) &&
13139 "MPSADBW operates on 128-bit or 256-bit vectors");
13140 unsigned NumLanes = SourceLen / LaneSize;
13141 unsigned Imm = SourceImm.getZExtValue();
13142
13143 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13144 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13145 SmallVector<APValue, 16> ResultElements;
13146 ResultElements.reserve(N: SourceLen / 2);
13147
13148 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
13149 unsigned Ctrl = (Imm >> (3 * Lane)) & 0x7;
13150 unsigned AOff = ((Ctrl >> 2) & 1) * 4;
13151 unsigned BOff = (Ctrl & 3) * 4;
13152 for (unsigned J = 0; J != 8; ++J) {
13153 uint16_t Sad = 0;
13154 for (unsigned K = 0; K != 4; ++K) {
13155 uint8_t A = static_cast<uint8_t>(
13156 SourceA.getVectorElt(I: Lane * LaneSize + AOff + J + K)
13157 .getInt()
13158 .getZExtValue());
13159 uint8_t B = static_cast<uint8_t>(
13160 SourceB.getVectorElt(I: Lane * LaneSize + BOff + K)
13161 .getInt()
13162 .getZExtValue());
13163 Sad += (A > B) ? (A - B) : (B - A);
13164 }
13165 ResultElements.push_back(Elt: APValue(APSInt(APInt(16, Sad), DestUnsigned)));
13166 }
13167 }
13168 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13169 }
13170
13171 case clang::X86::BI__builtin_ia32_pmulhuw128:
13172 case clang::X86::BI__builtin_ia32_pmulhuw256:
13173 case clang::X86::BI__builtin_ia32_pmulhuw512:
13174 return EvaluateBinOpExpr(llvm::APIntOps::mulhu);
13175
13176 case clang::X86::BI__builtin_ia32_pmulhw128:
13177 case clang::X86::BI__builtin_ia32_pmulhw256:
13178 case clang::X86::BI__builtin_ia32_pmulhw512:
13179 return EvaluateBinOpExpr(llvm::APIntOps::mulhs);
13180
13181 case clang::X86::BI__builtin_ia32_psllv2di:
13182 case clang::X86::BI__builtin_ia32_psllv4di:
13183 case clang::X86::BI__builtin_ia32_psllv4si:
13184 case clang::X86::BI__builtin_ia32_psllv8di:
13185 case clang::X86::BI__builtin_ia32_psllv8hi:
13186 case clang::X86::BI__builtin_ia32_psllv8si:
13187 case clang::X86::BI__builtin_ia32_psllv16hi:
13188 case clang::X86::BI__builtin_ia32_psllv16si:
13189 case clang::X86::BI__builtin_ia32_psllv32hi:
13190 case clang::X86::BI__builtin_ia32_psllwi128:
13191 case clang::X86::BI__builtin_ia32_pslldi128:
13192 case clang::X86::BI__builtin_ia32_psllqi128:
13193 case clang::X86::BI__builtin_ia32_psllwi256:
13194 case clang::X86::BI__builtin_ia32_pslldi256:
13195 case clang::X86::BI__builtin_ia32_psllqi256:
13196 case clang::X86::BI__builtin_ia32_psllwi512:
13197 case clang::X86::BI__builtin_ia32_pslldi512:
13198 case clang::X86::BI__builtin_ia32_psllqi512:
13199 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13200 if (RHS.uge(RHS: LHS.getBitWidth())) {
13201 return APInt::getZero(numBits: LHS.getBitWidth());
13202 }
13203 return LHS.shl(shiftAmt: RHS.getZExtValue());
13204 });
13205
13206 case clang::X86::BI__builtin_ia32_psrav4si:
13207 case clang::X86::BI__builtin_ia32_psrav8di:
13208 case clang::X86::BI__builtin_ia32_psrav8hi:
13209 case clang::X86::BI__builtin_ia32_psrav8si:
13210 case clang::X86::BI__builtin_ia32_psrav16hi:
13211 case clang::X86::BI__builtin_ia32_psrav16si:
13212 case clang::X86::BI__builtin_ia32_psrav32hi:
13213 case clang::X86::BI__builtin_ia32_psravq128:
13214 case clang::X86::BI__builtin_ia32_psravq256:
13215 case clang::X86::BI__builtin_ia32_psrawi128:
13216 case clang::X86::BI__builtin_ia32_psradi128:
13217 case clang::X86::BI__builtin_ia32_psraqi128:
13218 case clang::X86::BI__builtin_ia32_psrawi256:
13219 case clang::X86::BI__builtin_ia32_psradi256:
13220 case clang::X86::BI__builtin_ia32_psraqi256:
13221 case clang::X86::BI__builtin_ia32_psrawi512:
13222 case clang::X86::BI__builtin_ia32_psradi512:
13223 case clang::X86::BI__builtin_ia32_psraqi512:
13224 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13225 if (RHS.uge(RHS: LHS.getBitWidth())) {
13226 return LHS.ashr(ShiftAmt: LHS.getBitWidth() - 1);
13227 }
13228 return LHS.ashr(ShiftAmt: RHS.getZExtValue());
13229 });
13230
13231 case clang::X86::BI__builtin_ia32_psrlv2di:
13232 case clang::X86::BI__builtin_ia32_psrlv4di:
13233 case clang::X86::BI__builtin_ia32_psrlv4si:
13234 case clang::X86::BI__builtin_ia32_psrlv8di:
13235 case clang::X86::BI__builtin_ia32_psrlv8hi:
13236 case clang::X86::BI__builtin_ia32_psrlv8si:
13237 case clang::X86::BI__builtin_ia32_psrlv16hi:
13238 case clang::X86::BI__builtin_ia32_psrlv16si:
13239 case clang::X86::BI__builtin_ia32_psrlv32hi:
13240 case clang::X86::BI__builtin_ia32_psrlwi128:
13241 case clang::X86::BI__builtin_ia32_psrldi128:
13242 case clang::X86::BI__builtin_ia32_psrlqi128:
13243 case clang::X86::BI__builtin_ia32_psrlwi256:
13244 case clang::X86::BI__builtin_ia32_psrldi256:
13245 case clang::X86::BI__builtin_ia32_psrlqi256:
13246 case clang::X86::BI__builtin_ia32_psrlwi512:
13247 case clang::X86::BI__builtin_ia32_psrldi512:
13248 case clang::X86::BI__builtin_ia32_psrlqi512:
13249 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13250 if (RHS.uge(RHS: LHS.getBitWidth())) {
13251 return APInt::getZero(numBits: LHS.getBitWidth());
13252 }
13253 return LHS.lshr(shiftAmt: RHS.getZExtValue());
13254 });
13255 case X86::BI__builtin_ia32_packsswb128:
13256 case X86::BI__builtin_ia32_packsswb256:
13257 case X86::BI__builtin_ia32_packsswb512:
13258 case X86::BI__builtin_ia32_packssdw128:
13259 case X86::BI__builtin_ia32_packssdw256:
13260 case X86::BI__builtin_ia32_packssdw512:
13261 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
13262 return APSInt(Src).truncSSat(width: Src.getBitWidth() / 2);
13263 });
13264 case X86::BI__builtin_ia32_packusdw128:
13265 case X86::BI__builtin_ia32_packusdw256:
13266 case X86::BI__builtin_ia32_packusdw512:
13267 case X86::BI__builtin_ia32_packuswb128:
13268 case X86::BI__builtin_ia32_packuswb256:
13269 case X86::BI__builtin_ia32_packuswb512:
13270 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
13271 return APSInt(Src).truncSSatU(width: Src.getBitWidth() / 2);
13272 });
13273 case clang::X86::BI__builtin_ia32_selectss_128:
13274 return EvalSelectScalar(4);
13275 case clang::X86::BI__builtin_ia32_selectsd_128:
13276 return EvalSelectScalar(2);
13277 case clang::X86::BI__builtin_ia32_selectsh_128:
13278 case clang::X86::BI__builtin_ia32_selectsbf_128:
13279 return EvalSelectScalar(8);
13280 case clang::X86::BI__builtin_ia32_pmuldq128:
13281 case clang::X86::BI__builtin_ia32_pmuldq256:
13282 case clang::X86::BI__builtin_ia32_pmuldq512:
13283 case clang::X86::BI__builtin_ia32_pmuludq128:
13284 case clang::X86::BI__builtin_ia32_pmuludq256:
13285 case clang::X86::BI__builtin_ia32_pmuludq512: {
13286 APValue SourceLHS, SourceRHS;
13287 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13288 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13289 return false;
13290
13291 unsigned SourceLen = SourceLHS.getVectorLength();
13292 SmallVector<APValue, 4> ResultElements;
13293 ResultElements.reserve(N: SourceLen / 2);
13294
13295 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
13296 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13297 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13298
13299 switch (BuiltinOp) {
13300 case clang::X86::BI__builtin_ia32_pmuludq128:
13301 case clang::X86::BI__builtin_ia32_pmuludq256:
13302 case clang::X86::BI__builtin_ia32_pmuludq512:
13303 ResultElements.push_back(
13304 Elt: APValue(APSInt(llvm::APIntOps::muluExtended(C1: LHS, C2: RHS), true)));
13305 break;
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 ResultElements.push_back(
13310 Elt: APValue(APSInt(llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS), false)));
13311 break;
13312 }
13313 }
13314
13315 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13316 }
13317
13318 case X86::BI__builtin_ia32_vpmadd52luq128:
13319 case X86::BI__builtin_ia32_vpmadd52luq256:
13320 case X86::BI__builtin_ia32_vpmadd52luq512: {
13321 APValue A, B, C;
13322 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
13323 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
13324 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
13325 return false;
13326
13327 unsigned ALen = A.getVectorLength();
13328 SmallVector<APValue, 4> ResultElements;
13329 ResultElements.reserve(N: ALen);
13330
13331 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
13332 APInt AElt = A.getVectorElt(I: EltNum).getInt();
13333 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13334 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13335 APSInt ResElt(AElt + (BElt * CElt).zext(width: 64), false);
13336 ResultElements.push_back(Elt: APValue(ResElt));
13337 }
13338
13339 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13340 }
13341 case X86::BI__builtin_ia32_vpmadd52huq128:
13342 case X86::BI__builtin_ia32_vpmadd52huq256:
13343 case X86::BI__builtin_ia32_vpmadd52huq512: {
13344 APValue A, B, C;
13345 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
13346 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
13347 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
13348 return false;
13349
13350 unsigned ALen = A.getVectorLength();
13351 SmallVector<APValue, 4> ResultElements;
13352 ResultElements.reserve(N: ALen);
13353
13354 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
13355 APInt AElt = A.getVectorElt(I: EltNum).getInt();
13356 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13357 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13358 APSInt ResElt(AElt + llvm::APIntOps::mulhu(C1: BElt, C2: CElt).zext(width: 64), false);
13359 ResultElements.push_back(Elt: APValue(ResElt));
13360 }
13361
13362 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13363 }
13364
13365 case clang::X86::BI__builtin_ia32_vprotbi:
13366 case clang::X86::BI__builtin_ia32_vprotdi:
13367 case clang::X86::BI__builtin_ia32_vprotqi:
13368 case clang::X86::BI__builtin_ia32_vprotwi:
13369 case clang::X86::BI__builtin_ia32_prold128:
13370 case clang::X86::BI__builtin_ia32_prold256:
13371 case clang::X86::BI__builtin_ia32_prold512:
13372 case clang::X86::BI__builtin_ia32_prolq128:
13373 case clang::X86::BI__builtin_ia32_prolq256:
13374 case clang::X86::BI__builtin_ia32_prolq512:
13375 return EvaluateBinOpExpr(
13376 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotl(rotateAmt: RHS); });
13377
13378 case clang::X86::BI__builtin_ia32_prord128:
13379 case clang::X86::BI__builtin_ia32_prord256:
13380 case clang::X86::BI__builtin_ia32_prord512:
13381 case clang::X86::BI__builtin_ia32_prorq128:
13382 case clang::X86::BI__builtin_ia32_prorq256:
13383 case clang::X86::BI__builtin_ia32_prorq512:
13384 return EvaluateBinOpExpr(
13385 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotr(rotateAmt: RHS); });
13386
13387 case Builtin::BI__builtin_elementwise_max:
13388 case Builtin::BI__builtin_elementwise_min: {
13389 APValue SourceLHS, SourceRHS;
13390 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13391 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13392 return false;
13393
13394 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13395
13396 if (!DestEltTy->isIntegerType())
13397 return false;
13398
13399 unsigned SourceLen = SourceLHS.getVectorLength();
13400 SmallVector<APValue, 4> ResultElements;
13401 ResultElements.reserve(N: SourceLen);
13402
13403 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13404 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13405 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13406 switch (BuiltinOp) {
13407 case Builtin::BI__builtin_elementwise_max:
13408 ResultElements.push_back(
13409 Elt: APValue(APSInt(std::max(a: LHS, b: RHS),
13410 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13411 break;
13412 case Builtin::BI__builtin_elementwise_min:
13413 ResultElements.push_back(
13414 Elt: APValue(APSInt(std::min(a: LHS, b: RHS),
13415 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13416 break;
13417 }
13418 }
13419
13420 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13421 }
13422 case X86::BI__builtin_ia32_vpshldd128:
13423 case X86::BI__builtin_ia32_vpshldd256:
13424 case X86::BI__builtin_ia32_vpshldd512:
13425 case X86::BI__builtin_ia32_vpshldq128:
13426 case X86::BI__builtin_ia32_vpshldq256:
13427 case X86::BI__builtin_ia32_vpshldq512:
13428 case X86::BI__builtin_ia32_vpshldw128:
13429 case X86::BI__builtin_ia32_vpshldw256:
13430 case X86::BI__builtin_ia32_vpshldw512: {
13431 APValue SourceHi, SourceLo, SourceAmt;
13432 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
13433 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
13434 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13435 return false;
13436
13437 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13438 unsigned SourceLen = SourceHi.getVectorLength();
13439 SmallVector<APValue, 32> ResultElements;
13440 ResultElements.reserve(N: SourceLen);
13441
13442 APInt Amt = SourceAmt.getInt();
13443 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13444 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13445 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13446 APInt R = llvm::APIntOps::fshl(Hi, Lo, Shift: Amt);
13447 ResultElements.push_back(
13448 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13449 }
13450
13451 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13452 }
13453 case X86::BI__builtin_ia32_vpshrdd128:
13454 case X86::BI__builtin_ia32_vpshrdd256:
13455 case X86::BI__builtin_ia32_vpshrdd512:
13456 case X86::BI__builtin_ia32_vpshrdq128:
13457 case X86::BI__builtin_ia32_vpshrdq256:
13458 case X86::BI__builtin_ia32_vpshrdq512:
13459 case X86::BI__builtin_ia32_vpshrdw128:
13460 case X86::BI__builtin_ia32_vpshrdw256:
13461 case X86::BI__builtin_ia32_vpshrdw512: {
13462 // NOTE: Reversed Hi/Lo operands.
13463 APValue SourceHi, SourceLo, SourceAmt;
13464 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLo) ||
13465 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceHi) ||
13466 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13467 return false;
13468
13469 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13470 unsigned SourceLen = SourceHi.getVectorLength();
13471 SmallVector<APValue, 32> ResultElements;
13472 ResultElements.reserve(N: SourceLen);
13473
13474 APInt Amt = SourceAmt.getInt();
13475 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13476 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13477 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13478 APInt R = llvm::APIntOps::fshr(Hi, Lo, Shift: Amt);
13479 ResultElements.push_back(
13480 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13481 }
13482
13483 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13484 }
13485 case X86::BI__builtin_ia32_compressdf128_mask:
13486 case X86::BI__builtin_ia32_compressdf256_mask:
13487 case X86::BI__builtin_ia32_compressdf512_mask:
13488 case X86::BI__builtin_ia32_compressdi128_mask:
13489 case X86::BI__builtin_ia32_compressdi256_mask:
13490 case X86::BI__builtin_ia32_compressdi512_mask:
13491 case X86::BI__builtin_ia32_compresshi128_mask:
13492 case X86::BI__builtin_ia32_compresshi256_mask:
13493 case X86::BI__builtin_ia32_compresshi512_mask:
13494 case X86::BI__builtin_ia32_compressqi128_mask:
13495 case X86::BI__builtin_ia32_compressqi256_mask:
13496 case X86::BI__builtin_ia32_compressqi512_mask:
13497 case X86::BI__builtin_ia32_compresssf128_mask:
13498 case X86::BI__builtin_ia32_compresssf256_mask:
13499 case X86::BI__builtin_ia32_compresssf512_mask:
13500 case X86::BI__builtin_ia32_compresssi128_mask:
13501 case X86::BI__builtin_ia32_compresssi256_mask:
13502 case X86::BI__builtin_ia32_compresssi512_mask: {
13503 APValue Source, Passthru;
13504 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13505 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13506 return false;
13507 APSInt Mask;
13508 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13509 return false;
13510
13511 unsigned NumElts = Source.getVectorLength();
13512 SmallVector<APValue, 64> ResultElements;
13513 ResultElements.reserve(N: NumElts);
13514
13515 for (unsigned I = 0; I != NumElts; ++I) {
13516 if (Mask[I])
13517 ResultElements.push_back(Elt: Source.getVectorElt(I));
13518 }
13519 for (unsigned I = ResultElements.size(); I != NumElts; ++I) {
13520 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13521 }
13522
13523 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13524 }
13525 case X86::BI__builtin_ia32_expanddf128_mask:
13526 case X86::BI__builtin_ia32_expanddf256_mask:
13527 case X86::BI__builtin_ia32_expanddf512_mask:
13528 case X86::BI__builtin_ia32_expanddi128_mask:
13529 case X86::BI__builtin_ia32_expanddi256_mask:
13530 case X86::BI__builtin_ia32_expanddi512_mask:
13531 case X86::BI__builtin_ia32_expandhi128_mask:
13532 case X86::BI__builtin_ia32_expandhi256_mask:
13533 case X86::BI__builtin_ia32_expandhi512_mask:
13534 case X86::BI__builtin_ia32_expandqi128_mask:
13535 case X86::BI__builtin_ia32_expandqi256_mask:
13536 case X86::BI__builtin_ia32_expandqi512_mask:
13537 case X86::BI__builtin_ia32_expandsf128_mask:
13538 case X86::BI__builtin_ia32_expandsf256_mask:
13539 case X86::BI__builtin_ia32_expandsf512_mask:
13540 case X86::BI__builtin_ia32_expandsi128_mask:
13541 case X86::BI__builtin_ia32_expandsi256_mask:
13542 case X86::BI__builtin_ia32_expandsi512_mask: {
13543 APValue Source, Passthru;
13544 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13545 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13546 return false;
13547 APSInt Mask;
13548 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13549 return false;
13550
13551 unsigned NumElts = Source.getVectorLength();
13552 SmallVector<APValue, 64> ResultElements;
13553 ResultElements.reserve(N: NumElts);
13554
13555 unsigned SourceIdx = 0;
13556 for (unsigned I = 0; I != NumElts; ++I) {
13557 if (Mask[I])
13558 ResultElements.push_back(Elt: Source.getVectorElt(I: SourceIdx++));
13559 else
13560 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13561 }
13562 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13563 }
13564 case X86::BI__builtin_ia32_vpconflictsi_128:
13565 case X86::BI__builtin_ia32_vpconflictsi_256:
13566 case X86::BI__builtin_ia32_vpconflictsi_512:
13567 case X86::BI__builtin_ia32_vpconflictdi_128:
13568 case X86::BI__builtin_ia32_vpconflictdi_256:
13569 case X86::BI__builtin_ia32_vpconflictdi_512: {
13570 APValue Source;
13571
13572 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
13573 return false;
13574
13575 unsigned SourceLen = Source.getVectorLength();
13576 SmallVector<APValue, 32> ResultElements;
13577 ResultElements.reserve(N: SourceLen);
13578
13579 const auto *VecT = E->getType()->castAs<VectorType>();
13580 bool DestUnsigned =
13581 VecT->getElementType()->isUnsignedIntegerOrEnumerationType();
13582
13583 for (unsigned I = 0; I != SourceLen; ++I) {
13584 const APValue &EltI = Source.getVectorElt(I);
13585
13586 APInt ConflictMask(EltI.getInt().getBitWidth(), 0);
13587 for (unsigned J = 0; J != I; ++J) {
13588 const APValue &EltJ = Source.getVectorElt(I: J);
13589 ConflictMask.setBitVal(BitPosition: J, BitValue: EltI.getInt() == EltJ.getInt());
13590 }
13591 ResultElements.push_back(Elt: APValue(APSInt(ConflictMask, DestUnsigned)));
13592 }
13593 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13594 }
13595 case X86::BI__builtin_ia32_blendpd:
13596 case X86::BI__builtin_ia32_blendpd256:
13597 case X86::BI__builtin_ia32_blendps:
13598 case X86::BI__builtin_ia32_blendps256:
13599 case X86::BI__builtin_ia32_pblendw128:
13600 case X86::BI__builtin_ia32_pblendw256:
13601 case X86::BI__builtin_ia32_pblendd128:
13602 case X86::BI__builtin_ia32_pblendd256: {
13603 APValue SourceF, SourceT, SourceC;
13604 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13605 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13606 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13607 return false;
13608
13609 const APInt &C = SourceC.getInt();
13610 unsigned SourceLen = SourceF.getVectorLength();
13611 SmallVector<APValue, 32> ResultElements;
13612 ResultElements.reserve(N: SourceLen);
13613 for (unsigned EltNum = 0; EltNum != SourceLen; ++EltNum) {
13614 const APValue &F = SourceF.getVectorElt(I: EltNum);
13615 const APValue &T = SourceT.getVectorElt(I: EltNum);
13616 ResultElements.push_back(Elt: C[EltNum % 8] ? T : F);
13617 }
13618
13619 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13620 }
13621
13622 case X86::BI__builtin_ia32_psignb128:
13623 case X86::BI__builtin_ia32_psignb256:
13624 case X86::BI__builtin_ia32_psignw128:
13625 case X86::BI__builtin_ia32_psignw256:
13626 case X86::BI__builtin_ia32_psignd128:
13627 case X86::BI__builtin_ia32_psignd256:
13628 return EvaluateBinOpExpr([](const APInt &AElem, const APInt &BElem) {
13629 if (BElem.isZero())
13630 return APInt::getZero(numBits: AElem.getBitWidth());
13631 if (BElem.isNegative())
13632 return -AElem;
13633 return AElem;
13634 });
13635
13636 case X86::BI__builtin_ia32_blendvpd:
13637 case X86::BI__builtin_ia32_blendvpd256:
13638 case X86::BI__builtin_ia32_blendvps:
13639 case X86::BI__builtin_ia32_blendvps256:
13640 case X86::BI__builtin_ia32_pblendvb128:
13641 case X86::BI__builtin_ia32_pblendvb256: {
13642 // SSE blendv by mask signbit: "Result = C[] < 0 ? T[] : F[]".
13643 APValue SourceF, SourceT, SourceC;
13644 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13645 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13646 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13647 return false;
13648
13649 unsigned SourceLen = SourceF.getVectorLength();
13650 SmallVector<APValue, 32> ResultElements;
13651 ResultElements.reserve(N: SourceLen);
13652
13653 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13654 const APValue &F = SourceF.getVectorElt(I: EltNum);
13655 const APValue &T = SourceT.getVectorElt(I: EltNum);
13656 const APValue &C = SourceC.getVectorElt(I: EltNum);
13657 APInt M = C.isInt() ? (APInt)C.getInt() : C.getFloat().bitcastToAPInt();
13658 ResultElements.push_back(Elt: M.isNegative() ? T : F);
13659 }
13660
13661 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13662 }
13663 case X86::BI__builtin_ia32_selectb_128:
13664 case X86::BI__builtin_ia32_selectb_256:
13665 case X86::BI__builtin_ia32_selectb_512:
13666 case X86::BI__builtin_ia32_selectw_128:
13667 case X86::BI__builtin_ia32_selectw_256:
13668 case X86::BI__builtin_ia32_selectw_512:
13669 case X86::BI__builtin_ia32_selectd_128:
13670 case X86::BI__builtin_ia32_selectd_256:
13671 case X86::BI__builtin_ia32_selectd_512:
13672 case X86::BI__builtin_ia32_selectq_128:
13673 case X86::BI__builtin_ia32_selectq_256:
13674 case X86::BI__builtin_ia32_selectq_512:
13675 case X86::BI__builtin_ia32_selectph_128:
13676 case X86::BI__builtin_ia32_selectph_256:
13677 case X86::BI__builtin_ia32_selectph_512:
13678 case X86::BI__builtin_ia32_selectpbf_128:
13679 case X86::BI__builtin_ia32_selectpbf_256:
13680 case X86::BI__builtin_ia32_selectpbf_512:
13681 case X86::BI__builtin_ia32_selectps_128:
13682 case X86::BI__builtin_ia32_selectps_256:
13683 case X86::BI__builtin_ia32_selectps_512:
13684 case X86::BI__builtin_ia32_selectpd_128:
13685 case X86::BI__builtin_ia32_selectpd_256:
13686 case X86::BI__builtin_ia32_selectpd_512: {
13687 // AVX512 predicated move: "Result = Mask[] ? LHS[] : RHS[]".
13688 APValue SourceMask, SourceLHS, SourceRHS;
13689 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceMask) ||
13690 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLHS) ||
13691 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceRHS))
13692 return false;
13693
13694 APSInt Mask = SourceMask.getInt();
13695 unsigned SourceLen = SourceLHS.getVectorLength();
13696 SmallVector<APValue, 4> ResultElements;
13697 ResultElements.reserve(N: SourceLen);
13698
13699 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13700 const APValue &LHS = SourceLHS.getVectorElt(I: EltNum);
13701 const APValue &RHS = SourceRHS.getVectorElt(I: EltNum);
13702 ResultElements.push_back(Elt: Mask[EltNum] ? LHS : RHS);
13703 }
13704
13705 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13706 }
13707
13708 case X86::BI__builtin_ia32_cvtsd2ss: {
13709 APValue VecA, VecB;
13710 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13711 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB))
13712 return false;
13713
13714 SmallVector<APValue, 4> Elements;
13715
13716 APValue ResultVal;
13717 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13718 Result&: ResultVal))
13719 return false;
13720
13721 Elements.push_back(Elt: ResultVal);
13722
13723 unsigned NumEltsA = VecA.getVectorLength();
13724 for (unsigned I = 1; I < NumEltsA; ++I) {
13725 Elements.push_back(Elt: VecA.getVectorElt(I));
13726 }
13727
13728 return Success(V: Elements, E);
13729 }
13730 case X86::BI__builtin_ia32_cvtsd2ss_round_mask: {
13731 APValue VecA, VecB, VecSrc, MaskValue;
13732
13733 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13734 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
13735 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
13736 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: MaskValue))
13737 return false;
13738
13739 unsigned Mask = MaskValue.getInt().getZExtValue();
13740 SmallVector<APValue, 4> Elements;
13741
13742 if (Mask & 1) {
13743 APValue ResultVal;
13744 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13745 Result&: ResultVal))
13746 return false;
13747 Elements.push_back(Elt: ResultVal);
13748 } else {
13749 Elements.push_back(Elt: VecSrc.getVectorElt(I: 0));
13750 }
13751
13752 unsigned NumEltsA = VecA.getVectorLength();
13753 for (unsigned I = 1; I < NumEltsA; ++I) {
13754 Elements.push_back(Elt: VecA.getVectorElt(I));
13755 }
13756
13757 return Success(V: Elements, E);
13758 }
13759 case X86::BI__builtin_ia32_cvtpd2ps:
13760 case X86::BI__builtin_ia32_cvtpd2ps256:
13761 case X86::BI__builtin_ia32_cvtpd2ps_mask:
13762 case X86::BI__builtin_ia32_cvtpd2ps512_mask: {
13763
13764 const auto BuiltinID = BuiltinOp;
13765 bool IsMasked = (BuiltinID == X86::BI__builtin_ia32_cvtpd2ps_mask ||
13766 BuiltinID == X86::BI__builtin_ia32_cvtpd2ps512_mask);
13767
13768 APValue InputValue;
13769 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: InputValue))
13770 return false;
13771
13772 APValue MergeValue;
13773 unsigned Mask = 0xFFFFFFFF;
13774 bool NeedsMerge = false;
13775 if (IsMasked) {
13776 APValue MaskValue;
13777 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MaskValue))
13778 return false;
13779 Mask = MaskValue.getInt().getZExtValue();
13780 auto NumEltsResult = E->getType()->getAs<VectorType>()->getNumElements();
13781 for (unsigned I = 0; I < NumEltsResult; ++I) {
13782 if (!((Mask >> I) & 1)) {
13783 NeedsMerge = true;
13784 break;
13785 }
13786 }
13787 if (NeedsMerge) {
13788 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: MergeValue))
13789 return false;
13790 }
13791 }
13792
13793 unsigned NumEltsResult =
13794 E->getType()->getAs<VectorType>()->getNumElements();
13795 unsigned NumEltsInput = InputValue.getVectorLength();
13796 SmallVector<APValue, 8> Elements;
13797 for (unsigned I = 0; I < NumEltsResult; ++I) {
13798 if (IsMasked && !((Mask >> I) & 1)) {
13799 if (!NeedsMerge) {
13800 return false;
13801 }
13802 Elements.push_back(Elt: MergeValue.getVectorElt(I));
13803 continue;
13804 }
13805
13806 if (I >= NumEltsInput) {
13807 Elements.push_back(Elt: APValue(APFloat::getZero(Sem: APFloat::IEEEsingle())));
13808 continue;
13809 }
13810
13811 APValue ResultVal;
13812 if (!ConvertDoubleToFloatStrict(
13813 Info, E, OrigVal: InputValue.getVectorElt(I).getFloat(), Result&: ResultVal))
13814 return false;
13815
13816 Elements.push_back(Elt: ResultVal);
13817 }
13818 return Success(V: Elements, E);
13819 }
13820
13821 case X86::BI__builtin_ia32_shufps:
13822 case X86::BI__builtin_ia32_shufps256:
13823 case X86::BI__builtin_ia32_shufps512: {
13824 APValue R;
13825 if (!evalShuffleGeneric(
13826 Info, Call: E, Out&: R,
13827 GetSourceIndex: [](unsigned DstIdx,
13828 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13829 constexpr unsigned LaneBits = 128u;
13830 unsigned NumElemPerLane = LaneBits / 32;
13831 unsigned NumSelectableElems = NumElemPerLane / 2;
13832 unsigned BitsPerElem = 2;
13833 unsigned IndexMask = (1u << BitsPerElem) - 1;
13834 unsigned MaskBits = 8;
13835 unsigned Lane = DstIdx / NumElemPerLane;
13836 unsigned ElemInLane = DstIdx % NumElemPerLane;
13837 unsigned LaneOffset = Lane * NumElemPerLane;
13838 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
13839 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
13840 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
13841 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
13842 }))
13843 return false;
13844 return Success(V: R, E);
13845 }
13846 case X86::BI__builtin_ia32_shufpd:
13847 case X86::BI__builtin_ia32_shufpd256:
13848 case X86::BI__builtin_ia32_shufpd512: {
13849 APValue R;
13850 if (!evalShuffleGeneric(
13851 Info, Call: E, Out&: R,
13852 GetSourceIndex: [](unsigned DstIdx,
13853 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13854 constexpr unsigned LaneBits = 128u;
13855 unsigned NumElemPerLane = LaneBits / 64;
13856 unsigned NumSelectableElems = NumElemPerLane / 2;
13857 unsigned BitsPerElem = 1;
13858 unsigned IndexMask = (1u << BitsPerElem) - 1;
13859 unsigned MaskBits = 8;
13860 unsigned Lane = DstIdx / NumElemPerLane;
13861 unsigned ElemInLane = DstIdx % NumElemPerLane;
13862 unsigned LaneOffset = Lane * NumElemPerLane;
13863 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
13864 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
13865 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
13866 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
13867 }))
13868 return false;
13869 return Success(V: R, E);
13870 }
13871 case X86::BI__builtin_ia32_insertps128: {
13872 APValue R;
13873 if (!evalShuffleGeneric(
13874 Info, Call: E, Out&: R,
13875 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13876 // Bits [3:0]: zero mask - if bit is set, zero this element
13877 if ((Mask & (1 << DstIdx)) != 0) {
13878 return {0, -1};
13879 }
13880 // Bits [7:6]: select element from source vector Y (0-3)
13881 // Bits [5:4]: select destination position (0-3)
13882 unsigned SrcElem = (Mask >> 6) & 0x3;
13883 unsigned DstElem = (Mask >> 4) & 0x3;
13884 if (DstIdx == DstElem) {
13885 // Insert element from source vector (B) at this position
13886 return {1, static_cast<int>(SrcElem)};
13887 } else {
13888 // Copy from destination vector (A)
13889 return {0, static_cast<int>(DstIdx)};
13890 }
13891 }))
13892 return false;
13893 return Success(V: R, E);
13894 }
13895 case X86::BI__builtin_ia32_pshufb128:
13896 case X86::BI__builtin_ia32_pshufb256:
13897 case X86::BI__builtin_ia32_pshufb512: {
13898 APValue R;
13899 if (!evalShuffleGeneric(
13900 Info, Call: E, Out&: R,
13901 GetSourceIndex: [](unsigned DstIdx,
13902 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13903 uint8_t Ctlb = static_cast<uint8_t>(ShuffleMask);
13904 if (Ctlb & 0x80)
13905 return std::make_pair(x: 0, y: -1);
13906
13907 unsigned LaneBase = (DstIdx / 16) * 16;
13908 unsigned SrcOffset = Ctlb & 0x0F;
13909 unsigned SrcIdx = LaneBase + SrcOffset;
13910 return std::make_pair(x: 0, y: static_cast<int>(SrcIdx));
13911 }))
13912 return false;
13913 return Success(V: R, E);
13914 }
13915
13916 case X86::BI__builtin_ia32_pshuflw:
13917 case X86::BI__builtin_ia32_pshuflw256:
13918 case X86::BI__builtin_ia32_pshuflw512: {
13919 APValue R;
13920 if (!evalShuffleGeneric(
13921 Info, Call: E, Out&: R,
13922 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13923 constexpr unsigned LaneBits = 128u;
13924 constexpr unsigned ElemBits = 16u;
13925 constexpr unsigned LaneElts = LaneBits / ElemBits;
13926 constexpr unsigned HalfSize = 4;
13927 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13928 unsigned LaneIdx = DstIdx % LaneElts;
13929 if (LaneIdx < HalfSize) {
13930 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
13931 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
13932 }
13933 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
13934 }))
13935 return false;
13936 return Success(V: R, E);
13937 }
13938
13939 case X86::BI__builtin_ia32_pshufhw:
13940 case X86::BI__builtin_ia32_pshufhw256:
13941 case X86::BI__builtin_ia32_pshufhw512: {
13942 APValue R;
13943 if (!evalShuffleGeneric(
13944 Info, Call: E, Out&: R,
13945 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13946 constexpr unsigned LaneBits = 128u;
13947 constexpr unsigned ElemBits = 16u;
13948 constexpr unsigned LaneElts = LaneBits / ElemBits;
13949 constexpr unsigned HalfSize = 4;
13950 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13951 unsigned LaneIdx = DstIdx % LaneElts;
13952 if (LaneIdx >= HalfSize) {
13953 unsigned Rel = LaneIdx - HalfSize;
13954 unsigned Sel = (Mask >> (2 * Rel)) & 0x3;
13955 return std::make_pair(
13956 x: 0, y: static_cast<int>(LaneBase + HalfSize + Sel));
13957 }
13958 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
13959 }))
13960 return false;
13961 return Success(V: R, E);
13962 }
13963
13964 case X86::BI__builtin_ia32_pshufd:
13965 case X86::BI__builtin_ia32_pshufd256:
13966 case X86::BI__builtin_ia32_pshufd512:
13967 case X86::BI__builtin_ia32_vpermilps:
13968 case X86::BI__builtin_ia32_vpermilps256:
13969 case X86::BI__builtin_ia32_vpermilps512: {
13970 APValue R;
13971 if (!evalShuffleGeneric(
13972 Info, Call: E, Out&: R,
13973 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13974 constexpr unsigned LaneBits = 128u;
13975 constexpr unsigned ElemBits = 32u;
13976 constexpr unsigned LaneElts = LaneBits / ElemBits;
13977 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13978 unsigned LaneIdx = DstIdx % LaneElts;
13979 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
13980 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
13981 }))
13982 return false;
13983 return Success(V: R, E);
13984 }
13985
13986 case X86::BI__builtin_ia32_vpermilvarpd:
13987 case X86::BI__builtin_ia32_vpermilvarpd256:
13988 case X86::BI__builtin_ia32_vpermilvarpd512: {
13989 APValue R;
13990 if (!evalShuffleGeneric(
13991 Info, Call: E, Out&: R,
13992 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13993 unsigned NumElemPerLane = 2;
13994 unsigned Lane = DstIdx / NumElemPerLane;
13995 unsigned Offset = Mask & 0b10 ? 1 : 0;
13996 return std::make_pair(
13997 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
13998 }))
13999 return false;
14000 return Success(V: R, E);
14001 }
14002
14003 case X86::BI__builtin_ia32_vpermilpd:
14004 case X86::BI__builtin_ia32_vpermilpd256:
14005 case X86::BI__builtin_ia32_vpermilpd512: {
14006 APValue R;
14007 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
14008 unsigned NumElemPerLane = 2;
14009 unsigned BitsPerElem = 1;
14010 unsigned MaskBits = 8;
14011 unsigned IndexMask = 0x1;
14012 unsigned Lane = DstIdx / NumElemPerLane;
14013 unsigned LaneOffset = Lane * NumElemPerLane;
14014 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
14015 unsigned Index = (Control >> BitIndex) & IndexMask;
14016 return std::make_pair(x: 0, y: static_cast<int>(LaneOffset + Index));
14017 }))
14018 return false;
14019 return Success(V: R, E);
14020 }
14021
14022 case X86::BI__builtin_ia32_permdf256:
14023 case X86::BI__builtin_ia32_permdi256: {
14024 APValue R;
14025 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
14026 // permute4x64 operates on 4 64-bit elements
14027 // For element i (0-3), extract bits [2*i+1:2*i] from Control
14028 unsigned Index = (Control >> (2 * DstIdx)) & 0x3;
14029 return std::make_pair(x: 0, y: static_cast<int>(Index));
14030 }))
14031 return false;
14032 return Success(V: R, E);
14033 }
14034
14035 case X86::BI__builtin_ia32_vpermilvarps:
14036 case X86::BI__builtin_ia32_vpermilvarps256:
14037 case X86::BI__builtin_ia32_vpermilvarps512: {
14038 APValue R;
14039 if (!evalShuffleGeneric(
14040 Info, Call: E, Out&: R,
14041 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14042 unsigned NumElemPerLane = 4;
14043 unsigned Lane = DstIdx / NumElemPerLane;
14044 unsigned Offset = Mask & 0b11;
14045 return std::make_pair(
14046 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
14047 }))
14048 return false;
14049 return Success(V: R, E);
14050 }
14051
14052 case X86::BI__builtin_ia32_vpmultishiftqb128:
14053 case X86::BI__builtin_ia32_vpmultishiftqb256:
14054 case X86::BI__builtin_ia32_vpmultishiftqb512: {
14055 assert(E->getNumArgs() == 2);
14056
14057 APValue A, B;
14058 if (!Evaluate(Result&: A, Info, E: E->getArg(Arg: 0)) || !Evaluate(Result&: B, Info, E: E->getArg(Arg: 1)))
14059 return false;
14060
14061 assert(A.getVectorLength() == B.getVectorLength());
14062 unsigned NumBytesInQWord = 8;
14063 unsigned NumBitsInByte = 8;
14064 unsigned NumBytes = A.getVectorLength();
14065 unsigned NumQWords = NumBytes / NumBytesInQWord;
14066 SmallVector<APValue, 64> Result;
14067 Result.reserve(N: NumBytes);
14068
14069 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
14070 APInt BQWord(64, 0);
14071 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14072 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
14073 uint64_t Byte = B.getVectorElt(I: Idx).getInt().getZExtValue();
14074 BQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
14075 }
14076
14077 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14078 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
14079 uint64_t Ctrl = A.getVectorElt(I: Idx).getInt().getZExtValue() & 0x3F;
14080
14081 APInt Byte(8, 0);
14082 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
14083 Byte.setBitVal(BitPosition: BitIdx, BitValue: BQWord[(Ctrl + BitIdx) & 0x3F]);
14084 }
14085 Result.push_back(Elt: APValue(APSInt(Byte, /*isUnsigned*/ true)));
14086 }
14087 }
14088 return Success(V: APValue(Result.data(), Result.size()), E);
14089 }
14090
14091 case X86::BI__builtin_ia32_phminposuw128: {
14092 APValue Source;
14093 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
14094 return false;
14095 unsigned SourceLen = Source.getVectorLength();
14096 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14097 QualType ElemQT = VT->getElementType();
14098 unsigned ElemBitWidth = Info.Ctx.getTypeSize(T: ElemQT);
14099
14100 APInt MinIndex(ElemBitWidth, 0);
14101 APInt MinVal = Source.getVectorElt(I: 0).getInt();
14102 for (unsigned I = 1; I != SourceLen; ++I) {
14103 APInt Val = Source.getVectorElt(I).getInt();
14104 if (MinVal.ugt(RHS: Val)) {
14105 MinVal = Val;
14106 MinIndex = I;
14107 }
14108 }
14109
14110 bool ResultUnsigned = E->getCallReturnType(Ctx: Info.Ctx)
14111 ->castAs<VectorType>()
14112 ->getElementType()
14113 ->isUnsignedIntegerOrEnumerationType();
14114
14115 SmallVector<APValue, 8> Result;
14116 Result.reserve(N: SourceLen);
14117 Result.emplace_back(Args: APSInt(MinVal, ResultUnsigned));
14118 Result.emplace_back(Args: APSInt(MinIndex, ResultUnsigned));
14119 for (unsigned I = 0; I != SourceLen - 2; ++I) {
14120 Result.emplace_back(Args: APSInt(APInt(ElemBitWidth, 0), ResultUnsigned));
14121 }
14122 return Success(V: APValue(Result.data(), Result.size()), E);
14123 }
14124
14125 case X86::BI__builtin_ia32_psraq128:
14126 case X86::BI__builtin_ia32_psraq256:
14127 case X86::BI__builtin_ia32_psraq512:
14128 case X86::BI__builtin_ia32_psrad128:
14129 case X86::BI__builtin_ia32_psrad256:
14130 case X86::BI__builtin_ia32_psrad512:
14131 case X86::BI__builtin_ia32_psraw128:
14132 case X86::BI__builtin_ia32_psraw256:
14133 case X86::BI__builtin_ia32_psraw512: {
14134 APValue R;
14135 if (!evalShiftWithCount(
14136 Info, Call: E, Out&: R,
14137 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.ashr(ShiftAmt: Count); },
14138 OverflowOp: [](const APInt &Elt, unsigned Width) {
14139 return Elt.ashr(ShiftAmt: Width - 1);
14140 }))
14141 return false;
14142 return Success(V: R, E);
14143 }
14144
14145 case X86::BI__builtin_ia32_psllq128:
14146 case X86::BI__builtin_ia32_psllq256:
14147 case X86::BI__builtin_ia32_psllq512:
14148 case X86::BI__builtin_ia32_pslld128:
14149 case X86::BI__builtin_ia32_pslld256:
14150 case X86::BI__builtin_ia32_pslld512:
14151 case X86::BI__builtin_ia32_psllw128:
14152 case X86::BI__builtin_ia32_psllw256:
14153 case X86::BI__builtin_ia32_psllw512: {
14154 APValue R;
14155 if (!evalShiftWithCount(
14156 Info, Call: E, Out&: R,
14157 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.shl(shiftAmt: Count); },
14158 OverflowOp: [](const APInt &Elt, unsigned Width) {
14159 return APInt::getZero(numBits: Width);
14160 }))
14161 return false;
14162 return Success(V: R, E);
14163 }
14164
14165 case X86::BI__builtin_ia32_psrlq128:
14166 case X86::BI__builtin_ia32_psrlq256:
14167 case X86::BI__builtin_ia32_psrlq512:
14168 case X86::BI__builtin_ia32_psrld128:
14169 case X86::BI__builtin_ia32_psrld256:
14170 case X86::BI__builtin_ia32_psrld512:
14171 case X86::BI__builtin_ia32_psrlw128:
14172 case X86::BI__builtin_ia32_psrlw256:
14173 case X86::BI__builtin_ia32_psrlw512: {
14174 APValue R;
14175 if (!evalShiftWithCount(
14176 Info, Call: E, Out&: R,
14177 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.lshr(shiftAmt: Count); },
14178 OverflowOp: [](const APInt &Elt, unsigned Width) {
14179 return APInt::getZero(numBits: Width);
14180 }))
14181 return false;
14182 return Success(V: R, E);
14183 }
14184
14185 case X86::BI__builtin_ia32_pternlogd128_mask:
14186 case X86::BI__builtin_ia32_pternlogd256_mask:
14187 case X86::BI__builtin_ia32_pternlogd512_mask:
14188 case X86::BI__builtin_ia32_pternlogq128_mask:
14189 case X86::BI__builtin_ia32_pternlogq256_mask:
14190 case X86::BI__builtin_ia32_pternlogq512_mask: {
14191 APValue AValue, BValue, CValue, ImmValue, UValue;
14192 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
14193 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
14194 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
14195 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
14196 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
14197 return false;
14198
14199 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14200 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14201 APInt Imm = ImmValue.getInt();
14202 APInt U = UValue.getInt();
14203 unsigned ResultLen = AValue.getVectorLength();
14204 SmallVector<APValue, 16> ResultElements;
14205 ResultElements.reserve(N: ResultLen);
14206
14207 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
14208 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
14209 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
14210 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
14211
14212 if (U[EltNum]) {
14213 unsigned BitWidth = ALane.getBitWidth();
14214 APInt ResLane(BitWidth, 0);
14215
14216 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
14217 unsigned ABit = ALane[Bit];
14218 unsigned BBit = BLane[Bit];
14219 unsigned CBit = CLane[Bit];
14220
14221 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
14222 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
14223 }
14224 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
14225 } else {
14226 ResultElements.push_back(Elt: APValue(APSInt(ALane, DestUnsigned)));
14227 }
14228 }
14229 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14230 }
14231 case X86::BI__builtin_ia32_pternlogd128_maskz:
14232 case X86::BI__builtin_ia32_pternlogd256_maskz:
14233 case X86::BI__builtin_ia32_pternlogd512_maskz:
14234 case X86::BI__builtin_ia32_pternlogq128_maskz:
14235 case X86::BI__builtin_ia32_pternlogq256_maskz:
14236 case X86::BI__builtin_ia32_pternlogq512_maskz: {
14237 APValue AValue, BValue, CValue, ImmValue, UValue;
14238 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
14239 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
14240 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
14241 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
14242 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
14243 return false;
14244
14245 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14246 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14247 APInt Imm = ImmValue.getInt();
14248 APInt U = UValue.getInt();
14249 unsigned ResultLen = AValue.getVectorLength();
14250 SmallVector<APValue, 16> ResultElements;
14251 ResultElements.reserve(N: ResultLen);
14252
14253 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
14254 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
14255 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
14256 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
14257
14258 unsigned BitWidth = ALane.getBitWidth();
14259 APInt ResLane(BitWidth, 0);
14260
14261 if (U[EltNum]) {
14262 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
14263 unsigned ABit = ALane[Bit];
14264 unsigned BBit = BLane[Bit];
14265 unsigned CBit = CLane[Bit];
14266
14267 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
14268 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
14269 }
14270 }
14271 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
14272 }
14273 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14274 }
14275
14276 case Builtin::BI__builtin_elementwise_clzg:
14277 case Builtin::BI__builtin_elementwise_ctzg: {
14278 APValue SourceLHS;
14279 std::optional<APValue> Fallback;
14280 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS))
14281 return false;
14282 if (E->getNumArgs() > 1) {
14283 APValue FallbackTmp;
14284 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: FallbackTmp))
14285 return false;
14286 Fallback = FallbackTmp;
14287 }
14288
14289 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14290 unsigned SourceLen = SourceLHS.getVectorLength();
14291 SmallVector<APValue, 4> ResultElements;
14292 ResultElements.reserve(N: SourceLen);
14293
14294 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14295 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
14296 if (!LHS) {
14297 // Without a fallback, a zero element is undefined
14298 if (!Fallback) {
14299 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
14300 << /*IsTrailing=*/(BuiltinOp ==
14301 Builtin::BI__builtin_elementwise_ctzg);
14302 return false;
14303 }
14304 ResultElements.push_back(Elt: Fallback->getVectorElt(I: EltNum));
14305 continue;
14306 }
14307 switch (BuiltinOp) {
14308 case Builtin::BI__builtin_elementwise_clzg:
14309 ResultElements.push_back(Elt: APValue(
14310 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countl_zero()),
14311 DestEltTy->isUnsignedIntegerOrEnumerationType())));
14312 break;
14313 case Builtin::BI__builtin_elementwise_ctzg:
14314 ResultElements.push_back(Elt: APValue(
14315 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countr_zero()),
14316 DestEltTy->isUnsignedIntegerOrEnumerationType())));
14317 break;
14318 }
14319 }
14320
14321 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14322 }
14323
14324 case Builtin::BI__builtin_elementwise_fma: {
14325 APValue SourceX, SourceY, SourceZ;
14326 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceX) ||
14327 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceY) ||
14328 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceZ))
14329 return false;
14330
14331 unsigned SourceLen = SourceX.getVectorLength();
14332 SmallVector<APValue> ResultElements;
14333 ResultElements.reserve(N: SourceLen);
14334 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14335 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14336 const APFloat &X = SourceX.getVectorElt(I: EltNum).getFloat();
14337 const APFloat &Y = SourceY.getVectorElt(I: EltNum).getFloat();
14338 const APFloat &Z = SourceZ.getVectorElt(I: EltNum).getFloat();
14339 APFloat Result(X);
14340 (void)Result.fusedMultiplyAdd(Multiplicand: Y, Addend: Z, RM);
14341 ResultElements.push_back(Elt: APValue(Result));
14342 }
14343 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14344 }
14345
14346 case clang::X86::BI__builtin_ia32_phaddw128:
14347 case clang::X86::BI__builtin_ia32_phaddw256:
14348 case clang::X86::BI__builtin_ia32_phaddd128:
14349 case clang::X86::BI__builtin_ia32_phaddd256:
14350 case clang::X86::BI__builtin_ia32_phaddsw128:
14351 case clang::X86::BI__builtin_ia32_phaddsw256:
14352
14353 case clang::X86::BI__builtin_ia32_phsubw128:
14354 case clang::X86::BI__builtin_ia32_phsubw256:
14355 case clang::X86::BI__builtin_ia32_phsubd128:
14356 case clang::X86::BI__builtin_ia32_phsubd256:
14357 case clang::X86::BI__builtin_ia32_phsubsw128:
14358 case clang::X86::BI__builtin_ia32_phsubsw256: {
14359 APValue SourceLHS, SourceRHS;
14360 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14361 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14362 return false;
14363 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14364 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14365
14366 unsigned NumElts = SourceLHS.getVectorLength();
14367 unsigned EltBits = Info.Ctx.getIntWidth(T: DestEltTy);
14368 unsigned EltsPerLane = 128 / EltBits;
14369 SmallVector<APValue, 4> ResultElements;
14370 ResultElements.reserve(N: NumElts);
14371
14372 for (unsigned LaneStart = 0; LaneStart != NumElts;
14373 LaneStart += EltsPerLane) {
14374 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14375 APSInt LHSA = SourceLHS.getVectorElt(I: LaneStart + I).getInt();
14376 APSInt LHSB = SourceLHS.getVectorElt(I: LaneStart + I + 1).getInt();
14377 switch (BuiltinOp) {
14378 case clang::X86::BI__builtin_ia32_phaddw128:
14379 case clang::X86::BI__builtin_ia32_phaddw256:
14380 case clang::X86::BI__builtin_ia32_phaddd128:
14381 case clang::X86::BI__builtin_ia32_phaddd256: {
14382 APSInt Res(LHSA + LHSB, DestUnsigned);
14383 ResultElements.push_back(Elt: APValue(Res));
14384 break;
14385 }
14386 case clang::X86::BI__builtin_ia32_phaddsw128:
14387 case clang::X86::BI__builtin_ia32_phaddsw256: {
14388 APSInt Res(LHSA.sadd_sat(RHS: LHSB));
14389 ResultElements.push_back(Elt: APValue(Res));
14390 break;
14391 }
14392 case clang::X86::BI__builtin_ia32_phsubw128:
14393 case clang::X86::BI__builtin_ia32_phsubw256:
14394 case clang::X86::BI__builtin_ia32_phsubd128:
14395 case clang::X86::BI__builtin_ia32_phsubd256: {
14396 APSInt Res(LHSA - LHSB, DestUnsigned);
14397 ResultElements.push_back(Elt: APValue(Res));
14398 break;
14399 }
14400 case clang::X86::BI__builtin_ia32_phsubsw128:
14401 case clang::X86::BI__builtin_ia32_phsubsw256: {
14402 APSInt Res(LHSA.ssub_sat(RHS: LHSB));
14403 ResultElements.push_back(Elt: APValue(Res));
14404 break;
14405 }
14406 }
14407 }
14408 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14409 APSInt RHSA = SourceRHS.getVectorElt(I: LaneStart + I).getInt();
14410 APSInt RHSB = SourceRHS.getVectorElt(I: LaneStart + I + 1).getInt();
14411 switch (BuiltinOp) {
14412 case clang::X86::BI__builtin_ia32_phaddw128:
14413 case clang::X86::BI__builtin_ia32_phaddw256:
14414 case clang::X86::BI__builtin_ia32_phaddd128:
14415 case clang::X86::BI__builtin_ia32_phaddd256: {
14416 APSInt Res(RHSA + RHSB, DestUnsigned);
14417 ResultElements.push_back(Elt: APValue(Res));
14418 break;
14419 }
14420 case clang::X86::BI__builtin_ia32_phaddsw128:
14421 case clang::X86::BI__builtin_ia32_phaddsw256: {
14422 APSInt Res(RHSA.sadd_sat(RHS: RHSB));
14423 ResultElements.push_back(Elt: APValue(Res));
14424 break;
14425 }
14426 case clang::X86::BI__builtin_ia32_phsubw128:
14427 case clang::X86::BI__builtin_ia32_phsubw256:
14428 case clang::X86::BI__builtin_ia32_phsubd128:
14429 case clang::X86::BI__builtin_ia32_phsubd256: {
14430 APSInt Res(RHSA - RHSB, DestUnsigned);
14431 ResultElements.push_back(Elt: APValue(Res));
14432 break;
14433 }
14434 case clang::X86::BI__builtin_ia32_phsubsw128:
14435 case clang::X86::BI__builtin_ia32_phsubsw256: {
14436 APSInt Res(RHSA.ssub_sat(RHS: RHSB));
14437 ResultElements.push_back(Elt: APValue(Res));
14438 break;
14439 }
14440 }
14441 }
14442 }
14443 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14444 }
14445 case clang::X86::BI__builtin_ia32_haddpd:
14446 case clang::X86::BI__builtin_ia32_haddps:
14447 case clang::X86::BI__builtin_ia32_haddps256:
14448 case clang::X86::BI__builtin_ia32_haddpd256:
14449 case clang::X86::BI__builtin_ia32_hsubpd:
14450 case clang::X86::BI__builtin_ia32_hsubps:
14451 case clang::X86::BI__builtin_ia32_hsubps256:
14452 case clang::X86::BI__builtin_ia32_hsubpd256: {
14453 APValue SourceLHS, SourceRHS;
14454 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14455 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14456 return false;
14457 unsigned NumElts = SourceLHS.getVectorLength();
14458 SmallVector<APValue, 4> ResultElements;
14459 ResultElements.reserve(N: NumElts);
14460 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14461 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14462 unsigned EltBits = Info.Ctx.getTypeSize(T: DestEltTy);
14463 unsigned NumLanes = NumElts * EltBits / 128;
14464 unsigned NumElemsPerLane = NumElts / NumLanes;
14465 unsigned HalfElemsPerLane = NumElemsPerLane / 2;
14466
14467 for (unsigned L = 0; L != NumElts; L += NumElemsPerLane) {
14468 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14469 APFloat LHSA = SourceLHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14470 APFloat LHSB = SourceLHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14471 switch (BuiltinOp) {
14472 case clang::X86::BI__builtin_ia32_haddpd:
14473 case clang::X86::BI__builtin_ia32_haddps:
14474 case clang::X86::BI__builtin_ia32_haddps256:
14475 case clang::X86::BI__builtin_ia32_haddpd256:
14476 LHSA.add(RHS: LHSB, RM);
14477 break;
14478 case clang::X86::BI__builtin_ia32_hsubpd:
14479 case clang::X86::BI__builtin_ia32_hsubps:
14480 case clang::X86::BI__builtin_ia32_hsubps256:
14481 case clang::X86::BI__builtin_ia32_hsubpd256:
14482 LHSA.subtract(RHS: LHSB, RM);
14483 break;
14484 }
14485 ResultElements.push_back(Elt: APValue(LHSA));
14486 }
14487 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14488 APFloat RHSA = SourceRHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14489 APFloat RHSB = SourceRHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14490 switch (BuiltinOp) {
14491 case clang::X86::BI__builtin_ia32_haddpd:
14492 case clang::X86::BI__builtin_ia32_haddps:
14493 case clang::X86::BI__builtin_ia32_haddps256:
14494 case clang::X86::BI__builtin_ia32_haddpd256:
14495 RHSA.add(RHS: RHSB, RM);
14496 break;
14497 case clang::X86::BI__builtin_ia32_hsubpd:
14498 case clang::X86::BI__builtin_ia32_hsubps:
14499 case clang::X86::BI__builtin_ia32_hsubps256:
14500 case clang::X86::BI__builtin_ia32_hsubpd256:
14501 RHSA.subtract(RHS: RHSB, RM);
14502 break;
14503 }
14504 ResultElements.push_back(Elt: APValue(RHSA));
14505 }
14506 }
14507 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14508 }
14509 case clang::X86::BI__builtin_ia32_addsubpd:
14510 case clang::X86::BI__builtin_ia32_addsubps:
14511 case clang::X86::BI__builtin_ia32_addsubpd256:
14512 case clang::X86::BI__builtin_ia32_addsubps256: {
14513 // Addsub: alternates between subtraction and addition
14514 // Result[i] = (i % 2 == 0) ? (a[i] - b[i]) : (a[i] + b[i])
14515 APValue SourceLHS, SourceRHS;
14516 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14517 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14518 return false;
14519 unsigned NumElems = SourceLHS.getVectorLength();
14520 SmallVector<APValue, 8> ResultElements;
14521 ResultElements.reserve(N: NumElems);
14522 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14523
14524 for (unsigned I = 0; I != NumElems; ++I) {
14525 APFloat LHS = SourceLHS.getVectorElt(I).getFloat();
14526 APFloat RHS = SourceRHS.getVectorElt(I).getFloat();
14527 if (I % 2 == 0) {
14528 // Even indices: subtract
14529 LHS.subtract(RHS, RM);
14530 } else {
14531 // Odd indices: add
14532 LHS.add(RHS, RM);
14533 }
14534 ResultElements.push_back(Elt: APValue(LHS));
14535 }
14536 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14537 }
14538 case clang::X86::BI__builtin_ia32_pclmulqdq128:
14539 case clang::X86::BI__builtin_ia32_pclmulqdq256:
14540 case clang::X86::BI__builtin_ia32_pclmulqdq512: {
14541 // PCLMULQDQ: carry-less multiplication of selected 64-bit halves
14542 // imm8 bit 0: selects lower (0) or upper (1) 64 bits of first operand
14543 // imm8 bit 4: selects lower (0) or upper (1) 64 bits of second operand
14544 APValue SourceLHS, SourceRHS;
14545 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14546 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14547 return false;
14548
14549 APSInt Imm8;
14550 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm8, Info))
14551 return false;
14552
14553 // Extract bits 0 and 4 from imm8
14554 bool SelectUpperA = (Imm8 & 0x01) != 0;
14555 bool SelectUpperB = (Imm8 & 0x10) != 0;
14556
14557 unsigned NumElems = SourceLHS.getVectorLength();
14558 SmallVector<APValue, 8> ResultElements;
14559 ResultElements.reserve(N: NumElems);
14560 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14561 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14562
14563 // Process each 128-bit lane
14564 for (unsigned Lane = 0; Lane < NumElems; Lane += 2) {
14565 // Get the two 64-bit halves of the first operand
14566 APSInt A0 = SourceLHS.getVectorElt(I: Lane + 0).getInt();
14567 APSInt A1 = SourceLHS.getVectorElt(I: Lane + 1).getInt();
14568 // Get the two 64-bit halves of the second operand
14569 APSInt B0 = SourceRHS.getVectorElt(I: Lane + 0).getInt();
14570 APSInt B1 = SourceRHS.getVectorElt(I: Lane + 1).getInt();
14571
14572 // Select the appropriate 64-bit values based on imm8
14573 APInt A = SelectUpperA ? A1 : A0;
14574 APInt B = SelectUpperB ? B1 : B0;
14575
14576 // Extend both operands to 128 bits for carry-less multiplication
14577 APInt A128 = A.zext(width: 128);
14578 APInt B128 = B.zext(width: 128);
14579
14580 // Use APIntOps::clmul for carry-less multiplication
14581 APInt Result = llvm::APIntOps::clmul(LHS: A128, RHS: B128);
14582
14583 // Split the 128-bit result into two 64-bit halves
14584 APSInt ResultLow(Result.extractBits(numBits: 64, bitPosition: 0), DestUnsigned);
14585 APSInt ResultHigh(Result.extractBits(numBits: 64, bitPosition: 64), DestUnsigned);
14586
14587 ResultElements.push_back(Elt: APValue(ResultLow));
14588 ResultElements.push_back(Elt: APValue(ResultHigh));
14589 }
14590
14591 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14592 }
14593 case Builtin::BI__builtin_elementwise_clmul:
14594 return EvaluateBinOpExpr(llvm::APIntOps::clmul);
14595 case Builtin::BI__builtin_elementwise_pext:
14596 return EvaluateBinOpExpr(llvm::APIntOps::pext);
14597 case Builtin::BI__builtin_elementwise_pdep:
14598 return EvaluateBinOpExpr(llvm::APIntOps::pdep);
14599 case Builtin::BI__builtin_elementwise_fshl:
14600 case Builtin::BI__builtin_elementwise_fshr: {
14601 APValue SourceHi, SourceLo, SourceShift;
14602 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
14603 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
14604 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceShift))
14605 return false;
14606
14607 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14608 if (!DestEltTy->isIntegerType())
14609 return false;
14610
14611 unsigned SourceLen = SourceHi.getVectorLength();
14612 SmallVector<APValue> ResultElements;
14613 ResultElements.reserve(N: SourceLen);
14614 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14615 const APSInt &Hi = SourceHi.getVectorElt(I: EltNum).getInt();
14616 const APSInt &Lo = SourceLo.getVectorElt(I: EltNum).getInt();
14617 const APSInt &Shift = SourceShift.getVectorElt(I: EltNum).getInt();
14618 switch (BuiltinOp) {
14619 case Builtin::BI__builtin_elementwise_fshl:
14620 ResultElements.push_back(Elt: APValue(
14621 APSInt(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned())));
14622 break;
14623 case Builtin::BI__builtin_elementwise_fshr:
14624 ResultElements.push_back(Elt: APValue(
14625 APSInt(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned())));
14626 break;
14627 }
14628 }
14629
14630 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14631 }
14632
14633 case X86::BI__builtin_ia32_shuf_f32x4_256:
14634 case X86::BI__builtin_ia32_shuf_i32x4_256:
14635 case X86::BI__builtin_ia32_shuf_f64x2_256:
14636 case X86::BI__builtin_ia32_shuf_i64x2_256:
14637 case X86::BI__builtin_ia32_shuf_f32x4:
14638 case X86::BI__builtin_ia32_shuf_i32x4:
14639 case X86::BI__builtin_ia32_shuf_f64x2:
14640 case X86::BI__builtin_ia32_shuf_i64x2: {
14641 APValue SourceA, SourceB;
14642 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
14643 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB))
14644 return false;
14645
14646 APSInt Imm;
14647 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14648 return false;
14649
14650 // Destination and sources A, B all have the same type.
14651 unsigned NumElems = SourceA.getVectorLength();
14652 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14653 QualType ElemQT = VT->getElementType();
14654 unsigned ElemBits = Info.Ctx.getTypeSize(T: ElemQT);
14655 unsigned LaneBits = 128u;
14656 unsigned NumLanes = (NumElems * ElemBits) / LaneBits;
14657 unsigned NumElemsPerLane = LaneBits / ElemBits;
14658
14659 unsigned DstLen = SourceA.getVectorLength();
14660 SmallVector<APValue, 16> ResultElements;
14661 ResultElements.reserve(N: DstLen);
14662
14663 APValue R;
14664 if (!evalShuffleGeneric(
14665 Info, Call: E, Out&: R,
14666 GetSourceIndex: [NumLanes, NumElemsPerLane](unsigned DstIdx, unsigned ShuffleMask)
14667 -> std::pair<unsigned, int> {
14668 // DstIdx determines source. ShuffleMask selects lane in source.
14669 unsigned BitsPerElem = NumLanes / 2;
14670 unsigned IndexMask = (1u << BitsPerElem) - 1;
14671 unsigned Lane = DstIdx / NumElemsPerLane;
14672 unsigned SrcIdx = (Lane < NumLanes / 2) ? 0 : 1;
14673 unsigned BitIdx = BitsPerElem * Lane;
14674 unsigned SrcLaneIdx = (ShuffleMask >> BitIdx) & IndexMask;
14675 unsigned ElemInLane = DstIdx % NumElemsPerLane;
14676 unsigned IdxToPick = SrcLaneIdx * NumElemsPerLane + ElemInLane;
14677 return {SrcIdx, IdxToPick};
14678 }))
14679 return false;
14680 return Success(V: R, E);
14681 }
14682
14683 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14684 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14685 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi:
14686 case X86::BI__builtin_ia32_vgf2p8affineqb_v16qi:
14687 case X86::BI__builtin_ia32_vgf2p8affineqb_v32qi:
14688 case X86::BI__builtin_ia32_vgf2p8affineqb_v64qi: {
14689
14690 APValue X, A;
14691 APSInt Imm;
14692 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: X) ||
14693 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: A) ||
14694 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14695 return false;
14696
14697 assert(X.isVector() && A.isVector());
14698 assert(X.getVectorLength() == A.getVectorLength());
14699
14700 bool IsInverse = false;
14701 switch (BuiltinOp) {
14702 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14703 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14704 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi: {
14705 IsInverse = true;
14706 }
14707 }
14708
14709 unsigned NumBitsInByte = 8;
14710 unsigned NumBytesInQWord = 8;
14711 unsigned NumBitsInQWord = 64;
14712 unsigned NumBytes = A.getVectorLength();
14713 unsigned NumQWords = NumBytes / NumBytesInQWord;
14714 SmallVector<APValue, 64> Result;
14715 Result.reserve(N: NumBytes);
14716
14717 // computing A*X + Imm
14718 for (unsigned QWordIdx = 0; QWordIdx != NumQWords; ++QWordIdx) {
14719 // Extract the QWords from X, A
14720 APInt XQWord(NumBitsInQWord, 0);
14721 APInt AQWord(NumBitsInQWord, 0);
14722 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14723 unsigned Idx = QWordIdx * NumBytesInQWord + ByteIdx;
14724 APInt XByte = X.getVectorElt(I: Idx).getInt();
14725 APInt AByte = A.getVectorElt(I: Idx).getInt();
14726 XQWord.insertBits(SubBits: XByte, bitPosition: ByteIdx * NumBitsInByte);
14727 AQWord.insertBits(SubBits: AByte, bitPosition: ByteIdx * NumBitsInByte);
14728 }
14729
14730 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14731 uint8_t XByte =
14732 XQWord.lshr(shiftAmt: ByteIdx * NumBitsInByte).getLoBits(numBits: 8).getZExtValue();
14733 Result.push_back(Elt: APValue(APSInt(
14734 APInt(8, GFNIAffine(XByte, AQword: AQWord, Imm, Inverse: IsInverse)), false)));
14735 }
14736 }
14737
14738 return Success(V: APValue(Result.data(), Result.size()), E);
14739 }
14740
14741 case X86::BI__builtin_ia32_vgf2p8mulb_v16qi:
14742 case X86::BI__builtin_ia32_vgf2p8mulb_v32qi:
14743 case X86::BI__builtin_ia32_vgf2p8mulb_v64qi: {
14744 APValue A, B;
14745 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
14746 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
14747 return false;
14748
14749 assert(A.isVector() && B.isVector());
14750 assert(A.getVectorLength() == B.getVectorLength());
14751
14752 unsigned NumBytes = A.getVectorLength();
14753 SmallVector<APValue, 64> Result;
14754 Result.reserve(N: NumBytes);
14755
14756 for (unsigned ByteIdx = 0; ByteIdx != NumBytes; ++ByteIdx) {
14757 uint8_t AByte = A.getVectorElt(I: ByteIdx).getInt().getZExtValue();
14758 uint8_t BByte = B.getVectorElt(I: ByteIdx).getInt().getZExtValue();
14759 Result.push_back(Elt: APValue(
14760 APSInt(APInt(8, GFNIMul(AByte, BByte)), /*IsUnsigned=*/false)));
14761 }
14762
14763 return Success(V: APValue(Result.data(), Result.size()), E);
14764 }
14765
14766 case X86::BI__builtin_ia32_insertf32x4_256:
14767 case X86::BI__builtin_ia32_inserti32x4_256:
14768 case X86::BI__builtin_ia32_insertf64x2_256:
14769 case X86::BI__builtin_ia32_inserti64x2_256:
14770 case X86::BI__builtin_ia32_insertf32x4:
14771 case X86::BI__builtin_ia32_inserti32x4:
14772 case X86::BI__builtin_ia32_insertf64x2_512:
14773 case X86::BI__builtin_ia32_inserti64x2_512:
14774 case X86::BI__builtin_ia32_insertf32x8:
14775 case X86::BI__builtin_ia32_inserti32x8:
14776 case X86::BI__builtin_ia32_insertf64x4:
14777 case X86::BI__builtin_ia32_inserti64x4:
14778 case X86::BI__builtin_ia32_vinsertf128_ps256:
14779 case X86::BI__builtin_ia32_vinsertf128_pd256:
14780 case X86::BI__builtin_ia32_vinsertf128_si256:
14781 case X86::BI__builtin_ia32_insert128i256: {
14782 APValue SourceDst, SourceSub;
14783 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceDst) ||
14784 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceSub))
14785 return false;
14786
14787 APSInt Imm;
14788 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14789 return false;
14790
14791 assert(SourceDst.isVector() && SourceSub.isVector());
14792 unsigned DstLen = SourceDst.getVectorLength();
14793 unsigned SubLen = SourceSub.getVectorLength();
14794 assert(SubLen != 0 && DstLen != 0 && (DstLen % SubLen) == 0);
14795 unsigned NumLanes = DstLen / SubLen;
14796 unsigned LaneIdx = (Imm.getZExtValue() % NumLanes) * SubLen;
14797
14798 SmallVector<APValue, 16> ResultElements;
14799 ResultElements.reserve(N: DstLen);
14800
14801 for (unsigned EltNum = 0; EltNum < DstLen; ++EltNum) {
14802 if (EltNum >= LaneIdx && EltNum < LaneIdx + SubLen)
14803 ResultElements.push_back(Elt: SourceSub.getVectorElt(I: EltNum - LaneIdx));
14804 else
14805 ResultElements.push_back(Elt: SourceDst.getVectorElt(I: EltNum));
14806 }
14807
14808 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14809 }
14810
14811 case clang::X86::BI__builtin_ia32_vec_set_v4hi:
14812 case clang::X86::BI__builtin_ia32_vec_set_v16qi:
14813 case clang::X86::BI__builtin_ia32_vec_set_v8hi:
14814 case clang::X86::BI__builtin_ia32_vec_set_v4si:
14815 case clang::X86::BI__builtin_ia32_vec_set_v2di:
14816 case clang::X86::BI__builtin_ia32_vec_set_v32qi:
14817 case clang::X86::BI__builtin_ia32_vec_set_v16hi:
14818 case clang::X86::BI__builtin_ia32_vec_set_v8si:
14819 case clang::X86::BI__builtin_ia32_vec_set_v4di: {
14820 APValue VecVal;
14821 APSInt Scalar, IndexAPS;
14822 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: VecVal, Info) ||
14823 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Scalar, Info) ||
14824 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: IndexAPS, Info))
14825 return false;
14826
14827 QualType ElemTy = E->getType()->castAs<VectorType>()->getElementType();
14828 unsigned ElemWidth = Info.Ctx.getIntWidth(T: ElemTy);
14829 bool ElemUnsigned = ElemTy->isUnsignedIntegerOrEnumerationType();
14830 Scalar.setIsUnsigned(ElemUnsigned);
14831 APSInt ElemAPS = Scalar.extOrTrunc(width: ElemWidth);
14832 APValue ElemAV(ElemAPS);
14833
14834 unsigned NumElems = VecVal.getVectorLength();
14835 unsigned Index =
14836 static_cast<unsigned>(IndexAPS.getZExtValue() & (NumElems - 1));
14837
14838 SmallVector<APValue, 4> Elems;
14839 Elems.reserve(N: NumElems);
14840 for (unsigned ElemNum = 0; ElemNum != NumElems; ++ElemNum)
14841 Elems.push_back(Elt: ElemNum == Index ? ElemAV : VecVal.getVectorElt(I: ElemNum));
14842
14843 return Success(V: APValue(Elems.data(), NumElems), E);
14844 }
14845
14846 case X86::BI__builtin_ia32_pslldqi128_byteshift:
14847 case X86::BI__builtin_ia32_pslldqi256_byteshift:
14848 case X86::BI__builtin_ia32_pslldqi512_byteshift: {
14849 APValue R;
14850 if (!evalShuffleGeneric(
14851 Info, Call: E, Out&: R,
14852 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
14853 unsigned LaneBase = (DstIdx / 16) * 16;
14854 unsigned LaneIdx = DstIdx % 16;
14855 if (LaneIdx < Shift)
14856 return std::make_pair(x: 0, y: -1);
14857
14858 return std::make_pair(
14859 x: 0, y: static_cast<int>(LaneBase + LaneIdx - Shift));
14860 }))
14861 return false;
14862 return Success(V: R, E);
14863 }
14864
14865 case X86::BI__builtin_ia32_psrldqi128_byteshift:
14866 case X86::BI__builtin_ia32_psrldqi256_byteshift:
14867 case X86::BI__builtin_ia32_psrldqi512_byteshift: {
14868 APValue R;
14869 if (!evalShuffleGeneric(
14870 Info, Call: E, Out&: R,
14871 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
14872 unsigned LaneBase = (DstIdx / 16) * 16;
14873 unsigned LaneIdx = DstIdx % 16;
14874 if (LaneIdx + Shift < 16)
14875 return std::make_pair(
14876 x: 0, y: static_cast<int>(LaneBase + LaneIdx + Shift));
14877
14878 return std::make_pair(x: 0, y: -1);
14879 }))
14880 return false;
14881 return Success(V: R, E);
14882 }
14883
14884 case X86::BI__builtin_ia32_palignr128:
14885 case X86::BI__builtin_ia32_palignr256:
14886 case X86::BI__builtin_ia32_palignr512: {
14887 APValue R;
14888 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Shift) {
14889 // Default to -1 → zero-fill this destination element
14890 unsigned VecIdx = 1;
14891 int ElemIdx = -1;
14892
14893 int Lane = DstIdx / 16;
14894 int Offset = DstIdx % 16;
14895
14896 // Elements come from VecB first, then VecA after the shift boundary
14897 unsigned ShiftedIdx = Offset + (Shift & 0xFF);
14898 if (ShiftedIdx < 16) { // from VecB
14899 ElemIdx = ShiftedIdx + (Lane * 16);
14900 } else if (ShiftedIdx < 32) { // from VecA
14901 VecIdx = 0;
14902 ElemIdx = (ShiftedIdx - 16) + (Lane * 16);
14903 }
14904
14905 return std::pair<unsigned, int>{VecIdx, ElemIdx};
14906 }))
14907 return false;
14908 return Success(V: R, E);
14909 }
14910 case X86::BI__builtin_ia32_alignd128:
14911 case X86::BI__builtin_ia32_alignd256:
14912 case X86::BI__builtin_ia32_alignd512:
14913 case X86::BI__builtin_ia32_alignq128:
14914 case X86::BI__builtin_ia32_alignq256:
14915 case X86::BI__builtin_ia32_alignq512: {
14916 APValue R;
14917 unsigned NumElems = E->getType()->castAs<VectorType>()->getNumElements();
14918 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14919 GetSourceIndex: [NumElems](unsigned DstIdx, unsigned Shift) {
14920 unsigned Imm = Shift & 0xFF;
14921 unsigned EffectiveShift = Imm & (NumElems - 1);
14922 unsigned SourcePos = DstIdx + EffectiveShift;
14923 unsigned VecIdx = SourcePos < NumElems ? 1 : 0;
14924 unsigned ElemIdx = SourcePos & (NumElems - 1);
14925
14926 return std::pair<unsigned, int>{
14927 VecIdx, static_cast<int>(ElemIdx)};
14928 }))
14929 return false;
14930 return Success(V: R, E);
14931 }
14932 case X86::BI__builtin_ia32_permvarsi256:
14933 case X86::BI__builtin_ia32_permvarsf256:
14934 case X86::BI__builtin_ia32_permvardf512:
14935 case X86::BI__builtin_ia32_permvardi512:
14936 case X86::BI__builtin_ia32_permvarhi128: {
14937 APValue R;
14938 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14939 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14940 int Offset = ShuffleMask & 0x7;
14941 return std::pair<unsigned, int>{0, Offset};
14942 }))
14943 return false;
14944 return Success(V: R, E);
14945 }
14946 case X86::BI__builtin_ia32_permvarqi128:
14947 case X86::BI__builtin_ia32_permvarhi256:
14948 case X86::BI__builtin_ia32_permvarsi512:
14949 case X86::BI__builtin_ia32_permvarsf512: {
14950 APValue R;
14951 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14952 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14953 int Offset = ShuffleMask & 0xF;
14954 return std::pair<unsigned, int>{0, Offset};
14955 }))
14956 return false;
14957 return Success(V: R, E);
14958 }
14959 case X86::BI__builtin_ia32_permvardi256:
14960 case X86::BI__builtin_ia32_permvardf256: {
14961 APValue R;
14962 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14963 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14964 int Offset = ShuffleMask & 0x3;
14965 return std::pair<unsigned, int>{0, Offset};
14966 }))
14967 return false;
14968 return Success(V: R, E);
14969 }
14970 case X86::BI__builtin_ia32_permvarqi256:
14971 case X86::BI__builtin_ia32_permvarhi512: {
14972 APValue R;
14973 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14974 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14975 int Offset = ShuffleMask & 0x1F;
14976 return std::pair<unsigned, int>{0, Offset};
14977 }))
14978 return false;
14979 return Success(V: R, E);
14980 }
14981 case X86::BI__builtin_ia32_permvarqi512: {
14982 APValue R;
14983 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14984 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14985 int Offset = ShuffleMask & 0x3F;
14986 return std::pair<unsigned, int>{0, Offset};
14987 }))
14988 return false;
14989 return Success(V: R, E);
14990 }
14991 case X86::BI__builtin_ia32_vpermi2varq128:
14992 case X86::BI__builtin_ia32_vpermi2varpd128: {
14993 APValue R;
14994 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14995 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14996 int Offset = ShuffleMask & 0x1;
14997 unsigned SrcIdx = (ShuffleMask >> 1) & 0x1;
14998 return std::pair<unsigned, int>{SrcIdx, Offset};
14999 }))
15000 return false;
15001 return Success(V: R, E);
15002 }
15003 case X86::BI__builtin_ia32_vpermi2vard128:
15004 case X86::BI__builtin_ia32_vpermi2varps128:
15005 case X86::BI__builtin_ia32_vpermi2varq256:
15006 case X86::BI__builtin_ia32_vpermi2varpd256: {
15007 APValue R;
15008 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15009 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15010 int Offset = ShuffleMask & 0x3;
15011 unsigned SrcIdx = (ShuffleMask >> 2) & 0x1;
15012 return std::pair<unsigned, int>{SrcIdx, Offset};
15013 }))
15014 return false;
15015 return Success(V: R, E);
15016 }
15017 case X86::BI__builtin_ia32_vpermi2varhi128:
15018 case X86::BI__builtin_ia32_vpermi2vard256:
15019 case X86::BI__builtin_ia32_vpermi2varps256:
15020 case X86::BI__builtin_ia32_vpermi2varq512:
15021 case X86::BI__builtin_ia32_vpermi2varpd512: {
15022 APValue R;
15023 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15024 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15025 int Offset = ShuffleMask & 0x7;
15026 unsigned SrcIdx = (ShuffleMask >> 3) & 0x1;
15027 return std::pair<unsigned, int>{SrcIdx, Offset};
15028 }))
15029 return false;
15030 return Success(V: R, E);
15031 }
15032 case X86::BI__builtin_ia32_vpermi2varqi128:
15033 case X86::BI__builtin_ia32_vpermi2varhi256:
15034 case X86::BI__builtin_ia32_vpermi2vard512:
15035 case X86::BI__builtin_ia32_vpermi2varps512: {
15036 APValue R;
15037 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15038 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15039 int Offset = ShuffleMask & 0xF;
15040 unsigned SrcIdx = (ShuffleMask >> 4) & 0x1;
15041 return std::pair<unsigned, int>{SrcIdx, Offset};
15042 }))
15043 return false;
15044 return Success(V: R, E);
15045 }
15046 case X86::BI__builtin_ia32_vpermi2varqi256:
15047 case X86::BI__builtin_ia32_vpermi2varhi512: {
15048 APValue R;
15049 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15050 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15051 int Offset = ShuffleMask & 0x1F;
15052 unsigned SrcIdx = (ShuffleMask >> 5) & 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_vpermi2varqi512: {
15059 APValue R;
15060 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15061 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15062 int Offset = ShuffleMask & 0x3F;
15063 unsigned SrcIdx = (ShuffleMask >> 6) & 0x1;
15064 return std::pair<unsigned, int>{SrcIdx, Offset};
15065 }))
15066 return false;
15067 return Success(V: R, E);
15068 }
15069
15070 case clang::X86::BI__builtin_ia32_minps:
15071 case clang::X86::BI__builtin_ia32_minpd:
15072 case clang::X86::BI__builtin_ia32_minps256:
15073 case clang::X86::BI__builtin_ia32_minpd256:
15074 case clang::X86::BI__builtin_ia32_minps512:
15075 case clang::X86::BI__builtin_ia32_minpd512:
15076 case clang::X86::BI__builtin_ia32_minph128:
15077 case clang::X86::BI__builtin_ia32_minph256:
15078 case clang::X86::BI__builtin_ia32_minph512:
15079 return EvaluateFpBinOpExpr(
15080 [](const APFloat &A, const APFloat &B,
15081 std::optional<APSInt>) -> std::optional<APFloat> {
15082 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
15083 B.isInfinity() || B.isDenormal())
15084 return std::nullopt;
15085 if (A.isZero() && B.isZero())
15086 return B;
15087 return llvm::minimum(A, B);
15088 });
15089
15090 case clang::X86::BI__builtin_ia32_minss:
15091 case clang::X86::BI__builtin_ia32_minsd:
15092 return EvaluateFpBinOpExpr(
15093 [](const APFloat &A, const APFloat &B,
15094 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15095 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/true);
15096 },
15097 /*IsScalar=*/true);
15098
15099 case clang::X86::BI__builtin_ia32_minsd_round_mask:
15100 case clang::X86::BI__builtin_ia32_minss_round_mask:
15101 case clang::X86::BI__builtin_ia32_minsh_round_mask:
15102 case clang::X86::BI__builtin_ia32_maxsd_round_mask:
15103 case clang::X86::BI__builtin_ia32_maxss_round_mask:
15104 case clang::X86::BI__builtin_ia32_maxsh_round_mask: {
15105 bool IsMin = BuiltinOp == clang::X86::BI__builtin_ia32_minsd_round_mask ||
15106 BuiltinOp == clang::X86::BI__builtin_ia32_minss_round_mask ||
15107 BuiltinOp == clang::X86::BI__builtin_ia32_minsh_round_mask;
15108 return EvaluateScalarFpRoundMaskBinOp(
15109 [IsMin](const APFloat &A, const APFloat &B,
15110 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15111 return EvalScalarMinMaxFp(A, B, RoundingMode, IsMin);
15112 });
15113 }
15114
15115 case clang::X86::BI__builtin_ia32_maxps:
15116 case clang::X86::BI__builtin_ia32_maxpd:
15117 case clang::X86::BI__builtin_ia32_maxps256:
15118 case clang::X86::BI__builtin_ia32_maxpd256:
15119 case clang::X86::BI__builtin_ia32_maxps512:
15120 case clang::X86::BI__builtin_ia32_maxpd512:
15121 case clang::X86::BI__builtin_ia32_maxph128:
15122 case clang::X86::BI__builtin_ia32_maxph256:
15123 case clang::X86::BI__builtin_ia32_maxph512:
15124 return EvaluateFpBinOpExpr(
15125 [](const APFloat &A, const APFloat &B,
15126 std::optional<APSInt>) -> std::optional<APFloat> {
15127 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
15128 B.isInfinity() || B.isDenormal())
15129 return std::nullopt;
15130 if (A.isZero() && B.isZero())
15131 return B;
15132 return llvm::maximum(A, B);
15133 });
15134
15135 case clang::X86::BI__builtin_ia32_maxss:
15136 case clang::X86::BI__builtin_ia32_maxsd:
15137 return EvaluateFpBinOpExpr(
15138 [](const APFloat &A, const APFloat &B,
15139 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15140 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/false);
15141 },
15142 /*IsScalar=*/true);
15143
15144 case clang::X86::BI__builtin_ia32_vcvtps2ph:
15145 case clang::X86::BI__builtin_ia32_vcvtps2ph256: {
15146 APValue SrcVec;
15147 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SrcVec))
15148 return false;
15149
15150 APSInt Imm;
15151 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info))
15152 return false;
15153
15154 const auto *SrcVTy = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
15155 unsigned SrcNumElems = SrcVTy->getNumElements();
15156 const auto *DstVTy = E->getType()->castAs<VectorType>();
15157 unsigned DstNumElems = DstVTy->getNumElements();
15158 QualType DstElemTy = DstVTy->getElementType();
15159
15160 const llvm::fltSemantics &HalfSem =
15161 Info.Ctx.getFloatTypeSemantics(T: Info.Ctx.HalfTy);
15162
15163 int ImmVal = Imm.getZExtValue();
15164 bool UseMXCSR = (ImmVal & 4) != 0;
15165 bool IsFPConstrained =
15166 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained();
15167
15168 llvm::RoundingMode RM;
15169 if (!UseMXCSR) {
15170 switch (ImmVal & 3) {
15171 case 0:
15172 RM = llvm::RoundingMode::NearestTiesToEven;
15173 break;
15174 case 1:
15175 RM = llvm::RoundingMode::TowardNegative;
15176 break;
15177 case 2:
15178 RM = llvm::RoundingMode::TowardPositive;
15179 break;
15180 case 3:
15181 RM = llvm::RoundingMode::TowardZero;
15182 break;
15183 default:
15184 llvm_unreachable("Invalid immediate rounding mode");
15185 }
15186 } else {
15187 RM = llvm::RoundingMode::NearestTiesToEven;
15188 }
15189
15190 SmallVector<APValue, 8> ResultElements;
15191 ResultElements.reserve(N: DstNumElems);
15192
15193 for (unsigned I = 0; I < SrcNumElems; ++I) {
15194 APFloat SrcVal = SrcVec.getVectorElt(I).getFloat();
15195
15196 bool LostInfo;
15197 APFloat::opStatus St = SrcVal.convert(ToSemantics: HalfSem, RM, losesInfo: &LostInfo);
15198
15199 if (UseMXCSR && IsFPConstrained && St != APFloat::opOK) {
15200 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
15201 return false;
15202 }
15203
15204 APSInt DstInt(SrcVal.bitcastToAPInt(),
15205 DstElemTy->isUnsignedIntegerOrEnumerationType());
15206 ResultElements.push_back(Elt: APValue(DstInt));
15207 }
15208
15209 if (DstNumElems > SrcNumElems) {
15210 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: DstElemTy);
15211 for (unsigned I = SrcNumElems; I < DstNumElems; ++I) {
15212 ResultElements.push_back(Elt: APValue(Zero));
15213 }
15214 }
15215
15216 return Success(V: ResultElements, E);
15217 }
15218 case X86::BI__builtin_ia32_vperm2f128_pd256:
15219 case X86::BI__builtin_ia32_vperm2f128_ps256:
15220 case X86::BI__builtin_ia32_vperm2f128_si256:
15221 case X86::BI__builtin_ia32_permti256: {
15222 unsigned NumElements =
15223 E->getArg(Arg: 0)->getType()->getAs<VectorType>()->getNumElements();
15224 unsigned PreservedBitsCnt = NumElements >> 2;
15225 APValue R;
15226 if (!evalShuffleGeneric(
15227 Info, Call: E, Out&: R,
15228 GetSourceIndex: [PreservedBitsCnt](unsigned DstIdx, unsigned ShuffleMask) {
15229 unsigned ControlBitsCnt = DstIdx >> PreservedBitsCnt << 2;
15230 unsigned ControlBits = ShuffleMask >> ControlBitsCnt;
15231
15232 if (ControlBits & 0b1000)
15233 return std::make_pair(x: 0u, y: -1);
15234
15235 unsigned SrcVecIdx = (ControlBits & 0b10) >> 1;
15236 unsigned PreservedBitsMask = (1 << PreservedBitsCnt) - 1;
15237 int SrcIdx = ((ControlBits & 0b1) << PreservedBitsCnt) |
15238 (DstIdx & PreservedBitsMask);
15239 return std::make_pair(x&: SrcVecIdx, y&: SrcIdx);
15240 }))
15241 return false;
15242 return Success(V: R, E);
15243 }
15244 case X86::BI__builtin_ia32_vpdpwssd128:
15245 case X86::BI__builtin_ia32_vpdpwssd256:
15246 case X86::BI__builtin_ia32_vpdpwssd512:
15247 case X86::BI__builtin_ia32_vpdpbusd128:
15248 case X86::BI__builtin_ia32_vpdpbusd256:
15249 case X86::BI__builtin_ia32_vpdpbusd512:
15250 return EvalVectorDotProduct(false);
15251 case X86::BI__builtin_ia32_vpdpwssds128:
15252 case X86::BI__builtin_ia32_vpdpwssds256:
15253 case X86::BI__builtin_ia32_vpdpwssds512:
15254 case X86::BI__builtin_ia32_vpdpbusds128:
15255 case X86::BI__builtin_ia32_vpdpbusds256:
15256 case X86::BI__builtin_ia32_vpdpbusds512:
15257 return EvalVectorDotProduct(true);
15258 }
15259}
15260
15261bool VectorExprEvaluator::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
15262 APValue Source;
15263 QualType SourceVecType = E->getSrcExpr()->getType();
15264 if (!EvaluateAsRValue(Info, E: E->getSrcExpr(), Result&: Source))
15265 return false;
15266
15267 QualType DestTy = E->getType()->castAs<VectorType>()->getElementType();
15268 QualType SourceTy = SourceVecType->castAs<VectorType>()->getElementType();
15269
15270 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15271
15272 auto SourceLen = Source.getVectorLength();
15273 SmallVector<APValue, 4> ResultElements;
15274 ResultElements.reserve(N: SourceLen);
15275 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
15276 APValue Elt;
15277 if (!handleVectorElementCast(Info, FPO, E, SourceTy, DestTy,
15278 Original: Source.getVectorElt(I: EltNum), Result&: Elt))
15279 return false;
15280 ResultElements.push_back(Elt: std::move(Elt));
15281 }
15282
15283 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15284}
15285
15286static bool handleVectorShuffle(EvalInfo &Info, const ShuffleVectorExpr *E,
15287 QualType ElemType, APValue const &VecVal1,
15288 APValue const &VecVal2, unsigned EltNum,
15289 APValue &Result) {
15290 unsigned const TotalElementsInInputVector1 = VecVal1.getVectorLength();
15291 unsigned const TotalElementsInInputVector2 = VecVal2.getVectorLength();
15292
15293 APSInt IndexVal = E->getShuffleMaskIdx(N: EltNum);
15294 int64_t index = IndexVal.getExtValue();
15295 // The spec says that -1 should be treated as undef for optimizations,
15296 // but in constexpr we'd have to produce an APValue::Indeterminate,
15297 // which is prohibited from being a top-level constant value. Emit a
15298 // diagnostic instead.
15299 if (index == -1) {
15300 Info.FFDiag(
15301 E, DiagId: diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
15302 << EltNum;
15303 return false;
15304 }
15305
15306 if (index < 0 ||
15307 index >= TotalElementsInInputVector1 + TotalElementsInInputVector2)
15308 llvm_unreachable("Out of bounds shuffle index");
15309
15310 if (index >= TotalElementsInInputVector1)
15311 Result = VecVal2.getVectorElt(I: index - TotalElementsInInputVector1);
15312 else
15313 Result = VecVal1.getVectorElt(I: index);
15314 return true;
15315}
15316
15317bool VectorExprEvaluator::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
15318 // FIXME: Unary shuffle with mask not currently supported.
15319 if (E->getNumSubExprs() == 2)
15320 return Error(E);
15321 APValue VecVal1;
15322 const Expr *Vec1 = E->getExpr(Index: 0);
15323 if (!EvaluateAsRValue(Info, E: Vec1, Result&: VecVal1))
15324 return false;
15325 APValue VecVal2;
15326 const Expr *Vec2 = E->getExpr(Index: 1);
15327 if (!EvaluateAsRValue(Info, E: Vec2, Result&: VecVal2))
15328 return false;
15329
15330 VectorType const *DestVecTy = E->getType()->castAs<VectorType>();
15331 QualType DestElTy = DestVecTy->getElementType();
15332
15333 auto TotalElementsInOutputVector = DestVecTy->getNumElements();
15334
15335 SmallVector<APValue, 4> ResultElements;
15336 ResultElements.reserve(N: TotalElementsInOutputVector);
15337 for (unsigned EltNum = 0; EltNum < TotalElementsInOutputVector; ++EltNum) {
15338 APValue Elt;
15339 if (!handleVectorShuffle(Info, E, ElemType: DestElTy, VecVal1, VecVal2, EltNum, Result&: Elt))
15340 return false;
15341 ResultElements.push_back(Elt: std::move(Elt));
15342 }
15343
15344 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15345}
15346
15347//===----------------------------------------------------------------------===//
15348// Matrix Evaluation
15349//===----------------------------------------------------------------------===//
15350
15351namespace {
15352class MatrixExprEvaluator : public ExprEvaluatorBase<MatrixExprEvaluator> {
15353 APValue &Result;
15354
15355public:
15356 MatrixExprEvaluator(EvalInfo &Info, APValue &Result)
15357 : ExprEvaluatorBaseTy(Info), Result(Result) {}
15358
15359 bool Success(ArrayRef<APValue> M, const Expr *E) {
15360 auto *CMTy = E->getType()->castAs<ConstantMatrixType>();
15361 assert(M.size() == CMTy->getNumElementsFlattened());
15362 // FIXME: remove this APValue copy.
15363 Result = APValue(M.data(), CMTy->getNumRows(), CMTy->getNumColumns());
15364 return true;
15365 }
15366 bool Success(const APValue &M, const Expr *E) {
15367 assert(M.isMatrix() && "expected matrix");
15368 Result = M;
15369 return true;
15370 }
15371
15372 bool VisitCastExpr(const CastExpr *E);
15373 bool VisitInitListExpr(const InitListExpr *E);
15374};
15375} // end anonymous namespace
15376
15377static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info) {
15378 assert(E->isPRValue() && E->getType()->isConstantMatrixType() &&
15379 "not a matrix prvalue");
15380 return MatrixExprEvaluator(Info, Result).Visit(S: E);
15381}
15382
15383bool MatrixExprEvaluator::VisitCastExpr(const CastExpr *E) {
15384 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15385 unsigned NumRows = MT->getNumRows();
15386 unsigned NumCols = MT->getNumColumns();
15387 unsigned NElts = NumRows * NumCols;
15388 QualType EltTy = MT->getElementType();
15389 const Expr *SE = E->getSubExpr();
15390
15391 switch (E->getCastKind()) {
15392 case CK_HLSLAggregateSplatCast: {
15393 APValue Val;
15394 QualType ValTy;
15395
15396 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15397 return false;
15398
15399 APValue CastedVal;
15400 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15401 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: EltTy, Original: Val, Result&: CastedVal))
15402 return false;
15403
15404 SmallVector<APValue, 16> SplatEls(NElts, CastedVal);
15405 return Success(M: SplatEls, E);
15406 }
15407 case CK_HLSLElementwiseCast: {
15408 SmallVector<APValue> SrcVals;
15409 SmallVector<QualType> SrcTypes;
15410
15411 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
15412 return false;
15413
15414 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15415 SmallVector<QualType, 16> DestTypes(NElts, EltTy);
15416 SmallVector<APValue, 16> ResultEls(NElts);
15417 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
15418 Results&: ResultEls))
15419 return false;
15420 return Success(M: ResultEls, E);
15421 }
15422 default:
15423 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15424 }
15425}
15426
15427bool MatrixExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
15428 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15429 QualType EltTy = MT->getElementType();
15430
15431 assert(E->getNumInits() == MT->getNumElementsFlattened() &&
15432 "Expected number of elements in initializer list to match the number "
15433 "of matrix elements");
15434
15435 SmallVector<APValue, 16> Elements;
15436 Elements.reserve(N: MT->getNumElementsFlattened());
15437
15438 // The following loop assumes the elements of the matrix InitListExpr are in
15439 // row-major order, which matches the row-major ordering assumption of the
15440 // matrix APValue.
15441 for (unsigned I = 0, N = MT->getNumElementsFlattened(); I < N; ++I) {
15442 if (EltTy->isIntegerType()) {
15443 llvm::APSInt IntVal;
15444 if (!EvaluateInteger(E: E->getInit(Init: I), Result&: IntVal, Info))
15445 return false;
15446 Elements.push_back(Elt: APValue(IntVal));
15447 } else {
15448 llvm::APFloat FloatVal(0.0);
15449 if (!EvaluateFloat(E: E->getInit(Init: I), Result&: FloatVal, Info))
15450 return false;
15451 Elements.push_back(Elt: APValue(FloatVal));
15452 }
15453 }
15454
15455 return Success(M: Elements, E);
15456}
15457
15458//===----------------------------------------------------------------------===//
15459// Array Evaluation
15460//===----------------------------------------------------------------------===//
15461
15462namespace {
15463 class ArrayExprEvaluator
15464 : public ExprEvaluatorBase<ArrayExprEvaluator> {
15465 const LValue &This;
15466 APValue &Result;
15467 public:
15468
15469 ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
15470 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
15471
15472 bool Success(const APValue &V, const Expr *E) {
15473 assert(V.isArray() && "expected array");
15474 Result = V;
15475 return true;
15476 }
15477
15478 bool ZeroInitialization(const Expr *E) {
15479 const ConstantArrayType *CAT =
15480 Info.Ctx.getAsConstantArrayType(T: E->getType());
15481 if (!CAT) {
15482 if (E->getType()->isIncompleteArrayType()) {
15483 // We can be asked to zero-initialize a flexible array member; this
15484 // is represented as an ImplicitValueInitExpr of incomplete array
15485 // type. In this case, the array has zero elements.
15486 Result = APValue(APValue::UninitArray(), 0, 0);
15487 return true;
15488 }
15489 // FIXME: We could handle VLAs here.
15490 return Error(E);
15491 }
15492
15493 Result = APValue(APValue::UninitArray(), 0, CAT->getZExtSize());
15494 if (!Result.hasArrayFiller())
15495 return true;
15496
15497 // Zero-initialize all elements.
15498 LValue Subobject = This;
15499 Subobject.addArray(Info, E, CAT);
15500 ImplicitValueInitExpr VIE(CAT->getElementType());
15501 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject, E: &VIE);
15502 }
15503
15504 bool VisitCallExpr(const CallExpr *E) {
15505 return handleCallExpr(E, Result, ResultSlot: &This);
15506 }
15507 bool VisitCastExpr(const CastExpr *E);
15508 bool VisitInitListExpr(const InitListExpr *E,
15509 QualType AllocType = QualType());
15510 bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E);
15511 bool VisitCXXConstructExpr(const CXXConstructExpr *E);
15512 bool VisitCXXConstructExpr(const CXXConstructExpr *E,
15513 const LValue &Subobject,
15514 APValue *Value, QualType Type);
15515 bool VisitStringLiteral(const StringLiteral *E,
15516 QualType AllocType = QualType()) {
15517 expandStringLiteral(Info, S: E, Result, AllocType);
15518 return true;
15519 }
15520 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
15521 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
15522 ArrayRef<Expr *> Args,
15523 const Expr *ArrayFiller,
15524 QualType AllocType = QualType());
15525 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
15526 };
15527} // end anonymous namespace
15528
15529static bool EvaluateArray(const Expr *E, const LValue &This,
15530 APValue &Result, EvalInfo &Info) {
15531 assert(!E->isValueDependent());
15532 assert(E->isPRValue() && E->getType()->isArrayType() &&
15533 "not an array prvalue");
15534 return ArrayExprEvaluator(Info, This, Result).Visit(S: E);
15535}
15536
15537static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
15538 APValue &Result, const InitListExpr *ILE,
15539 QualType AllocType) {
15540 assert(!ILE->isValueDependent());
15541 assert(ILE->isPRValue() && ILE->getType()->isArrayType() &&
15542 "not an array prvalue");
15543 return ArrayExprEvaluator(Info, This, Result)
15544 .VisitInitListExpr(E: ILE, AllocType);
15545}
15546
15547static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
15548 APValue &Result,
15549 const CXXConstructExpr *CCE,
15550 QualType AllocType) {
15551 assert(!CCE->isValueDependent());
15552 assert(CCE->isPRValue() && CCE->getType()->isArrayType() &&
15553 "not an array prvalue");
15554 return ArrayExprEvaluator(Info, This, Result)
15555 .VisitCXXConstructExpr(E: CCE, Subobject: This, Value: &Result, Type: AllocType);
15556}
15557
15558// Return true iff the given array filler may depend on the element index.
15559static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) {
15560 // For now, just allow non-class value-initialization and initialization
15561 // lists comprised of them.
15562 if (isa<ImplicitValueInitExpr>(Val: FillerExpr))
15563 return false;
15564 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Val: FillerExpr)) {
15565 for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) {
15566 if (MaybeElementDependentArrayFiller(FillerExpr: ILE->getInit(Init: I)))
15567 return true;
15568 }
15569
15570 if (ILE->hasArrayFiller() &&
15571 MaybeElementDependentArrayFiller(FillerExpr: ILE->getArrayFiller()))
15572 return true;
15573
15574 return false;
15575 }
15576 return true;
15577}
15578
15579bool ArrayExprEvaluator::VisitCastExpr(const CastExpr *E) {
15580 const Expr *SE = E->getSubExpr();
15581
15582 switch (E->getCastKind()) {
15583 default:
15584 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15585 case CK_HLSLAggregateSplatCast: {
15586 APValue Val;
15587 QualType ValTy;
15588
15589 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15590 return false;
15591
15592 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
15593
15594 SmallVector<APValue> SplatEls(NEls, Val);
15595 SmallVector<QualType> SplatType(NEls, ValTy);
15596
15597 // cast the elements
15598 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15599 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
15600 ElTypes&: SplatType))
15601 return false;
15602
15603 return true;
15604 }
15605 case CK_HLSLElementwiseCast: {
15606 SmallVector<APValue> SrcEls;
15607 SmallVector<QualType> SrcTypes;
15608
15609 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals&: SrcEls, SrcTypes))
15610 return false;
15611
15612 // cast the elements
15613 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15614 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
15615 ElTypes&: SrcTypes))
15616 return false;
15617 return true;
15618 }
15619 }
15620}
15621
15622bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E,
15623 QualType AllocType) {
15624 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15625 T: AllocType.isNull() ? E->getType() : AllocType);
15626 if (!CAT)
15627 return Error(E);
15628
15629 // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
15630 // an appropriately-typed string literal enclosed in braces.
15631 if (E->isStringLiteralInit()) {
15632 auto *SL = dyn_cast<StringLiteral>(Val: E->getInit(Init: 0)->IgnoreParenImpCasts());
15633 // FIXME: Support ObjCEncodeExpr here once we support it in
15634 // ArrayExprEvaluator generally.
15635 if (!SL)
15636 return Error(E);
15637 return VisitStringLiteral(E: SL, AllocType);
15638 }
15639 // Any other transparent list init will need proper handling of the
15640 // AllocType; we can't just recurse to the inner initializer.
15641 assert(!E->isTransparent() &&
15642 "transparent array list initialization is not string literal init?");
15643
15644 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits(), ArrayFiller: E->getArrayFiller(),
15645 AllocType);
15646}
15647
15648bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
15649 const Expr *ExprToVisit, ArrayRef<Expr *> Args, const Expr *ArrayFiller,
15650 QualType AllocType) {
15651 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15652 T: AllocType.isNull() ? ExprToVisit->getType() : AllocType);
15653
15654 bool Success = true;
15655
15656 unsigned NumEltsToInit = Args.size();
15657 unsigned NumElts = CAT->getZExtSize();
15658
15659 // If the initializer might depend on the array index, run it for each
15660 // array element.
15661 if (NumEltsToInit != NumElts &&
15662 MaybeElementDependentArrayFiller(FillerExpr: ArrayFiller)) {
15663 NumEltsToInit = NumElts;
15664 } else {
15665 // Add additional elements represented by EmbedExpr.
15666 for (auto *Init : Args) {
15667 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts()))
15668 NumEltsToInit += EmbedS->getDataElementCount() - 1;
15669 }
15670 // If we have extra elements in the list, they will be discarded.
15671 if (NumEltsToInit > NumElts)
15672 NumEltsToInit = NumElts;
15673 // If we're overwriting memory which already has an object, make sure we
15674 // don't reduce the number of non-filler elements. (It's possible to
15675 // optimize this in some cases, but the logic gets really complicated.)
15676 if (Result.hasValue() && NumEltsToInit < Result.getArrayInitializedElts())
15677 NumEltsToInit = Result.getArrayInitializedElts();
15678 }
15679
15680 LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
15681 << NumEltsToInit << ".\n");
15682
15683 if (!Result.hasValue()) {
15684 Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15685 } else if (Result.getArrayInitializedElts() != NumEltsToInit) {
15686 // Number of inititalized elts changed. Recreate the APValue, and copy over
15687 // the relevant elements. (This is essentially just fixing the internal
15688 // representation of the value, because it's tied to the number of
15689 // non-filler elements.)
15690 //
15691 // This should be hit rarely, but there are some edge cases:
15692 //
15693 // - The array could be zero-initialized.
15694 // - There could be a DesignatedInitListExpr.
15695 // - operator new[] can be used to start the lifetime early.
15696 APValue NewResult = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15697 // First copy existing elements.
15698 unsigned NumOldElts = Result.getArrayInitializedElts();
15699 for (unsigned I = 0; I < NumOldElts; ++I) {
15700 NewResult.getArrayInitializedElt(I) =
15701 std::move(Result.getArrayInitializedElt(I));
15702 }
15703 // Then copy the array filler over the remaining elements.
15704 for (unsigned I = Result.getArrayInitializedElts(); I < NumEltsToInit; ++I)
15705 NewResult.getArrayInitializedElt(I) = Result.getArrayFiller();
15706 if (NewResult.hasArrayFiller() && Result.hasArrayFiller())
15707 NewResult.getArrayFiller() = Result.getArrayFiller();
15708 Result = std::move(NewResult);
15709 }
15710
15711 LValue Subobject = This;
15712 Subobject.addArray(Info, E: ExprToVisit, CAT);
15713 auto Eval = [&](const Expr *Init, unsigned ArrayIndex) {
15714 if (Init->isValueDependent())
15715 return EvaluateDependentExpr(E: Init, Info);
15716
15717 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
15718 // aren't supposed to be modified.
15719 if (isa<NoInitExpr>(Val: Init))
15720 return true;
15721
15722 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: ArrayIndex), Info,
15723 This: Subobject, E: Init) ||
15724 !HandleLValueArrayAdjustment(Info, E: Init, LVal&: Subobject,
15725 EltTy: CAT->getElementType(), Adjustment: 1)) {
15726 if (!Info.noteFailure())
15727 return false;
15728 Success = false;
15729 }
15730 return true;
15731 };
15732 unsigned ArrayIndex = 0;
15733 QualType DestTy = CAT->getElementType();
15734 APSInt Value(Info.Ctx.getTypeSize(T: DestTy), DestTy->isUnsignedIntegerType());
15735 for (unsigned Index = 0; Index != NumEltsToInit; ++Index) {
15736 const Expr *Init = Index < Args.size() ? Args[Index] : ArrayFiller;
15737 if (ArrayIndex >= NumEltsToInit)
15738 break;
15739 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) {
15740 StringLiteral *SL = EmbedS->getDataStringLiteral();
15741 for (unsigned I = EmbedS->getStartingElementPos(),
15742 N = EmbedS->getDataElementCount();
15743 I != EmbedS->getStartingElementPos() + N; ++I) {
15744 Value = SL->getCodeUnit(i: I);
15745 if (DestTy->isIntegerType()) {
15746 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(Value);
15747 } else {
15748 assert(DestTy->isFloatingType() && "unexpected type");
15749 const FPOptions FPO =
15750 Init->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15751 APFloat FValue(0.0);
15752 if (!HandleIntToFloatCast(Info, E: Init, FPO, SrcType: EmbedS->getType(), Value,
15753 DestType: DestTy, Result&: FValue))
15754 return false;
15755 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(FValue);
15756 }
15757 ArrayIndex++;
15758 }
15759 } else {
15760 if (!Eval(Init, ArrayIndex))
15761 return false;
15762 ++ArrayIndex;
15763 }
15764 }
15765
15766 if (!Result.hasArrayFiller())
15767 return Success;
15768
15769 // If we get here, we have a trivial filler, which we can just evaluate
15770 // once and splat over the rest of the array elements.
15771 assert(ArrayFiller && "no array filler for incomplete init list");
15772 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject,
15773 E: ArrayFiller) &&
15774 Success;
15775}
15776
15777bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
15778 LValue CommonLV;
15779 if (E->getCommonExpr() &&
15780 !Evaluate(Result&: Info.CurrentCall->createTemporary(
15781 Key: E->getCommonExpr(),
15782 T: getStorageType(Ctx: Info.Ctx, E: E->getCommonExpr()),
15783 Scope: ScopeKind::FullExpression, LV&: CommonLV),
15784 Info, E: E->getCommonExpr()->getSourceExpr()))
15785 return false;
15786
15787 auto *CAT = cast<ConstantArrayType>(Val: E->getType()->castAsArrayTypeUnsafe());
15788
15789 uint64_t Elements = CAT->getZExtSize();
15790 Result = APValue(APValue::UninitArray(), Elements, Elements);
15791
15792 LValue Subobject = This;
15793 Subobject.addArray(Info, E, CAT);
15794
15795 bool Success = true;
15796 for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) {
15797 // C++ [class.temporary]/5
15798 // There are four contexts in which temporaries are destroyed at a different
15799 // point than the end of the full-expression. [...] The second context is
15800 // when a copy constructor is called to copy an element of an array while
15801 // the entire array is copied [...]. In either case, if the constructor has
15802 // one or more default arguments, the destruction of every temporary created
15803 // in a default argument is sequenced before the construction of the next
15804 // array element, if any.
15805 FullExpressionRAII Scope(Info);
15806
15807 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index),
15808 Info, This: Subobject, E: E->getSubExpr()) ||
15809 !HandleLValueArrayAdjustment(Info, E, LVal&: Subobject,
15810 EltTy: CAT->getElementType(), Adjustment: 1)) {
15811 if (!Info.noteFailure())
15812 return false;
15813 Success = false;
15814 }
15815
15816 // Make sure we run the destructors too.
15817 Scope.destroy();
15818 }
15819
15820 return Success;
15821}
15822
15823bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
15824 return VisitCXXConstructExpr(E, Subobject: This, Value: &Result, Type: E->getType());
15825}
15826
15827bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
15828 const LValue &Subobject,
15829 APValue *Value,
15830 QualType Type) {
15831 bool HadZeroInit = Value->hasValue();
15832
15833 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: Type)) {
15834 unsigned FinalSize = CAT->getZExtSize();
15835
15836 // Preserve the array filler if we had prior zero-initialization.
15837 APValue Filler =
15838 HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller()
15839 : APValue();
15840
15841 *Value = APValue(APValue::UninitArray(), 0, FinalSize);
15842 if (FinalSize == 0)
15843 return true;
15844
15845 bool HasTrivialConstructor = CheckTrivialDefaultConstructor(
15846 Info, Loc: E->getExprLoc(), CD: E->getConstructor(),
15847 IsValueInitialization: E->requiresZeroInitialization());
15848 LValue ArrayElt = Subobject;
15849 ArrayElt.addArray(Info, E, CAT);
15850 // We do the whole initialization in two passes, first for just one element,
15851 // then for the whole array. It's possible we may find out we can't do const
15852 // init in the first pass, in which case we avoid allocating a potentially
15853 // large array. We don't do more passes because expanding array requires
15854 // copying the data, which is wasteful.
15855 for (const unsigned N : {1u, FinalSize}) {
15856 unsigned OldElts = Value->getArrayInitializedElts();
15857 if (OldElts == N)
15858 break;
15859
15860 // Expand the array to appropriate size.
15861 APValue NewValue(APValue::UninitArray(), N, FinalSize);
15862 for (unsigned I = 0; I < OldElts; ++I)
15863 NewValue.getArrayInitializedElt(I).swap(
15864 RHS&: Value->getArrayInitializedElt(I));
15865 Value->swap(RHS&: NewValue);
15866
15867 if (HadZeroInit)
15868 for (unsigned I = OldElts; I < N; ++I)
15869 Value->getArrayInitializedElt(I) = Filler;
15870
15871 if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) {
15872 // If we have a trivial constructor, only evaluate it once and copy
15873 // the result into all the array elements.
15874 APValue &FirstResult = Value->getArrayInitializedElt(I: 0);
15875 for (unsigned I = OldElts; I < FinalSize; ++I)
15876 Value->getArrayInitializedElt(I) = FirstResult;
15877 } else {
15878 for (unsigned I = OldElts; I < N; ++I) {
15879 if (!VisitCXXConstructExpr(E, Subobject: ArrayElt,
15880 Value: &Value->getArrayInitializedElt(I),
15881 Type: CAT->getElementType()) ||
15882 !HandleLValueArrayAdjustment(Info, E, LVal&: ArrayElt,
15883 EltTy: CAT->getElementType(), Adjustment: 1))
15884 return false;
15885 // When checking for const initilization any diagnostic is considered
15886 // an error.
15887 if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&
15888 !Info.keepEvaluatingAfterFailure())
15889 return false;
15890 }
15891 }
15892 }
15893
15894 return true;
15895 }
15896
15897 if (!Type->isRecordType())
15898 return Error(E);
15899
15900 return RecordExprEvaluator(Info, Subobject, *Value)
15901 .VisitCXXConstructExpr(E, T: Type);
15902}
15903
15904bool ArrayExprEvaluator::VisitCXXParenListInitExpr(
15905 const CXXParenListInitExpr *E) {
15906 assert(E->getType()->isConstantArrayType() &&
15907 "Expression result is not a constant array type");
15908
15909 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs(),
15910 ArrayFiller: E->getArrayFiller());
15911}
15912
15913bool ArrayExprEvaluator::VisitDesignatedInitUpdateExpr(
15914 const DesignatedInitUpdateExpr *E) {
15915 if (!Visit(S: E->getBase()))
15916 return false;
15917 return Visit(S: E->getUpdater());
15918}
15919
15920//===----------------------------------------------------------------------===//
15921// Integer Evaluation
15922//
15923// As a GNU extension, we support casting pointers to sufficiently-wide integer
15924// types and back in constant folding. Integer values are thus represented
15925// either as an integer-valued APValue, or as an lvalue-valued APValue.
15926//===----------------------------------------------------------------------===//
15927
15928namespace {
15929class IntExprEvaluator
15930 : public ExprEvaluatorBase<IntExprEvaluator> {
15931 APValue &Result;
15932public:
15933 IntExprEvaluator(EvalInfo &info, APValue &result)
15934 : ExprEvaluatorBaseTy(info), Result(result) {}
15935
15936 bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) {
15937 assert(E->getType()->isIntegralOrEnumerationType() &&
15938 "Invalid evaluation result.");
15939 assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
15940 "Invalid evaluation result.");
15941 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
15942 "Invalid evaluation result.");
15943 Result = APValue(SI);
15944 return true;
15945 }
15946 bool Success(const llvm::APSInt &SI, const Expr *E) {
15947 return Success(SI, E, Result);
15948 }
15949
15950 bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) {
15951 assert(E->getType()->isIntegralOrEnumerationType() &&
15952 "Invalid evaluation result.");
15953 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
15954 "Invalid evaluation result.");
15955 Result = APValue(APSInt(I));
15956 Result.getInt().setIsUnsigned(
15957 E->getType()->isUnsignedIntegerOrEnumerationType());
15958 return true;
15959 }
15960 bool Success(const llvm::APInt &I, const Expr *E) {
15961 return Success(I, E, Result);
15962 }
15963
15964 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
15965 assert(E->getType()->isIntegralOrEnumerationType() &&
15966 "Invalid evaluation result.");
15967 Result = APValue(Info.Ctx.MakeIntValue(Value, Type: E->getType()));
15968 return true;
15969 }
15970 bool Success(uint64_t Value, const Expr *E) {
15971 return Success(Value, E, Result);
15972 }
15973
15974 bool Success(CharUnits Size, const Expr *E) {
15975 return Success(Value: Size.getQuantity(), E);
15976 }
15977
15978 bool Success(const APValue &V, const Expr *E) {
15979 // C++23 [expr.const]p8 If we have a variable that is unknown reference or
15980 // pointer allow further evaluation of the value.
15981 if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate() ||
15982 V.allowConstexprUnknown()) {
15983 Result = V;
15984 return true;
15985 }
15986 return Success(SI: V.getInt(), E);
15987 }
15988
15989 bool ZeroInitialization(const Expr *E) { return Success(Value: 0, E); }
15990
15991 friend std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &,
15992 const CallExpr *);
15993
15994 //===--------------------------------------------------------------------===//
15995 // Visitor Methods
15996 //===--------------------------------------------------------------------===//
15997
15998 bool VisitIntegerLiteral(const IntegerLiteral *E) {
15999 return Success(I: E->getValue(), E);
16000 }
16001 bool VisitCharacterLiteral(const CharacterLiteral *E) {
16002 return Success(Value: E->getValue(), E);
16003 }
16004
16005 bool CheckReferencedDecl(const Expr *E, const Decl *D);
16006 bool VisitDeclRefExpr(const DeclRefExpr *E) {
16007 if (CheckReferencedDecl(E, D: E->getDecl()))
16008 return true;
16009
16010 return ExprEvaluatorBaseTy::VisitDeclRefExpr(S: E);
16011 }
16012 bool VisitMemberExpr(const MemberExpr *E) {
16013 if (CheckReferencedDecl(E, D: E->getMemberDecl())) {
16014 VisitIgnoredBaseExpression(E: E->getBase());
16015 return true;
16016 }
16017
16018 return ExprEvaluatorBaseTy::VisitMemberExpr(E);
16019 }
16020
16021 bool VisitCallExpr(const CallExpr *E);
16022 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
16023 bool VisitBinaryOperator(const BinaryOperator *E);
16024 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
16025 bool VisitUnaryOperator(const UnaryOperator *E);
16026
16027 bool VisitCastExpr(const CastExpr* E);
16028 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
16029
16030 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
16031 return Success(Value: E->getValue(), E);
16032 }
16033
16034 bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
16035 return Success(Value: E->getValue(), E);
16036 }
16037
16038 bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
16039 if (Info.ArrayInitIndex == uint64_t(-1)) {
16040 // We were asked to evaluate this subexpression independent of the
16041 // enclosing ArrayInitLoopExpr. We can't do that.
16042 Info.FFDiag(E);
16043 return false;
16044 }
16045 return Success(Value: Info.ArrayInitIndex, E);
16046 }
16047
16048 // Note, GNU defines __null as an integer, not a pointer.
16049 bool VisitGNUNullExpr(const GNUNullExpr *E) {
16050 return ZeroInitialization(E);
16051 }
16052
16053 bool VisitTypeTraitExpr(const TypeTraitExpr *E) {
16054 if (E->isStoredAsBoolean())
16055 return Success(Value: E->getBoolValue(), E);
16056 if (E->getAPValue().isAbsent())
16057 return false;
16058 assert(E->getAPValue().isInt() && "APValue type not supported");
16059 return Success(SI: E->getAPValue().getInt(), E);
16060 }
16061
16062 bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
16063 return Success(Value: E->getValue(), E);
16064 }
16065
16066 bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
16067 return Success(Value: E->getValue(), E);
16068 }
16069
16070 bool VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *E) {
16071 // This should not be evaluated during constant expr evaluation, as it
16072 // should always be in an unevaluated context (the args list of a 'gang' or
16073 // 'tile' clause).
16074 return Error(E);
16075 }
16076
16077 bool VisitUnaryReal(const UnaryOperator *E);
16078 bool VisitUnaryImag(const UnaryOperator *E);
16079
16080 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
16081 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
16082 bool VisitSourceLocExpr(const SourceLocExpr *E);
16083 bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);
16084 bool VisitRequiresExpr(const RequiresExpr *E);
16085 // FIXME: Missing: array subscript of vector, member of vector
16086};
16087
16088class FixedPointExprEvaluator
16089 : public ExprEvaluatorBase<FixedPointExprEvaluator> {
16090 APValue &Result;
16091
16092 public:
16093 FixedPointExprEvaluator(EvalInfo &info, APValue &result)
16094 : ExprEvaluatorBaseTy(info), Result(result) {}
16095
16096 bool Success(const llvm::APInt &I, const Expr *E) {
16097 return Success(
16098 V: APFixedPoint(I, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
16099 }
16100
16101 bool Success(uint64_t Value, const Expr *E) {
16102 return Success(
16103 V: APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
16104 }
16105
16106 bool Success(const APValue &V, const Expr *E) {
16107 return Success(V: V.getFixedPoint(), E);
16108 }
16109
16110 bool Success(const APFixedPoint &V, const Expr *E) {
16111 assert(E->getType()->isFixedPointType() && "Invalid evaluation result.");
16112 assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16113 "Invalid evaluation result.");
16114 Result = APValue(V);
16115 return true;
16116 }
16117
16118 bool ZeroInitialization(const Expr *E) {
16119 return Success(Value: 0, E);
16120 }
16121
16122 //===--------------------------------------------------------------------===//
16123 // Visitor Methods
16124 //===--------------------------------------------------------------------===//
16125
16126 bool VisitFixedPointLiteral(const FixedPointLiteral *E) {
16127 return Success(I: E->getValue(), E);
16128 }
16129
16130 bool VisitCastExpr(const CastExpr *E);
16131 bool VisitUnaryOperator(const UnaryOperator *E);
16132 bool VisitBinaryOperator(const BinaryOperator *E);
16133};
16134} // end anonymous namespace
16135
16136/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
16137/// produce either the integer value or a pointer.
16138///
16139/// GCC has a heinous extension which folds casts between pointer types and
16140/// pointer-sized integral types. We support this by allowing the evaluation of
16141/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
16142/// Some simple arithmetic on such values is supported (they are treated much
16143/// like char*).
16144static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
16145 EvalInfo &Info) {
16146 assert(!E->isValueDependent());
16147 assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType());
16148 return IntExprEvaluator(Info, Result).Visit(S: E);
16149}
16150
16151static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
16152 assert(!E->isValueDependent());
16153 APValue Val;
16154 if (!EvaluateIntegerOrLValue(E, Result&: Val, Info))
16155 return false;
16156 if (!Val.isInt()) {
16157 // FIXME: It would be better to produce the diagnostic for casting
16158 // a pointer to an integer.
16159 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
16160 return false;
16161 }
16162 Result = Val.getInt();
16163 return true;
16164}
16165
16166bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) {
16167 APValue Evaluated = E->EvaluateInContext(
16168 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
16169 return Success(V: Evaluated, E);
16170}
16171
16172static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
16173 EvalInfo &Info) {
16174 assert(!E->isValueDependent());
16175 if (E->getType()->isFixedPointType()) {
16176 APValue Val;
16177 if (!FixedPointExprEvaluator(Info, Val).Visit(S: E))
16178 return false;
16179 if (!Val.isFixedPoint())
16180 return false;
16181
16182 Result = Val.getFixedPoint();
16183 return true;
16184 }
16185 return false;
16186}
16187
16188static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
16189 EvalInfo &Info) {
16190 assert(!E->isValueDependent());
16191 if (E->getType()->isIntegerType()) {
16192 auto FXSema = Info.Ctx.getFixedPointSemantics(Ty: E->getType());
16193 APSInt Val;
16194 if (!EvaluateInteger(E, Result&: Val, Info))
16195 return false;
16196 Result = APFixedPoint(Val, FXSema);
16197 return true;
16198 } else if (E->getType()->isFixedPointType()) {
16199 return EvaluateFixedPoint(E, Result, Info);
16200 }
16201 return false;
16202}
16203
16204/// Check whether the given declaration can be directly converted to an integral
16205/// rvalue. If not, no diagnostic is produced; there are other things we can
16206/// try.
16207bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
16208 // Enums are integer constant exprs.
16209 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(Val: D)) {
16210 // Check for signedness/width mismatches between E type and ECD value.
16211 bool SameSign = (ECD->getInitVal().isSigned()
16212 == E->getType()->isSignedIntegerOrEnumerationType());
16213 bool SameWidth = (ECD->getInitVal().getBitWidth()
16214 == Info.Ctx.getIntWidth(T: E->getType()));
16215 if (SameSign && SameWidth)
16216 return Success(SI: ECD->getInitVal(), E);
16217 else {
16218 // Get rid of mismatch (otherwise Success assertions will fail)
16219 // by computing a new value matching the type of E.
16220 llvm::APSInt Val = ECD->getInitVal();
16221 if (!SameSign)
16222 Val.setIsSigned(!ECD->getInitVal().isSigned());
16223 if (!SameWidth)
16224 Val = Val.extOrTrunc(width: Info.Ctx.getIntWidth(T: E->getType()));
16225 return Success(SI: Val, E);
16226 }
16227 }
16228 return false;
16229}
16230
16231/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16232/// as GCC.
16233GCCTypeClass EvaluateBuiltinClassifyType(QualType T,
16234 const LangOptions &LangOpts) {
16235 assert(!T->isDependentType() && "unexpected dependent type");
16236
16237 QualType CanTy = T.getCanonicalType();
16238
16239 switch (CanTy->getTypeClass()) {
16240#define TYPE(ID, BASE)
16241#define DEPENDENT_TYPE(ID, BASE) case Type::ID:
16242#define NON_CANONICAL_TYPE(ID, BASE) case Type::ID:
16243#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID:
16244#include "clang/AST/TypeNodes.inc"
16245 case Type::Auto:
16246 case Type::DeducedTemplateSpecialization:
16247 llvm_unreachable("unexpected non-canonical or dependent type");
16248
16249 case Type::Builtin:
16250 switch (cast<BuiltinType>(Val&: CanTy)->getKind()) {
16251#define BUILTIN_TYPE(ID, SINGLETON_ID)
16252#define SIGNED_TYPE(ID, SINGLETON_ID) \
16253 case BuiltinType::ID: return GCCTypeClass::Integer;
16254#define FLOATING_TYPE(ID, SINGLETON_ID) \
16255 case BuiltinType::ID: return GCCTypeClass::RealFloat;
16256#define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \
16257 case BuiltinType::ID: break;
16258#include "clang/AST/BuiltinTypes.def"
16259 case BuiltinType::Void:
16260 return GCCTypeClass::Void;
16261
16262 case BuiltinType::Bool:
16263 return GCCTypeClass::Bool;
16264
16265 case BuiltinType::Char_U:
16266 case BuiltinType::UChar:
16267 case BuiltinType::WChar_U:
16268 case BuiltinType::Char8:
16269 case BuiltinType::Char16:
16270 case BuiltinType::Char32:
16271 case BuiltinType::UShort:
16272 case BuiltinType::UInt:
16273 case BuiltinType::ULong:
16274 case BuiltinType::ULongLong:
16275 case BuiltinType::UInt128:
16276 return GCCTypeClass::Integer;
16277
16278 case BuiltinType::UShortAccum:
16279 case BuiltinType::UAccum:
16280 case BuiltinType::ULongAccum:
16281 case BuiltinType::UShortFract:
16282 case BuiltinType::UFract:
16283 case BuiltinType::ULongFract:
16284 case BuiltinType::SatUShortAccum:
16285 case BuiltinType::SatUAccum:
16286 case BuiltinType::SatULongAccum:
16287 case BuiltinType::SatUShortFract:
16288 case BuiltinType::SatUFract:
16289 case BuiltinType::SatULongFract:
16290 return GCCTypeClass::None;
16291
16292 case BuiltinType::NullPtr:
16293
16294 case BuiltinType::ObjCId:
16295 case BuiltinType::ObjCClass:
16296 case BuiltinType::ObjCSel:
16297#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
16298 case BuiltinType::Id:
16299#include "clang/Basic/OpenCLImageTypes.def"
16300#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
16301 case BuiltinType::Id:
16302#include "clang/Basic/OpenCLExtensionTypes.def"
16303 case BuiltinType::OCLSampler:
16304 case BuiltinType::OCLEvent:
16305 case BuiltinType::OCLClkEvent:
16306 case BuiltinType::OCLQueue:
16307 case BuiltinType::OCLReserveID:
16308#define SVE_TYPE(Name, Id, SingletonId) \
16309 case BuiltinType::Id:
16310#include "clang/Basic/AArch64ACLETypes.def"
16311#define PPC_VECTOR_TYPE(Name, Id, Size) \
16312 case BuiltinType::Id:
16313#include "clang/Basic/PPCTypes.def"
16314#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16315#include "clang/Basic/RISCVVTypes.def"
16316#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16317#include "clang/Basic/WebAssemblyReferenceTypes.def"
16318#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
16319#include "clang/Basic/AMDGPUTypes.def"
16320#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16321#include "clang/Basic/HLSLIntangibleTypes.def"
16322 return GCCTypeClass::None;
16323
16324 case BuiltinType::Dependent:
16325 llvm_unreachable("unexpected dependent type");
16326 };
16327 llvm_unreachable("unexpected placeholder type");
16328
16329 case Type::Enum:
16330 return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer;
16331
16332 case Type::Pointer:
16333 case Type::ConstantArray:
16334 case Type::VariableArray:
16335 case Type::IncompleteArray:
16336 case Type::FunctionNoProto:
16337 case Type::FunctionProto:
16338 case Type::ArrayParameter:
16339 return GCCTypeClass::Pointer;
16340
16341 case Type::MemberPointer:
16342 return CanTy->isMemberDataPointerType()
16343 ? GCCTypeClass::PointerToDataMember
16344 : GCCTypeClass::PointerToMemberFunction;
16345
16346 case Type::Complex:
16347 return GCCTypeClass::Complex;
16348
16349 case Type::Record:
16350 return CanTy->isUnionType() ? GCCTypeClass::Union
16351 : GCCTypeClass::ClassOrStruct;
16352
16353 case Type::Atomic:
16354 // GCC classifies _Atomic T the same as T.
16355 return EvaluateBuiltinClassifyType(
16356 T: CanTy->castAs<AtomicType>()->getValueType(), LangOpts);
16357
16358 case Type::Vector:
16359 case Type::ExtVector:
16360 return GCCTypeClass::Vector;
16361
16362 case Type::BlockPointer:
16363 case Type::ConstantMatrix:
16364 case Type::ObjCObject:
16365 case Type::ObjCInterface:
16366 case Type::ObjCObjectPointer:
16367 case Type::Pipe:
16368 case Type::HLSLAttributedResource:
16369 case Type::HLSLInlineSpirv:
16370 case Type::OverflowBehavior:
16371 // Classify all other types that don't fit into the regular
16372 // classification the same way.
16373 return GCCTypeClass::None;
16374
16375 case Type::BitInt:
16376 return GCCTypeClass::BitInt;
16377
16378 case Type::LValueReference:
16379 case Type::RValueReference:
16380 llvm_unreachable("invalid type for expression");
16381 }
16382
16383 llvm_unreachable("unexpected type class");
16384}
16385
16386/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16387/// as GCC.
16388static GCCTypeClass
16389EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) {
16390 // If no argument was supplied, default to None. This isn't
16391 // ideal, however it is what gcc does.
16392 if (E->getNumArgs() == 0)
16393 return GCCTypeClass::None;
16394
16395 // FIXME: Bizarrely, GCC treats a call with more than one argument as not
16396 // being an ICE, but still folds it to a constant using the type of the first
16397 // argument.
16398 return EvaluateBuiltinClassifyType(T: E->getArg(Arg: 0)->getType(), LangOpts);
16399}
16400
16401/// EvaluateBuiltinConstantPForLValue - Determine the result of
16402/// __builtin_constant_p when applied to the given pointer.
16403///
16404/// A pointer is only "constant" if it is null (or a pointer cast to integer)
16405/// or it points to the first character of a string literal.
16406static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) {
16407 APValue::LValueBase Base = LV.getLValueBase();
16408 if (Base.isNull()) {
16409 // A null base is acceptable.
16410 return true;
16411 } else if (const Expr *E = Base.dyn_cast<const Expr *>()) {
16412 if (!isa<StringLiteral>(Val: E))
16413 return false;
16414 return LV.getLValueOffset().isZero();
16415 } else if (Base.is<TypeInfoLValue>()) {
16416 // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to
16417 // evaluate to true.
16418 return true;
16419 } else {
16420 // Any other base is not constant enough for GCC.
16421 return false;
16422 }
16423}
16424
16425/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
16426/// GCC as we can manage.
16427static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) {
16428 // This evaluation is not permitted to have side-effects, so evaluate it in
16429 // a speculative evaluation context.
16430 SpeculativeEvaluationRAII SpeculativeEval(Info);
16431
16432 // Constant-folding is always enabled for the operand of __builtin_constant_p
16433 // (even when the enclosing evaluation context otherwise requires a strict
16434 // language-specific constant expression).
16435 FoldConstant Fold(Info, true);
16436
16437 QualType ArgType = Arg->getType();
16438
16439 // __builtin_constant_p always has one operand. The rules which gcc follows
16440 // are not precisely documented, but are as follows:
16441 //
16442 // - If the operand is of integral, floating, complex or enumeration type,
16443 // and can be folded to a known value of that type, it returns 1.
16444 // - If the operand can be folded to a pointer to the first character
16445 // of a string literal (or such a pointer cast to an integral type)
16446 // or to a null pointer or an integer cast to a pointer, it returns 1.
16447 //
16448 // Otherwise, it returns 0.
16449 //
16450 // FIXME: GCC also intends to return 1 for literals of aggregate types, but
16451 // its support for this did not work prior to GCC 9 and is not yet well
16452 // understood.
16453 if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() ||
16454 ArgType->isAnyComplexType() || ArgType->isPointerType() ||
16455 ArgType->isNullPtrType()) {
16456 APValue V;
16457 if (!::EvaluateAsRValue(Info, E: Arg, Result&: V) || Info.EvalStatus.HasSideEffects) {
16458 Fold.keepDiagnostics();
16459 return false;
16460 }
16461
16462 // For a pointer (possibly cast to integer), there are special rules.
16463 if (V.getKind() == APValue::LValue)
16464 return EvaluateBuiltinConstantPForLValue(LV: V);
16465
16466 // Otherwise, any constant value is good enough.
16467 return V.hasValue();
16468 }
16469
16470 // Anything else isn't considered to be sufficiently constant.
16471 return false;
16472}
16473
16474/// Retrieves the "underlying object type" of the given expression,
16475/// as used by __builtin_object_size.
16476static QualType getObjectType(APValue::LValueBase B) {
16477 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
16478 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
16479 return VD->getType();
16480 } else if (const Expr *E = B.dyn_cast<const Expr*>()) {
16481 if (isa<CompoundLiteralExpr>(Val: E))
16482 return E->getType();
16483 } else if (B.is<TypeInfoLValue>()) {
16484 return B.getTypeInfoType();
16485 } else if (B.is<DynamicAllocLValue>()) {
16486 return B.getDynamicAllocType();
16487 }
16488
16489 return QualType();
16490}
16491
16492/// A more selective version of E->IgnoreParenCasts for
16493/// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only
16494/// to change the type of E.
16495/// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo`
16496///
16497/// Always returns an RValue with a pointer representation.
16498static const Expr *ignorePointerCastsAndParens(const Expr *E) {
16499 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
16500
16501 const Expr *NoParens = E->IgnoreParens();
16502 const auto *Cast = dyn_cast<CastExpr>(Val: NoParens);
16503 if (Cast == nullptr)
16504 return NoParens;
16505
16506 // We only conservatively allow a few kinds of casts, because this code is
16507 // inherently a simple solution that seeks to support the common case.
16508 auto CastKind = Cast->getCastKind();
16509 if (CastKind != CK_NoOp && CastKind != CK_BitCast &&
16510 CastKind != CK_AddressSpaceConversion)
16511 return NoParens;
16512
16513 const auto *SubExpr = Cast->getSubExpr();
16514 if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue())
16515 return NoParens;
16516 return ignorePointerCastsAndParens(E: SubExpr);
16517}
16518
16519/// Checks to see if the given LValue's Designator is at the end of the LValue's
16520/// record layout. e.g.
16521/// struct { struct { int a, b; } fst, snd; } obj;
16522/// obj.fst // no
16523/// obj.snd // yes
16524/// obj.fst.a // no
16525/// obj.fst.b // no
16526/// obj.snd.a // no
16527/// obj.snd.b // yes
16528///
16529/// Please note: this function is specialized for how __builtin_object_size
16530/// views "objects".
16531///
16532/// If this encounters an invalid RecordDecl or otherwise cannot determine the
16533/// correct result, it will always return true.
16534static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) {
16535 assert(!LVal.Designator.Invalid);
16536
16537 auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD) {
16538 const RecordDecl *Parent = FD->getParent();
16539 if (Parent->isInvalidDecl() || Parent->isUnion())
16540 return true;
16541 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(D: Parent);
16542 return FD->getFieldIndex() + 1 == Layout.getFieldCount();
16543 };
16544
16545 auto &Base = LVal.getLValueBase();
16546 if (auto *ME = dyn_cast_or_null<MemberExpr>(Val: Base.dyn_cast<const Expr *>())) {
16547 if (auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl())) {
16548 if (!IsLastOrInvalidFieldDecl(FD))
16549 return false;
16550 } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(Val: ME->getMemberDecl())) {
16551 for (auto *FD : IFD->chain()) {
16552 if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(Val: FD)))
16553 return false;
16554 }
16555 }
16556 }
16557
16558 unsigned I = 0;
16559 QualType BaseType = getType(B: Base);
16560 if (LVal.Designator.FirstEntryIsAnUnsizedArray) {
16561 // If we don't know the array bound, conservatively assume we're looking at
16562 // the final array element.
16563 ++I;
16564 if (BaseType->isIncompleteArrayType())
16565 BaseType = Ctx.getAsArrayType(T: BaseType)->getElementType();
16566 else
16567 BaseType = BaseType->castAs<PointerType>()->getPointeeType();
16568 }
16569
16570 for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) {
16571 const auto &Entry = LVal.Designator.Entries[I];
16572 if (BaseType->isArrayType()) {
16573 // Because __builtin_object_size treats arrays as objects, we can ignore
16574 // the index iff this is the last array in the Designator.
16575 if (I + 1 == E)
16576 return true;
16577 const auto *CAT = cast<ConstantArrayType>(Val: Ctx.getAsArrayType(T: BaseType));
16578 uint64_t Index = Entry.getAsArrayIndex();
16579 if (Index + 1 != CAT->getZExtSize())
16580 return false;
16581 BaseType = CAT->getElementType();
16582 } else if (BaseType->isAnyComplexType()) {
16583 const auto *CT = BaseType->castAs<ComplexType>();
16584 uint64_t Index = Entry.getAsArrayIndex();
16585 if (Index != 1)
16586 return false;
16587 BaseType = CT->getElementType();
16588 } else if (auto *FD = getAsField(E: Entry)) {
16589 if (!IsLastOrInvalidFieldDecl(FD))
16590 return false;
16591 BaseType = FD->getType();
16592 } else {
16593 assert(getAsBaseClass(Entry) && "Expecting cast to a base class");
16594 return false;
16595 }
16596 }
16597 return true;
16598}
16599
16600/// Tests to see if the LValue has a user-specified designator (that isn't
16601/// necessarily valid). Note that this always returns 'true' if the LValue has
16602/// an unsized array as its first designator entry, because there's currently no
16603/// way to tell if the user typed *foo or foo[0].
16604static bool refersToCompleteObject(const LValue &LVal) {
16605 if (LVal.Designator.Invalid)
16606 return false;
16607
16608 if (!LVal.Designator.Entries.empty())
16609 return LVal.Designator.isMostDerivedAnUnsizedArray();
16610
16611 if (!LVal.InvalidBase)
16612 return true;
16613
16614 // If `E` is a MemberExpr, then the first part of the designator is hiding in
16615 // the LValueBase.
16616 const auto *E = LVal.Base.dyn_cast<const Expr *>();
16617 return !E || !isa<MemberExpr>(Val: E);
16618}
16619
16620/// Attempts to detect a user writing into a piece of memory that's impossible
16621/// to figure out the size of by just using types.
16622static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) {
16623 const SubobjectDesignator &Designator = LVal.Designator;
16624 // Notes:
16625 // - Users can only write off of the end when we have an invalid base. Invalid
16626 // bases imply we don't know where the memory came from.
16627 // - We used to be a bit more aggressive here; we'd only be conservative if
16628 // the array at the end was flexible, or if it had 0 or 1 elements. This
16629 // broke some common standard library extensions (PR30346), but was
16630 // otherwise seemingly fine. It may be useful to reintroduce this behavior
16631 // with some sort of list. OTOH, it seems that GCC is always
16632 // conservative with the last element in structs (if it's an array), so our
16633 // current behavior is more compatible than an explicit list approach would
16634 // be.
16635 auto isFlexibleArrayMember = [&] {
16636 using FAMKind = LangOptions::StrictFlexArraysLevelKind;
16637 FAMKind StrictFlexArraysLevel =
16638 Ctx.getLangOpts().getStrictFlexArraysLevel();
16639
16640 if (Designator.isMostDerivedAnUnsizedArray())
16641 return true;
16642
16643 if (StrictFlexArraysLevel == FAMKind::Default)
16644 return true;
16645
16646 if (Designator.getMostDerivedArraySize() == 0 &&
16647 StrictFlexArraysLevel != FAMKind::IncompleteOnly)
16648 return true;
16649
16650 if (Designator.getMostDerivedArraySize() == 1 &&
16651 StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete)
16652 return true;
16653
16654 return false;
16655 };
16656
16657 return LVal.InvalidBase &&
16658 Designator.Entries.size() == Designator.MostDerivedPathLength &&
16659 Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() &&
16660 isDesignatorAtObjectEnd(Ctx, LVal);
16661}
16662
16663/// Converts the given APInt to CharUnits, assuming the APInt is unsigned.
16664/// Fails if the conversion would cause loss of precision.
16665static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int,
16666 CharUnits &Result) {
16667 auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max();
16668 if (Int.ugt(RHS: CharUnitsMax))
16669 return false;
16670 Result = CharUnits::fromQuantity(Quantity: Int.getZExtValue());
16671 return true;
16672}
16673
16674/// If we're evaluating the object size of an instance of a struct that
16675/// contains a flexible array member, add the size of the initializer.
16676static void addFlexibleArrayMemberInitSize(EvalInfo &Info, const QualType &T,
16677 const LValue &LV, CharUnits &Size) {
16678 if (!T.isNull() && T->isStructureType() &&
16679 T->castAsRecordDecl()->hasFlexibleArrayMember())
16680 if (const auto *V = LV.getLValueBase().dyn_cast<const ValueDecl *>())
16681 if (const auto *VD = dyn_cast<VarDecl>(Val: V))
16682 if (VD->hasInit())
16683 Size += VD->getFlexibleArrayInitChars(Ctx: Info.Ctx);
16684}
16685
16686/// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will
16687/// determine how many bytes exist from the beginning of the object to either
16688/// the end of the current subobject, or the end of the object itself, depending
16689/// on what the LValue looks like + the value of Type.
16690///
16691/// If this returns false, the value of Result is undefined.
16692static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc,
16693 unsigned Type, const LValue &LVal,
16694 CharUnits &EndOffset) {
16695 bool DetermineForCompleteObject = refersToCompleteObject(LVal);
16696
16697 auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) {
16698 if (Ty.isNull())
16699 return false;
16700
16701 Ty = Ty.getNonReferenceType();
16702
16703 if (Ty->isIncompleteType() || Ty->isFunctionType())
16704 return false;
16705
16706 return HandleSizeof(Info, Loc: ExprLoc, Type: Ty, Size&: Result);
16707 };
16708
16709 // We want to evaluate the size of the entire object. This is a valid fallback
16710 // for when Type=1 and the designator is invalid, because we're asked for an
16711 // upper-bound.
16712 if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) {
16713 // Type=3 wants a lower bound, so we can't fall back to this.
16714 if (Type == 3 && !DetermineForCompleteObject)
16715 return false;
16716
16717 llvm::APInt APEndOffset;
16718 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
16719 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
16720 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
16721
16722 if (LVal.InvalidBase)
16723 return false;
16724
16725 QualType BaseTy = getObjectType(B: LVal.getLValueBase());
16726 const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset);
16727 addFlexibleArrayMemberInitSize(Info, T: BaseTy, LV: LVal, Size&: EndOffset);
16728 return Ret;
16729 }
16730
16731 // We want to evaluate the size of a subobject.
16732 const SubobjectDesignator &Designator = LVal.Designator;
16733
16734 // The following is a moderately common idiom in C:
16735 //
16736 // struct Foo { int a; char c[1]; };
16737 // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar));
16738 // strcpy(&F->c[0], Bar);
16739 //
16740 // In order to not break too much legacy code, we need to support it.
16741 if (isUserWritingOffTheEnd(Ctx: Info.Ctx, LVal)) {
16742 // If we can resolve this to an alloc_size call, we can hand that back,
16743 // because we know for certain how many bytes there are to write to.
16744 llvm::APInt APEndOffset;
16745 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
16746 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
16747 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
16748
16749 // If we cannot determine the size of the initial allocation, then we can't
16750 // given an accurate upper-bound. However, we are still able to give
16751 // conservative lower-bounds for Type=3.
16752 if (Type == 1)
16753 return false;
16754 }
16755
16756 CharUnits BytesPerElem;
16757 if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem))
16758 return false;
16759
16760 // According to the GCC documentation, we want the size of the subobject
16761 // denoted by the pointer. But that's not quite right -- what we actually
16762 // want is the size of the immediately-enclosing array, if there is one.
16763 int64_t ElemsRemaining;
16764 if (Designator.MostDerivedIsArrayElement &&
16765 Designator.Entries.size() == Designator.MostDerivedPathLength) {
16766 uint64_t ArraySize = Designator.getMostDerivedArraySize();
16767 uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex();
16768 ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex;
16769 } else {
16770 ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1;
16771 }
16772
16773 EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining;
16774 return true;
16775}
16776
16777/// Tries to evaluate the __builtin_object_size for @p E. If successful,
16778/// returns true and stores the result in @p Size.
16779///
16780/// If @p WasError is non-null, this will report whether the failure to evaluate
16781/// is to be treated as an Error in IntExprEvaluator.
16782///
16783/// If @p IsDynamic is true (i.e. we're evaluating
16784/// __builtin_dynamic_object_size) and the operand designates a flexible array
16785/// member annotated with 'counted_by', we refuse to fold so that IR generation
16786/// can emit the count-based runtime size computation.
16787static std::optional<uint64_t>
16788tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, EvalInfo &Info,
16789 bool IsDynamic = false) {
16790
16791 // Determine the denoted object.
16792 LValue LVal;
16793 {
16794 // The operand of __builtin_object_size is never evaluated for side-effects.
16795 // If there are any, but we can determine the pointed-to object anyway, then
16796 // ignore the side-effects.
16797 SpeculativeEvaluationRAII SpeculativeEval(Info);
16798 IgnoreSideEffectsRAII Fold(Info);
16799
16800 if (E->isGLValue()) {
16801 // It's possible for us to be given GLValues if we're called via
16802 // Expr::tryEvaluateObjectSize.
16803 APValue RVal;
16804 if (!EvaluateAsRValue(Info, E, Result&: RVal))
16805 return std::nullopt;
16806 LVal.setFrom(Ctx: Info.Ctx, V: RVal);
16807 } else if (!EvaluatePointer(E: ignorePointerCastsAndParens(E), Result&: LVal, Info,
16808 /*InvalidBaseOK=*/true))
16809 return std::nullopt;
16810 }
16811
16812 // If we point to before the start of the object, there are no accessible
16813 // bytes.
16814 if (LVal.getLValueOffset().isNegative())
16815 return 0;
16816
16817 // For __builtin_dynamic_object_size on a counted_by-annotated flexible
16818 // array member, defer to IR generation (emitCountedBySize in CGBuiltin):
16819 // its runtime computation uses the live 'count' field and is more accurate
16820 // than the layout/initializer-derived size we'd produce here. Use the same
16821 // findStructFieldAccess form-recognition CGBuiltin does, so we refuse to
16822 // fold on exactly the shapes that path handles (and, importantly, *not*
16823 // on '&af.fam' which designates the array-as-a-whole and stays on the
16824 // layout-derived path to match GCC). Checked after the negative-offset
16825 // early return above so that obviously out-of-bounds operands still fold
16826 // to 0, preserving existing behavior.
16827 if (IsDynamic) {
16828 const auto *ME = dyn_cast_or_null<MemberExpr>(Val: findStructFieldAccess(E));
16829 const auto *FD = ME ? dyn_cast<FieldDecl>(Val: ME->getMemberDecl()) : nullptr;
16830 if (FD && FD->getType()->isCountAttributedType())
16831 return std::nullopt;
16832 }
16833
16834 CharUnits EndOffset;
16835 if (!determineEndOffset(Info, ExprLoc: E->getExprLoc(), Type, LVal, EndOffset))
16836 return std::nullopt;
16837
16838 // If we've fallen outside of the end offset, just pretend there's nothing to
16839 // write to/read from.
16840 if (EndOffset <= LVal.getLValueOffset())
16841 return 0;
16842 return (EndOffset - LVal.getLValueOffset()).getQuantity();
16843}
16844
16845bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
16846 if (!IsConstantEvaluatedBuiltinCall(E))
16847 return ExprEvaluatorBaseTy::VisitCallExpr(E);
16848 return VisitBuiltinCallExpr(E, BuiltinOp: ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E));
16849}
16850
16851static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info,
16852 APValue &Val, APSInt &Alignment) {
16853 QualType SrcTy = E->getArg(Arg: 0)->getType();
16854 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: SrcTy, Info, Alignment))
16855 return false;
16856 // Even though we are evaluating integer expressions we could get a pointer
16857 // argument for the __builtin_is_aligned() case.
16858 if (SrcTy->isPointerType()) {
16859 LValue Ptr;
16860 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Ptr, Info))
16861 return false;
16862 Ptr.moveInto(V&: Val);
16863 } else if (!SrcTy->isIntegralOrEnumerationType()) {
16864 Info.FFDiag(E: E->getArg(Arg: 0));
16865 return false;
16866 } else {
16867 APSInt SrcInt;
16868 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SrcInt, Info))
16869 return false;
16870 assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() &&
16871 "Bit widths must be the same");
16872 Val = APValue(SrcInt);
16873 }
16874 assert(Val.hasValue());
16875 return true;
16876}
16877
16878bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
16879 unsigned BuiltinOp) {
16880 auto EvalTestOp = [&](llvm::function_ref<bool(const APInt &, const APInt &)>
16881 Fn) {
16882 APValue SourceLHS, SourceRHS;
16883 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
16884 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
16885 return false;
16886
16887 unsigned SourceLen = SourceLHS.getVectorLength();
16888 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
16889 QualType ElemQT = VT->getElementType();
16890 unsigned LaneWidth = Info.Ctx.getTypeSize(T: ElemQT);
16891
16892 APInt AWide(LaneWidth * SourceLen, 0);
16893 APInt BWide(LaneWidth * SourceLen, 0);
16894
16895 for (unsigned I = 0; I != SourceLen; ++I) {
16896 APInt ALane;
16897 APInt BLane;
16898 if (ElemQT->isIntegerType()) { // Get value.
16899 ALane = SourceLHS.getVectorElt(I).getInt();
16900 BLane = SourceRHS.getVectorElt(I).getInt();
16901 } else if (ElemQT->isFloatingType()) { // Get only sign bit.
16902 ALane =
16903 SourceLHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
16904 BLane =
16905 SourceRHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
16906 } else { // Must be integer or floating type.
16907 return false;
16908 }
16909 AWide.insertBits(SubBits: ALane, bitPosition: I * LaneWidth);
16910 BWide.insertBits(SubBits: BLane, bitPosition: I * LaneWidth);
16911 }
16912 return Success(Value: Fn(AWide, BWide), E);
16913 };
16914
16915 auto HandleMaskBinOp =
16916 [&](llvm::function_ref<APSInt(const APSInt &, const APSInt &)> Fn)
16917 -> bool {
16918 APValue LHS, RHS;
16919 if (!Evaluate(Result&: LHS, Info, E: E->getArg(Arg: 0)) ||
16920 !Evaluate(Result&: RHS, Info, E: E->getArg(Arg: 1)))
16921 return false;
16922
16923 APSInt ResultInt = Fn(LHS.getInt(), RHS.getInt());
16924
16925 return Success(V: APValue(ResultInt), E);
16926 };
16927
16928 auto HandleCRC32 = [&](unsigned DataBytes) -> bool {
16929 APSInt CRC, Data;
16930 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CRC, Info) ||
16931 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Data, Info))
16932 return false;
16933
16934 uint64_t CRCVal = CRC.getZExtValue();
16935 uint64_t DataVal = Data.getZExtValue();
16936
16937 // CRC32C polynomial (iSCSI polynomial, bit-reversed)
16938 static const uint32_t CRC32C_POLY = 0x82F63B78;
16939
16940 // Process each byte
16941 uint32_t Result = static_cast<uint32_t>(CRCVal);
16942 for (unsigned I = 0; I != DataBytes; ++I) {
16943 uint8_t Byte = static_cast<uint8_t>((DataVal >> (I * 8)) & 0xFF);
16944 Result ^= Byte;
16945 for (int J = 0; J != 8; ++J) {
16946 Result = (Result >> 1) ^ ((Result & 1) ? CRC32C_POLY : 0);
16947 }
16948 }
16949
16950 return Success(Value: Result, E);
16951 };
16952
16953 switch (BuiltinOp) {
16954 default:
16955 return false;
16956
16957 case X86::BI__builtin_ia32_crc32qi:
16958 return HandleCRC32(1);
16959 case X86::BI__builtin_ia32_crc32hi:
16960 return HandleCRC32(2);
16961 case X86::BI__builtin_ia32_crc32si:
16962 return HandleCRC32(4);
16963 case X86::BI__builtin_ia32_crc32di:
16964 return HandleCRC32(8);
16965
16966 case Builtin::BI__builtin_dynamic_object_size:
16967 case Builtin::BI__builtin_object_size: {
16968 // The type was checked when we built the expression.
16969 unsigned Type =
16970 E->getArg(Arg: 1)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
16971 assert(Type <= 3 && "unexpected type");
16972
16973 bool IsDynamic = BuiltinOp == Builtin::BI__builtin_dynamic_object_size;
16974 if (std::optional<uint64_t> Size =
16975 tryEvaluateBuiltinObjectSize(E: E->getArg(Arg: 0), Type, Info, IsDynamic))
16976 return Success(Value: *Size, E);
16977
16978 if (E->getArg(Arg: 0)->HasSideEffects(Ctx: Info.Ctx))
16979 return Success(Value: (Type & 2) ? 0 : -1, E);
16980
16981 // Expression had no side effects, but we couldn't statically determine the
16982 // size of the referenced object.
16983 switch (Info.EvalMode) {
16984 case EvaluationMode::ConstantExpression:
16985 case EvaluationMode::ConstantFold:
16986 case EvaluationMode::IgnoreSideEffects:
16987 // Leave it to IR generation.
16988 return Error(E);
16989 case EvaluationMode::ConstantExpressionUnevaluated:
16990 // Reduce it to a constant now.
16991 return Success(Value: (Type & 2) ? 0 : -1, E);
16992 }
16993
16994 llvm_unreachable("unexpected EvalMode");
16995 }
16996
16997 case Builtin::BI__builtin_os_log_format_buffer_size: {
16998 analyze_os_log::OSLogBufferLayout Layout;
16999 analyze_os_log::computeOSLogBufferLayout(Ctx&: Info.Ctx, E, layout&: Layout);
17000 return Success(Value: Layout.size().getQuantity(), E);
17001 }
17002
17003 case Builtin::BI__builtin_is_aligned: {
17004 APValue Src;
17005 APSInt Alignment;
17006 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17007 return false;
17008 if (Src.isLValue()) {
17009 // If we evaluated a pointer, check the minimum known alignment.
17010 LValue Ptr;
17011 Ptr.setFrom(Ctx: Info.Ctx, V: Src);
17012 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Ptr);
17013 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Ptr.Offset);
17014 // We can return true if the known alignment at the computed offset is
17015 // greater than the requested alignment.
17016 assert(PtrAlign.isPowerOfTwo());
17017 assert(Alignment.isPowerOf2());
17018 if (PtrAlign.getQuantity() >= Alignment)
17019 return Success(Value: 1, E);
17020 // If the alignment is not known to be sufficient, some cases could still
17021 // be aligned at run time. However, if the requested alignment is less or
17022 // equal to the base alignment and the offset is not aligned, we know that
17023 // the run-time value can never be aligned.
17024 if (BaseAlignment.getQuantity() >= Alignment &&
17025 PtrAlign.getQuantity() < Alignment)
17026 return Success(Value: 0, E);
17027 // Otherwise we can't infer whether the value is sufficiently aligned.
17028 // TODO: __builtin_is_aligned(__builtin_align_{down,up{(expr, N), N)
17029 // in cases where we can't fully evaluate the pointer.
17030 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_compute)
17031 << Alignment;
17032 return false;
17033 }
17034 assert(Src.isInt());
17035 return Success(Value: (Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E);
17036 }
17037 case Builtin::BI__builtin_align_up: {
17038 APValue Src;
17039 APSInt Alignment;
17040 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17041 return false;
17042 if (!Src.isInt())
17043 return Error(E);
17044 APSInt AlignedVal =
17045 APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1),
17046 Src.getInt().isUnsigned());
17047 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
17048 return Success(SI: AlignedVal, E);
17049 }
17050 case Builtin::BI__builtin_align_down: {
17051 APValue Src;
17052 APSInt Alignment;
17053 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17054 return false;
17055 if (!Src.isInt())
17056 return Error(E);
17057 APSInt AlignedVal =
17058 APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned());
17059 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
17060 return Success(SI: AlignedVal, E);
17061 }
17062
17063 case Builtin::BI__builtin_bitreverseg:
17064 case Builtin::BI__builtin_bitreverse8:
17065 case Builtin::BI__builtin_bitreverse16:
17066 case Builtin::BI__builtin_bitreverse32:
17067 case Builtin::BI__builtin_bitreverse64:
17068 case Builtin::BI__builtin_elementwise_bitreverse: {
17069 APSInt Val;
17070 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17071 return false;
17072
17073 return Success(I: Val.reverseBits(), E);
17074 }
17075 case Builtin::BI__builtin_bswapg:
17076 case Builtin::BI__builtin_bswap16:
17077 case Builtin::BI__builtin_bswap32:
17078 case Builtin::BI__builtin_bswap64:
17079 case Builtin::BIstdc_memreverse8u8:
17080 case Builtin::BIstdc_memreverse8u16:
17081 case Builtin::BIstdc_memreverse8u32:
17082 case Builtin::BIstdc_memreverse8u64: {
17083 APSInt Val;
17084 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17085 return false;
17086 if (Val.getBitWidth() == 8 || Val.getBitWidth() == 1)
17087 return Success(SI: Val, E);
17088
17089 return Success(I: Val.byteSwap(), E);
17090 }
17091
17092 case Builtin::BI__builtin_classify_type:
17093 return Success(Value: (int)EvaluateBuiltinClassifyType(E, LangOpts: Info.getLangOpts()), E);
17094
17095 case Builtin::BI__builtin_clrsb:
17096 case Builtin::BI__builtin_clrsbl:
17097 case Builtin::BI__builtin_clrsbll: {
17098 APSInt Val;
17099 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17100 return false;
17101
17102 return Success(Value: Val.getBitWidth() - Val.getSignificantBits(), E);
17103 }
17104
17105 case Builtin::BI__builtin_clz:
17106 case Builtin::BI__builtin_clzl:
17107 case Builtin::BI__builtin_clzll:
17108 case Builtin::BI__builtin_clzs:
17109 case Builtin::BI__builtin_clzg:
17110 case Builtin::BI__builtin_elementwise_clzg:
17111 case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes
17112 case Builtin::BI__lzcnt:
17113 case Builtin::BI__lzcnt64: {
17114 APSInt Val;
17115 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17116 APValue Vec;
17117 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17118 return false;
17119 Val = ConvertBoolVectorToInt(Val: Vec);
17120 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17121 return false;
17122 }
17123
17124 std::optional<APSInt> Fallback;
17125 if ((BuiltinOp == Builtin::BI__builtin_clzg ||
17126 BuiltinOp == Builtin::BI__builtin_elementwise_clzg) &&
17127 E->getNumArgs() > 1) {
17128 APSInt FallbackTemp;
17129 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
17130 return false;
17131 Fallback = FallbackTemp;
17132 }
17133
17134 if (!Val) {
17135 if (Fallback)
17136 return Success(SI: *Fallback, E);
17137
17138 // When the argument is 0, the result of GCC builtins is undefined,
17139 // whereas for Microsoft intrinsics, the result is the bit-width of the
17140 // argument.
17141 bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 &&
17142 BuiltinOp != Builtin::BI__lzcnt &&
17143 BuiltinOp != Builtin::BI__lzcnt64;
17144
17145 if (BuiltinOp == Builtin::BI__builtin_elementwise_clzg) {
17146 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
17147 << /*IsTrailing=*/false;
17148 }
17149
17150 if (ZeroIsUndefined)
17151 return Error(E);
17152 }
17153
17154 return Success(Value: Val.countl_zero(), E);
17155 }
17156
17157 case Builtin::BI__builtin_constant_p: {
17158 const Expr *Arg = E->getArg(Arg: 0);
17159 if (EvaluateBuiltinConstantP(Info, Arg))
17160 return Success(Value: true, E);
17161 if (Info.InConstantContext || Arg->HasSideEffects(Ctx: Info.Ctx)) {
17162 // Outside a constant context, eagerly evaluate to false in the presence
17163 // of side-effects in order to avoid -Wunsequenced false-positives in
17164 // a branch on __builtin_constant_p(expr).
17165 return Success(Value: false, E);
17166 }
17167 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17168 return false;
17169 }
17170
17171 case Builtin::BI__noop:
17172 // __noop always evaluates successfully and returns 0.
17173 return Success(Value: 0, E);
17174
17175 case Builtin::BI__builtin_is_constant_evaluated: {
17176 const auto *Callee = Info.CurrentCall->getCallee();
17177 if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression &&
17178 (Info.CallStackDepth == 1 ||
17179 (Info.CallStackDepth == 2 && Callee->isInStdNamespace() &&
17180 Callee->getIdentifier() &&
17181 Callee->getIdentifier()->isStr(Str: "is_constant_evaluated")))) {
17182 // FIXME: Find a better way to avoid duplicated diagnostics.
17183 if (Info.EvalStatus.Diag)
17184 Info.report(Loc: (Info.CallStackDepth == 1)
17185 ? E->getExprLoc()
17186 : Info.CurrentCall->getCallRange().getBegin(),
17187 DiagId: diag::warn_is_constant_evaluated_always_true_constexpr)
17188 << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated"
17189 : "std::is_constant_evaluated");
17190 }
17191
17192 return Success(Value: Info.InConstantContext, E);
17193 }
17194
17195 case Builtin::BI__builtin_is_within_lifetime:
17196 if (auto result = EvaluateBuiltinIsWithinLifetime(*this, E))
17197 return Success(Value: *result, E);
17198 return false;
17199
17200 case Builtin::BI__builtin_ctz:
17201 case Builtin::BI__builtin_ctzl:
17202 case Builtin::BI__builtin_ctzll:
17203 case Builtin::BI__builtin_ctzs:
17204 case Builtin::BI__builtin_ctzg:
17205 case Builtin::BI__builtin_elementwise_ctzg: {
17206 APSInt Val;
17207 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17208 APValue Vec;
17209 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17210 return false;
17211 Val = ConvertBoolVectorToInt(Val: Vec);
17212 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17213 return false;
17214 }
17215
17216 std::optional<APSInt> Fallback;
17217 if ((BuiltinOp == Builtin::BI__builtin_ctzg ||
17218 BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) &&
17219 E->getNumArgs() > 1) {
17220 APSInt FallbackTemp;
17221 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
17222 return false;
17223 Fallback = FallbackTemp;
17224 }
17225
17226 if (!Val) {
17227 if (Fallback)
17228 return Success(SI: *Fallback, E);
17229
17230 if (BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) {
17231 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
17232 << /*IsTrailing=*/true;
17233 }
17234 return Error(E);
17235 }
17236
17237 return Success(Value: Val.countr_zero(), E);
17238 }
17239
17240 case Builtin::BI__builtin_eh_return_data_regno: {
17241 int Operand = E->getArg(Arg: 0)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
17242 Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(RegNo: Operand);
17243 return Success(Value: Operand, E);
17244 }
17245
17246 case Builtin::BI__builtin_elementwise_abs: {
17247 APSInt Val;
17248 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17249 return false;
17250
17251 return Success(I: Val.abs(), E);
17252 }
17253
17254 case Builtin::BI__builtin_expect:
17255 case Builtin::BI__builtin_expect_with_probability:
17256 return Visit(S: E->getArg(Arg: 0));
17257
17258 case Builtin::BI__builtin_ptrauth_string_discriminator: {
17259 const auto *Literal =
17260 cast<StringLiteral>(Val: E->getArg(Arg: 0)->IgnoreParenImpCasts());
17261 uint64_t Result = getPointerAuthStableSipHash(S: Literal->getString());
17262 return Success(Value: Result, E);
17263 }
17264
17265 case Builtin::BI__builtin_infer_alloc_token: {
17266 // If we fail to infer a type, this fails to be a constant expression; this
17267 // can be checked with __builtin_constant_p(...).
17268 QualType AllocType = infer_alloc::inferPossibleType(E, Ctx: Info.Ctx, CastE: nullptr);
17269 if (AllocType.isNull())
17270 return Error(
17271 E, D: diag::note_constexpr_infer_alloc_token_type_inference_failed);
17272 auto ATMD = infer_alloc::getAllocTokenMetadata(T: AllocType, Ctx: Info.Ctx);
17273 if (!ATMD)
17274 return Error(E, D: diag::note_constexpr_infer_alloc_token_no_metadata);
17275 auto Mode =
17276 Info.getLangOpts().AllocTokenMode.value_or(u: llvm::DefaultAllocTokenMode);
17277 uint64_t BitWidth = Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType());
17278 auto MaxTokensOpt = Info.getLangOpts().AllocTokenMax;
17279 uint64_t MaxTokens =
17280 MaxTokensOpt.value_or(u: 0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
17281 auto MaybeToken = llvm::getAllocToken(Mode, Metadata: *ATMD, MaxTokens);
17282 if (!MaybeToken)
17283 return Error(E, D: diag::note_constexpr_infer_alloc_token_stateful_mode);
17284 return Success(I: llvm::APInt(BitWidth, *MaybeToken), E);
17285 }
17286
17287 case Builtin::BI__builtin_ffs:
17288 case Builtin::BI__builtin_ffsl:
17289 case Builtin::BI__builtin_ffsll: {
17290 APSInt Val;
17291 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17292 return false;
17293
17294 unsigned N = Val.countr_zero();
17295 return Success(Value: N == Val.getBitWidth() ? 0 : N + 1, E);
17296 }
17297
17298 case Builtin::BI__builtin_fpclassify: {
17299 APFloat Val(0.0);
17300 if (!EvaluateFloat(E: E->getArg(Arg: 5), Result&: Val, Info))
17301 return false;
17302 unsigned Arg;
17303 switch (Val.getCategory()) {
17304 case APFloat::fcNaN: Arg = 0; break;
17305 case APFloat::fcInfinity: Arg = 1; break;
17306 case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break;
17307 case APFloat::fcZero: Arg = 4; break;
17308 }
17309 return Visit(S: E->getArg(Arg));
17310 }
17311
17312 case Builtin::BI__builtin_isinf_sign: {
17313 APFloat Val(0.0);
17314 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17315 Success(Value: Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E);
17316 }
17317
17318 case Builtin::BI__builtin_isinf: {
17319 APFloat Val(0.0);
17320 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17321 Success(Value: Val.isInfinity() ? 1 : 0, E);
17322 }
17323
17324 case Builtin::BI__builtin_isfinite: {
17325 APFloat Val(0.0);
17326 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17327 Success(Value: Val.isFinite() ? 1 : 0, E);
17328 }
17329
17330 case Builtin::BI__builtin_isnan: {
17331 APFloat Val(0.0);
17332 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17333 Success(Value: Val.isNaN() ? 1 : 0, E);
17334 }
17335
17336 case Builtin::BI__builtin_isnormal: {
17337 APFloat Val(0.0);
17338 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17339 Success(Value: Val.isNormal() ? 1 : 0, E);
17340 }
17341
17342 case Builtin::BI__builtin_issubnormal: {
17343 APFloat Val(0.0);
17344 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17345 Success(Value: Val.isDenormal() ? 1 : 0, E);
17346 }
17347
17348 case Builtin::BI__builtin_iszero: {
17349 APFloat Val(0.0);
17350 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17351 Success(Value: Val.isZero() ? 1 : 0, E);
17352 }
17353
17354 case Builtin::BI__builtin_signbit:
17355 case Builtin::BI__builtin_signbitf:
17356 case Builtin::BI__builtin_signbitl: {
17357 APFloat Val(0.0);
17358 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17359 Success(Value: Val.isNegative() ? 1 : 0, E);
17360 }
17361
17362 case Builtin::BI__builtin_isgreater:
17363 case Builtin::BI__builtin_isgreaterequal:
17364 case Builtin::BI__builtin_isless:
17365 case Builtin::BI__builtin_islessequal:
17366 case Builtin::BI__builtin_islessgreater:
17367 case Builtin::BI__builtin_isunordered: {
17368 APFloat LHS(0.0);
17369 APFloat RHS(0.0);
17370 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17371 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
17372 return false;
17373
17374 return Success(
17375 Value: [&] {
17376 switch (BuiltinOp) {
17377 case Builtin::BI__builtin_isgreater:
17378 return LHS > RHS;
17379 case Builtin::BI__builtin_isgreaterequal:
17380 return LHS >= RHS;
17381 case Builtin::BI__builtin_isless:
17382 return LHS < RHS;
17383 case Builtin::BI__builtin_islessequal:
17384 return LHS <= RHS;
17385 case Builtin::BI__builtin_islessgreater: {
17386 APFloat::cmpResult cmp = LHS.compare(RHS);
17387 return cmp == APFloat::cmpResult::cmpLessThan ||
17388 cmp == APFloat::cmpResult::cmpGreaterThan;
17389 }
17390 case Builtin::BI__builtin_isunordered:
17391 return LHS.compare(RHS) == APFloat::cmpResult::cmpUnordered;
17392 default:
17393 llvm_unreachable("Unexpected builtin ID: Should be a floating "
17394 "point comparison function");
17395 }
17396 }()
17397 ? 1
17398 : 0,
17399 E);
17400 }
17401
17402 case Builtin::BI__builtin_issignaling: {
17403 APFloat Val(0.0);
17404 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17405 Success(Value: Val.isSignaling() ? 1 : 0, E);
17406 }
17407
17408 case Builtin::BI__builtin_isfpclass: {
17409 APSInt MaskVal;
17410 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: MaskVal, Info))
17411 return false;
17412 unsigned Test = static_cast<llvm::FPClassTest>(MaskVal.getZExtValue());
17413 APFloat Val(0.0);
17414 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17415 Success(Value: (Val.classify() & Test) ? 1 : 0, E);
17416 }
17417
17418 case Builtin::BI__builtin_parity:
17419 case Builtin::BI__builtin_parityl:
17420 case Builtin::BI__builtin_parityll: {
17421 APSInt Val;
17422 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17423 return false;
17424
17425 return Success(Value: Val.popcount() % 2, E);
17426 }
17427
17428 case Builtin::BI__builtin_abs:
17429 case Builtin::BI__builtin_labs:
17430 case Builtin::BI__builtin_llabs: {
17431 APSInt Val;
17432 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17433 return false;
17434 if (Val == APSInt(APInt::getSignedMinValue(numBits: Val.getBitWidth()),
17435 /*IsUnsigned=*/false))
17436 return false;
17437 if (Val.isNegative())
17438 Val.negate();
17439 return Success(SI: Val, E);
17440 }
17441
17442 case Builtin::BI__builtin_popcount:
17443 case Builtin::BI__builtin_popcountl:
17444 case Builtin::BI__builtin_popcountll:
17445 case Builtin::BI__builtin_popcountg:
17446 case Builtin::BI__builtin_elementwise_popcount:
17447 case Builtin::BI__popcnt16: // Microsoft variants of popcount
17448 case Builtin::BI__popcnt:
17449 case Builtin::BI__popcnt64: {
17450 APSInt Val;
17451 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17452 APValue Vec;
17453 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17454 return false;
17455 Val = ConvertBoolVectorToInt(Val: Vec);
17456 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17457 return false;
17458 }
17459
17460 return Success(Value: Val.popcount(), E);
17461 }
17462
17463 case Builtin::BI__builtin_rotateleft8:
17464 case Builtin::BI__builtin_rotateleft16:
17465 case Builtin::BI__builtin_rotateleft32:
17466 case Builtin::BI__builtin_rotateleft64:
17467 case Builtin::BI__builtin_rotateright8:
17468 case Builtin::BI__builtin_rotateright16:
17469 case Builtin::BI__builtin_rotateright32:
17470 case Builtin::BI__builtin_rotateright64:
17471 case Builtin::BI__builtin_stdc_rotate_left:
17472 case Builtin::BI__builtin_stdc_rotate_right:
17473 case Builtin::BIstdc_rotate_left_uc:
17474 case Builtin::BIstdc_rotate_left_us:
17475 case Builtin::BIstdc_rotate_left_ui:
17476 case Builtin::BIstdc_rotate_left_ul:
17477 case Builtin::BIstdc_rotate_left_ull:
17478 case Builtin::BIstdc_rotate_right_uc:
17479 case Builtin::BIstdc_rotate_right_us:
17480 case Builtin::BIstdc_rotate_right_ui:
17481 case Builtin::BIstdc_rotate_right_ul:
17482 case Builtin::BIstdc_rotate_right_ull:
17483 case Builtin::BI_rotl8: // Microsoft variants of rotate left
17484 case Builtin::BI_rotl16:
17485 case Builtin::BI_rotl:
17486 case Builtin::BI_lrotl:
17487 case Builtin::BI_rotl64:
17488 case Builtin::BI_rotr8: // Microsoft variants of rotate right
17489 case Builtin::BI_rotr16:
17490 case Builtin::BI_rotr:
17491 case Builtin::BI_lrotr:
17492 case Builtin::BI_rotr64: {
17493 APSInt Value, Amount;
17494 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Value, Info) ||
17495 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amount, Info))
17496 return false;
17497
17498 Amount = NormalizeRotateAmount(Value, Amount);
17499
17500 switch (BuiltinOp) {
17501 case Builtin::BI__builtin_rotateright8:
17502 case Builtin::BI__builtin_rotateright16:
17503 case Builtin::BI__builtin_rotateright32:
17504 case Builtin::BI__builtin_rotateright64:
17505 case Builtin::BI__builtin_stdc_rotate_right:
17506 case Builtin::BIstdc_rotate_right_uc:
17507 case Builtin::BIstdc_rotate_right_us:
17508 case Builtin::BIstdc_rotate_right_ui:
17509 case Builtin::BIstdc_rotate_right_ul:
17510 case Builtin::BIstdc_rotate_right_ull:
17511 case Builtin::BI_rotr8:
17512 case Builtin::BI_rotr16:
17513 case Builtin::BI_rotr:
17514 case Builtin::BI_lrotr:
17515 case Builtin::BI_rotr64:
17516 return Success(
17517 SI: APSInt(Value.rotr(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17518 default:
17519 return Success(
17520 SI: APSInt(Value.rotl(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17521 }
17522 }
17523
17524 case Builtin::BIstdc_leading_zeros_uc:
17525 case Builtin::BIstdc_leading_zeros_us:
17526 case Builtin::BIstdc_leading_zeros_ui:
17527 case Builtin::BIstdc_leading_zeros_ul:
17528 case Builtin::BIstdc_leading_zeros_ull:
17529 case Builtin::BIstdc_leading_ones_uc:
17530 case Builtin::BIstdc_leading_ones_us:
17531 case Builtin::BIstdc_leading_ones_ui:
17532 case Builtin::BIstdc_leading_ones_ul:
17533 case Builtin::BIstdc_leading_ones_ull:
17534 case Builtin::BIstdc_trailing_zeros_uc:
17535 case Builtin::BIstdc_trailing_zeros_us:
17536 case Builtin::BIstdc_trailing_zeros_ui:
17537 case Builtin::BIstdc_trailing_zeros_ul:
17538 case Builtin::BIstdc_trailing_zeros_ull:
17539 case Builtin::BIstdc_trailing_ones_uc:
17540 case Builtin::BIstdc_trailing_ones_us:
17541 case Builtin::BIstdc_trailing_ones_ui:
17542 case Builtin::BIstdc_trailing_ones_ul:
17543 case Builtin::BIstdc_trailing_ones_ull:
17544 case Builtin::BIstdc_first_leading_zero_uc:
17545 case Builtin::BIstdc_first_leading_zero_us:
17546 case Builtin::BIstdc_first_leading_zero_ui:
17547 case Builtin::BIstdc_first_leading_zero_ul:
17548 case Builtin::BIstdc_first_leading_zero_ull:
17549 case Builtin::BIstdc_first_leading_one_uc:
17550 case Builtin::BIstdc_first_leading_one_us:
17551 case Builtin::BIstdc_first_leading_one_ui:
17552 case Builtin::BIstdc_first_leading_one_ul:
17553 case Builtin::BIstdc_first_leading_one_ull:
17554 case Builtin::BIstdc_first_trailing_zero_uc:
17555 case Builtin::BIstdc_first_trailing_zero_us:
17556 case Builtin::BIstdc_first_trailing_zero_ui:
17557 case Builtin::BIstdc_first_trailing_zero_ul:
17558 case Builtin::BIstdc_first_trailing_zero_ull:
17559 case Builtin::BIstdc_first_trailing_one_uc:
17560 case Builtin::BIstdc_first_trailing_one_us:
17561 case Builtin::BIstdc_first_trailing_one_ui:
17562 case Builtin::BIstdc_first_trailing_one_ul:
17563 case Builtin::BIstdc_first_trailing_one_ull:
17564 case Builtin::BIstdc_count_zeros_uc:
17565 case Builtin::BIstdc_count_zeros_us:
17566 case Builtin::BIstdc_count_zeros_ui:
17567 case Builtin::BIstdc_count_zeros_ul:
17568 case Builtin::BIstdc_count_zeros_ull:
17569 case Builtin::BIstdc_count_ones_uc:
17570 case Builtin::BIstdc_count_ones_us:
17571 case Builtin::BIstdc_count_ones_ui:
17572 case Builtin::BIstdc_count_ones_ul:
17573 case Builtin::BIstdc_count_ones_ull:
17574 case Builtin::BIstdc_has_single_bit_uc:
17575 case Builtin::BIstdc_has_single_bit_us:
17576 case Builtin::BIstdc_has_single_bit_ui:
17577 case Builtin::BIstdc_has_single_bit_ul:
17578 case Builtin::BIstdc_has_single_bit_ull:
17579 case Builtin::BIstdc_bit_width_uc:
17580 case Builtin::BIstdc_bit_width_us:
17581 case Builtin::BIstdc_bit_width_ui:
17582 case Builtin::BIstdc_bit_width_ul:
17583 case Builtin::BIstdc_bit_width_ull:
17584 case Builtin::BIstdc_bit_floor_uc:
17585 case Builtin::BIstdc_bit_floor_us:
17586 case Builtin::BIstdc_bit_floor_ui:
17587 case Builtin::BIstdc_bit_floor_ul:
17588 case Builtin::BIstdc_bit_floor_ull:
17589 case Builtin::BIstdc_bit_ceil_uc:
17590 case Builtin::BIstdc_bit_ceil_us:
17591 case Builtin::BIstdc_bit_ceil_ui:
17592 case Builtin::BIstdc_bit_ceil_ul:
17593 case Builtin::BIstdc_bit_ceil_ull:
17594 case Builtin::BI__builtin_stdc_leading_zeros:
17595 case Builtin::BI__builtin_stdc_leading_ones:
17596 case Builtin::BI__builtin_stdc_trailing_zeros:
17597 case Builtin::BI__builtin_stdc_trailing_ones:
17598 case Builtin::BI__builtin_stdc_first_leading_zero:
17599 case Builtin::BI__builtin_stdc_first_leading_one:
17600 case Builtin::BI__builtin_stdc_first_trailing_zero:
17601 case Builtin::BI__builtin_stdc_first_trailing_one:
17602 case Builtin::BI__builtin_stdc_count_zeros:
17603 case Builtin::BI__builtin_stdc_count_ones:
17604 case Builtin::BI__builtin_stdc_has_single_bit:
17605 case Builtin::BI__builtin_stdc_bit_width:
17606 case Builtin::BI__builtin_stdc_bit_floor:
17607 case Builtin::BI__builtin_stdc_bit_ceil: {
17608 APSInt Val;
17609 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17610 return false;
17611
17612 unsigned BitWidth = Val.getBitWidth();
17613 const unsigned ResBitWidth = Info.Ctx.getIntWidth(T: E->getType());
17614
17615 switch (BuiltinOp) {
17616 case Builtin::BIstdc_leading_zeros_uc:
17617 case Builtin::BIstdc_leading_zeros_us:
17618 case Builtin::BIstdc_leading_zeros_ui:
17619 case Builtin::BIstdc_leading_zeros_ul:
17620 case Builtin::BIstdc_leading_zeros_ull:
17621 case Builtin::BI__builtin_stdc_leading_zeros:
17622 return Success(I: APInt(ResBitWidth, Val.countl_zero()), E);
17623 case Builtin::BIstdc_leading_ones_uc:
17624 case Builtin::BIstdc_leading_ones_us:
17625 case Builtin::BIstdc_leading_ones_ui:
17626 case Builtin::BIstdc_leading_ones_ul:
17627 case Builtin::BIstdc_leading_ones_ull:
17628 case Builtin::BI__builtin_stdc_leading_ones:
17629 return Success(I: APInt(ResBitWidth, Val.countl_one()), E);
17630 case Builtin::BIstdc_trailing_zeros_uc:
17631 case Builtin::BIstdc_trailing_zeros_us:
17632 case Builtin::BIstdc_trailing_zeros_ui:
17633 case Builtin::BIstdc_trailing_zeros_ul:
17634 case Builtin::BIstdc_trailing_zeros_ull:
17635 case Builtin::BI__builtin_stdc_trailing_zeros:
17636 return Success(I: APInt(ResBitWidth, Val.countr_zero()), E);
17637 case Builtin::BIstdc_trailing_ones_uc:
17638 case Builtin::BIstdc_trailing_ones_us:
17639 case Builtin::BIstdc_trailing_ones_ui:
17640 case Builtin::BIstdc_trailing_ones_ul:
17641 case Builtin::BIstdc_trailing_ones_ull:
17642 case Builtin::BI__builtin_stdc_trailing_ones:
17643 return Success(I: APInt(ResBitWidth, Val.countr_one()), E);
17644 case Builtin::BIstdc_first_leading_zero_uc:
17645 case Builtin::BIstdc_first_leading_zero_us:
17646 case Builtin::BIstdc_first_leading_zero_ui:
17647 case Builtin::BIstdc_first_leading_zero_ul:
17648 case Builtin::BIstdc_first_leading_zero_ull:
17649 case Builtin::BI__builtin_stdc_first_leading_zero:
17650 return Success(
17651 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countl_one() + 1), E);
17652 case Builtin::BIstdc_first_leading_one_uc:
17653 case Builtin::BIstdc_first_leading_one_us:
17654 case Builtin::BIstdc_first_leading_one_ui:
17655 case Builtin::BIstdc_first_leading_one_ul:
17656 case Builtin::BIstdc_first_leading_one_ull:
17657 case Builtin::BI__builtin_stdc_first_leading_one:
17658 return Success(
17659 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countl_zero() + 1), E);
17660 case Builtin::BIstdc_first_trailing_zero_uc:
17661 case Builtin::BIstdc_first_trailing_zero_us:
17662 case Builtin::BIstdc_first_trailing_zero_ui:
17663 case Builtin::BIstdc_first_trailing_zero_ul:
17664 case Builtin::BIstdc_first_trailing_zero_ull:
17665 case Builtin::BI__builtin_stdc_first_trailing_zero:
17666 return Success(
17667 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countr_one() + 1), E);
17668 case Builtin::BIstdc_first_trailing_one_uc:
17669 case Builtin::BIstdc_first_trailing_one_us:
17670 case Builtin::BIstdc_first_trailing_one_ui:
17671 case Builtin::BIstdc_first_trailing_one_ul:
17672 case Builtin::BIstdc_first_trailing_one_ull:
17673 case Builtin::BI__builtin_stdc_first_trailing_one:
17674 return Success(
17675 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countr_zero() + 1), E);
17676 case Builtin::BIstdc_count_zeros_uc:
17677 case Builtin::BIstdc_count_zeros_us:
17678 case Builtin::BIstdc_count_zeros_ui:
17679 case Builtin::BIstdc_count_zeros_ul:
17680 case Builtin::BIstdc_count_zeros_ull:
17681 case Builtin::BI__builtin_stdc_count_zeros: {
17682 APInt Cnt(ResBitWidth, BitWidth - Val.popcount());
17683 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
17684 }
17685 case Builtin::BIstdc_count_ones_uc:
17686 case Builtin::BIstdc_count_ones_us:
17687 case Builtin::BIstdc_count_ones_ui:
17688 case Builtin::BIstdc_count_ones_ul:
17689 case Builtin::BIstdc_count_ones_ull:
17690 case Builtin::BI__builtin_stdc_count_ones: {
17691 APInt Cnt(ResBitWidth, Val.popcount());
17692 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
17693 }
17694 case Builtin::BIstdc_has_single_bit_uc:
17695 case Builtin::BIstdc_has_single_bit_us:
17696 case Builtin::BIstdc_has_single_bit_ui:
17697 case Builtin::BIstdc_has_single_bit_ul:
17698 case Builtin::BIstdc_has_single_bit_ull:
17699 case Builtin::BI__builtin_stdc_has_single_bit: {
17700 APInt Res(ResBitWidth, Val.popcount() == 1 ? 1 : 0);
17701 return Success(SI: APSInt(Res, /*IsUnsigned*/ true), E);
17702 }
17703 case Builtin::BIstdc_bit_width_uc:
17704 case Builtin::BIstdc_bit_width_us:
17705 case Builtin::BIstdc_bit_width_ui:
17706 case Builtin::BIstdc_bit_width_ul:
17707 case Builtin::BIstdc_bit_width_ull:
17708 case Builtin::BI__builtin_stdc_bit_width:
17709 return Success(I: APInt(ResBitWidth, BitWidth - Val.countl_zero()), E);
17710 case Builtin::BIstdc_bit_floor_uc:
17711 case Builtin::BIstdc_bit_floor_us:
17712 case Builtin::BIstdc_bit_floor_ui:
17713 case Builtin::BIstdc_bit_floor_ul:
17714 case Builtin::BIstdc_bit_floor_ull:
17715 case Builtin::BI__builtin_stdc_bit_floor: {
17716 if (Val.isZero())
17717 return Success(I: APInt(BitWidth, 0), E);
17718 unsigned Exp = BitWidth - Val.countl_zero() - 1;
17719 return Success(
17720 SI: APSInt(APInt::getOneBitSet(numBits: BitWidth, BitNo: Exp), /*IsUnsigned*/ true), E);
17721 }
17722 case Builtin::BIstdc_bit_ceil_uc:
17723 case Builtin::BIstdc_bit_ceil_us:
17724 case Builtin::BIstdc_bit_ceil_ui:
17725 case Builtin::BIstdc_bit_ceil_ul:
17726 case Builtin::BIstdc_bit_ceil_ull:
17727 case Builtin::BI__builtin_stdc_bit_ceil: {
17728 if (Val.ule(RHS: 1))
17729 return Success(SI: APSInt(APInt(BitWidth, 1), /*IsUnsigned*/ true), E);
17730 APInt ValMinusOne = Val - 1;
17731 unsigned LZ = ValMinusOne.countl_zero();
17732 if (LZ == 0)
17733 return Success(SI: APSInt(APInt(BitWidth, 0), /*IsUnsigned*/ true),
17734 E); // overflows; wrap to 0
17735 APInt Result = APInt::getOneBitSet(numBits: BitWidth, BitNo: BitWidth - LZ);
17736 return Success(SI: APSInt(Result, /*IsUnsigned*/ true), E);
17737 }
17738 default:
17739 llvm_unreachable("Unknown stdc builtin");
17740 }
17741 }
17742
17743 case Builtin::BI__builtin_elementwise_add_sat: {
17744 APSInt LHS, RHS;
17745 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17746 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17747 return false;
17748
17749 APInt Result = LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
17750 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17751 }
17752 case Builtin::BI__builtin_elementwise_sub_sat: {
17753 APSInt LHS, RHS;
17754 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17755 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17756 return false;
17757
17758 APInt Result = LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
17759 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17760 }
17761 case Builtin::BI__builtin_elementwise_max: {
17762 APSInt LHS, RHS;
17763 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17764 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17765 return false;
17766
17767 APInt Result = std::max(a: LHS, b: RHS);
17768 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17769 }
17770 case Builtin::BI__builtin_elementwise_min: {
17771 APSInt LHS, RHS;
17772 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17773 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17774 return false;
17775
17776 APInt Result = std::min(a: LHS, b: RHS);
17777 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17778 }
17779 case Builtin::BI__builtin_elementwise_clmul: {
17780 APSInt LHS, RHS;
17781 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17782 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17783 return false;
17784
17785 APInt Result = llvm::APIntOps::clmul(LHS, RHS);
17786 return Success(SI: APSInt(Result, LHS.isUnsigned()), E);
17787 }
17788 case Builtin::BI__builtin_elementwise_fshl:
17789 case Builtin::BI__builtin_elementwise_fshr: {
17790 APSInt Hi, Lo, Shift;
17791 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Hi, Info) ||
17792 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Lo, Info) ||
17793 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Shift, Info))
17794 return false;
17795
17796 switch (BuiltinOp) {
17797 case Builtin::BI__builtin_elementwise_fshl: {
17798 APSInt Result(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned());
17799 return Success(SI: Result, E);
17800 }
17801 case Builtin::BI__builtin_elementwise_fshr: {
17802 APSInt Result(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned());
17803 return Success(SI: Result, E);
17804 }
17805 }
17806 llvm_unreachable("Fully covered switch above");
17807 }
17808 case Builtin::BIstrlen:
17809 case Builtin::BIwcslen:
17810 // A call to strlen is not a constant expression.
17811 if (Info.getLangOpts().CPlusPlus11)
17812 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
17813 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
17814 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
17815 else
17816 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17817 [[fallthrough]];
17818 case Builtin::BI__builtin_strlen:
17819 case Builtin::BI__builtin_wcslen: {
17820 // As an extension, we support __builtin_strlen() as a constant expression,
17821 // and support folding strlen() to a constant.
17822 if (std::optional<uint64_t> StrLen =
17823 EvaluateBuiltinStrLen(E: E->getArg(Arg: 0), Info))
17824 return Success(Value: *StrLen, E);
17825 return false;
17826 }
17827
17828 case Builtin::BIstrcmp:
17829 case Builtin::BIwcscmp:
17830 case Builtin::BIstrncmp:
17831 case Builtin::BIwcsncmp:
17832 case Builtin::BImemcmp:
17833 case Builtin::BIbcmp:
17834 case Builtin::BIwmemcmp:
17835 // A call to strlen is not a constant expression.
17836 if (Info.getLangOpts().CPlusPlus11)
17837 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
17838 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
17839 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
17840 else
17841 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17842 [[fallthrough]];
17843 case Builtin::BI__builtin_strcmp:
17844 case Builtin::BI__builtin_wcscmp:
17845 case Builtin::BI__builtin_strncmp:
17846 case Builtin::BI__builtin_wcsncmp:
17847 case Builtin::BI__builtin_memcmp:
17848 case Builtin::BI__builtin_bcmp:
17849 case Builtin::BI__builtin_wmemcmp: {
17850 LValue String1, String2;
17851 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: String1, Info) ||
17852 !EvaluatePointer(E: E->getArg(Arg: 1), Result&: String2, Info))
17853 return false;
17854
17855 uint64_t MaxLength = uint64_t(-1);
17856 if (BuiltinOp != Builtin::BIstrcmp &&
17857 BuiltinOp != Builtin::BIwcscmp &&
17858 BuiltinOp != Builtin::BI__builtin_strcmp &&
17859 BuiltinOp != Builtin::BI__builtin_wcscmp) {
17860 APSInt N;
17861 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
17862 return false;
17863 MaxLength = N.getZExtValue();
17864 }
17865
17866 // Empty substrings compare equal by definition.
17867 if (MaxLength == 0u)
17868 return Success(Value: 0, E);
17869
17870 if (!String1.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
17871 !String2.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
17872 String1.Designator.Invalid || String2.Designator.Invalid)
17873 return false;
17874
17875 QualType CharTy1 = String1.Designator.getType(Ctx&: Info.Ctx);
17876 QualType CharTy2 = String2.Designator.getType(Ctx&: Info.Ctx);
17877
17878 bool IsRawByte = BuiltinOp == Builtin::BImemcmp ||
17879 BuiltinOp == Builtin::BIbcmp ||
17880 BuiltinOp == Builtin::BI__builtin_memcmp ||
17881 BuiltinOp == Builtin::BI__builtin_bcmp;
17882
17883 assert(IsRawByte ||
17884 (Info.Ctx.hasSameUnqualifiedType(
17885 CharTy1, E->getArg(0)->getType()->getPointeeType()) &&
17886 Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2)));
17887
17888 // For memcmp, allow comparing any arrays of '[[un]signed] char' or
17889 // 'char8_t', but no other types.
17890 if (IsRawByte &&
17891 !(isOneByteCharacterType(T: CharTy1) && isOneByteCharacterType(T: CharTy2))) {
17892 // FIXME: Consider using our bit_cast implementation to support this.
17893 Info.FFDiag(E, DiagId: diag::note_constexpr_memcmp_unsupported)
17894 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy1
17895 << CharTy2;
17896 return false;
17897 }
17898
17899 const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) {
17900 return handleLValueToRValueConversion(Info, Conv: E, Type: CharTy1, LVal: String1, RVal&: Char1) &&
17901 handleLValueToRValueConversion(Info, Conv: E, Type: CharTy2, LVal: String2, RVal&: Char2) &&
17902 Char1.isInt() && Char2.isInt();
17903 };
17904 const auto &AdvanceElems = [&] {
17905 return HandleLValueArrayAdjustment(Info, E, LVal&: String1, EltTy: CharTy1, Adjustment: 1) &&
17906 HandleLValueArrayAdjustment(Info, E, LVal&: String2, EltTy: CharTy2, Adjustment: 1);
17907 };
17908
17909 bool StopAtNull =
17910 (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp &&
17911 BuiltinOp != Builtin::BIwmemcmp &&
17912 BuiltinOp != Builtin::BI__builtin_memcmp &&
17913 BuiltinOp != Builtin::BI__builtin_bcmp &&
17914 BuiltinOp != Builtin::BI__builtin_wmemcmp);
17915 bool IsWide = BuiltinOp == Builtin::BIwcscmp ||
17916 BuiltinOp == Builtin::BIwcsncmp ||
17917 BuiltinOp == Builtin::BIwmemcmp ||
17918 BuiltinOp == Builtin::BI__builtin_wcscmp ||
17919 BuiltinOp == Builtin::BI__builtin_wcsncmp ||
17920 BuiltinOp == Builtin::BI__builtin_wmemcmp;
17921
17922 for (; MaxLength; --MaxLength) {
17923 APValue Char1, Char2;
17924 if (!ReadCurElems(Char1, Char2))
17925 return false;
17926 if (Char1.getInt().ne(RHS: Char2.getInt())) {
17927 if (IsWide) // wmemcmp compares with wchar_t signedness.
17928 return Success(Value: Char1.getInt() < Char2.getInt() ? -1 : 1, E);
17929 // memcmp always compares unsigned chars.
17930 return Success(Value: Char1.getInt().ult(RHS: Char2.getInt()) ? -1 : 1, E);
17931 }
17932 if (StopAtNull && !Char1.getInt())
17933 return Success(Value: 0, E);
17934 assert(!(StopAtNull && !Char2.getInt()));
17935 if (!AdvanceElems())
17936 return false;
17937 }
17938 // We hit the strncmp / memcmp limit.
17939 return Success(Value: 0, E);
17940 }
17941
17942 case Builtin::BI__atomic_always_lock_free:
17943 case Builtin::BI__atomic_is_lock_free:
17944 case Builtin::BI__c11_atomic_is_lock_free: {
17945 APSInt SizeVal;
17946 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SizeVal, Info))
17947 return false;
17948
17949 // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
17950 // of two less than or equal to the maximum inline atomic width, we know it
17951 // is lock-free. If the size isn't a power of two, or greater than the
17952 // maximum alignment where we promote atomics, we know it is not lock-free
17953 // (at least not in the sense of atomic_is_lock_free). Otherwise,
17954 // the answer can only be determined at runtime; for example, 16-byte
17955 // atomics have lock-free implementations on some, but not all,
17956 // x86-64 processors.
17957
17958 // Check power-of-two.
17959 CharUnits Size = CharUnits::fromQuantity(Quantity: SizeVal.getZExtValue());
17960 if (Size.isPowerOfTwo()) {
17961 // Check against inlining width.
17962 unsigned InlineWidthBits =
17963 Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
17964 if (Size <= Info.Ctx.toCharUnitsFromBits(BitSize: InlineWidthBits)) {
17965 if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||
17966 Size == CharUnits::One())
17967 return Success(Value: 1, E);
17968
17969 // If the pointer argument can be evaluated to a compile-time constant
17970 // integer (or nullptr), check if that value is appropriately aligned.
17971 const Expr *PtrArg = E->getArg(Arg: 1);
17972 Expr::EvalResult ExprResult;
17973 APSInt IntResult;
17974 if (PtrArg->EvaluateAsRValue(Result&: ExprResult, Ctx: Info.Ctx) &&
17975 ExprResult.Val.toIntegralConstant(Result&: IntResult, SrcTy: PtrArg->getType(),
17976 Ctx: Info.Ctx) &&
17977 IntResult.isAligned(A: Size.getAsAlign()))
17978 return Success(Value: 1, E);
17979
17980 // Otherwise, check if the type's alignment against Size.
17981 if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: PtrArg)) {
17982 // Drop the potential implicit-cast to 'const volatile void*', getting
17983 // the underlying type.
17984 if (ICE->getCastKind() == CK_BitCast)
17985 PtrArg = ICE->getSubExpr();
17986 }
17987
17988 if (auto PtrTy = PtrArg->getType()->getAs<PointerType>()) {
17989 QualType PointeeType = PtrTy->getPointeeType();
17990 if (!PointeeType->isIncompleteType() &&
17991 Info.Ctx.getTypeAlignInChars(T: PointeeType) >= Size) {
17992 // OK, we will inline operations on this object.
17993 return Success(Value: 1, E);
17994 }
17995 }
17996 }
17997 }
17998
17999 return BuiltinOp == Builtin::BI__atomic_always_lock_free ?
18000 Success(Value: 0, E) : Error(E);
18001 }
18002 case Builtin::BI__builtin_addcb:
18003 case Builtin::BI__builtin_addcs:
18004 case Builtin::BI__builtin_addc:
18005 case Builtin::BI__builtin_addcl:
18006 case Builtin::BI__builtin_addcll:
18007 case Builtin::BI__builtin_subcb:
18008 case Builtin::BI__builtin_subcs:
18009 case Builtin::BI__builtin_subc:
18010 case Builtin::BI__builtin_subcl:
18011 case Builtin::BI__builtin_subcll: {
18012 LValue CarryOutLValue;
18013 APSInt LHS, RHS, CarryIn, CarryOut, Result;
18014 QualType ResultType = E->getArg(Arg: 0)->getType();
18015 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18016 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18017 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: CarryIn, Info) ||
18018 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: CarryOutLValue, Info))
18019 return false;
18020 // Copy the number of bits and sign.
18021 Result = LHS;
18022 CarryOut = LHS;
18023
18024 bool FirstOverflowed = false;
18025 bool SecondOverflowed = false;
18026 switch (BuiltinOp) {
18027 default:
18028 llvm_unreachable("Invalid value for BuiltinOp");
18029 case Builtin::BI__builtin_addcb:
18030 case Builtin::BI__builtin_addcs:
18031 case Builtin::BI__builtin_addc:
18032 case Builtin::BI__builtin_addcl:
18033 case Builtin::BI__builtin_addcll:
18034 Result =
18035 LHS.uadd_ov(RHS, Overflow&: FirstOverflowed).uadd_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
18036 break;
18037 case Builtin::BI__builtin_subcb:
18038 case Builtin::BI__builtin_subcs:
18039 case Builtin::BI__builtin_subc:
18040 case Builtin::BI__builtin_subcl:
18041 case Builtin::BI__builtin_subcll:
18042 Result =
18043 LHS.usub_ov(RHS, Overflow&: FirstOverflowed).usub_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
18044 break;
18045 }
18046
18047 // It is possible for both overflows to happen but CGBuiltin uses an OR so
18048 // this is consistent.
18049 CarryOut = (uint64_t)(FirstOverflowed | SecondOverflowed);
18050 APValue APV{CarryOut};
18051 if (!handleAssignment(Info, E, LVal: CarryOutLValue, LValType: ResultType, Val&: APV))
18052 return false;
18053 return Success(SI: Result, E);
18054 }
18055 case Builtin::BI__builtin_add_overflow:
18056 case Builtin::BI__builtin_sub_overflow:
18057 case Builtin::BI__builtin_mul_overflow:
18058 case Builtin::BI__builtin_sadd_overflow:
18059 case Builtin::BI__builtin_uadd_overflow:
18060 case Builtin::BI__builtin_uaddl_overflow:
18061 case Builtin::BI__builtin_uaddll_overflow:
18062 case Builtin::BI__builtin_usub_overflow:
18063 case Builtin::BI__builtin_usubl_overflow:
18064 case Builtin::BI__builtin_usubll_overflow:
18065 case Builtin::BI__builtin_umul_overflow:
18066 case Builtin::BI__builtin_umull_overflow:
18067 case Builtin::BI__builtin_umulll_overflow:
18068 case Builtin::BI__builtin_saddl_overflow:
18069 case Builtin::BI__builtin_saddll_overflow:
18070 case Builtin::BI__builtin_ssub_overflow:
18071 case Builtin::BI__builtin_ssubl_overflow:
18072 case Builtin::BI__builtin_ssubll_overflow:
18073 case Builtin::BI__builtin_smul_overflow:
18074 case Builtin::BI__builtin_smull_overflow:
18075 case Builtin::BI__builtin_smulll_overflow: {
18076 LValue ResultLValue;
18077 APSInt LHS, RHS;
18078
18079 QualType ResultType = E->getArg(Arg: 2)->getType()->getPointeeType();
18080 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18081 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18082 !EvaluatePointer(E: E->getArg(Arg: 2), Result&: ResultLValue, Info))
18083 return false;
18084
18085 APSInt Result;
18086 bool DidOverflow = false;
18087
18088 // If the types don't have to match, enlarge all 3 to the largest of them.
18089 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
18090 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
18091 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
18092 bool IsSigned = LHS.isSigned() || RHS.isSigned() ||
18093 ResultType->isSignedIntegerOrEnumerationType();
18094 bool AllSigned = LHS.isSigned() && RHS.isSigned() &&
18095 ResultType->isSignedIntegerOrEnumerationType();
18096 uint64_t LHSSize = LHS.getBitWidth();
18097 uint64_t RHSSize = RHS.getBitWidth();
18098 uint64_t ResultSize = Info.Ctx.getIntWidth(T: ResultType);
18099 uint64_t MaxBits = std::max(a: std::max(a: LHSSize, b: RHSSize), b: ResultSize);
18100
18101 // Add an additional bit if the signedness isn't uniformly agreed to. We
18102 // could do this ONLY if there is a signed and an unsigned that both have
18103 // MaxBits, but the code to check that is pretty nasty. The issue will be
18104 // caught in the shrink-to-result later anyway.
18105 if (IsSigned && !AllSigned)
18106 ++MaxBits;
18107
18108 LHS = APSInt(LHS.extOrTrunc(width: MaxBits), !IsSigned);
18109 RHS = APSInt(RHS.extOrTrunc(width: MaxBits), !IsSigned);
18110 Result = APSInt(MaxBits, !IsSigned);
18111 }
18112
18113 // Find largest int.
18114 switch (BuiltinOp) {
18115 default:
18116 llvm_unreachable("Invalid value for BuiltinOp");
18117 case Builtin::BI__builtin_add_overflow:
18118 case Builtin::BI__builtin_sadd_overflow:
18119 case Builtin::BI__builtin_saddl_overflow:
18120 case Builtin::BI__builtin_saddll_overflow:
18121 case Builtin::BI__builtin_uadd_overflow:
18122 case Builtin::BI__builtin_uaddl_overflow:
18123 case Builtin::BI__builtin_uaddll_overflow:
18124 Result = LHS.isSigned() ? LHS.sadd_ov(RHS, Overflow&: DidOverflow)
18125 : LHS.uadd_ov(RHS, Overflow&: DidOverflow);
18126 break;
18127 case Builtin::BI__builtin_sub_overflow:
18128 case Builtin::BI__builtin_ssub_overflow:
18129 case Builtin::BI__builtin_ssubl_overflow:
18130 case Builtin::BI__builtin_ssubll_overflow:
18131 case Builtin::BI__builtin_usub_overflow:
18132 case Builtin::BI__builtin_usubl_overflow:
18133 case Builtin::BI__builtin_usubll_overflow:
18134 Result = LHS.isSigned() ? LHS.ssub_ov(RHS, Overflow&: DidOverflow)
18135 : LHS.usub_ov(RHS, Overflow&: DidOverflow);
18136 break;
18137 case Builtin::BI__builtin_mul_overflow:
18138 case Builtin::BI__builtin_smul_overflow:
18139 case Builtin::BI__builtin_smull_overflow:
18140 case Builtin::BI__builtin_smulll_overflow:
18141 case Builtin::BI__builtin_umul_overflow:
18142 case Builtin::BI__builtin_umull_overflow:
18143 case Builtin::BI__builtin_umulll_overflow:
18144 Result = LHS.isSigned() ? LHS.smul_ov(RHS, Overflow&: DidOverflow)
18145 : LHS.umul_ov(RHS, Overflow&: DidOverflow);
18146 break;
18147 }
18148
18149 // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead,
18150 // since it will give us the behavior of a TruncOrSelf in the case where
18151 // its parameter <= its size. We previously set Result to be at least the
18152 // integer width of the result, so getIntWidth(ResultType) <=
18153 // Result.BitWidth will work exactly like TruncOrSelf.
18154 APSInt Temp = Result.extOrTrunc(width: Info.Ctx.getIntWidth(T: ResultType));
18155 Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType());
18156
18157 // In the case where multiple sizes are allowed, truncate and see if
18158 // the values are the same.
18159 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
18160 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
18161 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
18162 if (!APSInt::isSameValue(I1: Temp, I2: Result))
18163 DidOverflow = true;
18164 }
18165 Result = Temp;
18166
18167 APValue APV{Result};
18168 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
18169 return false;
18170 return Success(Value: DidOverflow, E);
18171 }
18172
18173 case Builtin::BI__builtin_reduce_add:
18174 case Builtin::BI__builtin_reduce_mul:
18175 case Builtin::BI__builtin_reduce_and:
18176 case Builtin::BI__builtin_reduce_or:
18177 case Builtin::BI__builtin_reduce_xor:
18178 case Builtin::BI__builtin_reduce_min:
18179 case Builtin::BI__builtin_reduce_max: {
18180 APValue Source;
18181 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
18182 return false;
18183
18184 unsigned SourceLen = Source.getVectorLength();
18185 APSInt Reduced = Source.getVectorElt(I: 0).getInt();
18186 for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) {
18187 switch (BuiltinOp) {
18188 default:
18189 return false;
18190 case Builtin::BI__builtin_reduce_add: {
18191 if (!CheckedIntArithmetic(
18192 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
18193 BitWidth: Reduced.getBitWidth() + 1, Op: std::plus<APSInt>(), Result&: Reduced))
18194 return false;
18195 break;
18196 }
18197 case Builtin::BI__builtin_reduce_mul: {
18198 if (!CheckedIntArithmetic(
18199 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
18200 BitWidth: Reduced.getBitWidth() * 2, Op: std::multiplies<APSInt>(), Result&: Reduced))
18201 return false;
18202 break;
18203 }
18204 case Builtin::BI__builtin_reduce_and: {
18205 Reduced &= Source.getVectorElt(I: EltNum).getInt();
18206 break;
18207 }
18208 case Builtin::BI__builtin_reduce_or: {
18209 Reduced |= Source.getVectorElt(I: EltNum).getInt();
18210 break;
18211 }
18212 case Builtin::BI__builtin_reduce_xor: {
18213 Reduced ^= Source.getVectorElt(I: EltNum).getInt();
18214 break;
18215 }
18216 case Builtin::BI__builtin_reduce_min: {
18217 Reduced = std::min(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
18218 break;
18219 }
18220 case Builtin::BI__builtin_reduce_max: {
18221 Reduced = std::max(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
18222 break;
18223 }
18224 }
18225 }
18226
18227 return Success(SI: Reduced, E);
18228 }
18229
18230 case clang::X86::BI__builtin_ia32_addcarryx_u32:
18231 case clang::X86::BI__builtin_ia32_addcarryx_u64:
18232 case clang::X86::BI__builtin_ia32_subborrow_u32:
18233 case clang::X86::BI__builtin_ia32_subborrow_u64: {
18234 LValue ResultLValue;
18235 APSInt CarryIn, LHS, RHS;
18236 QualType ResultType = E->getArg(Arg: 3)->getType()->getPointeeType();
18237 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CarryIn, Info) ||
18238 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: LHS, Info) ||
18239 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: RHS, Info) ||
18240 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: ResultLValue, Info))
18241 return false;
18242
18243 bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||
18244 BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;
18245
18246 unsigned BitWidth = LHS.getBitWidth();
18247 unsigned CarryInBit = CarryIn.ugt(RHS: 0) ? 1 : 0;
18248 APInt ExResult =
18249 IsAdd
18250 ? (LHS.zext(width: BitWidth + 1) + (RHS.zext(width: BitWidth + 1) + CarryInBit))
18251 : (LHS.zext(width: BitWidth + 1) - (RHS.zext(width: BitWidth + 1) + CarryInBit));
18252
18253 APInt Result = ExResult.extractBits(numBits: BitWidth, bitPosition: 0);
18254 uint64_t CarryOut = ExResult.extractBitsAsZExtValue(numBits: 1, bitPosition: BitWidth);
18255
18256 APValue APV{APSInt(Result, /*isUnsigned=*/true)};
18257 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
18258 return false;
18259 return Success(Value: CarryOut, E);
18260 }
18261
18262 case clang::X86::BI__builtin_ia32_movmskps:
18263 case clang::X86::BI__builtin_ia32_movmskpd:
18264 case clang::X86::BI__builtin_ia32_pmovmskb128:
18265 case clang::X86::BI__builtin_ia32_pmovmskb256:
18266 case clang::X86::BI__builtin_ia32_movmskps256:
18267 case clang::X86::BI__builtin_ia32_movmskpd256: {
18268 APValue Source;
18269 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
18270 return false;
18271 unsigned SourceLen = Source.getVectorLength();
18272 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
18273 QualType ElemQT = VT->getElementType();
18274 unsigned ResultLen = Info.Ctx.getTypeSize(
18275 T: E->getCallReturnType(Ctx: Info.Ctx)); // Always 32-bit integer.
18276 APInt Result(ResultLen, 0);
18277
18278 for (unsigned I = 0; I != SourceLen; ++I) {
18279 APInt Elem;
18280 if (ElemQT->isIntegerType()) {
18281 Elem = Source.getVectorElt(I).getInt();
18282 } else if (ElemQT->isRealFloatingType()) {
18283 Elem = Source.getVectorElt(I).getFloat().bitcastToAPInt();
18284 } else {
18285 return false;
18286 }
18287 Result.setBitVal(BitPosition: I, BitValue: Elem.isNegative());
18288 }
18289 return Success(I: Result, E);
18290 }
18291
18292 case clang::X86::BI__builtin_ia32_bextr_u32:
18293 case clang::X86::BI__builtin_ia32_bextr_u64:
18294 case clang::X86::BI__builtin_ia32_bextri_u32:
18295 case clang::X86::BI__builtin_ia32_bextri_u64: {
18296 APSInt Val, Idx;
18297 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18298 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
18299 return false;
18300
18301 unsigned BitWidth = Val.getBitWidth();
18302 uint64_t Shift = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
18303 uint64_t Length = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 8);
18304 Length = Length > BitWidth ? BitWidth : Length;
18305
18306 // Handle out of bounds cases.
18307 if (Length == 0 || Shift >= BitWidth)
18308 return Success(Value: 0, E);
18309
18310 uint64_t Result = Val.getZExtValue() >> Shift;
18311 Result &= llvm::maskTrailingOnes<uint64_t>(N: Length);
18312 return Success(Value: Result, E);
18313 }
18314
18315 case clang::X86::BI__builtin_ia32_bzhi_si:
18316 case clang::X86::BI__builtin_ia32_bzhi_di: {
18317 APSInt Val, Idx;
18318 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18319 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
18320 return false;
18321
18322 unsigned BitWidth = Val.getBitWidth();
18323 unsigned Index = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
18324 if (Index < BitWidth)
18325 Val.clearHighBits(hiBits: BitWidth - Index);
18326 return Success(SI: Val, E);
18327 }
18328
18329 case clang::X86::BI__builtin_ia32_ktestcqi:
18330 case clang::X86::BI__builtin_ia32_ktestchi:
18331 case clang::X86::BI__builtin_ia32_ktestcsi:
18332 case clang::X86::BI__builtin_ia32_ktestcdi: {
18333 APSInt A, B;
18334 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18335 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18336 return false;
18337
18338 return Success(Value: (~A & B) == 0, E);
18339 }
18340
18341 case clang::X86::BI__builtin_ia32_ktestzqi:
18342 case clang::X86::BI__builtin_ia32_ktestzhi:
18343 case clang::X86::BI__builtin_ia32_ktestzsi:
18344 case clang::X86::BI__builtin_ia32_ktestzdi: {
18345 APSInt A, B;
18346 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18347 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18348 return false;
18349
18350 return Success(Value: (A & B) == 0, E);
18351 }
18352
18353 case clang::X86::BI__builtin_ia32_kortestcqi:
18354 case clang::X86::BI__builtin_ia32_kortestchi:
18355 case clang::X86::BI__builtin_ia32_kortestcsi:
18356 case clang::X86::BI__builtin_ia32_kortestcdi: {
18357 APSInt A, B;
18358 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18359 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18360 return false;
18361
18362 return Success(Value: ~(A | B) == 0, E);
18363 }
18364
18365 case clang::X86::BI__builtin_ia32_kortestzqi:
18366 case clang::X86::BI__builtin_ia32_kortestzhi:
18367 case clang::X86::BI__builtin_ia32_kortestzsi:
18368 case clang::X86::BI__builtin_ia32_kortestzdi: {
18369 APSInt A, B;
18370 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18371 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18372 return false;
18373
18374 return Success(Value: (A | B) == 0, E);
18375 }
18376
18377 case clang::X86::BI__builtin_ia32_kunpckhi:
18378 case clang::X86::BI__builtin_ia32_kunpckdi:
18379 case clang::X86::BI__builtin_ia32_kunpcksi: {
18380 APSInt A, B;
18381 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18382 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18383 return false;
18384
18385 // Generic kunpack: extract lower half of each operand and concatenate
18386 // Result = A[HalfWidth-1:0] concat B[HalfWidth-1:0]
18387 unsigned BW = A.getBitWidth();
18388 APSInt Result(A.trunc(width: BW / 2).concat(NewLSB: B.trunc(width: BW / 2)), A.isUnsigned());
18389 return Success(SI: Result, E);
18390 }
18391
18392 case clang::X86::BI__builtin_ia32_lzcnt_u16:
18393 case clang::X86::BI__builtin_ia32_lzcnt_u32:
18394 case clang::X86::BI__builtin_ia32_lzcnt_u64: {
18395 APSInt Val;
18396 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18397 return false;
18398 return Success(Value: Val.countLeadingZeros(), E);
18399 }
18400
18401 case clang::X86::BI__builtin_ia32_tzcnt_u16:
18402 case clang::X86::BI__builtin_ia32_tzcnt_u32:
18403 case clang::X86::BI__builtin_ia32_tzcnt_u64: {
18404 APSInt Val;
18405 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18406 return false;
18407 return Success(Value: Val.countTrailingZeros(), E);
18408 }
18409
18410 case clang::X86::BI__builtin_ia32_pdep_si:
18411 case clang::X86::BI__builtin_ia32_pdep_di:
18412 case Builtin::BI__builtin_elementwise_pdep: {
18413 APSInt Val, Msk;
18414 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18415 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18416 return false;
18417 return Success(I: llvm::APIntOps::pdep(Val, Mask: Msk), E);
18418 }
18419
18420 case clang::X86::BI__builtin_ia32_pext_si:
18421 case clang::X86::BI__builtin_ia32_pext_di:
18422 case Builtin::BI__builtin_elementwise_pext: {
18423 APSInt Val, Msk;
18424 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18425 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18426 return false;
18427 return Success(I: llvm::APIntOps::pext(Val, Mask: Msk), E);
18428 }
18429 case X86::BI__builtin_ia32_ptestz128:
18430 case X86::BI__builtin_ia32_ptestz256:
18431 case X86::BI__builtin_ia32_vtestzps:
18432 case X86::BI__builtin_ia32_vtestzps256:
18433 case X86::BI__builtin_ia32_vtestzpd:
18434 case X86::BI__builtin_ia32_vtestzpd256: {
18435 return EvalTestOp(
18436 [](const APInt &A, const APInt &B) { return (A & B) == 0; });
18437 }
18438 case X86::BI__builtin_ia32_ptestc128:
18439 case X86::BI__builtin_ia32_ptestc256:
18440 case X86::BI__builtin_ia32_vtestcps:
18441 case X86::BI__builtin_ia32_vtestcps256:
18442 case X86::BI__builtin_ia32_vtestcpd:
18443 case X86::BI__builtin_ia32_vtestcpd256: {
18444 return EvalTestOp(
18445 [](const APInt &A, const APInt &B) { return (~A & B) == 0; });
18446 }
18447 case X86::BI__builtin_ia32_ptestnzc128:
18448 case X86::BI__builtin_ia32_ptestnzc256:
18449 case X86::BI__builtin_ia32_vtestnzcps:
18450 case X86::BI__builtin_ia32_vtestnzcps256:
18451 case X86::BI__builtin_ia32_vtestnzcpd:
18452 case X86::BI__builtin_ia32_vtestnzcpd256: {
18453 return EvalTestOp([](const APInt &A, const APInt &B) {
18454 return ((A & B) != 0) && ((~A & B) != 0);
18455 });
18456 }
18457 case X86::BI__builtin_ia32_kandqi:
18458 case X86::BI__builtin_ia32_kandhi:
18459 case X86::BI__builtin_ia32_kandsi:
18460 case X86::BI__builtin_ia32_kanddi: {
18461 return HandleMaskBinOp(
18462 [](const APSInt &LHS, const APSInt &RHS) { return LHS & RHS; });
18463 }
18464
18465 case X86::BI__builtin_ia32_kandnqi:
18466 case X86::BI__builtin_ia32_kandnhi:
18467 case X86::BI__builtin_ia32_kandnsi:
18468 case X86::BI__builtin_ia32_kandndi: {
18469 return HandleMaskBinOp(
18470 [](const APSInt &LHS, const APSInt &RHS) { return ~LHS & RHS; });
18471 }
18472
18473 case X86::BI__builtin_ia32_korqi:
18474 case X86::BI__builtin_ia32_korhi:
18475 case X86::BI__builtin_ia32_korsi:
18476 case X86::BI__builtin_ia32_kordi: {
18477 return HandleMaskBinOp(
18478 [](const APSInt &LHS, const APSInt &RHS) { return LHS | RHS; });
18479 }
18480
18481 case X86::BI__builtin_ia32_kxnorqi:
18482 case X86::BI__builtin_ia32_kxnorhi:
18483 case X86::BI__builtin_ia32_kxnorsi:
18484 case X86::BI__builtin_ia32_kxnordi: {
18485 return HandleMaskBinOp(
18486 [](const APSInt &LHS, const APSInt &RHS) { return ~(LHS ^ RHS); });
18487 }
18488
18489 case X86::BI__builtin_ia32_kxorqi:
18490 case X86::BI__builtin_ia32_kxorhi:
18491 case X86::BI__builtin_ia32_kxorsi:
18492 case X86::BI__builtin_ia32_kxordi: {
18493 return HandleMaskBinOp(
18494 [](const APSInt &LHS, const APSInt &RHS) { return LHS ^ RHS; });
18495 }
18496
18497 case X86::BI__builtin_ia32_knotqi:
18498 case X86::BI__builtin_ia32_knothi:
18499 case X86::BI__builtin_ia32_knotsi:
18500 case X86::BI__builtin_ia32_knotdi: {
18501 APSInt Val;
18502 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18503 return false;
18504 APSInt Result = ~Val;
18505 return Success(V: APValue(Result), E);
18506 }
18507
18508 case X86::BI__builtin_ia32_kaddqi:
18509 case X86::BI__builtin_ia32_kaddhi:
18510 case X86::BI__builtin_ia32_kaddsi:
18511 case X86::BI__builtin_ia32_kadddi: {
18512 return HandleMaskBinOp(
18513 [](const APSInt &LHS, const APSInt &RHS) { return LHS + RHS; });
18514 }
18515
18516 case X86::BI__builtin_ia32_kmovb:
18517 case X86::BI__builtin_ia32_kmovw:
18518 case X86::BI__builtin_ia32_kmovd:
18519 case X86::BI__builtin_ia32_kmovq: {
18520 APSInt Val;
18521 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18522 return false;
18523 return Success(SI: Val, E);
18524 }
18525
18526 case X86::BI__builtin_ia32_kshiftliqi:
18527 case X86::BI__builtin_ia32_kshiftlihi:
18528 case X86::BI__builtin_ia32_kshiftlisi:
18529 case X86::BI__builtin_ia32_kshiftlidi: {
18530 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18531 unsigned Amt = RHS.getZExtValue() & 0xFF;
18532 if (Amt >= LHS.getBitWidth())
18533 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18534 return APSInt(LHS.shl(shiftAmt: Amt), LHS.isUnsigned());
18535 });
18536 }
18537
18538 case X86::BI__builtin_ia32_kshiftriqi:
18539 case X86::BI__builtin_ia32_kshiftrihi:
18540 case X86::BI__builtin_ia32_kshiftrisi:
18541 case X86::BI__builtin_ia32_kshiftridi: {
18542 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18543 unsigned Amt = RHS.getZExtValue() & 0xFF;
18544 if (Amt >= LHS.getBitWidth())
18545 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18546 return APSInt(LHS.lshr(shiftAmt: Amt), LHS.isUnsigned());
18547 });
18548 }
18549
18550 case clang::X86::BI__builtin_ia32_vec_ext_v4hi:
18551 case clang::X86::BI__builtin_ia32_vec_ext_v16qi:
18552 case clang::X86::BI__builtin_ia32_vec_ext_v8hi:
18553 case clang::X86::BI__builtin_ia32_vec_ext_v4si:
18554 case clang::X86::BI__builtin_ia32_vec_ext_v2di:
18555 case clang::X86::BI__builtin_ia32_vec_ext_v32qi:
18556 case clang::X86::BI__builtin_ia32_vec_ext_v16hi:
18557 case clang::X86::BI__builtin_ia32_vec_ext_v8si:
18558 case clang::X86::BI__builtin_ia32_vec_ext_v4di: {
18559 APValue Vec;
18560 APSInt IdxAPS;
18561 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
18562 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
18563 return false;
18564 unsigned N = Vec.getVectorLength();
18565 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
18566 return Success(SI: Vec.getVectorElt(I: Idx).getInt(), E);
18567 }
18568
18569 case clang::X86::BI__builtin_ia32_cvtb2mask128:
18570 case clang::X86::BI__builtin_ia32_cvtb2mask256:
18571 case clang::X86::BI__builtin_ia32_cvtb2mask512:
18572 case clang::X86::BI__builtin_ia32_cvtw2mask128:
18573 case clang::X86::BI__builtin_ia32_cvtw2mask256:
18574 case clang::X86::BI__builtin_ia32_cvtw2mask512:
18575 case clang::X86::BI__builtin_ia32_cvtd2mask128:
18576 case clang::X86::BI__builtin_ia32_cvtd2mask256:
18577 case clang::X86::BI__builtin_ia32_cvtd2mask512:
18578 case clang::X86::BI__builtin_ia32_cvtq2mask128:
18579 case clang::X86::BI__builtin_ia32_cvtq2mask256:
18580 case clang::X86::BI__builtin_ia32_cvtq2mask512: {
18581 assert(E->getNumArgs() == 1);
18582 APValue Vec;
18583 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
18584 return false;
18585
18586 unsigned VectorLen = Vec.getVectorLength();
18587 unsigned RetWidth = Info.Ctx.getIntWidth(T: E->getType());
18588 llvm::APInt Bits(RetWidth, 0);
18589
18590 for (unsigned ElemNum = 0; ElemNum != VectorLen; ++ElemNum) {
18591 const APSInt &A = Vec.getVectorElt(I: ElemNum).getInt();
18592 unsigned MSB = A[A.getBitWidth() - 1];
18593 Bits.setBitVal(BitPosition: ElemNum, BitValue: MSB);
18594 }
18595
18596 APSInt RetMask(Bits, /*isUnsigned=*/true);
18597 return Success(V: APValue(RetMask), E);
18598 }
18599
18600 case clang::X86::BI__builtin_ia32_cmpb128_mask:
18601 case clang::X86::BI__builtin_ia32_cmpw128_mask:
18602 case clang::X86::BI__builtin_ia32_cmpd128_mask:
18603 case clang::X86::BI__builtin_ia32_cmpq128_mask:
18604 case clang::X86::BI__builtin_ia32_cmpb256_mask:
18605 case clang::X86::BI__builtin_ia32_cmpw256_mask:
18606 case clang::X86::BI__builtin_ia32_cmpd256_mask:
18607 case clang::X86::BI__builtin_ia32_cmpq256_mask:
18608 case clang::X86::BI__builtin_ia32_cmpb512_mask:
18609 case clang::X86::BI__builtin_ia32_cmpw512_mask:
18610 case clang::X86::BI__builtin_ia32_cmpd512_mask:
18611 case clang::X86::BI__builtin_ia32_cmpq512_mask:
18612 case clang::X86::BI__builtin_ia32_ucmpb128_mask:
18613 case clang::X86::BI__builtin_ia32_ucmpw128_mask:
18614 case clang::X86::BI__builtin_ia32_ucmpd128_mask:
18615 case clang::X86::BI__builtin_ia32_ucmpq128_mask:
18616 case clang::X86::BI__builtin_ia32_ucmpb256_mask:
18617 case clang::X86::BI__builtin_ia32_ucmpw256_mask:
18618 case clang::X86::BI__builtin_ia32_ucmpd256_mask:
18619 case clang::X86::BI__builtin_ia32_ucmpq256_mask:
18620 case clang::X86::BI__builtin_ia32_ucmpb512_mask:
18621 case clang::X86::BI__builtin_ia32_ucmpw512_mask:
18622 case clang::X86::BI__builtin_ia32_ucmpd512_mask:
18623 case clang::X86::BI__builtin_ia32_ucmpq512_mask: {
18624 assert(E->getNumArgs() == 4);
18625
18626 bool IsUnsigned =
18627 (BuiltinOp >= clang::X86::BI__builtin_ia32_ucmpb128_mask &&
18628 BuiltinOp <= clang::X86::BI__builtin_ia32_ucmpw512_mask);
18629
18630 APValue LHS, RHS;
18631 APSInt Mask, Opcode;
18632 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18633 !EvaluateVector(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18634 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Opcode, Info) ||
18635 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: Mask, Info))
18636 return false;
18637
18638 assert(LHS.getVectorLength() == RHS.getVectorLength());
18639
18640 unsigned VectorLen = LHS.getVectorLength();
18641 unsigned RetWidth = Mask.getBitWidth();
18642
18643 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
18644
18645 for (unsigned ElemNum = 0; ElemNum < VectorLen; ++ElemNum) {
18646 const APSInt &A = LHS.getVectorElt(I: ElemNum).getInt();
18647 const APSInt &B = RHS.getVectorElt(I: ElemNum).getInt();
18648 bool Result = false;
18649
18650 switch (Opcode.getExtValue() & 0x7) {
18651 case 0: // _MM_CMPINT_EQ
18652 Result = (A == B);
18653 break;
18654 case 1: // _MM_CMPINT_LT
18655 Result = IsUnsigned ? A.ult(RHS: B) : A.slt(RHS: B);
18656 break;
18657 case 2: // _MM_CMPINT_LE
18658 Result = IsUnsigned ? A.ule(RHS: B) : A.sle(RHS: B);
18659 break;
18660 case 3: // _MM_CMPINT_FALSE
18661 Result = false;
18662 break;
18663 case 4: // _MM_CMPINT_NE
18664 Result = (A != B);
18665 break;
18666 case 5: // _MM_CMPINT_NLT (>=)
18667 Result = IsUnsigned ? A.uge(RHS: B) : A.sge(RHS: B);
18668 break;
18669 case 6: // _MM_CMPINT_NLE (>)
18670 Result = IsUnsigned ? A.ugt(RHS: B) : A.sgt(RHS: B);
18671 break;
18672 case 7: // _MM_CMPINT_TRUE
18673 Result = true;
18674 break;
18675 }
18676
18677 RetMask.setBitVal(BitPosition: ElemNum, BitValue: Mask[ElemNum] && Result);
18678 }
18679
18680 return Success(V: APValue(RetMask), E);
18681 }
18682 case X86::BI__builtin_ia32_vpshufbitqmb128_mask:
18683 case X86::BI__builtin_ia32_vpshufbitqmb256_mask:
18684 case X86::BI__builtin_ia32_vpshufbitqmb512_mask: {
18685 assert(E->getNumArgs() == 3);
18686
18687 APValue Source, ShuffleMask;
18688 APSInt ZeroMask;
18689 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
18690 !EvaluateVector(E: E->getArg(Arg: 1), Result&: ShuffleMask, Info) ||
18691 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: ZeroMask, Info))
18692 return false;
18693
18694 assert(Source.getVectorLength() == ShuffleMask.getVectorLength());
18695 assert(ZeroMask.getBitWidth() == Source.getVectorLength());
18696
18697 unsigned NumBytesInQWord = 8;
18698 unsigned NumBitsInByte = 8;
18699 unsigned NumBytes = Source.getVectorLength();
18700 unsigned NumQWords = NumBytes / NumBytesInQWord;
18701 unsigned RetWidth = ZeroMask.getBitWidth();
18702 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
18703
18704 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
18705 APInt SourceQWord(64, 0);
18706 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
18707 uint64_t Byte = Source.getVectorElt(I: QWordId * NumBytesInQWord + ByteIdx)
18708 .getInt()
18709 .getZExtValue();
18710 SourceQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
18711 }
18712
18713 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
18714 unsigned SelIdx = QWordId * NumBytesInQWord + ByteIdx;
18715 unsigned M =
18716 ShuffleMask.getVectorElt(I: SelIdx).getInt().getZExtValue() & 0x3F;
18717 if (ZeroMask[SelIdx]) {
18718 RetMask.setBitVal(BitPosition: SelIdx, BitValue: SourceQWord[M]);
18719 }
18720 }
18721 }
18722 return Success(V: APValue(RetMask), E);
18723 }
18724 }
18725}
18726
18727/// Determine whether this is a pointer past the end of the complete
18728/// object referred to by the lvalue.
18729static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx,
18730 const LValue &LV) {
18731 // A null pointer can be viewed as being "past the end" but we don't
18732 // choose to look at it that way here.
18733 if (!LV.getLValueBase())
18734 return false;
18735
18736 // If the designator is valid and refers to a subobject, we're not pointing
18737 // past the end.
18738 if (!LV.getLValueDesignator().Invalid &&
18739 !LV.getLValueDesignator().isOnePastTheEnd())
18740 return false;
18741
18742 // A pointer to an incomplete type might be past-the-end if the type's size is
18743 // zero. We cannot tell because the type is incomplete.
18744 QualType Ty = getType(B: LV.getLValueBase());
18745 if (Ty->isIncompleteType())
18746 return true;
18747
18748 // Can't be past the end of an invalid object.
18749 if (LV.getLValueDesignator().Invalid)
18750 return false;
18751
18752 // We're a past-the-end pointer if we point to the byte after the object,
18753 // no matter what our type or path is.
18754 auto Size = Ctx.getTypeSizeInChars(T: Ty);
18755 return LV.getLValueOffset() == Size;
18756}
18757
18758namespace {
18759
18760/// Data recursive integer evaluator of certain binary operators.
18761///
18762/// We use a data recursive algorithm for binary operators so that we are able
18763/// to handle extreme cases of chained binary operators without causing stack
18764/// overflow.
18765class DataRecursiveIntBinOpEvaluator {
18766 struct EvalResult {
18767 APValue Val;
18768 bool Failed = false;
18769
18770 EvalResult() = default;
18771
18772 void swap(EvalResult &RHS) {
18773 Val.swap(RHS&: RHS.Val);
18774 Failed = RHS.Failed;
18775 RHS.Failed = false;
18776 }
18777 };
18778
18779 struct Job {
18780 const Expr *E;
18781 EvalResult LHSResult; // meaningful only for binary operator expression.
18782 enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind;
18783
18784 Job() = default;
18785 Job(Job &&) = default;
18786
18787 void startSpeculativeEval(EvalInfo &Info) {
18788 SpecEvalRAII = SpeculativeEvaluationRAII(Info);
18789 }
18790
18791 private:
18792 SpeculativeEvaluationRAII SpecEvalRAII;
18793 };
18794
18795 SmallVector<Job, 16> Queue;
18796
18797 IntExprEvaluator &IntEval;
18798 EvalInfo &Info;
18799 APValue &FinalResult;
18800
18801public:
18802 DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result)
18803 : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { }
18804
18805 /// True if \param E is a binary operator that we are going to handle
18806 /// data recursively.
18807 /// We handle binary operators that are comma, logical, or that have operands
18808 /// with integral or enumeration type.
18809 static bool shouldEnqueue(const BinaryOperator *E) {
18810 return E->getOpcode() == BO_Comma || E->isLogicalOp() ||
18811 (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() &&
18812 E->getLHS()->getType()->isIntegralOrEnumerationType() &&
18813 E->getRHS()->getType()->isIntegralOrEnumerationType());
18814 }
18815
18816 bool Traverse(const BinaryOperator *E) {
18817 enqueue(E);
18818 EvalResult PrevResult;
18819 while (!Queue.empty())
18820 process(Result&: PrevResult);
18821
18822 if (PrevResult.Failed) return false;
18823
18824 FinalResult.swap(RHS&: PrevResult.Val);
18825 return true;
18826 }
18827
18828private:
18829 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
18830 return IntEval.Success(Value, E, Result);
18831 }
18832 bool Success(const APSInt &Value, const Expr *E, APValue &Result) {
18833 return IntEval.Success(SI: Value, E, Result);
18834 }
18835 bool Error(const Expr *E) {
18836 return IntEval.Error(E);
18837 }
18838 bool Error(const Expr *E, diag::kind D) {
18839 return IntEval.Error(E, D);
18840 }
18841
18842 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
18843 return Info.CCEDiag(E, DiagId: D);
18844 }
18845
18846 // Returns true if visiting the RHS is necessary, false otherwise.
18847 bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
18848 bool &SuppressRHSDiags);
18849
18850 bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
18851 const BinaryOperator *E, APValue &Result);
18852
18853 void EvaluateExpr(const Expr *E, EvalResult &Result) {
18854 Result.Failed = !Evaluate(Result&: Result.Val, Info, E);
18855 if (Result.Failed)
18856 Result.Val = APValue();
18857 }
18858
18859 void process(EvalResult &Result);
18860
18861 void enqueue(const Expr *E) {
18862 E = E->IgnoreParens();
18863 Queue.resize(N: Queue.size()+1);
18864 Queue.back().E = E;
18865 Queue.back().Kind = Job::AnyExprKind;
18866 }
18867};
18868
18869}
18870
18871bool DataRecursiveIntBinOpEvaluator::
18872 VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
18873 bool &SuppressRHSDiags) {
18874 if (E->getOpcode() == BO_Comma) {
18875 // Ignore LHS but note if we could not evaluate it.
18876 if (LHSResult.Failed)
18877 return Info.noteSideEffect();
18878 return true;
18879 }
18880
18881 if (E->isLogicalOp()) {
18882 bool LHSAsBool;
18883 if (!LHSResult.Failed && HandleConversionToBool(Val: LHSResult.Val, Result&: LHSAsBool)) {
18884 // We were able to evaluate the LHS, see if we can get away with not
18885 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
18886 if (LHSAsBool == (E->getOpcode() == BO_LOr)) {
18887 Success(Value: LHSAsBool, E, Result&: LHSResult.Val);
18888 return false; // Ignore RHS
18889 }
18890 } else {
18891 LHSResult.Failed = true;
18892
18893 // Since we weren't able to evaluate the left hand side, it
18894 // might have had side effects.
18895 if (!Info.noteSideEffect())
18896 return false;
18897
18898 // We can't evaluate the LHS; however, sometimes the result
18899 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
18900 // Don't ignore RHS and suppress diagnostics from this arm.
18901 SuppressRHSDiags = true;
18902 }
18903
18904 return true;
18905 }
18906
18907 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
18908 E->getRHS()->getType()->isIntegralOrEnumerationType());
18909
18910 if (LHSResult.Failed && !Info.noteFailure())
18911 return false; // Ignore RHS;
18912
18913 return true;
18914}
18915
18916static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index,
18917 bool IsSub) {
18918 // Compute the new offset in the appropriate width, wrapping at 64 bits.
18919 // FIXME: When compiling for a 32-bit target, we should use 32-bit
18920 // offsets.
18921 assert(!LVal.hasLValuePath() && "have designator for integer lvalue");
18922 CharUnits &Offset = LVal.getLValueOffset();
18923 uint64_t Offset64 = Offset.getQuantity();
18924 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
18925 Offset = CharUnits::fromQuantity(Quantity: IsSub ? Offset64 - Index64
18926 : Offset64 + Index64);
18927}
18928
18929bool DataRecursiveIntBinOpEvaluator::
18930 VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
18931 const BinaryOperator *E, APValue &Result) {
18932 if (E->getOpcode() == BO_Comma) {
18933 if (RHSResult.Failed)
18934 return false;
18935 Result = RHSResult.Val;
18936 return true;
18937 }
18938
18939 if (E->isLogicalOp()) {
18940 bool lhsResult, rhsResult;
18941 bool LHSIsOK = HandleConversionToBool(Val: LHSResult.Val, Result&: lhsResult);
18942 bool RHSIsOK = HandleConversionToBool(Val: RHSResult.Val, Result&: rhsResult);
18943
18944 if (LHSIsOK) {
18945 if (RHSIsOK) {
18946 if (E->getOpcode() == BO_LOr)
18947 return Success(Value: lhsResult || rhsResult, E, Result);
18948 else
18949 return Success(Value: lhsResult && rhsResult, E, Result);
18950 }
18951 } else {
18952 if (RHSIsOK) {
18953 // We can't evaluate the LHS; however, sometimes the result
18954 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
18955 if (rhsResult == (E->getOpcode() == BO_LOr))
18956 return Success(Value: rhsResult, E, Result);
18957 }
18958 }
18959
18960 return false;
18961 }
18962
18963 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
18964 E->getRHS()->getType()->isIntegralOrEnumerationType());
18965
18966 if (LHSResult.Failed || RHSResult.Failed)
18967 return false;
18968
18969 const APValue &LHSVal = LHSResult.Val;
18970 const APValue &RHSVal = RHSResult.Val;
18971
18972 // Handle cases like (unsigned long)&a + 4.
18973 if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
18974 Result = LHSVal;
18975 addOrSubLValueAsInteger(LVal&: Result, Index: RHSVal.getInt(), IsSub: E->getOpcode() == BO_Sub);
18976 return true;
18977 }
18978
18979 // Handle cases like 4 + (unsigned long)&a
18980 if (E->getOpcode() == BO_Add &&
18981 RHSVal.isLValue() && LHSVal.isInt()) {
18982 Result = RHSVal;
18983 addOrSubLValueAsInteger(LVal&: Result, Index: LHSVal.getInt(), /*IsSub*/false);
18984 return true;
18985 }
18986
18987 if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
18988 // Handle (intptr_t)&&A - (intptr_t)&&B.
18989 if (!LHSVal.getLValueOffset().isZero() ||
18990 !RHSVal.getLValueOffset().isZero())
18991 return false;
18992 const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
18993 const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
18994 if (!LHSExpr || !RHSExpr)
18995 return false;
18996 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
18997 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
18998 if (!LHSAddrExpr || !RHSAddrExpr)
18999 return false;
19000 // Make sure both labels come from the same function.
19001 if (LHSAddrExpr->getLabel()->getDeclContext() !=
19002 RHSAddrExpr->getLabel()->getDeclContext())
19003 return false;
19004 Result = APValue(LHSAddrExpr, RHSAddrExpr);
19005 return true;
19006 }
19007
19008 // All the remaining cases expect both operands to be an integer
19009 if (!LHSVal.isInt() || !RHSVal.isInt())
19010 return Error(E);
19011
19012 // Set up the width and signedness manually, in case it can't be deduced
19013 // from the operation we're performing.
19014 // FIXME: Don't do this in the cases where we can deduce it.
19015 APSInt Value(Info.Ctx.getIntWidth(T: E->getType()),
19016 E->getType()->isUnsignedIntegerOrEnumerationType());
19017 if (!handleIntIntBinOp(Info, E, LHS: LHSVal.getInt(), Opcode: E->getOpcode(),
19018 RHS: RHSVal.getInt(), Result&: Value))
19019 return false;
19020 return Success(Value, E, Result);
19021}
19022
19023void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) {
19024 Job &job = Queue.back();
19025
19026 switch (job.Kind) {
19027 case Job::AnyExprKind: {
19028 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(Val: job.E)) {
19029 if (shouldEnqueue(E: Bop)) {
19030 job.Kind = Job::BinOpKind;
19031 enqueue(E: Bop->getLHS());
19032 return;
19033 }
19034 }
19035
19036 EvaluateExpr(E: job.E, Result);
19037 Queue.pop_back();
19038 return;
19039 }
19040
19041 case Job::BinOpKind: {
19042 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
19043 bool SuppressRHSDiags = false;
19044 if (!VisitBinOpLHSOnly(LHSResult&: Result, E: Bop, SuppressRHSDiags)) {
19045 Queue.pop_back();
19046 return;
19047 }
19048 if (SuppressRHSDiags)
19049 job.startSpeculativeEval(Info);
19050 job.LHSResult.swap(RHS&: Result);
19051 job.Kind = Job::BinOpVisitedLHSKind;
19052 enqueue(E: Bop->getRHS());
19053 return;
19054 }
19055
19056 case Job::BinOpVisitedLHSKind: {
19057 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
19058 EvalResult RHS;
19059 RHS.swap(RHS&: Result);
19060 Result.Failed = !VisitBinOp(LHSResult: job.LHSResult, RHSResult: RHS, E: Bop, Result&: Result.Val);
19061 Queue.pop_back();
19062 return;
19063 }
19064 }
19065
19066 llvm_unreachable("Invalid Job::Kind!");
19067}
19068
19069namespace {
19070enum class CmpResult {
19071 Unequal,
19072 Less,
19073 Equal,
19074 Greater,
19075 Unordered,
19076};
19077}
19078
19079template <class SuccessCB, class AfterCB>
19080static bool
19081EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
19082 SuccessCB &&Success, AfterCB &&DoAfter) {
19083 assert(!E->isValueDependent());
19084 assert(E->isComparisonOp() && "expected comparison operator");
19085 assert((E->getOpcode() == BO_Cmp ||
19086 E->getType()->isIntegralOrEnumerationType()) &&
19087 "unsupported binary expression evaluation");
19088 auto Error = [&](const Expr *E) {
19089 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
19090 return false;
19091 };
19092
19093 bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp;
19094 bool IsEquality = E->isEqualityOp();
19095
19096 QualType LHSTy = E->getLHS()->getType();
19097 QualType RHSTy = E->getRHS()->getType();
19098
19099 if (LHSTy->isIntegralOrEnumerationType() &&
19100 RHSTy->isIntegralOrEnumerationType()) {
19101 APSInt LHS, RHS;
19102 bool LHSOK = EvaluateInteger(E: E->getLHS(), Result&: LHS, Info);
19103 if (!LHSOK && !Info.noteFailure())
19104 return false;
19105 if (!EvaluateInteger(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
19106 return false;
19107 if (LHS < RHS)
19108 return Success(CmpResult::Less, E);
19109 if (LHS > RHS)
19110 return Success(CmpResult::Greater, E);
19111 return Success(CmpResult::Equal, E);
19112 }
19113
19114 if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) {
19115 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHSTy));
19116 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHSTy));
19117
19118 bool LHSOK = EvaluateFixedPointOrInteger(E: E->getLHS(), Result&: LHSFX, Info);
19119 if (!LHSOK && !Info.noteFailure())
19120 return false;
19121 if (!EvaluateFixedPointOrInteger(E: E->getRHS(), Result&: RHSFX, Info) || !LHSOK)
19122 return false;
19123 if (LHSFX < RHSFX)
19124 return Success(CmpResult::Less, E);
19125 if (LHSFX > RHSFX)
19126 return Success(CmpResult::Greater, E);
19127 return Success(CmpResult::Equal, E);
19128 }
19129
19130 if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) {
19131 ComplexValue LHS, RHS;
19132 bool LHSOK;
19133 if (E->isAssignmentOp()) {
19134 LValue LV;
19135 EvaluateLValue(E: E->getLHS(), Result&: LV, Info);
19136 LHSOK = false;
19137 } else if (LHSTy->isRealFloatingType()) {
19138 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: LHS.FloatReal, Info);
19139 if (LHSOK) {
19140 LHS.makeComplexFloat();
19141 LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics());
19142 }
19143 } else {
19144 LHSOK = EvaluateComplex(E: E->getLHS(), Res&: LHS, Info);
19145 }
19146 if (!LHSOK && !Info.noteFailure())
19147 return false;
19148
19149 if (E->getRHS()->getType()->isRealFloatingType()) {
19150 if (!EvaluateFloat(E: E->getRHS(), Result&: RHS.FloatReal, Info) || !LHSOK)
19151 return false;
19152 RHS.makeComplexFloat();
19153 RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics());
19154 } else if (!EvaluateComplex(E: E->getRHS(), Res&: RHS, Info) || !LHSOK)
19155 return false;
19156
19157 if (LHS.isComplexFloat()) {
19158 APFloat::cmpResult CR_r =
19159 LHS.getComplexFloatReal().compare(RHS: RHS.getComplexFloatReal());
19160 APFloat::cmpResult CR_i =
19161 LHS.getComplexFloatImag().compare(RHS: RHS.getComplexFloatImag());
19162 bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual;
19163 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
19164 } else {
19165 assert(IsEquality && "invalid complex comparison");
19166 bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
19167 LHS.getComplexIntImag() == RHS.getComplexIntImag();
19168 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
19169 }
19170 }
19171
19172 if (LHSTy->isRealFloatingType() &&
19173 RHSTy->isRealFloatingType()) {
19174 APFloat RHS(0.0), LHS(0.0);
19175
19176 bool LHSOK = EvaluateFloat(E: E->getRHS(), Result&: RHS, Info);
19177 if (!LHSOK && !Info.noteFailure())
19178 return false;
19179
19180 if (!EvaluateFloat(E: E->getLHS(), Result&: LHS, Info) || !LHSOK)
19181 return false;
19182
19183 assert(E->isComparisonOp() && "Invalid binary operator!");
19184 llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS);
19185 if (!Info.InConstantContext &&
19186 APFloatCmpResult == APFloat::cmpUnordered &&
19187 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained()) {
19188 // Note: Compares may raise invalid in some cases involving NaN or sNaN.
19189 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
19190 return false;
19191 }
19192 auto GetCmpRes = [&]() {
19193 switch (APFloatCmpResult) {
19194 case APFloat::cmpEqual:
19195 return CmpResult::Equal;
19196 case APFloat::cmpLessThan:
19197 return CmpResult::Less;
19198 case APFloat::cmpGreaterThan:
19199 return CmpResult::Greater;
19200 case APFloat::cmpUnordered:
19201 return CmpResult::Unordered;
19202 }
19203 llvm_unreachable("Unrecognised APFloat::cmpResult enum");
19204 };
19205 return Success(GetCmpRes(), E);
19206 }
19207
19208 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
19209 LValue LHSValue, RHSValue;
19210
19211 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
19212 if (!LHSOK && !Info.noteFailure())
19213 return false;
19214
19215 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19216 return false;
19217
19218 // Reject differing bases from the normal codepath; we special-case
19219 // comparisons to null.
19220 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
19221 // Bail out early if we're checking potential constant expression.
19222 // Otherwise, prefer to diagnose other issues.
19223 if (Info.checkingPotentialConstantExpression() &&
19224 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
19225 return false;
19226 auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) {
19227 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
19228 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
19229 Info.FFDiag(E, DiagId: DiagID)
19230 << (Reversed ? RHS : LHS) << (Reversed ? LHS : RHS);
19231 return false;
19232 };
19233 // Inequalities and subtractions between unrelated pointers have
19234 // unspecified or undefined behavior.
19235 if (!IsEquality)
19236 return DiagComparison(
19237 diag::note_constexpr_pointer_comparison_unspecified);
19238 // A constant address may compare equal to the address of a symbol.
19239 // The one exception is that address of an object cannot compare equal
19240 // to a null pointer constant.
19241 // TODO: Should we restrict this to actual null pointers, and exclude the
19242 // case of zero cast to pointer type?
19243 if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
19244 (!RHSValue.Base && !RHSValue.Offset.isZero()))
19245 return DiagComparison(diag::note_constexpr_pointer_constant_comparison,
19246 !RHSValue.Base);
19247 // C++2c [intro.object]/10:
19248 // Two objects [...] may have the same address if [...] they are both
19249 // potentially non-unique objects.
19250 // C++2c [intro.object]/9:
19251 // An object is potentially non-unique if it is a string literal object,
19252 // the backing array of an initializer list, or a subobject thereof.
19253 //
19254 // This makes the comparison result unspecified, so it's not a constant
19255 // expression.
19256 //
19257 // TODO: Do we need to handle the initializer list case here?
19258 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
19259 return DiagComparison(diag::note_constexpr_literal_comparison);
19260 if (IsOpaqueConstantCall(LVal: LHSValue) || IsOpaqueConstantCall(LVal: RHSValue))
19261 return DiagComparison(diag::note_constexpr_opaque_call_comparison,
19262 !IsOpaqueConstantCall(LVal: LHSValue));
19263 // We can't tell whether weak symbols will end up pointing to the same
19264 // object.
19265 if (IsWeakLValue(Value: LHSValue) || IsWeakLValue(Value: RHSValue))
19266 return DiagComparison(diag::note_constexpr_pointer_weak_comparison,
19267 !IsWeakLValue(Value: LHSValue));
19268 // We can't compare the address of the start of one object with the
19269 // past-the-end address of another object, per C++ DR1652.
19270 if (LHSValue.Base && LHSValue.Offset.isZero() &&
19271 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: RHSValue))
19272 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
19273 true);
19274 if (RHSValue.Base && RHSValue.Offset.isZero() &&
19275 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: LHSValue))
19276 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
19277 false);
19278 // We can't tell whether an object is at the same address as another
19279 // zero sized object.
19280 if ((RHSValue.Base && isZeroSized(Value: LHSValue)) ||
19281 (LHSValue.Base && isZeroSized(Value: RHSValue)))
19282 return DiagComparison(
19283 diag::note_constexpr_pointer_comparison_zero_sized);
19284 if (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)
19285 return DiagComparison(
19286 diag::note_constexpr_pointer_comparison_unspecified);
19287 // FIXME: Verify both variables are live.
19288 return Success(CmpResult::Unequal, E);
19289 }
19290
19291 const CharUnits &LHSOffset = LHSValue.getLValueOffset();
19292 const CharUnits &RHSOffset = RHSValue.getLValueOffset();
19293
19294 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
19295 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
19296
19297 // C++11 [expr.rel]p2:
19298 // - If two pointers point to non-static data members of the same object,
19299 // or to subobjects or array elements fo such members, recursively, the
19300 // pointer to the later declared member compares greater provided the
19301 // two members have the same access control and provided their class is
19302 // not a union.
19303 // [...]
19304 // - Otherwise pointer comparisons are unspecified.
19305 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {
19306 bool WasArrayIndex;
19307 unsigned Mismatch = FindDesignatorMismatch(
19308 ObjType: LHSValue.Base.isNull() ? QualType()
19309 : getType(B: LHSValue.Base).getNonReferenceType(),
19310 A: LHSDesignator, B: RHSDesignator, WasArrayIndex);
19311 // At the point where the designators diverge, the comparison has a
19312 // specified value if:
19313 // - we are comparing array indices
19314 // - we are comparing fields of a union, or fields with the same access
19315 // Otherwise, the result is unspecified and thus the comparison is not a
19316 // constant expression.
19317 if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
19318 Mismatch < RHSDesignator.Entries.size()) {
19319 const FieldDecl *LF = getAsField(E: LHSDesignator.Entries[Mismatch]);
19320 const FieldDecl *RF = getAsField(E: RHSDesignator.Entries[Mismatch]);
19321 if (!LF && !RF)
19322 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_classes);
19323 else if (!LF)
19324 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
19325 << getAsBaseClass(E: LHSDesignator.Entries[Mismatch])
19326 << RF->getParent() << RF;
19327 else if (!RF)
19328 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
19329 << getAsBaseClass(E: RHSDesignator.Entries[Mismatch])
19330 << LF->getParent() << LF;
19331 else if (!LF->getParent()->isUnion() &&
19332 LF->getAccess() != RF->getAccess())
19333 Info.CCEDiag(E,
19334 DiagId: diag::note_constexpr_pointer_comparison_differing_access)
19335 << LF << LF->getAccess() << RF << RF->getAccess()
19336 << LF->getParent();
19337 }
19338 }
19339
19340 // The comparison here must be unsigned, and performed with the same
19341 // width as the pointer.
19342 unsigned PtrSize = Info.Ctx.getTypeSize(T: LHSTy);
19343 uint64_t CompareLHS = LHSOffset.getQuantity();
19344 uint64_t CompareRHS = RHSOffset.getQuantity();
19345 assert(PtrSize <= 64 && "Unexpected pointer width");
19346 uint64_t Mask = ~0ULL >> (64 - PtrSize);
19347 CompareLHS &= Mask;
19348 CompareRHS &= Mask;
19349
19350 // If there is a base and this is a relational operator, we can only
19351 // compare pointers within the object in question; otherwise, the result
19352 // depends on where the object is located in memory.
19353 if (!LHSValue.Base.isNull() && IsRelational) {
19354 QualType BaseTy = getType(B: LHSValue.Base).getNonReferenceType();
19355 if (BaseTy->isIncompleteType())
19356 return Error(E);
19357 CharUnits Size = Info.Ctx.getTypeSizeInChars(T: BaseTy);
19358 uint64_t OffsetLimit = Size.getQuantity();
19359 if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)
19360 return Error(E);
19361 }
19362
19363 if (CompareLHS < CompareRHS)
19364 return Success(CmpResult::Less, E);
19365 if (CompareLHS > CompareRHS)
19366 return Success(CmpResult::Greater, E);
19367 return Success(CmpResult::Equal, E);
19368 }
19369
19370 if (LHSTy->isMemberPointerType()) {
19371 assert(IsEquality && "unexpected member pointer operation");
19372 assert(RHSTy->isMemberPointerType() && "invalid comparison");
19373
19374 MemberPtr LHSValue, RHSValue;
19375
19376 bool LHSOK = EvaluateMemberPointer(E: E->getLHS(), Result&: LHSValue, Info);
19377 if (!LHSOK && !Info.noteFailure())
19378 return false;
19379
19380 if (!EvaluateMemberPointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19381 return false;
19382
19383 // If either operand is a pointer to a weak function, the comparison is not
19384 // constant.
19385 if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) {
19386 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19387 << LHSValue.getDecl();
19388 return false;
19389 }
19390 if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) {
19391 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19392 << RHSValue.getDecl();
19393 return false;
19394 }
19395
19396 // C++11 [expr.eq]p2:
19397 // If both operands are null, they compare equal. Otherwise if only one is
19398 // null, they compare unequal.
19399 if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
19400 bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
19401 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19402 }
19403
19404 // Otherwise if either is a pointer to a virtual member function, the
19405 // result is unspecified.
19406 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: LHSValue.getDecl()))
19407 if (MD->isVirtual())
19408 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19409 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: RHSValue.getDecl()))
19410 if (MD->isVirtual())
19411 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19412
19413 // Otherwise they compare equal if and only if they would refer to the
19414 // same member of the same most derived object or the same subobject if
19415 // they were dereferenced with a hypothetical object of the associated
19416 // class type.
19417 bool Equal = LHSValue == RHSValue;
19418 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19419 }
19420
19421 if (LHSTy->isNullPtrType()) {
19422 assert(E->isComparisonOp() && "unexpected nullptr operation");
19423 assert(RHSTy->isNullPtrType() && "missing pointer conversion");
19424 // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
19425 // are compared, the result is true of the operator is <=, >= or ==, and
19426 // false otherwise.
19427 LValue Res;
19428 if (!EvaluatePointer(E: E->getLHS(), Result&: Res, Info) ||
19429 !EvaluatePointer(E: E->getRHS(), Result&: Res, Info))
19430 return false;
19431 return Success(CmpResult::Equal, E);
19432 }
19433
19434 return DoAfter();
19435}
19436
19437bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) {
19438 if (!CheckLiteralType(Info, E))
19439 return false;
19440
19441 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19442 ComparisonCategoryResult CCR;
19443 switch (CR) {
19444 case CmpResult::Unequal:
19445 llvm_unreachable("should never produce Unequal for three-way comparison");
19446 case CmpResult::Less:
19447 CCR = ComparisonCategoryResult::Less;
19448 break;
19449 case CmpResult::Equal:
19450 CCR = ComparisonCategoryResult::Equal;
19451 break;
19452 case CmpResult::Greater:
19453 CCR = ComparisonCategoryResult::Greater;
19454 break;
19455 case CmpResult::Unordered:
19456 CCR = ComparisonCategoryResult::Unordered;
19457 break;
19458 }
19459 // Evaluation succeeded. Lookup the information for the comparison category
19460 // type and fetch the VarDecl for the result.
19461 const ComparisonCategoryInfo &CmpInfo =
19462 Info.Ctx.CompCategories.getInfoForType(Ty: E->getType());
19463 const VarDecl *VD = CmpInfo.getValueInfo(ValueKind: CmpInfo.makeWeakResult(Res: CCR))->VD;
19464 // Check and evaluate the result as a constant expression.
19465 LValue LV;
19466 LV.set(B: VD);
19467 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
19468 return false;
19469 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
19470 Kind: ConstantExprKind::Normal);
19471 };
19472 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19473 return ExprEvaluatorBaseTy::VisitBinCmp(S: E);
19474 });
19475}
19476
19477bool RecordExprEvaluator::VisitCXXParenListInitExpr(
19478 const CXXParenListInitExpr *E) {
19479 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs());
19480}
19481
19482bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
19483 // We don't support assignment in C. C++ assignments don't get here because
19484 // assignment is an lvalue in C++.
19485 if (E->isAssignmentOp()) {
19486 Error(E);
19487 if (!Info.noteFailure())
19488 return false;
19489 }
19490
19491 if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E))
19492 return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E);
19493
19494 assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() ||
19495 !E->getRHS()->getType()->isIntegralOrEnumerationType()) &&
19496 "DataRecursiveIntBinOpEvaluator should have handled integral types");
19497
19498 if (E->isComparisonOp()) {
19499 // Evaluate builtin binary comparisons by evaluating them as three-way
19500 // comparisons and then translating the result.
19501 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19502 assert((CR != CmpResult::Unequal || E->isEqualityOp()) &&
19503 "should only produce Unequal for equality comparisons");
19504 bool IsEqual = CR == CmpResult::Equal,
19505 IsLess = CR == CmpResult::Less,
19506 IsGreater = CR == CmpResult::Greater;
19507 auto Op = E->getOpcode();
19508 switch (Op) {
19509 default:
19510 llvm_unreachable("unsupported binary operator");
19511 case BO_EQ:
19512 case BO_NE:
19513 return Success(Value: IsEqual == (Op == BO_EQ), E);
19514 case BO_LT:
19515 return Success(Value: IsLess, E);
19516 case BO_GT:
19517 return Success(Value: IsGreater, E);
19518 case BO_LE:
19519 return Success(Value: IsEqual || IsLess, E);
19520 case BO_GE:
19521 return Success(Value: IsEqual || IsGreater, E);
19522 }
19523 };
19524 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19525 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19526 });
19527 }
19528
19529 QualType LHSTy = E->getLHS()->getType();
19530 QualType RHSTy = E->getRHS()->getType();
19531
19532 if (LHSTy->isPointerType() && RHSTy->isPointerType() &&
19533 E->getOpcode() == BO_Sub) {
19534 LValue LHSValue, RHSValue;
19535
19536 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
19537 if (!LHSOK && !Info.noteFailure())
19538 return false;
19539
19540 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19541 return false;
19542
19543 // Reject differing bases from the normal codepath; we special-case
19544 // comparisons to null.
19545 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
19546 if (Info.checkingPotentialConstantExpression() &&
19547 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
19548 return false;
19549
19550 const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>();
19551 const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>();
19552
19553 auto DiagArith = [&](unsigned DiagID) {
19554 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
19555 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
19556 Info.FFDiag(E, DiagId: DiagID) << LHS << RHS;
19557 if (LHSExpr && LHSExpr == RHSExpr)
19558 Info.Note(Loc: LHSExpr->getExprLoc(),
19559 DiagId: diag::note_constexpr_repeated_literal_eval)
19560 << LHSExpr->getSourceRange();
19561 return false;
19562 };
19563
19564 if (!LHSExpr || !RHSExpr)
19565 return DiagArith(diag::note_constexpr_pointer_arith_unspecified);
19566
19567 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
19568 return DiagArith(diag::note_constexpr_literal_arith);
19569
19570 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
19571 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
19572 if (!LHSAddrExpr || !RHSAddrExpr)
19573 return Error(E);
19574 // Make sure both labels come from the same function.
19575 if (LHSAddrExpr->getLabel()->getDeclContext() !=
19576 RHSAddrExpr->getLabel()->getDeclContext())
19577 return Error(E);
19578 return Success(V: APValue(LHSAddrExpr, RHSAddrExpr), E);
19579 }
19580 const CharUnits &LHSOffset = LHSValue.getLValueOffset();
19581 const CharUnits &RHSOffset = RHSValue.getLValueOffset();
19582
19583 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
19584 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
19585
19586 // C++11 [expr.add]p6:
19587 // Unless both pointers point to elements of the same array object, or
19588 // one past the last element of the array object, the behavior is
19589 // undefined.
19590 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
19591 !AreElementsOfSameArray(ObjType: getType(B: LHSValue.Base), A: LHSDesignator,
19592 B: RHSDesignator))
19593 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_not_same_array);
19594
19595 QualType Type = E->getLHS()->getType();
19596 QualType ElementType = Type->castAs<PointerType>()->getPointeeType();
19597
19598 CharUnits ElementSize;
19599 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElementType, Size&: ElementSize))
19600 return false;
19601
19602 // As an extension, a type may have zero size (empty struct or union in
19603 // C, array of zero length). Pointer subtraction in such cases has
19604 // undefined behavior, so is not constant.
19605 if (ElementSize.isZero()) {
19606 Info.FFDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_zero_size)
19607 << ElementType;
19608 return false;
19609 }
19610
19611 // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
19612 // and produce incorrect results when it overflows. Such behavior
19613 // appears to be non-conforming, but is common, so perhaps we should
19614 // assume the standard intended for such cases to be undefined behavior
19615 // and check for them.
19616
19617 // Compute (LHSOffset - RHSOffset) / Size carefully, checking for
19618 // overflow in the final conversion to ptrdiff_t.
19619 APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
19620 APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
19621 APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true),
19622 false);
19623 APSInt TrueResult = (LHS - RHS) / ElemSize;
19624 APSInt Result = TrueResult.trunc(width: Info.Ctx.getIntWidth(T: E->getType()));
19625
19626 if (Result.extend(width: 65) != TrueResult &&
19627 !HandleOverflow(Info, E, SrcValue: TrueResult, DestType: E->getType()))
19628 return false;
19629 return Success(SI: Result, E);
19630 }
19631
19632 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19633}
19634
19635/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
19636/// a result as the expression's type.
19637bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
19638 const UnaryExprOrTypeTraitExpr *E) {
19639 switch(E->getKind()) {
19640 case UETT_PreferredAlignOf:
19641 case UETT_AlignOf: {
19642 if (E->isArgumentType())
19643 return Success(
19644 Size: GetAlignOfType(Ctx: Info.Ctx, T: E->getArgumentType(), ExprKind: E->getKind()), E);
19645 else
19646 return Success(
19647 Size: GetAlignOfExpr(Ctx: Info.Ctx, E: E->getArgumentExpr(), ExprKind: E->getKind()), E);
19648 }
19649
19650 case UETT_PtrAuthTypeDiscriminator: {
19651 if (E->getArgumentType()->isDependentType())
19652 return false;
19653 return Success(
19654 Value: Info.Ctx.getPointerAuthTypeDiscriminator(T: E->getArgumentType()), E);
19655 }
19656 case UETT_VecStep: {
19657 QualType Ty = E->getTypeOfArgument();
19658
19659 if (Ty->isVectorType()) {
19660 unsigned n = Ty->castAs<VectorType>()->getNumElements();
19661
19662 // The vec_step built-in functions that take a 3-component
19663 // vector return 4. (OpenCL 1.1 spec 6.11.12)
19664 if (n == 3)
19665 n = 4;
19666
19667 return Success(Value: n, E);
19668 } else
19669 return Success(Value: 1, E);
19670 }
19671
19672 case UETT_DataSizeOf:
19673 case UETT_SizeOf: {
19674 QualType SrcTy = E->getTypeOfArgument();
19675 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
19676 // the result is the size of the referenced type."
19677 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
19678 SrcTy = Ref->getPointeeType();
19679
19680 CharUnits Sizeof;
19681 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: SrcTy, Size&: Sizeof,
19682 SOT: E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf
19683 : SizeOfType::SizeOf)) {
19684 return false;
19685 }
19686 return Success(Size: Sizeof, E);
19687 }
19688 case UETT_OpenMPRequiredSimdAlign:
19689 assert(E->isArgumentType());
19690 return Success(
19691 Value: Info.Ctx.toCharUnitsFromBits(
19692 BitSize: Info.Ctx.getOpenMPDefaultSimdAlign(T: E->getArgumentType()))
19693 .getQuantity(),
19694 E);
19695 case UETT_VectorElements: {
19696 QualType Ty = E->getTypeOfArgument();
19697 // If the vector has a fixed size, we can determine the number of elements
19698 // at compile time.
19699 if (const auto *VT = Ty->getAs<VectorType>())
19700 return Success(Value: VT->getNumElements(), E);
19701
19702 assert(Ty->isSizelessVectorType());
19703 if (Info.InConstantContext)
19704 Info.CCEDiag(E, DiagId: diag::note_constexpr_non_const_vectorelements)
19705 << E->getSourceRange();
19706
19707 return false;
19708 }
19709 case UETT_CountOf: {
19710 QualType Ty = E->getTypeOfArgument();
19711 assert(Ty->isArrayType());
19712
19713 // We don't need to worry about array element qualifiers, so getting the
19714 // unsafe array type is fine.
19715 if (const auto *CAT =
19716 dyn_cast<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe())) {
19717 return Success(I: CAT->getSize(), E);
19718 }
19719
19720 assert(!Ty->isConstantSizeType());
19721
19722 // If it's a variable-length array type, we need to check whether it is a
19723 // multidimensional array. If so, we need to check the size expression of
19724 // the VLA to see if it's a constant size. If so, we can return that value.
19725 const auto *VAT = Info.Ctx.getAsVariableArrayType(T: Ty);
19726 assert(VAT);
19727 if (VAT->getElementType()->isArrayType()) {
19728 // Variable array size expression could be missing (e.g. int a[*][10]) In
19729 // that case, it can't be a constant expression.
19730 if (!VAT->getSizeExpr()) {
19731 Info.FFDiag(Loc: E->getBeginLoc());
19732 return false;
19733 }
19734
19735 std::optional<APSInt> Res =
19736 VAT->getSizeExpr()->getIntegerConstantExpr(Ctx: Info.Ctx);
19737 if (Res) {
19738 // The resulting value always has type size_t, so we need to make the
19739 // returned APInt have the correct sign and bit-width.
19740 APInt Val{
19741 static_cast<unsigned>(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType())),
19742 Res->getZExtValue()};
19743 return Success(I: Val, E);
19744 }
19745 }
19746
19747 // Definitely a variable-length type, which is not an ICE.
19748 // FIXME: Better diagnostic.
19749 Info.FFDiag(Loc: E->getBeginLoc());
19750 return false;
19751 }
19752 }
19753
19754 llvm_unreachable("unknown expr/type trait");
19755}
19756
19757bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
19758 Info.Ctx.recordOffsetOfEvaluation(E: OOE);
19759 CharUnits Result;
19760 unsigned n = OOE->getNumComponents();
19761 if (n == 0)
19762 return Error(E: OOE);
19763 QualType CurrentType = OOE->getTypeSourceInfo()->getType();
19764 for (unsigned i = 0; i != n; ++i) {
19765 OffsetOfNode ON = OOE->getComponent(Idx: i);
19766 switch (ON.getKind()) {
19767 case OffsetOfNode::Array: {
19768 const Expr *Idx = OOE->getIndexExpr(Idx: ON.getArrayExprIndex());
19769 APSInt IdxResult;
19770 if (!EvaluateInteger(E: Idx, Result&: IdxResult, Info))
19771 return false;
19772 const ArrayType *AT = Info.Ctx.getAsArrayType(T: CurrentType);
19773 if (!AT)
19774 return Error(E: OOE);
19775 CurrentType = AT->getElementType();
19776 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(T: CurrentType);
19777 // Reject negative indices, indices too large to fit in int64_t,
19778 // and overflow in the offset computation.
19779 if (IdxResult.isNegative() || IdxResult.getActiveBits() > 63)
19780 return Error(E: OOE);
19781 int64_t IdxVal = IdxResult.getExtValue();
19782 int64_t ElemSize = ElementSize.getQuantity();
19783 if (IdxVal != 0 &&
19784 ElemSize > std::numeric_limits<int64_t>::max() / IdxVal)
19785 return Error(E: OOE, D: diag::note_constexpr_offsetof_overflow);
19786 int64_t Offset = IdxVal * ElemSize;
19787 if (Result.getQuantity() > std::numeric_limits<int64_t>::max() - Offset)
19788 return Error(E: OOE, D: diag::note_constexpr_offsetof_overflow);
19789 Result += CharUnits::fromQuantity(Quantity: Offset);
19790 break;
19791 }
19792
19793 case OffsetOfNode::Field: {
19794 FieldDecl *MemberDecl = ON.getField();
19795 const auto *RD = CurrentType->getAsRecordDecl();
19796 if (!RD)
19797 return Error(E: OOE);
19798 if (RD->isInvalidDecl()) return false;
19799 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
19800 unsigned i = MemberDecl->getFieldIndex();
19801 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
19802 Result += Info.Ctx.toCharUnitsFromBits(BitSize: RL.getFieldOffset(FieldNo: i));
19803 CurrentType = MemberDecl->getType().getNonReferenceType();
19804 break;
19805 }
19806
19807 case OffsetOfNode::Identifier:
19808 llvm_unreachable("dependent __builtin_offsetof");
19809
19810 case OffsetOfNode::Base: {
19811 CXXBaseSpecifier *BaseSpec = ON.getBase();
19812 if (BaseSpec->isVirtual())
19813 return Error(E: OOE);
19814
19815 // Find the layout of the class whose base we are looking into.
19816 const auto *RD = CurrentType->getAsCXXRecordDecl();
19817 if (!RD)
19818 return Error(E: OOE);
19819 if (RD->isInvalidDecl()) return false;
19820 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
19821
19822 // Find the base class itself.
19823 CurrentType = BaseSpec->getType();
19824 const auto *BaseRD = CurrentType->getAsCXXRecordDecl();
19825 if (!BaseRD)
19826 return Error(E: OOE);
19827
19828 // Add the offset to the base.
19829 Result += RL.getBaseClassOffset(Base: BaseRD);
19830 break;
19831 }
19832 }
19833 }
19834 return Success(Size: Result, E: OOE);
19835}
19836
19837bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
19838 switch (E->getOpcode()) {
19839 default:
19840 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
19841 // See C99 6.6p3.
19842 return Error(E);
19843 case UO_Extension:
19844 // FIXME: Should extension allow i-c-e extension expressions in its scope?
19845 // If so, we could clear the diagnostic ID.
19846 return Visit(S: E->getSubExpr());
19847 case UO_Plus:
19848 // The result is just the value.
19849 return Visit(S: E->getSubExpr());
19850 case UO_Minus: {
19851 if (!Visit(S: E->getSubExpr()))
19852 return false;
19853 if (!Result.isInt()) return Error(E);
19854 const APSInt &Value = Result.getInt();
19855 if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() &&
19856 !E->getType().isWrapType()) {
19857 if (Info.checkingForUndefinedBehavior())
19858 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
19859 DiagID: diag::warn_integer_constant_overflow)
19860 << toString(I: Value, Radix: 10, Signed: Value.isSigned(), /*formatAsCLiteral=*/false,
19861 /*UpperCase=*/true, /*InsertSeparators=*/true)
19862 << E->getType() << E->getSourceRange();
19863
19864 if (!HandleOverflow(Info, E, SrcValue: -Value.extend(width: Value.getBitWidth() + 1),
19865 DestType: E->getType()))
19866 return false;
19867 }
19868 return Success(SI: -Value, E);
19869 }
19870 case UO_Not: {
19871 if (!Visit(S: E->getSubExpr()))
19872 return false;
19873 if (!Result.isInt()) return Error(E);
19874 return Success(SI: ~Result.getInt(), E);
19875 }
19876 case UO_LNot: {
19877 bool bres;
19878 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
19879 return false;
19880 return Success(Value: !bres, E);
19881 }
19882 }
19883}
19884
19885/// HandleCast - This is used to evaluate implicit or explicit casts where the
19886/// result type is integer.
19887bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
19888 const Expr *SubExpr = E->getSubExpr();
19889 QualType DestType = E->getType();
19890 QualType SrcType = SubExpr->getType();
19891
19892 switch (E->getCastKind()) {
19893 case CK_BaseToDerived:
19894 case CK_DerivedToBase:
19895 case CK_UncheckedDerivedToBase:
19896 case CK_Dynamic:
19897 case CK_ToUnion:
19898 case CK_ArrayToPointerDecay:
19899 case CK_FunctionToPointerDecay:
19900 case CK_NullToPointer:
19901 case CK_NullToMemberPointer:
19902 case CK_BaseToDerivedMemberPointer:
19903 case CK_DerivedToBaseMemberPointer:
19904 case CK_ReinterpretMemberPointer:
19905 case CK_ConstructorConversion:
19906 case CK_IntegralToPointer:
19907 case CK_ToVoid:
19908 case CK_VectorSplat:
19909 case CK_IntegralToFloating:
19910 case CK_FloatingCast:
19911 case CK_CPointerToObjCPointerCast:
19912 case CK_BlockPointerToObjCPointerCast:
19913 case CK_AnyPointerToBlockPointerCast:
19914 case CK_ObjCObjectLValueCast:
19915 case CK_FloatingRealToComplex:
19916 case CK_FloatingComplexToReal:
19917 case CK_FloatingComplexCast:
19918 case CK_FloatingComplexToIntegralComplex:
19919 case CK_IntegralRealToComplex:
19920 case CK_IntegralComplexCast:
19921 case CK_IntegralComplexToFloatingComplex:
19922 case CK_BuiltinFnToFnPtr:
19923 case CK_ZeroToOCLOpaqueType:
19924 case CK_NonAtomicToAtomic:
19925 case CK_AddressSpaceConversion:
19926 case CK_IntToOCLSampler:
19927 case CK_FloatingToFixedPoint:
19928 case CK_FixedPointToFloating:
19929 case CK_FixedPointCast:
19930 case CK_IntegralToFixedPoint:
19931 case CK_MatrixCast:
19932 case CK_HLSLAggregateSplatCast:
19933 llvm_unreachable("invalid cast kind for integral value");
19934
19935 case CK_BitCast:
19936 case CK_Dependent:
19937 case CK_LValueBitCast:
19938 case CK_ARCProduceObject:
19939 case CK_ARCConsumeObject:
19940 case CK_ARCReclaimReturnedObject:
19941 case CK_ARCExtendBlockObject:
19942 case CK_CopyAndAutoreleaseBlockObject:
19943 return Error(E);
19944
19945 case CK_UserDefinedConversion:
19946 case CK_LValueToRValue:
19947 case CK_AtomicToNonAtomic:
19948 case CK_NoOp:
19949 case CK_LValueToRValueBitCast:
19950 case CK_HLSLArrayRValue:
19951 return ExprEvaluatorBaseTy::VisitCastExpr(E);
19952
19953 case CK_MemberPointerToBoolean:
19954 case CK_PointerToBoolean:
19955 case CK_IntegralToBoolean:
19956 case CK_FloatingToBoolean:
19957 case CK_BooleanToSignedIntegral:
19958 case CK_FloatingComplexToBoolean:
19959 case CK_IntegralComplexToBoolean: {
19960 bool BoolResult;
19961 if (!EvaluateAsBooleanCondition(E: SubExpr, Result&: BoolResult, Info))
19962 return false;
19963 uint64_t IntResult = BoolResult;
19964 if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral)
19965 IntResult = (uint64_t)-1;
19966 return Success(Value: IntResult, E);
19967 }
19968
19969 case CK_FixedPointToIntegral: {
19970 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SrcType));
19971 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
19972 return false;
19973 bool Overflowed;
19974 llvm::APSInt Result = Src.convertToInt(
19975 DstWidth: Info.Ctx.getIntWidth(T: DestType),
19976 DstSign: DestType->isSignedIntegerOrEnumerationType(), Overflow: &Overflowed);
19977 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Result, DestType))
19978 return false;
19979 return Success(SI: Result, E);
19980 }
19981
19982 case CK_FixedPointToBoolean: {
19983 // Unsigned padding does not affect this.
19984 APValue Val;
19985 if (!Evaluate(Result&: Val, Info, E: SubExpr))
19986 return false;
19987 return Success(Value: Val.getFixedPoint().getBoolValue(), E);
19988 }
19989
19990 case CK_IntegralCast: {
19991 if (!Visit(S: SubExpr))
19992 return false;
19993
19994 if (!Result.isInt()) {
19995 // Allow casts of address-of-label differences if they are no-ops
19996 // or narrowing, if the result is at least 32 bits wide.
19997 // (The narrowing case isn't actually guaranteed to
19998 // be constant-evaluatable except in some narrow cases which are hard
19999 // to detect here. We let it through on the assumption the user knows
20000 // what they are doing.)
20001 if (Result.isAddrLabelDiff()) {
20002 unsigned DestBits = Info.Ctx.getTypeSize(T: DestType);
20003 return DestBits >= 32 && DestBits <= Info.Ctx.getTypeSize(T: SrcType);
20004 }
20005 // Only allow casts of lvalues if they are lossless.
20006 return Info.Ctx.getTypeSize(T: DestType) == Info.Ctx.getTypeSize(T: SrcType);
20007 }
20008
20009 if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) {
20010 const auto *ED = DestType->getAsEnumDecl();
20011 // Check that the value is within the range of the enumeration values.
20012 //
20013 // This corressponds to [expr.static.cast]p10 which says:
20014 // A value of integral or enumeration type can be explicitly converted
20015 // to a complete enumeration type ... If the enumeration type does not
20016 // have a fixed underlying type, the value is unchanged if the original
20017 // value is within the range of the enumeration values ([dcl.enum]), and
20018 // otherwise, the behavior is undefined.
20019 //
20020 // This was resolved as part of DR2338 which has CD5 status.
20021 if (!ED->isFixed()) {
20022 llvm::APInt Min;
20023 llvm::APInt Max;
20024
20025 ED->getValueRange(Max, Min);
20026 --Max;
20027
20028 if (ED->getNumNegativeBits() &&
20029 (Max.slt(RHS: Result.getInt().getSExtValue()) ||
20030 Min.sgt(RHS: Result.getInt().getSExtValue())))
20031 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
20032 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getSExtValue()
20033 << Max.getSExtValue() << ED;
20034 else if (!ED->getNumNegativeBits() &&
20035 Max.ult(RHS: Result.getInt().getZExtValue()))
20036 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
20037 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getZExtValue()
20038 << Max.getZExtValue() << ED;
20039 }
20040 }
20041
20042 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType,
20043 Value: Result.getInt()), E);
20044 }
20045
20046 case CK_PointerToIntegral: {
20047 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
20048 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
20049 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
20050
20051 LValue LV;
20052 if (!EvaluatePointer(E: SubExpr, Result&: LV, Info))
20053 return false;
20054
20055 if (LV.getLValueBase()) {
20056 // Only allow based lvalue casts if they are lossless.
20057 // FIXME: Allow a larger integer size than the pointer size, and allow
20058 // narrowing back down to pointer width in subsequent integral casts.
20059 // FIXME: Check integer type's active bits, not its type size.
20060 if (Info.Ctx.getTypeSize(T: DestType) != Info.Ctx.getTypeSize(T: SrcType))
20061 return Error(E);
20062
20063 LV.Designator.setInvalid();
20064 LV.moveInto(V&: Result);
20065 return true;
20066 }
20067
20068 APSInt AsInt;
20069 APValue V;
20070 LV.moveInto(V);
20071 if (!V.toIntegralConstant(Result&: AsInt, SrcTy: SrcType, Ctx: Info.Ctx))
20072 llvm_unreachable("Can't cast this!");
20073
20074 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType, Value: AsInt), E);
20075 }
20076
20077 case CK_IntegralComplexToReal: {
20078 ComplexValue C;
20079 if (!EvaluateComplex(E: SubExpr, Res&: C, Info))
20080 return false;
20081 return Success(SI: C.getComplexIntReal(), E);
20082 }
20083
20084 case CK_FloatingToIntegral: {
20085 APFloat F(0.0);
20086 if (!EvaluateFloat(E: SubExpr, Result&: F, Info))
20087 return false;
20088
20089 APSInt Value;
20090 if (!HandleFloatToIntCast(Info, E, SrcType, Value: F, DestType, Result&: Value))
20091 return false;
20092 return Success(SI: Value, E);
20093 }
20094 case CK_HLSLVectorTruncation: {
20095 APValue Val;
20096 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
20097 return Error(E);
20098 return Success(V: Val.getVectorElt(I: 0), E);
20099 }
20100 case CK_HLSLMatrixTruncation: {
20101 APValue Val;
20102 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
20103 return Error(E);
20104 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), E);
20105 }
20106 case CK_HLSLElementwiseCast: {
20107 SmallVector<APValue> SrcVals;
20108 SmallVector<QualType> SrcTypes;
20109
20110 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: DestType, SrcVals, SrcTypes))
20111 return false;
20112
20113 // cast our single element
20114 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
20115 APValue ResultVal;
20116 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: DestType, Original: SrcVals[0],
20117 Result&: ResultVal))
20118 return false;
20119 return Success(V: ResultVal, E);
20120 }
20121 }
20122
20123 llvm_unreachable("unknown cast resulting in integral value");
20124}
20125
20126bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
20127 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20128 ComplexValue LV;
20129 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
20130 return false;
20131 if (!LV.isComplexInt())
20132 return Error(E);
20133 return Success(SI: LV.getComplexIntReal(), E);
20134 }
20135
20136 return Visit(S: E->getSubExpr());
20137}
20138
20139bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
20140 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
20141 ComplexValue LV;
20142 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
20143 return false;
20144 if (!LV.isComplexInt())
20145 return Error(E);
20146 return Success(SI: LV.getComplexIntImag(), E);
20147 }
20148
20149 VisitIgnoredValue(E: E->getSubExpr());
20150 return Success(Value: 0, E);
20151}
20152
20153bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
20154 return Success(Value: E->getPackLength(), E);
20155}
20156
20157bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
20158 return Success(Value: E->getValue(), E);
20159}
20160
20161bool IntExprEvaluator::VisitConceptSpecializationExpr(
20162 const ConceptSpecializationExpr *E) {
20163 return Success(Value: E->isSatisfied(), E);
20164}
20165
20166bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) {
20167 return Success(Value: E->isSatisfied(), E);
20168}
20169
20170bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20171 switch (E->getOpcode()) {
20172 default:
20173 // Invalid unary operators
20174 return Error(E);
20175 case UO_Plus:
20176 // The result is just the value.
20177 return Visit(S: E->getSubExpr());
20178 case UO_Minus: {
20179 if (!Visit(S: E->getSubExpr())) return false;
20180 if (!Result.isFixedPoint())
20181 return Error(E);
20182 bool Overflowed;
20183 APFixedPoint Negated = Result.getFixedPoint().negate(Overflow: &Overflowed);
20184 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Negated, DestType: E->getType()))
20185 return false;
20186 return Success(V: Negated, E);
20187 }
20188 case UO_LNot: {
20189 bool bres;
20190 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
20191 return false;
20192 return Success(Value: !bres, E);
20193 }
20194 }
20195}
20196
20197bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) {
20198 const Expr *SubExpr = E->getSubExpr();
20199 QualType DestType = E->getType();
20200 assert(DestType->isFixedPointType() &&
20201 "Expected destination type to be a fixed point type");
20202 auto DestFXSema = Info.Ctx.getFixedPointSemantics(Ty: DestType);
20203
20204 switch (E->getCastKind()) {
20205 case CK_FixedPointCast: {
20206 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
20207 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
20208 return false;
20209 bool Overflowed;
20210 APFixedPoint Result = Src.convert(DstSema: DestFXSema, Overflow: &Overflowed);
20211 if (Overflowed) {
20212 if (Info.checkingForUndefinedBehavior())
20213 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20214 DiagID: diag::warn_fixedpoint_constant_overflow)
20215 << Result.toString() << E->getType();
20216 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20217 return false;
20218 }
20219 return Success(V: Result, E);
20220 }
20221 case CK_IntegralToFixedPoint: {
20222 APSInt Src;
20223 if (!EvaluateInteger(E: SubExpr, Result&: Src, Info))
20224 return false;
20225
20226 bool Overflowed;
20227 APFixedPoint IntResult = APFixedPoint::getFromIntValue(
20228 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
20229
20230 if (Overflowed) {
20231 if (Info.checkingForUndefinedBehavior())
20232 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20233 DiagID: diag::warn_fixedpoint_constant_overflow)
20234 << IntResult.toString() << E->getType();
20235 if (!HandleOverflow(Info, E, SrcValue: IntResult, DestType: E->getType()))
20236 return false;
20237 }
20238
20239 return Success(V: IntResult, E);
20240 }
20241 case CK_FloatingToFixedPoint: {
20242 APFloat Src(0.0);
20243 if (!EvaluateFloat(E: SubExpr, Result&: Src, Info))
20244 return false;
20245
20246 bool Overflowed;
20247 APFixedPoint Result = APFixedPoint::getFromFloatValue(
20248 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
20249
20250 if (Overflowed) {
20251 if (Info.checkingForUndefinedBehavior())
20252 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20253 DiagID: diag::warn_fixedpoint_constant_overflow)
20254 << Result.toString() << E->getType();
20255 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20256 return false;
20257 }
20258
20259 return Success(V: Result, E);
20260 }
20261 case CK_NoOp:
20262 case CK_LValueToRValue:
20263 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20264 default:
20265 return Error(E);
20266 }
20267}
20268
20269bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20270 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20271 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20272
20273 const Expr *LHS = E->getLHS();
20274 const Expr *RHS = E->getRHS();
20275 FixedPointSemantics ResultFXSema =
20276 Info.Ctx.getFixedPointSemantics(Ty: E->getType());
20277
20278 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHS->getType()));
20279 if (!EvaluateFixedPointOrInteger(E: LHS, Result&: LHSFX, Info))
20280 return false;
20281 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHS->getType()));
20282 if (!EvaluateFixedPointOrInteger(E: RHS, Result&: RHSFX, Info))
20283 return false;
20284
20285 bool OpOverflow = false, ConversionOverflow = false;
20286 APFixedPoint Result(LHSFX.getSemantics());
20287 switch (E->getOpcode()) {
20288 case BO_Add: {
20289 Result = LHSFX.add(Other: RHSFX, Overflow: &OpOverflow)
20290 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20291 break;
20292 }
20293 case BO_Sub: {
20294 Result = LHSFX.sub(Other: RHSFX, Overflow: &OpOverflow)
20295 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20296 break;
20297 }
20298 case BO_Mul: {
20299 Result = LHSFX.mul(Other: RHSFX, Overflow: &OpOverflow)
20300 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20301 break;
20302 }
20303 case BO_Div: {
20304 if (RHSFX.getValue() == 0) {
20305 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero);
20306 return false;
20307 }
20308 Result = LHSFX.div(Other: RHSFX, Overflow: &OpOverflow)
20309 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20310 break;
20311 }
20312 case BO_Shl:
20313 case BO_Shr: {
20314 FixedPointSemantics LHSSema = LHSFX.getSemantics();
20315 llvm::APSInt RHSVal = RHSFX.getValue();
20316
20317 unsigned ShiftBW =
20318 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding();
20319 unsigned Amt = RHSVal.getLimitedValue(Limit: ShiftBW - 1);
20320 // Embedded-C 4.1.6.2.2:
20321 // The right operand must be nonnegative and less than the total number
20322 // of (nonpadding) bits of the fixed-point operand ...
20323 if (RHSVal.isNegative())
20324 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHSVal;
20325 else if (Amt != RHSVal)
20326 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
20327 << RHSVal << E->getType() << ShiftBW;
20328
20329 if (E->getOpcode() == BO_Shl)
20330 Result = LHSFX.shl(Amt, Overflow: &OpOverflow);
20331 else
20332 Result = LHSFX.shr(Amt, Overflow: &OpOverflow);
20333 break;
20334 }
20335 default:
20336 return false;
20337 }
20338 if (OpOverflow || ConversionOverflow) {
20339 if (Info.checkingForUndefinedBehavior())
20340 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20341 DiagID: diag::warn_fixedpoint_constant_overflow)
20342 << Result.toString() << E->getType();
20343 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20344 return false;
20345 }
20346 return Success(V: Result, E);
20347}
20348
20349//===----------------------------------------------------------------------===//
20350// Float Evaluation
20351//===----------------------------------------------------------------------===//
20352
20353namespace {
20354class FloatExprEvaluator
20355 : public ExprEvaluatorBase<FloatExprEvaluator> {
20356 APFloat &Result;
20357public:
20358 FloatExprEvaluator(EvalInfo &info, APFloat &result)
20359 : ExprEvaluatorBaseTy(info), Result(result) {}
20360
20361 bool Success(const APValue &V, const Expr *e) {
20362 Result = V.getFloat();
20363 return true;
20364 }
20365
20366 bool ZeroInitialization(const Expr *E) {
20367 Result = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
20368 return true;
20369 }
20370
20371 bool VisitCallExpr(const CallExpr *E);
20372
20373 bool VisitUnaryOperator(const UnaryOperator *E);
20374 bool VisitBinaryOperator(const BinaryOperator *E);
20375 bool VisitFloatingLiteral(const FloatingLiteral *E);
20376 bool VisitCastExpr(const CastExpr *E);
20377
20378 bool VisitUnaryReal(const UnaryOperator *E);
20379 bool VisitUnaryImag(const UnaryOperator *E);
20380
20381 // FIXME: Missing: array subscript of vector, member of vector
20382};
20383} // end anonymous namespace
20384
20385static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
20386 assert(!E->isValueDependent());
20387 assert(E->isPRValue() && E->getType()->isRealFloatingType());
20388 return FloatExprEvaluator(Info, Result).Visit(S: E);
20389}
20390
20391static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
20392 QualType ResultTy,
20393 const Expr *Arg,
20394 bool SNaN,
20395 llvm::APFloat &Result) {
20396 const StringLiteral *S = dyn_cast<StringLiteral>(Val: Arg->IgnoreParenCasts());
20397 if (!S) return false;
20398
20399 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(T: ResultTy);
20400
20401 llvm::APInt fill;
20402
20403 // Treat empty strings as if they were zero.
20404 if (S->getString().empty())
20405 fill = llvm::APInt(32, 0);
20406 else if (S->getString().getAsInteger(Radix: 0, Result&: fill))
20407 return false;
20408
20409 if (Context.getTargetInfo().isNan2008()) {
20410 if (SNaN)
20411 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20412 else
20413 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20414 } else {
20415 // Prior to IEEE 754-2008, architectures were allowed to choose whether
20416 // the first bit of their significand was set for qNaN or sNaN. MIPS chose
20417 // a different encoding to what became a standard in 2008, and for pre-
20418 // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as
20419 // sNaN. This is now known as "legacy NaN" encoding.
20420 if (SNaN)
20421 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20422 else
20423 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20424 }
20425
20426 return true;
20427}
20428
20429bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
20430 if (!IsConstantEvaluatedBuiltinCall(E))
20431 return ExprEvaluatorBaseTy::VisitCallExpr(E);
20432
20433 unsigned BuiltinOp = ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E);
20434
20435 switch (BuiltinOp) {
20436 default:
20437 return false;
20438
20439 case Builtin::BI__builtin_huge_val:
20440 case Builtin::BI__builtin_huge_valf:
20441 case Builtin::BI__builtin_huge_vall:
20442 case Builtin::BI__builtin_huge_valf16:
20443 case Builtin::BI__builtin_huge_valf128:
20444 case Builtin::BI__builtin_inf:
20445 case Builtin::BI__builtin_inff:
20446 case Builtin::BI__builtin_infl:
20447 case Builtin::BI__builtin_inff16:
20448 case Builtin::BI__builtin_inff128: {
20449 const llvm::fltSemantics &Sem =
20450 Info.Ctx.getFloatTypeSemantics(T: E->getType());
20451 Result = llvm::APFloat::getInf(Sem);
20452 return true;
20453 }
20454
20455 case Builtin::BI__builtin_nans:
20456 case Builtin::BI__builtin_nansf:
20457 case Builtin::BI__builtin_nansl:
20458 case Builtin::BI__builtin_nansf16:
20459 case Builtin::BI__builtin_nansf128:
20460 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20461 SNaN: true, Result))
20462 return Error(E);
20463 return true;
20464
20465 case Builtin::BI__builtin_nan:
20466 case Builtin::BI__builtin_nanf:
20467 case Builtin::BI__builtin_nanl:
20468 case Builtin::BI__builtin_nanf16:
20469 case Builtin::BI__builtin_nanf128:
20470 // If this is __builtin_nan() turn this into a nan, otherwise we
20471 // can't constant fold it.
20472 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20473 SNaN: false, Result))
20474 return Error(E);
20475 return true;
20476
20477 case Builtin::BI__builtin_elementwise_abs:
20478 case Builtin::BI__builtin_fabs:
20479 case Builtin::BI__builtin_fabsf:
20480 case Builtin::BI__builtin_fabsl:
20481 case Builtin::BI__builtin_fabsf128:
20482 // The C standard says "fabs raises no floating-point exceptions,
20483 // even if x is a signaling NaN. The returned value is independent of
20484 // the current rounding direction mode." Therefore constant folding can
20485 // proceed without regard to the floating point settings.
20486 // Reference, WG14 N2478 F.10.4.3
20487 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info))
20488 return false;
20489
20490 if (Result.isNegative())
20491 Result.changeSign();
20492 return true;
20493
20494 case Builtin::BI__arithmetic_fence:
20495 return EvaluateFloat(E: E->getArg(Arg: 0), Result, Info);
20496
20497 // FIXME: Builtin::BI__builtin_powi
20498 // FIXME: Builtin::BI__builtin_powif
20499 // FIXME: Builtin::BI__builtin_powil
20500
20501 case Builtin::BI__builtin_copysign:
20502 case Builtin::BI__builtin_copysignf:
20503 case Builtin::BI__builtin_copysignl:
20504 case Builtin::BI__builtin_copysignf128: {
20505 APFloat RHS(0.);
20506 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20507 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20508 return false;
20509 Result.copySign(RHS);
20510 return true;
20511 }
20512
20513 case Builtin::BI__builtin_fmax:
20514 case Builtin::BI__builtin_fmaxf:
20515 case Builtin::BI__builtin_fmaxl:
20516 case Builtin::BI__builtin_fmaxf16:
20517 case Builtin::BI__builtin_fmaxf128: {
20518 APFloat RHS(0.);
20519 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20520 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20521 return false;
20522 Result = maxnum(A: Result, B: RHS);
20523 return true;
20524 }
20525
20526 case Builtin::BI__builtin_fmin:
20527 case Builtin::BI__builtin_fminf:
20528 case Builtin::BI__builtin_fminl:
20529 case Builtin::BI__builtin_fminf16:
20530 case Builtin::BI__builtin_fminf128: {
20531 APFloat RHS(0.);
20532 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20533 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20534 return false;
20535 Result = minnum(A: Result, B: RHS);
20536 return true;
20537 }
20538
20539 case Builtin::BI__builtin_fmaximum_num:
20540 case Builtin::BI__builtin_fmaximum_numf:
20541 case Builtin::BI__builtin_fmaximum_numl:
20542 case Builtin::BI__builtin_fmaximum_numf16:
20543 case Builtin::BI__builtin_fmaximum_numf128: {
20544 APFloat RHS(0.);
20545 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20546 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20547 return false;
20548 Result = maximumnum(A: Result, B: RHS);
20549 return true;
20550 }
20551
20552 case Builtin::BI__builtin_fminimum_num:
20553 case Builtin::BI__builtin_fminimum_numf:
20554 case Builtin::BI__builtin_fminimum_numl:
20555 case Builtin::BI__builtin_fminimum_numf16:
20556 case Builtin::BI__builtin_fminimum_numf128: {
20557 APFloat RHS(0.);
20558 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20559 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20560 return false;
20561 Result = minimumnum(A: Result, B: RHS);
20562 return true;
20563 }
20564
20565 case Builtin::BI__builtin_elementwise_fma: {
20566 if (!E->getArg(Arg: 0)->isPRValue() || !E->getArg(Arg: 1)->isPRValue() ||
20567 !E->getArg(Arg: 2)->isPRValue()) {
20568 return false;
20569 }
20570 APFloat SourceY(0.), SourceZ(0.);
20571 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20572 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: SourceY, Info) ||
20573 !EvaluateFloat(E: E->getArg(Arg: 2), Result&: SourceZ, Info))
20574 return false;
20575 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
20576 (void)Result.fusedMultiplyAdd(Multiplicand: SourceY, Addend: SourceZ, RM);
20577 return true;
20578 }
20579
20580 case clang::X86::BI__builtin_ia32_vec_ext_v4sf: {
20581 APValue Vec;
20582 APSInt IdxAPS;
20583 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
20584 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
20585 return false;
20586 unsigned N = Vec.getVectorLength();
20587 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
20588 return Success(V: Vec.getVectorElt(I: Idx), e: E);
20589 }
20590 }
20591}
20592
20593bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
20594 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20595 ComplexValue CV;
20596 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
20597 return false;
20598 Result = CV.FloatReal;
20599 return true;
20600 }
20601
20602 return Visit(S: E->getSubExpr());
20603}
20604
20605bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
20606 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20607 ComplexValue CV;
20608 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
20609 return false;
20610 Result = CV.FloatImag;
20611 return true;
20612 }
20613
20614 VisitIgnoredValue(E: E->getSubExpr());
20615 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(T: E->getType());
20616 Result = llvm::APFloat::getZero(Sem);
20617 return true;
20618}
20619
20620bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20621 switch (E->getOpcode()) {
20622 default: return Error(E);
20623 case UO_Plus:
20624 return EvaluateFloat(E: E->getSubExpr(), Result, Info);
20625 case UO_Minus:
20626 // In C standard, WG14 N2478 F.3 p4
20627 // "the unary - raises no floating point exceptions,
20628 // even if the operand is signalling."
20629 if (!EvaluateFloat(E: E->getSubExpr(), Result, Info))
20630 return false;
20631 Result.changeSign();
20632 return true;
20633 }
20634}
20635
20636bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20637 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20638 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20639
20640 APFloat RHS(0.0);
20641 bool LHSOK = EvaluateFloat(E: E->getLHS(), Result, Info);
20642 if (!LHSOK && !Info.noteFailure())
20643 return false;
20644 return EvaluateFloat(E: E->getRHS(), Result&: RHS, Info) && LHSOK &&
20645 handleFloatFloatBinOp(Info, E, LHS&: Result, Opcode: E->getOpcode(), RHS);
20646}
20647
20648bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
20649 Result = E->getValue();
20650 return true;
20651}
20652
20653bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
20654 const Expr* SubExpr = E->getSubExpr();
20655
20656 switch (E->getCastKind()) {
20657 default:
20658 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20659
20660 case CK_HLSLAggregateSplatCast:
20661 llvm_unreachable("invalid cast kind for floating value");
20662
20663 case CK_IntegralToFloating: {
20664 APSInt IntResult;
20665 const FPOptions FPO = E->getFPFeaturesInEffect(
20666 LO: Info.Ctx.getLangOpts());
20667 return EvaluateInteger(E: SubExpr, Result&: IntResult, Info) &&
20668 HandleIntToFloatCast(Info, E, FPO, SrcType: SubExpr->getType(),
20669 Value: IntResult, DestType: E->getType(), Result);
20670 }
20671
20672 case CK_FixedPointToFloating: {
20673 APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
20674 if (!EvaluateFixedPoint(E: SubExpr, Result&: FixResult, Info))
20675 return false;
20676 Result =
20677 FixResult.convertToFloat(FloatSema: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
20678 return true;
20679 }
20680
20681 case CK_FloatingCast: {
20682 if (!Visit(S: SubExpr))
20683 return false;
20684 return HandleFloatToFloatCast(Info, E, SrcType: SubExpr->getType(), DestType: E->getType(),
20685 Result);
20686 }
20687
20688 case CK_FloatingComplexToReal: {
20689 ComplexValue V;
20690 if (!EvaluateComplex(E: SubExpr, Res&: V, Info))
20691 return false;
20692 Result = V.getComplexFloatReal();
20693 return true;
20694 }
20695 case CK_HLSLVectorTruncation: {
20696 APValue Val;
20697 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
20698 return Error(E);
20699 return Success(V: Val.getVectorElt(I: 0), e: E);
20700 }
20701 case CK_HLSLMatrixTruncation: {
20702 APValue Val;
20703 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
20704 return Error(E);
20705 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), e: E);
20706 }
20707 case CK_HLSLElementwiseCast: {
20708 SmallVector<APValue> SrcVals;
20709 SmallVector<QualType> SrcTypes;
20710
20711 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: E->getType(), SrcVals,
20712 SrcTypes))
20713 return false;
20714 APValue Val;
20715
20716 // cast our single element
20717 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
20718 APValue ResultVal;
20719 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: E->getType(), Original: SrcVals[0],
20720 Result&: ResultVal))
20721 return false;
20722 return Success(V: ResultVal, e: E);
20723 }
20724 }
20725}
20726
20727//===----------------------------------------------------------------------===//
20728// Complex Evaluation (for float and integer)
20729//===----------------------------------------------------------------------===//
20730
20731namespace {
20732class ComplexExprEvaluator
20733 : public ExprEvaluatorBase<ComplexExprEvaluator> {
20734 ComplexValue &Result;
20735
20736public:
20737 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
20738 : ExprEvaluatorBaseTy(info), Result(Result) {}
20739
20740 bool Success(const APValue &V, const Expr *e) {
20741 Result.setFrom(V);
20742 return true;
20743 }
20744
20745 bool ZeroInitialization(const Expr *E);
20746
20747 //===--------------------------------------------------------------------===//
20748 // Visitor Methods
20749 //===--------------------------------------------------------------------===//
20750
20751 bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
20752 bool VisitCastExpr(const CastExpr *E);
20753 bool VisitBinaryOperator(const BinaryOperator *E);
20754 bool VisitUnaryOperator(const UnaryOperator *E);
20755 bool VisitInitListExpr(const InitListExpr *E);
20756 bool VisitCallExpr(const CallExpr *E);
20757};
20758} // end anonymous namespace
20759
20760static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
20761 EvalInfo &Info) {
20762 assert(!E->isValueDependent());
20763 assert(E->isPRValue() && E->getType()->isAnyComplexType());
20764 return ComplexExprEvaluator(Info, Result).Visit(S: E);
20765}
20766
20767bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
20768 QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();
20769 if (ElemTy->isRealFloatingType()) {
20770 Result.makeComplexFloat();
20771 APFloat Zero = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy));
20772 Result.FloatReal = Zero;
20773 Result.FloatImag = Zero;
20774 } else {
20775 Result.makeComplexInt();
20776 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy);
20777 Result.IntReal = Zero;
20778 Result.IntImag = Zero;
20779 }
20780 return true;
20781}
20782
20783bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
20784 const Expr* SubExpr = E->getSubExpr();
20785
20786 if (SubExpr->getType()->isRealFloatingType()) {
20787 Result.makeComplexFloat();
20788 APFloat &Imag = Result.FloatImag;
20789 if (!EvaluateFloat(E: SubExpr, Result&: Imag, Info))
20790 return false;
20791
20792 Result.FloatReal = APFloat(Imag.getSemantics());
20793 return true;
20794 } else {
20795 assert(SubExpr->getType()->isIntegerType() &&
20796 "Unexpected imaginary literal.");
20797
20798 Result.makeComplexInt();
20799 APSInt &Imag = Result.IntImag;
20800 if (!EvaluateInteger(E: SubExpr, Result&: Imag, Info))
20801 return false;
20802
20803 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
20804 return true;
20805 }
20806}
20807
20808bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
20809
20810 switch (E->getCastKind()) {
20811 case CK_BitCast:
20812 case CK_BaseToDerived:
20813 case CK_DerivedToBase:
20814 case CK_UncheckedDerivedToBase:
20815 case CK_Dynamic:
20816 case CK_ToUnion:
20817 case CK_ArrayToPointerDecay:
20818 case CK_FunctionToPointerDecay:
20819 case CK_NullToPointer:
20820 case CK_NullToMemberPointer:
20821 case CK_BaseToDerivedMemberPointer:
20822 case CK_DerivedToBaseMemberPointer:
20823 case CK_MemberPointerToBoolean:
20824 case CK_ReinterpretMemberPointer:
20825 case CK_ConstructorConversion:
20826 case CK_IntegralToPointer:
20827 case CK_PointerToIntegral:
20828 case CK_PointerToBoolean:
20829 case CK_ToVoid:
20830 case CK_VectorSplat:
20831 case CK_IntegralCast:
20832 case CK_BooleanToSignedIntegral:
20833 case CK_IntegralToBoolean:
20834 case CK_IntegralToFloating:
20835 case CK_FloatingToIntegral:
20836 case CK_FloatingToBoolean:
20837 case CK_FloatingCast:
20838 case CK_CPointerToObjCPointerCast:
20839 case CK_BlockPointerToObjCPointerCast:
20840 case CK_AnyPointerToBlockPointerCast:
20841 case CK_ObjCObjectLValueCast:
20842 case CK_FloatingComplexToReal:
20843 case CK_FloatingComplexToBoolean:
20844 case CK_IntegralComplexToReal:
20845 case CK_IntegralComplexToBoolean:
20846 case CK_ARCProduceObject:
20847 case CK_ARCConsumeObject:
20848 case CK_ARCReclaimReturnedObject:
20849 case CK_ARCExtendBlockObject:
20850 case CK_CopyAndAutoreleaseBlockObject:
20851 case CK_BuiltinFnToFnPtr:
20852 case CK_ZeroToOCLOpaqueType:
20853 case CK_NonAtomicToAtomic:
20854 case CK_AddressSpaceConversion:
20855 case CK_IntToOCLSampler:
20856 case CK_FloatingToFixedPoint:
20857 case CK_FixedPointToFloating:
20858 case CK_FixedPointCast:
20859 case CK_FixedPointToBoolean:
20860 case CK_FixedPointToIntegral:
20861 case CK_IntegralToFixedPoint:
20862 case CK_MatrixCast:
20863 case CK_HLSLVectorTruncation:
20864 case CK_HLSLMatrixTruncation:
20865 case CK_HLSLElementwiseCast:
20866 case CK_HLSLAggregateSplatCast:
20867 llvm_unreachable("invalid cast kind for complex value");
20868
20869 case CK_LValueToRValue:
20870 case CK_AtomicToNonAtomic:
20871 case CK_NoOp:
20872 case CK_LValueToRValueBitCast:
20873 case CK_HLSLArrayRValue:
20874 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20875
20876 case CK_Dependent:
20877 case CK_LValueBitCast:
20878 case CK_UserDefinedConversion:
20879 return Error(E);
20880
20881 case CK_FloatingRealToComplex: {
20882 APFloat &Real = Result.FloatReal;
20883 if (!EvaluateFloat(E: E->getSubExpr(), Result&: Real, Info))
20884 return false;
20885
20886 Result.makeComplexFloat();
20887 Result.FloatImag = APFloat(Real.getSemantics());
20888 return true;
20889 }
20890
20891 case CK_FloatingComplexCast: {
20892 if (!Visit(S: E->getSubExpr()))
20893 return false;
20894
20895 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
20896 QualType From
20897 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
20898
20899 return HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatReal) &&
20900 HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatImag);
20901 }
20902
20903 case CK_FloatingComplexToIntegralComplex: {
20904 if (!Visit(S: E->getSubExpr()))
20905 return false;
20906
20907 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
20908 QualType From
20909 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
20910 Result.makeComplexInt();
20911 return HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatReal,
20912 DestType: To, Result&: Result.IntReal) &&
20913 HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatImag,
20914 DestType: To, Result&: Result.IntImag);
20915 }
20916
20917 case CK_IntegralRealToComplex: {
20918 APSInt &Real = Result.IntReal;
20919 if (!EvaluateInteger(E: E->getSubExpr(), Result&: Real, Info))
20920 return false;
20921
20922 Result.makeComplexInt();
20923 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
20924 return true;
20925 }
20926
20927 case CK_IntegralComplexCast: {
20928 if (!Visit(S: E->getSubExpr()))
20929 return false;
20930
20931 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
20932 QualType From
20933 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
20934
20935 Result.IntReal = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntReal);
20936 Result.IntImag = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntImag);
20937 return true;
20938 }
20939
20940 case CK_IntegralComplexToFloatingComplex: {
20941 if (!Visit(S: E->getSubExpr()))
20942 return false;
20943
20944 const FPOptions FPO = E->getFPFeaturesInEffect(
20945 LO: Info.Ctx.getLangOpts());
20946 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
20947 QualType From
20948 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
20949 Result.makeComplexFloat();
20950 return HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntReal,
20951 DestType: To, Result&: Result.FloatReal) &&
20952 HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntImag,
20953 DestType: To, Result&: Result.FloatImag);
20954 }
20955 }
20956
20957 llvm_unreachable("unknown cast resulting in complex value");
20958}
20959
20960uint8_t GFNIMultiplicativeInverse(uint8_t Byte) {
20961 // Lookup Table for Multiplicative Inverse in GF(2^8)
20962 const uint8_t GFInv[256] = {
20963 0x00, 0x01, 0x8d, 0xf6, 0xcb, 0x52, 0x7b, 0xd1, 0xe8, 0x4f, 0x29, 0xc0,
20964 0xb0, 0xe1, 0xe5, 0xc7, 0x74, 0xb4, 0xaa, 0x4b, 0x99, 0x2b, 0x60, 0x5f,
20965 0x58, 0x3f, 0xfd, 0xcc, 0xff, 0x40, 0xee, 0xb2, 0x3a, 0x6e, 0x5a, 0xf1,
20966 0x55, 0x4d, 0xa8, 0xc9, 0xc1, 0x0a, 0x98, 0x15, 0x30, 0x44, 0xa2, 0xc2,
20967 0x2c, 0x45, 0x92, 0x6c, 0xf3, 0x39, 0x66, 0x42, 0xf2, 0x35, 0x20, 0x6f,
20968 0x77, 0xbb, 0x59, 0x19, 0x1d, 0xfe, 0x37, 0x67, 0x2d, 0x31, 0xf5, 0x69,
20969 0xa7, 0x64, 0xab, 0x13, 0x54, 0x25, 0xe9, 0x09, 0xed, 0x5c, 0x05, 0xca,
20970 0x4c, 0x24, 0x87, 0xbf, 0x18, 0x3e, 0x22, 0xf0, 0x51, 0xec, 0x61, 0x17,
20971 0x16, 0x5e, 0xaf, 0xd3, 0x49, 0xa6, 0x36, 0x43, 0xf4, 0x47, 0x91, 0xdf,
20972 0x33, 0x93, 0x21, 0x3b, 0x79, 0xb7, 0x97, 0x85, 0x10, 0xb5, 0xba, 0x3c,
20973 0xb6, 0x70, 0xd0, 0x06, 0xa1, 0xfa, 0x81, 0x82, 0x83, 0x7e, 0x7f, 0x80,
20974 0x96, 0x73, 0xbe, 0x56, 0x9b, 0x9e, 0x95, 0xd9, 0xf7, 0x02, 0xb9, 0xa4,
20975 0xde, 0x6a, 0x32, 0x6d, 0xd8, 0x8a, 0x84, 0x72, 0x2a, 0x14, 0x9f, 0x88,
20976 0xf9, 0xdc, 0x89, 0x9a, 0xfb, 0x7c, 0x2e, 0xc3, 0x8f, 0xb8, 0x65, 0x48,
20977 0x26, 0xc8, 0x12, 0x4a, 0xce, 0xe7, 0xd2, 0x62, 0x0c, 0xe0, 0x1f, 0xef,
20978 0x11, 0x75, 0x78, 0x71, 0xa5, 0x8e, 0x76, 0x3d, 0xbd, 0xbc, 0x86, 0x57,
20979 0x0b, 0x28, 0x2f, 0xa3, 0xda, 0xd4, 0xe4, 0x0f, 0xa9, 0x27, 0x53, 0x04,
20980 0x1b, 0xfc, 0xac, 0xe6, 0x7a, 0x07, 0xae, 0x63, 0xc5, 0xdb, 0xe2, 0xea,
20981 0x94, 0x8b, 0xc4, 0xd5, 0x9d, 0xf8, 0x90, 0x6b, 0xb1, 0x0d, 0xd6, 0xeb,
20982 0xc6, 0x0e, 0xcf, 0xad, 0x08, 0x4e, 0xd7, 0xe3, 0x5d, 0x50, 0x1e, 0xb3,
20983 0x5b, 0x23, 0x38, 0x34, 0x68, 0x46, 0x03, 0x8c, 0xdd, 0x9c, 0x7d, 0xa0,
20984 0xcd, 0x1a, 0x41, 0x1c};
20985
20986 return GFInv[Byte];
20987}
20988
20989uint8_t GFNIAffine(uint8_t XByte, const APInt &AQword, const APSInt &Imm,
20990 bool Inverse) {
20991 unsigned NumBitsInByte = 8;
20992 // Computing the affine transformation
20993 uint8_t RetByte = 0;
20994 for (uint32_t BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
20995 uint8_t AByte =
20996 AQword.lshr(shiftAmt: (7 - static_cast<int32_t>(BitIdx)) * NumBitsInByte)
20997 .getLoBits(numBits: 8)
20998 .getZExtValue();
20999 uint8_t Product;
21000 if (Inverse) {
21001 Product = AByte & GFNIMultiplicativeInverse(Byte: XByte);
21002 } else {
21003 Product = AByte & XByte;
21004 }
21005 uint8_t Parity = 0;
21006
21007 // Dot product in GF(2) uses XOR instead of addition
21008 for (unsigned PBitIdx = 0; PBitIdx != NumBitsInByte; ++PBitIdx) {
21009 Parity = Parity ^ ((Product >> PBitIdx) & 0x1);
21010 }
21011
21012 uint8_t Temp = Imm[BitIdx] ? 1 : 0;
21013 RetByte |= (Temp ^ Parity) << BitIdx;
21014 }
21015 return RetByte;
21016}
21017
21018uint8_t GFNIMul(uint8_t AByte, uint8_t BByte) {
21019 // Multiplying two polynomials of degree 7
21020 // Polynomial of degree 7
21021 // x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
21022 uint16_t TWord = 0;
21023 unsigned NumBitsInByte = 8;
21024 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
21025 if ((BByte >> BitIdx) & 0x1) {
21026 TWord = TWord ^ (AByte << BitIdx);
21027 }
21028 }
21029
21030 // When multiplying two polynomials of degree 7
21031 // results in a polynomial of degree 14
21032 // so the result has to be reduced to 7
21033 // Reduction polynomial is x^8 + x^4 + x^3 + x + 1 i.e. 0x11B
21034 for (int32_t BitIdx = 14; BitIdx > 7; --BitIdx) {
21035 if ((TWord >> BitIdx) & 0x1) {
21036 TWord = TWord ^ (0x11B << (BitIdx - 8));
21037 }
21038 }
21039 return (TWord & 0xFF);
21040}
21041
21042void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D,
21043 APFloat &ResR, APFloat &ResI) {
21044 // This is an implementation of complex multiplication according to the
21045 // constraints laid out in C11 Annex G. The implementation uses the
21046 // following naming scheme:
21047 // (a + ib) * (c + id)
21048
21049 APFloat AC = A * C;
21050 APFloat BD = B * D;
21051 APFloat AD = A * D;
21052 APFloat BC = B * C;
21053 ResR = AC - BD;
21054 ResI = AD + BC;
21055 if (ResR.isNaN() && ResI.isNaN()) {
21056 bool Recalc = false;
21057 if (A.isInfinity() || B.isInfinity()) {
21058 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
21059 Sign: A);
21060 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
21061 Sign: B);
21062 if (C.isNaN())
21063 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
21064 if (D.isNaN())
21065 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
21066 Recalc = true;
21067 }
21068 if (C.isInfinity() || D.isInfinity()) {
21069 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
21070 Sign: C);
21071 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
21072 Sign: D);
21073 if (A.isNaN())
21074 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
21075 if (B.isNaN())
21076 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
21077 Recalc = true;
21078 }
21079 if (!Recalc && (AC.isInfinity() || BD.isInfinity() || AD.isInfinity() ||
21080 BC.isInfinity())) {
21081 if (A.isNaN())
21082 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
21083 if (B.isNaN())
21084 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
21085 if (C.isNaN())
21086 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
21087 if (D.isNaN())
21088 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
21089 Recalc = true;
21090 }
21091 if (Recalc) {
21092 ResR = APFloat::getInf(Sem: A.getSemantics()) * (A * C - B * D);
21093 ResI = APFloat::getInf(Sem: A.getSemantics()) * (A * D + B * C);
21094 }
21095 }
21096}
21097
21098void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D,
21099 APFloat &ResR, APFloat &ResI) {
21100 // This is an implementation of complex division according to the
21101 // constraints laid out in C11 Annex G. The implementation uses the
21102 // following naming scheme:
21103 // (a + ib) / (c + id)
21104
21105 int DenomLogB = 0;
21106 APFloat MaxCD = maxnum(A: abs(X: C), B: abs(X: D));
21107 if (MaxCD.isFinite()) {
21108 DenomLogB = ilogb(Arg: MaxCD);
21109 C = scalbn(X: C, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21110 D = scalbn(X: D, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21111 }
21112 APFloat Denom = C * C + D * D;
21113 ResR =
21114 scalbn(X: (A * C + B * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21115 ResI =
21116 scalbn(X: (B * C - A * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21117 if (ResR.isNaN() && ResI.isNaN()) {
21118 if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) {
21119 ResR = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * A;
21120 ResI = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * B;
21121 } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() &&
21122 D.isFinite()) {
21123 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
21124 Sign: A);
21125 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
21126 Sign: B);
21127 ResR = APFloat::getInf(Sem: ResR.getSemantics()) * (A * C + B * D);
21128 ResI = APFloat::getInf(Sem: ResI.getSemantics()) * (B * C - A * D);
21129 } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) {
21130 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
21131 Sign: C);
21132 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
21133 Sign: D);
21134 ResR = APFloat::getZero(Sem: ResR.getSemantics()) * (A * C + B * D);
21135 ResI = APFloat::getZero(Sem: ResI.getSemantics()) * (B * C - A * D);
21136 }
21137 }
21138}
21139
21140APSInt NormalizeRotateAmount(const APSInt &Value, const APSInt &Amount) {
21141 // Normalize shift amount to [0, BitWidth) range to match runtime behavior
21142 APSInt NormAmt = Amount;
21143 unsigned BitWidth = Value.getBitWidth();
21144 unsigned AmtBitWidth = NormAmt.getBitWidth();
21145 if (BitWidth == 1) {
21146 // Rotating a 1-bit value is always a no-op
21147 NormAmt = APSInt(APInt(AmtBitWidth, 0), NormAmt.isUnsigned());
21148 } else if (BitWidth == 2) {
21149 // For 2-bit values: rotation amount is 0 or 1 based on
21150 // whether the amount is even or odd. We can't use srem here because
21151 // the divisor (2) would be misinterpreted as -2 in 2-bit signed arithmetic.
21152 NormAmt =
21153 APSInt(APInt(AmtBitWidth, NormAmt[0] ? 1 : 0), NormAmt.isUnsigned());
21154 } else {
21155 APInt Divisor;
21156 if (AmtBitWidth > BitWidth) {
21157 Divisor = llvm::APInt(AmtBitWidth, BitWidth);
21158 } else {
21159 Divisor = llvm::APInt(BitWidth, BitWidth);
21160 if (AmtBitWidth < BitWidth) {
21161 NormAmt = NormAmt.extend(width: BitWidth);
21162 }
21163 }
21164
21165 // Normalize to [0, BitWidth)
21166 if (NormAmt.isSigned()) {
21167 NormAmt = APSInt(NormAmt.srem(RHS: Divisor), /*isUnsigned=*/false);
21168 if (NormAmt.isNegative()) {
21169 APSInt SignedDivisor(Divisor, /*isUnsigned=*/false);
21170 NormAmt += SignedDivisor;
21171 }
21172 } else {
21173 NormAmt = APSInt(NormAmt.urem(RHS: Divisor), /*isUnsigned=*/true);
21174 }
21175 }
21176
21177 return NormAmt;
21178}
21179
21180bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
21181 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
21182 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
21183
21184 // Track whether the LHS or RHS is real at the type system level. When this is
21185 // the case we can simplify our evaluation strategy.
21186 bool LHSReal = false, RHSReal = false;
21187
21188 bool LHSOK;
21189 if (E->getLHS()->getType()->isRealFloatingType()) {
21190 LHSReal = true;
21191 APFloat &Real = Result.FloatReal;
21192 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: Real, Info);
21193 if (LHSOK) {
21194 Result.makeComplexFloat();
21195 Result.FloatImag = APFloat(Real.getSemantics());
21196 }
21197 } else {
21198 LHSOK = Visit(S: E->getLHS());
21199 }
21200 if (!LHSOK && !Info.noteFailure())
21201 return false;
21202
21203 ComplexValue RHS;
21204 if (E->getRHS()->getType()->isRealFloatingType()) {
21205 RHSReal = true;
21206 APFloat &Real = RHS.FloatReal;
21207 if (!EvaluateFloat(E: E->getRHS(), Result&: Real, Info) || !LHSOK)
21208 return false;
21209 RHS.makeComplexFloat();
21210 RHS.FloatImag = APFloat(Real.getSemantics());
21211 } else if (!EvaluateComplex(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
21212 return false;
21213
21214 assert(!(LHSReal && RHSReal) &&
21215 "Cannot have both operands of a complex operation be real.");
21216 switch (E->getOpcode()) {
21217 default: return Error(E);
21218 case BO_Add:
21219 if (Result.isComplexFloat()) {
21220 Result.getComplexFloatReal().add(RHS: RHS.getComplexFloatReal(),
21221 RM: APFloat::rmNearestTiesToEven);
21222 if (LHSReal)
21223 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
21224 else if (!RHSReal)
21225 Result.getComplexFloatImag().add(RHS: RHS.getComplexFloatImag(),
21226 RM: APFloat::rmNearestTiesToEven);
21227 } else {
21228 Result.getComplexIntReal() += RHS.getComplexIntReal();
21229 Result.getComplexIntImag() += RHS.getComplexIntImag();
21230 }
21231 break;
21232 case BO_Sub:
21233 if (Result.isComplexFloat()) {
21234 Result.getComplexFloatReal().subtract(RHS: RHS.getComplexFloatReal(),
21235 RM: APFloat::rmNearestTiesToEven);
21236 if (LHSReal) {
21237 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
21238 Result.getComplexFloatImag().changeSign();
21239 } else if (!RHSReal) {
21240 Result.getComplexFloatImag().subtract(RHS: RHS.getComplexFloatImag(),
21241 RM: APFloat::rmNearestTiesToEven);
21242 }
21243 } else {
21244 Result.getComplexIntReal() -= RHS.getComplexIntReal();
21245 Result.getComplexIntImag() -= RHS.getComplexIntImag();
21246 }
21247 break;
21248 case BO_Mul:
21249 if (Result.isComplexFloat()) {
21250 // This is an implementation of complex multiplication according to the
21251 // constraints laid out in C11 Annex G. The implementation uses the
21252 // following naming scheme:
21253 // (a + ib) * (c + id)
21254 ComplexValue LHS = Result;
21255 APFloat &A = LHS.getComplexFloatReal();
21256 APFloat &B = LHS.getComplexFloatImag();
21257 APFloat &C = RHS.getComplexFloatReal();
21258 APFloat &D = RHS.getComplexFloatImag();
21259 APFloat &ResR = Result.getComplexFloatReal();
21260 APFloat &ResI = Result.getComplexFloatImag();
21261 if (LHSReal) {
21262 assert(!RHSReal && "Cannot have two real operands for a complex op!");
21263 ResR = A;
21264 ResI = A;
21265 // ResR = A * C;
21266 // ResI = A * D;
21267 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: C) ||
21268 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: D))
21269 return false;
21270 } else if (RHSReal) {
21271 // ResR = C * A;
21272 // ResI = C * B;
21273 ResR = C;
21274 ResI = C;
21275 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: A) ||
21276 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: B))
21277 return false;
21278 } else {
21279 HandleComplexComplexMul(A, B, C, D, ResR, ResI);
21280 }
21281 } else {
21282 ComplexValue LHS = Result;
21283 Result.getComplexIntReal() =
21284 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
21285 LHS.getComplexIntImag() * RHS.getComplexIntImag());
21286 Result.getComplexIntImag() =
21287 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
21288 LHS.getComplexIntImag() * RHS.getComplexIntReal());
21289 }
21290 break;
21291 case BO_Div:
21292 if (Result.isComplexFloat()) {
21293 // This is an implementation of complex division according to the
21294 // constraints laid out in C11 Annex G. The implementation uses the
21295 // following naming scheme:
21296 // (a + ib) / (c + id)
21297 ComplexValue LHS = Result;
21298 APFloat &A = LHS.getComplexFloatReal();
21299 APFloat &B = LHS.getComplexFloatImag();
21300 APFloat &C = RHS.getComplexFloatReal();
21301 APFloat &D = RHS.getComplexFloatImag();
21302 APFloat &ResR = Result.getComplexFloatReal();
21303 APFloat &ResI = Result.getComplexFloatImag();
21304 if (RHSReal) {
21305 ResR = A;
21306 ResI = B;
21307 // ResR = A / C;
21308 // ResI = B / C;
21309 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Div, RHS: C) ||
21310 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Div, RHS: C))
21311 return false;
21312 } else {
21313 if (LHSReal) {
21314 // No real optimizations we can do here, stub out with zero.
21315 B = APFloat::getZero(Sem: A.getSemantics());
21316 }
21317 HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
21318 }
21319 } else {
21320 ComplexValue LHS = Result;
21321 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
21322 RHS.getComplexIntImag() * RHS.getComplexIntImag();
21323 if (Den.isZero())
21324 return Error(E, D: diag::note_expr_divide_by_zero);
21325
21326 Result.getComplexIntReal() =
21327 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
21328 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
21329 Result.getComplexIntImag() =
21330 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
21331 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
21332 }
21333 break;
21334 }
21335
21336 return true;
21337}
21338
21339bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
21340 // Get the operand value into 'Result'.
21341 if (!Visit(S: E->getSubExpr()))
21342 return false;
21343
21344 switch (E->getOpcode()) {
21345 default:
21346 return Error(E);
21347 case UO_Extension:
21348 return true;
21349 case UO_Plus:
21350 // The result is always just the subexpr.
21351 return true;
21352 case UO_Minus:
21353 if (Result.isComplexFloat()) {
21354 Result.getComplexFloatReal().changeSign();
21355 Result.getComplexFloatImag().changeSign();
21356 }
21357 else {
21358 Result.getComplexIntReal() = -Result.getComplexIntReal();
21359 Result.getComplexIntImag() = -Result.getComplexIntImag();
21360 }
21361 return true;
21362 case UO_Not:
21363 if (Result.isComplexFloat())
21364 Result.getComplexFloatImag().changeSign();
21365 else
21366 Result.getComplexIntImag() = -Result.getComplexIntImag();
21367 return true;
21368 }
21369}
21370
21371bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
21372 if (E->getNumInits() == 2) {
21373 if (E->getType()->isComplexType()) {
21374 Result.makeComplexFloat();
21375 if (!EvaluateFloat(E: E->getInit(Init: 0), Result&: Result.FloatReal, Info))
21376 return false;
21377 if (!EvaluateFloat(E: E->getInit(Init: 1), Result&: Result.FloatImag, Info))
21378 return false;
21379 } else {
21380 Result.makeComplexInt();
21381 if (!EvaluateInteger(E: E->getInit(Init: 0), Result&: Result.IntReal, Info))
21382 return false;
21383 if (!EvaluateInteger(E: E->getInit(Init: 1), Result&: Result.IntImag, Info))
21384 return false;
21385 }
21386 return true;
21387 }
21388 return ExprEvaluatorBaseTy::VisitInitListExpr(E);
21389}
21390
21391bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) {
21392 if (!IsConstantEvaluatedBuiltinCall(E))
21393 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21394
21395 switch (E->getBuiltinCallee()) {
21396 case Builtin::BI__builtin_complex:
21397 Result.makeComplexFloat();
21398 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: Result.FloatReal, Info))
21399 return false;
21400 if (!EvaluateFloat(E: E->getArg(Arg: 1), Result&: Result.FloatImag, Info))
21401 return false;
21402 return true;
21403
21404 default:
21405 return false;
21406 }
21407}
21408
21409//===----------------------------------------------------------------------===//
21410// Atomic expression evaluation, essentially just handling the NonAtomicToAtomic
21411// implicit conversion.
21412//===----------------------------------------------------------------------===//
21413
21414namespace {
21415class AtomicExprEvaluator :
21416 public ExprEvaluatorBase<AtomicExprEvaluator> {
21417 const LValue *This;
21418 APValue &Result;
21419public:
21420 AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result)
21421 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
21422
21423 bool Success(const APValue &V, const Expr *E) {
21424 Result = V;
21425 return true;
21426 }
21427
21428 bool ZeroInitialization(const Expr *E) {
21429 ImplicitValueInitExpr VIE(
21430 E->getType()->castAs<AtomicType>()->getValueType());
21431 // For atomic-qualified class (and array) types in C++, initialize the
21432 // _Atomic-wrapped subobject directly, in-place.
21433 return This ? EvaluateInPlace(Result, Info, This: *This, E: &VIE)
21434 : Evaluate(Result, Info, E: &VIE);
21435 }
21436
21437 bool VisitCastExpr(const CastExpr *E) {
21438 switch (E->getCastKind()) {
21439 default:
21440 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21441 case CK_NullToPointer:
21442 VisitIgnoredValue(E: E->getSubExpr());
21443 return ZeroInitialization(E);
21444 case CK_NonAtomicToAtomic:
21445 return This ? EvaluateInPlace(Result, Info, This: *This, E: E->getSubExpr())
21446 : Evaluate(Result, Info, E: E->getSubExpr());
21447 }
21448 }
21449};
21450} // end anonymous namespace
21451
21452static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
21453 EvalInfo &Info) {
21454 assert(!E->isValueDependent());
21455 assert(E->isPRValue() && E->getType()->isAtomicType());
21456 return AtomicExprEvaluator(Info, This, Result).Visit(S: E);
21457}
21458
21459//===----------------------------------------------------------------------===//
21460// Void expression evaluation, primarily for a cast to void on the LHS of a
21461// comma operator
21462//===----------------------------------------------------------------------===//
21463
21464namespace {
21465class VoidExprEvaluator
21466 : public ExprEvaluatorBase<VoidExprEvaluator> {
21467public:
21468 VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
21469
21470 bool Success(const APValue &V, const Expr *e) { return true; }
21471
21472 bool ZeroInitialization(const Expr *E) { return true; }
21473
21474 bool VisitCastExpr(const CastExpr *E) {
21475 switch (E->getCastKind()) {
21476 default:
21477 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21478 case CK_ToVoid:
21479 VisitIgnoredValue(E: E->getSubExpr());
21480 return true;
21481 }
21482 }
21483
21484 bool VisitCallExpr(const CallExpr *E) {
21485 if (!IsConstantEvaluatedBuiltinCall(E))
21486 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21487
21488 switch (E->getBuiltinCallee()) {
21489 case Builtin::BI__assume:
21490 case Builtin::BI__builtin_assume:
21491 // The argument is not evaluated!
21492 return true;
21493
21494 case Builtin::BI__builtin_operator_delete:
21495 return HandleOperatorDeleteCall(Info, E);
21496
21497 default:
21498 return false;
21499 }
21500 }
21501
21502 bool VisitCXXDeleteExpr(const CXXDeleteExpr *E);
21503};
21504} // end anonymous namespace
21505
21506bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
21507 // We cannot speculatively evaluate a delete expression.
21508 if (Info.SpeculativeEvaluationDepth)
21509 return false;
21510
21511 FunctionDecl *OperatorDelete = E->getOperatorDelete();
21512 if (!OperatorDelete
21513 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
21514 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
21515 << isa<CXXMethodDecl>(Val: OperatorDelete) << OperatorDelete;
21516 return false;
21517 }
21518
21519 const Expr *Arg = E->getArgument();
21520
21521 LValue Pointer;
21522 if (!EvaluatePointer(E: Arg, Result&: Pointer, Info))
21523 return false;
21524 if (Pointer.Designator.Invalid)
21525 return false;
21526
21527 // Deleting a null pointer has no effect.
21528 if (Pointer.isNullPointer()) {
21529 // This is the only case where we need to produce an extension warning:
21530 // the only other way we can succeed is if we find a dynamic allocation,
21531 // and we will have warned when we allocated it in that case.
21532 if (!Info.getLangOpts().CPlusPlus20)
21533 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
21534 return true;
21535 }
21536
21537 std::optional<DynAlloc *> Alloc = CheckDeleteKind(
21538 Info, E, Pointer, DeallocKind: E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New);
21539 if (!Alloc)
21540 return false;
21541 QualType AllocType = Pointer.Base.getDynamicAllocType();
21542
21543 // For the non-array case, the designator must be empty if the static type
21544 // does not have a virtual destructor.
21545 if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 &&
21546 !hasVirtualDestructor(T: Arg->getType()->getPointeeType())) {
21547 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_base_nonvirt_dtor)
21548 << Arg->getType()->getPointeeType() << AllocType;
21549 return false;
21550 }
21551
21552 // For a class type with a virtual destructor, the selected operator delete
21553 // is the one looked up when building the destructor.
21554 if (!E->isArrayForm() && !E->isGlobalDelete()) {
21555 const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(T: AllocType);
21556 if (VirtualDelete &&
21557 !VirtualDelete
21558 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
21559 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
21560 << isa<CXXMethodDecl>(Val: VirtualDelete) << VirtualDelete;
21561 return false;
21562 }
21563 }
21564
21565 if (!HandleDestruction(Info, Loc: E->getExprLoc(), LVBase: Pointer.getLValueBase(),
21566 Value&: (*Alloc)->Value, T: AllocType))
21567 return false;
21568
21569 if (!Info.HeapAllocs.erase(x: Pointer.Base.dyn_cast<DynamicAllocLValue>())) {
21570 // The element was already erased. This means the destructor call also
21571 // deleted the object.
21572 // FIXME: This probably results in undefined behavior before we get this
21573 // far, and should be diagnosed elsewhere first.
21574 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
21575 return false;
21576 }
21577
21578 return true;
21579}
21580
21581static bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
21582 assert(!E->isValueDependent());
21583 assert(E->isPRValue() && E->getType()->isVoidType());
21584 return VoidExprEvaluator(Info).Visit(S: E);
21585}
21586
21587//===----------------------------------------------------------------------===//
21588// Top level Expr::EvaluateAsRValue method.
21589//===----------------------------------------------------------------------===//
21590
21591static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {
21592 assert(!E->isValueDependent());
21593 // In C, function designators are not lvalues, but we evaluate them as if they
21594 // are.
21595 QualType T = E->getType();
21596 if (E->isGLValue() || T->isFunctionType()) {
21597 LValue LV;
21598 if (!EvaluateLValue(E, Result&: LV, Info))
21599 return false;
21600 LV.moveInto(V&: Result);
21601 } else if (T->isVectorType()) {
21602 if (!EvaluateVector(E, Result, Info))
21603 return false;
21604 } else if (T->isConstantMatrixType()) {
21605 if (!EvaluateMatrix(E, Result, Info))
21606 return false;
21607 } else if (T->isIntegralOrEnumerationType()) {
21608 if (!IntExprEvaluator(Info, Result).Visit(S: E))
21609 return false;
21610 } else if (T->hasPointerRepresentation()) {
21611 LValue LV;
21612 if (!EvaluatePointer(E, Result&: LV, Info))
21613 return false;
21614 LV.moveInto(V&: Result);
21615 } else if (T->isRealFloatingType()) {
21616 llvm::APFloat F(0.0);
21617 if (!EvaluateFloat(E, Result&: F, Info))
21618 return false;
21619 Result = APValue(F);
21620 } else if (T->isAnyComplexType()) {
21621 ComplexValue C;
21622 if (!EvaluateComplex(E, Result&: C, Info))
21623 return false;
21624 C.moveInto(v&: Result);
21625 } else if (T->isFixedPointType()) {
21626 if (!FixedPointExprEvaluator(Info, Result).Visit(S: E)) return false;
21627 } else if (T->isMemberPointerType()) {
21628 MemberPtr P;
21629 if (!EvaluateMemberPointer(E, Result&: P, Info))
21630 return false;
21631 P.moveInto(V&: Result);
21632 return true;
21633 } else if (T->isArrayType()) {
21634 LValue LV;
21635 APValue &Value =
21636 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
21637 if (!EvaluateArray(E, This: LV, Result&: Value, Info))
21638 return false;
21639 Result = Value;
21640 } else if (T->isRecordType()) {
21641 LValue LV;
21642 APValue &Value =
21643 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
21644 if (!EvaluateRecord(E, This: LV, Result&: Value, Info))
21645 return false;
21646 Result = Value;
21647 } else if (T->isVoidType()) {
21648 if (!Info.getLangOpts().CPlusPlus11)
21649 Info.CCEDiag(E, DiagId: diag::note_constexpr_nonliteral)
21650 << E->getType();
21651 if (!EvaluateVoid(E, Info))
21652 return false;
21653 } else if (T->isAtomicType()) {
21654 QualType Unqual = T.getAtomicUnqualifiedType();
21655 if (Unqual->isArrayType() || Unqual->isRecordType()) {
21656 LValue LV;
21657 APValue &Value = Info.CurrentCall->createTemporary(
21658 Key: E, T: Unqual, Scope: ScopeKind::FullExpression, LV);
21659 if (!EvaluateAtomic(E, This: &LV, Result&: Value, Info))
21660 return false;
21661 Result = Value;
21662 } else {
21663 if (!EvaluateAtomic(E, This: nullptr, Result, Info))
21664 return false;
21665 }
21666 } else if (Info.getLangOpts().CPlusPlus11) {
21667 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral) << E->getType();
21668 return false;
21669 } else {
21670 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
21671 return false;
21672 }
21673
21674 return true;
21675}
21676
21677/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some
21678/// cases, the in-place evaluation is essential, since later initializers for
21679/// an object can indirectly refer to subobjects which were initialized earlier.
21680static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,
21681 const Expr *E, bool AllowNonLiteralTypes) {
21682 assert(!E->isValueDependent());
21683
21684 // Normally expressions passed to EvaluateInPlace have a type, but not when
21685 // a VarDecl initializer is evaluated before the untyped ParenListExpr is
21686 // replaced with a CXXConstructExpr. This can happen in LLDB.
21687 if (E->getType().isNull())
21688 return false;
21689
21690 if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, This: &This))
21691 return false;
21692
21693 if (E->isPRValue()) {
21694 // Evaluate arrays and record types in-place, so that later initializers can
21695 // refer to earlier-initialized members of the object.
21696 QualType T = E->getType();
21697 if (T->isArrayType())
21698 return EvaluateArray(E, This, Result, Info);
21699 else if (T->isRecordType())
21700 return EvaluateRecord(E, This, Result, Info);
21701 else if (T->isAtomicType()) {
21702 QualType Unqual = T.getAtomicUnqualifiedType();
21703 if (Unqual->isArrayType() || Unqual->isRecordType())
21704 return EvaluateAtomic(E, This: &This, Result, Info);
21705 }
21706 }
21707
21708 // For any other type, in-place evaluation is unimportant.
21709 return Evaluate(Result, Info, E);
21710}
21711
21712/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
21713/// lvalue-to-rvalue cast if it is an lvalue.
21714static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
21715 assert(!E->isValueDependent());
21716
21717 if (E->getType().isNull())
21718 return false;
21719
21720 if (!CheckLiteralType(Info, E))
21721 return false;
21722
21723 if (Info.EnableNewConstInterp) {
21724 if (!Info.Ctx.getInterpContext().evaluateAsRValue(Parent&: Info, E, Result))
21725 return false;
21726 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
21727 Kind: ConstantExprKind::Normal);
21728 }
21729
21730 if (!::Evaluate(Result, Info, E))
21731 return false;
21732
21733 // Implicit lvalue-to-rvalue cast.
21734 if (E->isGLValue()) {
21735 LValue LV;
21736 LV.setFrom(Ctx: Info.Ctx, V: Result);
21737 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
21738 return false;
21739 }
21740
21741 // Check this core constant expression is a constant expression.
21742 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
21743 Kind: ConstantExprKind::Normal) &&
21744 CheckMemoryLeaks(Info);
21745}
21746
21747static bool FastEvaluateAsRValue(const Expr *Exp, APValue &Result,
21748 const ASTContext &Ctx, bool &IsConst) {
21749 // Fast-path evaluations of integer literals, since we sometimes see files
21750 // containing vast quantities of these.
21751 if (const auto *L = dyn_cast<IntegerLiteral>(Val: Exp)) {
21752 Result =
21753 APValue(APSInt(L->getValue(), L->getType()->isUnsignedIntegerType()));
21754 IsConst = true;
21755 return true;
21756 }
21757
21758 if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Val: Exp)) {
21759 Result = APValue(APSInt(APInt(1, L->getValue())));
21760 IsConst = true;
21761 return true;
21762 }
21763
21764 if (const auto *FL = dyn_cast<FloatingLiteral>(Val: Exp)) {
21765 Result = APValue(FL->getValue());
21766 IsConst = true;
21767 return true;
21768 }
21769
21770 if (const auto *L = dyn_cast<CharacterLiteral>(Val: Exp)) {
21771 Result = APValue(Ctx.MakeIntValue(Value: L->getValue(), Type: L->getType()));
21772 IsConst = true;
21773 return true;
21774 }
21775
21776 if (const auto *CE = dyn_cast<ConstantExpr>(Val: Exp)) {
21777 if (CE->hasAPValueResult()) {
21778 APValue APV = CE->getAPValueResult();
21779 if (!APV.isLValue()) {
21780 Result = std::move(APV);
21781 IsConst = true;
21782 return true;
21783 }
21784 }
21785
21786 // The SubExpr is usually just an IntegerLiteral.
21787 return FastEvaluateAsRValue(Exp: CE->getSubExpr(), Result, Ctx, IsConst);
21788 }
21789
21790 // This case should be rare, but we need to check it before we check on
21791 // the type below.
21792 if (Exp->getType().isNull()) {
21793 IsConst = false;
21794 return true;
21795 }
21796
21797 return false;
21798}
21799
21800static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result,
21801 Expr::SideEffectsKind SEK) {
21802 return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) ||
21803 (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior);
21804}
21805
21806static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result,
21807 const ASTContext &Ctx, EvalInfo &Info) {
21808 assert(!E->isValueDependent());
21809 bool IsConst;
21810 if (FastEvaluateAsRValue(Exp: E, Result&: Result.Val, Ctx, IsConst))
21811 return IsConst;
21812
21813 return EvaluateAsRValue(Info, E, Result&: Result.Val);
21814}
21815
21816static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult,
21817 const ASTContext &Ctx,
21818 Expr::SideEffectsKind AllowSideEffects,
21819 EvalInfo &Info) {
21820 assert(!E->isValueDependent());
21821 if (!E->getType()->isIntegralOrEnumerationType())
21822 return false;
21823
21824 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info) ||
21825 !ExprResult.Val.isInt() ||
21826 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
21827 return false;
21828
21829 return true;
21830}
21831
21832static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult,
21833 const ASTContext &Ctx,
21834 Expr::SideEffectsKind AllowSideEffects,
21835 EvalInfo &Info) {
21836 assert(!E->isValueDependent());
21837 if (!E->getType()->isFixedPointType())
21838 return false;
21839
21840 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info))
21841 return false;
21842
21843 if (!ExprResult.Val.isFixedPoint() ||
21844 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
21845 return false;
21846
21847 return true;
21848}
21849
21850/// EvaluateAsRValue - Return true if this is a constant which we can fold using
21851/// any crazy technique (that has nothing to do with language standards) that
21852/// we want to. If this function returns true, it returns the folded constant
21853/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
21854/// will be applied to the result.
21855bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx,
21856 bool InConstantContext) const {
21857 assert(!isValueDependent() &&
21858 "Expression evaluator can't be called on a dependent expression.");
21859 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsRValue");
21860 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21861 Info.InConstantContext = InConstantContext;
21862 return ::EvaluateAsRValue(E: this, Result, Ctx, Info);
21863}
21864
21865bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx,
21866 bool InConstantContext) const {
21867 assert(!isValueDependent() &&
21868 "Expression evaluator can't be called on a dependent expression.");
21869 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsBooleanCondition");
21870 EvalResult Scratch;
21871 return EvaluateAsRValue(Result&: Scratch, Ctx, InConstantContext) &&
21872 HandleConversionToBool(Val: Scratch.Val, Result);
21873}
21874
21875bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx,
21876 SideEffectsKind AllowSideEffects,
21877 bool InConstantContext) const {
21878 assert(!isValueDependent() &&
21879 "Expression evaluator can't be called on a dependent expression.");
21880 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsInt");
21881 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21882 Info.InConstantContext = InConstantContext;
21883 return ::EvaluateAsInt(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
21884}
21885
21886bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx,
21887 SideEffectsKind AllowSideEffects,
21888 bool InConstantContext) const {
21889 assert(!isValueDependent() &&
21890 "Expression evaluator can't be called on a dependent expression.");
21891 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFixedPoint");
21892 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21893 Info.InConstantContext = InConstantContext;
21894 return ::EvaluateAsFixedPoint(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
21895}
21896
21897bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx,
21898 SideEffectsKind AllowSideEffects,
21899 bool InConstantContext) const {
21900 assert(!isValueDependent() &&
21901 "Expression evaluator can't be called on a dependent expression.");
21902
21903 if (!getType()->isRealFloatingType())
21904 return false;
21905
21906 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFloat");
21907 EvalResult ExprResult;
21908 if (!EvaluateAsRValue(Result&: ExprResult, Ctx, InConstantContext) ||
21909 !ExprResult.Val.isFloat() ||
21910 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
21911 return false;
21912
21913 Result = ExprResult.Val.getFloat();
21914 return true;
21915}
21916
21917bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx,
21918 bool InConstantContext) const {
21919 assert(!isValueDependent() &&
21920 "Expression evaluator can't be called on a dependent expression.");
21921
21922 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsLValue");
21923 EvalInfo Info(Ctx, Result, EvaluationMode::ConstantFold);
21924 Info.InConstantContext = InConstantContext;
21925 LValue LV;
21926 CheckedTemporaries CheckedTemps;
21927
21928 if (Info.EnableNewConstInterp) {
21929 if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val,
21930 Kind: ConstantExprKind::Normal))
21931 return false;
21932
21933 LV.setFrom(Ctx, V: Result.Val);
21934 return CheckLValueConstantExpression(
21935 Info, Loc: getExprLoc(), Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV,
21936 Kind: ConstantExprKind::Normal, CheckedTemps);
21937 }
21938
21939 if (!EvaluateLValue(E: this, Result&: LV, Info) || !Info.discardCleanups() ||
21940 Result.HasSideEffects ||
21941 !CheckLValueConstantExpression(Info, Loc: getExprLoc(),
21942 Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV,
21943 Kind: ConstantExprKind::Normal, CheckedTemps))
21944 return false;
21945
21946 LV.moveInto(V&: Result.Val);
21947 return true;
21948}
21949
21950static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base,
21951 APValue DestroyedValue, QualType Type,
21952 SourceLocation Loc, Expr::EvalStatus &EStatus,
21953 bool IsConstantDestruction) {
21954 EvalInfo Info(Ctx, EStatus,
21955 IsConstantDestruction ? EvaluationMode::ConstantExpression
21956 : EvaluationMode::ConstantFold);
21957 Info.setEvaluatingDecl(Base, Value&: DestroyedValue,
21958 EDK: EvalInfo::EvaluatingDeclKind::Dtor);
21959 Info.InConstantContext = IsConstantDestruction;
21960
21961 LValue LVal;
21962 LVal.set(B: Base);
21963
21964 if (!HandleDestruction(Info, Loc, LVBase: Base, Value&: DestroyedValue, T: Type) ||
21965 EStatus.HasSideEffects)
21966 return false;
21967
21968 if (!Info.discardCleanups())
21969 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
21970
21971 return true;
21972}
21973
21974bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
21975 ConstantExprKind Kind) const {
21976 assert(!isValueDependent() &&
21977 "Expression evaluator can't be called on a dependent expression.");
21978 bool IsConst;
21979 if (FastEvaluateAsRValue(Exp: this, Result&: Result.Val, Ctx, IsConst) &&
21980 Result.Val.hasValue())
21981 return true;
21982
21983 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr");
21984 EvaluationMode EM = EvaluationMode::ConstantExpression;
21985 EvalInfo Info(Ctx, Result, EM);
21986 Info.InConstantContext = true;
21987
21988 if (Info.EnableNewConstInterp) {
21989 if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val, Kind))
21990 return false;
21991 return CheckConstantExpression(Info, DiagLoc: getExprLoc(),
21992 Type: getStorageType(Ctx, E: this), Value: Result.Val, Kind);
21993 }
21994
21995 // The type of the object we're initializing is 'const T' for a class NTTP.
21996 QualType T = getType();
21997 if (Kind == ConstantExprKind::ClassTemplateArgument)
21998 T.addConst();
21999
22000 // If we're evaluating a prvalue, fake up a MaterializeTemporaryExpr to
22001 // represent the result of the evaluation. CheckConstantExpression ensures
22002 // this doesn't escape.
22003 MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true);
22004 APValue::LValueBase Base(&BaseMTE);
22005 Info.setEvaluatingDecl(Base, Value&: Result.Val);
22006
22007 LValue LVal;
22008 LVal.set(B: Base);
22009 // C++23 [intro.execution]/p5
22010 // A full-expression is [...] a constant-expression
22011 // So we need to make sure temporary objects are destroyed after having
22012 // evaluating the expression (per C++23 [class.temporary]/p4).
22013 FullExpressionRAII Scope(Info);
22014 if (!::EvaluateInPlace(Result&: Result.Val, Info, This: LVal, E: this) ||
22015 Result.HasSideEffects || !Scope.destroy())
22016 return false;
22017
22018 if (!Info.discardCleanups())
22019 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22020
22021 if (!CheckConstantExpression(Info, DiagLoc: getExprLoc(), Type: getStorageType(Ctx, E: this),
22022 Value: Result.Val, Kind))
22023 return false;
22024 if (!CheckMemoryLeaks(Info))
22025 return false;
22026
22027 // If this is a class template argument, it's required to have constant
22028 // destruction too.
22029 if (Kind == ConstantExprKind::ClassTemplateArgument &&
22030 (!EvaluateDestruction(Ctx, Base, DestroyedValue: Result.Val, Type: T, Loc: getBeginLoc(), EStatus&: Result,
22031 IsConstantDestruction: true) ||
22032 Result.HasSideEffects)) {
22033 // FIXME: Prefix a note to indicate that the problem is lack of constant
22034 // destruction.
22035 return false;
22036 }
22037
22038 return true;
22039}
22040
22041bool Expr::EvaluateAsInitializer(const ASTContext &Ctx, const VarDecl *VD,
22042 Expr::EvalResult &EStatus,
22043 bool IsConstantInitialization) const {
22044 assert(!isValueDependent() &&
22045 "Expression evaluator can't be called on a dependent expression.");
22046 assert(VD && "Need a valid VarDecl");
22047
22048 llvm::TimeTraceScope TimeScope("EvaluateAsInitializer", [&] {
22049 std::string Name;
22050 llvm::raw_string_ostream OS(Name);
22051 VD->printQualifiedName(OS);
22052 return Name;
22053 });
22054
22055 EvalInfo Info(Ctx, EStatus,
22056 (IsConstantInitialization &&
22057 (Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23))
22058 ? EvaluationMode::ConstantExpression
22059 : EvaluationMode::ConstantFold);
22060 Info.setEvaluatingDecl(Base: VD, Value&: EStatus.Val);
22061 Info.InConstantContext = IsConstantInitialization;
22062
22063 SourceLocation DeclLoc = VD->getLocation();
22064 QualType DeclTy = VD->getType();
22065
22066 if (Info.EnableNewConstInterp) {
22067 auto &InterpCtx = Ctx.getInterpContext();
22068 if (!InterpCtx.evaluateAsInitializer(Parent&: Info, VD, Init: this, Result&: EStatus.Val))
22069 return false;
22070
22071 return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value: EStatus.Val,
22072 Kind: ConstantExprKind::Normal);
22073 } else {
22074 LValue LVal;
22075 LVal.set(B: VD);
22076
22077 {
22078 // C++23 [intro.execution]/p5
22079 // A full-expression is ... an init-declarator ([dcl.decl]) or a
22080 // mem-initializer.
22081 // So we need to make sure temporary objects are destroyed after having
22082 // evaluated the expression (per C++23 [class.temporary]/p4).
22083 //
22084 // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the
22085 // serialization code calls ParmVarDecl::getDefaultArg() which strips the
22086 // outermost FullExpr, such as ExprWithCleanups.
22087 FullExpressionRAII Scope(Info);
22088 if (!EvaluateInPlace(Result&: EStatus.Val, Info, This: LVal, E: this,
22089 /*AllowNonLiteralTypes=*/true) ||
22090 EStatus.HasSideEffects)
22091 return false;
22092 }
22093
22094 // At this point, any lifetime-extended temporaries are completely
22095 // initialized.
22096 Info.performLifetimeExtension();
22097
22098 if (!Info.discardCleanups())
22099 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22100 }
22101
22102 return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value: EStatus.Val,
22103 Kind: ConstantExprKind::Normal) &&
22104 CheckMemoryLeaks(Info);
22105}
22106
22107bool VarDecl::evaluateDestruction(
22108 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
22109 // This function is only meaningful for records and arrays of records.
22110 QualType VarTy = getType();
22111 if (VarTy->isArrayType()) {
22112 QualType ElemTy = getASTContext().getBaseElementType(QT: VarTy);
22113 if (!ElemTy->isRecordType()) {
22114 ensureEvaluatedStmt()->HasConstantDestruction = true;
22115 return true;
22116 }
22117 } else if (!VarTy->isRecordType()) {
22118 ensureEvaluatedStmt()->HasConstantDestruction = true;
22119 return true;
22120 }
22121
22122 Expr::EvalStatus EStatus;
22123 EStatus.Diag = &Notes;
22124
22125 // Only treat the destruction as constant destruction if we formally have
22126 // constant initialization (or are usable in a constant expression).
22127 bool IsConstantDestruction = hasConstantInitialization();
22128 ASTContext &Ctx = getASTContext();
22129
22130 // Make a copy of the value for the destructor to mutate, if we know it.
22131 // Otherwise, treat the value as default-initialized; if the destructor works
22132 // anyway, then the destruction is constant (and must be essentially empty).
22133 APValue DestroyedValue;
22134 if (getEvaluatedValue())
22135 DestroyedValue = *getEvaluatedValue();
22136 else if (!handleDefaultInitValue(T: VarTy, Result&: DestroyedValue))
22137 return false;
22138
22139 if (Ctx.getLangOpts().EnableNewConstInterp) {
22140 EvalInfo Info(Ctx, EStatus,
22141 IsConstantDestruction ? EvaluationMode::ConstantExpression
22142 : EvaluationMode::ConstantFold);
22143 Info.InConstantContext = IsConstantDestruction;
22144 if (!Ctx.getInterpContext().evaluateDestruction(Parent&: Info, VD: this,
22145 Value: std::move(DestroyedValue)))
22146 return false;
22147 ensureEvaluatedStmt()->HasConstantDestruction = true;
22148 return true;
22149 }
22150
22151 if (!EvaluateDestruction(Ctx, Base: this, DestroyedValue: std::move(DestroyedValue), Type: VarTy,
22152 Loc: getLocation(), EStatus, IsConstantDestruction) ||
22153 EStatus.HasSideEffects)
22154 return false;
22155
22156 ensureEvaluatedStmt()->HasConstantDestruction = true;
22157 return true;
22158}
22159
22160/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
22161/// constant folded, but discard the result.
22162bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const {
22163 assert(!isValueDependent() &&
22164 "Expression evaluator can't be called on a dependent expression.");
22165
22166 EvalResult Result;
22167 return EvaluateAsRValue(Result, Ctx, /* in constant context */ InConstantContext: true) &&
22168 !hasUnacceptableSideEffect(Result, SEK);
22169}
22170
22171APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
22172 assert(!isValueDependent() &&
22173 "Expression evaluator can't be called on a dependent expression.");
22174
22175 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstInt");
22176 EvalResult EVResult;
22177 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22178 Info.InConstantContext = true;
22179
22180 bool Result = ::EvaluateAsRValue(E: this, Result&: EVResult, Ctx, Info);
22181 (void)Result;
22182 assert(Result && "Could not evaluate expression");
22183 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22184
22185 return EVResult.Val.getInt();
22186}
22187
22188APSInt Expr::EvaluateKnownConstIntCheckOverflow(
22189 const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const {
22190 assert(!isValueDependent() &&
22191 "Expression evaluator can't be called on a dependent expression.");
22192
22193 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstIntCheckOverflow");
22194 EvalResult EVResult;
22195 EVResult.Diag = Diag;
22196 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22197 Info.InConstantContext = true;
22198 Info.CheckingForUndefinedBehavior = true;
22199
22200 bool Result = ::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
22201 (void)Result;
22202 assert(Result && "Could not evaluate expression");
22203 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22204
22205 return EVResult.Val.getInt();
22206}
22207
22208void Expr::EvaluateForOverflow(const ASTContext &Ctx) const {
22209 assert(!isValueDependent() &&
22210 "Expression evaluator can't be called on a dependent expression.");
22211
22212 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow");
22213 bool IsConst;
22214 EvalResult EVResult;
22215 if (!FastEvaluateAsRValue(Exp: this, Result&: EVResult.Val, Ctx, IsConst)) {
22216 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22217 Info.CheckingForUndefinedBehavior = true;
22218 (void)::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
22219 }
22220}
22221
22222bool Expr::EvalResult::isGlobalLValue() const {
22223 assert(Val.isLValue());
22224 return IsGlobalLValue(B: Val.getLValueBase());
22225}
22226
22227/// isIntegerConstantExpr - this recursive routine will test if an expression is
22228/// an integer constant expression.
22229
22230/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
22231/// comma, etc
22232
22233// CheckICE - This function does the fundamental ICE checking: the returned
22234// ICEDiag contains an ICEKind indicating whether the expression is an ICE.
22235//
22236// Note that to reduce code duplication, this helper does no evaluation
22237// itself; the caller checks whether the expression is evaluatable, and
22238// in the rare cases where CheckICE actually cares about the evaluated
22239// value, it calls into Evaluate.
22240
22241namespace {
22242
22243enum ICEKind {
22244 /// This expression is an ICE.
22245 IK_ICE,
22246 /// This expression is not an ICE, but if it isn't evaluated, it's
22247 /// a legal subexpression for an ICE. This return value is used to handle
22248 /// the comma operator in C99 mode, and non-constant subexpressions.
22249 IK_ICEIfUnevaluated,
22250 /// This expression is not an ICE, and is not a legal subexpression for one.
22251 IK_NotICE
22252};
22253
22254struct ICEDiag {
22255 ICEKind Kind;
22256 SourceLocation Loc;
22257
22258 ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {}
22259};
22260
22261}
22262
22263static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); }
22264
22265static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; }
22266
22267static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) {
22268 Expr::EvalResult EVResult;
22269 Expr::EvalStatus Status;
22270 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
22271
22272 Info.InConstantContext = true;
22273 if (!::EvaluateAsRValue(E, Result&: EVResult, Ctx, Info) || EVResult.HasSideEffects ||
22274 !EVResult.Val.isInt())
22275 return ICEDiag(IK_NotICE, E->getBeginLoc());
22276
22277 return NoDiag();
22278}
22279
22280static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) {
22281 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
22282 if (!E->getType()->isIntegralOrEnumerationType())
22283 return ICEDiag(IK_NotICE, E->getBeginLoc());
22284
22285 switch (E->getStmtClass()) {
22286#define ABSTRACT_STMT(Node)
22287#define STMT(Node, Base) case Expr::Node##Class:
22288#define EXPR(Node, Base)
22289#include "clang/AST/StmtNodes.inc"
22290 case Expr::PredefinedExprClass:
22291 case Expr::FloatingLiteralClass:
22292 case Expr::ImaginaryLiteralClass:
22293 case Expr::StringLiteralClass:
22294 case Expr::ArraySubscriptExprClass:
22295 case Expr::MatrixSingleSubscriptExprClass:
22296 case Expr::MatrixSubscriptExprClass:
22297 case Expr::ArraySectionExprClass:
22298 case Expr::OMPArrayShapingExprClass:
22299 case Expr::OMPIteratorExprClass:
22300 case Expr::CompoundAssignOperatorClass:
22301 case Expr::CompoundLiteralExprClass:
22302 case Expr::ExtVectorElementExprClass:
22303 case Expr::MatrixElementExprClass:
22304 case Expr::DesignatedInitExprClass:
22305 case Expr::ArrayInitLoopExprClass:
22306 case Expr::ArrayInitIndexExprClass:
22307 case Expr::NoInitExprClass:
22308 case Expr::DesignatedInitUpdateExprClass:
22309 case Expr::ImplicitValueInitExprClass:
22310 case Expr::ParenListExprClass:
22311 case Expr::VAArgExprClass:
22312 case Expr::AddrLabelExprClass:
22313 case Expr::StmtExprClass:
22314 case Expr::CXXMemberCallExprClass:
22315 case Expr::CUDAKernelCallExprClass:
22316 case Expr::CXXAddrspaceCastExprClass:
22317 case Expr::CXXDynamicCastExprClass:
22318 case Expr::CXXTypeidExprClass:
22319 case Expr::CXXUuidofExprClass:
22320 case Expr::MSPropertyRefExprClass:
22321 case Expr::MSPropertySubscriptExprClass:
22322 case Expr::CXXNullPtrLiteralExprClass:
22323 case Expr::UserDefinedLiteralClass:
22324 case Expr::CXXThisExprClass:
22325 case Expr::CXXThrowExprClass:
22326 case Expr::CXXNewExprClass:
22327 case Expr::CXXDeleteExprClass:
22328 case Expr::CXXPseudoDestructorExprClass:
22329 case Expr::UnresolvedLookupExprClass:
22330 case Expr::RecoveryExprClass:
22331 case Expr::DependentScopeDeclRefExprClass:
22332 case Expr::CXXConstructExprClass:
22333 case Expr::CXXInheritedCtorInitExprClass:
22334 case Expr::CXXStdInitializerListExprClass:
22335 case Expr::CXXBindTemporaryExprClass:
22336 case Expr::ExprWithCleanupsClass:
22337 case Expr::CXXTemporaryObjectExprClass:
22338 case Expr::CXXUnresolvedConstructExprClass:
22339 case Expr::CXXDependentScopeMemberExprClass:
22340 case Expr::UnresolvedMemberExprClass:
22341 case Expr::ObjCStringLiteralClass:
22342 case Expr::ObjCBoxedExprClass:
22343 case Expr::ObjCArrayLiteralClass:
22344 case Expr::ObjCDictionaryLiteralClass:
22345 case Expr::ObjCEncodeExprClass:
22346 case Expr::ObjCMessageExprClass:
22347 case Expr::ObjCSelectorExprClass:
22348 case Expr::ObjCProtocolExprClass:
22349 case Expr::ObjCIvarRefExprClass:
22350 case Expr::ObjCPropertyRefExprClass:
22351 case Expr::ObjCSubscriptRefExprClass:
22352 case Expr::ObjCIsaExprClass:
22353 case Expr::ObjCAvailabilityCheckExprClass:
22354 case Expr::ShuffleVectorExprClass:
22355 case Expr::ConvertVectorExprClass:
22356 case Expr::BlockExprClass:
22357 case Expr::NoStmtClass:
22358 case Expr::OpaqueValueExprClass:
22359 case Expr::PackExpansionExprClass:
22360 case Expr::SubstNonTypeTemplateParmPackExprClass:
22361 case Expr::FunctionParmPackExprClass:
22362 case Expr::AsTypeExprClass:
22363 case Expr::ObjCIndirectCopyRestoreExprClass:
22364 case Expr::MaterializeTemporaryExprClass:
22365 case Expr::PseudoObjectExprClass:
22366 case Expr::AtomicExprClass:
22367 case Expr::LambdaExprClass:
22368 case Expr::CXXFoldExprClass:
22369 case Expr::CoawaitExprClass:
22370 case Expr::DependentCoawaitExprClass:
22371 case Expr::CoyieldExprClass:
22372 case Expr::SYCLUniqueStableNameExprClass:
22373 case Expr::CXXParenListInitExprClass:
22374 case Expr::HLSLOutArgExprClass:
22375 case Expr::CXXExpansionSelectExprClass:
22376 return ICEDiag(IK_NotICE, E->getBeginLoc());
22377
22378 case Expr::MemberExprClass: {
22379 if (Ctx.getLangOpts().C23) {
22380 const Expr *ME = E->IgnoreParenImpCasts();
22381 while (const auto *M = dyn_cast<MemberExpr>(Val: ME)) {
22382 if (M->isArrow())
22383 return ICEDiag(IK_NotICE, E->getBeginLoc());
22384 ME = M->getBase()->IgnoreParenImpCasts();
22385 }
22386 const auto *DRE = dyn_cast<DeclRefExpr>(Val: ME);
22387 if (DRE) {
22388 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl());
22389 VD && VD->isConstexpr())
22390 return CheckEvalInICE(E, Ctx);
22391 }
22392 }
22393 return ICEDiag(IK_NotICE, E->getBeginLoc());
22394 }
22395
22396 case Expr::InitListExprClass: {
22397 // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the
22398 // form "T x = { a };" is equivalent to "T x = a;".
22399 // Unless we're initializing a reference, T is a scalar as it is known to be
22400 // of integral or enumeration type.
22401 if (E->isPRValue())
22402 if (cast<InitListExpr>(Val: E)->getNumInits() == 1)
22403 return CheckICE(E: cast<InitListExpr>(Val: E)->getInit(Init: 0), Ctx);
22404 return ICEDiag(IK_NotICE, E->getBeginLoc());
22405 }
22406
22407 case Expr::SizeOfPackExprClass:
22408 case Expr::GNUNullExprClass:
22409 case Expr::SourceLocExprClass:
22410 case Expr::EmbedExprClass:
22411 case Expr::OpenACCAsteriskSizeExprClass:
22412 return NoDiag();
22413
22414 case Expr::PackIndexingExprClass:
22415 return CheckICE(E: cast<PackIndexingExpr>(Val: E)->getSelectedExpr(), Ctx);
22416
22417 case Expr::SubstNonTypeTemplateParmExprClass:
22418 return
22419 CheckICE(E: cast<SubstNonTypeTemplateParmExpr>(Val: E)->getReplacement(), Ctx);
22420
22421 case Expr::ConstantExprClass:
22422 return CheckICE(E: cast<ConstantExpr>(Val: E)->getSubExpr(), Ctx);
22423
22424 case Expr::ParenExprClass:
22425 return CheckICE(E: cast<ParenExpr>(Val: E)->getSubExpr(), Ctx);
22426 case Expr::GenericSelectionExprClass:
22427 return CheckICE(E: cast<GenericSelectionExpr>(Val: E)->getResultExpr(), Ctx);
22428 case Expr::IntegerLiteralClass:
22429 case Expr::FixedPointLiteralClass:
22430 case Expr::CharacterLiteralClass:
22431 case Expr::ObjCBoolLiteralExprClass:
22432 case Expr::CXXBoolLiteralExprClass:
22433 case Expr::CXXScalarValueInitExprClass:
22434 case Expr::TypeTraitExprClass:
22435 case Expr::ConceptSpecializationExprClass:
22436 case Expr::RequiresExprClass:
22437 case Expr::ArrayTypeTraitExprClass:
22438 case Expr::ExpressionTraitExprClass:
22439 case Expr::CXXNoexceptExprClass:
22440 case Expr::CXXReflectExprClass:
22441 return NoDiag();
22442 case Expr::CallExprClass:
22443 case Expr::CXXOperatorCallExprClass: {
22444 // C99 6.6/3 allows function calls within unevaluated subexpressions of
22445 // constant expressions, but they can never be ICEs because an ICE cannot
22446 // contain an operand of (pointer to) function type.
22447 const CallExpr *CE = cast<CallExpr>(Val: E);
22448 if (CE->getBuiltinCallee())
22449 return CheckEvalInICE(E, Ctx);
22450 return ICEDiag(IK_NotICE, E->getBeginLoc());
22451 }
22452 case Expr::CXXRewrittenBinaryOperatorClass:
22453 return CheckICE(E: cast<CXXRewrittenBinaryOperator>(Val: E)->getSemanticForm(),
22454 Ctx);
22455 case Expr::DeclRefExprClass: {
22456 const NamedDecl *D = cast<DeclRefExpr>(Val: E)->getDecl();
22457 if (isa<EnumConstantDecl>(Val: D))
22458 return NoDiag();
22459
22460 // C++ and OpenCL (FIXME: spec reference?) allow reading const-qualified
22461 // integer variables in constant expressions:
22462 //
22463 // C++ 7.1.5.1p2
22464 // A variable of non-volatile const-qualified integral or enumeration
22465 // type initialized by an ICE can be used in ICEs.
22466 //
22467 // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In
22468 // that mode, use of reference variables should not be allowed.
22469 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
22470 if (VD && VD->isUsableInConstantExpressions(C: Ctx) &&
22471 !VD->getType()->isReferenceType())
22472 return NoDiag();
22473
22474 return ICEDiag(IK_NotICE, E->getBeginLoc());
22475 }
22476 case Expr::UnaryOperatorClass: {
22477 const UnaryOperator *Exp = cast<UnaryOperator>(Val: E);
22478 switch (Exp->getOpcode()) {
22479 case UO_PostInc:
22480 case UO_PostDec:
22481 case UO_PreInc:
22482 case UO_PreDec:
22483 case UO_AddrOf:
22484 case UO_Deref:
22485 case UO_Coawait:
22486 // C99 6.6/3 allows increment and decrement within unevaluated
22487 // subexpressions of constant expressions, but they can never be ICEs
22488 // because an ICE cannot contain an lvalue operand.
22489 return ICEDiag(IK_NotICE, E->getBeginLoc());
22490 case UO_Extension:
22491 case UO_LNot:
22492 case UO_Plus:
22493 case UO_Minus:
22494 case UO_Not:
22495 case UO_Real:
22496 case UO_Imag:
22497 return CheckICE(E: Exp->getSubExpr(), Ctx);
22498 }
22499 llvm_unreachable("invalid unary operator class");
22500 }
22501 case Expr::OffsetOfExprClass: {
22502 // Note that per C99, offsetof must be an ICE. And AFAIK, using
22503 // EvaluateAsRValue matches the proposed gcc behavior for cases like
22504 // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect
22505 // compliance: we should warn earlier for offsetof expressions with
22506 // array subscripts that aren't ICEs, and if the array subscripts
22507 // are ICEs, the value of the offsetof must be an integer constant.
22508 return CheckEvalInICE(E, Ctx);
22509 }
22510 case Expr::UnaryExprOrTypeTraitExprClass: {
22511 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(Val: E);
22512 if ((Exp->getKind() == UETT_SizeOf) &&
22513 Exp->getTypeOfArgument()->isVariableArrayType())
22514 return ICEDiag(IK_NotICE, E->getBeginLoc());
22515 if (Exp->getKind() == UETT_CountOf) {
22516 QualType ArgTy = Exp->getTypeOfArgument();
22517 if (ArgTy->isVariableArrayType()) {
22518 // We need to look whether the array is multidimensional. If it is,
22519 // then we want to check the size expression manually to see whether
22520 // it is an ICE or not.
22521 const auto *VAT = Ctx.getAsVariableArrayType(T: ArgTy);
22522 if (VAT->getElementType()->isArrayType())
22523 // Variable array size expression could be missing (e.g. int a[*][10])
22524 // In that case, it can't be a constant expression.
22525 return VAT->getSizeExpr() ? CheckICE(E: VAT->getSizeExpr(), Ctx)
22526 : ICEDiag(IK_NotICE, E->getBeginLoc());
22527
22528 // Otherwise, this is a regular VLA, which is definitely not an ICE.
22529 return ICEDiag(IK_NotICE, E->getBeginLoc());
22530 }
22531 }
22532 return NoDiag();
22533 }
22534 case Expr::BinaryOperatorClass: {
22535 const BinaryOperator *Exp = cast<BinaryOperator>(Val: E);
22536 switch (Exp->getOpcode()) {
22537 case BO_PtrMemD:
22538 case BO_PtrMemI:
22539 case BO_Assign:
22540 case BO_MulAssign:
22541 case BO_DivAssign:
22542 case BO_RemAssign:
22543 case BO_AddAssign:
22544 case BO_SubAssign:
22545 case BO_ShlAssign:
22546 case BO_ShrAssign:
22547 case BO_AndAssign:
22548 case BO_XorAssign:
22549 case BO_OrAssign:
22550 // C99 6.6/3 allows assignments within unevaluated subexpressions of
22551 // constant expressions, but they can never be ICEs because an ICE cannot
22552 // contain an lvalue operand.
22553 return ICEDiag(IK_NotICE, E->getBeginLoc());
22554
22555 case BO_Mul:
22556 case BO_Div:
22557 case BO_Rem:
22558 case BO_Add:
22559 case BO_Sub:
22560 case BO_Shl:
22561 case BO_Shr:
22562 case BO_LT:
22563 case BO_GT:
22564 case BO_LE:
22565 case BO_GE:
22566 case BO_EQ:
22567 case BO_NE:
22568 case BO_And:
22569 case BO_Xor:
22570 case BO_Or:
22571 case BO_Comma:
22572 case BO_Cmp: {
22573 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
22574 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
22575 if (Exp->getOpcode() == BO_Div ||
22576 Exp->getOpcode() == BO_Rem) {
22577 // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
22578 // we don't evaluate one.
22579 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) {
22580 llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
22581 if (REval == 0)
22582 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22583 if (REval.isSigned() && REval.isAllOnes()) {
22584 llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
22585 if (LEval.isMinSignedValue())
22586 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22587 }
22588 }
22589 }
22590 if (Exp->getOpcode() == BO_Comma) {
22591 if (Ctx.getLangOpts().C99) {
22592 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
22593 // if it isn't evaluated.
22594 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE)
22595 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22596 } else {
22597 // In both C89 and C++, commas in ICEs are illegal.
22598 return ICEDiag(IK_NotICE, E->getBeginLoc());
22599 }
22600 }
22601 return Worst(A: LHSResult, B: RHSResult);
22602 }
22603 case BO_LAnd:
22604 case BO_LOr: {
22605 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
22606 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
22607 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) {
22608 // Rare case where the RHS has a comma "side-effect"; we need
22609 // to actually check the condition to see whether the side
22610 // with the comma is evaluated.
22611 if ((Exp->getOpcode() == BO_LAnd) !=
22612 (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
22613 return RHSResult;
22614 return NoDiag();
22615 }
22616
22617 return Worst(A: LHSResult, B: RHSResult);
22618 }
22619 }
22620 llvm_unreachable("invalid binary operator kind");
22621 }
22622 case Expr::ImplicitCastExprClass:
22623 case Expr::CStyleCastExprClass:
22624 case Expr::CXXFunctionalCastExprClass:
22625 case Expr::CXXStaticCastExprClass:
22626 case Expr::CXXReinterpretCastExprClass:
22627 case Expr::CXXConstCastExprClass:
22628 case Expr::ObjCBridgedCastExprClass: {
22629 const Expr *SubExpr = cast<CastExpr>(Val: E)->getSubExpr();
22630 if (isa<ExplicitCastExpr>(Val: E)) {
22631 if (const FloatingLiteral *FL
22632 = dyn_cast<FloatingLiteral>(Val: SubExpr->IgnoreParenImpCasts())) {
22633 unsigned DestWidth = Ctx.getIntWidth(T: E->getType());
22634 bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
22635 APSInt IgnoredVal(DestWidth, !DestSigned);
22636 bool Ignored;
22637 // If the value does not fit in the destination type, the behavior is
22638 // undefined, so we are not required to treat it as a constant
22639 // expression.
22640 if (FL->getValue().convertToInteger(Result&: IgnoredVal,
22641 RM: llvm::APFloat::rmTowardZero,
22642 IsExact: &Ignored) & APFloat::opInvalidOp)
22643 return ICEDiag(IK_NotICE, E->getBeginLoc());
22644 return NoDiag();
22645 }
22646 }
22647 switch (cast<CastExpr>(Val: E)->getCastKind()) {
22648 case CK_LValueToRValue:
22649 case CK_AtomicToNonAtomic:
22650 case CK_NonAtomicToAtomic:
22651 case CK_NoOp:
22652 case CK_IntegralToBoolean:
22653 case CK_IntegralCast:
22654 return CheckICE(E: SubExpr, Ctx);
22655 default:
22656 return ICEDiag(IK_NotICE, E->getBeginLoc());
22657 }
22658 }
22659 case Expr::BinaryConditionalOperatorClass: {
22660 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(Val: E);
22661 ICEDiag CommonResult = CheckICE(E: Exp->getCommon(), Ctx);
22662 if (CommonResult.Kind == IK_NotICE) return CommonResult;
22663 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
22664 if (FalseResult.Kind == IK_NotICE) return FalseResult;
22665 if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult;
22666 if (FalseResult.Kind == IK_ICEIfUnevaluated &&
22667 Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag();
22668 return FalseResult;
22669 }
22670 case Expr::ConditionalOperatorClass: {
22671 const ConditionalOperator *Exp = cast<ConditionalOperator>(Val: E);
22672 // If the condition (ignoring parens) is a __builtin_constant_p call,
22673 // then only the true side is actually considered in an integer constant
22674 // expression, and it is fully evaluated. This is an important GNU
22675 // extension. See GCC PR38377 for discussion.
22676 if (const CallExpr *CallCE
22677 = dyn_cast<CallExpr>(Val: Exp->getCond()->IgnoreParenCasts()))
22678 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
22679 return CheckEvalInICE(E, Ctx);
22680 ICEDiag CondResult = CheckICE(E: Exp->getCond(), Ctx);
22681 if (CondResult.Kind == IK_NotICE)
22682 return CondResult;
22683
22684 ICEDiag TrueResult = CheckICE(E: Exp->getTrueExpr(), Ctx);
22685 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
22686
22687 if (TrueResult.Kind == IK_NotICE)
22688 return TrueResult;
22689 if (FalseResult.Kind == IK_NotICE)
22690 return FalseResult;
22691 if (CondResult.Kind == IK_ICEIfUnevaluated)
22692 return CondResult;
22693 if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE)
22694 return NoDiag();
22695 // Rare case where the diagnostics depend on which side is evaluated
22696 // Note that if we get here, CondResult is 0, and at least one of
22697 // TrueResult and FalseResult is non-zero.
22698 if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0)
22699 return FalseResult;
22700 return TrueResult;
22701 }
22702 case Expr::CXXDefaultArgExprClass:
22703 return CheckICE(E: cast<CXXDefaultArgExpr>(Val: E)->getExpr(), Ctx);
22704 case Expr::CXXDefaultInitExprClass:
22705 return CheckICE(E: cast<CXXDefaultInitExpr>(Val: E)->getExpr(), Ctx);
22706 case Expr::ChooseExprClass: {
22707 return CheckICE(E: cast<ChooseExpr>(Val: E)->getChosenSubExpr(), Ctx);
22708 }
22709 case Expr::BuiltinBitCastExprClass: {
22710 if (!checkBitCastConstexprEligibility(Info: nullptr, Ctx, BCE: cast<CastExpr>(Val: E)))
22711 return ICEDiag(IK_NotICE, E->getBeginLoc());
22712 return CheckICE(E: cast<CastExpr>(Val: E)->getSubExpr(), Ctx);
22713 }
22714 }
22715
22716 llvm_unreachable("Invalid StmtClass!");
22717}
22718
22719/// Evaluate an expression as a C++11 integral constant expression.
22720static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx,
22721 const Expr *E,
22722 llvm::APSInt *Value) {
22723 if (!E->getType()->isIntegralOrUnscopedEnumerationType())
22724 return false;
22725
22726 APValue Result;
22727 if (!E->isCXX11ConstantExpr(Ctx, Result: &Result))
22728 return false;
22729
22730 if (!Result.isInt())
22731 return false;
22732
22733 if (Value) *Value = Result.getInt();
22734 return true;
22735}
22736
22737bool Expr::isIntegerConstantExpr(const ASTContext &Ctx) const {
22738 assert(!isValueDependent() &&
22739 "Expression evaluator can't be called on a dependent expression.");
22740
22741 ExprTimeTraceScope TimeScope(this, Ctx, "isIntegerConstantExpr");
22742
22743 if (Ctx.getLangOpts().CPlusPlus11)
22744 return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: nullptr);
22745
22746 ICEDiag D = CheckICE(E: this, Ctx);
22747 if (D.Kind != IK_ICE)
22748 return false;
22749 return true;
22750}
22751
22752std::optional<llvm::APSInt>
22753Expr::getIntegerConstantExpr(const ASTContext &Ctx) const {
22754 if (isValueDependent()) {
22755 // Expression evaluator can't succeed on a dependent expression.
22756 return std::nullopt;
22757 }
22758
22759 if (Ctx.getLangOpts().CPlusPlus11) {
22760 APSInt Value;
22761 if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: &Value))
22762 return Value;
22763 return std::nullopt;
22764 }
22765
22766 if (!isIntegerConstantExpr(Ctx))
22767 return std::nullopt;
22768
22769 // The only possible side-effects here are due to UB discovered in the
22770 // evaluation (for instance, INT_MAX + 1). In such a case, we are still
22771 // required to treat the expression as an ICE, so we produce the folded
22772 // value.
22773 EvalResult ExprResult;
22774 Expr::EvalStatus Status;
22775 EvalInfo Info(Ctx, Status, EvaluationMode::IgnoreSideEffects);
22776 Info.InConstantContext = true;
22777
22778 if (!::EvaluateAsInt(E: this, ExprResult, Ctx, AllowSideEffects: SE_AllowSideEffects, Info))
22779 llvm_unreachable("ICE cannot be evaluated!");
22780
22781 return ExprResult.Val.getInt();
22782}
22783
22784bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const {
22785 assert(!isValueDependent() &&
22786 "Expression evaluator can't be called on a dependent expression.");
22787
22788 return CheckICE(E: this, Ctx).Kind == IK_ICE;
22789}
22790
22791bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result) const {
22792 assert(!isValueDependent() &&
22793 "Expression evaluator can't be called on a dependent expression.");
22794
22795 // We support this checking in C++98 mode in order to diagnose compatibility
22796 // issues.
22797 assert(Ctx.getLangOpts().CPlusPlus);
22798
22799 bool IsConst;
22800 APValue Scratch;
22801 if (FastEvaluateAsRValue(Exp: this, Result&: Scratch, Ctx, IsConst) && Scratch.hasValue()) {
22802 if (Result)
22803 *Result = std::move(Scratch);
22804 return true;
22805 }
22806
22807 // Build evaluation settings.
22808 Expr::EvalStatus Status;
22809 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
22810
22811 bool IsConstExpr =
22812 ::EvaluateAsRValue(Info, E: this, Result&: Result ? *Result : Scratch) &&
22813 // NOTE: We don't produce a diagnostic for this, but the callers that
22814 // call us on arbitrary full-expressions should generally not care.
22815 Info.discardCleanups() && !Status.HasSideEffects;
22816
22817 return IsConstExpr && !Status.DiagEmitted;
22818}
22819
22820bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx,
22821 const FunctionDecl *Callee,
22822 ArrayRef<const Expr*> Args,
22823 const Expr *This) const {
22824 assert(!isValueDependent() &&
22825 "Expression evaluator can't be called on a dependent expression.");
22826
22827 llvm::TimeTraceScope TimeScope("EvaluateWithSubstitution", [&] {
22828 std::string Name;
22829 llvm::raw_string_ostream OS(Name);
22830 Callee->getNameForDiagnostic(OS, Policy: Ctx.getPrintingPolicy(),
22831 /*Qualified=*/true);
22832 return Name;
22833 });
22834
22835 Expr::EvalStatus Status;
22836 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpressionUnevaluated);
22837 Info.InConstantContext = true;
22838
22839 if (Info.EnableNewConstInterp) {
22840 if (std::optional<bool> BoolResult =
22841 Info.Ctx.getInterpContext().evaluateWithSubstitution(
22842 Parent&: Info, Callee, Args, This, Condition: this)) {
22843 Value = APValue(APSInt(APInt(1, static_cast<uint64_t>(*BoolResult))));
22844 return true;
22845 }
22846 return false;
22847 }
22848
22849 LValue ThisVal;
22850 const LValue *ThisPtr = nullptr;
22851 if (This) {
22852#ifndef NDEBUG
22853 auto *MD = dyn_cast<CXXMethodDecl>(Callee);
22854 assert(MD && "Don't provide `this` for non-methods.");
22855 assert(MD->isImplicitObjectMemberFunction() &&
22856 "Don't provide `this` for methods without an implicit object.");
22857#endif
22858 if (!This->isValueDependent() &&
22859 EvaluateObjectArgument(Info, Object: This, This&: ThisVal) &&
22860 !Info.EvalStatus.HasSideEffects)
22861 ThisPtr = &ThisVal;
22862
22863 // Ignore any side-effects from a failed evaluation. This is safe because
22864 // they can't interfere with any other argument evaluation.
22865 Info.EvalStatus.HasSideEffects = false;
22866 }
22867
22868 CallRef Call = Info.CurrentCall->createCall(Callee);
22869 for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
22870 I != E; ++I) {
22871 unsigned Idx = I - Args.begin();
22872 if (Idx >= Callee->getNumParams())
22873 break;
22874 const ParmVarDecl *PVD = Callee->getParamDecl(i: Idx);
22875 if ((*I)->isValueDependent() ||
22876 !EvaluateCallArg(PVD, Arg: *I, Call, Info) ||
22877 Info.EvalStatus.HasSideEffects) {
22878 // If evaluation fails, throw away the argument entirely.
22879 if (APValue *Slot = Info.getParamSlot(Call, PVD))
22880 *Slot = APValue();
22881 }
22882
22883 // Ignore any side-effects from a failed evaluation. This is safe because
22884 // they can't interfere with any other argument evaluation.
22885 Info.EvalStatus.HasSideEffects = false;
22886 }
22887
22888 // Parameter cleanups happen in the caller and are not part of this
22889 // evaluation.
22890 Info.discardCleanups();
22891 Info.EvalStatus.HasSideEffects = false;
22892
22893 // Build fake call to Callee.
22894 CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, This,
22895 Call);
22896 // FIXME: Missing ExprWithCleanups in enable_if conditions?
22897 FullExpressionRAII Scope(Info);
22898 return Evaluate(Result&: Value, Info, E: this) && Scope.destroy() &&
22899 !Info.EvalStatus.HasSideEffects;
22900}
22901
22902bool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
22903 SmallVectorImpl<
22904 PartialDiagnosticAt> &Diags) {
22905 // FIXME: It would be useful to check constexpr function templates, but at the
22906 // moment the constant expression evaluator cannot cope with the non-rigorous
22907 // ASTs which we build for dependent expressions.
22908 if (FD->isDependentContext())
22909 return true;
22910
22911 llvm::TimeTraceScope TimeScope("isPotentialConstantExpr", [&] {
22912 std::string Name;
22913 llvm::raw_string_ostream OS(Name);
22914 FD->getNameForDiagnostic(OS, Policy: FD->getASTContext().getPrintingPolicy(),
22915 /*Qualified=*/true);
22916 return Name;
22917 });
22918
22919 Expr::EvalStatus Status;
22920 Status.Diag = &Diags;
22921
22922 EvalInfo Info(FD->getASTContext(), Status,
22923 EvaluationMode::ConstantExpression);
22924 Info.InConstantContext = true;
22925 Info.CheckingPotentialConstantExpression = true;
22926
22927 // The constexpr VM attempts to compile all methods to bytecode here.
22928 if (Info.EnableNewConstInterp) {
22929 Info.Ctx.getInterpContext().isPotentialConstantExpr(Parent&: Info, FD);
22930 return Diags.empty();
22931 }
22932
22933 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
22934 const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr;
22935
22936 // Fabricate an arbitrary expression on the stack and pretend that it
22937 // is a temporary being used as the 'this' pointer.
22938 LValue This;
22939 ImplicitValueInitExpr VIE(RD ? Info.Ctx.getCanonicalTagType(TD: RD)
22940 : Info.Ctx.IntTy);
22941 This.set(B: {&VIE, Info.CurrentCall->Index});
22942
22943 ArrayRef<const Expr*> Args;
22944
22945 APValue Scratch;
22946 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Val: FD)) {
22947 // Evaluate the call as a constant initializer, to allow the construction
22948 // of objects of non-literal types.
22949 Info.setEvaluatingDecl(Base: This.getLValueBase(), Value&: Scratch);
22950 HandleConstructorCall(E: &VIE, This, Args, Definition: CD, Info, Result&: Scratch);
22951 } else {
22952 SourceLocation Loc = FD->getLocation();
22953 HandleFunctionCall(
22954 CallLoc: Loc, Callee: FD, ObjectArg: (MD && MD->isImplicitObjectMemberFunction()) ? &This : nullptr,
22955 E: &VIE, Args, Call: CallRef(), Body: FD->getBody(), Info, Result&: Scratch,
22956 /*ResultSlot=*/nullptr);
22957 }
22958
22959 return Diags.empty();
22960}
22961
22962bool Expr::isPotentialConstantExprUnevaluated(Expr *E,
22963 const FunctionDecl *FD,
22964 SmallVectorImpl<
22965 PartialDiagnosticAt> &Diags) {
22966 assert(!E->isValueDependent() &&
22967 "Expression evaluator can't be called on a dependent expression.");
22968
22969 Expr::EvalStatus Status;
22970 Status.Diag = &Diags;
22971
22972 EvalInfo Info(FD->getASTContext(), Status,
22973 EvaluationMode::ConstantExpressionUnevaluated);
22974 Info.InConstantContext = true;
22975 Info.CheckingPotentialConstantExpression = true;
22976
22977 if (Info.EnableNewConstInterp) {
22978 Info.Ctx.getInterpContext().isPotentialConstantExprUnevaluated(Parent&: Info, E, FD);
22979 return Diags.empty();
22980 }
22981
22982 // Fabricate a call stack frame to give the arguments a plausible cover story.
22983 CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr,
22984 /*CallExpr=*/nullptr, CallRef());
22985
22986 APValue ResultScratch;
22987 Evaluate(Result&: ResultScratch, Info, E);
22988 return Diags.empty();
22989}
22990
22991std::optional<uint64_t> Expr::tryEvaluateObjectSize(const ASTContext &Ctx,
22992 unsigned Type) const {
22993 if (!getType()->isPointerType())
22994 return std::nullopt;
22995
22996 Expr::EvalStatus Status;
22997 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
22998 if (Info.EnableNewConstInterp)
22999 return Info.Ctx.getInterpContext().tryEvaluateObjectSize(Parent&: Info, E: this, Kind: Type);
23000 return tryEvaluateBuiltinObjectSize(E: this, Type, Info);
23001}
23002
23003static std::optional<uint64_t>
23004EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
23005 std::string *StringResult) {
23006 if (!E->getType()->hasPointerRepresentation() || !E->isPRValue())
23007 return std::nullopt;
23008
23009 LValue String;
23010
23011 if (!EvaluatePointer(E, Result&: String, Info))
23012 return std::nullopt;
23013
23014 QualType CharTy = E->getType()->getPointeeType();
23015
23016 // Fast path: if it's a string literal, search the string value.
23017 if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>(
23018 Val: String.getLValueBase().dyn_cast<const Expr *>())) {
23019 StringRef Str = S->getBytes();
23020 int64_t Off = String.Offset.getQuantity();
23021 if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() &&
23022 S->getCharByteWidth() == 1 &&
23023 // FIXME: Add fast-path for wchar_t too.
23024 Info.Ctx.hasSameUnqualifiedType(T1: CharTy, T2: Info.Ctx.CharTy)) {
23025 Str = Str.substr(Start: Off);
23026
23027 StringRef::size_type Pos = Str.find(C: 0);
23028 if (Pos != StringRef::npos)
23029 Str = Str.substr(Start: 0, N: Pos);
23030
23031 if (StringResult)
23032 *StringResult = Str;
23033 return Str.size();
23034 }
23035
23036 // Fall through to slow path.
23037 }
23038
23039 // Slow path: scan the bytes of the string looking for the terminating 0.
23040 for (uint64_t Strlen = 0; /**/; ++Strlen) {
23041 APValue Char;
23042 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: String, RVal&: Char) ||
23043 !Char.isInt())
23044 return std::nullopt;
23045 if (!Char.getInt())
23046 return Strlen;
23047 else if (StringResult)
23048 StringResult->push_back(c: Char.getInt().getExtValue());
23049 if (!HandleLValueArrayAdjustment(Info, E, LVal&: String, EltTy: CharTy, Adjustment: 1))
23050 return std::nullopt;
23051 }
23052}
23053
23054std::optional<std::string> Expr::tryEvaluateString(ASTContext &Ctx) const {
23055 Expr::EvalStatus Status;
23056 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23057 std::string StringResult;
23058
23059 if (Info.EnableNewConstInterp) {
23060 if (!Info.Ctx.getInterpContext().evaluateString(Parent&: Info, E: this, Result&: StringResult))
23061 return std::nullopt;
23062 return StringResult;
23063 }
23064
23065 if (EvaluateBuiltinStrLen(E: this, Info, StringResult: &StringResult))
23066 return StringResult;
23067 return std::nullopt;
23068}
23069
23070template <typename T>
23071static bool EvaluateCharRangeAsStringImpl(const Expr *, T &Result,
23072 const Expr *SizeExpression,
23073 const Expr *PtrExpression,
23074 ASTContext &Ctx,
23075 Expr::EvalResult &Status) {
23076 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
23077 Info.InConstantContext = true;
23078
23079 if (Info.EnableNewConstInterp)
23080 return Info.Ctx.getInterpContext().evaluateCharRange(Info, SizeExpression,
23081 PtrExpression, Result);
23082
23083 LValue String;
23084 FullExpressionRAII Scope(Info);
23085 APSInt SizeValue;
23086 if (!::EvaluateInteger(E: SizeExpression, Result&: SizeValue, Info))
23087 return false;
23088
23089 uint64_t Size = SizeValue.getZExtValue();
23090
23091 // FIXME: better protect against invalid or excessive sizes
23092 if constexpr (std::is_same_v<APValue, T>)
23093 Result = APValue(APValue::UninitArray{}, Size, Size);
23094 else {
23095 if (Size < Result.max_size())
23096 Result.reserve(Size);
23097 }
23098 if (!::EvaluatePointer(E: PtrExpression, Result&: String, Info))
23099 return false;
23100
23101 QualType CharTy = PtrExpression->getType()->getPointeeType();
23102 for (uint64_t I = 0; I < Size; ++I) {
23103 APValue Char;
23104 if (!handleLValueToRValueConversion(Info, Conv: PtrExpression, Type: CharTy, LVal: String,
23105 RVal&: Char))
23106 return false;
23107
23108 if constexpr (std::is_same_v<APValue, T>) {
23109 Result.getArrayInitializedElt(I) = std::move(Char);
23110 } else {
23111 APSInt C = Char.getInt();
23112
23113 assert(C.getBitWidth() <= 8 &&
23114 "string element not representable in char");
23115
23116 Result.push_back(static_cast<char>(C.getExtValue()));
23117 }
23118
23119 if (!HandleLValueArrayAdjustment(Info, E: PtrExpression, LVal&: String, EltTy: CharTy, Adjustment: 1))
23120 return false;
23121 }
23122
23123 return Scope.destroy() && CheckMemoryLeaks(Info);
23124}
23125
23126bool Expr::EvaluateCharRangeAsString(std::string &Result,
23127 const Expr *SizeExpression,
23128 const Expr *PtrExpression, ASTContext &Ctx,
23129 EvalResult &Status) const {
23130 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
23131 PtrExpression, Ctx, Status);
23132}
23133
23134bool Expr::EvaluateCharRangeAsString(APValue &Result,
23135 const Expr *SizeExpression,
23136 const Expr *PtrExpression, ASTContext &Ctx,
23137 EvalResult &Status) const {
23138 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
23139 PtrExpression, Ctx, Status);
23140}
23141
23142std::optional<uint64_t> Expr::tryEvaluateStrLen(const ASTContext &Ctx) const {
23143 Expr::EvalStatus Status;
23144 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23145
23146 if (Info.EnableNewConstInterp)
23147 return Info.Ctx.getInterpContext().evaluateStrlen(Parent&: Info, E: this);
23148 return EvaluateBuiltinStrLen(E: this, Info);
23149}
23150
23151namespace {
23152struct IsWithinLifetimeHandler {
23153 EvalInfo &Info;
23154 static constexpr AccessKinds AccessKind = AccessKinds::AK_IsWithinLifetime;
23155 using result_type = std::optional<bool>;
23156 std::optional<bool> failed() { return std::nullopt; }
23157 template <typename T>
23158 std::optional<bool> found(T &Subobj, QualType SubobjType,
23159 APValue::LValueBase) {
23160 return true;
23161 }
23162 template <typename T>
23163 std::optional<bool> found(T &Subobj, QualType SubobjType) {
23164 return true;
23165 }
23166};
23167
23168std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &IEE,
23169 const CallExpr *E) {
23170 EvalInfo &Info = IEE.Info;
23171 // Sometimes this is called during some sorts of constant folding / early
23172 // evaluation. These are meant for non-constant expressions and are not
23173 // necessary since this consteval builtin will never be evaluated at runtime.
23174 // Just fail to evaluate when not in a constant context.
23175 if (!Info.InConstantContext)
23176 return std::nullopt;
23177 assert(E->getBuiltinCallee() == Builtin::BI__builtin_is_within_lifetime);
23178 const Expr *Arg = E->getArg(Arg: 0);
23179 if (Arg->isValueDependent())
23180 return std::nullopt;
23181 LValue Val;
23182 if (!EvaluatePointer(E: Arg, Result&: Val, Info))
23183 return std::nullopt;
23184
23185 if (Val.allowConstexprUnknown())
23186 return true;
23187
23188 auto Error = [&](int Diag) {
23189 bool CalledFromStd = false;
23190 const auto *Callee = Info.CurrentCall->getCallee();
23191 if (Callee && Callee->isInStdNamespace()) {
23192 const IdentifierInfo *Identifier = Callee->getIdentifier();
23193 CalledFromStd = Identifier && Identifier->isStr(Str: "is_within_lifetime");
23194 }
23195 Info.CCEDiag(Loc: CalledFromStd ? Info.CurrentCall->getCallRange().getBegin()
23196 : E->getExprLoc(),
23197 DiagId: diag::err_invalid_is_within_lifetime)
23198 << (CalledFromStd ? "std::is_within_lifetime"
23199 : "__builtin_is_within_lifetime")
23200 << Diag;
23201 return std::nullopt;
23202 };
23203 // C++2c [meta.const.eval]p4:
23204 // During the evaluation of an expression E as a core constant expression, a
23205 // call to this function is ill-formed unless p points to an object that is
23206 // usable in constant expressions or whose complete object's lifetime began
23207 // within E.
23208
23209 // Make sure it points to an object
23210 // nullptr does not point to an object
23211 if (Val.isNullPointer() || Val.getLValueBase().isNull())
23212 return Error(0);
23213 QualType T = Val.getLValueBase().getType();
23214 assert(!T->isFunctionType() &&
23215 "Pointers to functions should have been typed as function pointers "
23216 "which would have been rejected earlier");
23217 assert(T->isObjectType());
23218 // Hypothetical array element is not an object
23219 if (Val.getLValueDesignator().isOnePastTheEnd())
23220 return Error(1);
23221 assert(Val.getLValueDesignator().isValidSubobject() &&
23222 "Unchecked case for valid subobject");
23223 // All other ill-formed values should have failed EvaluatePointer, so the
23224 // object should be a pointer to an object that is usable in a constant
23225 // expression or whose complete lifetime began within the expression
23226 CompleteObject CO =
23227 findCompleteObject(Info, E, AK: AccessKinds::AK_IsWithinLifetime, LVal: Val, LValType: T);
23228 // The lifetime hasn't begun yet if we are still evaluating the
23229 // initializer ([basic.life]p(1.2))
23230 if (Info.EvaluatingDeclValue && CO.Value == Info.EvaluatingDeclValue)
23231 return Error(2);
23232
23233 if (!CO)
23234 return false;
23235 IsWithinLifetimeHandler handler{.Info: Info};
23236 return findSubobject(Info, E, Obj: CO, Sub: Val.getLValueDesignator(), handler);
23237}
23238} // namespace
23239