1//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Expr constant evaluator.
10//
11// Constant expression evaluation produces four main results:
12//
13// * A success/failure flag indicating whether constant folding was successful.
14// This is the 'bool' return value used by most of the code in this file. A
15// 'false' return value indicates that constant folding has failed, and any
16// appropriate diagnostic has already been produced.
17//
18// * An evaluated result, valid only if constant folding has not failed.
19//
20// * A flag indicating if evaluation encountered (unevaluated) side-effects.
21// These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1),
22// where it is possible to determine the evaluated result regardless.
23//
24// * A set of notes indicating why the evaluation was not a constant expression
25// (under the C++11 / C++1y rules only, at the moment), or, if folding failed
26// too, why the expression could not be folded.
27//
28// If we are checking for a potential constant expression, failure to constant
29// fold a potential constant sub-expression will be indicated by a 'false'
30// return value (the expression could not be folded) and no diagnostic (the
31// expression is not necessarily non-constant).
32//
33//===----------------------------------------------------------------------===//
34
35#include "ByteCode/Context.h"
36#include "ByteCode/EvalSettings.h"
37#include "ByteCode/Frame.h"
38#include "ByteCode/State.h"
39#include "ExprConstShared.h"
40#include "clang/AST/APValue.h"
41#include "clang/AST/ASTContext.h"
42#include "clang/AST/ASTLambda.h"
43#include "clang/AST/Attr.h"
44#include "clang/AST/CXXInheritance.h"
45#include "clang/AST/CharUnits.h"
46#include "clang/AST/ComparisonCategories.h"
47#include "clang/AST/CurrentSourceLocExprScope.h"
48#include "clang/AST/Expr.h"
49#include "clang/AST/InferAlloc.h"
50#include "clang/AST/OSLog.h"
51#include "clang/AST/OptionalDiagnostic.h"
52#include "clang/AST/RecordLayout.h"
53#include "clang/AST/Reflection.h"
54#include "clang/AST/SemaProxy.h"
55#include "clang/AST/StmtVisitor.h"
56#include "clang/AST/Type.h"
57#include "clang/AST/TypeLoc.h"
58#include "clang/Basic/Builtins.h"
59#include "clang/Basic/DiagnosticSema.h"
60#include "clang/Basic/TargetBuiltins.h"
61#include "clang/Basic/TargetInfo.h"
62#include "llvm/ADT/APFixedPoint.h"
63#include "llvm/ADT/Sequence.h"
64#include "llvm/ADT/SmallBitVector.h"
65#include "llvm/ADT/StringExtras.h"
66#include "llvm/ADT/bit.h"
67#include "llvm/Support/CRC.h"
68#include "llvm/Support/Casting.h"
69#include "llvm/Support/Debug.h"
70#include "llvm/Support/MathExtras.h"
71#include "llvm/Support/SaveAndRestore.h"
72#include "llvm/Support/SipHash.h"
73#include "llvm/Support/TimeProfiler.h"
74#include "llvm/Support/raw_ostream.h"
75#include <cstring>
76#include <functional>
77#include <limits>
78#include <optional>
79
80#define DEBUG_TYPE "exprconstant"
81
82using namespace clang;
83using llvm::APFixedPoint;
84using llvm::APInt;
85using llvm::APSInt;
86using llvm::APFloat;
87using llvm::FixedPointSemantics;
88
89namespace {
90 struct LValue;
91 class CallStackFrame;
92 class EvalInfo;
93
94 using SourceLocExprScopeGuard =
95 CurrentSourceLocExprScope::SourceLocExprScopeGuard;
96
97 static QualType getType(APValue::LValueBase B) {
98 return B.getType();
99 }
100
101 /// Get an LValue path entry, which is known to not be an array index, as a
102 /// field declaration.
103 static const FieldDecl *getAsField(APValue::LValuePathEntry E) {
104 return dyn_cast_or_null<FieldDecl>(Val: E.getAsBaseOrMember().getPointer());
105 }
106 /// Get an LValue path entry, which is known to not be an array index, as a
107 /// base class declaration.
108 static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) {
109 return dyn_cast_or_null<CXXRecordDecl>(Val: E.getAsBaseOrMember().getPointer());
110 }
111 /// Determine whether this LValue path entry for a base class names a virtual
112 /// base class.
113 static bool isVirtualBaseClass(APValue::LValuePathEntry E) {
114 return E.getAsBaseOrMember().getInt();
115 }
116
117 /// Given an expression, determine the type used to store the result of
118 /// evaluating that expression.
119 static QualType getStorageType(const ASTContext &Ctx, const Expr *E) {
120 if (E->isPRValue())
121 return E->getType();
122 return Ctx.getLValueReferenceType(T: E->getType());
123 }
124
125 static unsigned countNonVirtualBases(const CXXRecordDecl *RD) {
126 return llvm::count_if(Range: RD->bases(), P: [](auto &B) { return !B.isVirtual(); });
127 }
128
129 /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.
130 /// This will look through a single cast.
131 ///
132 /// Returns null if we couldn't unwrap a function with alloc_size.
133 static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) {
134 if (!E->getType()->isPointerType())
135 return nullptr;
136
137 E = E->IgnoreParens();
138 // If we're doing a variable assignment from e.g. malloc(N), there will
139 // probably be a cast of some kind. In exotic cases, we might also see a
140 // top-level ExprWithCleanups. Ignore them either way.
141 if (const auto *FE = dyn_cast<FullExpr>(Val: E))
142 E = FE->getSubExpr()->IgnoreParens();
143
144 if (const auto *Cast = dyn_cast<CastExpr>(Val: E))
145 E = Cast->getSubExpr()->IgnoreParens();
146
147 if (const auto *CE = dyn_cast<CallExpr>(Val: E))
148 return CE->getCalleeAllocSizeAttr() ? CE : nullptr;
149 return nullptr;
150 }
151
152 /// Determines whether or not the given Base contains a call to a function
153 /// with the alloc_size attribute.
154 static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) {
155 const auto *E = Base.dyn_cast<const Expr *>();
156 return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E);
157 }
158
159 /// The bound to claim that an array of unknown bound has.
160 /// The value in MostDerivedArraySize is undefined in this case. So, set it
161 /// to an arbitrary value that's likely to loudly break things if it's used.
162 static const uint64_t AssumedSizeForUnsizedArray =
163 std::numeric_limits<uint64_t>::max() / 2;
164
165 /// Determines if an LValue with the given LValueBase will have an unsized
166 /// array in its designator.
167 /// Find the path length and type of the most-derived subobject in the given
168 /// path, and find the size of the containing array, if any.
169 static unsigned
170 findMostDerivedSubobject(const ASTContext &Ctx, APValue::LValueBase Base,
171 ArrayRef<APValue::LValuePathEntry> Path,
172 uint64_t &ArraySize, QualType &Type, bool &IsArray,
173 bool &FirstEntryIsUnsizedArray) {
174 // This only accepts LValueBases from APValues, and APValues don't support
175 // arrays that lack size info.
176 assert(!isBaseAnAllocSizeCall(Base) &&
177 "Unsized arrays shouldn't appear here");
178 unsigned MostDerivedLength = 0;
179 // The type of Base is a reference type if the base is a constexpr-unknown
180 // variable. In that case, look through the reference type.
181 Type = getType(B: Base).getNonReferenceType();
182
183 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
184 if (Type->isArrayType()) {
185 const ArrayType *AT = Ctx.getAsArrayType(T: Type);
186 Type = AT->getElementType();
187 MostDerivedLength = I + 1;
188 IsArray = true;
189
190 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) {
191 ArraySize = CAT->getZExtSize();
192 } else {
193 assert(I == 0 && "unexpected unsized array designator");
194 FirstEntryIsUnsizedArray = true;
195 ArraySize = AssumedSizeForUnsizedArray;
196 }
197 } else if (Type->isAnyComplexType()) {
198 const ComplexType *CT = Type->castAs<ComplexType>();
199 Type = CT->getElementType();
200 ArraySize = 2;
201 MostDerivedLength = I + 1;
202 IsArray = true;
203 } else if (const auto *VT = Type->getAs<VectorType>()) {
204 Type = VT->getElementType();
205 ArraySize = VT->getNumElements();
206 MostDerivedLength = I + 1;
207 IsArray = true;
208 } else if (const FieldDecl *FD = getAsField(E: Path[I])) {
209 Type = FD->getType();
210 ArraySize = 0;
211 MostDerivedLength = I + 1;
212 IsArray = false;
213 } else {
214 // Path[I] describes a base class.
215 ArraySize = 0;
216 IsArray = false;
217 }
218 }
219 return MostDerivedLength;
220 }
221
222 /// A path from a glvalue to a subobject of that glvalue.
223 struct SubobjectDesignator {
224 /// True if the subobject was named in a manner not supported by C++11. Such
225 /// lvalues can still be folded, but they are not core constant expressions
226 /// and we cannot perform lvalue-to-rvalue conversions on them.
227 LLVM_PREFERRED_TYPE(bool)
228 unsigned Invalid : 1;
229
230 /// Is this a pointer one past the end of an object?
231 LLVM_PREFERRED_TYPE(bool)
232 unsigned IsOnePastTheEnd : 1;
233
234 /// Indicator of whether the first entry is an unsized array.
235 LLVM_PREFERRED_TYPE(bool)
236 unsigned FirstEntryIsAnUnsizedArray : 1;
237
238 /// Indicator of whether the most-derived object is an array element.
239 LLVM_PREFERRED_TYPE(bool)
240 unsigned MostDerivedIsArrayElement : 1;
241
242 /// The length of the path to the most-derived object of which this is a
243 /// subobject.
244 unsigned MostDerivedPathLength : 28;
245
246 /// The size of the array of which the most-derived object is an element.
247 /// This will always be 0 if the most-derived object is not an array
248 /// element. 0 is not an indicator of whether or not the most-derived object
249 /// is an array, however, because 0-length arrays are allowed.
250 ///
251 /// If the current array is an unsized array, the value of this is
252 /// undefined.
253 uint64_t MostDerivedArraySize;
254 /// The type of the most derived object referred to by this address.
255 QualType MostDerivedType;
256
257 typedef APValue::LValuePathEntry PathEntry;
258
259 /// The entries on the path from the glvalue to the designated subobject.
260 SmallVector<PathEntry, 8> Entries;
261
262 SubobjectDesignator() : Invalid(true) {}
263
264 explicit SubobjectDesignator(QualType T)
265 : Invalid(false), IsOnePastTheEnd(false),
266 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
267 MostDerivedPathLength(0), MostDerivedArraySize(0),
268 MostDerivedType(T.isNull() ? QualType() : T.getNonReferenceType()) {}
269
270 SubobjectDesignator(const ASTContext &Ctx, const APValue &V)
271 : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false),
272 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
273 MostDerivedPathLength(0), MostDerivedArraySize(0) {
274 assert(V.isLValue() && "Non-LValue used to make an LValue designator?");
275 if (!Invalid) {
276 IsOnePastTheEnd = V.isLValueOnePastTheEnd();
277 llvm::append_range(C&: Entries, R: V.getLValuePath());
278 if (V.getLValueBase()) {
279 bool IsArray = false;
280 bool FirstIsUnsizedArray = false;
281 MostDerivedPathLength = findMostDerivedSubobject(
282 Ctx, Base: V.getLValueBase(), Path: V.getLValuePath(), ArraySize&: MostDerivedArraySize,
283 Type&: MostDerivedType, IsArray, FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
284 MostDerivedIsArrayElement = IsArray;
285 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
286 }
287 }
288 }
289
290 void truncate(ASTContext &Ctx, APValue::LValueBase Base,
291 unsigned NewLength) {
292 if (Invalid)
293 return;
294
295 assert(Base && "cannot truncate path for null pointer");
296 assert(NewLength <= Entries.size() && "not a truncation");
297
298 if (NewLength == Entries.size())
299 return;
300 Entries.resize(N: NewLength);
301
302 bool IsArray = false;
303 bool FirstIsUnsizedArray = false;
304 MostDerivedPathLength = findMostDerivedSubobject(
305 Ctx, Base, Path: Entries, ArraySize&: MostDerivedArraySize, Type&: MostDerivedType, IsArray,
306 FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
307 MostDerivedIsArrayElement = IsArray;
308 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
309 }
310
311 void setInvalid() {
312 Invalid = true;
313 Entries.clear();
314 }
315
316 /// Determine whether the most derived subobject is an array without a
317 /// known bound.
318 bool isMostDerivedAnUnsizedArray() const {
319 assert(!Invalid && "Calling this makes no sense on invalid designators");
320 return Entries.size() == 1 && FirstEntryIsAnUnsizedArray;
321 }
322
323 /// Determine what the most derived array's size is. Results in an assertion
324 /// failure if the most derived array lacks a size.
325 uint64_t getMostDerivedArraySize() const {
326 assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size");
327 return MostDerivedArraySize;
328 }
329
330 /// Determine whether this is a one-past-the-end pointer.
331 bool isOnePastTheEnd() const {
332 assert(!Invalid);
333 if (IsOnePastTheEnd)
334 return true;
335 if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement &&
336 Entries[MostDerivedPathLength - 1].getAsArrayIndex() ==
337 MostDerivedArraySize)
338 return true;
339 return false;
340 }
341
342 /// Get the range of valid index adjustments in the form
343 /// {maximum value that can be subtracted from this pointer,
344 /// maximum value that can be added to this pointer}
345 std::pair<uint64_t, uint64_t> validIndexAdjustments() {
346 if (Invalid || isMostDerivedAnUnsizedArray())
347 return {0, 0};
348
349 // [expr.add]p4: For the purposes of these operators, a pointer to a
350 // nonarray object behaves the same as a pointer to the first element of
351 // an array of length one with the type of the object as its element type.
352 bool IsArray = MostDerivedPathLength == Entries.size() &&
353 MostDerivedIsArrayElement;
354 uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex()
355 : (uint64_t)IsOnePastTheEnd;
356 uint64_t ArraySize =
357 IsArray ? getMostDerivedArraySize() : (uint64_t)1;
358 return {ArrayIndex, ArraySize - ArrayIndex};
359 }
360
361 /// Check that this refers to a valid subobject.
362 bool isValidSubobject() const {
363 if (Invalid)
364 return false;
365 return !isOnePastTheEnd();
366 }
367 /// Check that this refers to a valid subobject, and if not, produce a
368 /// relevant diagnostic and set the designator as invalid.
369 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK);
370
371 /// Get the type of the designated object.
372 QualType getType(ASTContext &Ctx) const {
373 assert(!Invalid && "invalid designator has no subobject type");
374 return MostDerivedPathLength == Entries.size()
375 ? MostDerivedType
376 : Ctx.getCanonicalTagType(TD: getAsBaseClass(E: Entries.back()));
377 }
378
379 /// Update this designator to refer to the first element within this array.
380 void addArrayUnchecked(const ConstantArrayType *CAT) {
381 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
382
383 // This is a most-derived object.
384 MostDerivedType = CAT->getElementType();
385 MostDerivedIsArrayElement = true;
386 MostDerivedArraySize = CAT->getZExtSize();
387 MostDerivedPathLength = Entries.size();
388 }
389 /// Update this designator to refer to the first element within the array of
390 /// elements of type T. This is an array of unknown size.
391 void addUnsizedArrayUnchecked(QualType ElemTy) {
392 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
393
394 MostDerivedType = ElemTy;
395 MostDerivedIsArrayElement = true;
396 // The value in MostDerivedArraySize is undefined in this case. So, set it
397 // to an arbitrary value that's likely to loudly break things if it's
398 // used.
399 MostDerivedArraySize = AssumedSizeForUnsizedArray;
400 MostDerivedPathLength = Entries.size();
401 }
402 /// Update this designator to refer to the given base or member of this
403 /// object.
404 void addDeclUnchecked(const Decl *D, bool Virtual = false) {
405 Entries.push_back(Elt: APValue::BaseOrMemberType(D, Virtual));
406
407 // If this isn't a base class, it's a new most-derived object.
408 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: D)) {
409 MostDerivedType = FD->getType();
410 MostDerivedIsArrayElement = false;
411 MostDerivedArraySize = 0;
412 MostDerivedPathLength = Entries.size();
413 }
414 }
415 /// Update this designator to refer to the given complex component.
416 void addComplexUnchecked(QualType EltTy, bool Imag) {
417 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Imag));
418
419 // This is technically a most-derived object, though in practice this
420 // is unlikely to matter.
421 MostDerivedType = EltTy;
422 MostDerivedIsArrayElement = true;
423 MostDerivedArraySize = 2;
424 MostDerivedPathLength = Entries.size();
425 }
426
427 void addVectorElementUnchecked(QualType EltTy, uint64_t Size,
428 uint64_t Idx) {
429 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Idx));
430 MostDerivedType = EltTy;
431 MostDerivedPathLength = Entries.size();
432 MostDerivedArraySize = 0;
433 MostDerivedIsArrayElement = false;
434 }
435
436 void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E);
437 void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E,
438 const APSInt &N);
439 /// Add N to the address of this subobject.
440 void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N, const LValue &LV);
441 };
442
443 /// A scope at the end of which an object can need to be destroyed.
444 enum class ScopeKind {
445 Block,
446 FullExpression,
447 Call
448 };
449
450 /// A reference to a particular call and its arguments.
451 struct CallRef {
452 CallRef() : OrigCallee(), CallIndex(0), Version() {}
453 CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version)
454 : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {}
455
456 explicit operator bool() const { return OrigCallee; }
457
458 /// Get the parameter that the caller initialized, corresponding to the
459 /// given parameter in the callee.
460 const ParmVarDecl *getOrigParam(const ParmVarDecl *PVD) const {
461 return OrigCallee ? OrigCallee->getParamDecl(i: PVD->getFunctionScopeIndex())
462 : PVD;
463 }
464
465 /// The callee at the point where the arguments were evaluated. This might
466 /// be different from the actual callee (a different redeclaration, or a
467 /// virtual override), but this function's parameters are the ones that
468 /// appear in the parameter map.
469 const FunctionDecl *OrigCallee;
470 /// The call index of the frame that holds the argument values.
471 unsigned CallIndex;
472 /// The version of the parameters corresponding to this call.
473 unsigned Version;
474 };
475
476 /// A stack frame in the constexpr call stack.
477 class CallStackFrame : public interp::Frame {
478 public:
479 EvalInfo &Info;
480
481 /// Parent - The caller of this stack frame.
482 CallStackFrame *Caller;
483
484 /// Callee - The function which was called.
485 const FunctionDecl *Callee;
486
487 /// This - The binding for the this pointer in this call, if any.
488 const LValue *This;
489
490 /// CallExpr - The syntactical structure of member function calls
491 const Expr *CallExpr;
492
493 /// Information on how to find the arguments to this call. Our arguments
494 /// are stored in our parent's CallStackFrame, using the ParmVarDecl* as a
495 /// key and this value as the version.
496 CallRef Arguments;
497
498 /// Source location information about the default argument or default
499 /// initializer expression we're evaluating, if any.
500 CurrentSourceLocExprScope CurSourceLocExprScope;
501
502 // Note that we intentionally use std::map here so that references to
503 // values are stable.
504 typedef std::pair<const void *, unsigned> MapKeyTy;
505 typedef std::map<MapKeyTy, APValue> MapTy;
506 /// Temporaries - Temporary lvalues materialized within this stack frame.
507 MapTy Temporaries;
508
509 /// CallRange - The source range of the call expression for this call.
510 SourceRange CallRange;
511
512 /// Index - The call index of this call.
513 unsigned Index;
514
515 /// The stack of integers for tracking version numbers for temporaries.
516 SmallVector<unsigned, 2> TempVersionStack = {1};
517 unsigned CurTempVersion = TempVersionStack.back();
518
519 unsigned getTempVersion() const { return TempVersionStack.back(); }
520
521 void pushTempVersion() {
522 TempVersionStack.push_back(Elt: ++CurTempVersion);
523 }
524
525 void popTempVersion() {
526 TempVersionStack.pop_back();
527 }
528
529 CallRef createCall(const FunctionDecl *Callee) {
530 return {Callee, Index, ++CurTempVersion};
531 }
532
533 // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact
534 // on the overall stack usage of deeply-recursing constexpr evaluations.
535 // (We should cache this map rather than recomputing it repeatedly.)
536 // But let's try this and see how it goes; we can look into caching the map
537 // as a later change.
538
539 /// LambdaCaptureFields - Mapping from captured variables/this to
540 /// corresponding data members in the closure class.
541 llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;
542 FieldDecl *LambdaThisCaptureField = nullptr;
543
544 CallStackFrame(EvalInfo &Info, SourceRange CallRange,
545 const FunctionDecl *Callee, const LValue *This,
546 const Expr *CallExpr, CallRef Arguments);
547 ~CallStackFrame();
548
549 // Return the temporary for Key whose version number is Version.
550 APValue *getTemporary(const void *Key, unsigned Version) {
551 MapKeyTy KV(Key, Version);
552 auto LB = Temporaries.lower_bound(x: KV);
553 if (LB != Temporaries.end() && LB->first == KV)
554 return &LB->second;
555 return nullptr;
556 }
557
558 // Return the current temporary for Key in the map.
559 APValue *getCurrentTemporary(const void *Key) {
560 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
561 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
562 return &std::prev(x: UB)->second;
563 return nullptr;
564 }
565
566 // Return the version number of the current temporary for Key.
567 unsigned getCurrentTemporaryVersion(const void *Key) const {
568 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
569 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
570 return std::prev(x: UB)->first.second;
571 return 0;
572 }
573
574 /// Allocate storage for an object of type T in this stack frame.
575 /// Populates LV with a handle to the created object. Key identifies
576 /// the temporary within the stack frame, and must not be reused without
577 /// bumping the temporary version number.
578 template<typename KeyT>
579 APValue &createTemporary(const KeyT *Key, QualType T,
580 ScopeKind Scope, LValue &LV);
581
582 /// Allocate storage for a parameter of a function call made in this frame.
583 APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV);
584
585 void describe(llvm::raw_ostream &OS) const override;
586
587 Frame *getCaller() const override { return Caller; }
588 SourceRange getCallRange() const override { return CallRange; }
589 const FunctionDecl *getCallee() const override { return Callee; }
590
591 bool isStdFunction() const {
592 for (const DeclContext *DC = Callee; DC; DC = DC->getParent())
593 if (DC->isStdNamespace())
594 return true;
595 return false;
596 }
597
598 /// Whether we're in a context where [[msvc::constexpr]] evaluation is
599 /// permitted. See MSConstexprDocs for description of permitted contexts.
600 bool CanEvalMSConstexpr = false;
601
602 private:
603 APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T,
604 ScopeKind Scope);
605 };
606
607 /// Temporarily override 'this'.
608 class ThisOverrideRAII {
609 public:
610 ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable)
611 : Frame(Frame), OldThis(Frame.This) {
612 if (Enable)
613 Frame.This = NewThis;
614 }
615 ~ThisOverrideRAII() {
616 Frame.This = OldThis;
617 }
618 private:
619 CallStackFrame &Frame;
620 const LValue *OldThis;
621 };
622
623 // A shorthand time trace scope struct, prints source range, for example
624 // {"name":"EvaluateAsRValue","args":{"detail":"<test.cc:8:21, col:25>"}}}
625 class ExprTimeTraceScope {
626 public:
627 ExprTimeTraceScope(const Expr *E, const ASTContext &Ctx, StringRef Name)
628 : TimeScope(Name, [E, &Ctx] {
629 return E->getSourceRange().printToString(SM: Ctx.getSourceManager());
630 }) {}
631
632 private:
633 llvm::TimeTraceScope TimeScope;
634 };
635
636 /// RAII object used to change the current ability of
637 /// [[msvc::constexpr]] evaulation.
638 struct MSConstexprContextRAII {
639 CallStackFrame &Frame;
640 bool OldValue;
641 explicit MSConstexprContextRAII(CallStackFrame &Frame, bool Value)
642 : Frame(Frame), OldValue(Frame.CanEvalMSConstexpr) {
643 Frame.CanEvalMSConstexpr = Value;
644 }
645
646 ~MSConstexprContextRAII() { Frame.CanEvalMSConstexpr = OldValue; }
647 };
648}
649
650static bool HandleDestruction(EvalInfo &Info, const Expr *E,
651 const LValue &This, QualType ThisType);
652static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
653 APValue::LValueBase LVBase, APValue &Value,
654 QualType T);
655
656namespace {
657 /// A cleanup, and a flag indicating whether it is lifetime-extended.
658 class Cleanup {
659 llvm::PointerIntPair<APValue*, 2, ScopeKind> Value;
660 APValue::LValueBase Base;
661 QualType T;
662
663 public:
664 Cleanup(APValue *Val, APValue::LValueBase Base, QualType T,
665 ScopeKind Scope)
666 : Value(Val, Scope), Base(Base), T(T) {}
667
668 /// Determine whether this cleanup should be performed at the end of the
669 /// given kind of scope.
670 bool isDestroyedAtEndOf(ScopeKind K) const {
671 return (int)Value.getInt() >= (int)K;
672 }
673 bool endLifetime(EvalInfo &Info, bool RunDestructors) {
674 if (RunDestructors) {
675 SourceLocation Loc;
676 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
677 Loc = VD->getLocation();
678 else if (const Expr *E = Base.dyn_cast<const Expr*>())
679 Loc = E->getExprLoc();
680 return HandleDestruction(Info, Loc, LVBase: Base, Value&: *Value.getPointer(), T);
681 }
682 *Value.getPointer() = APValue();
683 return true;
684 }
685
686 bool hasSideEffect() {
687 return T.isDestructedType();
688 }
689 };
690
691 /// A reference to an object whose construction we are currently evaluating.
692 struct ObjectUnderConstruction {
693 APValue::LValueBase Base;
694 ArrayRef<APValue::LValuePathEntry> Path;
695 friend bool operator==(const ObjectUnderConstruction &LHS,
696 const ObjectUnderConstruction &RHS) {
697 return LHS.Base == RHS.Base && LHS.Path == RHS.Path;
698 }
699 friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) {
700 return llvm::hash_combine(args: Obj.Base, args: Obj.Path);
701 }
702 };
703 enum class ConstructionPhase {
704 None,
705 Bases,
706 AfterBases,
707 AfterFields,
708 Destroying,
709 DestroyingBases
710 };
711}
712
713namespace llvm {
714template<> struct DenseMapInfo<ObjectUnderConstruction> {
715 using Base = DenseMapInfo<APValue::LValueBase>;
716 static unsigned getHashValue(const ObjectUnderConstruction &Object) {
717 return hash_value(Obj: Object);
718 }
719 static bool isEqual(const ObjectUnderConstruction &LHS,
720 const ObjectUnderConstruction &RHS) {
721 return LHS == RHS;
722 }
723};
724}
725
726namespace {
727 /// A dynamically-allocated heap object.
728 struct DynAlloc {
729 /// The value of this heap-allocated object.
730 APValue Value;
731 /// The allocating expression; used for diagnostics. Either a CXXNewExpr
732 /// or a CallExpr (the latter is for direct calls to operator new inside
733 /// std::allocator<T>::allocate).
734 const Expr *AllocExpr = nullptr;
735
736 /// Get the kind of the allocation. This must match between allocation
737 /// and deallocation.
738 static DynAllocKind kindOfExpr(const Expr *AllocExpr) {
739 if (auto *NE = dyn_cast<CXXNewExpr>(Val: AllocExpr))
740 return NE->isArray() ? DynAllocKind::ArrayNew : DynAllocKind::New;
741 assert(isa<CallExpr>(AllocExpr));
742 return DynAllocKind::StdAllocator;
743 }
744
745 DynAllocKind getKind() const { return kindOfExpr(AllocExpr); }
746 };
747
748 struct DynAllocOrder {
749 bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const {
750 return L.getIndex() < R.getIndex();
751 }
752 };
753
754 /// EvalInfo - This is a private struct used by the evaluator to capture
755 /// information about a subexpression as it is folded. It retains information
756 /// about the AST context, but also maintains information about the folded
757 /// expression.
758 ///
759 /// If an expression could be evaluated, it is still possible it is not a C
760 /// "integer constant expression" or constant expression. If not, this struct
761 /// captures information about how and why not.
762 ///
763 /// One bit of information passed *into* the request for constant folding
764 /// indicates whether the subexpression is "evaluated" or not according to C
765 /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
766 /// evaluate the expression regardless of what the RHS is, but C only allows
767 /// certain things in certain situations.
768 class EvalInfo final : public interp::State {
769 public:
770 /// CurrentCall - The top of the constexpr call stack.
771 CallStackFrame *CurrentCall;
772
773 /// CallStackDepth - The number of calls in the call stack right now.
774 unsigned CallStackDepth;
775
776 /// NextCallIndex - The next call index to assign.
777 unsigned NextCallIndex;
778
779 /// StepsLeft - The remaining number of evaluation steps we're permitted
780 /// to perform. This is essentially a limit for the number of statements
781 /// we will evaluate.
782 unsigned StepsLeft;
783
784 /// BottomFrame - The frame in which evaluation started. This must be
785 /// initialized after CurrentCall and CallStackDepth.
786 CallStackFrame BottomFrame;
787
788 /// A stack of values whose lifetimes end at the end of some surrounding
789 /// evaluation frame.
790 llvm::SmallVector<Cleanup, 16> CleanupStack;
791
792 /// EvaluatingDecl - This is the declaration whose initializer is being
793 /// evaluated, if any.
794 APValue::LValueBase EvaluatingDecl;
795
796 enum class EvaluatingDeclKind {
797 None,
798 /// We're evaluating the construction of EvaluatingDecl.
799 Ctor,
800 /// We're evaluating the destruction of EvaluatingDecl.
801 Dtor,
802 };
803 EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None;
804
805 /// EvaluatingDeclValue - This is the value being constructed for the
806 /// declaration whose initializer is being evaluated, if any.
807 APValue *EvaluatingDeclValue;
808
809 /// Stack of loops and 'switch' statements which we're currently
810 /// breaking/continuing; null entries are used to mark unlabeled
811 /// break/continue.
812 SmallVector<const Stmt *> BreakContinueStack;
813
814 /// Set of objects that are currently being constructed.
815 llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase>
816 ObjectsUnderConstruction;
817
818 /// Current heap allocations, along with the location where each was
819 /// allocated. We use std::map here because we need stable addresses
820 /// for the stored APValues.
821 std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs;
822
823 /// The number of heap allocations performed so far in this evaluation.
824 uintptr_t NumHeapAllocs = 0;
825
826 struct EvaluatingConstructorRAII {
827 EvalInfo &EI;
828 ObjectUnderConstruction Object;
829 bool DidInsert;
830 EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object,
831 bool HasBases)
832 : EI(EI), Object(Object) {
833 DidInsert =
834 EI.ObjectsUnderConstruction
835 .insert(KV: {Object, HasBases ? ConstructionPhase::Bases
836 : ConstructionPhase::AfterBases})
837 .second;
838 }
839 void finishedConstructingBases() {
840 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases;
841 }
842 void finishedConstructingFields() {
843 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterFields;
844 }
845 ~EvaluatingConstructorRAII() {
846 if (DidInsert) EI.ObjectsUnderConstruction.erase(Val: Object);
847 }
848 };
849
850 struct EvaluatingDestructorRAII {
851 EvalInfo &EI;
852 ObjectUnderConstruction Object;
853 bool DidInsert;
854 EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object)
855 : EI(EI), Object(Object) {
856 DidInsert = EI.ObjectsUnderConstruction
857 .insert(KV: {Object, ConstructionPhase::Destroying})
858 .second;
859 }
860 void startedDestroyingBases() {
861 EI.ObjectsUnderConstruction[Object] =
862 ConstructionPhase::DestroyingBases;
863 }
864 ~EvaluatingDestructorRAII() {
865 if (DidInsert)
866 EI.ObjectsUnderConstruction.erase(Val: Object);
867 }
868 };
869
870 ConstructionPhase
871 isEvaluatingCtorDtor(APValue::LValueBase Base,
872 ArrayRef<APValue::LValuePathEntry> Path) {
873 return ObjectsUnderConstruction.lookup(Val: {.Base: Base, .Path: Path});
874 }
875
876 /// If we're currently speculatively evaluating, the outermost call stack
877 /// depth at which we can mutate state, otherwise 0.
878 unsigned SpeculativeEvaluationDepth = 0;
879
880 /// The current array initialization index, if we're performing array
881 /// initialization.
882 uint64_t ArrayInitIndex = -1;
883
884 EvalInfo(const ASTContext &C, SemaProxy *SProxy, Expr::EvalStatus &S,
885 EvaluationMode Mode)
886 : State(const_cast<ASTContext &>(C), SProxy, S), CurrentCall(nullptr),
887 CallStackDepth(0), NextCallIndex(1),
888 StepsLeft(C.getLangOpts().ConstexprStepLimit),
889 BottomFrame(*this, SourceLocation(), /*Callee=*/nullptr,
890 /*This=*/nullptr,
891 /*CallExpr=*/nullptr, CallRef()),
892 EvaluatingDecl((const ValueDecl *)nullptr),
893 EvaluatingDeclValue(nullptr) {
894 EvalMode = Mode;
895 }
896
897 ~EvalInfo() {
898 discardCleanups();
899 }
900
901 void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value,
902 EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) {
903 EvaluatingDecl = Base;
904 IsEvaluatingDecl = EDK;
905 EvaluatingDeclValue = &Value;
906 }
907
908 bool CheckCallLimit(SourceLocation Loc) {
909 // Don't perform any constexpr calls (other than the call we're checking)
910 // when checking a potential constant expression.
911 if (checkingPotentialConstantExpression() && CallStackDepth > 1)
912 return false;
913 if (NextCallIndex == 0) {
914 // NextCallIndex has wrapped around.
915 FFDiag(Loc, DiagId: diag::note_constexpr_call_limit_exceeded);
916 return false;
917 }
918 if (CallStackDepth <= getLangOpts().ConstexprCallDepth)
919 return true;
920 FFDiag(Loc, DiagId: diag::note_constexpr_depth_limit_exceeded)
921 << getLangOpts().ConstexprCallDepth;
922 return false;
923 }
924
925 bool CheckArraySize(SourceLocation Loc, unsigned BitWidth,
926 uint64_t ElemCount, bool Diag) {
927 // FIXME: GH63562
928 // APValue stores array extents as unsigned,
929 // so anything that is greater that unsigned would overflow when
930 // constructing the array, we catch this here.
931 if (BitWidth > ConstantArrayType::getMaxSizeBits(Context: Ctx) ||
932 ElemCount > uint64_t(std::numeric_limits<unsigned>::max())) {
933 if (Diag)
934 FFDiag(Loc, DiagId: diag::note_constexpr_new_too_large) << ElemCount;
935 return false;
936 }
937
938 // FIXME: GH63562
939 // Arrays allocate an APValue per element.
940 // We use the number of constexpr steps as a proxy for the maximum size
941 // of arrays to avoid exhausting the system resources, as initialization
942 // of each element is likely to take some number of steps anyway.
943 uint64_t Limit = getLangOpts().ConstexprStepLimit;
944 if (Limit != 0 && ElemCount > Limit) {
945 if (Diag) {
946 FFDiag(Loc, DiagId: diag::note_constexpr_new_exceeds_limits, ExtraNotes: 1)
947 << ElemCount << Limit;
948 Note(Loc, DiagId: diag::note_constexpr_steps);
949 }
950 return false;
951 }
952 return true;
953 }
954
955 std::pair<CallStackFrame *, unsigned>
956 getCallFrameAndDepth(unsigned CallIndex) {
957 assert(CallIndex && "no call index in getCallFrameAndDepth");
958 // We will eventually hit BottomFrame, which has Index 1, so Frame can't
959 // be null in this loop.
960 unsigned Depth = CallStackDepth;
961 CallStackFrame *Frame = CurrentCall;
962 while (Frame->Index > CallIndex) {
963 Frame = Frame->Caller;
964 --Depth;
965 }
966 if (Frame->Index == CallIndex)
967 return {Frame, Depth};
968 return {nullptr, 0};
969 }
970
971 bool nextStep(const Stmt *S) {
972 if (getLangOpts().ConstexprStepLimit == 0)
973 return true;
974
975 if (!StepsLeft) {
976 FFDiag(Loc: S->getBeginLoc(), DiagId: diag::note_constexpr_step_limit_exceeded, ExtraNotes: 1)
977 << getLangOpts().ConstexprStepLimit;
978 Note(Loc: S->getBeginLoc(), DiagId: diag::note_constexpr_steps);
979 return false;
980 }
981 --StepsLeft;
982 return true;
983 }
984
985 APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV);
986
987 std::optional<DynAlloc *> lookupDynamicAlloc(DynamicAllocLValue DA) {
988 std::optional<DynAlloc *> Result;
989 auto It = HeapAllocs.find(x: DA);
990 if (It != HeapAllocs.end())
991 Result = &It->second;
992 return Result;
993 }
994
995 /// Get the allocated storage for the given parameter of the given call.
996 APValue *getParamSlot(CallRef Call, const ParmVarDecl *PVD) {
997 CallStackFrame *Frame = getCallFrameAndDepth(CallIndex: Call.CallIndex).first;
998 return Frame ? Frame->getTemporary(Key: Call.getOrigParam(PVD), Version: Call.Version)
999 : nullptr;
1000 }
1001
1002 /// Information about a stack frame for std::allocator<T>::[de]allocate.
1003 struct StdAllocatorCaller {
1004 unsigned FrameIndex;
1005 QualType ElemType;
1006 const Expr *Call;
1007 explicit operator bool() const { return FrameIndex != 0; };
1008 };
1009
1010 StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const {
1011 for (const CallStackFrame *Call = CurrentCall; Call->Caller != nullptr;
1012 Call = Call->Caller) {
1013 const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Val: Call->Callee);
1014 if (!MD)
1015 continue;
1016 const IdentifierInfo *FnII = MD->getIdentifier();
1017 if (!FnII || !FnII->isStr(Str: FnName))
1018 continue;
1019
1020 const auto *CTSD =
1021 dyn_cast<ClassTemplateSpecializationDecl>(Val: MD->getParent());
1022 if (!CTSD)
1023 continue;
1024
1025 const IdentifierInfo *ClassII = CTSD->getIdentifier();
1026 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
1027 if (CTSD->isInStdNamespace() && ClassII &&
1028 ClassII->isStr(Str: "allocator") && TAL.size() >= 1 &&
1029 TAL[0].getKind() == TemplateArgument::Type)
1030 return {.FrameIndex: Call->Index, .ElemType: TAL[0].getAsType(), .Call: Call->CallExpr};
1031 }
1032
1033 return {};
1034 }
1035
1036 void performLifetimeExtension() {
1037 // Disable the cleanups for lifetime-extended temporaries.
1038 llvm::erase_if(C&: CleanupStack, P: [](Cleanup &C) {
1039 return !C.isDestroyedAtEndOf(K: ScopeKind::FullExpression);
1040 });
1041 }
1042
1043 /// Throw away any remaining cleanups at the end of evaluation. If any
1044 /// cleanups would have had a side-effect, note that as an unmodeled
1045 /// side-effect and return false. Otherwise, return true.
1046 bool discardCleanups() {
1047 for (Cleanup &C : CleanupStack) {
1048 if (C.hasSideEffect() && !noteSideEffect()) {
1049 CleanupStack.clear();
1050 return false;
1051 }
1052 }
1053 CleanupStack.clear();
1054 return true;
1055 }
1056
1057 private:
1058 const interp::Frame *getCurrentFrame() override { return CurrentCall; }
1059
1060 unsigned getCallStackDepth() override { return CallStackDepth; }
1061 bool stepsLeft() const override { return StepsLeft > 0; }
1062
1063 public:
1064 /// Notes that we failed to evaluate an expression that other expressions
1065 /// directly depend on, and determine if we should keep evaluating. This
1066 /// should only be called if we actually intend to keep evaluating.
1067 ///
1068 /// Call noteSideEffect() instead if we may be able to ignore the value that
1069 /// we failed to evaluate, e.g. if we failed to evaluate Foo() in:
1070 ///
1071 /// (Foo(), 1) // use noteSideEffect
1072 /// (Foo() || true) // use noteSideEffect
1073 /// Foo() + 1 // use noteFailure
1074 [[nodiscard]] bool noteFailure() {
1075 // Failure when evaluating some expression often means there is some
1076 // subexpression whose evaluation was skipped. Therefore, (because we
1077 // don't track whether we skipped an expression when unwinding after an
1078 // evaluation failure) every evaluation failure that bubbles up from a
1079 // subexpression implies that a side-effect has potentially happened. We
1080 // skip setting the HasSideEffects flag to true until we decide to
1081 // continue evaluating after that point, which happens here.
1082 bool KeepGoing = keepEvaluatingAfterFailure();
1083 EvalStatus.HasSideEffects |= KeepGoing;
1084 return KeepGoing;
1085 }
1086
1087 class ArrayInitLoopIndex {
1088 EvalInfo &Info;
1089 uint64_t OuterIndex;
1090
1091 public:
1092 ArrayInitLoopIndex(EvalInfo &Info)
1093 : Info(Info), OuterIndex(Info.ArrayInitIndex) {
1094 Info.ArrayInitIndex = 0;
1095 }
1096 ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; }
1097
1098 operator uint64_t&() { return Info.ArrayInitIndex; }
1099 };
1100 };
1101
1102 /// Object used to treat all foldable expressions as constant expressions.
1103 struct FoldConstant {
1104 EvalInfo &Info;
1105 bool Enabled;
1106 bool HadNoPriorDiags;
1107 EvaluationMode OldMode;
1108
1109 explicit FoldConstant(EvalInfo &Info, bool Enabled)
1110 : Info(Info),
1111 Enabled(Enabled),
1112 HadNoPriorDiags(Info.EvalStatus.Diag &&
1113 Info.EvalStatus.Diag->empty() &&
1114 !Info.EvalStatus.HasSideEffects),
1115 OldMode(Info.EvalMode) {
1116 if (Enabled)
1117 Info.EvalMode = EvaluationMode::ConstantFold;
1118 }
1119 void keepDiagnostics() { Enabled = false; }
1120 ~FoldConstant() {
1121 if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() &&
1122 !Info.EvalStatus.HasSideEffects) {
1123 Info.EvalStatus.Diag->clear();
1124 Info.EvalStatus.DiagEmitted = false;
1125 }
1126 Info.EvalMode = OldMode;
1127 }
1128 };
1129
1130 /// RAII object used to set the current evaluation mode to ignore
1131 /// side-effects.
1132 struct IgnoreSideEffectsRAII {
1133 EvalInfo &Info;
1134 EvaluationMode OldMode;
1135 explicit IgnoreSideEffectsRAII(EvalInfo &Info)
1136 : Info(Info), OldMode(Info.EvalMode) {
1137 Info.EvalMode = EvaluationMode::IgnoreSideEffects;
1138 }
1139
1140 ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; }
1141 };
1142
1143 /// RAII object used to optionally suppress diagnostics and side-effects from
1144 /// a speculative evaluation.
1145 class SpeculativeEvaluationRAII {
1146 EvalInfo *Info = nullptr;
1147 Expr::EvalStatus OldStatus;
1148 unsigned OldSpeculativeEvaluationDepth = 0;
1149
1150 void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) {
1151 Info = Other.Info;
1152 OldStatus = Other.OldStatus;
1153 OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth;
1154 Other.Info = nullptr;
1155 }
1156
1157 void maybeRestoreState() {
1158 if (!Info)
1159 return;
1160
1161 Info->EvalStatus = OldStatus;
1162 Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth;
1163 }
1164
1165 public:
1166 SpeculativeEvaluationRAII() = default;
1167
1168 SpeculativeEvaluationRAII(
1169 EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr)
1170 : Info(&Info), OldStatus(Info.EvalStatus),
1171 OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) {
1172 Info.EvalStatus.Diag = NewDiag;
1173 Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1;
1174 }
1175
1176 SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete;
1177 SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) {
1178 moveFromAndCancel(Other: std::move(Other));
1179 }
1180
1181 SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) {
1182 maybeRestoreState();
1183 moveFromAndCancel(Other: std::move(Other));
1184 return *this;
1185 }
1186
1187 ~SpeculativeEvaluationRAII() { maybeRestoreState(); }
1188 };
1189
1190 /// RAII object wrapping a full-expression or block scope, and handling
1191 /// the ending of the lifetime of temporaries created within it.
1192 template<ScopeKind Kind>
1193 class ScopeRAII {
1194 EvalInfo &Info;
1195 unsigned OldStackSize;
1196 public:
1197 ScopeRAII(EvalInfo &Info)
1198 : Info(Info), OldStackSize(Info.CleanupStack.size()) {
1199 // Push a new temporary version. This is needed to distinguish between
1200 // temporaries created in different iterations of a loop.
1201 Info.CurrentCall->pushTempVersion();
1202 }
1203 bool destroy(bool RunDestructors = true) {
1204 bool OK = cleanup(Info, RunDestructors, OldStackSize);
1205 OldStackSize = std::numeric_limits<unsigned>::max();
1206 return OK;
1207 }
1208 ~ScopeRAII() {
1209 if (OldStackSize != std::numeric_limits<unsigned>::max())
1210 destroy(RunDestructors: false);
1211 // Body moved to a static method to encourage the compiler to inline away
1212 // instances of this class.
1213 Info.CurrentCall->popTempVersion();
1214 }
1215 private:
1216 static bool cleanup(EvalInfo &Info, bool RunDestructors,
1217 unsigned OldStackSize) {
1218 assert(OldStackSize <= Info.CleanupStack.size() &&
1219 "running cleanups out of order?");
1220
1221 // Run all cleanups for a block scope, and non-lifetime-extended cleanups
1222 // for a full-expression scope.
1223 bool Success = true;
1224 for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) {
1225 if (Info.CleanupStack[I - 1].isDestroyedAtEndOf(K: Kind)) {
1226 if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) {
1227 Success = false;
1228 break;
1229 }
1230 }
1231 }
1232
1233 // Compact any retained cleanups.
1234 auto NewEnd = Info.CleanupStack.begin() + OldStackSize;
1235 if (Kind != ScopeKind::Block)
1236 NewEnd =
1237 std::remove_if(NewEnd, Info.CleanupStack.end(), [](Cleanup &C) {
1238 return C.isDestroyedAtEndOf(K: Kind);
1239 });
1240 Info.CleanupStack.erase(CS: NewEnd, CE: Info.CleanupStack.end());
1241 return Success;
1242 }
1243 };
1244 typedef ScopeRAII<ScopeKind::Block> BlockScopeRAII;
1245 typedef ScopeRAII<ScopeKind::FullExpression> FullExpressionRAII;
1246 typedef ScopeRAII<ScopeKind::Call> CallScopeRAII;
1247}
1248
1249bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,
1250 CheckSubobjectKind CSK) {
1251 if (Invalid)
1252 return false;
1253 if (isOnePastTheEnd()) {
1254 Info.CCEDiag(E, DiagId: diag::note_constexpr_past_end_subobject)
1255 << CSK;
1256 setInvalid();
1257 return false;
1258 }
1259 // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there
1260 // must actually be at least one array element; even a VLA cannot have a
1261 // bound of zero. And if our index is nonzero, we already had a CCEDiag.
1262 return true;
1263}
1264
1265void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info,
1266 const Expr *E) {
1267 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsized_array_indexed);
1268 // Do not set the designator as invalid: we can represent this situation,
1269 // and correct handling of __builtin_object_size requires us to do so.
1270}
1271
1272void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
1273 const Expr *E,
1274 const APSInt &N) {
1275 // If we're complaining, we must be able to statically determine the size of
1276 // the most derived array.
1277 if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement)
1278 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1279 << N << /*array*/ 0
1280 << static_cast<unsigned>(getMostDerivedArraySize());
1281 else
1282 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1283 << N << /*non-array*/ 1;
1284 setInvalid();
1285}
1286
1287CallStackFrame::CallStackFrame(EvalInfo &Info, SourceRange CallRange,
1288 const FunctionDecl *Callee, const LValue *This,
1289 const Expr *CallExpr, CallRef Call)
1290 : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This),
1291 CallExpr(CallExpr), Arguments(Call), CallRange(CallRange),
1292 Index(Info.NextCallIndex++) {
1293 Info.CurrentCall = this;
1294 ++Info.CallStackDepth;
1295}
1296
1297CallStackFrame::~CallStackFrame() {
1298 assert(Info.CurrentCall == this && "calls retired out of order");
1299 --Info.CallStackDepth;
1300 Info.CurrentCall = Caller;
1301}
1302
1303static bool isRead(AccessKinds AK) {
1304 return AK == AK_Read || AK == AK_ReadObjectRepresentation ||
1305 AK == AK_IsWithinLifetime || AK == AK_Dereference;
1306}
1307
1308static bool isModification(AccessKinds AK) {
1309 switch (AK) {
1310 case AK_Read:
1311 case AK_ReadObjectRepresentation:
1312 case AK_MemberCall:
1313 case AK_DynamicCast:
1314 case AK_TypeId:
1315 case AK_IsWithinLifetime:
1316 case AK_Dereference:
1317 return false;
1318 case AK_Assign:
1319 case AK_Increment:
1320 case AK_Decrement:
1321 case AK_Construct:
1322 case AK_Destroy:
1323 return true;
1324 }
1325 llvm_unreachable("unknown access kind");
1326}
1327
1328static bool isAnyAccess(AccessKinds AK) {
1329 return isRead(AK) || isModification(AK);
1330}
1331
1332/// Is this an access per the C++ definition?
1333static bool isFormalAccess(AccessKinds AK) {
1334 return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy &&
1335 AK != AK_IsWithinLifetime && AK != AK_Dereference;
1336}
1337
1338/// Is this kind of access valid on an indeterminate object value?
1339static bool isValidIndeterminateAccess(AccessKinds AK) {
1340 switch (AK) {
1341 case AK_Read:
1342 case AK_Increment:
1343 case AK_Decrement:
1344 case AK_Dereference:
1345 // These need the object's value.
1346 return false;
1347
1348 case AK_IsWithinLifetime:
1349 case AK_ReadObjectRepresentation:
1350 case AK_Assign:
1351 case AK_Construct:
1352 case AK_Destroy:
1353 // Construction and destruction don't need the value.
1354 return true;
1355
1356 case AK_MemberCall:
1357 case AK_DynamicCast:
1358 case AK_TypeId:
1359 // These aren't really meaningful on scalars.
1360 return true;
1361 }
1362 llvm_unreachable("unknown access kind");
1363}
1364
1365namespace {
1366 struct ComplexValue {
1367 private:
1368 bool IsInt;
1369
1370 public:
1371 APSInt IntReal, IntImag;
1372 APFloat FloatReal, FloatImag;
1373
1374 ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {}
1375
1376 void makeComplexFloat() { IsInt = false; }
1377 bool isComplexFloat() const { return !IsInt; }
1378 APFloat &getComplexFloatReal() { return FloatReal; }
1379 APFloat &getComplexFloatImag() { return FloatImag; }
1380
1381 void makeComplexInt() { IsInt = true; }
1382 bool isComplexInt() const { return IsInt; }
1383 APSInt &getComplexIntReal() { return IntReal; }
1384 APSInt &getComplexIntImag() { return IntImag; }
1385
1386 void moveInto(APValue &v) const {
1387 if (isComplexFloat())
1388 v = APValue(FloatReal, FloatImag);
1389 else
1390 v = APValue(IntReal, IntImag);
1391 }
1392 void setFrom(const APValue &v) {
1393 assert(v.isComplexFloat() || v.isComplexInt());
1394 if (v.isComplexFloat()) {
1395 makeComplexFloat();
1396 FloatReal = v.getComplexFloatReal();
1397 FloatImag = v.getComplexFloatImag();
1398 } else {
1399 makeComplexInt();
1400 IntReal = v.getComplexIntReal();
1401 IntImag = v.getComplexIntImag();
1402 }
1403 }
1404 };
1405
1406 struct LValue {
1407 APValue::LValueBase Base;
1408 CharUnits Offset;
1409 SubobjectDesignator Designator;
1410 bool IsNullPtr : 1;
1411 bool InvalidBase : 1;
1412 // P2280R4 track if we have an unknown reference or pointer.
1413 bool AllowConstexprUnknown = false;
1414
1415 const APValue::LValueBase getLValueBase() const { return Base; }
1416 bool allowConstexprUnknown() const { return AllowConstexprUnknown; }
1417 CharUnits &getLValueOffset() { return Offset; }
1418 CharUnits getLValueOffset() const { return Offset; }
1419 SubobjectDesignator &getLValueDesignator() { return Designator; }
1420 const SubobjectDesignator &getLValueDesignator() const { return Designator;}
1421 bool isNullPointer() const { return IsNullPtr;}
1422
1423 unsigned getLValueCallIndex() const { return Base.getCallIndex(); }
1424 unsigned getLValueVersion() const { return Base.getVersion(); }
1425
1426 bool pointsToCompleteClass(const CXXRecordDecl *D) const {
1427 if (Designator.Entries.empty())
1428 return true;
1429
1430 return Designator.MostDerivedType->getAsCXXRecordDecl() == D;
1431 }
1432
1433 void moveInto(APValue &V) const {
1434 if (Designator.Invalid)
1435 V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr);
1436 else {
1437 assert(!InvalidBase && "APValues can't handle invalid LValue bases");
1438 V = APValue(Base, Offset, Designator.Entries,
1439 Designator.IsOnePastTheEnd, IsNullPtr);
1440 }
1441 if (AllowConstexprUnknown)
1442 V.setConstexprUnknown();
1443 }
1444 void setFrom(const ASTContext &Ctx, const APValue &V) {
1445 assert(V.isLValue() && "Setting LValue from a non-LValue?");
1446 Base = V.getLValueBase();
1447 Offset = V.getLValueOffset();
1448 InvalidBase = false;
1449 Designator = SubobjectDesignator(Ctx, V);
1450 IsNullPtr = V.isNullPointer();
1451 AllowConstexprUnknown = V.allowConstexprUnknown();
1452 }
1453
1454 void set(APValue::LValueBase B, bool BInvalid = false) {
1455#ifndef NDEBUG
1456 // We only allow a few types of invalid bases. Enforce that here.
1457 if (BInvalid) {
1458 const auto *E = B.get<const Expr *>();
1459 assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) &&
1460 "Unexpected type of invalid base");
1461 }
1462#endif
1463
1464 Base = B;
1465 Offset = CharUnits::fromQuantity(Quantity: 0);
1466 InvalidBase = BInvalid;
1467 Designator = SubobjectDesignator(getType(B));
1468 IsNullPtr = false;
1469 AllowConstexprUnknown = false;
1470 }
1471
1472 void setNull(ASTContext &Ctx, QualType PointerTy) {
1473 Base = (const ValueDecl *)nullptr;
1474 Offset =
1475 CharUnits::fromQuantity(Quantity: Ctx.getTargetNullPointerValue(QT: PointerTy));
1476 InvalidBase = false;
1477 Designator = SubobjectDesignator(PointerTy->getPointeeType());
1478 IsNullPtr = true;
1479 AllowConstexprUnknown = false;
1480 }
1481
1482 void setInvalid(APValue::LValueBase B, unsigned I = 0) {
1483 set(B, BInvalid: true);
1484 }
1485
1486 std::string toString(ASTContext &Ctx, QualType T) const {
1487 APValue Printable;
1488 moveInto(V&: Printable);
1489 return Printable.getAsString(Ctx, Ty: T);
1490 }
1491
1492 private:
1493 // Check that this LValue is not based on a null pointer. If it is, produce
1494 // a diagnostic and mark the designator as invalid.
1495 template <typename GenDiagType>
1496 bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) {
1497 if (Designator.Invalid)
1498 return false;
1499 if (IsNullPtr) {
1500 GenDiag();
1501 Designator.setInvalid();
1502 return false;
1503 }
1504 return true;
1505 }
1506
1507 public:
1508 bool checkNullPointer(EvalInfo &Info, const Expr *E,
1509 CheckSubobjectKind CSK) {
1510 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, CSK] {
1511 Info.CCEDiag(E, DiagId: diag::note_constexpr_null_subobject) << CSK;
1512 });
1513 }
1514
1515 bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E,
1516 AccessKinds AK) {
1517 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, AK] {
1518 if (AK == AccessKinds::AK_Dereference)
1519 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
1520 else
1521 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
1522 });
1523 }
1524
1525 // Check this LValue refers to an object. If not, set the designator to be
1526 // invalid and emit a diagnostic.
1527 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {
1528 return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) &&
1529 Designator.checkSubobject(Info, E, CSK);
1530 }
1531
1532 void addDecl(EvalInfo &Info, const Expr *E,
1533 const Decl *D, bool Virtual = false) {
1534 if (checkSubobject(Info, E, CSK: isa<FieldDecl>(Val: D) ? CSK_Field : CSK_Base))
1535 Designator.addDeclUnchecked(D, Virtual);
1536 }
1537 void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) {
1538 if (!Designator.Entries.empty()) {
1539 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsupported_unsized_array);
1540 Designator.setInvalid();
1541 return;
1542 }
1543 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer)) {
1544 assert(!Base || getType(Base).getNonReferenceType()->isPointerType() ||
1545 getType(Base).getNonReferenceType()->isArrayType());
1546 Designator.FirstEntryIsAnUnsizedArray = true;
1547 Designator.addUnsizedArrayUnchecked(ElemTy);
1548 }
1549 }
1550 void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {
1551 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer))
1552 Designator.addArrayUnchecked(CAT);
1553 }
1554 void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) {
1555 if (checkSubobject(Info, E, CSK: Imag ? CSK_Imag : CSK_Real))
1556 Designator.addComplexUnchecked(EltTy, Imag);
1557 }
1558 void addVectorElement(EvalInfo &Info, const Expr *E, QualType EltTy,
1559 uint64_t Size, uint64_t Idx) {
1560 if (checkSubobject(Info, E, CSK: CSK_VectorElement))
1561 Designator.addVectorElementUnchecked(EltTy, Size, Idx);
1562 }
1563 void clearIsNullPointer() {
1564 IsNullPtr = false;
1565 }
1566 void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E,
1567 const APSInt &Index, CharUnits ElementSize) {
1568 // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB,
1569 // but we're not required to diagnose it and it's valid in C++.)
1570 if (!Index)
1571 return;
1572
1573 // Compute the new offset in the appropriate width, wrapping at 64 bits.
1574 // FIXME: When compiling for a 32-bit target, we should use 32-bit
1575 // offsets.
1576 uint64_t Offset64 = Offset.getQuantity();
1577 uint64_t ElemSize64 = ElementSize.getQuantity();
1578 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
1579 Offset = CharUnits::fromQuantity(Quantity: Offset64 + ElemSize64 * Index64);
1580
1581 if (checkNullPointer(Info, E, CSK: CSK_ArrayIndex))
1582 Designator.adjustIndex(Info, E, N: Index, LV: *this);
1583 clearIsNullPointer();
1584 }
1585 void adjustOffset(CharUnits N) {
1586 Offset += N;
1587 if (N.getQuantity())
1588 clearIsNullPointer();
1589 }
1590 };
1591
1592 struct MemberPtr {
1593 MemberPtr() {}
1594 explicit MemberPtr(const ValueDecl *Decl)
1595 : DeclAndIsDerivedMember(Decl, false) {}
1596
1597 /// The member or (direct or indirect) field referred to by this member
1598 /// pointer, or 0 if this is a null member pointer.
1599 const ValueDecl *getDecl() const {
1600 return DeclAndIsDerivedMember.getPointer();
1601 }
1602 /// Is this actually a member of some type derived from the relevant class?
1603 bool isDerivedMember() const {
1604 return DeclAndIsDerivedMember.getInt();
1605 }
1606 /// Get the class which the declaration actually lives in.
1607 const CXXRecordDecl *getContainingRecord() const {
1608 return cast<CXXRecordDecl>(
1609 Val: DeclAndIsDerivedMember.getPointer()->getDeclContext());
1610 }
1611
1612 void moveInto(APValue &V) const {
1613 V = APValue(getDecl(), isDerivedMember(), Path);
1614 }
1615 void setFrom(const APValue &V) {
1616 assert(V.isMemberPointer());
1617 DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());
1618 DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());
1619 Path.clear();
1620 llvm::append_range(C&: Path, R: V.getMemberPointerPath());
1621 }
1622
1623 /// DeclAndIsDerivedMember - The member declaration, and a flag indicating
1624 /// whether the member is a member of some class derived from the class type
1625 /// of the member pointer.
1626 llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;
1627 /// Path - The path of base/derived classes from the member declaration's
1628 /// class (exclusive) to the class type of the member pointer (inclusive).
1629 SmallVector<const CXXRecordDecl*, 4> Path;
1630
1631 /// Perform a cast towards the class of the Decl (either up or down the
1632 /// hierarchy).
1633 bool castBack(const CXXRecordDecl *Class) {
1634 assert(!Path.empty());
1635 const CXXRecordDecl *Expected;
1636 if (Path.size() >= 2)
1637 Expected = Path[Path.size() - 2];
1638 else
1639 Expected = getContainingRecord();
1640 if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {
1641 // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
1642 // if B does not contain the original member and is not a base or
1643 // derived class of the class containing the original member, the result
1644 // of the cast is undefined.
1645 // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
1646 // (D::*). We consider that to be a language defect.
1647 return false;
1648 }
1649 Path.pop_back();
1650 return true;
1651 }
1652 /// Perform a base-to-derived member pointer cast.
1653 bool castToDerived(const CXXRecordDecl *Derived) {
1654 if (!getDecl())
1655 return true;
1656 if (!isDerivedMember()) {
1657 Path.push_back(Elt: Derived);
1658 return true;
1659 }
1660 if (!castBack(Class: Derived))
1661 return false;
1662 if (Path.empty())
1663 DeclAndIsDerivedMember.setInt(false);
1664 return true;
1665 }
1666 /// Perform a derived-to-base member pointer cast.
1667 bool castToBase(const CXXRecordDecl *Base) {
1668 if (!getDecl())
1669 return true;
1670 if (Path.empty())
1671 DeclAndIsDerivedMember.setInt(true);
1672 if (isDerivedMember()) {
1673 Path.push_back(Elt: Base);
1674 return true;
1675 }
1676 return castBack(Class: Base);
1677 }
1678 };
1679
1680 /// Compare two member pointers, which are assumed to be of the same type.
1681 static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {
1682 if (!LHS.getDecl() || !RHS.getDecl())
1683 return !LHS.getDecl() && !RHS.getDecl();
1684 if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())
1685 return false;
1686 return LHS.Path == RHS.Path;
1687 }
1688}
1689
1690void SubobjectDesignator::adjustIndex(EvalInfo &Info, const Expr *E, APSInt N,
1691 const LValue &LV) {
1692 if (Invalid || !N)
1693 return;
1694 uint64_t TruncatedN = N.extOrTrunc(width: 64).getZExtValue();
1695 if (isMostDerivedAnUnsizedArray()) {
1696 diagnoseUnsizedArrayPointerArithmetic(Info, E);
1697 // Can't verify -- trust that the user is doing the right thing (or if
1698 // not, trust that the caller will catch the bad behavior).
1699 // FIXME: Should we reject if this overflows, at least?
1700 Entries.back() =
1701 PathEntry::ArrayIndex(Index: Entries.back().getAsArrayIndex() + TruncatedN);
1702 return;
1703 }
1704
1705 // [expr.add]p4: For the purposes of these operators, a pointer to a
1706 // nonarray object behaves the same as a pointer to the first element of
1707 // an array of length one with the type of the object as its element type.
1708 bool IsArray =
1709 MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement;
1710 uint64_t ArrayIndex =
1711 IsArray ? Entries.back().getAsArrayIndex() : (uint64_t)IsOnePastTheEnd;
1712 uint64_t ArraySize = IsArray ? getMostDerivedArraySize() : (uint64_t)1;
1713
1714 if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) {
1715 if (!Info.checkingPotentialConstantExpression() ||
1716 !LV.AllowConstexprUnknown) {
1717 // Calculate the actual index in a wide enough type, so we can include
1718 // it in the note.
1719 N = N.extend(width: std::max<unsigned>(a: N.getBitWidth() + 1, b: 65));
1720 (llvm::APInt &)N += ArrayIndex;
1721 assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index");
1722 diagnosePointerArithmetic(Info, E, N);
1723 }
1724 setInvalid();
1725 return;
1726 }
1727
1728 ArrayIndex += TruncatedN;
1729 assert(ArrayIndex <= ArraySize &&
1730 "bounds check succeeded for out-of-bounds index");
1731
1732 if (IsArray)
1733 Entries.back() = PathEntry::ArrayIndex(Index: ArrayIndex);
1734 else
1735 IsOnePastTheEnd = (ArrayIndex != 0);
1736}
1737
1738static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E);
1739static bool EvaluateInPlace(APValue &Result, EvalInfo &Info,
1740 const LValue &This, const Expr *E,
1741 bool AllowNonLiteralTypes = false);
1742static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
1743 bool InvalidBaseOK = false);
1744static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info,
1745 bool InvalidBaseOK = false);
1746static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
1747 EvalInfo &Info);
1748static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info);
1749static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
1750static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
1751 EvalInfo &Info);
1752static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
1753static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
1754static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info);
1755static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
1756 EvalInfo &Info);
1757static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result);
1758static std::optional<uint64_t>
1759EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
1760 std::string *StringResult = nullptr);
1761
1762/// Evaluate an integer or fixed point expression into an APResult.
1763static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
1764 EvalInfo &Info);
1765
1766/// Evaluate only a fixed point expression into an APResult.
1767static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
1768 EvalInfo &Info);
1769
1770//===----------------------------------------------------------------------===//
1771// Misc utilities
1772//===----------------------------------------------------------------------===//
1773
1774/// Negate an APSInt in place, converting it to a signed form if necessary, and
1775/// preserving its value (by extending by up to one bit as needed).
1776static void negateAsSigned(APSInt &Int) {
1777 if (Int.isUnsigned() || Int.isMinSignedValue()) {
1778 Int = Int.extend(width: Int.getBitWidth() + 1);
1779 Int.setIsSigned(true);
1780 }
1781 Int = -Int;
1782}
1783
1784template<typename KeyT>
1785APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T,
1786 ScopeKind Scope, LValue &LV) {
1787 unsigned Version = getTempVersion();
1788 APValue::LValueBase Base(Key, Index, Version);
1789 LV.set(B: Base);
1790 return createLocal(Base, Key, T, Scope);
1791}
1792
1793/// Allocate storage for a parameter of a function call made in this frame.
1794APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD,
1795 LValue &LV) {
1796 assert(Args.CallIndex == Index && "creating parameter in wrong frame");
1797 APValue::LValueBase Base(PVD, Index, Args.Version);
1798 LV.set(B: Base);
1799 // We always destroy parameters at the end of the call, even if we'd allow
1800 // them to live to the end of the full-expression at runtime, in order to
1801 // give portable results and match other compilers.
1802 return createLocal(Base, Key: PVD, T: PVD->getType(), Scope: ScopeKind::Call);
1803}
1804
1805APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key,
1806 QualType T, ScopeKind Scope) {
1807 assert(Base.getCallIndex() == Index && "lvalue for wrong frame");
1808 unsigned Version = Base.getVersion();
1809 APValue &Result = Temporaries[MapKeyTy(Key, Version)];
1810 assert(Result.isAbsent() && "local created multiple times");
1811
1812 // If we're creating a local immediately in the operand of a speculative
1813 // evaluation, don't register a cleanup to be run outside the speculative
1814 // evaluation context, since we won't actually be able to initialize this
1815 // object.
1816 if (Index <= Info.SpeculativeEvaluationDepth) {
1817 if (T.isDestructedType())
1818 Info.noteSideEffect();
1819 } else {
1820 Info.CleanupStack.push_back(Elt: Cleanup(&Result, Base, T, Scope));
1821 }
1822 return Result;
1823}
1824
1825CharUnits clang::GetAlignOfDynamicAlloc(const ASTContext &Ctx,
1826 QualType AllocType,
1827 DynAllocKind AllocKind) {
1828 assert((AllocKind != DynAllocKind::None) &&
1829 "should only be called on dynamically allocated blocks");
1830 assert((AllocKind != DynAllocKind::BuiltinOperatorNew) &&
1831 "__builtin_operator_new should have been allowed only from "
1832 "std::allocator::allocate");
1833
1834 const TargetInfo &TI = Ctx.getTargetInfo();
1835 uint64_t DefaultNewAlign = TI.getNewAlign();
1836
1837 uint64_t TypeAlignment = Ctx.getTypeAlign(T: AllocType);
1838 assert(TypeAlignment > 0 && "Unknown alignment for allocated type!");
1839
1840 uint64_t AllocSize = Ctx.getTypeSize(T: AllocType);
1841
1842 if (AllocSize == 0) {
1843 switch (AllocKind) {
1844 // Allocating a zero-sized array is allowed, however it doesn't have
1845 // any alignment guarantees.
1846 case DynAllocKind::ArrayNew:
1847 case DynAllocKind::StdAllocator:
1848 return CharUnits::One();
1849
1850 // Flexible array members are allowed as only member as an extension.
1851 // In this case the size of the type will be zero, but the allocation
1852 // should still be suitable for the array element type.
1853 case DynAllocKind::New:
1854 return Ctx.toCharUnitsFromBits(BitSize: TypeAlignment);
1855
1856 default:
1857 llvm_unreachable("Unhandled DynAllocKind");
1858 }
1859 }
1860
1861 assert(TypeAlignment <= AllocSize && "Invalid alignment/size for type!");
1862 assert(AllocSize % TypeAlignment == 0 && "Invalid alignment/size for type!");
1863
1864 // For new-extended alignment the ::operator new overload with
1865 // std::align_val_t parameter is used. According to C++
1866 // [basic.stc.dynamic.allocation]p3.1 this overload returns memory
1867 // according to the requested alignment. No stricter guarantees are
1868 // made.
1869 if (TypeAlignment > DefaultNewAlign)
1870 return Ctx.toCharUnitsFromBits(BitSize: TypeAlignment);
1871
1872 switch (AllocKind) {
1873 // According to C++ [allocator.members]p5 it is unspecified how the
1874 // memory obtained from ::operator new is used by
1875 // std::allocator::allocate, therefore be conservative here.
1876 case DynAllocKind::StdAllocator:
1877 return Ctx.toCharUnitsFromBits(BitSize: TypeAlignment);
1878
1879 // The non-array form of new does not permit allocation overhead and
1880 // therefore provides alignment as guaranteed by ::operator new.
1881 // According to C++ [basic.stc.dynamic.allocation]p3.3 the allocation
1882 // is suitably aligned for all objects without new-extended alignment
1883 // with the exact size of the allocation. An object of the exact size
1884 // AllocSize can have alignment of at most the lowest bit set in
1885 // AllocSize.
1886 case DynAllocKind::New:
1887 return Ctx.toCharUnitsFromBits(
1888 BitSize: std::min(a: DefaultNewAlign, b: uint64_t(1) << llvm::countr_zero(Val: AllocSize)));
1889
1890 case DynAllocKind::ArrayNew: {
1891 const Type *ET = AllocType.getTypePtr()
1892 ->getArrayElementTypeNoTypeQual()
1893 ->getCanonicalTypeUnqualified()
1894 .getTypePtr();
1895 // According to C++ [expr.new]p17, unless the element type of an
1896 // array new expression is char, unsigned char or std::byte, the
1897 // allocation may be offset into the allocation returned by
1898 // ::operator new[]. Therefore no stricter alignment than the type's
1899 // alignment is guaranteed. For char, unsigned char and std::byte
1900 if (!ET->isSpecificBuiltinType(K: BuiltinType::UChar) &&
1901 !ET->isSpecificBuiltinType(K: BuiltinType::Char_U) &&
1902 !ET->isSpecificBuiltinType(K: BuiltinType::Char_S) && !ET->isStdByteType())
1903 return Ctx.toCharUnitsFromBits(BitSize: TypeAlignment);
1904
1905 // Otherwise, the allocation is offset from the result of ::operator
1906 // new[] by a multiple of the strictest fundamental alignment.
1907 // According C++ [basic.stc.dynamic.allocation]p3.2 the allocation
1908 // returned by ::operator new[] is suitably aligned for all objects
1909 // without new-extended alignment and size up to the allocated size.
1910 uint64_t MaxFundamentalAlign =
1911 std::max(a: TI.getLongLongAlign(), b: TI.getLongDoubleAlign());
1912 return Ctx.toCharUnitsFromBits(BitSize: std::min(
1913 l: {DefaultNewAlign, MaxFundamentalAlign, llvm::bit_floor(Value: AllocSize)}));
1914 }
1915
1916 default:
1917 llvm_unreachable("Unhandled DynAllocKind");
1918 }
1919}
1920
1921APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) {
1922 if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) {
1923 FFDiag(E, DiagId: diag::note_constexpr_heap_alloc_limit_exceeded);
1924 return nullptr;
1925 }
1926
1927 DynamicAllocLValue DA(NumHeapAllocs++, DynAlloc::kindOfExpr(AllocExpr: E));
1928 LV.set(B: APValue::LValueBase::getDynamicAlloc(LV: DA, Type: T));
1929 auto Result = HeapAllocs.emplace(args: std::piecewise_construct,
1930 args: std::forward_as_tuple(args&: DA), args: std::tuple<>());
1931 assert(Result.second && "reused a heap alloc index?");
1932 Result.first->second.AllocExpr = E;
1933 return &Result.first->second.Value;
1934}
1935
1936/// Produce a string describing the given constexpr call.
1937void CallStackFrame::describe(raw_ostream &Out) const {
1938 bool IsMemberCall = false;
1939 bool ExplicitInstanceParam = false;
1940 clang::PrintingPolicy PrintingPolicy = Info.Ctx.getPrintingPolicy();
1941 PrintingPolicy.SuppressLambdaBody = true;
1942
1943 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: Callee)) {
1944 IsMemberCall = !isa<CXXConstructorDecl>(Val: MD) && !MD->isStatic();
1945 ExplicitInstanceParam = MD->isExplicitObjectMemberFunction();
1946 }
1947
1948 if (!IsMemberCall)
1949 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1950 /*Qualified=*/false);
1951
1952 if (This && IsMemberCall) {
1953 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(Val: CallExpr)) {
1954 const Expr *Object = MCE->getImplicitObjectArgument();
1955 Object->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1956 /*Indentation=*/0);
1957 if (Object->getType()->isPointerType())
1958 Out << "->";
1959 else
1960 Out << ".";
1961 } else if (const auto *OCE =
1962 dyn_cast_if_present<CXXOperatorCallExpr>(Val: CallExpr)) {
1963 OCE->getArg(Arg: 0)->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1964 /*Indentation=*/0);
1965 Out << ".";
1966 } else {
1967 APValue Val;
1968 This->moveInto(V&: Val);
1969 Val.printPretty(
1970 OS&: Out, Ctx: Info.Ctx,
1971 Ty: Info.Ctx.getLValueReferenceType(T: This->Designator.MostDerivedType));
1972 Out << ".";
1973 }
1974 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1975 /*Qualified=*/false);
1976 }
1977
1978 Out << '(';
1979
1980 llvm::ListSeparator Comma;
1981 for (const ParmVarDecl *Param :
1982 Callee->parameters().slice(N: ExplicitInstanceParam)) {
1983 Out << Comma;
1984 const APValue *V = Info.getParamSlot(Call: Arguments, PVD: Param);
1985 if (V)
1986 V->printPretty(OS&: Out, Ctx: Info.Ctx, Ty: Param->getType());
1987 else
1988 Out << "<...>";
1989 }
1990
1991 Out << ')';
1992}
1993
1994/// Evaluate an expression to see if it had side-effects, and discard its
1995/// result.
1996/// \return \c true if the caller should keep evaluating.
1997static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) {
1998 assert(!E->isValueDependent());
1999 APValue Scratch;
2000 if (!Evaluate(Result&: Scratch, Info, E))
2001 // We don't need the value, but we might have skipped a side effect here.
2002 return Info.noteSideEffect();
2003 return true;
2004}
2005
2006static bool IsOpaqueConstantCall(const LValue &LVal) {
2007 const auto *BaseExpr =
2008 llvm::dyn_cast_if_present<CallExpr>(Val: LVal.Base.dyn_cast<const Expr *>());
2009 return BaseExpr && isOpaqueConstantCall(E: BaseExpr);
2010}
2011
2012static bool IsGlobalLValue(APValue::LValueBase B) {
2013 if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>())
2014 return true;
2015
2016 return isGlobalLValue(D: B.dyn_cast<const ValueDecl *>(),
2017 E: B.dyn_cast<const Expr *>());
2018}
2019
2020bool isGlobalLValue(const ValueDecl *D, const Expr *E) {
2021 // C++11 [expr.const]p3 An address constant expression is a prvalue core
2022 // constant expression of pointer type that evaluates to...
2023
2024 // ... a null pointer value, or a prvalue core constant expression of type
2025 // std::nullptr_t.
2026 if (!D && !E)
2027 return true;
2028
2029 if (D) {
2030 // ... the address of an object with static storage duration,
2031 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
2032 return VD->hasGlobalStorage();
2033 if (isa<TemplateParamObjectDecl>(Val: D))
2034 return true;
2035 // ... the address of a function,
2036 // ... the address of a GUID [MS extension],
2037 // ... the address of an unnamed global constant
2038 return isa<FunctionDecl, MSGuidDecl, UnnamedGlobalConstantDecl>(Val: D);
2039 }
2040
2041 assert(E);
2042
2043 switch (E->getStmtClass()) {
2044 default:
2045 return false;
2046 case Expr::CompoundLiteralExprClass: {
2047 const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(Val: E);
2048 return CLE->isFileScope() && CLE->isLValue();
2049 }
2050 case Expr::MaterializeTemporaryExprClass:
2051 // A materialized temporary might have been lifetime-extended to static
2052 // storage duration.
2053 return cast<MaterializeTemporaryExpr>(Val: E)->getStorageDuration() == SD_Static;
2054 // A string literal has static storage duration.
2055 case Expr::StringLiteralClass:
2056 case Expr::PredefinedExprClass:
2057 case Expr::ObjCStringLiteralClass:
2058 case Expr::ObjCEncodeExprClass:
2059 return true;
2060 case Expr::ObjCBoxedExprClass:
2061 case Expr::ObjCArrayLiteralClass:
2062 case Expr::ObjCDictionaryLiteralClass:
2063 return cast<ObjCObjectLiteral>(Val: E)->isExpressibleAsConstantInitializer();
2064 case Expr::CallExprClass:
2065 return isOpaqueConstantCall(E: cast<CallExpr>(Val: E));
2066 // For GCC compatibility, &&label has static storage duration.
2067 case Expr::AddrLabelExprClass:
2068 return true;
2069 // A Block literal expression may be used as the initialization value for
2070 // Block variables at global or local static scope.
2071 case Expr::BlockExprClass:
2072 return !cast<BlockExpr>(Val: E)->getBlockDecl()->hasCaptures();
2073 // The APValue generated from a __builtin_source_location will be emitted as a
2074 // literal.
2075 case Expr::SourceLocExprClass:
2076 return true;
2077 case Expr::ImplicitValueInitExprClass:
2078 // FIXME:
2079 // We can never form an lvalue with an implicit value initialization as its
2080 // base through expression evaluation, so these only appear in one case: the
2081 // implicit variable declaration we invent when checking whether a constexpr
2082 // constructor can produce a constant expression. We must assume that such
2083 // an expression might be a global lvalue.
2084 return true;
2085 }
2086
2087 llvm_unreachable("Unhandled stmt kind in switch with default?");
2088}
2089
2090static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
2091 return LVal.Base.dyn_cast<const ValueDecl*>();
2092}
2093
2094// Information about an LValueBase that is some kind of string.
2095struct LValueBaseString {
2096 std::string ObjCEncodeStorage;
2097 StringRef Bytes;
2098 int CharWidth;
2099};
2100
2101// Gets the lvalue base of LVal as a string.
2102static bool GetLValueBaseAsString(const EvalInfo &Info, const LValue &LVal,
2103 LValueBaseString &AsString) {
2104 const auto *BaseExpr = LVal.Base.dyn_cast<const Expr *>();
2105 if (!BaseExpr)
2106 return false;
2107
2108 // For ObjCEncodeExpr, we need to compute and store the string.
2109 if (const auto *EE = dyn_cast<ObjCEncodeExpr>(Val: BaseExpr)) {
2110 Info.Ctx.getObjCEncodingForType(T: EE->getEncodedType(),
2111 S&: AsString.ObjCEncodeStorage);
2112 AsString.Bytes = AsString.ObjCEncodeStorage;
2113 AsString.CharWidth = 1;
2114 return true;
2115 }
2116
2117 // Otherwise, we have a StringLiteral.
2118 const auto *Lit = dyn_cast<StringLiteral>(Val: BaseExpr);
2119 if (const auto *PE = dyn_cast<PredefinedExpr>(Val: BaseExpr))
2120 Lit = PE->getFunctionName();
2121
2122 if (!Lit)
2123 return false;
2124
2125 AsString.Bytes = Lit->getBytes();
2126 AsString.CharWidth = Lit->getCharByteWidth();
2127 return true;
2128}
2129
2130// Determine whether two string literals potentially overlap. This will be the
2131// case if they agree on the values of all the bytes on the overlapping region
2132// between them.
2133//
2134// The overlapping region is the portion of the two string literals that must
2135// overlap in memory if the pointers actually point to the same address at
2136// runtime. For example, if LHS is "abcdef" + 3 and RHS is "cdef\0gh" + 1 then
2137// the overlapping region is "cdef\0", which in this case does agree, so the
2138// strings are potentially overlapping. Conversely, for "foobar" + 3 versus
2139// "bazbar" + 3, the overlapping region contains all of both strings, so they
2140// are not potentially overlapping, even though they agree from the given
2141// addresses onwards.
2142//
2143// See open core issue CWG2765 which is discussing the desired rule here.
2144static bool ArePotentiallyOverlappingStringLiterals(const EvalInfo &Info,
2145 const LValue &LHS,
2146 const LValue &RHS) {
2147 LValueBaseString LHSString, RHSString;
2148 if (!GetLValueBaseAsString(Info, LVal: LHS, AsString&: LHSString) ||
2149 !GetLValueBaseAsString(Info, LVal: RHS, AsString&: RHSString))
2150 return false;
2151
2152 // This is the byte offset to the location of the first character of LHS
2153 // within RHS. We don't need to look at the characters of one string that
2154 // would appear before the start of the other string if they were merged.
2155 CharUnits Offset = RHS.Offset - LHS.Offset;
2156 if (Offset.isNegative()) {
2157 if (LHSString.Bytes.size() < (size_t)-Offset.getQuantity())
2158 return false;
2159 LHSString.Bytes = LHSString.Bytes.drop_front(N: -Offset.getQuantity());
2160 } else {
2161 if (RHSString.Bytes.size() < (size_t)Offset.getQuantity())
2162 return false;
2163 RHSString.Bytes = RHSString.Bytes.drop_front(N: Offset.getQuantity());
2164 }
2165
2166 bool LHSIsLonger = LHSString.Bytes.size() > RHSString.Bytes.size();
2167 StringRef Longer = LHSIsLonger ? LHSString.Bytes : RHSString.Bytes;
2168 StringRef Shorter = LHSIsLonger ? RHSString.Bytes : LHSString.Bytes;
2169 int ShorterCharWidth = (LHSIsLonger ? RHSString : LHSString).CharWidth;
2170
2171 // The null terminator isn't included in the string data, so check for it
2172 // manually. If the longer string doesn't have a null terminator where the
2173 // shorter string ends, they aren't potentially overlapping.
2174 for (int NullByte : llvm::seq(Size: ShorterCharWidth)) {
2175 if (Shorter.size() + NullByte >= Longer.size())
2176 break;
2177 if (Longer[Shorter.size() + NullByte])
2178 return false;
2179 }
2180
2181 // Otherwise, they're potentially overlapping if and only if the overlapping
2182 // region is the same.
2183 return Shorter == Longer.take_front(N: Shorter.size());
2184}
2185
2186static bool IsWeakLValue(const LValue &Value) {
2187 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2188 return Decl && Decl->isWeak();
2189}
2190
2191static bool isZeroSized(const LValue &Value) {
2192 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2193 if (isa_and_nonnull<VarDecl>(Val: Decl)) {
2194 QualType Ty = Decl->getType();
2195 if (Ty->isArrayType())
2196 return Ty->isIncompleteType() ||
2197 Decl->getASTContext().getTypeSize(T: Ty) == 0;
2198 }
2199 return false;
2200}
2201
2202static bool HasSameBase(const LValue &A, const LValue &B) {
2203 if (!A.getLValueBase())
2204 return !B.getLValueBase();
2205 if (!B.getLValueBase())
2206 return false;
2207
2208 if (A.getLValueBase().getOpaqueValue() !=
2209 B.getLValueBase().getOpaqueValue())
2210 return false;
2211
2212 return A.getLValueCallIndex() == B.getLValueCallIndex() &&
2213 A.getLValueVersion() == B.getLValueVersion();
2214}
2215
2216static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) {
2217 assert(Base && "no location for a null lvalue");
2218 const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
2219
2220 // For a parameter, find the corresponding call stack frame (if it still
2221 // exists), and point at the parameter of the function definition we actually
2222 // invoked.
2223 if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(Val: VD)) {
2224 unsigned Idx = PVD->getFunctionScopeIndex();
2225 for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) {
2226 if (F->Arguments.CallIndex == Base.getCallIndex() &&
2227 F->Arguments.Version == Base.getVersion() && F->Callee &&
2228 Idx < F->Callee->getNumParams()) {
2229 VD = F->Callee->getParamDecl(i: Idx);
2230 break;
2231 }
2232 }
2233 }
2234
2235 if (VD)
2236 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
2237 else if (const Expr *E = Base.dyn_cast<const Expr*>())
2238 Info.Note(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
2239 else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
2240 // FIXME: Produce a note for dangling pointers too.
2241 if (std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA))
2242 Info.Note(Loc: (*Alloc)->AllocExpr->getExprLoc(),
2243 DiagId: diag::note_constexpr_dynamic_alloc_here);
2244 }
2245
2246 // We have no information to show for a typeid(T) object.
2247}
2248
2249enum class CheckEvaluationResultKind {
2250 ConstantExpression,
2251 FullyInitialized,
2252};
2253
2254/// Materialized temporaries that we've already checked to determine if they're
2255/// initializsed by a constant expression.
2256using CheckedTemporaries =
2257 llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>;
2258
2259static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2260 EvalInfo &Info, SourceLocation DiagLoc,
2261 QualType Type, const APValue &Value,
2262 ConstantExprKind Kind,
2263 const FieldDecl *SubobjectDecl,
2264 CheckedTemporaries &CheckedTemps,
2265 bool IsCompleteClass = true);
2266
2267/// Check that this reference or pointer core constant expression is a valid
2268/// value for an address or reference constant expression. Return true if we
2269/// can fold this expression, whether or not it's a constant expression.
2270static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
2271 QualType Type, const LValue &LVal,
2272 ConstantExprKind Kind,
2273 CheckedTemporaries &CheckedTemps) {
2274 bool IsReferenceType = Type->isReferenceType();
2275
2276 APValue::LValueBase Base = LVal.getLValueBase();
2277 const SubobjectDesignator &Designator = LVal.getLValueDesignator();
2278
2279 const Expr *BaseE = Base.dyn_cast<const Expr *>();
2280 const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>();
2281
2282 // Additional restrictions apply in a template argument. We only enforce the
2283 // C++20 restrictions here; additional syntactic and semantic restrictions
2284 // are applied elsewhere.
2285 if (isTemplateArgument(Kind)) {
2286 int InvalidBaseKind = -1;
2287 StringRef Ident;
2288 if (Base.is<TypeInfoLValue>())
2289 InvalidBaseKind = 0;
2290 else if (isa_and_nonnull<StringLiteral>(Val: BaseE))
2291 InvalidBaseKind = 1;
2292 else if (isa_and_nonnull<MaterializeTemporaryExpr>(Val: BaseE) ||
2293 isa_and_nonnull<LifetimeExtendedTemporaryDecl>(Val: BaseVD))
2294 InvalidBaseKind = 2;
2295 else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(Val: BaseE)) {
2296 InvalidBaseKind = 3;
2297 Ident = PE->getIdentKindName();
2298 }
2299
2300 if (InvalidBaseKind != -1) {
2301 Info.FFDiag(Loc, DiagId: diag::note_constexpr_invalid_template_arg)
2302 << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind
2303 << Ident;
2304 return false;
2305 }
2306 }
2307
2308 if (auto *FD = dyn_cast_or_null<FunctionDecl>(Val: BaseVD);
2309 FD && FD->isImmediateFunction()) {
2310 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible)
2311 << !Type->isAnyPointerType();
2312 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2313 return false;
2314 }
2315
2316 // Check that the object is a global. Note that the fake 'this' object we
2317 // manufacture when checking potential constant expressions is conservatively
2318 // assumed to be global here.
2319 if (!IsGlobalLValue(B: Base)) {
2320 if (Info.getLangOpts().CPlusPlus11) {
2321 Info.FFDiag(Loc, DiagId: diag::note_constexpr_non_global, ExtraNotes: 1)
2322 << IsReferenceType << !Designator.Entries.empty() << !!BaseVD
2323 << BaseVD;
2324 auto *VarD = dyn_cast_or_null<VarDecl>(Val: BaseVD);
2325 if (VarD && VarD->isConstexpr()) {
2326 // Non-static local constexpr variables have unintuitive semantics:
2327 // constexpr int a = 1;
2328 // constexpr const int *p = &a;
2329 // ... is invalid because the address of 'a' is not constant. Suggest
2330 // adding a 'static' in this case.
2331 Info.Note(Loc: VarD->getLocation(), DiagId: diag::note_constexpr_not_static)
2332 << VarD
2333 << FixItHint::CreateInsertion(InsertionLoc: VarD->getBeginLoc(), Code: "static ");
2334 } else {
2335 NoteLValueLocation(Info, Base);
2336 }
2337 } else {
2338 Info.FFDiag(Loc);
2339 }
2340 // Don't allow references to temporaries to escape.
2341 return false;
2342 }
2343 assert((Info.checkingPotentialConstantExpression() ||
2344 LVal.getLValueCallIndex() == 0) &&
2345 "have call index for global lvalue");
2346
2347 if (LVal.allowConstexprUnknown()) {
2348 if (BaseVD) {
2349 Info.FFDiag(Loc, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << BaseVD;
2350 NoteLValueLocation(Info, Base);
2351 } else {
2352 Info.FFDiag(Loc);
2353 }
2354 return false;
2355 }
2356
2357 if (Base.is<DynamicAllocLValue>()) {
2358 Info.FFDiag(Loc, DiagId: diag::note_constexpr_dynamic_alloc)
2359 << IsReferenceType << !Designator.Entries.empty();
2360 NoteLValueLocation(Info, Base);
2361 return false;
2362 }
2363
2364 if (BaseVD) {
2365 if (const VarDecl *Var = dyn_cast<const VarDecl>(Val: BaseVD)) {
2366 // Check if this is a thread-local variable.
2367 if (Var->getTLSKind())
2368 // FIXME: Diagnostic!
2369 return false;
2370
2371 // A dllimport variable never acts like a constant, unless we're
2372 // evaluating a value for use only in name mangling, and unless it's a
2373 // static local. For the latter case, we'd still need to evaluate the
2374 // constant expression in case we're inside a (inlined) function.
2375 if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>() &&
2376 !Var->isStaticLocal())
2377 return false;
2378
2379 // Address of a managed variable is never a constant expression.
2380 if (Info.getLangOpts().CUDA && Var->hasAttr<HIPManagedAttr>())
2381 return false;
2382
2383 // In CUDA/HIP device compilation, only device side variables have
2384 // constant addresses.
2385 if (Info.getLangOpts().CUDA && Info.getLangOpts().CUDAIsDevice &&
2386 Info.Ctx.CUDAConstantEvalCtx.NoWrongSidedVars) {
2387 if ((!Var->hasAttr<CUDADeviceAttr>() &&
2388 !Var->hasAttr<CUDAConstantAttr>() &&
2389 !Var->getType()->isCUDADeviceBuiltinSurfaceType() &&
2390 !Var->getType()->isCUDADeviceBuiltinTextureType()))
2391 return false;
2392 }
2393 }
2394 if (const auto *FD = dyn_cast<const FunctionDecl>(Val: BaseVD)) {
2395 // __declspec(dllimport) must be handled very carefully:
2396 // We must never initialize an expression with the thunk in C++.
2397 // Doing otherwise would allow the same id-expression to yield
2398 // different addresses for the same function in different translation
2399 // units. However, this means that we must dynamically initialize the
2400 // expression with the contents of the import address table at runtime.
2401 //
2402 // The C language has no notion of ODR; furthermore, it has no notion of
2403 // dynamic initialization. This means that we are permitted to
2404 // perform initialization with the address of the thunk.
2405 if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) &&
2406 FD->hasAttr<DLLImportAttr>())
2407 // FIXME: Diagnostic!
2408 return false;
2409 }
2410 } else if (const auto *MTE =
2411 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: BaseE)) {
2412 if (CheckedTemps.insert(Ptr: MTE).second) {
2413 QualType TempType = getType(B: Base);
2414 if (TempType.isDestructedType()) {
2415 Info.FFDiag(Loc: MTE->getExprLoc(),
2416 DiagId: diag::note_constexpr_unsupported_temporary_nontrivial_dtor)
2417 << TempType;
2418 return false;
2419 }
2420
2421 APValue *V = MTE->getOrCreateValue(MayCreate: false);
2422 assert(V && "evasluation result refers to uninitialised temporary");
2423 if (!CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2424 Info, DiagLoc: MTE->getExprLoc(), Type: TempType, Value: *V, Kind,
2425 /*SubobjectDecl=*/nullptr, CheckedTemps))
2426 return false;
2427 }
2428 }
2429
2430 // Allow address constant expressions to be past-the-end pointers. This is
2431 // an extension: the standard requires them to point to an object.
2432 if (!IsReferenceType)
2433 return true;
2434
2435 // A reference constant expression must refer to an object.
2436 if (!Base) {
2437 // FIXME: diagnostic
2438 Info.CCEDiag(Loc);
2439 return true;
2440 }
2441
2442 // Does this refer one past the end of some object?
2443 if (!Designator.Invalid && Designator.isOnePastTheEnd()) {
2444 Info.FFDiag(Loc, DiagId: diag::note_constexpr_past_end, ExtraNotes: 1)
2445 << !Designator.Entries.empty() << !!BaseVD << BaseVD;
2446 NoteLValueLocation(Info, Base);
2447 }
2448
2449 return true;
2450}
2451
2452/// Member pointers are constant expressions unless they point to a
2453/// non-virtual dllimport member function.
2454static bool CheckMemberPointerConstantExpression(EvalInfo &Info,
2455 SourceLocation Loc,
2456 QualType Type,
2457 const APValue &Value,
2458 ConstantExprKind Kind) {
2459 const ValueDecl *Member = Value.getMemberPointerDecl();
2460 const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Val: Member);
2461 if (!FD)
2462 return true;
2463 if (FD->isImmediateFunction()) {
2464 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible) << /*pointer*/ 0;
2465 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2466 return false;
2467 }
2468 return isForManglingOnly(Kind) || FD->isVirtual() ||
2469 !FD->hasAttr<DLLImportAttr>();
2470}
2471
2472/// Check that this core constant expression is of literal type, and if not,
2473/// produce an appropriate diagnostic.
2474static bool CheckLiteralType(EvalInfo &Info, const Expr *E,
2475 const LValue *This = nullptr) {
2476 // The restriction to literal types does not exist in C++23 anymore.
2477 if (Info.getLangOpts().CPlusPlus23)
2478 return true;
2479
2480 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Info.Ctx))
2481 return true;
2482
2483 // C++1y: A constant initializer for an object o [...] may also invoke
2484 // constexpr constructors for o and its subobjects even if those objects
2485 // are of non-literal class types.
2486 //
2487 // C++11 missed this detail for aggregates, so classes like this:
2488 // struct foo_t { union { int i; volatile int j; } u; };
2489 // are not (obviously) initializable like so:
2490 // __attribute__((__require_constant_initialization__))
2491 // static const foo_t x = {{0}};
2492 // because "i" is a subobject with non-literal initialization (due to the
2493 // volatile member of the union). See:
2494 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677
2495 // Therefore, we use the C++1y behavior.
2496 if (This && Info.EvaluatingDecl == This->getLValueBase())
2497 return true;
2498
2499 // Prvalue constant expressions must be of literal types.
2500 if (Info.getLangOpts().CPlusPlus11)
2501 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral)
2502 << E->getType();
2503 else
2504 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
2505 return false;
2506}
2507
2508static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2509 EvalInfo &Info, SourceLocation DiagLoc,
2510 QualType Type, const APValue &Value,
2511 ConstantExprKind Kind,
2512 const FieldDecl *SubobjectDecl,
2513 CheckedTemporaries &CheckedTemps,
2514 bool IsCompleteClass) {
2515 if (!Value.hasValue()) {
2516 if (SubobjectDecl) {
2517 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2518 << /*(name)*/ 1 << SubobjectDecl;
2519 Info.Note(Loc: SubobjectDecl->getLocation(),
2520 DiagId: diag::note_constexpr_subobject_declared_here);
2521 } else {
2522 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2523 << /*of type*/ 0 << Type;
2524 }
2525 return false;
2526 }
2527
2528 // We allow _Atomic(T) to be initialized from anything that T can be
2529 // initialized from.
2530 if (const AtomicType *AT = Type->getAs<AtomicType>())
2531 Type = AT->getValueType();
2532
2533 // Core issue 1454: For a literal constant expression of array or class type,
2534 // each subobject of its value shall have been initialized by a constant
2535 // expression.
2536 if (Value.isArray()) {
2537 QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();
2538 for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {
2539 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2540 Value: Value.getArrayInitializedElt(I), Kind,
2541 SubobjectDecl, CheckedTemps))
2542 return false;
2543 }
2544 if (!Value.hasArrayFiller())
2545 return true;
2546 return CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2547 Value: Value.getArrayFiller(), Kind, SubobjectDecl,
2548 CheckedTemps);
2549 }
2550 if (Value.isUnion() && Value.getUnionField()) {
2551 return CheckEvaluationResult(
2552 CERK, Info, DiagLoc, Type: Value.getUnionField()->getType(),
2553 Value: Value.getUnionValue(), Kind, SubobjectDecl: Value.getUnionField(), CheckedTemps);
2554 }
2555 if (Value.isStruct()) {
2556 auto *RD = Type->castAsRecordDecl();
2557 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2558 unsigned BaseIndex = 0;
2559 for (const CXXBaseSpecifier &BS : CD->bases()) {
2560 if (BS.isVirtual())
2561 continue;
2562 const APValue &BaseValue = Value.getStructBase(i: BaseIndex);
2563 if (!BaseValue.hasValue()) {
2564 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2565 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2566 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2567 return false;
2568 }
2569 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(), Value: BaseValue,
2570 Kind, /*SubobjectDecl=*/nullptr,
2571 CheckedTemps, /*IsCompleteClass=*/false))
2572 return false;
2573 ++BaseIndex;
2574 }
2575 }
2576 for (const auto *I : RD->fields()) {
2577 if (I->isUnnamedBitField())
2578 continue;
2579
2580 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: I->getType(),
2581 Value: Value.getStructField(i: I->getFieldIndex()), Kind,
2582 SubobjectDecl: I, CheckedTemps))
2583 return false;
2584 }
2585
2586 if (IsCompleteClass) {
2587 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2588 unsigned BaseIndex = 0;
2589 for (const CXXBaseSpecifier &BS : CD->vbases()) {
2590 assert(BS.isVirtual());
2591 const APValue &BaseValue = Value.getStructVirtualBase(i: BaseIndex);
2592 if (!BaseValue.hasValue()) {
2593 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2594 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2595 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2596 return false;
2597 }
2598 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(),
2599 Value: BaseValue, Kind, /*SubobjectDecl=*/nullptr,
2600 CheckedTemps, /*IsCompleteClass=*/false))
2601 return false;
2602 ++BaseIndex;
2603 }
2604 }
2605 }
2606 }
2607
2608 if (Value.isLValue() &&
2609 CERK == CheckEvaluationResultKind::ConstantExpression) {
2610 LValue LVal;
2611 LVal.setFrom(Ctx: Info.Ctx, V: Value);
2612 return CheckLValueConstantExpression(Info, Loc: DiagLoc, Type, LVal, Kind,
2613 CheckedTemps);
2614 }
2615
2616 if (Value.isMemberPointer() &&
2617 CERK == CheckEvaluationResultKind::ConstantExpression)
2618 return CheckMemberPointerConstantExpression(Info, Loc: DiagLoc, Type, Value, Kind);
2619
2620 // Everything else is fine.
2621 return true;
2622}
2623
2624/// Check that this core constant expression value is a valid value for a
2625/// constant expression. If not, report an appropriate diagnostic. Does not
2626/// check that the expression is of literal type.
2627static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
2628 QualType Type, const APValue &Value,
2629 ConstantExprKind Kind) {
2630 // Nothing to check for a constant expression of type 'cv void'.
2631 if (Type->isVoidType())
2632 return true;
2633
2634 CheckedTemporaries CheckedTemps;
2635 return CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2636 Info, DiagLoc, Type, Value, Kind,
2637 /*SubobjectDecl=*/nullptr, CheckedTemps);
2638}
2639
2640/// Check that this evaluated value is fully-initialized and can be loaded by
2641/// an lvalue-to-rvalue conversion.
2642static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc,
2643 QualType Type, const APValue &Value) {
2644 CheckedTemporaries CheckedTemps;
2645 return CheckEvaluationResult(
2646 CERK: CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value,
2647 Kind: ConstantExprKind::Normal, /*SubobjectDecl=*/nullptr, CheckedTemps);
2648}
2649
2650/// Enforce C++2a [expr.const]/4.17, which disallows new-expressions unless
2651/// "the allocated storage is deallocated within the evaluation".
2652static bool CheckMemoryLeaks(EvalInfo &Info) {
2653 if (!Info.HeapAllocs.empty()) {
2654 // We can still fold to a constant despite a compile-time memory leak,
2655 // so long as the heap allocation isn't referenced in the result (we check
2656 // that in CheckConstantExpression).
2657 Info.CCEDiag(E: Info.HeapAllocs.begin()->second.AllocExpr,
2658 DiagId: diag::note_constexpr_memory_leak)
2659 << unsigned(Info.HeapAllocs.size() - 1);
2660 }
2661 return true;
2662}
2663
2664static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {
2665 // A null base expression indicates a null pointer. These are always
2666 // evaluatable, and they are false unless the offset is zero.
2667 if (!Value.getLValueBase()) {
2668 // TODO: Should a non-null pointer with an offset of zero evaluate to true?
2669 Result = !Value.getLValueOffset().isZero();
2670 return true;
2671 }
2672
2673 // We have a non-null base. These are generally known to be true, but if it's
2674 // a weak declaration it can be null at runtime.
2675 Result = true;
2676 const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
2677 return !Decl || !Decl->isWeak();
2678}
2679
2680static bool HandleConversionToBool(const APValue &Val, bool &Result) {
2681 // TODO: This function should produce notes if it fails.
2682 switch (Val.getKind()) {
2683 case APValue::None:
2684 case APValue::Indeterminate:
2685 return false;
2686 case APValue::Int:
2687 Result = Val.getInt().getBoolValue();
2688 return true;
2689 case APValue::FixedPoint:
2690 Result = Val.getFixedPoint().getBoolValue();
2691 return true;
2692 case APValue::Float:
2693 Result = !Val.getFloat().isZero();
2694 return true;
2695 case APValue::ComplexInt:
2696 Result = Val.getComplexIntReal().getBoolValue() ||
2697 Val.getComplexIntImag().getBoolValue();
2698 return true;
2699 case APValue::ComplexFloat:
2700 Result = !Val.getComplexFloatReal().isZero() ||
2701 !Val.getComplexFloatImag().isZero();
2702 return true;
2703 case APValue::LValue:
2704 return EvalPointerValueAsBool(Value: Val, Result);
2705 case APValue::MemberPointer:
2706 if (Val.getMemberPointerDecl() && Val.getMemberPointerDecl()->isWeak()) {
2707 return false;
2708 }
2709 Result = Val.getMemberPointerDecl();
2710 return true;
2711 case APValue::Vector:
2712 case APValue::Matrix:
2713 case APValue::Array:
2714 case APValue::Struct:
2715 case APValue::Union:
2716 case APValue::AddrLabelDiff:
2717 case APValue::Reflection:
2718 return false;
2719 }
2720
2721 llvm_unreachable("unknown APValue kind");
2722}
2723
2724static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
2725 EvalInfo &Info) {
2726 assert(!E->isValueDependent());
2727 assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition");
2728 APValue Val;
2729 if (!Evaluate(Result&: Val, Info, E))
2730 return false;
2731 return HandleConversionToBool(Val, Result);
2732}
2733
2734template<typename T>
2735static bool HandleOverflow(EvalInfo &Info, const Expr *E,
2736 const T &SrcValue, QualType DestType) {
2737 Info.CCEDiag(E, DiagId: diag::note_constexpr_overflow) << SrcValue << DestType;
2738 if (const auto *OBT = DestType->getAs<OverflowBehaviorType>();
2739 OBT && OBT->isTrapKind()) {
2740 return false;
2741 }
2742 return Info.noteUndefinedBehavior();
2743}
2744
2745static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
2746 QualType SrcType, const APFloat &Value,
2747 QualType DestType, APSInt &Result) {
2748 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2749 // Determine whether we are converting to unsigned or signed.
2750 bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
2751 // This handles converstion to bool.
2752 if (DestType->isBooleanType()) {
2753 Result = APSInt(APInt(1, !Value.isZero()), /*isUnsigned=*/true);
2754 return true;
2755 }
2756 Result = APSInt(DestWidth, !DestSigned);
2757 bool ignored;
2758
2759 if (Value.convertToInteger(Result, RM: llvm::APFloat::rmTowardZero, IsExact: &ignored)
2760 & APFloat::opInvalidOp)
2761 return HandleOverflow(Info, E, SrcValue: Value, DestType);
2762 return true;
2763}
2764
2765/// Get rounding mode to use in evaluation of the specified expression.
2766///
2767/// If rounding mode is unknown at compile time, still try to evaluate the
2768/// expression. If the result is exact, it does not depend on rounding mode.
2769/// So return "tonearest" mode instead of "dynamic".
2770static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E) {
2771 llvm::RoundingMode RM =
2772 E->getFPFeaturesInEffect(LO: Info.getLangOpts()).getRoundingMode();
2773 if (RM == llvm::RoundingMode::Dynamic)
2774 RM = llvm::RoundingMode::NearestTiesToEven;
2775 return RM;
2776}
2777
2778/// Check if the given floating-point evaluation result is allowed for
2779/// compile-time constant folding during translation (as opposed to mandatory
2780/// constant expression evaluation).
2781static bool checkFloatingPointResultForConstantFolding(EvalInfo &Info,
2782 const Expr *E,
2783 APFloat::opStatus St) {
2784 // In a constant context, assume that any dynamic rounding mode or FP
2785 // exception state matches the default floating-point environment.
2786 if (Info.InConstantContext)
2787 return true;
2788
2789 FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.getLangOpts());
2790 if ((St & APFloat::opInexact) &&
2791 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
2792 // Inexact result means that it depends on rounding mode. If the requested
2793 // mode is dynamic, the evaluation cannot be made in compile time.
2794 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
2795 return false;
2796 }
2797
2798 if ((St != APFloat::opOK) &&
2799 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||
2800 FPO.getExceptionMode() != LangOptions::FPE_Ignore ||
2801 FPO.getAllowFEnvAccess())) {
2802 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
2803 return false;
2804 }
2805
2806 if ((St & APFloat::opStatus::opInvalidOp) &&
2807 FPO.getExceptionMode() != LangOptions::FPE_Ignore) {
2808 // There is no usefully definable result.
2809 Info.FFDiag(E);
2810 return false;
2811 }
2812
2813 // FIXME: if:
2814 // - evaluation triggered other FP exception, and
2815 // - exception mode is not "ignore", and
2816 // - the expression being evaluated is not a part of global variable
2817 // initializer,
2818 // the evaluation probably need to be rejected.
2819 return true;
2820}
2821
2822static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
2823 QualType SrcType, QualType DestType,
2824 APFloat &Result) {
2825 assert((isa<CastExpr>(E) || isa<CompoundAssignOperator>(E) ||
2826 isa<ConvertVectorExpr>(E)) &&
2827 "HandleFloatToFloatCast has been checked with only CastExpr, "
2828 "CompoundAssignOperator and ConvertVectorExpr. Please either validate "
2829 "the new expression or address the root cause of this usage.");
2830 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2831 APFloat::opStatus St;
2832 bool ignored;
2833 St = Result.convert(ToSemantics: Info.Ctx.getFloatTypeSemantics(T: DestType), RM, losesInfo: &ignored);
2834 return checkFloatingPointResultForConstantFolding(Info, E, St);
2835}
2836
2837static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
2838 QualType DestType, QualType SrcType,
2839 const APSInt &Value) {
2840 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2841 // Figure out if this is a truncate, extend or noop cast.
2842 // If the input is signed, do a sign extend, noop, or truncate.
2843 APSInt Result = Value.extOrTrunc(width: DestWidth);
2844 Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
2845 if (DestType->isBooleanType())
2846 Result = Value.getBoolValue();
2847 return Result;
2848}
2849
2850static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
2851 const FPOptions FPO,
2852 QualType SrcType, const APSInt &Value,
2853 QualType DestType, APFloat &Result) {
2854 Result = APFloat(Info.Ctx.getFloatTypeSemantics(T: DestType), 1);
2855 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2856 APFloat::opStatus St = Result.convertFromAPInt(Input: Value, IsSigned: Value.isSigned(), RM);
2857 return checkFloatingPointResultForConstantFolding(Info, E, St);
2858}
2859
2860static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E,
2861 APValue &Value, const FieldDecl *FD) {
2862 assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield");
2863
2864 if (!Value.isInt()) {
2865 // Trying to store a pointer-cast-to-integer into a bitfield.
2866 // FIXME: In this case, we should provide the diagnostic for casting
2867 // a pointer to an integer.
2868 assert(Value.isLValue() && "integral value neither int nor lvalue?");
2869 Info.FFDiag(E);
2870 return false;
2871 }
2872
2873 APSInt &Int = Value.getInt();
2874 unsigned OldBitWidth = Int.getBitWidth();
2875 unsigned NewBitWidth = FD->getBitWidthValue();
2876 if (NewBitWidth < OldBitWidth)
2877 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
2878 return true;
2879}
2880
2881/// Perform the given integer operation, which is known to need at most BitWidth
2882/// bits, and check for overflow in the original type (if that type was not an
2883/// unsigned type).
2884template<typename Operation>
2885static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
2886 const APSInt &LHS, const APSInt &RHS,
2887 unsigned BitWidth, Operation Op,
2888 APSInt &Result) {
2889 if (LHS.isUnsigned()) {
2890 Result = Op(LHS, RHS);
2891 return true;
2892 }
2893
2894 APSInt Value(Op(LHS.extend(width: BitWidth), RHS.extend(width: BitWidth)), false);
2895 Result = Value.trunc(width: LHS.getBitWidth());
2896 if (Result.extend(width: BitWidth) != Value && !E->getType().isWrapType()) {
2897 if (Info.checkingForUndefinedBehavior())
2898 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
2899 DiagID: diag::warn_integer_constant_overflow)
2900 << toString(I: Result, Radix: 10, Signed: Result.isSigned(), /*formatAsCLiteral=*/false,
2901 /*UpperCase=*/true, /*InsertSeparators=*/true)
2902 << E->getType() << E->getSourceRange();
2903 return HandleOverflow(Info, E, SrcValue: Value, DestType: E->getType());
2904 }
2905 return true;
2906}
2907
2908/// Perform the given binary integer operation.
2909static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E,
2910 const APSInt &LHS, BinaryOperatorKind Opcode,
2911 APSInt RHS, APSInt &Result) {
2912 bool HandleOverflowResult = true;
2913 switch (Opcode) {
2914 default:
2915 Info.FFDiag(E);
2916 return false;
2917 case BO_Mul:
2918 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() * 2,
2919 Op: std::multiplies<APSInt>(), Result);
2920 case BO_Add:
2921 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2922 Op: std::plus<APSInt>(), Result);
2923 case BO_Sub:
2924 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2925 Op: std::minus<APSInt>(), Result);
2926 case BO_And: Result = LHS & RHS; return true;
2927 case BO_Xor: Result = LHS ^ RHS; return true;
2928 case BO_Or: Result = LHS | RHS; return true;
2929 case BO_Div:
2930 case BO_Rem:
2931 if (RHS == 0) {
2932 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero)
2933 << E->getRHS()->getSourceRange();
2934 return false;
2935 }
2936 // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports
2937 // this operation and gives the two's complement result.
2938 if (RHS.isNegative() && RHS.isAllOnes() && LHS.isSigned() &&
2939 LHS.isMinSignedValue())
2940 HandleOverflowResult = HandleOverflow(
2941 Info, E, SrcValue: -LHS.extend(width: LHS.getBitWidth() + 1), DestType: E->getType());
2942 Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS);
2943 return HandleOverflowResult;
2944 case BO_Shl: {
2945 if (Info.getLangOpts().OpenCL)
2946 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2947 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2948 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2949 RHS.isUnsigned());
2950 else if (RHS.isSigned() && RHS.isNegative()) {
2951 // During constant-folding, a negative shift is an opposite shift. Such
2952 // a shift is not a constant expression.
2953 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2954 if (!Info.noteUndefinedBehavior())
2955 return false;
2956 RHS = -RHS;
2957 goto shift_right;
2958 }
2959 shift_left:
2960 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
2961 // the shifted type.
2962 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2963 if (SA != RHS) {
2964 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
2965 << RHS << E->getType() << LHS.getBitWidth();
2966 if (!Info.noteUndefinedBehavior())
2967 return false;
2968 } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) {
2969 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
2970 // operand, and must not overflow the corresponding unsigned type.
2971 // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to
2972 // E1 x 2^E2 module 2^N.
2973 if (LHS.isNegative()) {
2974 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_of_negative) << LHS;
2975 if (!Info.noteUndefinedBehavior())
2976 return false;
2977 } else if (LHS.countl_zero() < SA) {
2978 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_discards);
2979 if (!Info.noteUndefinedBehavior())
2980 return false;
2981 }
2982 }
2983 Result = LHS << SA;
2984 return true;
2985 }
2986 case BO_Shr: {
2987 if (Info.getLangOpts().OpenCL)
2988 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2989 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2990 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2991 RHS.isUnsigned());
2992 else if (RHS.isSigned() && RHS.isNegative()) {
2993 // During constant-folding, a negative shift is an opposite shift. Such a
2994 // shift is not a constant expression.
2995 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2996 if (!Info.noteUndefinedBehavior())
2997 return false;
2998 RHS = -RHS;
2999 goto shift_left;
3000 }
3001 shift_right:
3002 // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
3003 // shifted type.
3004 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
3005 if (SA != RHS) {
3006 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
3007 << RHS << E->getType() << LHS.getBitWidth();
3008 if (!Info.noteUndefinedBehavior())
3009 return false;
3010 }
3011
3012 Result = LHS >> SA;
3013 return true;
3014 }
3015
3016 case BO_LT: Result = LHS < RHS; return true;
3017 case BO_GT: Result = LHS > RHS; return true;
3018 case BO_LE: Result = LHS <= RHS; return true;
3019 case BO_GE: Result = LHS >= RHS; return true;
3020 case BO_EQ: Result = LHS == RHS; return true;
3021 case BO_NE: Result = LHS != RHS; return true;
3022 case BO_Cmp:
3023 llvm_unreachable("BO_Cmp should be handled elsewhere");
3024 }
3025}
3026
3027/// Perform the given binary floating-point operation, in-place, on LHS.
3028static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E,
3029 APFloat &LHS, BinaryOperatorKind Opcode,
3030 const APFloat &RHS) {
3031 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
3032 APFloat::opStatus St;
3033 switch (Opcode) {
3034 default:
3035 Info.FFDiag(E);
3036 return false;
3037 case BO_Mul:
3038 St = LHS.multiply(RHS, RM);
3039 break;
3040 case BO_Add:
3041 St = LHS.add(RHS, RM);
3042 break;
3043 case BO_Sub:
3044 St = LHS.subtract(RHS, RM);
3045 break;
3046 case BO_Div:
3047 // [expr.mul]p4:
3048 // If the second operand of / or % is zero the behavior is undefined.
3049 if (RHS.isZero())
3050 Info.CCEDiag(E, DiagId: diag::note_expr_divide_by_zero);
3051 St = LHS.divide(RHS, RM);
3052 break;
3053 }
3054
3055 // FIXME: The standard quote below is deleted by P3899R3.
3056 // [expr.pre]p4:
3057 // If during the evaluation of an expression, the result is not
3058 // mathematically defined [...], the behavior is undefined.
3059 // FIXME: C++ rules require us to not conform to IEEE 754 here.
3060 // FIXME: The NaN check should not be applied outside of "constant contexts"
3061 // because it prevents NaN propagation and the "invalid" status is the
3062 // responsibility of checkFloatingPointResultForConstantFolding.
3063 if (LHS.isNaN()) {
3064 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << LHS.isNaN();
3065 return Info.noteUndefinedBehavior();
3066 }
3067
3068 return checkFloatingPointResultForConstantFolding(Info, E, St);
3069}
3070
3071static bool handleLogicalOpForVector(const APInt &LHSValue,
3072 BinaryOperatorKind Opcode,
3073 const APInt &RHSValue, APInt &Result) {
3074 bool LHS = (LHSValue != 0);
3075 bool RHS = (RHSValue != 0);
3076
3077 if (Opcode == BO_LAnd)
3078 Result = LHS && RHS;
3079 else
3080 Result = LHS || RHS;
3081 return true;
3082}
3083static bool handleLogicalOpForVector(const APFloat &LHSValue,
3084 BinaryOperatorKind Opcode,
3085 const APFloat &RHSValue, APInt &Result) {
3086 bool LHS = !LHSValue.isZero();
3087 bool RHS = !RHSValue.isZero();
3088
3089 if (Opcode == BO_LAnd)
3090 Result = LHS && RHS;
3091 else
3092 Result = LHS || RHS;
3093 return true;
3094}
3095
3096static bool handleLogicalOpForVector(const APValue &LHSValue,
3097 BinaryOperatorKind Opcode,
3098 const APValue &RHSValue, APInt &Result) {
3099 // The result is always an int type, however operands match the first.
3100 if (LHSValue.getKind() == APValue::Int)
3101 return handleLogicalOpForVector(LHSValue: LHSValue.getInt(), Opcode,
3102 RHSValue: RHSValue.getInt(), Result);
3103 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3104 return handleLogicalOpForVector(LHSValue: LHSValue.getFloat(), Opcode,
3105 RHSValue: RHSValue.getFloat(), Result);
3106}
3107
3108template <typename APTy>
3109static bool
3110handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode,
3111 const APTy &RHSValue, APInt &Result) {
3112 switch (Opcode) {
3113 default:
3114 llvm_unreachable("unsupported binary operator");
3115 case BO_EQ:
3116 Result = (LHSValue == RHSValue);
3117 break;
3118 case BO_NE:
3119 Result = (LHSValue != RHSValue);
3120 break;
3121 case BO_LT:
3122 Result = (LHSValue < RHSValue);
3123 break;
3124 case BO_GT:
3125 Result = (LHSValue > RHSValue);
3126 break;
3127 case BO_LE:
3128 Result = (LHSValue <= RHSValue);
3129 break;
3130 case BO_GE:
3131 Result = (LHSValue >= RHSValue);
3132 break;
3133 }
3134
3135 // The boolean operations on these vector types use an instruction that
3136 // results in a mask of '-1' for the 'truth' value. Ensure that we negate 1
3137 // to -1 to make sure that we produce the correct value.
3138 Result.negate();
3139
3140 return true;
3141}
3142
3143static bool handleCompareOpForVector(const APValue &LHSValue,
3144 BinaryOperatorKind Opcode,
3145 const APValue &RHSValue, APInt &Result) {
3146 // The result is always an int type, however operands match the first.
3147 if (LHSValue.getKind() == APValue::Int)
3148 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getInt(), Opcode,
3149 RHSValue: RHSValue.getInt(), Result);
3150 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3151 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getFloat(), Opcode,
3152 RHSValue: RHSValue.getFloat(), Result);
3153}
3154
3155// Perform binary operations for vector types, in place on the LHS.
3156static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E,
3157 BinaryOperatorKind Opcode,
3158 APValue &LHSValue,
3159 const APValue &RHSValue) {
3160 assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI &&
3161 "Operation not supported on vector types");
3162
3163 const auto *VT = E->getType()->castAs<VectorType>();
3164 unsigned NumElements = VT->getNumElements();
3165 QualType EltTy = VT->getElementType();
3166
3167 // In the cases (typically C as I've observed) where we aren't evaluating
3168 // constexpr but are checking for cases where the LHS isn't yet evaluatable,
3169 // just give up.
3170 if (!LHSValue.isVector()) {
3171 assert(LHSValue.isLValue() &&
3172 "A vector result that isn't a vector OR uncalculated LValue");
3173 Info.FFDiag(E);
3174 return false;
3175 }
3176
3177 assert(LHSValue.getVectorLength() == NumElements &&
3178 RHSValue.getVectorLength() == NumElements && "Different vector sizes");
3179
3180 SmallVector<APValue, 4> ResultElements;
3181
3182 for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) {
3183 APValue LHSElt = LHSValue.getVectorElt(I: EltNum);
3184 APValue RHSElt = RHSValue.getVectorElt(I: EltNum);
3185
3186 if (EltTy->isIntegerType()) {
3187 APSInt EltResult{Info.Ctx.getIntWidth(T: EltTy),
3188 EltTy->isUnsignedIntegerType()};
3189 bool Success = true;
3190
3191 if (BinaryOperator::isLogicalOp(Opc: Opcode))
3192 Success = handleLogicalOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3193 else if (BinaryOperator::isComparisonOp(Opc: Opcode))
3194 Success = handleCompareOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3195 else
3196 Success = handleIntIntBinOp(Info, E, LHS: LHSElt.getInt(), Opcode,
3197 RHS: RHSElt.getInt(), Result&: EltResult);
3198
3199 if (!Success) {
3200 Info.FFDiag(E);
3201 return false;
3202 }
3203 ResultElements.emplace_back(Args&: EltResult);
3204
3205 } else if (EltTy->isFloatingType()) {
3206 assert(LHSElt.getKind() == APValue::Float &&
3207 RHSElt.getKind() == APValue::Float &&
3208 "Mismatched LHS/RHS/Result Type");
3209 APFloat LHSFloat = LHSElt.getFloat();
3210
3211 if (!handleFloatFloatBinOp(Info, E, LHS&: LHSFloat, Opcode,
3212 RHS: RHSElt.getFloat())) {
3213 Info.FFDiag(E);
3214 return false;
3215 }
3216
3217 ResultElements.emplace_back(Args&: LHSFloat);
3218 }
3219 }
3220
3221 LHSValue = APValue(ResultElements.data(), ResultElements.size());
3222 return true;
3223}
3224
3225/// Cast an lvalue referring to a base subobject to a derived class, by
3226/// truncating the lvalue's path to the given length.
3227static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
3228 const RecordDecl *TruncatedType,
3229 unsigned TruncatedElements) {
3230 SubobjectDesignator &D = Result.Designator;
3231
3232 // Check we actually point to a derived class object.
3233 if (TruncatedElements == D.Entries.size())
3234 return true;
3235 assert(TruncatedElements >= D.MostDerivedPathLength &&
3236 "not casting to a derived class");
3237 if (!Result.checkSubobject(Info, E, CSK: CSK_Derived))
3238 return false;
3239
3240 // Truncate the path to the subobject, and remove any derived-to-base offsets.
3241 const RecordDecl *RD = TruncatedType;
3242 for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
3243 if (RD->isInvalidDecl()) return false;
3244 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
3245 const CXXRecordDecl *Base = getAsBaseClass(E: D.Entries[I]);
3246 if (isVirtualBaseClass(E: D.Entries[I]))
3247 Result.Offset -= Layout.getVBaseClassOffset(VBase: Base);
3248 else
3249 Result.Offset -= Layout.getBaseClassOffset(Base);
3250 RD = Base;
3251 }
3252 D.Entries.resize(N: TruncatedElements);
3253 return true;
3254}
3255
3256static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3257 const CXXRecordDecl *Derived,
3258 const CXXRecordDecl *Base,
3259 const ASTRecordLayout *RL = nullptr) {
3260 if (!RL) {
3261 if (Derived->isInvalidDecl()) return false;
3262 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3263 }
3264
3265 Obj.addDecl(Info, E, D: Base, /*Virtual=*/false);
3266 Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
3267 return true;
3268}
3269
3270static bool HandleLValueDirectVirtualBase(EvalInfo &Info, const Expr *E,
3271 LValue &Obj,
3272 const CXXRecordDecl *Derived,
3273 const CXXRecordDecl *Base,
3274 const ASTRecordLayout *RL = nullptr) {
3275 if (!RL) {
3276 if (Derived->isInvalidDecl())
3277 return false;
3278 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3279 }
3280
3281 Obj.addDecl(Info, E, D: Base, /*Virtual=*/true);
3282 Obj.getLValueOffset() += RL->getVBaseClassOffset(VBase: Base);
3283 return true;
3284}
3285
3286static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3287 const CXXRecordDecl *DerivedDecl,
3288 const CXXBaseSpecifier *Base) {
3289 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
3290
3291 if (!Base->isVirtual())
3292 return HandleLValueDirectBase(Info, E, Obj, Derived: DerivedDecl, Base: BaseDecl);
3293
3294 SubobjectDesignator &D = Obj.Designator;
3295 if (D.Invalid)
3296 return false;
3297
3298 // Extract most-derived object and corresponding type.
3299 // FIXME: After implementing P2280R4 it became possible to get references
3300 // here. We do MostDerivedType->getAsCXXRecordDecl() in several other
3301 // locations and if we see crashes in those locations in the future
3302 // it may make more sense to move this fix into Lvalue::set.
3303 DerivedDecl = D.MostDerivedType.getNonReferenceType()->getAsCXXRecordDecl();
3304 if (!CastToDerivedClass(Info, E, Result&: Obj, TruncatedType: DerivedDecl, TruncatedElements: D.MostDerivedPathLength))
3305 return false;
3306
3307 // Find the virtual base class.
3308 if (DerivedDecl->isInvalidDecl()) return false;
3309 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: DerivedDecl);
3310 Obj.addDecl(Info, E, D: BaseDecl, /*Virtual*/ true);
3311 Obj.getLValueOffset() += Layout.getVBaseClassOffset(VBase: BaseDecl);
3312 return true;
3313}
3314
3315static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E,
3316 QualType Type, LValue &Result) {
3317 for (CastExpr::path_const_iterator PathI = E->path_begin(),
3318 PathE = E->path_end();
3319 PathI != PathE; ++PathI) {
3320 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Type->getAsCXXRecordDecl(),
3321 Base: *PathI))
3322 return false;
3323 Type = (*PathI)->getType();
3324 }
3325 return true;
3326}
3327
3328/// Cast an lvalue referring to a derived class to a known base subobject.
3329static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result,
3330 const CXXRecordDecl *DerivedRD,
3331 const CXXRecordDecl *BaseRD) {
3332 CXXBasePaths Paths(/*FindAmbiguities=*/false,
3333 /*RecordPaths=*/true, /*DetectVirtual=*/false);
3334 if (!DerivedRD->isDerivedFrom(Base: BaseRD, Paths))
3335 llvm_unreachable("Class must be derived from the passed in base class!");
3336
3337 for (CXXBasePathElement &Elem : Paths.front())
3338 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Elem.Class, Base: Elem.Base))
3339 return false;
3340 return true;
3341}
3342
3343/// Update LVal to refer to the given field, which must be a member of the type
3344/// currently described by LVal.
3345static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
3346 const FieldDecl *FD,
3347 const ASTRecordLayout *RL = nullptr) {
3348 if (!RL) {
3349 const RecordDecl *RD = FD->getParent();
3350 if (RD->isInvalidDecl())
3351 return false;
3352 // There are some cases where the base is not yet complete but we haven't
3353 // disagnosed (such as in a template instantation of an attribute that
3354 // references the expression, ala enable_if). These aren't necessarily
3355 // constant expressions so we return 'false', but they might be, so we don't
3356 // diagnose.
3357 if (!RD->isCompleteDefinition())
3358 return false;
3359 RL = &Info.Ctx.getASTRecordLayout(D: RD);
3360 }
3361
3362 unsigned I = FD->getFieldIndex();
3363 LVal.addDecl(Info, E, D: FD);
3364 LVal.adjustOffset(N: Info.Ctx.toCharUnitsFromBits(BitSize: RL->getFieldOffset(FieldNo: I)));
3365 return true;
3366}
3367
3368/// Update LVal to refer to the given indirect field.
3369static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
3370 LValue &LVal,
3371 const IndirectFieldDecl *IFD) {
3372 for (const auto *C : IFD->chain())
3373 if (!HandleLValueMember(Info, E, LVal, FD: cast<FieldDecl>(Val: C)))
3374 return false;
3375 return true;
3376}
3377
3378enum class SizeOfType {
3379 SizeOf,
3380 DataSizeOf,
3381};
3382
3383/// Get the size of the given type in char units.
3384static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type,
3385 CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) {
3386 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
3387 // extension.
3388 if (Type->isVoidType() || Type->isFunctionType()) {
3389 Size = CharUnits::One();
3390 return true;
3391 }
3392
3393 if (Type->isDependentType()) {
3394 Info.FFDiag(Loc);
3395 return false;
3396 }
3397
3398 if (!Type->isConstantSizeType()) {
3399 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
3400 // FIXME: Better diagnostic.
3401 Info.FFDiag(Loc);
3402 return false;
3403 }
3404
3405 if (SOT == SizeOfType::SizeOf)
3406 Size = Info.Ctx.getTypeSizeInChars(T: Type);
3407 else
3408 Size = Info.Ctx.getTypeInfoDataSizeInChars(T: Type).Width;
3409 return true;
3410}
3411
3412/// Update a pointer value to model pointer arithmetic.
3413/// \param Info - Information about the ongoing evaluation.
3414/// \param E - The expression being evaluated, for diagnostic purposes.
3415/// \param LVal - The pointer value to be updated.
3416/// \param EltTy - The pointee type represented by LVal.
3417/// \param Adjustment - The adjustment, in objects of type EltTy, to add.
3418static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3419 LValue &LVal, QualType EltTy,
3420 APSInt Adjustment) {
3421 CharUnits SizeOfPointee;
3422 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfPointee))
3423 return false;
3424
3425 LVal.adjustOffsetAndIndex(Info, E, Index: Adjustment, ElementSize: SizeOfPointee);
3426 return true;
3427}
3428
3429static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3430 LValue &LVal, QualType EltTy,
3431 int64_t Adjustment) {
3432 return HandleLValueArrayAdjustment(Info, E, LVal, EltTy,
3433 Adjustment: APSInt::get(X: Adjustment));
3434}
3435
3436/// Update an lvalue to refer to a component of a complex number.
3437/// \param Info - Information about the ongoing evaluation.
3438/// \param LVal - The lvalue to be updated.
3439/// \param EltTy - The complex number's component type.
3440/// \param Imag - False for the real component, true for the imaginary.
3441static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E,
3442 LValue &LVal, QualType EltTy,
3443 bool Imag) {
3444 if (Imag) {
3445 CharUnits SizeOfComponent;
3446 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfComponent))
3447 return false;
3448 LVal.Offset += SizeOfComponent;
3449 }
3450 LVal.addComplex(Info, E, EltTy, Imag);
3451 return true;
3452}
3453
3454static bool HandleLValueVectorElement(EvalInfo &Info, const Expr *E,
3455 LValue &LVal, QualType EltTy,
3456 uint64_t Size, uint64_t Idx) {
3457 if (Idx) {
3458 CharUnits SizeOfElement;
3459 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfElement))
3460 return false;
3461 LVal.Offset += SizeOfElement * Idx;
3462 }
3463 LVal.addVectorElement(Info, E, EltTy, Size, Idx);
3464 return true;
3465}
3466
3467/// Try to evaluate the initializer for a variable declaration.
3468///
3469/// \param Info Information about the ongoing evaluation.
3470/// \param E An expression to be used when printing diagnostics.
3471/// \param VD The variable whose initializer should be obtained.
3472/// \param Version The version of the variable within the frame.
3473/// \param Frame The frame in which the variable was created. Must be null
3474/// if this variable is not local to the evaluation.
3475/// \param Result Filled in with a pointer to the value of the variable.
3476static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
3477 const VarDecl *VD, CallStackFrame *Frame,
3478 unsigned Version, APValue *&Result) {
3479 // C++23 [expr.const]p8 If we have a reference type allow unknown references
3480 // and pointers.
3481 bool AllowConstexprUnknown =
3482 Info.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType();
3483
3484 APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version);
3485
3486 auto CheckUninitReference = [&](bool IsLocalVariable) {
3487 if (!Result || (!Result->hasValue() && VD->getType()->isReferenceType())) {
3488 // C++23 [expr.const]p8
3489 // ... For such an object that is not usable in constant expressions, the
3490 // dynamic type of the object is constexpr-unknown. For such a reference
3491 // that is not usable in constant expressions, the reference is treated
3492 // as binding to an unspecified object of the referenced type whose
3493 // lifetime and that of all subobjects includes the entire constant
3494 // evaluation and whose dynamic type is constexpr-unknown.
3495 //
3496 // Variables that are part of the current evaluation are not
3497 // constexpr-unknown.
3498 if (!AllowConstexprUnknown || IsLocalVariable) {
3499 if (!Info.checkingPotentialConstantExpression())
3500 Info.FFDiag(E, DiagId: diag::note_constexpr_use_uninit_reference);
3501 return false;
3502 }
3503 Result = nullptr;
3504 }
3505 return true;
3506 };
3507
3508 // If this is a local variable, dig out its value.
3509 if (Frame) {
3510 Result = Frame->getTemporary(Key: VD, Version);
3511 if (Result)
3512 return CheckUninitReference(/*IsLocalVariable=*/true);
3513
3514 if (!isa<ParmVarDecl>(Val: VD)) {
3515 // Assume variables referenced within a lambda's call operator that were
3516 // not declared within the call operator are captures and during checking
3517 // of a potential constant expression, assume they are unknown constant
3518 // expressions.
3519 assert(isLambdaCallOperator(Frame->Callee) &&
3520 (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) &&
3521 "missing value for local variable");
3522 if (Info.checkingPotentialConstantExpression())
3523 return false;
3524
3525 llvm_unreachable(
3526 "A variable in a frame should either be a local or a parameter");
3527 }
3528 }
3529
3530 // If we're currently evaluating the initializer of this declaration, use that
3531 // in-flight value.
3532 if (Info.EvaluatingDecl == Base) {
3533 Result = Info.EvaluatingDeclValue;
3534 return CheckUninitReference(/*IsLocalVariable=*/false);
3535 }
3536
3537 // P2280R4 struck the restriction that variable of reference type lifetime
3538 // should begin within the evaluation of E
3539 // Used to be C++20 [expr.const]p5.12.2:
3540 // ... its lifetime began within the evaluation of E;
3541 if (isa<ParmVarDecl>(Val: VD)) {
3542 if (AllowConstexprUnknown) {
3543 Result = nullptr;
3544 return true;
3545 }
3546
3547 // Assume parameters of a potential constant expression are usable in
3548 // constant expressions.
3549 if (!Info.checkingPotentialConstantExpression() ||
3550 !Info.CurrentCall->Callee ||
3551 !Info.CurrentCall->Callee->Equals(DC: VD->getDeclContext())) {
3552 if (Info.getLangOpts().CPlusPlus11) {
3553 Info.FFDiag(E, DiagId: diag::note_constexpr_function_param_value_unknown)
3554 << VD;
3555 NoteLValueLocation(Info, Base);
3556 } else {
3557 Info.FFDiag(E);
3558 }
3559 }
3560 return false;
3561 }
3562
3563 if (E->isValueDependent())
3564 return false;
3565
3566 // Dig out the initializer, and use the declaration which it's attached to.
3567 // FIXME: We should eventually check whether the variable has a reachable
3568 // initializing declaration.
3569 const Expr *Init = VD->getAnyInitializer(D&: VD);
3570 // P2280R4 struck the restriction that variable of reference type should have
3571 // a preceding initialization.
3572 // Used to be C++20 [expr.const]p5.12:
3573 // ... reference has a preceding initialization and either ...
3574 if (!Init && !AllowConstexprUnknown) {
3575 // Don't diagnose during potential constant expression checking; an
3576 // initializer might be added later.
3577 if (!Info.checkingPotentialConstantExpression()) {
3578 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_unknown, ExtraNotes: 1)
3579 << VD;
3580 NoteLValueLocation(Info, Base);
3581 }
3582 return false;
3583 }
3584
3585 // P2280R4 struck the initialization requirement for variables of reference
3586 // type so we can no longer assume we have an Init.
3587 // Used to be C++20 [expr.const]p5.12:
3588 // ... reference has a preceding initialization and either ...
3589 if (Init && Init->isValueDependent()) {
3590 if (!Info.checkingPotentialConstantExpression()) {
3591 Info.FFDiag(E,
3592 DiagId: Info.getLangOpts().CPlusPlus11
3593 ? diag::note_constexpr_ltor_non_constexpr
3594 : diag::note_constexpr_ltor_non_integral,
3595 ExtraNotes: 1)
3596 << VD << VD->getType();
3597 NoteLValueLocation(Info, Base);
3598 }
3599
3600 // A recovery initializer can be value-dependent even when the expression
3601 // referring to the variable is not.
3602 if (Init->containsErrors())
3603 return false;
3604
3605 // The DeclRefExpr is not value-dependent, but the variable it refers to
3606 // has a value-dependent initializer. This should only happen in
3607 // constant-folding cases, where the variable is not actually of a suitable
3608 // type for use in a constant expression (otherwise the DeclRefExpr would
3609 // have been value-dependent too), so diagnose that.
3610 assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx));
3611 return false;
3612 }
3613
3614 // Check that we can fold the initializer. In C++, we will have already done
3615 // this in the cases where it matters for conformance.
3616 // P2280R4 struck the initialization requirement for variables of reference
3617 // type so we can no longer assume we have an Init.
3618 // Used to be C++20 [expr.const]p5.12:
3619 // ... reference has a preceding initialization and either ...
3620 if (Init && !VD->evaluateValue() && !AllowConstexprUnknown) {
3621 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3622 NoteLValueLocation(Info, Base);
3623 return false;
3624 }
3625
3626 // Check that the variable is actually usable in constant expressions. For a
3627 // const integral variable or a reference, we might have a non-constant
3628 // initializer that we can nonetheless evaluate the initializer for. Such
3629 // variables are not usable in constant expressions. In C++98, the
3630 // initializer also syntactically needs to be an ICE.
3631 //
3632 // FIXME: We don't diagnose cases that aren't potentially usable in constant
3633 // expressions here; doing so would regress diagnostics for things like
3634 // reading from a volatile constexpr variable.
3635 if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() &&
3636 VD->mightBeUsableInConstantExpressions(C: Info.Ctx) &&
3637 !AllowConstexprUnknown) ||
3638 ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) &&
3639 !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Context: Info.Ctx))) {
3640 if (Init) {
3641 Info.CCEDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3642 NoteLValueLocation(Info, Base);
3643 } else {
3644 Info.CCEDiag(E);
3645 }
3646 }
3647
3648 // Never use the initializer of a weak variable, not even for constant
3649 // folding. We can't be sure that this is the definition that will be used.
3650 if (VD->isWeak()) {
3651 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_weak) << VD;
3652 NoteLValueLocation(Info, Base);
3653 return false;
3654 }
3655
3656 Result = const_cast<APValue *>(VD->getEvaluatedValue());
3657
3658 if (!Result && !AllowConstexprUnknown)
3659 return false;
3660
3661 return CheckUninitReference(/*IsLocalVariable=*/false);
3662}
3663
3664/// Get the base index of the given base class within an APValue representing
3665/// the given derived class.
3666static unsigned getBaseIndex(const CXXRecordDecl *Derived,
3667 const CXXRecordDecl *Base) {
3668 Base = Base->getCanonicalDecl();
3669 unsigned Index = 0;
3670 for (const CXXBaseSpecifier &B : Derived->bases()) {
3671 if (B.isVirtual())
3672 continue;
3673 if (B.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3674 return Index;
3675 ++Index;
3676 }
3677
3678 for (const CXXBaseSpecifier &B : Derived->vbases()) {
3679 if (B.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3680 return Index;
3681 ++Index;
3682 }
3683
3684 llvm_unreachable("base class missing from derived class's bases list");
3685}
3686
3687/// Extract the value of a character from a string literal.
3688static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
3689 uint64_t Index) {
3690 assert(!isa<SourceLocExpr>(Lit) &&
3691 "SourceLocExpr should have already been converted to a StringLiteral");
3692
3693 // FIXME: Support MakeStringConstant
3694 if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Val: Lit)) {
3695 std::string Str;
3696 Info.Ctx.getObjCEncodingForType(T: ObjCEnc->getEncodedType(), S&: Str);
3697 assert(Index <= Str.size() && "Index too large");
3698 return APSInt::getUnsigned(X: Str.c_str()[Index]);
3699 }
3700
3701 if (auto PE = dyn_cast<PredefinedExpr>(Val: Lit))
3702 Lit = PE->getFunctionName();
3703 const StringLiteral *S = cast<StringLiteral>(Val: Lit);
3704 const ConstantArrayType *CAT =
3705 Info.Ctx.getAsConstantArrayType(T: S->getType());
3706 assert(CAT && "string literal isn't an array");
3707 QualType CharType = CAT->getElementType();
3708 assert(CharType->isIntegerType() && "unexpected character type");
3709 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3710 CharType->isUnsignedIntegerType());
3711 if (Index < S->getLength())
3712 Value = S->getCodeUnit(I: Index);
3713 return Value;
3714}
3715
3716// Expand a string literal into an array of characters.
3717//
3718// FIXME: This is inefficient; we should probably introduce something similar
3719// to the LLVM ConstantDataArray to make this cheaper.
3720static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S,
3721 APValue &Result,
3722 QualType AllocType = QualType()) {
3723 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
3724 T: AllocType.isNull() ? S->getType() : AllocType);
3725 assert(CAT && "string literal isn't an array");
3726 QualType CharType = CAT->getElementType();
3727 assert(CharType->isIntegerType() && "unexpected character type");
3728
3729 unsigned Elts = CAT->getZExtSize();
3730 Result = APValue(APValue::UninitArray(),
3731 std::min(a: S->getLength(), b: Elts), Elts);
3732 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3733 CharType->isUnsignedIntegerType());
3734 if (Result.hasArrayFiller())
3735 Result.getArrayFiller() = APValue(Value);
3736 for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) {
3737 Value = S->getCodeUnit(I);
3738 Result.getArrayInitializedElt(I) = APValue(Value);
3739 }
3740}
3741
3742// Expand an array so that it has more than Index filled elements.
3743static void expandArray(APValue &Array, unsigned Index) {
3744 unsigned Size = Array.getArraySize();
3745 assert(Index < Size);
3746
3747 // Always at least double the number of elements for which we store a value.
3748 unsigned OldElts = Array.getArrayInitializedElts();
3749 unsigned NewElts = std::max(a: Index+1, b: OldElts * 2);
3750 NewElts = std::min(a: Size, b: std::max(a: NewElts, b: 8u));
3751
3752 // Copy the data across.
3753 APValue NewValue(APValue::UninitArray(), NewElts, Size);
3754 for (unsigned I = 0; I != OldElts; ++I)
3755 NewValue.getArrayInitializedElt(I).swap(RHS&: Array.getArrayInitializedElt(I));
3756 for (unsigned I = OldElts; I != NewElts; ++I)
3757 NewValue.getArrayInitializedElt(I) = Array.getArrayFiller();
3758 if (NewValue.hasArrayFiller())
3759 NewValue.getArrayFiller() = Array.getArrayFiller();
3760 Array.swap(RHS&: NewValue);
3761}
3762
3763// Expand an indeterminate vector to materialize all elements.
3764static void expandVector(APValue &Vec, unsigned NumElements) {
3765 assert(Vec.isIndeterminate());
3766 SmallVector<APValue, 4> Elts(NumElements, APValue::IndeterminateValue());
3767 Vec = APValue(Elts.data(), Elts.size());
3768}
3769
3770/// Determine whether a type would actually be read by an lvalue-to-rvalue
3771/// conversion. If it's of class type, we may assume that the copy operation
3772/// is trivial. Note that this is never true for a union type with fields
3773/// (because the copy always "reads" the active member) and always true for
3774/// a non-class type.
3775bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD);
3776bool isReadByLvalueToRvalueConversion(QualType T) {
3777 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3778 return !RD || isReadByLvalueToRvalueConversion(RD);
3779}
3780bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) {
3781 // FIXME: A trivial copy of a union copies the object representation, even if
3782 // the union is empty.
3783 if (RD->isUnion())
3784 return !RD->field_empty();
3785 if (RD->isEmpty())
3786 return false;
3787
3788 for (auto *Field : RD->fields())
3789 if (!Field->isUnnamedBitField() &&
3790 isReadByLvalueToRvalueConversion(T: Field->getType()))
3791 return true;
3792
3793 for (auto &BaseSpec : RD->bases())
3794 if (isReadByLvalueToRvalueConversion(T: BaseSpec.getType()))
3795 return true;
3796
3797 return false;
3798}
3799
3800/// Diagnose an attempt to read from any unreadable field within the specified
3801/// type, which might be a class type.
3802static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK,
3803 QualType T) {
3804 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3805 if (!RD)
3806 return false;
3807
3808 if (!RD->hasMutableFields())
3809 return false;
3810
3811 for (auto *Field : RD->fields()) {
3812 // If we're actually going to read this field in some way, then it can't
3813 // be mutable. If we're in a union, then assigning to a mutable field
3814 // (even an empty one) can change the active member, so that's not OK.
3815 // FIXME: Add core issue number for the union case.
3816 if (Field->isMutable() &&
3817 (RD->isUnion() || isReadByLvalueToRvalueConversion(T: Field->getType()))) {
3818 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1) << AK << Field;
3819 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
3820 return true;
3821 }
3822
3823 if (diagnoseMutableFields(Info, E, AK, T: Field->getType()))
3824 return true;
3825 }
3826
3827 for (auto &BaseSpec : RD->bases())
3828 if (diagnoseMutableFields(Info, E, AK, T: BaseSpec.getType()))
3829 return true;
3830
3831 // All mutable fields were empty, and thus not actually read.
3832 return false;
3833}
3834
3835static bool lifetimeStartedInEvaluation(EvalInfo &Info,
3836 APValue::LValueBase Base,
3837 bool MutableSubobject = false) {
3838 // A temporary or transient heap allocation we created.
3839 if (Base.getCallIndex() || Base.is<DynamicAllocLValue>())
3840 return true;
3841
3842 switch (Info.IsEvaluatingDecl) {
3843 case EvalInfo::EvaluatingDeclKind::None:
3844 return false;
3845
3846 case EvalInfo::EvaluatingDeclKind::Ctor:
3847 // The variable whose initializer we're evaluating.
3848 if (Info.EvaluatingDecl == Base)
3849 return true;
3850
3851 // A temporary lifetime-extended by the variable whose initializer we're
3852 // evaluating.
3853 if (auto *BaseE = Base.dyn_cast<const Expr *>())
3854 if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(Val: BaseE))
3855 return Info.EvaluatingDecl == BaseMTE->getExtendingDecl();
3856 return false;
3857
3858 case EvalInfo::EvaluatingDeclKind::Dtor:
3859 // C++2a [expr.const]p6:
3860 // [during constant destruction] the lifetime of a and its non-mutable
3861 // subobjects (but not its mutable subobjects) [are] considered to start
3862 // within e.
3863 if (MutableSubobject || Base != Info.EvaluatingDecl)
3864 return false;
3865 // FIXME: We can meaningfully extend this to cover non-const objects, but
3866 // we will need special handling: we should be able to access only
3867 // subobjects of such objects that are themselves declared const.
3868 QualType T = getType(B: Base);
3869 return T.isConstQualified() || T->isReferenceType();
3870 }
3871
3872 llvm_unreachable("unknown evaluating decl kind");
3873}
3874
3875static bool CheckArraySize(EvalInfo &Info, const ConstantArrayType *CAT,
3876 SourceLocation CallLoc = {}) {
3877 return Info.CheckArraySize(
3878 Loc: CAT->getSizeExpr() ? CAT->getSizeExpr()->getBeginLoc() : CallLoc,
3879 BitWidth: CAT->getNumAddressingBits(Context: Info.Ctx), ElemCount: CAT->getZExtSize(),
3880 /*Diag=*/true);
3881}
3882
3883static bool handleScalarCast(EvalInfo &Info, const FPOptions FPO, const Expr *E,
3884 QualType SourceTy, QualType DestTy,
3885 APValue const &Original, APValue &Result) {
3886 // boolean must be checked before integer
3887 // since IsIntegerType() is true for bool
3888 if (SourceTy->isBooleanType()) {
3889 if (DestTy->isBooleanType()) {
3890 Result = Original;
3891 return true;
3892 }
3893 if (DestTy->isIntegerType() || DestTy->isRealFloatingType()) {
3894 bool BoolResult;
3895 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3896 return false;
3897 uint64_t IntResult = BoolResult;
3898 QualType IntType = DestTy->isIntegerType()
3899 ? DestTy
3900 : Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false);
3901 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: IntType));
3902 }
3903 if (DestTy->isRealFloatingType()) {
3904 APValue Result2 = APValue(APFloat(0.0));
3905 if (!HandleIntToFloatCast(Info, E, FPO,
3906 SrcType: Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false),
3907 Value: Result.getInt(), DestType: DestTy, Result&: Result2.getFloat()))
3908 return false;
3909 Result = std::move(Result2);
3910 }
3911 return true;
3912 }
3913 if (SourceTy->isIntegerType()) {
3914 if (DestTy->isRealFloatingType()) {
3915 Result = APValue(APFloat(0.0));
3916 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
3917 DestType: DestTy, Result&: Result.getFloat());
3918 }
3919 if (DestTy->isBooleanType()) {
3920 bool BoolResult;
3921 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3922 return false;
3923 uint64_t IntResult = BoolResult;
3924 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3925 return true;
3926 }
3927 if (DestTy->isIntegerType()) {
3928 Result = APValue(
3929 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
3930 return true;
3931 }
3932 } else if (SourceTy->isRealFloatingType()) {
3933 if (DestTy->isRealFloatingType()) {
3934 Result = Original;
3935 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
3936 Result&: Result.getFloat());
3937 }
3938 if (DestTy->isBooleanType()) {
3939 bool BoolResult;
3940 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3941 return false;
3942 uint64_t IntResult = BoolResult;
3943 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3944 return true;
3945 }
3946 if (DestTy->isIntegerType()) {
3947 Result = APValue(APSInt());
3948 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
3949 DestType: DestTy, Result&: Result.getInt());
3950 }
3951 }
3952
3953 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
3954 return false;
3955}
3956
3957// do the heavy lifting for casting to aggregate types
3958// because we have to deal with bitfields specially
3959static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,
3960 const Expr *E, APValue &Result,
3961 QualType ResultType,
3962 SmallVectorImpl<APValue> &Elements,
3963 SmallVectorImpl<QualType> &ElTypes) {
3964
3965 SmallVector<std::tuple<APValue *, QualType, unsigned>> WorkList = {
3966 {&Result, ResultType, 0}};
3967
3968 unsigned ElI = 0;
3969 while (!WorkList.empty() && ElI < Elements.size()) {
3970 auto [Res, Type, BitWidth] = WorkList.pop_back_val();
3971
3972 if (Type->isRealFloatingType()) {
3973 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3974 Result&: *Res))
3975 return false;
3976 ElI++;
3977 continue;
3978 }
3979 if (Type->isIntegerType()) {
3980 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3981 Result&: *Res))
3982 return false;
3983 if (BitWidth > 0) {
3984 if (!Res->isInt())
3985 return false;
3986 APSInt &Int = Res->getInt();
3987 unsigned OldBitWidth = Int.getBitWidth();
3988 unsigned NewBitWidth = BitWidth;
3989 if (NewBitWidth < OldBitWidth)
3990 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
3991 }
3992 ElI++;
3993 continue;
3994 }
3995 if (Type->isVectorType()) {
3996 QualType ElTy = Type->castAs<VectorType>()->getElementType();
3997 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
3998 SmallVector<APValue> Vals(NumEl);
3999 for (unsigned I = 0; I < NumEl; ++I) {
4000 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: ElTy, Original: Elements[ElI],
4001 Result&: Vals[I]))
4002 return false;
4003 ElI++;
4004 }
4005 *Res = APValue(Vals.data(), NumEl);
4006 continue;
4007 }
4008 if (Type->isConstantArrayType()) {
4009 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4010 ->getElementType();
4011 uint64_t Size =
4012 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
4013 *Res = APValue(APValue::UninitArray(), Size, Size);
4014 for (int64_t I = Size - 1; I > -1; --I)
4015 WorkList.emplace_back(Args: &Res->getArrayInitializedElt(I), Args&: ElTy, Args: 0u);
4016 continue;
4017 }
4018 if (Type->isRecordType()) {
4019 const RecordDecl *RD = Type->getAsRecordDecl();
4020
4021 unsigned NumBases = 0;
4022 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
4023 NumBases = CXXRD->getNumBases();
4024
4025 *Res = APValue(APValue::UninitStruct(), NumBases, RD->getNumFields());
4026
4027 SmallVector<std::tuple<APValue *, QualType, unsigned>> ReverseList;
4028 // we need to traverse backwards
4029 // Visit the base classes.
4030 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4031 if (CXXRD->getNumBases() > 0) {
4032 assert(CXXRD->getNumBases() == 1);
4033 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4034 ReverseList.emplace_back(Args: &Res->getStructBase(i: 0), Args: BS.getType(), Args: 0u);
4035 }
4036 }
4037
4038 // Visit the fields.
4039 for (FieldDecl *FD : RD->fields()) {
4040 unsigned FDBW = 0;
4041 if (FD->isUnnamedBitField())
4042 continue;
4043 if (FD->isBitField()) {
4044 FDBW = FD->getBitWidthValue();
4045 }
4046
4047 ReverseList.emplace_back(Args: &Res->getStructField(i: FD->getFieldIndex()),
4048 Args: FD->getType(), Args&: FDBW);
4049 }
4050
4051 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
4052 llvm::append_range(C&: WorkList, R&: ReverseList);
4053 continue;
4054 }
4055 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4056 return false;
4057 }
4058 return true;
4059}
4060
4061static bool handleElementwiseCast(EvalInfo &Info, const Expr *E,
4062 const FPOptions FPO,
4063 SmallVectorImpl<APValue> &Elements,
4064 SmallVectorImpl<QualType> &SrcTypes,
4065 SmallVectorImpl<QualType> &DestTypes,
4066 SmallVectorImpl<APValue> &Results) {
4067
4068 assert((Elements.size() == SrcTypes.size()) &&
4069 (Elements.size() == DestTypes.size()));
4070
4071 for (unsigned I = 0, ESz = Elements.size(); I < ESz; ++I) {
4072 APValue Original = Elements[I];
4073 QualType SourceTy = SrcTypes[I];
4074 QualType DestTy = DestTypes[I];
4075
4076 if (!handleScalarCast(Info, FPO, E, SourceTy, DestTy, Original, Result&: Results[I]))
4077 return false;
4078 }
4079 return true;
4080}
4081
4082static unsigned elementwiseSize(EvalInfo &Info, QualType BaseTy) {
4083
4084 SmallVector<QualType> WorkList = {BaseTy};
4085
4086 unsigned Size = 0;
4087 while (!WorkList.empty()) {
4088 QualType Type = WorkList.pop_back_val();
4089 if (Type->isRealFloatingType() || Type->isIntegerType() ||
4090 Type->isBooleanType()) {
4091 ++Size;
4092 continue;
4093 }
4094 if (Type->isVectorType()) {
4095 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
4096 Size += NumEl;
4097 continue;
4098 }
4099 if (Type->isConstantMatrixType()) {
4100 unsigned NumEl =
4101 Type->castAs<ConstantMatrixType>()->getNumElementsFlattened();
4102 Size += NumEl;
4103 continue;
4104 }
4105 if (Type->isConstantArrayType()) {
4106 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4107 ->getElementType();
4108 uint64_t ArrSize =
4109 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
4110 for (uint64_t I = 0; I < ArrSize; ++I) {
4111 WorkList.push_back(Elt: ElTy);
4112 }
4113 continue;
4114 }
4115 if (Type->isRecordType()) {
4116 const RecordDecl *RD = Type->getAsRecordDecl();
4117
4118 // Visit the base classes.
4119 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4120 if (CXXRD->getNumBases() > 0) {
4121 assert(CXXRD->getNumBases() == 1);
4122 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4123 WorkList.push_back(Elt: BS.getType());
4124 }
4125 }
4126
4127 // visit the fields.
4128 for (FieldDecl *FD : RD->fields()) {
4129 if (FD->isUnnamedBitField())
4130 continue;
4131 WorkList.push_back(Elt: FD->getType());
4132 }
4133 continue;
4134 }
4135 }
4136 return Size;
4137}
4138
4139static bool hlslAggSplatHelper(EvalInfo &Info, const Expr *E, APValue &SrcVal,
4140 QualType &SrcTy) {
4141 SrcTy = E->getType();
4142
4143 if (!Evaluate(Result&: SrcVal, Info, E))
4144 return false;
4145
4146 assert((SrcVal.isFloat() || SrcVal.isInt() ||
4147 (SrcVal.isVector() && SrcVal.getVectorLength() == 1)) &&
4148 "Not a valid HLSLAggregateSplatCast.");
4149
4150 if (SrcVal.isVector()) {
4151 assert(SrcTy->isVectorType() && "Type mismatch.");
4152 SrcTy = SrcTy->castAs<VectorType>()->getElementType();
4153 SrcVal = SrcVal.getVectorElt(I: 0);
4154 }
4155 if (SrcVal.isMatrix()) {
4156 assert(SrcTy->isConstantMatrixType() && "Type mismatch.");
4157 SrcTy = SrcTy->castAs<ConstantMatrixType>()->getElementType();
4158 SrcVal = SrcVal.getMatrixElt(Row: 0, Col: 0);
4159 }
4160 return true;
4161}
4162
4163static bool flattenAPValue(EvalInfo &Info, const Expr *E, APValue Value,
4164 QualType BaseTy, SmallVectorImpl<APValue> &Elements,
4165 SmallVectorImpl<QualType> &Types, unsigned Size) {
4166
4167 SmallVector<std::pair<APValue, QualType>> WorkList = {{Value, BaseTy}};
4168 unsigned Populated = 0;
4169 while (!WorkList.empty() && Populated < Size) {
4170 auto [Work, Type] = WorkList.pop_back_val();
4171
4172 if (Work.isFloat() || Work.isInt()) {
4173 Elements.push_back(Elt: Work);
4174 Types.push_back(Elt: Type);
4175 Populated++;
4176 continue;
4177 }
4178 if (Work.isVector()) {
4179 assert(Type->isVectorType() && "Type mismatch.");
4180 QualType ElTy = Type->castAs<VectorType>()->getElementType();
4181 for (unsigned I = 0; I < Work.getVectorLength() && Populated < Size;
4182 I++) {
4183 Elements.push_back(Elt: Work.getVectorElt(I));
4184 Types.push_back(Elt: ElTy);
4185 Populated++;
4186 }
4187 continue;
4188 }
4189 if (Work.isMatrix()) {
4190 assert(Type->isConstantMatrixType() && "Type mismatch.");
4191 const auto *MT = Type->castAs<ConstantMatrixType>();
4192 QualType ElTy = MT->getElementType();
4193 // Matrix elements are flattened in row-major order.
4194 for (unsigned Row = 0; Row < Work.getMatrixNumRows() && Populated < Size;
4195 Row++) {
4196 for (unsigned Col = 0;
4197 Col < Work.getMatrixNumColumns() && Populated < Size; Col++) {
4198 Elements.push_back(Elt: Work.getMatrixElt(Row, Col));
4199 Types.push_back(Elt: ElTy);
4200 Populated++;
4201 }
4202 }
4203 continue;
4204 }
4205 if (Work.isArray()) {
4206 assert(Type->isConstantArrayType() && "Type mismatch.");
4207 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4208 ->getElementType();
4209 for (int64_t I = Work.getArraySize() - 1; I > -1; --I) {
4210 WorkList.emplace_back(Args&: Work.getArrayInitializedElt(I), Args&: ElTy);
4211 }
4212 continue;
4213 }
4214
4215 if (Work.isStruct()) {
4216 assert(Type->isRecordType() && "Type mismatch.");
4217
4218 const RecordDecl *RD = Type->getAsRecordDecl();
4219
4220 SmallVector<std::pair<APValue, QualType>> ReverseList;
4221 // Visit the fields.
4222 for (FieldDecl *FD : RD->fields()) {
4223 if (FD->isUnnamedBitField())
4224 continue;
4225 ReverseList.emplace_back(Args&: Work.getStructField(i: FD->getFieldIndex()),
4226 Args: FD->getType());
4227 }
4228
4229 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
4230 llvm::append_range(C&: WorkList, R&: ReverseList);
4231
4232 // Visit the base classes.
4233 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4234 if (CXXRD->getNumBases() > 0) {
4235 assert(CXXRD->getNumBases() == 1);
4236 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4237 const APValue &Base = Work.getStructBase(i: 0);
4238
4239 // Can happen in error cases.
4240 if (!Base.isStruct())
4241 return false;
4242
4243 WorkList.emplace_back(Args: Base, Args: BS.getType());
4244 }
4245 }
4246 continue;
4247 }
4248 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4249 return false;
4250 }
4251 return true;
4252}
4253
4254namespace {
4255/// A handle to a complete object (an object that is not a subobject of
4256/// another object).
4257struct CompleteObject {
4258 /// The identity of the object.
4259 APValue::LValueBase Base;
4260 /// The value of the complete object.
4261 APValue *Value;
4262 /// The type of the complete object.
4263 QualType Type;
4264
4265 CompleteObject() : Value(nullptr) {}
4266 CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type)
4267 : Base(Base), Value(Value), Type(Type) {}
4268
4269 bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const {
4270 // If this isn't a "real" access (eg, if it's just accessing the type
4271 // info), allow it. We assume the type doesn't change dynamically for
4272 // subobjects of constexpr objects (even though we'd hit UB here if it
4273 // did). FIXME: Is this right?
4274 if (!isAnyAccess(AK))
4275 return true;
4276
4277 // In C++14 onwards, it is permitted to read a mutable member whose
4278 // lifetime began within the evaluation.
4279 // FIXME: Should we also allow this in C++11?
4280 if (!Info.getLangOpts().CPlusPlus14 &&
4281 AK != AccessKinds::AK_IsWithinLifetime)
4282 return false;
4283 return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true);
4284 }
4285
4286 explicit operator bool() const { return !Type.isNull(); }
4287};
4288} // end anonymous namespace
4289
4290static QualType getSubobjectType(QualType ObjType, QualType SubobjType,
4291 bool IsMutable = false) {
4292 // C++ [basic.type.qualifier]p1:
4293 // - A const object is an object of type const T or a non-mutable subobject
4294 // of a const object.
4295 if (ObjType.isConstQualified() && !IsMutable)
4296 SubobjType.addConst();
4297 // - A volatile object is an object of type const T or a subobject of a
4298 // volatile object.
4299 if (ObjType.isVolatileQualified())
4300 SubobjType.addVolatile();
4301 return SubobjType;
4302}
4303
4304/// Find the designated sub-object of an rvalue.
4305template <typename SubobjectHandler>
4306static typename SubobjectHandler::result_type
4307findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
4308 const SubobjectDesignator &Sub, SubobjectHandler &handler) {
4309 if (Sub.Invalid)
4310 // A diagnostic will have already been produced.
4311 return handler.failed();
4312 if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) {
4313 if (Info.getLangOpts().CPlusPlus11)
4314 Info.FFDiag(E, DiagId: Sub.isOnePastTheEnd()
4315 ? diag::note_constexpr_access_past_end
4316 : diag::note_constexpr_access_unsized_array)
4317 << handler.AccessKind;
4318 else
4319 Info.FFDiag(E);
4320 return handler.failed();
4321 }
4322
4323 APValue *O = Obj.Value;
4324 QualType ObjType = Obj.Type;
4325 const FieldDecl *LastField = nullptr;
4326 const FieldDecl *VolatileField = nullptr;
4327
4328 // Walk the designator's path to find the subobject.
4329 for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) {
4330 // Reading an indeterminate value is undefined, but assigning over one is OK.
4331 if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) ||
4332 (O->isIndeterminate() &&
4333 !isValidIndeterminateAccess(handler.AccessKind))) {
4334 // Object has ended lifetime.
4335 // If I is non-zero, some subobject (member or array element) of a
4336 // complete object has ended its lifetime, so this is valid for
4337 // IsWithinLifetime, resulting in false.
4338 if (I != 0 && handler.AccessKind == AK_IsWithinLifetime)
4339 return false;
4340 if (!Info.checkingPotentialConstantExpression()) {
4341 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
4342 << handler.AccessKind << O->isIndeterminate()
4343 << E->getSourceRange();
4344 NoteLValueLocation(Info, Base: Obj.Base);
4345 }
4346 return handler.failed();
4347 }
4348
4349 // C++ [class.ctor]p5, C++ [class.dtor]p5:
4350 // const and volatile semantics are not applied on an object under
4351 // {con,de}struction.
4352 if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) &&
4353 ObjType->isRecordType() &&
4354 Info.isEvaluatingCtorDtor(
4355 Base: Obj.Base, Path: ArrayRef(Sub.Entries.begin(), Sub.Entries.begin() + I)) !=
4356 ConstructionPhase::None) {
4357 ObjType = Info.Ctx.getCanonicalType(T: ObjType);
4358 ObjType.removeLocalConst();
4359 ObjType.removeLocalVolatile();
4360 }
4361
4362 // If this is our last pass, check that the final object type is OK.
4363 if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) {
4364 // Accesses to volatile objects are prohibited.
4365 if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) {
4366 if (Info.getLangOpts().CPlusPlus) {
4367 int DiagKind;
4368 SourceLocation Loc;
4369 const NamedDecl *Decl = nullptr;
4370 if (VolatileField) {
4371 DiagKind = 2;
4372 Loc = VolatileField->getLocation();
4373 Decl = VolatileField;
4374 } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) {
4375 DiagKind = 1;
4376 Loc = VD->getLocation();
4377 Decl = VD;
4378 } else {
4379 DiagKind = 0;
4380 if (auto *E = Obj.Base.dyn_cast<const Expr *>())
4381 Loc = E->getExprLoc();
4382 }
4383 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_obj, ExtraNotes: 1)
4384 << handler.AccessKind << DiagKind << Decl;
4385 Info.Note(Loc, DiagId: diag::note_constexpr_volatile_here) << DiagKind;
4386 } else {
4387 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4388 }
4389 return handler.failed();
4390 }
4391
4392 // If we are reading an object of class type, there may still be more
4393 // things we need to check: if there are any mutable subobjects, we
4394 // cannot perform this read. (This only happens when performing a trivial
4395 // copy or assignment.)
4396 if (ObjType->isRecordType() &&
4397 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind) &&
4398 diagnoseMutableFields(Info, E, handler.AccessKind, ObjType))
4399 return handler.failed();
4400 }
4401
4402 if (I == N) {
4403 if (!handler.found(*O, ObjType, Obj.Base))
4404 return false;
4405
4406 // If we modified a bit-field, truncate it to the right width.
4407 if (isModification(handler.AccessKind) &&
4408 LastField && LastField->isBitField() &&
4409 !truncateBitfieldValue(Info, E, Value&: *O, FD: LastField))
4410 return false;
4411
4412 return true;
4413 }
4414
4415 LastField = nullptr;
4416
4417 // The value of an atomic object is represented like a value of the
4418 // underlying type, so look through the _Atomic wrapper.
4419 if (const AtomicType *AT = ObjType->getAs<AtomicType>())
4420 ObjType = Info.Ctx.getQualifiedType(T: AT->getValueType(),
4421 Qs: ObjType.getQualifiers());
4422
4423 if (ObjType->isArrayType()) {
4424 // Next subobject is an array element.
4425 const ArrayType *AT = Info.Ctx.getAsArrayType(T: ObjType);
4426 assert((isa<ConstantArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
4427 "vla in literal type?");
4428 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4429 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4430 CAT && CAT->getSize().ule(RHS: Index)) {
4431 // Note, it should not be possible to form a pointer with a valid
4432 // designator which points more than one past the end of the array.
4433 if (Info.getLangOpts().CPlusPlus11)
4434 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4435 << handler.AccessKind;
4436 else
4437 Info.FFDiag(E);
4438 return handler.failed();
4439 }
4440
4441 ObjType = AT->getElementType();
4442
4443 if (O->getArrayInitializedElts() > Index)
4444 O = &O->getArrayInitializedElt(I: Index);
4445 else if (!isRead(handler.AccessKind)) {
4446 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4447 CAT && !CheckArraySize(Info, CAT, CallLoc: E->getExprLoc()))
4448 return handler.failed();
4449
4450 expandArray(Array&: *O, Index);
4451 O = &O->getArrayInitializedElt(I: Index);
4452 } else
4453 O = &O->getArrayFiller();
4454 } else if (ObjType->isAnyComplexType()) {
4455 // Next subobject is a complex number.
4456 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4457 if (Index > 1) {
4458 if (Info.getLangOpts().CPlusPlus11)
4459 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4460 << handler.AccessKind;
4461 else
4462 Info.FFDiag(E);
4463 return handler.failed();
4464 }
4465
4466 ObjType = getSubobjectType(
4467 ObjType, SubobjType: ObjType->castAs<ComplexType>()->getElementType());
4468
4469 assert(I == N - 1 && "extracting subobject of scalar?");
4470 if (O->isComplexInt()) {
4471 return handler.found(Index ? O->getComplexIntImag()
4472 : O->getComplexIntReal(), ObjType);
4473 } else {
4474 assert(O->isComplexFloat());
4475 return handler.found(Index ? O->getComplexFloatImag()
4476 : O->getComplexFloatReal(), ObjType);
4477 }
4478 } else if (const auto *VT = ObjType->getAs<VectorType>()) {
4479 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4480 unsigned NumElements = VT->getNumElements();
4481 if (Index == NumElements) {
4482 if (Info.getLangOpts().CPlusPlus11)
4483 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4484 << handler.AccessKind;
4485 else
4486 Info.FFDiag(E);
4487 return handler.failed();
4488 }
4489
4490 if (Index > NumElements) {
4491 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
4492 << Index << /*array*/ 0 << NumElements;
4493 return handler.failed();
4494 }
4495
4496 ObjType = VT->getElementType();
4497 assert(I == N - 1 && "extracting subobject of scalar?");
4498
4499 if (O->isIndeterminate()) {
4500 if (isRead(handler.AccessKind)) {
4501 Info.FFDiag(E);
4502 return handler.failed();
4503 }
4504 expandVector(Vec&: *O, NumElements);
4505 }
4506 assert(O->isVector() && "unexpected object during vector element access");
4507 return handler.found(O->getVectorElt(I: Index), ObjType, Obj.Base);
4508 } else if (const FieldDecl *Field = getAsField(E: Sub.Entries[I])) {
4509 if (Field->isMutable() &&
4510 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind)) {
4511 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1)
4512 << handler.AccessKind << Field;
4513 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
4514 return handler.failed();
4515 }
4516
4517 // Next subobject is a class, struct or union field.
4518 RecordDecl *RD = ObjType->castAsCanonical<RecordType>()->getDecl();
4519 if (RD->isUnion()) {
4520 const FieldDecl *UnionField = O->getUnionField();
4521 if (!UnionField ||
4522 UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {
4523 if (I == N - 1 && handler.AccessKind == AK_Construct) {
4524 // Placement new onto an inactive union member makes it active.
4525 O->setUnion(Field, Value: APValue());
4526 } else {
4527 // Pointer to/into inactive union member: Not within lifetime
4528 if (handler.AccessKind == AK_IsWithinLifetime)
4529 return false;
4530 // FIXME: If O->getUnionValue() is absent, report that there's no
4531 // active union member rather than reporting the prior active union
4532 // member. We'll need to fix nullptr_t to not use APValue() as its
4533 // representation first.
4534 Info.FFDiag(E, DiagId: diag::note_constexpr_access_inactive_union_member)
4535 << handler.AccessKind << Field << !UnionField << UnionField;
4536 return handler.failed();
4537 }
4538 }
4539 O = &O->getUnionValue();
4540 } else
4541 O = &O->getStructField(i: Field->getFieldIndex());
4542
4543 ObjType = getSubobjectType(ObjType, SubobjType: Field->getType(), IsMutable: Field->isMutable());
4544 LastField = Field;
4545 if (Field->getType().isVolatileQualified())
4546 VolatileField = Field;
4547 } else {
4548 // Next subobject is a base class.
4549 const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();
4550 const CXXRecordDecl *Base = getAsBaseClass(E: Sub.Entries[I]);
4551
4552 unsigned BaseIndex = getBaseIndex(Derived, Base);
4553 unsigned NumNonVirtualBases = O->getStructNumBases();
4554 if (BaseIndex >= NumNonVirtualBases) {
4555 O = &O->getStructVirtualBase(i: BaseIndex - NumNonVirtualBases);
4556 } else
4557 O = &O->getStructBase(i: BaseIndex);
4558
4559 ObjType = getSubobjectType(ObjType, SubobjType: Info.Ctx.getCanonicalTagType(TD: Base));
4560 }
4561 }
4562}
4563
4564namespace {
4565struct ExtractSubobjectHandler {
4566 EvalInfo &Info;
4567 const Expr *E;
4568 APValue &Result;
4569 const AccessKinds AccessKind;
4570
4571 typedef bool result_type;
4572 bool failed() { return false; }
4573 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4574 Result = Subobj;
4575 if (AccessKind == AK_ReadObjectRepresentation)
4576 return true;
4577 return CheckFullyInitialized(Info, DiagLoc: E->getExprLoc(), Type: SubobjType, Value: Result);
4578 }
4579 bool found(APSInt &Value, QualType SubobjType) {
4580 Result = APValue(Value);
4581 return true;
4582 }
4583 bool found(APFloat &Value, QualType SubobjType) {
4584 Result = APValue(Value);
4585 return true;
4586 }
4587};
4588} // end anonymous namespace
4589
4590/// Extract the designated sub-object of an rvalue.
4591static bool extractSubobject(EvalInfo &Info, const Expr *E,
4592 const CompleteObject &Obj,
4593 const SubobjectDesignator &Sub, APValue &Result,
4594 AccessKinds AK = AK_Read) {
4595 assert(AK == AK_Read || AK == AK_ReadObjectRepresentation);
4596 ExtractSubobjectHandler Handler = {.Info: Info, .E: E, .Result: Result, .AccessKind: AK};
4597 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4598}
4599
4600namespace {
4601struct ModifySubobjectHandler {
4602 EvalInfo &Info;
4603 APValue &NewVal;
4604 const Expr *E;
4605
4606 typedef bool result_type;
4607 static const AccessKinds AccessKind = AK_Assign;
4608
4609 bool checkConst(QualType QT) {
4610 // Assigning to a const object has undefined behavior.
4611 if (QT.isConstQualified()) {
4612 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
4613 return false;
4614 }
4615 return true;
4616 }
4617
4618 bool failed() { return false; }
4619 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4620 if (!checkConst(QT: SubobjType))
4621 return false;
4622 // We've been given ownership of NewVal, so just swap it in.
4623 Subobj.swap(RHS&: NewVal);
4624 return true;
4625 }
4626 bool found(APSInt &Value, QualType SubobjType) {
4627 if (!checkConst(QT: SubobjType))
4628 return false;
4629 if (!NewVal.isInt()) {
4630 // Maybe trying to write a cast pointer value into a complex?
4631 Info.FFDiag(E);
4632 return false;
4633 }
4634 Value = NewVal.getInt();
4635 return true;
4636 }
4637 bool found(APFloat &Value, QualType SubobjType) {
4638 if (!checkConst(QT: SubobjType))
4639 return false;
4640 Value = NewVal.getFloat();
4641 return true;
4642 }
4643};
4644} // end anonymous namespace
4645
4646const AccessKinds ModifySubobjectHandler::AccessKind;
4647
4648/// Update the designated sub-object of an rvalue to the given value.
4649static bool modifySubobject(EvalInfo &Info, const Expr *E,
4650 const CompleteObject &Obj,
4651 const SubobjectDesignator &Sub,
4652 APValue &NewVal) {
4653 ModifySubobjectHandler Handler = { .Info: Info, .NewVal: NewVal, .E: E };
4654 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4655}
4656
4657/// Find the position where two subobject designators diverge, or equivalently
4658/// the length of the common initial subsequence.
4659static unsigned FindDesignatorMismatch(QualType ObjType,
4660 const SubobjectDesignator &A,
4661 const SubobjectDesignator &B,
4662 bool &WasArrayIndex) {
4663 unsigned I = 0, N = std::min(a: A.Entries.size(), b: B.Entries.size());
4664 for (/**/; I != N; ++I) {
4665 if (!ObjType.isNull() &&
4666 (ObjType->isArrayType() || ObjType->isAnyComplexType())) {
4667 // Next subobject is an array element.
4668 if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) {
4669 WasArrayIndex = true;
4670 return I;
4671 }
4672 if (ObjType->isAnyComplexType())
4673 ObjType = ObjType->castAs<ComplexType>()->getElementType();
4674 else
4675 ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();
4676 } else {
4677 if (A.Entries[I].getAsBaseOrMember() !=
4678 B.Entries[I].getAsBaseOrMember()) {
4679 WasArrayIndex = false;
4680 return I;
4681 }
4682 if (const FieldDecl *FD = getAsField(E: A.Entries[I]))
4683 // Next subobject is a field.
4684 ObjType = FD->getType();
4685 else
4686 // Next subobject is a base class.
4687 ObjType = QualType();
4688 }
4689 }
4690 WasArrayIndex = false;
4691 return I;
4692}
4693
4694/// Determine whether the given subobject designators refer to elements of the
4695/// same array object.
4696static bool AreElementsOfSameArray(QualType ObjType,
4697 const SubobjectDesignator &A,
4698 const SubobjectDesignator &B) {
4699 if (A.Entries.size() != B.Entries.size())
4700 return false;
4701
4702 bool IsArray = A.MostDerivedIsArrayElement;
4703 if (IsArray && A.MostDerivedPathLength != A.Entries.size())
4704 // A is a subobject of the array element.
4705 return false;
4706
4707 // If A (and B) designates an array element, the last entry will be the array
4708 // index. That doesn't have to match. Otherwise, we're in the 'implicit array
4709 // of length 1' case, and the entire path must match.
4710 bool WasArrayIndex;
4711 unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);
4712 return CommonLength >= A.Entries.size() - IsArray;
4713}
4714
4715/// Find the complete object to which an LValue refers.
4716static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
4717 AccessKinds AK, const LValue &LVal,
4718 QualType LValType) {
4719 if (LVal.InvalidBase) {
4720 Info.FFDiag(E);
4721 return CompleteObject();
4722 }
4723
4724 if (!LVal.Base) {
4725 if (AK == AccessKinds::AK_Dereference)
4726 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
4727 else
4728 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
4729 return CompleteObject();
4730 }
4731
4732 CallStackFrame *Frame = nullptr;
4733 unsigned Depth = 0;
4734 if (LVal.getLValueCallIndex()) {
4735 std::tie(args&: Frame, args&: Depth) =
4736 Info.getCallFrameAndDepth(CallIndex: LVal.getLValueCallIndex());
4737 if (!Frame) {
4738 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit, ExtraNotes: 1)
4739 << AK << /*Indeterminate=*/false << E->getSourceRange();
4740 NoteLValueLocation(Info, Base: LVal.Base);
4741 return CompleteObject();
4742 }
4743 }
4744
4745 bool IsAccess = isAnyAccess(AK);
4746
4747 // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type
4748 // is not a constant expression (even if the object is non-volatile). We also
4749 // apply this rule to C++98, in order to conform to the expected 'volatile'
4750 // semantics.
4751 if (isFormalAccess(AK) && LValType.isVolatileQualified()) {
4752 if (Info.getLangOpts().CPlusPlus)
4753 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_type)
4754 << AK << LValType;
4755 else
4756 Info.FFDiag(E);
4757 return CompleteObject();
4758 }
4759
4760 // Compute value storage location and type of base object.
4761 APValue *BaseVal = nullptr;
4762 QualType BaseType = getType(B: LVal.Base);
4763
4764 if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl &&
4765 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4766 // This is the object whose initializer we're evaluating, so its lifetime
4767 // started in the current evaluation.
4768 BaseVal = Info.EvaluatingDeclValue;
4769 } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) {
4770 // Allow reading from a GUID declaration.
4771 if (auto *GD = dyn_cast<MSGuidDecl>(Val: D)) {
4772 if (isModification(AK)) {
4773 // All the remaining cases do not permit modification of the object.
4774 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4775 return CompleteObject();
4776 }
4777 APValue &V = GD->getAsAPValue();
4778 if (V.isAbsent()) {
4779 Info.FFDiag(E, DiagId: diag::note_constexpr_unsupported_layout)
4780 << GD->getType();
4781 return CompleteObject();
4782 }
4783 return CompleteObject(LVal.Base, &V, GD->getType());
4784 }
4785
4786 // Allow reading the APValue from an UnnamedGlobalConstantDecl.
4787 if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(Val: D)) {
4788 if (isModification(AK)) {
4789 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4790 return CompleteObject();
4791 }
4792 return CompleteObject(LVal.Base, const_cast<APValue *>(&GCD->getValue()),
4793 GCD->getType());
4794 }
4795
4796 // Allow reading from template parameter objects.
4797 if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: D)) {
4798 if (isModification(AK)) {
4799 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4800 return CompleteObject();
4801 }
4802 return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()),
4803 TPO->getType());
4804 }
4805
4806 // In C++98, const, non-volatile integers initialized with ICEs are ICEs.
4807 // In C++11, constexpr, non-volatile variables initialized with constant
4808 // expressions are constant expressions too. Inside constexpr functions,
4809 // parameters are constant expressions even if they're non-const.
4810 // In C++1y, objects local to a constant expression (those with a Frame) are
4811 // both readable and writable inside constant expressions.
4812 // In C, such things can also be folded, although they are not ICEs.
4813 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
4814 if (VD) {
4815 if (const VarDecl *VDef = VD->getDefinition(C&: Info.Ctx))
4816 VD = VDef;
4817 }
4818 if (!VD || VD->isInvalidDecl()) {
4819 Info.FFDiag(E);
4820 return CompleteObject();
4821 }
4822
4823 bool IsConstant = BaseType.isConstant(Ctx: Info.Ctx);
4824 bool ConstexprVar = false;
4825 if (const auto *VD = dyn_cast_if_present<VarDecl>(
4826 Val: Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()))
4827 ConstexprVar = VD->isConstexpr();
4828
4829 // Unless we're looking at a local variable or argument in a constexpr call,
4830 // the variable we're reading must be const (unless we are binding to a
4831 // reference).
4832 if (AK != clang::AK_Dereference && !Frame) {
4833 if (IsAccess && isa<ParmVarDecl>(Val: VD)) {
4834 // Access of a parameter that's not associated with a frame isn't going
4835 // to work out, but we can leave it to evaluateVarDeclInit to provide a
4836 // suitable diagnostic.
4837 } else if (Info.getLangOpts().CPlusPlus14 &&
4838 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4839 // OK, we can read and modify an object if we're in the process of
4840 // evaluating its initializer, because its lifetime began in this
4841 // evaluation.
4842 } else if (isModification(AK)) {
4843 // All the remaining cases do not permit modification of the object.
4844 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4845 return CompleteObject();
4846 } else if (VD->isConstexpr()) {
4847 // OK, we can read this variable.
4848 } else if (Info.getLangOpts().C23 && ConstexprVar) {
4849 Info.FFDiag(E);
4850 return CompleteObject();
4851 } else if (VD->isCXXForRangeImplicitVar()) {
4852 if (!IsAccess)
4853 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4854 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_for_range_var) << VD;
4855 return CompleteObject();
4856 } else if (BaseType->isIntegralOrEnumerationType()) {
4857 if (!IsConstant) {
4858 if (!IsAccess)
4859 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4860 if (Info.getLangOpts().CPlusPlus) {
4861 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_non_const_int, ExtraNotes: 1) << VD;
4862 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4863 } else {
4864 Info.FFDiag(E);
4865 }
4866 return CompleteObject();
4867 }
4868 } else if (!IsAccess) {
4869 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4870 } else if ((IsConstant || BaseType->isReferenceType()) &&
4871 Info.checkingPotentialConstantExpression() &&
4872 BaseType->isLiteralType(Ctx: Info.Ctx) && !VD->hasDefinition()) {
4873 // This variable might end up being constexpr. Don't diagnose it yet.
4874 } else if (IsConstant) {
4875 // Keep evaluating to see what we can do. In particular, we support
4876 // folding of const floating-point types, in order to make static const
4877 // data members of such types (supported as an extension) more useful.
4878 if (Info.getLangOpts().CPlusPlus) {
4879 Info.CCEDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4880 ? diag::note_constexpr_ltor_non_constexpr
4881 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4882 << VD << BaseType;
4883 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4884 } else {
4885 Info.CCEDiag(E);
4886 }
4887 } else {
4888 // Never allow reading a non-const value.
4889 if (Info.getLangOpts().CPlusPlus) {
4890 Info.FFDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4891 ? diag::note_constexpr_ltor_non_constexpr
4892 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4893 << VD << BaseType;
4894 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4895 } else {
4896 Info.FFDiag(E);
4897 }
4898 return CompleteObject();
4899 }
4900 }
4901
4902 // When binding to a reference, the variable does not need to be constexpr
4903 // or have constant initalization.
4904 if (AK != clang::AK_Dereference &&
4905 !evaluateVarDeclInit(Info, E, VD, Frame, Version: LVal.getLValueVersion(),
4906 Result&: BaseVal))
4907 return CompleteObject();
4908 // If evaluateVarDeclInit sees a constexpr-unknown variable, it returns
4909 // a null BaseVal. Any constexpr-unknown variable seen here is an error:
4910 // we can't access a constexpr-unknown object.
4911 if (AK != clang::AK_Dereference && !BaseVal) {
4912 if (!Info.checkingPotentialConstantExpression()) {
4913 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unknown_variable, ExtraNotes: 1)
4914 << AK << VD;
4915 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4916 }
4917 return CompleteObject();
4918 }
4919 } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) {
4920 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
4921 if (!Alloc) {
4922 Info.FFDiag(E, DiagId: diag::note_constexpr_access_deleted_object) << AK;
4923 return CompleteObject();
4924 }
4925 return CompleteObject(LVal.Base, &(*Alloc)->Value,
4926 LVal.Base.getDynamicAllocType());
4927 }
4928 // When binding to a reference, the variable does not need to be
4929 // within its lifetime.
4930 else if (AK != clang::AK_Dereference) {
4931 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
4932
4933 if (!Frame) {
4934 if (const MaterializeTemporaryExpr *MTE =
4935 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: Base)) {
4936 assert(MTE->getStorageDuration() == SD_Static &&
4937 "should have a frame for a non-global materialized temporary");
4938
4939 // C++20 [expr.const]p4: [DR2126]
4940 // An object or reference is usable in constant expressions if it is
4941 // - a temporary object of non-volatile const-qualified literal type
4942 // whose lifetime is extended to that of a variable that is usable
4943 // in constant expressions
4944 //
4945 // C++20 [expr.const]p5:
4946 // an lvalue-to-rvalue conversion [is not allowed unless it applies to]
4947 // - a non-volatile glvalue that refers to an object that is usable
4948 // in constant expressions, or
4949 // - a non-volatile glvalue of literal type that refers to a
4950 // non-volatile object whose lifetime began within the evaluation
4951 // of E;
4952 //
4953 // C++11 misses the 'began within the evaluation of e' check and
4954 // instead allows all temporaries, including things like:
4955 // int &&r = 1;
4956 // int x = ++r;
4957 // constexpr int k = r;
4958 // Therefore we use the C++14-onwards rules in C++11 too.
4959 //
4960 // Note that temporaries whose lifetimes began while evaluating a
4961 // variable's constructor are not usable while evaluating the
4962 // corresponding destructor, not even if they're of const-qualified
4963 // types.
4964 if (!MTE->isUsableInConstantExpressions(Context: Info.Ctx) &&
4965 !lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4966 if (!IsAccess)
4967 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4968 Info.FFDiag(E, DiagId: diag::note_constexpr_access_static_temporary, ExtraNotes: 1) << AK;
4969 Info.Note(Loc: MTE->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
4970 return CompleteObject();
4971 }
4972
4973 BaseVal = MTE->getOrCreateValue(MayCreate: false);
4974 assert(BaseVal && "got reference to unevaluated temporary");
4975 } else if (const CompoundLiteralExpr *CLE =
4976 dyn_cast_or_null<CompoundLiteralExpr>(Val: Base)) {
4977 // According to GCC info page:
4978 //
4979 // 6.28 Compound Literals
4980 //
4981 // As an optimization, G++ sometimes gives array compound literals
4982 // longer lifetimes: when the array either appears outside a function or
4983 // has a const-qualified type. If foo and its initializer had elements
4984 // of type char *const rather than char *, or if foo were a global
4985 // variable, the array would have static storage duration. But it is
4986 // probably safest just to avoid the use of array compound literals in
4987 // C++ code.
4988 //
4989 // Obey that rule by checking constness for converted array types.
4990 if (QualType CLETy = CLE->getType(); CLETy->isArrayType() &&
4991 !LValType->isArrayType() &&
4992 !CLETy.isConstant(Ctx: Info.Ctx)) {
4993 Info.FFDiag(E);
4994 Info.Note(Loc: CLE->getExprLoc(), DiagId: diag::note_declared_at);
4995 return CompleteObject();
4996 }
4997
4998 BaseVal = &CLE->getStaticValue();
4999 } else {
5000 if (!IsAccess)
5001 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
5002 APValue Val;
5003 LVal.moveInto(V&: Val);
5004 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unreadable_object)
5005 << AK
5006 << Val.getAsString(Ctx: Info.Ctx,
5007 Ty: Info.Ctx.getLValueReferenceType(T: LValType));
5008 NoteLValueLocation(Info, Base: LVal.Base);
5009 return CompleteObject();
5010 }
5011 } else if (AK != clang::AK_Dereference) {
5012 BaseVal = Frame->getTemporary(Key: Base, Version: LVal.Base.getVersion());
5013 assert(BaseVal && "missing value for temporary");
5014 }
5015 }
5016
5017 // In C++14, we can't safely access any mutable state when we might be
5018 // evaluating after an unmodeled side effect. Parameters are modeled as state
5019 // in the caller, but aren't visible once the call returns, so they can be
5020 // modified in a speculatively-evaluated call.
5021 //
5022 // FIXME: Not all local state is mutable. Allow local constant subobjects
5023 // to be read here (but take care with 'mutable' fields).
5024 unsigned VisibleDepth = Depth;
5025 if (llvm::isa_and_nonnull<ParmVarDecl>(
5026 Val: LVal.Base.dyn_cast<const ValueDecl *>()))
5027 ++VisibleDepth;
5028 if ((Frame && Info.getLangOpts().CPlusPlus14 &&
5029 Info.EvalStatus.HasSideEffects) ||
5030 (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth))
5031 return CompleteObject();
5032
5033 return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType);
5034}
5035
5036/// Perform an lvalue-to-rvalue conversion on the given glvalue. This
5037/// can also be used for 'lvalue-to-lvalue' conversions for looking up the
5038/// glvalue referred to by an entity of reference type.
5039///
5040/// \param Info - Information about the ongoing evaluation.
5041/// \param Conv - The expression for which we are performing the conversion.
5042/// Used for diagnostics.
5043/// \param Type - The type of the glvalue (before stripping cv-qualifiers in the
5044/// case of a non-class type).
5045/// \param LVal - The glvalue on which we are attempting to perform this action.
5046/// \param RVal - The produced value will be placed here.
5047/// \param WantObjectRepresentation - If true, we're looking for the object
5048/// representation rather than the value, and in particular,
5049/// there is no requirement that the result be fully initialized.
5050static bool
5051handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type,
5052 const LValue &LVal, APValue &RVal,
5053 bool WantObjectRepresentation = false) {
5054 if (LVal.Designator.Invalid)
5055 return false;
5056
5057 // Check for special cases where there is no existing APValue to look at.
5058 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
5059
5060 AccessKinds AK =
5061 WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read;
5062
5063 if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) {
5064 if (isa<StringLiteral>(Val: Base) || isa<PredefinedExpr>(Val: Base)) {
5065 // Special-case character extraction so we don't have to construct an
5066 // APValue for the whole string.
5067 assert(LVal.Designator.Entries.size() <= 1 &&
5068 "Can only read characters from string literals");
5069 if (LVal.Designator.Entries.empty()) {
5070 // Fail for now for LValue to RValue conversion of an array.
5071 // (This shouldn't show up in C/C++, but it could be triggered by a
5072 // weird EvaluateAsRValue call from a tool.)
5073 Info.FFDiag(E: Conv);
5074 return false;
5075 }
5076 if (LVal.Designator.isOnePastTheEnd()) {
5077 if (Info.getLangOpts().CPlusPlus11)
5078 Info.FFDiag(E: Conv, DiagId: diag::note_constexpr_access_past_end) << AK;
5079 else
5080 Info.FFDiag(E: Conv);
5081 return false;
5082 }
5083 uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex();
5084 RVal = APValue(extractStringLiteralCharacter(Info, Lit: Base, Index: CharIndex));
5085 return true;
5086 }
5087 }
5088
5089 CompleteObject Obj = findCompleteObject(Info, E: Conv, AK, LVal, LValType: Type);
5090 return Obj && extractSubobject(Info, E: Conv, Obj, Sub: LVal.Designator, Result&: RVal, AK);
5091}
5092
5093static bool hlslElementwiseCastHelper(EvalInfo &Info, const Expr *E,
5094 QualType DestTy,
5095 SmallVectorImpl<APValue> &SrcVals,
5096 SmallVectorImpl<QualType> &SrcTypes) {
5097 APValue Val;
5098 if (!Evaluate(Result&: Val, Info, E))
5099 return false;
5100
5101 // must be dealing with a record
5102 if (Val.isLValue()) {
5103 LValue LVal;
5104 LVal.setFrom(Ctx: Info.Ctx, V: Val);
5105 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal, RVal&: Val))
5106 return false;
5107 }
5108
5109 unsigned NEls = elementwiseSize(Info, BaseTy: DestTy);
5110 // flatten the source
5111 if (!flattenAPValue(Info, E, Value: Val, BaseTy: E->getType(), Elements&: SrcVals, Types&: SrcTypes, Size: NEls))
5112 return false;
5113
5114 return true;
5115}
5116
5117/// Perform an assignment of Val to LVal. Takes ownership of Val.
5118static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal,
5119 QualType LValType, APValue &Val) {
5120 if (LVal.Designator.Invalid)
5121 return false;
5122
5123 if (!Info.getLangOpts().CPlusPlus14) {
5124 Info.FFDiag(E);
5125 return false;
5126 }
5127
5128 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5129 return Obj && modifySubobject(Info, E, Obj, Sub: LVal.Designator, NewVal&: Val);
5130}
5131
5132namespace {
5133struct CompoundAssignSubobjectHandler {
5134 EvalInfo &Info;
5135 const CompoundAssignOperator *E;
5136 QualType PromotedLHSType;
5137 BinaryOperatorKind Opcode;
5138 const APValue &RHS;
5139
5140 static const AccessKinds AccessKind = AK_Assign;
5141
5142 typedef bool result_type;
5143
5144 bool checkConst(QualType QT) {
5145 // Assigning to a const object has undefined behavior.
5146 if (QT.isConstQualified()) {
5147 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5148 return false;
5149 }
5150 return true;
5151 }
5152
5153 bool failed() { return false; }
5154 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5155 switch (Subobj.getKind()) {
5156 case APValue::Int:
5157 return found(Value&: Subobj.getInt(), SubobjType);
5158 case APValue::Float:
5159 return found(Value&: Subobj.getFloat(), SubobjType);
5160 case APValue::ComplexInt:
5161 case APValue::ComplexFloat:
5162 // FIXME: Implement complex compound assignment.
5163 Info.FFDiag(E);
5164 return false;
5165 case APValue::LValue:
5166 return foundPointer(Subobj, SubobjType);
5167 case APValue::Vector:
5168 return foundVector(Value&: Subobj, SubobjType);
5169 case APValue::Indeterminate:
5170 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
5171 << /*read of=*/0 << /*uninitialized object=*/1
5172 << E->getLHS()->getSourceRange();
5173 NoteLValueLocation(Info, Base);
5174 return false;
5175 default:
5176 // FIXME: can this happen?
5177 Info.FFDiag(E);
5178 return false;
5179 }
5180 }
5181
5182 bool foundVector(APValue &Value, QualType SubobjType) {
5183 if (!checkConst(QT: SubobjType))
5184 return false;
5185
5186 if (!SubobjType->isVectorType()) {
5187 Info.FFDiag(E);
5188 return false;
5189 }
5190 return handleVectorVectorBinOp(Info, E, Opcode, LHSValue&: Value, RHSValue: RHS);
5191 }
5192
5193 bool found(APSInt &Value, QualType SubobjType) {
5194 if (!checkConst(QT: SubobjType))
5195 return false;
5196
5197 if (!SubobjType->isIntegerType()) {
5198 // We don't support compound assignment on integer-cast-to-pointer
5199 // values.
5200 Info.FFDiag(E);
5201 return false;
5202 }
5203
5204 if (RHS.isInt()) {
5205 APSInt LHS =
5206 HandleIntToIntCast(Info, E, DestType: PromotedLHSType, SrcType: SubobjType, Value);
5207 if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS: RHS.getInt(), Result&: LHS))
5208 return false;
5209 Value = HandleIntToIntCast(Info, E, DestType: SubobjType, SrcType: PromotedLHSType, Value: LHS);
5210 return true;
5211 } else if (RHS.isFloat()) {
5212 const FPOptions FPO = E->getFPFeaturesInEffect(
5213 LO: Info.Ctx.getLangOpts());
5214 APFloat FValue(0.0);
5215 return HandleIntToFloatCast(Info, E, FPO, SrcType: SubobjType, Value,
5216 DestType: PromotedLHSType, Result&: FValue) &&
5217 handleFloatFloatBinOp(Info, E, LHS&: FValue, Opcode, RHS: RHS.getFloat()) &&
5218 HandleFloatToIntCast(Info, E, SrcType: PromotedLHSType, Value: FValue, DestType: SubobjType,
5219 Result&: Value);
5220 }
5221
5222 Info.FFDiag(E);
5223 return false;
5224 }
5225 bool found(APFloat &Value, QualType SubobjType) {
5226 return checkConst(QT: SubobjType) &&
5227 HandleFloatToFloatCast(Info, E, SrcType: SubobjType, DestType: PromotedLHSType,
5228 Result&: Value) &&
5229 handleFloatFloatBinOp(Info, E, LHS&: Value, Opcode, RHS: RHS.getFloat()) &&
5230 HandleFloatToFloatCast(Info, E, SrcType: PromotedLHSType, DestType: SubobjType, Result&: Value);
5231 }
5232 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5233 if (!checkConst(QT: SubobjType))
5234 return false;
5235
5236 QualType PointeeType;
5237 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5238 PointeeType = PT->getPointeeType();
5239
5240 if (PointeeType.isNull() || !RHS.isInt() ||
5241 (Opcode != BO_Add && Opcode != BO_Sub)) {
5242 Info.FFDiag(E);
5243 return false;
5244 }
5245
5246 APSInt Offset = RHS.getInt();
5247 if (Opcode == BO_Sub)
5248 negateAsSigned(Int&: Offset);
5249
5250 LValue LVal;
5251 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5252 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType, Adjustment: Offset))
5253 return false;
5254 LVal.moveInto(V&: Subobj);
5255 return true;
5256 }
5257};
5258} // end anonymous namespace
5259
5260const AccessKinds CompoundAssignSubobjectHandler::AccessKind;
5261
5262/// Perform a compound assignment of LVal <op>= RVal.
5263static bool handleCompoundAssignment(EvalInfo &Info,
5264 const CompoundAssignOperator *E,
5265 const LValue &LVal, QualType LValType,
5266 QualType PromotedLValType,
5267 BinaryOperatorKind Opcode,
5268 const APValue &RVal) {
5269 if (LVal.Designator.Invalid)
5270 return false;
5271
5272 if (!Info.getLangOpts().CPlusPlus14) {
5273 Info.FFDiag(E);
5274 return false;
5275 }
5276
5277 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5278 CompoundAssignSubobjectHandler Handler = { .Info: Info, .E: E, .PromotedLHSType: PromotedLValType, .Opcode: Opcode,
5279 .RHS: RVal };
5280 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5281}
5282
5283namespace {
5284struct IncDecSubobjectHandler {
5285 EvalInfo &Info;
5286 const UnaryOperator *E;
5287 AccessKinds AccessKind;
5288 APValue *Old;
5289
5290 typedef bool result_type;
5291
5292 bool checkConst(QualType QT) {
5293 // Assigning to a const object has undefined behavior.
5294 if (QT.isConstQualified()) {
5295 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5296 return false;
5297 }
5298 return true;
5299 }
5300
5301 bool failed() { return false; }
5302 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5303 // Stash the old value. Also clear Old, so we don't clobber it later
5304 // if we're post-incrementing a complex.
5305 if (Old) {
5306 *Old = Subobj;
5307 Old = nullptr;
5308 }
5309
5310 switch (Subobj.getKind()) {
5311 case APValue::Int:
5312 return found(Value&: Subobj.getInt(), SubobjType);
5313 case APValue::Float:
5314 return found(Value&: Subobj.getFloat(), SubobjType);
5315 case APValue::ComplexInt:
5316 return found(Value&: Subobj.getComplexIntReal(),
5317 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5318 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5319 case APValue::ComplexFloat:
5320 return found(Value&: Subobj.getComplexFloatReal(),
5321 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5322 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5323 case APValue::LValue:
5324 return foundPointer(Subobj, SubobjType);
5325 default:
5326 // FIXME: can this happen?
5327 Info.FFDiag(E);
5328 return false;
5329 }
5330 }
5331 bool found(APSInt &Value, QualType SubobjType) {
5332 if (!checkConst(QT: SubobjType))
5333 return false;
5334
5335 if (!SubobjType->isIntegerType()) {
5336 // We don't support increment / decrement on integer-cast-to-pointer
5337 // values.
5338 Info.FFDiag(E);
5339 return false;
5340 }
5341
5342 if (Old) *Old = APValue(Value);
5343
5344 // bool arithmetic promotes to int, and the conversion back to bool
5345 // doesn't reduce mod 2^n, so special-case it.
5346 if (SubobjType->isBooleanType()) {
5347 if (AccessKind == AK_Increment)
5348 Value = 1;
5349 else
5350 Value = !Value;
5351 return true;
5352 }
5353
5354 bool WasNegative = Value.isNegative();
5355 if (AccessKind == AK_Increment) {
5356 ++Value;
5357
5358 if (!WasNegative && Value.isNegative() && E->canOverflow() &&
5359 !SubobjType.isWrapType()) {
5360 APSInt ActualValue(Value, /*IsUnsigned*/true);
5361 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5362 }
5363 } else {
5364 --Value;
5365
5366 if (WasNegative && !Value.isNegative() && E->canOverflow() &&
5367 !SubobjType.isWrapType()) {
5368 unsigned BitWidth = Value.getBitWidth();
5369 APSInt ActualValue(Value.sext(width: BitWidth + 1), /*IsUnsigned*/false);
5370 ActualValue.setBit(BitWidth);
5371 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5372 }
5373 }
5374 return true;
5375 }
5376 bool found(APFloat &Value, QualType SubobjType) {
5377 if (!checkConst(QT: SubobjType))
5378 return false;
5379
5380 if (Old) *Old = APValue(Value);
5381
5382 APFloat One(Value.getSemantics(), 1);
5383 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
5384 APFloat::opStatus St;
5385 if (AccessKind == AK_Increment)
5386 St = Value.add(RHS: One, RM);
5387 else
5388 St = Value.subtract(RHS: One, RM);
5389 return checkFloatingPointResultForConstantFolding(Info, E, St);
5390 }
5391 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5392 if (!checkConst(QT: SubobjType))
5393 return false;
5394
5395 QualType PointeeType;
5396 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5397 PointeeType = PT->getPointeeType();
5398 else {
5399 Info.FFDiag(E);
5400 return false;
5401 }
5402
5403 LValue LVal;
5404 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5405 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType,
5406 Adjustment: AccessKind == AK_Increment ? 1 : -1))
5407 return false;
5408 LVal.moveInto(V&: Subobj);
5409 return true;
5410 }
5411};
5412} // end anonymous namespace
5413
5414/// Perform an increment or decrement on LVal.
5415static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal,
5416 QualType LValType, bool IsIncrement, APValue *Old) {
5417 if (LVal.Designator.Invalid)
5418 return false;
5419
5420 if (!Info.getLangOpts().CPlusPlus14) {
5421 Info.FFDiag(E);
5422 return false;
5423 }
5424
5425 AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement;
5426 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType);
5427 IncDecSubobjectHandler Handler = {.Info: Info, .E: cast<UnaryOperator>(Val: E), .AccessKind: AK, .Old: Old};
5428 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5429}
5430
5431/// Build an lvalue for the object argument of a member function call.
5432static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,
5433 LValue &This) {
5434 if (Object->getType()->isPointerType() && Object->isPRValue())
5435 return EvaluatePointer(E: Object, Result&: This, Info);
5436
5437 if (Object->isGLValue())
5438 return EvaluateLValue(E: Object, Result&: This, Info);
5439
5440 if (Object->getType()->isLiteralType(Ctx: Info.Ctx))
5441 return EvaluateTemporary(E: Object, Result&: This, Info);
5442
5443 if (Object->getType()->isRecordType() && Object->isPRValue())
5444 return EvaluateTemporary(E: Object, Result&: This, Info);
5445
5446 Info.FFDiag(E: Object, DiagId: diag::note_constexpr_nonliteral) << Object->getType();
5447 return false;
5448}
5449
5450/// HandleMemberPointerAccess - Evaluate a member access operation and build an
5451/// lvalue referring to the result.
5452///
5453/// \param Info - Information about the ongoing evaluation.
5454/// \param LV - An lvalue referring to the base of the member pointer.
5455/// \param RHS - The member pointer expression.
5456/// \param IncludeMember - Specifies whether the member itself is included in
5457/// the resulting LValue subobject designator. This is not possible when
5458/// creating a bound member function.
5459/// \return The field or method declaration to which the member pointer refers,
5460/// or 0 if evaluation fails.
5461static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5462 QualType LVType,
5463 LValue &LV,
5464 const Expr *RHS,
5465 bool IncludeMember = true) {
5466 MemberPtr MemPtr;
5467 if (!EvaluateMemberPointer(E: RHS, Result&: MemPtr, Info))
5468 return nullptr;
5469
5470 // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to
5471 // member value, the behavior is undefined.
5472 if (!MemPtr.getDecl()) {
5473 // FIXME: Specific diagnostic.
5474 Info.FFDiag(E: RHS);
5475 return nullptr;
5476 }
5477
5478 if (MemPtr.isDerivedMember()) {
5479 // This is a member of some derived class. Truncate LV appropriately.
5480 // The end of the derived-to-base path for the base object must match the
5481 // derived-to-base path for the member pointer.
5482 // C++23 [expr.mptr.oper]p4:
5483 // If the result of E1 is an object [...] whose most derived object does
5484 // not contain the member to which E2 refers, the behavior is undefined.
5485 if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >
5486 LV.Designator.Entries.size()) {
5487 Info.FFDiag(E: RHS);
5488 return nullptr;
5489 }
5490 unsigned PathLengthToMember =
5491 LV.Designator.Entries.size() - MemPtr.Path.size();
5492 for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {
5493 const CXXRecordDecl *LVDecl = getAsBaseClass(
5494 E: LV.Designator.Entries[PathLengthToMember + I]);
5495 const CXXRecordDecl *MPDecl = MemPtr.Path[I];
5496 if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) {
5497 Info.FFDiag(E: RHS);
5498 return nullptr;
5499 }
5500 }
5501 // MemPtr.Path only contains the base classes of the class directly
5502 // containing the member E2. It is still necessary to check that the class
5503 // directly containing the member E2 lies on the derived-to-base path of E1
5504 // to avoid incorrectly permitting member pointer access into a sibling
5505 // class of the class containing the member E2. If this class would
5506 // correspond to the most-derived class of E1, it either isn't contained in
5507 // LV.Designator.Entries or the corresponding entry refers to an array
5508 // element instead. Therefore get the most derived class directly in this
5509 // case. Otherwise the previous entry should correpond to this class.
5510 const CXXRecordDecl *LastLVDecl =
5511 (PathLengthToMember > LV.Designator.MostDerivedPathLength)
5512 ? getAsBaseClass(E: LV.Designator.Entries[PathLengthToMember - 1])
5513 : LV.Designator.MostDerivedType->getAsCXXRecordDecl();
5514 const CXXRecordDecl *LastMPDecl = MemPtr.getContainingRecord();
5515 if (LastLVDecl->getCanonicalDecl() != LastMPDecl->getCanonicalDecl()) {
5516 Info.FFDiag(E: RHS);
5517 return nullptr;
5518 }
5519
5520 // Truncate the lvalue to the appropriate derived class.
5521 if (!CastToDerivedClass(Info, E: RHS, Result&: LV, TruncatedType: MemPtr.getContainingRecord(),
5522 TruncatedElements: PathLengthToMember))
5523 return nullptr;
5524 } else if (!MemPtr.Path.empty()) {
5525 // Extend the LValue path with the member pointer's path.
5526 LV.Designator.Entries.reserve(N: LV.Designator.Entries.size() +
5527 MemPtr.Path.size() + IncludeMember);
5528
5529 // Walk down to the appropriate base class.
5530 if (const PointerType *PT = LVType->getAs<PointerType>())
5531 LVType = PT->getPointeeType();
5532 const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();
5533 assert(RD && "member pointer access on non-class-type expression");
5534 // The first class in the path is that of the lvalue.
5535 for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {
5536 const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];
5537 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, Base))
5538 return nullptr;
5539 RD = Base;
5540 }
5541 // Finally cast to the class containing the member.
5542 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD,
5543 Base: MemPtr.getContainingRecord()))
5544 return nullptr;
5545 }
5546
5547 // Add the member. Note that we cannot build bound member functions here.
5548 if (IncludeMember) {
5549 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: MemPtr.getDecl())) {
5550 if (!HandleLValueMember(Info, E: RHS, LVal&: LV, FD))
5551 return nullptr;
5552 } else if (const IndirectFieldDecl *IFD =
5553 dyn_cast<IndirectFieldDecl>(Val: MemPtr.getDecl())) {
5554 if (!HandleLValueIndirectMember(Info, E: RHS, LVal&: LV, IFD))
5555 return nullptr;
5556 } else {
5557 llvm_unreachable("can't construct reference to bound member function");
5558 }
5559 }
5560
5561 return MemPtr.getDecl();
5562}
5563
5564static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5565 const BinaryOperator *BO,
5566 LValue &LV,
5567 bool IncludeMember = true) {
5568 assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);
5569
5570 if (!EvaluateObjectArgument(Info, Object: BO->getLHS(), This&: LV)) {
5571 if (Info.noteFailure()) {
5572 MemberPtr MemPtr;
5573 EvaluateMemberPointer(E: BO->getRHS(), Result&: MemPtr, Info);
5574 }
5575 return nullptr;
5576 }
5577
5578 return HandleMemberPointerAccess(Info, LVType: BO->getLHS()->getType(), LV,
5579 RHS: BO->getRHS(), IncludeMember);
5580}
5581
5582/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
5583/// the provided lvalue, which currently refers to the base object.
5584static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
5585 LValue &Result) {
5586 SubobjectDesignator &D = Result.Designator;
5587 if (D.Invalid || !Result.checkNullPointer(Info, E, CSK: CSK_Derived))
5588 return false;
5589
5590 QualType TargetQT = E->getType();
5591 if (const PointerType *PT = TargetQT->getAs<PointerType>())
5592 TargetQT = PT->getPointeeType();
5593
5594 auto InvalidCast = [&]() {
5595 if (!Info.checkingPotentialConstantExpression() ||
5596 !Result.AllowConstexprUnknown) {
5597 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_downcast)
5598 << D.MostDerivedType << TargetQT;
5599 }
5600 return false;
5601 };
5602
5603 // Check this cast lands within the final derived-to-base subobject path.
5604 if (D.MostDerivedPathLength + E->path_size() > D.Entries.size())
5605 return InvalidCast();
5606
5607 // Check the type of the final cast. We don't need to check the path,
5608 // since a cast can only be formed if the path is unique.
5609 unsigned NewEntriesSize = D.Entries.size() - E->path_size();
5610 const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
5611 const CXXRecordDecl *FinalType;
5612 if (NewEntriesSize == D.MostDerivedPathLength)
5613 FinalType = D.MostDerivedType->getAsCXXRecordDecl();
5614 else
5615 FinalType = getAsBaseClass(E: D.Entries[NewEntriesSize - 1]);
5616 if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl())
5617 return InvalidCast();
5618
5619 // Truncate the lvalue to the appropriate derived class.
5620 return CastToDerivedClass(Info, E, Result, TruncatedType: TargetType, TruncatedElements: NewEntriesSize);
5621}
5622
5623/// Get the value to use for a default-initialized object of type T.
5624/// Return false if it encounters something invalid.
5625static bool handleDefaultInitValue(QualType T, APValue &Result,
5626 bool IsCompleteClass = true) {
5627 bool Success = true;
5628
5629 // If there is already a value present don't overwrite it.
5630 if (!Result.isAbsent())
5631 return true;
5632
5633 if (auto *RD = T->getAsCXXRecordDecl()) {
5634 if (RD->isInvalidDecl()) {
5635 Result = APValue();
5636 return false;
5637 }
5638 if (RD->isUnion()) {
5639 Result = APValue((const FieldDecl *)nullptr);
5640 return true;
5641 }
5642
5643 // bases() includes directly specified virtual bases as well.
5644 unsigned NonVirtualBases = countNonVirtualBases(RD);
5645 Result =
5646 APValue(APValue::UninitStruct(), NonVirtualBases, RD->getNumFields(),
5647 IsCompleteClass ? RD->getNumVBases() : 0);
5648
5649 unsigned Index = 0;
5650 for (const CXXBaseSpecifier &B : RD->bases()) {
5651 if (B.isVirtual())
5652 continue;
5653 Success &= handleDefaultInitValue(
5654 T: B.getType(), Result&: Result.getStructBase(i: Index), /*IsCompleteClass=*/false);
5655 ++Index;
5656 }
5657
5658 for (const auto *I : RD->fields()) {
5659 if (I->isUnnamedBitField())
5660 continue;
5661 Success &= handleDefaultInitValue(
5662 T: I->getType(), Result&: Result.getStructField(i: I->getFieldIndex()));
5663 }
5664
5665 if (IsCompleteClass) {
5666 Index = 0;
5667
5668 for (const auto &B : RD->vbases()) {
5669 Success &= handleDefaultInitValue(T: B.getType(),
5670 Result&: Result.getStructVirtualBase(i: Index),
5671 /*IsCompleteClass=*/false);
5672 ++Index;
5673 }
5674 } else {
5675 // Virtual bases should only exist at the top level of an APValue.
5676 assert(Result.getStructNumVirtualBases() == 0);
5677 }
5678
5679 return Success;
5680 }
5681
5682 if (auto *AT =
5683 dyn_cast_or_null<ConstantArrayType>(Val: T->getAsArrayTypeUnsafe())) {
5684 Result = APValue(APValue::UninitArray(), 0, AT->getZExtSize());
5685 if (Result.hasArrayFiller())
5686 Success &=
5687 handleDefaultInitValue(T: AT->getElementType(), Result&: Result.getArrayFiller());
5688 return Success;
5689 }
5690
5691 Result = APValue::IndeterminateValue();
5692 return true;
5693}
5694
5695namespace {
5696enum EvalStmtResult {
5697 /// Evaluation failed.
5698 ESR_Failed,
5699 /// Hit a 'return' statement.
5700 ESR_Returned,
5701 /// Evaluation succeeded.
5702 ESR_Succeeded,
5703 /// Hit a 'continue' statement.
5704 ESR_Continue,
5705 /// Hit a 'break' statement.
5706 ESR_Break,
5707 /// Still scanning for 'case' or 'default' statement.
5708 ESR_CaseNotFound
5709};
5710}
5711/// Evaluates the initializer of a reference.
5712static bool EvaluateInitForDeclOfReferenceType(EvalInfo &Info,
5713 const ValueDecl *D,
5714 const Expr *Init, LValue &Result,
5715 APValue &Val) {
5716 assert(Init->isGLValue() && D->getType()->isReferenceType());
5717 // A reference is an lvalue.
5718 if (!EvaluateLValue(E: Init, Result, Info))
5719 return false;
5720 // [C++26][decl.ref]
5721 // The object designated by such a glvalue can be outside its lifetime
5722 // Because a null pointer value or a pointer past the end of an object
5723 // does not point to an object, a reference in a well-defined program cannot
5724 // refer to such things;
5725 if (!Result.Designator.Invalid && Result.Designator.isOnePastTheEnd()) {
5726 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_access_past_end) << AK_Dereference;
5727 return false;
5728 }
5729
5730 // Save the result.
5731 Result.moveInto(V&: Val);
5732 return true;
5733}
5734
5735static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) {
5736 if (VD->isInvalidDecl())
5737 return false;
5738 // We don't need to evaluate the initializer for a static local.
5739 if (!VD->hasLocalStorage())
5740 return true;
5741
5742 LValue Result;
5743 APValue &Val = Info.CurrentCall->createTemporary(Key: VD, T: VD->getType(),
5744 Scope: ScopeKind::Block, LV&: Result);
5745
5746 const Expr *InitE = VD->getInit();
5747 if (!InitE) {
5748 if (VD->getType()->isDependentType())
5749 return Info.noteSideEffect();
5750 return handleDefaultInitValue(T: VD->getType(), Result&: Val);
5751 }
5752 if (InitE->isValueDependent())
5753 return false;
5754
5755 // For references to objects, check they do not designate a one-past-the-end
5756 // object.
5757 if (VD->getType()->isReferenceType()) {
5758 return EvaluateInitForDeclOfReferenceType(Info, D: VD, Init: InitE, Result, Val);
5759 } else if (!EvaluateInPlace(Result&: Val, Info, This: Result, E: InitE)) {
5760 // Wipe out any partially-computed value, to allow tracking that this
5761 // evaluation failed.
5762 Val = APValue();
5763 return false;
5764 }
5765
5766 return true;
5767}
5768
5769static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5770 const DecompositionDecl *DD);
5771
5772static bool EvaluateDecl(EvalInfo &Info, const Decl *D,
5773 bool EvaluateConditionDecl = false) {
5774 bool OK = true;
5775 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
5776 OK &= EvaluateVarDecl(Info, VD);
5777
5778 if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(Val: D);
5779 EvaluateConditionDecl && DD)
5780 OK &= EvaluateDecompositionDeclInit(Info, DD);
5781
5782 return OK;
5783}
5784
5785static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5786 const DecompositionDecl *DD) {
5787 bool OK = true;
5788 for (auto *BD : DD->flat_bindings())
5789 if (auto *VD = BD->getHoldingVar())
5790 OK &= EvaluateDecl(Info, D: VD, /*EvaluateConditionDecl=*/true);
5791
5792 return OK;
5793}
5794
5795static bool MaybeEvaluateDeferredVarDeclInit(EvalInfo &Info,
5796 const VarDecl *VD) {
5797 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(Val: VD)) {
5798 if (!EvaluateDecompositionDeclInit(Info, DD))
5799 return false;
5800 }
5801 return true;
5802}
5803
5804static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) {
5805 assert(E->isValueDependent());
5806 if (Info.noteSideEffect())
5807 return true;
5808 assert(E->containsErrors() && "valid value-dependent expression should never "
5809 "reach invalid code path.");
5810 return false;
5811}
5812
5813/// Evaluate a condition (either a variable declaration or an expression).
5814static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl,
5815 const Expr *Cond, bool &Result) {
5816 if (Cond->isValueDependent())
5817 return false;
5818 FullExpressionRAII Scope(Info);
5819 if (CondDecl && !EvaluateDecl(Info, D: CondDecl))
5820 return false;
5821 if (!EvaluateAsBooleanCondition(E: Cond, Result, Info))
5822 return false;
5823 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: CondDecl))
5824 return false;
5825 return Scope.destroy();
5826}
5827
5828namespace {
5829/// A location where the result (returned value) of evaluating a
5830/// statement should be stored.
5831struct StmtResult {
5832 /// The APValue that should be filled in with the returned value.
5833 APValue &Value;
5834 /// The location containing the result, if any (used to support RVO).
5835 const LValue *Slot;
5836};
5837
5838struct TempVersionRAII {
5839 CallStackFrame &Frame;
5840
5841 TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) {
5842 Frame.pushTempVersion();
5843 }
5844
5845 ~TempVersionRAII() {
5846 Frame.popTempVersion();
5847 }
5848};
5849
5850}
5851
5852static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
5853 const Stmt *S,
5854 const SwitchCase *SC = nullptr);
5855
5856/// Helper to implement named break/continue. Returns 'true' if the evaluation
5857/// result should be propagated up. Otherwise, it sets the evaluation result
5858/// to either Continue to continue the current loop, or Succeeded to break it.
5859static bool ShouldPropagateBreakContinue(EvalInfo &Info,
5860 const Stmt *LoopOrSwitch,
5861 ArrayRef<BlockScopeRAII *> Scopes,
5862 EvalStmtResult &ESR) {
5863 bool IsSwitch = isa<SwitchStmt>(Val: LoopOrSwitch);
5864
5865 // For loops, map Succeeded to Continue so we don't have to check for both.
5866 if (!IsSwitch && ESR == ESR_Succeeded) {
5867 ESR = ESR_Continue;
5868 return false;
5869 }
5870
5871 if (ESR != ESR_Break && ESR != ESR_Continue)
5872 return false;
5873
5874 // Are we breaking out of or continuing this statement?
5875 bool CanBreakOrContinue = !IsSwitch || ESR == ESR_Break;
5876 const Stmt *StackTop = Info.BreakContinueStack.back();
5877 if (CanBreakOrContinue && (StackTop == nullptr || StackTop == LoopOrSwitch)) {
5878 Info.BreakContinueStack.pop_back();
5879 if (ESR == ESR_Break)
5880 ESR = ESR_Succeeded;
5881 return false;
5882 }
5883
5884 // We're not. Propagate the result up.
5885 for (BlockScopeRAII *S : Scopes) {
5886 if (!S->destroy()) {
5887 ESR = ESR_Failed;
5888 break;
5889 }
5890 }
5891 return true;
5892}
5893
5894/// Evaluate the body of a loop, and translate the result as appropriate.
5895static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info,
5896 const Stmt *Body,
5897 const SwitchCase *Case = nullptr) {
5898 BlockScopeRAII Scope(Info);
5899
5900 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Body, SC: Case);
5901 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5902 ESR = ESR_Failed;
5903
5904 return ESR;
5905}
5906
5907/// Evaluate a switch statement.
5908static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
5909 const SwitchStmt *SS) {
5910 BlockScopeRAII Scope(Info);
5911
5912 // Evaluate the switch condition.
5913 APSInt Value;
5914 {
5915 if (const Stmt *Init = SS->getInit()) {
5916 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
5917 if (ESR != ESR_Succeeded) {
5918 if (ESR != ESR_Failed && !Scope.destroy())
5919 ESR = ESR_Failed;
5920 return ESR;
5921 }
5922 }
5923
5924 FullExpressionRAII CondScope(Info);
5925 if (SS->getConditionVariable() &&
5926 !EvaluateDecl(Info, D: SS->getConditionVariable()))
5927 return ESR_Failed;
5928 if (SS->getCond()->isValueDependent()) {
5929 // We don't know what the value is, and which branch should jump to.
5930 EvaluateDependentExpr(E: SS->getCond(), Info);
5931 return ESR_Failed;
5932 }
5933 if (!EvaluateInteger(E: SS->getCond(), Result&: Value, Info))
5934 return ESR_Failed;
5935
5936 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: SS->getConditionVariable()))
5937 return ESR_Failed;
5938
5939 if (!CondScope.destroy())
5940 return ESR_Failed;
5941 }
5942
5943 // Find the switch case corresponding to the value of the condition.
5944 // FIXME: Cache this lookup.
5945 const SwitchCase *Found = nullptr;
5946 for (const SwitchCase *SC = SS->getSwitchCaseList(); SC;
5947 SC = SC->getNextSwitchCase()) {
5948 if (isa<DefaultStmt>(Val: SC)) {
5949 Found = SC;
5950 continue;
5951 }
5952
5953 const CaseStmt *CS = cast<CaseStmt>(Val: SC);
5954 const Expr *LHS = CS->getLHS();
5955 const Expr *RHS = CS->getRHS();
5956 if (LHS->isValueDependent() || (RHS && RHS->isValueDependent()))
5957 return ESR_Failed;
5958 APSInt LHSValue = LHS->EvaluateKnownConstInt(Ctx: Info.Ctx);
5959 APSInt RHSValue = RHS ? RHS->EvaluateKnownConstInt(Ctx: Info.Ctx) : LHSValue;
5960 if (LHSValue <= Value && Value <= RHSValue) {
5961 Found = SC;
5962 break;
5963 }
5964 }
5965
5966 if (!Found)
5967 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
5968
5969 // Search the switch body for the switch case and evaluate it from there.
5970 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SS->getBody(), SC: Found);
5971 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5972 return ESR_Failed;
5973 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: SS, /*Scopes=*/{}, ESR))
5974 return ESR;
5975
5976 switch (ESR) {
5977 case ESR_Break:
5978 llvm_unreachable("Should have been converted to Succeeded");
5979 case ESR_Succeeded:
5980 case ESR_Continue:
5981 case ESR_Failed:
5982 case ESR_Returned:
5983 return ESR;
5984 case ESR_CaseNotFound:
5985 // This can only happen if the switch case is nested within a statement
5986 // expression. We have no intention of supporting that.
5987 Info.FFDiag(Loc: Found->getBeginLoc(),
5988 DiagId: diag::note_constexpr_stmt_expr_unsupported);
5989 return ESR_Failed;
5990 }
5991 llvm_unreachable("Invalid EvalStmtResult!");
5992}
5993
5994static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) {
5995 // An expression E is a core constant expression unless the evaluation of E
5996 // would evaluate one of the following: [C++23] - a control flow that passes
5997 // through a declaration of a variable with static or thread storage duration
5998 // unless that variable is usable in constant expressions.
5999 if (VD->isLocalVarDecl() && VD->isStaticLocal() &&
6000 !VD->isUsableInConstantExpressions(C: Info.Ctx)) {
6001 Info.CCEDiag(Loc: VD->getLocation(), DiagId: diag::note_constexpr_static_local)
6002 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
6003 return false;
6004 }
6005 return true;
6006}
6007
6008// Evaluate a statement.
6009static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
6010 const Stmt *S, const SwitchCase *Case) {
6011 if (!Info.nextStep(S))
6012 return ESR_Failed;
6013
6014 // If we're hunting down a 'case' or 'default' label, recurse through
6015 // substatements until we hit the label.
6016 if (Case) {
6017 switch (S->getStmtClass()) {
6018 case Stmt::CompoundStmtClass:
6019 // FIXME: Precompute which substatement of a compound statement we
6020 // would jump to, and go straight there rather than performing a
6021 // linear scan each time.
6022 case Stmt::LabelStmtClass:
6023 case Stmt::AttributedStmtClass:
6024 case Stmt::DoStmtClass:
6025 break;
6026
6027 case Stmt::CaseStmtClass:
6028 case Stmt::DefaultStmtClass:
6029 if (Case == S)
6030 Case = nullptr;
6031 break;
6032
6033 case Stmt::IfStmtClass: {
6034 // FIXME: Precompute which side of an 'if' we would jump to, and go
6035 // straight there rather than scanning both sides.
6036 const IfStmt *IS = cast<IfStmt>(Val: S);
6037
6038 // Wrap the evaluation in a block scope, in case it's a DeclStmt
6039 // preceded by our switch label.
6040 BlockScopeRAII Scope(Info);
6041
6042 // Step into the init statement in case it brings an (uninitialized)
6043 // variable into scope.
6044 if (const Stmt *Init = IS->getInit()) {
6045 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
6046 if (ESR != ESR_CaseNotFound) {
6047 assert(ESR != ESR_Succeeded);
6048 return ESR;
6049 }
6050 }
6051
6052 // Condition variable must be initialized if it exists.
6053 // FIXME: We can skip evaluating the body if there's a condition
6054 // variable, as there can't be any case labels within it.
6055 // (The same is true for 'for' statements.)
6056
6057 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: IS->getThen(), Case);
6058 if (ESR == ESR_Failed)
6059 return ESR;
6060 if (ESR != ESR_CaseNotFound)
6061 return Scope.destroy() ? ESR : ESR_Failed;
6062 if (!IS->getElse())
6063 return ESR_CaseNotFound;
6064
6065 ESR = EvaluateStmt(Result, Info, S: IS->getElse(), Case);
6066 if (ESR == ESR_Failed)
6067 return ESR;
6068 if (ESR != ESR_CaseNotFound)
6069 return Scope.destroy() ? ESR : ESR_Failed;
6070 return ESR_CaseNotFound;
6071 }
6072
6073 case Stmt::WhileStmtClass: {
6074 EvalStmtResult ESR =
6075 EvaluateLoopBody(Result, Info, Body: cast<WhileStmt>(Val: S)->getBody(), Case);
6076 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: S, /*Scopes=*/{}, ESR))
6077 return ESR;
6078 if (ESR != ESR_Continue)
6079 return ESR;
6080 break;
6081 }
6082
6083 case Stmt::ForStmtClass: {
6084 const ForStmt *FS = cast<ForStmt>(Val: S);
6085 BlockScopeRAII Scope(Info);
6086
6087 // Step into the init statement in case it brings an (uninitialized)
6088 // variable into scope.
6089 if (const Stmt *Init = FS->getInit()) {
6090 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
6091 if (ESR != ESR_CaseNotFound) {
6092 assert(ESR != ESR_Succeeded);
6093 return ESR;
6094 }
6095 }
6096
6097 EvalStmtResult ESR =
6098 EvaluateLoopBody(Result, Info, Body: FS->getBody(), Case);
6099 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, /*Scopes=*/{}, ESR))
6100 return ESR;
6101 if (ESR != ESR_Continue)
6102 return ESR;
6103 if (const auto *Inc = FS->getInc()) {
6104 if (Inc->isValueDependent()) {
6105 if (!EvaluateDependentExpr(E: Inc, Info))
6106 return ESR_Failed;
6107 } else {
6108 FullExpressionRAII IncScope(Info);
6109 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6110 return ESR_Failed;
6111 }
6112 }
6113 break;
6114 }
6115
6116 case Stmt::DeclStmtClass: {
6117 // Start the lifetime of any uninitialized variables we encounter. They
6118 // might be used by the selected branch of the switch.
6119 const DeclStmt *DS = cast<DeclStmt>(Val: S);
6120 for (const auto *D : DS->decls()) {
6121 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
6122 if (!CheckLocalVariableDeclaration(Info, VD))
6123 return ESR_Failed;
6124 if (VD->hasLocalStorage() && !VD->getInit())
6125 if (!EvaluateVarDecl(Info, VD))
6126 return ESR_Failed;
6127 // FIXME: If the variable has initialization that can't be jumped
6128 // over, bail out of any immediately-surrounding compound-statement
6129 // too. There can't be any case labels here.
6130 }
6131 }
6132 return ESR_CaseNotFound;
6133 }
6134
6135 default:
6136 return ESR_CaseNotFound;
6137 }
6138 }
6139
6140 switch (S->getStmtClass()) {
6141 default:
6142 if (const Expr *E = dyn_cast<Expr>(Val: S)) {
6143 if (E->isValueDependent()) {
6144 if (!EvaluateDependentExpr(E, Info))
6145 return ESR_Failed;
6146 } else {
6147 // Don't bother evaluating beyond an expression-statement which couldn't
6148 // be evaluated.
6149 // FIXME: Do we need the FullExpressionRAII object here?
6150 // VisitExprWithCleanups should create one when necessary.
6151 FullExpressionRAII Scope(Info);
6152 if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy())
6153 return ESR_Failed;
6154 }
6155 return ESR_Succeeded;
6156 }
6157
6158 Info.FFDiag(Loc: S->getBeginLoc()) << S->getSourceRange();
6159 return ESR_Failed;
6160
6161 case Stmt::NullStmtClass:
6162 return ESR_Succeeded;
6163
6164 case Stmt::DeclStmtClass: {
6165 const DeclStmt *DS = cast<DeclStmt>(Val: S);
6166 for (const auto *D : DS->decls()) {
6167 const VarDecl *VD = dyn_cast_or_null<VarDecl>(Val: D);
6168 if (VD && !CheckLocalVariableDeclaration(Info, VD))
6169 return ESR_Failed;
6170
6171 if (const auto *ESD = dyn_cast<CXXExpansionStmtDecl>(Val: D)) {
6172 assert(ESD->getInstantiations() && "not expanded?");
6173 return EvaluateStmt(Result, Info, S: ESD->getInstantiations(), Case);
6174 }
6175
6176 // Each declaration initialization is its own full-expression.
6177 FullExpressionRAII Scope(Info);
6178 if (!EvaluateDecl(Info, D, /*EvaluateConditionDecl=*/true) &&
6179 !Info.noteFailure())
6180 return ESR_Failed;
6181 if (!Scope.destroy())
6182 return ESR_Failed;
6183 }
6184 return ESR_Succeeded;
6185 }
6186
6187 case Stmt::ReturnStmtClass: {
6188 const Expr *RetExpr = cast<ReturnStmt>(Val: S)->getRetValue();
6189 FullExpressionRAII Scope(Info);
6190 if (RetExpr && RetExpr->isValueDependent()) {
6191 EvaluateDependentExpr(E: RetExpr, Info);
6192 // We know we returned, but we don't know what the value is.
6193 return ESR_Failed;
6194 }
6195 if (RetExpr &&
6196 !(Result.Slot
6197 ? EvaluateInPlace(Result&: Result.Value, Info, This: *Result.Slot, E: RetExpr)
6198 : Evaluate(Result&: Result.Value, Info, E: RetExpr)))
6199 return ESR_Failed;
6200 return Scope.destroy() ? ESR_Returned : ESR_Failed;
6201 }
6202
6203 case Stmt::CompoundStmtClass: {
6204 BlockScopeRAII Scope(Info);
6205
6206 const CompoundStmt *CS = cast<CompoundStmt>(Val: S);
6207 for (const auto *BI : CS->body()) {
6208 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: BI, Case);
6209 if (ESR == ESR_Succeeded)
6210 Case = nullptr;
6211 else if (ESR != ESR_CaseNotFound) {
6212 if (ESR != ESR_Failed && !Scope.destroy())
6213 return ESR_Failed;
6214 return ESR;
6215 }
6216 }
6217 if (Case)
6218 return ESR_CaseNotFound;
6219 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6220 }
6221
6222 case Stmt::IfStmtClass: {
6223 const IfStmt *IS = cast<IfStmt>(Val: S);
6224
6225 // Evaluate the condition, as either a var decl or as an expression.
6226 BlockScopeRAII Scope(Info);
6227 if (const Stmt *Init = IS->getInit()) {
6228 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
6229 if (ESR != ESR_Succeeded) {
6230 if (ESR != ESR_Failed && !Scope.destroy())
6231 return ESR_Failed;
6232 return ESR;
6233 }
6234 }
6235 bool Cond;
6236 if (IS->isConsteval()) {
6237 Cond = IS->isNonNegatedConsteval();
6238 // If we are not in a constant context, if consteval should not evaluate
6239 // to true.
6240 if (!Info.InConstantContext)
6241 Cond = !Cond;
6242 } else if (!EvaluateCond(Info, CondDecl: IS->getConditionVariable(), Cond: IS->getCond(),
6243 Result&: Cond))
6244 return ESR_Failed;
6245
6246 if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) {
6247 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SubStmt);
6248 if (ESR != ESR_Succeeded) {
6249 if (ESR != ESR_Failed && !Scope.destroy())
6250 return ESR_Failed;
6251 return ESR;
6252 }
6253 }
6254 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6255 }
6256
6257 case Stmt::WhileStmtClass: {
6258 const WhileStmt *WS = cast<WhileStmt>(Val: S);
6259 while (true) {
6260 BlockScopeRAII Scope(Info);
6261 bool Continue;
6262 if (!EvaluateCond(Info, CondDecl: WS->getConditionVariable(), Cond: WS->getCond(),
6263 Result&: Continue))
6264 return ESR_Failed;
6265 if (!Continue)
6266 break;
6267
6268 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: WS->getBody());
6269 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: WS, Scopes: &Scope, ESR))
6270 return ESR;
6271
6272 if (ESR != ESR_Continue) {
6273 if (ESR != ESR_Failed && !Scope.destroy())
6274 return ESR_Failed;
6275 return ESR;
6276 }
6277 if (!Scope.destroy())
6278 return ESR_Failed;
6279 }
6280 return ESR_Succeeded;
6281 }
6282
6283 case Stmt::DoStmtClass: {
6284 const DoStmt *DS = cast<DoStmt>(Val: S);
6285 bool Continue;
6286 do {
6287 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: DS->getBody(), Case);
6288 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: DS, /*Scopes=*/{}, ESR))
6289 return ESR;
6290 if (ESR != ESR_Continue)
6291 return ESR;
6292 Case = nullptr;
6293
6294 if (DS->getCond()->isValueDependent()) {
6295 EvaluateDependentExpr(E: DS->getCond(), Info);
6296 // Bailout as we don't know whether to keep going or terminate the loop.
6297 return ESR_Failed;
6298 }
6299 FullExpressionRAII CondScope(Info);
6300 if (!EvaluateAsBooleanCondition(E: DS->getCond(), Result&: Continue, Info) ||
6301 !CondScope.destroy())
6302 return ESR_Failed;
6303 } while (Continue);
6304 return ESR_Succeeded;
6305 }
6306
6307 case Stmt::ForStmtClass: {
6308 const ForStmt *FS = cast<ForStmt>(Val: S);
6309 BlockScopeRAII ForScope(Info);
6310 if (FS->getInit()) {
6311 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6312 if (ESR != ESR_Succeeded) {
6313 if (ESR != ESR_Failed && !ForScope.destroy())
6314 return ESR_Failed;
6315 return ESR;
6316 }
6317 }
6318 while (true) {
6319 BlockScopeRAII IterScope(Info);
6320 bool Continue = true;
6321 if (FS->getCond() && !EvaluateCond(Info, CondDecl: FS->getConditionVariable(),
6322 Cond: FS->getCond(), Result&: Continue))
6323 return ESR_Failed;
6324
6325 if (!Continue) {
6326 if (!IterScope.destroy())
6327 return ESR_Failed;
6328 break;
6329 }
6330
6331 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6332 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&IterScope, &ForScope}, ESR))
6333 return ESR;
6334 if (ESR != ESR_Continue) {
6335 if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy()))
6336 return ESR_Failed;
6337 return ESR;
6338 }
6339
6340 if (const auto *Inc = FS->getInc()) {
6341 if (Inc->isValueDependent()) {
6342 if (!EvaluateDependentExpr(E: Inc, Info))
6343 return ESR_Failed;
6344 } else {
6345 FullExpressionRAII IncScope(Info);
6346 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6347 return ESR_Failed;
6348 }
6349 }
6350
6351 if (!IterScope.destroy())
6352 return ESR_Failed;
6353 }
6354 return ForScope.destroy() ? ESR_Succeeded : ESR_Failed;
6355 }
6356
6357 case Stmt::CXXForRangeStmtClass: {
6358 const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(Val: S);
6359 BlockScopeRAII Scope(Info);
6360
6361 // Evaluate the init-statement if present.
6362 if (FS->getInit()) {
6363 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6364 if (ESR != ESR_Succeeded) {
6365 if (ESR != ESR_Failed && !Scope.destroy())
6366 return ESR_Failed;
6367 return ESR;
6368 }
6369 }
6370
6371 // Initialize the __range variable.
6372 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getRangeStmt());
6373 if (ESR != ESR_Succeeded) {
6374 if (ESR != ESR_Failed && !Scope.destroy())
6375 return ESR_Failed;
6376 return ESR;
6377 }
6378
6379 // In error-recovery cases it's possible to get here even if we failed to
6380 // synthesize the __begin and __end variables.
6381 if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond())
6382 return ESR_Failed;
6383
6384 // Create the __begin and __end iterators.
6385 ESR = EvaluateStmt(Result, Info, S: FS->getBeginStmt());
6386 if (ESR != ESR_Succeeded) {
6387 if (ESR != ESR_Failed && !Scope.destroy())
6388 return ESR_Failed;
6389 return ESR;
6390 }
6391 ESR = EvaluateStmt(Result, Info, S: FS->getEndStmt());
6392 if (ESR != ESR_Succeeded) {
6393 if (ESR != ESR_Failed && !Scope.destroy())
6394 return ESR_Failed;
6395 return ESR;
6396 }
6397
6398 while (true) {
6399 // Condition: __begin != __end.
6400 {
6401 if (FS->getCond()->isValueDependent()) {
6402 EvaluateDependentExpr(E: FS->getCond(), Info);
6403 // We don't know whether to keep going or terminate the loop.
6404 return ESR_Failed;
6405 }
6406 bool Continue = true;
6407 FullExpressionRAII CondExpr(Info);
6408 if (!EvaluateAsBooleanCondition(E: FS->getCond(), Result&: Continue, Info))
6409 return ESR_Failed;
6410 if (!Continue)
6411 break;
6412 }
6413
6414 // User's variable declaration, initialized by *__begin.
6415 BlockScopeRAII InnerScope(Info);
6416 ESR = EvaluateStmt(Result, Info, S: FS->getLoopVarStmt());
6417 if (ESR != ESR_Succeeded) {
6418 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6419 return ESR_Failed;
6420 return ESR;
6421 }
6422
6423 // Loop body.
6424 ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6425 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&InnerScope, &Scope}, ESR))
6426 return ESR;
6427 if (ESR != ESR_Continue) {
6428 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6429 return ESR_Failed;
6430 return ESR;
6431 }
6432 if (FS->getInc()->isValueDependent()) {
6433 if (!EvaluateDependentExpr(E: FS->getInc(), Info))
6434 return ESR_Failed;
6435 } else {
6436 // Increment: ++__begin
6437 if (!EvaluateIgnoredValue(Info, E: FS->getInc()))
6438 return ESR_Failed;
6439 }
6440
6441 if (!InnerScope.destroy())
6442 return ESR_Failed;
6443 }
6444
6445 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6446 }
6447
6448 case Stmt::CXXExpansionStmtInstantiationClass: {
6449 BlockScopeRAII Scope(Info);
6450 const auto *Expansion = cast<CXXExpansionStmtInstantiation>(Val: S);
6451 for (const Stmt *PreambleStmt : Expansion->getPreambleStmts()) {
6452 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: PreambleStmt);
6453 if (ESR != ESR_Succeeded) {
6454 if (ESR != ESR_Failed && !Scope.destroy())
6455 return ESR_Failed;
6456 return ESR;
6457 }
6458 }
6459
6460 // No need to push an extra scope for these since they're already
6461 // CompoundStmts.
6462 EvalStmtResult ESR = ESR_Succeeded;
6463 for (const Stmt *Instantiation : Expansion->getInstantiations()) {
6464 ESR = EvaluateStmt(Result, Info, S: Instantiation);
6465 if (ESR == ESR_Failed ||
6466 ShouldPropagateBreakContinue(Info, LoopOrSwitch: Expansion, Scopes: &Scope, ESR))
6467 return ESR;
6468 if (ESR != ESR_Continue) {
6469 // Succeeded here actually means we encountered a 'break'.
6470 assert(ESR == ESR_Succeeded || ESR == ESR_Returned);
6471 break;
6472 }
6473 }
6474
6475 // Map Continue back to Succeeded if we fell off the end of the loop.
6476 if (ESR == ESR_Continue)
6477 ESR = ESR_Succeeded;
6478
6479 return Scope.destroy() ? ESR : ESR_Failed;
6480 }
6481
6482 case Stmt::SwitchStmtClass:
6483 return EvaluateSwitch(Result, Info, SS: cast<SwitchStmt>(Val: S));
6484
6485 case Stmt::ContinueStmtClass:
6486 case Stmt::BreakStmtClass: {
6487 auto *B = cast<LoopControlStmt>(Val: S);
6488 const Stmt *LoopOrSwitch = nullptr;
6489 if (B->isNamed()) {
6490 LoopOrSwitch = B->getNamedLoopOrSwitch();
6491 // May be null if the named loop is outside of the
6492 // current evaluation context, i.e. not a valid target.
6493 if (!LoopOrSwitch) {
6494 Info.FFDiag(Loc: S->getBeginLoc(),
6495 DiagId: diag::note_break_continue_label_outside_context)
6496 << isa<ContinueStmt>(Val: B);
6497 return ESR_Failed;
6498 }
6499 }
6500
6501 Info.BreakContinueStack.push_back(Elt: LoopOrSwitch);
6502 return isa<ContinueStmt>(Val: S) ? ESR_Continue : ESR_Break;
6503 }
6504
6505 case Stmt::LabelStmtClass:
6506 return EvaluateStmt(Result, Info, S: cast<LabelStmt>(Val: S)->getSubStmt(), Case);
6507
6508 case Stmt::AttributedStmtClass: {
6509 const auto *AS = cast<AttributedStmt>(Val: S);
6510 const auto *SS = AS->getSubStmt();
6511 MSConstexprContextRAII ConstexprContext(
6512 *Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(container: AS->getAttrs()) &&
6513 isa<ReturnStmt>(Val: SS));
6514
6515 auto LO = Info.Ctx.getLangOpts();
6516 if (LO.CXXAssumptions && !LO.MSVCCompat) {
6517 for (auto *Attr : AS->getAttrs()) {
6518 auto *AA = dyn_cast<CXXAssumeAttr>(Val: Attr);
6519 if (!AA)
6520 continue;
6521
6522 auto *Assumption = AA->getAssumption();
6523 if (Assumption->isValueDependent())
6524 return ESR_Failed;
6525
6526 if (Assumption->HasSideEffects(Ctx: Info.Ctx))
6527 continue;
6528
6529 bool Value;
6530 if (!EvaluateAsBooleanCondition(E: Assumption, Result&: Value, Info))
6531 return ESR_Failed;
6532 if (!Value) {
6533 Info.CCEDiag(Loc: Assumption->getExprLoc(),
6534 DiagId: diag::note_constexpr_assumption_failed);
6535 return ESR_Failed;
6536 }
6537 }
6538 }
6539
6540 return EvaluateStmt(Result, Info, S: SS, Case);
6541 }
6542
6543 case Stmt::CaseStmtClass:
6544 case Stmt::DefaultStmtClass:
6545 return EvaluateStmt(Result, Info, S: cast<SwitchCase>(Val: S)->getSubStmt(), Case);
6546 case Stmt::CXXTryStmtClass:
6547 // Evaluate try blocks by evaluating all sub statements.
6548 return EvaluateStmt(Result, Info, S: cast<CXXTryStmt>(Val: S)->getTryBlock(), Case);
6549 }
6550}
6551
6552/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
6553/// default constructor. If so, we'll fold it whether or not it's marked as
6554/// constexpr. If it is marked as constexpr, we will never implicitly define it,
6555/// so we need special handling.
6556static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
6557 const CXXConstructorDecl *CD,
6558 bool IsValueInitialization) {
6559 if (!CD->isTrivial() || !CD->isDefaultConstructor())
6560 return false;
6561
6562 // Value-initialization does not call a trivial default constructor, so such a
6563 // call is a core constant expression whether or not the constructor is
6564 // constexpr.
6565 if (!CD->isConstexpr() && !IsValueInitialization) {
6566 if (Info.getLangOpts().CPlusPlus11) {
6567 // FIXME: If DiagDecl is an implicitly-declared special member function,
6568 // we should be much more explicit about why it's not constexpr.
6569 Info.CCEDiag(Loc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6570 << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
6571 Info.Note(Loc: CD->getLocation(), DiagId: diag::note_declared_at);
6572 } else {
6573 Info.CCEDiag(Loc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6574 }
6575 }
6576 return true;
6577}
6578
6579/// CheckConstexprFunction - Check that a function can be called in a constant
6580/// expression.
6581static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
6582 const FunctionDecl *Declaration,
6583 const FunctionDecl *Definition,
6584 const Stmt *Body) {
6585 // Potential constant expressions can contain calls to declared, but not yet
6586 // defined, constexpr functions.
6587 if (Info.checkingPotentialConstantExpression() && !Definition &&
6588 Declaration->isConstexpr())
6589 return false;
6590
6591 // Bail out if the function declaration itself is invalid. We will
6592 // have produced a relevant diagnostic while parsing it, so just
6593 // note the problematic sub-expression.
6594 if (Declaration->isInvalidDecl()) {
6595 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6596 return false;
6597 }
6598
6599 // DR1872: An instantiated virtual constexpr function can't be called in a
6600 // constant expression (prior to C++20). We can still constant-fold such a
6601 // call.
6602 if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Val: Declaration) &&
6603 cast<CXXMethodDecl>(Val: Declaration)->isVirtual())
6604 Info.CCEDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_call);
6605
6606 if (Definition && Definition->isInvalidDecl()) {
6607 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6608 return false;
6609 }
6610
6611 // Can we evaluate this function call?
6612 if (Definition && Body &&
6613 (Definition->isConstexpr() || (Info.CurrentCall->CanEvalMSConstexpr &&
6614 Definition->hasAttr<MSConstexprAttr>())))
6615 return true;
6616
6617 const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
6618 // Special note for the assert() macro, as the normal error message falsely
6619 // implies we cannot use an assertion during constant evaluation.
6620 if (CallLoc.isMacroID() && DiagDecl->getIdentifier()) {
6621 // FIXME: Instead of checking for an implementation-defined function,
6622 // check and evaluate the assert() macro.
6623 StringRef Name = DiagDecl->getName();
6624 bool AssertFailed =
6625 Name == "__assert_rtn" || Name == "__assert_fail" || Name == "_wassert";
6626 if (AssertFailed) {
6627 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_assert_failed);
6628 return false;
6629 }
6630 }
6631
6632 if (Info.getLangOpts().CPlusPlus11) {
6633 // If this function is not constexpr because it is an inherited
6634 // non-constexpr constructor, diagnose that directly.
6635 auto *CD = dyn_cast<CXXConstructorDecl>(Val: DiagDecl);
6636 if (CD && CD->isInheritingConstructor()) {
6637 auto *Inherited = CD->getInheritedConstructor().getConstructor();
6638 if (!Inherited->isConstexpr())
6639 DiagDecl = CD = Inherited;
6640 }
6641
6642 // FIXME: If DiagDecl is an implicitly-declared special member function
6643 // or an inheriting constructor, we should be much more explicit about why
6644 // it's not constexpr.
6645 if (CD && CD->isInheritingConstructor())
6646 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_inhctor, ExtraNotes: 1)
6647 << CD->getInheritedConstructor().getConstructor()->getParent();
6648 else
6649 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6650 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
6651 Info.Note(Loc: DiagDecl->getLocation(), DiagId: diag::note_declared_at);
6652 } else {
6653 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6654 }
6655 return false;
6656}
6657
6658namespace {
6659struct CheckDynamicTypeHandler {
6660 AccessKinds AccessKind;
6661 typedef bool result_type;
6662 bool failed() { return false; }
6663 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
6664 return true;
6665 }
6666 bool found(APSInt &Value, QualType SubobjType) { return true; }
6667 bool found(APFloat &Value, QualType SubobjType) { return true; }
6668};
6669} // end anonymous namespace
6670
6671/// Check that we can access the notional vptr of an object / determine its
6672/// dynamic type.
6673static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This,
6674 AccessKinds AK, bool Polymorphic) {
6675 if (This.Designator.Invalid)
6676 return false;
6677
6678 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: This, LValType: QualType());
6679
6680 if (!Obj)
6681 return false;
6682
6683 if (!Obj.Value) {
6684 // The object is not usable in constant expressions, so we can't inspect
6685 // its value to see if it's in-lifetime or what the active union members
6686 // are. We can still check for a one-past-the-end lvalue.
6687 if (This.Designator.isOnePastTheEnd() ||
6688 This.Designator.isMostDerivedAnUnsizedArray()) {
6689 Info.FFDiag(E, DiagId: This.Designator.isOnePastTheEnd()
6690 ? diag::note_constexpr_access_past_end
6691 : diag::note_constexpr_access_unsized_array)
6692 << AK;
6693 return false;
6694 } else if (Polymorphic) {
6695 // Conservatively refuse to perform a polymorphic operation if we would
6696 // not be able to read a notional 'vptr' value.
6697 if (!Info.checkingPotentialConstantExpression() ||
6698 !This.AllowConstexprUnknown) {
6699 APValue Val;
6700 This.moveInto(V&: Val);
6701 QualType StarThisType =
6702 Info.Ctx.getLValueReferenceType(T: This.Designator.getType(Ctx&: Info.Ctx));
6703 Info.FFDiag(E, DiagId: diag::note_constexpr_polymorphic_unknown_dynamic_type)
6704 << AK << Val.getAsString(Ctx: Info.Ctx, Ty: StarThisType);
6705 }
6706 return false;
6707 }
6708 return true;
6709 }
6710
6711 CheckDynamicTypeHandler Handler{.AccessKind: AK};
6712 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
6713}
6714
6715/// Check that the pointee of the 'this' pointer in a member function call is
6716/// either within its lifetime or in its period of construction or destruction.
6717static bool
6718checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E,
6719 const LValue &This,
6720 const CXXMethodDecl *NamedMember) {
6721 return checkDynamicType(
6722 Info, E, This,
6723 AK: isa<CXXDestructorDecl>(Val: NamedMember) ? AK_Destroy : AK_MemberCall, Polymorphic: false);
6724}
6725
6726struct DynamicType {
6727 /// The dynamic class type of the object.
6728 const CXXRecordDecl *Type;
6729 /// The corresponding path length in the lvalue.
6730 unsigned PathLength;
6731};
6732
6733static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator,
6734 unsigned PathLength) {
6735 assert(PathLength >= Designator.MostDerivedPathLength && PathLength <=
6736 Designator.Entries.size() && "invalid path length");
6737 return (PathLength == Designator.MostDerivedPathLength)
6738 ? Designator.MostDerivedType->getAsCXXRecordDecl()
6739 : getAsBaseClass(E: Designator.Entries[PathLength - 1]);
6740}
6741
6742/// Determine the dynamic type of an object.
6743static std::optional<DynamicType> ComputeDynamicType(EvalInfo &Info,
6744 const Expr *E,
6745 LValue &This,
6746 AccessKinds AK) {
6747 // If we don't have an lvalue denoting an object of class type, there is no
6748 // meaningful dynamic type. (We consider objects of non-class type to have no
6749 // dynamic type.)
6750 if (!checkDynamicType(Info, E, This, AK,
6751 Polymorphic: AK != AK_TypeId || This.AllowConstexprUnknown))
6752 return std::nullopt;
6753
6754 if (This.Designator.Invalid)
6755 return std::nullopt;
6756
6757 // Refuse to compute a dynamic type in the presence of virtual bases
6758 // before C++26. This shouldn't happen other than in constant-folding
6759 // situations, since literal types can't have virtual bases.
6760 const CXXRecordDecl *Class =
6761 This.Designator.MostDerivedType->getAsCXXRecordDecl();
6762 if (!Class || (!Info.getLangOpts().CPlusPlus26 && Class->getNumVBases())) {
6763 Info.FFDiag(E);
6764 return std::nullopt;
6765 }
6766
6767 // FIXME: For very deep class hierarchies, it might be beneficial to use a
6768 // binary search here instead. But the overwhelmingly common case is that
6769 // we're not in the middle of a constructor, so it probably doesn't matter
6770 // in practice.
6771 ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries;
6772 for (unsigned PathLength = This.Designator.MostDerivedPathLength;
6773 PathLength <= Path.size(); ++PathLength) {
6774 switch (Info.isEvaluatingCtorDtor(Base: This.getLValueBase(),
6775 Path: Path.slice(N: 0, M: PathLength))) {
6776 case ConstructionPhase::Bases:
6777 case ConstructionPhase::DestroyingBases:
6778 // We're constructing or destroying a base class. This is not the dynamic
6779 // type.
6780 break;
6781
6782 case ConstructionPhase::None:
6783 case ConstructionPhase::AfterBases:
6784 case ConstructionPhase::AfterFields:
6785 case ConstructionPhase::Destroying:
6786 // We've finished constructing the base classes and not yet started
6787 // destroying them again, so this is the dynamic type.
6788 return DynamicType{.Type: getBaseClassType(Designator&: This.Designator, PathLength),
6789 .PathLength: PathLength};
6790 }
6791 }
6792
6793 // CWG issue 1517: we're constructing a base class of the object described by
6794 // 'This', so that object has not yet begun its period of construction and
6795 // any polymorphic operation on it results in undefined behavior.
6796 Info.FFDiag(E);
6797 return std::nullopt;
6798}
6799
6800/// Perform virtual dispatch.
6801static const CXXMethodDecl *HandleVirtualDispatch(
6802 EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found,
6803 llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) {
6804 std::optional<DynamicType> DynType = ComputeDynamicType(
6805 Info, E, This,
6806 AK: isa<CXXDestructorDecl>(Val: Found) ? AK_Destroy : AK_MemberCall);
6807 if (!DynType)
6808 return nullptr;
6809
6810 // Find the final overrider. It must be declared in one of the classes on the
6811 // path from the dynamic type to the static type.
6812 // FIXME: If we ever allow literal types to have virtual base classes, that
6813 // won't be true.
6814 const CXXMethodDecl *Callee = Found;
6815 unsigned PathLength = DynType->PathLength;
6816 for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) {
6817 const CXXRecordDecl *Class = getBaseClassType(Designator&: This.Designator, PathLength);
6818 const CXXMethodDecl *Overrider =
6819 Found->getCorrespondingMethodDeclaredInClass(RD: Class, MayBeBase: false);
6820 if (Overrider) {
6821 Callee = Overrider;
6822 break;
6823 }
6824 }
6825
6826 // C++2a [class.abstract]p6:
6827 // the effect of making a virtual call to a pure virtual function [...] is
6828 // undefined
6829 if (Callee->isPureVirtual()) {
6830 Info.FFDiag(E, DiagId: diag::note_constexpr_pure_virtual_call, ExtraNotes: 1) << Callee;
6831 Info.Note(Loc: Callee->getLocation(), DiagId: diag::note_declared_at);
6832 return nullptr;
6833 }
6834
6835 // If necessary, walk the rest of the path to determine the sequence of
6836 // covariant adjustment steps to apply.
6837 if (!Info.Ctx.hasSameUnqualifiedType(T1: Callee->getReturnType(),
6838 T2: Found->getReturnType())) {
6839 CovariantAdjustmentPath.push_back(Elt: Callee->getReturnType());
6840 for (unsigned CovariantPathLength = PathLength + 1;
6841 CovariantPathLength != This.Designator.Entries.size();
6842 ++CovariantPathLength) {
6843 const CXXRecordDecl *NextClass =
6844 getBaseClassType(Designator&: This.Designator, PathLength: CovariantPathLength);
6845 const CXXMethodDecl *Next =
6846 Found->getCorrespondingMethodDeclaredInClass(RD: NextClass, MayBeBase: false);
6847 if (Next && !Info.Ctx.hasSameUnqualifiedType(
6848 T1: Next->getReturnType(), T2: CovariantAdjustmentPath.back()))
6849 CovariantAdjustmentPath.push_back(Elt: Next->getReturnType());
6850 }
6851 if (!Info.Ctx.hasSameUnqualifiedType(T1: Found->getReturnType(),
6852 T2: CovariantAdjustmentPath.back()))
6853 CovariantAdjustmentPath.push_back(Elt: Found->getReturnType());
6854 }
6855
6856 // Perform 'this' adjustment.
6857 if (!CastToDerivedClass(Info, E, Result&: This, TruncatedType: Callee->getParent(), TruncatedElements: PathLength))
6858 return nullptr;
6859
6860 return Callee;
6861}
6862
6863/// Perform the adjustment from a value returned by a virtual function to
6864/// a value of the statically expected type, which may be a pointer or
6865/// reference to a base class of the returned type.
6866static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E,
6867 APValue &Result,
6868 ArrayRef<QualType> Path) {
6869 assert(Result.isLValue() &&
6870 "unexpected kind of APValue for covariant return");
6871 if (Result.isNullPointer())
6872 return true;
6873
6874 LValue LVal;
6875 LVal.setFrom(Ctx: Info.Ctx, V: Result);
6876
6877 const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl();
6878 for (unsigned I = 1; I != Path.size(); ++I) {
6879 const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl();
6880 assert(OldClass && NewClass && "unexpected kind of covariant return");
6881 if (OldClass != NewClass &&
6882 !CastToBaseClass(Info, E, Result&: LVal, DerivedRD: OldClass, BaseRD: NewClass))
6883 return false;
6884 OldClass = NewClass;
6885 }
6886
6887 LVal.moveInto(V&: Result);
6888 return true;
6889}
6890
6891/// Determine whether \p Base, which is known to be a direct base class of
6892/// \p Derived, is a public base class.
6893static bool isBaseClassPublic(const CXXRecordDecl *Derived,
6894 const CXXRecordDecl *Base) {
6895 for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) {
6896 if (BaseSpec.isVirtual())
6897 continue;
6898 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6899 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6900 return BaseSpec.getAccessSpecifier() == AS_public;
6901 }
6902 for (const CXXBaseSpecifier &BaseSpec : Derived->vbases()) {
6903 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6904 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6905 return BaseSpec.getAccessSpecifier() == AS_public;
6906 }
6907
6908 llvm_unreachable("Base is not a direct base of Derived");
6909}
6910
6911/// Apply the given dynamic cast operation on the provided lvalue.
6912///
6913/// This implements the hard case of dynamic_cast, requiring a "runtime check"
6914/// to find a suitable target subobject.
6915static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E,
6916 LValue &Ptr) {
6917 // We can't do anything with a non-symbolic pointer value.
6918 SubobjectDesignator &D = Ptr.Designator;
6919 if (D.Invalid)
6920 return false;
6921
6922 // C++ [expr.dynamic.cast]p6:
6923 // If v is a null pointer value, the result is a null pointer value.
6924 if (Ptr.isNullPointer() && !E->isGLValue())
6925 return true;
6926
6927 // For all the other cases, we need the pointer to point to an object within
6928 // its lifetime / period of construction / destruction, and we need to know
6929 // its dynamic type.
6930 std::optional<DynamicType> DynType =
6931 ComputeDynamicType(Info, E, This&: Ptr, AK: AK_DynamicCast);
6932 if (!DynType)
6933 return false;
6934
6935 // C++ [expr.dynamic.cast]p7:
6936 // If T is "pointer to cv void", then the result is a pointer to the most
6937 // derived object
6938 if (E->getType()->isVoidPointerType())
6939 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength);
6940
6941 const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl();
6942 assert(C && "dynamic_cast target is not void pointer nor class");
6943 CanQualType CQT = Info.Ctx.getCanonicalTagType(TD: C);
6944
6945 auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) {
6946 // C++ [expr.dynamic.cast]p9:
6947 if (!E->isGLValue()) {
6948 // The value of a failed cast to pointer type is the null pointer value
6949 // of the required result type.
6950 Ptr.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
6951 return true;
6952 }
6953
6954 // A failed cast to reference type throws [...] std::bad_cast.
6955 unsigned DiagKind;
6956 if (!Paths && (declaresSameEntity(D1: DynType->Type, D2: C) ||
6957 DynType->Type->isDerivedFrom(Base: C)))
6958 DiagKind = 0;
6959 else if (!Paths || Paths->begin() == Paths->end())
6960 DiagKind = 1;
6961 else if (Paths->isAmbiguous(BaseType: CQT))
6962 DiagKind = 2;
6963 else {
6964 assert(Paths->front().Access != AS_public && "why did the cast fail?");
6965 DiagKind = 3;
6966 }
6967 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_cast_to_reference_failed)
6968 << DiagKind << Ptr.Designator.getType(Ctx&: Info.Ctx)
6969 << Info.Ctx.getCanonicalTagType(TD: DynType->Type)
6970 << E->getType().getUnqualifiedType();
6971 return false;
6972 };
6973
6974 // Runtime check, phase 1:
6975 // Walk from the base subobject towards the derived object looking for the
6976 // target type.
6977 for (int PathLength = Ptr.Designator.Entries.size();
6978 PathLength >= (int)DynType->PathLength; --PathLength) {
6979 const CXXRecordDecl *Class = getBaseClassType(Designator&: Ptr.Designator, PathLength);
6980 if (declaresSameEntity(D1: Class, D2: C))
6981 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: Class, TruncatedElements: PathLength);
6982 // We can only walk across public inheritance edges.
6983 if (PathLength > (int)DynType->PathLength &&
6984 !isBaseClassPublic(Derived: getBaseClassType(Designator&: Ptr.Designator, PathLength: PathLength - 1),
6985 Base: Class))
6986 return RuntimeCheckFailed(nullptr);
6987 }
6988
6989 // Runtime check, phase 2:
6990 // Search the dynamic type for an unambiguous public base of type C.
6991 CXXBasePaths Paths(/*FindAmbiguities=*/true,
6992 /*RecordPaths=*/true, /*DetectVirtual=*/false);
6993 if (DynType->Type->isDerivedFrom(Base: C, Paths) && !Paths.isAmbiguous(BaseType: CQT) &&
6994 Paths.front().Access == AS_public) {
6995 // Downcast to the dynamic type...
6996 if (!CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength))
6997 return false;
6998 // ... then upcast to the chosen base class subobject.
6999 for (CXXBasePathElement &Elem : Paths.front())
7000 if (!HandleLValueBase(Info, E, Obj&: Ptr, DerivedDecl: Elem.Class, Base: Elem.Base))
7001 return false;
7002 return true;
7003 }
7004
7005 // Otherwise, the runtime check fails.
7006 return RuntimeCheckFailed(&Paths);
7007}
7008
7009namespace {
7010struct StartLifetimeOfUnionMemberHandler {
7011 EvalInfo &Info;
7012 const Expr *LHSExpr;
7013 const FieldDecl *Field;
7014 bool DuringInit;
7015 bool Failed = false;
7016 static const AccessKinds AccessKind = AK_Assign;
7017
7018 typedef bool result_type;
7019 bool failed() { return Failed; }
7020 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
7021 // We are supposed to perform no initialization but begin the lifetime of
7022 // the object. We interpret that as meaning to do what default
7023 // initialization of the object would do if all constructors involved were
7024 // trivial:
7025 // * All base, non-variant member, and array element subobjects' lifetimes
7026 // begin
7027 // * No variant members' lifetimes begin
7028 // * All scalar subobjects whose lifetimes begin have indeterminate values
7029 assert(SubobjType->isUnionType());
7030 if (declaresSameEntity(D1: Subobj.getUnionField(), D2: Field)) {
7031 // This union member is already active. If it's also in-lifetime, there's
7032 // nothing to do.
7033 if (Subobj.getUnionValue().hasValue())
7034 return true;
7035 } else if (DuringInit) {
7036 // We're currently in the process of initializing a different union
7037 // member. If we carried on, that initialization would attempt to
7038 // store to an inactive union member, resulting in undefined behavior.
7039 Info.FFDiag(E: LHSExpr,
7040 DiagId: diag::note_constexpr_union_member_change_during_init);
7041 return false;
7042 }
7043 APValue Result;
7044 Failed = !handleDefaultInitValue(T: Field->getType(), Result);
7045 Subobj.setUnion(Field, Value: Result);
7046 return true;
7047 }
7048 bool found(APSInt &Value, QualType SubobjType) {
7049 llvm_unreachable("wrong value kind for union object");
7050 }
7051 bool found(APFloat &Value, QualType SubobjType) {
7052 llvm_unreachable("wrong value kind for union object");
7053 }
7054};
7055} // end anonymous namespace
7056
7057const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind;
7058
7059/// Handle a builtin simple-assignment or a call to a trivial assignment
7060/// operator whose left-hand side might involve a union member access. If it
7061/// does, implicitly start the lifetime of any accessed union elements per
7062/// C++20 [class.union]5.
7063static bool MaybeHandleUnionActiveMemberChange(EvalInfo &Info,
7064 const Expr *LHSExpr,
7065 const LValue &LHS) {
7066 if (LHS.InvalidBase || LHS.Designator.Invalid)
7067 return false;
7068
7069 llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths;
7070 // C++ [class.union]p5:
7071 // define the set S(E) of subexpressions of E as follows:
7072 unsigned PathLength = LHS.Designator.Entries.size();
7073 for (const Expr *E = LHSExpr; E != nullptr;) {
7074 // -- If E is of the form A.B, S(E) contains the elements of S(A)...
7075 if (auto *ME = dyn_cast<MemberExpr>(Val: E)) {
7076 auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl());
7077 // Note that we can't implicitly start the lifetime of a reference,
7078 // so we don't need to proceed any further if we reach one.
7079 if (!FD || FD->getType()->isReferenceType())
7080 break;
7081
7082 // ... and also contains A.B if B names a union member ...
7083 if (FD->getParent()->isUnion()) {
7084 // ... of a non-class, non-array type, or of a class type with a
7085 // trivial default constructor that is not deleted, or an array of
7086 // such types.
7087 auto *RD =
7088 FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
7089 if (!RD || RD->hasTrivialDefaultConstructor())
7090 UnionPathLengths.push_back(Elt: {PathLength - 1, FD});
7091 }
7092
7093 E = ME->getBase();
7094 --PathLength;
7095 assert(declaresSameEntity(FD,
7096 LHS.Designator.Entries[PathLength]
7097 .getAsBaseOrMember().getPointer()));
7098
7099 // -- If E is of the form A[B] and is interpreted as a built-in array
7100 // subscripting operator, S(E) is [S(the array operand, if any)].
7101 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) {
7102 // Step over an ArrayToPointerDecay implicit cast.
7103 auto *Base = ASE->getBase()->IgnoreImplicit();
7104 if (!Base->getType()->isArrayType())
7105 break;
7106
7107 E = Base;
7108 --PathLength;
7109
7110 } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E)) {
7111 // Step over a derived-to-base conversion.
7112 E = ICE->getSubExpr();
7113 if (ICE->getCastKind() == CK_NoOp)
7114 continue;
7115 if (ICE->getCastKind() != CK_DerivedToBase &&
7116 ICE->getCastKind() != CK_UncheckedDerivedToBase)
7117 break;
7118 // Walk path backwards as we walk up from the base to the derived class.
7119 for (const CXXBaseSpecifier *Elt : llvm::reverse(C: ICE->path())) {
7120 if (Elt->isVirtual()) {
7121 // A class with virtual base classes never has a trivial default
7122 // constructor, so S(E) is empty in this case.
7123 E = nullptr;
7124 break;
7125 }
7126
7127 --PathLength;
7128 assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(),
7129 LHS.Designator.Entries[PathLength]
7130 .getAsBaseOrMember().getPointer()));
7131 }
7132
7133 // -- Otherwise, S(E) is empty.
7134 } else {
7135 break;
7136 }
7137 }
7138
7139 // Common case: no unions' lifetimes are started.
7140 if (UnionPathLengths.empty())
7141 return true;
7142
7143 // if modification of X [would access an inactive union member], an object
7144 // of the type of X is implicitly created
7145 CompleteObject Obj =
7146 findCompleteObject(Info, E: LHSExpr, AK: AK_Assign, LVal: LHS, LValType: LHSExpr->getType());
7147 if (!Obj)
7148 return false;
7149 for (std::pair<unsigned, const FieldDecl *> LengthAndField :
7150 llvm::reverse(C&: UnionPathLengths)) {
7151 // Form a designator for the union object.
7152 SubobjectDesignator D = LHS.Designator;
7153 D.truncate(Ctx&: Info.Ctx, Base: LHS.Base, NewLength: LengthAndField.first);
7154
7155 bool DuringInit = Info.isEvaluatingCtorDtor(Base: LHS.Base, Path: D.Entries) ==
7156 ConstructionPhase::AfterBases;
7157 StartLifetimeOfUnionMemberHandler StartLifetime{
7158 .Info: Info, .LHSExpr: LHSExpr, .Field: LengthAndField.second, .DuringInit: DuringInit};
7159 if (!findSubobject(Info, E: LHSExpr, Obj, Sub: D, handler&: StartLifetime))
7160 return false;
7161 }
7162
7163 return true;
7164}
7165
7166static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg,
7167 CallRef Call, EvalInfo &Info, bool NonNull = false,
7168 APValue **EvaluatedArg = nullptr) {
7169 LValue LV;
7170 // Create the parameter slot and register its destruction. For a vararg
7171 // argument, create a temporary.
7172 // FIXME: For calling conventions that destroy parameters in the callee,
7173 // should we consider performing destruction when the function returns
7174 // instead?
7175 APValue &V = PVD ? Info.CurrentCall->createParam(Args: Call, PVD, LV)
7176 : Info.CurrentCall->createTemporary(Key: Arg, T: Arg->getType(),
7177 Scope: ScopeKind::Call, LV);
7178 if (!EvaluateInPlace(Result&: V, Info, This: LV, E: Arg))
7179 return false;
7180
7181 // Passing a null pointer to an __attribute__((nonnull)) parameter results in
7182 // undefined behavior, so is non-constant.
7183 if (NonNull && V.isLValue() && V.isNullPointer()) {
7184 Info.CCEDiag(E: Arg, DiagId: diag::note_non_null_attribute_failed);
7185 return false;
7186 }
7187
7188 if (EvaluatedArg)
7189 *EvaluatedArg = &V;
7190
7191 return true;
7192}
7193
7194/// Evaluate the arguments to a function call.
7195static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call,
7196 EvalInfo &Info, const FunctionDecl *Callee,
7197 bool RightToLeft = false,
7198 LValue *ObjectArg = nullptr) {
7199 bool Success = true;
7200 llvm::SmallBitVector ForbiddenNullArgs;
7201 if (Callee->hasAttr<NonNullAttr>()) {
7202 ForbiddenNullArgs.resize(N: Args.size());
7203 for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) {
7204 if (!Attr->args_size()) {
7205 ForbiddenNullArgs.set();
7206 break;
7207 } else
7208 for (auto Idx : Attr->args()) {
7209 unsigned ASTIdx = Idx.getASTIndex();
7210 if (ASTIdx >= Args.size())
7211 continue;
7212 ForbiddenNullArgs[ASTIdx] = true;
7213 }
7214 }
7215 }
7216 for (unsigned I = 0; I < Args.size(); I++) {
7217 unsigned Idx = RightToLeft ? Args.size() - I - 1 : I;
7218 const ParmVarDecl *PVD =
7219 Idx < Callee->getNumParams() ? Callee->getParamDecl(i: Idx) : nullptr;
7220 bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx];
7221 APValue *That = nullptr;
7222 if (!EvaluateCallArg(PVD, Arg: Args[Idx], Call, Info, NonNull, EvaluatedArg: &That)) {
7223 // If we're checking for a potential constant expression, evaluate all
7224 // initializers even if some of them fail.
7225 if (!Info.noteFailure())
7226 return false;
7227 Success = false;
7228 }
7229 if (PVD && PVD->isExplicitObjectParameter() && That && That->isLValue())
7230 ObjectArg->setFrom(Ctx: Info.Ctx, V: *That);
7231 }
7232 return Success;
7233}
7234
7235/// Perform a trivial copy from Param, which is the parameter of a copy or move
7236/// constructor or assignment operator.
7237static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param,
7238 const Expr *E, APValue &Result,
7239 bool CopyObjectRepresentation) {
7240 // Find the reference argument.
7241 CallStackFrame *Frame = Info.CurrentCall;
7242 APValue *RefValue = Info.getParamSlot(Call: Frame->Arguments, PVD: Param);
7243 if (!RefValue) {
7244 Info.FFDiag(E);
7245 return false;
7246 }
7247
7248 // Copy out the contents of the RHS object.
7249 LValue RefLValue;
7250 RefLValue.setFrom(Ctx: Info.Ctx, V: *RefValue);
7251 return handleLValueToRValueConversion(
7252 Info, Conv: E, Type: Param->getType().getNonReferenceType(), LVal: RefLValue, RVal&: Result,
7253 WantObjectRepresentation: CopyObjectRepresentation);
7254}
7255
7256bool FunctionDefinitionCanBeLazilyInstantiated(const FunctionDecl *FD) {
7257 // If the function is a specialization of a member function template which has
7258 // been declared a friend by another class, 'FD' might be a Decl marked as an
7259 // explicit specialization, which would then be classified as "not implicitly
7260 // instantiable". Walking to the most recent Decl resolves this.
7261 FD = FD->getMostRecentDecl();
7262
7263 if (FD->isDefined() || !FD->isImplicitlyInstantiable() || !FD->isConstexpr())
7264 return false;
7265
7266 FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
7267 return Pattern && Pattern->isDefined();
7268}
7269
7270static void TryInstantiateFunctionBeforeCall(const FunctionDecl *FD,
7271 EvalInfo &Info,
7272 SourceLocation Loc) {
7273 // [C++26] [temp.inst] p5
7274 // [...] the function template specialization is implicitly instantiated
7275 // when the specialization is referenced in a context that requires a function
7276 // definition to exist or if the existence of the definition affects the
7277 // semantics of the program.
7278
7279 SemaProxy *SProxy = Info.getSemaProxy();
7280 if (SProxy && FunctionDefinitionCanBeLazilyInstantiated(FD) &&
7281 Info.InConstantContext)
7282 SProxy->instantiateFunctionDefinition(PointOfInstantiation: Loc, Function: const_cast<FunctionDecl *>(FD));
7283}
7284
7285/// Evaluate a function call.
7286static bool HandleFunctionCall(SourceLocation CallLoc,
7287 const FunctionDecl *Callee,
7288 const LValue *ObjectArg, const Expr *E,
7289 ArrayRef<const Expr *> Args, CallRef Call,
7290 const Stmt *Body, EvalInfo &Info,
7291 APValue &Result, const LValue *ResultSlot) {
7292 if (!Info.CheckCallLimit(Loc: CallLoc))
7293 return false;
7294
7295 CallStackFrame Frame(Info, E->getSourceRange(), Callee, ObjectArg, E, Call);
7296
7297 // For a trivial copy or move assignment, perform an APValue copy. This is
7298 // essential for unions, where the operations performed by the assignment
7299 // operator cannot be represented as statements.
7300 //
7301 // Skip this for non-union classes with no fields; in that case, the defaulted
7302 // copy/move does not actually read the object.
7303 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: Callee);
7304
7305 auto IsTrivialMemoryOperation = [&](const CXXMethodDecl *MD) {
7306 if (!MD || !MD->isDefaulted())
7307 return false;
7308 if (!MD->isCopyAssignmentOperator() && !MD->isMoveAssignmentOperator())
7309 return false;
7310 return MD->getParent()->isUnion() ||
7311 (MD->isTrivial() &&
7312 isReadByLvalueToRvalueConversion(RD: MD->getParent()));
7313 };
7314
7315 if (IsTrivialMemoryOperation(MD)) {
7316 unsigned ExplicitOffset = MD->isExplicitObjectMemberFunction() ? 1 : 0;
7317 assert(ObjectArg);
7318 APValue RHSValue;
7319 if (!handleTrivialCopy(Info, Param: MD->getParamDecl(i: 0), E: Args[0], Result&: RHSValue,
7320 CopyObjectRepresentation: MD->getParent()->isUnion()))
7321 return false;
7322
7323 if (!handleAssignment(Info, E: Args[ExplicitOffset], LVal: *ObjectArg,
7324 LValType: MD->getFunctionObjectParameterReferenceType(),
7325 Val&: RHSValue))
7326 return false;
7327 ObjectArg->moveInto(V&: Result);
7328 return true;
7329 } else if (MD && isLambdaCallOperator(MD)) {
7330 // We're in a lambda; determine the lambda capture field maps unless we're
7331 // just constexpr checking a lambda's call operator. constexpr checking is
7332 // done before the captures have been added to the closure object (unless
7333 // we're inferring constexpr-ness), so we don't have access to them in this
7334 // case. But since we don't need the captures to constexpr check, we can
7335 // just ignore them.
7336 if (!Info.checkingPotentialConstantExpression())
7337 MD->getParent()->getCaptureFields(Captures&: Frame.LambdaCaptureFields,
7338 ThisCapture&: Frame.LambdaThisCaptureField);
7339 }
7340
7341 StmtResult Ret = {.Value: Result, .Slot: ResultSlot};
7342 EvalStmtResult ESR = EvaluateStmt(Result&: Ret, Info, S: Body);
7343 if (ESR == ESR_Succeeded) {
7344 if (Callee->getReturnType()->isVoidType())
7345 return true;
7346 Info.FFDiag(Loc: Callee->getEndLoc(), DiagId: diag::note_constexpr_no_return);
7347 }
7348 return ESR == ESR_Returned;
7349}
7350
7351static bool HandleConstructorCall(const Expr *E, const LValue &This,
7352 CallRef Call,
7353 const CXXConstructorDecl *Definition,
7354 EvalInfo &Info, APValue &Result,
7355 bool IsCompleteClass = true);
7356
7357static bool HandleConstructorCall(const Expr *E, const LValue &This,
7358 ArrayRef<const Expr *> Args,
7359 const CXXConstructorDecl *Definition,
7360 EvalInfo &Info, APValue &Result,
7361 bool IsCompleteClass = true) {
7362 CallScopeRAII CallScope(Info);
7363 CallRef Call = Info.CurrentCall->createCall(Callee: Definition);
7364 if (!EvaluateArgs(Args, Call, Info, Callee: Definition))
7365 return false;
7366
7367 return HandleConstructorCall(E, This, Call, Definition, Info, Result,
7368 IsCompleteClass) &&
7369 CallScope.destroy();
7370}
7371
7372/// Evaluate a constructor call.
7373static bool HandleConstructorCall(const Expr *E, const LValue &This,
7374 CallRef Call,
7375 const CXXConstructorDecl *Definition,
7376 EvalInfo &Info, APValue &Result,
7377 bool IsCompleteClass) {
7378
7379 SourceLocation CallLoc = E->getExprLoc();
7380 if (!Info.CheckCallLimit(Loc: CallLoc))
7381 return false;
7382
7383 const CXXRecordDecl *RD = Definition->getParent();
7384 if (!Info.getLangOpts().CPlusPlus26 && RD->getNumVBases()) {
7385 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_base) << RD;
7386 return false;
7387 }
7388
7389 EvalInfo::EvaluatingConstructorRAII EvalObj(
7390 Info,
7391 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
7392 RD->getNumBases());
7393 CallStackFrame Frame(Info, E->getSourceRange(), Definition, &This, E, Call);
7394
7395 // FIXME: Creating an APValue just to hold a nonexistent return value is
7396 // wasteful.
7397 APValue RetVal;
7398 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7399
7400 // If it's a delegating constructor, delegate.
7401 if (Definition->isDelegatingConstructor()) {
7402 CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
7403 if ((*I)->getInit()->isValueDependent()) {
7404 if (!EvaluateDependentExpr(E: (*I)->getInit(), Info))
7405 return false;
7406 } else {
7407 FullExpressionRAII InitScope(Info);
7408 if (!EvaluateInPlace(Result, Info, This, E: (*I)->getInit()) ||
7409 !InitScope.destroy())
7410 return false;
7411 }
7412 return EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed;
7413 }
7414
7415 // For a trivial copy or move constructor, perform an APValue copy. This is
7416 // essential for unions (or classes with anonymous union members), where the
7417 // operations performed by the constructor cannot be represented by
7418 // ctor-initializers.
7419 //
7420 // Skip this for empty non-union classes; we should not perform an
7421 // lvalue-to-rvalue conversion on them because their copy constructor does not
7422 // actually read them.
7423 if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() &&
7424 (Definition->getParent()->isUnion() ||
7425 (Definition->isTrivial() &&
7426 isReadByLvalueToRvalueConversion(RD: Definition->getParent())))) {
7427 return handleTrivialCopy(Info, Param: Definition->getParamDecl(i: 0), E, Result,
7428 CopyObjectRepresentation: Definition->getParent()->isUnion());
7429 }
7430
7431 // Reserve space for the struct members.
7432 if (!Result.hasValue()) {
7433 if (!RD->isUnion()) {
7434 unsigned NonVirtualBases = countNonVirtualBases(RD);
7435 Result = APValue(APValue::UninitStruct(), NonVirtualBases,
7436 RD->getNumFields(), RD->getNumVBases());
7437 } else
7438 // A union starts with no active member.
7439 Result = APValue((const FieldDecl*)nullptr);
7440 }
7441
7442 if (RD->isInvalidDecl()) return false;
7443 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7444
7445 // A scope for temporaries lifetime-extended by reference members.
7446 BlockScopeRAII LifetimeExtendedScope(Info);
7447
7448 bool Success = true;
7449 unsigned BasesSeen = 0;
7450 unsigned VirtualBasesSeen = 0;
7451 unsigned NonVirtualBases = countNonVirtualBases(RD);
7452
7453 CXXRecordDecl::field_iterator FieldIt = RD->field_begin();
7454 auto SkipToField = [&](FieldDecl *FD, bool Indirect) {
7455 // We might be initializing the same field again if this is an indirect
7456 // field initialization.
7457 if (FieldIt == RD->field_end() ||
7458 FieldIt->getFieldIndex() > FD->getFieldIndex()) {
7459 assert(Indirect && "fields out of order?");
7460 return;
7461 }
7462
7463 // Default-initialize any fields with no explicit initializer.
7464 for (; !declaresSameEntity(D1: *FieldIt, D2: FD); ++FieldIt) {
7465 assert(FieldIt != RD->field_end() && "missing field?");
7466 if (!FieldIt->isUnnamedBitField())
7467 Success &= handleDefaultInitValue(
7468 T: FieldIt->getType(),
7469 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7470 }
7471 ++FieldIt;
7472 };
7473 for (const auto *I : Definition->inits()) {
7474 LValue Subobject = This;
7475 LValue SubobjectParent = This;
7476 APValue *Value = &Result;
7477
7478 // Determine the subobject to initialize.
7479 FieldDecl *FD = nullptr;
7480 if (I->isBaseInitializer()) {
7481 QualType BaseType(I->getBaseClass(), 0);
7482 if (I->isBaseVirtual()) {
7483 if (This.pointsToCompleteClass(D: RD)) {
7484 if (!HandleLValueDirectVirtualBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7485 Base: BaseType->getAsCXXRecordDecl(),
7486 RL: &Layout))
7487 return false;
7488 Value = &Result.getStructVirtualBase(i: VirtualBasesSeen++);
7489 } else {
7490 continue;
7491 }
7492
7493 } else {
7494 if (!HandleLValueDirectBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7495 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7496 return false;
7497 Value = &Result.getStructBase(i: BasesSeen++);
7498 }
7499 } else if ((FD = I->getMember())) {
7500 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD, RL: &Layout))
7501 return false;
7502 if (RD->isUnion()) {
7503 Result = APValue(FD);
7504 Value = &Result.getUnionValue();
7505 } else {
7506 SkipToField(FD, false);
7507 Value = &Result.getStructField(i: FD->getFieldIndex());
7508 }
7509 } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) {
7510 // Walk the indirect field decl's chain to find the object to initialize,
7511 // and make sure we've initialized every step along it.
7512 auto IndirectFieldChain = IFD->chain();
7513 for (auto *C : IndirectFieldChain) {
7514 FD = cast<FieldDecl>(Val: C);
7515 CXXRecordDecl *CD = cast<CXXRecordDecl>(Val: FD->getParent());
7516 // Switch the union field if it differs. This happens if we had
7517 // preceding zero-initialization, and we're now initializing a union
7518 // subobject other than the first.
7519 // FIXME: In this case, the values of the other subobjects are
7520 // specified, since zero-initialization sets all padding bits to zero.
7521 if (!Value->hasValue() ||
7522 (Value->isUnion() &&
7523 !declaresSameEntity(D1: Value->getUnionField(), D2: FD))) {
7524 if (CD->isUnion())
7525 *Value = APValue(FD);
7526 else
7527 // FIXME: This immediately starts the lifetime of all members of
7528 // an anonymous struct. It would be preferable to strictly start
7529 // member lifetime in initialization order.
7530 Success &= handleDefaultInitValue(T: Info.Ctx.getCanonicalTagType(TD: CD),
7531 Result&: *Value);
7532 }
7533 // Store Subobject as its parent before updating it for the last element
7534 // in the chain.
7535 if (C == IndirectFieldChain.back())
7536 SubobjectParent = Subobject;
7537 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD))
7538 return false;
7539 if (CD->isUnion())
7540 Value = &Value->getUnionValue();
7541 else {
7542 if (C == IndirectFieldChain.front() && !RD->isUnion())
7543 SkipToField(FD, true);
7544 Value = &Value->getStructField(i: FD->getFieldIndex());
7545 }
7546 }
7547 } else {
7548 llvm_unreachable("unknown base initializer kind");
7549 }
7550
7551 // Need to override This for implicit field initializers as in this case
7552 // This refers to innermost anonymous struct/union containing initializer,
7553 // not to currently constructed class.
7554 const Expr *Init = I->getInit();
7555 if (Init->isValueDependent()) {
7556 if (!EvaluateDependentExpr(E: Init, Info))
7557 return false;
7558 } else {
7559 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent,
7560 isa<CXXDefaultInitExpr>(Val: Init));
7561 FullExpressionRAII InitScope(Info);
7562 if (FD && FD->getType()->isReferenceType() &&
7563 !FD->getType()->isFunctionReferenceType()) {
7564 LValue Result;
7565 if (!EvaluateInitForDeclOfReferenceType(Info, D: FD, Init, Result,
7566 Val&: *Value)) {
7567 if (!Info.noteFailure())
7568 return false;
7569 Success = false;
7570 }
7571 } else if (!EvaluateInPlace(Result&: *Value, Info, This: Subobject, E: Init) ||
7572 (FD && FD->isBitField() &&
7573 !truncateBitfieldValue(Info, E: Init, Value&: *Value, FD))) {
7574 // If we're checking for a potential constant expression, evaluate all
7575 // initializers even if some of them fail.
7576 if (!Info.noteFailure())
7577 return false;
7578 Success = false;
7579 }
7580 }
7581
7582 // This is the point at which the dynamic type of the object becomes this
7583 // class type.
7584 if (I->isBaseInitializer() && BasesSeen == NonVirtualBases)
7585 EvalObj.finishedConstructingBases();
7586 }
7587
7588 // Default-initialize any remaining fields.
7589 if (!RD->isUnion()) {
7590 for (; FieldIt != RD->field_end(); ++FieldIt) {
7591 if (!FieldIt->isUnnamedBitField())
7592 Success &= handleDefaultInitValue(
7593 T: FieldIt->getType(),
7594 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7595 }
7596 }
7597
7598 EvalObj.finishedConstructingFields();
7599
7600 return Success &&
7601 EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed &&
7602 LifetimeExtendedScope.destroy();
7603}
7604
7605static bool HandleDestructionImpl(EvalInfo &Info, SourceRange CallRange,
7606 const LValue &This, APValue &Value,
7607 QualType T, bool IsCompleteClass = true) {
7608 // Objects can only be destroyed while they're within their lifetimes.
7609 // FIXME: We have no representation for whether an object of type nullptr_t
7610 // is in its lifetime; it usually doesn't matter. Perhaps we should model it
7611 // as indeterminate instead?
7612 if (Value.isAbsent() && !T->isNullPtrType()) {
7613 APValue Printable;
7614 This.moveInto(V&: Printable);
7615 Info.FFDiag(Loc: CallRange.getBegin(),
7616 DiagId: diag::note_constexpr_destroy_out_of_lifetime)
7617 << Printable.getAsString(Ctx: Info.Ctx, Ty: Info.Ctx.getLValueReferenceType(T));
7618 return false;
7619 }
7620
7621 // Invent an expression for location purposes.
7622 // FIXME: We shouldn't need to do this.
7623 OpaqueValueExpr LocE(CallRange.getBegin(), Info.Ctx.IntTy, VK_PRValue);
7624
7625 // For arrays, destroy elements right-to-left.
7626 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) {
7627 uint64_t Size = CAT->getZExtSize();
7628 QualType ElemT = CAT->getElementType();
7629
7630 if (!CheckArraySize(Info, CAT, CallLoc: CallRange.getBegin()))
7631 return false;
7632
7633 LValue ElemLV = This;
7634 ElemLV.addArray(Info, E: &LocE, CAT);
7635 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: Size))
7636 return false;
7637
7638 // Ensure that we have actual array elements available to destroy; the
7639 // destructors might mutate the value, so we can't run them on the array
7640 // filler.
7641 if (Size && Size > Value.getArrayInitializedElts())
7642 expandArray(Array&: Value, Index: Value.getArraySize() - 1);
7643
7644 // The size of the array might have been reduced by
7645 // a placement new.
7646 for (Size = Value.getArraySize(); Size != 0; --Size) {
7647 APValue &Elem = Value.getArrayInitializedElt(I: Size - 1);
7648 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: -1) ||
7649 !HandleDestructionImpl(Info, CallRange, This: ElemLV, Value&: Elem, T: ElemT))
7650 return false;
7651 }
7652
7653 // End the lifetime of this array now.
7654 Value = APValue();
7655 return true;
7656 }
7657
7658 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
7659 if (!RD) {
7660 if (T.isDestructedType()) {
7661 Info.FFDiag(Loc: CallRange.getBegin(),
7662 DiagId: diag::note_constexpr_unsupported_destruction)
7663 << T;
7664 return false;
7665 }
7666
7667 Value = APValue();
7668 return true;
7669 }
7670
7671 if (!Info.getLangOpts().CPlusPlus26 && RD->getNumVBases()) {
7672 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_virtual_base) << RD;
7673 return false;
7674 }
7675
7676 // If an anonymous union would be destroyed, some enclosing destructor must
7677 // have been explicitly defined, and the anonymous union destruction should
7678 // have no effect.
7679 if (RD->isAnonymousStructOrUnion() && RD->isUnion()) {
7680 Value = APValue();
7681 return true;
7682 }
7683
7684 const CXXDestructorDecl *DD = RD->getDestructor();
7685 if (!DD && !RD->hasTrivialDestructor()) {
7686 Info.FFDiag(Loc: CallRange.getBegin());
7687 return false;
7688 }
7689
7690 if (!DD || DD->isTrivial()) {
7691 // A trivial destructor just ends the lifetime of the object. Check for
7692 // this case before checking for a body, because we might not bother
7693 // building a body for a trivial destructor. Note that it doesn't matter
7694 // whether the destructor is constexpr in this case; all trivial
7695 // destructors are constexpr.
7696 Value = APValue();
7697 return true;
7698 }
7699
7700 if (!Info.CheckCallLimit(Loc: CallRange.getBegin()))
7701 return false;
7702
7703 TryInstantiateFunctionBeforeCall(FD: DD, Info, Loc: CallRange.getBegin());
7704
7705 const FunctionDecl *Definition = nullptr;
7706 const Stmt *Body = DD->getBody(Definition);
7707
7708 if (!CheckConstexprFunction(Info, CallLoc: CallRange.getBegin(), Declaration: DD, Definition, Body))
7709 return false;
7710
7711 CallStackFrame Frame(Info, CallRange, Definition, &This, /*CallExpr=*/nullptr,
7712 CallRef());
7713
7714 // We're now in the period of destruction of this object.
7715 EvalInfo::EvaluatingDestructorRAII EvalObj(
7716 Info,
7717 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries});
7718 unsigned NonVirtualBases = countNonVirtualBases(RD);
7719 unsigned NumVirtualBases = RD->getNumVBases();
7720 unsigned BasesLeft = NonVirtualBases;
7721 if (!EvalObj.DidInsert) {
7722 // C++2a [class.dtor]p19:
7723 // the behavior is undefined if the destructor is invoked for an object
7724 // whose lifetime has ended
7725 // (Note that formally the lifetime ends when the period of destruction
7726 // begins, even though certain uses of the object remain valid until the
7727 // period of destruction ends.)
7728 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_double_destroy);
7729 return false;
7730 }
7731
7732 // FIXME: Creating an APValue just to hold a nonexistent return value is
7733 // wasteful.
7734 APValue RetVal;
7735 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7736 if (EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) == ESR_Failed)
7737 return false;
7738
7739 // A union destructor does not implicitly destroy its members.
7740 if (RD->isUnion())
7741 return true;
7742
7743 if (!ASTContext::hasLayout(D: RD))
7744 return false;
7745 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7746
7747 // We don't have a good way to iterate fields in reverse, so collect all the
7748 // fields first and then walk them backwards.
7749 SmallVector<FieldDecl*, 16> Fields(RD->fields());
7750 for (const FieldDecl *FD : llvm::reverse(C&: Fields)) {
7751 if (FD->isUnnamedBitField())
7752 continue;
7753
7754 LValue Subobject = This;
7755 if (!HandleLValueMember(Info, E: &LocE, LVal&: Subobject, FD, RL: &Layout))
7756 return false;
7757
7758 APValue *SubobjectValue = &Value.getStructField(i: FD->getFieldIndex());
7759 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7760 T: FD->getType()))
7761 return false;
7762 }
7763
7764 if (BasesLeft != 0 || NumVirtualBases != 0)
7765 EvalObj.startedDestroyingBases();
7766
7767 // Destroy base classes in reverse order.
7768 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->bases())) {
7769 if (Base.isVirtual())
7770 continue;
7771 --BasesLeft;
7772
7773 QualType BaseType = Base.getType();
7774 LValue Subobject = This;
7775 if (!HandleLValueDirectBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7776 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7777 return false;
7778
7779 APValue *SubobjectValue = &Value.getStructBase(i: BasesLeft);
7780 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7781 T: BaseType, /*IsCompleteClass=*/false))
7782 return false;
7783 }
7784 assert(BasesLeft == 0 && "NumBases was wrong?");
7785
7786 // Virtual bases.
7787 if (IsCompleteClass) {
7788 unsigned VirtualBasesLeft = NumVirtualBases;
7789 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->vbases())) {
7790 --VirtualBasesLeft;
7791
7792 QualType BaseType = Base.getType();
7793 LValue Subobject = This;
7794 if (!HandleLValueDirectVirtualBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7795 Base: BaseType->getAsCXXRecordDecl(),
7796 RL: &Layout))
7797 return false;
7798
7799 APValue *SubobjectValue = &Value.getStructVirtualBase(i: VirtualBasesLeft);
7800 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7801 T: BaseType, /*IsCompleteClass=*/false))
7802 return false;
7803 }
7804 assert(VirtualBasesLeft == 0 && "NumVirtualBases was wrong?");
7805 }
7806
7807 // The period of destruction ends now. The object is gone.
7808 Value = APValue();
7809 return true;
7810}
7811
7812namespace {
7813struct DestroyObjectHandler {
7814 EvalInfo &Info;
7815 const Expr *E;
7816 const LValue &This;
7817 const AccessKinds AccessKind;
7818
7819 typedef bool result_type;
7820 bool failed() { return false; }
7821 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
7822 return HandleDestructionImpl(Info, CallRange: E->getSourceRange(), This, Value&: Subobj,
7823 T: SubobjType);
7824 }
7825 bool found(APSInt &Value, QualType SubobjType) {
7826 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7827 return false;
7828 }
7829 bool found(APFloat &Value, QualType SubobjType) {
7830 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7831 return false;
7832 }
7833};
7834}
7835
7836/// Perform a destructor or pseudo-destructor call on the given object, which
7837/// might in general not be a complete object.
7838static bool HandleDestruction(EvalInfo &Info, const Expr *E,
7839 const LValue &This, QualType ThisType) {
7840 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Destroy, LVal: This, LValType: ThisType);
7841 DestroyObjectHandler Handler = {.Info: Info, .E: E, .This: This, .AccessKind: AK_Destroy};
7842 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
7843}
7844
7845/// Destroy and end the lifetime of the given complete object.
7846static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
7847 APValue::LValueBase LVBase, APValue &Value,
7848 QualType T) {
7849 // If we've had an unmodeled side-effect, we can't rely on mutable state
7850 // (such as the object we're about to destroy) being correct.
7851 if (Info.EvalStatus.HasSideEffects)
7852 return false;
7853
7854 LValue LV;
7855 LV.set(B: {LVBase});
7856 return HandleDestructionImpl(Info, CallRange: Loc, This: LV, Value, T);
7857}
7858
7859/// Perform a call to 'operator new' or to `__builtin_operator_new'.
7860static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E,
7861 LValue &Result) {
7862 if (Info.checkingPotentialConstantExpression() ||
7863 Info.SpeculativeEvaluationDepth)
7864 return false;
7865
7866 // This is permitted only within a call to std::allocator<T>::allocate.
7867 auto Caller = Info.getStdAllocatorCaller(FnName: "allocate");
7868 if (!Caller) {
7869 Info.FFDiag(Loc: E->getExprLoc(), DiagId: Info.getLangOpts().CPlusPlus20
7870 ? diag::note_constexpr_new_untyped
7871 : diag::note_constexpr_new);
7872 return false;
7873 }
7874
7875 QualType ElemType = Caller.ElemType;
7876 if (ElemType->isIncompleteType() || ElemType->isFunctionType()) {
7877 Info.FFDiag(Loc: E->getExprLoc(),
7878 DiagId: diag::note_constexpr_new_not_complete_object_type)
7879 << (ElemType->isIncompleteType() ? 0 : 1) << ElemType;
7880 return false;
7881 }
7882
7883 APSInt ByteSize;
7884 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: ByteSize, Info))
7885 return false;
7886 bool IsNothrow = false;
7887 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) {
7888 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7889 IsNothrow |= E->getType()->isNothrowT();
7890 }
7891
7892 CharUnits ElemSize;
7893 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElemType, Size&: ElemSize))
7894 return false;
7895 APInt Size, Remainder;
7896 APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity());
7897 APInt::udivrem(LHS: ByteSize, RHS: ElemSizeAP, Quotient&: Size, Remainder);
7898 if (Remainder != 0) {
7899 // This likely indicates a bug in the implementation of 'std::allocator'.
7900 Info.FFDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_operator_new_bad_size)
7901 << ByteSize << APSInt(ElemSizeAP, true) << ElemType;
7902 return false;
7903 }
7904
7905 if (!Info.CheckArraySize(Loc: E->getBeginLoc(), BitWidth: ByteSize.getActiveBits(),
7906 ElemCount: Size.getZExtValue(), /*Diag=*/!IsNothrow)) {
7907 if (IsNothrow) {
7908 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
7909 return true;
7910 }
7911 return false;
7912 }
7913
7914 QualType AllocType = Info.Ctx.getConstantArrayType(
7915 EltTy: ElemType, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
7916 APValue *Val = Info.createHeapAlloc(E: Caller.Call, T: AllocType, LV&: Result);
7917 *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue());
7918 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: AllocType));
7919 return true;
7920}
7921
7922static bool hasVirtualDestructor(QualType T) {
7923 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7924 if (CXXDestructorDecl *DD = RD->getDestructor())
7925 return DD->isVirtual();
7926 return false;
7927}
7928
7929static const FunctionDecl *getVirtualOperatorDelete(QualType T) {
7930 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7931 if (CXXDestructorDecl *DD = RD->getDestructor())
7932 return DD->isVirtual() ? DD->getOperatorDelete() : nullptr;
7933 return nullptr;
7934}
7935
7936/// Check that the given object is a suitable pointer to a heap allocation that
7937/// still exists and is of the right kind for the purpose of a deletion.
7938///
7939/// On success, returns the heap allocation to deallocate. On failure, produces
7940/// a diagnostic and returns std::nullopt.
7941static std::optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E,
7942 const LValue &Pointer,
7943 DynAllocKind DeallocKind) {
7944 auto PointerAsString = [&] {
7945 return Pointer.toString(Ctx&: Info.Ctx, T: Info.Ctx.VoidPtrTy);
7946 };
7947
7948 DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>();
7949 if (!DA) {
7950 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_not_heap_alloc)
7951 << PointerAsString();
7952 if (Pointer.Base)
7953 NoteLValueLocation(Info, Base: Pointer.Base);
7954 return std::nullopt;
7955 }
7956
7957 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
7958 if (!Alloc) {
7959 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
7960 return std::nullopt;
7961 }
7962
7963 if (DeallocKind != (*Alloc)->getKind()) {
7964 QualType AllocType = Pointer.Base.getDynamicAllocType();
7965 Info.FFDiag(E, DiagId: diag::note_constexpr_new_delete_mismatch)
7966 << DeallocKind << (*Alloc)->getKind() << AllocType;
7967 NoteLValueLocation(Info, Base: Pointer.Base);
7968 return std::nullopt;
7969 }
7970
7971 bool Subobject = false;
7972 if (DeallocKind == DynAllocKind::New) {
7973 Subobject = Pointer.Designator.MostDerivedPathLength != 0 ||
7974 Pointer.Designator.isOnePastTheEnd();
7975 } else {
7976 Subobject = Pointer.Designator.Entries.size() != 1 ||
7977 Pointer.Designator.Entries[0].getAsArrayIndex() != 0;
7978 }
7979 if (Subobject) {
7980 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_subobject)
7981 << PointerAsString() << Pointer.Designator.isOnePastTheEnd();
7982 return std::nullopt;
7983 }
7984
7985 return Alloc;
7986}
7987
7988// Perform a call to 'operator delete' or '__builtin_operator_delete'.
7989static bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) {
7990 if (Info.checkingPotentialConstantExpression() ||
7991 Info.SpeculativeEvaluationDepth)
7992 return false;
7993
7994 // This is permitted only within a call to std::allocator<T>::deallocate.
7995 if (!Info.getStdAllocatorCaller(FnName: "deallocate")) {
7996 Info.FFDiag(Loc: E->getExprLoc());
7997 return true;
7998 }
7999
8000 LValue Pointer;
8001 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Pointer, Info))
8002 return false;
8003 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I)
8004 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
8005
8006 if (Pointer.Designator.Invalid)
8007 return false;
8008
8009 // Deleting a null pointer would have no effect, but it's not permitted by
8010 // std::allocator<T>::deallocate's contract.
8011 if (Pointer.isNullPointer()) {
8012 Info.CCEDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_deallocate_null);
8013 return true;
8014 }
8015
8016 if (!CheckDeleteKind(Info, E, Pointer, DeallocKind: DynAllocKind::StdAllocator))
8017 return false;
8018
8019 Info.HeapAllocs.erase(x: Pointer.Base.get<DynamicAllocLValue>());
8020 return true;
8021}
8022
8023//===----------------------------------------------------------------------===//
8024// Generic Evaluation
8025//===----------------------------------------------------------------------===//
8026namespace {
8027
8028class BitCastBuffer {
8029 // FIXME: We're going to need bit-level granularity when we support
8030 // bit-fields.
8031 // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but
8032 // we don't support a host or target where that is the case. Still, we should
8033 // use a more generic type in case we ever do.
8034 SmallVector<std::optional<unsigned char>, 32> Bytes;
8035
8036 static_assert(std::numeric_limits<unsigned char>::digits >= 8,
8037 "Need at least 8 bit unsigned char");
8038
8039 bool TargetIsLittleEndian;
8040
8041public:
8042 BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian)
8043 : Bytes(Width.getQuantity()),
8044 TargetIsLittleEndian(TargetIsLittleEndian) {}
8045
8046 [[nodiscard]] bool readObject(CharUnits Offset, CharUnits Width,
8047 SmallVectorImpl<unsigned char> &Output) const {
8048 for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) {
8049 // If a byte of an integer is uninitialized, then the whole integer is
8050 // uninitialized.
8051 if (!Bytes[I.getQuantity()])
8052 return false;
8053 Output.push_back(Elt: *Bytes[I.getQuantity()]);
8054 }
8055 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
8056 std::reverse(first: Output.begin(), last: Output.end());
8057 return true;
8058 }
8059
8060 void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) {
8061 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
8062 std::reverse(first: Input.begin(), last: Input.end());
8063
8064 size_t Index = 0;
8065 for (unsigned char Byte : Input) {
8066 assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?");
8067 Bytes[Offset.getQuantity() + Index] = Byte;
8068 ++Index;
8069 }
8070 }
8071
8072 size_t size() { return Bytes.size(); }
8073};
8074
8075/// Traverse an APValue to produce an BitCastBuffer, emulating how the current
8076/// target would represent the value at runtime.
8077class APValueToBufferConverter {
8078 EvalInfo &Info;
8079 BitCastBuffer Buffer;
8080 const CastExpr *BCE;
8081
8082 APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth,
8083 const CastExpr *BCE)
8084 : Info(Info),
8085 Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()),
8086 BCE(BCE) {}
8087
8088 bool visit(const APValue &Val, QualType Ty) {
8089 return visit(Val, Ty, Offset: CharUnits::fromQuantity(Quantity: 0));
8090 }
8091
8092 // Write out Val with type Ty into Buffer starting at Offset.
8093 bool visit(const APValue &Val, QualType Ty, CharUnits Offset) {
8094 assert((size_t)Offset.getQuantity() <= Buffer.size());
8095
8096 // As a special case, nullptr_t has an indeterminate value.
8097 if (Ty->isNullPtrType())
8098 return true;
8099
8100 // Dig through Src to find the byte at SrcOffset.
8101 switch (Val.getKind()) {
8102 case APValue::Indeterminate:
8103 case APValue::None:
8104 return true;
8105
8106 case APValue::Int:
8107 return visitInt(Val: Val.getInt(), Ty, Offset);
8108 case APValue::Float:
8109 return visitFloat(Val: Val.getFloat(), Ty, Offset);
8110 case APValue::Array:
8111 return visitArray(Val, Ty, Offset);
8112 case APValue::Struct:
8113 return visitRecord(Val, Ty, Offset);
8114 case APValue::Vector:
8115 return visitVector(Val, Ty, Offset);
8116
8117 case APValue::ComplexInt:
8118 case APValue::ComplexFloat:
8119 return visitComplex(Val, Ty, Offset);
8120 case APValue::FixedPoint:
8121 // FIXME: We should support these.
8122
8123 case APValue::LValue:
8124 case APValue::Matrix:
8125 case APValue::Union:
8126 case APValue::MemberPointer:
8127 case APValue::AddrLabelDiff:
8128 case APValue::Reflection: {
8129 Info.FFDiag(Loc: BCE->getBeginLoc(),
8130 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8131 << Ty;
8132 return false;
8133 }
8134 }
8135 llvm_unreachable("Unhandled APValue::ValueKind");
8136 }
8137
8138 bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) {
8139 const RecordDecl *RD = Ty->getAsRecordDecl();
8140 if (!ASTContext::hasLayout(D: RD))
8141 return false;
8142 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8143
8144 // Visit the base classes.
8145 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8146 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8147 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8148 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8149 const APValue &Base = Val.getStructBase(i: I);
8150
8151 // Can happen in error cases.
8152 if (!Base.isStruct())
8153 return false;
8154
8155 if (!visitRecord(Val: Base, Ty: BS.getType(),
8156 Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset))
8157 return false;
8158 }
8159 }
8160
8161 // Visit the fields.
8162 unsigned FieldIdx = 0;
8163 for (FieldDecl *FD : RD->fields()) {
8164 if (FD->isBitField()) {
8165 Info.FFDiag(Loc: BCE->getBeginLoc(),
8166 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8167 return false;
8168 }
8169
8170 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8171
8172 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 &&
8173 "only bit-fields can have sub-char alignment");
8174 CharUnits FieldOffset =
8175 Info.Ctx.toCharUnitsFromBits(BitSize: FieldOffsetBits) + Offset;
8176 QualType FieldTy = FD->getType();
8177 if (!visit(Val: Val.getStructField(i: FieldIdx), Ty: FieldTy, Offset: FieldOffset))
8178 return false;
8179 ++FieldIdx;
8180 }
8181
8182 return true;
8183 }
8184
8185 bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) {
8186 const auto *CAT =
8187 dyn_cast_or_null<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe());
8188 if (!CAT)
8189 return false;
8190
8191 CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(T: CAT->getElementType());
8192 unsigned NumInitializedElts = Val.getArrayInitializedElts();
8193 unsigned ArraySize = Val.getArraySize();
8194 // First, initialize the initialized elements.
8195 for (unsigned I = 0; I != NumInitializedElts; ++I) {
8196 const APValue &SubObj = Val.getArrayInitializedElt(I);
8197 if (!visit(Val: SubObj, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
8198 return false;
8199 }
8200
8201 // Next, initialize the rest of the array using the filler.
8202 if (Val.hasArrayFiller()) {
8203 const APValue &Filler = Val.getArrayFiller();
8204 for (unsigned I = NumInitializedElts; I != ArraySize; ++I) {
8205 if (!visit(Val: Filler, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
8206 return false;
8207 }
8208 }
8209
8210 return true;
8211 }
8212
8213 bool visitComplex(const APValue &Val, QualType Ty, CharUnits Offset) {
8214 const ComplexType *ComplexTy = Ty->castAs<ComplexType>();
8215 QualType EltTy = ComplexTy->getElementType();
8216 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8217 bool IsInt = Val.isComplexInt();
8218
8219 if (IsInt) {
8220 if (!visitInt(Val: Val.getComplexIntReal(), Ty: EltTy,
8221 Offset: Offset + (0 * EltSizeChars)))
8222 return false;
8223 if (!visitInt(Val: Val.getComplexIntImag(), Ty: EltTy,
8224 Offset: Offset + (1 * EltSizeChars)))
8225 return false;
8226 } else {
8227 if (!visitFloat(Val: Val.getComplexFloatReal(), Ty: EltTy,
8228 Offset: Offset + (0 * EltSizeChars)))
8229 return false;
8230 if (!visitFloat(Val: Val.getComplexFloatImag(), Ty: EltTy,
8231 Offset: Offset + (1 * EltSizeChars)))
8232 return false;
8233 }
8234
8235 return true;
8236 }
8237
8238 bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) {
8239 const VectorType *VTy = Ty->castAs<VectorType>();
8240 QualType EltTy = VTy->getElementType();
8241 unsigned NElts = VTy->getNumElements();
8242
8243 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8244 // Special handling for OpenCL bool vectors:
8245 // Since these vectors are stored as packed bits, but we can't write
8246 // individual bits to the BitCastBuffer, we'll buffer all of the elements
8247 // together into an appropriately sized APInt and write them all out at
8248 // once. Because we don't accept vectors where NElts * EltSize isn't a
8249 // multiple of the char size, there will be no padding space, so we don't
8250 // have to worry about writing data which should have been left
8251 // uninitialized.
8252 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8253
8254 llvm::APInt Res = llvm::APInt::getZero(numBits: NElts);
8255 for (unsigned I = 0; I < NElts; ++I) {
8256 const llvm::APSInt &EltAsInt = Val.getVectorElt(I).getInt();
8257 assert(EltAsInt.isUnsigned() && EltAsInt.getBitWidth() == 1 &&
8258 "bool vector element must be 1-bit unsigned integer!");
8259
8260 Res.insertBits(SubBits: EltAsInt, bitPosition: BigEndian ? (NElts - I - 1) : I);
8261 }
8262
8263 SmallVector<uint8_t, 8> Bytes(NElts / 8);
8264 llvm::StoreIntToMemory(IntVal: Res, Dst: &*Bytes.begin(), StoreBytes: NElts / 8);
8265 Buffer.writeObject(Offset, Input&: Bytes);
8266 } else {
8267 // Iterate over each of the elements and write them out to the buffer at
8268 // the appropriate offset.
8269 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8270 for (unsigned I = 0; I < NElts; ++I) {
8271 if (!visit(Val: Val.getVectorElt(I), Ty: EltTy, Offset: Offset + I * EltSizeChars))
8272 return false;
8273 }
8274 }
8275
8276 return true;
8277 }
8278
8279 bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) {
8280 APSInt AdjustedVal = Val;
8281 unsigned Width = AdjustedVal.getBitWidth();
8282 if (Ty->isBooleanType()) {
8283 Width = Info.Ctx.getTypeSize(T: Ty);
8284 AdjustedVal = AdjustedVal.extend(width: Width);
8285 }
8286
8287 SmallVector<uint8_t, 8> Bytes(Width / 8);
8288 llvm::StoreIntToMemory(IntVal: AdjustedVal, Dst: &*Bytes.begin(), StoreBytes: Width / 8);
8289 Buffer.writeObject(Offset, Input&: Bytes);
8290 return true;
8291 }
8292
8293 bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) {
8294 APSInt AsInt(Val.bitcastToAPInt());
8295 return visitInt(Val: AsInt, Ty, Offset);
8296 }
8297
8298public:
8299 static std::optional<BitCastBuffer>
8300 convert(EvalInfo &Info, const APValue &Src, const CastExpr *BCE) {
8301 CharUnits DstSize = Info.Ctx.getTypeSizeInChars(T: BCE->getType());
8302 APValueToBufferConverter Converter(Info, DstSize, BCE);
8303 if (!Converter.visit(Val: Src, Ty: BCE->getSubExpr()->getType()))
8304 return std::nullopt;
8305 return Converter.Buffer;
8306 }
8307};
8308
8309/// Write an BitCastBuffer into an APValue.
8310class BufferToAPValueConverter {
8311 EvalInfo &Info;
8312 const BitCastBuffer &Buffer;
8313 const CastExpr *BCE;
8314
8315 BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer,
8316 const CastExpr *BCE)
8317 : Info(Info), Buffer(Buffer), BCE(BCE) {}
8318
8319 // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast
8320 // with an invalid type, so anything left is a deficiency on our part (FIXME).
8321 // Ideally this will be unreachable.
8322 std::nullopt_t unsupportedType(QualType Ty) {
8323 Info.FFDiag(Loc: BCE->getBeginLoc(),
8324 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8325 << Ty;
8326 return std::nullopt;
8327 }
8328
8329 std::nullopt_t unrepresentableValue(QualType Ty, const APSInt &Val) {
8330 Info.FFDiag(Loc: BCE->getBeginLoc(),
8331 DiagId: diag::note_constexpr_bit_cast_unrepresentable_value)
8332 << Ty << toString(I: Val, /*Radix=*/10);
8333 return std::nullopt;
8334 }
8335
8336 std::optional<APValue> visit(const BuiltinType *T, CharUnits Offset,
8337 const EnumType *EnumSugar = nullptr) {
8338 if (T->isNullPtrType()) {
8339 uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QT: QualType(T, 0));
8340 return APValue((Expr *)nullptr,
8341 /*Offset=*/CharUnits::fromQuantity(Quantity: NullValue),
8342 APValue::NoLValuePath{}, /*IsNullPtr=*/true);
8343 }
8344
8345 CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T);
8346
8347 // Work around floating point types that contain unused padding bytes. This
8348 // is really just `long double` on x86, which is the only fundamental type
8349 // with padding bytes.
8350 if (T->isRealFloatingType()) {
8351 const llvm::fltSemantics &Semantics =
8352 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8353 unsigned NumBits = llvm::APFloatBase::getSizeInBits(Sem: Semantics);
8354 assert(NumBits % 8 == 0);
8355 CharUnits NumBytes = CharUnits::fromQuantity(Quantity: NumBits / 8);
8356 if (NumBytes != SizeOf)
8357 SizeOf = NumBytes;
8358 }
8359
8360 SmallVector<uint8_t, 8> Bytes;
8361 if (!Buffer.readObject(Offset, Width: SizeOf, Output&: Bytes)) {
8362 // If this is std::byte or unsigned char, then its okay to store an
8363 // indeterminate value.
8364 bool IsStdByte = EnumSugar && EnumSugar->isStdByteType();
8365 bool IsUChar =
8366 !EnumSugar && (T->isSpecificBuiltinType(K: BuiltinType::UChar) ||
8367 T->isSpecificBuiltinType(K: BuiltinType::Char_U));
8368 if (!IsStdByte && !IsUChar) {
8369 QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0);
8370 Info.FFDiag(Loc: BCE->getExprLoc(),
8371 DiagId: diag::note_constexpr_bit_cast_indet_dest)
8372 << DisplayType << Info.Ctx.getLangOpts().CharIsSigned;
8373 return std::nullopt;
8374 }
8375
8376 return APValue::IndeterminateValue();
8377 }
8378
8379 APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true);
8380 llvm::LoadIntFromMemory(IntVal&: Val, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8381
8382 if (T->isIntegralOrEnumerationType()) {
8383 Val.setIsSigned(T->isSignedIntegerOrEnumerationType());
8384
8385 unsigned IntWidth = Info.Ctx.getIntWidth(T: QualType(T, 0));
8386 if (IntWidth != Val.getBitWidth()) {
8387 APSInt Truncated = Val.trunc(width: IntWidth);
8388 if (Truncated.extend(width: Val.getBitWidth()) != Val)
8389 return unrepresentableValue(Ty: QualType(T, 0), Val);
8390 Val = Truncated;
8391 }
8392
8393 return APValue(Val);
8394 }
8395
8396 if (T->isRealFloatingType()) {
8397 const llvm::fltSemantics &Semantics =
8398 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8399 return APValue(APFloat(Semantics, Val));
8400 }
8401
8402 return unsupportedType(Ty: QualType(T, 0));
8403 }
8404
8405 std::optional<APValue> visit(const RecordType *RTy, CharUnits Offset) {
8406 const RecordDecl *RD = RTy->getAsRecordDecl();
8407 if (RD->isInvalidDecl())
8408 return std::nullopt;
8409 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8410
8411 unsigned NumBases = 0;
8412 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
8413 NumBases = CXXRD->getNumBases();
8414
8415 APValue ResultVal(APValue::UninitStruct(), NumBases, RD->getNumFields());
8416
8417 // Visit the base classes.
8418 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8419 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8420 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8421 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8422
8423 std::optional<APValue> SubObj = visitType(
8424 Ty: BS.getType(), Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset);
8425 if (!SubObj)
8426 return std::nullopt;
8427 ResultVal.getStructBase(i: I) = *SubObj;
8428 }
8429 }
8430
8431 // Visit the fields.
8432 unsigned FieldIdx = 0;
8433 for (FieldDecl *FD : RD->fields()) {
8434 // FIXME: We don't currently support bit-fields. A lot of the logic for
8435 // this is in CodeGen, so we need to factor it around.
8436 if (FD->isBitField()) {
8437 Info.FFDiag(Loc: BCE->getBeginLoc(),
8438 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8439 return std::nullopt;
8440 }
8441
8442 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8443 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0);
8444
8445 CharUnits FieldOffset =
8446 CharUnits::fromQuantity(Quantity: FieldOffsetBits / Info.Ctx.getCharWidth()) +
8447 Offset;
8448 QualType FieldTy = FD->getType();
8449 std::optional<APValue> SubObj = visitType(Ty: FieldTy, Offset: FieldOffset);
8450 if (!SubObj)
8451 return std::nullopt;
8452 ResultVal.getStructField(i: FieldIdx) = *SubObj;
8453 ++FieldIdx;
8454 }
8455
8456 return ResultVal;
8457 }
8458
8459 std::optional<APValue> visit(const EnumType *Ty, CharUnits Offset) {
8460 QualType RepresentationType =
8461 Ty->getDecl()->getDefinitionOrSelf()->getIntegerType();
8462 assert(!RepresentationType.isNull() &&
8463 "enum forward decl should be caught by Sema");
8464 const auto *AsBuiltin =
8465 RepresentationType.getCanonicalType()->castAs<BuiltinType>();
8466 // Recurse into the underlying type. Treat std::byte transparently as
8467 // unsigned char.
8468 return visit(T: AsBuiltin, Offset, /*EnumTy=*/EnumSugar: Ty);
8469 }
8470
8471 std::optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) {
8472 size_t Size = Ty->getLimitedSize();
8473 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: Ty->getElementType());
8474
8475 APValue ArrayValue(APValue::UninitArray(), Size, Size);
8476 for (size_t I = 0; I != Size; ++I) {
8477 std::optional<APValue> ElementValue =
8478 visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8479 if (!ElementValue)
8480 return std::nullopt;
8481 ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue);
8482 }
8483
8484 return ArrayValue;
8485 }
8486
8487 std::optional<APValue> visit(const ComplexType *Ty, CharUnits Offset) {
8488 QualType ElementType = Ty->getElementType();
8489 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: ElementType);
8490 bool IsInt = ElementType->isIntegerType();
8491
8492 std::optional<APValue> Values[2];
8493 for (unsigned I = 0; I != 2; ++I) {
8494 Values[I] = visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8495 if (!Values[I])
8496 return std::nullopt;
8497 }
8498
8499 if (IsInt)
8500 return APValue(Values[0]->getInt(), Values[1]->getInt());
8501 return APValue(Values[0]->getFloat(), Values[1]->getFloat());
8502 }
8503
8504 std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) {
8505 QualType EltTy = VTy->getElementType();
8506 unsigned NElts = VTy->getNumElements();
8507 unsigned EltSize =
8508 VTy->isPackedVectorBoolType(ctx: Info.Ctx) ? 1 : Info.Ctx.getTypeSize(T: EltTy);
8509
8510 SmallVector<APValue, 4> Elts;
8511 Elts.reserve(N: NElts);
8512 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8513 // Special handling for OpenCL bool vectors:
8514 // Since these vectors are stored as packed bits, but we can't read
8515 // individual bits from the BitCastBuffer, we'll buffer all of the
8516 // elements together into an appropriately sized APInt and write them all
8517 // out at once. Because we don't accept vectors where NElts * EltSize
8518 // isn't a multiple of the char size, there will be no padding space, so
8519 // we don't have to worry about reading any padding data which didn't
8520 // actually need to be accessed.
8521 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8522
8523 SmallVector<uint8_t, 8> Bytes;
8524 Bytes.reserve(N: NElts / 8);
8525 if (!Buffer.readObject(Offset, Width: CharUnits::fromQuantity(Quantity: NElts / 8), Output&: Bytes))
8526 return std::nullopt;
8527
8528 APSInt SValInt(NElts, true);
8529 llvm::LoadIntFromMemory(IntVal&: SValInt, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8530
8531 for (unsigned I = 0; I < NElts; ++I) {
8532 llvm::APInt Elt =
8533 SValInt.extractBits(numBits: 1, bitPosition: (BigEndian ? NElts - I - 1 : I) * EltSize);
8534 Elts.emplace_back(
8535 Args: APSInt(std::move(Elt), !EltTy->isSignedIntegerType()));
8536 }
8537 } else {
8538 // Iterate over each of the elements and read them from the buffer at
8539 // the appropriate offset.
8540 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8541 for (unsigned I = 0; I < NElts; ++I) {
8542 std::optional<APValue> EltValue =
8543 visitType(Ty: EltTy, Offset: Offset + I * EltSizeChars);
8544 if (!EltValue)
8545 return std::nullopt;
8546 Elts.push_back(Elt: std::move(*EltValue));
8547 }
8548 }
8549
8550 return APValue(Elts.data(), Elts.size());
8551 }
8552
8553 std::optional<APValue> visit(const Type *Ty, CharUnits Offset) {
8554 return unsupportedType(Ty: QualType(Ty, 0));
8555 }
8556
8557 std::optional<APValue> visitType(QualType Ty, CharUnits Offset) {
8558 QualType Can = Ty.getCanonicalType();
8559
8560 switch (Can->getTypeClass()) {
8561#define TYPE(Class, Base) \
8562 case Type::Class: \
8563 return visit(cast<Class##Type>(Can.getTypePtr()), Offset);
8564#define ABSTRACT_TYPE(Class, Base)
8565#define NON_CANONICAL_TYPE(Class, Base) \
8566 case Type::Class: \
8567 llvm_unreachable("non-canonical type should be impossible!");
8568#define DEPENDENT_TYPE(Class, Base) \
8569 case Type::Class: \
8570 llvm_unreachable( \
8571 "dependent types aren't supported in the constant evaluator!");
8572#define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \
8573 case Type::Class: \
8574 llvm_unreachable("either dependent or not canonical!");
8575#include "clang/AST/TypeNodes.inc"
8576 }
8577 llvm_unreachable("Unhandled Type::TypeClass");
8578 }
8579
8580public:
8581 // Pull out a full value of type DstType.
8582 static std::optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer,
8583 const CastExpr *BCE) {
8584 BufferToAPValueConverter Converter(Info, Buffer, BCE);
8585 return Converter.visitType(Ty: BCE->getType(), Offset: CharUnits::fromQuantity(Quantity: 0));
8586 }
8587};
8588
8589static bool checkBitCastConstexprEligibilityType(SourceLocation Loc,
8590 QualType Ty, EvalInfo *Info,
8591 const ASTContext &Ctx,
8592 bool CheckingDest) {
8593 Ty = Ty.getCanonicalType();
8594
8595 auto diag = [&](int Reason) {
8596 if (Info)
8597 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_type)
8598 << CheckingDest << (Reason == 4) << Reason;
8599 return false;
8600 };
8601 auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) {
8602 if (Info)
8603 Info->Note(Loc: NoteLoc, DiagId: diag::note_constexpr_bit_cast_invalid_subtype)
8604 << NoteTy << Construct << Ty;
8605 return false;
8606 };
8607
8608 if (Ty->isUnionType())
8609 return diag(0);
8610 if (Ty->isPointerType())
8611 return diag(1);
8612 if (Ty->isMemberPointerType())
8613 return diag(2);
8614 if (Ty.isVolatileQualified())
8615 return diag(3);
8616
8617 if (RecordDecl *Record = Ty->getAsRecordDecl()) {
8618 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: Record)) {
8619 for (CXXBaseSpecifier &BS : CXXRD->bases())
8620 if (!checkBitCastConstexprEligibilityType(Loc, Ty: BS.getType(), Info, Ctx,
8621 CheckingDest))
8622 return note(1, BS.getType(), BS.getBeginLoc());
8623 }
8624 for (FieldDecl *FD : Record->fields()) {
8625 if (FD->getType()->isReferenceType())
8626 return diag(4);
8627 if (!checkBitCastConstexprEligibilityType(Loc, Ty: FD->getType(), Info, Ctx,
8628 CheckingDest))
8629 return note(0, FD->getType(), FD->getBeginLoc());
8630 }
8631 }
8632
8633 if (Ty->isArrayType() &&
8634 !checkBitCastConstexprEligibilityType(Loc, Ty: Ctx.getBaseElementType(QT: Ty),
8635 Info, Ctx, CheckingDest))
8636 return false;
8637
8638 if (const auto *VTy = Ty->getAs<VectorType>()) {
8639 QualType EltTy = VTy->getElementType();
8640 unsigned NElts = VTy->getNumElements();
8641 unsigned EltSize =
8642 VTy->isPackedVectorBoolType(ctx: Ctx) ? 1 : Ctx.getTypeSize(T: EltTy);
8643
8644 if ((NElts * EltSize) % Ctx.getCharWidth() != 0) {
8645 // The vector's size in bits is not a multiple of the target's byte size,
8646 // so its layout is unspecified. For now, we'll simply treat these cases
8647 // as unsupported (this should only be possible with OpenCL bool vectors
8648 // whose element count isn't a multiple of the byte size).
8649 if (Info)
8650 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_vector)
8651 << QualType(VTy, 0) << EltSize << NElts << Ctx.getCharWidth();
8652 return false;
8653 }
8654
8655 if (EltTy->isRealFloatingType() &&
8656 &Ctx.getFloatTypeSemantics(T: EltTy) == &APFloat::x87DoubleExtended()) {
8657 // The layout for x86_fp80 vectors seems to be handled very inconsistently
8658 // by both clang and LLVM, so for now we won't allow bit_casts involving
8659 // it in a constexpr context.
8660 if (Info)
8661 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8662 << EltTy;
8663 return false;
8664 }
8665 }
8666
8667 return true;
8668}
8669
8670static bool checkBitCastConstexprEligibility(EvalInfo *Info,
8671 const ASTContext &Ctx,
8672 const CastExpr *BCE) {
8673 bool DestOK = checkBitCastConstexprEligibilityType(
8674 Loc: BCE->getBeginLoc(), Ty: BCE->getType(), Info, Ctx, CheckingDest: true);
8675 bool SourceOK = DestOK && checkBitCastConstexprEligibilityType(
8676 Loc: BCE->getBeginLoc(),
8677 Ty: BCE->getSubExpr()->getType(), Info, Ctx, CheckingDest: false);
8678 return SourceOK;
8679}
8680
8681static bool handleRValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8682 const APValue &SourceRValue,
8683 const CastExpr *BCE) {
8684 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8685 "no host or target supports non 8-bit chars");
8686
8687 if (!checkBitCastConstexprEligibility(Info: &Info, Ctx: Info.Ctx, BCE))
8688 return false;
8689
8690 // Read out SourceValue into a char buffer.
8691 std::optional<BitCastBuffer> Buffer =
8692 APValueToBufferConverter::convert(Info, Src: SourceRValue, BCE);
8693 if (!Buffer)
8694 return false;
8695
8696 // Write out the buffer into a new APValue.
8697 std::optional<APValue> MaybeDestValue =
8698 BufferToAPValueConverter::convert(Info, Buffer&: *Buffer, BCE);
8699 if (!MaybeDestValue)
8700 return false;
8701
8702 DestValue = std::move(*MaybeDestValue);
8703 return true;
8704}
8705
8706static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8707 APValue &SourceValue,
8708 const CastExpr *BCE) {
8709 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8710 "no host or target supports non 8-bit chars");
8711 assert(SourceValue.isLValue() &&
8712 "LValueToRValueBitcast requires an lvalue operand!");
8713
8714 LValue SourceLValue;
8715 APValue SourceRValue;
8716 SourceLValue.setFrom(Ctx: Info.Ctx, V: SourceValue);
8717 if (!handleLValueToRValueConversion(
8718 Info, Conv: BCE, Type: BCE->getSubExpr()->getType().withConst(), LVal: SourceLValue,
8719 RVal&: SourceRValue, /*WantObjectRepresentation=*/true))
8720 return false;
8721
8722 return handleRValueToRValueBitCast(Info, DestValue, SourceRValue, BCE);
8723}
8724
8725/// Diagnose a relocated BPF enumerator, i.e., of an enum with the
8726/// preserve_access_index attribute, which is not a constant expression. Return
8727/// true if diagnosed.
8728static bool diagnoseBPFPreserveAIEnumerator(EvalInfo &Info,
8729 const PseudoObjectExpr *E) {
8730 const auto *DRE = dyn_cast<DeclRefExpr>(Val: E->getSyntacticForm());
8731 if (!DRE)
8732 return false;
8733 const auto *ECD = dyn_cast<EnumConstantDecl>(Val: DRE->getDecl());
8734 if (!ECD)
8735 return false;
8736 const auto *ED = dyn_cast<EnumDecl>(Val: ECD->getDeclContext());
8737 if (!ED || !ED->hasAttr<BPFPreserveAccessIndexAttr>())
8738 return false;
8739
8740 // An anonymous enum is only relocated through its typedef name.
8741 QualType EnumTy;
8742 if (ED->getDeclName())
8743 EnumTy = Info.Ctx.getCanonicalTagType(TD: ED);
8744 else if (const TypedefNameDecl *TD = ED->getTypedefNameForAnonDecl())
8745 EnumTy = Info.Ctx.getTypeDeclType(Decl: TD);
8746 else
8747 return false;
8748
8749 Info.FFDiag(E, DiagId: diag::note_constexpr_bpf_preserve_ai_enumerator)
8750 << ECD << EnumTy << E->getSourceRange();
8751 return true;
8752}
8753
8754template <class Derived>
8755class ExprEvaluatorBase
8756 : public ConstStmtVisitor<Derived, bool> {
8757private:
8758 Derived &getDerived() { return static_cast<Derived&>(*this); }
8759 bool DerivedSuccess(const APValue &V, const Expr *E) {
8760 return getDerived().Success(V, E);
8761 }
8762 bool DerivedZeroInitialization(const Expr *E) {
8763 return getDerived().ZeroInitialization(E);
8764 }
8765
8766 // Check whether a conditional operator with a non-constant condition is a
8767 // potential constant expression. If neither arm is a potential constant
8768 // expression, then the conditional operator is not either.
8769 template<typename ConditionalOperator>
8770 void CheckPotentialConstantConditional(const ConditionalOperator *E) {
8771 assert(Info.checkingPotentialConstantExpression());
8772
8773 // Speculatively evaluate both arms.
8774 SmallVector<PartialDiagnosticAt, 8> Diag;
8775 {
8776 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8777 StmtVisitorTy::Visit(E->getFalseExpr());
8778 if (Diag.empty())
8779 return;
8780 }
8781
8782 {
8783 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8784 Diag.clear();
8785 Info.EvalStatus.DiagEmitted = false;
8786 StmtVisitorTy::Visit(E->getTrueExpr());
8787 if (Diag.empty())
8788 return;
8789 }
8790
8791 Error(E, diag::note_constexpr_conditional_never_const);
8792 }
8793
8794
8795 template<typename ConditionalOperator>
8796 bool HandleConditionalOperator(const ConditionalOperator *E) {
8797 bool BoolResult;
8798 if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
8799 if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) {
8800 CheckPotentialConstantConditional(E);
8801 return false;
8802 }
8803 if (Info.noteFailure()) {
8804 StmtVisitorTy::Visit(E->getTrueExpr());
8805 StmtVisitorTy::Visit(E->getFalseExpr());
8806 }
8807 return false;
8808 }
8809
8810 Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
8811 return StmtVisitorTy::Visit(EvalExpr);
8812 }
8813
8814protected:
8815 EvalInfo &Info;
8816 typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy;
8817 typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
8818
8819 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
8820 return Info.CCEDiag(E, DiagId: D);
8821 }
8822
8823 bool ZeroInitialization(const Expr *E) { return Error(E); }
8824
8825 bool IsConstantEvaluatedBuiltinCall(const CallExpr *E) {
8826 unsigned BuiltinOp = E->getBuiltinCallee();
8827 return BuiltinOp != 0 &&
8828 Info.Ctx.BuiltinInfo.isConstantEvaluated(ID: BuiltinOp);
8829 }
8830
8831public:
8832 ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
8833
8834 EvalInfo &getEvalInfo() { return Info; }
8835
8836 /// Report an evaluation error. This should only be called when an error is
8837 /// first discovered. When propagating an error, just return false.
8838 bool Error(const Expr *E, diag::kind D) {
8839 Info.FFDiag(E, DiagId: D) << E->getSourceRange();
8840 return false;
8841 }
8842 bool Error(const Expr *E) {
8843 return Error(E, diag::note_invalid_subexpr_in_const_expr);
8844 }
8845
8846 bool VisitStmt(const Stmt *) {
8847 llvm_unreachable("Expression evaluator should not be called on stmts");
8848 }
8849 bool VisitExpr(const Expr *E) {
8850 return Error(E);
8851 }
8852
8853 bool VisitEmbedExpr(const EmbedExpr *E) {
8854 const auto It = E->begin();
8855 return StmtVisitorTy::Visit(*It);
8856 }
8857
8858 bool VisitPredefinedExpr(const PredefinedExpr *E) {
8859 return StmtVisitorTy::Visit(E->getFunctionName());
8860 }
8861 bool VisitConstantExpr(const ConstantExpr *E) {
8862 if (E->hasAPValueResult())
8863 return DerivedSuccess(V: E->getAPValueResult(), E);
8864
8865 return StmtVisitorTy::Visit(E->getSubExpr());
8866 }
8867
8868 bool VisitParenExpr(const ParenExpr *E)
8869 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8870 bool VisitUnaryExtension(const UnaryOperator *E)
8871 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8872 bool VisitUnaryPlus(const UnaryOperator *E)
8873 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8874 bool VisitChooseExpr(const ChooseExpr *E)
8875 { return StmtVisitorTy::Visit(E->getChosenSubExpr()); }
8876 bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
8877 { return StmtVisitorTy::Visit(E->getResultExpr()); }
8878 bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
8879 { return StmtVisitorTy::Visit(E->getReplacement()); }
8880 bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
8881 TempVersionRAII RAII(*Info.CurrentCall);
8882 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8883 return StmtVisitorTy::Visit(E->getExpr());
8884 }
8885 bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
8886 TempVersionRAII RAII(*Info.CurrentCall);
8887 // The initializer may not have been parsed yet, or might be erroneous.
8888 if (!E->getExpr())
8889 return Error(E);
8890 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8891 return StmtVisitorTy::Visit(E->getExpr());
8892 }
8893
8894 bool VisitExprWithCleanups(const ExprWithCleanups *E) {
8895 FullExpressionRAII Scope(Info);
8896 return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy();
8897 }
8898
8899 // Temporaries are registered when created, so we don't care about
8900 // CXXBindTemporaryExpr.
8901 bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) {
8902 return StmtVisitorTy::Visit(E->getSubExpr());
8903 }
8904
8905 bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
8906 if (E->getCastKind() != CK_PointerToIntegral)
8907 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8908 << diag::ConstexprInvalidCastKind::Reinterpret;
8909 return static_cast<Derived*>(this)->VisitCastExpr(E);
8910 }
8911 bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
8912 if (!Info.Ctx.getLangOpts().CPlusPlus20)
8913 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8914 << diag::ConstexprInvalidCastKind::Dynamic;
8915 return static_cast<Derived*>(this)->VisitCastExpr(E);
8916 }
8917 bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
8918 return static_cast<Derived*>(this)->VisitCastExpr(E);
8919 }
8920
8921 bool VisitBinaryOperator(const BinaryOperator *E) {
8922 switch (E->getOpcode()) {
8923 default:
8924 return Error(E);
8925
8926 case BO_Comma:
8927 VisitIgnoredValue(E: E->getLHS());
8928 return StmtVisitorTy::Visit(E->getRHS());
8929
8930 case BO_PtrMemD:
8931 case BO_PtrMemI: {
8932 LValue Obj;
8933 if (!HandleMemberPointerAccess(Info, BO: E, LV&: Obj))
8934 return false;
8935 APValue Result;
8936 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: Obj, RVal&: Result))
8937 return false;
8938 return DerivedSuccess(V: Result, E);
8939 }
8940 }
8941 }
8942
8943 bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) {
8944 return StmtVisitorTy::Visit(E->getSemanticForm());
8945 }
8946
8947 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
8948 // Evaluate and cache the common expression. We treat it as a temporary,
8949 // even though it's not quite the same thing.
8950 LValue CommonLV;
8951 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8952 Key: E->getOpaqueValue(),
8953 T: getStorageType(Ctx: Info.Ctx, E: E->getOpaqueValue()),
8954 Scope: ScopeKind::FullExpression, LV&: CommonLV),
8955 Info, E: E->getCommon()))
8956 return false;
8957
8958 return HandleConditionalOperator(E);
8959 }
8960
8961 bool VisitConditionalOperator(const ConditionalOperator *E) {
8962 bool IsBcpCall = false;
8963 // If the condition (ignoring parens) is a __builtin_constant_p call,
8964 // the result is a constant expression if it can be folded without
8965 // side-effects. This is an important GNU extension. See GCC PR38377
8966 // for discussion.
8967 if (const CallExpr *CallCE =
8968 dyn_cast<CallExpr>(Val: E->getCond()->IgnoreParenCasts()))
8969 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
8970 IsBcpCall = true;
8971
8972 // Always assume __builtin_constant_p(...) ? ... : ... is a potential
8973 // constant expression; we can't check whether it's potentially foldable.
8974 // FIXME: We should instead treat __builtin_constant_p as non-constant if
8975 // it would return 'false' in this mode.
8976 if (Info.checkingPotentialConstantExpression() && IsBcpCall)
8977 return false;
8978
8979 FoldConstant Fold(Info, IsBcpCall);
8980 if (!HandleConditionalOperator(E)) {
8981 Fold.keepDiagnostics();
8982 return false;
8983 }
8984
8985 return true;
8986 }
8987
8988 bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
8989 if (APValue *Value = Info.CurrentCall->getCurrentTemporary(Key: E);
8990 Value && !Value->isAbsent())
8991 return DerivedSuccess(V: *Value, E);
8992
8993 const Expr *Source = E->getSourceExpr();
8994 if (!Source)
8995 return Error(E);
8996 if (Source == E) {
8997 assert(0 && "OpaqueValueExpr recursively refers to itself");
8998 return Error(E);
8999 }
9000 return StmtVisitorTy::Visit(Source);
9001 }
9002
9003 bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
9004 if (diagnoseBPFPreserveAIEnumerator(Info, E))
9005 return false;
9006 for (const Expr *SemE : E->semantics()) {
9007 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: SemE)) {
9008 // FIXME: We can't handle the case where an OpaqueValueExpr is also the
9009 // result expression: there could be two different LValues that would
9010 // refer to the same object in that case, and we can't model that.
9011 if (SemE == E->getResultExpr())
9012 return Error(E);
9013
9014 // Unique OVEs get evaluated if and when we encounter them when
9015 // emitting the rest of the semantic form, rather than eagerly.
9016 if (OVE->isUnique())
9017 continue;
9018
9019 LValue LV;
9020 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
9021 Key: OVE, T: getStorageType(Ctx: Info.Ctx, E: OVE),
9022 Scope: ScopeKind::FullExpression, LV),
9023 Info, E: OVE->getSourceExpr()))
9024 return false;
9025 } else if (SemE == E->getResultExpr()) {
9026 if (!StmtVisitorTy::Visit(SemE))
9027 return false;
9028 } else {
9029 if (!EvaluateIgnoredValue(Info, E: SemE))
9030 return false;
9031 }
9032 }
9033 return true;
9034 }
9035
9036 bool VisitCallExpr(const CallExpr *E) {
9037 APValue Result;
9038 if (!handleCallExpr(E, Result, ResultSlot: nullptr))
9039 return false;
9040 return DerivedSuccess(V: Result, E);
9041 }
9042
9043 bool handleCallExpr(const CallExpr *E, APValue &Result,
9044 const LValue *ResultSlot) {
9045 CallScopeRAII CallScope(Info);
9046
9047 const Expr *Callee = E->getCallee()->IgnoreParens();
9048 QualType CalleeType = Callee->getType();
9049
9050 const FunctionDecl *FD = nullptr;
9051 LValue *This = nullptr, ObjectArg;
9052 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
9053 bool HasQualifier = false;
9054
9055 CallRef Call;
9056
9057 // Extract function decl and 'this' pointer from the callee.
9058 if (CalleeType->isSpecificBuiltinType(K: BuiltinType::BoundMember)) {
9059 const CXXMethodDecl *Member = nullptr;
9060 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: Callee)) {
9061 // Explicit bound member calls, such as x.f() or p->g();
9062 if (!EvaluateObjectArgument(Info, Object: ME->getBase(), This&: ObjectArg))
9063 return false;
9064 Member = dyn_cast<CXXMethodDecl>(Val: ME->getMemberDecl());
9065 if (!Member)
9066 return Error(Callee);
9067 This = &ObjectArg;
9068 HasQualifier = ME->hasQualifier();
9069 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Val: Callee)) {
9070 // Indirect bound member calls ('.*' or '->*').
9071 const ValueDecl *D =
9072 HandleMemberPointerAccess(Info, BO: BE, LV&: ObjectArg, IncludeMember: false);
9073 if (!D)
9074 return false;
9075 Member = dyn_cast<CXXMethodDecl>(Val: D);
9076 if (!Member)
9077 return Error(Callee);
9078 This = &ObjectArg;
9079 } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Val: Callee)) {
9080 if (!Info.getLangOpts().CPlusPlus20)
9081 Info.CCEDiag(E: PDE, DiagId: diag::note_constexpr_pseudo_destructor);
9082 return EvaluateObjectArgument(Info, Object: PDE->getBase(), This&: ObjectArg) &&
9083 HandleDestruction(Info, E: PDE, This: ObjectArg, ThisType: PDE->getDestroyedType());
9084 } else
9085 return Error(Callee);
9086 FD = Member;
9087 } else if (CalleeType->isFunctionPointerType()) {
9088 LValue CalleeLV;
9089 if (!EvaluatePointer(E: Callee, Result&: CalleeLV, Info))
9090 return false;
9091
9092 if (!CalleeLV.getLValueOffset().isZero())
9093 return Error(Callee);
9094 if (CalleeLV.isNullPointer()) {
9095 Info.FFDiag(E: Callee, DiagId: diag::note_constexpr_null_callee)
9096 << const_cast<Expr *>(Callee);
9097 return false;
9098 }
9099 FD = dyn_cast_or_null<FunctionDecl>(
9100 Val: CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>());
9101 if (!FD)
9102 return Error(Callee);
9103 // Don't call function pointers which have been cast to some other type.
9104 // Per DR (no number yet), the caller and callee can differ in noexcept.
9105 if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec(
9106 T: CalleeType->getPointeeType(), U: FD->getType())) {
9107 return Error(E);
9108 }
9109
9110 // For an (overloaded) assignment expression, evaluate the RHS before the
9111 // LHS.
9112 auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E);
9113 if (OCE && OCE->isAssignmentOp()) {
9114 assert(Args.size() == 2 && "wrong number of arguments in assignment");
9115 Call = Info.CurrentCall->createCall(Callee: FD);
9116 bool HasThis = false;
9117 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FD))
9118 HasThis = MD->isImplicitObjectMemberFunction();
9119 if (!EvaluateArgs(Args: HasThis ? Args.slice(N: 1) : Args, Call, Info, Callee: FD,
9120 /*RightToLeft=*/true, ObjectArg: &ObjectArg))
9121 return false;
9122 }
9123
9124 // Overloaded operator calls to member functions are represented as normal
9125 // calls with '*this' as the first argument.
9126 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
9127 if (MD &&
9128 (MD->isImplicitObjectMemberFunction() || (OCE && MD->isStatic()))) {
9129 // FIXME: When selecting an implicit conversion for an overloaded
9130 // operator delete, we sometimes try to evaluate calls to conversion
9131 // operators without a 'this' parameter!
9132 if (Args.empty())
9133 return Error(E);
9134
9135 if (!EvaluateObjectArgument(Info, Object: Args[0], This&: ObjectArg))
9136 return false;
9137
9138 // If we are calling a static operator, the 'this' argument needs to be
9139 // ignored after being evaluated.
9140 if (MD->isInstance())
9141 This = &ObjectArg;
9142
9143 // If this is syntactically a simple assignment using a trivial
9144 // assignment operator, start the lifetimes of union members as needed,
9145 // per C++20 [class.union]5.
9146 if (Info.getLangOpts().CPlusPlus20 && OCE &&
9147 OCE->getOperator() == OO_Equal && MD->isTrivial() &&
9148 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: Args[0], LHS: ObjectArg))
9149 return false;
9150
9151 Args = Args.slice(N: 1);
9152 } else if (MD && MD->isLambdaStaticInvoker()) {
9153 // Map the static invoker for the lambda back to the call operator.
9154 // Conveniently, we don't have to slice out the 'this' argument (as is
9155 // being done for the non-static case), since a static member function
9156 // doesn't have an implicit argument passed in.
9157 const CXXRecordDecl *ClosureClass = MD->getParent();
9158 assert(
9159 ClosureClass->captures().empty() &&
9160 "Number of captures must be zero for conversion to function-ptr");
9161
9162 const CXXMethodDecl *LambdaCallOp =
9163 ClosureClass->getLambdaCallOperator();
9164
9165 // Set 'FD', the function that will be called below, to the call
9166 // operator. If the closure object represents a generic lambda, find
9167 // the corresponding specialization of the call operator.
9168
9169 if (ClosureClass->isGenericLambda()) {
9170 assert(MD->isFunctionTemplateSpecialization() &&
9171 "A generic lambda's static-invoker function must be a "
9172 "template specialization");
9173 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
9174 FunctionTemplateDecl *CallOpTemplate =
9175 LambdaCallOp->getDescribedFunctionTemplate();
9176 llvm::FoldingSetInsertToken InsertToken;
9177 FunctionDecl *CorrespondingCallOpSpecialization =
9178 CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertToken);
9179 assert(CorrespondingCallOpSpecialization &&
9180 "We must always have a function call operator specialization "
9181 "that corresponds to our static invoker specialization");
9182 assert(isa<CXXMethodDecl>(CorrespondingCallOpSpecialization));
9183 FD = CorrespondingCallOpSpecialization;
9184 } else
9185 FD = LambdaCallOp;
9186 } else if (FD->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
9187 if (FD->getDeclName().isAnyOperatorNew()) {
9188 LValue Ptr;
9189 if (!HandleOperatorNewCall(Info, E, Result&: Ptr))
9190 return false;
9191 Ptr.moveInto(V&: Result);
9192 return CallScope.destroy();
9193 } else {
9194 return HandleOperatorDeleteCall(Info, E) && CallScope.destroy();
9195 }
9196 }
9197 } else
9198 return Error(E);
9199
9200 // Evaluate the arguments now if we've not already done so.
9201 if (!Call) {
9202 Call = Info.CurrentCall->createCall(Callee: FD);
9203 if (!EvaluateArgs(Args, Call, Info, Callee: FD, /*RightToLeft*/ false,
9204 ObjectArg: &ObjectArg))
9205 return false;
9206 }
9207
9208 SmallVector<QualType, 4> CovariantAdjustmentPath;
9209 if (This) {
9210 auto *NamedMember = dyn_cast<CXXMethodDecl>(Val: FD);
9211 if (NamedMember && NamedMember->isVirtual() && !HasQualifier) {
9212 // Perform virtual dispatch, if necessary.
9213 FD = HandleVirtualDispatch(Info, E, This&: *This, Found: NamedMember,
9214 CovariantAdjustmentPath);
9215 if (!FD)
9216 return false;
9217 } else if (NamedMember && NamedMember->isImplicitObjectMemberFunction()) {
9218 // Check that the 'this' pointer points to an object of the right type.
9219 // FIXME: If this is an assignment operator call, we may need to change
9220 // the active union member before we check this.
9221 if (!checkNonVirtualMemberCallThisPointer(Info, E, This: *This, NamedMember))
9222 return false;
9223 }
9224 }
9225
9226 // Destructor calls are different enough that they have their own codepath.
9227 if (auto *DD = dyn_cast<CXXDestructorDecl>(Val: FD)) {
9228 assert(This && "no 'this' pointer for destructor call");
9229 return HandleDestruction(Info, E, This: *This,
9230 ThisType: Info.Ctx.getCanonicalTagType(TD: DD->getParent())) &&
9231 CallScope.destroy();
9232 }
9233
9234 SourceLocation Loc = E->getExprLoc();
9235
9236 TryInstantiateFunctionBeforeCall(FD, Info, Loc);
9237
9238 const FunctionDecl *Definition = nullptr;
9239 Stmt *Body = FD->getBody(Definition);
9240
9241 // Treat the object argument as `this` when evaluating defaulted
9242 // special menmber functions
9243 if (FD->hasCXXExplicitFunctionObjectParameter())
9244 This = &ObjectArg;
9245
9246 if (!CheckConstexprFunction(Info, CallLoc: Loc, Declaration: FD, Definition, Body) ||
9247 !HandleFunctionCall(CallLoc: Loc, Callee: Definition, ObjectArg: This, E, Args, Call, Body, Info,
9248 Result, ResultSlot))
9249 return false;
9250
9251 if (!CovariantAdjustmentPath.empty() &&
9252 !HandleCovariantReturnAdjustment(Info, E, Result,
9253 Path: CovariantAdjustmentPath))
9254 return false;
9255
9256 return CallScope.destroy();
9257 }
9258
9259 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9260 return StmtVisitorTy::Visit(E->getInitializer());
9261 }
9262 bool VisitInitListExpr(const InitListExpr *E) {
9263 if (E->getNumInits() == 0)
9264 return DerivedZeroInitialization(E);
9265 if (E->getNumInits() == 1)
9266 return StmtVisitorTy::Visit(E->getInit(Init: 0));
9267 return Error(E);
9268 }
9269 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
9270 return DerivedZeroInitialization(E);
9271 }
9272 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
9273 return DerivedZeroInitialization(E);
9274 }
9275 bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
9276 return DerivedZeroInitialization(E);
9277 }
9278
9279 /// A member expression where the object is a prvalue is itself a prvalue.
9280 bool VisitMemberExpr(const MemberExpr *E) {
9281 assert(!Info.Ctx.getLangOpts().CPlusPlus11 &&
9282 "missing temporary materialization conversion");
9283 assert(!E->isArrow() && "missing call to bound member function?");
9284
9285 APValue Val;
9286 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
9287 return false;
9288
9289 QualType BaseTy = E->getBase()->getType();
9290
9291 const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl());
9292 if (!FD) return Error(E);
9293 assert(!FD->getType()->isReferenceType() && "prvalue reference?");
9294 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9295 FD->getParent()->getCanonicalDecl() &&
9296 "record / field mismatch");
9297
9298 // Note: there is no lvalue base here. But this case should only ever
9299 // happen in C or in C++98, where we cannot be evaluating a constexpr
9300 // constructor, which is the only case the base matters.
9301 CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy);
9302 SubobjectDesignator Designator(BaseTy);
9303 Designator.addDeclUnchecked(D: FD);
9304
9305 APValue Result;
9306 return extractSubobject(Info, E, Obj, Sub: Designator, Result) &&
9307 DerivedSuccess(V: Result, E);
9308 }
9309
9310 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) {
9311 APValue Val;
9312 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
9313 return false;
9314
9315 if (Val.isVector()) {
9316 SmallVector<uint32_t, 4> Indices;
9317 E->getEncodedElementAccess(Elts&: Indices);
9318 if (Indices.size() == 1) {
9319 // Return scalar.
9320 return DerivedSuccess(V: Val.getVectorElt(I: Indices[0]), E);
9321 } else {
9322 // Construct new APValue vector.
9323 SmallVector<APValue, 4> Elts;
9324 for (unsigned I = 0; I < Indices.size(); ++I) {
9325 Elts.push_back(Elt: Val.getVectorElt(I: Indices[I]));
9326 }
9327 APValue VecResult(Elts.data(), Indices.size());
9328 return DerivedSuccess(V: VecResult, E);
9329 }
9330 }
9331
9332 return false;
9333 }
9334
9335 bool VisitCastExpr(const CastExpr *E) {
9336 switch (E->getCastKind()) {
9337 default:
9338 break;
9339
9340 case CK_AtomicToNonAtomic: {
9341 APValue AtomicVal;
9342 // This does not need to be done in place even for class/array types:
9343 // atomic-to-non-atomic conversion implies copying the object
9344 // representation.
9345 if (!Evaluate(Result&: AtomicVal, Info, E: E->getSubExpr()))
9346 return false;
9347 return DerivedSuccess(V: AtomicVal, E);
9348 }
9349
9350 case CK_NoOp:
9351 case CK_UserDefinedConversion:
9352 return StmtVisitorTy::Visit(E->getSubExpr());
9353
9354 case CK_HLSLArrayRValue: {
9355 const Expr *SubExpr = E->getSubExpr();
9356 if (!SubExpr->isGLValue()) {
9357 APValue Val;
9358 if (!Evaluate(Result&: Val, Info, E: SubExpr))
9359 return false;
9360 return DerivedSuccess(V: Val, E);
9361 }
9362
9363 LValue LVal;
9364 if (!EvaluateLValue(E: SubExpr, Result&: LVal, Info))
9365 return false;
9366 APValue RVal;
9367 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9368 if (!handleLValueToRValueConversion(Info, Conv: E, Type: SubExpr->getType(), LVal,
9369 RVal))
9370 return false;
9371 return DerivedSuccess(V: RVal, E);
9372 }
9373 case CK_LValueToRValue: {
9374 LValue LVal;
9375 if (!EvaluateLValue(E: E->getSubExpr(), Result&: LVal, Info))
9376 return false;
9377 APValue RVal;
9378 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9379 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
9380 LVal, RVal))
9381 return false;
9382 return DerivedSuccess(V: RVal, E);
9383 }
9384 case CK_LValueToRValueBitCast: {
9385 APValue DestValue, SourceValue;
9386 if (!Evaluate(Result&: SourceValue, Info, E: E->getSubExpr()))
9387 return false;
9388 if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, BCE: E))
9389 return false;
9390 return DerivedSuccess(V: DestValue, E);
9391 }
9392
9393 case CK_AddressSpaceConversion: {
9394 APValue Value;
9395 if (!Evaluate(Result&: Value, Info, E: E->getSubExpr()))
9396 return false;
9397 return DerivedSuccess(V: Value, E);
9398 }
9399 }
9400
9401 return Error(E);
9402 }
9403
9404 bool VisitUnaryPostInc(const UnaryOperator *UO) {
9405 return VisitUnaryPostIncDec(UO);
9406 }
9407 bool VisitUnaryPostDec(const UnaryOperator *UO) {
9408 return VisitUnaryPostIncDec(UO);
9409 }
9410 bool VisitUnaryPostIncDec(const UnaryOperator *UO) {
9411 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9412 return Error(UO);
9413
9414 LValue LVal;
9415 if (!EvaluateLValue(E: UO->getSubExpr(), Result&: LVal, Info))
9416 return false;
9417 APValue RVal;
9418 if (!handleIncDec(Info&: this->Info, E: UO, LVal, LValType: UO->getSubExpr()->getType(),
9419 IsIncrement: UO->isIncrementOp(), Old: &RVal))
9420 return false;
9421 return DerivedSuccess(V: RVal, E: UO);
9422 }
9423
9424 bool VisitStmtExpr(const StmtExpr *E) {
9425 // We will have checked the full-expressions inside the statement expression
9426 // when they were completed, and don't need to check them again now.
9427 llvm::SaveAndRestore NotCheckingForUB(Info.CheckingForUndefinedBehavior,
9428 false);
9429
9430 const CompoundStmt *CS = E->getSubStmt();
9431 if (CS->body_empty())
9432 return true;
9433
9434 BlockScopeRAII Scope(Info);
9435 for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
9436 BE = CS->body_end();
9437 /**/; ++BI) {
9438 if (BI + 1 == BE) {
9439 const Expr *FinalExpr = dyn_cast<Expr>(Val: *BI);
9440 if (!FinalExpr) {
9441 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9442 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9443 return false;
9444 }
9445 return this->Visit(FinalExpr) && Scope.destroy();
9446 }
9447
9448 APValue ReturnValue;
9449 StmtResult Result = { .Value: ReturnValue, .Slot: nullptr };
9450 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: *BI);
9451 if (ESR != ESR_Succeeded) {
9452 // FIXME: If the statement-expression terminated due to 'return',
9453 // 'break', or 'continue', it would be nice to propagate that to
9454 // the outer statement evaluation rather than bailing out.
9455 if (ESR != ESR_Failed)
9456 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9457 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9458 return false;
9459 }
9460 }
9461
9462 llvm_unreachable("Return from function from the loop above.");
9463 }
9464
9465 bool VisitPackIndexingExpr(const PackIndexingExpr *E) {
9466 return StmtVisitorTy::Visit(E->getSelectedExpr());
9467 }
9468
9469 /// Visit a value which is evaluated, but whose value is ignored.
9470 void VisitIgnoredValue(const Expr *E) {
9471 EvaluateIgnoredValue(Info, E);
9472 }
9473
9474 /// Potentially visit a MemberExpr's base expression.
9475 void VisitIgnoredBaseExpression(const Expr *E) {
9476 // While MSVC doesn't evaluate the base expression, it does diagnose the
9477 // presence of side-effecting behavior.
9478 if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Ctx: Info.Ctx))
9479 return;
9480 VisitIgnoredValue(E);
9481 }
9482};
9483
9484} // namespace
9485
9486//===----------------------------------------------------------------------===//
9487// Common base class for lvalue and temporary evaluation.
9488//===----------------------------------------------------------------------===//
9489namespace {
9490template<class Derived>
9491class LValueExprEvaluatorBase
9492 : public ExprEvaluatorBase<Derived> {
9493protected:
9494 LValue &Result;
9495 bool InvalidBaseOK;
9496 typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
9497 typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy;
9498
9499 bool Success(APValue::LValueBase B) {
9500 Result.set(B);
9501 return true;
9502 }
9503
9504 bool evaluatePointer(const Expr *E, LValue &Result) {
9505 return EvaluatePointer(E, Result, this->Info, InvalidBaseOK);
9506 }
9507
9508public:
9509 LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK)
9510 : ExprEvaluatorBaseTy(Info), Result(Result),
9511 InvalidBaseOK(InvalidBaseOK) {}
9512
9513 bool Success(const APValue &V, const Expr *E) {
9514 Result.setFrom(Ctx: this->Info.Ctx, V);
9515 return true;
9516 }
9517
9518 bool VisitMemberExpr(const MemberExpr *E) {
9519 // Handle non-static data members.
9520 QualType BaseTy;
9521 bool EvalOK;
9522 if (E->isArrow()) {
9523 EvalOK = evaluatePointer(E: E->getBase(), Result);
9524 BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType();
9525 } else if (E->getBase()->isPRValue()) {
9526 assert(E->getBase()->getType()->isRecordType());
9527 EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info);
9528 BaseTy = E->getBase()->getType();
9529 } else {
9530 EvalOK = this->Visit(E->getBase());
9531 BaseTy = E->getBase()->getType();
9532 }
9533 if (!EvalOK) {
9534 if (!InvalidBaseOK)
9535 return false;
9536 Result.setInvalid(B: E);
9537 return true;
9538 }
9539
9540 const ValueDecl *MD = E->getMemberDecl();
9541 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl())) {
9542 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9543 FD->getParent()->getCanonicalDecl() &&
9544 "record / field mismatch");
9545 (void)BaseTy;
9546 if (!HandleLValueMember(this->Info, E, Result, FD))
9547 return false;
9548 } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Val: MD)) {
9549 if (!HandleLValueIndirectMember(this->Info, E, Result, IFD))
9550 return false;
9551 } else
9552 return this->Error(E);
9553
9554 if (MD->getType()->isReferenceType()) {
9555 APValue RefValue;
9556 if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
9557 RefValue))
9558 return false;
9559 return Success(RefValue, E);
9560 }
9561 return true;
9562 }
9563
9564 bool VisitBinaryOperator(const BinaryOperator *E) {
9565 switch (E->getOpcode()) {
9566 default:
9567 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
9568
9569 case BO_PtrMemD:
9570 case BO_PtrMemI:
9571 return HandleMemberPointerAccess(this->Info, E, Result);
9572 }
9573 }
9574
9575 bool VisitCastExpr(const CastExpr *E) {
9576 switch (E->getCastKind()) {
9577 default:
9578 return ExprEvaluatorBaseTy::VisitCastExpr(E);
9579
9580 case CK_DerivedToBase:
9581 case CK_UncheckedDerivedToBase:
9582 if (!this->Visit(E->getSubExpr()))
9583 return false;
9584
9585 // Now figure out the necessary offset to add to the base LV to get from
9586 // the derived class to the base class.
9587 return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(),
9588 Result);
9589 }
9590 }
9591};
9592}
9593
9594//===----------------------------------------------------------------------===//
9595// LValue Evaluation
9596//
9597// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
9598// function designators (in C), decl references to void objects (in C), and
9599// temporaries (if building with -Wno-address-of-temporary).
9600//
9601// LValue evaluation produces values comprising a base expression of one of the
9602// following types:
9603// - Declarations
9604// * VarDecl
9605// * FunctionDecl
9606// - Literals
9607// * CompoundLiteralExpr in C (and in global scope in C++)
9608// * StringLiteral
9609// * PredefinedExpr
9610// * ObjCStringLiteralExpr
9611// * ObjCEncodeExpr
9612// * AddrLabelExpr
9613// * BlockExpr
9614// * CallExpr for a MakeStringConstant builtin
9615// - typeid(T) expressions, as TypeInfoLValues
9616// - Locals and temporaries
9617// * MaterializeTemporaryExpr
9618// * Any Expr, with a CallIndex indicating the function in which the temporary
9619// was evaluated, for cases where the MaterializeTemporaryExpr is missing
9620// from the AST (FIXME).
9621// * A MaterializeTemporaryExpr that has static storage duration, with no
9622// CallIndex, for a lifetime-extended temporary.
9623// * The ConstantExpr that is currently being evaluated during evaluation of an
9624// immediate invocation.
9625// plus an offset in bytes.
9626//===----------------------------------------------------------------------===//
9627namespace {
9628class LValueExprEvaluator
9629 : public LValueExprEvaluatorBase<LValueExprEvaluator> {
9630public:
9631 LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) :
9632 LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {}
9633
9634 bool VisitVarDecl(const Expr *E, const VarDecl *VD);
9635 bool VisitUnaryPreIncDec(const UnaryOperator *UO);
9636
9637 bool VisitCallExpr(const CallExpr *E);
9638 bool VisitDeclRefExpr(const DeclRefExpr *E);
9639 bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(B: E); }
9640 bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
9641 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
9642 bool VisitMemberExpr(const MemberExpr *E);
9643 bool VisitStringLiteral(const StringLiteral *E) {
9644 return Success(
9645 B: APValue::LValueBase(E, 0, Info.Ctx.getNextStringLiteralVersion()));
9646 }
9647 bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(B: E); }
9648 bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
9649 bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
9650 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
9651 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E);
9652 bool VisitUnaryDeref(const UnaryOperator *E);
9653 bool VisitUnaryReal(const UnaryOperator *E);
9654 bool VisitUnaryImag(const UnaryOperator *E);
9655 bool VisitUnaryPreInc(const UnaryOperator *UO) {
9656 return VisitUnaryPreIncDec(UO);
9657 }
9658 bool VisitUnaryPreDec(const UnaryOperator *UO) {
9659 return VisitUnaryPreIncDec(UO);
9660 }
9661 bool VisitBinAssign(const BinaryOperator *BO);
9662 bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO);
9663
9664 bool VisitCastExpr(const CastExpr *E) {
9665 switch (E->getCastKind()) {
9666 default:
9667 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
9668
9669 case CK_LValueBitCast:
9670 this->CCEDiag(E, D: diag::note_constexpr_invalid_cast)
9671 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
9672 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
9673 if (!Visit(S: E->getSubExpr()))
9674 return false;
9675 Result.Designator.setInvalid();
9676 return true;
9677
9678 case CK_BaseToDerived:
9679 if (!Visit(S: E->getSubExpr()))
9680 return false;
9681 return HandleBaseToDerivedCast(Info, E, Result);
9682
9683 case CK_Dynamic:
9684 if (!Visit(S: E->getSubExpr()))
9685 return false;
9686 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
9687 }
9688 }
9689};
9690} // end anonymous namespace
9691
9692/// Get an lvalue to a field of a lambda's closure type.
9693static bool HandleLambdaCapture(EvalInfo &Info, const Expr *E, LValue &Result,
9694 const CXXMethodDecl *MD, const FieldDecl *FD,
9695 bool LValueToRValueConversion) {
9696 // Static lambda function call operators can't have captures. We already
9697 // diagnosed this, so bail out here.
9698 if (MD->isStatic()) {
9699 assert(Info.CurrentCall->This == nullptr &&
9700 "This should not be set for a static call operator");
9701 return false;
9702 }
9703
9704 // Start with 'Result' referring to the complete closure object...
9705 if (MD->isExplicitObjectMemberFunction()) {
9706 // Self may be passed by reference or by value.
9707 const ParmVarDecl *Self = MD->getParamDecl(i: 0);
9708 if (Self->getType()->isReferenceType()) {
9709 APValue *RefValue = Info.getParamSlot(Call: Info.CurrentCall->Arguments, PVD: Self);
9710 if (!RefValue->allowConstexprUnknown() || RefValue->hasValue())
9711 Result.setFrom(Ctx: Info.Ctx, V: *RefValue);
9712 } else {
9713 const ParmVarDecl *VD = Info.CurrentCall->Arguments.getOrigParam(PVD: Self);
9714 CallStackFrame *Frame =
9715 Info.getCallFrameAndDepth(CallIndex: Info.CurrentCall->Arguments.CallIndex)
9716 .first;
9717 unsigned Version = Info.CurrentCall->Arguments.Version;
9718 Result.set(B: {VD, Frame->Index, Version});
9719 }
9720 } else
9721 Result = *Info.CurrentCall->This;
9722
9723 // ... then update it to refer to the field of the closure object
9724 // that represents the capture.
9725 if (!HandleLValueMember(Info, E, LVal&: Result, FD))
9726 return false;
9727
9728 // And if the field is of reference type (or if we captured '*this' by
9729 // reference), update 'Result' to refer to what
9730 // the field refers to.
9731 if (LValueToRValueConversion) {
9732 APValue RVal;
9733 if (!handleLValueToRValueConversion(Info, Conv: E, Type: FD->getType(), LVal: Result, RVal))
9734 return false;
9735 Result.setFrom(Ctx: Info.Ctx, V: RVal);
9736 }
9737 return true;
9738}
9739
9740/// Evaluate an expression as an lvalue. This can be legitimately called on
9741/// expressions which are not glvalues, in three cases:
9742/// * function designators in C, and
9743/// * "extern void" objects
9744/// * @selector() expressions in Objective-C
9745static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
9746 bool InvalidBaseOK) {
9747 assert(!E->isValueDependent());
9748 assert(E->isGLValue() || E->getType()->isFunctionType() ||
9749 E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E->IgnoreParens()));
9750 return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
9751}
9752
9753bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
9754 const ValueDecl *D = E->getDecl();
9755
9756 // If we are within a lambda's call operator, check whether the 'VD' referred
9757 // to within 'E' actually represents a lambda-capture that maps to a
9758 // data-member/field within the closure object, and if so, evaluate to the
9759 // field or what the field refers to.
9760 if (Info.CurrentCall && isLambdaCallOperator(DC: Info.CurrentCall->Callee) &&
9761 E->refersToEnclosingVariableOrCapture()) {
9762 // We don't always have a complete capture-map when checking or inferring if
9763 // the function call operator meets the requirements of a constexpr function
9764 // - but we don't need to evaluate the captures to determine constexprness
9765 // (dcl.constexpr C++17).
9766 if (Info.checkingPotentialConstantExpression())
9767 return false;
9768
9769 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(Val: D)) {
9770 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
9771 return HandleLambdaCapture(Info, E, Result, MD, FD,
9772 LValueToRValueConversion: FD->getType()->isReferenceType());
9773 }
9774 }
9775
9776 if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl,
9777 UnnamedGlobalConstantDecl>(Val: D))
9778 return Success(B: cast<ValueDecl>(Val: D));
9779 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
9780 return VisitVarDecl(E, VD);
9781 if (const BindingDecl *BD = dyn_cast<BindingDecl>(Val: D))
9782 return Visit(S: BD->getBinding());
9783 return Error(E);
9784}
9785
9786bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
9787 CallStackFrame *Frame = nullptr;
9788 unsigned Version = 0;
9789 if (VD->hasLocalStorage()) {
9790 // Only if a local variable was declared in the function currently being
9791 // evaluated, do we expect to be able to find its value in the current
9792 // frame. (Otherwise it was likely declared in an enclosing context and
9793 // could either have a valid evaluatable value (for e.g. a constexpr
9794 // variable) or be ill-formed (and trigger an appropriate evaluation
9795 // diagnostic)).
9796 CallStackFrame *CurrFrame = Info.CurrentCall;
9797 if (CurrFrame->Callee && CurrFrame->Callee->Equals(DC: VD->getDeclContext())) {
9798 // Function parameters are stored in some caller's frame. (Usually the
9799 // immediate caller, but for an inherited constructor they may be more
9800 // distant.)
9801 if (auto *PVD = dyn_cast<ParmVarDecl>(Val: VD)) {
9802 if (CurrFrame->Arguments) {
9803 VD = CurrFrame->Arguments.getOrigParam(PVD);
9804 Frame =
9805 Info.getCallFrameAndDepth(CallIndex: CurrFrame->Arguments.CallIndex).first;
9806 Version = CurrFrame->Arguments.Version;
9807 }
9808 } else {
9809 Frame = CurrFrame;
9810 Version = CurrFrame->getCurrentTemporaryVersion(Key: VD);
9811 }
9812 }
9813 }
9814
9815 if (!VD->getType()->isReferenceType()) {
9816 if (Frame) {
9817 Result.set(B: {VD, Frame->Index, Version});
9818 return true;
9819 }
9820 return Success(B: VD);
9821 }
9822
9823 if (!Info.getLangOpts().CPlusPlus11) {
9824 Info.CCEDiag(E, DiagId: diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
9825 << VD << VD->getType();
9826 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
9827 }
9828
9829 APValue *V;
9830 if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, Result&: V))
9831 return false;
9832
9833 if (!V) {
9834 Result.set(B: VD);
9835 Result.AllowConstexprUnknown = true;
9836 return true;
9837 }
9838
9839 return Success(V: *V, E);
9840}
9841
9842bool LValueExprEvaluator::VisitCallExpr(const CallExpr *E) {
9843 if (!IsConstantEvaluatedBuiltinCall(E))
9844 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9845
9846 switch (E->getBuiltinCallee()) {
9847 default:
9848 return false;
9849 case Builtin::BIas_const:
9850 case Builtin::BIforward:
9851 case Builtin::BIforward_like:
9852 case Builtin::BImove:
9853 case Builtin::BImove_if_noexcept:
9854 if (cast<FunctionDecl>(Val: E->getCalleeDecl())->isConstexpr())
9855 return Visit(S: E->getArg(Arg: 0));
9856 break;
9857 }
9858
9859 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9860}
9861
9862bool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
9863 const MaterializeTemporaryExpr *E) {
9864 // Walk through the expression to find the materialized temporary itself.
9865 SmallVector<const Expr *, 2> CommaLHSs;
9866 SmallVector<SubobjectAdjustment, 2> Adjustments;
9867 const Expr *Inner =
9868 E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments);
9869
9870 // If we passed any comma operators, evaluate their LHSs.
9871 for (const Expr *E : CommaLHSs)
9872 if (!EvaluateIgnoredValue(Info, E))
9873 return false;
9874
9875 // A materialized temporary with static storage duration can appear within the
9876 // result of a constant expression evaluation, so we need to preserve its
9877 // value for use outside this evaluation.
9878 APValue *Value;
9879 if (E->getStorageDuration() == SD_Static) {
9880 if (Info.EvalMode == EvaluationMode::ConstantFold)
9881 return false;
9882 // FIXME: What about SD_Thread?
9883 Value = E->getOrCreateValue(MayCreate: true);
9884 *Value = APValue();
9885 Result.set(B: E);
9886 } else {
9887 Value = &Info.CurrentCall->createTemporary(
9888 Key: E, T: Inner->getType(),
9889 Scope: E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression
9890 : ScopeKind::Block,
9891 LV&: Result);
9892 }
9893
9894 QualType Type = Inner->getType();
9895
9896 // Materialize the temporary itself.
9897 if (!EvaluateInPlace(Result&: *Value, Info, This: Result, E: Inner)) {
9898 *Value = APValue();
9899 return false;
9900 }
9901
9902 // Adjust our lvalue to refer to the desired subobject.
9903 for (unsigned I = Adjustments.size(); I != 0; /**/) {
9904 --I;
9905 switch (Adjustments[I].Kind) {
9906 case SubobjectAdjustment::DerivedToBaseAdjustment:
9907 if (!HandleLValueBasePath(Info, E: Adjustments[I].DerivedToBase.BasePath,
9908 Type, Result))
9909 return false;
9910 Type = Adjustments[I].DerivedToBase.BasePath->getType();
9911 break;
9912
9913 case SubobjectAdjustment::FieldAdjustment:
9914 if (!HandleLValueMember(Info, E, LVal&: Result, FD: Adjustments[I].Field))
9915 return false;
9916 Type = Adjustments[I].Field->getType();
9917 break;
9918
9919 case SubobjectAdjustment::MemberPointerAdjustment:
9920 if (!HandleMemberPointerAccess(Info&: this->Info, LVType: Type, LV&: Result,
9921 RHS: Adjustments[I].Ptr.RHS))
9922 return false;
9923 Type = Adjustments[I].Ptr.MPT->getPointeeType();
9924 break;
9925 }
9926 }
9927
9928 return true;
9929}
9930
9931bool
9932LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9933 assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) &&
9934 "lvalue compound literal in c++?");
9935 APValue *Lit;
9936 // If CompountLiteral has static storage, its value can be used outside
9937 // this expression. So evaluate it once and store it in ASTContext.
9938 if (E->hasStaticStorage()) {
9939 Lit = &E->getOrCreateStaticValue(Ctx&: Info.Ctx);
9940 Result.set(B: E);
9941 // Reset any previously evaluated state, otherwise evaluation below might
9942 // fail.
9943 // FIXME: Should we just re-use the previously evaluated value instead?
9944 *Lit = APValue();
9945 } else {
9946 assert(!Info.getLangOpts().CPlusPlus);
9947 Lit = &Info.CurrentCall->createTemporary(Key: E, T: E->getInitializer()->getType(),
9948 Scope: ScopeKind::Block, LV&: Result);
9949 }
9950 // FIXME: Evaluating in place isn't always right. We should figure out how to
9951 // use appropriate evaluation context here, see
9952 // clang/test/AST/static-compound-literals-reeval.cpp for a failure.
9953 if (!EvaluateInPlace(Result&: *Lit, Info, This: Result, E: E->getInitializer())) {
9954 *Lit = APValue();
9955 return false;
9956 }
9957 return true;
9958}
9959
9960bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
9961 TypeInfoLValue TypeInfo;
9962
9963 if (!E->isPotentiallyEvaluated()) {
9964 if (E->isTypeOperand())
9965 TypeInfo = TypeInfoLValue(E->getTypeOperand(Context: Info.Ctx).getTypePtr());
9966 else
9967 TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr());
9968 } else {
9969 if (!Info.Ctx.getLangOpts().CPlusPlus20) {
9970 Info.CCEDiag(E, DiagId: diag::note_constexpr_typeid_polymorphic)
9971 << E->getExprOperand()->getType()
9972 << E->getExprOperand()->getSourceRange();
9973 }
9974
9975 if (!Visit(S: E->getExprOperand()))
9976 return false;
9977
9978 std::optional<DynamicType> DynType =
9979 ComputeDynamicType(Info, E, This&: Result, AK: AK_TypeId);
9980 if (!DynType)
9981 return false;
9982
9983 TypeInfo = TypeInfoLValue(
9984 Info.Ctx.getCanonicalTagType(TD: DynType->Type).getTypePtr());
9985 }
9986
9987 return Success(B: APValue::LValueBase::getTypeInfo(LV: TypeInfo, TypeInfo: E->getType()));
9988}
9989
9990bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
9991 return Success(B: E->getGuidDecl());
9992}
9993
9994bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
9995 // Handle static data members.
9996 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: E->getMemberDecl())) {
9997 VisitIgnoredBaseExpression(E: E->getBase());
9998 return VisitVarDecl(E, VD);
9999 }
10000
10001 // Handle static member functions.
10002 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: E->getMemberDecl())) {
10003 if (MD->isStatic()) {
10004 VisitIgnoredBaseExpression(E: E->getBase());
10005 return Success(B: MD);
10006 }
10007 }
10008
10009 // Handle non-static data members.
10010 return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
10011}
10012
10013bool LValueExprEvaluator::VisitExtVectorElementExpr(
10014 const ExtVectorElementExpr *E) {
10015 bool Success = true;
10016
10017 APValue Val;
10018 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
10019 if (!Info.noteFailure())
10020 return false;
10021 Success = false;
10022 }
10023
10024 SmallVector<uint32_t, 4> Indices;
10025 E->getEncodedElementAccess(Elts&: Indices);
10026 // FIXME: support accessing more than one element
10027 if (Indices.size() > 1)
10028 return false;
10029
10030 if (Success) {
10031 Result.setFrom(Ctx: Info.Ctx, V: Val);
10032 QualType BaseType = E->getBase()->getType();
10033 if (E->isArrow())
10034 BaseType = BaseType->getPointeeType();
10035 const auto *VT = BaseType->castAs<VectorType>();
10036 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
10037 Size: VT->getNumElements(), Idx: Indices[0]);
10038 }
10039
10040 return Success;
10041}
10042
10043bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
10044 if (E->getBase()->getType()->isSveVLSBuiltinType())
10045 return Error(E);
10046
10047 APSInt Index;
10048 bool Success = true;
10049
10050 if (const auto *VT = E->getBase()->getType()->getAs<VectorType>()) {
10051 APValue Val;
10052 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
10053 if (!Info.noteFailure())
10054 return false;
10055 Success = false;
10056 }
10057
10058 if (!EvaluateInteger(E: E->getIdx(), Result&: Index, Info)) {
10059 if (!Info.noteFailure())
10060 return false;
10061 Success = false;
10062 }
10063
10064 if (Success) {
10065 Result.setFrom(Ctx: Info.Ctx, V: Val);
10066 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
10067 Size: VT->getNumElements(), Idx: Index.getZExtValue());
10068 }
10069
10070 return Success;
10071 }
10072
10073 // C++17's rules require us to evaluate the LHS first, regardless of which
10074 // side is the base.
10075 for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) {
10076 if (SubExpr == E->getBase() ? !evaluatePointer(E: SubExpr, Result)
10077 : !EvaluateInteger(E: SubExpr, Result&: Index, Info)) {
10078 if (!Info.noteFailure())
10079 return false;
10080 Success = false;
10081 }
10082 }
10083
10084 return Success &&
10085 HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: E->getType(), Adjustment: Index);
10086}
10087
10088bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
10089 bool Success = evaluatePointer(E: E->getSubExpr(), Result);
10090 // [C++26][expr.unary.op]
10091 // If the operand points to an object or function, the result
10092 // denotes that object or function; otherwise, the behavior is undefined.
10093 // Because &(*(type*)0) is a common pattern, we do not fail the evaluation
10094 // immediately.
10095 if (!Success || !E->getType().getNonReferenceType()->isObjectType())
10096 return Success;
10097 return bool(findCompleteObject(Info, E, AK: AK_Dereference, LVal: Result,
10098 LValType: E->getType())) ||
10099 Info.noteUndefinedBehavior();
10100}
10101
10102bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
10103 if (!Visit(S: E->getSubExpr()))
10104 return false;
10105 // __real is a no-op on scalar lvalues.
10106 if (E->getSubExpr()->getType()->isAnyComplexType())
10107 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: false);
10108 return true;
10109}
10110
10111bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
10112 assert(E->getSubExpr()->getType()->isAnyComplexType() &&
10113 "lvalue __imag__ on scalar?");
10114 if (!Visit(S: E->getSubExpr()))
10115 return false;
10116 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: true);
10117 return true;
10118}
10119
10120bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) {
10121 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
10122 return Error(E: UO);
10123
10124 if (!this->Visit(S: UO->getSubExpr()))
10125 return false;
10126
10127 return handleIncDec(
10128 Info&: this->Info, E: UO, LVal: Result, LValType: UO->getSubExpr()->getType(),
10129 IsIncrement: UO->isIncrementOp(), Old: nullptr);
10130}
10131
10132bool LValueExprEvaluator::VisitCompoundAssignOperator(
10133 const CompoundAssignOperator *CAO) {
10134 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
10135 return Error(E: CAO);
10136
10137 bool Success = true;
10138
10139 // C++17 onwards require that we evaluate the RHS first.
10140 APValue RHS;
10141 if (!Evaluate(Result&: RHS, Info&: this->Info, E: CAO->getRHS())) {
10142 if (!Info.noteFailure())
10143 return false;
10144 Success = false;
10145 }
10146
10147 // The overall lvalue result is the result of evaluating the LHS.
10148 if (!this->Visit(S: CAO->getLHS()) || !Success)
10149 return false;
10150
10151 return handleCompoundAssignment(
10152 Info&: this->Info, E: CAO,
10153 LVal: Result, LValType: CAO->getLHS()->getType(), PromotedLValType: CAO->getComputationLHSType(),
10154 Opcode: CAO->getOpForCompoundAssignment(Opc: CAO->getOpcode()), RVal: RHS);
10155}
10156
10157bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
10158 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
10159 return Error(E);
10160
10161 bool Success = true;
10162
10163 // C++17 onwards require that we evaluate the RHS first.
10164 APValue NewVal;
10165 if (!Evaluate(Result&: NewVal, Info&: this->Info, E: E->getRHS())) {
10166 if (!Info.noteFailure())
10167 return false;
10168 Success = false;
10169 }
10170
10171 if (!this->Visit(S: E->getLHS()) || !Success)
10172 return false;
10173
10174 if (Info.getLangOpts().CPlusPlus20 &&
10175 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: E->getLHS(), LHS: Result))
10176 return false;
10177
10178 return handleAssignment(Info&: this->Info, E, LVal: Result, LValType: E->getLHS()->getType(),
10179 Val&: NewVal);
10180}
10181
10182//===----------------------------------------------------------------------===//
10183// Pointer Evaluation
10184//===----------------------------------------------------------------------===//
10185
10186/// Convenience function. LVal's base must be a call to an alloc_size
10187/// function.
10188static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
10189 const LValue &LVal,
10190 llvm::APInt &Result) {
10191 assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) &&
10192 "Can't get the size of a non alloc_size function");
10193 const auto *Base = LVal.getLValueBase().get<const Expr *>();
10194 const CallExpr *CE = tryUnwrapAllocSizeCall(E: Base);
10195 std::optional<llvm::APInt> Size =
10196 CE->evaluateBytesReturnedByAllocSizeCall(Ctx);
10197 if (!Size)
10198 return false;
10199
10200 Result = std::move(*Size);
10201 return true;
10202}
10203
10204/// Attempts to evaluate the given LValueBase as the result of a call to
10205/// a function with the alloc_size attribute. If it was possible to do so, this
10206/// function will return true, make Result's Base point to said function call,
10207/// and mark Result's Base as invalid.
10208static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base,
10209 LValue &Result) {
10210 if (Base.isNull())
10211 return false;
10212
10213 // Because we do no form of static analysis, we only support const variables.
10214 //
10215 // Additionally, we can't support parameters, nor can we support static
10216 // variables (in the latter case, use-before-assign isn't UB; in the former,
10217 // we have no clue what they'll be assigned to).
10218 const auto *VD =
10219 dyn_cast_or_null<VarDecl>(Val: Base.dyn_cast<const ValueDecl *>());
10220 if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified())
10221 return false;
10222
10223 const Expr *Init = VD->getAnyInitializer();
10224 if (!Init || Init->getType().isNull())
10225 return false;
10226
10227 const Expr *E = Init->IgnoreParens();
10228 if (!tryUnwrapAllocSizeCall(E))
10229 return false;
10230
10231 // Store E instead of E unwrapped so that the type of the LValue's base is
10232 // what the user wanted.
10233 Result.setInvalid(B: E);
10234
10235 QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType();
10236 Result.addUnsizedArray(Info, E, ElemTy: Pointee);
10237 return true;
10238}
10239
10240namespace {
10241class PointerExprEvaluator
10242 : public ExprEvaluatorBase<PointerExprEvaluator> {
10243 LValue &Result;
10244 bool InvalidBaseOK;
10245
10246 bool Success(const Expr *E) {
10247 Result.set(B: E);
10248 return true;
10249 }
10250
10251 bool evaluateLValue(const Expr *E, LValue &Result) {
10252 return EvaluateLValue(E, Result, Info, InvalidBaseOK);
10253 }
10254
10255 bool evaluatePointer(const Expr *E, LValue &Result) {
10256 return EvaluatePointer(E, Result, Info, InvalidBaseOK);
10257 }
10258
10259 bool visitNonBuiltinCallExpr(const CallExpr *E);
10260public:
10261
10262 PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK)
10263 : ExprEvaluatorBaseTy(info), Result(Result),
10264 InvalidBaseOK(InvalidBaseOK) {}
10265
10266 bool Success(const APValue &V, const Expr *E) {
10267 Result.setFrom(Ctx: Info.Ctx, V);
10268 return true;
10269 }
10270 bool ZeroInitialization(const Expr *E) {
10271 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10272 return true;
10273 }
10274
10275 bool VisitBinaryOperator(const BinaryOperator *E);
10276 bool VisitCastExpr(const CastExpr* E);
10277 bool VisitUnaryAddrOf(const UnaryOperator *E);
10278 bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
10279 { return Success(E); }
10280 bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
10281 if (E->isExpressibleAsConstantInitializer())
10282 return Success(E);
10283 if (Info.noteFailure())
10284 EvaluateIgnoredValue(Info, E: E->getSubExpr());
10285 return Error(E);
10286 }
10287 bool VisitObjCArrayLiteral(const ObjCArrayLiteral *E) {
10288 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
10289 }
10290 bool VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) {
10291 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
10292 }
10293 bool VisitAddrLabelExpr(const AddrLabelExpr *E)
10294 { return Success(E); }
10295 bool VisitCallExpr(const CallExpr *E);
10296 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
10297 bool VisitBlockExpr(const BlockExpr *E) {
10298 if (!E->getBlockDecl()->hasCaptures())
10299 return Success(E);
10300 return Error(E);
10301 }
10302 bool VisitCXXThisExpr(const CXXThisExpr *E) {
10303 auto DiagnoseInvalidUseOfThis = [&] {
10304 if (Info.getLangOpts().CPlusPlus11)
10305 Info.FFDiag(E, DiagId: diag::note_constexpr_this) << E->isImplicit();
10306 else
10307 Info.FFDiag(E);
10308 };
10309
10310 // Can't look at 'this' when checking a potential constant expression.
10311 if (Info.checkingPotentialConstantExpression())
10312 return false;
10313
10314 bool IsExplicitLambda =
10315 isLambdaCallWithExplicitObjectParameter(DC: Info.CurrentCall->Callee);
10316 if (!IsExplicitLambda) {
10317 if (!Info.CurrentCall->This) {
10318 DiagnoseInvalidUseOfThis();
10319 return false;
10320 }
10321
10322 Result = *Info.CurrentCall->This;
10323 }
10324
10325 if (isLambdaCallOperator(DC: Info.CurrentCall->Callee)) {
10326 // Ensure we actually have captured 'this'. If something was wrong with
10327 // 'this' capture, the error would have been previously reported.
10328 // Otherwise we can be inside of a default initialization of an object
10329 // declared by lambda's body, so no need to return false.
10330 if (!Info.CurrentCall->LambdaThisCaptureField) {
10331 if (IsExplicitLambda && !Info.CurrentCall->This) {
10332 DiagnoseInvalidUseOfThis();
10333 return false;
10334 }
10335
10336 return true;
10337 }
10338
10339 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
10340 return HandleLambdaCapture(
10341 Info, E, Result, MD, FD: Info.CurrentCall->LambdaThisCaptureField,
10342 LValueToRValueConversion: Info.CurrentCall->LambdaThisCaptureField->getType()->isPointerType());
10343 }
10344 return true;
10345 }
10346
10347 bool VisitCXXNewExpr(const CXXNewExpr *E);
10348
10349 bool VisitSourceLocExpr(const SourceLocExpr *E) {
10350 assert(!E->isIntType() && "SourceLocExpr isn't a pointer type?");
10351 APValue LValResult = E->EvaluateInContext(
10352 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
10353 Result.setFrom(Ctx: Info.Ctx, V: LValResult);
10354 return true;
10355 }
10356
10357 bool VisitEmbedExpr(const EmbedExpr *E) {
10358 llvm::report_fatal_error(reason: "Not yet implemented for ExprConstant.cpp");
10359 return true;
10360 }
10361
10362 bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) {
10363 std::string ResultStr = E->ComputeName(Context&: Info.Ctx);
10364
10365 QualType CharTy = Info.Ctx.CharTy.withConst();
10366 APInt Size(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType()),
10367 ResultStr.size() + 1);
10368 QualType ArrayTy = Info.Ctx.getConstantArrayType(
10369 EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
10370
10371 StringLiteral *SL =
10372 StringLiteral::Create(Ctx: Info.Ctx, Str: ResultStr, Kind: StringLiteralKind::Ordinary,
10373 /*Pascal*/ false, Ty: ArrayTy, Locs: E->getLocation());
10374
10375 evaluateLValue(E: SL, Result);
10376 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: ArrayTy));
10377 return true;
10378 }
10379
10380 // FIXME: Missing: @protocol, @selector
10381};
10382} // end anonymous namespace
10383
10384static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info,
10385 bool InvalidBaseOK) {
10386 assert(!E->isValueDependent());
10387 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
10388 return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
10389}
10390
10391bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
10392 if (E->getOpcode() != BO_Add &&
10393 E->getOpcode() != BO_Sub)
10394 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
10395
10396 const Expr *PExp = E->getLHS();
10397 const Expr *IExp = E->getRHS();
10398 if (IExp->getType()->isPointerType())
10399 std::swap(a&: PExp, b&: IExp);
10400
10401 bool EvalPtrOK = evaluatePointer(E: PExp, Result);
10402 if (!EvalPtrOK && !Info.noteFailure())
10403 return false;
10404
10405 llvm::APSInt Offset;
10406 if (!EvaluateInteger(E: IExp, Result&: Offset, Info) || !EvalPtrOK)
10407 return false;
10408
10409 if (E->getOpcode() == BO_Sub)
10410 negateAsSigned(Int&: Offset);
10411
10412 QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType();
10413 return HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: Pointee, Adjustment: Offset);
10414}
10415
10416bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
10417 // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*'
10418 // operator, neither operator is evaluated and the result is as if both were
10419 // omitted (except that the operators' constraints, already enforced by Sema,
10420 // still apply, and the result is not an lvalue). So '&*p' is just the pointer
10421 // value 'p' with no dereference, and forming it is therefore not undefined
10422 // behavior even when 'p' is null, e.g. '&*(int *)0'. Evaluate the pointer
10423 // operand directly so we don't spuriously diagnose a null dereference.
10424 if (!Info.getLangOpts().CPlusPlus) {
10425 const Expr *Sub = E->getSubExpr()->IgnoreParens();
10426 if (const auto *Deref = dyn_cast<UnaryOperator>(Val: Sub);
10427 Deref && Deref->getOpcode() == UO_Deref)
10428 return evaluatePointer(E: Deref->getSubExpr(), Result);
10429 }
10430 return evaluateLValue(E: E->getSubExpr(), Result);
10431}
10432
10433// Is the provided decl 'std::source_location::current'?
10434static bool IsDeclSourceLocationCurrent(const FunctionDecl *FD) {
10435 if (!FD)
10436 return false;
10437 const IdentifierInfo *FnII = FD->getIdentifier();
10438 if (!FnII || !FnII->isStr(Str: "current"))
10439 return false;
10440
10441 const auto *RD = dyn_cast<RecordDecl>(Val: FD->getParent());
10442 if (!RD)
10443 return false;
10444
10445 const IdentifierInfo *ClassII = RD->getIdentifier();
10446 return RD->isInStdNamespace() && ClassII && ClassII->isStr(Str: "source_location");
10447}
10448
10449bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
10450 const Expr *SubExpr = E->getSubExpr();
10451
10452 switch (E->getCastKind()) {
10453 default:
10454 break;
10455 case CK_BitCast:
10456 case CK_CPointerToObjCPointerCast:
10457 case CK_BlockPointerToObjCPointerCast:
10458 case CK_AnyPointerToBlockPointerCast:
10459 case CK_AddressSpaceConversion:
10460 if (!Visit(S: SubExpr))
10461 return false;
10462 if (E->getType()->isFunctionPointerType() ||
10463 SubExpr->getType()->isFunctionPointerType()) {
10464 // Casting between two function pointer types, or between a function
10465 // pointer and an object pointer, is always a reinterpret_cast.
10466 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10467 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10468 << Info.Ctx.getLangOpts().CPlusPlus;
10469 Result.Designator.setInvalid();
10470 } else if (!E->getType()->isVoidPointerType()) {
10471 // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
10472 // permitted in constant expressions in C++11. Bitcasts from cv void* are
10473 // also static_casts, but we disallow them as a resolution to DR1312.
10474 //
10475 // In some circumstances, we permit casting from void* to cv1 T*, when the
10476 // actual pointee object is actually a cv2 T.
10477 bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&
10478 !Result.IsNullPtr;
10479 bool VoidPtrCastMaybeOK =
10480 Result.IsNullPtr ||
10481 (HasValidResult &&
10482 Info.Ctx.hasSimilarType(T1: Result.Designator.getType(Ctx&: Info.Ctx),
10483 T2: E->getType()->getPointeeType()));
10484 // 1. We'll allow it in std::allocator::allocate, and anything which that
10485 // calls.
10486 // 2. HACK 2022-03-28: Work around an issue with libstdc++'s
10487 // <source_location> header. Fixed in GCC 12 and later (2022-04-??).
10488 // We'll allow it in the body of std::source_location::current. GCC's
10489 // implementation had a parameter of type `void*`, and casts from
10490 // that back to `const __impl*` in its body.
10491 if (VoidPtrCastMaybeOK &&
10492 (Info.getStdAllocatorCaller(FnName: "allocate") ||
10493 IsDeclSourceLocationCurrent(FD: Info.CurrentCall->Callee) ||
10494 Info.getLangOpts().CPlusPlus26)) {
10495 // Permitted.
10496 } else {
10497 if (SubExpr->getType()->isVoidPointerType() &&
10498 Info.getLangOpts().CPlusPlus) {
10499 if (HasValidResult)
10500 CCEDiag(E, D: diag::note_constexpr_invalid_void_star_cast)
10501 << SubExpr->getType() << Info.getLangOpts().CPlusPlus26
10502 << Result.Designator.getType(Ctx&: Info.Ctx).getCanonicalType()
10503 << E->getType()->getPointeeType();
10504 else
10505 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10506 << diag::ConstexprInvalidCastKind::CastFrom
10507 << SubExpr->getType();
10508 } else
10509 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10510 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10511 << Info.Ctx.getLangOpts().CPlusPlus;
10512 Result.Designator.setInvalid();
10513 }
10514 }
10515 if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr)
10516 ZeroInitialization(E);
10517 return true;
10518
10519 case CK_DerivedToBase:
10520 case CK_UncheckedDerivedToBase:
10521 if (!evaluatePointer(E: E->getSubExpr(), Result))
10522 return false;
10523 if (!Result.Base && Result.Offset.isZero())
10524 return true;
10525
10526 // Now figure out the necessary offset to add to the base LV to get from
10527 // the derived class to the base class.
10528 return HandleLValueBasePath(Info, E, Type: E->getSubExpr()->getType()->
10529 castAs<PointerType>()->getPointeeType(),
10530 Result);
10531
10532 case CK_BaseToDerived:
10533 if (!Visit(S: E->getSubExpr()))
10534 return false;
10535 if (!Result.Base && Result.Offset.isZero())
10536 return true;
10537 return HandleBaseToDerivedCast(Info, E, Result);
10538
10539 case CK_Dynamic:
10540 if (!Visit(S: E->getSubExpr()))
10541 return false;
10542 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
10543
10544 case CK_NullToPointer:
10545 VisitIgnoredValue(E: E->getSubExpr());
10546 return ZeroInitialization(E);
10547
10548 case CK_IntegralToPointer: {
10549 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10550 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10551 << Info.Ctx.getLangOpts().CPlusPlus;
10552
10553 APValue Value;
10554 if (!EvaluateIntegerOrLValue(E: SubExpr, Result&: Value, Info))
10555 break;
10556
10557 if (Value.isInt()) {
10558 unsigned Size = Info.Ctx.getTypeSize(T: E->getType());
10559 uint64_t N = Value.getInt().extOrTrunc(width: Size).getZExtValue();
10560 if (N == Info.Ctx.getTargetNullPointerValue(QT: E->getType())) {
10561 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10562 } else {
10563 Result.Base = (Expr *)nullptr;
10564 Result.InvalidBase = false;
10565 Result.Offset = CharUnits::fromQuantity(Quantity: N);
10566 Result.Designator.setInvalid();
10567 Result.IsNullPtr = false;
10568 }
10569 return true;
10570 } else {
10571 // In rare instances, the value isn't an lvalue.
10572 // For example, when the value is the difference between the addresses of
10573 // two labels. We reject that as a constant expression because we can't
10574 // compute a valid offset to convert into a pointer.
10575 if (!Value.isLValue())
10576 return false;
10577
10578 // Cast is of an lvalue, no need to change value.
10579 Result.setFrom(Ctx: Info.Ctx, V: Value);
10580 return true;
10581 }
10582 }
10583
10584 case CK_ArrayToPointerDecay: {
10585 if (SubExpr->isGLValue()) {
10586 if (!evaluateLValue(E: SubExpr, Result))
10587 return false;
10588 } else {
10589 APValue &Value = Info.CurrentCall->createTemporary(
10590 Key: SubExpr, T: SubExpr->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
10591 if (!EvaluateInPlace(Result&: Value, Info, This: Result, E: SubExpr))
10592 return false;
10593 }
10594 // The result is a pointer to the first element of the array.
10595 auto *AT = Info.Ctx.getAsArrayType(T: SubExpr->getType());
10596 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT))
10597 Result.addArray(Info, E, CAT);
10598 else
10599 Result.addUnsizedArray(Info, E, ElemTy: AT->getElementType());
10600 return true;
10601 }
10602
10603 case CK_FunctionToPointerDecay:
10604 return evaluateLValue(E: SubExpr, Result);
10605
10606 case CK_LValueToRValue: {
10607 LValue LVal;
10608 if (!evaluateLValue(E: E->getSubExpr(), Result&: LVal))
10609 return false;
10610
10611 APValue RVal;
10612 // Note, we use the subexpression's type in order to retain cv-qualifiers.
10613 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
10614 LVal, RVal))
10615 return InvalidBaseOK &&
10616 evaluateLValueAsAllocSize(Info, Base: LVal.Base, Result);
10617 return Success(V: RVal, E);
10618 }
10619 }
10620
10621 return ExprEvaluatorBaseTy::VisitCastExpr(E);
10622}
10623
10624static CharUnits GetAlignOfType(const ASTContext &Ctx, QualType T,
10625 UnaryExprOrTypeTrait ExprKind) {
10626 // C++ [expr.alignof]p3:
10627 // When alignof is applied to a reference type, the result is the
10628 // alignment of the referenced type.
10629 T = T.getNonReferenceType();
10630
10631 if (T.getQualifiers().hasUnaligned())
10632 return CharUnits::One();
10633
10634 const bool AlignOfReturnsPreferred =
10635 Ctx.getLangOpts().isCompatibleWith(Version: LangOptions::ClangABI::Ver7);
10636
10637 // __alignof is defined to return the preferred alignment.
10638 // Before 8, clang returned the preferred alignment for alignof and _Alignof
10639 // as well.
10640 if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
10641 return Ctx.toCharUnitsFromBits(BitSize: Ctx.getPreferredTypeAlign(T: T.getTypePtr()));
10642 // alignof and _Alignof are defined to return the ABI alignment.
10643 else if (ExprKind == UETT_AlignOf)
10644 return Ctx.getTypeAlignInChars(T: T.getTypePtr());
10645 else
10646 llvm_unreachable("GetAlignOfType on a non-alignment ExprKind");
10647}
10648
10649// Convert a builtin ID to the canonical x86 builtin ID the constant evaluators
10650// dispatch on in their x86 target-specific cases, or 0 if \p BuiltinOp is a
10651// target builtin those cases should not handle.
10652//
10653// Target-independent builtins are returned unchanged. Target builtin IDs of
10654// different targets overlap (each target numbers its builtins from
10655// Builtin::FirstTSBuiltin), so a target builtin ID is only meaningful for the
10656// target that owns it. Determine the owning target (translating an auxiliary ID
10657// back to its canonical value) and only return the ID when x86 owns it;
10658// otherwise an overlapping ID could be misinterpreted as an unrelated x86
10659// builtin.
10660unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
10661 unsigned BuiltinOp) {
10662 // Target-independent builtins have the same ID regardless of the target, so
10663 // they can be dispatched as-is. This is the common case and is intentionally
10664 // kept to a single comparison so callers can use this on hot paths (e.g. the
10665 // bytecode interpreter's builtin dispatch) without re-deriving the ID from
10666 // the call expression.
10667 if (BuiltinOp < Builtin::FirstTSBuiltin)
10668 return BuiltinOp;
10669
10670 // Determine the target that owns this builtin, translating an auxiliary ID
10671 // back to its canonical value.
10672 const TargetInfo *OwningTarget;
10673 if (Ctx.BuiltinInfo.isAuxBuiltinID(ID: BuiltinOp)) {
10674 OwningTarget = Ctx.getAuxTargetInfo();
10675 BuiltinOp = Ctx.BuiltinInfo.getAuxBuiltinID(ID: BuiltinOp);
10676 } else {
10677 OwningTarget = &Ctx.getTargetInfo();
10678 }
10679
10680 if (!OwningTarget)
10681 return 0;
10682
10683 // x86 and x86_64 share a single builtin set and are the only architectures
10684 // whose target-specific builtins the constant evaluators currently fold.
10685 switch (OwningTarget->getTriple().getArch()) {
10686 case llvm::Triple::x86:
10687 case llvm::Triple::x86_64:
10688 return BuiltinOp;
10689 default:
10690 return 0;
10691 }
10692}
10693
10694unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
10695 const CallExpr *E) {
10696 return ConvertBuiltinIDToX86BuiltinID(Ctx, BuiltinOp: E->getBuiltinCallee());
10697}
10698
10699CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E,
10700 UnaryExprOrTypeTrait ExprKind) {
10701 E = E->IgnoreParens();
10702
10703 // The kinds of expressions that we have special-case logic here for
10704 // should be kept up to date with the special checks for those
10705 // expressions in Sema.
10706
10707 // alignof decl is always accepted, even if it doesn't make sense: we default
10708 // to 1 in those cases.
10709 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E))
10710 return Ctx.getDeclAlign(D: DRE->getDecl(),
10711 /*ForAlignOf=*/ForAlignof: true);
10712
10713 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: E))
10714 return Ctx.getDeclAlign(D: ME->getMemberDecl(),
10715 /*ForAlignOf=*/ForAlignof: true);
10716
10717 return GetAlignOfType(Ctx, T: E->getType(), ExprKind);
10718}
10719
10720static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) {
10721 if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>())
10722 return Info.Ctx.getDeclAlign(D: VD);
10723 if (const auto *E = Value.Base.dyn_cast<const Expr *>())
10724 return GetAlignOfExpr(Ctx: Info.Ctx, E, ExprKind: UETT_AlignOf);
10725 if (const auto &DA = Value.Base.dyn_cast<DynamicAllocLValue>())
10726 return GetAlignOfDynamicAlloc(Ctx: Info.getASTContext(), AllocType: Value.Base.getType(),
10727 AllocKind: DA.getAllocKind());
10728 return GetAlignOfType(Ctx: Info.Ctx, T: Value.Base.getTypeInfoType(), ExprKind: UETT_AlignOf);
10729}
10730
10731/// Evaluate the value of the alignment argument to __builtin_align_{up,down},
10732/// __builtin_is_aligned and __builtin_assume_aligned.
10733static bool getAlignmentArgument(const Expr *E, QualType ForType,
10734 EvalInfo &Info, APSInt &Alignment) {
10735 if (!EvaluateInteger(E, Result&: Alignment, Info))
10736 return false;
10737 if (Alignment < 0 || !Alignment.isPowerOf2()) {
10738 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_alignment) << Alignment;
10739 return false;
10740 }
10741 unsigned SrcWidth = Info.Ctx.getIntWidth(T: ForType);
10742 APSInt MaxValue(APInt::getOneBitSet(numBits: SrcWidth, BitNo: SrcWidth - 1));
10743 if (APSInt::compareValues(I1: Alignment, I2: MaxValue) > 0) {
10744 Info.FFDiag(E, DiagId: diag::note_constexpr_alignment_too_big)
10745 << MaxValue << ForType << Alignment;
10746 return false;
10747 }
10748 // Ensure both alignment and source value have the same bit width so that we
10749 // don't assert when computing the resulting value.
10750 APSInt ExtAlignment =
10751 APSInt(Alignment.zextOrTrunc(width: SrcWidth), /*isUnsigned=*/true);
10752 assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 &&
10753 "Alignment should not be changed by ext/trunc");
10754 Alignment = ExtAlignment;
10755 assert(Alignment.getBitWidth() == SrcWidth);
10756 return true;
10757}
10758
10759// To be clear: this happily visits unsupported builtins. Better name welcomed.
10760bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {
10761 if (ExprEvaluatorBaseTy::VisitCallExpr(E))
10762 return true;
10763
10764 if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr()))
10765 return false;
10766
10767 Result.setInvalid(B: E);
10768 QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType();
10769 Result.addUnsizedArray(Info, E, ElemTy: PointeeTy);
10770 return true;
10771}
10772
10773bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
10774 if (!IsConstantEvaluatedBuiltinCall(E))
10775 return visitNonBuiltinCallExpr(E);
10776 return VisitBuiltinCallExpr(E, BuiltinOp: ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E));
10777}
10778
10779// Determine if T is a character type for which we guarantee that
10780// sizeof(T) == 1.
10781static bool isOneByteCharacterType(QualType T) {
10782 return T->isCharType() || T->isChar8Type();
10783}
10784
10785bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
10786 unsigned BuiltinOp) {
10787 if (isOpaqueConstantCall(E))
10788 return Success(E);
10789
10790 switch (BuiltinOp) {
10791 case Builtin::BIaddressof:
10792 case Builtin::BI__addressof:
10793 case Builtin::BI__builtin_addressof:
10794 return evaluateLValue(E: E->getArg(Arg: 0), Result);
10795 case Builtin::BI__builtin_assume_aligned: {
10796 // We need to be very careful here because: if the pointer does not have the
10797 // asserted alignment, then the behavior is undefined, and undefined
10798 // behavior is non-constant.
10799 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10800 return false;
10801
10802 LValue OffsetResult(Result);
10803 APSInt Alignment;
10804 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10805 Alignment))
10806 return false;
10807 CharUnits Align = CharUnits::fromQuantity(Quantity: Alignment.getZExtValue());
10808
10809 if (E->getNumArgs() > 2) {
10810 APSInt Offset;
10811 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Offset, Info))
10812 return false;
10813
10814 int64_t AdditionalOffset = -Offset.getZExtValue();
10815 OffsetResult.Offset += CharUnits::fromQuantity(Quantity: AdditionalOffset);
10816 }
10817
10818 // If there is a base object, then it must have the correct alignment.
10819 if (OffsetResult.Base) {
10820 CharUnits BaseAlignment = getBaseAlignment(Info, Value: OffsetResult);
10821
10822 if (BaseAlignment < Align) {
10823 Result.Designator.setInvalid();
10824 CCEDiag(E: E->getArg(Arg: 0), D: diag::note_constexpr_baa_insufficient_alignment)
10825 << 0 << BaseAlignment.getQuantity() << Align.getQuantity();
10826 return false;
10827 }
10828 }
10829
10830 // The offset must also have the correct alignment.
10831 if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) {
10832 Result.Designator.setInvalid();
10833
10834 (OffsetResult.Base
10835 ? CCEDiag(E: E->getArg(Arg: 0),
10836 D: diag::note_constexpr_baa_insufficient_alignment)
10837 << 1
10838 : CCEDiag(E: E->getArg(Arg: 0),
10839 D: diag::note_constexpr_baa_value_insufficient_alignment))
10840 << OffsetResult.Offset.getQuantity() << Align.getQuantity();
10841 return false;
10842 }
10843
10844 return true;
10845 }
10846 case Builtin::BI__builtin_align_up:
10847 case Builtin::BI__builtin_align_down: {
10848 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10849 return false;
10850 APSInt Alignment;
10851 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10852 Alignment))
10853 return false;
10854
10855 if (!Result.Base) {
10856 // Null pointers are always aligned and align_up/align_down preserve null.
10857 if (Result.Offset.isZero())
10858 return true;
10859
10860 // Non-null pointers without a base (for example, integer-to-pointer
10861 // casts such as (void *)32) do not have enough information to perform
10862 // pointer arithmetic during constant evaluation.
10863 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_adjust)
10864 << Alignment;
10865 return false;
10866 }
10867
10868 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Result);
10869 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Result.Offset);
10870 // For align_up/align_down, we can return the same value if the alignment
10871 // is known to be greater or equal to the requested value.
10872 if (PtrAlign.getQuantity() >= Alignment)
10873 return true;
10874
10875 // The alignment could be greater than the minimum at run-time, so we cannot
10876 // infer much about the resulting pointer value. One case is possible:
10877 // For `_Alignas(32) char buf[N]; __builtin_align_down(&buf[idx], 32)` we
10878 // can infer the correct index if the requested alignment is smaller than
10879 // the base alignment so we can perform the computation on the offset.
10880 if (BaseAlignment.getQuantity() >= Alignment) {
10881 assert(Alignment.getBitWidth() <= 64 &&
10882 "Cannot handle > 64-bit address-space");
10883 uint64_t Alignment64 = Alignment.getZExtValue();
10884 CharUnits NewOffset = CharUnits::fromQuantity(
10885 Quantity: BuiltinOp == Builtin::BI__builtin_align_down
10886 ? llvm::alignDown(Value: Result.Offset.getQuantity(), Align: Alignment64)
10887 : llvm::alignTo(Value: Result.Offset.getQuantity(), Align: Alignment64));
10888 Result.adjustOffset(N: NewOffset - Result.Offset);
10889 // TODO: diagnose out-of-bounds values/only allow for arrays?
10890 return true;
10891 }
10892 // Otherwise, we cannot constant-evaluate the result.
10893 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_adjust)
10894 << Alignment;
10895 return false;
10896 }
10897 case Builtin::BI__builtin_operator_new:
10898 return HandleOperatorNewCall(Info, E, Result);
10899 case Builtin::BI__builtin_launder:
10900 return evaluatePointer(E: E->getArg(Arg: 0), Result);
10901 case Builtin::BIstrchr:
10902 case Builtin::BIwcschr:
10903 case Builtin::BImemchr:
10904 case Builtin::BIwmemchr:
10905 if (Info.getLangOpts().CPlusPlus11)
10906 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10907 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10908 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10909 else
10910 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10911 [[fallthrough]];
10912 case Builtin::BI__builtin_strchr:
10913 case Builtin::BI__builtin_wcschr:
10914 case Builtin::BI__builtin_memchr:
10915 case Builtin::BI__builtin_char_memchr:
10916 case Builtin::BI__builtin_wmemchr: {
10917 if (!Visit(S: E->getArg(Arg: 0)))
10918 return false;
10919 APSInt Desired;
10920 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Desired, Info))
10921 return false;
10922 uint64_t MaxLength = uint64_t(-1);
10923 if (BuiltinOp != Builtin::BIstrchr &&
10924 BuiltinOp != Builtin::BIwcschr &&
10925 BuiltinOp != Builtin::BI__builtin_strchr &&
10926 BuiltinOp != Builtin::BI__builtin_wcschr) {
10927 APSInt N;
10928 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10929 return false;
10930 MaxLength = N.getZExtValue();
10931 }
10932 // We cannot find the value if there are no candidates to match against.
10933 if (MaxLength == 0u)
10934 return ZeroInitialization(E);
10935 if (!Result.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
10936 Result.Designator.Invalid)
10937 return false;
10938 QualType CharTy = Result.Designator.getType(Ctx&: Info.Ctx);
10939 bool IsRawByte = BuiltinOp == Builtin::BImemchr ||
10940 BuiltinOp == Builtin::BI__builtin_memchr;
10941 assert(IsRawByte ||
10942 Info.Ctx.hasSameUnqualifiedType(
10943 CharTy, E->getArg(0)->getType()->getPointeeType()));
10944 // Pointers to const void may point to objects of incomplete type.
10945 if (IsRawByte && CharTy->isIncompleteType()) {
10946 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_incomplete_type) << CharTy;
10947 return false;
10948 }
10949 // Give up on byte-oriented matching against multibyte elements.
10950 // FIXME: We can compare the bytes in the correct order.
10951 if (IsRawByte && !isOneByteCharacterType(T: CharTy)) {
10952 Info.FFDiag(E, DiagId: diag::note_constexpr_memchr_unsupported)
10953 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy;
10954 return false;
10955 }
10956 // Figure out what value we're actually looking for (after converting to
10957 // the corresponding unsigned type if necessary).
10958 uint64_t DesiredVal;
10959 bool StopAtNull = false;
10960 switch (BuiltinOp) {
10961 case Builtin::BIstrchr:
10962 case Builtin::BI__builtin_strchr:
10963 StopAtNull = true;
10964 [[fallthrough]];
10965 case Builtin::BImemchr:
10966 case Builtin::BI__builtin_memchr:
10967 case Builtin::BI__builtin_char_memchr:
10968 // memchr compares by converting both sides to unsigned char. That's also
10969 // correct for strchr if we get this far (to cope with plain char being
10970 // unsigned in the strchr case).
10971 DesiredVal = Desired.trunc(width: Info.Ctx.getCharWidth()).getZExtValue();
10972 break;
10973
10974 case Builtin::BIwcschr:
10975 case Builtin::BI__builtin_wcschr:
10976 StopAtNull = true;
10977 [[fallthrough]];
10978 case Builtin::BIwmemchr:
10979 case Builtin::BI__builtin_wmemchr:
10980 // wcschr and wmemchr are given a wchar_t to look for. Just use it.
10981 DesiredVal = Desired.getZExtValue();
10982 break;
10983 }
10984
10985 for (; MaxLength; --MaxLength) {
10986 APValue Char;
10987 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: Result, RVal&: Char) ||
10988 !Char.isInt())
10989 return false;
10990 if (Char.getInt().getZExtValue() == DesiredVal)
10991 return true;
10992 if (StopAtNull && !Char.getInt())
10993 break;
10994 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: CharTy, Adjustment: 1))
10995 return false;
10996 }
10997 // Not found: return nullptr.
10998 return ZeroInitialization(E);
10999 }
11000
11001 case Builtin::BImemcpy:
11002 case Builtin::BImemmove:
11003 case Builtin::BIwmemcpy:
11004 case Builtin::BIwmemmove:
11005 if (Info.getLangOpts().CPlusPlus11)
11006 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
11007 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
11008 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
11009 else
11010 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
11011 [[fallthrough]];
11012 case Builtin::BI__builtin_memcpy:
11013 case Builtin::BI__builtin_memmove:
11014 case Builtin::BI__builtin_wmemcpy:
11015 case Builtin::BI__builtin_wmemmove: {
11016 bool WChar = BuiltinOp == Builtin::BIwmemcpy ||
11017 BuiltinOp == Builtin::BIwmemmove ||
11018 BuiltinOp == Builtin::BI__builtin_wmemcpy ||
11019 BuiltinOp == Builtin::BI__builtin_wmemmove;
11020 bool Move = BuiltinOp == Builtin::BImemmove ||
11021 BuiltinOp == Builtin::BIwmemmove ||
11022 BuiltinOp == Builtin::BI__builtin_memmove ||
11023 BuiltinOp == Builtin::BI__builtin_wmemmove;
11024
11025 // The result of mem* is the first argument.
11026 if (!Visit(S: E->getArg(Arg: 0)))
11027 return false;
11028 LValue Dest = Result;
11029
11030 LValue Src;
11031 if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Src, Info))
11032 return false;
11033
11034 APSInt N;
11035 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
11036 return false;
11037 assert(!N.isSigned() && "memcpy and friends take an unsigned size");
11038
11039 // If the size is zero, we treat this as always being a valid no-op.
11040 // (Even if one of the src and dest pointers is null.)
11041 if (!N)
11042 return true;
11043
11044 // Otherwise, if either of the operands is null, we can't proceed. Don't
11045 // try to determine the type of the copied objects, because there aren't
11046 // any.
11047 if (!Src.Base || !Dest.Base) {
11048 APValue Val;
11049 (!Src.Base ? Src : Dest).moveInto(V&: Val);
11050 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_null)
11051 << Move << WChar << !!Src.Base
11052 << Val.getAsString(Ctx: Info.Ctx, Ty: E->getArg(Arg: 0)->getType());
11053 return false;
11054 }
11055 if (Src.Designator.Invalid || Dest.Designator.Invalid)
11056 return false;
11057
11058 // We require that Src and Dest are both pointers to arrays of
11059 // trivially-copyable type. (For the wide version, the designator will be
11060 // invalid if the designated object is not a wchar_t.)
11061 QualType T = Dest.Designator.getType(Ctx&: Info.Ctx);
11062 QualType SrcT = Src.Designator.getType(Ctx&: Info.Ctx);
11063 if (!Info.Ctx.hasSameUnqualifiedType(T1: T, T2: SrcT)) {
11064 // FIXME: Consider using our bit_cast implementation to support this.
11065 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T;
11066 return false;
11067 }
11068 if (T->isIncompleteType()) {
11069 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_incomplete_type) << Move << T;
11070 return false;
11071 }
11072 if (!T.isTriviallyCopyableType(Context: Info.Ctx)) {
11073 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_nontrivial) << Move << T;
11074 return false;
11075 }
11076
11077 // Figure out how many T's we're copying.
11078 uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity();
11079 if (TSize == 0)
11080 return false;
11081 if (!WChar) {
11082 uint64_t Remainder;
11083 llvm::APInt OrigN = N;
11084 llvm::APInt::udivrem(LHS: OrigN, RHS: TSize, Quotient&: N, Remainder);
11085 if (Remainder) {
11086 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
11087 << Move << WChar << 0 << T << toString(I: OrigN, Radix: 10, /*Signed*/false)
11088 << (unsigned)TSize;
11089 return false;
11090 }
11091 }
11092
11093 // Check that the copying will remain within the arrays, just so that we
11094 // can give a more meaningful diagnostic. This implicitly also checks that
11095 // N fits into 64 bits.
11096 uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second;
11097 uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second;
11098 if (N.ugt(RHS: RemainingSrcSize) || N.ugt(RHS: RemainingDestSize)) {
11099 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
11100 << Move << WChar << (N.ugt(RHS: RemainingSrcSize) ? 1 : 2) << T
11101 << toString(I: N, Radix: 10, /*Signed*/false);
11102 return false;
11103 }
11104 uint64_t NElems = N.getZExtValue();
11105 uint64_t NBytes = NElems * TSize;
11106
11107 // Check for overlap.
11108 int Direction = 1;
11109 if (HasSameBase(A: Src, B: Dest)) {
11110 uint64_t SrcOffset = Src.getLValueOffset().getQuantity();
11111 uint64_t DestOffset = Dest.getLValueOffset().getQuantity();
11112 if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) {
11113 // Dest is inside the source region.
11114 if (!Move) {
11115 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
11116 return false;
11117 }
11118 // For memmove and friends, copy backwards.
11119 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: NElems - 1) ||
11120 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: NElems - 1))
11121 return false;
11122 Direction = -1;
11123 } else if (!Move && SrcOffset >= DestOffset &&
11124 SrcOffset - DestOffset < NBytes) {
11125 // Src is inside the destination region for memcpy: invalid.
11126 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
11127 return false;
11128 }
11129 }
11130
11131 while (true) {
11132 APValue Val;
11133 // FIXME: Set WantObjectRepresentation to true if we're copying a
11134 // char-like type?
11135 if (!handleLValueToRValueConversion(Info, Conv: E, Type: T, LVal: Src, RVal&: Val) ||
11136 !handleAssignment(Info, E, LVal: Dest, LValType: T, Val))
11137 return false;
11138 // Do not iterate past the last element; if we're copying backwards, that
11139 // might take us off the start of the array.
11140 if (--NElems == 0)
11141 return true;
11142 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: Direction) ||
11143 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: Direction))
11144 return false;
11145 }
11146 }
11147
11148 default:
11149 return false;
11150 }
11151}
11152
11153static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
11154 APValue &Result, const InitListExpr *ILE,
11155 QualType AllocType);
11156static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
11157 APValue &Result,
11158 const CXXConstructExpr *CCE,
11159 QualType AllocType);
11160
11161bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {
11162 if (!Info.getLangOpts().CPlusPlus20)
11163 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
11164
11165 // We cannot speculatively evaluate a delete expression.
11166 if (Info.SpeculativeEvaluationDepth)
11167 return false;
11168
11169 FunctionDecl *OperatorNew = E->getOperatorNew();
11170 QualType AllocType = E->getAllocatedType();
11171 QualType TargetType = AllocType;
11172
11173 bool IsNothrow = false;
11174 bool IsPlacement = false;
11175
11176 // The only new-placement list we support (other than the reserved placement
11177 // form) is of the form (std::nothrow).
11178 //
11179 // FIXME: There is no restriction on this, but it's not clear that any
11180 // other form makes any sense. We get here for cases such as:
11181 //
11182 // new (std::align_val_t{N}) X(int)
11183 //
11184 // (which should presumably be valid only if N is a multiple of
11185 // alignof(int), and in any case can't be deallocated unless N is
11186 // alignof(X) and X has new-extended alignment).
11187 bool HasNothrowArg = E->getNumPlacementArgs() == 1 &&
11188 E->getPlacementArg(I: 0)->getType()->isNothrowT();
11189
11190 if (OperatorNew->isReservedGlobalPlacementOperator()) {
11191 if (Info.CurrentCall->isStdFunction() || Info.getLangOpts().CPlusPlus26 ||
11192 (Info.CurrentCall->CanEvalMSConstexpr &&
11193 OperatorNew->hasAttr<MSConstexprAttr>())) {
11194 if (!EvaluatePointer(E: E->getPlacementArg(I: 0), Result, Info))
11195 return false;
11196 if (Result.Designator.Invalid)
11197 return false;
11198 TargetType = E->getPlacementArg(I: 0)->getType();
11199 IsPlacement = true;
11200 } else {
11201 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
11202 << /*C++26 feature*/ 1 << E->getSourceRange();
11203 return false;
11204 }
11205 } else if (E->getNumPlacementArgs() && !HasNothrowArg) {
11206 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
11207 << /*Unsupported*/ 0 << E->getSourceRange();
11208 return false;
11209 } else if (!OperatorNew
11210 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
11211 // [expr.const] only permits new-expressions that select a replaceable
11212 // global allocation function. Check this before evaluating a
11213 // (std::nothrow) placement argument.
11214 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
11215 << isa<CXXMethodDecl>(Val: OperatorNew) << OperatorNew;
11216 return false;
11217 } else if (HasNothrowArg) {
11218 LValue Nothrow;
11219 if (!EvaluateLValue(E: E->getPlacementArg(I: 0), Result&: Nothrow, Info))
11220 return false;
11221 IsNothrow = true;
11222 }
11223
11224 const Expr *Init = E->getInitializer();
11225 const InitListExpr *ResizedArrayILE = nullptr;
11226 const CXXConstructExpr *ResizedArrayCCE = nullptr;
11227 bool ValueInit = false;
11228
11229 if (std::optional<const Expr *> ArraySize = E->getArraySize()) {
11230 const Expr *Stripped = *ArraySize;
11231 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped);
11232 Stripped = ICE->getSubExpr())
11233 if (ICE->getCastKind() != CK_NoOp &&
11234 ICE->getCastKind() != CK_IntegralCast)
11235 break;
11236
11237 llvm::APSInt ArrayBound;
11238 if (!EvaluateInteger(E: Stripped, Result&: ArrayBound, Info))
11239 return false;
11240
11241 // C++ [expr.new]p9:
11242 // The expression is erroneous if:
11243 // -- [...] its value before converting to size_t [or] applying the
11244 // second standard conversion sequence is less than zero
11245 if (ArrayBound.isSigned() && ArrayBound.isNegative()) {
11246 if (IsNothrow)
11247 return ZeroInitialization(E);
11248
11249 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_negative)
11250 << ArrayBound << (*ArraySize)->getSourceRange();
11251 return false;
11252 }
11253
11254 // -- its value is such that the size of the allocated object would
11255 // exceed the implementation-defined limit
11256 if (!Info.CheckArraySize(Loc: ArraySize.value()->getExprLoc(),
11257 BitWidth: ConstantArrayType::getNumAddressingBits(
11258 Context: Info.Ctx, ElementType: AllocType, NumElements: ArrayBound),
11259 ElemCount: ArrayBound.getZExtValue(), /*Diag=*/!IsNothrow)) {
11260 if (IsNothrow)
11261 return ZeroInitialization(E);
11262 return false;
11263 }
11264
11265 // -- the new-initializer is a braced-init-list and the number of
11266 // array elements for which initializers are provided [...]
11267 // exceeds the number of elements to initialize
11268 if (!Init) {
11269 // No initialization is performed.
11270 } else if (isa<CXXScalarValueInitExpr>(Val: Init) ||
11271 isa<ImplicitValueInitExpr>(Val: Init)) {
11272 ValueInit = true;
11273 } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Val: Init)) {
11274 ResizedArrayCCE = CCE;
11275 } else {
11276 auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType());
11277 assert(CAT && "unexpected type for array initializer");
11278
11279 unsigned Bits =
11280 std::max(a: CAT->getSizeBitWidth(), b: ArrayBound.getBitWidth());
11281 llvm::APInt InitBound = CAT->getSize().zext(width: Bits);
11282 llvm::APInt AllocBound = ArrayBound.zext(width: Bits);
11283 if (InitBound.ugt(RHS: AllocBound)) {
11284 if (IsNothrow)
11285 return ZeroInitialization(E);
11286
11287 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_too_small)
11288 << toString(I: AllocBound, Radix: 10, /*Signed=*/false)
11289 << toString(I: InitBound, Radix: 10, /*Signed=*/false)
11290 << (*ArraySize)->getSourceRange();
11291 return false;
11292 }
11293
11294 // If the sizes differ, we must have an initializer list, and we need
11295 // special handling for this case when we initialize.
11296 if (InitBound != AllocBound)
11297 ResizedArrayILE = cast<InitListExpr>(Val: Init);
11298 }
11299
11300 AllocType = Info.Ctx.getConstantArrayType(EltTy: AllocType, ArySize: ArrayBound, SizeExpr: nullptr,
11301 ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
11302 } else if (E->isArray()) {
11303 // We have an array new-expression whose array size could not be
11304 // determined, e.g. 'new int[]()', where the bound is neither given nor
11305 // deducible from the initializer. This is ill-formed and already
11306 // diagnosed, so bail out rather than mis-evaluating a scalar allocation
11307 // as an array (which would later crash the evaluator).
11308 return false;
11309 } else {
11310 assert(!AllocType->isArrayType() &&
11311 "array allocation with non-array new");
11312 }
11313
11314 APValue *Val;
11315 if (IsPlacement) {
11316 AccessKinds AK = AK_Construct;
11317 struct FindObjectHandler {
11318 EvalInfo &Info;
11319 const Expr *E;
11320 QualType AllocType;
11321 const AccessKinds AccessKind;
11322 APValue *Value;
11323
11324 typedef bool result_type;
11325 bool failed() { return false; }
11326 bool checkConst(QualType QT) {
11327 if (QT.isConstQualified()) {
11328 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
11329 return false;
11330 }
11331 return true;
11332 }
11333 bool found(APValue &Subobj, QualType SubobjType,
11334 APValue::LValueBase Base) {
11335 if (!checkConst(QT: SubobjType))
11336 return false;
11337 // FIXME: Reject the cases where [basic.life]p8 would not permit the
11338 // old name of the object to be used to name the new object.
11339 if (!Info.Ctx.hasSimilarType(T1: SubobjType, T2: AllocType)) {
11340 Info.FFDiag(E, DiagId: diag::note_constexpr_placement_new_wrong_type)
11341 << SubobjType << AllocType;
11342 return false;
11343 }
11344 Value = &Subobj;
11345 return true;
11346 }
11347 bool found(APSInt &Value, QualType SubobjType) {
11348 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
11349 return false;
11350 }
11351 bool found(APFloat &Value, QualType SubobjType) {
11352 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
11353 return false;
11354 }
11355 } Handler = {.Info: Info, .E: E, .AllocType: AllocType, .AccessKind: AK, .Value: nullptr};
11356
11357 if (AllocType->isArrayType() &&
11358 Result.Designator.MostDerivedIsArrayElement &&
11359 Result.Designator.Entries.back().getAsArrayIndex() == 0) {
11360 // The destination of placement new is pointing to the first element
11361 // of an array. There's a special case in [expr.const]: "[...] if T is an
11362 // array type, to the first element of such an object [...]". Handle
11363 // that case here by dropping the last entry in the designator list.
11364 QualType AllocElementType =
11365 Info.Ctx.getAsArrayType(T: AllocType)->getElementType();
11366 if (Info.Ctx.hasSimilarType(T1: AllocElementType,
11367 T2: Result.Designator.MostDerivedType)) {
11368 Result.Designator.truncate(Ctx&: Info.Ctx, Base: Result.Base,
11369 NewLength: Result.Designator.MostDerivedPathLength - 1);
11370 }
11371 }
11372
11373 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: Result, LValType: AllocType);
11374 if (!Obj || !findSubobject(Info, E, Obj, Sub: Result.Designator, handler&: Handler))
11375 return false;
11376
11377 Val = Handler.Value;
11378
11379 // [basic.life]p1:
11380 // The lifetime of an object o of type T ends when [...] the storage
11381 // which the object occupies is [...] reused by an object that is not
11382 // nested within o (6.6.2).
11383 *Val = APValue();
11384 } else {
11385 // Perform the allocation and obtain a pointer to the resulting object.
11386 Val = Info.createHeapAlloc(E, T: AllocType, LV&: Result);
11387 if (!Val)
11388 return false;
11389 }
11390
11391 if (ValueInit) {
11392 ImplicitValueInitExpr VIE(AllocType);
11393 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: &VIE))
11394 return false;
11395 } else if (ResizedArrayILE) {
11396 if (!EvaluateArrayNewInitList(Info, This&: Result, Result&: *Val, ILE: ResizedArrayILE,
11397 AllocType))
11398 return false;
11399 } else if (ResizedArrayCCE) {
11400 if (!EvaluateArrayNewConstructExpr(Info, This&: Result, Result&: *Val, CCE: ResizedArrayCCE,
11401 AllocType))
11402 return false;
11403 } else if (Init) {
11404 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: Init))
11405 return false;
11406 } else if (!handleDefaultInitValue(T: AllocType, Result&: *Val)) {
11407 return false;
11408 }
11409
11410 // Array new returns a pointer to the first element, not a pointer to the
11411 // array.
11412 if (auto *AT = AllocType->getAsArrayTypeUnsafe())
11413 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val: AT));
11414
11415 return true;
11416}
11417
11418//===----------------------------------------------------------------------===//
11419// Reflection expression evaluation
11420//===----------------------------------------------------------------------===//
11421
11422namespace {
11423class ReflectionEvaluator : public ExprEvaluatorBase<ReflectionEvaluator> {
11424
11425 using BaseType = ExprEvaluatorBase<ReflectionEvaluator>;
11426
11427 APValue &Result;
11428
11429public:
11430 ReflectionEvaluator(EvalInfo &E, APValue &Result)
11431 : ExprEvaluatorBaseTy(E), Result(Result) {}
11432
11433 bool Success(const APValue &V, const Expr *E) {
11434 Result = V;
11435 return true;
11436 }
11437
11438 bool VisitCXXReflectExpr(const CXXReflectExpr *E);
11439 bool ZeroInitialization(const Expr *E);
11440};
11441
11442bool ReflectionEvaluator::VisitCXXReflectExpr(const CXXReflectExpr *E) {
11443 switch (E->getKind()) {
11444 case ReflectionKind::Null: {
11445 assert(false && "null reflection can't be constructed from parsing a "
11446 "reflection operand");
11447 return false;
11448 }
11449 case ReflectionKind::Type: {
11450 APValue ReflectionValue(ReflectionKind::Type, E->getOpaqueValue());
11451 return Success(V: ReflectionValue, E);
11452 }
11453 }
11454 assert(false && "unknown or unimplemented reflection entities");
11455 return false;
11456}
11457
11458bool ReflectionEvaluator::ZeroInitialization(const Expr *E) {
11459 Result = APValue(ReflectionKind::Null, /*Operand=*/nullptr);
11460 return true;
11461}
11462
11463} // end anonymous namespace
11464
11465static bool EvaluateReflection(const Expr *E, APValue &Result, EvalInfo &Info) {
11466 assert(E->isPRValue() && E->getType()->isMetaInfoType());
11467 return ReflectionEvaluator(Info, Result).Visit(S: E);
11468}
11469
11470static bool EvaluateReflectionMetafunctionBuiltin(EvalInfo &Info,
11471 const CallExpr *E,
11472 unsigned BuiltinOp,
11473 APValue &Result) {
11474 switch (BuiltinOp) {
11475 case Builtin::BI__builtin_meta_is_type: {
11476 APValue Reflection;
11477 if (!EvaluateReflection(E: E->getArg(Arg: 0), Result&: Reflection, Info))
11478 return false;
11479 Result = APValue(Info.Ctx.MakeIntValue(
11480 Value: Reflection.getReflectionOperandKind() == ReflectionKind::Type,
11481 Type: E->getType()));
11482 return true;
11483 }
11484 default:
11485 llvm_unreachable("unknown metafunction builtin");
11486 }
11487}
11488
11489//===----------------------------------------------------------------------===//
11490// Member Pointer Evaluation
11491//===----------------------------------------------------------------------===//
11492
11493namespace {
11494class MemberPointerExprEvaluator
11495 : public ExprEvaluatorBase<MemberPointerExprEvaluator> {
11496 MemberPtr &Result;
11497
11498 bool Success(const ValueDecl *D) {
11499 Result = MemberPtr(D);
11500 return true;
11501 }
11502public:
11503
11504 MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
11505 : ExprEvaluatorBaseTy(Info), Result(Result) {}
11506
11507 bool Success(const APValue &V, const Expr *E) {
11508 Result.setFrom(V);
11509 return true;
11510 }
11511 bool ZeroInitialization(const Expr *E) {
11512 return Success(D: (const ValueDecl*)nullptr);
11513 }
11514
11515 bool VisitCastExpr(const CastExpr *E);
11516 bool VisitUnaryAddrOf(const UnaryOperator *E);
11517};
11518} // end anonymous namespace
11519
11520static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
11521 EvalInfo &Info) {
11522 assert(!E->isValueDependent());
11523 assert(E->isPRValue() && E->getType()->isMemberPointerType());
11524 return MemberPointerExprEvaluator(Info, Result).Visit(S: E);
11525}
11526
11527bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
11528 switch (E->getCastKind()) {
11529 default:
11530 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11531
11532 case CK_NullToMemberPointer:
11533 VisitIgnoredValue(E: E->getSubExpr());
11534 return ZeroInitialization(E);
11535
11536 case CK_BaseToDerivedMemberPointer: {
11537 if (!Visit(S: E->getSubExpr()))
11538 return false;
11539 if (E->path_empty())
11540 return true;
11541 // Base-to-derived member pointer casts store the path in derived-to-base
11542 // order, so iterate backwards. The CXXBaseSpecifier also provides us with
11543 // the wrong end of the derived->base arc, so stagger the path by one class.
11544 typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
11545 for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
11546 PathI != PathE; ++PathI) {
11547 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11548 const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
11549 if (!Result.castToDerived(Derived))
11550 return Error(E);
11551 }
11552 if (!Result.castToDerived(Derived: E->getType()
11553 ->castAs<MemberPointerType>()
11554 ->getMostRecentCXXRecordDecl()))
11555 return Error(E);
11556 return true;
11557 }
11558
11559 case CK_DerivedToBaseMemberPointer:
11560 if (!Visit(S: E->getSubExpr()))
11561 return false;
11562 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11563 PathE = E->path_end(); PathI != PathE; ++PathI) {
11564 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11565 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11566 if (!Result.castToBase(Base))
11567 return Error(E);
11568 }
11569 return true;
11570 }
11571}
11572
11573bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
11574 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
11575 // member can be formed.
11576 return Success(D: cast<DeclRefExpr>(Val: E->getSubExpr())->getDecl());
11577}
11578
11579//===----------------------------------------------------------------------===//
11580// Record Evaluation
11581//===----------------------------------------------------------------------===//
11582
11583namespace {
11584 class RecordExprEvaluator
11585 : public ExprEvaluatorBase<RecordExprEvaluator> {
11586 const LValue &This;
11587 APValue &Result;
11588 public:
11589
11590 RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
11591 : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
11592
11593 bool Success(const APValue &V, const Expr *E) {
11594 Result = V;
11595 return true;
11596 }
11597 bool ZeroInitialization(const Expr *E) {
11598 return ZeroInitialization(E, T: E->getType());
11599 }
11600 bool ZeroInitialization(const Expr *E, QualType T);
11601
11602 bool VisitCallExpr(const CallExpr *E) {
11603 return handleCallExpr(E, Result, ResultSlot: &This);
11604 }
11605 bool VisitCastExpr(const CastExpr *E);
11606 bool VisitInitListExpr(const InitListExpr *E);
11607 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
11608 return VisitCXXConstructExpr(E, T: E->getType());
11609 }
11610 bool VisitLambdaExpr(const LambdaExpr *E);
11611 bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E);
11612 bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T);
11613 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E);
11614 bool VisitBinCmp(const BinaryOperator *E);
11615 bool VisitTypeTraitExpr(const TypeTraitExpr *E);
11616 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
11617 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
11618 ArrayRef<Expr *> Args);
11619 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
11620 };
11621}
11622
11623/// Perform zero-initialization on an object of non-union class type.
11624/// C++11 [dcl.init]p5:
11625/// To zero-initialize an object or reference of type T means:
11626/// [...]
11627/// -- if T is a (possibly cv-qualified) non-union class type,
11628/// each non-static data member and each base-class subobject is
11629/// zero-initialized
11630static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
11631 const RecordDecl *RD,
11632 const LValue &This, APValue &Result,
11633 bool IsCompleteClass = true) {
11634 assert(!RD->isUnion() && "Expected non-union class type");
11635 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD);
11636
11637 if (CD) {
11638 unsigned NonVirtualBases = countNonVirtualBases(RD: CD);
11639 Result =
11640 APValue(APValue::UninitStruct(), NonVirtualBases, RD->getNumFields(),
11641 IsCompleteClass ? CD->getNumVBases() : 0);
11642 } else {
11643 Result = APValue(APValue::UninitStruct(), 0, RD->getNumFields());
11644 }
11645
11646 if (RD->isInvalidDecl()) return false;
11647 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11648
11649 if (CD) {
11650 unsigned Index = 0;
11651
11652 for (const auto &B : CD->bases()) {
11653 if (B.isVirtual())
11654 continue;
11655 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
11656 LValue Subobject = This;
11657 if (!HandleLValueDirectBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11658 return false;
11659 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11660 Result&: Result.getStructBase(i: Index),
11661 /*IsCompleteClass=*/false))
11662 return false;
11663 ++Index;
11664 }
11665 }
11666
11667 for (const auto *I : RD->fields()) {
11668 // -- if T is a reference type, no initialization is performed.
11669 if (I->isUnnamedBitField() || I->getType()->isReferenceType())
11670 continue;
11671
11672 LValue Subobject = This;
11673 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: I, RL: &Layout))
11674 return false;
11675
11676 ImplicitValueInitExpr VIE(I->getType());
11677 if (!EvaluateInPlace(
11678 Result&: Result.getStructField(i: I->getFieldIndex()), Info, This: Subobject, E: &VIE))
11679 return false;
11680 }
11681
11682 if (CD && This.pointsToCompleteClass(D: CD)) {
11683 unsigned Index = 0;
11684 for (const auto &B : CD->vbases()) {
11685 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
11686 LValue Subobject = This;
11687 if (!HandleLValueDirectVirtualBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11688 return false;
11689 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11690 Result&: Result.getStructVirtualBase(i: Index),
11691 /*IsCompleteClass=*/false))
11692 return false;
11693 ++Index;
11694 }
11695 }
11696
11697 return true;
11698}
11699
11700bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) {
11701 const auto *RD = T->castAsRecordDecl();
11702 if (RD->isInvalidDecl()) return false;
11703 if (RD->isUnion()) {
11704 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
11705 // object's first non-static named data member is zero-initialized
11706 RecordDecl::field_iterator I = RD->field_begin();
11707 while (I != RD->field_end() && (*I)->isUnnamedBitField())
11708 ++I;
11709 if (I == RD->field_end()) {
11710 Result = APValue((const FieldDecl*)nullptr);
11711 return true;
11712 }
11713
11714 LValue Subobject = This;
11715 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: *I))
11716 return false;
11717 Result = APValue(*I);
11718 ImplicitValueInitExpr VIE(I->getType());
11719 return EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: &VIE);
11720 }
11721
11722 if (!Info.getLangOpts().CPlusPlus26) {
11723 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11724 CXXRD && CXXRD->getNumVBases()) {
11725 Info.FFDiag(E, DiagId: diag::note_constexpr_virtual_base) << RD;
11726 return false;
11727 }
11728 }
11729
11730 return HandleClassZeroInitialization(Info, E, RD, This, Result);
11731}
11732
11733bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
11734 switch (E->getCastKind()) {
11735 default:
11736 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11737
11738 case CK_ConstructorConversion:
11739 return Visit(S: E->getSubExpr());
11740
11741 case CK_DerivedToBase:
11742 case CK_UncheckedDerivedToBase: {
11743 APValue DerivedObject;
11744 if (!Evaluate(Result&: DerivedObject, Info, E: E->getSubExpr()))
11745 return false;
11746 if (!DerivedObject.isStruct())
11747 return Error(E: E->getSubExpr());
11748
11749 // Derived-to-base rvalue conversion: just slice off the derived part.
11750 APValue *Value = &DerivedObject;
11751 const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
11752 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11753 PathE = E->path_end(); PathI != PathE; ++PathI) {
11754 assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
11755 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11756 Value = &Value->getStructBase(i: getBaseIndex(Derived: RD, Base));
11757 RD = Base;
11758 }
11759 Result = *Value;
11760 return true;
11761 }
11762 case CK_HLSLAggregateSplatCast: {
11763 APValue Val;
11764 QualType ValTy;
11765
11766 if (!hlslAggSplatHelper(Info, E: E->getSubExpr(), SrcVal&: Val, SrcTy&: ValTy))
11767 return false;
11768
11769 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
11770 // splat our Val
11771 SmallVector<APValue> SplatEls(NEls, Val);
11772 SmallVector<QualType> SplatType(NEls, ValTy);
11773
11774 // cast the elements and construct our struct result
11775 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11776 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
11777 ElTypes&: SplatType))
11778 return false;
11779
11780 return true;
11781 }
11782 case CK_HLSLElementwiseCast: {
11783 SmallVector<APValue> SrcEls;
11784 SmallVector<QualType> SrcTypes;
11785
11786 if (!hlslElementwiseCastHelper(Info, E: E->getSubExpr(), DestTy: E->getType(), SrcVals&: SrcEls,
11787 SrcTypes))
11788 return false;
11789
11790 // cast the elements and construct our struct result
11791 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11792 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
11793 ElTypes&: SrcTypes))
11794 return false;
11795
11796 return true;
11797 }
11798 case CK_ToUnion: {
11799 const FieldDecl *Field = E->getTargetUnionField();
11800 LValue Subobject = This;
11801 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field))
11802 return false;
11803 Result = APValue(Field);
11804 if (!EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject,
11805 E: E->getSubExpr()))
11806 return false;
11807 if (Field->isBitField()) {
11808 if (!truncateBitfieldValue(Info, E: E->getSubExpr(), Value&: Result.getUnionValue(),
11809 FD: Field))
11810 return false;
11811 }
11812 return true;
11813 }
11814 }
11815}
11816
11817bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
11818 if (E->isTransparent())
11819 return Visit(S: E->getInit(Init: 0));
11820 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits());
11821}
11822
11823bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr(
11824 const Expr *ExprToVisit, ArrayRef<Expr *> Args) {
11825 const auto *RD = ExprToVisit->getType()->castAsRecordDecl();
11826 if (RD->isInvalidDecl()) return false;
11827 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11828 auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11829
11830 EvalInfo::EvaluatingConstructorRAII EvalObj(
11831 Info,
11832 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
11833 CXXRD && CXXRD->getNumBases());
11834
11835 if (RD->isUnion()) {
11836 const FieldDecl *Field;
11837 if (auto *ILE = dyn_cast<InitListExpr>(Val: ExprToVisit)) {
11838 Field = ILE->getInitializedFieldInUnion();
11839 } else if (auto *PLIE = dyn_cast<CXXParenListInitExpr>(Val: ExprToVisit)) {
11840 Field = PLIE->getInitializedFieldInUnion();
11841 } else {
11842 llvm_unreachable(
11843 "Expression is neither an init list nor a C++ paren list");
11844 }
11845
11846 Result = APValue(Field);
11847 if (!Field)
11848 return true;
11849
11850 // If the initializer list for a union does not contain any elements, the
11851 // first element of the union is value-initialized.
11852 // FIXME: The element should be initialized from an initializer list.
11853 // Is this difference ever observable for initializer lists which
11854 // we don't build?
11855 ImplicitValueInitExpr VIE(Field->getType());
11856 const Expr *InitExpr = Args.empty() ? &VIE : Args[0];
11857
11858 LValue Subobject = This;
11859 if (!HandleLValueMember(Info, E: InitExpr, LVal&: Subobject, FD: Field, RL: &Layout))
11860 return false;
11861
11862 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11863 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11864 isa<CXXDefaultInitExpr>(Val: InitExpr));
11865
11866 if (EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: InitExpr)) {
11867 if (Field->isBitField())
11868 return truncateBitfieldValue(Info, E: InitExpr, Value&: Result.getUnionValue(),
11869 FD: Field);
11870 return true;
11871 }
11872
11873 return false;
11874 }
11875
11876 if (!Result.hasValue())
11877 Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0,
11878 RD->getNumFields());
11879 unsigned ElementNo = 0;
11880 bool Success = true;
11881
11882 // Initialize base classes.
11883 if (CXXRD && CXXRD->getNumBases()) {
11884 for (const auto &Base : CXXRD->bases()) {
11885 assert(ElementNo < Args.size() && "missing init for base class");
11886 const Expr *Init = Args[ElementNo];
11887
11888 LValue Subobject = This;
11889 if (!HandleLValueBase(Info, E: Init, Obj&: Subobject, DerivedDecl: CXXRD, Base: &Base))
11890 return false;
11891
11892 APValue &FieldVal = Result.getStructBase(i: ElementNo);
11893 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init)) {
11894 if (!Info.noteFailure())
11895 return false;
11896 Success = false;
11897 }
11898 ++ElementNo;
11899 }
11900
11901 EvalObj.finishedConstructingBases();
11902 }
11903
11904 // Initialize members.
11905 for (const auto *Field : RD->fields()) {
11906 // Anonymous bit-fields are not considered members of the class for
11907 // purposes of aggregate initialization.
11908 if (Field->isUnnamedBitField())
11909 continue;
11910
11911 LValue Subobject = This;
11912
11913 bool HaveInit = ElementNo < Args.size();
11914
11915 // FIXME: Diagnostics here should point to the end of the initializer
11916 // list, not the start.
11917 if (!HandleLValueMember(Info, E: HaveInit ? Args[ElementNo] : ExprToVisit,
11918 LVal&: Subobject, FD: Field, RL: &Layout))
11919 return false;
11920
11921 // Perform an implicit value-initialization for members beyond the end of
11922 // the initializer list.
11923 ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
11924 const Expr *Init = HaveInit ? Args[ElementNo++] : &VIE;
11925
11926 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
11927 // aren't supposed to be modified.
11928 if (isa<NoInitExpr>(Val: Init))
11929 continue;
11930
11931 if (Field->getType()->isIncompleteArrayType()) {
11932 if (auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType())) {
11933 if (!CAT->isZeroSize()) {
11934 // Bail out for now. This might sort of "work", but the rest of the
11935 // code isn't really prepared to handle it.
11936 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_unsupported_flexible_array);
11937 return false;
11938 }
11939 }
11940 }
11941
11942 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11943 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11944 isa<CXXDefaultInitExpr>(Val: Init));
11945
11946 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
11947 if (Field->getType()->isReferenceType()) {
11948 LValue Result;
11949 if (!EvaluateInitForDeclOfReferenceType(Info, D: Field, Init, Result,
11950 Val&: FieldVal)) {
11951 if (!Info.noteFailure())
11952 return false;
11953 Success = false;
11954 }
11955 } else if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init) ||
11956 (Field->isBitField() &&
11957 !truncateBitfieldValue(Info, E: Init, Value&: FieldVal, FD: Field))) {
11958 if (!Info.noteFailure())
11959 return false;
11960 Success = false;
11961 }
11962 }
11963
11964 EvalObj.finishedConstructingFields();
11965
11966 return Success;
11967}
11968
11969bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
11970 QualType T) {
11971 // Note that E's type is not necessarily the type of our class here; we might
11972 // be initializing an array element instead.
11973 const CXXConstructorDecl *FD = E->getConstructor();
11974 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false;
11975
11976 bool ZeroInit = E->requiresZeroInitialization();
11977 if (CheckTrivialDefaultConstructor(Info, Loc: E->getExprLoc(), CD: FD, IsValueInitialization: ZeroInit)) {
11978 if (ZeroInit)
11979 return ZeroInitialization(E, T);
11980
11981 return handleDefaultInitValue(T, Result);
11982 }
11983
11984 TryInstantiateFunctionBeforeCall(FD, Info, Loc: E->getBeginLoc());
11985
11986 const FunctionDecl *Definition = nullptr;
11987 auto Body = FD->getBody(Definition);
11988
11989 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11990 return false;
11991
11992 // Avoid materializing a temporary for an elidable copy/move constructor.
11993 if (E->isElidable() && !ZeroInit) {
11994 // FIXME: This only handles the simplest case, where the source object
11995 // is passed directly as the first argument to the constructor.
11996 // This should also handle stepping though implicit casts and
11997 // and conversion sequences which involve two steps, with a
11998 // conversion operator followed by a converting constructor.
11999 const Expr *SrcObj = E->getArg(Arg: 0);
12000 assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent()));
12001 assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType()));
12002 if (const MaterializeTemporaryExpr *ME =
12003 dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj))
12004 return Visit(S: ME->getSubExpr());
12005 }
12006
12007 if (ZeroInit && !ZeroInitialization(E, T))
12008 return false;
12009
12010 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
12011 return HandleConstructorCall(E, This, Args,
12012 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
12013 Result);
12014}
12015
12016bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr(
12017 const CXXInheritedCtorInitExpr *E) {
12018 if (!Info.CurrentCall) {
12019 assert(Info.checkingPotentialConstantExpression());
12020 return false;
12021 }
12022
12023 const CXXConstructorDecl *FD = E->getConstructor();
12024 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl())
12025 return false;
12026
12027 TryInstantiateFunctionBeforeCall(FD, Info, Loc: E->getBeginLoc());
12028
12029 const FunctionDecl *Definition = nullptr;
12030 auto Body = FD->getBody(Definition);
12031
12032 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
12033 return false;
12034
12035 return HandleConstructorCall(E, This, Call: Info.CurrentCall->Arguments,
12036 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
12037 Result);
12038}
12039
12040bool RecordExprEvaluator::VisitCXXStdInitializerListExpr(
12041 const CXXStdInitializerListExpr *E) {
12042 const ConstantArrayType *ArrayType =
12043 Info.Ctx.getAsConstantArrayType(T: E->getSubExpr()->getType());
12044
12045 LValue Array;
12046 if (!EvaluateLValue(E: E->getSubExpr(), Result&: Array, Info))
12047 return false;
12048
12049 assert(ArrayType && "unexpected type for array initializer");
12050
12051 // Get a pointer to the first element of the array.
12052 Array.addArray(Info, E, CAT: ArrayType);
12053
12054 // FIXME: What if the initializer_list type has base classes, etc?
12055 Result = APValue(APValue::UninitStruct(), 0, 2);
12056 Array.moveInto(V&: Result.getStructField(i: 0));
12057
12058 auto *Record = E->getType()->castAsRecordDecl();
12059 RecordDecl::field_iterator Field = Record->field_begin();
12060 assert(Field != Record->field_end() &&
12061 Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
12062 ArrayType->getElementType()) &&
12063 "Expected std::initializer_list first field to be const E *");
12064 ++Field;
12065 assert(Field != Record->field_end() &&
12066 "Expected std::initializer_list to have two fields");
12067
12068 if (Info.Ctx.hasSameType(T1: Field->getType(), T2: Info.Ctx.getSizeType())) {
12069 // Length.
12070 Result.getStructField(i: 1) = APValue(APSInt(ArrayType->getSize()));
12071 } else {
12072 // End pointer.
12073 assert(Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
12074 ArrayType->getElementType()) &&
12075 "Expected std::initializer_list second field to be const E *");
12076 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Array,
12077 EltTy: ArrayType->getElementType(),
12078 Adjustment: ArrayType->getZExtSize()))
12079 return false;
12080 Array.moveInto(V&: Result.getStructField(i: 1));
12081 }
12082
12083 assert(++Field == Record->field_end() &&
12084 "Expected std::initializer_list to only have two fields");
12085
12086 return true;
12087}
12088
12089bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {
12090 const CXXRecordDecl *ClosureClass = E->getLambdaClass();
12091 if (ClosureClass->isInvalidDecl())
12092 return false;
12093
12094 const size_t NumFields = ClosureClass->getNumFields();
12095
12096 assert(NumFields == (size_t)std::distance(E->capture_init_begin(),
12097 E->capture_init_end()) &&
12098 "The number of lambda capture initializers should equal the number of "
12099 "fields within the closure type");
12100
12101 Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields);
12102 // Iterate through all the lambda's closure object's fields and initialize
12103 // them.
12104 auto *CaptureInitIt = E->capture_init_begin();
12105 bool Success = true;
12106 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: ClosureClass);
12107 for (const auto *Field : ClosureClass->fields()) {
12108 assert(CaptureInitIt != E->capture_init_end());
12109 // Get the initializer for this field
12110 Expr *const CurFieldInit = *CaptureInitIt++;
12111
12112 // If there is no initializer, either this is a VLA or an error has
12113 // occurred.
12114 if (!CurFieldInit || CurFieldInit->containsErrors())
12115 return Error(E);
12116
12117 LValue Subobject = This;
12118
12119 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field, RL: &Layout))
12120 return false;
12121
12122 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
12123 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: CurFieldInit)) {
12124 if (!Info.keepEvaluatingAfterFailure())
12125 return false;
12126 Success = false;
12127 }
12128 }
12129 return Success;
12130}
12131
12132bool RecordExprEvaluator::VisitDesignatedInitUpdateExpr(
12133 const DesignatedInitUpdateExpr *E) {
12134 if (!Visit(S: E->getBase()))
12135 return false;
12136 return Visit(S: E->getUpdater());
12137}
12138
12139static bool EvaluateRecord(const Expr *E, const LValue &This,
12140 APValue &Result, EvalInfo &Info) {
12141 assert(!E->isValueDependent());
12142 assert(E->isPRValue() && E->getType()->isRecordType() &&
12143 "can't evaluate expression as a record rvalue");
12144 return RecordExprEvaluator(Info, This, Result).Visit(S: E);
12145}
12146
12147//===----------------------------------------------------------------------===//
12148// Temporary Evaluation
12149//
12150// Temporaries are represented in the AST as rvalues, but generally behave like
12151// lvalues. The full-object of which the temporary is a subobject is implicitly
12152// materialized so that a reference can bind to it.
12153//===----------------------------------------------------------------------===//
12154namespace {
12155class TemporaryExprEvaluator
12156 : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
12157public:
12158 TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
12159 LValueExprEvaluatorBaseTy(Info, Result, false) {}
12160
12161 /// Visit an expression which constructs the value of this temporary.
12162 bool VisitConstructExpr(const Expr *E) {
12163 APValue &Value = Info.CurrentCall->createTemporary(
12164 Key: E, T: E->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
12165 return EvaluateInPlace(Result&: Value, Info, This: Result, E);
12166 }
12167
12168 bool VisitCastExpr(const CastExpr *E) {
12169 switch (E->getCastKind()) {
12170 default:
12171 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
12172
12173 case CK_ConstructorConversion:
12174 return VisitConstructExpr(E: E->getSubExpr());
12175 }
12176 }
12177 bool VisitInitListExpr(const InitListExpr *E) {
12178 return VisitConstructExpr(E);
12179 }
12180 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
12181 return VisitConstructExpr(E);
12182 }
12183 bool VisitCallExpr(const CallExpr *E) {
12184 return VisitConstructExpr(E);
12185 }
12186 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) {
12187 return VisitConstructExpr(E);
12188 }
12189 bool VisitLambdaExpr(const LambdaExpr *E) {
12190 return VisitConstructExpr(E);
12191 }
12192};
12193} // end anonymous namespace
12194
12195/// Evaluate an expression of record type as a temporary.
12196static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
12197 assert(!E->isValueDependent());
12198 assert(E->isPRValue() && E->getType()->isRecordType());
12199 return TemporaryExprEvaluator(Info, Result).Visit(S: E);
12200}
12201
12202//===----------------------------------------------------------------------===//
12203// Vector Evaluation
12204//===----------------------------------------------------------------------===//
12205
12206namespace {
12207 class VectorExprEvaluator
12208 : public ExprEvaluatorBase<VectorExprEvaluator> {
12209 APValue &Result;
12210 public:
12211
12212 VectorExprEvaluator(EvalInfo &info, APValue &Result)
12213 : ExprEvaluatorBaseTy(info), Result(Result) {}
12214
12215 bool Success(ArrayRef<APValue> V, const Expr *E) {
12216 assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
12217 // FIXME: remove this APValue copy.
12218 Result = APValue(V.data(), V.size());
12219 return true;
12220 }
12221 bool Success(const APValue &V, const Expr *E) {
12222 assert(V.isVector());
12223 Result = V;
12224 return true;
12225 }
12226 bool ZeroInitialization(const Expr *E);
12227
12228 bool VisitUnaryReal(const UnaryOperator *E)
12229 { return Visit(S: E->getSubExpr()); }
12230 bool VisitCastExpr(const CastExpr* E);
12231 bool VisitInitListExpr(const InitListExpr *E);
12232 bool VisitUnaryImag(const UnaryOperator *E);
12233 bool VisitBinaryOperator(const BinaryOperator *E);
12234 bool VisitUnaryOperator(const UnaryOperator *E);
12235 bool VisitCallExpr(const CallExpr *E);
12236 bool VisitConvertVectorExpr(const ConvertVectorExpr *E);
12237 bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E);
12238
12239 // FIXME: Missing: conditional operator (for GNU
12240 // conditional select), ExtVectorElementExpr
12241 };
12242} // end anonymous namespace
12243
12244static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
12245 assert(E->isPRValue() && E->getType()->isVectorType() &&
12246 "not a vector prvalue");
12247 return VectorExprEvaluator(Info, Result).Visit(S: E);
12248}
12249
12250static llvm::APInt ConvertBoolVectorToInt(const APValue &Val) {
12251 assert(Val.isVector() && "expected vector APValue");
12252 unsigned NumElts = Val.getVectorLength();
12253
12254 // Each element is one bit, so create an integer with NumElts bits.
12255 llvm::APInt Result(NumElts, 0);
12256
12257 for (unsigned I = 0; I < NumElts; ++I) {
12258 const APValue &Elt = Val.getVectorElt(I);
12259 assert(Elt.isInt() && "expected integer element in bool vector");
12260
12261 if (Elt.getInt().getBoolValue())
12262 Result.setBit(I);
12263 }
12264
12265 return Result;
12266}
12267
12268bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) {
12269 const VectorType *VTy = E->getType()->castAs<VectorType>();
12270 unsigned NElts = VTy->getNumElements();
12271
12272 const Expr *SE = E->getSubExpr();
12273 QualType SETy = SE->getType();
12274
12275 switch (E->getCastKind()) {
12276 case CK_VectorSplat: {
12277 APValue Val = APValue();
12278 if (SETy->isIntegerType()) {
12279 APSInt IntResult;
12280 if (!EvaluateInteger(E: SE, Result&: IntResult, Info))
12281 return false;
12282 Val = APValue(std::move(IntResult));
12283 } else if (SETy->isRealFloatingType()) {
12284 APFloat FloatResult(0.0);
12285 if (!EvaluateFloat(E: SE, Result&: FloatResult, Info))
12286 return false;
12287 Val = APValue(std::move(FloatResult));
12288 } else {
12289 return Error(E);
12290 }
12291
12292 // Splat and create vector APValue.
12293 SmallVector<APValue, 4> Elts(NElts, Val);
12294 return Success(V: Elts, E);
12295 }
12296 case CK_BitCast: {
12297 APValue SVal;
12298 if (!Evaluate(Result&: SVal, Info, E: SE))
12299 return false;
12300
12301 if (!SVal.isInt() && !SVal.isFloat() && !SVal.isVector()) {
12302 // Give up if the input isn't an int, float, or vector. For example, we
12303 // reject "(v4i16)(intptr_t)&a".
12304 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_cast)
12305 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
12306 << Info.Ctx.getLangOpts().CPlusPlus;
12307 return false;
12308 }
12309
12310 if (!handleRValueToRValueBitCast(Info, DestValue&: Result, SourceRValue: SVal, BCE: E))
12311 return false;
12312
12313 return true;
12314 }
12315 case CK_HLSLVectorTruncation: {
12316 APValue Val;
12317 SmallVector<APValue, 4> Elements;
12318 if (!EvaluateVector(E: SE, Result&: Val, Info))
12319 return Error(E);
12320 for (unsigned I = 0; I < NElts; I++)
12321 Elements.push_back(Elt: Val.getVectorElt(I));
12322 return Success(V: Elements, E);
12323 }
12324 case CK_HLSLMatrixTruncation: {
12325 // Matrix truncation occurs in row-major order.
12326 APValue Val;
12327 if (!EvaluateMatrix(E: SE, Result&: Val, Info))
12328 return Error(E);
12329 SmallVector<APValue, 16> Elements;
12330 for (unsigned Row = 0;
12331 Row < Val.getMatrixNumRows() && Elements.size() < NElts; Row++)
12332 for (unsigned Col = 0;
12333 Col < Val.getMatrixNumColumns() && Elements.size() < NElts; Col++)
12334 Elements.push_back(Elt: Val.getMatrixElt(Row, Col));
12335 return Success(V: Elements, E);
12336 }
12337 case CK_HLSLAggregateSplatCast: {
12338 APValue Val;
12339 QualType ValTy;
12340
12341 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
12342 return false;
12343
12344 // cast our Val once.
12345 APValue Result;
12346 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12347 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: VTy->getElementType(), Original: Val,
12348 Result))
12349 return false;
12350
12351 SmallVector<APValue, 4> SplatEls(NElts, Result);
12352 return Success(V: SplatEls, E);
12353 }
12354 case CK_HLSLElementwiseCast: {
12355 SmallVector<APValue> SrcVals;
12356 SmallVector<QualType> SrcTypes;
12357
12358 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
12359 return false;
12360
12361 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12362 SmallVector<QualType, 4> DestTypes(NElts, VTy->getElementType());
12363 SmallVector<APValue, 4> ResultEls(NElts);
12364 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
12365 Results&: ResultEls))
12366 return false;
12367 return Success(V: ResultEls, E);
12368 }
12369 case CK_IntegralToFloating:
12370 case CK_FloatingToIntegral:
12371 case CK_IntegralCast:
12372 case CK_FloatingCast:
12373 case CK_FloatingToBoolean:
12374 case CK_IntegralToBoolean: {
12375 // These casts apply element-wise when the source is a vector type.
12376 assert(SETy->isVectorType() && "expected vector source type");
12377 APValue SrcVal;
12378 if (!EvaluateVector(E: SE, Result&: SrcVal, Info))
12379 return Error(E);
12380
12381 assert(SrcVal.getVectorLength() == NElts);
12382 QualType SrcEltTy = SETy->castAs<VectorType>()->getElementType();
12383 QualType DstEltTy = VTy->getElementType();
12384 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12385
12386 SmallVector<APValue, 4> ResultEls(NElts);
12387 for (unsigned I = 0; I < NElts; ++I) {
12388 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcEltTy, DestTy: DstEltTy,
12389 Original: SrcVal.getVectorElt(I), Result&: ResultEls[I]))
12390 return Error(E);
12391 }
12392 return Success(V: ResultEls, E);
12393 }
12394 default:
12395 return ExprEvaluatorBaseTy::VisitCastExpr(E);
12396 }
12397}
12398
12399bool
12400VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
12401 const VectorType *VT = E->getType()->castAs<VectorType>();
12402 unsigned NumInits = E->getNumInits();
12403 unsigned NumElements = VT->getNumElements();
12404
12405 QualType EltTy = VT->getElementType();
12406 SmallVector<APValue, 4> Elements;
12407
12408 // MFloat8 type doesn't have constants and thus constant folding
12409 // is impossible.
12410 if (EltTy->isMFloat8Type())
12411 return false;
12412
12413 // The number of initializers can be less than the number of
12414 // vector elements. For OpenCL, this can be due to nested vector
12415 // initialization. For GCC compatibility, missing trailing elements
12416 // should be initialized with zeroes.
12417 unsigned CountInits = 0, CountElts = 0;
12418 while (CountElts < NumElements) {
12419 // Handle nested vector initialization.
12420 if (CountInits < NumInits
12421 && E->getInit(Init: CountInits)->getType()->isVectorType()) {
12422 APValue v;
12423 if (!EvaluateVector(E: E->getInit(Init: CountInits), Result&: v, Info))
12424 return Error(E);
12425 unsigned vlen = v.getVectorLength();
12426 for (unsigned j = 0; j < vlen; j++)
12427 Elements.push_back(Elt: v.getVectorElt(I: j));
12428 CountElts += vlen;
12429 } else if (EltTy->isIntegerType()) {
12430 llvm::APSInt sInt(32);
12431 if (CountInits < NumInits) {
12432 if (!EvaluateInteger(E: E->getInit(Init: CountInits), Result&: sInt, Info))
12433 return false;
12434 } else // trailing integer zero.
12435 sInt = Info.Ctx.MakeIntValue(Value: 0, Type: EltTy);
12436 Elements.push_back(Elt: APValue(sInt));
12437 CountElts++;
12438 } else {
12439 llvm::APFloat f(0.0);
12440 if (CountInits < NumInits) {
12441 if (!EvaluateFloat(E: E->getInit(Init: CountInits), Result&: f, Info))
12442 return false;
12443 } else // trailing float zero.
12444 f = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy));
12445 Elements.push_back(Elt: APValue(f));
12446 CountElts++;
12447 }
12448 CountInits++;
12449 }
12450 return Success(V: Elements, E);
12451}
12452
12453bool
12454VectorExprEvaluator::ZeroInitialization(const Expr *E) {
12455 const auto *VT = E->getType()->castAs<VectorType>();
12456 QualType EltTy = VT->getElementType();
12457 APValue ZeroElement;
12458 if (EltTy->isIntegerType())
12459 ZeroElement = APValue(Info.Ctx.MakeIntValue(Value: 0, Type: EltTy));
12460 else
12461 ZeroElement =
12462 APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy)));
12463
12464 SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
12465 return Success(V: Elements, E);
12466}
12467
12468bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
12469 VisitIgnoredValue(E: E->getSubExpr());
12470 return ZeroInitialization(E);
12471}
12472
12473bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
12474 BinaryOperatorKind Op = E->getOpcode();
12475 assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp &&
12476 "Operation not supported on vector types");
12477
12478 if (Op == BO_Comma)
12479 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
12480
12481 Expr *LHS = E->getLHS();
12482 Expr *RHS = E->getRHS();
12483
12484 [[maybe_unused]] QualType LHSType = LHS->getType().getAtomicUnqualifiedType();
12485 [[maybe_unused]] QualType RHSType = RHS->getType().getAtomicUnqualifiedType();
12486 assert(LHSType->isVectorType() && RHSType->isVectorType() &&
12487 "Must both be vector types");
12488 // Checking JUST the types are the same would be fine, except shifts don't
12489 // need to have their types be the same (since you always shift by an int).
12490 assert(LHSType->castAs<VectorType>()->getNumElements() ==
12491 E->getType()->castAs<VectorType>()->getNumElements() &&
12492 RHSType->castAs<VectorType>()->getNumElements() ==
12493 E->getType()->castAs<VectorType>()->getNumElements() &&
12494 "All operands must be the same size.");
12495
12496 APValue LHSValue;
12497 APValue RHSValue;
12498 bool LHSOK = Evaluate(Result&: LHSValue, Info, E: LHS);
12499 if (!LHSOK && !Info.noteFailure())
12500 return false;
12501 if (!Evaluate(Result&: RHSValue, Info, E: RHS) || !LHSOK)
12502 return false;
12503
12504 if (!handleVectorVectorBinOp(Info, E, Opcode: Op, LHSValue, RHSValue))
12505 return false;
12506
12507 return Success(V: LHSValue, E);
12508}
12509
12510static std::optional<APValue> handleVectorUnaryOperator(ASTContext &Ctx,
12511 QualType ResultTy,
12512 UnaryOperatorKind Op,
12513 APValue Elt) {
12514 switch (Op) {
12515 case UO_Plus:
12516 // Nothing to do here.
12517 return Elt;
12518 case UO_Minus:
12519 if (Elt.getKind() == APValue::Int) {
12520 Elt.getInt().negate();
12521 } else {
12522 assert(Elt.getKind() == APValue::Float &&
12523 "Vector can only be int or float type");
12524 Elt.getFloat().changeSign();
12525 }
12526 return Elt;
12527 case UO_Not:
12528 // This is only valid for integral types anyway, so we don't have to handle
12529 // float here.
12530 assert(Elt.getKind() == APValue::Int &&
12531 "Vector operator ~ can only be int");
12532 Elt.getInt().flipAllBits();
12533 return Elt;
12534 case UO_LNot: {
12535 if (Elt.getKind() == APValue::Int) {
12536 Elt.getInt() = !Elt.getInt();
12537 // operator ! on vectors returns -1 for 'truth', so negate it.
12538 Elt.getInt().negate();
12539 return Elt;
12540 }
12541 assert(Elt.getKind() == APValue::Float &&
12542 "Vector can only be int or float type");
12543 // Float types result in an int of the same size, but -1 for true, or 0 for
12544 // false.
12545 APSInt EltResult{Ctx.getIntWidth(T: ResultTy),
12546 ResultTy->isUnsignedIntegerType()};
12547 if (Elt.getFloat().isZero())
12548 EltResult.setAllBits();
12549 else
12550 EltResult.clearAllBits();
12551
12552 return APValue{EltResult};
12553 }
12554 default:
12555 // FIXME: Implement the rest of the unary operators.
12556 return std::nullopt;
12557 }
12558}
12559
12560bool VectorExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
12561 Expr *SubExpr = E->getSubExpr();
12562 const auto *VD = SubExpr->getType()->castAs<VectorType>();
12563 // This result element type differs in the case of negating a floating point
12564 // vector, since the result type is the a vector of the equivilant sized
12565 // integer.
12566 const QualType ResultEltTy = VD->getElementType();
12567 UnaryOperatorKind Op = E->getOpcode();
12568
12569 APValue SubExprValue;
12570 if (!Evaluate(Result&: SubExprValue, Info, E: SubExpr))
12571 return false;
12572
12573 // FIXME: This vector evaluator someday needs to be changed to be LValue
12574 // aware/keep LValue information around, rather than dealing with just vector
12575 // types directly. Until then, we cannot handle cases where the operand to
12576 // these unary operators is an LValue. The only case I've been able to see
12577 // cause this is operator++ assigning to a member expression (only valid in
12578 // altivec compilations) in C mode, so this shouldn't limit us too much.
12579 if (SubExprValue.isLValue())
12580 return false;
12581
12582 assert(SubExprValue.getVectorLength() == VD->getNumElements() &&
12583 "Vector length doesn't match type?");
12584
12585 SmallVector<APValue, 4> ResultElements;
12586 for (unsigned EltNum = 0; EltNum < VD->getNumElements(); ++EltNum) {
12587 std::optional<APValue> Elt = handleVectorUnaryOperator(
12588 Ctx&: Info.Ctx, ResultTy: ResultEltTy, Op, Elt: SubExprValue.getVectorElt(I: EltNum));
12589 if (!Elt)
12590 return false;
12591 ResultElements.push_back(Elt: *Elt);
12592 }
12593 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12594}
12595
12596static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO,
12597 const Expr *E, QualType SourceTy,
12598 QualType DestTy, APValue const &Original,
12599 APValue &Result) {
12600 if (SourceTy->isIntegerType()) {
12601 if (DestTy->isRealFloatingType()) {
12602 Result = APValue(APFloat(0.0));
12603 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
12604 DestType: DestTy, Result&: Result.getFloat());
12605 }
12606 if (DestTy->isIntegerType()) {
12607 Result = APValue(
12608 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
12609 return true;
12610 }
12611 } else if (SourceTy->isRealFloatingType()) {
12612 if (DestTy->isRealFloatingType()) {
12613 Result = Original;
12614 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
12615 Result&: Result.getFloat());
12616 }
12617 if (DestTy->isIntegerType()) {
12618 Result = APValue(APSInt());
12619 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
12620 DestType: DestTy, Result&: Result.getInt());
12621 }
12622 }
12623
12624 Info.FFDiag(E, DiagId: diag::err_convertvector_constexpr_unsupported_vector_cast)
12625 << SourceTy << DestTy;
12626 return false;
12627}
12628
12629static bool evalPackBuiltin(const CallExpr *E, EvalInfo &Info, APValue &Result,
12630 llvm::function_ref<APInt(const APSInt &)> PackFn) {
12631 APValue LHS, RHS;
12632 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: LHS) ||
12633 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: RHS))
12634 return false;
12635
12636 unsigned LHSVecLen = LHS.getVectorLength();
12637 unsigned RHSVecLen = RHS.getVectorLength();
12638
12639 assert(LHSVecLen != 0 && LHSVecLen == RHSVecLen &&
12640 "pack builtin LHSVecLen must equal to RHSVecLen");
12641
12642 const VectorType *VT0 = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
12643 const unsigned SrcBits = Info.Ctx.getIntWidth(T: VT0->getElementType());
12644
12645 const VectorType *DstVT = E->getType()->castAs<VectorType>();
12646 QualType DstElemTy = DstVT->getElementType();
12647 const bool DstIsUnsigned = DstElemTy->isUnsignedIntegerType();
12648
12649 const unsigned SrcPerLane = 128 / SrcBits;
12650 const unsigned Lanes = LHSVecLen * SrcBits / 128;
12651
12652 SmallVector<APValue, 64> Out;
12653 Out.reserve(N: LHSVecLen + RHSVecLen);
12654
12655 for (unsigned Lane = 0; Lane != Lanes; ++Lane) {
12656 unsigned base = Lane * SrcPerLane;
12657 for (unsigned I = 0; I != SrcPerLane; ++I)
12658 Out.emplace_back(Args: APValue(
12659 APSInt(PackFn(LHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12660 for (unsigned I = 0; I != SrcPerLane; ++I)
12661 Out.emplace_back(Args: APValue(
12662 APSInt(PackFn(RHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12663 }
12664
12665 Result = APValue(Out.data(), Out.size());
12666 return true;
12667}
12668
12669static bool evalShuffleGeneric(
12670 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12671 llvm::function_ref<std::pair<unsigned, int>(unsigned, unsigned)>
12672 GetSourceIndex) {
12673
12674 const auto *VT = Call->getType()->getAs<VectorType>();
12675 if (!VT)
12676 return false;
12677
12678 unsigned ShuffleMask = 0;
12679 APValue A, MaskVector, B;
12680 bool IsVectorMask = false;
12681 bool IsSingleOperand = (Call->getNumArgs() == 2);
12682
12683 if (IsSingleOperand) {
12684 QualType MaskType = Call->getArg(Arg: 1)->getType();
12685 if (MaskType->isVectorType()) {
12686 IsVectorMask = true;
12687 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12688 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector))
12689 return false;
12690 B = A;
12691 } else if (MaskType->isIntegerType()) {
12692 APSInt MaskImm;
12693 if (!EvaluateInteger(E: Call->getArg(Arg: 1), Result&: MaskImm, Info))
12694 return false;
12695 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12696 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A))
12697 return false;
12698 B = A;
12699 } else {
12700 return false;
12701 }
12702 } else {
12703 QualType Arg2Type = Call->getArg(Arg: 2)->getType();
12704 if (Arg2Type->isVectorType()) {
12705 IsVectorMask = true;
12706 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12707 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector) ||
12708 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 2), Result&: B))
12709 return false;
12710 } else if (Arg2Type->isIntegerType()) {
12711 APSInt MaskImm;
12712 if (!EvaluateInteger(E: Call->getArg(Arg: 2), Result&: MaskImm, Info))
12713 return false;
12714 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12715 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12716 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: B))
12717 return false;
12718 } else {
12719 return false;
12720 }
12721 }
12722
12723 unsigned NumElts = VT->getNumElements();
12724 SmallVector<APValue, 64> ResultElements;
12725 ResultElements.reserve(N: NumElts);
12726
12727 for (unsigned DstIdx = 0; DstIdx != NumElts; ++DstIdx) {
12728 if (IsVectorMask) {
12729 ShuffleMask = static_cast<unsigned>(
12730 MaskVector.getVectorElt(I: DstIdx).getInt().getZExtValue());
12731 }
12732 auto [SrcVecIdx, SrcIdx] = GetSourceIndex(DstIdx, ShuffleMask);
12733
12734 if (SrcIdx < 0) {
12735 // Zero out this element
12736 QualType ElemTy = VT->getElementType();
12737 if (ElemTy->isRealFloatingType()) {
12738 ResultElements.push_back(
12739 Elt: APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy))));
12740 } else if (ElemTy->isIntegerType()) {
12741 APValue Zero(Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy));
12742 ResultElements.push_back(Elt: APValue(Zero));
12743 } else {
12744 // Other types of fallback logic
12745 ResultElements.push_back(Elt: APValue());
12746 }
12747 } else {
12748 const APValue &Src = (SrcVecIdx == 0) ? A : B;
12749 ResultElements.push_back(Elt: Src.getVectorElt(I: SrcIdx));
12750 }
12751 }
12752
12753 Out = APValue(ResultElements.data(), ResultElements.size());
12754 return true;
12755}
12756static bool ConvertDoubleToFloatStrict(EvalInfo &Info, const Expr *E,
12757 APFloat OrigVal, APValue &Result) {
12758
12759 if (OrigVal.isInfinity()) {
12760 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 0;
12761 return false;
12762 }
12763 if (OrigVal.isNaN()) {
12764 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 1;
12765 return false;
12766 }
12767
12768 APFloat Val = OrigVal;
12769 bool LosesInfo = false;
12770 APFloat::opStatus Status = Val.convert(
12771 ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven, losesInfo: &LosesInfo);
12772
12773 if (LosesInfo || Val.isDenormal()) {
12774 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
12775 return false;
12776 }
12777
12778 if (Status != APFloat::opOK) {
12779 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
12780 return false;
12781 }
12782
12783 Result = APValue(Val);
12784 return true;
12785}
12786static bool evalShiftWithCount(
12787 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12788 llvm::function_ref<APInt(const APInt &, uint64_t)> ShiftOp,
12789 llvm::function_ref<APInt(const APInt &, unsigned)> OverflowOp) {
12790
12791 APValue Source, Count;
12792 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: Source) ||
12793 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: Count))
12794 return false;
12795
12796 assert(Call->getNumArgs() == 2);
12797
12798 QualType SourceTy = Call->getArg(Arg: 0)->getType();
12799 assert(SourceTy->isVectorType() &&
12800 Call->getArg(1)->getType()->isVectorType());
12801
12802 QualType DestEltTy = SourceTy->castAs<VectorType>()->getElementType();
12803 unsigned DestEltWidth = Source.getVectorElt(I: 0).getInt().getBitWidth();
12804 unsigned DestLen = Source.getVectorLength();
12805 bool IsDestUnsigned = DestEltTy->isUnsignedIntegerType();
12806 unsigned CountEltWidth = Count.getVectorElt(I: 0).getInt().getBitWidth();
12807 unsigned NumBitsInQWord = 64;
12808 unsigned NumCountElts = NumBitsInQWord / CountEltWidth;
12809 SmallVector<APValue, 64> Result;
12810 Result.reserve(N: DestLen);
12811
12812 uint64_t CountLQWord = 0;
12813 for (unsigned EltIdx = 0; EltIdx != NumCountElts; ++EltIdx) {
12814 uint64_t Elt = Count.getVectorElt(I: EltIdx).getInt().getZExtValue();
12815 CountLQWord |= (Elt << (EltIdx * CountEltWidth));
12816 }
12817
12818 for (unsigned EltIdx = 0; EltIdx != DestLen; ++EltIdx) {
12819 APInt Elt = Source.getVectorElt(I: EltIdx).getInt();
12820 if (CountLQWord < DestEltWidth) {
12821 Result.push_back(
12822 Elt: APValue(APSInt(ShiftOp(Elt, CountLQWord), IsDestUnsigned)));
12823 } else {
12824 Result.push_back(
12825 Elt: APValue(APSInt(OverflowOp(Elt, DestEltWidth), IsDestUnsigned)));
12826 }
12827 }
12828 Out = APValue(Result.data(), Result.size());
12829 return true;
12830}
12831
12832std::optional<APFloat> EvalScalarMinMaxFp(const APFloat &A, const APFloat &B,
12833 std::optional<APSInt> RoundingMode,
12834 bool IsMin) {
12835 APSInt DefaultMode(APInt(32, 4), /*isUnsigned=*/true);
12836 if (RoundingMode.value_or(u&: DefaultMode) != 4)
12837 return std::nullopt;
12838 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
12839 B.isInfinity() || B.isDenormal())
12840 return std::nullopt;
12841 if (A.isZero() && B.isZero())
12842 return B;
12843 return IsMin ? llvm::minimum(A, B) : llvm::maximum(A, B);
12844}
12845
12846bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
12847 if (!IsConstantEvaluatedBuiltinCall(E))
12848 return ExprEvaluatorBaseTy::VisitCallExpr(E);
12849
12850 unsigned BuiltinOp = ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E);
12851
12852 auto EvaluateBinOpExpr =
12853 [&](llvm::function_ref<APInt(const APSInt &, const APSInt &)> Fn) {
12854 APValue SourceLHS, SourceRHS;
12855 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12856 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12857 return false;
12858
12859 auto *DestTy = E->getType()->castAs<VectorType>();
12860 QualType DestEltTy = DestTy->getElementType();
12861 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12862 unsigned SourceLen = SourceLHS.getVectorLength();
12863 SmallVector<APValue, 4> ResultElements;
12864 ResultElements.reserve(N: SourceLen);
12865
12866 if (SourceRHS.isInt()) {
12867 const APSInt &RHS = SourceRHS.getInt();
12868 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12869 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12870 ResultElements.push_back(
12871 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12872 }
12873 } else {
12874 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12875 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12876 const APSInt &RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
12877 ResultElements.push_back(
12878 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12879 }
12880 }
12881 return Success(V: APValue(ResultElements.data(), SourceLen), E);
12882 };
12883
12884 auto EvaluateFpBinOpExpr =
12885 [&](llvm::function_ref<std::optional<APFloat>(
12886 const APFloat &, const APFloat &, std::optional<APSInt>)>
12887 Fn,
12888 bool IsScalar = false) {
12889 assert(E->getNumArgs() == 2 || E->getNumArgs() == 3);
12890 APValue A, B;
12891 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
12892 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
12893 return false;
12894
12895 assert(A.isVector() && B.isVector());
12896 assert(A.getVectorLength() == B.getVectorLength());
12897
12898 std::optional<APSInt> RoundingMode;
12899 if (E->getNumArgs() == 3) {
12900 APSInt Imm;
12901 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
12902 return false;
12903 RoundingMode = Imm;
12904 }
12905
12906 unsigned NumElems = A.getVectorLength();
12907 SmallVector<APValue, 4> ResultElements;
12908 ResultElements.reserve(N: NumElems);
12909
12910 for (unsigned EltNum = 0; EltNum < NumElems; ++EltNum) {
12911 if (IsScalar && EltNum > 0) {
12912 ResultElements.push_back(Elt: A.getVectorElt(I: EltNum));
12913 continue;
12914 }
12915 const APFloat &EltA = A.getVectorElt(I: EltNum).getFloat();
12916 const APFloat &EltB = B.getVectorElt(I: EltNum).getFloat();
12917 std::optional<APFloat> Result = Fn(EltA, EltB, RoundingMode);
12918 if (!Result)
12919 return false;
12920 ResultElements.push_back(Elt: APValue(*Result));
12921 }
12922 return Success(V: APValue(ResultElements.data(), NumElems), E);
12923 };
12924
12925 auto EvaluateScalarFpRoundMaskBinOp =
12926 [&](llvm::function_ref<std::optional<APFloat>(
12927 const APFloat &, const APFloat &, std::optional<APSInt>)>
12928 Fn) {
12929 assert(E->getNumArgs() == 5);
12930 APValue VecA, VecB, VecSrc;
12931 APSInt MaskVal, Rounding;
12932
12933 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
12934 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
12935 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
12936 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskVal, Info) ||
12937 !EvaluateInteger(E: E->getArg(Arg: 4), Result&: Rounding, Info))
12938 return false;
12939
12940 unsigned NumElems = VecA.getVectorLength();
12941 SmallVector<APValue, 8> ResultElements;
12942 ResultElements.reserve(N: NumElems);
12943
12944 if (MaskVal.getZExtValue() & 1) {
12945 const APFloat &EltA = VecA.getVectorElt(I: 0).getFloat();
12946 const APFloat &EltB = VecB.getVectorElt(I: 0).getFloat();
12947 std::optional<APFloat> Result = Fn(EltA, EltB, Rounding);
12948 if (!Result)
12949 return false;
12950 ResultElements.push_back(Elt: APValue(*Result));
12951 } else {
12952 ResultElements.push_back(Elt: VecSrc.getVectorElt(I: 0));
12953 }
12954
12955 for (unsigned I = 1; I < NumElems; ++I)
12956 ResultElements.push_back(Elt: VecA.getVectorElt(I));
12957
12958 return Success(V: APValue(ResultElements.data(), NumElems), E);
12959 };
12960
12961 auto EvalSelectScalar = [&](unsigned Len) -> bool {
12962 APSInt Mask;
12963 APValue AVal, WVal;
12964 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info) ||
12965 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: AVal) ||
12966 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: WVal))
12967 return false;
12968
12969 bool TakeA0 = (Mask.getZExtValue() & 1u) != 0;
12970 SmallVector<APValue, 4> Res;
12971 Res.reserve(N: Len);
12972 Res.push_back(Elt: TakeA0 ? AVal.getVectorElt(I: 0) : WVal.getVectorElt(I: 0));
12973 for (unsigned I = 1; I < Len; ++I)
12974 Res.push_back(Elt: WVal.getVectorElt(I));
12975 APValue V(Res.data(), Res.size());
12976 return Success(V, E);
12977 };
12978
12979 auto EvalVectorDotProduct = [&](bool IsSaturating) -> bool {
12980 APValue Source, OperandA, OperandB;
12981 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
12982 !EvaluateVector(E: E->getArg(Arg: 1), Result&: OperandA, Info) ||
12983 !EvaluateVector(E: E->getArg(Arg: 2), Result&: OperandB, Info)) {
12984 return false;
12985 }
12986
12987 unsigned NumSrcElems = Source.getVectorLength();
12988 unsigned NumOperandElems = OperandA.getVectorLength();
12989 unsigned ElemsPerLane = NumOperandElems / NumSrcElems;
12990
12991 assert(OperandA.getVectorLength() == OperandB.getVectorLength());
12992
12993 SmallVector<APValue, 16> Result;
12994 Result.reserve(N: NumSrcElems);
12995 for (unsigned I = 0; I != NumSrcElems; ++I) {
12996 APSInt DotProduct = Source.getVectorElt(I).getInt();
12997 DotProduct = DotProduct.extend(width: 64);
12998 for (unsigned J = 0; J != ElemsPerLane; ++J) {
12999 APSInt OpA = APSInt(
13000 OperandA.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
13001 false);
13002 APSInt OpB = APSInt(
13003 OperandB.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
13004 false);
13005 DotProduct += OpA * OpB;
13006 }
13007 if (IsSaturating) {
13008 DotProduct = APSInt(DotProduct.truncSSat(width: 32), false);
13009 } else {
13010 DotProduct = APSInt(DotProduct.trunc(width: 32), false);
13011 }
13012 Result.push_back(Elt: APValue(DotProduct));
13013 }
13014
13015 return Success(V: APValue(Result.data(), Result.size()), E);
13016 };
13017
13018 switch (BuiltinOp) {
13019 default:
13020 return false;
13021 case Builtin::BI__builtin_elementwise_popcount:
13022 case Builtin::BI__builtin_elementwise_bitreverse: {
13023 APValue Source;
13024 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
13025 return false;
13026
13027 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13028 unsigned SourceLen = Source.getVectorLength();
13029 SmallVector<APValue, 4> ResultElements;
13030 ResultElements.reserve(N: SourceLen);
13031
13032 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13033 APSInt Elt = Source.getVectorElt(I: EltNum).getInt();
13034 switch (BuiltinOp) {
13035 case Builtin::BI__builtin_elementwise_popcount:
13036 ResultElements.push_back(Elt: APValue(
13037 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), Elt.popcount()),
13038 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13039 break;
13040 case Builtin::BI__builtin_elementwise_bitreverse:
13041 ResultElements.push_back(
13042 Elt: APValue(APSInt(Elt.reverseBits(),
13043 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13044 break;
13045 }
13046 }
13047
13048 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13049 }
13050 case Builtin::BI__builtin_elementwise_abs: {
13051 APValue Source;
13052 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
13053 return false;
13054
13055 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13056 unsigned SourceLen = Source.getVectorLength();
13057 SmallVector<APValue, 4> ResultElements;
13058 ResultElements.reserve(N: SourceLen);
13059
13060 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13061 APValue CurrentEle = Source.getVectorElt(I: EltNum);
13062 APValue Val = DestEltTy->isFloatingType()
13063 ? APValue(llvm::abs(X: CurrentEle.getFloat()))
13064 : APValue(APSInt(
13065 CurrentEle.getInt().abs(),
13066 DestEltTy->isUnsignedIntegerOrEnumerationType()));
13067 ResultElements.push_back(Elt: Val);
13068 }
13069
13070 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13071 }
13072
13073 case Builtin::BI__builtin_elementwise_add_sat:
13074 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13075 return LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
13076 });
13077
13078 case Builtin::BI__builtin_elementwise_sub_sat:
13079 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13080 return LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
13081 });
13082
13083 case X86::BI__builtin_ia32_extract128i256:
13084 case X86::BI__builtin_ia32_vextractf128_pd256:
13085 case X86::BI__builtin_ia32_vextractf128_ps256:
13086 case X86::BI__builtin_ia32_vextractf128_si256: {
13087 APValue SourceVec, SourceImm;
13088 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
13089 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceImm))
13090 return false;
13091
13092 if (!SourceVec.isVector())
13093 return false;
13094
13095 const auto *RetVT = E->getType()->castAs<VectorType>();
13096 unsigned RetLen = RetVT->getNumElements();
13097 unsigned Idx = SourceImm.getInt().getZExtValue() & 1;
13098
13099 SmallVector<APValue, 32> ResultElements;
13100 ResultElements.reserve(N: RetLen);
13101
13102 for (unsigned I = 0; I < RetLen; I++)
13103 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Idx * RetLen + I));
13104
13105 return Success(V: APValue(ResultElements.data(), RetLen), E);
13106 }
13107
13108 case clang::X86::BI__builtin_ia32_cvtmask2b128:
13109 case clang::X86::BI__builtin_ia32_cvtmask2b256:
13110 case clang::X86::BI__builtin_ia32_cvtmask2b512:
13111 case clang::X86::BI__builtin_ia32_cvtmask2w128:
13112 case clang::X86::BI__builtin_ia32_cvtmask2w256:
13113 case clang::X86::BI__builtin_ia32_cvtmask2w512:
13114 case clang::X86::BI__builtin_ia32_cvtmask2d128:
13115 case clang::X86::BI__builtin_ia32_cvtmask2d256:
13116 case clang::X86::BI__builtin_ia32_cvtmask2d512:
13117 case clang::X86::BI__builtin_ia32_cvtmask2q128:
13118 case clang::X86::BI__builtin_ia32_cvtmask2q256:
13119 case clang::X86::BI__builtin_ia32_cvtmask2q512: {
13120 assert(E->getNumArgs() == 1);
13121 APSInt Mask;
13122 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info))
13123 return false;
13124
13125 QualType VecTy = E->getType();
13126 const VectorType *VT = VecTy->castAs<VectorType>();
13127 unsigned VectorLen = VT->getNumElements();
13128 QualType ElemTy = VT->getElementType();
13129 unsigned ElemWidth = Info.Ctx.getTypeSize(T: ElemTy);
13130
13131 SmallVector<APValue, 16> Elems;
13132 for (unsigned I = 0; I != VectorLen; ++I) {
13133 bool BitSet = Mask[I];
13134 APSInt ElemVal(ElemWidth, /*isUnsigned=*/false);
13135 if (BitSet) {
13136 ElemVal.setAllBits();
13137 }
13138 Elems.push_back(Elt: APValue(ElemVal));
13139 }
13140 return Success(V: APValue(Elems.data(), VectorLen), E);
13141 }
13142
13143 case X86::BI__builtin_ia32_extracti32x4_256_mask:
13144 case X86::BI__builtin_ia32_extractf32x4_256_mask:
13145 case X86::BI__builtin_ia32_extracti32x4_mask:
13146 case X86::BI__builtin_ia32_extractf32x4_mask:
13147 case X86::BI__builtin_ia32_extracti32x8_mask:
13148 case X86::BI__builtin_ia32_extractf32x8_mask:
13149 case X86::BI__builtin_ia32_extracti64x2_256_mask:
13150 case X86::BI__builtin_ia32_extractf64x2_256_mask:
13151 case X86::BI__builtin_ia32_extracti64x2_512_mask:
13152 case X86::BI__builtin_ia32_extractf64x2_512_mask:
13153 case X86::BI__builtin_ia32_extracti64x4_mask:
13154 case X86::BI__builtin_ia32_extractf64x4_mask: {
13155 APValue SourceVec, MergeVec;
13156 APSInt Imm, MaskImm;
13157
13158 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
13159 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info) ||
13160 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MergeVec) ||
13161 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskImm, Info))
13162 return false;
13163
13164 const auto *RetVT = E->getType()->castAs<VectorType>();
13165 unsigned RetLen = RetVT->getNumElements();
13166
13167 if (!SourceVec.isVector() || !MergeVec.isVector())
13168 return false;
13169 unsigned SrcLen = SourceVec.getVectorLength();
13170 unsigned Lanes = SrcLen / RetLen;
13171 unsigned Lane = static_cast<unsigned>(Imm.getZExtValue() % Lanes);
13172 unsigned Base = Lane * RetLen;
13173
13174 SmallVector<APValue, 32> ResultElements;
13175 ResultElements.reserve(N: RetLen);
13176 for (unsigned I = 0; I < RetLen; ++I) {
13177 if (MaskImm[I])
13178 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Base + I));
13179 else
13180 ResultElements.push_back(Elt: MergeVec.getVectorElt(I));
13181 }
13182 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13183 }
13184
13185 case clang::X86::BI__builtin_ia32_pavgb128:
13186 case clang::X86::BI__builtin_ia32_pavgw128:
13187 case clang::X86::BI__builtin_ia32_pavgb256:
13188 case clang::X86::BI__builtin_ia32_pavgw256:
13189 case clang::X86::BI__builtin_ia32_pavgb512:
13190 case clang::X86::BI__builtin_ia32_pavgw512:
13191 return EvaluateBinOpExpr(llvm::APIntOps::avgCeilU);
13192
13193 case clang::X86::BI__builtin_ia32_pmulhrsw128:
13194 case clang::X86::BI__builtin_ia32_pmulhrsw256:
13195 case clang::X86::BI__builtin_ia32_pmulhrsw512:
13196 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13197 return (llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS).ashr(ShiftAmt: 14) + 1)
13198 .extractBits(numBits: 16, bitPosition: 1);
13199 });
13200
13201 case clang::X86::BI__builtin_ia32_psadbw128:
13202 case clang::X86::BI__builtin_ia32_psadbw256:
13203 case clang::X86::BI__builtin_ia32_psadbw512: {
13204 APValue SourceLHS, SourceRHS;
13205 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13206 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13207 return false;
13208
13209 assert(SourceLHS.isVector() && SourceRHS.isVector());
13210 unsigned SourceLen = SourceLHS.getVectorLength();
13211 assert(SourceLen == SourceRHS.getVectorLength());
13212 assert((SourceLen % 8) == 0);
13213
13214 auto *DestTy = E->getType()->castAs<VectorType>();
13215 QualType DestEltTy = DestTy->getElementType();
13216 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13217 SmallVector<APValue, 8> ResultElements;
13218 ResultElements.reserve(N: SourceLen / 8);
13219
13220 for (unsigned Lane = 0; Lane != SourceLen; Lane += 8) {
13221 APInt Sum(64, 0);
13222 for (unsigned I = 0; I != 8; ++I) {
13223 APInt LHS = SourceLHS.getVectorElt(I: Lane + I).getInt().extOrTrunc(width: 8);
13224 APInt RHS = SourceRHS.getVectorElt(I: Lane + I).getInt().extOrTrunc(width: 8);
13225 Sum += llvm::APIntOps::abdu(A: LHS, B: RHS).zext(width: 64);
13226 }
13227 ResultElements.push_back(Elt: APValue(APSInt(Sum, DestUnsigned)));
13228 }
13229
13230 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13231 }
13232
13233 case clang::X86::BI__builtin_ia32_pmaddubsw128:
13234 case clang::X86::BI__builtin_ia32_pmaddubsw256:
13235 case clang::X86::BI__builtin_ia32_pmaddubsw512:
13236 case clang::X86::BI__builtin_ia32_pmaddwd128:
13237 case clang::X86::BI__builtin_ia32_pmaddwd256:
13238 case clang::X86::BI__builtin_ia32_pmaddwd512: {
13239 APValue SourceLHS, SourceRHS;
13240 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13241 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13242 return false;
13243
13244 auto *DestTy = E->getType()->castAs<VectorType>();
13245 QualType DestEltTy = DestTy->getElementType();
13246 unsigned SourceLen = SourceLHS.getVectorLength();
13247 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13248 SmallVector<APValue, 4> ResultElements;
13249 ResultElements.reserve(N: SourceLen / 2);
13250
13251 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
13252 const APSInt &LoLHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13253 const APSInt &HiLHS = SourceLHS.getVectorElt(I: EltNum + 1).getInt();
13254 const APSInt &LoRHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13255 const APSInt &HiRHS = SourceRHS.getVectorElt(I: EltNum + 1).getInt();
13256 unsigned BitWidth = 2 * LoLHS.getBitWidth();
13257
13258 switch (BuiltinOp) {
13259 case clang::X86::BI__builtin_ia32_pmaddubsw128:
13260 case clang::X86::BI__builtin_ia32_pmaddubsw256:
13261 case clang::X86::BI__builtin_ia32_pmaddubsw512:
13262 ResultElements.push_back(Elt: APValue(
13263 APSInt((LoLHS.zext(width: BitWidth) * LoRHS.sext(width: BitWidth))
13264 .sadd_sat(RHS: (HiLHS.zext(width: BitWidth) * HiRHS.sext(width: BitWidth))),
13265 DestUnsigned)));
13266 break;
13267 case clang::X86::BI__builtin_ia32_pmaddwd128:
13268 case clang::X86::BI__builtin_ia32_pmaddwd256:
13269 case clang::X86::BI__builtin_ia32_pmaddwd512:
13270 ResultElements.push_back(
13271 Elt: APValue(APSInt((LoLHS.sext(width: BitWidth) * LoRHS.sext(width: BitWidth)) +
13272 (HiLHS.sext(width: BitWidth) * HiRHS.sext(width: BitWidth)),
13273 DestUnsigned)));
13274 break;
13275 }
13276 }
13277
13278 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13279 }
13280
13281 case clang::X86::BI__builtin_ia32_bmacor16x16x16_v16hi:
13282 case clang::X86::BI__builtin_ia32_bmacor16x16x16_v32hi:
13283 case clang::X86::BI__builtin_ia32_bmacxor16x16x16_v16hi:
13284 case clang::X86::BI__builtin_ia32_bmacxor16x16x16_v32hi: {
13285 // Bit Matrix Multiply and Accumulate (AVX512BMM). Each 256-bit lane holds
13286 // a 16x16 bit matrix as 16 x i16 elements; element i is row i and bit j of
13287 // that element is entry [i][j]. The accumulator (third argument, src1 in
13288 // the AMD ISA) provides the initial value of each result bit, into which
13289 // the bit-matrix product of the first two arguments (src2 * src3) is
13290 // reduced with OR (vbmacor) or XOR (vbmacxor):
13291 // for i in 0..15, j in 0..15:
13292 // bit = C[16*i+j]
13293 // for k in 0..15: bit OP= A[16*i+k] & B[16*k+j]
13294 // dest[16*i+j] = bit
13295 APValue SourceA, SourceB, SourceC;
13296 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
13297 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
13298 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13299 return false;
13300
13301 bool IsXor = E->getBuiltinCallee() ==
13302 clang::X86::BI__builtin_ia32_bmacxor16x16x16_v16hi ||
13303 E->getBuiltinCallee() ==
13304 clang::X86::BI__builtin_ia32_bmacxor16x16x16_v32hi;
13305
13306 unsigned SourceLen = SourceA.getVectorLength();
13307 assert(SourceLen % 16 == 0 && "BMM operates on 256-bit lanes of 16 x i16");
13308 auto *DestTy = E->getType()->castAs<VectorType>();
13309 QualType DestEltTy = DestTy->getElementType();
13310 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13311
13312 SmallVector<APValue, 32> ResultElements(SourceLen);
13313 for (unsigned Lane = 0; Lane != SourceLen; Lane += 16) {
13314 for (unsigned I = 0; I != 16; ++I) {
13315 uint16_t A =
13316 (uint16_t)SourceA.getVectorElt(I: Lane + I).getInt().getZExtValue();
13317 uint16_t Dst =
13318 (uint16_t)SourceC.getVectorElt(I: Lane + I).getInt().getZExtValue();
13319 for (unsigned J = 0; J != 16; ++J) {
13320 // Seed the reduction with the accumulator bit, then fold in each
13321 // product term with the same operator (OR for vbmacor, XOR for
13322 // vbmacxor).
13323 unsigned Bit = (Dst >> J) & 1u;
13324 for (unsigned K = 0; K != 16; ++K) {
13325 uint16_t B = (uint16_t)SourceB.getVectorElt(I: Lane + K)
13326 .getInt()
13327 .getZExtValue();
13328 unsigned Product = ((A >> K) & 1u) & ((B >> J) & 1u);
13329 Bit = IsXor ? (Bit ^ Product) : (Bit | Product);
13330 }
13331 Dst = (Dst & ~(uint16_t(1) << J)) | (uint16_t(Bit) << J);
13332 }
13333 ResultElements[Lane + I] =
13334 APValue(APSInt(APInt(16, Dst), DestUnsigned));
13335 }
13336 }
13337 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13338 }
13339
13340 case clang::X86::BI__builtin_ia32_dbpsadbw128:
13341 case clang::X86::BI__builtin_ia32_dbpsadbw256:
13342 case clang::X86::BI__builtin_ia32_dbpsadbw512: {
13343 APValue SourceA, SourceB, SourceImm;
13344 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
13345 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
13346 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceImm))
13347 return false;
13348
13349 unsigned SourceLen = SourceA.getVectorLength();
13350 constexpr unsigned LaneSize = 16; // 128-bit lane = 16 bytes
13351 unsigned Imm = SourceImm.getInt().getZExtValue();
13352
13353 auto *DestTy = E->getType()->castAs<VectorType>();
13354 QualType DestEltTy = DestTy->getElementType();
13355 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13356 SmallVector<APValue, 32> ResultElements;
13357 ResultElements.reserve(N: SourceLen / 2);
13358
13359 // Phase 1: Shuffle SourceB using all four 2-bit fields of imm8.
13360 // Within each 128-bit lane, for group j (0..3), select a 4-byte block
13361 // from SourceB based on bits [2*j+1:2*j] of imm8.
13362 SmallVector<uint8_t, 64> Shuffled(SourceLen);
13363 for (unsigned I = 0; I < SourceLen; I += LaneSize) {
13364 for (unsigned J = 0; J < 4; ++J) {
13365 unsigned Part = (Imm >> (2 * J)) & 3;
13366 for (unsigned K = 0; K < 4; ++K) {
13367 Shuffled[I + 4 * J + K] = static_cast<uint8_t>(
13368 SourceB.getVectorElt(I: I + 4 * Part + K).getInt().getZExtValue());
13369 }
13370 }
13371 }
13372
13373 // Phase 2: Sliding SAD computation.
13374 // For every group of 4 output u16 values, compute absolute differences
13375 // using overlapping windows into SourceA and the shuffled array.
13376 unsigned Size = SourceLen / 2; // number of output u16 elements
13377 for (unsigned I = 0; I < Size; I += 4) {
13378 unsigned Sad[4] = {0, 0, 0, 0};
13379 for (unsigned J = 0; J < 4; ++J) {
13380 uint8_t A1 = static_cast<uint8_t>(
13381 SourceA.getVectorElt(I: 2 * I + J).getInt().getZExtValue());
13382 uint8_t A2 = static_cast<uint8_t>(
13383 SourceA.getVectorElt(I: 2 * I + J + 4).getInt().getZExtValue());
13384 uint8_t B0 = Shuffled[2 * I + J];
13385 uint8_t B1 = Shuffled[2 * I + J + 1];
13386 uint8_t B2 = Shuffled[2 * I + J + 2];
13387 uint8_t B3 = Shuffled[2 * I + J + 3];
13388 Sad[0] += (A1 > B0) ? (A1 - B0) : (B0 - A1);
13389 Sad[1] += (A1 > B1) ? (A1 - B1) : (B1 - A1);
13390 Sad[2] += (A2 > B2) ? (A2 - B2) : (B2 - A2);
13391 Sad[3] += (A2 > B3) ? (A2 - B3) : (B3 - A2);
13392 }
13393 for (unsigned R = 0; R < 4; ++R)
13394 ResultElements.push_back(
13395 Elt: APValue(APSInt(APInt(16, Sad[R]), DestUnsigned)));
13396 }
13397
13398 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13399 }
13400
13401 case clang::X86::BI__builtin_ia32_mpsadbw128:
13402 case clang::X86::BI__builtin_ia32_mpsadbw256: {
13403 APValue SourceA, SourceB;
13404 APSInt SourceImm;
13405 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: SourceA, Info) ||
13406 !EvaluateVector(E: E->getArg(Arg: 1), Result&: SourceB, Info) ||
13407 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: SourceImm, Info))
13408 return false;
13409 unsigned SourceLen = SourceA.getVectorLength();
13410 constexpr unsigned LaneSize = 16;
13411 assert((SourceLen == LaneSize || SourceLen == 2 * LaneSize) &&
13412 "MPSADBW operates on 128-bit or 256-bit vectors");
13413 unsigned NumLanes = SourceLen / LaneSize;
13414 unsigned Imm = SourceImm.getZExtValue();
13415
13416 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13417 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13418 SmallVector<APValue, 16> ResultElements;
13419 ResultElements.reserve(N: SourceLen / 2);
13420
13421 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
13422 unsigned Ctrl = (Imm >> (3 * Lane)) & 0x7;
13423 unsigned AOff = ((Ctrl >> 2) & 1) * 4;
13424 unsigned BOff = (Ctrl & 3) * 4;
13425 for (unsigned J = 0; J != 8; ++J) {
13426 uint16_t Sad = 0;
13427 for (unsigned K = 0; K != 4; ++K) {
13428 uint8_t A = static_cast<uint8_t>(
13429 SourceA.getVectorElt(I: Lane * LaneSize + AOff + J + K)
13430 .getInt()
13431 .getZExtValue());
13432 uint8_t B = static_cast<uint8_t>(
13433 SourceB.getVectorElt(I: Lane * LaneSize + BOff + K)
13434 .getInt()
13435 .getZExtValue());
13436 Sad += (A > B) ? (A - B) : (B - A);
13437 }
13438 ResultElements.push_back(Elt: APValue(APSInt(APInt(16, Sad), DestUnsigned)));
13439 }
13440 }
13441 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13442 }
13443
13444 case clang::X86::BI__builtin_ia32_pmulhuw128:
13445 case clang::X86::BI__builtin_ia32_pmulhuw256:
13446 case clang::X86::BI__builtin_ia32_pmulhuw512:
13447 return EvaluateBinOpExpr(llvm::APIntOps::mulhu);
13448
13449 case clang::X86::BI__builtin_ia32_pmulhw128:
13450 case clang::X86::BI__builtin_ia32_pmulhw256:
13451 case clang::X86::BI__builtin_ia32_pmulhw512:
13452 return EvaluateBinOpExpr(llvm::APIntOps::mulhs);
13453
13454 case clang::X86::BI__builtin_ia32_psllv2di:
13455 case clang::X86::BI__builtin_ia32_psllv4di:
13456 case clang::X86::BI__builtin_ia32_psllv4si:
13457 case clang::X86::BI__builtin_ia32_psllv8di:
13458 case clang::X86::BI__builtin_ia32_psllv8hi:
13459 case clang::X86::BI__builtin_ia32_psllv8si:
13460 case clang::X86::BI__builtin_ia32_psllv16hi:
13461 case clang::X86::BI__builtin_ia32_psllv16si:
13462 case clang::X86::BI__builtin_ia32_psllv32hi:
13463 case clang::X86::BI__builtin_ia32_psllwi128:
13464 case clang::X86::BI__builtin_ia32_pslldi128:
13465 case clang::X86::BI__builtin_ia32_psllqi128:
13466 case clang::X86::BI__builtin_ia32_psllwi256:
13467 case clang::X86::BI__builtin_ia32_pslldi256:
13468 case clang::X86::BI__builtin_ia32_psllqi256:
13469 case clang::X86::BI__builtin_ia32_psllwi512:
13470 case clang::X86::BI__builtin_ia32_pslldi512:
13471 case clang::X86::BI__builtin_ia32_psllqi512:
13472 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13473 if (RHS.uge(RHS: LHS.getBitWidth())) {
13474 return APInt::getZero(numBits: LHS.getBitWidth());
13475 }
13476 return LHS.shl(shiftAmt: RHS.getZExtValue());
13477 });
13478
13479 case clang::X86::BI__builtin_ia32_psrav4si:
13480 case clang::X86::BI__builtin_ia32_psrav8di:
13481 case clang::X86::BI__builtin_ia32_psrav8hi:
13482 case clang::X86::BI__builtin_ia32_psrav8si:
13483 case clang::X86::BI__builtin_ia32_psrav16hi:
13484 case clang::X86::BI__builtin_ia32_psrav16si:
13485 case clang::X86::BI__builtin_ia32_psrav32hi:
13486 case clang::X86::BI__builtin_ia32_psravq128:
13487 case clang::X86::BI__builtin_ia32_psravq256:
13488 case clang::X86::BI__builtin_ia32_psrawi128:
13489 case clang::X86::BI__builtin_ia32_psradi128:
13490 case clang::X86::BI__builtin_ia32_psraqi128:
13491 case clang::X86::BI__builtin_ia32_psrawi256:
13492 case clang::X86::BI__builtin_ia32_psradi256:
13493 case clang::X86::BI__builtin_ia32_psraqi256:
13494 case clang::X86::BI__builtin_ia32_psrawi512:
13495 case clang::X86::BI__builtin_ia32_psradi512:
13496 case clang::X86::BI__builtin_ia32_psraqi512:
13497 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13498 if (RHS.uge(RHS: LHS.getBitWidth())) {
13499 return LHS.ashr(ShiftAmt: LHS.getBitWidth() - 1);
13500 }
13501 return LHS.ashr(ShiftAmt: RHS.getZExtValue());
13502 });
13503
13504 case clang::X86::BI__builtin_ia32_psrlv2di:
13505 case clang::X86::BI__builtin_ia32_psrlv4di:
13506 case clang::X86::BI__builtin_ia32_psrlv4si:
13507 case clang::X86::BI__builtin_ia32_psrlv8di:
13508 case clang::X86::BI__builtin_ia32_psrlv8hi:
13509 case clang::X86::BI__builtin_ia32_psrlv8si:
13510 case clang::X86::BI__builtin_ia32_psrlv16hi:
13511 case clang::X86::BI__builtin_ia32_psrlv16si:
13512 case clang::X86::BI__builtin_ia32_psrlv32hi:
13513 case clang::X86::BI__builtin_ia32_psrlwi128:
13514 case clang::X86::BI__builtin_ia32_psrldi128:
13515 case clang::X86::BI__builtin_ia32_psrlqi128:
13516 case clang::X86::BI__builtin_ia32_psrlwi256:
13517 case clang::X86::BI__builtin_ia32_psrldi256:
13518 case clang::X86::BI__builtin_ia32_psrlqi256:
13519 case clang::X86::BI__builtin_ia32_psrlwi512:
13520 case clang::X86::BI__builtin_ia32_psrldi512:
13521 case clang::X86::BI__builtin_ia32_psrlqi512:
13522 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13523 if (RHS.uge(RHS: LHS.getBitWidth())) {
13524 return APInt::getZero(numBits: LHS.getBitWidth());
13525 }
13526 return LHS.lshr(shiftAmt: RHS.getZExtValue());
13527 });
13528 case X86::BI__builtin_ia32_packsswb128:
13529 case X86::BI__builtin_ia32_packsswb256:
13530 case X86::BI__builtin_ia32_packsswb512:
13531 case X86::BI__builtin_ia32_packssdw128:
13532 case X86::BI__builtin_ia32_packssdw256:
13533 case X86::BI__builtin_ia32_packssdw512:
13534 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
13535 return APSInt(Src).truncSSat(width: Src.getBitWidth() / 2);
13536 });
13537 case X86::BI__builtin_ia32_packusdw128:
13538 case X86::BI__builtin_ia32_packusdw256:
13539 case X86::BI__builtin_ia32_packusdw512:
13540 case X86::BI__builtin_ia32_packuswb128:
13541 case X86::BI__builtin_ia32_packuswb256:
13542 case X86::BI__builtin_ia32_packuswb512:
13543 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
13544 return APSInt(Src).truncSSatU(width: Src.getBitWidth() / 2);
13545 });
13546 case clang::X86::BI__builtin_ia32_selectss_128:
13547 return EvalSelectScalar(4);
13548 case clang::X86::BI__builtin_ia32_selectsd_128:
13549 return EvalSelectScalar(2);
13550 case clang::X86::BI__builtin_ia32_selectsh_128:
13551 case clang::X86::BI__builtin_ia32_selectsbf_128:
13552 return EvalSelectScalar(8);
13553 case clang::X86::BI__builtin_ia32_pmuldq128:
13554 case clang::X86::BI__builtin_ia32_pmuldq256:
13555 case clang::X86::BI__builtin_ia32_pmuldq512:
13556 case clang::X86::BI__builtin_ia32_pmuludq128:
13557 case clang::X86::BI__builtin_ia32_pmuludq256:
13558 case clang::X86::BI__builtin_ia32_pmuludq512: {
13559 APValue SourceLHS, SourceRHS;
13560 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13561 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13562 return false;
13563
13564 unsigned SourceLen = SourceLHS.getVectorLength();
13565 SmallVector<APValue, 4> ResultElements;
13566 ResultElements.reserve(N: SourceLen / 2);
13567
13568 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
13569 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13570 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13571
13572 switch (BuiltinOp) {
13573 case clang::X86::BI__builtin_ia32_pmuludq128:
13574 case clang::X86::BI__builtin_ia32_pmuludq256:
13575 case clang::X86::BI__builtin_ia32_pmuludq512:
13576 ResultElements.push_back(
13577 Elt: APValue(APSInt(llvm::APIntOps::muluExtended(C1: LHS, C2: RHS), true)));
13578 break;
13579 case clang::X86::BI__builtin_ia32_pmuldq128:
13580 case clang::X86::BI__builtin_ia32_pmuldq256:
13581 case clang::X86::BI__builtin_ia32_pmuldq512:
13582 ResultElements.push_back(
13583 Elt: APValue(APSInt(llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS), false)));
13584 break;
13585 }
13586 }
13587
13588 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13589 }
13590
13591 case X86::BI__builtin_ia32_vpmadd52luq128:
13592 case X86::BI__builtin_ia32_vpmadd52luq256:
13593 case X86::BI__builtin_ia32_vpmadd52luq512: {
13594 APValue A, B, C;
13595 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
13596 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
13597 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
13598 return false;
13599
13600 unsigned ALen = A.getVectorLength();
13601 SmallVector<APValue, 4> ResultElements;
13602 ResultElements.reserve(N: ALen);
13603
13604 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
13605 APInt AElt = A.getVectorElt(I: EltNum).getInt();
13606 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13607 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13608 APSInt ResElt(AElt + (BElt * CElt).zext(width: 64), false);
13609 ResultElements.push_back(Elt: APValue(ResElt));
13610 }
13611
13612 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13613 }
13614 case X86::BI__builtin_ia32_vpmadd52huq128:
13615 case X86::BI__builtin_ia32_vpmadd52huq256:
13616 case X86::BI__builtin_ia32_vpmadd52huq512: {
13617 APValue A, B, C;
13618 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
13619 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
13620 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
13621 return false;
13622
13623 unsigned ALen = A.getVectorLength();
13624 SmallVector<APValue, 4> ResultElements;
13625 ResultElements.reserve(N: ALen);
13626
13627 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
13628 APInt AElt = A.getVectorElt(I: EltNum).getInt();
13629 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13630 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13631 APSInt ResElt(AElt + llvm::APIntOps::mulhu(C1: BElt, C2: CElt).zext(width: 64), false);
13632 ResultElements.push_back(Elt: APValue(ResElt));
13633 }
13634
13635 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13636 }
13637
13638 case clang::X86::BI__builtin_ia32_vprotbi:
13639 case clang::X86::BI__builtin_ia32_vprotdi:
13640 case clang::X86::BI__builtin_ia32_vprotqi:
13641 case clang::X86::BI__builtin_ia32_vprotwi:
13642 case clang::X86::BI__builtin_ia32_prold128:
13643 case clang::X86::BI__builtin_ia32_prold256:
13644 case clang::X86::BI__builtin_ia32_prold512:
13645 case clang::X86::BI__builtin_ia32_prolq128:
13646 case clang::X86::BI__builtin_ia32_prolq256:
13647 case clang::X86::BI__builtin_ia32_prolq512:
13648 return EvaluateBinOpExpr(
13649 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotl(rotateAmt: RHS); });
13650
13651 case clang::X86::BI__builtin_ia32_prord128:
13652 case clang::X86::BI__builtin_ia32_prord256:
13653 case clang::X86::BI__builtin_ia32_prord512:
13654 case clang::X86::BI__builtin_ia32_prorq128:
13655 case clang::X86::BI__builtin_ia32_prorq256:
13656 case clang::X86::BI__builtin_ia32_prorq512:
13657 return EvaluateBinOpExpr(
13658 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotr(rotateAmt: RHS); });
13659
13660 case Builtin::BI__builtin_elementwise_max:
13661 case Builtin::BI__builtin_elementwise_min: {
13662 APValue SourceLHS, SourceRHS;
13663 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13664 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13665 return false;
13666
13667 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13668
13669 if (!DestEltTy->isIntegerType())
13670 return false;
13671
13672 unsigned SourceLen = SourceLHS.getVectorLength();
13673 SmallVector<APValue, 4> ResultElements;
13674 ResultElements.reserve(N: SourceLen);
13675
13676 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13677 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13678 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13679 switch (BuiltinOp) {
13680 case Builtin::BI__builtin_elementwise_max:
13681 ResultElements.push_back(
13682 Elt: APValue(APSInt(std::max(a: LHS, b: RHS),
13683 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13684 break;
13685 case Builtin::BI__builtin_elementwise_min:
13686 ResultElements.push_back(
13687 Elt: APValue(APSInt(std::min(a: LHS, b: RHS),
13688 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13689 break;
13690 }
13691 }
13692
13693 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13694 }
13695 case X86::BI__builtin_ia32_vpshldd128:
13696 case X86::BI__builtin_ia32_vpshldd256:
13697 case X86::BI__builtin_ia32_vpshldd512:
13698 case X86::BI__builtin_ia32_vpshldq128:
13699 case X86::BI__builtin_ia32_vpshldq256:
13700 case X86::BI__builtin_ia32_vpshldq512:
13701 case X86::BI__builtin_ia32_vpshldw128:
13702 case X86::BI__builtin_ia32_vpshldw256:
13703 case X86::BI__builtin_ia32_vpshldw512: {
13704 APValue SourceHi, SourceLo, SourceAmt;
13705 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
13706 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
13707 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13708 return false;
13709
13710 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13711 unsigned SourceLen = SourceHi.getVectorLength();
13712 SmallVector<APValue, 32> ResultElements;
13713 ResultElements.reserve(N: SourceLen);
13714
13715 APInt Amt = SourceAmt.getInt();
13716 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13717 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13718 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13719 APInt R = llvm::APIntOps::fshl(Hi, Lo, Shift: Amt);
13720 ResultElements.push_back(
13721 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13722 }
13723
13724 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13725 }
13726 case X86::BI__builtin_ia32_vpshrdd128:
13727 case X86::BI__builtin_ia32_vpshrdd256:
13728 case X86::BI__builtin_ia32_vpshrdd512:
13729 case X86::BI__builtin_ia32_vpshrdq128:
13730 case X86::BI__builtin_ia32_vpshrdq256:
13731 case X86::BI__builtin_ia32_vpshrdq512:
13732 case X86::BI__builtin_ia32_vpshrdw128:
13733 case X86::BI__builtin_ia32_vpshrdw256:
13734 case X86::BI__builtin_ia32_vpshrdw512: {
13735 // NOTE: Reversed Hi/Lo operands.
13736 APValue SourceHi, SourceLo, SourceAmt;
13737 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLo) ||
13738 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceHi) ||
13739 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13740 return false;
13741
13742 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13743 unsigned SourceLen = SourceHi.getVectorLength();
13744 SmallVector<APValue, 32> ResultElements;
13745 ResultElements.reserve(N: SourceLen);
13746
13747 APInt Amt = SourceAmt.getInt();
13748 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13749 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13750 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13751 APInt R = llvm::APIntOps::fshr(Hi, Lo, Shift: Amt);
13752 ResultElements.push_back(
13753 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13754 }
13755
13756 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13757 }
13758 case X86::BI__builtin_ia32_compressdf128_mask:
13759 case X86::BI__builtin_ia32_compressdf256_mask:
13760 case X86::BI__builtin_ia32_compressdf512_mask:
13761 case X86::BI__builtin_ia32_compressdi128_mask:
13762 case X86::BI__builtin_ia32_compressdi256_mask:
13763 case X86::BI__builtin_ia32_compressdi512_mask:
13764 case X86::BI__builtin_ia32_compresshi128_mask:
13765 case X86::BI__builtin_ia32_compresshi256_mask:
13766 case X86::BI__builtin_ia32_compresshi512_mask:
13767 case X86::BI__builtin_ia32_compressqi128_mask:
13768 case X86::BI__builtin_ia32_compressqi256_mask:
13769 case X86::BI__builtin_ia32_compressqi512_mask:
13770 case X86::BI__builtin_ia32_compresssf128_mask:
13771 case X86::BI__builtin_ia32_compresssf256_mask:
13772 case X86::BI__builtin_ia32_compresssf512_mask:
13773 case X86::BI__builtin_ia32_compresssi128_mask:
13774 case X86::BI__builtin_ia32_compresssi256_mask:
13775 case X86::BI__builtin_ia32_compresssi512_mask: {
13776 APValue Source, Passthru;
13777 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13778 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13779 return false;
13780 APSInt Mask;
13781 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13782 return false;
13783
13784 unsigned NumElts = Source.getVectorLength();
13785 SmallVector<APValue, 64> ResultElements;
13786 ResultElements.reserve(N: NumElts);
13787
13788 for (unsigned I = 0; I != NumElts; ++I) {
13789 if (Mask[I])
13790 ResultElements.push_back(Elt: Source.getVectorElt(I));
13791 }
13792 for (unsigned I = ResultElements.size(); I != NumElts; ++I) {
13793 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13794 }
13795
13796 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13797 }
13798 case X86::BI__builtin_ia32_expanddf128_mask:
13799 case X86::BI__builtin_ia32_expanddf256_mask:
13800 case X86::BI__builtin_ia32_expanddf512_mask:
13801 case X86::BI__builtin_ia32_expanddi128_mask:
13802 case X86::BI__builtin_ia32_expanddi256_mask:
13803 case X86::BI__builtin_ia32_expanddi512_mask:
13804 case X86::BI__builtin_ia32_expandhi128_mask:
13805 case X86::BI__builtin_ia32_expandhi256_mask:
13806 case X86::BI__builtin_ia32_expandhi512_mask:
13807 case X86::BI__builtin_ia32_expandqi128_mask:
13808 case X86::BI__builtin_ia32_expandqi256_mask:
13809 case X86::BI__builtin_ia32_expandqi512_mask:
13810 case X86::BI__builtin_ia32_expandsf128_mask:
13811 case X86::BI__builtin_ia32_expandsf256_mask:
13812 case X86::BI__builtin_ia32_expandsf512_mask:
13813 case X86::BI__builtin_ia32_expandsi128_mask:
13814 case X86::BI__builtin_ia32_expandsi256_mask:
13815 case X86::BI__builtin_ia32_expandsi512_mask: {
13816 APValue Source, Passthru;
13817 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13818 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13819 return false;
13820 APSInt Mask;
13821 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13822 return false;
13823
13824 unsigned NumElts = Source.getVectorLength();
13825 SmallVector<APValue, 64> ResultElements;
13826 ResultElements.reserve(N: NumElts);
13827
13828 unsigned SourceIdx = 0;
13829 for (unsigned I = 0; I != NumElts; ++I) {
13830 if (Mask[I])
13831 ResultElements.push_back(Elt: Source.getVectorElt(I: SourceIdx++));
13832 else
13833 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13834 }
13835 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13836 }
13837 case X86::BI__builtin_ia32_vpconflictsi_128:
13838 case X86::BI__builtin_ia32_vpconflictsi_256:
13839 case X86::BI__builtin_ia32_vpconflictsi_512:
13840 case X86::BI__builtin_ia32_vpconflictdi_128:
13841 case X86::BI__builtin_ia32_vpconflictdi_256:
13842 case X86::BI__builtin_ia32_vpconflictdi_512: {
13843 APValue Source;
13844
13845 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
13846 return false;
13847
13848 unsigned SourceLen = Source.getVectorLength();
13849 SmallVector<APValue, 32> ResultElements;
13850 ResultElements.reserve(N: SourceLen);
13851
13852 const auto *VecT = E->getType()->castAs<VectorType>();
13853 bool DestUnsigned =
13854 VecT->getElementType()->isUnsignedIntegerOrEnumerationType();
13855
13856 for (unsigned I = 0; I != SourceLen; ++I) {
13857 const APValue &EltI = Source.getVectorElt(I);
13858
13859 APInt ConflictMask(EltI.getInt().getBitWidth(), 0);
13860 for (unsigned J = 0; J != I; ++J) {
13861 const APValue &EltJ = Source.getVectorElt(I: J);
13862 ConflictMask.setBitVal(BitPosition: J, BitValue: EltI.getInt() == EltJ.getInt());
13863 }
13864 ResultElements.push_back(Elt: APValue(APSInt(ConflictMask, DestUnsigned)));
13865 }
13866 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13867 }
13868 case X86::BI__builtin_ia32_blendpd:
13869 case X86::BI__builtin_ia32_blendpd256:
13870 case X86::BI__builtin_ia32_blendps:
13871 case X86::BI__builtin_ia32_blendps256:
13872 case X86::BI__builtin_ia32_pblendw128:
13873 case X86::BI__builtin_ia32_pblendw256:
13874 case X86::BI__builtin_ia32_pblendd128:
13875 case X86::BI__builtin_ia32_pblendd256: {
13876 APValue SourceF, SourceT, SourceC;
13877 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13878 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13879 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13880 return false;
13881
13882 const APInt &C = SourceC.getInt();
13883 unsigned SourceLen = SourceF.getVectorLength();
13884 SmallVector<APValue, 32> ResultElements;
13885 ResultElements.reserve(N: SourceLen);
13886 for (unsigned EltNum = 0; EltNum != SourceLen; ++EltNum) {
13887 const APValue &F = SourceF.getVectorElt(I: EltNum);
13888 const APValue &T = SourceT.getVectorElt(I: EltNum);
13889 ResultElements.push_back(Elt: C[EltNum % 8] ? T : F);
13890 }
13891
13892 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13893 }
13894
13895 case X86::BI__builtin_ia32_psignb128:
13896 case X86::BI__builtin_ia32_psignb256:
13897 case X86::BI__builtin_ia32_psignw128:
13898 case X86::BI__builtin_ia32_psignw256:
13899 case X86::BI__builtin_ia32_psignd128:
13900 case X86::BI__builtin_ia32_psignd256:
13901 return EvaluateBinOpExpr([](const APInt &AElem, const APInt &BElem) {
13902 if (BElem.isZero())
13903 return APInt::getZero(numBits: AElem.getBitWidth());
13904 if (BElem.isNegative())
13905 return -AElem;
13906 return AElem;
13907 });
13908
13909 case X86::BI__builtin_ia32_blendvpd:
13910 case X86::BI__builtin_ia32_blendvpd256:
13911 case X86::BI__builtin_ia32_blendvps:
13912 case X86::BI__builtin_ia32_blendvps256:
13913 case X86::BI__builtin_ia32_pblendvb128:
13914 case X86::BI__builtin_ia32_pblendvb256: {
13915 // SSE blendv by mask signbit: "Result = C[] < 0 ? T[] : F[]".
13916 APValue SourceF, SourceT, SourceC;
13917 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13918 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13919 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13920 return false;
13921
13922 unsigned SourceLen = SourceF.getVectorLength();
13923 SmallVector<APValue, 32> ResultElements;
13924 ResultElements.reserve(N: SourceLen);
13925
13926 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13927 const APValue &F = SourceF.getVectorElt(I: EltNum);
13928 const APValue &T = SourceT.getVectorElt(I: EltNum);
13929 const APValue &C = SourceC.getVectorElt(I: EltNum);
13930 APInt M = C.isInt() ? (APInt)C.getInt() : C.getFloat().bitcastToAPInt();
13931 ResultElements.push_back(Elt: M.isNegative() ? T : F);
13932 }
13933
13934 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13935 }
13936 case X86::BI__builtin_ia32_selectb_128:
13937 case X86::BI__builtin_ia32_selectb_256:
13938 case X86::BI__builtin_ia32_selectb_512:
13939 case X86::BI__builtin_ia32_selectw_128:
13940 case X86::BI__builtin_ia32_selectw_256:
13941 case X86::BI__builtin_ia32_selectw_512:
13942 case X86::BI__builtin_ia32_selectd_128:
13943 case X86::BI__builtin_ia32_selectd_256:
13944 case X86::BI__builtin_ia32_selectd_512:
13945 case X86::BI__builtin_ia32_selectq_128:
13946 case X86::BI__builtin_ia32_selectq_256:
13947 case X86::BI__builtin_ia32_selectq_512:
13948 case X86::BI__builtin_ia32_selectph_128:
13949 case X86::BI__builtin_ia32_selectph_256:
13950 case X86::BI__builtin_ia32_selectph_512:
13951 case X86::BI__builtin_ia32_selectpbf_128:
13952 case X86::BI__builtin_ia32_selectpbf_256:
13953 case X86::BI__builtin_ia32_selectpbf_512:
13954 case X86::BI__builtin_ia32_selectps_128:
13955 case X86::BI__builtin_ia32_selectps_256:
13956 case X86::BI__builtin_ia32_selectps_512:
13957 case X86::BI__builtin_ia32_selectpd_128:
13958 case X86::BI__builtin_ia32_selectpd_256:
13959 case X86::BI__builtin_ia32_selectpd_512: {
13960 // AVX512 predicated move: "Result = Mask[] ? LHS[] : RHS[]".
13961 APValue SourceMask, SourceLHS, SourceRHS;
13962 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceMask) ||
13963 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLHS) ||
13964 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceRHS))
13965 return false;
13966
13967 APSInt Mask = SourceMask.getInt();
13968 unsigned SourceLen = SourceLHS.getVectorLength();
13969 SmallVector<APValue, 4> ResultElements;
13970 ResultElements.reserve(N: SourceLen);
13971
13972 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13973 const APValue &LHS = SourceLHS.getVectorElt(I: EltNum);
13974 const APValue &RHS = SourceRHS.getVectorElt(I: EltNum);
13975 ResultElements.push_back(Elt: Mask[EltNum] ? LHS : RHS);
13976 }
13977
13978 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13979 }
13980
13981 case X86::BI__builtin_ia32_cvtsd2ss: {
13982 APValue VecA, VecB;
13983 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13984 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB))
13985 return false;
13986
13987 SmallVector<APValue, 4> Elements;
13988
13989 APValue ResultVal;
13990 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13991 Result&: ResultVal))
13992 return false;
13993
13994 Elements.push_back(Elt: ResultVal);
13995
13996 unsigned NumEltsA = VecA.getVectorLength();
13997 for (unsigned I = 1; I < NumEltsA; ++I) {
13998 Elements.push_back(Elt: VecA.getVectorElt(I));
13999 }
14000
14001 return Success(V: Elements, E);
14002 }
14003 case X86::BI__builtin_ia32_cvtsd2ss_round_mask: {
14004 APValue VecA, VecB, VecSrc, MaskValue;
14005
14006 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
14007 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
14008 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
14009 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: MaskValue))
14010 return false;
14011
14012 unsigned Mask = MaskValue.getInt().getZExtValue();
14013 SmallVector<APValue, 4> Elements;
14014
14015 if (Mask & 1) {
14016 APValue ResultVal;
14017 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
14018 Result&: ResultVal))
14019 return false;
14020 Elements.push_back(Elt: ResultVal);
14021 } else {
14022 Elements.push_back(Elt: VecSrc.getVectorElt(I: 0));
14023 }
14024
14025 unsigned NumEltsA = VecA.getVectorLength();
14026 for (unsigned I = 1; I < NumEltsA; ++I) {
14027 Elements.push_back(Elt: VecA.getVectorElt(I));
14028 }
14029
14030 return Success(V: Elements, E);
14031 }
14032 case X86::BI__builtin_ia32_cvtpd2ps:
14033 case X86::BI__builtin_ia32_cvtpd2ps256:
14034 case X86::BI__builtin_ia32_cvtpd2ps_mask:
14035 case X86::BI__builtin_ia32_cvtpd2ps512_mask: {
14036
14037 const auto BuiltinID = BuiltinOp;
14038 bool IsMasked = (BuiltinID == X86::BI__builtin_ia32_cvtpd2ps_mask ||
14039 BuiltinID == X86::BI__builtin_ia32_cvtpd2ps512_mask);
14040
14041 APValue InputValue;
14042 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: InputValue))
14043 return false;
14044
14045 APValue MergeValue;
14046 unsigned Mask = 0xFFFFFFFF;
14047 bool NeedsMerge = false;
14048 if (IsMasked) {
14049 APValue MaskValue;
14050 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MaskValue))
14051 return false;
14052 Mask = MaskValue.getInt().getZExtValue();
14053 auto NumEltsResult = E->getType()->getAs<VectorType>()->getNumElements();
14054 for (unsigned I = 0; I < NumEltsResult; ++I) {
14055 if (!((Mask >> I) & 1)) {
14056 NeedsMerge = true;
14057 break;
14058 }
14059 }
14060 if (NeedsMerge) {
14061 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: MergeValue))
14062 return false;
14063 }
14064 }
14065
14066 unsigned NumEltsResult =
14067 E->getType()->getAs<VectorType>()->getNumElements();
14068 unsigned NumEltsInput = InputValue.getVectorLength();
14069 SmallVector<APValue, 8> Elements;
14070 for (unsigned I = 0; I < NumEltsResult; ++I) {
14071 if (IsMasked && !((Mask >> I) & 1)) {
14072 if (!NeedsMerge) {
14073 return false;
14074 }
14075 Elements.push_back(Elt: MergeValue.getVectorElt(I));
14076 continue;
14077 }
14078
14079 if (I >= NumEltsInput) {
14080 Elements.push_back(Elt: APValue(APFloat::getZero(Sem: APFloat::IEEEsingle())));
14081 continue;
14082 }
14083
14084 APValue ResultVal;
14085 if (!ConvertDoubleToFloatStrict(
14086 Info, E, OrigVal: InputValue.getVectorElt(I).getFloat(), Result&: ResultVal))
14087 return false;
14088
14089 Elements.push_back(Elt: ResultVal);
14090 }
14091 return Success(V: Elements, E);
14092 }
14093
14094 case X86::BI__builtin_ia32_shufps:
14095 case X86::BI__builtin_ia32_shufps256:
14096 case X86::BI__builtin_ia32_shufps512: {
14097 APValue R;
14098 if (!evalShuffleGeneric(
14099 Info, Call: E, Out&: R,
14100 GetSourceIndex: [](unsigned DstIdx,
14101 unsigned ShuffleMask) -> std::pair<unsigned, int> {
14102 constexpr unsigned LaneBits = 128u;
14103 unsigned NumElemPerLane = LaneBits / 32;
14104 unsigned NumSelectableElems = NumElemPerLane / 2;
14105 unsigned BitsPerElem = 2;
14106 unsigned IndexMask = (1u << BitsPerElem) - 1;
14107 unsigned MaskBits = 8;
14108 unsigned Lane = DstIdx / NumElemPerLane;
14109 unsigned ElemInLane = DstIdx % NumElemPerLane;
14110 unsigned LaneOffset = Lane * NumElemPerLane;
14111 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
14112 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
14113 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
14114 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
14115 }))
14116 return false;
14117 return Success(V: R, E);
14118 }
14119 case X86::BI__builtin_ia32_shufpd:
14120 case X86::BI__builtin_ia32_shufpd256:
14121 case X86::BI__builtin_ia32_shufpd512: {
14122 APValue R;
14123 if (!evalShuffleGeneric(
14124 Info, Call: E, Out&: R,
14125 GetSourceIndex: [](unsigned DstIdx,
14126 unsigned ShuffleMask) -> std::pair<unsigned, int> {
14127 constexpr unsigned LaneBits = 128u;
14128 unsigned NumElemPerLane = LaneBits / 64;
14129 unsigned NumSelectableElems = NumElemPerLane / 2;
14130 unsigned BitsPerElem = 1;
14131 unsigned IndexMask = (1u << BitsPerElem) - 1;
14132 unsigned MaskBits = 8;
14133 unsigned Lane = DstIdx / NumElemPerLane;
14134 unsigned ElemInLane = DstIdx % NumElemPerLane;
14135 unsigned LaneOffset = Lane * NumElemPerLane;
14136 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
14137 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
14138 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
14139 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
14140 }))
14141 return false;
14142 return Success(V: R, E);
14143 }
14144 case X86::BI__builtin_ia32_insertps128: {
14145 APValue R;
14146 if (!evalShuffleGeneric(
14147 Info, Call: E, Out&: R,
14148 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14149 // Bits [3:0]: zero mask - if bit is set, zero this element
14150 if ((Mask & (1 << DstIdx)) != 0) {
14151 return {0, -1};
14152 }
14153 // Bits [7:6]: select element from source vector Y (0-3)
14154 // Bits [5:4]: select destination position (0-3)
14155 unsigned SrcElem = (Mask >> 6) & 0x3;
14156 unsigned DstElem = (Mask >> 4) & 0x3;
14157 if (DstIdx == DstElem) {
14158 // Insert element from source vector (B) at this position
14159 return {1, static_cast<int>(SrcElem)};
14160 } else {
14161 // Copy from destination vector (A)
14162 return {0, static_cast<int>(DstIdx)};
14163 }
14164 }))
14165 return false;
14166 return Success(V: R, E);
14167 }
14168 case X86::BI__builtin_ia32_pshufb128:
14169 case X86::BI__builtin_ia32_pshufb256:
14170 case X86::BI__builtin_ia32_pshufb512: {
14171 APValue R;
14172 if (!evalShuffleGeneric(
14173 Info, Call: E, Out&: R,
14174 GetSourceIndex: [](unsigned DstIdx,
14175 unsigned ShuffleMask) -> std::pair<unsigned, int> {
14176 uint8_t Ctlb = static_cast<uint8_t>(ShuffleMask);
14177 if (Ctlb & 0x80)
14178 return std::make_pair(x: 0, y: -1);
14179
14180 unsigned LaneBase = (DstIdx / 16) * 16;
14181 unsigned SrcOffset = Ctlb & 0x0F;
14182 unsigned SrcIdx = LaneBase + SrcOffset;
14183 return std::make_pair(x: 0, y: static_cast<int>(SrcIdx));
14184 }))
14185 return false;
14186 return Success(V: R, E);
14187 }
14188
14189 case X86::BI__builtin_ia32_pshuflw:
14190 case X86::BI__builtin_ia32_pshuflw256:
14191 case X86::BI__builtin_ia32_pshuflw512: {
14192 APValue R;
14193 if (!evalShuffleGeneric(
14194 Info, Call: E, Out&: R,
14195 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14196 constexpr unsigned LaneBits = 128u;
14197 constexpr unsigned ElemBits = 16u;
14198 constexpr unsigned LaneElts = LaneBits / ElemBits;
14199 constexpr unsigned HalfSize = 4;
14200 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
14201 unsigned LaneIdx = DstIdx % LaneElts;
14202 if (LaneIdx < HalfSize) {
14203 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
14204 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
14205 }
14206 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
14207 }))
14208 return false;
14209 return Success(V: R, E);
14210 }
14211
14212 case X86::BI__builtin_ia32_pshufhw:
14213 case X86::BI__builtin_ia32_pshufhw256:
14214 case X86::BI__builtin_ia32_pshufhw512: {
14215 APValue R;
14216 if (!evalShuffleGeneric(
14217 Info, Call: E, Out&: R,
14218 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14219 constexpr unsigned LaneBits = 128u;
14220 constexpr unsigned ElemBits = 16u;
14221 constexpr unsigned LaneElts = LaneBits / ElemBits;
14222 constexpr unsigned HalfSize = 4;
14223 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
14224 unsigned LaneIdx = DstIdx % LaneElts;
14225 if (LaneIdx >= HalfSize) {
14226 unsigned Rel = LaneIdx - HalfSize;
14227 unsigned Sel = (Mask >> (2 * Rel)) & 0x3;
14228 return std::make_pair(
14229 x: 0, y: static_cast<int>(LaneBase + HalfSize + Sel));
14230 }
14231 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
14232 }))
14233 return false;
14234 return Success(V: R, E);
14235 }
14236
14237 case X86::BI__builtin_ia32_pshufd:
14238 case X86::BI__builtin_ia32_pshufd256:
14239 case X86::BI__builtin_ia32_pshufd512:
14240 case X86::BI__builtin_ia32_vpermilps:
14241 case X86::BI__builtin_ia32_vpermilps256:
14242 case X86::BI__builtin_ia32_vpermilps512: {
14243 APValue R;
14244 if (!evalShuffleGeneric(
14245 Info, Call: E, Out&: R,
14246 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14247 constexpr unsigned LaneBits = 128u;
14248 constexpr unsigned ElemBits = 32u;
14249 constexpr unsigned LaneElts = LaneBits / ElemBits;
14250 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
14251 unsigned LaneIdx = DstIdx % LaneElts;
14252 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
14253 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
14254 }))
14255 return false;
14256 return Success(V: R, E);
14257 }
14258
14259 case X86::BI__builtin_ia32_vpermilvarpd:
14260 case X86::BI__builtin_ia32_vpermilvarpd256:
14261 case X86::BI__builtin_ia32_vpermilvarpd512: {
14262 APValue R;
14263 if (!evalShuffleGeneric(
14264 Info, Call: E, Out&: R,
14265 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14266 unsigned NumElemPerLane = 2;
14267 unsigned Lane = DstIdx / NumElemPerLane;
14268 unsigned Offset = Mask & 0b10 ? 1 : 0;
14269 return std::make_pair(
14270 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
14271 }))
14272 return false;
14273 return Success(V: R, E);
14274 }
14275
14276 case X86::BI__builtin_ia32_vpermilpd:
14277 case X86::BI__builtin_ia32_vpermilpd256:
14278 case X86::BI__builtin_ia32_vpermilpd512: {
14279 APValue R;
14280 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
14281 unsigned NumElemPerLane = 2;
14282 unsigned BitsPerElem = 1;
14283 unsigned MaskBits = 8;
14284 unsigned IndexMask = 0x1;
14285 unsigned Lane = DstIdx / NumElemPerLane;
14286 unsigned LaneOffset = Lane * NumElemPerLane;
14287 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
14288 unsigned Index = (Control >> BitIndex) & IndexMask;
14289 return std::make_pair(x: 0, y: static_cast<int>(LaneOffset + Index));
14290 }))
14291 return false;
14292 return Success(V: R, E);
14293 }
14294
14295 case X86::BI__builtin_ia32_permdf256:
14296 case X86::BI__builtin_ia32_permdi256: {
14297 APValue R;
14298 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
14299 // permute4x64 operates on 4 64-bit elements
14300 // For element i (0-3), extract bits [2*i+1:2*i] from Control
14301 unsigned Index = (Control >> (2 * DstIdx)) & 0x3;
14302 return std::make_pair(x: 0, y: static_cast<int>(Index));
14303 }))
14304 return false;
14305 return Success(V: R, E);
14306 }
14307
14308 case X86::BI__builtin_ia32_vpermilvarps:
14309 case X86::BI__builtin_ia32_vpermilvarps256:
14310 case X86::BI__builtin_ia32_vpermilvarps512: {
14311 APValue R;
14312 if (!evalShuffleGeneric(
14313 Info, Call: E, Out&: R,
14314 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14315 unsigned NumElemPerLane = 4;
14316 unsigned Lane = DstIdx / NumElemPerLane;
14317 unsigned Offset = Mask & 0b11;
14318 return std::make_pair(
14319 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
14320 }))
14321 return false;
14322 return Success(V: R, E);
14323 }
14324
14325 case X86::BI__builtin_ia32_vpmultishiftqb128:
14326 case X86::BI__builtin_ia32_vpmultishiftqb256:
14327 case X86::BI__builtin_ia32_vpmultishiftqb512: {
14328 assert(E->getNumArgs() == 2);
14329
14330 APValue A, B;
14331 if (!Evaluate(Result&: A, Info, E: E->getArg(Arg: 0)) || !Evaluate(Result&: B, Info, E: E->getArg(Arg: 1)))
14332 return false;
14333
14334 assert(A.getVectorLength() == B.getVectorLength());
14335 unsigned NumBytesInQWord = 8;
14336 unsigned NumBitsInByte = 8;
14337 unsigned NumBytes = A.getVectorLength();
14338 unsigned NumQWords = NumBytes / NumBytesInQWord;
14339 SmallVector<APValue, 64> Result;
14340 Result.reserve(N: NumBytes);
14341
14342 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
14343 APInt BQWord(64, 0);
14344 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14345 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
14346 uint64_t Byte = B.getVectorElt(I: Idx).getInt().getZExtValue();
14347 BQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
14348 }
14349
14350 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14351 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
14352 uint64_t Ctrl = A.getVectorElt(I: Idx).getInt().getZExtValue() & 0x3F;
14353
14354 APInt Byte(8, 0);
14355 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
14356 Byte.setBitVal(BitPosition: BitIdx, BitValue: BQWord[(Ctrl + BitIdx) & 0x3F]);
14357 }
14358 Result.push_back(Elt: APValue(APSInt(Byte, /*isUnsigned*/ true)));
14359 }
14360 }
14361 return Success(V: APValue(Result.data(), Result.size()), E);
14362 }
14363
14364 case X86::BI__builtin_ia32_phminposuw128: {
14365 APValue Source;
14366 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
14367 return false;
14368 unsigned SourceLen = Source.getVectorLength();
14369 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14370 QualType ElemQT = VT->getElementType();
14371 unsigned ElemBitWidth = Info.Ctx.getTypeSize(T: ElemQT);
14372
14373 APInt MinIndex(ElemBitWidth, 0);
14374 APInt MinVal = Source.getVectorElt(I: 0).getInt();
14375 for (unsigned I = 1; I != SourceLen; ++I) {
14376 APInt Val = Source.getVectorElt(I).getInt();
14377 if (MinVal.ugt(RHS: Val)) {
14378 MinVal = Val;
14379 MinIndex = I;
14380 }
14381 }
14382
14383 bool ResultUnsigned = E->getCallReturnType(Ctx: Info.Ctx)
14384 ->castAs<VectorType>()
14385 ->getElementType()
14386 ->isUnsignedIntegerOrEnumerationType();
14387
14388 SmallVector<APValue, 8> Result;
14389 Result.reserve(N: SourceLen);
14390 Result.emplace_back(Args: APSInt(MinVal, ResultUnsigned));
14391 Result.emplace_back(Args: APSInt(MinIndex, ResultUnsigned));
14392 for (unsigned I = 0; I != SourceLen - 2; ++I) {
14393 Result.emplace_back(Args: APSInt(APInt(ElemBitWidth, 0), ResultUnsigned));
14394 }
14395 return Success(V: APValue(Result.data(), Result.size()), E);
14396 }
14397
14398 case X86::BI__builtin_ia32_psraq128:
14399 case X86::BI__builtin_ia32_psraq256:
14400 case X86::BI__builtin_ia32_psraq512:
14401 case X86::BI__builtin_ia32_psrad128:
14402 case X86::BI__builtin_ia32_psrad256:
14403 case X86::BI__builtin_ia32_psrad512:
14404 case X86::BI__builtin_ia32_psraw128:
14405 case X86::BI__builtin_ia32_psraw256:
14406 case X86::BI__builtin_ia32_psraw512: {
14407 APValue R;
14408 if (!evalShiftWithCount(
14409 Info, Call: E, Out&: R,
14410 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.ashr(ShiftAmt: Count); },
14411 OverflowOp: [](const APInt &Elt, unsigned Width) {
14412 return Elt.ashr(ShiftAmt: Width - 1);
14413 }))
14414 return false;
14415 return Success(V: R, E);
14416 }
14417
14418 case X86::BI__builtin_ia32_psllq128:
14419 case X86::BI__builtin_ia32_psllq256:
14420 case X86::BI__builtin_ia32_psllq512:
14421 case X86::BI__builtin_ia32_pslld128:
14422 case X86::BI__builtin_ia32_pslld256:
14423 case X86::BI__builtin_ia32_pslld512:
14424 case X86::BI__builtin_ia32_psllw128:
14425 case X86::BI__builtin_ia32_psllw256:
14426 case X86::BI__builtin_ia32_psllw512: {
14427 APValue R;
14428 if (!evalShiftWithCount(
14429 Info, Call: E, Out&: R,
14430 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.shl(shiftAmt: Count); },
14431 OverflowOp: [](const APInt &Elt, unsigned Width) {
14432 return APInt::getZero(numBits: Width);
14433 }))
14434 return false;
14435 return Success(V: R, E);
14436 }
14437
14438 case X86::BI__builtin_ia32_psrlq128:
14439 case X86::BI__builtin_ia32_psrlq256:
14440 case X86::BI__builtin_ia32_psrlq512:
14441 case X86::BI__builtin_ia32_psrld128:
14442 case X86::BI__builtin_ia32_psrld256:
14443 case X86::BI__builtin_ia32_psrld512:
14444 case X86::BI__builtin_ia32_psrlw128:
14445 case X86::BI__builtin_ia32_psrlw256:
14446 case X86::BI__builtin_ia32_psrlw512: {
14447 APValue R;
14448 if (!evalShiftWithCount(
14449 Info, Call: E, Out&: R,
14450 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.lshr(shiftAmt: Count); },
14451 OverflowOp: [](const APInt &Elt, unsigned Width) {
14452 return APInt::getZero(numBits: Width);
14453 }))
14454 return false;
14455 return Success(V: R, E);
14456 }
14457
14458 case X86::BI__builtin_ia32_pternlogd128_mask:
14459 case X86::BI__builtin_ia32_pternlogd256_mask:
14460 case X86::BI__builtin_ia32_pternlogd512_mask:
14461 case X86::BI__builtin_ia32_pternlogq128_mask:
14462 case X86::BI__builtin_ia32_pternlogq256_mask:
14463 case X86::BI__builtin_ia32_pternlogq512_mask: {
14464 APValue AValue, BValue, CValue, ImmValue, UValue;
14465 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
14466 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
14467 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
14468 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
14469 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
14470 return false;
14471
14472 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14473 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14474 APInt Imm = ImmValue.getInt();
14475 APInt U = UValue.getInt();
14476 unsigned ResultLen = AValue.getVectorLength();
14477 SmallVector<APValue, 16> ResultElements;
14478 ResultElements.reserve(N: ResultLen);
14479
14480 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
14481 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
14482 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
14483 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
14484
14485 if (U[EltNum]) {
14486 unsigned BitWidth = ALane.getBitWidth();
14487 APInt ResLane(BitWidth, 0);
14488
14489 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
14490 unsigned ABit = ALane[Bit];
14491 unsigned BBit = BLane[Bit];
14492 unsigned CBit = CLane[Bit];
14493
14494 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
14495 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
14496 }
14497 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
14498 } else {
14499 ResultElements.push_back(Elt: APValue(APSInt(ALane, DestUnsigned)));
14500 }
14501 }
14502 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14503 }
14504 case X86::BI__builtin_ia32_pternlogd128_maskz:
14505 case X86::BI__builtin_ia32_pternlogd256_maskz:
14506 case X86::BI__builtin_ia32_pternlogd512_maskz:
14507 case X86::BI__builtin_ia32_pternlogq128_maskz:
14508 case X86::BI__builtin_ia32_pternlogq256_maskz:
14509 case X86::BI__builtin_ia32_pternlogq512_maskz: {
14510 APValue AValue, BValue, CValue, ImmValue, UValue;
14511 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
14512 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
14513 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
14514 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
14515 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
14516 return false;
14517
14518 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14519 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14520 APInt Imm = ImmValue.getInt();
14521 APInt U = UValue.getInt();
14522 unsigned ResultLen = AValue.getVectorLength();
14523 SmallVector<APValue, 16> ResultElements;
14524 ResultElements.reserve(N: ResultLen);
14525
14526 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
14527 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
14528 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
14529 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
14530
14531 unsigned BitWidth = ALane.getBitWidth();
14532 APInt ResLane(BitWidth, 0);
14533
14534 if (U[EltNum]) {
14535 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
14536 unsigned ABit = ALane[Bit];
14537 unsigned BBit = BLane[Bit];
14538 unsigned CBit = CLane[Bit];
14539
14540 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
14541 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
14542 }
14543 }
14544 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
14545 }
14546 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14547 }
14548
14549 case Builtin::BI__builtin_elementwise_clzg:
14550 case Builtin::BI__builtin_elementwise_ctzg: {
14551 APValue SourceLHS;
14552 std::optional<APValue> Fallback;
14553 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS))
14554 return false;
14555 if (E->getNumArgs() > 1) {
14556 APValue FallbackTmp;
14557 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: FallbackTmp))
14558 return false;
14559 Fallback = FallbackTmp;
14560 }
14561
14562 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14563 unsigned SourceLen = SourceLHS.getVectorLength();
14564 SmallVector<APValue, 4> ResultElements;
14565 ResultElements.reserve(N: SourceLen);
14566
14567 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14568 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
14569 if (!LHS) {
14570 // Without a fallback, a zero element is undefined
14571 if (!Fallback) {
14572 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
14573 << /*IsTrailing=*/(BuiltinOp ==
14574 Builtin::BI__builtin_elementwise_ctzg);
14575 return false;
14576 }
14577 ResultElements.push_back(Elt: Fallback->getVectorElt(I: EltNum));
14578 continue;
14579 }
14580 switch (BuiltinOp) {
14581 case Builtin::BI__builtin_elementwise_clzg:
14582 ResultElements.push_back(Elt: APValue(
14583 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countl_zero()),
14584 DestEltTy->isUnsignedIntegerOrEnumerationType())));
14585 break;
14586 case Builtin::BI__builtin_elementwise_ctzg:
14587 ResultElements.push_back(Elt: APValue(
14588 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countr_zero()),
14589 DestEltTy->isUnsignedIntegerOrEnumerationType())));
14590 break;
14591 }
14592 }
14593
14594 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14595 }
14596
14597 case Builtin::BI__builtin_elementwise_fma: {
14598 APValue SourceX, SourceY, SourceZ;
14599 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceX) ||
14600 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceY) ||
14601 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceZ))
14602 return false;
14603
14604 unsigned SourceLen = SourceX.getVectorLength();
14605 SmallVector<APValue> ResultElements;
14606 ResultElements.reserve(N: SourceLen);
14607 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14608 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14609 const APFloat &X = SourceX.getVectorElt(I: EltNum).getFloat();
14610 const APFloat &Y = SourceY.getVectorElt(I: EltNum).getFloat();
14611 const APFloat &Z = SourceZ.getVectorElt(I: EltNum).getFloat();
14612 APFloat Result(X);
14613 (void)Result.fusedMultiplyAdd(Multiplicand: Y, Addend: Z, RM);
14614 ResultElements.push_back(Elt: APValue(Result));
14615 }
14616 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14617 }
14618
14619 case clang::X86::BI__builtin_ia32_phaddw128:
14620 case clang::X86::BI__builtin_ia32_phaddw256:
14621 case clang::X86::BI__builtin_ia32_phaddd128:
14622 case clang::X86::BI__builtin_ia32_phaddd256:
14623 case clang::X86::BI__builtin_ia32_phaddsw128:
14624 case clang::X86::BI__builtin_ia32_phaddsw256:
14625
14626 case clang::X86::BI__builtin_ia32_phsubw128:
14627 case clang::X86::BI__builtin_ia32_phsubw256:
14628 case clang::X86::BI__builtin_ia32_phsubd128:
14629 case clang::X86::BI__builtin_ia32_phsubd256:
14630 case clang::X86::BI__builtin_ia32_phsubsw128:
14631 case clang::X86::BI__builtin_ia32_phsubsw256: {
14632 APValue SourceLHS, SourceRHS;
14633 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14634 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14635 return false;
14636 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14637 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14638
14639 unsigned NumElts = SourceLHS.getVectorLength();
14640 unsigned EltBits = Info.Ctx.getIntWidth(T: DestEltTy);
14641 unsigned EltsPerLane = 128 / EltBits;
14642 SmallVector<APValue, 4> ResultElements;
14643 ResultElements.reserve(N: NumElts);
14644
14645 for (unsigned LaneStart = 0; LaneStart != NumElts;
14646 LaneStart += EltsPerLane) {
14647 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14648 APSInt LHSA = SourceLHS.getVectorElt(I: LaneStart + I).getInt();
14649 APSInt LHSB = SourceLHS.getVectorElt(I: LaneStart + I + 1).getInt();
14650 switch (BuiltinOp) {
14651 case clang::X86::BI__builtin_ia32_phaddw128:
14652 case clang::X86::BI__builtin_ia32_phaddw256:
14653 case clang::X86::BI__builtin_ia32_phaddd128:
14654 case clang::X86::BI__builtin_ia32_phaddd256: {
14655 APSInt Res(LHSA + LHSB, DestUnsigned);
14656 ResultElements.push_back(Elt: APValue(Res));
14657 break;
14658 }
14659 case clang::X86::BI__builtin_ia32_phaddsw128:
14660 case clang::X86::BI__builtin_ia32_phaddsw256: {
14661 APSInt Res(LHSA.sadd_sat(RHS: LHSB));
14662 ResultElements.push_back(Elt: APValue(Res));
14663 break;
14664 }
14665 case clang::X86::BI__builtin_ia32_phsubw128:
14666 case clang::X86::BI__builtin_ia32_phsubw256:
14667 case clang::X86::BI__builtin_ia32_phsubd128:
14668 case clang::X86::BI__builtin_ia32_phsubd256: {
14669 APSInt Res(LHSA - LHSB, DestUnsigned);
14670 ResultElements.push_back(Elt: APValue(Res));
14671 break;
14672 }
14673 case clang::X86::BI__builtin_ia32_phsubsw128:
14674 case clang::X86::BI__builtin_ia32_phsubsw256: {
14675 APSInt Res(LHSA.ssub_sat(RHS: LHSB));
14676 ResultElements.push_back(Elt: APValue(Res));
14677 break;
14678 }
14679 }
14680 }
14681 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14682 APSInt RHSA = SourceRHS.getVectorElt(I: LaneStart + I).getInt();
14683 APSInt RHSB = SourceRHS.getVectorElt(I: LaneStart + I + 1).getInt();
14684 switch (BuiltinOp) {
14685 case clang::X86::BI__builtin_ia32_phaddw128:
14686 case clang::X86::BI__builtin_ia32_phaddw256:
14687 case clang::X86::BI__builtin_ia32_phaddd128:
14688 case clang::X86::BI__builtin_ia32_phaddd256: {
14689 APSInt Res(RHSA + RHSB, DestUnsigned);
14690 ResultElements.push_back(Elt: APValue(Res));
14691 break;
14692 }
14693 case clang::X86::BI__builtin_ia32_phaddsw128:
14694 case clang::X86::BI__builtin_ia32_phaddsw256: {
14695 APSInt Res(RHSA.sadd_sat(RHS: RHSB));
14696 ResultElements.push_back(Elt: APValue(Res));
14697 break;
14698 }
14699 case clang::X86::BI__builtin_ia32_phsubw128:
14700 case clang::X86::BI__builtin_ia32_phsubw256:
14701 case clang::X86::BI__builtin_ia32_phsubd128:
14702 case clang::X86::BI__builtin_ia32_phsubd256: {
14703 APSInt Res(RHSA - RHSB, DestUnsigned);
14704 ResultElements.push_back(Elt: APValue(Res));
14705 break;
14706 }
14707 case clang::X86::BI__builtin_ia32_phsubsw128:
14708 case clang::X86::BI__builtin_ia32_phsubsw256: {
14709 APSInt Res(RHSA.ssub_sat(RHS: RHSB));
14710 ResultElements.push_back(Elt: APValue(Res));
14711 break;
14712 }
14713 }
14714 }
14715 }
14716 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14717 }
14718 case clang::X86::BI__builtin_ia32_haddpd:
14719 case clang::X86::BI__builtin_ia32_haddps:
14720 case clang::X86::BI__builtin_ia32_haddps256:
14721 case clang::X86::BI__builtin_ia32_haddpd256:
14722 case clang::X86::BI__builtin_ia32_hsubpd:
14723 case clang::X86::BI__builtin_ia32_hsubps:
14724 case clang::X86::BI__builtin_ia32_hsubps256:
14725 case clang::X86::BI__builtin_ia32_hsubpd256: {
14726 APValue SourceLHS, SourceRHS;
14727 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14728 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14729 return false;
14730 unsigned NumElts = SourceLHS.getVectorLength();
14731 SmallVector<APValue, 4> ResultElements;
14732 ResultElements.reserve(N: NumElts);
14733 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14734 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14735 unsigned EltBits = Info.Ctx.getTypeSize(T: DestEltTy);
14736 unsigned NumLanes = NumElts * EltBits / 128;
14737 unsigned NumElemsPerLane = NumElts / NumLanes;
14738 unsigned HalfElemsPerLane = NumElemsPerLane / 2;
14739
14740 for (unsigned L = 0; L != NumElts; L += NumElemsPerLane) {
14741 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14742 APFloat LHSA = SourceLHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14743 APFloat LHSB = SourceLHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14744 switch (BuiltinOp) {
14745 case clang::X86::BI__builtin_ia32_haddpd:
14746 case clang::X86::BI__builtin_ia32_haddps:
14747 case clang::X86::BI__builtin_ia32_haddps256:
14748 case clang::X86::BI__builtin_ia32_haddpd256:
14749 LHSA.add(RHS: LHSB, RM);
14750 break;
14751 case clang::X86::BI__builtin_ia32_hsubpd:
14752 case clang::X86::BI__builtin_ia32_hsubps:
14753 case clang::X86::BI__builtin_ia32_hsubps256:
14754 case clang::X86::BI__builtin_ia32_hsubpd256:
14755 LHSA.subtract(RHS: LHSB, RM);
14756 break;
14757 }
14758 ResultElements.push_back(Elt: APValue(LHSA));
14759 }
14760 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14761 APFloat RHSA = SourceRHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14762 APFloat RHSB = SourceRHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14763 switch (BuiltinOp) {
14764 case clang::X86::BI__builtin_ia32_haddpd:
14765 case clang::X86::BI__builtin_ia32_haddps:
14766 case clang::X86::BI__builtin_ia32_haddps256:
14767 case clang::X86::BI__builtin_ia32_haddpd256:
14768 RHSA.add(RHS: RHSB, RM);
14769 break;
14770 case clang::X86::BI__builtin_ia32_hsubpd:
14771 case clang::X86::BI__builtin_ia32_hsubps:
14772 case clang::X86::BI__builtin_ia32_hsubps256:
14773 case clang::X86::BI__builtin_ia32_hsubpd256:
14774 RHSA.subtract(RHS: RHSB, RM);
14775 break;
14776 }
14777 ResultElements.push_back(Elt: APValue(RHSA));
14778 }
14779 }
14780 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14781 }
14782 case clang::X86::BI__builtin_ia32_addsubpd:
14783 case clang::X86::BI__builtin_ia32_addsubps:
14784 case clang::X86::BI__builtin_ia32_addsubpd256:
14785 case clang::X86::BI__builtin_ia32_addsubps256: {
14786 // Addsub: alternates between subtraction and addition
14787 // Result[i] = (i % 2 == 0) ? (a[i] - b[i]) : (a[i] + b[i])
14788 APValue SourceLHS, SourceRHS;
14789 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14790 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14791 return false;
14792 unsigned NumElems = SourceLHS.getVectorLength();
14793 SmallVector<APValue, 8> ResultElements;
14794 ResultElements.reserve(N: NumElems);
14795 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14796
14797 for (unsigned I = 0; I != NumElems; ++I) {
14798 APFloat LHS = SourceLHS.getVectorElt(I).getFloat();
14799 APFloat RHS = SourceRHS.getVectorElt(I).getFloat();
14800 if (I % 2 == 0) {
14801 // Even indices: subtract
14802 LHS.subtract(RHS, RM);
14803 } else {
14804 // Odd indices: add
14805 LHS.add(RHS, RM);
14806 }
14807 ResultElements.push_back(Elt: APValue(LHS));
14808 }
14809 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14810 }
14811 case clang::X86::BI__builtin_ia32_pclmulqdq128:
14812 case clang::X86::BI__builtin_ia32_pclmulqdq256:
14813 case clang::X86::BI__builtin_ia32_pclmulqdq512: {
14814 // PCLMULQDQ: carry-less multiplication of selected 64-bit halves
14815 // imm8 bit 0: selects lower (0) or upper (1) 64 bits of first operand
14816 // imm8 bit 4: selects lower (0) or upper (1) 64 bits of second operand
14817 APValue SourceLHS, SourceRHS;
14818 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14819 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14820 return false;
14821
14822 APSInt Imm8;
14823 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm8, Info))
14824 return false;
14825
14826 // Extract bits 0 and 4 from imm8
14827 bool SelectUpperA = (Imm8 & 0x01) != 0;
14828 bool SelectUpperB = (Imm8 & 0x10) != 0;
14829
14830 unsigned NumElems = SourceLHS.getVectorLength();
14831 SmallVector<APValue, 8> ResultElements;
14832 ResultElements.reserve(N: NumElems);
14833 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14834 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14835
14836 // Process each 128-bit lane
14837 for (unsigned Lane = 0; Lane < NumElems; Lane += 2) {
14838 // Get the two 64-bit halves of the first operand
14839 APSInt A0 = SourceLHS.getVectorElt(I: Lane + 0).getInt();
14840 APSInt A1 = SourceLHS.getVectorElt(I: Lane + 1).getInt();
14841 // Get the two 64-bit halves of the second operand
14842 APSInt B0 = SourceRHS.getVectorElt(I: Lane + 0).getInt();
14843 APSInt B1 = SourceRHS.getVectorElt(I: Lane + 1).getInt();
14844
14845 // Select the appropriate 64-bit values based on imm8
14846 APInt A = SelectUpperA ? A1 : A0;
14847 APInt B = SelectUpperB ? B1 : B0;
14848
14849 // Extend both operands to 128 bits for carry-less multiplication
14850 APInt A128 = A.zext(width: 128);
14851 APInt B128 = B.zext(width: 128);
14852
14853 // Use APIntOps::clmul for carry-less multiplication
14854 APInt Result = llvm::APIntOps::clmul(LHS: A128, RHS: B128);
14855
14856 // Split the 128-bit result into two 64-bit halves
14857 APSInt ResultLow(Result.extractBits(numBits: 64, bitPosition: 0), DestUnsigned);
14858 APSInt ResultHigh(Result.extractBits(numBits: 64, bitPosition: 64), DestUnsigned);
14859
14860 ResultElements.push_back(Elt: APValue(ResultLow));
14861 ResultElements.push_back(Elt: APValue(ResultHigh));
14862 }
14863
14864 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14865 }
14866 case Builtin::BI__builtin_elementwise_clmul:
14867 return EvaluateBinOpExpr(llvm::APIntOps::clmul);
14868 case Builtin::BI__builtin_elementwise_pext:
14869 return EvaluateBinOpExpr(llvm::APIntOps::pext);
14870 case Builtin::BI__builtin_elementwise_pdep:
14871 return EvaluateBinOpExpr(llvm::APIntOps::pdep);
14872 case Builtin::BI__builtin_elementwise_fshl:
14873 case Builtin::BI__builtin_elementwise_fshr: {
14874 APValue SourceHi, SourceLo, SourceShift;
14875 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
14876 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
14877 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceShift))
14878 return false;
14879
14880 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14881 if (!DestEltTy->isIntegerType())
14882 return false;
14883
14884 unsigned SourceLen = SourceHi.getVectorLength();
14885 SmallVector<APValue> ResultElements;
14886 ResultElements.reserve(N: SourceLen);
14887 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14888 const APSInt &Hi = SourceHi.getVectorElt(I: EltNum).getInt();
14889 const APSInt &Lo = SourceLo.getVectorElt(I: EltNum).getInt();
14890 const APSInt &Shift = SourceShift.getVectorElt(I: EltNum).getInt();
14891 switch (BuiltinOp) {
14892 case Builtin::BI__builtin_elementwise_fshl:
14893 ResultElements.push_back(Elt: APValue(
14894 APSInt(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned())));
14895 break;
14896 case Builtin::BI__builtin_elementwise_fshr:
14897 ResultElements.push_back(Elt: APValue(
14898 APSInt(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned())));
14899 break;
14900 }
14901 }
14902
14903 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14904 }
14905
14906 case X86::BI__builtin_ia32_shuf_f32x4_256:
14907 case X86::BI__builtin_ia32_shuf_i32x4_256:
14908 case X86::BI__builtin_ia32_shuf_f64x2_256:
14909 case X86::BI__builtin_ia32_shuf_i64x2_256:
14910 case X86::BI__builtin_ia32_shuf_f32x4:
14911 case X86::BI__builtin_ia32_shuf_i32x4:
14912 case X86::BI__builtin_ia32_shuf_f64x2:
14913 case X86::BI__builtin_ia32_shuf_i64x2: {
14914 APValue SourceA, SourceB;
14915 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
14916 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB))
14917 return false;
14918
14919 APSInt Imm;
14920 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14921 return false;
14922
14923 // Destination and sources A, B all have the same type.
14924 unsigned NumElems = SourceA.getVectorLength();
14925 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14926 QualType ElemQT = VT->getElementType();
14927 unsigned ElemBits = Info.Ctx.getTypeSize(T: ElemQT);
14928 unsigned LaneBits = 128u;
14929 unsigned NumLanes = (NumElems * ElemBits) / LaneBits;
14930 unsigned NumElemsPerLane = LaneBits / ElemBits;
14931
14932 unsigned DstLen = SourceA.getVectorLength();
14933 SmallVector<APValue, 16> ResultElements;
14934 ResultElements.reserve(N: DstLen);
14935
14936 APValue R;
14937 if (!evalShuffleGeneric(
14938 Info, Call: E, Out&: R,
14939 GetSourceIndex: [NumLanes, NumElemsPerLane](unsigned DstIdx, unsigned ShuffleMask)
14940 -> std::pair<unsigned, int> {
14941 // DstIdx determines source. ShuffleMask selects lane in source.
14942 unsigned BitsPerElem = NumLanes / 2;
14943 unsigned IndexMask = (1u << BitsPerElem) - 1;
14944 unsigned Lane = DstIdx / NumElemsPerLane;
14945 unsigned SrcIdx = (Lane < NumLanes / 2) ? 0 : 1;
14946 unsigned BitIdx = BitsPerElem * Lane;
14947 unsigned SrcLaneIdx = (ShuffleMask >> BitIdx) & IndexMask;
14948 unsigned ElemInLane = DstIdx % NumElemsPerLane;
14949 unsigned IdxToPick = SrcLaneIdx * NumElemsPerLane + ElemInLane;
14950 return {SrcIdx, IdxToPick};
14951 }))
14952 return false;
14953 return Success(V: R, E);
14954 }
14955
14956 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14957 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14958 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi:
14959 case X86::BI__builtin_ia32_vgf2p8affineqb_v16qi:
14960 case X86::BI__builtin_ia32_vgf2p8affineqb_v32qi:
14961 case X86::BI__builtin_ia32_vgf2p8affineqb_v64qi: {
14962
14963 APValue X, A;
14964 APSInt Imm;
14965 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: X) ||
14966 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: A) ||
14967 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14968 return false;
14969
14970 assert(X.isVector() && A.isVector());
14971 assert(X.getVectorLength() == A.getVectorLength());
14972
14973 bool IsInverse = false;
14974 switch (BuiltinOp) {
14975 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14976 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14977 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi: {
14978 IsInverse = true;
14979 }
14980 }
14981
14982 unsigned NumBitsInByte = 8;
14983 unsigned NumBytesInQWord = 8;
14984 unsigned NumBitsInQWord = 64;
14985 unsigned NumBytes = A.getVectorLength();
14986 unsigned NumQWords = NumBytes / NumBytesInQWord;
14987 SmallVector<APValue, 64> Result;
14988 Result.reserve(N: NumBytes);
14989
14990 // computing A*X + Imm
14991 for (unsigned QWordIdx = 0; QWordIdx != NumQWords; ++QWordIdx) {
14992 // Extract the QWords from X, A
14993 APInt XQWord(NumBitsInQWord, 0);
14994 APInt AQWord(NumBitsInQWord, 0);
14995 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14996 unsigned Idx = QWordIdx * NumBytesInQWord + ByteIdx;
14997 APInt XByte = X.getVectorElt(I: Idx).getInt();
14998 APInt AByte = A.getVectorElt(I: Idx).getInt();
14999 XQWord.insertBits(SubBits: XByte, bitPosition: ByteIdx * NumBitsInByte);
15000 AQWord.insertBits(SubBits: AByte, bitPosition: ByteIdx * NumBitsInByte);
15001 }
15002
15003 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
15004 uint8_t XByte =
15005 XQWord.lshr(shiftAmt: ByteIdx * NumBitsInByte).getLoBits(numBits: 8).getZExtValue();
15006 Result.push_back(Elt: APValue(APSInt(
15007 APInt(8, GFNIAffine(XByte, AQword: AQWord, Imm, Inverse: IsInverse)), false)));
15008 }
15009 }
15010
15011 return Success(V: APValue(Result.data(), Result.size()), E);
15012 }
15013
15014 case X86::BI__builtin_ia32_vgf2p8mulb_v16qi:
15015 case X86::BI__builtin_ia32_vgf2p8mulb_v32qi:
15016 case X86::BI__builtin_ia32_vgf2p8mulb_v64qi: {
15017 APValue A, B;
15018 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
15019 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
15020 return false;
15021
15022 assert(A.isVector() && B.isVector());
15023 assert(A.getVectorLength() == B.getVectorLength());
15024
15025 unsigned NumBytes = A.getVectorLength();
15026 SmallVector<APValue, 64> Result;
15027 Result.reserve(N: NumBytes);
15028
15029 for (unsigned ByteIdx = 0; ByteIdx != NumBytes; ++ByteIdx) {
15030 uint8_t AByte = A.getVectorElt(I: ByteIdx).getInt().getZExtValue();
15031 uint8_t BByte = B.getVectorElt(I: ByteIdx).getInt().getZExtValue();
15032 Result.push_back(Elt: APValue(
15033 APSInt(APInt(8, GFNIMul(AByte, BByte)), /*IsUnsigned=*/false)));
15034 }
15035
15036 return Success(V: APValue(Result.data(), Result.size()), E);
15037 }
15038
15039 case X86::BI__builtin_ia32_insertf32x4_256:
15040 case X86::BI__builtin_ia32_inserti32x4_256:
15041 case X86::BI__builtin_ia32_insertf64x2_256:
15042 case X86::BI__builtin_ia32_inserti64x2_256:
15043 case X86::BI__builtin_ia32_insertf32x4:
15044 case X86::BI__builtin_ia32_inserti32x4:
15045 case X86::BI__builtin_ia32_insertf64x2_512:
15046 case X86::BI__builtin_ia32_inserti64x2_512:
15047 case X86::BI__builtin_ia32_insertf32x8:
15048 case X86::BI__builtin_ia32_inserti32x8:
15049 case X86::BI__builtin_ia32_insertf64x4:
15050 case X86::BI__builtin_ia32_inserti64x4:
15051 case X86::BI__builtin_ia32_vinsertf128_ps256:
15052 case X86::BI__builtin_ia32_vinsertf128_pd256:
15053 case X86::BI__builtin_ia32_vinsertf128_si256:
15054 case X86::BI__builtin_ia32_insert128i256: {
15055 APValue SourceDst, SourceSub;
15056 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceDst) ||
15057 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceSub))
15058 return false;
15059
15060 APSInt Imm;
15061 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
15062 return false;
15063
15064 assert(SourceDst.isVector() && SourceSub.isVector());
15065 unsigned DstLen = SourceDst.getVectorLength();
15066 unsigned SubLen = SourceSub.getVectorLength();
15067 assert(SubLen != 0 && DstLen != 0 && (DstLen % SubLen) == 0);
15068 unsigned NumLanes = DstLen / SubLen;
15069 unsigned LaneIdx = (Imm.getZExtValue() % NumLanes) * SubLen;
15070
15071 SmallVector<APValue, 16> ResultElements;
15072 ResultElements.reserve(N: DstLen);
15073
15074 for (unsigned EltNum = 0; EltNum < DstLen; ++EltNum) {
15075 if (EltNum >= LaneIdx && EltNum < LaneIdx + SubLen)
15076 ResultElements.push_back(Elt: SourceSub.getVectorElt(I: EltNum - LaneIdx));
15077 else
15078 ResultElements.push_back(Elt: SourceDst.getVectorElt(I: EltNum));
15079 }
15080
15081 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15082 }
15083
15084 case clang::X86::BI__builtin_ia32_vec_set_v4hi:
15085 case clang::X86::BI__builtin_ia32_vec_set_v16qi:
15086 case clang::X86::BI__builtin_ia32_vec_set_v8hi:
15087 case clang::X86::BI__builtin_ia32_vec_set_v4si:
15088 case clang::X86::BI__builtin_ia32_vec_set_v2di:
15089 case clang::X86::BI__builtin_ia32_vec_set_v32qi:
15090 case clang::X86::BI__builtin_ia32_vec_set_v16hi:
15091 case clang::X86::BI__builtin_ia32_vec_set_v8si:
15092 case clang::X86::BI__builtin_ia32_vec_set_v4di: {
15093 APValue VecVal;
15094 APSInt Scalar, IndexAPS;
15095 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: VecVal, Info) ||
15096 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Scalar, Info) ||
15097 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: IndexAPS, Info))
15098 return false;
15099
15100 QualType ElemTy = E->getType()->castAs<VectorType>()->getElementType();
15101 unsigned ElemWidth = Info.Ctx.getIntWidth(T: ElemTy);
15102 bool ElemUnsigned = ElemTy->isUnsignedIntegerOrEnumerationType();
15103 Scalar.setIsUnsigned(ElemUnsigned);
15104 APSInt ElemAPS = Scalar.extOrTrunc(width: ElemWidth);
15105 APValue ElemAV(ElemAPS);
15106
15107 unsigned NumElems = VecVal.getVectorLength();
15108 unsigned Index =
15109 static_cast<unsigned>(IndexAPS.getZExtValue() & (NumElems - 1));
15110
15111 SmallVector<APValue, 4> Elems;
15112 Elems.reserve(N: NumElems);
15113 for (unsigned ElemNum = 0; ElemNum != NumElems; ++ElemNum)
15114 Elems.push_back(Elt: ElemNum == Index ? ElemAV : VecVal.getVectorElt(I: ElemNum));
15115
15116 return Success(V: APValue(Elems.data(), NumElems), E);
15117 }
15118
15119 case X86::BI__builtin_ia32_pslldqi128_byteshift:
15120 case X86::BI__builtin_ia32_pslldqi256_byteshift:
15121 case X86::BI__builtin_ia32_pslldqi512_byteshift: {
15122 APValue R;
15123 if (!evalShuffleGeneric(
15124 Info, Call: E, Out&: R,
15125 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
15126 unsigned LaneBase = (DstIdx / 16) * 16;
15127 unsigned LaneIdx = DstIdx % 16;
15128 if (LaneIdx < Shift)
15129 return std::make_pair(x: 0, y: -1);
15130
15131 return std::make_pair(
15132 x: 0, y: static_cast<int>(LaneBase + LaneIdx - Shift));
15133 }))
15134 return false;
15135 return Success(V: R, E);
15136 }
15137
15138 case X86::BI__builtin_ia32_psrldqi128_byteshift:
15139 case X86::BI__builtin_ia32_psrldqi256_byteshift:
15140 case X86::BI__builtin_ia32_psrldqi512_byteshift: {
15141 APValue R;
15142 if (!evalShuffleGeneric(
15143 Info, Call: E, Out&: R,
15144 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
15145 unsigned LaneBase = (DstIdx / 16) * 16;
15146 unsigned LaneIdx = DstIdx % 16;
15147 if (LaneIdx + Shift < 16)
15148 return std::make_pair(
15149 x: 0, y: static_cast<int>(LaneBase + LaneIdx + Shift));
15150
15151 return std::make_pair(x: 0, y: -1);
15152 }))
15153 return false;
15154 return Success(V: R, E);
15155 }
15156
15157 case X86::BI__builtin_ia32_palignr128:
15158 case X86::BI__builtin_ia32_palignr256:
15159 case X86::BI__builtin_ia32_palignr512: {
15160 APValue R;
15161 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Shift) {
15162 // Default to -1 → zero-fill this destination element
15163 unsigned VecIdx = 1;
15164 int ElemIdx = -1;
15165
15166 int Lane = DstIdx / 16;
15167 int Offset = DstIdx % 16;
15168
15169 // Elements come from VecB first, then VecA after the shift boundary
15170 unsigned ShiftedIdx = Offset + (Shift & 0xFF);
15171 if (ShiftedIdx < 16) { // from VecB
15172 ElemIdx = ShiftedIdx + (Lane * 16);
15173 } else if (ShiftedIdx < 32) { // from VecA
15174 VecIdx = 0;
15175 ElemIdx = (ShiftedIdx - 16) + (Lane * 16);
15176 }
15177
15178 return std::pair<unsigned, int>{VecIdx, ElemIdx};
15179 }))
15180 return false;
15181 return Success(V: R, E);
15182 }
15183 case X86::BI__builtin_ia32_alignd128:
15184 case X86::BI__builtin_ia32_alignd256:
15185 case X86::BI__builtin_ia32_alignd512:
15186 case X86::BI__builtin_ia32_alignq128:
15187 case X86::BI__builtin_ia32_alignq256:
15188 case X86::BI__builtin_ia32_alignq512: {
15189 APValue R;
15190 unsigned NumElems = E->getType()->castAs<VectorType>()->getNumElements();
15191 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15192 GetSourceIndex: [NumElems](unsigned DstIdx, unsigned Shift) {
15193 unsigned Imm = Shift & 0xFF;
15194 unsigned EffectiveShift = Imm & (NumElems - 1);
15195 unsigned SourcePos = DstIdx + EffectiveShift;
15196 unsigned VecIdx = SourcePos < NumElems ? 1 : 0;
15197 unsigned ElemIdx = SourcePos & (NumElems - 1);
15198
15199 return std::pair<unsigned, int>{
15200 VecIdx, static_cast<int>(ElemIdx)};
15201 }))
15202 return false;
15203 return Success(V: R, E);
15204 }
15205 case X86::BI__builtin_ia32_permvarsi256:
15206 case X86::BI__builtin_ia32_permvarsf256:
15207 case X86::BI__builtin_ia32_permvardf512:
15208 case X86::BI__builtin_ia32_permvardi512:
15209 case X86::BI__builtin_ia32_permvarhi128: {
15210 APValue R;
15211 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15212 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15213 int Offset = ShuffleMask & 0x7;
15214 return std::pair<unsigned, int>{0, Offset};
15215 }))
15216 return false;
15217 return Success(V: R, E);
15218 }
15219 case X86::BI__builtin_ia32_permvarqi128:
15220 case X86::BI__builtin_ia32_permvarhi256:
15221 case X86::BI__builtin_ia32_permvarsi512:
15222 case X86::BI__builtin_ia32_permvarsf512: {
15223 APValue R;
15224 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15225 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15226 int Offset = ShuffleMask & 0xF;
15227 return std::pair<unsigned, int>{0, Offset};
15228 }))
15229 return false;
15230 return Success(V: R, E);
15231 }
15232 case X86::BI__builtin_ia32_permvardi256:
15233 case X86::BI__builtin_ia32_permvardf256: {
15234 APValue R;
15235 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15236 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15237 int Offset = ShuffleMask & 0x3;
15238 return std::pair<unsigned, int>{0, Offset};
15239 }))
15240 return false;
15241 return Success(V: R, E);
15242 }
15243 case X86::BI__builtin_ia32_permvarqi256:
15244 case X86::BI__builtin_ia32_permvarhi512: {
15245 APValue R;
15246 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15247 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15248 int Offset = ShuffleMask & 0x1F;
15249 return std::pair<unsigned, int>{0, Offset};
15250 }))
15251 return false;
15252 return Success(V: R, E);
15253 }
15254 case X86::BI__builtin_ia32_permvarqi512: {
15255 APValue R;
15256 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15257 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15258 int Offset = ShuffleMask & 0x3F;
15259 return std::pair<unsigned, int>{0, Offset};
15260 }))
15261 return false;
15262 return Success(V: R, E);
15263 }
15264 case X86::BI__builtin_ia32_vpermi2varq128:
15265 case X86::BI__builtin_ia32_vpermi2varpd128: {
15266 APValue R;
15267 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15268 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15269 int Offset = ShuffleMask & 0x1;
15270 unsigned SrcIdx = (ShuffleMask >> 1) & 0x1;
15271 return std::pair<unsigned, int>{SrcIdx, Offset};
15272 }))
15273 return false;
15274 return Success(V: R, E);
15275 }
15276 case X86::BI__builtin_ia32_vpermi2vard128:
15277 case X86::BI__builtin_ia32_vpermi2varps128:
15278 case X86::BI__builtin_ia32_vpermi2varq256:
15279 case X86::BI__builtin_ia32_vpermi2varpd256: {
15280 APValue R;
15281 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15282 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15283 int Offset = ShuffleMask & 0x3;
15284 unsigned SrcIdx = (ShuffleMask >> 2) & 0x1;
15285 return std::pair<unsigned, int>{SrcIdx, Offset};
15286 }))
15287 return false;
15288 return Success(V: R, E);
15289 }
15290 case X86::BI__builtin_ia32_vpermi2varhi128:
15291 case X86::BI__builtin_ia32_vpermi2vard256:
15292 case X86::BI__builtin_ia32_vpermi2varps256:
15293 case X86::BI__builtin_ia32_vpermi2varq512:
15294 case X86::BI__builtin_ia32_vpermi2varpd512: {
15295 APValue R;
15296 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15297 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15298 int Offset = ShuffleMask & 0x7;
15299 unsigned SrcIdx = (ShuffleMask >> 3) & 0x1;
15300 return std::pair<unsigned, int>{SrcIdx, Offset};
15301 }))
15302 return false;
15303 return Success(V: R, E);
15304 }
15305 case X86::BI__builtin_ia32_vpermi2varqi128:
15306 case X86::BI__builtin_ia32_vpermi2varhi256:
15307 case X86::BI__builtin_ia32_vpermi2vard512:
15308 case X86::BI__builtin_ia32_vpermi2varps512: {
15309 APValue R;
15310 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15311 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15312 int Offset = ShuffleMask & 0xF;
15313 unsigned SrcIdx = (ShuffleMask >> 4) & 0x1;
15314 return std::pair<unsigned, int>{SrcIdx, Offset};
15315 }))
15316 return false;
15317 return Success(V: R, E);
15318 }
15319 case X86::BI__builtin_ia32_vpermi2varqi256:
15320 case X86::BI__builtin_ia32_vpermi2varhi512: {
15321 APValue R;
15322 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15323 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15324 int Offset = ShuffleMask & 0x1F;
15325 unsigned SrcIdx = (ShuffleMask >> 5) & 0x1;
15326 return std::pair<unsigned, int>{SrcIdx, Offset};
15327 }))
15328 return false;
15329 return Success(V: R, E);
15330 }
15331 case X86::BI__builtin_ia32_vpermi2varqi512: {
15332 APValue R;
15333 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15334 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15335 int Offset = ShuffleMask & 0x3F;
15336 unsigned SrcIdx = (ShuffleMask >> 6) & 0x1;
15337 return std::pair<unsigned, int>{SrcIdx, Offset};
15338 }))
15339 return false;
15340 return Success(V: R, E);
15341 }
15342
15343 case clang::X86::BI__builtin_ia32_minps:
15344 case clang::X86::BI__builtin_ia32_minpd:
15345 case clang::X86::BI__builtin_ia32_minps256:
15346 case clang::X86::BI__builtin_ia32_minpd256:
15347 case clang::X86::BI__builtin_ia32_minps512:
15348 case clang::X86::BI__builtin_ia32_minpd512:
15349 case clang::X86::BI__builtin_ia32_minph128:
15350 case clang::X86::BI__builtin_ia32_minph256:
15351 case clang::X86::BI__builtin_ia32_minph512:
15352 return EvaluateFpBinOpExpr(
15353 [](const APFloat &A, const APFloat &B,
15354 std::optional<APSInt>) -> std::optional<APFloat> {
15355 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
15356 B.isInfinity() || B.isDenormal())
15357 return std::nullopt;
15358 if (A.isZero() && B.isZero())
15359 return B;
15360 return llvm::minimum(A, B);
15361 });
15362
15363 case clang::X86::BI__builtin_ia32_minss:
15364 case clang::X86::BI__builtin_ia32_minsd:
15365 return EvaluateFpBinOpExpr(
15366 [](const APFloat &A, const APFloat &B,
15367 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15368 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/true);
15369 },
15370 /*IsScalar=*/true);
15371
15372 case clang::X86::BI__builtin_ia32_minsd_round_mask:
15373 case clang::X86::BI__builtin_ia32_minss_round_mask:
15374 case clang::X86::BI__builtin_ia32_minsh_round_mask:
15375 case clang::X86::BI__builtin_ia32_maxsd_round_mask:
15376 case clang::X86::BI__builtin_ia32_maxss_round_mask:
15377 case clang::X86::BI__builtin_ia32_maxsh_round_mask: {
15378 bool IsMin = BuiltinOp == clang::X86::BI__builtin_ia32_minsd_round_mask ||
15379 BuiltinOp == clang::X86::BI__builtin_ia32_minss_round_mask ||
15380 BuiltinOp == clang::X86::BI__builtin_ia32_minsh_round_mask;
15381 return EvaluateScalarFpRoundMaskBinOp(
15382 [IsMin](const APFloat &A, const APFloat &B,
15383 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15384 return EvalScalarMinMaxFp(A, B, RoundingMode, IsMin);
15385 });
15386 }
15387
15388 case clang::X86::BI__builtin_ia32_maxps:
15389 case clang::X86::BI__builtin_ia32_maxpd:
15390 case clang::X86::BI__builtin_ia32_maxps256:
15391 case clang::X86::BI__builtin_ia32_maxpd256:
15392 case clang::X86::BI__builtin_ia32_maxps512:
15393 case clang::X86::BI__builtin_ia32_maxpd512:
15394 case clang::X86::BI__builtin_ia32_maxph128:
15395 case clang::X86::BI__builtin_ia32_maxph256:
15396 case clang::X86::BI__builtin_ia32_maxph512:
15397 return EvaluateFpBinOpExpr(
15398 [](const APFloat &A, const APFloat &B,
15399 std::optional<APSInt>) -> std::optional<APFloat> {
15400 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
15401 B.isInfinity() || B.isDenormal())
15402 return std::nullopt;
15403 if (A.isZero() && B.isZero())
15404 return B;
15405 return llvm::maximum(A, B);
15406 });
15407
15408 case clang::X86::BI__builtin_ia32_maxss:
15409 case clang::X86::BI__builtin_ia32_maxsd:
15410 return EvaluateFpBinOpExpr(
15411 [](const APFloat &A, const APFloat &B,
15412 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15413 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/false);
15414 },
15415 /*IsScalar=*/true);
15416
15417 case clang::X86::BI__builtin_ia32_vcvtps2ph:
15418 case clang::X86::BI__builtin_ia32_vcvtps2ph256: {
15419 APValue SrcVec;
15420 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SrcVec))
15421 return false;
15422
15423 APSInt Imm;
15424 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info))
15425 return false;
15426
15427 const auto *SrcVTy = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
15428 unsigned SrcNumElems = SrcVTy->getNumElements();
15429 const auto *DstVTy = E->getType()->castAs<VectorType>();
15430 unsigned DstNumElems = DstVTy->getNumElements();
15431 QualType DstElemTy = DstVTy->getElementType();
15432
15433 const llvm::fltSemantics &HalfSem =
15434 Info.Ctx.getFloatTypeSemantics(T: Info.Ctx.HalfTy);
15435
15436 int ImmVal = Imm.getZExtValue();
15437 bool UseMXCSR = (ImmVal & 4) != 0;
15438 bool IsFPConstrained =
15439 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained();
15440
15441 llvm::RoundingMode RM;
15442 if (!UseMXCSR) {
15443 switch (ImmVal & 3) {
15444 case 0:
15445 RM = llvm::RoundingMode::NearestTiesToEven;
15446 break;
15447 case 1:
15448 RM = llvm::RoundingMode::TowardNegative;
15449 break;
15450 case 2:
15451 RM = llvm::RoundingMode::TowardPositive;
15452 break;
15453 case 3:
15454 RM = llvm::RoundingMode::TowardZero;
15455 break;
15456 default:
15457 llvm_unreachable("Invalid immediate rounding mode");
15458 }
15459 } else {
15460 RM = llvm::RoundingMode::NearestTiesToEven;
15461 }
15462
15463 SmallVector<APValue, 8> ResultElements;
15464 ResultElements.reserve(N: DstNumElems);
15465
15466 for (unsigned I = 0; I < SrcNumElems; ++I) {
15467 APFloat SrcVal = SrcVec.getVectorElt(I).getFloat();
15468
15469 bool LostInfo;
15470 APFloat::opStatus St = SrcVal.convert(ToSemantics: HalfSem, RM, losesInfo: &LostInfo);
15471
15472 if (UseMXCSR && IsFPConstrained && St != APFloat::opOK) {
15473 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
15474 return false;
15475 }
15476
15477 APSInt DstInt(SrcVal.bitcastToAPInt(),
15478 DstElemTy->isUnsignedIntegerOrEnumerationType());
15479 ResultElements.push_back(Elt: APValue(DstInt));
15480 }
15481
15482 if (DstNumElems > SrcNumElems) {
15483 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: DstElemTy);
15484 for (unsigned I = SrcNumElems; I < DstNumElems; ++I) {
15485 ResultElements.push_back(Elt: APValue(Zero));
15486 }
15487 }
15488
15489 return Success(V: ResultElements, E);
15490 }
15491 case X86::BI__builtin_ia32_vperm2f128_pd256:
15492 case X86::BI__builtin_ia32_vperm2f128_ps256:
15493 case X86::BI__builtin_ia32_vperm2f128_si256:
15494 case X86::BI__builtin_ia32_permti256: {
15495 unsigned NumElements =
15496 E->getArg(Arg: 0)->getType()->getAs<VectorType>()->getNumElements();
15497 unsigned PreservedBitsCnt = NumElements >> 2;
15498 APValue R;
15499 if (!evalShuffleGeneric(
15500 Info, Call: E, Out&: R,
15501 GetSourceIndex: [PreservedBitsCnt](unsigned DstIdx, unsigned ShuffleMask) {
15502 unsigned ControlBitsCnt = DstIdx >> PreservedBitsCnt << 2;
15503 unsigned ControlBits = ShuffleMask >> ControlBitsCnt;
15504
15505 if (ControlBits & 0b1000)
15506 return std::make_pair(x: 0u, y: -1);
15507
15508 unsigned SrcVecIdx = (ControlBits & 0b10) >> 1;
15509 unsigned PreservedBitsMask = (1 << PreservedBitsCnt) - 1;
15510 int SrcIdx = ((ControlBits & 0b1) << PreservedBitsCnt) |
15511 (DstIdx & PreservedBitsMask);
15512 return std::make_pair(x&: SrcVecIdx, y&: SrcIdx);
15513 }))
15514 return false;
15515 return Success(V: R, E);
15516 }
15517 case X86::BI__builtin_ia32_vpdpwssd128:
15518 case X86::BI__builtin_ia32_vpdpwssd256:
15519 case X86::BI__builtin_ia32_vpdpwssd512:
15520 case X86::BI__builtin_ia32_vpdpbusd128:
15521 case X86::BI__builtin_ia32_vpdpbusd256:
15522 case X86::BI__builtin_ia32_vpdpbusd512:
15523 return EvalVectorDotProduct(false);
15524 case X86::BI__builtin_ia32_vpdpwssds128:
15525 case X86::BI__builtin_ia32_vpdpwssds256:
15526 case X86::BI__builtin_ia32_vpdpwssds512:
15527 case X86::BI__builtin_ia32_vpdpbusds128:
15528 case X86::BI__builtin_ia32_vpdpbusds256:
15529 case X86::BI__builtin_ia32_vpdpbusds512:
15530 return EvalVectorDotProduct(true);
15531 case X86::BI__builtin_ia32_cvtpd2dq:
15532 case X86::BI__builtin_ia32_cvtps2dq:
15533 case X86::BI__builtin_ia32_cvttpd2dq:
15534 case X86::BI__builtin_ia32_cvttps2dq:
15535 case X86::BI__builtin_ia32_cvtpd2dq256:
15536 case X86::BI__builtin_ia32_cvtps2dq256:
15537 case X86::BI__builtin_ia32_cvttpd2dq256:
15538 case X86::BI__builtin_ia32_cvttps2dq256: {
15539 APValue SrcVec;
15540 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SrcVec) || !SrcVec.isVector())
15541 return false;
15542
15543 const auto *VT = E->getType()->castAs<VectorType>();
15544 QualType EltTy = VT->getElementType();
15545 bool isUnsigned = EltTy->isUnsignedIntegerType();
15546 unsigned BitWidth = Info.Ctx.getIntWidth(T: EltTy);
15547
15548 unsigned NumSrcElems = SrcVec.getVectorLength();
15549 unsigned NumDstElems = VT->getNumElements();
15550
15551 SmallVector<APValue, 8> ResultElts;
15552 for (unsigned i = 0; i != NumDstElems; ++i) {
15553 if (i < NumSrcElems) {
15554 llvm::APFloat FloatElem = SrcVec.getVectorElt(I: i).getFloat();
15555 llvm::APSInt IntResult(BitWidth, isUnsigned);
15556 bool IsExact = false;
15557 // We only allow exact conversions so rounding mode does not matter for
15558 // cvt* and cvtt* builtins
15559 FloatElem.convertToInteger(Result&: IntResult, RM: llvm::APFloat::rmTowardZero,
15560 IsExact: &IsExact);
15561 if (!IsExact)
15562 return false;
15563 ResultElts.push_back(Elt: APValue(IntResult));
15564 } else
15565 // Pad remaining lanes with zero
15566 ResultElts.push_back(Elt: APValue(llvm::APSInt(BitWidth, isUnsigned)));
15567 }
15568 return Success(V: ResultElts, E);
15569 }
15570 }
15571}
15572
15573bool VectorExprEvaluator::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
15574 APValue Source;
15575 QualType SourceVecType = E->getSrcExpr()->getType();
15576 if (!EvaluateAsRValue(Info, E: E->getSrcExpr(), Result&: Source))
15577 return false;
15578
15579 QualType DestTy = E->getType()->castAs<VectorType>()->getElementType();
15580 QualType SourceTy = SourceVecType->castAs<VectorType>()->getElementType();
15581
15582 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15583
15584 auto SourceLen = Source.getVectorLength();
15585 SmallVector<APValue, 4> ResultElements;
15586 ResultElements.reserve(N: SourceLen);
15587 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
15588 APValue Elt;
15589 if (!handleVectorElementCast(Info, FPO, E, SourceTy, DestTy,
15590 Original: Source.getVectorElt(I: EltNum), Result&: Elt))
15591 return false;
15592 ResultElements.push_back(Elt: std::move(Elt));
15593 }
15594
15595 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15596}
15597
15598static bool handleVectorShuffle(EvalInfo &Info, const ShuffleVectorExpr *E,
15599 QualType ElemType, APValue const &VecVal1,
15600 APValue const &VecVal2, unsigned EltNum,
15601 APValue &Result) {
15602 unsigned const TotalElementsInInputVector1 = VecVal1.getVectorLength();
15603 unsigned const TotalElementsInInputVector2 = VecVal2.getVectorLength();
15604
15605 APSInt IndexVal = E->getShuffleMaskIdx(N: EltNum);
15606 int64_t index = IndexVal.getExtValue();
15607 // The spec says that -1 should be treated as undef for optimizations,
15608 // but in constexpr we'd have to produce an APValue::Indeterminate,
15609 // which is prohibited from being a top-level constant value. Emit a
15610 // diagnostic instead.
15611 if (index == -1) {
15612 Info.FFDiag(
15613 E, DiagId: diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
15614 << EltNum;
15615 return false;
15616 }
15617
15618 if (index < 0 ||
15619 index >= TotalElementsInInputVector1 + TotalElementsInInputVector2)
15620 llvm_unreachable("Out of bounds shuffle index");
15621
15622 if (index >= TotalElementsInInputVector1)
15623 Result = VecVal2.getVectorElt(I: index - TotalElementsInInputVector1);
15624 else
15625 Result = VecVal1.getVectorElt(I: index);
15626 return true;
15627}
15628
15629bool VectorExprEvaluator::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
15630 // FIXME: Unary shuffle with mask not currently supported.
15631 if (E->getNumSubExprs() == 2)
15632 return Error(E);
15633 APValue VecVal1;
15634 const Expr *Vec1 = E->getExpr(Index: 0);
15635 if (!EvaluateAsRValue(Info, E: Vec1, Result&: VecVal1))
15636 return false;
15637 APValue VecVal2;
15638 const Expr *Vec2 = E->getExpr(Index: 1);
15639 if (!EvaluateAsRValue(Info, E: Vec2, Result&: VecVal2))
15640 return false;
15641
15642 VectorType const *DestVecTy = E->getType()->castAs<VectorType>();
15643 QualType DestElTy = DestVecTy->getElementType();
15644
15645 auto TotalElementsInOutputVector = DestVecTy->getNumElements();
15646
15647 SmallVector<APValue, 4> ResultElements;
15648 ResultElements.reserve(N: TotalElementsInOutputVector);
15649 for (unsigned EltNum = 0; EltNum < TotalElementsInOutputVector; ++EltNum) {
15650 APValue Elt;
15651 if (!handleVectorShuffle(Info, E, ElemType: DestElTy, VecVal1, VecVal2, EltNum, Result&: Elt))
15652 return false;
15653 ResultElements.push_back(Elt: std::move(Elt));
15654 }
15655
15656 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15657}
15658
15659//===----------------------------------------------------------------------===//
15660// Matrix Evaluation
15661//===----------------------------------------------------------------------===//
15662
15663namespace {
15664class MatrixExprEvaluator : public ExprEvaluatorBase<MatrixExprEvaluator> {
15665 APValue &Result;
15666
15667public:
15668 MatrixExprEvaluator(EvalInfo &Info, APValue &Result)
15669 : ExprEvaluatorBaseTy(Info), Result(Result) {}
15670
15671 bool Success(ArrayRef<APValue> M, const Expr *E) {
15672 auto *CMTy = E->getType()->castAs<ConstantMatrixType>();
15673 assert(M.size() == CMTy->getNumElementsFlattened());
15674 // FIXME: remove this APValue copy.
15675 Result = APValue(M.data(), CMTy->getNumRows(), CMTy->getNumColumns());
15676 return true;
15677 }
15678 bool Success(const APValue &M, const Expr *E) {
15679 assert(M.isMatrix() && "expected matrix");
15680 Result = M;
15681 return true;
15682 }
15683
15684 bool VisitCastExpr(const CastExpr *E);
15685 bool VisitInitListExpr(const InitListExpr *E);
15686};
15687} // end anonymous namespace
15688
15689static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info) {
15690 assert(E->isPRValue() && E->getType()->isConstantMatrixType() &&
15691 "not a matrix prvalue");
15692 return MatrixExprEvaluator(Info, Result).Visit(S: E);
15693}
15694
15695bool MatrixExprEvaluator::VisitCastExpr(const CastExpr *E) {
15696 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15697 unsigned NumRows = MT->getNumRows();
15698 unsigned NumCols = MT->getNumColumns();
15699 unsigned NElts = NumRows * NumCols;
15700 QualType EltTy = MT->getElementType();
15701 const Expr *SE = E->getSubExpr();
15702
15703 switch (E->getCastKind()) {
15704 case CK_HLSLAggregateSplatCast: {
15705 APValue Val;
15706 QualType ValTy;
15707
15708 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15709 return false;
15710
15711 APValue CastedVal;
15712 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15713 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: EltTy, Original: Val, Result&: CastedVal))
15714 return false;
15715
15716 SmallVector<APValue, 16> SplatEls(NElts, CastedVal);
15717 return Success(M: SplatEls, E);
15718 }
15719 case CK_HLSLElementwiseCast: {
15720 SmallVector<APValue> SrcVals;
15721 SmallVector<QualType> SrcTypes;
15722
15723 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
15724 return false;
15725
15726 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15727 SmallVector<QualType, 16> DestTypes(NElts, EltTy);
15728 SmallVector<APValue, 16> ResultEls(NElts);
15729 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
15730 Results&: ResultEls))
15731 return false;
15732 return Success(M: ResultEls, E);
15733 }
15734 default:
15735 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15736 }
15737}
15738
15739bool MatrixExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
15740 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15741 QualType EltTy = MT->getElementType();
15742
15743 assert(E->getNumInits() == MT->getNumElementsFlattened() &&
15744 "Expected number of elements in initializer list to match the number "
15745 "of matrix elements");
15746
15747 SmallVector<APValue, 16> Elements;
15748 Elements.reserve(N: MT->getNumElementsFlattened());
15749
15750 // The following loop assumes the elements of the matrix InitListExpr are in
15751 // row-major order, which matches the row-major ordering assumption of the
15752 // matrix APValue.
15753 for (unsigned I = 0, N = MT->getNumElementsFlattened(); I < N; ++I) {
15754 if (EltTy->isIntegerType()) {
15755 llvm::APSInt IntVal;
15756 if (!EvaluateInteger(E: E->getInit(Init: I), Result&: IntVal, Info))
15757 return false;
15758 Elements.push_back(Elt: APValue(IntVal));
15759 } else {
15760 llvm::APFloat FloatVal(0.0);
15761 if (!EvaluateFloat(E: E->getInit(Init: I), Result&: FloatVal, Info))
15762 return false;
15763 Elements.push_back(Elt: APValue(FloatVal));
15764 }
15765 }
15766
15767 return Success(M: Elements, E);
15768}
15769
15770//===----------------------------------------------------------------------===//
15771// Array Evaluation
15772//===----------------------------------------------------------------------===//
15773
15774namespace {
15775 class ArrayExprEvaluator
15776 : public ExprEvaluatorBase<ArrayExprEvaluator> {
15777 const LValue &This;
15778 APValue &Result;
15779 public:
15780
15781 ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
15782 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
15783
15784 bool Success(const APValue &V, const Expr *E) {
15785 assert(V.isArray() && "expected array");
15786 Result = V;
15787 return true;
15788 }
15789
15790 bool ZeroInitialization(const Expr *E) {
15791 const ConstantArrayType *CAT =
15792 Info.Ctx.getAsConstantArrayType(T: E->getType());
15793 if (!CAT) {
15794 if (E->getType()->isIncompleteArrayType()) {
15795 // We can be asked to zero-initialize a flexible array member; this
15796 // is represented as an ImplicitValueInitExpr of incomplete array
15797 // type. In this case, the array has zero elements.
15798 Result = APValue(APValue::UninitArray(), 0, 0);
15799 return true;
15800 }
15801 // FIXME: We could handle VLAs here.
15802 return Error(E);
15803 }
15804
15805 Result = APValue(APValue::UninitArray(), 0, CAT->getZExtSize());
15806 if (!Result.hasArrayFiller())
15807 return true;
15808
15809 // Zero-initialize all elements.
15810 LValue Subobject = This;
15811 Subobject.addArray(Info, E, CAT);
15812 ImplicitValueInitExpr VIE(CAT->getElementType());
15813 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject, E: &VIE);
15814 }
15815
15816 bool VisitCallExpr(const CallExpr *E) {
15817 return handleCallExpr(E, Result, ResultSlot: &This);
15818 }
15819 bool VisitCastExpr(const CastExpr *E);
15820 bool VisitInitListExpr(const InitListExpr *E,
15821 QualType AllocType = QualType());
15822 bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E);
15823 bool VisitCXXConstructExpr(const CXXConstructExpr *E);
15824 bool VisitCXXConstructExpr(const CXXConstructExpr *E,
15825 const LValue &Subobject,
15826 APValue *Value, QualType Type);
15827 bool VisitStringLiteral(const StringLiteral *E,
15828 QualType AllocType = QualType()) {
15829 expandStringLiteral(Info, S: E, Result, AllocType);
15830 return true;
15831 }
15832 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
15833 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
15834 ArrayRef<Expr *> Args,
15835 const Expr *ArrayFiller,
15836 QualType AllocType = QualType());
15837 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
15838 };
15839} // end anonymous namespace
15840
15841static bool EvaluateArray(const Expr *E, const LValue &This,
15842 APValue &Result, EvalInfo &Info) {
15843 assert(!E->isValueDependent());
15844 assert(E->isPRValue() && E->getType()->isArrayType() &&
15845 "not an array prvalue");
15846 return ArrayExprEvaluator(Info, This, Result).Visit(S: E);
15847}
15848
15849static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
15850 APValue &Result, const InitListExpr *ILE,
15851 QualType AllocType) {
15852 assert(!ILE->isValueDependent());
15853 assert(ILE->isPRValue() && ILE->getType()->isArrayType() &&
15854 "not an array prvalue");
15855 return ArrayExprEvaluator(Info, This, Result)
15856 .VisitInitListExpr(E: ILE, AllocType);
15857}
15858
15859static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
15860 APValue &Result,
15861 const CXXConstructExpr *CCE,
15862 QualType AllocType) {
15863 assert(!CCE->isValueDependent());
15864 assert(CCE->isPRValue() && CCE->getType()->isArrayType() &&
15865 "not an array prvalue");
15866 return ArrayExprEvaluator(Info, This, Result)
15867 .VisitCXXConstructExpr(E: CCE, Subobject: This, Value: &Result, Type: AllocType);
15868}
15869
15870// Return true iff the given array filler may depend on the element index.
15871static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) {
15872 // For now, just allow non-class value-initialization and initialization
15873 // lists comprised of them.
15874 if (isa<ImplicitValueInitExpr>(Val: FillerExpr))
15875 return false;
15876 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Val: FillerExpr)) {
15877 for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) {
15878 if (MaybeElementDependentArrayFiller(FillerExpr: ILE->getInit(Init: I)))
15879 return true;
15880 }
15881
15882 if (ILE->hasArrayFiller() &&
15883 MaybeElementDependentArrayFiller(FillerExpr: ILE->getArrayFiller()))
15884 return true;
15885
15886 return false;
15887 }
15888 return true;
15889}
15890
15891bool ArrayExprEvaluator::VisitCastExpr(const CastExpr *E) {
15892 const Expr *SE = E->getSubExpr();
15893
15894 switch (E->getCastKind()) {
15895 default:
15896 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15897 case CK_HLSLAggregateSplatCast: {
15898 APValue Val;
15899 QualType ValTy;
15900
15901 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15902 return false;
15903
15904 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
15905
15906 SmallVector<APValue> SplatEls(NEls, Val);
15907 SmallVector<QualType> SplatType(NEls, ValTy);
15908
15909 // cast the elements
15910 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15911 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
15912 ElTypes&: SplatType))
15913 return false;
15914
15915 return true;
15916 }
15917 case CK_HLSLElementwiseCast: {
15918 SmallVector<APValue> SrcEls;
15919 SmallVector<QualType> SrcTypes;
15920
15921 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals&: SrcEls, SrcTypes))
15922 return false;
15923
15924 // cast the elements
15925 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15926 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
15927 ElTypes&: SrcTypes))
15928 return false;
15929 return true;
15930 }
15931 }
15932}
15933
15934bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E,
15935 QualType AllocType) {
15936 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15937 T: AllocType.isNull() ? E->getType() : AllocType);
15938 if (!CAT)
15939 return Error(E);
15940
15941 // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
15942 // an appropriately-typed string literal enclosed in braces.
15943 if (E->isStringLiteralInit()) {
15944 auto *SL = dyn_cast<StringLiteral>(Val: E->getInit(Init: 0)->IgnoreParenImpCasts());
15945 // FIXME: Support ObjCEncodeExpr here once we support it in
15946 // ArrayExprEvaluator generally.
15947 if (!SL)
15948 return Error(E);
15949 return VisitStringLiteral(E: SL, AllocType);
15950 }
15951 // Any other transparent list init will need proper handling of the
15952 // AllocType; we can't just recurse to the inner initializer.
15953 assert(!E->isTransparent() &&
15954 "transparent array list initialization is not string literal init?");
15955
15956 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits(), ArrayFiller: E->getArrayFiller(),
15957 AllocType);
15958}
15959
15960bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
15961 const Expr *ExprToVisit, ArrayRef<Expr *> Args, const Expr *ArrayFiller,
15962 QualType AllocType) {
15963 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15964 T: AllocType.isNull() ? ExprToVisit->getType() : AllocType);
15965
15966 bool Success = true;
15967
15968 unsigned NumEltsToInit = Args.size();
15969 unsigned NumElts = CAT->getZExtSize();
15970
15971 // If the initializer might depend on the array index, run it for each
15972 // array element.
15973 if (NumEltsToInit != NumElts &&
15974 MaybeElementDependentArrayFiller(FillerExpr: ArrayFiller)) {
15975 NumEltsToInit = NumElts;
15976 } else {
15977 // Add additional elements represented by EmbedExpr.
15978 for (auto *Init : Args) {
15979 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts()))
15980 NumEltsToInit += EmbedS->getDataElementCount() - 1;
15981 }
15982 // If we have extra elements in the list, they will be discarded.
15983 if (NumEltsToInit > NumElts)
15984 NumEltsToInit = NumElts;
15985 // If we're overwriting memory which already has an object, make sure we
15986 // don't reduce the number of non-filler elements. (It's possible to
15987 // optimize this in some cases, but the logic gets really complicated.)
15988 if (Result.hasValue() && NumEltsToInit < Result.getArrayInitializedElts())
15989 NumEltsToInit = Result.getArrayInitializedElts();
15990 }
15991
15992 LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
15993 << NumEltsToInit << ".\n");
15994
15995 if (!Result.hasValue()) {
15996 Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15997 } else if (Result.getArrayInitializedElts() != NumEltsToInit) {
15998 // Number of inititalized elts changed. Recreate the APValue, and copy over
15999 // the relevant elements. (This is essentially just fixing the internal
16000 // representation of the value, because it's tied to the number of
16001 // non-filler elements.)
16002 //
16003 // This should be hit rarely, but there are some edge cases:
16004 //
16005 // - The array could be zero-initialized.
16006 // - There could be a DesignatedInitListExpr.
16007 // - operator new[] can be used to start the lifetime early.
16008 APValue NewResult = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
16009 // First copy existing elements.
16010 unsigned NumOldElts = Result.getArrayInitializedElts();
16011 for (unsigned I = 0; I < NumOldElts; ++I) {
16012 NewResult.getArrayInitializedElt(I) =
16013 std::move(Result.getArrayInitializedElt(I));
16014 }
16015 // Then copy the array filler over the remaining elements.
16016 for (unsigned I = Result.getArrayInitializedElts(); I < NumEltsToInit; ++I)
16017 NewResult.getArrayInitializedElt(I) = Result.getArrayFiller();
16018 if (NewResult.hasArrayFiller() && Result.hasArrayFiller())
16019 NewResult.getArrayFiller() = Result.getArrayFiller();
16020 Result = std::move(NewResult);
16021 }
16022
16023 LValue Subobject = This;
16024 Subobject.addArray(Info, E: ExprToVisit, CAT);
16025 auto Eval = [&](const Expr *Init, unsigned ArrayIndex) {
16026 if (Init->isValueDependent())
16027 return EvaluateDependentExpr(E: Init, Info);
16028
16029 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
16030 // aren't supposed to be modified.
16031 if (isa<NoInitExpr>(Val: Init))
16032 return true;
16033
16034 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: ArrayIndex), Info,
16035 This: Subobject, E: Init) ||
16036 !HandleLValueArrayAdjustment(Info, E: Init, LVal&: Subobject,
16037 EltTy: CAT->getElementType(), Adjustment: 1)) {
16038 if (!Info.noteFailure())
16039 return false;
16040 Success = false;
16041 }
16042 return true;
16043 };
16044 unsigned ArrayIndex = 0;
16045 QualType DestTy = CAT->getElementType();
16046 APSInt Value(Info.Ctx.getTypeSize(T: DestTy), DestTy->isUnsignedIntegerType());
16047 for (unsigned Index = 0; Index != NumEltsToInit; ++Index) {
16048 const Expr *Init = Index < Args.size() ? Args[Index] : ArrayFiller;
16049 if (ArrayIndex >= NumEltsToInit)
16050 break;
16051 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) {
16052 StringLiteral *SL = EmbedS->getDataStringLiteral();
16053 for (unsigned I = EmbedS->getStartingElementPos(),
16054 N = EmbedS->getDataElementCount();
16055 I != EmbedS->getStartingElementPos() + N; ++I) {
16056 Value = SL->getCodeUnit(I);
16057 if (DestTy->isIntegerType()) {
16058 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(Value);
16059 } else {
16060 assert(DestTy->isFloatingType() && "unexpected type");
16061 const FPOptions FPO =
16062 Init->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
16063 APFloat FValue(0.0);
16064 if (!HandleIntToFloatCast(Info, E: Init, FPO, SrcType: EmbedS->getType(), Value,
16065 DestType: DestTy, Result&: FValue))
16066 return false;
16067 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(FValue);
16068 }
16069 ArrayIndex++;
16070 }
16071 } else {
16072 if (!Eval(Init, ArrayIndex))
16073 return false;
16074 ++ArrayIndex;
16075 }
16076 }
16077
16078 if (!Result.hasArrayFiller())
16079 return Success;
16080
16081 // If we get here, we have a trivial filler, which we can just evaluate
16082 // once and splat over the rest of the array elements.
16083 assert(ArrayFiller && "no array filler for incomplete init list");
16084 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject,
16085 E: ArrayFiller) &&
16086 Success;
16087}
16088
16089bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
16090 LValue CommonLV;
16091 if (E->getCommonExpr() &&
16092 !Evaluate(Result&: Info.CurrentCall->createTemporary(
16093 Key: E->getCommonExpr(),
16094 T: getStorageType(Ctx: Info.Ctx, E: E->getCommonExpr()),
16095 Scope: ScopeKind::FullExpression, LV&: CommonLV),
16096 Info, E: E->getCommonExpr()->getSourceExpr()))
16097 return false;
16098
16099 auto *CAT = cast<ConstantArrayType>(Val: E->getType()->castAsArrayTypeUnsafe());
16100 if (!CheckArraySize(Info, CAT, CallLoc: E->getExprLoc()))
16101 return false;
16102
16103 uint64_t Elements = CAT->getZExtSize();
16104 Result = APValue(APValue::UninitArray(), Elements, Elements);
16105
16106 LValue Subobject = This;
16107 Subobject.addArray(Info, E, CAT);
16108
16109 bool Success = true;
16110 for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) {
16111 // C++ [class.temporary]/5
16112 // There are four contexts in which temporaries are destroyed at a different
16113 // point than the end of the full-expression. [...] The second context is
16114 // when a copy constructor is called to copy an element of an array while
16115 // the entire array is copied [...]. In either case, if the constructor has
16116 // one or more default arguments, the destruction of every temporary created
16117 // in a default argument is sequenced before the construction of the next
16118 // array element, if any.
16119 FullExpressionRAII Scope(Info);
16120
16121 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index),
16122 Info, This: Subobject, E: E->getSubExpr()) ||
16123 !HandleLValueArrayAdjustment(Info, E, LVal&: Subobject,
16124 EltTy: CAT->getElementType(), Adjustment: 1)) {
16125 if (!Info.noteFailure())
16126 return false;
16127 Success = false;
16128 }
16129
16130 // Make sure we run the destructors too.
16131 Scope.destroy();
16132 }
16133
16134 return Success;
16135}
16136
16137bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
16138 return VisitCXXConstructExpr(E, Subobject: This, Value: &Result, Type: E->getType());
16139}
16140
16141bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
16142 const LValue &Subobject,
16143 APValue *Value,
16144 QualType Type) {
16145 bool HadZeroInit = Value->hasValue();
16146
16147 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: Type)) {
16148 if (!CheckArraySize(Info, CAT, CallLoc: E->getExprLoc()))
16149 return false;
16150 unsigned FinalSize = CAT->getZExtSize();
16151
16152 // Preserve the array filler if we had prior zero-initialization.
16153 APValue Filler =
16154 HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller()
16155 : APValue();
16156
16157 *Value = APValue(APValue::UninitArray(), 0, FinalSize);
16158 if (FinalSize == 0)
16159 return true;
16160
16161 bool HasTrivialConstructor = CheckTrivialDefaultConstructor(
16162 Info, Loc: E->getExprLoc(), CD: E->getConstructor(),
16163 IsValueInitialization: E->requiresZeroInitialization());
16164 LValue ArrayElt = Subobject;
16165 ArrayElt.addArray(Info, E, CAT);
16166 // We do the whole initialization in two passes, first for just one element,
16167 // then for the whole array. It's possible we may find out we can't do const
16168 // init in the first pass, in which case we avoid allocating a potentially
16169 // large array. We don't do more passes because expanding array requires
16170 // copying the data, which is wasteful.
16171 for (const unsigned N : {1u, FinalSize}) {
16172 unsigned OldElts = Value->getArrayInitializedElts();
16173 if (OldElts == N)
16174 break;
16175
16176 // Expand the array to appropriate size.
16177 APValue NewValue(APValue::UninitArray(), N, FinalSize);
16178 for (unsigned I = 0; I < OldElts; ++I)
16179 NewValue.getArrayInitializedElt(I).swap(
16180 RHS&: Value->getArrayInitializedElt(I));
16181 Value->swap(RHS&: NewValue);
16182
16183 if (HadZeroInit)
16184 for (unsigned I = OldElts; I < N; ++I)
16185 Value->getArrayInitializedElt(I) = Filler;
16186
16187 if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) {
16188 // If we have a trivial constructor, only evaluate it once and copy
16189 // the result into all the array elements.
16190 APValue &FirstResult = Value->getArrayInitializedElt(I: 0);
16191 for (unsigned I = OldElts; I < FinalSize; ++I)
16192 Value->getArrayInitializedElt(I) = FirstResult;
16193 } else {
16194 for (unsigned I = OldElts; I < N; ++I) {
16195 if (!VisitCXXConstructExpr(E, Subobject: ArrayElt,
16196 Value: &Value->getArrayInitializedElt(I),
16197 Type: CAT->getElementType()) ||
16198 !HandleLValueArrayAdjustment(Info, E, LVal&: ArrayElt,
16199 EltTy: CAT->getElementType(), Adjustment: 1))
16200 return false;
16201 // When checking for const initilization any diagnostic is considered
16202 // an error.
16203 if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&
16204 !Info.keepEvaluatingAfterFailure())
16205 return false;
16206 }
16207 }
16208 }
16209
16210 return true;
16211 }
16212
16213 if (!Type->isRecordType())
16214 return Error(E);
16215
16216 return RecordExprEvaluator(Info, Subobject, *Value)
16217 .VisitCXXConstructExpr(E, T: Type);
16218}
16219
16220bool ArrayExprEvaluator::VisitCXXParenListInitExpr(
16221 const CXXParenListInitExpr *E) {
16222 assert(E->getType()->isConstantArrayType() &&
16223 "Expression result is not a constant array type");
16224
16225 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs(),
16226 ArrayFiller: E->getArrayFiller());
16227}
16228
16229bool ArrayExprEvaluator::VisitDesignatedInitUpdateExpr(
16230 const DesignatedInitUpdateExpr *E) {
16231 if (!Visit(S: E->getBase()))
16232 return false;
16233 return Visit(S: E->getUpdater());
16234}
16235
16236//===----------------------------------------------------------------------===//
16237// Integer Evaluation
16238//
16239// As a GNU extension, we support casting pointers to sufficiently-wide integer
16240// types and back in constant folding. Integer values are thus represented
16241// either as an integer-valued APValue, or as an lvalue-valued APValue.
16242//===----------------------------------------------------------------------===//
16243
16244namespace {
16245class IntExprEvaluator
16246 : public ExprEvaluatorBase<IntExprEvaluator> {
16247 APValue &Result;
16248public:
16249 IntExprEvaluator(EvalInfo &info, APValue &result)
16250 : ExprEvaluatorBaseTy(info), Result(result) {}
16251
16252 bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) {
16253 assert(E->getType()->isIntegralOrEnumerationType() &&
16254 "Invalid evaluation result.");
16255 assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
16256 "Invalid evaluation result.");
16257 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16258 "Invalid evaluation result.");
16259 Result = APValue(SI);
16260 return true;
16261 }
16262 bool Success(const llvm::APSInt &SI, const Expr *E) {
16263 return Success(SI, E, Result);
16264 }
16265
16266 bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) {
16267 assert(E->getType()->isIntegralOrEnumerationType() &&
16268 "Invalid evaluation result.");
16269 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16270 "Invalid evaluation result.");
16271 Result = APValue(APSInt(I));
16272 Result.getInt().setIsUnsigned(
16273 E->getType()->isUnsignedIntegerOrEnumerationType());
16274 return true;
16275 }
16276 bool Success(const llvm::APInt &I, const Expr *E) {
16277 return Success(I, E, Result);
16278 }
16279
16280 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
16281 assert(E->getType()->isIntegralOrEnumerationType() &&
16282 "Invalid evaluation result.");
16283 Result = APValue(Info.Ctx.MakeIntValue(Value, Type: E->getType()));
16284 return true;
16285 }
16286 bool Success(uint64_t Value, const Expr *E) {
16287 return Success(Value, E, Result);
16288 }
16289
16290 bool Success(CharUnits Size, const Expr *E) {
16291 return Success(Value: Size.getQuantity(), E);
16292 }
16293
16294 bool Success(const APValue &V, const Expr *E) {
16295 // C++23 [expr.const]p8 If we have a variable that is unknown reference or
16296 // pointer allow further evaluation of the value.
16297 if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate() ||
16298 V.allowConstexprUnknown()) {
16299 Result = V;
16300 return true;
16301 }
16302 return Success(SI: V.getInt(), E);
16303 }
16304
16305 bool ZeroInitialization(const Expr *E) { return Success(Value: 0, E); }
16306
16307 friend std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &,
16308 const CallExpr *);
16309
16310 //===--------------------------------------------------------------------===//
16311 // Visitor Methods
16312 //===--------------------------------------------------------------------===//
16313
16314 bool VisitIntegerLiteral(const IntegerLiteral *E) {
16315 return Success(I: E->getValue(), E);
16316 }
16317 bool VisitCharacterLiteral(const CharacterLiteral *E) {
16318 return Success(Value: E->getValue(), E);
16319 }
16320
16321 bool CheckReferencedDecl(const Expr *E, const Decl *D);
16322 bool VisitDeclRefExpr(const DeclRefExpr *E) {
16323 if (CheckReferencedDecl(E, D: E->getDecl()))
16324 return true;
16325
16326 return ExprEvaluatorBaseTy::VisitDeclRefExpr(S: E);
16327 }
16328 bool VisitMemberExpr(const MemberExpr *E) {
16329 if (CheckReferencedDecl(E, D: E->getMemberDecl())) {
16330 VisitIgnoredBaseExpression(E: E->getBase());
16331 return true;
16332 }
16333
16334 return ExprEvaluatorBaseTy::VisitMemberExpr(E);
16335 }
16336
16337 bool VisitCallExpr(const CallExpr *E);
16338 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
16339 bool VisitBinaryOperator(const BinaryOperator *E);
16340 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
16341 bool VisitUnaryOperator(const UnaryOperator *E);
16342
16343 bool VisitCastExpr(const CastExpr* E);
16344 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
16345
16346 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
16347 return Success(Value: E->getValue(), E);
16348 }
16349
16350 bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
16351 return Success(Value: E->getValue(), E);
16352 }
16353
16354 bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
16355 if (Info.ArrayInitIndex == uint64_t(-1)) {
16356 // We were asked to evaluate this subexpression independent of the
16357 // enclosing ArrayInitLoopExpr. We can't do that.
16358 Info.FFDiag(E);
16359 return false;
16360 }
16361 return Success(Value: Info.ArrayInitIndex, E);
16362 }
16363
16364 // Note, GNU defines __null as an integer, not a pointer.
16365 bool VisitGNUNullExpr(const GNUNullExpr *E) {
16366 return ZeroInitialization(E);
16367 }
16368
16369 bool VisitTypeTraitExpr(const TypeTraitExpr *E) {
16370 if (E->isStoredAsBoolean())
16371 return Success(Value: E->getBoolValue(), E);
16372 if (E->getAPValue().isAbsent())
16373 return false;
16374 assert(E->getAPValue().isInt() && "APValue type not supported");
16375 return Success(SI: E->getAPValue().getInt(), E);
16376 }
16377
16378 bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
16379 return Success(Value: E->getValue(), E);
16380 }
16381
16382 bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
16383 return Success(Value: E->getValue(), E);
16384 }
16385
16386 bool VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *E) {
16387 // This should not be evaluated during constant expr evaluation, as it
16388 // should always be in an unevaluated context (the args list of a 'gang' or
16389 // 'tile' clause).
16390 return Error(E);
16391 }
16392
16393 bool VisitUnaryReal(const UnaryOperator *E);
16394 bool VisitUnaryImag(const UnaryOperator *E);
16395
16396 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
16397 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
16398 bool VisitSourceLocExpr(const SourceLocExpr *E);
16399 bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);
16400 bool VisitRequiresExpr(const RequiresExpr *E);
16401 // FIXME: Missing: array subscript of vector, member of vector
16402};
16403
16404class FixedPointExprEvaluator
16405 : public ExprEvaluatorBase<FixedPointExprEvaluator> {
16406 APValue &Result;
16407
16408 public:
16409 FixedPointExprEvaluator(EvalInfo &info, APValue &result)
16410 : ExprEvaluatorBaseTy(info), Result(result) {}
16411
16412 bool Success(const llvm::APInt &I, const Expr *E) {
16413 return Success(
16414 V: APFixedPoint(I, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
16415 }
16416
16417 bool Success(uint64_t Value, const Expr *E) {
16418 return Success(
16419 V: APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
16420 }
16421
16422 bool Success(const APValue &V, const Expr *E) {
16423 return Success(V: V.getFixedPoint(), E);
16424 }
16425
16426 bool Success(const APFixedPoint &V, const Expr *E) {
16427 assert(E->getType()->isFixedPointType() && "Invalid evaluation result.");
16428 assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16429 "Invalid evaluation result.");
16430 Result = APValue(V);
16431 return true;
16432 }
16433
16434 bool ZeroInitialization(const Expr *E) {
16435 return Success(Value: 0, E);
16436 }
16437
16438 //===--------------------------------------------------------------------===//
16439 // Visitor Methods
16440 //===--------------------------------------------------------------------===//
16441
16442 bool VisitFixedPointLiteral(const FixedPointLiteral *E) {
16443 return Success(I: E->getValue(), E);
16444 }
16445
16446 bool VisitCastExpr(const CastExpr *E);
16447 bool VisitUnaryOperator(const UnaryOperator *E);
16448 bool VisitBinaryOperator(const BinaryOperator *E);
16449};
16450} // end anonymous namespace
16451
16452/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
16453/// produce either the integer value or a pointer.
16454///
16455/// GCC has a heinous extension which folds casts between pointer types and
16456/// pointer-sized integral types. We support this by allowing the evaluation of
16457/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
16458/// Some simple arithmetic on such values is supported (they are treated much
16459/// like char*).
16460static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
16461 EvalInfo &Info) {
16462 assert(!E->isValueDependent());
16463 assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType());
16464 return IntExprEvaluator(Info, Result).Visit(S: E);
16465}
16466
16467static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
16468 assert(!E->isValueDependent());
16469 APValue Val;
16470 if (!EvaluateIntegerOrLValue(E, Result&: Val, Info))
16471 return false;
16472 if (!Val.isInt()) {
16473 // FIXME: It would be better to produce the diagnostic for casting
16474 // a pointer to an integer.
16475 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
16476 return false;
16477 }
16478 Result = Val.getInt();
16479 return true;
16480}
16481
16482bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) {
16483 APValue Evaluated = E->EvaluateInContext(
16484 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
16485 return Success(V: Evaluated, E);
16486}
16487
16488static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
16489 EvalInfo &Info) {
16490 assert(!E->isValueDependent());
16491 if (E->getType()->isFixedPointType()) {
16492 APValue Val;
16493 if (!FixedPointExprEvaluator(Info, Val).Visit(S: E))
16494 return false;
16495 if (!Val.isFixedPoint())
16496 return false;
16497
16498 Result = Val.getFixedPoint();
16499 return true;
16500 }
16501 return false;
16502}
16503
16504static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
16505 EvalInfo &Info) {
16506 assert(!E->isValueDependent());
16507 if (E->getType()->isIntegerType()) {
16508 auto FXSema = Info.Ctx.getFixedPointSemantics(Ty: E->getType());
16509 APSInt Val;
16510 if (!EvaluateInteger(E, Result&: Val, Info))
16511 return false;
16512 Result = APFixedPoint(Val, FXSema);
16513 return true;
16514 } else if (E->getType()->isFixedPointType()) {
16515 return EvaluateFixedPoint(E, Result, Info);
16516 }
16517 return false;
16518}
16519
16520/// Check whether the given declaration can be directly converted to an integral
16521/// rvalue. If not, no diagnostic is produced; there are other things we can
16522/// try.
16523bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
16524 // Enums are integer constant exprs.
16525 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(Val: D)) {
16526 // Check for signedness/width mismatches between E type and ECD value.
16527 bool SameSign = (ECD->getInitVal().isSigned()
16528 == E->getType()->isSignedIntegerOrEnumerationType());
16529 bool SameWidth = (ECD->getInitVal().getBitWidth()
16530 == Info.Ctx.getIntWidth(T: E->getType()));
16531 if (SameSign && SameWidth)
16532 return Success(SI: ECD->getInitVal(), E);
16533 else {
16534 // Get rid of mismatch (otherwise Success assertions will fail)
16535 // by computing a new value matching the type of E.
16536 llvm::APSInt Val = ECD->getInitVal();
16537 if (!SameSign)
16538 Val.setIsSigned(!ECD->getInitVal().isSigned());
16539 if (!SameWidth)
16540 Val = Val.extOrTrunc(width: Info.Ctx.getIntWidth(T: E->getType()));
16541 return Success(SI: Val, E);
16542 }
16543 }
16544 return false;
16545}
16546
16547/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16548/// as GCC.
16549GCCTypeClass EvaluateBuiltinClassifyType(QualType T,
16550 const LangOptions &LangOpts) {
16551 assert(!T->isDependentType() && "unexpected dependent type");
16552
16553 QualType CanTy = T.getCanonicalType();
16554
16555 switch (CanTy->getTypeClass()) {
16556#define TYPE(ID, BASE)
16557#define DEPENDENT_TYPE(ID, BASE) case Type::ID:
16558#define NON_CANONICAL_TYPE(ID, BASE) case Type::ID:
16559#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID:
16560#include "clang/AST/TypeNodes.inc"
16561 case Type::Auto:
16562 case Type::DeducedTemplateSpecialization:
16563 llvm_unreachable("unexpected non-canonical or dependent type");
16564
16565 case Type::Builtin:
16566 switch (cast<BuiltinType>(Val&: CanTy)->getKind()) {
16567#define BUILTIN_TYPE(ID, SINGLETON_ID)
16568#define SIGNED_TYPE(ID, SINGLETON_ID) \
16569 case BuiltinType::ID: return GCCTypeClass::Integer;
16570#define FLOATING_TYPE(ID, SINGLETON_ID) \
16571 case BuiltinType::ID: return GCCTypeClass::RealFloat;
16572#define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \
16573 case BuiltinType::ID: break;
16574#include "clang/AST/BuiltinTypes.def"
16575 case BuiltinType::Void:
16576 return GCCTypeClass::Void;
16577
16578 case BuiltinType::Bool:
16579 return GCCTypeClass::Bool;
16580
16581 case BuiltinType::Char_U:
16582 case BuiltinType::UChar:
16583 case BuiltinType::WChar_U:
16584 case BuiltinType::Char8:
16585 case BuiltinType::Char16:
16586 case BuiltinType::Char32:
16587 case BuiltinType::UShort:
16588 case BuiltinType::UInt:
16589 case BuiltinType::ULong:
16590 case BuiltinType::ULongLong:
16591 case BuiltinType::UInt128:
16592 return GCCTypeClass::Integer;
16593
16594 case BuiltinType::UShortAccum:
16595 case BuiltinType::UAccum:
16596 case BuiltinType::ULongAccum:
16597 case BuiltinType::UShortFract:
16598 case BuiltinType::UFract:
16599 case BuiltinType::ULongFract:
16600 case BuiltinType::SatUShortAccum:
16601 case BuiltinType::SatUAccum:
16602 case BuiltinType::SatULongAccum:
16603 case BuiltinType::SatUShortFract:
16604 case BuiltinType::SatUFract:
16605 case BuiltinType::SatULongFract:
16606 return GCCTypeClass::None;
16607
16608 case BuiltinType::NullPtr:
16609
16610 case BuiltinType::ObjCId:
16611 case BuiltinType::ObjCClass:
16612 case BuiltinType::ObjCSel:
16613#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
16614 case BuiltinType::Id:
16615#include "clang/Basic/OpenCLImageTypes.def"
16616#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
16617 case BuiltinType::Id:
16618#include "clang/Basic/OpenCLExtensionTypes.def"
16619 case BuiltinType::OCLSampler:
16620 case BuiltinType::OCLEvent:
16621 case BuiltinType::OCLClkEvent:
16622 case BuiltinType::OCLQueue:
16623 case BuiltinType::OCLReserveID:
16624#define SVE_TYPE(Name, Id, SingletonId) \
16625 case BuiltinType::Id:
16626#include "clang/Basic/AArch64ACLETypes.def"
16627#define PPC_VECTOR_TYPE(Name, Id, Size) \
16628 case BuiltinType::Id:
16629#include "clang/Basic/PPCTypes.def"
16630#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16631#include "clang/Basic/RISCVVTypes.def"
16632#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16633#include "clang/Basic/WebAssemblyReferenceTypes.def"
16634#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
16635#include "clang/Basic/AMDGPUTypes.def"
16636#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16637#include "clang/Basic/HLSLIntangibleTypes.def"
16638#define HLSL_PACKED_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16639#include "clang/Basic/HLSLPackedTypes.def"
16640#define SPIRV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16641#include "clang/Basic/SPIRVTypes.def"
16642 case BuiltinType::MetaInfo:
16643 return GCCTypeClass::None;
16644
16645 case BuiltinType::Dependent:
16646 llvm_unreachable("unexpected dependent type");
16647 };
16648 llvm_unreachable("unexpected placeholder type");
16649
16650 case Type::Enum:
16651 return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer;
16652
16653 case Type::Pointer:
16654 case Type::ConstantArray:
16655 case Type::VariableArray:
16656 case Type::IncompleteArray:
16657 case Type::FunctionNoProto:
16658 case Type::FunctionProto:
16659 case Type::ArrayParameter:
16660 return GCCTypeClass::Pointer;
16661
16662 case Type::MemberPointer:
16663 return CanTy->isMemberDataPointerType()
16664 ? GCCTypeClass::PointerToDataMember
16665 : GCCTypeClass::PointerToMemberFunction;
16666
16667 case Type::Complex:
16668 return GCCTypeClass::Complex;
16669
16670 case Type::Record:
16671 return CanTy->isUnionType() ? GCCTypeClass::Union
16672 : GCCTypeClass::ClassOrStruct;
16673
16674 case Type::Atomic:
16675 // GCC classifies _Atomic T the same as T.
16676 return EvaluateBuiltinClassifyType(
16677 T: CanTy->castAs<AtomicType>()->getValueType(), LangOpts);
16678
16679 case Type::Vector:
16680 case Type::ExtVector:
16681 return GCCTypeClass::Vector;
16682
16683 case Type::BlockPointer:
16684 case Type::ConstantMatrix:
16685 case Type::ObjCObject:
16686 case Type::ObjCInterface:
16687 case Type::ObjCObjectPointer:
16688 case Type::Pipe:
16689 case Type::HLSLAttributedResource:
16690 case Type::HLSLInlineSpirv:
16691 case Type::OverflowBehavior:
16692 // Classify all other types that don't fit into the regular
16693 // classification the same way.
16694 return GCCTypeClass::None;
16695
16696 case Type::BitInt:
16697 return GCCTypeClass::BitInt;
16698
16699 case Type::LValueReference:
16700 case Type::RValueReference:
16701 llvm_unreachable("invalid type for expression");
16702 }
16703
16704 llvm_unreachable("unexpected type class");
16705}
16706
16707/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16708/// as GCC.
16709static GCCTypeClass
16710EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) {
16711 // If no argument was supplied, default to None. This isn't
16712 // ideal, however it is what gcc does.
16713 if (E->getNumArgs() == 0)
16714 return GCCTypeClass::None;
16715
16716 // FIXME: Bizarrely, GCC treats a call with more than one argument as not
16717 // being an ICE, but still folds it to a constant using the type of the first
16718 // argument.
16719 return EvaluateBuiltinClassifyType(T: E->getArg(Arg: 0)->getType(), LangOpts);
16720}
16721
16722/// EvaluateBuiltinConstantPForLValue - Determine the result of
16723/// __builtin_constant_p when applied to the given pointer.
16724///
16725/// A pointer is only "constant" if it is null (or a pointer cast to integer)
16726/// or it points to the first character of a string literal.
16727static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) {
16728 APValue::LValueBase Base = LV.getLValueBase();
16729 if (Base.isNull()) {
16730 // A null base is acceptable.
16731 return true;
16732 } else if (const Expr *E = Base.dyn_cast<const Expr *>()) {
16733 if (!isa<StringLiteral>(Val: E))
16734 return false;
16735 return LV.getLValueOffset().isZero();
16736 } else if (Base.is<TypeInfoLValue>()) {
16737 // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to
16738 // evaluate to true.
16739 return true;
16740 } else {
16741 // Any other base is not constant enough for GCC.
16742 return false;
16743 }
16744}
16745
16746/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
16747/// GCC as we can manage.
16748static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) {
16749 // This evaluation is not permitted to have side-effects, so evaluate it in
16750 // a speculative evaluation context.
16751 SpeculativeEvaluationRAII SpeculativeEval(Info);
16752
16753 // Constant-folding is always enabled for the operand of __builtin_constant_p
16754 // (even when the enclosing evaluation context otherwise requires a strict
16755 // language-specific constant expression).
16756 FoldConstant Fold(Info, true);
16757
16758 QualType ArgType = Arg->getType();
16759
16760 // __builtin_constant_p always has one operand. The rules which gcc follows
16761 // are not precisely documented, but are as follows:
16762 //
16763 // - If the operand is of integral, floating, complex or enumeration type,
16764 // and can be folded to a known value of that type, it returns 1.
16765 // - If the operand can be folded to a pointer to the first character
16766 // of a string literal (or such a pointer cast to an integral type)
16767 // or to a null pointer or an integer cast to a pointer, it returns 1.
16768 //
16769 // Otherwise, it returns 0.
16770 //
16771 // FIXME: GCC also intends to return 1 for literals of aggregate types, but
16772 // its support for this did not work prior to GCC 9 and is not yet well
16773 // understood.
16774 if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() ||
16775 ArgType->isAnyComplexType() || ArgType->isPointerType() ||
16776 ArgType->isNullPtrType()) {
16777 APValue V;
16778 if (!::EvaluateAsRValue(Info, E: Arg, Result&: V) || Info.EvalStatus.HasSideEffects) {
16779 Fold.keepDiagnostics();
16780 return false;
16781 }
16782
16783 // For a pointer (possibly cast to integer), there are special rules.
16784 if (V.getKind() == APValue::LValue)
16785 return EvaluateBuiltinConstantPForLValue(LV: V);
16786
16787 // Otherwise, any constant value is good enough.
16788 return V.hasValue();
16789 }
16790
16791 // Anything else isn't considered to be sufficiently constant.
16792 return false;
16793}
16794
16795/// Retrieves the "underlying object type" of the given expression,
16796/// as used by __builtin_object_size.
16797static QualType getObjectType(APValue::LValueBase B) {
16798 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
16799 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
16800 return VD->getType();
16801 } else if (const Expr *E = B.dyn_cast<const Expr*>()) {
16802 if (isa<CompoundLiteralExpr>(Val: E))
16803 return E->getType();
16804 } else if (B.is<TypeInfoLValue>()) {
16805 return B.getTypeInfoType();
16806 } else if (B.is<DynamicAllocLValue>()) {
16807 return B.getDynamicAllocType();
16808 }
16809
16810 return QualType();
16811}
16812
16813/// A more selective version of E->IgnoreParenCasts for
16814/// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only
16815/// to change the type of E.
16816/// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo`
16817///
16818/// Always returns an RValue with a pointer representation.
16819const Expr *ignorePointerCastsAndParens(const Expr *E) {
16820 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
16821
16822 const Expr *NoParens = E->IgnoreParens();
16823 const auto *Cast = dyn_cast<CastExpr>(Val: NoParens);
16824 if (Cast == nullptr)
16825 return NoParens;
16826
16827 // We only conservatively allow a few kinds of casts, because this code is
16828 // inherently a simple solution that seeks to support the common case.
16829 auto CastKind = Cast->getCastKind();
16830 if (CastKind != CK_NoOp && CastKind != CK_BitCast &&
16831 CastKind != CK_AddressSpaceConversion)
16832 return NoParens;
16833
16834 const auto *SubExpr = Cast->getSubExpr();
16835 if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue())
16836 return NoParens;
16837 return ignorePointerCastsAndParens(E: SubExpr);
16838}
16839
16840/// Checks to see if the given LValue's Designator is at the end of the LValue's
16841/// record layout. e.g.
16842/// struct { struct { int a, b; } fst, snd; } obj;
16843/// obj.fst // no
16844/// obj.snd // yes
16845/// obj.fst.a // no
16846/// obj.fst.b // no
16847/// obj.snd.a // no
16848/// obj.snd.b // yes
16849///
16850/// Please note: this function is specialized for how __builtin_object_size
16851/// views "objects".
16852///
16853/// If this encounters an invalid RecordDecl or otherwise cannot determine the
16854/// correct result, it will always return true.
16855static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) {
16856 assert(!LVal.Designator.Invalid);
16857
16858 auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD) {
16859 const RecordDecl *Parent = FD->getParent();
16860 if (Parent->isInvalidDecl() || Parent->isUnion())
16861 return true;
16862 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(D: Parent);
16863 return FD->getFieldIndex() + 1 == Layout.getFieldCount();
16864 };
16865
16866 auto &Base = LVal.getLValueBase();
16867 if (auto *ME = dyn_cast_or_null<MemberExpr>(Val: Base.dyn_cast<const Expr *>())) {
16868 if (auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl())) {
16869 if (!IsLastOrInvalidFieldDecl(FD))
16870 return false;
16871 } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(Val: ME->getMemberDecl())) {
16872 for (auto *FD : IFD->chain()) {
16873 if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(Val: FD)))
16874 return false;
16875 }
16876 }
16877 }
16878
16879 unsigned I = 0;
16880 QualType BaseType = getType(B: Base);
16881 if (LVal.Designator.FirstEntryIsAnUnsizedArray) {
16882 // If we don't know the array bound, conservatively assume we're looking at
16883 // the final array element.
16884 ++I;
16885 if (BaseType->isIncompleteArrayType())
16886 BaseType = Ctx.getAsArrayType(T: BaseType)->getElementType();
16887 else
16888 BaseType = BaseType->castAs<PointerType>()->getPointeeType();
16889 }
16890
16891 for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) {
16892 const auto &Entry = LVal.Designator.Entries[I];
16893 if (BaseType->isArrayType()) {
16894 // Because __builtin_object_size treats arrays as objects, we can ignore
16895 // the index iff this is the last array in the Designator.
16896 if (I + 1 == E)
16897 return true;
16898 const auto *CAT = cast<ConstantArrayType>(Val: Ctx.getAsArrayType(T: BaseType));
16899 uint64_t Index = Entry.getAsArrayIndex();
16900 if (Index + 1 != CAT->getZExtSize())
16901 return false;
16902 BaseType = CAT->getElementType();
16903 } else if (BaseType->isAnyComplexType()) {
16904 const auto *CT = BaseType->castAs<ComplexType>();
16905 uint64_t Index = Entry.getAsArrayIndex();
16906 if (Index != 1)
16907 return false;
16908 BaseType = CT->getElementType();
16909 } else if (auto *FD = getAsField(E: Entry)) {
16910 if (!IsLastOrInvalidFieldDecl(FD))
16911 return false;
16912 BaseType = FD->getType();
16913 } else {
16914 assert(getAsBaseClass(Entry) && "Expecting cast to a base class");
16915 return false;
16916 }
16917 }
16918 return true;
16919}
16920
16921/// Tests to see if the LValue has a user-specified designator (that isn't
16922/// necessarily valid). Note that this always returns 'true' if the LValue has
16923/// an unsized array as its first designator entry, because there's currently no
16924/// way to tell if the user typed *foo or foo[0].
16925static bool refersToCompleteObject(const LValue &LVal) {
16926 if (LVal.Designator.Invalid)
16927 return false;
16928
16929 if (!LVal.Designator.Entries.empty())
16930 return LVal.Designator.isMostDerivedAnUnsizedArray();
16931
16932 if (!LVal.InvalidBase)
16933 return true;
16934
16935 // If `E` is a MemberExpr, then the first part of the designator is hiding in
16936 // the LValueBase.
16937 const auto *E = LVal.Base.dyn_cast<const Expr *>();
16938 return !E || !isa<MemberExpr>(Val: E);
16939}
16940
16941/// Attempts to detect a user writing into a piece of memory that's impossible
16942/// to figure out the size of by just using types.
16943static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) {
16944 const SubobjectDesignator &Designator = LVal.Designator;
16945 // Notes:
16946 // - Users can only write off of the end when we have an invalid base. Invalid
16947 // bases imply we don't know where the memory came from.
16948 // - We used to be a bit more aggressive here; we'd only be conservative if
16949 // the array at the end was flexible, or if it had 0 or 1 elements. This
16950 // broke some common standard library extensions (PR30346), but was
16951 // otherwise seemingly fine. It may be useful to reintroduce this behavior
16952 // with some sort of list. OTOH, it seems that GCC is always
16953 // conservative with the last element in structs (if it's an array), so our
16954 // current behavior is more compatible than an explicit list approach would
16955 // be.
16956 auto isFlexibleArrayMember = [&] {
16957 using FAMKind = LangOptions::StrictFlexArraysLevelKind;
16958 FAMKind StrictFlexArraysLevel =
16959 Ctx.getLangOpts().getStrictFlexArraysLevel();
16960
16961 if (Designator.isMostDerivedAnUnsizedArray())
16962 return true;
16963
16964 if (StrictFlexArraysLevel == FAMKind::Default)
16965 return true;
16966
16967 if (Designator.getMostDerivedArraySize() == 0 &&
16968 StrictFlexArraysLevel != FAMKind::IncompleteOnly)
16969 return true;
16970
16971 if (Designator.getMostDerivedArraySize() == 1 &&
16972 StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete)
16973 return true;
16974
16975 return false;
16976 };
16977
16978 return LVal.InvalidBase &&
16979 Designator.Entries.size() == Designator.MostDerivedPathLength &&
16980 Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() &&
16981 isDesignatorAtObjectEnd(Ctx, LVal);
16982}
16983
16984/// Converts the given APInt to CharUnits, assuming the APInt is unsigned.
16985/// Fails if the conversion would cause loss of precision.
16986static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int,
16987 CharUnits &Result) {
16988 auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max();
16989 if (Int.ugt(RHS: CharUnitsMax))
16990 return false;
16991 Result = CharUnits::fromQuantity(Quantity: Int.getZExtValue());
16992 return true;
16993}
16994
16995/// If we're evaluating the object size of an instance of a struct that
16996/// contains a flexible array member, add the size of the initializer.
16997static void addFlexibleArrayMemberInitSize(EvalInfo &Info, const QualType &T,
16998 const LValue &LV, CharUnits &Size) {
16999 if (!T.isNull() && T->isStructureType() &&
17000 T->castAsRecordDecl()->hasFlexibleArrayMember())
17001 if (const auto *V = LV.getLValueBase().dyn_cast<const ValueDecl *>())
17002 if (const auto *VD = dyn_cast<VarDecl>(Val: V))
17003 if (VD->hasInit())
17004 Size += VD->getFlexibleArrayInitChars(Ctx: Info.Ctx);
17005}
17006
17007/// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will
17008/// determine how many bytes exist from the beginning of the object to either
17009/// the end of the current subobject, or the end of the object itself, depending
17010/// on what the LValue looks like + the value of Type.
17011///
17012/// If this returns false, the value of Result is undefined.
17013static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc,
17014 unsigned Type, const LValue &LVal,
17015 CharUnits &EndOffset) {
17016 bool DetermineForCompleteObject = refersToCompleteObject(LVal);
17017
17018 auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) {
17019 if (Ty.isNull())
17020 return false;
17021
17022 Ty = Ty.getNonReferenceType();
17023
17024 if (Ty->isIncompleteType() || Ty->isFunctionType())
17025 return false;
17026
17027 return HandleSizeof(Info, Loc: ExprLoc, Type: Ty, Size&: Result);
17028 };
17029
17030 // We want to evaluate the size of the entire object. This is a valid fallback
17031 // for when Type=1 and the designator is invalid, because we're asked for an
17032 // upper-bound.
17033 if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) {
17034 // Type=3 wants a lower bound, so we can't fall back to this.
17035 if (Type == 3 && !DetermineForCompleteObject)
17036 return false;
17037
17038 llvm::APInt APEndOffset;
17039 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
17040 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
17041 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
17042
17043 if (LVal.InvalidBase)
17044 return false;
17045
17046 QualType BaseTy = getObjectType(B: LVal.getLValueBase());
17047 const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset);
17048 addFlexibleArrayMemberInitSize(Info, T: BaseTy, LV: LVal, Size&: EndOffset);
17049 return Ret;
17050 }
17051
17052 // We want to evaluate the size of a subobject.
17053 const SubobjectDesignator &Designator = LVal.Designator;
17054
17055 // The following is a moderately common idiom in C:
17056 //
17057 // struct Foo { int a; char c[1]; };
17058 // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar));
17059 // strcpy(&F->c[0], Bar);
17060 //
17061 // In order to not break too much legacy code, we need to support it.
17062 if (isUserWritingOffTheEnd(Ctx: Info.Ctx, LVal)) {
17063 // If we can resolve this to an alloc_size call, we can hand that back,
17064 // because we know for certain how many bytes there are to write to.
17065 llvm::APInt APEndOffset;
17066 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
17067 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
17068 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
17069
17070 // If we cannot determine the size of the initial allocation, then we can't
17071 // given an accurate upper-bound. However, we are still able to give
17072 // conservative lower-bounds for Type=3.
17073 if (Type == 1)
17074 return false;
17075 }
17076
17077 CharUnits BytesPerElem;
17078 if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem))
17079 return false;
17080
17081 // According to the GCC documentation, we want the size of the subobject
17082 // denoted by the pointer. But that's not quite right -- what we actually
17083 // want is the size of the immediately-enclosing array, if there is one.
17084 int64_t ElemsRemaining;
17085 if (Designator.MostDerivedIsArrayElement &&
17086 Designator.Entries.size() == Designator.MostDerivedPathLength) {
17087 uint64_t ArraySize = Designator.getMostDerivedArraySize();
17088 uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex();
17089 ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex;
17090 } else {
17091 ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1;
17092 }
17093
17094 EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining;
17095 return true;
17096}
17097
17098/// Tries to evaluate the __builtin_object_size for @p E.
17099///
17100/// If @p IsDynamic is true (i.e. we're evaluating
17101/// __builtin_dynamic_object_size) and the operand designates a flexible array
17102/// member annotated with 'counted_by', we refuse to fold so that IR generation
17103/// can emit the count-based runtime size computation.
17104static std::optional<uint64_t>
17105tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, EvalInfo &Info,
17106 bool IsDynamic = false) {
17107
17108 // Determine the denoted object.
17109 LValue LVal;
17110 {
17111 // The operand of __builtin_object_size is never evaluated for side-effects.
17112 // If there are any, but we can determine the pointed-to object anyway, then
17113 // ignore the side-effects.
17114 SpeculativeEvaluationRAII SpeculativeEval(Info);
17115 IgnoreSideEffectsRAII Fold(Info);
17116
17117 if (E->isGLValue()) {
17118 // It's possible for us to be given GLValues if we're called via
17119 // Expr::tryEvaluateObjectSize.
17120 APValue RVal;
17121 if (!EvaluateAsRValue(Info, E, Result&: RVal))
17122 return std::nullopt;
17123 LVal.setFrom(Ctx: Info.Ctx, V: RVal);
17124 } else if (!EvaluatePointer(E: ignorePointerCastsAndParens(E), Result&: LVal, Info,
17125 /*InvalidBaseOK=*/true))
17126 return std::nullopt;
17127 }
17128
17129 // If we point to before the start of the object, there are no accessible
17130 // bytes.
17131 if (LVal.getLValueOffset().isNegative())
17132 return 0;
17133
17134 // For __builtin_dynamic_object_size on a counted_by-annotated flexible
17135 // array member, defer to IR generation (emitCountedBySize in CGBuiltin):
17136 // its runtime computation uses the live 'count' field and is more accurate
17137 // than the layout/initializer-derived size we'd produce here. Use the same
17138 // findStructFieldAccess form-recognition CGBuiltin does, so we refuse to
17139 // fold on exactly the shapes that path handles (and, importantly, *not*
17140 // on '&af.fam' which designates the array-as-a-whole and stays on the
17141 // layout-derived path to match GCC). Checked after the negative-offset
17142 // early return above so that obviously out-of-bounds operands still fold
17143 // to 0, preserving existing behavior.
17144 if (IsDynamic) {
17145 const auto *ME = dyn_cast_or_null<MemberExpr>(Val: findStructFieldAccess(E));
17146 const auto *FD = ME ? dyn_cast<FieldDecl>(Val: ME->getMemberDecl()) : nullptr;
17147 if (FD && FD->getType()->isCountAttributedType())
17148 return std::nullopt;
17149 }
17150
17151 CharUnits EndOffset;
17152 if (!determineEndOffset(Info, ExprLoc: E->getExprLoc(), Type, LVal, EndOffset))
17153 return std::nullopt;
17154
17155 // If we've fallen outside of the end offset, just pretend there's nothing to
17156 // write to/read from.
17157 if (EndOffset <= LVal.getLValueOffset())
17158 return 0;
17159 return (EndOffset - LVal.getLValueOffset()).getQuantity();
17160}
17161
17162bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
17163 if (!IsConstantEvaluatedBuiltinCall(E))
17164 return ExprEvaluatorBaseTy::VisitCallExpr(E);
17165 return VisitBuiltinCallExpr(E, BuiltinOp: ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E));
17166}
17167
17168static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info,
17169 APValue &Val, APSInt &Alignment) {
17170 QualType SrcTy = E->getArg(Arg: 0)->getType();
17171 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: SrcTy, Info, Alignment))
17172 return false;
17173 // Even though we are evaluating integer expressions we could get a pointer
17174 // argument for the __builtin_is_aligned() case.
17175 if (SrcTy->isPointerType()) {
17176 LValue Ptr;
17177 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Ptr, Info))
17178 return false;
17179 Ptr.moveInto(V&: Val);
17180 } else if (!SrcTy->isIntegralOrEnumerationType()) {
17181 Info.FFDiag(E: E->getArg(Arg: 0));
17182 return false;
17183 } else {
17184 APSInt SrcInt;
17185 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SrcInt, Info))
17186 return false;
17187 assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() &&
17188 "Bit widths must be the same");
17189 Val = APValue(SrcInt);
17190 }
17191 assert(Val.hasValue());
17192 return true;
17193}
17194
17195// Implements the C2y §7.18.21 stdc_load8_* family: reads N bytes from
17196// E->getArg(0) and assembles them per IsBE into a value of E's type. The
17197// aligned_* variants additionally require the pointer to be aligned to the
17198// natural alignment of E's type.
17199static bool EvaluateStdcLoad8(EvalInfo &Info, const CallExpr *E, bool IsBE,
17200 bool IsAligned, APSInt &Result) {
17201 LValue Ptr;
17202 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Ptr, Info))
17203 return false;
17204
17205 if (!Ptr.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
17206 Ptr.Designator.Invalid)
17207 return false;
17208
17209 if (Ptr.Designator.isMostDerivedAnUnsizedArray())
17210 return false;
17211
17212 if (IsAligned) {
17213 CharUnits RequiredAlign = Info.Ctx.getTypeAlignInChars(T: E->getType());
17214 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Ptr);
17215 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Ptr.Offset);
17216 if (PtrAlign < RequiredAlign) {
17217 Info.FFDiag(E, DiagId: diag::note_constexpr_load8_unaligned)
17218 << Info.Ctx.BuiltinInfo.getQuotedName(ID: E->getBuiltinCallee())
17219 << RequiredAlign.getQuantity() << PtrAlign.getQuantity();
17220 return false;
17221 }
17222 }
17223
17224 QualType CharTy = Ptr.Designator.getType(Ctx&: Info.Ctx);
17225 unsigned ByteWidth = Info.Ctx.getTypeSize(T: E->getType()) / 8;
17226 uint64_t RemainingElems = Ptr.Designator.validIndexAdjustments().second;
17227 if (ByteWidth > RemainingElems) {
17228 uint64_t ArrayIndex = Ptr.Designator.MostDerivedIsArrayElement
17229 ? Ptr.Designator.Entries.back().getAsArrayIndex()
17230 : (uint64_t)Ptr.Designator.IsOnePastTheEnd;
17231 APSInt Index = APSInt::get(X: ArrayIndex + ByteWidth - 1);
17232 Ptr.Designator.diagnosePointerArithmetic(Info, E, N: Index);
17233 return false;
17234 }
17235
17236 // Load the bytes and assemble them per C2y §7.18.21:
17237 // result = sum(b_index * 2^(8*index)) where b_index = ptr[index] (LE)
17238 // or ptr[N/8 - index - 1] (BE).
17239 uint64_t ResultBits = 0;
17240 LValue BytePtr = Ptr;
17241 for (unsigned I = 0; I < ByteWidth; ++I) {
17242 APValue ByteVal;
17243 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: BytePtr, RVal&: ByteVal))
17244 return false;
17245 unsigned Shift = 8 * (IsBE ? ByteWidth - I - 1 : I);
17246 ResultBits |= ByteVal.getInt().getZExtValue() << Shift;
17247 if (I + 1 < ByteWidth)
17248 if (!HandleLValueArrayAdjustment(Info, E, LVal&: BytePtr, EltTy: CharTy, Adjustment: 1))
17249 return false;
17250 }
17251
17252 bool IsSigned = E->getType()->isSignedIntegerType();
17253 Result = APSInt(APInt(ByteWidth * 8, ResultBits), !IsSigned);
17254 return true;
17255}
17256
17257bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
17258 unsigned BuiltinOp) {
17259 if (Info.Ctx.BuiltinInfo.isReflectionMetafunctionBuiltin(ID: BuiltinOp)) {
17260 APValue Result;
17261 if (!EvaluateReflectionMetafunctionBuiltin(Info, E, BuiltinOp, Result))
17262 return false;
17263 return Success(V: Result, E);
17264 }
17265
17266 auto EvalTestOp = [&](llvm::function_ref<bool(const APInt &, const APInt &)>
17267 Fn) {
17268 APValue SourceLHS, SourceRHS;
17269 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
17270 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
17271 return false;
17272
17273 unsigned SourceLen = SourceLHS.getVectorLength();
17274 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
17275 QualType ElemQT = VT->getElementType();
17276 unsigned LaneWidth = Info.Ctx.getTypeSize(T: ElemQT);
17277
17278 APInt AWide(LaneWidth * SourceLen, 0);
17279 APInt BWide(LaneWidth * SourceLen, 0);
17280
17281 for (unsigned I = 0; I != SourceLen; ++I) {
17282 APInt ALane;
17283 APInt BLane;
17284 if (ElemQT->isIntegerType()) { // Get value.
17285 ALane = SourceLHS.getVectorElt(I).getInt();
17286 BLane = SourceRHS.getVectorElt(I).getInt();
17287 } else if (ElemQT->isFloatingType()) { // Get only sign bit.
17288 ALane =
17289 SourceLHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
17290 BLane =
17291 SourceRHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
17292 } else { // Must be integer or floating type.
17293 return false;
17294 }
17295 AWide.insertBits(SubBits: ALane, bitPosition: I * LaneWidth);
17296 BWide.insertBits(SubBits: BLane, bitPosition: I * LaneWidth);
17297 }
17298 return Success(Value: Fn(AWide, BWide), E);
17299 };
17300
17301 auto HandleMaskBinOp =
17302 [&](llvm::function_ref<APSInt(const APSInt &, const APSInt &)> Fn)
17303 -> bool {
17304 APValue LHS, RHS;
17305 if (!Evaluate(Result&: LHS, Info, E: E->getArg(Arg: 0)) ||
17306 !Evaluate(Result&: RHS, Info, E: E->getArg(Arg: 1)))
17307 return false;
17308
17309 APSInt ResultInt = Fn(LHS.getInt(), RHS.getInt());
17310
17311 return Success(V: APValue(ResultInt), E);
17312 };
17313
17314 auto HandleCRC32 = [&](unsigned DataBytes) -> bool {
17315 APSInt CRC, Data;
17316 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CRC, Info) ||
17317 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Data, Info))
17318 return false;
17319
17320 uint64_t CRCVal = CRC.getZExtValue();
17321 uint64_t DataVal = Data.getZExtValue();
17322
17323 // CRC32C polynomial (iSCSI polynomial, bit-reversed)
17324 static const uint32_t CRC32C_POLY = 0x82F63B78;
17325
17326 uint32_t Result = llvm::calculateReflectedCRC32(
17327 Crc: static_cast<uint32_t>(CRCVal), Data: DataVal, DataBytes, Poly: CRC32C_POLY);
17328
17329 return Success(Value: Result, E);
17330 };
17331
17332 switch (BuiltinOp) {
17333 default:
17334 return false;
17335
17336 case X86::BI__builtin_ia32_crc32qi:
17337 return HandleCRC32(1);
17338 case X86::BI__builtin_ia32_crc32hi:
17339 return HandleCRC32(2);
17340 case X86::BI__builtin_ia32_crc32si:
17341 return HandleCRC32(4);
17342 case X86::BI__builtin_ia32_crc32di:
17343 return HandleCRC32(8);
17344
17345 case Builtin::BI__builtin_dynamic_object_size:
17346 case Builtin::BI__builtin_object_size: {
17347 APSInt TypeValue;
17348 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: TypeValue, Info))
17349 return false;
17350 if (TypeValue < 0 || TypeValue > 3) {
17351 Info.FFDiag(E: E->getArg(Arg: 1), DiagId: diag::note_constexpr_builtin_arg_out_of_range)
17352 << TypeValue << 0 << 3;
17353 return false;
17354 }
17355 unsigned Type = TypeValue.getZExtValue();
17356
17357 bool IsDynamic = BuiltinOp == Builtin::BI__builtin_dynamic_object_size;
17358 if (std::optional<uint64_t> Size =
17359 tryEvaluateBuiltinObjectSize(E: E->getArg(Arg: 0), Type, Info, IsDynamic))
17360 return Success(Value: *Size, E);
17361
17362 if (E->getArg(Arg: 0)->HasSideEffects(Ctx: Info.Ctx))
17363 return Success(Value: (Type & 2) ? 0 : -1, E);
17364
17365 // Expression had no side effects, but we couldn't statically determine the
17366 // size of the referenced object.
17367 switch (Info.EvalMode) {
17368 case EvaluationMode::ConstantExpression:
17369 case EvaluationMode::ConstantFold:
17370 case EvaluationMode::IgnoreSideEffects:
17371 // Leave it to IR generation.
17372 return Error(E);
17373 case EvaluationMode::ConstantExpressionUnevaluated:
17374 // Reduce it to a constant now.
17375 return Success(Value: (Type & 2) ? 0 : -1, E);
17376 }
17377
17378 llvm_unreachable("unexpected EvalMode");
17379 }
17380
17381 case Builtin::BI__builtin_os_log_format_buffer_size: {
17382 analyze_os_log::OSLogBufferLayout Layout;
17383 analyze_os_log::computeOSLogBufferLayout(Ctx&: Info.Ctx, E, layout&: Layout);
17384 return Success(Value: Layout.size().getQuantity(), E);
17385 }
17386
17387 case Builtin::BI__builtin_is_aligned: {
17388 APValue Src;
17389 APSInt Alignment;
17390 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17391 return false;
17392 if (Src.isLValue()) {
17393 // If we evaluated a pointer, check the minimum known alignment.
17394 LValue Ptr;
17395 Ptr.setFrom(Ctx: Info.Ctx, V: Src);
17396 if (!Ptr.Base) {
17397 // Null pointers are always aligned.
17398 if (Ptr.Offset.isZero())
17399 return Success(Value: 1, E);
17400
17401 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_compute)
17402 << Alignment;
17403 // Reject non-null pointers without an underlying object.
17404 // Do not interpret the pointer offset as an integer address.
17405 return false;
17406 }
17407
17408 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Ptr);
17409 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Ptr.Offset);
17410 // We can return true if the known alignment at the computed offset is
17411 // greater than the requested alignment.
17412 assert(PtrAlign.isPowerOfTwo());
17413 assert(Alignment.isPowerOf2());
17414 if (PtrAlign.getQuantity() >= Alignment)
17415 return Success(Value: 1, E);
17416 // If the alignment is not known to be sufficient, some cases could still
17417 // be aligned at run time. However, if the requested alignment is less or
17418 // equal to the base alignment and the offset is not aligned, we know that
17419 // the run-time value can never be aligned.
17420 if (BaseAlignment.getQuantity() >= Alignment &&
17421 PtrAlign.getQuantity() < Alignment)
17422 return Success(Value: 0, E);
17423 // Otherwise we can't infer whether the value is sufficiently aligned.
17424 // TODO: __builtin_is_aligned(__builtin_align_{down,up{(expr, N), N)
17425 // in cases where we can't fully evaluate the pointer.
17426 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_compute)
17427 << Alignment;
17428 return false;
17429 }
17430 assert(Src.isInt());
17431 return Success(Value: (Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E);
17432 }
17433 case Builtin::BI__builtin_align_up: {
17434 APValue Src;
17435 APSInt Alignment;
17436 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17437 return false;
17438 if (!Src.isInt())
17439 return Error(E);
17440 APSInt AlignedVal =
17441 APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1),
17442 Src.getInt().isUnsigned());
17443 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
17444 return Success(SI: AlignedVal, E);
17445 }
17446 case Builtin::BI__builtin_align_down: {
17447 APValue Src;
17448 APSInt Alignment;
17449 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17450 return false;
17451 if (!Src.isInt())
17452 return Error(E);
17453 APSInt AlignedVal =
17454 APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned());
17455 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
17456 return Success(SI: AlignedVal, E);
17457 }
17458
17459 case Builtin::BI__builtin_bitreverseg:
17460 case Builtin::BI__builtin_bitreverse8:
17461 case Builtin::BI__builtin_bitreverse16:
17462 case Builtin::BI__builtin_bitreverse32:
17463 case Builtin::BI__builtin_bitreverse64:
17464 case Builtin::BI__builtin_elementwise_bitreverse: {
17465 APSInt Val;
17466 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17467 return false;
17468
17469 return Success(I: Val.reverseBits(), E);
17470 }
17471 case Builtin::BI__builtin_bswapg:
17472 case Builtin::BI__builtin_bswap16:
17473 case Builtin::BI__builtin_bswap32:
17474 case Builtin::BI__builtin_bswap64:
17475 case Builtin::BIstdc_memreverse8u8:
17476 case Builtin::BIstdc_memreverse8u16:
17477 case Builtin::BIstdc_memreverse8u32:
17478 case Builtin::BIstdc_memreverse8u64: {
17479 APSInt Val;
17480 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17481 return false;
17482 if (Val.getBitWidth() == 8 || Val.getBitWidth() == 1)
17483 return Success(SI: Val, E);
17484
17485 return Success(I: Val.byteSwap(), E);
17486 }
17487
17488 case Builtin::BIstdc_load8_leu8:
17489 case Builtin::BIstdc_load8_leu16:
17490 case Builtin::BIstdc_load8_leu32:
17491 case Builtin::BIstdc_load8_leu64:
17492 case Builtin::BIstdc_load8_les8:
17493 case Builtin::BIstdc_load8_les16:
17494 case Builtin::BIstdc_load8_les32:
17495 case Builtin::BIstdc_load8_les64: {
17496 APSInt Result;
17497 if (!EvaluateStdcLoad8(Info, E, /*IsBE=*/false, /*IsAligned=*/false,
17498 Result))
17499 return false;
17500 return Success(SI: Result, E);
17501 }
17502
17503 case Builtin::BIstdc_load8_aligned_leu8:
17504 case Builtin::BIstdc_load8_aligned_leu16:
17505 case Builtin::BIstdc_load8_aligned_leu32:
17506 case Builtin::BIstdc_load8_aligned_leu64:
17507 case Builtin::BIstdc_load8_aligned_les8:
17508 case Builtin::BIstdc_load8_aligned_les16:
17509 case Builtin::BIstdc_load8_aligned_les32:
17510 case Builtin::BIstdc_load8_aligned_les64: {
17511 APSInt Result;
17512 if (!EvaluateStdcLoad8(Info, E, /*IsBE=*/false, /*IsAligned=*/true, Result))
17513 return false;
17514 return Success(SI: Result, E);
17515 }
17516
17517 case Builtin::BIstdc_load8_beu8:
17518 case Builtin::BIstdc_load8_beu16:
17519 case Builtin::BIstdc_load8_beu32:
17520 case Builtin::BIstdc_load8_beu64:
17521 case Builtin::BIstdc_load8_bes8:
17522 case Builtin::BIstdc_load8_bes16:
17523 case Builtin::BIstdc_load8_bes32:
17524 case Builtin::BIstdc_load8_bes64: {
17525 APSInt Result;
17526 if (!EvaluateStdcLoad8(Info, E, /*IsBE=*/true, /*IsAligned=*/false, Result))
17527 return false;
17528 return Success(SI: Result, E);
17529 }
17530
17531 case Builtin::BIstdc_load8_aligned_beu8:
17532 case Builtin::BIstdc_load8_aligned_beu16:
17533 case Builtin::BIstdc_load8_aligned_beu32:
17534 case Builtin::BIstdc_load8_aligned_beu64:
17535 case Builtin::BIstdc_load8_aligned_bes8:
17536 case Builtin::BIstdc_load8_aligned_bes16:
17537 case Builtin::BIstdc_load8_aligned_bes32:
17538 case Builtin::BIstdc_load8_aligned_bes64: {
17539 APSInt Result;
17540 if (!EvaluateStdcLoad8(Info, E, /*IsBE=*/true, /*IsAligned=*/true, Result))
17541 return false;
17542 return Success(SI: Result, E);
17543 }
17544
17545 case Builtin::BI__builtin_classify_type:
17546 return Success(Value: (int)EvaluateBuiltinClassifyType(E, LangOpts: Info.getLangOpts()), E);
17547
17548 case Builtin::BI__builtin_clrsb:
17549 case Builtin::BI__builtin_clrsbl:
17550 case Builtin::BI__builtin_clrsbll: {
17551 APSInt Val;
17552 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17553 return false;
17554
17555 return Success(Value: Val.getBitWidth() - Val.getSignificantBits(), E);
17556 }
17557
17558 case Builtin::BI__builtin_clz:
17559 case Builtin::BI__builtin_clzl:
17560 case Builtin::BI__builtin_clzll:
17561 case Builtin::BI__builtin_clzs:
17562 case Builtin::BI__builtin_clzg:
17563 case Builtin::BI__builtin_elementwise_clzg:
17564 case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes
17565 case Builtin::BI__lzcnt:
17566 case Builtin::BI__lzcnt64: {
17567 APSInt Val;
17568 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17569 APValue Vec;
17570 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17571 return false;
17572 Val = ConvertBoolVectorToInt(Val: Vec);
17573 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17574 return false;
17575 }
17576
17577 std::optional<APSInt> Fallback;
17578 if ((BuiltinOp == Builtin::BI__builtin_clzg ||
17579 BuiltinOp == Builtin::BI__builtin_elementwise_clzg) &&
17580 E->getNumArgs() > 1) {
17581 APSInt FallbackTemp;
17582 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
17583 return false;
17584 Fallback = FallbackTemp;
17585 }
17586
17587 if (!Val) {
17588 if (Fallback)
17589 return Success(SI: *Fallback, E);
17590
17591 // When the argument is 0, the result of GCC builtins is undefined,
17592 // whereas for Microsoft intrinsics, the result is the bit-width of the
17593 // argument.
17594 bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 &&
17595 BuiltinOp != Builtin::BI__lzcnt &&
17596 BuiltinOp != Builtin::BI__lzcnt64;
17597
17598 if (BuiltinOp == Builtin::BI__builtin_elementwise_clzg) {
17599 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
17600 << /*IsTrailing=*/false;
17601 }
17602
17603 if (ZeroIsUndefined)
17604 return Error(E);
17605 }
17606
17607 return Success(Value: Val.countl_zero(), E);
17608 }
17609
17610 case Builtin::BI__builtin_constant_p: {
17611 const Expr *Arg = E->getArg(Arg: 0);
17612 if (EvaluateBuiltinConstantP(Info, Arg))
17613 return Success(Value: true, E);
17614 if (Info.InConstantContext || Arg->HasSideEffects(Ctx: Info.Ctx)) {
17615 // Outside a constant context, eagerly evaluate to false in the presence
17616 // of side-effects in order to avoid -Wunsequenced false-positives in
17617 // a branch on __builtin_constant_p(expr).
17618 return Success(Value: false, E);
17619 }
17620 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17621 return false;
17622 }
17623
17624 case Builtin::BI__noop:
17625 // __noop always evaluates successfully and returns 0.
17626 return Success(Value: 0, E);
17627
17628 case Builtin::BI__builtin_is_constant_evaluated: {
17629 const auto *Callee = Info.CurrentCall->getCallee();
17630 if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression &&
17631 (Info.CallStackDepth == 1 ||
17632 (Info.CallStackDepth == 2 && Callee->isInStdNamespace() &&
17633 Callee->getIdentifier() &&
17634 Callee->getIdentifier()->isStr(Str: "is_constant_evaluated")))) {
17635 // FIXME: Find a better way to avoid duplicated diagnostics.
17636 if (Info.EvalStatus.Diag)
17637 Info.report(Loc: (Info.CallStackDepth == 1)
17638 ? E->getExprLoc()
17639 : Info.CurrentCall->getCallRange().getBegin(),
17640 DiagId: diag::warn_is_constant_evaluated_always_true_constexpr)
17641 << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated"
17642 : "std::is_constant_evaluated");
17643 }
17644
17645 return Success(Value: Info.InConstantContext, E);
17646 }
17647
17648 case Builtin::BI__builtin_is_within_lifetime:
17649 if (auto result = EvaluateBuiltinIsWithinLifetime(*this, E))
17650 return Success(Value: *result, E);
17651 return false;
17652
17653 case Builtin::BI__builtin_ctz:
17654 case Builtin::BI__builtin_ctzl:
17655 case Builtin::BI__builtin_ctzll:
17656 case Builtin::BI__builtin_ctzs:
17657 case Builtin::BI__builtin_ctzg:
17658 case Builtin::BI__builtin_elementwise_ctzg: {
17659 APSInt Val;
17660 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17661 APValue Vec;
17662 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17663 return false;
17664 Val = ConvertBoolVectorToInt(Val: Vec);
17665 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17666 return false;
17667 }
17668
17669 std::optional<APSInt> Fallback;
17670 if ((BuiltinOp == Builtin::BI__builtin_ctzg ||
17671 BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) &&
17672 E->getNumArgs() > 1) {
17673 APSInt FallbackTemp;
17674 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
17675 return false;
17676 Fallback = FallbackTemp;
17677 }
17678
17679 if (!Val) {
17680 if (Fallback)
17681 return Success(SI: *Fallback, E);
17682
17683 if (BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) {
17684 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
17685 << /*IsTrailing=*/true;
17686 }
17687 return Error(E);
17688 }
17689
17690 return Success(Value: Val.countr_zero(), E);
17691 }
17692
17693 case Builtin::BI__builtin_eh_return_data_regno: {
17694 int Operand = E->getArg(Arg: 0)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
17695 Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(RegNo: Operand);
17696 return Success(Value: Operand, E);
17697 }
17698
17699 case Builtin::BI__builtin_elementwise_abs: {
17700 APSInt Val;
17701 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17702 return false;
17703
17704 return Success(I: Val.abs(), E);
17705 }
17706
17707 case Builtin::BI__builtin_expect:
17708 case Builtin::BI__builtin_expect_with_probability:
17709 return Visit(S: E->getArg(Arg: 0));
17710
17711 case Builtin::BI__builtin_ptrauth_string_discriminator: {
17712 const auto *Literal =
17713 cast<StringLiteral>(Val: E->getArg(Arg: 0)->IgnoreParenImpCasts());
17714 uint64_t Result = getPointerAuthStableSipHash(S: Literal->getString());
17715 return Success(Value: Result, E);
17716 }
17717
17718 case Builtin::BI__builtin_infer_alloc_token: {
17719 // If we fail to infer a type, this fails to be a constant expression; this
17720 // can be checked with __builtin_constant_p(...).
17721 QualType AllocType = infer_alloc::inferPossibleType(E, Ctx: Info.Ctx, CastE: nullptr);
17722 if (AllocType.isNull())
17723 return Error(
17724 E, D: diag::note_constexpr_infer_alloc_token_type_inference_failed);
17725 auto ATMD = infer_alloc::getAllocTokenMetadata(T: AllocType, Ctx: Info.Ctx);
17726 if (!ATMD)
17727 return Error(E, D: diag::note_constexpr_infer_alloc_token_no_metadata);
17728 auto Mode =
17729 Info.getLangOpts().AllocTokenMode.value_or(u: llvm::DefaultAllocTokenMode);
17730 uint64_t BitWidth = Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType());
17731 auto MaxTokensOpt = Info.getLangOpts().AllocTokenMax;
17732 uint64_t MaxTokens =
17733 MaxTokensOpt.value_or(u: 0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
17734 auto MaybeToken = llvm::getAllocToken(Mode, Metadata: *ATMD, MaxTokens);
17735 if (!MaybeToken)
17736 return Error(E, D: diag::note_constexpr_infer_alloc_token_unsupported_mode);
17737 return Success(I: llvm::APInt(BitWidth, *MaybeToken), E);
17738 }
17739
17740 case Builtin::BI__builtin_ffs:
17741 case Builtin::BI__builtin_ffsl:
17742 case Builtin::BI__builtin_ffsll: {
17743 APSInt Val;
17744 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17745 return false;
17746
17747 unsigned N = Val.countr_zero();
17748 return Success(Value: N == Val.getBitWidth() ? 0 : N + 1, E);
17749 }
17750
17751 case Builtin::BI__builtin_fpclassify: {
17752 APFloat Val(0.0);
17753 if (!EvaluateFloat(E: E->getArg(Arg: 5), Result&: Val, Info))
17754 return false;
17755 unsigned Arg;
17756 switch (Val.getCategory()) {
17757 case APFloat::fcNaN: Arg = 0; break;
17758 case APFloat::fcInfinity: Arg = 1; break;
17759 case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break;
17760 case APFloat::fcZero: Arg = 4; break;
17761 }
17762 return Visit(S: E->getArg(Arg));
17763 }
17764
17765 case Builtin::BI__builtin_isinf_sign: {
17766 APFloat Val(0.0);
17767 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17768 Success(Value: Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E);
17769 }
17770
17771 case Builtin::BI__builtin_isinf: {
17772 APFloat Val(0.0);
17773 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17774 Success(Value: Val.isInfinity() ? 1 : 0, E);
17775 }
17776
17777 case Builtin::BI__builtin_isfinite: {
17778 APFloat Val(0.0);
17779 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17780 Success(Value: Val.isFinite() ? 1 : 0, E);
17781 }
17782
17783 case Builtin::BI__builtin_isnan: {
17784 APFloat Val(0.0);
17785 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17786 Success(Value: Val.isNaN() ? 1 : 0, E);
17787 }
17788
17789 case Builtin::BI__builtin_isnormal: {
17790 APFloat Val(0.0);
17791 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17792 Success(Value: Val.isNormal() ? 1 : 0, E);
17793 }
17794
17795 case Builtin::BI__builtin_issubnormal: {
17796 APFloat Val(0.0);
17797 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17798 Success(Value: Val.isDenormal() ? 1 : 0, E);
17799 }
17800
17801 case Builtin::BI__builtin_iszero: {
17802 APFloat Val(0.0);
17803 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17804 Success(Value: Val.isZero() ? 1 : 0, E);
17805 }
17806
17807 case Builtin::BI__builtin_signbit:
17808 case Builtin::BI__builtin_signbitf:
17809 case Builtin::BI__builtin_signbitl: {
17810 APFloat Val(0.0);
17811 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17812 Success(Value: Val.isNegative() ? 1 : 0, E);
17813 }
17814
17815 case Builtin::BI__builtin_isgreater:
17816 case Builtin::BI__builtin_isgreaterequal:
17817 case Builtin::BI__builtin_isless:
17818 case Builtin::BI__builtin_islessequal:
17819 case Builtin::BI__builtin_islessgreater:
17820 case Builtin::BI__builtin_isunordered: {
17821 APFloat LHS(0.0);
17822 APFloat RHS(0.0);
17823 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17824 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
17825 return false;
17826
17827 return Success(
17828 Value: [&] {
17829 switch (BuiltinOp) {
17830 case Builtin::BI__builtin_isgreater:
17831 return LHS > RHS;
17832 case Builtin::BI__builtin_isgreaterequal:
17833 return LHS >= RHS;
17834 case Builtin::BI__builtin_isless:
17835 return LHS < RHS;
17836 case Builtin::BI__builtin_islessequal:
17837 return LHS <= RHS;
17838 case Builtin::BI__builtin_islessgreater: {
17839 APFloat::cmpResult cmp = LHS.compare(RHS);
17840 return cmp == APFloat::cmpResult::cmpLessThan ||
17841 cmp == APFloat::cmpResult::cmpGreaterThan;
17842 }
17843 case Builtin::BI__builtin_isunordered:
17844 return LHS.compare(RHS) == APFloat::cmpResult::cmpUnordered;
17845 default:
17846 llvm_unreachable("Unexpected builtin ID: Should be a floating "
17847 "point comparison function");
17848 }
17849 }()
17850 ? 1
17851 : 0,
17852 E);
17853 }
17854
17855 case Builtin::BI__builtin_issignaling: {
17856 APFloat Val(0.0);
17857 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17858 Success(Value: Val.isSignaling() ? 1 : 0, E);
17859 }
17860
17861 case Builtin::BI__builtin_isfpclass: {
17862 APSInt MaskVal;
17863 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: MaskVal, Info))
17864 return false;
17865 unsigned Test = static_cast<llvm::FPClassTest>(MaskVal.getZExtValue());
17866 APFloat Val(0.0);
17867 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17868 Success(Value: (Val.classify() & Test) ? 1 : 0, E);
17869 }
17870
17871 case Builtin::BI__builtin_parity:
17872 case Builtin::BI__builtin_parityl:
17873 case Builtin::BI__builtin_parityll: {
17874 APSInt Val;
17875 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17876 return false;
17877
17878 return Success(Value: Val.popcount() % 2, E);
17879 }
17880
17881 case Builtin::BI__builtin_abs:
17882 case Builtin::BI__builtin_labs:
17883 case Builtin::BI__builtin_llabs: {
17884 APSInt Val;
17885 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17886 return false;
17887 if (Val == APSInt(APInt::getSignedMinValue(numBits: Val.getBitWidth()),
17888 /*IsUnsigned=*/false))
17889 return false;
17890 if (Val.isNegative())
17891 Val.negate();
17892 return Success(SI: Val, E);
17893 }
17894
17895 case Builtin::BI__builtin_popcount:
17896 case Builtin::BI__builtin_popcountl:
17897 case Builtin::BI__builtin_popcountll:
17898 case Builtin::BI__builtin_popcountg:
17899 case Builtin::BI__builtin_elementwise_popcount:
17900 case Builtin::BI__popcnt16: // Microsoft variants of popcount
17901 case Builtin::BI__popcnt:
17902 case Builtin::BI__popcnt64: {
17903 APSInt Val;
17904 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17905 APValue Vec;
17906 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17907 return false;
17908 Val = ConvertBoolVectorToInt(Val: Vec);
17909 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17910 return false;
17911 }
17912
17913 return Success(Value: Val.popcount(), E);
17914 }
17915
17916 case Builtin::BI__builtin_rotateleft8:
17917 case Builtin::BI__builtin_rotateleft16:
17918 case Builtin::BI__builtin_rotateleft32:
17919 case Builtin::BI__builtin_rotateleft64:
17920 case Builtin::BI__builtin_rotateright8:
17921 case Builtin::BI__builtin_rotateright16:
17922 case Builtin::BI__builtin_rotateright32:
17923 case Builtin::BI__builtin_rotateright64:
17924 case Builtin::BI__builtin_stdc_rotate_left:
17925 case Builtin::BI__builtin_stdc_rotate_right:
17926 case Builtin::BIstdc_rotate_left_uc:
17927 case Builtin::BIstdc_rotate_left_us:
17928 case Builtin::BIstdc_rotate_left_ui:
17929 case Builtin::BIstdc_rotate_left_ul:
17930 case Builtin::BIstdc_rotate_left_ull:
17931 case Builtin::BIstdc_rotate_right_uc:
17932 case Builtin::BIstdc_rotate_right_us:
17933 case Builtin::BIstdc_rotate_right_ui:
17934 case Builtin::BIstdc_rotate_right_ul:
17935 case Builtin::BIstdc_rotate_right_ull:
17936 case Builtin::BI_rotl8: // Microsoft variants of rotate left
17937 case Builtin::BI_rotl16:
17938 case Builtin::BI_rotl:
17939 case Builtin::BI_lrotl:
17940 case Builtin::BI_rotl64:
17941 case Builtin::BI_rotr8: // Microsoft variants of rotate right
17942 case Builtin::BI_rotr16:
17943 case Builtin::BI_rotr:
17944 case Builtin::BI_lrotr:
17945 case Builtin::BI_rotr64: {
17946 APSInt Value, Amount;
17947 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Value, Info) ||
17948 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amount, Info))
17949 return false;
17950
17951 Amount = NormalizeRotateAmount(Value, Amount);
17952
17953 switch (BuiltinOp) {
17954 case Builtin::BI__builtin_rotateright8:
17955 case Builtin::BI__builtin_rotateright16:
17956 case Builtin::BI__builtin_rotateright32:
17957 case Builtin::BI__builtin_rotateright64:
17958 case Builtin::BI__builtin_stdc_rotate_right:
17959 case Builtin::BIstdc_rotate_right_uc:
17960 case Builtin::BIstdc_rotate_right_us:
17961 case Builtin::BIstdc_rotate_right_ui:
17962 case Builtin::BIstdc_rotate_right_ul:
17963 case Builtin::BIstdc_rotate_right_ull:
17964 case Builtin::BI_rotr8:
17965 case Builtin::BI_rotr16:
17966 case Builtin::BI_rotr:
17967 case Builtin::BI_lrotr:
17968 case Builtin::BI_rotr64:
17969 return Success(
17970 SI: APSInt(Value.rotr(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17971 default:
17972 return Success(
17973 SI: APSInt(Value.rotl(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17974 }
17975 }
17976
17977 case Builtin::BIstdc_leading_zeros_uc:
17978 case Builtin::BIstdc_leading_zeros_us:
17979 case Builtin::BIstdc_leading_zeros_ui:
17980 case Builtin::BIstdc_leading_zeros_ul:
17981 case Builtin::BIstdc_leading_zeros_ull:
17982 case Builtin::BIstdc_leading_ones_uc:
17983 case Builtin::BIstdc_leading_ones_us:
17984 case Builtin::BIstdc_leading_ones_ui:
17985 case Builtin::BIstdc_leading_ones_ul:
17986 case Builtin::BIstdc_leading_ones_ull:
17987 case Builtin::BIstdc_trailing_zeros_uc:
17988 case Builtin::BIstdc_trailing_zeros_us:
17989 case Builtin::BIstdc_trailing_zeros_ui:
17990 case Builtin::BIstdc_trailing_zeros_ul:
17991 case Builtin::BIstdc_trailing_zeros_ull:
17992 case Builtin::BIstdc_trailing_ones_uc:
17993 case Builtin::BIstdc_trailing_ones_us:
17994 case Builtin::BIstdc_trailing_ones_ui:
17995 case Builtin::BIstdc_trailing_ones_ul:
17996 case Builtin::BIstdc_trailing_ones_ull:
17997 case Builtin::BIstdc_first_leading_zero_uc:
17998 case Builtin::BIstdc_first_leading_zero_us:
17999 case Builtin::BIstdc_first_leading_zero_ui:
18000 case Builtin::BIstdc_first_leading_zero_ul:
18001 case Builtin::BIstdc_first_leading_zero_ull:
18002 case Builtin::BIstdc_first_leading_one_uc:
18003 case Builtin::BIstdc_first_leading_one_us:
18004 case Builtin::BIstdc_first_leading_one_ui:
18005 case Builtin::BIstdc_first_leading_one_ul:
18006 case Builtin::BIstdc_first_leading_one_ull:
18007 case Builtin::BIstdc_first_trailing_zero_uc:
18008 case Builtin::BIstdc_first_trailing_zero_us:
18009 case Builtin::BIstdc_first_trailing_zero_ui:
18010 case Builtin::BIstdc_first_trailing_zero_ul:
18011 case Builtin::BIstdc_first_trailing_zero_ull:
18012 case Builtin::BIstdc_first_trailing_one_uc:
18013 case Builtin::BIstdc_first_trailing_one_us:
18014 case Builtin::BIstdc_first_trailing_one_ui:
18015 case Builtin::BIstdc_first_trailing_one_ul:
18016 case Builtin::BIstdc_first_trailing_one_ull:
18017 case Builtin::BIstdc_count_zeros_uc:
18018 case Builtin::BIstdc_count_zeros_us:
18019 case Builtin::BIstdc_count_zeros_ui:
18020 case Builtin::BIstdc_count_zeros_ul:
18021 case Builtin::BIstdc_count_zeros_ull:
18022 case Builtin::BIstdc_count_ones_uc:
18023 case Builtin::BIstdc_count_ones_us:
18024 case Builtin::BIstdc_count_ones_ui:
18025 case Builtin::BIstdc_count_ones_ul:
18026 case Builtin::BIstdc_count_ones_ull:
18027 case Builtin::BIstdc_has_single_bit_uc:
18028 case Builtin::BIstdc_has_single_bit_us:
18029 case Builtin::BIstdc_has_single_bit_ui:
18030 case Builtin::BIstdc_has_single_bit_ul:
18031 case Builtin::BIstdc_has_single_bit_ull:
18032 case Builtin::BIstdc_bit_width_uc:
18033 case Builtin::BIstdc_bit_width_us:
18034 case Builtin::BIstdc_bit_width_ui:
18035 case Builtin::BIstdc_bit_width_ul:
18036 case Builtin::BIstdc_bit_width_ull:
18037 case Builtin::BIstdc_bit_floor_uc:
18038 case Builtin::BIstdc_bit_floor_us:
18039 case Builtin::BIstdc_bit_floor_ui:
18040 case Builtin::BIstdc_bit_floor_ul:
18041 case Builtin::BIstdc_bit_floor_ull:
18042 case Builtin::BIstdc_bit_ceil_uc:
18043 case Builtin::BIstdc_bit_ceil_us:
18044 case Builtin::BIstdc_bit_ceil_ui:
18045 case Builtin::BIstdc_bit_ceil_ul:
18046 case Builtin::BIstdc_bit_ceil_ull:
18047 case Builtin::BI__builtin_stdc_leading_zeros:
18048 case Builtin::BI__builtin_stdc_leading_ones:
18049 case Builtin::BI__builtin_stdc_trailing_zeros:
18050 case Builtin::BI__builtin_stdc_trailing_ones:
18051 case Builtin::BI__builtin_stdc_first_leading_zero:
18052 case Builtin::BI__builtin_stdc_first_leading_one:
18053 case Builtin::BI__builtin_stdc_first_trailing_zero:
18054 case Builtin::BI__builtin_stdc_first_trailing_one:
18055 case Builtin::BI__builtin_stdc_count_zeros:
18056 case Builtin::BI__builtin_stdc_count_ones:
18057 case Builtin::BI__builtin_stdc_has_single_bit:
18058 case Builtin::BI__builtin_stdc_bit_width:
18059 case Builtin::BI__builtin_stdc_bit_floor:
18060 case Builtin::BI__builtin_stdc_bit_ceil: {
18061 APSInt Val;
18062 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18063 return false;
18064
18065 unsigned BitWidth = Val.getBitWidth();
18066 const unsigned ResBitWidth = Info.Ctx.getIntWidth(T: E->getType());
18067
18068 switch (BuiltinOp) {
18069 case Builtin::BIstdc_leading_zeros_uc:
18070 case Builtin::BIstdc_leading_zeros_us:
18071 case Builtin::BIstdc_leading_zeros_ui:
18072 case Builtin::BIstdc_leading_zeros_ul:
18073 case Builtin::BIstdc_leading_zeros_ull:
18074 case Builtin::BI__builtin_stdc_leading_zeros:
18075 return Success(I: APInt(ResBitWidth, Val.countl_zero()), E);
18076 case Builtin::BIstdc_leading_ones_uc:
18077 case Builtin::BIstdc_leading_ones_us:
18078 case Builtin::BIstdc_leading_ones_ui:
18079 case Builtin::BIstdc_leading_ones_ul:
18080 case Builtin::BIstdc_leading_ones_ull:
18081 case Builtin::BI__builtin_stdc_leading_ones:
18082 return Success(I: APInt(ResBitWidth, Val.countl_one()), E);
18083 case Builtin::BIstdc_trailing_zeros_uc:
18084 case Builtin::BIstdc_trailing_zeros_us:
18085 case Builtin::BIstdc_trailing_zeros_ui:
18086 case Builtin::BIstdc_trailing_zeros_ul:
18087 case Builtin::BIstdc_trailing_zeros_ull:
18088 case Builtin::BI__builtin_stdc_trailing_zeros:
18089 return Success(I: APInt(ResBitWidth, Val.countr_zero()), E);
18090 case Builtin::BIstdc_trailing_ones_uc:
18091 case Builtin::BIstdc_trailing_ones_us:
18092 case Builtin::BIstdc_trailing_ones_ui:
18093 case Builtin::BIstdc_trailing_ones_ul:
18094 case Builtin::BIstdc_trailing_ones_ull:
18095 case Builtin::BI__builtin_stdc_trailing_ones:
18096 return Success(I: APInt(ResBitWidth, Val.countr_one()), E);
18097 case Builtin::BIstdc_first_leading_zero_uc:
18098 case Builtin::BIstdc_first_leading_zero_us:
18099 case Builtin::BIstdc_first_leading_zero_ui:
18100 case Builtin::BIstdc_first_leading_zero_ul:
18101 case Builtin::BIstdc_first_leading_zero_ull:
18102 case Builtin::BI__builtin_stdc_first_leading_zero:
18103 return Success(
18104 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countl_one() + 1), E);
18105 case Builtin::BIstdc_first_leading_one_uc:
18106 case Builtin::BIstdc_first_leading_one_us:
18107 case Builtin::BIstdc_first_leading_one_ui:
18108 case Builtin::BIstdc_first_leading_one_ul:
18109 case Builtin::BIstdc_first_leading_one_ull:
18110 case Builtin::BI__builtin_stdc_first_leading_one:
18111 return Success(
18112 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countl_zero() + 1), E);
18113 case Builtin::BIstdc_first_trailing_zero_uc:
18114 case Builtin::BIstdc_first_trailing_zero_us:
18115 case Builtin::BIstdc_first_trailing_zero_ui:
18116 case Builtin::BIstdc_first_trailing_zero_ul:
18117 case Builtin::BIstdc_first_trailing_zero_ull:
18118 case Builtin::BI__builtin_stdc_first_trailing_zero:
18119 return Success(
18120 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countr_one() + 1), E);
18121 case Builtin::BIstdc_first_trailing_one_uc:
18122 case Builtin::BIstdc_first_trailing_one_us:
18123 case Builtin::BIstdc_first_trailing_one_ui:
18124 case Builtin::BIstdc_first_trailing_one_ul:
18125 case Builtin::BIstdc_first_trailing_one_ull:
18126 case Builtin::BI__builtin_stdc_first_trailing_one:
18127 return Success(
18128 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countr_zero() + 1), E);
18129 case Builtin::BIstdc_count_zeros_uc:
18130 case Builtin::BIstdc_count_zeros_us:
18131 case Builtin::BIstdc_count_zeros_ui:
18132 case Builtin::BIstdc_count_zeros_ul:
18133 case Builtin::BIstdc_count_zeros_ull:
18134 case Builtin::BI__builtin_stdc_count_zeros: {
18135 APInt Cnt(ResBitWidth, BitWidth - Val.popcount());
18136 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
18137 }
18138 case Builtin::BIstdc_count_ones_uc:
18139 case Builtin::BIstdc_count_ones_us:
18140 case Builtin::BIstdc_count_ones_ui:
18141 case Builtin::BIstdc_count_ones_ul:
18142 case Builtin::BIstdc_count_ones_ull:
18143 case Builtin::BI__builtin_stdc_count_ones: {
18144 APInt Cnt(ResBitWidth, Val.popcount());
18145 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
18146 }
18147 case Builtin::BIstdc_has_single_bit_uc:
18148 case Builtin::BIstdc_has_single_bit_us:
18149 case Builtin::BIstdc_has_single_bit_ui:
18150 case Builtin::BIstdc_has_single_bit_ul:
18151 case Builtin::BIstdc_has_single_bit_ull:
18152 case Builtin::BI__builtin_stdc_has_single_bit: {
18153 APInt Res(ResBitWidth, Val.popcount() == 1 ? 1 : 0);
18154 return Success(SI: APSInt(Res, /*IsUnsigned*/ true), E);
18155 }
18156 case Builtin::BIstdc_bit_width_uc:
18157 case Builtin::BIstdc_bit_width_us:
18158 case Builtin::BIstdc_bit_width_ui:
18159 case Builtin::BIstdc_bit_width_ul:
18160 case Builtin::BIstdc_bit_width_ull:
18161 case Builtin::BI__builtin_stdc_bit_width:
18162 return Success(I: APInt(ResBitWidth, BitWidth - Val.countl_zero()), E);
18163 case Builtin::BIstdc_bit_floor_uc:
18164 case Builtin::BIstdc_bit_floor_us:
18165 case Builtin::BIstdc_bit_floor_ui:
18166 case Builtin::BIstdc_bit_floor_ul:
18167 case Builtin::BIstdc_bit_floor_ull:
18168 case Builtin::BI__builtin_stdc_bit_floor: {
18169 if (Val.isZero())
18170 return Success(I: APInt(BitWidth, 0), E);
18171 unsigned Exp = BitWidth - Val.countl_zero() - 1;
18172 return Success(
18173 SI: APSInt(APInt::getOneBitSet(numBits: BitWidth, BitNo: Exp), /*IsUnsigned*/ true), E);
18174 }
18175 case Builtin::BIstdc_bit_ceil_uc:
18176 case Builtin::BIstdc_bit_ceil_us:
18177 case Builtin::BIstdc_bit_ceil_ui:
18178 case Builtin::BIstdc_bit_ceil_ul:
18179 case Builtin::BIstdc_bit_ceil_ull:
18180 case Builtin::BI__builtin_stdc_bit_ceil: {
18181 if (Val.ule(RHS: 1))
18182 return Success(SI: APSInt(APInt(BitWidth, 1), /*IsUnsigned*/ true), E);
18183 APInt ValMinusOne = Val - 1;
18184 unsigned LZ = ValMinusOne.countl_zero();
18185 if (LZ == 0)
18186 return Success(SI: APSInt(APInt(BitWidth, 0), /*IsUnsigned*/ true),
18187 E); // overflows; wrap to 0
18188 APInt Result = APInt::getOneBitSet(numBits: BitWidth, BitNo: BitWidth - LZ);
18189 return Success(SI: APSInt(Result, /*IsUnsigned*/ true), E);
18190 }
18191 default:
18192 llvm_unreachable("Unknown stdc builtin");
18193 }
18194 }
18195
18196 case Builtin::BI__builtin_elementwise_add_sat: {
18197 APSInt LHS, RHS;
18198 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18199 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
18200 return false;
18201
18202 APInt Result = LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
18203 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
18204 }
18205 case Builtin::BI__builtin_elementwise_sub_sat: {
18206 APSInt LHS, RHS;
18207 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18208 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
18209 return false;
18210
18211 APInt Result = LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
18212 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
18213 }
18214 case Builtin::BI__builtin_elementwise_max: {
18215 APSInt LHS, RHS;
18216 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18217 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
18218 return false;
18219
18220 APInt Result = std::max(a: LHS, b: RHS);
18221 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
18222 }
18223 case Builtin::BI__builtin_elementwise_min: {
18224 APSInt LHS, RHS;
18225 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18226 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
18227 return false;
18228
18229 APInt Result = std::min(a: LHS, b: RHS);
18230 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
18231 }
18232 case Builtin::BI__builtin_elementwise_clmul: {
18233 APSInt LHS, RHS;
18234 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18235 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
18236 return false;
18237
18238 APInt Result = llvm::APIntOps::clmul(LHS, RHS);
18239 return Success(SI: APSInt(Result, LHS.isUnsigned()), E);
18240 }
18241 case Builtin::BI__builtin_elementwise_fshl:
18242 case Builtin::BI__builtin_elementwise_fshr: {
18243 APSInt Hi, Lo, Shift;
18244 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Hi, Info) ||
18245 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Lo, Info) ||
18246 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Shift, Info))
18247 return false;
18248
18249 switch (BuiltinOp) {
18250 case Builtin::BI__builtin_elementwise_fshl: {
18251 APSInt Result(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned());
18252 return Success(SI: Result, E);
18253 }
18254 case Builtin::BI__builtin_elementwise_fshr: {
18255 APSInt Result(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned());
18256 return Success(SI: Result, E);
18257 }
18258 }
18259 llvm_unreachable("Fully covered switch above");
18260 }
18261 case Builtin::BIstrlen:
18262 case Builtin::BIwcslen:
18263 // A call to strlen is not a constant expression.
18264 if (Info.getLangOpts().CPlusPlus11)
18265 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
18266 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
18267 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
18268 else
18269 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
18270 [[fallthrough]];
18271 case Builtin::BI__builtin_strlen:
18272 case Builtin::BI__builtin_wcslen: {
18273 // As an extension, we support __builtin_strlen() as a constant expression,
18274 // and support folding strlen() to a constant.
18275 if (std::optional<uint64_t> StrLen =
18276 EvaluateBuiltinStrLen(E: E->getArg(Arg: 0), Info))
18277 return Success(Value: *StrLen, E);
18278 return false;
18279 }
18280
18281 case Builtin::BIstrcmp:
18282 case Builtin::BIwcscmp:
18283 case Builtin::BIstrncmp:
18284 case Builtin::BIwcsncmp:
18285 case Builtin::BImemcmp:
18286 case Builtin::BIbcmp:
18287 case Builtin::BIwmemcmp:
18288 // A call to strlen is not a constant expression.
18289 if (Info.getLangOpts().CPlusPlus11)
18290 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
18291 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
18292 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
18293 else
18294 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
18295 [[fallthrough]];
18296 case Builtin::BI__builtin_strcmp:
18297 case Builtin::BI__builtin_wcscmp:
18298 case Builtin::BI__builtin_strncmp:
18299 case Builtin::BI__builtin_wcsncmp:
18300 case Builtin::BI__builtin_memcmp:
18301 case Builtin::BI__builtin_bcmp:
18302 case Builtin::BI__builtin_wmemcmp: {
18303 LValue String1, String2;
18304 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: String1, Info) ||
18305 !EvaluatePointer(E: E->getArg(Arg: 1), Result&: String2, Info))
18306 return false;
18307
18308 uint64_t MaxLength = uint64_t(-1);
18309 if (BuiltinOp != Builtin::BIstrcmp &&
18310 BuiltinOp != Builtin::BIwcscmp &&
18311 BuiltinOp != Builtin::BI__builtin_strcmp &&
18312 BuiltinOp != Builtin::BI__builtin_wcscmp) {
18313 APSInt N;
18314 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
18315 return false;
18316 MaxLength = N.getZExtValue();
18317 }
18318
18319 // Empty substrings compare equal by definition.
18320 if (MaxLength == 0u)
18321 return Success(Value: 0, E);
18322
18323 if (!String1.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
18324 !String2.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
18325 String1.Designator.Invalid || String2.Designator.Invalid)
18326 return false;
18327
18328 QualType CharTy1 = String1.Designator.getType(Ctx&: Info.Ctx);
18329 QualType CharTy2 = String2.Designator.getType(Ctx&: Info.Ctx);
18330
18331 bool IsRawByte = BuiltinOp == Builtin::BImemcmp ||
18332 BuiltinOp == Builtin::BIbcmp ||
18333 BuiltinOp == Builtin::BI__builtin_memcmp ||
18334 BuiltinOp == Builtin::BI__builtin_bcmp;
18335
18336 assert(IsRawByte ||
18337 (Info.Ctx.hasSameUnqualifiedType(
18338 CharTy1, E->getArg(0)->getType()->getPointeeType()) &&
18339 Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2)));
18340
18341 // For memcmp, allow comparing any arrays of '[[un]signed] char' or
18342 // 'char8_t', but no other types.
18343 if (IsRawByte &&
18344 !(isOneByteCharacterType(T: CharTy1) && isOneByteCharacterType(T: CharTy2))) {
18345 // FIXME: Consider using our bit_cast implementation to support this.
18346 Info.FFDiag(E, DiagId: diag::note_constexpr_memcmp_unsupported)
18347 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy1
18348 << CharTy2;
18349 return false;
18350 }
18351
18352 const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) {
18353 return handleLValueToRValueConversion(Info, Conv: E, Type: CharTy1, LVal: String1, RVal&: Char1) &&
18354 handleLValueToRValueConversion(Info, Conv: E, Type: CharTy2, LVal: String2, RVal&: Char2) &&
18355 Char1.isInt() && Char2.isInt();
18356 };
18357 const auto &AdvanceElems = [&] {
18358 return HandleLValueArrayAdjustment(Info, E, LVal&: String1, EltTy: CharTy1, Adjustment: 1) &&
18359 HandleLValueArrayAdjustment(Info, E, LVal&: String2, EltTy: CharTy2, Adjustment: 1);
18360 };
18361
18362 bool StopAtNull =
18363 (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp &&
18364 BuiltinOp != Builtin::BIwmemcmp &&
18365 BuiltinOp != Builtin::BI__builtin_memcmp &&
18366 BuiltinOp != Builtin::BI__builtin_bcmp &&
18367 BuiltinOp != Builtin::BI__builtin_wmemcmp);
18368 bool IsWide = BuiltinOp == Builtin::BIwcscmp ||
18369 BuiltinOp == Builtin::BIwcsncmp ||
18370 BuiltinOp == Builtin::BIwmemcmp ||
18371 BuiltinOp == Builtin::BI__builtin_wcscmp ||
18372 BuiltinOp == Builtin::BI__builtin_wcsncmp ||
18373 BuiltinOp == Builtin::BI__builtin_wmemcmp;
18374
18375 for (; MaxLength; --MaxLength) {
18376 APValue Char1, Char2;
18377 if (!ReadCurElems(Char1, Char2))
18378 return false;
18379 if (Char1.getInt().ne(RHS: Char2.getInt())) {
18380 if (IsWide) // wmemcmp compares with wchar_t signedness.
18381 return Success(Value: Char1.getInt() < Char2.getInt() ? -1 : 1, E);
18382 // memcmp always compares unsigned chars.
18383 return Success(Value: Char1.getInt().ult(RHS: Char2.getInt()) ? -1 : 1, E);
18384 }
18385 if (StopAtNull && !Char1.getInt())
18386 return Success(Value: 0, E);
18387 assert(!(StopAtNull && !Char2.getInt()));
18388 if (!AdvanceElems())
18389 return false;
18390 }
18391 // We hit the strncmp / memcmp limit.
18392 return Success(Value: 0, E);
18393 }
18394
18395 case Builtin::BI__atomic_always_lock_free:
18396 case Builtin::BI__atomic_is_lock_free:
18397 case Builtin::BI__c11_atomic_is_lock_free: {
18398 APSInt SizeVal;
18399 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SizeVal, Info))
18400 return false;
18401
18402 // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
18403 // of two less than or equal to the maximum inline atomic width, we know it
18404 // is lock-free. If the size isn't a power of two, or greater than the
18405 // maximum alignment where we promote atomics, we know it is not lock-free
18406 // (at least not in the sense of atomic_is_lock_free). Otherwise,
18407 // the answer can only be determined at runtime; for example, 16-byte
18408 // atomics have lock-free implementations on some, but not all,
18409 // x86-64 processors.
18410
18411 // Check power-of-two.
18412 CharUnits Size = CharUnits::fromQuantity(Quantity: SizeVal.getZExtValue());
18413 if (Size.isPowerOfTwo()) {
18414 // Check against inlining width.
18415 unsigned InlineWidthBits =
18416 Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
18417 if (Size <= Info.Ctx.toCharUnitsFromBits(BitSize: InlineWidthBits)) {
18418 if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||
18419 Size == CharUnits::One())
18420 return Success(Value: 1, E);
18421
18422 // If the pointer argument can be evaluated to a compile-time constant
18423 // integer (or nullptr), check if that value is appropriately aligned.
18424 const Expr *PtrArg = E->getArg(Arg: 1);
18425 Expr::EvalResult ExprResult;
18426 APSInt IntResult;
18427 if (PtrArg->EvaluateAsRValue(Result&: ExprResult, Ctx: Info.Ctx) &&
18428 ExprResult.Val.toIntegralConstant(Result&: IntResult, SrcTy: PtrArg->getType(),
18429 Ctx: Info.Ctx) &&
18430 IntResult.isAligned(A: Size.getAsAlign()))
18431 return Success(Value: 1, E);
18432
18433 // Otherwise, check if the type's alignment against Size.
18434 if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: PtrArg)) {
18435 // Drop the potential implicit-cast to 'const volatile void*', getting
18436 // the underlying type.
18437 if (ICE->getCastKind() == CK_BitCast)
18438 PtrArg = ICE->getSubExpr();
18439 }
18440
18441 if (auto PtrTy = PtrArg->getType()->getAs<PointerType>()) {
18442 QualType PointeeType = PtrTy->getPointeeType();
18443 if (!PointeeType->isIncompleteType() &&
18444 Info.Ctx.getTypeAlignInChars(T: PointeeType) >= Size) {
18445 // OK, we will inline operations on this object.
18446 return Success(Value: 1, E);
18447 }
18448 }
18449 }
18450 }
18451
18452 return BuiltinOp == Builtin::BI__atomic_always_lock_free ?
18453 Success(Value: 0, E) : Error(E);
18454 }
18455 case Builtin::BI__builtin_addcb:
18456 case Builtin::BI__builtin_addcs:
18457 case Builtin::BI__builtin_addc:
18458 case Builtin::BI__builtin_addcl:
18459 case Builtin::BI__builtin_addcll:
18460 case Builtin::BI__builtin_subcb:
18461 case Builtin::BI__builtin_subcs:
18462 case Builtin::BI__builtin_subc:
18463 case Builtin::BI__builtin_subcl:
18464 case Builtin::BI__builtin_subcll: {
18465 LValue CarryOutLValue;
18466 APSInt LHS, RHS, CarryIn, CarryOut, Result;
18467 QualType ResultType = E->getArg(Arg: 0)->getType();
18468 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18469 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18470 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: CarryIn, Info) ||
18471 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: CarryOutLValue, Info))
18472 return false;
18473 // Copy the number of bits and sign.
18474 Result = LHS;
18475 CarryOut = LHS;
18476
18477 bool FirstOverflowed = false;
18478 bool SecondOverflowed = false;
18479 switch (BuiltinOp) {
18480 default:
18481 llvm_unreachable("Invalid value for BuiltinOp");
18482 case Builtin::BI__builtin_addcb:
18483 case Builtin::BI__builtin_addcs:
18484 case Builtin::BI__builtin_addc:
18485 case Builtin::BI__builtin_addcl:
18486 case Builtin::BI__builtin_addcll:
18487 Result =
18488 LHS.uadd_ov(RHS, Overflow&: FirstOverflowed).uadd_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
18489 break;
18490 case Builtin::BI__builtin_subcb:
18491 case Builtin::BI__builtin_subcs:
18492 case Builtin::BI__builtin_subc:
18493 case Builtin::BI__builtin_subcl:
18494 case Builtin::BI__builtin_subcll:
18495 Result =
18496 LHS.usub_ov(RHS, Overflow&: FirstOverflowed).usub_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
18497 break;
18498 }
18499
18500 // It is possible for both overflows to happen but CGBuiltin uses an OR so
18501 // this is consistent.
18502 CarryOut = (uint64_t)(FirstOverflowed | SecondOverflowed);
18503 APValue APV{CarryOut};
18504 if (!handleAssignment(Info, E, LVal: CarryOutLValue, LValType: ResultType, Val&: APV))
18505 return false;
18506 return Success(SI: Result, E);
18507 }
18508 case Builtin::BI__builtin_add_overflow:
18509 case Builtin::BI__builtin_sub_overflow:
18510 case Builtin::BI__builtin_mul_overflow:
18511 case Builtin::BI__builtin_sadd_overflow:
18512 case Builtin::BI__builtin_uadd_overflow:
18513 case Builtin::BI__builtin_uaddl_overflow:
18514 case Builtin::BI__builtin_uaddll_overflow:
18515 case Builtin::BI__builtin_usub_overflow:
18516 case Builtin::BI__builtin_usubl_overflow:
18517 case Builtin::BI__builtin_usubll_overflow:
18518 case Builtin::BI__builtin_umul_overflow:
18519 case Builtin::BI__builtin_umull_overflow:
18520 case Builtin::BI__builtin_umulll_overflow:
18521 case Builtin::BI__builtin_saddl_overflow:
18522 case Builtin::BI__builtin_saddll_overflow:
18523 case Builtin::BI__builtin_ssub_overflow:
18524 case Builtin::BI__builtin_ssubl_overflow:
18525 case Builtin::BI__builtin_ssubll_overflow:
18526 case Builtin::BI__builtin_smul_overflow:
18527 case Builtin::BI__builtin_smull_overflow:
18528 case Builtin::BI__builtin_smulll_overflow: {
18529 LValue ResultLValue;
18530 APSInt LHS, RHS;
18531
18532 QualType ResultType = E->getArg(Arg: 2)->getType()->getPointeeType();
18533 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18534 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18535 !EvaluatePointer(E: E->getArg(Arg: 2), Result&: ResultLValue, Info))
18536 return false;
18537
18538 APSInt Result;
18539 bool DidOverflow = false;
18540
18541 // If the types don't have to match, enlarge all 3 to the largest of them.
18542 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
18543 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
18544 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
18545 bool IsSigned = LHS.isSigned() || RHS.isSigned() ||
18546 ResultType->isSignedIntegerOrEnumerationType();
18547 bool AllSigned = LHS.isSigned() && RHS.isSigned() &&
18548 ResultType->isSignedIntegerOrEnumerationType();
18549 uint64_t LHSSize = LHS.getBitWidth();
18550 uint64_t RHSSize = RHS.getBitWidth();
18551 uint64_t ResultSize = Info.Ctx.getIntWidth(T: ResultType);
18552 uint64_t MaxBits = std::max(a: std::max(a: LHSSize, b: RHSSize), b: ResultSize);
18553
18554 // Add an additional bit if the signedness isn't uniformly agreed to. We
18555 // could do this ONLY if there is a signed and an unsigned that both have
18556 // MaxBits, but the code to check that is pretty nasty. The issue will be
18557 // caught in the shrink-to-result later anyway.
18558 if (IsSigned && !AllSigned)
18559 ++MaxBits;
18560
18561 LHS = APSInt(LHS.extOrTrunc(width: MaxBits), !IsSigned);
18562 RHS = APSInt(RHS.extOrTrunc(width: MaxBits), !IsSigned);
18563 Result = APSInt(MaxBits, !IsSigned);
18564 }
18565
18566 // Find largest int.
18567 switch (BuiltinOp) {
18568 default:
18569 llvm_unreachable("Invalid value for BuiltinOp");
18570 case Builtin::BI__builtin_add_overflow:
18571 case Builtin::BI__builtin_sadd_overflow:
18572 case Builtin::BI__builtin_saddl_overflow:
18573 case Builtin::BI__builtin_saddll_overflow:
18574 case Builtin::BI__builtin_uadd_overflow:
18575 case Builtin::BI__builtin_uaddl_overflow:
18576 case Builtin::BI__builtin_uaddll_overflow:
18577 Result = LHS.isSigned() ? LHS.sadd_ov(RHS, Overflow&: DidOverflow)
18578 : LHS.uadd_ov(RHS, Overflow&: DidOverflow);
18579 break;
18580 case Builtin::BI__builtin_sub_overflow:
18581 case Builtin::BI__builtin_ssub_overflow:
18582 case Builtin::BI__builtin_ssubl_overflow:
18583 case Builtin::BI__builtin_ssubll_overflow:
18584 case Builtin::BI__builtin_usub_overflow:
18585 case Builtin::BI__builtin_usubl_overflow:
18586 case Builtin::BI__builtin_usubll_overflow:
18587 Result = LHS.isSigned() ? LHS.ssub_ov(RHS, Overflow&: DidOverflow)
18588 : LHS.usub_ov(RHS, Overflow&: DidOverflow);
18589 break;
18590 case Builtin::BI__builtin_mul_overflow:
18591 case Builtin::BI__builtin_smul_overflow:
18592 case Builtin::BI__builtin_smull_overflow:
18593 case Builtin::BI__builtin_smulll_overflow:
18594 case Builtin::BI__builtin_umul_overflow:
18595 case Builtin::BI__builtin_umull_overflow:
18596 case Builtin::BI__builtin_umulll_overflow:
18597 Result = LHS.isSigned() ? LHS.smul_ov(RHS, Overflow&: DidOverflow)
18598 : LHS.umul_ov(RHS, Overflow&: DidOverflow);
18599 break;
18600 }
18601
18602 // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead,
18603 // since it will give us the behavior of a TruncOrSelf in the case where
18604 // its parameter <= its size. We previously set Result to be at least the
18605 // integer width of the result, so getIntWidth(ResultType) <=
18606 // Result.BitWidth will work exactly like TruncOrSelf.
18607 APSInt Temp = Result.extOrTrunc(width: Info.Ctx.getIntWidth(T: ResultType));
18608 Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType());
18609
18610 // In the case where multiple sizes are allowed, truncate and see if
18611 // the values are the same.
18612 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
18613 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
18614 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
18615 if (!APSInt::isSameValue(I1: Temp, I2: Result))
18616 DidOverflow = true;
18617 }
18618 Result = Temp;
18619
18620 APValue APV{Result};
18621 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
18622 return false;
18623 return Success(Value: DidOverflow, E);
18624 }
18625
18626 case Builtin::BI__builtin_reduce_add:
18627 case Builtin::BI__builtin_reduce_mul:
18628 case Builtin::BI__builtin_reduce_and:
18629 case Builtin::BI__builtin_reduce_or:
18630 case Builtin::BI__builtin_reduce_xor:
18631 case Builtin::BI__builtin_reduce_min:
18632 case Builtin::BI__builtin_reduce_max: {
18633 APValue Source;
18634 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
18635 return false;
18636
18637 unsigned SourceLen = Source.getVectorLength();
18638 APSInt Reduced = Source.getVectorElt(I: 0).getInt();
18639 for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) {
18640 switch (BuiltinOp) {
18641 default:
18642 return false;
18643 case Builtin::BI__builtin_reduce_add: {
18644 if (!CheckedIntArithmetic(
18645 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
18646 BitWidth: Reduced.getBitWidth() + 1, Op: std::plus<APSInt>(), Result&: Reduced))
18647 return false;
18648 break;
18649 }
18650 case Builtin::BI__builtin_reduce_mul: {
18651 if (!CheckedIntArithmetic(
18652 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
18653 BitWidth: Reduced.getBitWidth() * 2, Op: std::multiplies<APSInt>(), Result&: Reduced))
18654 return false;
18655 break;
18656 }
18657 case Builtin::BI__builtin_reduce_and: {
18658 Reduced &= Source.getVectorElt(I: EltNum).getInt();
18659 break;
18660 }
18661 case Builtin::BI__builtin_reduce_or: {
18662 Reduced |= Source.getVectorElt(I: EltNum).getInt();
18663 break;
18664 }
18665 case Builtin::BI__builtin_reduce_xor: {
18666 Reduced ^= Source.getVectorElt(I: EltNum).getInt();
18667 break;
18668 }
18669 case Builtin::BI__builtin_reduce_min: {
18670 Reduced = std::min(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
18671 break;
18672 }
18673 case Builtin::BI__builtin_reduce_max: {
18674 Reduced = std::max(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
18675 break;
18676 }
18677 }
18678 }
18679
18680 return Success(SI: Reduced, E);
18681 }
18682
18683 case clang::X86::BI__builtin_ia32_addcarryx_u32:
18684 case clang::X86::BI__builtin_ia32_addcarryx_u64:
18685 case clang::X86::BI__builtin_ia32_subborrow_u32:
18686 case clang::X86::BI__builtin_ia32_subborrow_u64: {
18687 LValue ResultLValue;
18688 APSInt CarryIn, LHS, RHS;
18689 QualType ResultType = E->getArg(Arg: 3)->getType()->getPointeeType();
18690 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CarryIn, Info) ||
18691 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: LHS, Info) ||
18692 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: RHS, Info) ||
18693 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: ResultLValue, Info))
18694 return false;
18695
18696 bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||
18697 BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;
18698
18699 unsigned BitWidth = LHS.getBitWidth();
18700 unsigned CarryInBit = CarryIn.ugt(RHS: 0) ? 1 : 0;
18701 APInt ExResult =
18702 IsAdd
18703 ? (LHS.zext(width: BitWidth + 1) + (RHS.zext(width: BitWidth + 1) + CarryInBit))
18704 : (LHS.zext(width: BitWidth + 1) - (RHS.zext(width: BitWidth + 1) + CarryInBit));
18705
18706 APInt Result = ExResult.extractBits(numBits: BitWidth, bitPosition: 0);
18707 uint64_t CarryOut = ExResult.extractBitsAsZExtValue(numBits: 1, bitPosition: BitWidth);
18708
18709 APValue APV{APSInt(Result, /*isUnsigned=*/true)};
18710 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
18711 return false;
18712 return Success(Value: CarryOut, E);
18713 }
18714
18715 case clang::X86::BI__builtin_ia32_movmskps:
18716 case clang::X86::BI__builtin_ia32_movmskpd:
18717 case clang::X86::BI__builtin_ia32_pmovmskb128:
18718 case clang::X86::BI__builtin_ia32_pmovmskb256:
18719 case clang::X86::BI__builtin_ia32_movmskps256:
18720 case clang::X86::BI__builtin_ia32_movmskpd256: {
18721 APValue Source;
18722 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
18723 return false;
18724 unsigned SourceLen = Source.getVectorLength();
18725 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
18726 QualType ElemQT = VT->getElementType();
18727 unsigned ResultLen = Info.Ctx.getTypeSize(
18728 T: E->getCallReturnType(Ctx: Info.Ctx)); // Always 32-bit integer.
18729 APInt Result(ResultLen, 0);
18730
18731 for (unsigned I = 0; I != SourceLen; ++I) {
18732 APInt Elem;
18733 if (ElemQT->isIntegerType()) {
18734 Elem = Source.getVectorElt(I).getInt();
18735 } else if (ElemQT->isRealFloatingType()) {
18736 Elem = Source.getVectorElt(I).getFloat().bitcastToAPInt();
18737 } else {
18738 return false;
18739 }
18740 Result.setBitVal(BitPosition: I, BitValue: Elem.isNegative());
18741 }
18742 return Success(I: Result, E);
18743 }
18744
18745 case clang::X86::BI__builtin_ia32_bextr_u32:
18746 case clang::X86::BI__builtin_ia32_bextr_u64:
18747 case clang::X86::BI__builtin_ia32_bextri_u32:
18748 case clang::X86::BI__builtin_ia32_bextri_u64: {
18749 APSInt Val, Idx;
18750 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18751 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
18752 return false;
18753
18754 unsigned BitWidth = Val.getBitWidth();
18755 uint64_t Shift = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
18756 uint64_t Length = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 8);
18757 Length = Length > BitWidth ? BitWidth : Length;
18758
18759 // Handle out of bounds cases.
18760 if (Length == 0 || Shift >= BitWidth)
18761 return Success(Value: 0, E);
18762
18763 uint64_t Result = Val.getZExtValue() >> Shift;
18764 Result &= llvm::maskTrailingOnes<uint64_t>(N: Length);
18765 return Success(Value: Result, E);
18766 }
18767
18768 case clang::X86::BI__builtin_ia32_bzhi_si:
18769 case clang::X86::BI__builtin_ia32_bzhi_di: {
18770 APSInt Val, Idx;
18771 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18772 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
18773 return false;
18774
18775 unsigned BitWidth = Val.getBitWidth();
18776 unsigned Index = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
18777 if (Index < BitWidth)
18778 Val.clearHighBits(hiBits: BitWidth - Index);
18779 return Success(SI: Val, E);
18780 }
18781
18782 case clang::X86::BI__builtin_ia32_ktestcqi:
18783 case clang::X86::BI__builtin_ia32_ktestchi:
18784 case clang::X86::BI__builtin_ia32_ktestcsi:
18785 case clang::X86::BI__builtin_ia32_ktestcdi: {
18786 APSInt A, B;
18787 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18788 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18789 return false;
18790
18791 return Success(Value: (~A & B) == 0, E);
18792 }
18793
18794 case clang::X86::BI__builtin_ia32_ktestzqi:
18795 case clang::X86::BI__builtin_ia32_ktestzhi:
18796 case clang::X86::BI__builtin_ia32_ktestzsi:
18797 case clang::X86::BI__builtin_ia32_ktestzdi: {
18798 APSInt A, B;
18799 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18800 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18801 return false;
18802
18803 return Success(Value: (A & B) == 0, E);
18804 }
18805
18806 case clang::X86::BI__builtin_ia32_kortestcqi:
18807 case clang::X86::BI__builtin_ia32_kortestchi:
18808 case clang::X86::BI__builtin_ia32_kortestcsi:
18809 case clang::X86::BI__builtin_ia32_kortestcdi: {
18810 APSInt A, B;
18811 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18812 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18813 return false;
18814
18815 return Success(Value: ~(A | B) == 0, E);
18816 }
18817
18818 case clang::X86::BI__builtin_ia32_kortestzqi:
18819 case clang::X86::BI__builtin_ia32_kortestzhi:
18820 case clang::X86::BI__builtin_ia32_kortestzsi:
18821 case clang::X86::BI__builtin_ia32_kortestzdi: {
18822 APSInt A, B;
18823 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18824 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18825 return false;
18826
18827 return Success(Value: (A | B) == 0, E);
18828 }
18829
18830 case clang::X86::BI__builtin_ia32_kunpckhi:
18831 case clang::X86::BI__builtin_ia32_kunpckdi:
18832 case clang::X86::BI__builtin_ia32_kunpcksi: {
18833 APSInt A, B;
18834 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18835 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18836 return false;
18837
18838 // Generic kunpack: extract lower half of each operand and concatenate
18839 // Result = A[HalfWidth-1:0] concat B[HalfWidth-1:0]
18840 unsigned BW = A.getBitWidth();
18841 APSInt Result(A.trunc(width: BW / 2).concat(NewLSB: B.trunc(width: BW / 2)), A.isUnsigned());
18842 return Success(SI: Result, E);
18843 }
18844
18845 case clang::X86::BI__builtin_ia32_lzcnt_u16:
18846 case clang::X86::BI__builtin_ia32_lzcnt_u32:
18847 case clang::X86::BI__builtin_ia32_lzcnt_u64: {
18848 APSInt Val;
18849 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18850 return false;
18851 return Success(Value: Val.countLeadingZeros(), E);
18852 }
18853
18854 case clang::X86::BI__builtin_ia32_tzcnt_u16:
18855 case clang::X86::BI__builtin_ia32_tzcnt_u32:
18856 case clang::X86::BI__builtin_ia32_tzcnt_u64: {
18857 APSInt Val;
18858 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18859 return false;
18860 return Success(Value: Val.countTrailingZeros(), E);
18861 }
18862
18863 case Builtin::BI__builtin_elementwise_pdep: {
18864 APSInt Val, Msk;
18865 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18866 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18867 return false;
18868 return Success(I: llvm::APIntOps::pdep(Val, Mask: Msk), E);
18869 }
18870
18871 case Builtin::BI__builtin_elementwise_pext: {
18872 APSInt Val, Msk;
18873 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18874 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18875 return false;
18876 return Success(I: llvm::APIntOps::pext(Val, Mask: Msk), E);
18877 }
18878
18879 case X86::BI__builtin_ia32_ptestz128:
18880 case X86::BI__builtin_ia32_ptestz256:
18881 case X86::BI__builtin_ia32_vtestzps:
18882 case X86::BI__builtin_ia32_vtestzps256:
18883 case X86::BI__builtin_ia32_vtestzpd:
18884 case X86::BI__builtin_ia32_vtestzpd256: {
18885 return EvalTestOp(
18886 [](const APInt &A, const APInt &B) { return (A & B) == 0; });
18887 }
18888 case X86::BI__builtin_ia32_ptestc128:
18889 case X86::BI__builtin_ia32_ptestc256:
18890 case X86::BI__builtin_ia32_vtestcps:
18891 case X86::BI__builtin_ia32_vtestcps256:
18892 case X86::BI__builtin_ia32_vtestcpd:
18893 case X86::BI__builtin_ia32_vtestcpd256: {
18894 return EvalTestOp(
18895 [](const APInt &A, const APInt &B) { return (~A & B) == 0; });
18896 }
18897 case X86::BI__builtin_ia32_ptestnzc128:
18898 case X86::BI__builtin_ia32_ptestnzc256:
18899 case X86::BI__builtin_ia32_vtestnzcps:
18900 case X86::BI__builtin_ia32_vtestnzcps256:
18901 case X86::BI__builtin_ia32_vtestnzcpd:
18902 case X86::BI__builtin_ia32_vtestnzcpd256: {
18903 return EvalTestOp([](const APInt &A, const APInt &B) {
18904 return ((A & B) != 0) && ((~A & B) != 0);
18905 });
18906 }
18907 case X86::BI__builtin_ia32_kandqi:
18908 case X86::BI__builtin_ia32_kandhi:
18909 case X86::BI__builtin_ia32_kandsi:
18910 case X86::BI__builtin_ia32_kanddi: {
18911 return HandleMaskBinOp(
18912 [](const APSInt &LHS, const APSInt &RHS) { return LHS & RHS; });
18913 }
18914
18915 case X86::BI__builtin_ia32_kandnqi:
18916 case X86::BI__builtin_ia32_kandnhi:
18917 case X86::BI__builtin_ia32_kandnsi:
18918 case X86::BI__builtin_ia32_kandndi: {
18919 return HandleMaskBinOp(
18920 [](const APSInt &LHS, const APSInt &RHS) { return ~LHS & RHS; });
18921 }
18922
18923 case X86::BI__builtin_ia32_korqi:
18924 case X86::BI__builtin_ia32_korhi:
18925 case X86::BI__builtin_ia32_korsi:
18926 case X86::BI__builtin_ia32_kordi: {
18927 return HandleMaskBinOp(
18928 [](const APSInt &LHS, const APSInt &RHS) { return LHS | RHS; });
18929 }
18930
18931 case X86::BI__builtin_ia32_kxnorqi:
18932 case X86::BI__builtin_ia32_kxnorhi:
18933 case X86::BI__builtin_ia32_kxnorsi:
18934 case X86::BI__builtin_ia32_kxnordi: {
18935 return HandleMaskBinOp(
18936 [](const APSInt &LHS, const APSInt &RHS) { return ~(LHS ^ RHS); });
18937 }
18938
18939 case X86::BI__builtin_ia32_kxorqi:
18940 case X86::BI__builtin_ia32_kxorhi:
18941 case X86::BI__builtin_ia32_kxorsi:
18942 case X86::BI__builtin_ia32_kxordi: {
18943 return HandleMaskBinOp(
18944 [](const APSInt &LHS, const APSInt &RHS) { return LHS ^ RHS; });
18945 }
18946
18947 case X86::BI__builtin_ia32_knotqi:
18948 case X86::BI__builtin_ia32_knothi:
18949 case X86::BI__builtin_ia32_knotsi:
18950 case X86::BI__builtin_ia32_knotdi: {
18951 APSInt Val;
18952 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18953 return false;
18954 APSInt Result = ~Val;
18955 return Success(V: APValue(Result), E);
18956 }
18957
18958 case X86::BI__builtin_ia32_kaddqi:
18959 case X86::BI__builtin_ia32_kaddhi:
18960 case X86::BI__builtin_ia32_kaddsi:
18961 case X86::BI__builtin_ia32_kadddi: {
18962 return HandleMaskBinOp(
18963 [](const APSInt &LHS, const APSInt &RHS) { return LHS + RHS; });
18964 }
18965
18966 case X86::BI__builtin_ia32_kmovb:
18967 case X86::BI__builtin_ia32_kmovw:
18968 case X86::BI__builtin_ia32_kmovd:
18969 case X86::BI__builtin_ia32_kmovq: {
18970 APSInt Val;
18971 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18972 return false;
18973 return Success(SI: Val, E);
18974 }
18975
18976 case X86::BI__builtin_ia32_kshiftliqi:
18977 case X86::BI__builtin_ia32_kshiftlihi:
18978 case X86::BI__builtin_ia32_kshiftlisi:
18979 case X86::BI__builtin_ia32_kshiftlidi: {
18980 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18981 unsigned Amt = RHS.getZExtValue() & 0xFF;
18982 if (Amt >= LHS.getBitWidth())
18983 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18984 return APSInt(LHS.shl(shiftAmt: Amt), LHS.isUnsigned());
18985 });
18986 }
18987
18988 case X86::BI__builtin_ia32_kshiftriqi:
18989 case X86::BI__builtin_ia32_kshiftrihi:
18990 case X86::BI__builtin_ia32_kshiftrisi:
18991 case X86::BI__builtin_ia32_kshiftridi: {
18992 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18993 unsigned Amt = RHS.getZExtValue() & 0xFF;
18994 if (Amt >= LHS.getBitWidth())
18995 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18996 return APSInt(LHS.lshr(shiftAmt: Amt), LHS.isUnsigned());
18997 });
18998 }
18999
19000 case clang::X86::BI__builtin_ia32_vec_ext_v4hi:
19001 case clang::X86::BI__builtin_ia32_vec_ext_v16qi:
19002 case clang::X86::BI__builtin_ia32_vec_ext_v8hi:
19003 case clang::X86::BI__builtin_ia32_vec_ext_v4si:
19004 case clang::X86::BI__builtin_ia32_vec_ext_v2di:
19005 case clang::X86::BI__builtin_ia32_vec_ext_v32qi:
19006 case clang::X86::BI__builtin_ia32_vec_ext_v16hi:
19007 case clang::X86::BI__builtin_ia32_vec_ext_v8si:
19008 case clang::X86::BI__builtin_ia32_vec_ext_v4di: {
19009 APValue Vec;
19010 APSInt IdxAPS;
19011 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
19012 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
19013 return false;
19014 unsigned N = Vec.getVectorLength();
19015 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
19016 return Success(SI: Vec.getVectorElt(I: Idx).getInt(), E);
19017 }
19018
19019 case clang::X86::BI__builtin_ia32_cvtb2mask128:
19020 case clang::X86::BI__builtin_ia32_cvtb2mask256:
19021 case clang::X86::BI__builtin_ia32_cvtb2mask512:
19022 case clang::X86::BI__builtin_ia32_cvtw2mask128:
19023 case clang::X86::BI__builtin_ia32_cvtw2mask256:
19024 case clang::X86::BI__builtin_ia32_cvtw2mask512:
19025 case clang::X86::BI__builtin_ia32_cvtd2mask128:
19026 case clang::X86::BI__builtin_ia32_cvtd2mask256:
19027 case clang::X86::BI__builtin_ia32_cvtd2mask512:
19028 case clang::X86::BI__builtin_ia32_cvtq2mask128:
19029 case clang::X86::BI__builtin_ia32_cvtq2mask256:
19030 case clang::X86::BI__builtin_ia32_cvtq2mask512: {
19031 assert(E->getNumArgs() == 1);
19032 APValue Vec;
19033 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
19034 return false;
19035
19036 unsigned VectorLen = Vec.getVectorLength();
19037 unsigned RetWidth = Info.Ctx.getIntWidth(T: E->getType());
19038 llvm::APInt Bits(RetWidth, 0);
19039
19040 for (unsigned ElemNum = 0; ElemNum != VectorLen; ++ElemNum) {
19041 const APSInt &A = Vec.getVectorElt(I: ElemNum).getInt();
19042 unsigned MSB = A[A.getBitWidth() - 1];
19043 Bits.setBitVal(BitPosition: ElemNum, BitValue: MSB);
19044 }
19045
19046 APSInt RetMask(Bits, /*isUnsigned=*/true);
19047 return Success(V: APValue(RetMask), E);
19048 }
19049
19050 case clang::X86::BI__builtin_ia32_cmpb128_mask:
19051 case clang::X86::BI__builtin_ia32_cmpw128_mask:
19052 case clang::X86::BI__builtin_ia32_cmpd128_mask:
19053 case clang::X86::BI__builtin_ia32_cmpq128_mask:
19054 case clang::X86::BI__builtin_ia32_cmpb256_mask:
19055 case clang::X86::BI__builtin_ia32_cmpw256_mask:
19056 case clang::X86::BI__builtin_ia32_cmpd256_mask:
19057 case clang::X86::BI__builtin_ia32_cmpq256_mask:
19058 case clang::X86::BI__builtin_ia32_cmpb512_mask:
19059 case clang::X86::BI__builtin_ia32_cmpw512_mask:
19060 case clang::X86::BI__builtin_ia32_cmpd512_mask:
19061 case clang::X86::BI__builtin_ia32_cmpq512_mask:
19062 case clang::X86::BI__builtin_ia32_ucmpb128_mask:
19063 case clang::X86::BI__builtin_ia32_ucmpw128_mask:
19064 case clang::X86::BI__builtin_ia32_ucmpd128_mask:
19065 case clang::X86::BI__builtin_ia32_ucmpq128_mask:
19066 case clang::X86::BI__builtin_ia32_ucmpb256_mask:
19067 case clang::X86::BI__builtin_ia32_ucmpw256_mask:
19068 case clang::X86::BI__builtin_ia32_ucmpd256_mask:
19069 case clang::X86::BI__builtin_ia32_ucmpq256_mask:
19070 case clang::X86::BI__builtin_ia32_ucmpb512_mask:
19071 case clang::X86::BI__builtin_ia32_ucmpw512_mask:
19072 case clang::X86::BI__builtin_ia32_ucmpd512_mask:
19073 case clang::X86::BI__builtin_ia32_ucmpq512_mask: {
19074 assert(E->getNumArgs() == 4);
19075
19076 bool IsUnsigned =
19077 (BuiltinOp >= clang::X86::BI__builtin_ia32_ucmpb128_mask &&
19078 BuiltinOp <= clang::X86::BI__builtin_ia32_ucmpw512_mask);
19079
19080 APValue LHS, RHS;
19081 APSInt Mask, Opcode;
19082 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
19083 !EvaluateVector(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
19084 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Opcode, Info) ||
19085 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: Mask, Info))
19086 return false;
19087
19088 assert(LHS.getVectorLength() == RHS.getVectorLength());
19089
19090 unsigned VectorLen = LHS.getVectorLength();
19091 unsigned RetWidth = Mask.getBitWidth();
19092
19093 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
19094
19095 for (unsigned ElemNum = 0; ElemNum < VectorLen; ++ElemNum) {
19096 const APSInt &A = LHS.getVectorElt(I: ElemNum).getInt();
19097 const APSInt &B = RHS.getVectorElt(I: ElemNum).getInt();
19098 bool Result = false;
19099
19100 switch (Opcode.getExtValue() & 0x7) {
19101 case 0: // _MM_CMPINT_EQ
19102 Result = (A == B);
19103 break;
19104 case 1: // _MM_CMPINT_LT
19105 Result = IsUnsigned ? A.ult(RHS: B) : A.slt(RHS: B);
19106 break;
19107 case 2: // _MM_CMPINT_LE
19108 Result = IsUnsigned ? A.ule(RHS: B) : A.sle(RHS: B);
19109 break;
19110 case 3: // _MM_CMPINT_FALSE
19111 Result = false;
19112 break;
19113 case 4: // _MM_CMPINT_NE
19114 Result = (A != B);
19115 break;
19116 case 5: // _MM_CMPINT_NLT (>=)
19117 Result = IsUnsigned ? A.uge(RHS: B) : A.sge(RHS: B);
19118 break;
19119 case 6: // _MM_CMPINT_NLE (>)
19120 Result = IsUnsigned ? A.ugt(RHS: B) : A.sgt(RHS: B);
19121 break;
19122 case 7: // _MM_CMPINT_TRUE
19123 Result = true;
19124 break;
19125 }
19126
19127 RetMask.setBitVal(BitPosition: ElemNum, BitValue: Mask[ElemNum] && Result);
19128 }
19129
19130 return Success(V: APValue(RetMask), E);
19131 }
19132 case X86::BI__builtin_ia32_cvtss2si:
19133 case X86::BI__builtin_ia32_cvtsd2si:
19134 case X86::BI__builtin_ia32_cvttss2si:
19135 case X86::BI__builtin_ia32_cvttsd2si:
19136 case X86::BI__builtin_ia32_cvtss2si64:
19137 case X86::BI__builtin_ia32_cvtsd2si64:
19138 case X86::BI__builtin_ia32_cvttss2si64:
19139 case X86::BI__builtin_ia32_cvttsd2si64: {
19140 APValue ArgVal;
19141 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: ArgVal))
19142 return false;
19143
19144 assert(ArgVal.isVector() && "Expected a vector argument");
19145 llvm::APFloat FloatElem = ArgVal.getVectorElt(I: 0).getFloat();
19146 unsigned BitWidth = Info.Ctx.getIntWidth(T: E->getType());
19147 bool isUnsigned = E->getType()->isUnsignedIntegerType();
19148
19149 llvm::APSInt IntResult(BitWidth, isUnsigned);
19150 bool IsExact = false;
19151 // We only allow exact conversions so rounding mode does not matter for cvt*
19152 // and cvtt* builtins
19153 FloatElem.convertToInteger(Result&: IntResult, RM: llvm::APFloat::rmTowardZero,
19154 IsExact: &IsExact);
19155 if (!IsExact)
19156 return false;
19157
19158 return Success(SI: IntResult, E);
19159 }
19160 case X86::BI__builtin_ia32_vpshufbitqmb128_mask:
19161 case X86::BI__builtin_ia32_vpshufbitqmb256_mask:
19162 case X86::BI__builtin_ia32_vpshufbitqmb512_mask: {
19163 assert(E->getNumArgs() == 3);
19164
19165 APValue Source, ShuffleMask;
19166 APSInt ZeroMask;
19167 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
19168 !EvaluateVector(E: E->getArg(Arg: 1), Result&: ShuffleMask, Info) ||
19169 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: ZeroMask, Info))
19170 return false;
19171
19172 assert(Source.getVectorLength() == ShuffleMask.getVectorLength());
19173 assert(ZeroMask.getBitWidth() == Source.getVectorLength());
19174
19175 unsigned NumBytesInQWord = 8;
19176 unsigned NumBitsInByte = 8;
19177 unsigned NumBytes = Source.getVectorLength();
19178 unsigned NumQWords = NumBytes / NumBytesInQWord;
19179 unsigned RetWidth = ZeroMask.getBitWidth();
19180 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
19181
19182 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
19183 APInt SourceQWord(64, 0);
19184 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
19185 uint64_t Byte = Source.getVectorElt(I: QWordId * NumBytesInQWord + ByteIdx)
19186 .getInt()
19187 .getZExtValue();
19188 SourceQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
19189 }
19190
19191 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
19192 unsigned SelIdx = QWordId * NumBytesInQWord + ByteIdx;
19193 unsigned M =
19194 ShuffleMask.getVectorElt(I: SelIdx).getInt().getZExtValue() & 0x3F;
19195 if (ZeroMask[SelIdx]) {
19196 RetMask.setBitVal(BitPosition: SelIdx, BitValue: SourceQWord[M]);
19197 }
19198 }
19199 }
19200 return Success(V: APValue(RetMask), E);
19201 }
19202 }
19203}
19204
19205/// Determine whether this is a pointer past the end of the complete
19206/// object referred to by the lvalue.
19207static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx,
19208 const LValue &LV) {
19209 // A null pointer can be viewed as being "past the end" but we don't
19210 // choose to look at it that way here.
19211 if (!LV.getLValueBase())
19212 return false;
19213
19214 // If the designator is valid and refers to a subobject, we're not pointing
19215 // past the end.
19216 if (!LV.getLValueDesignator().Invalid &&
19217 !LV.getLValueDesignator().isOnePastTheEnd())
19218 return false;
19219
19220 // A pointer to an incomplete type might be past-the-end if the type's size is
19221 // zero. We cannot tell because the type is incomplete.
19222 QualType Ty = getType(B: LV.getLValueBase());
19223 if (Ty->isIncompleteType())
19224 return true;
19225
19226 // Can't be past the end of an invalid object.
19227 if (LV.getLValueDesignator().Invalid)
19228 return false;
19229
19230 // We're a past-the-end pointer if we point to the byte after the object,
19231 // no matter what our type or path is.
19232 auto Size = Ctx.getTypeSizeInChars(T: Ty);
19233 return LV.getLValueOffset() == Size;
19234}
19235
19236namespace {
19237
19238/// Data recursive integer evaluator of certain binary operators.
19239///
19240/// We use a data recursive algorithm for binary operators so that we are able
19241/// to handle extreme cases of chained binary operators without causing stack
19242/// overflow.
19243class DataRecursiveIntBinOpEvaluator {
19244 struct EvalResult {
19245 APValue Val;
19246 bool Failed = false;
19247
19248 EvalResult() = default;
19249
19250 void swap(EvalResult &RHS) {
19251 Val.swap(RHS&: RHS.Val);
19252 Failed = RHS.Failed;
19253 RHS.Failed = false;
19254 }
19255 };
19256
19257 struct Job {
19258 const Expr *E;
19259 EvalResult LHSResult; // meaningful only for binary operator expression.
19260 enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind;
19261
19262 Job() = default;
19263 Job(Job &&) = default;
19264
19265 void startSpeculativeEval(EvalInfo &Info) {
19266 SpecEvalRAII = SpeculativeEvaluationRAII(Info);
19267 }
19268
19269 private:
19270 SpeculativeEvaluationRAII SpecEvalRAII;
19271 };
19272
19273 SmallVector<Job, 16> Queue;
19274
19275 IntExprEvaluator &IntEval;
19276 EvalInfo &Info;
19277 APValue &FinalResult;
19278
19279public:
19280 DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result)
19281 : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { }
19282
19283 /// True if \param E is a binary operator that we are going to handle
19284 /// data recursively.
19285 /// We handle binary operators that are comma, logical, or that have operands
19286 /// with integral or enumeration type.
19287 static bool shouldEnqueue(const BinaryOperator *E) {
19288 return E->getOpcode() == BO_Comma || E->isLogicalOp() ||
19289 (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() &&
19290 E->getLHS()->getType()->isIntegralOrEnumerationType() &&
19291 E->getRHS()->getType()->isIntegralOrEnumerationType());
19292 }
19293
19294 bool Traverse(const BinaryOperator *E) {
19295 enqueue(E);
19296 EvalResult PrevResult;
19297 while (!Queue.empty())
19298 process(Result&: PrevResult);
19299
19300 if (PrevResult.Failed) return false;
19301
19302 FinalResult.swap(RHS&: PrevResult.Val);
19303 return true;
19304 }
19305
19306private:
19307 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
19308 return IntEval.Success(Value, E, Result);
19309 }
19310 bool Success(const APSInt &Value, const Expr *E, APValue &Result) {
19311 return IntEval.Success(SI: Value, E, Result);
19312 }
19313 bool Error(const Expr *E) {
19314 return IntEval.Error(E);
19315 }
19316 bool Error(const Expr *E, diag::kind D) {
19317 return IntEval.Error(E, D);
19318 }
19319
19320 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
19321 return Info.CCEDiag(E, DiagId: D);
19322 }
19323
19324 // Returns true if visiting the RHS is necessary, false otherwise.
19325 bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
19326 bool &SuppressRHSDiags);
19327
19328 bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
19329 const BinaryOperator *E, APValue &Result);
19330
19331 void EvaluateExpr(const Expr *E, EvalResult &Result) {
19332 Result.Failed = !Evaluate(Result&: Result.Val, Info, E);
19333 if (Result.Failed)
19334 Result.Val = APValue();
19335 }
19336
19337 void process(EvalResult &Result);
19338
19339 void enqueue(const Expr *E) {
19340 E = E->IgnoreParens();
19341 Queue.resize(N: Queue.size()+1);
19342 Queue.back().E = E;
19343 Queue.back().Kind = Job::AnyExprKind;
19344 }
19345};
19346
19347}
19348
19349bool DataRecursiveIntBinOpEvaluator::
19350 VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
19351 bool &SuppressRHSDiags) {
19352 if (E->getOpcode() == BO_Comma) {
19353 // Ignore LHS but note if we could not evaluate it.
19354 if (LHSResult.Failed)
19355 return Info.noteSideEffect();
19356 return true;
19357 }
19358
19359 if (E->isLogicalOp()) {
19360 bool LHSAsBool;
19361 if (!LHSResult.Failed && HandleConversionToBool(Val: LHSResult.Val, Result&: LHSAsBool)) {
19362 // We were able to evaluate the LHS, see if we can get away with not
19363 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
19364 if (LHSAsBool == (E->getOpcode() == BO_LOr)) {
19365 Success(Value: LHSAsBool, E, Result&: LHSResult.Val);
19366 return false; // Ignore RHS
19367 }
19368 } else {
19369 LHSResult.Failed = true;
19370
19371 // Since we weren't able to evaluate the left hand side, it
19372 // might have had side effects.
19373 if (!Info.noteSideEffect())
19374 return false;
19375
19376 // We can't evaluate the LHS; however, sometimes the result
19377 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
19378 // Don't ignore RHS and suppress diagnostics from this arm.
19379 SuppressRHSDiags = true;
19380 }
19381
19382 return true;
19383 }
19384
19385 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
19386 E->getRHS()->getType()->isIntegralOrEnumerationType());
19387
19388 if (LHSResult.Failed && !Info.noteFailure())
19389 return false; // Ignore RHS;
19390
19391 return true;
19392}
19393
19394static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index,
19395 bool IsSub) {
19396 // Compute the new offset in the appropriate width, wrapping at 64 bits.
19397 // FIXME: When compiling for a 32-bit target, we should use 32-bit
19398 // offsets.
19399 assert(!LVal.hasLValuePath() && "have designator for integer lvalue");
19400 CharUnits &Offset = LVal.getLValueOffset();
19401 uint64_t Offset64 = Offset.getQuantity();
19402 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
19403 Offset = CharUnits::fromQuantity(Quantity: IsSub ? Offset64 - Index64
19404 : Offset64 + Index64);
19405}
19406
19407bool DataRecursiveIntBinOpEvaluator::
19408 VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
19409 const BinaryOperator *E, APValue &Result) {
19410 if (E->getOpcode() == BO_Comma) {
19411 if (RHSResult.Failed)
19412 return false;
19413 Result = RHSResult.Val;
19414 return true;
19415 }
19416
19417 if (E->isLogicalOp()) {
19418 bool lhsResult, rhsResult;
19419 bool LHSIsOK = HandleConversionToBool(Val: LHSResult.Val, Result&: lhsResult);
19420 bool RHSIsOK = HandleConversionToBool(Val: RHSResult.Val, Result&: rhsResult);
19421
19422 if (LHSIsOK) {
19423 if (RHSIsOK) {
19424 if (E->getOpcode() == BO_LOr)
19425 return Success(Value: lhsResult || rhsResult, E, Result);
19426 else
19427 return Success(Value: lhsResult && rhsResult, E, Result);
19428 }
19429 } else {
19430 if (RHSIsOK) {
19431 // We can't evaluate the LHS; however, sometimes the result
19432 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
19433 if (rhsResult == (E->getOpcode() == BO_LOr))
19434 return Success(Value: rhsResult, E, Result);
19435 }
19436 }
19437
19438 return false;
19439 }
19440
19441 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
19442 E->getRHS()->getType()->isIntegralOrEnumerationType());
19443
19444 if (LHSResult.Failed || RHSResult.Failed)
19445 return false;
19446
19447 const APValue &LHSVal = LHSResult.Val;
19448 const APValue &RHSVal = RHSResult.Val;
19449
19450 // Handle cases like (unsigned long)&a + 4.
19451 if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
19452 Result = LHSVal;
19453 addOrSubLValueAsInteger(LVal&: Result, Index: RHSVal.getInt(), IsSub: E->getOpcode() == BO_Sub);
19454 return true;
19455 }
19456
19457 // Handle cases like 4 + (unsigned long)&a
19458 if (E->getOpcode() == BO_Add &&
19459 RHSVal.isLValue() && LHSVal.isInt()) {
19460 Result = RHSVal;
19461 addOrSubLValueAsInteger(LVal&: Result, Index: LHSVal.getInt(), /*IsSub*/false);
19462 return true;
19463 }
19464
19465 if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
19466 // Handle (intptr_t)&&A - (intptr_t)&&B.
19467 if (!LHSVal.getLValueOffset().isZero() ||
19468 !RHSVal.getLValueOffset().isZero())
19469 return false;
19470 const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
19471 const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
19472 if (!LHSExpr || !RHSExpr)
19473 return false;
19474 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
19475 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
19476 if (!LHSAddrExpr || !RHSAddrExpr)
19477 return false;
19478 // Make sure both labels come from the same function.
19479 if (LHSAddrExpr->getLabel()->getDeclContext() !=
19480 RHSAddrExpr->getLabel()->getDeclContext())
19481 return false;
19482 Result = APValue(LHSAddrExpr, RHSAddrExpr);
19483 return true;
19484 }
19485
19486 // All the remaining cases expect both operands to be an integer
19487 if (!LHSVal.isInt() || !RHSVal.isInt())
19488 return Error(E);
19489
19490 // Set up the width and signedness manually, in case it can't be deduced
19491 // from the operation we're performing.
19492 // FIXME: Don't do this in the cases where we can deduce it.
19493 APSInt Value(Info.Ctx.getIntWidth(T: E->getType()),
19494 E->getType()->isUnsignedIntegerOrEnumerationType());
19495 if (!handleIntIntBinOp(Info, E, LHS: LHSVal.getInt(), Opcode: E->getOpcode(),
19496 RHS: RHSVal.getInt(), Result&: Value))
19497 return false;
19498 return Success(Value, E, Result);
19499}
19500
19501void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) {
19502 Job &job = Queue.back();
19503
19504 switch (job.Kind) {
19505 case Job::AnyExprKind: {
19506 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(Val: job.E)) {
19507 if (shouldEnqueue(E: Bop)) {
19508 job.Kind = Job::BinOpKind;
19509 enqueue(E: Bop->getLHS());
19510 return;
19511 }
19512 }
19513
19514 EvaluateExpr(E: job.E, Result);
19515 Queue.pop_back();
19516 return;
19517 }
19518
19519 case Job::BinOpKind: {
19520 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
19521 bool SuppressRHSDiags = false;
19522 if (!VisitBinOpLHSOnly(LHSResult&: Result, E: Bop, SuppressRHSDiags)) {
19523 Queue.pop_back();
19524 return;
19525 }
19526 if (SuppressRHSDiags)
19527 job.startSpeculativeEval(Info);
19528 job.LHSResult.swap(RHS&: Result);
19529 job.Kind = Job::BinOpVisitedLHSKind;
19530 enqueue(E: Bop->getRHS());
19531 return;
19532 }
19533
19534 case Job::BinOpVisitedLHSKind: {
19535 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
19536 EvalResult RHS;
19537 RHS.swap(RHS&: Result);
19538 Result.Failed = !VisitBinOp(LHSResult: job.LHSResult, RHSResult: RHS, E: Bop, Result&: Result.Val);
19539 Queue.pop_back();
19540 return;
19541 }
19542 }
19543
19544 llvm_unreachable("Invalid Job::Kind!");
19545}
19546
19547namespace {
19548enum class CmpResult {
19549 Unequal,
19550 Less,
19551 Equal,
19552 Greater,
19553 Unordered,
19554};
19555}
19556
19557template <class SuccessCB, class AfterCB>
19558static bool
19559EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
19560 SuccessCB &&Success, AfterCB &&DoAfter) {
19561 assert(!E->isValueDependent());
19562 assert(E->isComparisonOp() && "expected comparison operator");
19563 assert((E->getOpcode() == BO_Cmp ||
19564 E->getType()->isIntegralOrEnumerationType()) &&
19565 "unsupported binary expression evaluation");
19566 auto Error = [&](const Expr *E) {
19567 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
19568 return false;
19569 };
19570
19571 bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp;
19572 bool IsEquality = E->isEqualityOp();
19573
19574 QualType LHSTy = E->getLHS()->getType();
19575 QualType RHSTy = E->getRHS()->getType();
19576
19577 if (LHSTy->isIntegralOrEnumerationType() &&
19578 RHSTy->isIntegralOrEnumerationType()) {
19579 APSInt LHS, RHS;
19580 bool LHSOK = EvaluateInteger(E: E->getLHS(), Result&: LHS, Info);
19581 if (!LHSOK && !Info.noteFailure())
19582 return false;
19583 if (!EvaluateInteger(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
19584 return false;
19585 if (LHS < RHS)
19586 return Success(CmpResult::Less, E);
19587 if (LHS > RHS)
19588 return Success(CmpResult::Greater, E);
19589 return Success(CmpResult::Equal, E);
19590 }
19591
19592 if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) {
19593 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHSTy));
19594 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHSTy));
19595
19596 bool LHSOK = EvaluateFixedPointOrInteger(E: E->getLHS(), Result&: LHSFX, Info);
19597 if (!LHSOK && !Info.noteFailure())
19598 return false;
19599 if (!EvaluateFixedPointOrInteger(E: E->getRHS(), Result&: RHSFX, Info) || !LHSOK)
19600 return false;
19601 if (LHSFX < RHSFX)
19602 return Success(CmpResult::Less, E);
19603 if (LHSFX > RHSFX)
19604 return Success(CmpResult::Greater, E);
19605 return Success(CmpResult::Equal, E);
19606 }
19607
19608 if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) {
19609 ComplexValue LHS, RHS;
19610 bool LHSOK;
19611 if (E->isAssignmentOp()) {
19612 LValue LV;
19613 EvaluateLValue(E: E->getLHS(), Result&: LV, Info);
19614 LHSOK = false;
19615 } else if (LHSTy->isRealFloatingType()) {
19616 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: LHS.FloatReal, Info);
19617 if (LHSOK) {
19618 LHS.makeComplexFloat();
19619 LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics());
19620 }
19621 } else {
19622 LHSOK = EvaluateComplex(E: E->getLHS(), Res&: LHS, Info);
19623 }
19624 if (!LHSOK && !Info.noteFailure())
19625 return false;
19626
19627 if (E->getRHS()->getType()->isRealFloatingType()) {
19628 if (!EvaluateFloat(E: E->getRHS(), Result&: RHS.FloatReal, Info) || !LHSOK)
19629 return false;
19630 RHS.makeComplexFloat();
19631 RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics());
19632 } else if (!EvaluateComplex(E: E->getRHS(), Res&: RHS, Info) || !LHSOK)
19633 return false;
19634
19635 if (LHS.isComplexFloat()) {
19636 APFloat::cmpResult CR_r =
19637 LHS.getComplexFloatReal().compare(RHS: RHS.getComplexFloatReal());
19638 APFloat::cmpResult CR_i =
19639 LHS.getComplexFloatImag().compare(RHS: RHS.getComplexFloatImag());
19640 bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual;
19641 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
19642 } else {
19643 assert(IsEquality && "invalid complex comparison");
19644 bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
19645 LHS.getComplexIntImag() == RHS.getComplexIntImag();
19646 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
19647 }
19648 }
19649
19650 if (LHSTy->isRealFloatingType() &&
19651 RHSTy->isRealFloatingType()) {
19652 APFloat RHS(0.0), LHS(0.0);
19653
19654 bool LHSOK = EvaluateFloat(E: E->getRHS(), Result&: RHS, Info);
19655 if (!LHSOK && !Info.noteFailure())
19656 return false;
19657
19658 if (!EvaluateFloat(E: E->getLHS(), Result&: LHS, Info) || !LHSOK)
19659 return false;
19660
19661 assert(E->isComparisonOp() && "Invalid binary operator!");
19662 llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS);
19663 if (!Info.InConstantContext &&
19664 APFloatCmpResult == APFloat::cmpUnordered &&
19665 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained()) {
19666 // Note: Compares may raise invalid in some cases involving NaN or sNaN.
19667 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
19668 return false;
19669 }
19670 auto GetCmpRes = [&]() {
19671 switch (APFloatCmpResult) {
19672 case APFloat::cmpEqual:
19673 return CmpResult::Equal;
19674 case APFloat::cmpLessThan:
19675 return CmpResult::Less;
19676 case APFloat::cmpGreaterThan:
19677 return CmpResult::Greater;
19678 case APFloat::cmpUnordered:
19679 return CmpResult::Unordered;
19680 }
19681 llvm_unreachable("Unrecognised APFloat::cmpResult enum");
19682 };
19683 return Success(GetCmpRes(), E);
19684 }
19685
19686 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
19687 LValue LHSValue, RHSValue;
19688
19689 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
19690 if (!LHSOK && !Info.noteFailure())
19691 return false;
19692
19693 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19694 return false;
19695
19696 // Reject differing bases from the normal codepath; we special-case
19697 // comparisons to null.
19698 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
19699 // Bail out early if we're checking potential constant expression.
19700 // Otherwise, prefer to diagnose other issues.
19701 if (Info.checkingPotentialConstantExpression() &&
19702 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
19703 return false;
19704 auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) {
19705 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
19706 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
19707 Info.FFDiag(E, DiagId: DiagID)
19708 << (Reversed ? RHS : LHS) << (Reversed ? LHS : RHS);
19709 return false;
19710 };
19711 // Inequalities and subtractions between unrelated pointers have
19712 // unspecified or undefined behavior.
19713 if (!IsEquality)
19714 return DiagComparison(
19715 diag::note_constexpr_pointer_comparison_unspecified);
19716 // A constant address may compare equal to the address of a symbol.
19717 // The one exception is that address of an object cannot compare equal
19718 // to a null pointer constant.
19719 // TODO: Should we restrict this to actual null pointers, and exclude the
19720 // case of zero cast to pointer type?
19721 if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
19722 (!RHSValue.Base && !RHSValue.Offset.isZero()))
19723 return DiagComparison(diag::note_constexpr_pointer_constant_comparison,
19724 !RHSValue.Base);
19725 // C++2c [intro.object]/10:
19726 // Two objects [...] may have the same address if [...] they are both
19727 // potentially non-unique objects.
19728 // C++2c [intro.object]/9:
19729 // An object is potentially non-unique if it is a string literal object,
19730 // the backing array of an initializer list, or a subobject thereof.
19731 //
19732 // This makes the comparison result unspecified, so it's not a constant
19733 // expression.
19734 //
19735 // TODO: Do we need to handle the initializer list case here?
19736 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
19737 return DiagComparison(diag::note_constexpr_literal_comparison);
19738 if (IsOpaqueConstantCall(LVal: LHSValue) || IsOpaqueConstantCall(LVal: RHSValue))
19739 return DiagComparison(diag::note_constexpr_opaque_call_comparison,
19740 !IsOpaqueConstantCall(LVal: LHSValue));
19741 // We can't tell whether weak symbols will end up pointing to the same
19742 // object.
19743 if (IsWeakLValue(Value: LHSValue) || IsWeakLValue(Value: RHSValue))
19744 return DiagComparison(diag::note_constexpr_pointer_weak_comparison,
19745 !IsWeakLValue(Value: LHSValue));
19746 // We can't compare the address of the start of one object with the
19747 // past-the-end address of another object, per C++ DR1652.
19748 if (LHSValue.Base && LHSValue.Offset.isZero() &&
19749 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: RHSValue))
19750 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
19751 true);
19752 if (RHSValue.Base && RHSValue.Offset.isZero() &&
19753 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: LHSValue))
19754 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
19755 false);
19756 // We can't tell whether an object is at the same address as another
19757 // zero sized object.
19758 if ((RHSValue.Base && isZeroSized(Value: LHSValue)) ||
19759 (LHSValue.Base && isZeroSized(Value: RHSValue)))
19760 return DiagComparison(
19761 diag::note_constexpr_pointer_comparison_zero_sized);
19762 if (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)
19763 return DiagComparison(
19764 diag::note_constexpr_pointer_comparison_unspecified);
19765 // FIXME: Verify both variables are live.
19766 return Success(CmpResult::Unequal, E);
19767 }
19768
19769 CharUnits LHSOffset = LHSValue.getLValueOffset();
19770 CharUnits RHSOffset = RHSValue.getLValueOffset();
19771
19772 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
19773 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
19774
19775 // C++11 [expr.rel]p2:
19776 // - If two pointers point to non-static data members of the same object,
19777 // or to subobjects or array elements fo such members, recursively, the
19778 // pointer to the later declared member compares greater provided the
19779 // two members have the same access control and provided their class is
19780 // not a union.
19781 // [...]
19782 // - Otherwise pointer comparisons are unspecified.
19783 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {
19784 bool WasArrayIndex;
19785 unsigned Mismatch = FindDesignatorMismatch(
19786 ObjType: LHSValue.Base.isNull() ? QualType()
19787 : getType(B: LHSValue.Base).getNonReferenceType(),
19788 A: LHSDesignator, B: RHSDesignator, WasArrayIndex);
19789 // At the point where the designators diverge, the comparison has a
19790 // specified value if:
19791 // - we are comparing array indices
19792 // - we are comparing fields of a union, or fields with the same access
19793 // Otherwise, the result is unspecified and thus the comparison is not a
19794 // constant expression.
19795 if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
19796 Mismatch < RHSDesignator.Entries.size()) {
19797 const FieldDecl *LF = getAsField(E: LHSDesignator.Entries[Mismatch]);
19798 const FieldDecl *RF = getAsField(E: RHSDesignator.Entries[Mismatch]);
19799 if (!LF && !RF)
19800 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_classes);
19801 else if (!LF)
19802 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
19803 << getAsBaseClass(E: LHSDesignator.Entries[Mismatch])
19804 << RF->getParent() << RF;
19805 else if (!RF)
19806 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
19807 << getAsBaseClass(E: RHSDesignator.Entries[Mismatch])
19808 << LF->getParent() << LF;
19809 else if (!LF->getParent()->isUnion() &&
19810 LF->getAccess() != RF->getAccess())
19811 Info.CCEDiag(E,
19812 DiagId: diag::note_constexpr_pointer_comparison_differing_access)
19813 << LF << LF->getAccess() << RF << RF->getAccess()
19814 << LF->getParent();
19815 }
19816 }
19817
19818 // The comparison here must be unsigned, and performed with the same
19819 // width as the pointer.
19820 unsigned PtrSize = Info.Ctx.getTypeSize(T: LHSTy);
19821 uint64_t CompareLHS = LHSOffset.getQuantity();
19822 uint64_t CompareRHS = RHSOffset.getQuantity();
19823 assert(PtrSize <= 64 && "Unexpected pointer width");
19824 uint64_t Mask = ~0ULL >> (64 - PtrSize);
19825 CompareLHS &= Mask;
19826 CompareRHS &= Mask;
19827
19828 // If there is a base and this is a relational operator, we can only
19829 // compare pointers within the object in question; otherwise, the result
19830 // depends on where the object is located in memory.
19831 if (!LHSValue.Base.isNull() && IsRelational) {
19832 QualType BaseTy = getType(B: LHSValue.Base).getNonReferenceType();
19833 if (BaseTy->isIncompleteType())
19834 return Error(E);
19835 CharUnits Size = Info.Ctx.getTypeSizeInChars(T: BaseTy);
19836 uint64_t OffsetLimit = Size.getQuantity();
19837 if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)
19838 return Error(E);
19839 }
19840
19841 if (CompareLHS < CompareRHS)
19842 return Success(CmpResult::Less, E);
19843 if (CompareLHS > CompareRHS)
19844 return Success(CmpResult::Greater, E);
19845 return Success(CmpResult::Equal, E);
19846 }
19847
19848 if (LHSTy->isMemberPointerType()) {
19849 assert(IsEquality && "unexpected member pointer operation");
19850 assert(RHSTy->isMemberPointerType() && "invalid comparison");
19851
19852 MemberPtr LHSValue, RHSValue;
19853
19854 bool LHSOK = EvaluateMemberPointer(E: E->getLHS(), Result&: LHSValue, Info);
19855 if (!LHSOK && !Info.noteFailure())
19856 return false;
19857
19858 if (!EvaluateMemberPointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19859 return false;
19860
19861 // If either operand is a pointer to a weak function, the comparison is not
19862 // constant.
19863 if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) {
19864 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19865 << LHSValue.getDecl();
19866 return false;
19867 }
19868 if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) {
19869 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19870 << RHSValue.getDecl();
19871 return false;
19872 }
19873
19874 // C++11 [expr.eq]p2:
19875 // If both operands are null, they compare equal. Otherwise if only one is
19876 // null, they compare unequal.
19877 if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
19878 bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
19879 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19880 }
19881
19882 // Otherwise if either is a pointer to a virtual member function, the
19883 // result is unspecified.
19884 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: LHSValue.getDecl()))
19885 if (MD->isVirtual())
19886 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19887 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: RHSValue.getDecl()))
19888 if (MD->isVirtual())
19889 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19890
19891 // Otherwise they compare equal if and only if they would refer to the
19892 // same member of the same most derived object or the same subobject if
19893 // they were dereferenced with a hypothetical object of the associated
19894 // class type.
19895 bool Equal = LHSValue == RHSValue;
19896 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19897 }
19898
19899 if (LHSTy->isNullPtrType()) {
19900 assert(E->isComparisonOp() && "unexpected nullptr operation");
19901 assert(RHSTy->isNullPtrType() && "missing pointer conversion");
19902 // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
19903 // are compared, the result is true of the operator is <=, >= or ==, and
19904 // false otherwise.
19905 LValue Res;
19906 if (!EvaluatePointer(E: E->getLHS(), Result&: Res, Info) ||
19907 !EvaluatePointer(E: E->getRHS(), Result&: Res, Info))
19908 return false;
19909 return Success(CmpResult::Equal, E);
19910 }
19911
19912 if (LHSTy->isMetaInfoType() && RHSTy->isMetaInfoType()) {
19913 APValue LHSValue, RHSValue;
19914 llvm::FoldingSetNodeID LID, RID;
19915 if (!Evaluate(Result&: LHSValue, Info, E: E->getLHS()))
19916 return false;
19917 LHSValue.Profile(ID&: LID);
19918
19919 if (!Evaluate(Result&: RHSValue, Info, E: E->getRHS()))
19920 return false;
19921 RHSValue.Profile(ID&: RID);
19922
19923 if (LID == RID)
19924 return Success(CmpResult::Equal, E);
19925 else
19926 return Success(CmpResult::Unequal, E);
19927 }
19928
19929 return DoAfter();
19930}
19931
19932static bool EvaluateComparisonResult(EvalInfo &Info, const Expr *E,
19933 ComparisonCategoryResult CCR,
19934 APValue &Result) {
19935 const ComparisonCategoryInfo &CmpInfo =
19936 Info.Ctx.CompCategories.getInfoForType(Ty: E->getType());
19937 const VarDecl *VD = CmpInfo.getValueInfo(ValueKind: CmpInfo.makeWeakResult(Res: CCR))->VD;
19938
19939 // Check and evaluate the result as a constant expression.
19940 LValue LV;
19941 LV.set(B: VD);
19942 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
19943 return false;
19944 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
19945 Kind: ConstantExprKind::Normal);
19946}
19947
19948bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) {
19949 if (!CheckLiteralType(Info, E))
19950 return false;
19951
19952 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19953 ComparisonCategoryResult CCR;
19954 switch (CR) {
19955 case CmpResult::Unequal:
19956 llvm_unreachable("should never produce Unequal for three-way comparison");
19957 case CmpResult::Less:
19958 CCR = ComparisonCategoryResult::Less;
19959 break;
19960 case CmpResult::Equal:
19961 CCR = ComparisonCategoryResult::Equal;
19962 break;
19963 case CmpResult::Greater:
19964 CCR = ComparisonCategoryResult::Greater;
19965 break;
19966 case CmpResult::Unordered:
19967 CCR = ComparisonCategoryResult::Unordered;
19968 break;
19969 }
19970 return EvaluateComparisonResult(Info, E, CCR, Result);
19971 };
19972 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19973 return ExprEvaluatorBaseTy::VisitBinCmp(S: E);
19974 });
19975}
19976
19977bool RecordExprEvaluator::VisitTypeTraitExpr(const TypeTraitExpr *E) {
19978 if (!CheckLiteralType(Info, E))
19979 return false;
19980
19981 assert(E->isStoredAsComparisonResult() &&
19982 "expected a strong_ordering type trait with a stored value");
19983
19984 ComparisonCategoryResult CCR = static_cast<ComparisonCategoryResult>(
19985 E->getAPValue().getInt().getZExtValue());
19986 return EvaluateComparisonResult(Info, E, CCR, Result);
19987}
19988
19989bool RecordExprEvaluator::VisitCXXParenListInitExpr(
19990 const CXXParenListInitExpr *E) {
19991 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs());
19992}
19993
19994bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
19995 // We don't support assignment in C. C++ assignments don't get here because
19996 // assignment is an lvalue in C++.
19997 if (E->isAssignmentOp()) {
19998 Error(E);
19999 if (!Info.noteFailure())
20000 return false;
20001 }
20002
20003 if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E))
20004 return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E);
20005
20006 assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() ||
20007 !E->getRHS()->getType()->isIntegralOrEnumerationType()) &&
20008 "DataRecursiveIntBinOpEvaluator should have handled integral types");
20009
20010 if (E->isComparisonOp()) {
20011 // Evaluate builtin binary comparisons by evaluating them as three-way
20012 // comparisons and then translating the result.
20013 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
20014 assert((CR != CmpResult::Unequal || E->isEqualityOp()) &&
20015 "should only produce Unequal for equality comparisons");
20016 bool IsEqual = CR == CmpResult::Equal,
20017 IsLess = CR == CmpResult::Less,
20018 IsGreater = CR == CmpResult::Greater;
20019 auto Op = E->getOpcode();
20020 switch (Op) {
20021 default:
20022 llvm_unreachable("unsupported binary operator");
20023 case BO_EQ:
20024 case BO_NE:
20025 return Success(Value: IsEqual == (Op == BO_EQ), E);
20026 case BO_LT:
20027 return Success(Value: IsLess, E);
20028 case BO_GT:
20029 return Success(Value: IsGreater, E);
20030 case BO_LE:
20031 return Success(Value: IsEqual || IsLess, E);
20032 case BO_GE:
20033 return Success(Value: IsEqual || IsGreater, E);
20034 }
20035 };
20036 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
20037 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20038 });
20039 }
20040
20041 QualType LHSTy = E->getLHS()->getType();
20042 QualType RHSTy = E->getRHS()->getType();
20043
20044 if (LHSTy->isPointerType() && RHSTy->isPointerType() &&
20045 E->getOpcode() == BO_Sub) {
20046 LValue LHSValue, RHSValue;
20047
20048 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
20049 if (!LHSOK && !Info.noteFailure())
20050 return false;
20051
20052 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
20053 return false;
20054
20055 // Reject differing bases from the normal codepath; we special-case
20056 // comparisons to null.
20057 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
20058 if (Info.checkingPotentialConstantExpression() &&
20059 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
20060 return false;
20061
20062 const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>();
20063 const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>();
20064
20065 auto DiagArith = [&](unsigned DiagID) {
20066 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
20067 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
20068 Info.FFDiag(E, DiagId: DiagID) << LHS << RHS;
20069 if (LHSExpr && LHSExpr == RHSExpr)
20070 Info.Note(Loc: LHSExpr->getExprLoc(),
20071 DiagId: diag::note_constexpr_repeated_literal_eval)
20072 << LHSExpr->getSourceRange();
20073 return false;
20074 };
20075
20076 if (!LHSExpr || !RHSExpr)
20077 return DiagArith(diag::note_constexpr_pointer_arith_unspecified);
20078
20079 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
20080 return DiagArith(diag::note_constexpr_literal_arith);
20081
20082 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
20083 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
20084 if (!LHSAddrExpr || !RHSAddrExpr)
20085 return Error(E);
20086 // Make sure both labels come from the same function.
20087 if (LHSAddrExpr->getLabel()->getDeclContext() !=
20088 RHSAddrExpr->getLabel()->getDeclContext())
20089 return Error(E);
20090 return Success(V: APValue(LHSAddrExpr, RHSAddrExpr), E);
20091 }
20092 CharUnits LHSOffset = LHSValue.getLValueOffset();
20093 CharUnits RHSOffset = RHSValue.getLValueOffset();
20094
20095 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
20096 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
20097
20098 // C++11 [expr.add]p6:
20099 // Unless both pointers point to elements of the same array object, or
20100 // one past the last element of the array object, the behavior is
20101 // undefined.
20102 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
20103 !AreElementsOfSameArray(ObjType: getType(B: LHSValue.Base), A: LHSDesignator,
20104 B: RHSDesignator))
20105 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_not_same_array);
20106
20107 QualType Type = E->getLHS()->getType();
20108 QualType ElementType = Type->castAs<PointerType>()->getPointeeType();
20109
20110 CharUnits ElementSize;
20111 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElementType, Size&: ElementSize))
20112 return false;
20113
20114 // As an extension, a type may have zero size (empty struct or union in
20115 // C, array of zero length). Pointer subtraction in such cases has
20116 // undefined behavior, so is not constant.
20117 if (ElementSize.isZero()) {
20118 Info.FFDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_zero_size)
20119 << ElementType;
20120 return false;
20121 }
20122
20123 // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
20124 // and produce incorrect results when it overflows. Such behavior
20125 // appears to be non-conforming, but is common, so perhaps we should
20126 // assume the standard intended for such cases to be undefined behavior
20127 // and check for them.
20128
20129 // Compute (LHSOffset - RHSOffset) / Size carefully, checking for
20130 // overflow in the final conversion to ptrdiff_t.
20131 APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
20132 APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
20133 APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true),
20134 false);
20135 APSInt TrueResult = (LHS - RHS) / ElemSize;
20136 APSInt Result = TrueResult.trunc(width: Info.Ctx.getIntWidth(T: E->getType()));
20137
20138 if (Result.extend(width: 65) != TrueResult &&
20139 !HandleOverflow(Info, E, SrcValue: TrueResult, DestType: E->getType()))
20140 return false;
20141 return Success(SI: Result, E);
20142 }
20143
20144 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20145}
20146
20147/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
20148/// a result as the expression's type.
20149bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
20150 const UnaryExprOrTypeTraitExpr *E) {
20151 switch(E->getKind()) {
20152 case UETT_PreferredAlignOf:
20153 case UETT_AlignOf: {
20154 if (E->isArgumentType())
20155 return Success(
20156 Size: GetAlignOfType(Ctx: Info.Ctx, T: E->getArgumentType(), ExprKind: E->getKind()), E);
20157 else
20158 return Success(
20159 Size: GetAlignOfExpr(Ctx: Info.Ctx, E: E->getArgumentExpr(), ExprKind: E->getKind()), E);
20160 }
20161
20162 case UETT_PtrAuthTypeDiscriminator: {
20163 if (E->getArgumentType()->isDependentType())
20164 return false;
20165 return Success(
20166 Value: Info.Ctx.getPointerAuthTypeDiscriminator(T: E->getArgumentType()), E);
20167 }
20168 case UETT_VecStep: {
20169 QualType Ty = E->getTypeOfArgument();
20170
20171 if (Ty->isVectorType()) {
20172 unsigned n = Ty->castAs<VectorType>()->getNumElements();
20173
20174 // The vec_step built-in functions that take a 3-component
20175 // vector return 4. (OpenCL 1.1 spec 6.11.12)
20176 if (n == 3)
20177 n = 4;
20178
20179 return Success(Value: n, E);
20180 } else
20181 return Success(Value: 1, E);
20182 }
20183
20184 case UETT_DataSizeOf:
20185 case UETT_SizeOf: {
20186 QualType SrcTy = E->getTypeOfArgument();
20187 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
20188 // the result is the size of the referenced type."
20189 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
20190 SrcTy = Ref->getPointeeType();
20191
20192 CharUnits Sizeof;
20193 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: SrcTy, Size&: Sizeof,
20194 SOT: E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf
20195 : SizeOfType::SizeOf)) {
20196 return false;
20197 }
20198 return Success(Size: Sizeof, E);
20199 }
20200 case UETT_OpenMPRequiredSimdAlign:
20201 assert(E->isArgumentType());
20202 return Success(
20203 Value: Info.Ctx.toCharUnitsFromBits(
20204 BitSize: Info.Ctx.getOpenMPDefaultSimdAlign(T: E->getArgumentType()))
20205 .getQuantity(),
20206 E);
20207 case UETT_VectorElements: {
20208 QualType Ty = E->getTypeOfArgument();
20209 // If the vector has a fixed size, we can determine the number of elements
20210 // at compile time.
20211 if (const auto *VT = Ty->getAs<VectorType>())
20212 return Success(Value: VT->getNumElements(), E);
20213
20214 assert(Ty->isSizelessVectorType());
20215 if (Info.InConstantContext)
20216 Info.CCEDiag(E, DiagId: diag::note_constexpr_non_const_vectorelements)
20217 << E->getSourceRange();
20218
20219 return false;
20220 }
20221 case UETT_CountOf: {
20222 QualType Ty = E->getTypeOfArgument();
20223 assert(Ty->isArrayType());
20224
20225 // We don't need to worry about array element qualifiers, so getting the
20226 // unsafe array type is fine.
20227 if (const auto *CAT =
20228 dyn_cast<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe())) {
20229 return Success(I: CAT->getSize(), E);
20230 }
20231
20232 assert(!Ty->isConstantSizeType());
20233
20234 // If it's a variable-length array type, we need to check whether it is a
20235 // multidimensional array. If so, we need to check the size expression of
20236 // the VLA to see if it's a constant size. If so, we can return that value.
20237 const auto *VAT = Info.Ctx.getAsVariableArrayType(T: Ty);
20238 assert(VAT);
20239 if (VAT->getElementType()->isArrayType()) {
20240 // Variable array size expression could be missing (e.g. int a[*][10]) In
20241 // that case, it can't be a constant expression.
20242 if (!VAT->getSizeExpr()) {
20243 Info.FFDiag(Loc: E->getBeginLoc());
20244 return false;
20245 }
20246
20247 std::optional<APSInt> Res =
20248 VAT->getSizeExpr()->getIntegerConstantExpr(Ctx: Info.Ctx);
20249 if (Res) {
20250 // The resulting value always has type size_t, so we need to make the
20251 // returned APInt have the correct sign and bit-width.
20252 APInt Val{
20253 static_cast<unsigned>(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType())),
20254 Res->getZExtValue()};
20255 return Success(I: Val, E);
20256 }
20257 }
20258
20259 // Definitely a variable-length type, which is not an ICE.
20260 // FIXME: Better diagnostic.
20261 Info.FFDiag(Loc: E->getBeginLoc());
20262 return false;
20263 }
20264 }
20265
20266 llvm_unreachable("unknown expr/type trait");
20267}
20268
20269bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
20270 Info.Ctx.recordOffsetOfEvaluation(E: OOE);
20271 CharUnits Result;
20272 unsigned n = OOE->getNumComponents();
20273 if (n == 0)
20274 return Error(E: OOE);
20275 QualType CurrentType = OOE->getTypeSourceInfo()->getType();
20276 for (unsigned i = 0; i != n; ++i) {
20277 OffsetOfNode ON = OOE->getComponent(Idx: i);
20278 switch (ON.getKind()) {
20279 case OffsetOfNode::Array: {
20280 const Expr *Idx = OOE->getIndexExpr(Idx: ON.getArrayExprIndex());
20281 APSInt IdxResult;
20282 if (!EvaluateInteger(E: Idx, Result&: IdxResult, Info))
20283 return false;
20284 const ArrayType *AT = Info.Ctx.getAsArrayType(T: CurrentType);
20285 if (!AT)
20286 return Error(E: OOE);
20287 CurrentType = AT->getElementType();
20288 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(T: CurrentType);
20289 // Reject negative indices, indices too large to fit in int64_t,
20290 // and overflow in the offset computation.
20291 if (IdxResult.isNegative() || IdxResult.getActiveBits() > 63)
20292 return Error(E: OOE);
20293 int64_t IdxVal = IdxResult.getExtValue();
20294 int64_t ElemSize = ElementSize.getQuantity();
20295 if (IdxVal != 0 &&
20296 ElemSize > std::numeric_limits<int64_t>::max() / IdxVal)
20297 return Error(E: OOE, D: diag::note_constexpr_offsetof_overflow);
20298 int64_t Offset = IdxVal * ElemSize;
20299 if (Result.getQuantity() > std::numeric_limits<int64_t>::max() - Offset)
20300 return Error(E: OOE, D: diag::note_constexpr_offsetof_overflow);
20301 Result += CharUnits::fromQuantity(Quantity: Offset);
20302 break;
20303 }
20304
20305 case OffsetOfNode::Field: {
20306 FieldDecl *MemberDecl = ON.getField();
20307 const auto *RD = CurrentType->getAsRecordDecl();
20308 if (!RD)
20309 return Error(E: OOE);
20310 if (RD->isInvalidDecl()) return false;
20311 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
20312 unsigned i = MemberDecl->getFieldIndex();
20313 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
20314 Result += Info.Ctx.toCharUnitsFromBits(BitSize: RL.getFieldOffset(FieldNo: i));
20315 CurrentType = MemberDecl->getType().getNonReferenceType();
20316 break;
20317 }
20318
20319 case OffsetOfNode::Identifier:
20320 llvm_unreachable("dependent __builtin_offsetof");
20321
20322 case OffsetOfNode::Base: {
20323 CXXBaseSpecifier *BaseSpec = ON.getBase();
20324 if (BaseSpec->isVirtual())
20325 return Error(E: OOE);
20326
20327 // Find the layout of the class whose base we are looking into.
20328 const auto *RD = CurrentType->getAsCXXRecordDecl();
20329 if (!RD)
20330 return Error(E: OOE);
20331 if (RD->isInvalidDecl()) return false;
20332 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
20333
20334 // Find the base class itself.
20335 CurrentType = BaseSpec->getType();
20336 const auto *BaseRD = CurrentType->getAsCXXRecordDecl();
20337 if (!BaseRD)
20338 return Error(E: OOE);
20339
20340 // Add the offset to the base.
20341 Result += RL.getBaseClassOffset(Base: BaseRD);
20342 break;
20343 }
20344 }
20345 }
20346 return Success(Size: Result, E: OOE);
20347}
20348
20349bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20350 switch (E->getOpcode()) {
20351 default:
20352 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
20353 // See C99 6.6p3.
20354 return Error(E);
20355 case UO_Extension:
20356 // FIXME: Should extension allow i-c-e extension expressions in its scope?
20357 // If so, we could clear the diagnostic ID.
20358 return Visit(S: E->getSubExpr());
20359 case UO_Plus:
20360 // The result is just the value.
20361 return Visit(S: E->getSubExpr());
20362 case UO_Minus: {
20363 if (!Visit(S: E->getSubExpr()))
20364 return false;
20365 if (!Result.isInt()) return Error(E);
20366 const APSInt &Value = Result.getInt();
20367 if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() &&
20368 !E->getType().isWrapType()) {
20369 if (Info.checkingForUndefinedBehavior())
20370 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20371 DiagID: diag::warn_integer_constant_overflow)
20372 << toString(I: Value, Radix: 10, Signed: Value.isSigned(), /*formatAsCLiteral=*/false,
20373 /*UpperCase=*/true, /*InsertSeparators=*/true)
20374 << E->getType() << E->getSourceRange();
20375
20376 if (!HandleOverflow(Info, E, SrcValue: -Value.extend(width: Value.getBitWidth() + 1),
20377 DestType: E->getType()))
20378 return false;
20379 }
20380 return Success(SI: -Value, E);
20381 }
20382 case UO_Not: {
20383 if (!Visit(S: E->getSubExpr()))
20384 return false;
20385 if (!Result.isInt()) return Error(E);
20386 return Success(SI: ~Result.getInt(), E);
20387 }
20388 case UO_LNot: {
20389 bool bres;
20390 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
20391 return false;
20392 return Success(Value: !bres, E);
20393 }
20394 }
20395}
20396
20397/// HandleCast - This is used to evaluate implicit or explicit casts where the
20398/// result type is integer.
20399bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
20400 const Expr *SubExpr = E->getSubExpr();
20401 QualType DestType = E->getType();
20402 QualType SrcType = SubExpr->getType();
20403
20404 switch (E->getCastKind()) {
20405 case CK_BaseToDerived:
20406 case CK_DerivedToBase:
20407 case CK_UncheckedDerivedToBase:
20408 case CK_Dynamic:
20409 case CK_ToUnion:
20410 case CK_ArrayToPointerDecay:
20411 case CK_FunctionToPointerDecay:
20412 case CK_NullToPointer:
20413 case CK_NullToMemberPointer:
20414 case CK_BaseToDerivedMemberPointer:
20415 case CK_DerivedToBaseMemberPointer:
20416 case CK_ReinterpretMemberPointer:
20417 case CK_ConstructorConversion:
20418 case CK_IntegralToPointer:
20419 case CK_ToVoid:
20420 case CK_VectorSplat:
20421 case CK_IntegralToFloating:
20422 case CK_FloatingCast:
20423 case CK_CPointerToObjCPointerCast:
20424 case CK_BlockPointerToObjCPointerCast:
20425 case CK_AnyPointerToBlockPointerCast:
20426 case CK_ObjCObjectLValueCast:
20427 case CK_FloatingRealToComplex:
20428 case CK_FloatingComplexToReal:
20429 case CK_FloatingComplexCast:
20430 case CK_FloatingComplexToIntegralComplex:
20431 case CK_IntegralRealToComplex:
20432 case CK_IntegralComplexCast:
20433 case CK_IntegralComplexToFloatingComplex:
20434 case CK_BuiltinFnToFnPtr:
20435 case CK_ZeroToOCLOpaqueType:
20436 case CK_NonAtomicToAtomic:
20437 case CK_AddressSpaceConversion:
20438 case CK_IntToOCLSampler:
20439 case CK_FloatingToFixedPoint:
20440 case CK_FixedPointToFloating:
20441 case CK_FixedPointCast:
20442 case CK_IntegralToFixedPoint:
20443 case CK_MatrixCast:
20444 case CK_HLSLAggregateSplatCast:
20445 llvm_unreachable("invalid cast kind for integral value");
20446
20447 case CK_Dependent:
20448 case CK_LValueBitCast:
20449 case CK_ARCProduceObject:
20450 case CK_ARCConsumeObject:
20451 case CK_ARCReclaimReturnedObject:
20452 case CK_ARCExtendBlockObject:
20453 case CK_CopyAndAutoreleaseBlockObject:
20454 return Error(E);
20455
20456 case CK_UserDefinedConversion:
20457 case CK_LValueToRValue:
20458 case CK_AtomicToNonAtomic:
20459 case CK_NoOp:
20460 case CK_LValueToRValueBitCast:
20461 case CK_HLSLArrayRValue:
20462 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20463
20464 case CK_BitCast: {
20465 APValue Src;
20466 if (!Evaluate(Result&: Src, Info, E: SubExpr))
20467 return false;
20468 APValue Dst;
20469 if (!handleRValueToRValueBitCast(Info, DestValue&: Dst, SourceRValue: Src, BCE: E))
20470 return false;
20471 return Success(V: Dst, E);
20472 }
20473
20474 case CK_MemberPointerToBoolean:
20475 case CK_PointerToBoolean:
20476 case CK_IntegralToBoolean:
20477 case CK_FloatingToBoolean:
20478 case CK_BooleanToSignedIntegral:
20479 case CK_FloatingComplexToBoolean:
20480 case CK_IntegralComplexToBoolean: {
20481 bool BoolResult;
20482 if (!EvaluateAsBooleanCondition(E: SubExpr, Result&: BoolResult, Info))
20483 return false;
20484 uint64_t IntResult = BoolResult;
20485 if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral)
20486 IntResult = (uint64_t)-1;
20487 return Success(Value: IntResult, E);
20488 }
20489
20490 case CK_FixedPointToIntegral: {
20491 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SrcType));
20492 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
20493 return false;
20494 bool Overflowed;
20495 llvm::APSInt Result = Src.convertToInt(
20496 DstWidth: Info.Ctx.getIntWidth(T: DestType),
20497 DstSign: DestType->isSignedIntegerOrEnumerationType(), Overflow: &Overflowed);
20498 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Result, DestType))
20499 return false;
20500 return Success(SI: Result, E);
20501 }
20502
20503 case CK_FixedPointToBoolean: {
20504 // Unsigned padding does not affect this.
20505 APValue Val;
20506 if (!Evaluate(Result&: Val, Info, E: SubExpr))
20507 return false;
20508 return Success(Value: Val.getFixedPoint().getBoolValue(), E);
20509 }
20510
20511 case CK_IntegralCast: {
20512 if (!Visit(S: SubExpr))
20513 return false;
20514
20515 if (!Result.isInt()) {
20516 // Allow casts of address-of-label differences if they are no-ops
20517 // or narrowing, if the result is at least 32 bits wide.
20518 // (The narrowing case isn't actually guaranteed to
20519 // be constant-evaluatable except in some narrow cases which are hard
20520 // to detect here. We let it through on the assumption the user knows
20521 // what they are doing.)
20522 if (Result.isAddrLabelDiff()) {
20523 unsigned DestBits = Info.Ctx.getTypeSize(T: DestType);
20524 return DestBits >= 32 && DestBits <= Info.Ctx.getTypeSize(T: SrcType);
20525 }
20526 // Only allow casts of lvalues if they are lossless.
20527 return Info.Ctx.getTypeSize(T: DestType) == Info.Ctx.getTypeSize(T: SrcType);
20528 }
20529
20530 if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) {
20531 const auto *ED = DestType->getAsEnumDecl();
20532 // Check that the value is within the range of the enumeration values.
20533 //
20534 // This corressponds to [expr.static.cast]p10 which says:
20535 // A value of integral or enumeration type can be explicitly converted
20536 // to a complete enumeration type ... If the enumeration type does not
20537 // have a fixed underlying type, the value is unchanged if the original
20538 // value is within the range of the enumeration values ([dcl.enum]), and
20539 // otherwise, the behavior is undefined.
20540 //
20541 // This was resolved as part of DR2338 which has CD5 status.
20542 if (!ED->isFixed()) {
20543 llvm::APInt Min;
20544 llvm::APInt Max;
20545
20546 ED->getValueRange(Max, Min);
20547 --Max;
20548
20549 if (ED->getNumNegativeBits() &&
20550 (Max.slt(RHS: Result.getInt().getSExtValue()) ||
20551 Min.sgt(RHS: Result.getInt().getSExtValue())))
20552 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
20553 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getSExtValue()
20554 << Max.getSExtValue() << ED;
20555 else if (!ED->getNumNegativeBits() &&
20556 Max.ult(RHS: Result.getInt().getZExtValue()))
20557 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
20558 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getZExtValue()
20559 << Max.getZExtValue() << ED;
20560 }
20561 }
20562
20563 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType,
20564 Value: Result.getInt()), E);
20565 }
20566
20567 case CK_PointerToIntegral: {
20568 CCEDiag(E, D: diag::note_constexpr_invalid_cast_ptrtoint)
20569 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
20570 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
20571
20572 LValue LV;
20573 if (!EvaluatePointer(E: SubExpr, Result&: LV, Info))
20574 return false;
20575
20576 if (LV.getLValueBase()) {
20577 CCEDiag(E, D: diag::note_constexpr_has_lvalue) << E->getSourceRange();
20578 // Only allow based lvalue casts if they are lossless.
20579 // FIXME: Allow a larger integer size than the pointer size, and allow
20580 // narrowing back down to pointer width in subsequent integral casts.
20581 // FIXME: Check integer type's active bits, not its type size.
20582 if (Info.Ctx.getTypeSize(T: DestType) != Info.Ctx.getTypeSize(T: SrcType))
20583 return Error(E);
20584
20585 LV.Designator.setInvalid();
20586 LV.moveInto(V&: Result);
20587 return true;
20588 }
20589
20590 APSInt AsInt;
20591 APValue V;
20592 LV.moveInto(V);
20593 if (!V.toIntegralConstant(Result&: AsInt, SrcTy: SrcType, Ctx: Info.Ctx))
20594 llvm_unreachable("Can't cast this!");
20595
20596 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType, Value: AsInt), E);
20597 }
20598
20599 case CK_IntegralComplexToReal: {
20600 ComplexValue C;
20601 if (!EvaluateComplex(E: SubExpr, Res&: C, Info))
20602 return false;
20603 return Success(SI: C.getComplexIntReal(), E);
20604 }
20605
20606 case CK_FloatingToIntegral: {
20607 APFloat F(0.0);
20608 if (!EvaluateFloat(E: SubExpr, Result&: F, Info))
20609 return false;
20610
20611 APSInt Value;
20612 if (!HandleFloatToIntCast(Info, E, SrcType, Value: F, DestType, Result&: Value))
20613 return false;
20614 return Success(SI: Value, E);
20615 }
20616 case CK_HLSLVectorTruncation: {
20617 APValue Val;
20618 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
20619 return Error(E);
20620 return Success(V: Val.getVectorElt(I: 0), E);
20621 }
20622 case CK_HLSLMatrixTruncation: {
20623 APValue Val;
20624 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
20625 return Error(E);
20626 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), E);
20627 }
20628 case CK_HLSLElementwiseCast: {
20629 SmallVector<APValue> SrcVals;
20630 SmallVector<QualType> SrcTypes;
20631
20632 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: DestType, SrcVals, SrcTypes))
20633 return false;
20634
20635 // cast our single element
20636 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
20637 APValue ResultVal;
20638 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: DestType, Original: SrcVals[0],
20639 Result&: ResultVal))
20640 return false;
20641 return Success(V: ResultVal, E);
20642 }
20643 }
20644
20645 llvm_unreachable("unknown cast resulting in integral value");
20646}
20647
20648bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
20649 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20650 ComplexValue LV;
20651 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
20652 return false;
20653 if (!LV.isComplexInt())
20654 return Error(E);
20655 return Success(SI: LV.getComplexIntReal(), E);
20656 }
20657
20658 return Visit(S: E->getSubExpr());
20659}
20660
20661bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
20662 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
20663 ComplexValue LV;
20664 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
20665 return false;
20666 if (!LV.isComplexInt())
20667 return Error(E);
20668 return Success(SI: LV.getComplexIntImag(), E);
20669 }
20670
20671 VisitIgnoredValue(E: E->getSubExpr());
20672 return Success(Value: 0, E);
20673}
20674
20675bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
20676 return Success(Value: E->getPackLength(), E);
20677}
20678
20679bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
20680 return Success(Value: E->getValue(), E);
20681}
20682
20683bool IntExprEvaluator::VisitConceptSpecializationExpr(
20684 const ConceptSpecializationExpr *E) {
20685 return Success(Value: E->isSatisfied(), E);
20686}
20687
20688bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) {
20689 return Success(Value: E->isSatisfied(), E);
20690}
20691
20692bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20693 switch (E->getOpcode()) {
20694 default:
20695 // Invalid unary operators
20696 return Error(E);
20697 case UO_Plus:
20698 // The result is just the value.
20699 return Visit(S: E->getSubExpr());
20700 case UO_Minus: {
20701 if (!Visit(S: E->getSubExpr())) return false;
20702 if (!Result.isFixedPoint())
20703 return Error(E);
20704 bool Overflowed;
20705 APFixedPoint Negated = Result.getFixedPoint().negate(Overflow: &Overflowed);
20706 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Negated, DestType: E->getType()))
20707 return false;
20708 return Success(V: Negated, E);
20709 }
20710 case UO_LNot: {
20711 bool bres;
20712 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
20713 return false;
20714 return Success(Value: !bres, E);
20715 }
20716 }
20717}
20718
20719bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) {
20720 const Expr *SubExpr = E->getSubExpr();
20721 QualType DestType = E->getType();
20722 assert(DestType->isFixedPointType() &&
20723 "Expected destination type to be a fixed point type");
20724 auto DestFXSema = Info.Ctx.getFixedPointSemantics(Ty: DestType);
20725
20726 switch (E->getCastKind()) {
20727 case CK_FixedPointCast: {
20728 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
20729 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
20730 return false;
20731 bool Overflowed;
20732 APFixedPoint Result = Src.convert(DstSema: DestFXSema, Overflow: &Overflowed);
20733 if (Overflowed) {
20734 if (Info.checkingForUndefinedBehavior())
20735 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20736 DiagID: diag::warn_fixedpoint_constant_overflow)
20737 << Result.toString() << E->getType();
20738 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20739 return false;
20740 }
20741 return Success(V: Result, E);
20742 }
20743 case CK_IntegralToFixedPoint: {
20744 APSInt Src;
20745 if (!EvaluateInteger(E: SubExpr, Result&: Src, Info))
20746 return false;
20747
20748 bool Overflowed;
20749 APFixedPoint IntResult = APFixedPoint::getFromIntValue(
20750 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
20751
20752 if (Overflowed) {
20753 if (Info.checkingForUndefinedBehavior())
20754 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20755 DiagID: diag::warn_fixedpoint_constant_overflow)
20756 << IntResult.toString() << E->getType();
20757 if (!HandleOverflow(Info, E, SrcValue: IntResult, DestType: E->getType()))
20758 return false;
20759 }
20760
20761 return Success(V: IntResult, E);
20762 }
20763 case CK_FloatingToFixedPoint: {
20764 APFloat Src(0.0);
20765 if (!EvaluateFloat(E: SubExpr, Result&: Src, Info))
20766 return false;
20767
20768 bool Overflowed;
20769 APFixedPoint Result = APFixedPoint::getFromFloatValue(
20770 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
20771
20772 if (Overflowed) {
20773 if (Info.checkingForUndefinedBehavior())
20774 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20775 DiagID: diag::warn_fixedpoint_constant_overflow)
20776 << Result.toString() << E->getType();
20777 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20778 return false;
20779 }
20780
20781 return Success(V: Result, E);
20782 }
20783 case CK_NoOp:
20784 case CK_LValueToRValue:
20785 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20786 default:
20787 return Error(E);
20788 }
20789}
20790
20791bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20792 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20793 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20794
20795 const Expr *LHS = E->getLHS();
20796 const Expr *RHS = E->getRHS();
20797 FixedPointSemantics ResultFXSema =
20798 Info.Ctx.getFixedPointSemantics(Ty: E->getType());
20799
20800 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHS->getType()));
20801 if (!EvaluateFixedPointOrInteger(E: LHS, Result&: LHSFX, Info))
20802 return false;
20803 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHS->getType()));
20804 if (!EvaluateFixedPointOrInteger(E: RHS, Result&: RHSFX, Info))
20805 return false;
20806
20807 bool OpOverflow = false, ConversionOverflow = false;
20808 APFixedPoint Result(LHSFX.getSemantics());
20809 switch (E->getOpcode()) {
20810 case BO_Add: {
20811 Result = LHSFX.add(Other: RHSFX, Overflow: &OpOverflow)
20812 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20813 break;
20814 }
20815 case BO_Sub: {
20816 Result = LHSFX.sub(Other: RHSFX, Overflow: &OpOverflow)
20817 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20818 break;
20819 }
20820 case BO_Mul: {
20821 Result = LHSFX.mul(Other: RHSFX, Overflow: &OpOverflow)
20822 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20823 break;
20824 }
20825 case BO_Div: {
20826 if (RHSFX.getValue() == 0) {
20827 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero);
20828 return false;
20829 }
20830 Result = LHSFX.div(Other: RHSFX, Overflow: &OpOverflow)
20831 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20832 break;
20833 }
20834 case BO_Shl:
20835 case BO_Shr: {
20836 FixedPointSemantics LHSSema = LHSFX.getSemantics();
20837 llvm::APSInt RHSVal = RHSFX.getValue();
20838
20839 unsigned ShiftBW =
20840 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding();
20841 unsigned Amt = RHSVal.getLimitedValue(Limit: ShiftBW - 1);
20842 // Embedded-C 4.1.6.2.2:
20843 // The right operand must be nonnegative and less than the total number
20844 // of (nonpadding) bits of the fixed-point operand ...
20845 if (RHSVal.isNegative())
20846 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHSVal;
20847 else if (Amt != RHSVal)
20848 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
20849 << RHSVal << E->getType() << ShiftBW;
20850
20851 if (E->getOpcode() == BO_Shl)
20852 Result = LHSFX.shl(Amt, Overflow: &OpOverflow);
20853 else
20854 Result = LHSFX.shr(Amt, Overflow: &OpOverflow);
20855 break;
20856 }
20857 default:
20858 return false;
20859 }
20860 if (OpOverflow || ConversionOverflow) {
20861 if (Info.checkingForUndefinedBehavior())
20862 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20863 DiagID: diag::warn_fixedpoint_constant_overflow)
20864 << Result.toString() << E->getType();
20865 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20866 return false;
20867 }
20868 return Success(V: Result, E);
20869}
20870
20871//===----------------------------------------------------------------------===//
20872// Float Evaluation
20873//===----------------------------------------------------------------------===//
20874
20875namespace {
20876class FloatExprEvaluator
20877 : public ExprEvaluatorBase<FloatExprEvaluator> {
20878 APFloat &Result;
20879public:
20880 FloatExprEvaluator(EvalInfo &info, APFloat &result)
20881 : ExprEvaluatorBaseTy(info), Result(result) {}
20882
20883 bool Success(const APValue &V, const Expr *e) {
20884 Result = V.getFloat();
20885 return true;
20886 }
20887
20888 bool ZeroInitialization(const Expr *E) {
20889 Result = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
20890 return true;
20891 }
20892
20893 bool VisitCallExpr(const CallExpr *E);
20894
20895 bool VisitUnaryOperator(const UnaryOperator *E);
20896 bool VisitBinaryOperator(const BinaryOperator *E);
20897 bool VisitFloatingLiteral(const FloatingLiteral *E);
20898 bool VisitCastExpr(const CastExpr *E);
20899
20900 bool VisitUnaryReal(const UnaryOperator *E);
20901 bool VisitUnaryImag(const UnaryOperator *E);
20902
20903 // FIXME: Missing: array subscript of vector, member of vector
20904};
20905} // end anonymous namespace
20906
20907static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
20908 assert(!E->isValueDependent());
20909 assert(E->isPRValue() && E->getType()->isRealFloatingType());
20910 return FloatExprEvaluator(Info, Result).Visit(S: E);
20911}
20912
20913static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
20914 QualType ResultTy,
20915 const Expr *Arg,
20916 bool SNaN,
20917 llvm::APFloat &Result) {
20918 const StringLiteral *S = dyn_cast<StringLiteral>(Val: Arg->IgnoreParenCasts());
20919 if (!S || !S->isOrdinary())
20920 return false;
20921
20922 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(T: ResultTy);
20923
20924 llvm::APInt fill;
20925
20926 // Treat empty strings as if they were zero.
20927 if (S->getString().empty())
20928 fill = llvm::APInt(32, 0);
20929 else if (S->getString().getAsInteger(Radix: 0, Result&: fill))
20930 return false;
20931
20932 if (Context.getTargetInfo().isNan2008()) {
20933 if (SNaN)
20934 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20935 else
20936 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20937 } else {
20938 // Prior to IEEE 754-2008, architectures were allowed to choose whether
20939 // the first bit of their significand was set for qNaN or sNaN. MIPS chose
20940 // a different encoding to what became a standard in 2008, and for pre-
20941 // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as
20942 // sNaN. This is now known as "legacy NaN" encoding.
20943 if (SNaN)
20944 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20945 else
20946 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20947 }
20948
20949 return true;
20950}
20951
20952bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
20953 if (!IsConstantEvaluatedBuiltinCall(E))
20954 return ExprEvaluatorBaseTy::VisitCallExpr(E);
20955
20956 unsigned BuiltinOp = ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E);
20957
20958 switch (BuiltinOp) {
20959 default:
20960 return false;
20961
20962 case Builtin::BI__builtin_huge_val:
20963 case Builtin::BI__builtin_huge_valf:
20964 case Builtin::BI__builtin_huge_vall:
20965 case Builtin::BI__builtin_huge_valf16:
20966 case Builtin::BI__builtin_huge_valf128:
20967 case Builtin::BI__builtin_inf:
20968 case Builtin::BI__builtin_inff:
20969 case Builtin::BI__builtin_infl:
20970 case Builtin::BI__builtin_inff16:
20971 case Builtin::BI__builtin_inff128: {
20972 const llvm::fltSemantics &Sem =
20973 Info.Ctx.getFloatTypeSemantics(T: E->getType());
20974 Result = llvm::APFloat::getInf(Sem);
20975 return true;
20976 }
20977
20978 case Builtin::BI__builtin_nans:
20979 case Builtin::BI__builtin_nansf:
20980 case Builtin::BI__builtin_nansl:
20981 case Builtin::BI__builtin_nansf16:
20982 case Builtin::BI__builtin_nansf128:
20983 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20984 SNaN: true, Result))
20985 return Error(E);
20986 return true;
20987
20988 case Builtin::BI__builtin_nan:
20989 case Builtin::BI__builtin_nanf:
20990 case Builtin::BI__builtin_nanl:
20991 case Builtin::BI__builtin_nanf16:
20992 case Builtin::BI__builtin_nanf128:
20993 // If this is __builtin_nan() turn this into a nan, otherwise we
20994 // can't constant fold it.
20995 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20996 SNaN: false, Result))
20997 return Error(E);
20998 return true;
20999
21000 case Builtin::BI__builtin_elementwise_abs:
21001 case Builtin::BI__builtin_fabs:
21002 case Builtin::BI__builtin_fabsf:
21003 case Builtin::BI__builtin_fabsl:
21004 case Builtin::BI__builtin_fabsf128:
21005 // The C standard says "fabs raises no floating-point exceptions,
21006 // even if x is a signaling NaN. The returned value is independent of
21007 // the current rounding direction mode." Therefore constant folding can
21008 // proceed without regard to the floating point settings.
21009 // Reference, WG14 N2478 F.10.4.3
21010 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info))
21011 return false;
21012
21013 if (Result.isNegative())
21014 Result.changeSign();
21015 return true;
21016
21017 case Builtin::BI__arithmetic_fence:
21018 return EvaluateFloat(E: E->getArg(Arg: 0), Result, Info);
21019
21020 // FIXME: Builtin::BI__builtin_powi
21021 // FIXME: Builtin::BI__builtin_powif
21022 // FIXME: Builtin::BI__builtin_powil
21023
21024 case Builtin::BI__builtin_copysign:
21025 case Builtin::BI__builtin_copysignf:
21026 case Builtin::BI__builtin_copysignl:
21027 case Builtin::BI__builtin_copysignf128: {
21028 APFloat RHS(0.);
21029 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
21030 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
21031 return false;
21032 Result.copySign(RHS);
21033 return true;
21034 }
21035
21036 case Builtin::BI__builtin_fmax:
21037 case Builtin::BI__builtin_fmaxf:
21038 case Builtin::BI__builtin_fmaxl:
21039 case Builtin::BI__builtin_fmaxf16:
21040 case Builtin::BI__builtin_fmaxf128: {
21041 APFloat RHS(0.);
21042 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
21043 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
21044 return false;
21045 Result = maxnum(A: Result, B: RHS);
21046 return true;
21047 }
21048
21049 case Builtin::BI__builtin_fmin:
21050 case Builtin::BI__builtin_fminf:
21051 case Builtin::BI__builtin_fminl:
21052 case Builtin::BI__builtin_fminf16:
21053 case Builtin::BI__builtin_fminf128: {
21054 APFloat RHS(0.);
21055 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
21056 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
21057 return false;
21058 Result = minnum(A: Result, B: RHS);
21059 return true;
21060 }
21061
21062 case Builtin::BI__builtin_fmaximum_num:
21063 case Builtin::BI__builtin_fmaximum_numf:
21064 case Builtin::BI__builtin_fmaximum_numl:
21065 case Builtin::BI__builtin_fmaximum_numf16:
21066 case Builtin::BI__builtin_fmaximum_numf128: {
21067 APFloat RHS(0.);
21068 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
21069 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
21070 return false;
21071 Result = maximumnum(A: Result, B: RHS);
21072 return true;
21073 }
21074
21075 case Builtin::BI__builtin_fminimum_num:
21076 case Builtin::BI__builtin_fminimum_numf:
21077 case Builtin::BI__builtin_fminimum_numl:
21078 case Builtin::BI__builtin_fminimum_numf16:
21079 case Builtin::BI__builtin_fminimum_numf128: {
21080 APFloat RHS(0.);
21081 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
21082 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
21083 return false;
21084 Result = minimumnum(A: Result, B: RHS);
21085 return true;
21086 }
21087
21088 case Builtin::BI__builtin_elementwise_fma: {
21089 if (!E->getArg(Arg: 0)->isPRValue() || !E->getArg(Arg: 1)->isPRValue() ||
21090 !E->getArg(Arg: 2)->isPRValue()) {
21091 return false;
21092 }
21093 APFloat SourceY(0.), SourceZ(0.);
21094 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
21095 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: SourceY, Info) ||
21096 !EvaluateFloat(E: E->getArg(Arg: 2), Result&: SourceZ, Info))
21097 return false;
21098 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
21099 (void)Result.fusedMultiplyAdd(Multiplicand: SourceY, Addend: SourceZ, RM);
21100 return true;
21101 }
21102
21103 case clang::X86::BI__builtin_ia32_vec_ext_v4sf: {
21104 APValue Vec;
21105 APSInt IdxAPS;
21106 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
21107 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
21108 return false;
21109 unsigned N = Vec.getVectorLength();
21110 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
21111 return Success(V: Vec.getVectorElt(I: Idx), e: E);
21112 }
21113 }
21114}
21115
21116bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
21117 if (E->getSubExpr()->getType()->isAnyComplexType()) {
21118 ComplexValue CV;
21119 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
21120 return false;
21121 Result = CV.FloatReal;
21122 return true;
21123 }
21124
21125 return Visit(S: E->getSubExpr());
21126}
21127
21128bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
21129 if (E->getSubExpr()->getType()->isAnyComplexType()) {
21130 ComplexValue CV;
21131 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
21132 return false;
21133 Result = CV.FloatImag;
21134 return true;
21135 }
21136
21137 VisitIgnoredValue(E: E->getSubExpr());
21138 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(T: E->getType());
21139 Result = llvm::APFloat::getZero(Sem);
21140 return true;
21141}
21142
21143bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
21144 switch (E->getOpcode()) {
21145 default: return Error(E);
21146 case UO_Plus:
21147 return EvaluateFloat(E: E->getSubExpr(), Result, Info);
21148 case UO_Minus:
21149 // In C standard, WG14 N2478 F.3 p4
21150 // "the unary - raises no floating point exceptions,
21151 // even if the operand is signalling."
21152 if (!EvaluateFloat(E: E->getSubExpr(), Result, Info))
21153 return false;
21154 Result.changeSign();
21155 return true;
21156 }
21157}
21158
21159bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
21160 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
21161 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
21162
21163 APFloat RHS(0.0);
21164 bool LHSOK = EvaluateFloat(E: E->getLHS(), Result, Info);
21165 if (!LHSOK && !Info.noteFailure())
21166 return false;
21167 return EvaluateFloat(E: E->getRHS(), Result&: RHS, Info) && LHSOK &&
21168 handleFloatFloatBinOp(Info, E, LHS&: Result, Opcode: E->getOpcode(), RHS);
21169}
21170
21171bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
21172 Result = E->getValue();
21173 return true;
21174}
21175
21176bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
21177 const Expr* SubExpr = E->getSubExpr();
21178
21179 switch (E->getCastKind()) {
21180 default:
21181 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21182
21183 case CK_HLSLAggregateSplatCast:
21184 llvm_unreachable("invalid cast kind for floating value");
21185
21186 case CK_IntegralToFloating: {
21187 APSInt IntResult;
21188 const FPOptions FPO = E->getFPFeaturesInEffect(
21189 LO: Info.Ctx.getLangOpts());
21190 return EvaluateInteger(E: SubExpr, Result&: IntResult, Info) &&
21191 HandleIntToFloatCast(Info, E, FPO, SrcType: SubExpr->getType(),
21192 Value: IntResult, DestType: E->getType(), Result);
21193 }
21194
21195 case CK_FixedPointToFloating: {
21196 APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
21197 if (!EvaluateFixedPoint(E: SubExpr, Result&: FixResult, Info))
21198 return false;
21199 Result =
21200 FixResult.convertToFloat(FloatSema: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
21201 return true;
21202 }
21203
21204 case CK_FloatingCast: {
21205 if (!Visit(S: SubExpr))
21206 return false;
21207 return HandleFloatToFloatCast(Info, E, SrcType: SubExpr->getType(), DestType: E->getType(),
21208 Result);
21209 }
21210
21211 case CK_FloatingComplexToReal: {
21212 ComplexValue V;
21213 if (!EvaluateComplex(E: SubExpr, Res&: V, Info))
21214 return false;
21215 Result = V.getComplexFloatReal();
21216 return true;
21217 }
21218 case CK_HLSLVectorTruncation: {
21219 APValue Val;
21220 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
21221 return Error(E);
21222 return Success(V: Val.getVectorElt(I: 0), e: E);
21223 }
21224 case CK_HLSLMatrixTruncation: {
21225 APValue Val;
21226 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
21227 return Error(E);
21228 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), e: E);
21229 }
21230 case CK_HLSLElementwiseCast: {
21231 SmallVector<APValue> SrcVals;
21232 SmallVector<QualType> SrcTypes;
21233
21234 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: E->getType(), SrcVals,
21235 SrcTypes))
21236 return false;
21237
21238 // Cast our single element.
21239 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
21240 APValue ResultVal;
21241 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: E->getType(), Original: SrcVals[0],
21242 Result&: ResultVal))
21243 return false;
21244 return Success(V: ResultVal, e: E);
21245 }
21246 }
21247}
21248
21249//===----------------------------------------------------------------------===//
21250// Complex Evaluation (for float and integer)
21251//===----------------------------------------------------------------------===//
21252
21253namespace {
21254class ComplexExprEvaluator
21255 : public ExprEvaluatorBase<ComplexExprEvaluator> {
21256 ComplexValue &Result;
21257
21258public:
21259 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
21260 : ExprEvaluatorBaseTy(info), Result(Result) {}
21261
21262 bool Success(const APValue &V, const Expr *e) {
21263 Result.setFrom(V);
21264 return true;
21265 }
21266
21267 bool ZeroInitialization(const Expr *E);
21268
21269 //===--------------------------------------------------------------------===//
21270 // Visitor Methods
21271 //===--------------------------------------------------------------------===//
21272
21273 bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
21274 bool VisitCastExpr(const CastExpr *E);
21275 bool VisitBinaryOperator(const BinaryOperator *E);
21276 bool VisitUnaryOperator(const UnaryOperator *E);
21277 bool VisitInitListExpr(const InitListExpr *E);
21278 bool VisitCallExpr(const CallExpr *E);
21279};
21280} // end anonymous namespace
21281
21282static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
21283 EvalInfo &Info) {
21284 assert(!E->isValueDependent());
21285 assert(E->isPRValue() && E->getType()->isAnyComplexType());
21286 return ComplexExprEvaluator(Info, Result).Visit(S: E);
21287}
21288
21289bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
21290 QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();
21291 if (ElemTy->isRealFloatingType()) {
21292 Result.makeComplexFloat();
21293 APFloat Zero = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy));
21294 Result.FloatReal = Zero;
21295 Result.FloatImag = Zero;
21296 } else {
21297 Result.makeComplexInt();
21298 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy);
21299 Result.IntReal = Zero;
21300 Result.IntImag = Zero;
21301 }
21302 return true;
21303}
21304
21305bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
21306 const Expr* SubExpr = E->getSubExpr();
21307
21308 if (SubExpr->getType()->isRealFloatingType()) {
21309 Result.makeComplexFloat();
21310 APFloat &Imag = Result.FloatImag;
21311 if (!EvaluateFloat(E: SubExpr, Result&: Imag, Info))
21312 return false;
21313
21314 Result.FloatReal = APFloat(Imag.getSemantics());
21315 return true;
21316 } else {
21317 assert(SubExpr->getType()->isIntegerType() &&
21318 "Unexpected imaginary literal.");
21319
21320 Result.makeComplexInt();
21321 APSInt &Imag = Result.IntImag;
21322 if (!EvaluateInteger(E: SubExpr, Result&: Imag, Info))
21323 return false;
21324
21325 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
21326 return true;
21327 }
21328}
21329
21330bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
21331
21332 switch (E->getCastKind()) {
21333 case CK_BitCast:
21334 case CK_BaseToDerived:
21335 case CK_DerivedToBase:
21336 case CK_UncheckedDerivedToBase:
21337 case CK_Dynamic:
21338 case CK_ToUnion:
21339 case CK_ArrayToPointerDecay:
21340 case CK_FunctionToPointerDecay:
21341 case CK_NullToPointer:
21342 case CK_NullToMemberPointer:
21343 case CK_BaseToDerivedMemberPointer:
21344 case CK_DerivedToBaseMemberPointer:
21345 case CK_MemberPointerToBoolean:
21346 case CK_ReinterpretMemberPointer:
21347 case CK_ConstructorConversion:
21348 case CK_IntegralToPointer:
21349 case CK_PointerToIntegral:
21350 case CK_PointerToBoolean:
21351 case CK_ToVoid:
21352 case CK_VectorSplat:
21353 case CK_IntegralCast:
21354 case CK_BooleanToSignedIntegral:
21355 case CK_IntegralToBoolean:
21356 case CK_IntegralToFloating:
21357 case CK_FloatingToIntegral:
21358 case CK_FloatingToBoolean:
21359 case CK_FloatingCast:
21360 case CK_CPointerToObjCPointerCast:
21361 case CK_BlockPointerToObjCPointerCast:
21362 case CK_AnyPointerToBlockPointerCast:
21363 case CK_ObjCObjectLValueCast:
21364 case CK_FloatingComplexToReal:
21365 case CK_FloatingComplexToBoolean:
21366 case CK_IntegralComplexToReal:
21367 case CK_IntegralComplexToBoolean:
21368 case CK_ARCProduceObject:
21369 case CK_ARCConsumeObject:
21370 case CK_ARCReclaimReturnedObject:
21371 case CK_ARCExtendBlockObject:
21372 case CK_CopyAndAutoreleaseBlockObject:
21373 case CK_BuiltinFnToFnPtr:
21374 case CK_ZeroToOCLOpaqueType:
21375 case CK_NonAtomicToAtomic:
21376 case CK_AddressSpaceConversion:
21377 case CK_IntToOCLSampler:
21378 case CK_FloatingToFixedPoint:
21379 case CK_FixedPointToFloating:
21380 case CK_FixedPointCast:
21381 case CK_FixedPointToBoolean:
21382 case CK_FixedPointToIntegral:
21383 case CK_IntegralToFixedPoint:
21384 case CK_MatrixCast:
21385 case CK_HLSLVectorTruncation:
21386 case CK_HLSLMatrixTruncation:
21387 case CK_HLSLElementwiseCast:
21388 case CK_HLSLAggregateSplatCast:
21389 llvm_unreachable("invalid cast kind for complex value");
21390
21391 case CK_LValueToRValue:
21392 case CK_AtomicToNonAtomic:
21393 case CK_NoOp:
21394 case CK_LValueToRValueBitCast:
21395 case CK_HLSLArrayRValue:
21396 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21397
21398 case CK_Dependent:
21399 case CK_LValueBitCast:
21400 case CK_UserDefinedConversion:
21401 return Error(E);
21402
21403 case CK_FloatingRealToComplex: {
21404 APFloat &Real = Result.FloatReal;
21405 if (!EvaluateFloat(E: E->getSubExpr(), Result&: Real, Info))
21406 return false;
21407
21408 Result.makeComplexFloat();
21409 Result.FloatImag = APFloat(Real.getSemantics());
21410 return true;
21411 }
21412
21413 case CK_FloatingComplexCast: {
21414 if (!Visit(S: E->getSubExpr()))
21415 return false;
21416
21417 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21418 QualType From
21419 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21420
21421 return HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatReal) &&
21422 HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatImag);
21423 }
21424
21425 case CK_FloatingComplexToIntegralComplex: {
21426 if (!Visit(S: E->getSubExpr()))
21427 return false;
21428
21429 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21430 QualType From
21431 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21432 Result.makeComplexInt();
21433 return HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatReal,
21434 DestType: To, Result&: Result.IntReal) &&
21435 HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatImag,
21436 DestType: To, Result&: Result.IntImag);
21437 }
21438
21439 case CK_IntegralRealToComplex: {
21440 APSInt &Real = Result.IntReal;
21441 if (!EvaluateInteger(E: E->getSubExpr(), Result&: Real, Info))
21442 return false;
21443
21444 Result.makeComplexInt();
21445 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
21446 return true;
21447 }
21448
21449 case CK_IntegralComplexCast: {
21450 if (!Visit(S: E->getSubExpr()))
21451 return false;
21452
21453 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21454 QualType From
21455 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21456
21457 Result.IntReal = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntReal);
21458 Result.IntImag = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntImag);
21459 return true;
21460 }
21461
21462 case CK_IntegralComplexToFloatingComplex: {
21463 if (!Visit(S: E->getSubExpr()))
21464 return false;
21465
21466 const FPOptions FPO = E->getFPFeaturesInEffect(
21467 LO: Info.Ctx.getLangOpts());
21468 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21469 QualType From
21470 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21471 Result.makeComplexFloat();
21472 return HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntReal,
21473 DestType: To, Result&: Result.FloatReal) &&
21474 HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntImag,
21475 DestType: To, Result&: Result.FloatImag);
21476 }
21477 }
21478
21479 llvm_unreachable("unknown cast resulting in complex value");
21480}
21481
21482uint8_t GFNIMultiplicativeInverse(uint8_t Byte) {
21483 // Lookup Table for Multiplicative Inverse in GF(2^8)
21484 const uint8_t GFInv[256] = {
21485 0x00, 0x01, 0x8d, 0xf6, 0xcb, 0x52, 0x7b, 0xd1, 0xe8, 0x4f, 0x29, 0xc0,
21486 0xb0, 0xe1, 0xe5, 0xc7, 0x74, 0xb4, 0xaa, 0x4b, 0x99, 0x2b, 0x60, 0x5f,
21487 0x58, 0x3f, 0xfd, 0xcc, 0xff, 0x40, 0xee, 0xb2, 0x3a, 0x6e, 0x5a, 0xf1,
21488 0x55, 0x4d, 0xa8, 0xc9, 0xc1, 0x0a, 0x98, 0x15, 0x30, 0x44, 0xa2, 0xc2,
21489 0x2c, 0x45, 0x92, 0x6c, 0xf3, 0x39, 0x66, 0x42, 0xf2, 0x35, 0x20, 0x6f,
21490 0x77, 0xbb, 0x59, 0x19, 0x1d, 0xfe, 0x37, 0x67, 0x2d, 0x31, 0xf5, 0x69,
21491 0xa7, 0x64, 0xab, 0x13, 0x54, 0x25, 0xe9, 0x09, 0xed, 0x5c, 0x05, 0xca,
21492 0x4c, 0x24, 0x87, 0xbf, 0x18, 0x3e, 0x22, 0xf0, 0x51, 0xec, 0x61, 0x17,
21493 0x16, 0x5e, 0xaf, 0xd3, 0x49, 0xa6, 0x36, 0x43, 0xf4, 0x47, 0x91, 0xdf,
21494 0x33, 0x93, 0x21, 0x3b, 0x79, 0xb7, 0x97, 0x85, 0x10, 0xb5, 0xba, 0x3c,
21495 0xb6, 0x70, 0xd0, 0x06, 0xa1, 0xfa, 0x81, 0x82, 0x83, 0x7e, 0x7f, 0x80,
21496 0x96, 0x73, 0xbe, 0x56, 0x9b, 0x9e, 0x95, 0xd9, 0xf7, 0x02, 0xb9, 0xa4,
21497 0xde, 0x6a, 0x32, 0x6d, 0xd8, 0x8a, 0x84, 0x72, 0x2a, 0x14, 0x9f, 0x88,
21498 0xf9, 0xdc, 0x89, 0x9a, 0xfb, 0x7c, 0x2e, 0xc3, 0x8f, 0xb8, 0x65, 0x48,
21499 0x26, 0xc8, 0x12, 0x4a, 0xce, 0xe7, 0xd2, 0x62, 0x0c, 0xe0, 0x1f, 0xef,
21500 0x11, 0x75, 0x78, 0x71, 0xa5, 0x8e, 0x76, 0x3d, 0xbd, 0xbc, 0x86, 0x57,
21501 0x0b, 0x28, 0x2f, 0xa3, 0xda, 0xd4, 0xe4, 0x0f, 0xa9, 0x27, 0x53, 0x04,
21502 0x1b, 0xfc, 0xac, 0xe6, 0x7a, 0x07, 0xae, 0x63, 0xc5, 0xdb, 0xe2, 0xea,
21503 0x94, 0x8b, 0xc4, 0xd5, 0x9d, 0xf8, 0x90, 0x6b, 0xb1, 0x0d, 0xd6, 0xeb,
21504 0xc6, 0x0e, 0xcf, 0xad, 0x08, 0x4e, 0xd7, 0xe3, 0x5d, 0x50, 0x1e, 0xb3,
21505 0x5b, 0x23, 0x38, 0x34, 0x68, 0x46, 0x03, 0x8c, 0xdd, 0x9c, 0x7d, 0xa0,
21506 0xcd, 0x1a, 0x41, 0x1c};
21507
21508 return GFInv[Byte];
21509}
21510
21511uint8_t GFNIAffine(uint8_t XByte, const APInt &AQword, const APSInt &Imm,
21512 bool Inverse) {
21513 unsigned NumBitsInByte = 8;
21514 // Computing the affine transformation
21515 uint8_t RetByte = 0;
21516 for (uint32_t BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
21517 uint8_t AByte =
21518 AQword.lshr(shiftAmt: (7 - static_cast<int32_t>(BitIdx)) * NumBitsInByte)
21519 .getLoBits(numBits: 8)
21520 .getZExtValue();
21521 uint8_t Product;
21522 if (Inverse) {
21523 Product = AByte & GFNIMultiplicativeInverse(Byte: XByte);
21524 } else {
21525 Product = AByte & XByte;
21526 }
21527 uint8_t Parity = 0;
21528
21529 // Dot product in GF(2) uses XOR instead of addition
21530 for (unsigned PBitIdx = 0; PBitIdx != NumBitsInByte; ++PBitIdx) {
21531 Parity = Parity ^ ((Product >> PBitIdx) & 0x1);
21532 }
21533
21534 uint8_t Temp = Imm[BitIdx] ? 1 : 0;
21535 RetByte |= (Temp ^ Parity) << BitIdx;
21536 }
21537 return RetByte;
21538}
21539
21540uint8_t GFNIMul(uint8_t AByte, uint8_t BByte) {
21541 // Multiplying two polynomials of degree 7
21542 // Polynomial of degree 7
21543 // x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
21544 uint16_t TWord = 0;
21545 unsigned NumBitsInByte = 8;
21546 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
21547 if ((BByte >> BitIdx) & 0x1) {
21548 TWord = TWord ^ (AByte << BitIdx);
21549 }
21550 }
21551
21552 // When multiplying two polynomials of degree 7
21553 // results in a polynomial of degree 14
21554 // so the result has to be reduced to 7
21555 // Reduction polynomial is x^8 + x^4 + x^3 + x + 1 i.e. 0x11B
21556 for (int32_t BitIdx = 14; BitIdx > 7; --BitIdx) {
21557 if ((TWord >> BitIdx) & 0x1) {
21558 TWord = TWord ^ (0x11B << (BitIdx - 8));
21559 }
21560 }
21561 return (TWord & 0xFF);
21562}
21563
21564void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D,
21565 APFloat &ResR, APFloat &ResI) {
21566 // This is an implementation of complex multiplication according to the
21567 // constraints laid out in C11 Annex G. The implementation uses the
21568 // following naming scheme:
21569 // (a + ib) * (c + id)
21570
21571 APFloat AC = A * C;
21572 APFloat BD = B * D;
21573 APFloat AD = A * D;
21574 APFloat BC = B * C;
21575 ResR = AC - BD;
21576 ResI = AD + BC;
21577 if (ResR.isNaN() && ResI.isNaN()) {
21578 bool Recalc = false;
21579 if (A.isInfinity() || B.isInfinity()) {
21580 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
21581 Sign: A);
21582 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
21583 Sign: B);
21584 if (C.isNaN())
21585 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
21586 if (D.isNaN())
21587 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
21588 Recalc = true;
21589 }
21590 if (C.isInfinity() || D.isInfinity()) {
21591 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
21592 Sign: C);
21593 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
21594 Sign: D);
21595 if (A.isNaN())
21596 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
21597 if (B.isNaN())
21598 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
21599 Recalc = true;
21600 }
21601 if (!Recalc && (AC.isInfinity() || BD.isInfinity() || AD.isInfinity() ||
21602 BC.isInfinity())) {
21603 if (A.isNaN())
21604 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
21605 if (B.isNaN())
21606 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
21607 if (C.isNaN())
21608 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
21609 if (D.isNaN())
21610 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
21611 Recalc = true;
21612 }
21613 if (Recalc) {
21614 ResR = APFloat::getInf(Sem: A.getSemantics()) * (A * C - B * D);
21615 ResI = APFloat::getInf(Sem: A.getSemantics()) * (A * D + B * C);
21616 }
21617 }
21618}
21619
21620void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D,
21621 APFloat &ResR, APFloat &ResI) {
21622 // This is an implementation of complex division according to the
21623 // constraints laid out in C11 Annex G. The implementation uses the
21624 // following naming scheme:
21625 // (a + ib) / (c + id)
21626
21627 int DenomLogB = 0;
21628 APFloat MaxCD = maxnum(A: abs(X: C), B: abs(X: D));
21629 if (MaxCD.isFinite()) {
21630 DenomLogB = ilogb(Arg: MaxCD);
21631 C = scalbn(X: C, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21632 D = scalbn(X: D, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21633 }
21634 APFloat Denom = C * C + D * D;
21635 ResR =
21636 scalbn(X: (A * C + B * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21637 ResI =
21638 scalbn(X: (B * C - A * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21639 if (ResR.isNaN() && ResI.isNaN()) {
21640 if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) {
21641 ResR = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * A;
21642 ResI = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * B;
21643 } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() &&
21644 D.isFinite()) {
21645 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
21646 Sign: A);
21647 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
21648 Sign: B);
21649 ResR = APFloat::getInf(Sem: ResR.getSemantics()) * (A * C + B * D);
21650 ResI = APFloat::getInf(Sem: ResI.getSemantics()) * (B * C - A * D);
21651 } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) {
21652 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
21653 Sign: C);
21654 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
21655 Sign: D);
21656 ResR = APFloat::getZero(Sem: ResR.getSemantics()) * (A * C + B * D);
21657 ResI = APFloat::getZero(Sem: ResI.getSemantics()) * (B * C - A * D);
21658 }
21659 }
21660}
21661
21662APSInt NormalizeRotateAmount(const APSInt &Value, const APSInt &Amount) {
21663 // Normalize shift amount to [0, BitWidth) range to match runtime behavior
21664 APSInt NormAmt = Amount;
21665 unsigned BitWidth = Value.getBitWidth();
21666 unsigned AmtBitWidth = NormAmt.getBitWidth();
21667 if (BitWidth == 1) {
21668 // Rotating a 1-bit value is always a no-op
21669 NormAmt = APSInt(APInt(AmtBitWidth, 0), NormAmt.isUnsigned());
21670 } else if (BitWidth == 2) {
21671 // For 2-bit values: rotation amount is 0 or 1 based on
21672 // whether the amount is even or odd. We can't use srem here because
21673 // the divisor (2) would be misinterpreted as -2 in 2-bit signed arithmetic.
21674 NormAmt =
21675 APSInt(APInt(AmtBitWidth, NormAmt[0] ? 1 : 0), NormAmt.isUnsigned());
21676 } else {
21677 APInt Divisor;
21678 if (AmtBitWidth > BitWidth) {
21679 Divisor = llvm::APInt(AmtBitWidth, BitWidth);
21680 } else {
21681 Divisor = llvm::APInt(BitWidth, BitWidth);
21682 if (AmtBitWidth < BitWidth) {
21683 NormAmt = NormAmt.extend(width: BitWidth);
21684 }
21685 }
21686
21687 // Normalize to [0, BitWidth)
21688 if (NormAmt.isSigned()) {
21689 NormAmt = APSInt(NormAmt.srem(RHS: Divisor), /*isUnsigned=*/false);
21690 if (NormAmt.isNegative()) {
21691 APSInt SignedDivisor(Divisor, /*isUnsigned=*/false);
21692 NormAmt += SignedDivisor;
21693 }
21694 } else {
21695 NormAmt = APSInt(NormAmt.urem(RHS: Divisor), /*isUnsigned=*/true);
21696 }
21697 }
21698
21699 return NormAmt;
21700}
21701
21702bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
21703 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
21704 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
21705
21706 // Track whether the LHS or RHS is real at the type system level. When this is
21707 // the case we can simplify our evaluation strategy.
21708 bool LHSReal = false, RHSReal = false;
21709
21710 bool LHSOK;
21711 if (E->getLHS()->getType()->isRealFloatingType()) {
21712 LHSReal = true;
21713 APFloat &Real = Result.FloatReal;
21714 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: Real, Info);
21715 if (LHSOK) {
21716 Result.makeComplexFloat();
21717 Result.FloatImag = APFloat(Real.getSemantics());
21718 }
21719 } else {
21720 LHSOK = Visit(S: E->getLHS());
21721 }
21722 if (!LHSOK && !Info.noteFailure())
21723 return false;
21724
21725 ComplexValue RHS;
21726 if (E->getRHS()->getType()->isRealFloatingType()) {
21727 RHSReal = true;
21728 APFloat &Real = RHS.FloatReal;
21729 if (!EvaluateFloat(E: E->getRHS(), Result&: Real, Info) || !LHSOK)
21730 return false;
21731 RHS.makeComplexFloat();
21732 RHS.FloatImag = APFloat(Real.getSemantics());
21733 } else if (!EvaluateComplex(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
21734 return false;
21735
21736 assert(!(LHSReal && RHSReal) &&
21737 "Cannot have both operands of a complex operation be real.");
21738 switch (E->getOpcode()) {
21739 default: return Error(E);
21740 case BO_Add:
21741 if (Result.isComplexFloat()) {
21742 Result.getComplexFloatReal().add(RHS: RHS.getComplexFloatReal(),
21743 RM: APFloat::rmNearestTiesToEven);
21744 if (LHSReal)
21745 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
21746 else if (!RHSReal)
21747 Result.getComplexFloatImag().add(RHS: RHS.getComplexFloatImag(),
21748 RM: APFloat::rmNearestTiesToEven);
21749 } else {
21750 Result.getComplexIntReal() += RHS.getComplexIntReal();
21751 Result.getComplexIntImag() += RHS.getComplexIntImag();
21752 }
21753 break;
21754 case BO_Sub:
21755 if (Result.isComplexFloat()) {
21756 Result.getComplexFloatReal().subtract(RHS: RHS.getComplexFloatReal(),
21757 RM: APFloat::rmNearestTiesToEven);
21758 if (LHSReal) {
21759 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
21760 Result.getComplexFloatImag().changeSign();
21761 } else if (!RHSReal) {
21762 Result.getComplexFloatImag().subtract(RHS: RHS.getComplexFloatImag(),
21763 RM: APFloat::rmNearestTiesToEven);
21764 }
21765 } else {
21766 Result.getComplexIntReal() -= RHS.getComplexIntReal();
21767 Result.getComplexIntImag() -= RHS.getComplexIntImag();
21768 }
21769 break;
21770 case BO_Mul:
21771 if (Result.isComplexFloat()) {
21772 // This is an implementation of complex multiplication according to the
21773 // constraints laid out in C11 Annex G. The implementation uses the
21774 // following naming scheme:
21775 // (a + ib) * (c + id)
21776 ComplexValue LHS = Result;
21777 APFloat &A = LHS.getComplexFloatReal();
21778 APFloat &B = LHS.getComplexFloatImag();
21779 APFloat &C = RHS.getComplexFloatReal();
21780 APFloat &D = RHS.getComplexFloatImag();
21781 APFloat &ResR = Result.getComplexFloatReal();
21782 APFloat &ResI = Result.getComplexFloatImag();
21783 if (LHSReal) {
21784 assert(!RHSReal && "Cannot have two real operands for a complex op!");
21785 ResR = A;
21786 ResI = A;
21787 // ResR = A * C;
21788 // ResI = A * D;
21789 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: C) ||
21790 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: D))
21791 return false;
21792 } else if (RHSReal) {
21793 // ResR = C * A;
21794 // ResI = C * B;
21795 ResR = C;
21796 ResI = C;
21797 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: A) ||
21798 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: B))
21799 return false;
21800 } else {
21801 HandleComplexComplexMul(A, B, C, D, ResR, ResI);
21802 }
21803 } else {
21804 ComplexValue LHS = Result;
21805 Result.getComplexIntReal() =
21806 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
21807 LHS.getComplexIntImag() * RHS.getComplexIntImag());
21808 Result.getComplexIntImag() =
21809 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
21810 LHS.getComplexIntImag() * RHS.getComplexIntReal());
21811 }
21812 break;
21813 case BO_Div:
21814 if (Result.isComplexFloat()) {
21815 // This is an implementation of complex division according to the
21816 // constraints laid out in C11 Annex G. The implementation uses the
21817 // following naming scheme:
21818 // (a + ib) / (c + id)
21819 ComplexValue LHS = Result;
21820 APFloat &A = LHS.getComplexFloatReal();
21821 APFloat &B = LHS.getComplexFloatImag();
21822 APFloat &C = RHS.getComplexFloatReal();
21823 APFloat &D = RHS.getComplexFloatImag();
21824 APFloat &ResR = Result.getComplexFloatReal();
21825 APFloat &ResI = Result.getComplexFloatImag();
21826 if (RHSReal) {
21827 ResR = A;
21828 ResI = B;
21829 // ResR = A / C;
21830 // ResI = B / C;
21831 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Div, RHS: C) ||
21832 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Div, RHS: C))
21833 return false;
21834 } else {
21835 if (LHSReal) {
21836 // No real optimizations we can do here, stub out with zero.
21837 B = APFloat::getZero(Sem: A.getSemantics());
21838 }
21839 HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
21840 }
21841 } else {
21842 ComplexValue LHS = Result;
21843 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
21844 RHS.getComplexIntImag() * RHS.getComplexIntImag();
21845 if (Den.isZero())
21846 return Error(E, D: diag::note_expr_divide_by_zero);
21847
21848 Result.getComplexIntReal() =
21849 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
21850 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
21851 Result.getComplexIntImag() =
21852 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
21853 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
21854 }
21855 break;
21856 }
21857
21858 return true;
21859}
21860
21861bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
21862 // Get the operand value into 'Result'.
21863 if (!Visit(S: E->getSubExpr()))
21864 return false;
21865
21866 switch (E->getOpcode()) {
21867 default:
21868 return Error(E);
21869 case UO_Extension:
21870 return true;
21871 case UO_Plus:
21872 // The result is always just the subexpr.
21873 return true;
21874 case UO_Minus:
21875 if (Result.isComplexFloat()) {
21876 Result.getComplexFloatReal().changeSign();
21877 Result.getComplexFloatImag().changeSign();
21878 }
21879 else {
21880 Result.getComplexIntReal() = -Result.getComplexIntReal();
21881 Result.getComplexIntImag() = -Result.getComplexIntImag();
21882 }
21883 return true;
21884 case UO_Not:
21885 if (Result.isComplexFloat())
21886 Result.getComplexFloatImag().changeSign();
21887 else
21888 Result.getComplexIntImag() = -Result.getComplexIntImag();
21889 return true;
21890 }
21891}
21892
21893bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
21894 if (E->getNumInits() == 2) {
21895 if (E->getType()->isComplexType()) {
21896 Result.makeComplexFloat();
21897 if (!EvaluateFloat(E: E->getInit(Init: 0), Result&: Result.FloatReal, Info))
21898 return false;
21899 if (!EvaluateFloat(E: E->getInit(Init: 1), Result&: Result.FloatImag, Info))
21900 return false;
21901 } else {
21902 Result.makeComplexInt();
21903 if (!EvaluateInteger(E: E->getInit(Init: 0), Result&: Result.IntReal, Info))
21904 return false;
21905 if (!EvaluateInteger(E: E->getInit(Init: 1), Result&: Result.IntImag, Info))
21906 return false;
21907 }
21908 return true;
21909 }
21910 return ExprEvaluatorBaseTy::VisitInitListExpr(E);
21911}
21912
21913bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) {
21914 if (!IsConstantEvaluatedBuiltinCall(E))
21915 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21916
21917 switch (E->getBuiltinCallee()) {
21918 case Builtin::BI__builtin_complex:
21919 Result.makeComplexFloat();
21920 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: Result.FloatReal, Info))
21921 return false;
21922 if (!EvaluateFloat(E: E->getArg(Arg: 1), Result&: Result.FloatImag, Info))
21923 return false;
21924 return true;
21925
21926 default:
21927 return false;
21928 }
21929}
21930
21931//===----------------------------------------------------------------------===//
21932// Atomic expression evaluation, essentially just handling the NonAtomicToAtomic
21933// implicit conversion.
21934//===----------------------------------------------------------------------===//
21935
21936namespace {
21937class AtomicExprEvaluator :
21938 public ExprEvaluatorBase<AtomicExprEvaluator> {
21939 const LValue *This;
21940 APValue &Result;
21941public:
21942 AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result)
21943 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
21944
21945 bool Success(const APValue &V, const Expr *E) {
21946 Result = V;
21947 return true;
21948 }
21949
21950 bool ZeroInitialization(const Expr *E) {
21951 ImplicitValueInitExpr VIE(
21952 E->getType()->castAs<AtomicType>()->getValueType());
21953 // For atomic-qualified class (and array) types in C++, initialize the
21954 // _Atomic-wrapped subobject directly, in-place.
21955 return This ? EvaluateInPlace(Result, Info, This: *This, E: &VIE)
21956 : Evaluate(Result, Info, E: &VIE);
21957 }
21958
21959 bool VisitCastExpr(const CastExpr *E) {
21960 switch (E->getCastKind()) {
21961 default:
21962 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21963 case CK_NullToPointer:
21964 VisitIgnoredValue(E: E->getSubExpr());
21965 return ZeroInitialization(E);
21966 case CK_NonAtomicToAtomic:
21967 return This ? EvaluateInPlace(Result, Info, This: *This, E: E->getSubExpr())
21968 : Evaluate(Result, Info, E: E->getSubExpr());
21969 }
21970 }
21971};
21972} // end anonymous namespace
21973
21974static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
21975 EvalInfo &Info) {
21976 assert(!E->isValueDependent());
21977 assert(E->isPRValue() && E->getType()->isAtomicType());
21978 return AtomicExprEvaluator(Info, This, Result).Visit(S: E);
21979}
21980
21981//===----------------------------------------------------------------------===//
21982// Void expression evaluation, primarily for a cast to void on the LHS of a
21983// comma operator
21984//===----------------------------------------------------------------------===//
21985
21986namespace {
21987class VoidExprEvaluator
21988 : public ExprEvaluatorBase<VoidExprEvaluator> {
21989public:
21990 VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
21991
21992 bool Success(const APValue &V, const Expr *e) { return true; }
21993
21994 bool ZeroInitialization(const Expr *E) { return true; }
21995
21996 bool VisitCastExpr(const CastExpr *E) {
21997 switch (E->getCastKind()) {
21998 default:
21999 return ExprEvaluatorBaseTy::VisitCastExpr(E);
22000 case CK_ToVoid:
22001 VisitIgnoredValue(E: E->getSubExpr());
22002 return true;
22003 }
22004 }
22005
22006 bool VisitCallExpr(const CallExpr *E) {
22007 if (!IsConstantEvaluatedBuiltinCall(E))
22008 return ExprEvaluatorBaseTy::VisitCallExpr(E);
22009
22010 switch (E->getBuiltinCallee()) {
22011 case Builtin::BI__assume:
22012 case Builtin::BI__builtin_assume:
22013 // The argument is not evaluated!
22014 return true;
22015
22016 case Builtin::BI__builtin_operator_delete:
22017 return HandleOperatorDeleteCall(Info, E);
22018
22019 case Builtin::BIstdc_memreverse8:
22020 case Builtin::BI__builtin_stdc_memreverse8: {
22021 APSInt N;
22022 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: N, Info))
22023 return false;
22024 uint64_t NElems = N.getZExtValue();
22025
22026 LValue Ptr;
22027 if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Ptr, Info))
22028 return false;
22029
22030 if (!Ptr.checkNullPointerForFoldAccess(Info, E, AK: AK_Assign) ||
22031 Ptr.Designator.Invalid)
22032 return false;
22033
22034 QualType CharTy = Ptr.Designator.getType(Ctx&: Info.Ctx);
22035 uint64_t RemainingElems = Ptr.Designator.validIndexAdjustments().second;
22036 if (NElems > RemainingElems) {
22037 uint64_t ArrayIndex =
22038 Ptr.Designator.MostDerivedIsArrayElement
22039 ? Ptr.Designator.Entries.back().getAsArrayIndex()
22040 : (uint64_t)Ptr.Designator.IsOnePastTheEnd;
22041 APSInt Index =
22042 APSInt::getUnsigned(X: llvm::SaturatingAdd(X: ArrayIndex, Y: NElems - 1));
22043 Ptr.Designator.diagnosePointerArithmetic(Info, E, N: Index);
22044 return false;
22045 }
22046
22047 if (NElems <= 1)
22048 return true;
22049
22050 LValue Lo = Ptr;
22051 LValue Hi = Ptr;
22052 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Hi, EltTy: CharTy, Adjustment: NElems - 1))
22053 return false;
22054
22055 for (uint64_t I = 0, Half = NElems / 2; I < Half; ++I) {
22056 APValue LoVal, HiVal;
22057 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: Lo, RVal&: LoVal) ||
22058 !handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: Hi, RVal&: HiVal) ||
22059 !handleAssignment(Info, E, LVal: Lo, LValType: CharTy, Val&: HiVal) ||
22060 !handleAssignment(Info, E, LVal: Hi, LValType: CharTy, Val&: LoVal) ||
22061 !HandleLValueArrayAdjustment(Info, E, LVal&: Lo, EltTy: CharTy, Adjustment: 1) ||
22062 !HandleLValueArrayAdjustment(Info, E, LVal&: Hi, EltTy: CharTy, Adjustment: -1))
22063 return false;
22064 }
22065 return true;
22066 }
22067
22068 default:
22069 return false;
22070 }
22071 }
22072
22073 bool VisitCXXDeleteExpr(const CXXDeleteExpr *E);
22074};
22075} // end anonymous namespace
22076
22077bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
22078 // We cannot speculatively evaluate a delete expression.
22079 if (Info.SpeculativeEvaluationDepth)
22080 return false;
22081
22082 FunctionDecl *OperatorDelete = E->getOperatorDelete();
22083 if (!OperatorDelete
22084 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
22085 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
22086 << isa<CXXMethodDecl>(Val: OperatorDelete) << OperatorDelete;
22087 return false;
22088 }
22089
22090 const Expr *Arg = E->getArgument();
22091
22092 LValue Pointer;
22093 if (!EvaluatePointer(E: Arg, Result&: Pointer, Info))
22094 return false;
22095 if (Pointer.Designator.Invalid)
22096 return false;
22097
22098 // Deleting a null pointer has no effect.
22099 if (Pointer.isNullPointer()) {
22100 // This is the only case where we need to produce an extension warning:
22101 // the only other way we can succeed is if we find a dynamic allocation,
22102 // and we will have warned when we allocated it in that case.
22103 if (!Info.getLangOpts().CPlusPlus20)
22104 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
22105 return true;
22106 }
22107
22108 std::optional<DynAlloc *> Alloc = CheckDeleteKind(
22109 Info, E, Pointer,
22110 DeallocKind: E->isArrayForm() ? DynAllocKind::ArrayNew : DynAllocKind::New);
22111 if (!Alloc)
22112 return false;
22113 QualType AllocType = Pointer.Base.getDynamicAllocType();
22114
22115 // For the non-array case, the designator must be empty if the static type
22116 // does not have a virtual destructor.
22117 if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 &&
22118 !hasVirtualDestructor(T: Arg->getType()->getPointeeType())) {
22119 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_base_nonvirt_dtor)
22120 << Arg->getType()->getPointeeType() << AllocType;
22121 return false;
22122 }
22123
22124 // For a class type with a virtual destructor, the selected operator delete
22125 // is the one looked up when building the destructor.
22126 if (!E->isArrayForm() && !E->isGlobalDelete()) {
22127 const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(T: AllocType);
22128 if (VirtualDelete &&
22129 !VirtualDelete
22130 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
22131 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
22132 << isa<CXXMethodDecl>(Val: VirtualDelete) << VirtualDelete;
22133 return false;
22134 }
22135 }
22136
22137 if (!HandleDestruction(Info, Loc: E->getExprLoc(), LVBase: Pointer.getLValueBase(),
22138 Value&: (*Alloc)->Value, T: AllocType))
22139 return false;
22140
22141 if (!Info.HeapAllocs.erase(x: Pointer.Base.dyn_cast<DynamicAllocLValue>())) {
22142 // The element was already erased. This means the destructor call also
22143 // deleted the object.
22144 // FIXME: This probably results in undefined behavior before we get this
22145 // far, and should be diagnosed elsewhere first.
22146 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
22147 return false;
22148 }
22149
22150 return true;
22151}
22152
22153static bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
22154 assert(!E->isValueDependent());
22155 assert(E->isPRValue() && E->getType()->isVoidType());
22156 return VoidExprEvaluator(Info).Visit(S: E);
22157}
22158
22159//===----------------------------------------------------------------------===//
22160// Top level Expr::EvaluateAsRValue method.
22161//===----------------------------------------------------------------------===//
22162
22163static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {
22164 assert(!E->isValueDependent());
22165 // In C, function designators are not lvalues, but we evaluate them as if they
22166 // are.
22167 QualType T = E->getType();
22168 if (E->isGLValue() || T->isFunctionType()) {
22169 LValue LV;
22170 if (!EvaluateLValue(E, Result&: LV, Info))
22171 return false;
22172 LV.moveInto(V&: Result);
22173 } else if (T->isVectorType()) {
22174 if (!EvaluateVector(E, Result, Info))
22175 return false;
22176 } else if (T->isConstantMatrixType()) {
22177 if (!EvaluateMatrix(E, Result, Info))
22178 return false;
22179 } else if (T->isIntegralOrEnumerationType()) {
22180 if (!IntExprEvaluator(Info, Result).Visit(S: E))
22181 return false;
22182 } else if (T->isMetaInfoType()) {
22183 if (!EvaluateReflection(E, Result, Info))
22184 return false;
22185 } else if (T->hasPointerRepresentation()) {
22186 LValue LV;
22187 if (!EvaluatePointer(E, Result&: LV, Info))
22188 return false;
22189 LV.moveInto(V&: Result);
22190 } else if (T->isRealFloatingType()) {
22191 llvm::APFloat F(0.0);
22192 if (!EvaluateFloat(E, Result&: F, Info))
22193 return false;
22194 Result = APValue(F);
22195 } else if (T->isAnyComplexType()) {
22196 ComplexValue C;
22197 if (!EvaluateComplex(E, Result&: C, Info))
22198 return false;
22199 C.moveInto(v&: Result);
22200 } else if (T->isFixedPointType()) {
22201 if (!FixedPointExprEvaluator(Info, Result).Visit(S: E)) return false;
22202 } else if (T->isMemberPointerType()) {
22203 MemberPtr P;
22204 if (!EvaluateMemberPointer(E, Result&: P, Info))
22205 return false;
22206 P.moveInto(V&: Result);
22207 return true;
22208 } else if (T->isArrayType()) {
22209 LValue LV;
22210 APValue &Value =
22211 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
22212 if (!EvaluateArray(E, This: LV, Result&: Value, Info))
22213 return false;
22214 Result = Value;
22215 } else if (T->isRecordType()) {
22216 LValue LV;
22217 APValue &Value =
22218 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
22219 if (!EvaluateRecord(E, This: LV, Result&: Value, Info))
22220 return false;
22221 Result = Value;
22222 } else if (T->isVoidType()) {
22223 if (!Info.getLangOpts().CPlusPlus11)
22224 Info.CCEDiag(E, DiagId: diag::note_constexpr_nonliteral)
22225 << E->getType();
22226 if (!EvaluateVoid(E, Info))
22227 return false;
22228 } else if (T->isAtomicType()) {
22229 QualType Unqual = T.getAtomicUnqualifiedType();
22230 if (Unqual->isArrayType() || Unqual->isRecordType()) {
22231 LValue LV;
22232 APValue &Value = Info.CurrentCall->createTemporary(
22233 Key: E, T: Unqual, Scope: ScopeKind::FullExpression, LV);
22234 if (!EvaluateAtomic(E, This: &LV, Result&: Value, Info))
22235 return false;
22236 Result = Value;
22237 } else {
22238 if (!EvaluateAtomic(E, This: nullptr, Result, Info))
22239 return false;
22240 }
22241 } else if (Info.getLangOpts().CPlusPlus11) {
22242 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral) << E->getType();
22243 return false;
22244 } else {
22245 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
22246 return false;
22247 }
22248
22249 return true;
22250}
22251
22252/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some
22253/// cases, the in-place evaluation is essential, since later initializers for
22254/// an object can indirectly refer to subobjects which were initialized earlier.
22255static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,
22256 const Expr *E, bool AllowNonLiteralTypes) {
22257 assert(!E->isValueDependent());
22258
22259 // Normally expressions passed to EvaluateInPlace have a type, but not when
22260 // a VarDecl initializer is evaluated before the untyped ParenListExpr is
22261 // replaced with a CXXConstructExpr. This can happen in LLDB.
22262 if (E->getType().isNull())
22263 return false;
22264
22265 if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, This: &This))
22266 return false;
22267
22268 if (E->isPRValue()) {
22269 // Evaluate arrays and record types in-place, so that later initializers can
22270 // refer to earlier-initialized members of the object.
22271 QualType T = E->getType();
22272 if (T->isArrayType())
22273 return EvaluateArray(E, This, Result, Info);
22274 else if (T->isRecordType())
22275 return EvaluateRecord(E, This, Result, Info);
22276 else if (T->isAtomicType()) {
22277 QualType Unqual = T.getAtomicUnqualifiedType();
22278 if (Unqual->isArrayType() || Unqual->isRecordType())
22279 return EvaluateAtomic(E, This: &This, Result, Info);
22280 }
22281 }
22282
22283 // For any other type, in-place evaluation is unimportant.
22284 return Evaluate(Result, Info, E);
22285}
22286
22287/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
22288/// lvalue-to-rvalue cast if it is an lvalue.
22289static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
22290 assert(!E->isValueDependent());
22291 assert(!Info.Ctx.getLangOpts().EnableNewConstInterp);
22292
22293 if (E->getType().isNull())
22294 return false;
22295
22296 if (!CheckLiteralType(Info, E))
22297 return false;
22298
22299 if (!::Evaluate(Result, Info, E))
22300 return false;
22301
22302 // Implicit lvalue-to-rvalue cast.
22303 if (E->isGLValue()) {
22304 LValue LV;
22305 LV.setFrom(Ctx: Info.Ctx, V: Result);
22306 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
22307 return false;
22308 }
22309
22310 // Check this core constant expression is a constant expression.
22311 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
22312 Kind: ConstantExprKind::Normal) &&
22313 CheckMemoryLeaks(Info);
22314}
22315
22316static bool FastEvaluateAsRValue(const Expr *Exp, APValue &Result,
22317 const ASTContext &Ctx, bool &IsConst) {
22318 // Fast-path evaluations of integer literals, since we sometimes see files
22319 // containing vast quantities of these.
22320 if (const auto *L = dyn_cast<IntegerLiteral>(Val: Exp)) {
22321 Result =
22322 APValue(APSInt(L->getValue(), L->getType()->isUnsignedIntegerType()));
22323 IsConst = true;
22324 return true;
22325 }
22326
22327 if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Val: Exp)) {
22328 Result = APValue(APSInt(APInt(1, L->getValue())));
22329 IsConst = true;
22330 return true;
22331 }
22332
22333 if (const auto *FL = dyn_cast<FloatingLiteral>(Val: Exp)) {
22334 Result = APValue(FL->getValue());
22335 IsConst = true;
22336 return true;
22337 }
22338
22339 if (const auto *L = dyn_cast<CharacterLiteral>(Val: Exp)) {
22340 Result = APValue(Ctx.MakeIntValue(Value: L->getValue(), Type: L->getType()));
22341 IsConst = true;
22342 return true;
22343 }
22344
22345 if (const auto *CE = dyn_cast<ConstantExpr>(Val: Exp)) {
22346 if (CE->hasAPValueResult()) {
22347 APValue APV = CE->getAPValueResult();
22348 if (!APV.isLValue()) {
22349 Result = std::move(APV);
22350 IsConst = true;
22351 return true;
22352 }
22353 }
22354
22355 // The SubExpr is usually just an IntegerLiteral.
22356 return FastEvaluateAsRValue(Exp: CE->getSubExpr(), Result, Ctx, IsConst);
22357 }
22358
22359 // This case should be rare, but we need to check it before we check on
22360 // the type below.
22361 if (Exp->getType().isNull()) {
22362 IsConst = false;
22363 return true;
22364 }
22365
22366 return false;
22367}
22368
22369static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result,
22370 Expr::SideEffectsKind SEK) {
22371 return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) ||
22372 (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior);
22373}
22374
22375static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result,
22376 const ASTContext &Ctx, EvalInfo &Info) {
22377 assert(!E->isValueDependent());
22378 bool IsConst;
22379 if (FastEvaluateAsRValue(Exp: E, Result&: Result.Val, Ctx, IsConst))
22380 return IsConst;
22381
22382 return EvaluateAsRValue(Info, E, Result&: Result.Val);
22383}
22384
22385static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult,
22386 const ASTContext &Ctx,
22387 Expr::SideEffectsKind AllowSideEffects,
22388 EvalInfo &Info) {
22389 assert(!E->isValueDependent());
22390 if (!E->getType()->isIntegralOrEnumerationType())
22391 return false;
22392
22393 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info) ||
22394 !ExprResult.Val.isInt() ||
22395 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
22396 return false;
22397
22398 return true;
22399}
22400
22401static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult,
22402 const ASTContext &Ctx,
22403 Expr::SideEffectsKind AllowSideEffects,
22404 EvalInfo &Info) {
22405 assert(!E->isValueDependent());
22406 if (!E->getType()->isFixedPointType())
22407 return false;
22408
22409 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info))
22410 return false;
22411
22412 if (!ExprResult.Val.isFixedPoint() ||
22413 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
22414 return false;
22415
22416 return true;
22417}
22418
22419static bool doEvaluateAsRValue(const Expr *E, Expr::EvalResult &Result,
22420 const ASTContext &Ctx, SemaProxy *SProxy,
22421 bool InConstantContext) {
22422 assert(!E->isValueDependent() &&
22423 "Expression evaluator can't be called on a dependent expression.");
22424
22425 bool IsConst;
22426 if (FastEvaluateAsRValue(Exp: E, Result&: Result.Val, Ctx, IsConst))
22427 return IsConst;
22428
22429 if (Ctx.getLangOpts().EnableNewConstInterp) {
22430 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, Result,
22431 SProxy);
22432 Settings.InConstantContext = InConstantContext;
22433 return Ctx.getInterpContext().evaluateAsRValue(Settings, E, Result&: Result.Val);
22434 }
22435
22436 EvalInfo Info(Ctx, SProxy, Result, EvaluationMode::IgnoreSideEffects);
22437 Info.InConstantContext = InConstantContext;
22438 return ::EvaluateAsRValue(E, Result, Ctx, Info);
22439}
22440
22441/// EvaluateAsRValue - Return true if this is a constant which we can fold using
22442/// any crazy technique (that has nothing to do with language standards) that
22443/// we want to. If this function returns true, it returns the folded constant
22444/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
22445/// will be applied to the result.
22446bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx,
22447 bool InConstantContext) const {
22448 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsRValue");
22449 return ::doEvaluateAsRValue(E: this, Result, Ctx, /*SProxy=*/nullptr,
22450 InConstantContext);
22451}
22452
22453bool Expr::EvaluateAsMandatedConstantRValue(EvalResult &Result,
22454 const ASTContext &Ctx,
22455 SemaProxy &SProxy) const {
22456 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsMandatedConstantRValue");
22457 return ::doEvaluateAsRValue(E: this, Result, Ctx, SProxy: &SProxy,
22458 /*InConstantContext=*/true);
22459}
22460
22461bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx,
22462 bool InConstantContext) const {
22463 assert(!isValueDependent() &&
22464 "Expression evaluator can't be called on a dependent expression.");
22465 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsBooleanCondition");
22466 EvalResult Scratch;
22467 return EvaluateAsRValue(Result&: Scratch, Ctx, InConstantContext) &&
22468 HandleConversionToBool(Val: Scratch.Val, Result);
22469}
22470
22471bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx,
22472 SideEffectsKind AllowSideEffects,
22473 bool InConstantContext) const {
22474 assert(!isValueDependent() &&
22475 "Expression evaluator can't be called on a dependent expression.");
22476
22477 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsInt");
22478
22479 if (!getType()->isIntegralOrEnumerationType())
22480 return false;
22481
22482 bool IsConst;
22483 if (FastEvaluateAsRValue(Exp: this, Result&: Result.Val, Ctx, IsConst))
22484 return IsConst;
22485
22486 if (Ctx.getLangOpts().EnableNewConstInterp) {
22487 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, Result,
22488 /*SProxy=*/nullptr);
22489 Settings.InConstantContext = InConstantContext;
22490 if (!Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: Result.Val))
22491 return false;
22492
22493 if (!Result.Val.isInt() ||
22494 hasUnacceptableSideEffect(Result, SEK: AllowSideEffects))
22495 return false;
22496 return true;
22497 }
22498
22499 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Result,
22500 EvaluationMode::IgnoreSideEffects);
22501 Info.InConstantContext = InConstantContext;
22502 return ::EvaluateAsInt(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
22503}
22504
22505bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx,
22506 SideEffectsKind AllowSideEffects,
22507 bool InConstantContext) const {
22508 assert(!isValueDependent() &&
22509 "Expression evaluator can't be called on a dependent expression.");
22510 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFixedPoint");
22511
22512 if (!getType()->isFixedPointType())
22513 return false;
22514
22515 if (Ctx.getLangOpts().EnableNewConstInterp) {
22516 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, Result,
22517 /*SProxy=*/nullptr);
22518 Settings.InConstantContext = InConstantContext;
22519
22520 if (!Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: Result.Val))
22521 return false;
22522
22523 if (!Result.Val.isFixedPoint() ||
22524 hasUnacceptableSideEffect(Result, SEK: AllowSideEffects))
22525 return false;
22526
22527 return true;
22528 }
22529
22530 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Result,
22531 EvaluationMode::IgnoreSideEffects);
22532 Info.InConstantContext = InConstantContext;
22533 return ::EvaluateAsFixedPoint(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
22534}
22535
22536bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx,
22537 SideEffectsKind AllowSideEffects,
22538 bool InConstantContext) const {
22539 assert(!isValueDependent() &&
22540 "Expression evaluator can't be called on a dependent expression.");
22541
22542 if (!getType()->isRealFloatingType())
22543 return false;
22544
22545 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFloat");
22546 EvalResult ExprResult;
22547 if (!EvaluateAsRValue(Result&: ExprResult, Ctx, InConstantContext) ||
22548 !ExprResult.Val.isFloat() ||
22549 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
22550 return false;
22551
22552 Result = ExprResult.Val.getFloat();
22553 return true;
22554}
22555
22556bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx,
22557 bool InConstantContext) const {
22558 assert(!isValueDependent() &&
22559 "Expression evaluator can't be called on a dependent expression.");
22560
22561 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsLValue");
22562
22563 if (Ctx.getLangOpts().EnableNewConstInterp) {
22564 interp::EvalSettings Settings(EvaluationMode::ConstantFold, Result,
22565 /*SProxy=*/nullptr);
22566 Settings.InConstantContext = InConstantContext;
22567 return Ctx.getInterpContext().evaluate(Settings, E: this, Result&: Result.Val);
22568 }
22569
22570 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Result, EvaluationMode::ConstantFold);
22571 Info.InConstantContext = InConstantContext;
22572 LValue LV;
22573 CheckedTemporaries CheckedTemps;
22574
22575 if (!EvaluateLValue(E: this, Result&: LV, Info) || !Info.discardCleanups() ||
22576 Result.HasSideEffects ||
22577 !CheckLValueConstantExpression(Info, Loc: getExprLoc(),
22578 Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV,
22579 Kind: ConstantExprKind::Normal, CheckedTemps))
22580 return false;
22581
22582 LV.moveInto(V&: Result.Val);
22583 return true;
22584}
22585
22586static bool EvaluateDestruction(const ASTContext &Ctx, SemaProxy *SProxy,
22587 APValue::LValueBase Base,
22588 APValue DestroyedValue, QualType Type,
22589 SourceLocation Loc, Expr::EvalStatus &EStatus,
22590 bool IsConstantDestruction) {
22591 EvalInfo Info(Ctx, SProxy, EStatus,
22592 IsConstantDestruction ? EvaluationMode::ConstantExpression
22593 : EvaluationMode::ConstantFold);
22594 Info.setEvaluatingDecl(Base, Value&: DestroyedValue,
22595 EDK: EvalInfo::EvaluatingDeclKind::Dtor);
22596 Info.InConstantContext = IsConstantDestruction;
22597
22598 LValue LVal;
22599 LVal.set(B: Base);
22600
22601 if (!HandleDestruction(Info, Loc, LVBase: Base, Value&: DestroyedValue, T: Type) ||
22602 EStatus.HasSideEffects)
22603 return false;
22604
22605 if (!Info.discardCleanups())
22606 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22607
22608 return true;
22609}
22610
22611static bool doEvaluateConstantExpr(Expr::EvalResult &Result,
22612 const ASTContext &Ctx, SemaProxy *SProxy,
22613 const Expr *E, ConstantExprKind Kind) {
22614 assert(!E->isValueDependent() &&
22615 "Expression evaluator can't be called on a dependent expression.");
22616
22617 bool IsConst;
22618 if (FastEvaluateAsRValue(Exp: E, Result&: Result.Val, Ctx, IsConst) &&
22619 Result.Val.hasValue())
22620 return true;
22621
22622 EvaluationMode EM = Kind == ConstantExprKind::Initializer
22623 ? EvaluationMode::IgnoreSideEffects
22624 : EvaluationMode::ConstantExpression;
22625 if (Ctx.getLangOpts().EnableNewConstInterp) {
22626 interp::EvalSettings Settings(EM, Result, SProxy, Kind);
22627 Settings.InConstantContext = true;
22628 return Ctx.getInterpContext().evaluate(Settings, E, Result&: Result.Val) &&
22629 !Result.HasSideEffects;
22630 }
22631
22632 EvalInfo Info(Ctx, SProxy, Result, EM);
22633 Info.InConstantContext = true;
22634
22635 // The type of the object we're initializing is 'const T' for a class NTTP.
22636 QualType T = E->getType();
22637 if (Kind == ConstantExprKind::ClassTemplateArgument)
22638 T.addConst();
22639
22640 // If we're evaluating a prvalue, fake up a MaterializeTemporaryExpr to
22641 // represent the result of the evaluation. CheckConstantExpression ensures
22642 // this doesn't escape.
22643 MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr *>(E), true);
22644 APValue::LValueBase Base(&BaseMTE);
22645 Info.setEvaluatingDecl(Base, Value&: Result.Val);
22646
22647 LValue LVal;
22648 LVal.set(B: Base);
22649 // C++23 [intro.execution]/p5
22650 // A full-expression is [...] a constant-expression
22651 // So we need to make sure temporary objects are destroyed after having
22652 // evaluating the expression (per C++23 [class.temporary]/p4).
22653 FullExpressionRAII Scope(Info);
22654 if (!::EvaluateInPlace(Result&: Result.Val, Info, This: LVal, E) || Result.HasSideEffects ||
22655 !Scope.destroy())
22656 return false;
22657
22658 if (!Info.discardCleanups())
22659 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22660
22661 if (Result.HasSideEffects ||
22662 !CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: getStorageType(Ctx, E),
22663 Value: Result.Val, Kind))
22664 return false;
22665 if (!CheckMemoryLeaks(Info))
22666 return false;
22667
22668 // If this is a class template argument, it's required to have constant
22669 // destruction too.
22670 if (Kind == ConstantExprKind::ClassTemplateArgument &&
22671 (!EvaluateDestruction(Ctx, SProxy: Info.getSemaProxy(), Base, DestroyedValue: Result.Val, Type: T,
22672 Loc: E->getBeginLoc(), EStatus&: Result, IsConstantDestruction: true) ||
22673 Result.HasSideEffects)) {
22674 // FIXME: Prefix a note to indicate that the problem is lack of constant
22675 // destruction.
22676 return false;
22677 }
22678
22679 return true;
22680}
22681
22682bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
22683 ConstantExprKind Kind) const {
22684
22685 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr");
22686 return ::doEvaluateConstantExpr(Result, Ctx, /*SProxy=*/nullptr, E: this, Kind);
22687}
22688
22689bool Expr::EvaluateAsMandatedConstantExpr(EvalResult &Result,
22690 const ASTContext &Ctx,
22691 SemaProxy &SProxy,
22692 ConstantExprKind Kind) const {
22693 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsMandatedConstantExpr");
22694 return ::doEvaluateConstantExpr(Result, Ctx, SProxy: &SProxy, E: this, Kind);
22695}
22696
22697static bool doEvaluateInitializer(const ASTContext &Ctx, SemaProxy *SProxy,
22698 const VarDecl *VD, const Expr *E,
22699 Expr::EvalResult &EStatus,
22700 bool IsConstantInitialization) {
22701 assert(!E->isValueDependent() &&
22702 "Expression evaluator can't be called on a dependent expression.");
22703 assert(VD && "Need a valid VarDecl");
22704
22705 EvaluationMode EvalMode =
22706 (IsConstantInitialization &&
22707 (Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23))
22708 ? EvaluationMode::ConstantExpression
22709 : EvaluationMode::ConstantFold;
22710
22711 if (Ctx.getLangOpts().EnableNewConstInterp) {
22712 interp::EvalSettings Settings(EvalMode, EStatus, SProxy);
22713 Settings.InConstantContext = IsConstantInitialization;
22714 return Ctx.getInterpContext().evaluateAsInitializer(Settings, VD, Init: E,
22715 Result&: EStatus.Val);
22716 }
22717
22718 SourceLocation DeclLoc = VD->getLocation();
22719 QualType DeclTy = VD->getType();
22720
22721 EvalInfo Info(Ctx, SProxy, EStatus, EvalMode);
22722 Info.setEvaluatingDecl(Base: VD, Value&: EStatus.Val);
22723 Info.InConstantContext = IsConstantInitialization;
22724
22725 LValue LVal;
22726 LVal.set(B: VD);
22727
22728 {
22729 // C++23 [intro.execution]/p5
22730 // A full-expression is ... an init-declarator ([dcl.decl]) or a
22731 // mem-initializer.
22732 // So we need to make sure temporary objects are destroyed after having
22733 // evaluated the expression (per C++23 [class.temporary]/p4).
22734 //
22735 // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the
22736 // serialization code calls ParmVarDecl::getDefaultArg() which strips the
22737 // outermost FullExpr, such as ExprWithCleanups.
22738 FullExpressionRAII Scope(Info);
22739 if (!EvaluateInPlace(Result&: EStatus.Val, Info, This: LVal, E,
22740 /*AllowNonLiteralTypes=*/true) ||
22741 EStatus.HasSideEffects)
22742 return false;
22743 }
22744
22745 // At this point, any lifetime-extended temporaries are completely
22746 // initialized.
22747 Info.performLifetimeExtension();
22748
22749 if (!Info.discardCleanups())
22750 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22751
22752 return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value: EStatus.Val,
22753 Kind: ConstantExprKind::Normal) &&
22754 CheckMemoryLeaks(Info);
22755}
22756
22757bool Expr::EvaluateAsInitializer(const ASTContext &Ctx, const VarDecl *VD,
22758 Expr::EvalResult &EStatus,
22759 bool IsConstantInitialization) const {
22760 llvm::TimeTraceScope TimeScope("EvaluateAsInitializer", [&] {
22761 std::string Name;
22762 llvm::raw_string_ostream OS(Name);
22763 VD->printQualifiedName(OS);
22764 return Name;
22765 });
22766
22767 return ::doEvaluateInitializer(Ctx, /*SProxy=*/nullptr, VD, E: this, EStatus,
22768 IsConstantInitialization);
22769}
22770
22771bool Expr::EvaluateAsMandatedConstantInitializer(EvalResult &EStatus,
22772 const ASTContext &Ctx,
22773 SemaProxy &SProxy,
22774 const VarDecl *VD) const {
22775 llvm::TimeTraceScope TimeScope("EvaluateAsMandatedConstantInitializer", [&] {
22776 std::string Name;
22777 llvm::raw_string_ostream OS(Name);
22778 VD->printQualifiedName(OS);
22779 return Name;
22780 });
22781
22782 return ::doEvaluateInitializer(Ctx, SProxy: &SProxy, VD, E: this, EStatus,
22783 /*IsConstantInitialization=*/true);
22784}
22785
22786static bool doEvaluateDestruction(const VarDecl *VD, SemaProxy *SProxy,
22787 bool IsConstantDestruction,
22788 SmallVectorImpl<PartialDiagnosticAt> &Notes) {
22789 ASTContext &Ctx = VD->getASTContext();
22790
22791 // This function is only meaningful for records and arrays of records.
22792 QualType VarTy = VD->getType();
22793 if (VarTy->isArrayType()) {
22794 QualType ElemTy = Ctx.getBaseElementType(QT: VarTy);
22795 if (!ElemTy->isRecordType()) {
22796 VD->ensureEvaluatedStmt()->HasConstantDestruction = true;
22797 return true;
22798 }
22799 } else if (!VarTy->isRecordType()) {
22800 VD->ensureEvaluatedStmt()->HasConstantDestruction = true;
22801 return true;
22802 }
22803
22804 Expr::EvalStatus EStatus;
22805 EStatus.Diag = &Notes;
22806
22807 // Make a copy of the value for the destructor to mutate, if we know it.
22808 // Otherwise, treat the value as default-initialized; if the destructor works
22809 // anyway, then the destruction is constant (and must be essentially empty).
22810 APValue DestroyedValue;
22811 if (VD->getEvaluatedValue())
22812 DestroyedValue = *VD->getEvaluatedValue();
22813 else if (!handleDefaultInitValue(T: VarTy, Result&: DestroyedValue))
22814 return false;
22815
22816 if (Ctx.getLangOpts().EnableNewConstInterp) {
22817 interp::EvalSettings Settings(IsConstantDestruction
22818 ? EvaluationMode::ConstantExpression
22819 : EvaluationMode::ConstantFold,
22820 EStatus, SProxy);
22821 Settings.InConstantContext = IsConstantDestruction;
22822 if (!Ctx.getInterpContext().evaluateDestruction(Settings, VD,
22823 Value: std::move(DestroyedValue)))
22824 return false;
22825 VD->ensureEvaluatedStmt()->HasConstantDestruction = true;
22826 return true;
22827 }
22828
22829 if (!EvaluateDestruction(Ctx, SProxy, Base: VD, DestroyedValue: std::move(DestroyedValue), Type: VarTy,
22830 Loc: VD->getLocation(), EStatus, IsConstantDestruction) ||
22831 EStatus.HasSideEffects)
22832 return false;
22833
22834 VD->ensureEvaluatedStmt()->HasConstantDestruction = true;
22835 return true;
22836}
22837
22838bool VarDecl::evaluateDestruction(
22839 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
22840 // Only treat the destruction as constant destruction if we formally have
22841 // constant initialization (or are usable in a constant expression).
22842 bool IsConstantDestruction = hasConstantInitialization();
22843
22844 return ::doEvaluateDestruction(VD: this, /*SProxy=*/nullptr,
22845 IsConstantDestruction, Notes);
22846}
22847
22848bool VarDecl::evaluateConstantDestruction(
22849 SmallVectorImpl<PartialDiagnosticAt> &Notes, SemaProxy &SProxy) const {
22850 // Only treat the destruction as constant destruction if we formally have
22851 // constant initialization (or are usable in a constant expression).
22852 bool IsConstantDestruction = hasConstantInitialization();
22853
22854 return ::doEvaluateDestruction(VD: this, SProxy: &SProxy, IsConstantDestruction, Notes);
22855}
22856
22857/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
22858/// constant folded, but discard the result.
22859bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const {
22860 assert(!isValueDependent() &&
22861 "Expression evaluator can't be called on a dependent expression.");
22862
22863 EvalResult Result;
22864 return EvaluateAsRValue(Result, Ctx, /* in constant context */ InConstantContext: true) &&
22865 !hasUnacceptableSideEffect(Result, SEK);
22866}
22867
22868APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
22869 assert(!isValueDependent() &&
22870 "Expression evaluator can't be called on a dependent expression.");
22871
22872 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstInt");
22873 EvalResult EVResult;
22874
22875 bool IsConst;
22876 if (FastEvaluateAsRValue(Exp: this, Result&: EVResult.Val, Ctx, IsConst)) {
22877 assert(IsConst);
22878 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22879 return EVResult.Val.getInt();
22880 }
22881
22882 if (Ctx.getLangOpts().EnableNewConstInterp) {
22883 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, EVResult,
22884 /*SProxy=*/nullptr);
22885 Settings.InConstantContext = true;
22886 [[maybe_unused]] bool Result =
22887 Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: EVResult.Val);
22888 assert(Result && "Could not evaluate expression");
22889 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22890
22891 return EVResult.Val.getInt();
22892 }
22893
22894 EvalInfo Info(Ctx, /*SProxy=*/nullptr, EVResult,
22895 EvaluationMode::IgnoreSideEffects);
22896 Info.InConstantContext = true;
22897
22898 bool Result = ::EvaluateAsRValue(E: this, Result&: EVResult, Ctx, Info);
22899 (void)Result;
22900 assert(Result && "Could not evaluate expression");
22901 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22902
22903 return EVResult.Val.getInt();
22904}
22905
22906APSInt Expr::EvaluateKnownConstIntCheckOverflow(
22907 const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const {
22908 assert(!isValueDependent() &&
22909 "Expression evaluator can't be called on a dependent expression.");
22910
22911 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstIntCheckOverflow");
22912 EvalResult EVResult;
22913 EVResult.Diag = Diag;
22914
22915 bool IsConst;
22916 if (FastEvaluateAsRValue(Exp: this, Result&: EVResult.Val, Ctx, IsConst)) {
22917 assert(IsConst);
22918 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22919 return EVResult.Val.getInt();
22920 }
22921
22922 if (Ctx.getLangOpts().EnableNewConstInterp) {
22923 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, EVResult,
22924 /*SProxy=*/nullptr);
22925 Settings.InConstantContext = true;
22926 Settings.CheckingForUndefinedBehavior = true;
22927 [[maybe_unused]] bool Result =
22928 Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: EVResult.Val);
22929 assert(Result && "Could not evaluate expression");
22930 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22931
22932 return EVResult.Val.getInt();
22933 }
22934
22935 EvalInfo Info(Ctx, /*SProxy=*/nullptr, EVResult,
22936 EvaluationMode::IgnoreSideEffects);
22937 Info.InConstantContext = true;
22938 Info.CheckingForUndefinedBehavior = true;
22939
22940 bool Result = ::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
22941 (void)Result;
22942 assert(Result && "Could not evaluate expression");
22943 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22944
22945 return EVResult.Val.getInt();
22946}
22947
22948void Expr::EvaluateForOverflow(const ASTContext &Ctx) const {
22949 assert(!isValueDependent() &&
22950 "Expression evaluator can't be called on a dependent expression.");
22951
22952 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow");
22953 bool IsConst;
22954 EvalResult EVResult;
22955 if (FastEvaluateAsRValue(Exp: this, Result&: EVResult.Val, Ctx, IsConst))
22956 return;
22957
22958 if (Ctx.getLangOpts().EnableNewConstInterp) {
22959 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, EVResult,
22960 /*SProxy=*/nullptr);
22961 Settings.CheckingForUndefinedBehavior = true;
22962 (void)Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result&: EVResult.Val);
22963 return;
22964 }
22965
22966 EvalInfo Info(Ctx, /*SProxy=*/nullptr, EVResult,
22967 EvaluationMode::IgnoreSideEffects);
22968 Info.CheckingForUndefinedBehavior = true;
22969 (void)::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
22970}
22971
22972bool Expr::EvalResult::isGlobalLValue() const {
22973 assert(Val.isLValue());
22974 return IsGlobalLValue(B: Val.getLValueBase());
22975}
22976
22977/// isIntegerConstantExpr - this recursive routine will test if an expression is
22978/// an integer constant expression.
22979
22980/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
22981/// comma, etc
22982
22983// CheckICE - This function does the fundamental ICE checking: the returned
22984// ICEDiag contains an ICEKind indicating whether the expression is an ICE.
22985//
22986// Note that to reduce code duplication, this helper does no evaluation
22987// itself; the caller checks whether the expression is evaluatable, and
22988// in the rare cases where CheckICE actually cares about the evaluated
22989// value, it calls into Evaluate.
22990
22991namespace {
22992
22993enum ICEKind {
22994 /// This expression is an ICE.
22995 IK_ICE,
22996 /// This expression is not an ICE, but if it isn't evaluated, it's
22997 /// a legal subexpression for an ICE. This return value is used to handle
22998 /// the comma operator in C99 mode, and non-constant subexpressions.
22999 IK_ICEIfUnevaluated,
23000 /// This expression is not an ICE, and is not a legal subexpression for one.
23001 IK_NotICE
23002};
23003
23004struct ICEDiag {
23005 ICEKind Kind;
23006 SourceLocation Loc;
23007
23008 ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {}
23009};
23010
23011}
23012
23013static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); }
23014
23015static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; }
23016
23017static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) {
23018 Expr::EvalResult EVResult;
23019
23020 if (Ctx.getLangOpts().EnableNewConstInterp) {
23021 interp::EvalSettings Settings(EvaluationMode::ConstantExpression, EVResult,
23022 /*SProxy=*/nullptr);
23023 Settings.InConstantContext = true;
23024 if (!Ctx.getInterpContext().evaluateAsRValue(Settings, E, Result&: EVResult.Val) ||
23025 EVResult.HasSideEffects || !EVResult.Val.isInt())
23026 return ICEDiag(IK_NotICE, E->getBeginLoc());
23027 return NoDiag();
23028 }
23029
23030 Expr::EvalStatus Status;
23031 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Status,
23032 EvaluationMode::ConstantExpression);
23033
23034 Info.InConstantContext = true;
23035 if (!::EvaluateAsRValue(E, Result&: EVResult, Ctx, Info) || EVResult.HasSideEffects ||
23036 !EVResult.Val.isInt())
23037 return ICEDiag(IK_NotICE, E->getBeginLoc());
23038
23039 return NoDiag();
23040}
23041
23042static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) {
23043 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
23044 if (!E->getType()->isIntegralOrEnumerationType())
23045 return ICEDiag(IK_NotICE, E->getBeginLoc());
23046
23047 switch (E->getStmtClass()) {
23048#define ABSTRACT_STMT(Node)
23049#define STMT(Node, Base) case Expr::Node##Class:
23050#define EXPR(Node, Base)
23051#include "clang/AST/StmtNodes.inc"
23052 case Expr::PredefinedExprClass:
23053 case Expr::FloatingLiteralClass:
23054 case Expr::ImaginaryLiteralClass:
23055 case Expr::StringLiteralClass:
23056 case Expr::ArraySubscriptExprClass:
23057 case Expr::MatrixSingleSubscriptExprClass:
23058 case Expr::MatrixSubscriptExprClass:
23059 case Expr::ArraySectionExprClass:
23060 case Expr::OMPArrayShapingExprClass:
23061 case Expr::OMPIteratorExprClass:
23062 case Expr::CompoundAssignOperatorClass:
23063 case Expr::CompoundLiteralExprClass:
23064 case Expr::ExtVectorElementExprClass:
23065 case Expr::MatrixElementExprClass:
23066 case Expr::DesignatedInitExprClass:
23067 case Expr::ArrayInitLoopExprClass:
23068 case Expr::ArrayInitIndexExprClass:
23069 case Expr::NoInitExprClass:
23070 case Expr::DesignatedInitUpdateExprClass:
23071 case Expr::ImplicitValueInitExprClass:
23072 case Expr::ParenListExprClass:
23073 case Expr::VAArgExprClass:
23074 case Expr::AddrLabelExprClass:
23075 case Expr::StmtExprClass:
23076 case Expr::CXXMemberCallExprClass:
23077 case Expr::CUDAKernelCallExprClass:
23078 case Expr::CXXAddrspaceCastExprClass:
23079 case Expr::CXXDynamicCastExprClass:
23080 case Expr::CXXTypeidExprClass:
23081 case Expr::CXXUuidofExprClass:
23082 case Expr::MSPropertyRefExprClass:
23083 case Expr::MSPropertySubscriptExprClass:
23084 case Expr::CXXNullPtrLiteralExprClass:
23085 case Expr::UserDefinedLiteralClass:
23086 case Expr::CXXThisExprClass:
23087 case Expr::CXXThrowExprClass:
23088 case Expr::CXXNewExprClass:
23089 case Expr::CXXDeleteExprClass:
23090 case Expr::CXXPseudoDestructorExprClass:
23091 case Expr::UnresolvedLookupExprClass:
23092 case Expr::RecoveryExprClass:
23093 case Expr::DependentScopeDeclRefExprClass:
23094 case Expr::DependentTemplateIdExprClass:
23095 case Expr::CXXConstructExprClass:
23096 case Expr::CXXInheritedCtorInitExprClass:
23097 case Expr::CXXStdInitializerListExprClass:
23098 case Expr::CXXBindTemporaryExprClass:
23099 case Expr::ExprWithCleanupsClass:
23100 case Expr::CXXTemporaryObjectExprClass:
23101 case Expr::CXXUnresolvedConstructExprClass:
23102 case Expr::CXXDependentScopeMemberExprClass:
23103 case Expr::UnresolvedMemberExprClass:
23104 case Expr::ObjCStringLiteralClass:
23105 case Expr::ObjCBoxedExprClass:
23106 case Expr::ObjCArrayLiteralClass:
23107 case Expr::ObjCDictionaryLiteralClass:
23108 case Expr::ObjCEncodeExprClass:
23109 case Expr::ObjCMessageExprClass:
23110 case Expr::ObjCSelectorExprClass:
23111 case Expr::ObjCProtocolExprClass:
23112 case Expr::ObjCIvarRefExprClass:
23113 case Expr::ObjCPropertyRefExprClass:
23114 case Expr::ObjCSubscriptRefExprClass:
23115 case Expr::ObjCIsaExprClass:
23116 case Expr::ObjCAvailabilityCheckExprClass:
23117 case Expr::ShuffleVectorExprClass:
23118 case Expr::ConvertVectorExprClass:
23119 case Expr::BlockExprClass:
23120 case Expr::NoStmtClass:
23121 case Expr::OpaqueValueExprClass:
23122 case Expr::PackExpansionExprClass:
23123 case Expr::SubstNonTypeTemplateParmPackExprClass:
23124 case Expr::FunctionParmPackExprClass:
23125 case Expr::AsTypeExprClass:
23126 case Expr::ObjCIndirectCopyRestoreExprClass:
23127 case Expr::MaterializeTemporaryExprClass:
23128 case Expr::PseudoObjectExprClass:
23129 case Expr::AtomicExprClass:
23130 case Expr::LambdaExprClass:
23131 case Expr::CXXFoldExprClass:
23132 case Expr::CoawaitExprClass:
23133 case Expr::DependentCoawaitExprClass:
23134 case Expr::CoyieldExprClass:
23135 case Expr::SYCLUniqueStableNameExprClass:
23136 case Expr::CXXParenListInitExprClass:
23137 case Expr::HLSLOutArgExprClass:
23138 case Expr::CXXExpansionSelectExprClass:
23139 return ICEDiag(IK_NotICE, E->getBeginLoc());
23140
23141 case Expr::MemberExprClass: {
23142 if (Ctx.getLangOpts().C23) {
23143 const Expr *ME = E->IgnoreParenImpCasts();
23144 while (const auto *M = dyn_cast<MemberExpr>(Val: ME)) {
23145 if (M->isArrow())
23146 return ICEDiag(IK_NotICE, E->getBeginLoc());
23147 ME = M->getBase()->IgnoreParenImpCasts();
23148 }
23149 const auto *DRE = dyn_cast<DeclRefExpr>(Val: ME);
23150 if (DRE) {
23151 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl());
23152 VD && VD->isConstexpr())
23153 return CheckEvalInICE(E, Ctx);
23154 }
23155 }
23156 return ICEDiag(IK_NotICE, E->getBeginLoc());
23157 }
23158
23159 case Expr::InitListExprClass: {
23160 // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the
23161 // form "T x = { a };" is equivalent to "T x = a;".
23162 // Unless we're initializing a reference, T is a scalar as it is known to be
23163 // of integral or enumeration type.
23164 if (E->isPRValue())
23165 if (cast<InitListExpr>(Val: E)->getNumInits() == 1)
23166 return CheckICE(E: cast<InitListExpr>(Val: E)->getInit(Init: 0), Ctx);
23167 return ICEDiag(IK_NotICE, E->getBeginLoc());
23168 }
23169
23170 case Expr::SizeOfPackExprClass:
23171 case Expr::GNUNullExprClass:
23172 case Expr::SourceLocExprClass:
23173 case Expr::EmbedExprClass:
23174 case Expr::OpenACCAsteriskSizeExprClass:
23175 return NoDiag();
23176
23177 case Expr::PackIndexingExprClass:
23178 return CheckICE(E: cast<PackIndexingExpr>(Val: E)->getSelectedExpr(), Ctx);
23179
23180 case Expr::SubstNonTypeTemplateParmExprClass:
23181 return
23182 CheckICE(E: cast<SubstNonTypeTemplateParmExpr>(Val: E)->getReplacement(), Ctx);
23183
23184 case Expr::ConstantExprClass:
23185 return CheckICE(E: cast<ConstantExpr>(Val: E)->getSubExpr(), Ctx);
23186
23187 case Expr::ParenExprClass:
23188 return CheckICE(E: cast<ParenExpr>(Val: E)->getSubExpr(), Ctx);
23189 case Expr::GenericSelectionExprClass:
23190 return CheckICE(E: cast<GenericSelectionExpr>(Val: E)->getResultExpr(), Ctx);
23191 case Expr::IntegerLiteralClass:
23192 case Expr::FixedPointLiteralClass:
23193 case Expr::CharacterLiteralClass:
23194 case Expr::ObjCBoolLiteralExprClass:
23195 case Expr::CXXBoolLiteralExprClass:
23196 case Expr::CXXScalarValueInitExprClass:
23197 case Expr::TypeTraitExprClass:
23198 case Expr::ConceptSpecializationExprClass:
23199 case Expr::RequiresExprClass:
23200 case Expr::ArrayTypeTraitExprClass:
23201 case Expr::ExpressionTraitExprClass:
23202 case Expr::CXXNoexceptExprClass:
23203 case Expr::CXXReflectExprClass:
23204 return NoDiag();
23205 case Expr::CallExprClass:
23206 case Expr::CXXOperatorCallExprClass: {
23207 // C99 6.6/3 allows function calls within unevaluated subexpressions of
23208 // constant expressions, but they can never be ICEs because an ICE cannot
23209 // contain an operand of (pointer to) function type.
23210 const CallExpr *CE = cast<CallExpr>(Val: E);
23211 if (CE->getBuiltinCallee())
23212 return CheckEvalInICE(E, Ctx);
23213 return ICEDiag(IK_NotICE, E->getBeginLoc());
23214 }
23215 case Expr::CXXRewrittenBinaryOperatorClass:
23216 return CheckICE(E: cast<CXXRewrittenBinaryOperator>(Val: E)->getSemanticForm(),
23217 Ctx);
23218 case Expr::DeclRefExprClass: {
23219 const NamedDecl *D = cast<DeclRefExpr>(Val: E)->getDecl();
23220 if (isa<EnumConstantDecl>(Val: D))
23221 return NoDiag();
23222
23223 // C++ and OpenCL (FIXME: spec reference?) allow reading const-qualified
23224 // integer variables in constant expressions:
23225 //
23226 // C++ 7.1.5.1p2
23227 // A variable of non-volatile const-qualified integral or enumeration
23228 // type initialized by an ICE can be used in ICEs.
23229 //
23230 // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In
23231 // that mode, use of reference variables should not be allowed.
23232 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
23233 if (VD && VD->isUsableInConstantExpressions(C: Ctx) &&
23234 !VD->getType()->isReferenceType())
23235 return NoDiag();
23236
23237 return ICEDiag(IK_NotICE, E->getBeginLoc());
23238 }
23239 case Expr::UnaryOperatorClass: {
23240 const UnaryOperator *Exp = cast<UnaryOperator>(Val: E);
23241 switch (Exp->getOpcode()) {
23242 case UO_PostInc:
23243 case UO_PostDec:
23244 case UO_PreInc:
23245 case UO_PreDec:
23246 case UO_AddrOf:
23247 case UO_Deref:
23248 case UO_Coawait:
23249 // C99 6.6/3 allows increment and decrement within unevaluated
23250 // subexpressions of constant expressions, but they can never be ICEs
23251 // because an ICE cannot contain an lvalue operand.
23252 return ICEDiag(IK_NotICE, E->getBeginLoc());
23253 case UO_Extension:
23254 case UO_LNot:
23255 case UO_Plus:
23256 case UO_Minus:
23257 case UO_Not:
23258 case UO_Real:
23259 case UO_Imag:
23260 return CheckICE(E: Exp->getSubExpr(), Ctx);
23261 }
23262 llvm_unreachable("invalid unary operator class");
23263 }
23264 case Expr::OffsetOfExprClass: {
23265 // Note that per C99, offsetof must be an ICE. And AFAIK, using
23266 // EvaluateAsRValue matches the proposed gcc behavior for cases like
23267 // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect
23268 // compliance: we should warn earlier for offsetof expressions with
23269 // array subscripts that aren't ICEs, and if the array subscripts
23270 // are ICEs, the value of the offsetof must be an integer constant.
23271 return CheckEvalInICE(E, Ctx);
23272 }
23273 case Expr::UnaryExprOrTypeTraitExprClass: {
23274 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(Val: E);
23275 if ((Exp->getKind() == UETT_SizeOf) &&
23276 Exp->getTypeOfArgument()->isVariableArrayType())
23277 return ICEDiag(IK_NotICE, E->getBeginLoc());
23278 if (Exp->getKind() == UETT_CountOf) {
23279 QualType ArgTy = Exp->getTypeOfArgument();
23280 if (ArgTy->isVariableArrayType()) {
23281 // We need to look whether the array is multidimensional. If it is,
23282 // then we want to check the size expression manually to see whether
23283 // it is an ICE or not.
23284 const auto *VAT = Ctx.getAsVariableArrayType(T: ArgTy);
23285 if (VAT->getElementType()->isArrayType())
23286 // Variable array size expression could be missing (e.g. int a[*][10])
23287 // In that case, it can't be a constant expression.
23288 return VAT->getSizeExpr() ? CheckICE(E: VAT->getSizeExpr(), Ctx)
23289 : ICEDiag(IK_NotICE, E->getBeginLoc());
23290
23291 // Otherwise, this is a regular VLA, which is definitely not an ICE.
23292 return ICEDiag(IK_NotICE, E->getBeginLoc());
23293 }
23294 }
23295 return NoDiag();
23296 }
23297 case Expr::BinaryOperatorClass: {
23298 const BinaryOperator *Exp = cast<BinaryOperator>(Val: E);
23299 switch (Exp->getOpcode()) {
23300 case BO_PtrMemD:
23301 case BO_PtrMemI:
23302 case BO_Assign:
23303 case BO_MulAssign:
23304 case BO_DivAssign:
23305 case BO_RemAssign:
23306 case BO_AddAssign:
23307 case BO_SubAssign:
23308 case BO_ShlAssign:
23309 case BO_ShrAssign:
23310 case BO_AndAssign:
23311 case BO_XorAssign:
23312 case BO_OrAssign:
23313 // C99 6.6/3 allows assignments within unevaluated subexpressions of
23314 // constant expressions, but they can never be ICEs because an ICE cannot
23315 // contain an lvalue operand.
23316 return ICEDiag(IK_NotICE, E->getBeginLoc());
23317
23318 case BO_Mul:
23319 case BO_Div:
23320 case BO_Rem:
23321 case BO_Add:
23322 case BO_Sub:
23323 case BO_Shl:
23324 case BO_Shr:
23325 case BO_LT:
23326 case BO_GT:
23327 case BO_LE:
23328 case BO_GE:
23329 case BO_EQ:
23330 case BO_NE:
23331 case BO_And:
23332 case BO_Xor:
23333 case BO_Or:
23334 case BO_Comma:
23335 case BO_Cmp: {
23336 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
23337 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
23338 if (Exp->getOpcode() == BO_Div ||
23339 Exp->getOpcode() == BO_Rem) {
23340 // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
23341 // we don't evaluate one.
23342 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) {
23343 llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
23344 if (REval == 0)
23345 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
23346 if (REval.isSigned() && REval.isAllOnes()) {
23347 llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
23348 if (LEval.isMinSignedValue())
23349 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
23350 }
23351 }
23352 }
23353 if (Exp->getOpcode() == BO_Comma) {
23354 if (Ctx.getLangOpts().C99) {
23355 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
23356 // if it isn't evaluated.
23357 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE)
23358 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
23359 } else {
23360 // In both C89 and C++, commas in ICEs are illegal.
23361 return ICEDiag(IK_NotICE, E->getBeginLoc());
23362 }
23363 }
23364 return Worst(A: LHSResult, B: RHSResult);
23365 }
23366 case BO_LAnd:
23367 case BO_LOr: {
23368 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
23369 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
23370 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) {
23371 // Rare case where the RHS has a comma "side-effect"; we need
23372 // to actually check the condition to see whether the side
23373 // with the comma is evaluated.
23374 if ((Exp->getOpcode() == BO_LAnd) !=
23375 (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
23376 return RHSResult;
23377 return NoDiag();
23378 }
23379
23380 return Worst(A: LHSResult, B: RHSResult);
23381 }
23382 }
23383 llvm_unreachable("invalid binary operator kind");
23384 }
23385 case Expr::ImplicitCastExprClass:
23386 case Expr::CStyleCastExprClass:
23387 case Expr::CXXFunctionalCastExprClass:
23388 case Expr::CXXStaticCastExprClass:
23389 case Expr::CXXReinterpretCastExprClass:
23390 case Expr::CXXConstCastExprClass:
23391 case Expr::ObjCBridgedCastExprClass: {
23392 const Expr *SubExpr = cast<CastExpr>(Val: E)->getSubExpr();
23393 if (isa<ExplicitCastExpr>(Val: E)) {
23394 if (const FloatingLiteral *FL
23395 = dyn_cast<FloatingLiteral>(Val: SubExpr->IgnoreParenImpCasts())) {
23396 unsigned DestWidth = Ctx.getIntWidth(T: E->getType());
23397 bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
23398 APSInt IgnoredVal(DestWidth, !DestSigned);
23399 bool Ignored;
23400 // If the value does not fit in the destination type, the behavior is
23401 // undefined, so we are not required to treat it as a constant
23402 // expression.
23403 if (FL->getValue().convertToInteger(Result&: IgnoredVal,
23404 RM: llvm::APFloat::rmTowardZero,
23405 IsExact: &Ignored) & APFloat::opInvalidOp)
23406 return ICEDiag(IK_NotICE, E->getBeginLoc());
23407 return NoDiag();
23408 }
23409 }
23410 switch (cast<CastExpr>(Val: E)->getCastKind()) {
23411 case CK_LValueToRValue:
23412 case CK_AtomicToNonAtomic:
23413 case CK_NonAtomicToAtomic:
23414 case CK_NoOp:
23415 case CK_IntegralToBoolean:
23416 case CK_IntegralCast:
23417 return CheckICE(E: SubExpr, Ctx);
23418 default:
23419 return ICEDiag(IK_NotICE, E->getBeginLoc());
23420 }
23421 }
23422 case Expr::BinaryConditionalOperatorClass: {
23423 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(Val: E);
23424 ICEDiag CommonResult = CheckICE(E: Exp->getCommon(), Ctx);
23425 if (CommonResult.Kind == IK_NotICE) return CommonResult;
23426 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
23427 if (FalseResult.Kind == IK_NotICE) return FalseResult;
23428 if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult;
23429 if (FalseResult.Kind == IK_ICEIfUnevaluated &&
23430 Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag();
23431 return FalseResult;
23432 }
23433 case Expr::ConditionalOperatorClass: {
23434 const ConditionalOperator *Exp = cast<ConditionalOperator>(Val: E);
23435 // If the condition (ignoring parens) is a __builtin_constant_p call,
23436 // then only the true side is actually considered in an integer constant
23437 // expression, and it is fully evaluated. This is an important GNU
23438 // extension. See GCC PR38377 for discussion.
23439 if (const CallExpr *CallCE
23440 = dyn_cast<CallExpr>(Val: Exp->getCond()->IgnoreParenCasts()))
23441 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
23442 return CheckEvalInICE(E, Ctx);
23443 ICEDiag CondResult = CheckICE(E: Exp->getCond(), Ctx);
23444 if (CondResult.Kind == IK_NotICE)
23445 return CondResult;
23446
23447 ICEDiag TrueResult = CheckICE(E: Exp->getTrueExpr(), Ctx);
23448 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
23449
23450 if (TrueResult.Kind == IK_NotICE)
23451 return TrueResult;
23452 if (FalseResult.Kind == IK_NotICE)
23453 return FalseResult;
23454 if (CondResult.Kind == IK_ICEIfUnevaluated)
23455 return CondResult;
23456 if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE)
23457 return NoDiag();
23458 // Rare case where the diagnostics depend on which side is evaluated
23459 // Note that if we get here, CondResult is 0, and at least one of
23460 // TrueResult and FalseResult is non-zero.
23461 if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0)
23462 return FalseResult;
23463 return TrueResult;
23464 }
23465 case Expr::CXXDefaultArgExprClass:
23466 return CheckICE(E: cast<CXXDefaultArgExpr>(Val: E)->getExpr(), Ctx);
23467 case Expr::CXXDefaultInitExprClass:
23468 return CheckICE(E: cast<CXXDefaultInitExpr>(Val: E)->getExpr(), Ctx);
23469 case Expr::ChooseExprClass: {
23470 return CheckICE(E: cast<ChooseExpr>(Val: E)->getChosenSubExpr(), Ctx);
23471 }
23472 case Expr::BuiltinBitCastExprClass: {
23473 if (!checkBitCastConstexprEligibility(Info: nullptr, Ctx, BCE: cast<CastExpr>(Val: E)))
23474 return ICEDiag(IK_NotICE, E->getBeginLoc());
23475 return CheckICE(E: cast<CastExpr>(Val: E)->getSubExpr(), Ctx);
23476 }
23477 }
23478
23479 llvm_unreachable("Invalid StmtClass!");
23480}
23481
23482/// Evaluate an expression as a C++11 integral constant expression.
23483static bool
23484EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, const Expr *E,
23485 llvm::APSInt *Value,
23486 bool AllowRelaxedEval = false) {
23487 if (!E->getType()->isIntegralOrUnscopedEnumerationType())
23488 return false;
23489
23490 APValue Result;
23491 if (!E->isCXX11ConstantExpr(Ctx, Result, AllowRelaxedEval))
23492 return false;
23493
23494 if (!Result.isInt())
23495 return false;
23496
23497 if (Value) *Value = Result.getInt();
23498 return true;
23499}
23500
23501bool Expr::isIntegerConstantExpr(const ASTContext &Ctx) const {
23502 assert(!isValueDependent() &&
23503 "Expression evaluator can't be called on a dependent expression.");
23504
23505 ExprTimeTraceScope TimeScope(this, Ctx, "isIntegerConstantExpr");
23506
23507 if (Ctx.getLangOpts().CPlusPlus11)
23508 return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: nullptr);
23509
23510 ICEDiag D = CheckICE(E: this, Ctx);
23511 if (D.Kind != IK_ICE)
23512 return false;
23513 return true;
23514}
23515
23516std::optional<llvm::APSInt>
23517Expr::getIntegerConstantExpr(const ASTContext &Ctx,
23518 bool AllowRelaxedEval) const {
23519 if (isValueDependent()) {
23520 // Expression evaluator can't succeed on a dependent expression.
23521 return std::nullopt;
23522 }
23523
23524 if (Ctx.getLangOpts().CPlusPlus11) {
23525 APSInt Value;
23526 if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: &Value,
23527 AllowRelaxedEval))
23528 return Value;
23529 return std::nullopt;
23530 }
23531
23532 if (!isIntegerConstantExpr(Ctx))
23533 return std::nullopt;
23534
23535 // The only possible side-effects here are due to UB discovered in the
23536 // evaluation (for instance, INT_MAX + 1). In such a case, we are still
23537 // required to treat the expression as an ICE, so we produce the folded
23538 // value.
23539 EvalResult ExprResult;
23540
23541 if (Ctx.getLangOpts().EnableNewConstInterp) {
23542 interp::EvalSettings Settings(EvaluationMode::IgnoreSideEffects, ExprResult,
23543 /*SProxy=*/nullptr);
23544 Settings.InConstantContext = true;
23545 if (!Ctx.getInterpContext().evaluateAsRValue(Settings, E: this,
23546 Result&: ExprResult.Val))
23547 llvm_unreachable("ICE cannot be evaluated!");
23548 return ExprResult.Val.getInt();
23549 }
23550
23551 Expr::EvalStatus Status;
23552 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Status,
23553 EvaluationMode::IgnoreSideEffects);
23554 Info.InConstantContext = true;
23555
23556 if (!::EvaluateAsInt(E: this, ExprResult, Ctx, AllowSideEffects: SE_AllowSideEffects, Info))
23557 llvm_unreachable("ICE cannot be evaluated!");
23558
23559 return ExprResult.Val.getInt();
23560}
23561
23562bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const {
23563 assert(!isValueDependent() &&
23564 "Expression evaluator can't be called on a dependent expression.");
23565
23566 return CheckICE(E: this, Ctx).Kind == IK_ICE;
23567}
23568
23569bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue &Result,
23570 bool AllowRelaxedEval) const {
23571 assert(!isValueDependent() &&
23572 "Expression evaluator can't be called on a dependent expression.");
23573
23574 // We support this checking in C++98 mode in order to diagnose compatibility
23575 // issues.
23576 assert(Ctx.getLangOpts().CPlusPlus);
23577
23578 bool IsConst;
23579 if (FastEvaluateAsRValue(Exp: this, Result, Ctx, IsConst) && Result.hasValue())
23580 return true;
23581
23582 bool IsConstExpr;
23583 Expr::EvalStatus Status;
23584 SmallVector<PartialDiagnosticAt> MSRelaxedDiag;
23585 Status.ExtendedDiag = AllowRelaxedEval ? &MSRelaxedDiag : nullptr;
23586
23587 if (Ctx.getLangOpts().EnableNewConstInterp) {
23588 interp::EvalSettings Settings(EvaluationMode::ConstantExpression, Status,
23589 /*SProxy=*/nullptr);
23590 IsConstExpr =
23591 Ctx.getInterpContext().evaluateAsRValue(Settings, E: this, Result);
23592 } else {
23593 // Build evaluation settings.
23594 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Status,
23595 EvaluationMode::ConstantExpression);
23596 IsConstExpr =
23597 ::EvaluateAsRValue(Info, E: this, Result) &&
23598 // NOTE: We don't produce a diagnostic for this, but the callers that
23599 // call us on arbitrary full-expressions should generally not care.
23600 Info.discardCleanups() && !Status.HasSideEffects;
23601 }
23602 return IsConstExpr && !Status.DiagEmitted;
23603}
23604
23605bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx,
23606 const FunctionDecl *Callee,
23607 ArrayRef<const Expr*> Args,
23608 const Expr *This) const {
23609 assert(!isValueDependent() &&
23610 "Expression evaluator can't be called on a dependent expression.");
23611
23612 llvm::TimeTraceScope TimeScope("EvaluateWithSubstitution", [&] {
23613 std::string Name;
23614 llvm::raw_string_ostream OS(Name);
23615 Callee->getNameForDiagnostic(OS, Policy: Ctx.getPrintingPolicy(),
23616 /*Qualified=*/true);
23617 return Name;
23618 });
23619
23620 Expr::EvalStatus Status;
23621
23622 if (Ctx.getLangOpts().EnableNewConstInterp) {
23623 interp::EvalSettings Settings(EvaluationMode::ConstantExpressionUnevaluated,
23624 Status, /*SProxy=*/nullptr);
23625 Settings.InConstantContext = true;
23626 if (std::optional<bool> BoolResult =
23627 Ctx.getInterpContext().evaluateWithSubstitution(Settings, Callee,
23628 Args, This, Condition: this)) {
23629 Value = APValue(APSInt(APInt(1, static_cast<uint64_t>(*BoolResult))));
23630 return true;
23631 }
23632 return false;
23633 }
23634
23635 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Status,
23636 EvaluationMode::ConstantExpressionUnevaluated);
23637 Info.InConstantContext = true;
23638
23639 LValue ThisVal;
23640 const LValue *ThisPtr = nullptr;
23641 if (This) {
23642#ifndef NDEBUG
23643 auto *MD = dyn_cast<CXXMethodDecl>(Callee);
23644 assert(MD && "Don't provide `this` for non-methods.");
23645 assert(MD->isImplicitObjectMemberFunction() &&
23646 "Don't provide `this` for methods without an implicit object.");
23647#endif
23648 if (!This->isValueDependent() &&
23649 EvaluateObjectArgument(Info, Object: This, This&: ThisVal) &&
23650 !Info.EvalStatus.HasSideEffects)
23651 ThisPtr = &ThisVal;
23652
23653 // Ignore any side-effects from a failed evaluation. This is safe because
23654 // they can't interfere with any other argument evaluation.
23655 Info.EvalStatus.HasSideEffects = false;
23656 }
23657
23658 CallRef Call = Info.CurrentCall->createCall(Callee);
23659 for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
23660 I != E; ++I) {
23661 unsigned Idx = I - Args.begin();
23662 if (Idx >= Callee->getNumParams())
23663 break;
23664 const ParmVarDecl *PVD = Callee->getParamDecl(i: Idx);
23665 if ((*I)->isValueDependent() ||
23666 !EvaluateCallArg(PVD, Arg: *I, Call, Info) ||
23667 Info.EvalStatus.HasSideEffects) {
23668 // If evaluation fails, throw away the argument entirely.
23669 if (APValue *Slot = Info.getParamSlot(Call, PVD))
23670 *Slot = APValue();
23671 }
23672
23673 // Ignore any side-effects from a failed evaluation. This is safe because
23674 // they can't interfere with any other argument evaluation.
23675 Info.EvalStatus.HasSideEffects = false;
23676 }
23677
23678 // Parameter cleanups happen in the caller and are not part of this
23679 // evaluation.
23680 Info.discardCleanups();
23681 Info.EvalStatus.HasSideEffects = false;
23682
23683 // Build fake call to Callee.
23684 CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, This,
23685 Call);
23686 // FIXME: Missing ExprWithCleanups in enable_if conditions?
23687 FullExpressionRAII Scope(Info);
23688 return Evaluate(Result&: Value, Info, E: this) && Scope.destroy() &&
23689 !Info.EvalStatus.HasSideEffects;
23690}
23691
23692bool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
23693 SmallVectorImpl<
23694 PartialDiagnosticAt> &Diags) {
23695 // FIXME: It would be useful to check constexpr function templates, but at the
23696 // moment the constant expression evaluator cannot cope with the non-rigorous
23697 // ASTs which we build for dependent expressions.
23698 if (FD->isDependentContext())
23699 return true;
23700
23701 llvm::TimeTraceScope TimeScope("isPotentialConstantExpr", [&] {
23702 std::string Name;
23703 llvm::raw_string_ostream OS(Name);
23704 FD->getNameForDiagnostic(OS, Policy: FD->getASTContext().getPrintingPolicy(),
23705 /*Qualified=*/true);
23706 return Name;
23707 });
23708
23709 const ASTContext &Ctx = FD->getASTContext();
23710 Expr::EvalStatus Status;
23711 Status.Diag = &Diags;
23712
23713 // The constexpr VM attempts to compile all methods to bytecode here.
23714 if (Ctx.getLangOpts().EnableNewConstInterp) {
23715 interp::EvalSettings Settings(EvaluationMode::ConstantExpression, Status,
23716 /*SProxy=*/nullptr);
23717 Settings.InConstantContext = true;
23718 Settings.CheckingPotentialConstantExpression = true;
23719 Ctx.getInterpContext().isPotentialConstantExpr(Settings, FD);
23720 return Diags.empty();
23721 }
23722
23723 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Status,
23724 EvaluationMode::ConstantExpression);
23725 Info.InConstantContext = true;
23726 Info.CheckingPotentialConstantExpression = true;
23727
23728 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
23729 const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr;
23730
23731 // Fabricate an arbitrary expression on the stack and pretend that it
23732 // is a temporary being used as the 'this' pointer.
23733 LValue This;
23734 ImplicitValueInitExpr VIE(RD ? Info.Ctx.getCanonicalTagType(TD: RD)
23735 : Info.Ctx.IntTy);
23736 This.set(B: {&VIE, Info.CurrentCall->Index});
23737
23738 ArrayRef<const Expr*> Args;
23739
23740 APValue Scratch;
23741 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Val: FD)) {
23742 // Evaluate the call as a constant initializer, to allow the construction
23743 // of objects of non-literal types.
23744 Info.setEvaluatingDecl(Base: This.getLValueBase(), Value&: Scratch);
23745 HandleConstructorCall(E: &VIE, This, Args, Definition: CD, Info, Result&: Scratch);
23746 } else {
23747 SourceLocation Loc = FD->getLocation();
23748 HandleFunctionCall(
23749 CallLoc: Loc, Callee: FD, ObjectArg: (MD && MD->isImplicitObjectMemberFunction()) ? &This : nullptr,
23750 E: &VIE, Args, Call: CallRef(), Body: FD->getBody(), Info, Result&: Scratch,
23751 /*ResultSlot=*/nullptr);
23752 }
23753
23754 return Diags.empty();
23755}
23756
23757bool Expr::isPotentialConstantExprUnevaluated(Expr *E,
23758 const FunctionDecl *FD,
23759 SmallVectorImpl<
23760 PartialDiagnosticAt> &Diags) {
23761 assert(!E->isValueDependent() &&
23762 "Expression evaluator can't be called on a dependent expression.");
23763
23764 const ASTContext &Ctx = FD->getASTContext();
23765 Expr::EvalStatus Status;
23766 Status.Diag = &Diags;
23767
23768 if (Ctx.getLangOpts().EnableNewConstInterp) {
23769 interp::EvalSettings Settings(EvaluationMode::ConstantExpressionUnevaluated,
23770 Status, /*SProxy=*/nullptr);
23771 Settings.InConstantContext = true;
23772 Settings.CheckingPotentialConstantExpression = true;
23773 Ctx.getInterpContext().isPotentialConstantExprUnevaluated(Settings, E, FD);
23774 return Diags.empty();
23775 }
23776
23777 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Status,
23778 EvaluationMode::ConstantExpressionUnevaluated);
23779 Info.InConstantContext = true;
23780 Info.CheckingPotentialConstantExpression = true;
23781
23782 // Fabricate a call stack frame to give the arguments a plausible cover story.
23783 CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr,
23784 /*CallExpr=*/nullptr, CallRef());
23785
23786 APValue ResultScratch;
23787 Evaluate(Result&: ResultScratch, Info, E);
23788 return Diags.empty();
23789}
23790
23791std::optional<uint64_t> Expr::tryEvaluateObjectSize(const ASTContext &Ctx,
23792 unsigned Type) const {
23793 if (!getType()->isPointerType())
23794 return std::nullopt;
23795
23796 Expr::EvalStatus Status;
23797 if (Ctx.getLangOpts().EnableNewConstInterp) {
23798 interp::EvalSettings Settings(EvaluationMode::ConstantFold, Status,
23799 /*SProxy=*/nullptr);
23800 return Ctx.getInterpContext().tryEvaluateObjectSize(Settings, E: this, Kind: Type,
23801 /*IsDynamic=*/false);
23802 }
23803
23804 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Status, EvaluationMode::ConstantFold);
23805 return tryEvaluateBuiltinObjectSize(E: this, Type, Info);
23806}
23807
23808static std::optional<uint64_t>
23809EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
23810 std::string *StringResult) {
23811 if (!E->getType()->hasPointerRepresentation() || !E->isPRValue())
23812 return std::nullopt;
23813
23814 LValue String;
23815
23816 if (!EvaluatePointer(E, Result&: String, Info))
23817 return std::nullopt;
23818
23819 // Fast path: if it's a string literal, search the string value.
23820 if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>(
23821 Val: String.getLValueBase().dyn_cast<const Expr *>())) {
23822 StringRef Str = S->getBytes();
23823 int64_t Off = String.Offset.getQuantity();
23824 if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size()) {
23825 UnsignedOrNone ZeroIndex = S->findZeroCodeUnit(StartIndex: Off);
23826 if (StringResult) {
23827 if (ZeroIndex)
23828 Str = Str.substr(Start: Off, N: *ZeroIndex);
23829 *StringResult = Str;
23830 }
23831
23832 return ZeroIndex.value_or(Def: Str.size());
23833 }
23834 // For an invalid index, fall through to the offset handling below.
23835 }
23836
23837 QualType CharTy = E->getType()->getPointeeType();
23838 // Slow path: scan the bytes of the string looking for the terminating 0.
23839 for (uint64_t Strlen = 0; /**/; ++Strlen) {
23840 APValue Char;
23841 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: String, RVal&: Char) ||
23842 !Char.isInt())
23843 return std::nullopt;
23844 if (!Char.getInt())
23845 return Strlen;
23846 else if (StringResult)
23847 StringResult->push_back(c: Char.getInt().getExtValue());
23848 if (!HandleLValueArrayAdjustment(Info, E, LVal&: String, EltTy: CharTy, Adjustment: 1))
23849 return std::nullopt;
23850 }
23851}
23852
23853std::optional<std::string> Expr::tryEvaluateString(ASTContext &Ctx) const {
23854 Expr::EvalStatus Status;
23855 std::string StringResult;
23856
23857 if (Ctx.getLangOpts().EnableNewConstInterp) {
23858 interp::EvalSettings Settings(EvaluationMode::ConstantFold, Status,
23859 /*SProxy=*/nullptr);
23860 if (!Ctx.getInterpContext().evaluateString(Settings, E: this, Result&: StringResult))
23861 return std::nullopt;
23862 return StringResult;
23863 }
23864
23865 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Status, EvaluationMode::ConstantFold);
23866 if (EvaluateBuiltinStrLen(E: this, Info, StringResult: &StringResult))
23867 return StringResult;
23868 return std::nullopt;
23869}
23870
23871template <typename T>
23872static bool EvaluateCharRangeAsStringImpl(const Expr *, T &Result,
23873 const Expr *SizeExpression,
23874 const Expr *PtrExpression,
23875 ASTContext &Ctx, SemaProxy &SProxy,
23876 Expr::EvalResult &Status) {
23877 if (Ctx.getLangOpts().EnableNewConstInterp) {
23878 interp::EvalSettings Settings(EvaluationMode::ConstantExpression, Status,
23879 &SProxy);
23880 Settings.InConstantContext = true;
23881 return Ctx.getInterpContext().evaluateCharRange(Settings, SizeExpression,
23882 PtrExpression, Result);
23883 }
23884
23885 EvalInfo Info(Ctx, &SProxy, Status, EvaluationMode::ConstantExpression);
23886 Info.InConstantContext = true;
23887
23888 LValue String;
23889 FullExpressionRAII Scope(Info);
23890 APSInt SizeValue;
23891 if (!::EvaluateInteger(E: SizeExpression, Result&: SizeValue, Info))
23892 return false;
23893
23894 uint64_t Size = SizeValue.getZExtValue();
23895
23896 // FIXME: better protect against invalid or excessive sizes
23897 if constexpr (std::is_same_v<APValue, T>)
23898 Result = APValue(APValue::UninitArray{}, Size, Size);
23899 else {
23900 if (Size < Result.max_size())
23901 Result.reserve(Size);
23902 }
23903 if (!::EvaluatePointer(E: PtrExpression, Result&: String, Info))
23904 return false;
23905
23906 QualType CharTy = PtrExpression->getType()->getPointeeType();
23907 for (uint64_t I = 0; I < Size; ++I) {
23908 APValue Char;
23909 if (!handleLValueToRValueConversion(Info, Conv: PtrExpression, Type: CharTy, LVal: String,
23910 RVal&: Char))
23911 return false;
23912
23913 if constexpr (std::is_same_v<APValue, T>) {
23914 Result.getArrayInitializedElt(I) = std::move(Char);
23915 } else {
23916 APSInt C = Char.getInt();
23917
23918 assert(C.getBitWidth() <= 8 &&
23919 "string element not representable in char");
23920
23921 Result.push_back(static_cast<char>(C.getExtValue()));
23922 }
23923
23924 if (!HandleLValueArrayAdjustment(Info, E: PtrExpression, LVal&: String, EltTy: CharTy, Adjustment: 1))
23925 return false;
23926 }
23927
23928 return Scope.destroy() && CheckMemoryLeaks(Info);
23929}
23930
23931bool Expr::EvaluateCharRangeAsString(std::string &Result,
23932 const Expr *SizeExpression,
23933 const Expr *PtrExpression, ASTContext &Ctx,
23934 SemaProxy &SProxy,
23935 EvalResult &Status) const {
23936 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
23937 PtrExpression, Ctx, SProxy, Status);
23938}
23939
23940bool Expr::EvaluateCharRangeAsString(APValue &Result,
23941 const Expr *SizeExpression,
23942 const Expr *PtrExpression, ASTContext &Ctx,
23943 SemaProxy &SProxy,
23944 EvalResult &Status) const {
23945 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
23946 PtrExpression, Ctx, SProxy, Status);
23947}
23948
23949std::optional<uint64_t> Expr::tryEvaluateStrLen(const ASTContext &Ctx) const {
23950 Expr::EvalStatus Status;
23951
23952 if (Ctx.getLangOpts().EnableNewConstInterp) {
23953 interp::EvalSettings Settings(EvaluationMode::ConstantFold, Status,
23954 /*SProxy=*/nullptr);
23955 return Ctx.getInterpContext().evaluateStrlen(Settings, E: this);
23956 }
23957 EvalInfo Info(Ctx, /*SProxy=*/nullptr, Status, EvaluationMode::ConstantFold);
23958 return EvaluateBuiltinStrLen(E: this, Info);
23959}
23960
23961namespace {
23962struct IsWithinLifetimeHandler {
23963 EvalInfo &Info;
23964 static constexpr AccessKinds AccessKind = AccessKinds::AK_IsWithinLifetime;
23965 using result_type = std::optional<bool>;
23966 std::optional<bool> failed() { return std::nullopt; }
23967 template <typename T>
23968 std::optional<bool> found(T &Subobj, QualType SubobjType,
23969 APValue::LValueBase) {
23970 return true;
23971 }
23972 template <typename T>
23973 std::optional<bool> found(T &Subobj, QualType SubobjType) {
23974 return true;
23975 }
23976};
23977
23978std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &IEE,
23979 const CallExpr *E) {
23980 EvalInfo &Info = IEE.Info;
23981 // Sometimes this is called during some sorts of constant folding / early
23982 // evaluation. These are meant for non-constant expressions and are not
23983 // necessary since this consteval builtin will never be evaluated at runtime.
23984 // Just fail to evaluate when not in a constant context.
23985 if (!Info.InConstantContext)
23986 return std::nullopt;
23987 assert(E->getBuiltinCallee() == Builtin::BI__builtin_is_within_lifetime);
23988 const Expr *Arg = E->getArg(Arg: 0);
23989 if (Arg->isValueDependent())
23990 return std::nullopt;
23991 LValue Val;
23992 if (!EvaluatePointer(E: Arg, Result&: Val, Info))
23993 return std::nullopt;
23994
23995 if (Val.allowConstexprUnknown())
23996 return true;
23997
23998 auto Error = [&](int Diag) {
23999 bool CalledFromStd = false;
24000 const auto *Callee = Info.CurrentCall->getCallee();
24001 if (Callee && Callee->isInStdNamespace()) {
24002 const IdentifierInfo *Identifier = Callee->getIdentifier();
24003 CalledFromStd = Identifier && Identifier->isStr(Str: "is_within_lifetime");
24004 }
24005 Info.CCEDiag(Loc: CalledFromStd ? Info.CurrentCall->getCallRange().getBegin()
24006 : E->getExprLoc(),
24007 DiagId: diag::err_invalid_is_within_lifetime)
24008 << (CalledFromStd ? "std::is_within_lifetime"
24009 : "__builtin_is_within_lifetime")
24010 << Diag;
24011 return std::nullopt;
24012 };
24013 // C++2c [meta.const.eval]p4:
24014 // During the evaluation of an expression E as a core constant expression, a
24015 // call to this function is ill-formed unless p points to an object that is
24016 // usable in constant expressions or whose complete object's lifetime began
24017 // within E.
24018
24019 // Make sure it points to an object
24020 // nullptr does not point to an object
24021 if (Val.isNullPointer() || Val.getLValueBase().isNull())
24022 return Error(0);
24023 QualType T = Val.getLValueBase().getType();
24024 assert(!T->isFunctionType() &&
24025 "Pointers to functions should have been typed as function pointers "
24026 "which would have been rejected earlier");
24027 assert(T->isObjectType());
24028 // Hypothetical array element is not an object
24029 if (Val.getLValueDesignator().isOnePastTheEnd())
24030 return Error(1);
24031 assert(Val.getLValueDesignator().isValidSubobject() &&
24032 "Unchecked case for valid subobject");
24033 // All other ill-formed values should have failed EvaluatePointer, so the
24034 // object should be a pointer to an object that is usable in a constant
24035 // expression or whose complete lifetime began within the expression
24036 CompleteObject CO =
24037 findCompleteObject(Info, E, AK: AccessKinds::AK_IsWithinLifetime, LVal: Val, LValType: T);
24038 // The lifetime hasn't begun yet if we are still evaluating the
24039 // initializer ([basic.life]p(1.2))
24040 if (Info.EvaluatingDeclValue && CO.Value == Info.EvaluatingDeclValue)
24041 return Error(2);
24042
24043 if (!CO)
24044 return false;
24045 IsWithinLifetimeHandler handler{.Info: Info};
24046 return findSubobject(Info, E, Obj: CO, Sub: Val.getLValueDesignator(), handler);
24047}
24048} // namespace
24049